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) { |
| 53 | err("emi26: kmalloc(%d) failed.", length); |
| 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; |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 67 | info("%s - %d", __func__, reset_bit); |
| 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) { |
| 71 | err("emi26: set_reset (%d) failed", reset_bit); |
| 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) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 91 | err( "%s - error loading firmware: error = %d", __func__, -ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | err = -ENOMEM; |
| 93 | goto wraperr; |
| 94 | } |
| 95 | |
David Woodhouse | ae93a55 | 2008-05-30 16:19:39 +0300 | [diff] [blame^] | 96 | err = request_ihex_firmware(&loader_fw, "emi26/loader.fw", &dev->dev); |
| 97 | if (err) |
| 98 | goto nofw; |
| 99 | |
| 100 | err = request_ihex_firmware(&bitstream_fw, "emi26/bitstream.fw", |
| 101 | &dev->dev); |
| 102 | if (err) |
| 103 | goto nofw; |
| 104 | |
| 105 | err = request_ihex_firmware(&firmware_fw, "emi26/firmware.fw", |
| 106 | &dev->dev); |
| 107 | if (err) { |
| 108 | nofw: |
| 109 | err( "%s - request_firmware() failed", __func__); |
| 110 | goto wraperr; |
| 111 | } |
| 112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | /* Assert reset (stop the CPU in the EMI) */ |
| 114 | err = emi26_set_reset(dev,1); |
| 115 | if (err < 0) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 116 | err( "%s - error loading firmware: error = %d", __func__, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | goto wraperr; |
| 118 | } |
| 119 | |
David Woodhouse | ae93a55 | 2008-05-30 16:19:39 +0300 | [diff] [blame^] | 120 | rec = (const struct ihex_binrec *)loader_fw->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | /* 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^] | 122 | while (rec) { |
| 123 | err = emi26_writememory(dev, be32_to_cpu(rec->addr), |
| 124 | rec->data, be16_to_cpu(rec->len), |
| 125 | ANCHOR_LOAD_INTERNAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | if (err < 0) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 127 | err("%s - error loading firmware: error = %d", __func__, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | goto wraperr; |
| 129 | } |
David Woodhouse | ae93a55 | 2008-05-30 16:19:39 +0300 | [diff] [blame^] | 130 | rec = ihex_next_binrec(rec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /* De-assert reset (let the CPU run) */ |
| 134 | err = emi26_set_reset(dev,0); |
Oliver Neukum | cf4cf0b | 2007-10-25 15:38:44 +0200 | [diff] [blame] | 135 | if (err < 0) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 136 | err("%s - error loading firmware: error = %d", __func__, err); |
Oliver Neukum | cf4cf0b | 2007-10-25 15:38:44 +0200 | [diff] [blame] | 137 | goto wraperr; |
| 138 | } |
Monty | 16c23f7 | 2006-05-09 12:37:22 -0700 | [diff] [blame] | 139 | msleep(250); /* let device settle */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
| 141 | /* 2. We upload the FPGA firmware into the EMI |
| 142 | * Note: collect up to 1023 (yes!) bytes and send them with |
| 143 | * a single request. This is _much_ faster! */ |
David Woodhouse | ae93a55 | 2008-05-30 16:19:39 +0300 | [diff] [blame^] | 144 | rec = (const struct ihex_binrec *)bitstream_fw->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | do { |
| 146 | i = 0; |
David Woodhouse | ae93a55 | 2008-05-30 16:19:39 +0300 | [diff] [blame^] | 147 | addr = be32_to_cpu(rec->addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
| 149 | /* intel hex records are terminated with type 0 element */ |
David Woodhouse | ae93a55 | 2008-05-30 16:19:39 +0300 | [diff] [blame^] | 150 | while (rec && (i + be16_to_cpu(rec->len) < FW_LOAD_SIZE)) { |
| 151 | memcpy(buf + i, rec->data, be16_to_cpu(rec->len)); |
| 152 | i += be16_to_cpu(rec->len); |
| 153 | rec = ihex_next_binrec(rec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | } |
| 155 | err = emi26_writememory(dev, addr, buf, i, ANCHOR_LOAD_FPGA); |
| 156 | if (err < 0) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 157 | err("%s - error loading firmware: error = %d", __func__, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | goto wraperr; |
| 159 | } |
| 160 | } while (i > 0); |
| 161 | |
| 162 | /* Assert reset (stop the CPU in the EMI) */ |
| 163 | err = emi26_set_reset(dev,1); |
| 164 | if (err < 0) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 165 | err("%s - error loading firmware: error = %d", __func__, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | goto wraperr; |
| 167 | } |
| 168 | |
| 169 | /* 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^] | 170 | for (rec = (const struct ihex_binrec *)loader_fw->data; |
| 171 | rec; rec = ihex_next_binrec(rec)) { |
| 172 | err = emi26_writememory(dev, be32_to_cpu(rec->addr), |
| 173 | rec->data, be16_to_cpu(rec->len), |
| 174 | ANCHOR_LOAD_INTERNAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | if (err < 0) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 176 | err("%s - error loading firmware: error = %d", __func__, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | goto wraperr; |
| 178 | } |
| 179 | } |
Monty | 16c23f7 | 2006-05-09 12:37:22 -0700 | [diff] [blame] | 180 | msleep(250); /* let device settle */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
| 182 | /* De-assert reset (let the CPU run) */ |
| 183 | err = emi26_set_reset(dev,0); |
| 184 | if (err < 0) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 185 | err("%s - error loading firmware: error = %d", __func__, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | goto wraperr; |
| 187 | } |
| 188 | |
| 189 | /* 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^] | 190 | |
| 191 | for (rec = (const struct ihex_binrec *)firmware_fw->data; |
| 192 | rec; rec = ihex_next_binrec(rec)) { |
| 193 | if (!INTERNAL_RAM(be32_to_cpu(rec->addr))) { |
| 194 | err = emi26_writememory(dev, be32_to_cpu(rec->addr), |
| 195 | rec->data, be16_to_cpu(rec->len), |
| 196 | ANCHOR_LOAD_EXTERNAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | if (err < 0) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 198 | err("%s - error loading firmware: error = %d", __func__, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | goto wraperr; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /* Assert reset (stop the CPU in the EMI) */ |
| 205 | err = emi26_set_reset(dev,1); |
| 206 | if (err < 0) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 207 | err("%s - error loading firmware: error = %d", __func__, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | goto wraperr; |
| 209 | } |
| 210 | |
David Woodhouse | ae93a55 | 2008-05-30 16:19:39 +0300 | [diff] [blame^] | 211 | for (rec = (const struct ihex_binrec *)firmware_fw->data; |
| 212 | rec; rec = ihex_next_binrec(rec)) { |
| 213 | if (INTERNAL_RAM(be32_to_cpu(rec->addr))) { |
| 214 | err = emi26_writememory(dev, be32_to_cpu(rec->addr), |
| 215 | rec->data, be16_to_cpu(rec->len), |
| 216 | ANCHOR_LOAD_INTERNAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | if (err < 0) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 218 | err("%s - error loading firmware: error = %d", __func__, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | goto wraperr; |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /* De-assert reset (let the CPU run) */ |
| 225 | err = emi26_set_reset(dev,0); |
| 226 | if (err < 0) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 227 | err("%s - error loading firmware: error = %d", __func__, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | goto wraperr; |
| 229 | } |
Monty | 16c23f7 | 2006-05-09 12:37:22 -0700 | [diff] [blame] | 230 | msleep(250); /* let device settle */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
| 232 | /* return 1 to fail the driver inialization |
| 233 | * and give real driver change to load */ |
| 234 | err = 1; |
| 235 | |
| 236 | wraperr: |
David Woodhouse | ae93a55 | 2008-05-30 16:19:39 +0300 | [diff] [blame^] | 237 | release_firmware(loader_fw); |
| 238 | release_firmware(bitstream_fw); |
| 239 | release_firmware(firmware_fw); |
| 240 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | kfree(buf); |
| 242 | return err; |
| 243 | } |
| 244 | |
| 245 | static struct usb_device_id id_table [] = { |
| 246 | { USB_DEVICE(EMI26_VENDOR_ID, EMI26_PRODUCT_ID) }, |
| 247 | { USB_DEVICE(EMI26_VENDOR_ID, EMI26B_PRODUCT_ID) }, |
| 248 | { } /* Terminating entry */ |
| 249 | }; |
| 250 | |
| 251 | MODULE_DEVICE_TABLE (usb, id_table); |
| 252 | |
| 253 | static int emi26_probe(struct usb_interface *intf, const struct usb_device_id *id) |
| 254 | { |
| 255 | struct usb_device *dev = interface_to_usbdev(intf); |
| 256 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 257 | info("%s start", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
| 259 | emi26_load_firmware(dev); |
| 260 | |
| 261 | /* do not return the driver context, let real audio driver do that */ |
| 262 | return -EIO; |
| 263 | } |
| 264 | |
| 265 | static void emi26_disconnect(struct usb_interface *intf) |
| 266 | { |
| 267 | } |
| 268 | |
| 269 | static struct usb_driver emi26_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | .name = "emi26 - firmware loader", |
| 271 | .probe = emi26_probe, |
| 272 | .disconnect = emi26_disconnect, |
| 273 | .id_table = id_table, |
| 274 | }; |
| 275 | |
| 276 | static int __init emi26_init (void) |
| 277 | { |
| 278 | return usb_register(&emi26_driver); |
| 279 | } |
| 280 | |
| 281 | static void __exit emi26_exit (void) |
| 282 | { |
| 283 | usb_deregister (&emi26_driver); |
| 284 | } |
| 285 | |
| 286 | module_init(emi26_init); |
| 287 | module_exit(emi26_exit); |
| 288 | |
Jan Engelhardt | 96de0e2 | 2007-10-19 23:21:04 +0200 | [diff] [blame] | 289 | MODULE_AUTHOR("Tapio Laxström"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | MODULE_DESCRIPTION("Emagic EMI 2|6 firmware loader."); |
| 291 | MODULE_LICENSE("GPL"); |
| 292 | |
David Woodhouse | ae93a55 | 2008-05-30 16:19:39 +0300 | [diff] [blame^] | 293 | MODULE_FIRMWARE("emi26/loader.fw"); |
| 294 | MODULE_FIRMWARE("emi26/bitstream.fw"); |
| 295 | MODULE_FIRMWARE("emi26/firmware.fw"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | /* vi:ai:syntax=c:sw=8:ts=8:tw=80 |
| 297 | */ |