Merge branch 'akpm'
[linux-2.6/next.git] / sound / usb / 6fire / firmware.c
blob3ebbdec2a46487f0917320bc4bb7d882ea60cbe0
1 /*
2 * Linux driver for TerraTec DMX 6Fire USB
4 * Firmware loader
6 * Author: Torsten Schenk <torsten.schenk@zoho.com>
7 * Created: Jan 01, 2011
8 * Version: 0.3.0
9 * Copyright: (C) Torsten Schenk
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
17 #include <linux/firmware.h>
18 #include <linux/module.h>
19 #include <linux/bitrev.h>
21 #include "firmware.h"
22 #include "chip.h"
24 MODULE_FIRMWARE("6fire/dmx6firel2.ihx");
25 MODULE_FIRMWARE("6fire/dmx6fireap.ihx");
26 MODULE_FIRMWARE("6fire/dmx6firecf.bin");
28 enum {
29 FPGA_BUFSIZE = 512, FPGA_EP = 2
33 * wMaxPacketSize of pcm endpoints.
34 * keep synced with rates_in_packet_size and rates_out_packet_size in pcm.c
35 * fpp: frames per isopacket
37 * CAUTION: keep sizeof <= buffer[] in usb6fire_fw_init
39 static const u8 ep_w_max_packet_size[] = {
40 0xe4, 0x00, 0xe4, 0x00, /* alt 1: 228 EP2 and EP6 (7 fpp) */
41 0xa4, 0x01, 0xa4, 0x01, /* alt 2: 420 EP2 and EP6 (13 fpp)*/
42 0x94, 0x01, 0x5c, 0x02 /* alt 3: 404 EP2 and 604 EP6 (25 fpp) */
45 static const u8 known_fw_versions[][4] = {
46 { 0x03, 0x01, 0x0b, 0x00 }
49 struct ihex_record {
50 u16 address;
51 u8 len;
52 u8 data[256];
53 char error; /* true if an error occurred parsing this record */
55 u8 max_len; /* maximum record length in whole ihex */
57 /* private */
58 const char *txt_data;
59 unsigned int txt_length;
60 unsigned int txt_offset; /* current position in txt_data */
63 static u8 usb6fire_fw_ihex_nibble(const u8 n)
65 if (n >= '0' && n <= '9')
66 return n - '0';
67 else if (n >= 'A' && n <= 'F')
68 return n - ('A' - 10);
69 else if (n >= 'a' && n <= 'f')
70 return n - ('a' - 10);
71 return 0;
74 static u8 usb6fire_fw_ihex_hex(const u8 *data, u8 *crc)
76 u8 val = (usb6fire_fw_ihex_nibble(data[0]) << 4) |
77 usb6fire_fw_ihex_nibble(data[1]);
78 *crc += val;
79 return val;
83 * returns true if record is available, false otherwise.
84 * iff an error occurred, false will be returned and record->error will be true.
86 static bool usb6fire_fw_ihex_next_record(struct ihex_record *record)
88 u8 crc = 0;
89 u8 type;
90 int i;
92 record->error = false;
94 /* find begin of record (marked by a colon) */
95 while (record->txt_offset < record->txt_length
96 && record->txt_data[record->txt_offset] != ':')
97 record->txt_offset++;
98 if (record->txt_offset == record->txt_length)
99 return false;
101 /* number of characters needed for len, addr and type entries */
102 record->txt_offset++;
103 if (record->txt_offset + 8 > record->txt_length) {
104 record->error = true;
105 return false;
108 record->len = usb6fire_fw_ihex_hex(record->txt_data +
109 record->txt_offset, &crc);
110 record->txt_offset += 2;
111 record->address = usb6fire_fw_ihex_hex(record->txt_data +
112 record->txt_offset, &crc) << 8;
113 record->txt_offset += 2;
114 record->address |= usb6fire_fw_ihex_hex(record->txt_data +
115 record->txt_offset, &crc);
116 record->txt_offset += 2;
117 type = usb6fire_fw_ihex_hex(record->txt_data +
118 record->txt_offset, &crc);
119 record->txt_offset += 2;
121 /* number of characters needed for data and crc entries */
122 if (record->txt_offset + 2 * (record->len + 1) > record->txt_length) {
123 record->error = true;
124 return false;
126 for (i = 0; i < record->len; i++) {
127 record->data[i] = usb6fire_fw_ihex_hex(record->txt_data
128 + record->txt_offset, &crc);
129 record->txt_offset += 2;
131 usb6fire_fw_ihex_hex(record->txt_data + record->txt_offset, &crc);
132 if (crc) {
133 record->error = true;
134 return false;
137 if (type == 1 || !record->len) /* eof */
138 return false;
139 else if (type == 0)
140 return true;
141 else {
142 record->error = true;
143 return false;
147 static int usb6fire_fw_ihex_init(const struct firmware *fw,
148 struct ihex_record *record)
150 record->txt_data = fw->data;
151 record->txt_length = fw->size;
152 record->txt_offset = 0;
153 record->max_len = 0;
154 /* read all records, if loop ends, record->error indicates,
155 * whether ihex is valid. */
156 while (usb6fire_fw_ihex_next_record(record))
157 record->max_len = max(record->len, record->max_len);
158 if (record->error)
159 return -EINVAL;
160 record->txt_offset = 0;
161 return 0;
164 static int usb6fire_fw_ezusb_write(struct usb_device *device,
165 int type, int value, char *data, int len)
167 int ret;
169 ret = usb_control_msg(device, usb_sndctrlpipe(device, 0), type,
170 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
171 value, 0, data, len, HZ);
172 if (ret < 0)
173 return ret;
174 else if (ret != len)
175 return -EIO;
176 return 0;
179 static int usb6fire_fw_ezusb_read(struct usb_device *device,
180 int type, int value, char *data, int len)
182 int ret = usb_control_msg(device, usb_rcvctrlpipe(device, 0), type,
183 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, value,
184 0, data, len, HZ);
185 if (ret < 0)
186 return ret;
187 else if (ret != len)
188 return -EIO;
189 return 0;
192 static int usb6fire_fw_fpga_write(struct usb_device *device,
193 char *data, int len)
195 int actual_len;
196 int ret;
198 ret = usb_bulk_msg(device, usb_sndbulkpipe(device, FPGA_EP), data, len,
199 &actual_len, HZ);
200 if (ret < 0)
201 return ret;
202 else if (actual_len != len)
203 return -EIO;
204 return 0;
207 static int usb6fire_fw_ezusb_upload(
208 struct usb_interface *intf, const char *fwname,
209 unsigned int postaddr, u8 *postdata, unsigned int postlen)
211 int ret;
212 u8 data;
213 struct usb_device *device = interface_to_usbdev(intf);
214 const struct firmware *fw = 0;
215 struct ihex_record *rec = kmalloc(sizeof(struct ihex_record),
216 GFP_KERNEL);
218 if (!rec)
219 return -ENOMEM;
221 ret = request_firmware(&fw, fwname, &device->dev);
222 if (ret < 0) {
223 kfree(rec);
224 snd_printk(KERN_ERR PREFIX "error requesting ezusb "
225 "firmware %s.\n", fwname);
226 return ret;
228 ret = usb6fire_fw_ihex_init(fw, rec);
229 if (ret < 0) {
230 kfree(rec);
231 release_firmware(fw);
232 snd_printk(KERN_ERR PREFIX "error validating ezusb "
233 "firmware %s.\n", fwname);
234 return ret;
236 /* upload firmware image */
237 data = 0x01; /* stop ezusb cpu */
238 ret = usb6fire_fw_ezusb_write(device, 0xa0, 0xe600, &data, 1);
239 if (ret < 0) {
240 kfree(rec);
241 release_firmware(fw);
242 snd_printk(KERN_ERR PREFIX "unable to upload ezusb "
243 "firmware %s: begin message.\n", fwname);
244 return ret;
247 while (usb6fire_fw_ihex_next_record(rec)) { /* write firmware */
248 ret = usb6fire_fw_ezusb_write(device, 0xa0, rec->address,
249 rec->data, rec->len);
250 if (ret < 0) {
251 kfree(rec);
252 release_firmware(fw);
253 snd_printk(KERN_ERR PREFIX "unable to upload ezusb "
254 "firmware %s: data urb.\n", fwname);
255 return ret;
259 release_firmware(fw);
260 kfree(rec);
261 if (postdata) { /* write data after firmware has been uploaded */
262 ret = usb6fire_fw_ezusb_write(device, 0xa0, postaddr,
263 postdata, postlen);
264 if (ret < 0) {
265 snd_printk(KERN_ERR PREFIX "unable to upload ezusb "
266 "firmware %s: post urb.\n", fwname);
267 return ret;
271 data = 0x00; /* resume ezusb cpu */
272 ret = usb6fire_fw_ezusb_write(device, 0xa0, 0xe600, &data, 1);
273 if (ret < 0) {
274 snd_printk(KERN_ERR PREFIX "unable to upload ezusb "
275 "firmware %s: end message.\n", fwname);
276 return ret;
278 return 0;
281 static int usb6fire_fw_fpga_upload(
282 struct usb_interface *intf, const char *fwname)
284 int ret;
285 int i;
286 struct usb_device *device = interface_to_usbdev(intf);
287 u8 *buffer = kmalloc(FPGA_BUFSIZE, GFP_KERNEL);
288 const char *c;
289 const char *end;
290 const struct firmware *fw;
292 if (!buffer)
293 return -ENOMEM;
295 ret = request_firmware(&fw, fwname, &device->dev);
296 if (ret < 0) {
297 snd_printk(KERN_ERR PREFIX "unable to get fpga firmware %s.\n",
298 fwname);
299 kfree(buffer);
300 return -EIO;
303 c = fw->data;
304 end = fw->data + fw->size;
306 ret = usb6fire_fw_ezusb_write(device, 8, 0, NULL, 0);
307 if (ret < 0) {
308 kfree(buffer);
309 release_firmware(fw);
310 snd_printk(KERN_ERR PREFIX "unable to upload fpga firmware: "
311 "begin urb.\n");
312 return ret;
315 while (c != end) {
316 for (i = 0; c != end && i < FPGA_BUFSIZE; i++, c++)
317 buffer[i] = byte_rev_table[(u8) *c];
319 ret = usb6fire_fw_fpga_write(device, buffer, i);
320 if (ret < 0) {
321 release_firmware(fw);
322 kfree(buffer);
323 snd_printk(KERN_ERR PREFIX "unable to upload fpga "
324 "firmware: fw urb.\n");
325 return ret;
328 release_firmware(fw);
329 kfree(buffer);
331 ret = usb6fire_fw_ezusb_write(device, 9, 0, NULL, 0);
332 if (ret < 0) {
333 snd_printk(KERN_ERR PREFIX "unable to upload fpga firmware: "
334 "end urb.\n");
335 return ret;
337 return 0;
340 /* check, if the firmware version the devices has currently loaded
341 * is known by this driver. 'version' needs to have 4 bytes version
342 * info data. */
343 static int usb6fire_fw_check(u8 *version)
345 int i;
347 for (i = 0; i < ARRAY_SIZE(known_fw_versions); i++)
348 if (!memcmp(version, known_fw_versions + i, 4))
349 return 0;
351 snd_printk(KERN_ERR PREFIX "invalid fimware version in device: "
352 "%02x %02x %02x %02x. "
353 "please reconnect to power. if this failure "
354 "still happens, check your firmware installation.",
355 version[0], version[1], version[2], version[3]);
356 return -EINVAL;
359 int usb6fire_fw_init(struct usb_interface *intf)
361 int i;
362 int ret;
363 struct usb_device *device = interface_to_usbdev(intf);
364 /* buffer: 8 receiving bytes from device and
365 * sizeof(EP_W_MAX_PACKET_SIZE) bytes for non-const copy */
366 u8 buffer[12];
368 ret = usb6fire_fw_ezusb_read(device, 1, 0, buffer, 8);
369 if (ret < 0) {
370 snd_printk(KERN_ERR PREFIX "unable to receive device "
371 "firmware state.\n");
372 return ret;
374 if (buffer[0] != 0xeb || buffer[1] != 0xaa || buffer[2] != 0x55) {
375 snd_printk(KERN_ERR PREFIX "unknown device firmware state "
376 "received from device: ");
377 for (i = 0; i < 8; i++)
378 snd_printk("%02x ", buffer[i]);
379 snd_printk("\n");
380 return -EIO;
382 /* do we need fpga loader ezusb firmware? */
383 if (buffer[3] == 0x01) {
384 ret = usb6fire_fw_ezusb_upload(intf,
385 "6fire/dmx6firel2.ihx", 0, NULL, 0);
386 if (ret < 0)
387 return ret;
388 return FW_NOT_READY;
390 /* do we need fpga firmware and application ezusb firmware? */
391 else if (buffer[3] == 0x02) {
392 ret = usb6fire_fw_check(buffer + 4);
393 if (ret < 0)
394 return ret;
395 ret = usb6fire_fw_fpga_upload(intf, "6fire/dmx6firecf.bin");
396 if (ret < 0)
397 return ret;
398 memcpy(buffer, ep_w_max_packet_size,
399 sizeof(ep_w_max_packet_size));
400 ret = usb6fire_fw_ezusb_upload(intf, "6fire/dmx6fireap.ihx",
401 0x0003, buffer, sizeof(ep_w_max_packet_size));
402 if (ret < 0)
403 return ret;
404 return FW_NOT_READY;
406 /* all fw loaded? */
407 else if (buffer[3] == 0x03)
408 return usb6fire_fw_check(buffer + 4);
409 /* unknown data? */
410 else {
411 snd_printk(KERN_ERR PREFIX "unknown device firmware state "
412 "received from device: ");
413 for (i = 0; i < 8; i++)
414 snd_printk("%02x ", buffer[i]);
415 snd_printk("\n");
416 return -EIO;
418 return 0;