blob: e720035fbd1eaf42af04272141e33e129258241f [file] [log] [blame]
Torsten Schenkc6d43ba2011-01-24 18:45:30 +01001/*
2 * Linux driver for TerraTec DMX 6Fire USB
3 *
4 * Firmware loader
5 *
Torsten Schenkc6d43ba2011-01-24 18:45:30 +01006 * Author: Torsten Schenk <torsten.schenk@zoho.com>
7 * Created: Jan 01, 2011
8 * Version: 0.3.0
9 * Copyright: (C) Torsten Schenk
10 *
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.
15 */
16
17#include <linux/firmware.h>
18
19#include "firmware.h"
20#include "chip.h"
21
22MODULE_FIRMWARE("6fire/dmx6firel2.ihx");
23MODULE_FIRMWARE("6fire/dmx6fireap.ihx");
24MODULE_FIRMWARE("6fire/dmx6firecf.bin");
25
26enum {
27 FPGA_BUFSIZE = 512, FPGA_EP = 2
28};
29
30static const u8 BIT_REVERSE_TABLE[256] = {
31 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, 0x10, 0x90, 0x50,
32 0xd0, 0x30, 0xb0, 0x70, 0xf0, 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8,
33 0x68, 0xe8, 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8, 0x04,
34 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4, 0x14, 0x94, 0x54, 0xd4,
35 0x34, 0xb4, 0x74, 0xf4, 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c,
36 0xec, 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc, 0x02, 0x82,
37 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2, 0x12, 0x92, 0x52, 0xd2, 0x32,
38 0xb2, 0x72, 0xf2, 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
39 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa, 0x06, 0x86, 0x46,
40 0xc6, 0x26, 0xa6, 0x66, 0xe6, 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6,
41 0x76, 0xf6, 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee, 0x1e,
42 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe, 0x01, 0x81, 0x41, 0xc1,
43 0x21, 0xa1, 0x61, 0xe1, 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71,
44 0xf1, 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9, 0x19, 0x99,
45 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9, 0x05, 0x85, 0x45, 0xc5, 0x25,
46 0xa5, 0x65, 0xe5, 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
47 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed, 0x1d, 0x9d, 0x5d,
48 0xdd, 0x3d, 0xbd, 0x7d, 0xfd, 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3,
49 0x63, 0xe3, 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3, 0x0b,
50 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb, 0x1b, 0x9b, 0x5b, 0xdb,
51 0x3b, 0xbb, 0x7b, 0xfb, 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67,
52 0xe7, 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7, 0x0f, 0x8f,
53 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, 0x1f, 0x9f, 0x5f, 0xdf, 0x3f,
54 0xbf, 0x7f, 0xff };
55
56/*
57 * wMaxPacketSize of pcm endpoints.
58 * keep synced with rates_in_packet_size and rates_out_packet_size in pcm.c
59 * fpp: frames per isopacket
60 *
61 * CAUTION: keep sizeof <= buffer[] in usb6fire_fw_init
62 */
63static const u8 ep_w_max_packet_size[] = {
64 0xe4, 0x00, 0xe4, 0x00, /* alt 1: 228 EP2 and EP6 (7 fpp) */
65 0xa4, 0x01, 0xa4, 0x01, /* alt 2: 420 EP2 and EP6 (13 fpp)*/
66 0x94, 0x01, 0x5c, 0x02 /* alt 3: 404 EP2 and 604 EP6 (25 fpp) */
67};
68
Torsten Schenkb84610b2011-04-04 11:49:57 +020069static const u8 known_fw_versions[][4] = {
70 { 0x03, 0x01, 0x0b, 0x00 }
71};
72
Torsten Schenkc6d43ba2011-01-24 18:45:30 +010073struct ihex_record {
74 u16 address;
75 u8 len;
76 u8 data[256];
77 char error; /* true if an error occured parsing this record */
78
79 u8 max_len; /* maximum record length in whole ihex */
80
81 /* private */
82 const char *txt_data;
83 unsigned int txt_length;
84 unsigned int txt_offset; /* current position in txt_data */
85};
86
87static u8 usb6fire_fw_ihex_nibble(const u8 n)
88{
89 if (n >= '0' && n <= '9')
90 return n - '0';
91 else if (n >= 'A' && n <= 'F')
92 return n - ('A' - 10);
93 else if (n >= 'a' && n <= 'f')
94 return n - ('a' - 10);
95 return 0;
96}
97
98static u8 usb6fire_fw_ihex_hex(const u8 *data, u8 *crc)
99{
100 u8 val = (usb6fire_fw_ihex_nibble(data[0]) << 4) |
101 usb6fire_fw_ihex_nibble(data[1]);
102 *crc += val;
103 return val;
104}
105
106/*
107 * returns true if record is available, false otherwise.
108 * iff an error occured, false will be returned and record->error will be true.
109 */
110static bool usb6fire_fw_ihex_next_record(struct ihex_record *record)
111{
112 u8 crc = 0;
113 u8 type;
114 int i;
115
116 record->error = false;
117
118 /* find begin of record (marked by a colon) */
119 while (record->txt_offset < record->txt_length
120 && record->txt_data[record->txt_offset] != ':')
121 record->txt_offset++;
122 if (record->txt_offset == record->txt_length)
123 return false;
124
125 /* number of characters needed for len, addr and type entries */
126 record->txt_offset++;
127 if (record->txt_offset + 8 > record->txt_length) {
128 record->error = true;
129 return false;
130 }
131
132 record->len = usb6fire_fw_ihex_hex(record->txt_data +
133 record->txt_offset, &crc);
134 record->txt_offset += 2;
135 record->address = usb6fire_fw_ihex_hex(record->txt_data +
136 record->txt_offset, &crc) << 8;
137 record->txt_offset += 2;
138 record->address |= usb6fire_fw_ihex_hex(record->txt_data +
139 record->txt_offset, &crc);
140 record->txt_offset += 2;
141 type = usb6fire_fw_ihex_hex(record->txt_data +
142 record->txt_offset, &crc);
143 record->txt_offset += 2;
144
145 /* number of characters needed for data and crc entries */
146 if (record->txt_offset + 2 * (record->len + 1) > record->txt_length) {
147 record->error = true;
148 return false;
149 }
150 for (i = 0; i < record->len; i++) {
151 record->data[i] = usb6fire_fw_ihex_hex(record->txt_data
152 + record->txt_offset, &crc);
153 record->txt_offset += 2;
154 }
155 usb6fire_fw_ihex_hex(record->txt_data + record->txt_offset, &crc);
156 if (crc) {
157 record->error = true;
158 return false;
159 }
160
161 if (type == 1 || !record->len) /* eof */
162 return false;
163 else if (type == 0)
164 return true;
165 else {
166 record->error = true;
167 return false;
168 }
169}
170
171static int usb6fire_fw_ihex_init(const struct firmware *fw,
172 struct ihex_record *record)
173{
174 record->txt_data = fw->data;
175 record->txt_length = fw->size;
176 record->txt_offset = 0;
177 record->max_len = 0;
178 /* read all records, if loop ends, record->error indicates,
179 * whether ihex is valid. */
180 while (usb6fire_fw_ihex_next_record(record))
181 record->max_len = max(record->len, record->max_len);
182 if (record->error)
183 return -EINVAL;
184 record->txt_offset = 0;
185 return 0;
186}
187
188static int usb6fire_fw_ezusb_write(struct usb_device *device,
189 int type, int value, char *data, int len)
190{
191 int ret;
192
193 ret = usb_control_msg(device, usb_sndctrlpipe(device, 0), type,
194 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
195 value, 0, data, len, HZ);
196 if (ret < 0)
197 return ret;
198 else if (ret != len)
199 return -EIO;
200 return 0;
201}
202
203static int usb6fire_fw_ezusb_read(struct usb_device *device,
204 int type, int value, char *data, int len)
205{
206 int ret = usb_control_msg(device, usb_rcvctrlpipe(device, 0), type,
207 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, value,
208 0, data, len, HZ);
209 if (ret < 0)
210 return ret;
211 else if (ret != len)
212 return -EIO;
213 return 0;
214}
215
216static int usb6fire_fw_fpga_write(struct usb_device *device,
217 char *data, int len)
218{
219 int actual_len;
220 int ret;
221
222 ret = usb_bulk_msg(device, usb_sndbulkpipe(device, FPGA_EP), data, len,
223 &actual_len, HZ);
224 if (ret < 0)
225 return ret;
226 else if (actual_len != len)
227 return -EIO;
228 return 0;
229}
230
231static int usb6fire_fw_ezusb_upload(
232 struct usb_interface *intf, const char *fwname,
233 unsigned int postaddr, u8 *postdata, unsigned int postlen)
234{
235 int ret;
236 u8 data;
237 struct usb_device *device = interface_to_usbdev(intf);
238 const struct firmware *fw = 0;
239 struct ihex_record *rec = kmalloc(sizeof(struct ihex_record),
240 GFP_KERNEL);
241
242 if (!rec)
243 return -ENOMEM;
244
245 ret = request_firmware(&fw, fwname, &device->dev);
246 if (ret < 0) {
247 kfree(rec);
248 snd_printk(KERN_ERR PREFIX "error requesting ezusb "
249 "firmware %s.\n", fwname);
250 return ret;
251 }
252 ret = usb6fire_fw_ihex_init(fw, rec);
253 if (ret < 0) {
254 kfree(rec);
255 snd_printk(KERN_ERR PREFIX "error validating ezusb "
256 "firmware %s.\n", fwname);
257 return ret;
258 }
259 /* upload firmware image */
260 data = 0x01; /* stop ezusb cpu */
261 ret = usb6fire_fw_ezusb_write(device, 0xa0, 0xe600, &data, 1);
262 if (ret < 0) {
263 kfree(rec);
264 release_firmware(fw);
265 snd_printk(KERN_ERR PREFIX "unable to upload ezusb "
266 "firmware %s: begin message.\n", fwname);
267 return ret;
268 }
269
270 while (usb6fire_fw_ihex_next_record(rec)) { /* write firmware */
271 ret = usb6fire_fw_ezusb_write(device, 0xa0, rec->address,
272 rec->data, rec->len);
273 if (ret < 0) {
274 kfree(rec);
275 release_firmware(fw);
276 snd_printk(KERN_ERR PREFIX "unable to upload ezusb "
277 "firmware %s: data urb.\n", fwname);
278 return ret;
279 }
280 }
281
282 release_firmware(fw);
283 kfree(rec);
284 if (postdata) { /* write data after firmware has been uploaded */
285 ret = usb6fire_fw_ezusb_write(device, 0xa0, postaddr,
286 postdata, postlen);
287 if (ret < 0) {
288 snd_printk(KERN_ERR PREFIX "unable to upload ezusb "
289 "firmware %s: post urb.\n", fwname);
290 return ret;
291 }
292 }
293
294 data = 0x00; /* resume ezusb cpu */
295 ret = usb6fire_fw_ezusb_write(device, 0xa0, 0xe600, &data, 1);
296 if (ret < 0) {
297 release_firmware(fw);
298 snd_printk(KERN_ERR PREFIX "unable to upload ezusb "
299 "firmware %s: end message.\n", fwname);
300 return ret;
301 }
302 return 0;
303}
304
305static int usb6fire_fw_fpga_upload(
306 struct usb_interface *intf, const char *fwname)
307{
308 int ret;
309 int i;
310 struct usb_device *device = interface_to_usbdev(intf);
311 u8 *buffer = kmalloc(FPGA_BUFSIZE, GFP_KERNEL);
312 const char *c;
313 const char *end;
314 const struct firmware *fw;
315
316 if (!buffer)
317 return -ENOMEM;
318
319 ret = request_firmware(&fw, fwname, &device->dev);
320 if (ret < 0) {
321 snd_printk(KERN_ERR PREFIX "unable to get fpga firmware %s.\n",
322 fwname);
323 kfree(buffer);
324 return -EIO;
325 }
326
327 c = fw->data;
328 end = fw->data + fw->size;
329
330 ret = usb6fire_fw_ezusb_write(device, 8, 0, NULL, 0);
331 if (ret < 0) {
332 kfree(buffer);
333 release_firmware(fw);
334 snd_printk(KERN_ERR PREFIX "unable to upload fpga firmware: "
335 "begin urb.\n");
336 return ret;
337 }
338
339 while (c != end) {
340 for (i = 0; c != end && i < FPGA_BUFSIZE; i++, c++)
341 buffer[i] = BIT_REVERSE_TABLE[(u8) *c];
342
343 ret = usb6fire_fw_fpga_write(device, buffer, i);
344 if (ret < 0) {
345 release_firmware(fw);
346 kfree(buffer);
347 snd_printk(KERN_ERR PREFIX "unable to upload fpga "
348 "firmware: fw urb.\n");
349 return ret;
350 }
351 }
352 release_firmware(fw);
353 kfree(buffer);
354
355 ret = usb6fire_fw_ezusb_write(device, 9, 0, NULL, 0);
356 if (ret < 0) {
357 snd_printk(KERN_ERR PREFIX "unable to upload fpga firmware: "
358 "end urb.\n");
359 return ret;
360 }
361 return 0;
362}
363
Torsten Schenkb84610b2011-04-04 11:49:57 +0200364/* check, if the firmware version the devices has currently loaded
365 * is known by this driver. 'version' needs to have 4 bytes version
366 * info data. */
367static int usb6fire_fw_check(u8 *version)
368{
369 int i;
370
371 for (i = 0; i < ARRAY_SIZE(known_fw_versions); i++)
372 if (!memcmp(version, known_fw_versions + i, 4))
373 return 0;
374
375 snd_printk(KERN_ERR PREFIX "invalid fimware version in device: "
376 "%02x %02x %02x %02x. "
377 "please reconnect to power. if this failure "
378 "still happens, check your firmware installation.",
379 version[0], version[1], version[2], version[3]);
380 return -EINVAL;
381}
382
Torsten Schenkc6d43ba2011-01-24 18:45:30 +0100383int usb6fire_fw_init(struct usb_interface *intf)
384{
385 int i;
386 int ret;
387 struct usb_device *device = interface_to_usbdev(intf);
388 /* buffer: 8 receiving bytes from device and
389 * sizeof(EP_W_MAX_PACKET_SIZE) bytes for non-const copy */
390 u8 buffer[12];
391
392 ret = usb6fire_fw_ezusb_read(device, 1, 0, buffer, 8);
393 if (ret < 0) {
394 snd_printk(KERN_ERR PREFIX "unable to receive device "
395 "firmware state.\n");
396 return ret;
397 }
Torsten Schenkb84610b2011-04-04 11:49:57 +0200398 if (buffer[0] != 0xeb || buffer[1] != 0xaa || buffer[2] != 0x55) {
Torsten Schenkc6d43ba2011-01-24 18:45:30 +0100399 snd_printk(KERN_ERR PREFIX "unknown device firmware state "
400 "received from device: ");
401 for (i = 0; i < 8; i++)
402 snd_printk("%02x ", buffer[i]);
403 snd_printk("\n");
404 return -EIO;
405 }
406 /* do we need fpga loader ezusb firmware? */
Torsten Schenkb84610b2011-04-04 11:49:57 +0200407 if (buffer[3] == 0x01) {
Torsten Schenkc6d43ba2011-01-24 18:45:30 +0100408 ret = usb6fire_fw_ezusb_upload(intf,
409 "6fire/dmx6firel2.ihx", 0, NULL, 0);
410 if (ret < 0)
411 return ret;
412 return FW_NOT_READY;
413 }
414 /* do we need fpga firmware and application ezusb firmware? */
Torsten Schenkb84610b2011-04-04 11:49:57 +0200415 else if (buffer[3] == 0x02) {
416 ret = usb6fire_fw_check(buffer + 4);
417 if (ret < 0)
418 return ret;
Torsten Schenkc6d43ba2011-01-24 18:45:30 +0100419 ret = usb6fire_fw_fpga_upload(intf, "6fire/dmx6firecf.bin");
420 if (ret < 0)
421 return ret;
422 memcpy(buffer, ep_w_max_packet_size,
423 sizeof(ep_w_max_packet_size));
424 ret = usb6fire_fw_ezusb_upload(intf, "6fire/dmx6fireap.ihx",
425 0x0003, buffer, sizeof(ep_w_max_packet_size));
426 if (ret < 0)
427 return ret;
428 return FW_NOT_READY;
429 }
430 /* all fw loaded? */
Torsten Schenkb84610b2011-04-04 11:49:57 +0200431 else if (buffer[3] == 0x03)
432 return usb6fire_fw_check(buffer + 4);
Torsten Schenkc6d43ba2011-01-24 18:45:30 +0100433 /* unknown data? */
434 else {
435 snd_printk(KERN_ERR PREFIX "unknown device firmware state "
436 "received from device: ");
437 for (i = 0; i < 8; i++)
438 snd_printk("%02x ", buffer[i]);
439 snd_printk("\n");
440 return -EIO;
441 }
442 return 0;
443}
444