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