2 * Emagic EMI 2|6 usb audio interface firmware loader.
4 * Tapio Laxström (tapio.laxstrom@iptime.fi)
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.
10 * $Id: emi62.c,v 1.15 2002/04/23 06:13:59 tapio Exp $
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/slab.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/usb.h>
18 #include <linux/delay.h>
19 #include <linux/firmware.h>
20 #include <linux/ihex.h>
22 /* include firmware (variables)*/
24 /* FIXME: This is quick and dirty solution! */
25 #define SPDIF /* if you want SPDIF comment next line */
26 //#undef SPDIF /* if you want MIDI uncomment this line */
29 #define FIRMWARE_FW "emi62/spdif.fw"
31 #define FIRMWARE_FW "emi62/midi.fw"
34 #define EMI62_VENDOR_ID 0x086a /* Emagic Soft-und Hardware GmBH */
35 #define EMI62_PRODUCT_ID 0x0110 /* EMI 6|2m without firmware */
37 #define ANCHOR_LOAD_INTERNAL 0xA0 /* Vendor specific request code for Anchor Upload/Download (This one is implemented in the core) */
38 #define ANCHOR_LOAD_EXTERNAL 0xA3 /* This command is not implemented in the core. Requires firmware */
39 #define ANCHOR_LOAD_FPGA 0xA5 /* This command is not implemented in the core. Requires firmware. Emagic extension */
40 #define MAX_INTERNAL_ADDRESS 0x1B3F /* This is the highest internal RAM address for the AN2131Q */
41 #define CPUCS_REG 0x7F92 /* EZ-USB Control and Status Register. Bit 0 controls 8051 reset */
42 #define INTERNAL_RAM(address) (address <= MAX_INTERNAL_ADDRESS)
44 static int emi62_writememory(struct usb_device *dev, int address,
45 const unsigned char *data, int length,
47 static int emi62_set_reset(struct usb_device *dev, unsigned char reset_bit);
48 static int emi62_load_firmware (struct usb_device *dev);
49 static int emi62_probe(struct usb_interface *intf, const struct usb_device_id *id);
50 static void emi62_disconnect(struct usb_interface *intf);
51 static int __init emi62_init (void);
52 static void __exit emi62_exit (void);
55 /* thanks to drivers/usb/serial/keyspan_pda.c code */
56 static int emi62_writememory(struct usb_device *dev, int address,
57 const unsigned char *data, int length,
61 unsigned char *buffer = kmemdup(data, length, GFP_KERNEL);
64 err("emi62: kmalloc(%d) failed.", length);
67 /* Note: usb_control_msg returns negative value on error or length of the
68 * data that was written! */
69 result = usb_control_msg (dev, usb_sndctrlpipe(dev, 0), request, 0x40, address, 0, buffer, length, 300);
74 /* thanks to drivers/usb/serial/keyspan_pda.c code */
75 static int emi62_set_reset (struct usb_device *dev, unsigned char reset_bit)
78 info("%s - %d", __func__, reset_bit);
80 response = emi62_writememory (dev, CPUCS_REG, &reset_bit, 1, 0xa0);
82 err("emi62: set_reset (%d) failed", reset_bit);
87 #define FW_LOAD_SIZE 1023
89 static int emi62_load_firmware (struct usb_device *dev)
91 const struct firmware *loader_fw = NULL;
92 const struct firmware *bitstream_fw = NULL;
93 const struct firmware *firmware_fw = NULL;
94 const struct ihex_binrec *rec;
97 __u32 addr; /* Address to write */
100 dev_dbg(&dev->dev, "load_firmware\n");
101 buf = kmalloc(FW_LOAD_SIZE, GFP_KERNEL);
103 err( "%s - error loading firmware: error = %d", __func__, -ENOMEM);
108 err = request_ihex_firmware(&loader_fw, "emi62/loader.fw", &dev->dev);
112 err = request_ihex_firmware(&bitstream_fw, "emi62/bitstream.fw",
117 err = request_ihex_firmware(&firmware_fw, FIRMWARE_FW, &dev->dev);
120 err( "%s - request_firmware() failed", __func__);
124 /* Assert reset (stop the CPU in the EMI) */
125 err = emi62_set_reset(dev,1);
127 err("%s - error loading firmware: error = %d", __func__, err);
131 rec = (const struct ihex_binrec *)loader_fw->data;
133 /* 1. We need to put the loader for the FPGA into the EZ-USB */
135 err = emi62_writememory(dev, be32_to_cpu(rec->addr),
136 rec->data, be16_to_cpu(rec->len),
137 ANCHOR_LOAD_INTERNAL);
139 err("%s - error loading firmware: error = %d", __func__, err);
142 rec = ihex_next_binrec(rec);
145 /* De-assert reset (let the CPU run) */
146 err = emi62_set_reset(dev,0);
148 err("%s - error loading firmware: error = %d", __func__, err);
151 msleep(250); /* let device settle */
153 /* 2. We upload the FPGA firmware into the EMI
154 * Note: collect up to 1023 (yes!) bytes and send them with
155 * a single request. This is _much_ faster! */
156 rec = (const struct ihex_binrec *)bitstream_fw->data;
159 addr = be32_to_cpu(rec->addr);
161 /* intel hex records are terminated with type 0 element */
162 while (rec && (i + be16_to_cpu(rec->len) < FW_LOAD_SIZE)) {
163 memcpy(buf + i, rec->data, be16_to_cpu(rec->len));
164 i += be16_to_cpu(rec->len);
165 rec = ihex_next_binrec(rec);
167 err = emi62_writememory(dev, addr, buf, i, ANCHOR_LOAD_FPGA);
169 err("%s - error loading firmware: error = %d", __func__, err);
174 /* Assert reset (stop the CPU in the EMI) */
175 err = emi62_set_reset(dev,1);
177 err("%s - error loading firmware: error = %d", __func__, err);
181 /* 3. We need to put the loader for the firmware into the EZ-USB (again...) */
182 for (rec = (const struct ihex_binrec *)loader_fw->data;
183 rec; rec = ihex_next_binrec(rec)) {
184 err = emi62_writememory(dev, be32_to_cpu(rec->addr),
185 rec->data, be16_to_cpu(rec->len),
186 ANCHOR_LOAD_INTERNAL);
188 err("%s - error loading firmware: error = %d", __func__, err);
193 /* De-assert reset (let the CPU run) */
194 err = emi62_set_reset(dev,0);
196 err("%s - error loading firmware: error = %d", __func__, err);
199 msleep(250); /* let device settle */
201 /* 4. We put the part of the firmware that lies in the external RAM into the EZ-USB */
203 for (rec = (const struct ihex_binrec *)firmware_fw->data;
204 rec; rec = ihex_next_binrec(rec)) {
205 if (!INTERNAL_RAM(be32_to_cpu(rec->addr))) {
206 err = emi62_writememory(dev, be32_to_cpu(rec->addr),
207 rec->data, be16_to_cpu(rec->len),
208 ANCHOR_LOAD_EXTERNAL);
210 err("%s - error loading firmware: error = %d", __func__, err);
216 /* Assert reset (stop the CPU in the EMI) */
217 err = emi62_set_reset(dev,1);
219 err("%s - error loading firmware: error = %d", __func__, err);
223 for (rec = (const struct ihex_binrec *)firmware_fw->data;
224 rec; rec = ihex_next_binrec(rec)) {
225 if (INTERNAL_RAM(be32_to_cpu(rec->addr))) {
226 err = emi62_writememory(dev, be32_to_cpu(rec->addr),
227 rec->data, be16_to_cpu(rec->len),
228 ANCHOR_LOAD_EXTERNAL);
230 err("%s - error loading firmware: error = %d", __func__, err);
236 /* De-assert reset (let the CPU run) */
237 err = emi62_set_reset(dev,0);
239 err("%s - error loading firmware: error = %d", __func__, err);
242 msleep(250); /* let device settle */
244 release_firmware(loader_fw);
245 release_firmware(bitstream_fw);
246 release_firmware(firmware_fw);
250 /* return 1 to fail the driver inialization
251 * and give real driver change to load */
255 release_firmware(loader_fw);
256 release_firmware(bitstream_fw);
257 release_firmware(firmware_fw);
260 dev_err(&dev->dev, "Error\n");
264 static __devinitdata struct usb_device_id id_table [] = {
265 { USB_DEVICE(EMI62_VENDOR_ID, EMI62_PRODUCT_ID) },
266 { } /* Terminating entry */
269 MODULE_DEVICE_TABLE (usb, id_table);
271 static int emi62_probe(struct usb_interface *intf, const struct usb_device_id *id)
273 struct usb_device *dev = interface_to_usbdev(intf);
274 dev_dbg(&intf->dev, "emi62_probe\n");
276 info("%s start", __func__);
278 emi62_load_firmware(dev);
280 /* do not return the driver context, let real audio driver do that */
284 static void emi62_disconnect(struct usb_interface *intf)
288 static struct usb_driver emi62_driver = {
289 .name = "emi62 - firmware loader",
290 .probe = emi62_probe,
291 .disconnect = emi62_disconnect,
292 .id_table = id_table,
295 static int __init emi62_init (void)
298 retval = usb_register (&emi62_driver);
300 printk(KERN_ERR "adi-emi: registration failed\n");
304 static void __exit emi62_exit (void)
306 usb_deregister (&emi62_driver);
309 module_init(emi62_init);
310 module_exit(emi62_exit);
312 MODULE_AUTHOR("Tapio Laxström");
313 MODULE_DESCRIPTION("Emagic EMI 6|2m firmware loader.");
314 MODULE_LICENSE("GPL");
316 MODULE_FIRMWARE("emi62/loader.fw");
317 MODULE_FIRMWARE("emi62/bitstream.fw");
318 MODULE_FIRMWARE(FIRMWARE_FW);
319 /* vi:ai:syntax=c:sw=8:ts=8:tw=80