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 * emi26.c,v 1.13 2002/03/08 13:10:26 tapio Exp
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>
19 #define MAX_INTEL_HEX_RECORD_LENGTH 16
20 typedef struct _INTEL_HEX_RECORD
25 __u8 data[MAX_INTEL_HEX_RECORD_LENGTH];
26 } INTEL_HEX_RECORD, *PINTEL_HEX_RECORD;
28 /* include firmware (variables) */
31 #define EMI26_VENDOR_ID 0x086a /* Emagic Soft-und Hardware GmBH */
32 #define EMI26_PRODUCT_ID 0x0100 /* EMI 2|6 without firmware */
33 #define EMI26B_PRODUCT_ID 0x0102 /* EMI 2|6 without firmware */
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)
42 static int emi26_writememory( struct usb_device *dev, int address, unsigned char *data, int length, __u8 bRequest);
43 static int emi26_set_reset(struct usb_device *dev, unsigned char reset_bit);
44 static int emi26_load_firmware (struct usb_device *dev);
45 static int emi26_probe(struct usb_interface *intf, const struct usb_device_id *id);
46 static void emi26_disconnect(struct usb_interface *intf);
47 static int __init emi26_init (void);
48 static void __exit emi26_exit (void);
51 /* thanks to drivers/usb/serial/keyspan_pda.c code */
52 static int emi26_writememory (struct usb_device *dev, int address, unsigned char *data, int length, __u8 request)
55 unsigned char *buffer = kmalloc (length, GFP_KERNEL);
58 err("emi26: kmalloc(%d) failed.", length);
61 memcpy (buffer, data, length);
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);
69 /* thanks to drivers/usb/serial/keyspan_pda.c code */
70 static int emi26_set_reset (struct usb_device *dev, unsigned char reset_bit)
73 info("%s - %d", __FUNCTION__, reset_bit);
74 /* printk(KERN_DEBUG "%s - %d", __FUNCTION__, reset_bit); */
75 response = emi26_writememory (dev, CPUCS_REG, &reset_bit, 1, 0xa0);
77 err("emi26: set_reset (%d) failed", reset_bit);
82 #define FW_LOAD_SIZE 1023
84 static int emi26_load_firmware (struct usb_device *dev)
88 int pos = 0; /* Position in hex record */
89 __u32 addr; /* Address to write */
92 buf = kmalloc(FW_LOAD_SIZE, GFP_KERNEL);
94 err( "%s - error loading firmware: error = %d", __FUNCTION__, -ENOMEM);
99 /* Assert reset (stop the CPU in the EMI) */
100 err = emi26_set_reset(dev,1);
102 err( "%s - error loading firmware: error = %d", __FUNCTION__, err);
106 /* 1. We need to put the loader for the FPGA into the EZ-USB */
107 for (i=0; g_Loader[i].type == 0; i++) {
108 err = emi26_writememory(dev, g_Loader[i].address, g_Loader[i].data, g_Loader[i].length, ANCHOR_LOAD_INTERNAL);
110 err("%s - error loading firmware: error = %d", __FUNCTION__, err);
115 /* De-assert reset (let the CPU run) */
116 err = emi26_set_reset(dev,0);
118 /* 2. We upload the FPGA firmware into the EMI
119 * Note: collect up to 1023 (yes!) bytes and send them with
120 * a single request. This is _much_ faster! */
123 addr = g_bitstream[pos].address;
125 /* intel hex records are terminated with type 0 element */
126 while ((g_bitstream[pos].type == 0) && (i + g_bitstream[pos].length < FW_LOAD_SIZE)) {
127 memcpy(buf + i, g_bitstream[pos].data, g_bitstream[pos].length);
128 i += g_bitstream[pos].length;
131 err = emi26_writememory(dev, addr, buf, i, ANCHOR_LOAD_FPGA);
133 err("%s - error loading firmware: error = %d", __FUNCTION__, err);
138 /* Assert reset (stop the CPU in the EMI) */
139 err = emi26_set_reset(dev,1);
141 err("%s - error loading firmware: error = %d", __FUNCTION__, err);
145 /* 3. We need to put the loader for the firmware into the EZ-USB (again...) */
146 for (i=0; g_Loader[i].type == 0; i++) {
147 err = emi26_writememory(dev, g_Loader[i].address, g_Loader[i].data, g_Loader[i].length, ANCHOR_LOAD_INTERNAL);
149 err("%s - error loading firmware: error = %d", __FUNCTION__, err);
154 /* De-assert reset (let the CPU run) */
155 err = emi26_set_reset(dev,0);
157 err("%s - error loading firmware: error = %d", __FUNCTION__, err);
161 /* 4. We put the part of the firmware that lies in the external RAM into the EZ-USB */
162 for (i=0; g_Firmware[i].type == 0; i++) {
163 if (!INTERNAL_RAM(g_Firmware[i].address)) {
164 err = emi26_writememory(dev, g_Firmware[i].address, g_Firmware[i].data, g_Firmware[i].length, ANCHOR_LOAD_EXTERNAL);
166 err("%s - error loading firmware: error = %d", __FUNCTION__, err);
172 /* Assert reset (stop the CPU in the EMI) */
173 err = emi26_set_reset(dev,1);
175 err("%s - error loading firmware: error = %d", __FUNCTION__, err);
179 for (i=0; g_Firmware[i].type == 0; i++) {
180 if (INTERNAL_RAM(g_Firmware[i].address)) {
181 err = emi26_writememory(dev, g_Firmware[i].address, g_Firmware[i].data, g_Firmware[i].length, ANCHOR_LOAD_INTERNAL);
183 err("%s - error loading firmware: error = %d", __FUNCTION__, err);
189 /* De-assert reset (let the CPU run) */
190 err = emi26_set_reset(dev,0);
192 err("%s - error loading firmware: error = %d", __FUNCTION__, err);
196 /* return 1 to fail the driver inialization
197 * and give real driver change to load */
205 static struct usb_device_id id_table [] = {
206 { USB_DEVICE(EMI26_VENDOR_ID, EMI26_PRODUCT_ID) },
207 { USB_DEVICE(EMI26_VENDOR_ID, EMI26B_PRODUCT_ID) },
208 { } /* Terminating entry */
211 MODULE_DEVICE_TABLE (usb, id_table);
213 static int emi26_probe(struct usb_interface *intf, const struct usb_device_id *id)
215 struct usb_device *dev = interface_to_usbdev(intf);
217 info("%s start", __FUNCTION__);
219 emi26_load_firmware(dev);
221 /* do not return the driver context, let real audio driver do that */
225 static void emi26_disconnect(struct usb_interface *intf)
229 static struct usb_driver emi26_driver = {
230 .name = "emi26 - firmware loader",
231 .probe = emi26_probe,
232 .disconnect = emi26_disconnect,
233 .id_table = id_table,
236 static int __init emi26_init (void)
238 return usb_register(&emi26_driver);
241 static void __exit emi26_exit (void)
243 usb_deregister (&emi26_driver);
246 module_init(emi26_init);
247 module_exit(emi26_exit);
249 MODULE_AUTHOR("tapio laxström");
250 MODULE_DESCRIPTION("Emagic EMI 2|6 firmware loader.");
251 MODULE_LICENSE("GPL");
253 /* vi:ai:syntax=c:sw=8:ts=8:tw=80