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