Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Emagic EMI 2|6 usb audio interface firmware loader. | |
3 | * Copyright (C) 2002 | |
96de0e25 | 4 | * Tapio Laxström (tapio.laxstrom@iptime.fi) |
1da177e4 LT |
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> | |
16c23f7d | 18 | #include <linux/delay.h> |
1da177e4 LT |
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; | |
5d7efe5b | 56 | unsigned char *buffer = kmemdup(data, length, GFP_KERNEL); |
1da177e4 LT |
57 | |
58 | if (!buffer) { | |
59 | err("emi26: kmalloc(%d) failed.", length); | |
60 | return -ENOMEM; | |
61 | } | |
1da177e4 LT |
62 | /* Note: usb_control_msg returns negative value on error or length of the |
63 | * data that was written! */ | |
64 | result = usb_control_msg (dev, usb_sndctrlpipe(dev, 0), request, 0x40, address, 0, buffer, length, 300); | |
65 | kfree (buffer); | |
66 | return result; | |
67 | } | |
68 | ||
69 | /* thanks to drivers/usb/serial/keyspan_pda.c code */ | |
70 | static int emi26_set_reset (struct usb_device *dev, unsigned char reset_bit) | |
71 | { | |
72 | int response; | |
73 | info("%s - %d", __FUNCTION__, reset_bit); | |
74 | /* printk(KERN_DEBUG "%s - %d", __FUNCTION__, reset_bit); */ | |
75 | response = emi26_writememory (dev, CPUCS_REG, &reset_bit, 1, 0xa0); | |
76 | if (response < 0) { | |
77 | err("emi26: set_reset (%d) failed", reset_bit); | |
78 | } | |
79 | return response; | |
80 | } | |
81 | ||
82 | #define FW_LOAD_SIZE 1023 | |
83 | ||
84 | static int emi26_load_firmware (struct usb_device *dev) | |
85 | { | |
86 | int err; | |
87 | int i; | |
88 | int pos = 0; /* Position in hex record */ | |
89 | __u32 addr; /* Address to write */ | |
90 | __u8 *buf; | |
91 | ||
92 | buf = kmalloc(FW_LOAD_SIZE, GFP_KERNEL); | |
93 | if (!buf) { | |
94 | err( "%s - error loading firmware: error = %d", __FUNCTION__, -ENOMEM); | |
95 | err = -ENOMEM; | |
96 | goto wraperr; | |
97 | } | |
98 | ||
99 | /* Assert reset (stop the CPU in the EMI) */ | |
100 | err = emi26_set_reset(dev,1); | |
101 | if (err < 0) { | |
102 | err( "%s - error loading firmware: error = %d", __FUNCTION__, err); | |
103 | goto wraperr; | |
104 | } | |
105 | ||
106 | /* 1. We need to put the loader for the FPGA into the EZ-USB */ | |
107 | for (i=0; g_Loader[i].type == 0; i++) { | |
108 | err = emi26_writememory(dev, g_Loader[i].address, g_Loader[i].data, g_Loader[i].length, ANCHOR_LOAD_INTERNAL); | |
109 | if (err < 0) { | |
110 | err("%s - error loading firmware: error = %d", __FUNCTION__, err); | |
111 | goto wraperr; | |
112 | } | |
113 | } | |
114 | ||
115 | /* De-assert reset (let the CPU run) */ | |
116 | err = emi26_set_reset(dev,0); | |
16c23f7d | 117 | msleep(250); /* let device settle */ |
1da177e4 LT |
118 | |
119 | /* 2. We upload the FPGA firmware into the EMI | |
120 | * Note: collect up to 1023 (yes!) bytes and send them with | |
121 | * a single request. This is _much_ faster! */ | |
122 | do { | |
123 | i = 0; | |
124 | addr = g_bitstream[pos].address; | |
125 | ||
126 | /* intel hex records are terminated with type 0 element */ | |
127 | while ((g_bitstream[pos].type == 0) && (i + g_bitstream[pos].length < FW_LOAD_SIZE)) { | |
128 | memcpy(buf + i, g_bitstream[pos].data, g_bitstream[pos].length); | |
129 | i += g_bitstream[pos].length; | |
130 | pos++; | |
131 | } | |
132 | err = emi26_writememory(dev, addr, buf, i, ANCHOR_LOAD_FPGA); | |
133 | if (err < 0) { | |
134 | err("%s - error loading firmware: error = %d", __FUNCTION__, err); | |
135 | goto wraperr; | |
136 | } | |
137 | } while (i > 0); | |
138 | ||
139 | /* Assert reset (stop the CPU in the EMI) */ | |
140 | err = emi26_set_reset(dev,1); | |
141 | if (err < 0) { | |
142 | err("%s - error loading firmware: error = %d", __FUNCTION__, err); | |
143 | goto wraperr; | |
144 | } | |
145 | ||
146 | /* 3. We need to put the loader for the firmware into the EZ-USB (again...) */ | |
147 | for (i=0; g_Loader[i].type == 0; i++) { | |
148 | err = emi26_writememory(dev, g_Loader[i].address, g_Loader[i].data, g_Loader[i].length, ANCHOR_LOAD_INTERNAL); | |
149 | if (err < 0) { | |
150 | err("%s - error loading firmware: error = %d", __FUNCTION__, err); | |
151 | goto wraperr; | |
152 | } | |
153 | } | |
16c23f7d | 154 | msleep(250); /* let device settle */ |
1da177e4 LT |
155 | |
156 | /* De-assert reset (let the CPU run) */ | |
157 | err = emi26_set_reset(dev,0); | |
158 | if (err < 0) { | |
159 | err("%s - error loading firmware: error = %d", __FUNCTION__, err); | |
160 | goto wraperr; | |
161 | } | |
162 | ||
163 | /* 4. We put the part of the firmware that lies in the external RAM into the EZ-USB */ | |
164 | for (i=0; g_Firmware[i].type == 0; i++) { | |
165 | if (!INTERNAL_RAM(g_Firmware[i].address)) { | |
166 | err = emi26_writememory(dev, g_Firmware[i].address, g_Firmware[i].data, g_Firmware[i].length, ANCHOR_LOAD_EXTERNAL); | |
167 | if (err < 0) { | |
168 | err("%s - error loading firmware: error = %d", __FUNCTION__, err); | |
169 | goto wraperr; | |
170 | } | |
171 | } | |
172 | } | |
173 | ||
174 | /* Assert reset (stop the CPU in the EMI) */ | |
175 | err = emi26_set_reset(dev,1); | |
176 | if (err < 0) { | |
177 | err("%s - error loading firmware: error = %d", __FUNCTION__, err); | |
178 | goto wraperr; | |
179 | } | |
180 | ||
181 | for (i=0; g_Firmware[i].type == 0; i++) { | |
182 | if (INTERNAL_RAM(g_Firmware[i].address)) { | |
183 | err = emi26_writememory(dev, g_Firmware[i].address, g_Firmware[i].data, g_Firmware[i].length, ANCHOR_LOAD_INTERNAL); | |
184 | if (err < 0) { | |
185 | err("%s - error loading firmware: error = %d", __FUNCTION__, err); | |
186 | goto wraperr; | |
187 | } | |
188 | } | |
189 | } | |
190 | ||
191 | /* De-assert reset (let the CPU run) */ | |
192 | err = emi26_set_reset(dev,0); | |
193 | if (err < 0) { | |
194 | err("%s - error loading firmware: error = %d", __FUNCTION__, err); | |
195 | goto wraperr; | |
196 | } | |
16c23f7d | 197 | msleep(250); /* let device settle */ |
1da177e4 LT |
198 | |
199 | /* return 1 to fail the driver inialization | |
200 | * and give real driver change to load */ | |
201 | err = 1; | |
202 | ||
203 | wraperr: | |
204 | kfree(buf); | |
205 | return err; | |
206 | } | |
207 | ||
208 | static struct usb_device_id id_table [] = { | |
209 | { USB_DEVICE(EMI26_VENDOR_ID, EMI26_PRODUCT_ID) }, | |
210 | { USB_DEVICE(EMI26_VENDOR_ID, EMI26B_PRODUCT_ID) }, | |
211 | { } /* Terminating entry */ | |
212 | }; | |
213 | ||
214 | MODULE_DEVICE_TABLE (usb, id_table); | |
215 | ||
216 | static int emi26_probe(struct usb_interface *intf, const struct usb_device_id *id) | |
217 | { | |
218 | struct usb_device *dev = interface_to_usbdev(intf); | |
219 | ||
220 | info("%s start", __FUNCTION__); | |
221 | ||
222 | emi26_load_firmware(dev); | |
223 | ||
224 | /* do not return the driver context, let real audio driver do that */ | |
225 | return -EIO; | |
226 | } | |
227 | ||
228 | static void emi26_disconnect(struct usb_interface *intf) | |
229 | { | |
230 | } | |
231 | ||
232 | static struct usb_driver emi26_driver = { | |
1da177e4 LT |
233 | .name = "emi26 - firmware loader", |
234 | .probe = emi26_probe, | |
235 | .disconnect = emi26_disconnect, | |
236 | .id_table = id_table, | |
237 | }; | |
238 | ||
239 | static int __init emi26_init (void) | |
240 | { | |
241 | return usb_register(&emi26_driver); | |
242 | } | |
243 | ||
244 | static void __exit emi26_exit (void) | |
245 | { | |
246 | usb_deregister (&emi26_driver); | |
247 | } | |
248 | ||
249 | module_init(emi26_init); | |
250 | module_exit(emi26_exit); | |
251 | ||
96de0e25 | 252 | MODULE_AUTHOR("Tapio Laxström"); |
1da177e4 LT |
253 | MODULE_DESCRIPTION("Emagic EMI 2|6 firmware loader."); |
254 | MODULE_LICENSE("GPL"); | |
255 | ||
256 | /* vi:ai:syntax=c:sw=8:ts=8:tw=80 | |
257 | */ |