2 * Linux driver for TerraTec DMX 6Fire USB
6 * Author: Torsten Schenk <torsten.schenk@zoho.com>
7 * Created: Jan 01, 2011
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>
24 MODULE_FIRMWARE("6fire/dmx6firel2.ihx");
25 MODULE_FIRMWARE("6fire/dmx6fireap.ihx");
26 MODULE_FIRMWARE("6fire/dmx6firecf.bin");
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 }
53 char error
; /* true if an error occurred parsing this record */
55 u8 max_len
; /* maximum record length in whole ihex */
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')
67 else if (n
>= 'A' && n
<= 'F')
68 return n
- ('A' - 10);
69 else if (n
>= 'a' && n
<= 'f')
70 return n
- ('a' - 10);
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]);
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
)
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
] != ':')
98 if (record
->txt_offset
== record
->txt_length
)
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;
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;
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
);
133 record
->error
= true;
137 if (type
== 1 || !record
->len
) /* eof */
142 record
->error
= true;
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;
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
);
160 record
->txt_offset
= 0;
164 static int usb6fire_fw_ezusb_write(struct usb_device
*device
,
165 int type
, int value
, char *data
, int len
)
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
);
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
,
192 static int usb6fire_fw_fpga_write(struct usb_device
*device
,
198 ret
= usb_bulk_msg(device
, usb_sndbulkpipe(device
, FPGA_EP
), data
, len
,
202 else if (actual_len
!= len
)
207 static int usb6fire_fw_ezusb_upload(
208 struct usb_interface
*intf
, const char *fwname
,
209 unsigned int postaddr
, u8
*postdata
, unsigned int postlen
)
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
),
221 ret
= request_firmware(&fw
, fwname
, &device
->dev
);
224 snd_printk(KERN_ERR PREFIX
"error requesting ezusb "
225 "firmware %s.\n", fwname
);
228 ret
= usb6fire_fw_ihex_init(fw
, rec
);
231 release_firmware(fw
);
232 snd_printk(KERN_ERR PREFIX
"error validating ezusb "
233 "firmware %s.\n", fwname
);
236 /* upload firmware image */
237 data
= 0x01; /* stop ezusb cpu */
238 ret
= usb6fire_fw_ezusb_write(device
, 0xa0, 0xe600, &data
, 1);
241 release_firmware(fw
);
242 snd_printk(KERN_ERR PREFIX
"unable to upload ezusb "
243 "firmware %s: begin message.\n", fwname
);
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
);
252 release_firmware(fw
);
253 snd_printk(KERN_ERR PREFIX
"unable to upload ezusb "
254 "firmware %s: data urb.\n", fwname
);
259 release_firmware(fw
);
261 if (postdata
) { /* write data after firmware has been uploaded */
262 ret
= usb6fire_fw_ezusb_write(device
, 0xa0, postaddr
,
265 snd_printk(KERN_ERR PREFIX
"unable to upload ezusb "
266 "firmware %s: post urb.\n", fwname
);
271 data
= 0x00; /* resume ezusb cpu */
272 ret
= usb6fire_fw_ezusb_write(device
, 0xa0, 0xe600, &data
, 1);
274 snd_printk(KERN_ERR PREFIX
"unable to upload ezusb "
275 "firmware %s: end message.\n", fwname
);
281 static int usb6fire_fw_fpga_upload(
282 struct usb_interface
*intf
, const char *fwname
)
286 struct usb_device
*device
= interface_to_usbdev(intf
);
287 u8
*buffer
= kmalloc(FPGA_BUFSIZE
, GFP_KERNEL
);
290 const struct firmware
*fw
;
295 ret
= request_firmware(&fw
, fwname
, &device
->dev
);
297 snd_printk(KERN_ERR PREFIX
"unable to get fpga firmware %s.\n",
304 end
= fw
->data
+ fw
->size
;
306 ret
= usb6fire_fw_ezusb_write(device
, 8, 0, NULL
, 0);
309 release_firmware(fw
);
310 snd_printk(KERN_ERR PREFIX
"unable to upload fpga firmware: "
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
);
321 release_firmware(fw
);
323 snd_printk(KERN_ERR PREFIX
"unable to upload fpga "
324 "firmware: fw urb.\n");
328 release_firmware(fw
);
331 ret
= usb6fire_fw_ezusb_write(device
, 9, 0, NULL
, 0);
333 snd_printk(KERN_ERR PREFIX
"unable to upload fpga firmware: "
340 /* check, if the firmware version the devices has currently loaded
341 * is known by this driver. 'version' needs to have 4 bytes version
343 static int usb6fire_fw_check(u8
*version
)
347 for (i
= 0; i
< ARRAY_SIZE(known_fw_versions
); i
++)
348 if (!memcmp(version
, known_fw_versions
+ i
, 4))
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]);
359 int usb6fire_fw_init(struct usb_interface
*intf
)
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 */
368 ret
= usb6fire_fw_ezusb_read(device
, 1, 0, buffer
, 8);
370 snd_printk(KERN_ERR PREFIX
"unable to receive device "
371 "firmware state.\n");
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
]);
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);
390 /* do we need fpga firmware and application ezusb firmware? */
391 else if (buffer
[3] == 0x02) {
392 ret
= usb6fire_fw_check(buffer
+ 4);
395 ret
= usb6fire_fw_fpga_upload(intf
, "6fire/dmx6firecf.bin");
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
));
407 else if (buffer
[3] == 0x03)
408 return usb6fire_fw_check(buffer
+ 4);
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
]);