]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - drivers/usb/misc/emi62.c
Merge branch 'i2c-for-linus' of git://jdelvare.pck.nerim.net/jdelvare-2.6
[mirror_ubuntu-hirsute-kernel.git] / drivers / usb / misc / emi62.c
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 * $Id: emi62.c,v 1.15 2002/04/23 06:13:59 tapio Exp $
11 */
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>
21
22 /* include firmware (variables)*/
23
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 */
27
28 #ifdef SPDIF
29 #define FIRMWARE_FW "emi62/spdif.fw"
30 #else
31 #define FIRMWARE_FW "emi62/midi.fw"
32 #endif
33
34 #define EMI62_VENDOR_ID 0x086a /* Emagic Soft-und Hardware GmBH */
35 #define EMI62_PRODUCT_ID 0x0110 /* EMI 6|2m without firmware */
36
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)
43
44 static int emi62_writememory(struct usb_device *dev, int address,
45 const unsigned char *data, int length,
46 __u8 bRequest);
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);
53
54
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,
58 __u8 request)
59 {
60 int result;
61 unsigned char *buffer = kmemdup(data, length, GFP_KERNEL);
62
63 if (!buffer) {
64 err("emi62: kmalloc(%d) failed.", length);
65 return -ENOMEM;
66 }
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);
70 kfree (buffer);
71 return result;
72 }
73
74 /* thanks to drivers/usb/serial/keyspan_pda.c code */
75 static int emi62_set_reset (struct usb_device *dev, unsigned char reset_bit)
76 {
77 int response;
78 info("%s - %d", __func__, reset_bit);
79
80 response = emi62_writememory (dev, CPUCS_REG, &reset_bit, 1, 0xa0);
81 if (response < 0) {
82 err("emi62: set_reset (%d) failed", reset_bit);
83 }
84 return response;
85 }
86
87 #define FW_LOAD_SIZE 1023
88
89 static int emi62_load_firmware (struct usb_device *dev)
90 {
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;
95 int err;
96 int i;
97 __u32 addr; /* Address to write */
98 __u8 *buf;
99
100 dev_dbg(&dev->dev, "load_firmware\n");
101 buf = kmalloc(FW_LOAD_SIZE, GFP_KERNEL);
102 if (!buf) {
103 err( "%s - error loading firmware: error = %d", __func__, -ENOMEM);
104 err = -ENOMEM;
105 goto wraperr;
106 }
107
108 err = request_ihex_firmware(&loader_fw, "emi62/loader.fw", &dev->dev);
109 if (err)
110 goto nofw;
111
112 err = request_ihex_firmware(&bitstream_fw, "emi62/bitstream.fw",
113 &dev->dev);
114 if (err)
115 goto nofw;
116
117 err = request_ihex_firmware(&firmware_fw, FIRMWARE_FW, &dev->dev);
118 if (err) {
119 nofw:
120 err( "%s - request_firmware() failed", __func__);
121 goto wraperr;
122 }
123
124 /* Assert reset (stop the CPU in the EMI) */
125 err = emi62_set_reset(dev,1);
126 if (err < 0) {
127 err("%s - error loading firmware: error = %d", __func__, err);
128 goto wraperr;
129 }
130
131 rec = (const struct ihex_binrec *)loader_fw->data;
132
133 /* 1. We need to put the loader for the FPGA into the EZ-USB */
134 while (rec) {
135 err = emi62_writememory(dev, be32_to_cpu(rec->addr),
136 rec->data, be16_to_cpu(rec->len),
137 ANCHOR_LOAD_INTERNAL);
138 if (err < 0) {
139 err("%s - error loading firmware: error = %d", __func__, err);
140 goto wraperr;
141 }
142 rec = ihex_next_binrec(rec);
143 }
144
145 /* De-assert reset (let the CPU run) */
146 err = emi62_set_reset(dev,0);
147 if (err < 0) {
148 err("%s - error loading firmware: error = %d", __func__, err);
149 goto wraperr;
150 }
151 msleep(250); /* let device settle */
152
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;
157 do {
158 i = 0;
159 addr = be32_to_cpu(rec->addr);
160
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);
166 }
167 err = emi62_writememory(dev, addr, buf, i, ANCHOR_LOAD_FPGA);
168 if (err < 0) {
169 err("%s - error loading firmware: error = %d", __func__, err);
170 goto wraperr;
171 }
172 } while (i > 0);
173
174 /* Assert reset (stop the CPU in the EMI) */
175 err = emi62_set_reset(dev,1);
176 if (err < 0) {
177 err("%s - error loading firmware: error = %d", __func__, err);
178 goto wraperr;
179 }
180
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);
187 if (err < 0) {
188 err("%s - error loading firmware: error = %d", __func__, err);
189 goto wraperr;
190 }
191 }
192
193 /* De-assert reset (let the CPU run) */
194 err = emi62_set_reset(dev,0);
195 if (err < 0) {
196 err("%s - error loading firmware: error = %d", __func__, err);
197 goto wraperr;
198 }
199 msleep(250); /* let device settle */
200
201 /* 4. We put the part of the firmware that lies in the external RAM into the EZ-USB */
202
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);
209 if (err < 0) {
210 err("%s - error loading firmware: error = %d", __func__, err);
211 goto wraperr;
212 }
213 }
214 }
215
216 /* Assert reset (stop the CPU in the EMI) */
217 err = emi62_set_reset(dev,1);
218 if (err < 0) {
219 err("%s - error loading firmware: error = %d", __func__, err);
220 goto wraperr;
221 }
222
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);
229 if (err < 0) {
230 err("%s - error loading firmware: error = %d", __func__, err);
231 goto wraperr;
232 }
233 }
234 }
235
236 /* De-assert reset (let the CPU run) */
237 err = emi62_set_reset(dev,0);
238 if (err < 0) {
239 err("%s - error loading firmware: error = %d", __func__, err);
240 goto wraperr;
241 }
242 msleep(250); /* let device settle */
243
244 release_firmware(loader_fw);
245 release_firmware(bitstream_fw);
246 release_firmware(firmware_fw);
247
248 kfree(buf);
249
250 /* return 1 to fail the driver inialization
251 * and give real driver change to load */
252 return 1;
253
254 wraperr:
255 release_firmware(loader_fw);
256 release_firmware(bitstream_fw);
257 release_firmware(firmware_fw);
258
259 kfree(buf);
260 dev_err(&dev->dev, "Error\n");
261 return err;
262 }
263
264 static __devinitdata struct usb_device_id id_table [] = {
265 { USB_DEVICE(EMI62_VENDOR_ID, EMI62_PRODUCT_ID) },
266 { } /* Terminating entry */
267 };
268
269 MODULE_DEVICE_TABLE (usb, id_table);
270
271 static int emi62_probe(struct usb_interface *intf, const struct usb_device_id *id)
272 {
273 struct usb_device *dev = interface_to_usbdev(intf);
274 dev_dbg(&intf->dev, "emi62_probe\n");
275
276 info("%s start", __func__);
277
278 emi62_load_firmware(dev);
279
280 /* do not return the driver context, let real audio driver do that */
281 return -EIO;
282 }
283
284 static void emi62_disconnect(struct usb_interface *intf)
285 {
286 }
287
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,
293 };
294
295 static int __init emi62_init (void)
296 {
297 int retval;
298 retval = usb_register (&emi62_driver);
299 if (retval)
300 printk(KERN_ERR "adi-emi: registration failed\n");
301 return retval;
302 }
303
304 static void __exit emi62_exit (void)
305 {
306 usb_deregister (&emi62_driver);
307 }
308
309 module_init(emi62_init);
310 module_exit(emi62_exit);
311
312 MODULE_AUTHOR("Tapio Laxström");
313 MODULE_DESCRIPTION("Emagic EMI 6|2m firmware loader.");
314 MODULE_LICENSE("GPL");
315
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
320 */