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