Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Emagic EMI 2|6 usb audio interface firmware loader. |
| 3 | * Copyright (C) 2002 |
Jan Engelhardt | 96de0e2 | 2007-10-19 23:21:04 +0200 | [diff] [blame] | 4 | * Tapio Laxström (tapio.laxstrom@iptime.fi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License, as published by |
| 8 | * the Free Software Foundation, version 2. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/errno.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/usb.h> |
Monty | 16c23f7 | 2006-05-09 12:37:22 -0700 | [diff] [blame] | 16 | #include <linux/delay.h> |
David Woodhouse | b8e24bf | 2008-05-30 17:35:47 +0300 | [diff] [blame] | 17 | #include <linux/firmware.h> |
| 18 | #include <linux/ihex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
| 20 | /* include firmware (variables)*/ |
| 21 | |
| 22 | /* FIXME: This is quick and dirty solution! */ |
| 23 | #define SPDIF /* if you want SPDIF comment next line */ |
| 24 | //#undef SPDIF /* if you want MIDI uncomment this line */ |
| 25 | |
| 26 | #ifdef SPDIF |
David Woodhouse | b8e24bf | 2008-05-30 17:35:47 +0300 | [diff] [blame] | 27 | #define FIRMWARE_FW "emi62/spdif.fw" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #else |
David Woodhouse | b8e24bf | 2008-05-30 17:35:47 +0300 | [diff] [blame] | 29 | #define FIRMWARE_FW "emi62/midi.fw" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #endif |
| 31 | |
| 32 | #define EMI62_VENDOR_ID 0x086a /* Emagic Soft-und Hardware GmBH */ |
| 33 | #define EMI62_PRODUCT_ID 0x0110 /* EMI 6|2m without firmware */ |
| 34 | |
| 35 | #define ANCHOR_LOAD_INTERNAL 0xA0 /* Vendor specific request code for Anchor Upload/Download (This one is implemented in the core) */ |
| 36 | #define ANCHOR_LOAD_EXTERNAL 0xA3 /* This command is not implemented in the core. Requires firmware */ |
| 37 | #define ANCHOR_LOAD_FPGA 0xA5 /* This command is not implemented in the core. Requires firmware. Emagic extension */ |
| 38 | #define MAX_INTERNAL_ADDRESS 0x1B3F /* This is the highest internal RAM address for the AN2131Q */ |
| 39 | #define CPUCS_REG 0x7F92 /* EZ-USB Control and Status Register. Bit 0 controls 8051 reset */ |
| 40 | #define INTERNAL_RAM(address) (address <= MAX_INTERNAL_ADDRESS) |
| 41 | |
David Woodhouse | b8e24bf | 2008-05-30 17:35:47 +0300 | [diff] [blame] | 42 | static int emi62_writememory(struct usb_device *dev, int address, |
| 43 | const unsigned char *data, int length, |
| 44 | __u8 bRequest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | static int emi62_set_reset(struct usb_device *dev, unsigned char reset_bit); |
| 46 | static int emi62_load_firmware (struct usb_device *dev); |
| 47 | static int emi62_probe(struct usb_interface *intf, const struct usb_device_id *id); |
| 48 | static void emi62_disconnect(struct usb_interface *intf); |
| 49 | static int __init emi62_init (void); |
| 50 | static void __exit emi62_exit (void); |
| 51 | |
| 52 | |
| 53 | /* thanks to drivers/usb/serial/keyspan_pda.c code */ |
David Woodhouse | b8e24bf | 2008-05-30 17:35:47 +0300 | [diff] [blame] | 54 | static int emi62_writememory(struct usb_device *dev, int address, |
| 55 | const unsigned char *data, int length, |
| 56 | __u8 request) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | { |
| 58 | int result; |
Eric Sesterhenn | 5d7efe5 | 2006-10-26 21:06:24 +0200 | [diff] [blame] | 59 | unsigned char *buffer = kmemdup(data, length, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | if (!buffer) { |
| 62 | err("emi62: kmalloc(%d) failed.", length); |
| 63 | return -ENOMEM; |
| 64 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | /* Note: usb_control_msg returns negative value on error or length of the |
| 66 | * data that was written! */ |
| 67 | result = usb_control_msg (dev, usb_sndctrlpipe(dev, 0), request, 0x40, address, 0, buffer, length, 300); |
| 68 | kfree (buffer); |
| 69 | return result; |
| 70 | } |
| 71 | |
| 72 | /* thanks to drivers/usb/serial/keyspan_pda.c code */ |
| 73 | static int emi62_set_reset (struct usb_device *dev, unsigned char reset_bit) |
| 74 | { |
| 75 | int response; |
Greg Kroah-Hartman | 1b29a37 | 2008-08-18 13:21:04 -0700 | [diff] [blame^] | 76 | dev_info(&dev->dev, "%s - %d\n", __func__, reset_bit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
| 78 | response = emi62_writememory (dev, CPUCS_REG, &reset_bit, 1, 0xa0); |
| 79 | if (response < 0) { |
| 80 | err("emi62: set_reset (%d) failed", reset_bit); |
| 81 | } |
| 82 | return response; |
| 83 | } |
| 84 | |
| 85 | #define FW_LOAD_SIZE 1023 |
| 86 | |
| 87 | static int emi62_load_firmware (struct usb_device *dev) |
| 88 | { |
David Woodhouse | b8e24bf | 2008-05-30 17:35:47 +0300 | [diff] [blame] | 89 | const struct firmware *loader_fw = NULL; |
| 90 | const struct firmware *bitstream_fw = NULL; |
| 91 | const struct firmware *firmware_fw = NULL; |
| 92 | const struct ihex_binrec *rec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | int err; |
| 94 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | __u32 addr; /* Address to write */ |
| 96 | __u8 *buf; |
| 97 | |
| 98 | dev_dbg(&dev->dev, "load_firmware\n"); |
| 99 | buf = kmalloc(FW_LOAD_SIZE, GFP_KERNEL); |
| 100 | if (!buf) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 101 | err( "%s - error loading firmware: error = %d", __func__, -ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | err = -ENOMEM; |
| 103 | goto wraperr; |
| 104 | } |
| 105 | |
David Woodhouse | b8e24bf | 2008-05-30 17:35:47 +0300 | [diff] [blame] | 106 | err = request_ihex_firmware(&loader_fw, "emi62/loader.fw", &dev->dev); |
| 107 | if (err) |
| 108 | goto nofw; |
| 109 | |
| 110 | err = request_ihex_firmware(&bitstream_fw, "emi62/bitstream.fw", |
| 111 | &dev->dev); |
| 112 | if (err) |
| 113 | goto nofw; |
| 114 | |
| 115 | err = request_ihex_firmware(&firmware_fw, FIRMWARE_FW, &dev->dev); |
| 116 | if (err) { |
| 117 | nofw: |
| 118 | err( "%s - request_firmware() failed", __func__); |
| 119 | goto wraperr; |
| 120 | } |
| 121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | /* Assert reset (stop the CPU in the EMI) */ |
| 123 | err = emi62_set_reset(dev,1); |
| 124 | if (err < 0) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 125 | err("%s - error loading firmware: error = %d", __func__, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | goto wraperr; |
| 127 | } |
| 128 | |
David Woodhouse | b8e24bf | 2008-05-30 17:35:47 +0300 | [diff] [blame] | 129 | rec = (const struct ihex_binrec *)loader_fw->data; |
| 130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | /* 1. We need to put the loader for the FPGA into the EZ-USB */ |
David Woodhouse | b8e24bf | 2008-05-30 17:35:47 +0300 | [diff] [blame] | 132 | while (rec) { |
| 133 | err = emi62_writememory(dev, be32_to_cpu(rec->addr), |
| 134 | rec->data, be16_to_cpu(rec->len), |
| 135 | ANCHOR_LOAD_INTERNAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | if (err < 0) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 137 | err("%s - error loading firmware: error = %d", __func__, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | goto wraperr; |
| 139 | } |
David Woodhouse | b8e24bf | 2008-05-30 17:35:47 +0300 | [diff] [blame] | 140 | rec = ihex_next_binrec(rec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /* De-assert reset (let the CPU run) */ |
| 144 | err = emi62_set_reset(dev,0); |
Oliver Neukum | 5919a43b | 2007-10-25 15:42:38 +0200 | [diff] [blame] | 145 | if (err < 0) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 146 | err("%s - error loading firmware: error = %d", __func__, err); |
Oliver Neukum | 5919a43b | 2007-10-25 15:42:38 +0200 | [diff] [blame] | 147 | goto wraperr; |
| 148 | } |
Monty | 16c23f7 | 2006-05-09 12:37:22 -0700 | [diff] [blame] | 149 | msleep(250); /* let device settle */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
| 151 | /* 2. We upload the FPGA firmware into the EMI |
| 152 | * Note: collect up to 1023 (yes!) bytes and send them with |
| 153 | * a single request. This is _much_ faster! */ |
David Woodhouse | b8e24bf | 2008-05-30 17:35:47 +0300 | [diff] [blame] | 154 | rec = (const struct ihex_binrec *)bitstream_fw->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | do { |
| 156 | i = 0; |
David Woodhouse | b8e24bf | 2008-05-30 17:35:47 +0300 | [diff] [blame] | 157 | addr = be32_to_cpu(rec->addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
| 159 | /* intel hex records are terminated with type 0 element */ |
David Woodhouse | b8e24bf | 2008-05-30 17:35:47 +0300 | [diff] [blame] | 160 | while (rec && (i + be16_to_cpu(rec->len) < FW_LOAD_SIZE)) { |
| 161 | memcpy(buf + i, rec->data, be16_to_cpu(rec->len)); |
| 162 | i += be16_to_cpu(rec->len); |
| 163 | rec = ihex_next_binrec(rec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |
| 165 | err = emi62_writememory(dev, addr, buf, i, ANCHOR_LOAD_FPGA); |
| 166 | if (err < 0) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 167 | err("%s - error loading firmware: error = %d", __func__, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | goto wraperr; |
| 169 | } |
| 170 | } while (i > 0); |
| 171 | |
| 172 | /* Assert reset (stop the CPU in the EMI) */ |
| 173 | err = emi62_set_reset(dev,1); |
| 174 | if (err < 0) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 175 | err("%s - error loading firmware: error = %d", __func__, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | goto wraperr; |
| 177 | } |
| 178 | |
| 179 | /* 3. We need to put the loader for the firmware into the EZ-USB (again...) */ |
David Woodhouse | b8e24bf | 2008-05-30 17:35:47 +0300 | [diff] [blame] | 180 | for (rec = (const struct ihex_binrec *)loader_fw->data; |
| 181 | rec; rec = ihex_next_binrec(rec)) { |
| 182 | err = emi62_writememory(dev, be32_to_cpu(rec->addr), |
| 183 | rec->data, be16_to_cpu(rec->len), |
| 184 | ANCHOR_LOAD_INTERNAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | if (err < 0) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 186 | err("%s - error loading firmware: error = %d", __func__, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | goto wraperr; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | /* De-assert reset (let the CPU run) */ |
| 192 | err = emi62_set_reset(dev,0); |
| 193 | if (err < 0) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 194 | err("%s - error loading firmware: error = %d", __func__, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | goto wraperr; |
| 196 | } |
Monty | 16c23f7 | 2006-05-09 12:37:22 -0700 | [diff] [blame] | 197 | msleep(250); /* let device settle */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
| 199 | /* 4. We put the part of the firmware that lies in the external RAM into the EZ-USB */ |
| 200 | |
David Woodhouse | b8e24bf | 2008-05-30 17:35:47 +0300 | [diff] [blame] | 201 | for (rec = (const struct ihex_binrec *)firmware_fw->data; |
| 202 | rec; rec = ihex_next_binrec(rec)) { |
| 203 | if (!INTERNAL_RAM(be32_to_cpu(rec->addr))) { |
| 204 | err = emi62_writememory(dev, be32_to_cpu(rec->addr), |
| 205 | rec->data, be16_to_cpu(rec->len), |
| 206 | ANCHOR_LOAD_EXTERNAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | if (err < 0) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 208 | err("%s - error loading firmware: error = %d", __func__, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | goto wraperr; |
| 210 | } |
| 211 | } |
| 212 | } |
David Woodhouse | b8e24bf | 2008-05-30 17:35:47 +0300 | [diff] [blame] | 213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | /* Assert reset (stop the CPU in the EMI) */ |
| 215 | err = emi62_set_reset(dev,1); |
| 216 | if (err < 0) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 217 | err("%s - error loading firmware: error = %d", __func__, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | goto wraperr; |
| 219 | } |
| 220 | |
David Woodhouse | b8e24bf | 2008-05-30 17:35:47 +0300 | [diff] [blame] | 221 | for (rec = (const struct ihex_binrec *)firmware_fw->data; |
| 222 | rec; rec = ihex_next_binrec(rec)) { |
| 223 | if (INTERNAL_RAM(be32_to_cpu(rec->addr))) { |
| 224 | err = emi62_writememory(dev, be32_to_cpu(rec->addr), |
| 225 | rec->data, be16_to_cpu(rec->len), |
| 226 | ANCHOR_LOAD_EXTERNAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | if (err < 0) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 228 | err("%s - error loading firmware: error = %d", __func__, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | goto wraperr; |
| 230 | } |
| 231 | } |
| 232 | } |
David Woodhouse | b8e24bf | 2008-05-30 17:35:47 +0300 | [diff] [blame] | 233 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | /* De-assert reset (let the CPU run) */ |
| 235 | err = emi62_set_reset(dev,0); |
| 236 | if (err < 0) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 237 | err("%s - error loading firmware: error = %d", __func__, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | goto wraperr; |
| 239 | } |
Monty | 16c23f7 | 2006-05-09 12:37:22 -0700 | [diff] [blame] | 240 | msleep(250); /* let device settle */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
David Woodhouse | b8e24bf | 2008-05-30 17:35:47 +0300 | [diff] [blame] | 242 | release_firmware(loader_fw); |
| 243 | release_firmware(bitstream_fw); |
| 244 | release_firmware(firmware_fw); |
| 245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | kfree(buf); |
| 247 | |
| 248 | /* return 1 to fail the driver inialization |
| 249 | * and give real driver change to load */ |
| 250 | return 1; |
| 251 | |
| 252 | wraperr: |
David Woodhouse | b8e24bf | 2008-05-30 17:35:47 +0300 | [diff] [blame] | 253 | release_firmware(loader_fw); |
| 254 | release_firmware(bitstream_fw); |
| 255 | release_firmware(firmware_fw); |
| 256 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | kfree(buf); |
| 258 | dev_err(&dev->dev, "Error\n"); |
| 259 | return err; |
| 260 | } |
| 261 | |
| 262 | static __devinitdata struct usb_device_id id_table [] = { |
| 263 | { USB_DEVICE(EMI62_VENDOR_ID, EMI62_PRODUCT_ID) }, |
| 264 | { } /* Terminating entry */ |
| 265 | }; |
| 266 | |
| 267 | MODULE_DEVICE_TABLE (usb, id_table); |
| 268 | |
| 269 | static int emi62_probe(struct usb_interface *intf, const struct usb_device_id *id) |
| 270 | { |
| 271 | struct usb_device *dev = interface_to_usbdev(intf); |
| 272 | dev_dbg(&intf->dev, "emi62_probe\n"); |
| 273 | |
Greg Kroah-Hartman | 1b29a37 | 2008-08-18 13:21:04 -0700 | [diff] [blame^] | 274 | dev_info(&intf->dev, "%s start\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | |
| 276 | emi62_load_firmware(dev); |
| 277 | |
| 278 | /* do not return the driver context, let real audio driver do that */ |
| 279 | return -EIO; |
| 280 | } |
| 281 | |
| 282 | static void emi62_disconnect(struct usb_interface *intf) |
| 283 | { |
| 284 | } |
| 285 | |
| 286 | static struct usb_driver emi62_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | .name = "emi62 - firmware loader", |
| 288 | .probe = emi62_probe, |
| 289 | .disconnect = emi62_disconnect, |
| 290 | .id_table = id_table, |
| 291 | }; |
| 292 | |
| 293 | static int __init emi62_init (void) |
| 294 | { |
| 295 | int retval; |
| 296 | retval = usb_register (&emi62_driver); |
| 297 | if (retval) |
| 298 | printk(KERN_ERR "adi-emi: registration failed\n"); |
| 299 | return retval; |
| 300 | } |
| 301 | |
| 302 | static void __exit emi62_exit (void) |
| 303 | { |
| 304 | usb_deregister (&emi62_driver); |
| 305 | } |
| 306 | |
| 307 | module_init(emi62_init); |
| 308 | module_exit(emi62_exit); |
| 309 | |
Jan Engelhardt | 96de0e2 | 2007-10-19 23:21:04 +0200 | [diff] [blame] | 310 | MODULE_AUTHOR("Tapio Laxström"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | MODULE_DESCRIPTION("Emagic EMI 6|2m firmware loader."); |
| 312 | MODULE_LICENSE("GPL"); |
| 313 | |
David Woodhouse | b8e24bf | 2008-05-30 17:35:47 +0300 | [diff] [blame] | 314 | MODULE_FIRMWARE("emi62/loader.fw"); |
| 315 | MODULE_FIRMWARE("emi62/bitstream.fw"); |
| 316 | MODULE_FIRMWARE(FIRMWARE_FW); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | /* vi:ai:syntax=c:sw=8:ts=8:tw=80 |
| 318 | */ |