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>
20 #define MAX_INTEL_HEX_RECORD_LENGTH 16
21 typedef struct _INTEL_HEX_RECORD
26 __u8 data
[MAX_INTEL_HEX_RECORD_LENGTH
];
27 } INTEL_HEX_RECORD
, *PINTEL_HEX_RECORD
;
29 /* include firmware (variables)*/
31 /* FIXME: This is quick and dirty solution! */
32 #define SPDIF /* if you want SPDIF comment next line */
33 //#undef SPDIF /* if you want MIDI uncomment this line */
36 # include "emi62_fw_s.h" /* spdif fw */
38 # include "emi62_fw_m.h" /* midi fw */
41 #define EMI62_VENDOR_ID 0x086a /* Emagic Soft-und Hardware GmBH */
42 #define EMI62_PRODUCT_ID 0x0110 /* EMI 6|2m without firmware */
44 #define ANCHOR_LOAD_INTERNAL 0xA0 /* Vendor specific request code for Anchor Upload/Download (This one is implemented in the core) */
45 #define ANCHOR_LOAD_EXTERNAL 0xA3 /* This command is not implemented in the core. Requires firmware */
46 #define ANCHOR_LOAD_FPGA 0xA5 /* This command is not implemented in the core. Requires firmware. Emagic extension */
47 #define MAX_INTERNAL_ADDRESS 0x1B3F /* This is the highest internal RAM address for the AN2131Q */
48 #define CPUCS_REG 0x7F92 /* EZ-USB Control and Status Register. Bit 0 controls 8051 reset */
49 #define INTERNAL_RAM(address) (address <= MAX_INTERNAL_ADDRESS)
51 static int emi62_writememory( struct usb_device
*dev
, int address
, unsigned char *data
, int length
, __u8 bRequest
);
52 static int emi62_set_reset(struct usb_device
*dev
, unsigned char reset_bit
);
53 static int emi62_load_firmware (struct usb_device
*dev
);
54 static int emi62_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
);
55 static void emi62_disconnect(struct usb_interface
*intf
);
56 static int __init
emi62_init (void);
57 static void __exit
emi62_exit (void);
60 /* thanks to drivers/usb/serial/keyspan_pda.c code */
61 static int emi62_writememory (struct usb_device
*dev
, int address
, unsigned char *data
, int length
, __u8 request
)
64 unsigned char *buffer
= kmemdup(data
, length
, GFP_KERNEL
);
67 err("emi62: kmalloc(%d) failed.", length
);
70 /* Note: usb_control_msg returns negative value on error or length of the
71 * data that was written! */
72 result
= usb_control_msg (dev
, usb_sndctrlpipe(dev
, 0), request
, 0x40, address
, 0, buffer
, length
, 300);
77 /* thanks to drivers/usb/serial/keyspan_pda.c code */
78 static int emi62_set_reset (struct usb_device
*dev
, unsigned char reset_bit
)
81 info("%s - %d", __FUNCTION__
, reset_bit
);
83 response
= emi62_writememory (dev
, CPUCS_REG
, &reset_bit
, 1, 0xa0);
85 err("emi62: set_reset (%d) failed", reset_bit
);
90 #define FW_LOAD_SIZE 1023
92 static int emi62_load_firmware (struct usb_device
*dev
)
96 int pos
= 0; /* Position in hex record */
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", __FUNCTION__
, -ENOMEM
);
108 /* Assert reset (stop the CPU in the EMI) */
109 err
= emi62_set_reset(dev
,1);
111 err("%s - error loading firmware: error = %d", __FUNCTION__
, err
);
115 /* 1. We need to put the loader for the FPGA into the EZ-USB */
116 for (i
=0; g_emi62_loader
[i
].type
== 0; i
++) {
117 err
= emi62_writememory(dev
, g_emi62_loader
[i
].address
, g_emi62_loader
[i
].data
, g_emi62_loader
[i
].length
, ANCHOR_LOAD_INTERNAL
);
119 err("%s - error loading firmware: error = %d", __FUNCTION__
, err
);
124 /* De-assert reset (let the CPU run) */
125 err
= emi62_set_reset(dev
,0);
126 msleep(250); /* let device settle */
128 /* 2. We upload the FPGA firmware into the EMI
129 * Note: collect up to 1023 (yes!) bytes and send them with
130 * a single request. This is _much_ faster! */
133 addr
= g_emi62bs
[pos
].address
;
135 /* intel hex records are terminated with type 0 element */
136 while ((g_emi62bs
[pos
].type
== 0) && (i
+ g_emi62bs
[pos
].length
< FW_LOAD_SIZE
)) {
137 memcpy(buf
+ i
, g_emi62bs
[pos
].data
, g_emi62bs
[pos
].length
);
138 i
+= g_emi62bs
[pos
].length
;
141 err
= emi62_writememory(dev
, addr
, buf
, i
, ANCHOR_LOAD_FPGA
);
143 err("%s - error loading firmware: error = %d", __FUNCTION__
, err
);
148 /* Assert reset (stop the CPU in the EMI) */
149 err
= emi62_set_reset(dev
,1);
151 err("%s - error loading firmware: error = %d", __FUNCTION__
, err
);
155 /* 3. We need to put the loader for the firmware into the EZ-USB (again...) */
156 for (i
=0; g_emi62_loader
[i
].type
== 0; i
++) {
157 err
= emi62_writememory(dev
, g_emi62_loader
[i
].address
, g_emi62_loader
[i
].data
, g_emi62_loader
[i
].length
, ANCHOR_LOAD_INTERNAL
);
159 err("%s - error loading firmware: error = %d", __FUNCTION__
, err
);
164 /* De-assert reset (let the CPU run) */
165 err
= emi62_set_reset(dev
,0);
167 err("%s - error loading firmware: error = %d", __FUNCTION__
, err
);
170 msleep(250); /* let device settle */
172 /* 4. We put the part of the firmware that lies in the external RAM into the EZ-USB */
174 /* FIXME: quick and dirty ifdefs */
176 for (i
=0; g_HexSpdifFw62
[i
].type
== 0; i
++) {
177 if (!INTERNAL_RAM(g_HexSpdifFw62
[i
].address
)) {
178 err
= emi62_writememory(dev
, g_HexSpdifFw62
[i
].address
, g_HexSpdifFw62
[i
].data
, g_HexSpdifFw62
[i
].length
, ANCHOR_LOAD_EXTERNAL
);
180 err("%s - error loading firmware: error = %d", __FUNCTION__
, err
);
186 for (i
=0; g_HexMidiFw62
[i
].type
== 0; i
++) {
187 if (!INTERNAL_RAM(g_HexMidiFw62
[i
].address
)) {
188 err
= emi62_writememory(dev
, g_HexMidiFw62
[i
].address
, g_HexMidiFw62
[i
].data
, g_HexMidiFw62
[i
].length
, ANCHOR_LOAD_EXTERNAL
);
190 err("%s - error loading firmware: error = %d\n", __FUNCTION__
, err
);
197 /* Assert reset (stop the CPU in the EMI) */
198 err
= emi62_set_reset(dev
,1);
200 err("%s - error loading firmware: error = %d", __FUNCTION__
, err
);
204 /* FIXME: quick and dirty ifdefs */
206 for (i
=0; g_HexSpdifFw62
[i
].type
== 0; i
++) {
207 if (INTERNAL_RAM(g_HexSpdifFw62
[i
].address
)) {
208 err
= emi62_writememory(dev
, g_HexSpdifFw62
[i
].address
, g_HexSpdifFw62
[i
].data
, g_HexSpdifFw62
[i
].length
, ANCHOR_LOAD_INTERNAL
);
210 err("%s - error loading firmware: error = %d", __FUNCTION__
, err
);
216 for (i
=0; g_HexMidiFw62
[i
].type
== 0; i
++) {
217 if (INTERNAL_RAM(g_HexMidiFw62
[i
].address
)) {
218 err
= emi62_writememory(dev
, g_HexMidiFw62
[i
].address
, g_HexMidiFw62
[i
].data
, g_HexMidiFw62
[i
].length
, ANCHOR_LOAD_INTERNAL
);
220 err("%s - error loading firmware: error = %d\n", __FUNCTION__
, err
);
227 /* De-assert reset (let the CPU run) */
228 err
= emi62_set_reset(dev
,0);
230 err("%s - error loading firmware: error = %d", __FUNCTION__
, err
);
233 msleep(250); /* let device settle */
237 /* return 1 to fail the driver inialization
238 * and give real driver change to load */
243 dev_err(&dev
->dev
, "Error\n");
247 static __devinitdata
struct usb_device_id id_table
[] = {
248 { USB_DEVICE(EMI62_VENDOR_ID
, EMI62_PRODUCT_ID
) },
249 { } /* Terminating entry */
252 MODULE_DEVICE_TABLE (usb
, id_table
);
254 static int emi62_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
256 struct usb_device
*dev
= interface_to_usbdev(intf
);
257 dev_dbg(&intf
->dev
, "emi62_probe\n");
259 info("%s start", __FUNCTION__
);
261 emi62_load_firmware(dev
);
263 /* do not return the driver context, let real audio driver do that */
267 static void emi62_disconnect(struct usb_interface
*intf
)
271 static struct usb_driver emi62_driver
= {
272 .name
= "emi62 - firmware loader",
273 .probe
= emi62_probe
,
274 .disconnect
= emi62_disconnect
,
275 .id_table
= id_table
,
278 static int __init
emi62_init (void)
281 retval
= usb_register (&emi62_driver
);
283 printk(KERN_ERR
"adi-emi: registration failed\n");
287 static void __exit
emi62_exit (void)
289 usb_deregister (&emi62_driver
);
292 module_init(emi62_init
);
293 module_exit(emi62_exit
);
295 MODULE_AUTHOR("tapio laxström");
296 MODULE_DESCRIPTION("Emagic EMI 6|2m firmware loader.");
297 MODULE_LICENSE("GPL");
299 /* vi:ai:syntax=c:sw=8:ts=8:tw=80