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