1 // SPDX-License-Identifier: GPL-2.0
3 * Emagic EMI 2|6 usb audio interface firmware loader.
5 * Tapio Laxström (tapio.laxstrom@iptime.fi)
7 #include <linux/kernel.h>
8 #include <linux/errno.h>
9 #include <linux/slab.h>
10 #include <linux/module.h>
11 #include <linux/usb.h>
12 #include <linux/delay.h>
13 #include <linux/firmware.h>
14 #include <linux/ihex.h>
16 /* include firmware (variables)*/
18 /* FIXME: This is quick and dirty solution! */
19 #define SPDIF /* if you want SPDIF comment next line */
20 //#undef SPDIF /* if you want MIDI uncomment this line */
23 #define FIRMWARE_FW "emi62/spdif.fw"
25 #define FIRMWARE_FW "emi62/midi.fw"
28 #define EMI62_VENDOR_ID 0x086a /* Emagic Soft-und Hardware GmBH */
29 #define EMI62_PRODUCT_ID 0x0110 /* EMI 6|2m without firmware */
31 #define ANCHOR_LOAD_INTERNAL 0xA0 /* Vendor specific request code for Anchor Upload/Download (This one is implemented in the core) */
32 #define ANCHOR_LOAD_EXTERNAL 0xA3 /* This command is not implemented in the core. Requires firmware */
33 #define ANCHOR_LOAD_FPGA 0xA5 /* This command is not implemented in the core. Requires firmware. Emagic extension */
34 #define MAX_INTERNAL_ADDRESS 0x1B3F /* This is the highest internal RAM address for the AN2131Q */
35 #define CPUCS_REG 0x7F92 /* EZ-USB Control and Status Register. Bit 0 controls 8051 reset */
36 #define INTERNAL_RAM(address) (address <= MAX_INTERNAL_ADDRESS)
38 static int emi62_writememory(struct usb_device
*dev
, int address
,
39 const unsigned char *data
, int length
,
41 static int emi62_set_reset(struct usb_device
*dev
, unsigned char reset_bit
);
42 static int emi62_load_firmware (struct usb_device
*dev
);
43 static int emi62_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
);
44 static void emi62_disconnect(struct usb_interface
*intf
);
46 /* thanks to drivers/usb/serial/keyspan_pda.c code */
47 static int emi62_writememory(struct usb_device
*dev
, int address
,
48 const unsigned char *data
, int length
,
52 unsigned char *buffer
= kmemdup(data
, length
, GFP_KERNEL
);
55 dev_err(&dev
->dev
, "kmalloc(%d) failed.\n", length
);
58 /* Note: usb_control_msg returns negative value on error or length of the
59 * data that was written! */
60 result
= usb_control_msg (dev
, usb_sndctrlpipe(dev
, 0), request
, 0x40, address
, 0, buffer
, length
, 300);
65 /* thanks to drivers/usb/serial/keyspan_pda.c code */
66 static int emi62_set_reset (struct usb_device
*dev
, unsigned char reset_bit
)
69 dev_info(&dev
->dev
, "%s - %d\n", __func__
, reset_bit
);
71 response
= emi62_writememory (dev
, CPUCS_REG
, &reset_bit
, 1, 0xa0);
73 dev_err(&dev
->dev
, "set_reset (%d) failed\n", reset_bit
);
77 #define FW_LOAD_SIZE 1023
79 static int emi62_load_firmware (struct usb_device
*dev
)
81 const struct firmware
*loader_fw
= NULL
;
82 const struct firmware
*bitstream_fw
= NULL
;
83 const struct firmware
*firmware_fw
= NULL
;
84 const struct ihex_binrec
*rec
;
87 __u32 addr
; /* Address to write */
90 dev_dbg(&dev
->dev
, "load_firmware\n");
91 buf
= kmalloc(FW_LOAD_SIZE
, GFP_KERNEL
);
95 err
= request_ihex_firmware(&loader_fw
, "emi62/loader.fw", &dev
->dev
);
99 err
= request_ihex_firmware(&bitstream_fw
, "emi62/bitstream.fw",
104 err
= request_ihex_firmware(&firmware_fw
, FIRMWARE_FW
, &dev
->dev
);
110 /* Assert reset (stop the CPU in the EMI) */
111 err
= emi62_set_reset(dev
,1);
115 rec
= (const struct ihex_binrec
*)loader_fw
->data
;
117 /* 1. We need to put the loader for the FPGA into the EZ-USB */
119 err
= emi62_writememory(dev
, be32_to_cpu(rec
->addr
),
120 rec
->data
, be16_to_cpu(rec
->len
),
121 ANCHOR_LOAD_INTERNAL
);
124 rec
= ihex_next_binrec(rec
);
127 /* De-assert reset (let the CPU run) */
128 err
= emi62_set_reset(dev
,0);
131 msleep(250); /* let device settle */
133 /* 2. We upload the FPGA firmware into the EMI
134 * Note: collect up to 1023 (yes!) bytes and send them with
135 * a single request. This is _much_ faster! */
136 rec
= (const struct ihex_binrec
*)bitstream_fw
->data
;
139 addr
= be32_to_cpu(rec
->addr
);
141 /* intel hex records are terminated with type 0 element */
142 while (rec
&& (i
+ be16_to_cpu(rec
->len
) < FW_LOAD_SIZE
)) {
143 memcpy(buf
+ i
, rec
->data
, be16_to_cpu(rec
->len
));
144 i
+= be16_to_cpu(rec
->len
);
145 rec
= ihex_next_binrec(rec
);
147 err
= emi62_writememory(dev
, addr
, buf
, i
, ANCHOR_LOAD_FPGA
);
152 /* Assert reset (stop the CPU in the EMI) */
153 err
= emi62_set_reset(dev
,1);
157 /* 3. We need to put the loader for the firmware into the EZ-USB (again...) */
158 for (rec
= (const struct ihex_binrec
*)loader_fw
->data
;
159 rec
; rec
= ihex_next_binrec(rec
)) {
160 err
= emi62_writememory(dev
, be32_to_cpu(rec
->addr
),
161 rec
->data
, be16_to_cpu(rec
->len
),
162 ANCHOR_LOAD_INTERNAL
);
167 /* De-assert reset (let the CPU run) */
168 err
= emi62_set_reset(dev
,0);
171 msleep(250); /* let device settle */
173 /* 4. We put the part of the firmware that lies in the external RAM into the EZ-USB */
175 for (rec
= (const struct ihex_binrec
*)firmware_fw
->data
;
176 rec
; rec
= ihex_next_binrec(rec
)) {
177 if (!INTERNAL_RAM(be32_to_cpu(rec
->addr
))) {
178 err
= emi62_writememory(dev
, be32_to_cpu(rec
->addr
),
179 rec
->data
, be16_to_cpu(rec
->len
),
180 ANCHOR_LOAD_EXTERNAL
);
186 /* Assert reset (stop the CPU in the EMI) */
187 err
= emi62_set_reset(dev
,1);
191 for (rec
= (const struct ihex_binrec
*)firmware_fw
->data
;
192 rec
; rec
= ihex_next_binrec(rec
)) {
193 if (INTERNAL_RAM(be32_to_cpu(rec
->addr
))) {
194 err
= emi62_writememory(dev
, be32_to_cpu(rec
->addr
),
195 rec
->data
, be16_to_cpu(rec
->len
),
196 ANCHOR_LOAD_EXTERNAL
);
202 /* De-assert reset (let the CPU run) */
203 err
= emi62_set_reset(dev
,0);
206 msleep(250); /* let device settle */
208 release_firmware(loader_fw
);
209 release_firmware(bitstream_fw
);
210 release_firmware(firmware_fw
);
214 /* return 1 to fail the driver inialization
215 * and give real driver change to load */
220 dev_err(&dev
->dev
,"%s - error loading firmware: error = %d\n",
222 release_firmware(loader_fw
);
223 release_firmware(bitstream_fw
);
224 release_firmware(firmware_fw
);
227 dev_err(&dev
->dev
, "Error\n");
231 static const struct usb_device_id id_table
[] = {
232 { USB_DEVICE(EMI62_VENDOR_ID
, EMI62_PRODUCT_ID
) },
233 { } /* Terminating entry */
236 MODULE_DEVICE_TABLE (usb
, id_table
);
238 static int emi62_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
240 struct usb_device
*dev
= interface_to_usbdev(intf
);
241 dev_dbg(&intf
->dev
, "emi62_probe\n");
243 dev_info(&intf
->dev
, "%s start\n", __func__
);
245 emi62_load_firmware(dev
);
247 /* do not return the driver context, let real audio driver do that */
251 static void emi62_disconnect(struct usb_interface
*intf
)
255 static struct usb_driver emi62_driver
= {
256 .name
= "emi62 - firmware loader",
257 .probe
= emi62_probe
,
258 .disconnect
= emi62_disconnect
,
259 .id_table
= id_table
,
262 module_usb_driver(emi62_driver
);
264 MODULE_AUTHOR("Tapio Laxström");
265 MODULE_DESCRIPTION("Emagic EMI 6|2m firmware loader.");
266 MODULE_LICENSE("GPL");
268 MODULE_FIRMWARE("emi62/loader.fw");
269 MODULE_FIRMWARE("emi62/bitstream.fw");
270 MODULE_FIRMWARE(FIRMWARE_FW
);
271 /* vi:ai:syntax=c:sw=8:ts=8:tw=80