Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / drivers / usb / misc / emi26.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  * emi26.c,v 1.13 2002/03/08 13:10:26 tapio Exp
11  */
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>
18 #include <linux/delay.h>
19
20 #define MAX_INTEL_HEX_RECORD_LENGTH 16
21 typedef struct _INTEL_HEX_RECORD
22 {
23         __u32   length;
24         __u32   address;
25         __u32   type;
26         __u8    data[MAX_INTEL_HEX_RECORD_LENGTH];
27 } INTEL_HEX_RECORD, *PINTEL_HEX_RECORD;
28
29 /* include firmware (variables) */
30 #include "emi26_fw.h"
31
32 #define EMI26_VENDOR_ID                 0x086a  /* Emagic Soft-und Hardware GmBH */
33 #define EMI26_PRODUCT_ID                0x0100  /* EMI 2|6 without firmware */
34 #define EMI26B_PRODUCT_ID               0x0102  /* EMI 2|6 without firmware */
35
36 #define ANCHOR_LOAD_INTERNAL    0xA0    /* Vendor specific request code for Anchor Upload/Download (This one is implemented in the core) */
37 #define ANCHOR_LOAD_EXTERNAL    0xA3    /* This command is not implemented in the core. Requires firmware */
38 #define ANCHOR_LOAD_FPGA        0xA5    /* This command is not implemented in the core. Requires firmware. Emagic extension */
39 #define MAX_INTERNAL_ADDRESS    0x1B3F  /* This is the highest internal RAM address for the AN2131Q */
40 #define CPUCS_REG               0x7F92  /* EZ-USB Control and Status Register.  Bit 0 controls 8051 reset */ 
41 #define INTERNAL_RAM(address)   (address <= MAX_INTERNAL_ADDRESS)
42
43 static int emi26_writememory( struct usb_device *dev, int address, unsigned char *data, int length, __u8 bRequest);
44 static int emi26_set_reset(struct usb_device *dev, unsigned char reset_bit);
45 static int emi26_load_firmware (struct usb_device *dev);
46 static int emi26_probe(struct usb_interface *intf, const struct usb_device_id *id);
47 static void emi26_disconnect(struct usb_interface *intf);
48 static int __init emi26_init (void);
49 static void __exit emi26_exit (void);
50
51
52 /* thanks to drivers/usb/serial/keyspan_pda.c code */
53 static int emi26_writememory (struct usb_device *dev, int address, unsigned char *data, int length, __u8 request)
54 {
55         int result;
56         unsigned char *buffer =  kmalloc (length, GFP_KERNEL);
57
58         if (!buffer) {
59                 err("emi26: kmalloc(%d) failed.", length);
60                 return -ENOMEM;
61         }
62         memcpy (buffer, data, length);
63         /* Note: usb_control_msg returns negative value on error or length of the
64          *               data that was written! */
65         result = usb_control_msg (dev, usb_sndctrlpipe(dev, 0), request, 0x40, address, 0, buffer, length, 300);
66         kfree (buffer);
67         return result;
68 }
69
70 /* thanks to drivers/usb/serial/keyspan_pda.c code */
71 static int emi26_set_reset (struct usb_device *dev, unsigned char reset_bit)
72 {
73         int response;
74         info("%s - %d", __FUNCTION__, reset_bit);
75         /* printk(KERN_DEBUG "%s - %d", __FUNCTION__, reset_bit); */
76         response = emi26_writememory (dev, CPUCS_REG, &reset_bit, 1, 0xa0);
77         if (response < 0) {
78                 err("emi26: set_reset (%d) failed", reset_bit);
79         }
80         return response;
81 }
82
83 #define FW_LOAD_SIZE            1023
84
85 static int emi26_load_firmware (struct usb_device *dev)
86 {
87         int err;
88         int i;
89         int pos = 0;    /* Position in hex record */
90         __u32 addr;     /* Address to write */
91         __u8 *buf;
92
93         buf = kmalloc(FW_LOAD_SIZE, GFP_KERNEL);
94         if (!buf) {
95                 err( "%s - error loading firmware: error = %d", __FUNCTION__, -ENOMEM);
96                 err = -ENOMEM;
97                 goto wraperr;
98         }
99
100         /* Assert reset (stop the CPU in the EMI) */
101         err = emi26_set_reset(dev,1);
102         if (err < 0) {
103                 err( "%s - error loading firmware: error = %d", __FUNCTION__, err);
104                 goto wraperr;
105         }
106
107         /* 1. We need to put the loader for the FPGA into the EZ-USB */
108         for (i=0; g_Loader[i].type == 0; i++) {
109                 err = emi26_writememory(dev, g_Loader[i].address, g_Loader[i].data, g_Loader[i].length, ANCHOR_LOAD_INTERNAL);
110                 if (err < 0) {
111                         err("%s - error loading firmware: error = %d", __FUNCTION__, err);
112                         goto wraperr;
113                 }
114         }
115
116         /* De-assert reset (let the CPU run) */
117         err = emi26_set_reset(dev,0);
118         msleep(250);    /* let device settle */
119
120         /* 2. We upload the FPGA firmware into the EMI
121          * Note: collect up to 1023 (yes!) bytes and send them with
122          * a single request. This is _much_ faster! */
123         do {
124                 i = 0;
125                 addr = g_bitstream[pos].address;
126
127                 /* intel hex records are terminated with type 0 element */
128                 while ((g_bitstream[pos].type == 0) && (i + g_bitstream[pos].length < FW_LOAD_SIZE)) {
129                         memcpy(buf + i, g_bitstream[pos].data, g_bitstream[pos].length);
130                         i += g_bitstream[pos].length;
131                         pos++;
132                 }
133                 err = emi26_writememory(dev, addr, buf, i, ANCHOR_LOAD_FPGA);
134                 if (err < 0) {
135                         err("%s - error loading firmware: error = %d", __FUNCTION__, err);
136                         goto wraperr;
137                 }
138         } while (i > 0);
139
140         /* Assert reset (stop the CPU in the EMI) */
141         err = emi26_set_reset(dev,1);
142         if (err < 0) {
143                 err("%s - error loading firmware: error = %d", __FUNCTION__, err);
144                 goto wraperr;
145         }
146
147         /* 3. We need to put the loader for the firmware into the EZ-USB (again...) */
148         for (i=0; g_Loader[i].type == 0; i++) {
149                 err = emi26_writememory(dev, g_Loader[i].address, g_Loader[i].data, g_Loader[i].length, ANCHOR_LOAD_INTERNAL);
150                 if (err < 0) {
151                         err("%s - error loading firmware: error = %d", __FUNCTION__, err);
152                         goto wraperr;
153                 }
154         }
155         msleep(250);    /* let device settle */
156
157         /* De-assert reset (let the CPU run) */
158         err = emi26_set_reset(dev,0);
159         if (err < 0) {
160                 err("%s - error loading firmware: error = %d", __FUNCTION__, err);
161                 goto wraperr;
162         }
163
164         /* 4. We put the part of the firmware that lies in the external RAM into the EZ-USB */
165         for (i=0; g_Firmware[i].type == 0; i++) {
166                 if (!INTERNAL_RAM(g_Firmware[i].address)) {
167                         err = emi26_writememory(dev, g_Firmware[i].address, g_Firmware[i].data, g_Firmware[i].length, ANCHOR_LOAD_EXTERNAL);
168                         if (err < 0) {
169                                 err("%s - error loading firmware: error = %d", __FUNCTION__, err);
170                                 goto wraperr;
171                         }
172                 }
173         }
174         
175         /* Assert reset (stop the CPU in the EMI) */
176         err = emi26_set_reset(dev,1);
177         if (err < 0) {
178                 err("%s - error loading firmware: error = %d", __FUNCTION__, err);
179                 goto wraperr;
180         }
181
182         for (i=0; g_Firmware[i].type == 0; i++) {
183                 if (INTERNAL_RAM(g_Firmware[i].address)) {
184                         err = emi26_writememory(dev, g_Firmware[i].address, g_Firmware[i].data, g_Firmware[i].length, ANCHOR_LOAD_INTERNAL);
185                         if (err < 0) {
186                                 err("%s - error loading firmware: error = %d", __FUNCTION__, err);
187                                 goto wraperr;
188                         }
189                 }
190         }
191
192         /* De-assert reset (let the CPU run) */
193         err = emi26_set_reset(dev,0);
194         if (err < 0) {
195                 err("%s - error loading firmware: error = %d", __FUNCTION__, err);
196                 goto wraperr;
197         }
198         msleep(250);    /* let device settle */
199
200         /* return 1 to fail the driver inialization
201          * and give real driver change to load */
202         err = 1;
203
204 wraperr:
205         kfree(buf);
206         return err;
207 }
208
209 static struct usb_device_id id_table [] = {
210         { USB_DEVICE(EMI26_VENDOR_ID, EMI26_PRODUCT_ID) },
211         { USB_DEVICE(EMI26_VENDOR_ID, EMI26B_PRODUCT_ID) },
212         { }                                             /* Terminating entry */
213 };
214
215 MODULE_DEVICE_TABLE (usb, id_table);
216
217 static int emi26_probe(struct usb_interface *intf, const struct usb_device_id *id)
218 {
219         struct usb_device *dev = interface_to_usbdev(intf);
220
221         info("%s start", __FUNCTION__); 
222
223         emi26_load_firmware(dev);
224
225         /* do not return the driver context, let real audio driver do that */
226         return -EIO;
227 }
228
229 static void emi26_disconnect(struct usb_interface *intf)
230 {
231 }
232
233 static struct usb_driver emi26_driver = {
234         .name           = "emi26 - firmware loader",
235         .probe          = emi26_probe,
236         .disconnect     = emi26_disconnect,
237         .id_table       = id_table,
238 };
239
240 static int __init emi26_init (void)
241 {
242         return usb_register(&emi26_driver);
243 }
244
245 static void __exit emi26_exit (void)
246 {
247         usb_deregister (&emi26_driver);
248 }
249
250 module_init(emi26_init);
251 module_exit(emi26_exit);
252
253 MODULE_AUTHOR("tapio laxström");
254 MODULE_DESCRIPTION("Emagic EMI 2|6 firmware loader.");
255 MODULE_LICENSE("GPL");
256
257 /* vi:ai:syntax=c:sw=8:ts=8:tw=80
258  */