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>
20 #include <linux/kernel.h>
25 MODULE_FIRMWARE("6fire/dmx6firel2.ihx");
26 MODULE_FIRMWARE("6fire/dmx6fireap.ihx");
27 MODULE_FIRMWARE("6fire/dmx6firecf.bin");
30 FPGA_BUFSIZE
= 512, FPGA_EP
= 2
34 * wMaxPacketSize of pcm endpoints.
35 * keep synced with rates_in_packet_size and rates_out_packet_size in pcm.c
36 * fpp: frames per isopacket
38 * CAUTION: keep sizeof <= buffer[] in usb6fire_fw_init
40 static const u8 ep_w_max_packet_size
[] = {
41 0xe4, 0x00, 0xe4, 0x00, /* alt 1: 228 EP2 and EP6 (7 fpp) */
42 0xa4, 0x01, 0xa4, 0x01, /* alt 2: 420 EP2 and EP6 (13 fpp)*/
43 0x94, 0x01, 0x5c, 0x02 /* alt 3: 404 EP2 and 604 EP6 (25 fpp) */
46 static const u8 known_fw_versions
[][4] = {
47 { 0x03, 0x01, 0x0b, 0x00 }
54 char error
; /* true if an error occurred parsing this record */
56 u8 max_len
; /* maximum record length in whole ihex */
60 unsigned int txt_length
;
61 unsigned int txt_offset
; /* current position in txt_data */
64 static u8
usb6fire_fw_ihex_hex(const u8
*data
, u8
*crc
)
69 hval
= hex_to_bin(data
[0]);
73 hval
= hex_to_bin(data
[1]);
82 * returns true if record is available, false otherwise.
83 * iff an error occurred, false will be returned and record->error will be true.
85 static bool usb6fire_fw_ihex_next_record(struct ihex_record
*record
)
91 record
->error
= false;
93 /* find begin of record (marked by a colon) */
94 while (record
->txt_offset
< record
->txt_length
95 && record
->txt_data
[record
->txt_offset
] != ':')
97 if (record
->txt_offset
== record
->txt_length
)
100 /* number of characters needed for len, addr and type entries */
101 record
->txt_offset
++;
102 if (record
->txt_offset
+ 8 > record
->txt_length
) {
103 record
->error
= true;
107 record
->len
= usb6fire_fw_ihex_hex(record
->txt_data
+
108 record
->txt_offset
, &crc
);
109 record
->txt_offset
+= 2;
110 record
->address
= usb6fire_fw_ihex_hex(record
->txt_data
+
111 record
->txt_offset
, &crc
) << 8;
112 record
->txt_offset
+= 2;
113 record
->address
|= usb6fire_fw_ihex_hex(record
->txt_data
+
114 record
->txt_offset
, &crc
);
115 record
->txt_offset
+= 2;
116 type
= usb6fire_fw_ihex_hex(record
->txt_data
+
117 record
->txt_offset
, &crc
);
118 record
->txt_offset
+= 2;
120 /* number of characters needed for data and crc entries */
121 if (record
->txt_offset
+ 2 * (record
->len
+ 1) > record
->txt_length
) {
122 record
->error
= true;
125 for (i
= 0; i
< record
->len
; i
++) {
126 record
->data
[i
] = usb6fire_fw_ihex_hex(record
->txt_data
127 + record
->txt_offset
, &crc
);
128 record
->txt_offset
+= 2;
130 usb6fire_fw_ihex_hex(record
->txt_data
+ record
->txt_offset
, &crc
);
132 record
->error
= true;
136 if (type
== 1 || !record
->len
) /* eof */
141 record
->error
= true;
146 static int usb6fire_fw_ihex_init(const struct firmware
*fw
,
147 struct ihex_record
*record
)
149 record
->txt_data
= fw
->data
;
150 record
->txt_length
= fw
->size
;
151 record
->txt_offset
= 0;
153 /* read all records, if loop ends, record->error indicates,
154 * whether ihex is valid. */
155 while (usb6fire_fw_ihex_next_record(record
))
156 record
->max_len
= max(record
->len
, record
->max_len
);
159 record
->txt_offset
= 0;
163 static int usb6fire_fw_ezusb_write(struct usb_device
*device
,
164 int type
, int value
, char *data
, int len
)
168 ret
= usb_control_msg(device
, usb_sndctrlpipe(device
, 0), type
,
169 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
170 value
, 0, data
, len
, HZ
);
178 static int usb6fire_fw_ezusb_read(struct usb_device
*device
,
179 int type
, int value
, char *data
, int len
)
181 int ret
= usb_control_msg(device
, usb_rcvctrlpipe(device
, 0), type
,
182 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
, value
,
191 static int usb6fire_fw_fpga_write(struct usb_device
*device
,
197 ret
= usb_bulk_msg(device
, usb_sndbulkpipe(device
, FPGA_EP
), data
, len
,
201 else if (actual_len
!= len
)
206 static int usb6fire_fw_ezusb_upload(
207 struct usb_interface
*intf
, const char *fwname
,
208 unsigned int postaddr
, u8
*postdata
, unsigned int postlen
)
212 struct usb_device
*device
= interface_to_usbdev(intf
);
213 const struct firmware
*fw
= 0;
214 struct ihex_record
*rec
= kmalloc(sizeof(struct ihex_record
),
220 ret
= request_firmware(&fw
, fwname
, &device
->dev
);
223 snd_printk(KERN_ERR PREFIX
"error requesting ezusb "
224 "firmware %s.\n", fwname
);
227 ret
= usb6fire_fw_ihex_init(fw
, rec
);
230 release_firmware(fw
);
231 snd_printk(KERN_ERR PREFIX
"error validating ezusb "
232 "firmware %s.\n", fwname
);
235 /* upload firmware image */
236 data
= 0x01; /* stop ezusb cpu */
237 ret
= usb6fire_fw_ezusb_write(device
, 0xa0, 0xe600, &data
, 1);
240 release_firmware(fw
);
241 snd_printk(KERN_ERR PREFIX
"unable to upload ezusb "
242 "firmware %s: begin message.\n", fwname
);
246 while (usb6fire_fw_ihex_next_record(rec
)) { /* write firmware */
247 ret
= usb6fire_fw_ezusb_write(device
, 0xa0, rec
->address
,
248 rec
->data
, rec
->len
);
251 release_firmware(fw
);
252 snd_printk(KERN_ERR PREFIX
"unable to upload ezusb "
253 "firmware %s: data urb.\n", fwname
);
258 release_firmware(fw
);
260 if (postdata
) { /* write data after firmware has been uploaded */
261 ret
= usb6fire_fw_ezusb_write(device
, 0xa0, postaddr
,
264 snd_printk(KERN_ERR PREFIX
"unable to upload ezusb "
265 "firmware %s: post urb.\n", fwname
);
270 data
= 0x00; /* resume ezusb cpu */
271 ret
= usb6fire_fw_ezusb_write(device
, 0xa0, 0xe600, &data
, 1);
273 snd_printk(KERN_ERR PREFIX
"unable to upload ezusb "
274 "firmware %s: end message.\n", fwname
);
280 static int usb6fire_fw_fpga_upload(
281 struct usb_interface
*intf
, const char *fwname
)
285 struct usb_device
*device
= interface_to_usbdev(intf
);
286 u8
*buffer
= kmalloc(FPGA_BUFSIZE
, GFP_KERNEL
);
289 const struct firmware
*fw
;
294 ret
= request_firmware(&fw
, fwname
, &device
->dev
);
296 snd_printk(KERN_ERR PREFIX
"unable to get fpga firmware %s.\n",
303 end
= fw
->data
+ fw
->size
;
305 ret
= usb6fire_fw_ezusb_write(device
, 8, 0, NULL
, 0);
308 release_firmware(fw
);
309 snd_printk(KERN_ERR PREFIX
"unable to upload fpga firmware: "
315 for (i
= 0; c
!= end
&& i
< FPGA_BUFSIZE
; i
++, c
++)
316 buffer
[i
] = byte_rev_table
[(u8
) *c
];
318 ret
= usb6fire_fw_fpga_write(device
, buffer
, i
);
320 release_firmware(fw
);
322 snd_printk(KERN_ERR PREFIX
"unable to upload fpga "
323 "firmware: fw urb.\n");
327 release_firmware(fw
);
330 ret
= usb6fire_fw_ezusb_write(device
, 9, 0, NULL
, 0);
332 snd_printk(KERN_ERR PREFIX
"unable to upload fpga firmware: "
339 /* check, if the firmware version the devices has currently loaded
340 * is known by this driver. 'version' needs to have 4 bytes version
342 static int usb6fire_fw_check(u8
*version
)
346 for (i
= 0; i
< ARRAY_SIZE(known_fw_versions
); i
++)
347 if (!memcmp(version
, known_fw_versions
+ i
, 4))
350 snd_printk(KERN_ERR PREFIX
"invalid fimware version in device: "
351 "%02x %02x %02x %02x. "
352 "please reconnect to power. if this failure "
353 "still happens, check your firmware installation.",
354 version
[0], version
[1], version
[2], version
[3]);
358 int usb6fire_fw_init(struct usb_interface
*intf
)
362 struct usb_device
*device
= interface_to_usbdev(intf
);
363 /* buffer: 8 receiving bytes from device and
364 * sizeof(EP_W_MAX_PACKET_SIZE) bytes for non-const copy */
367 ret
= usb6fire_fw_ezusb_read(device
, 1, 0, buffer
, 8);
369 snd_printk(KERN_ERR PREFIX
"unable to receive device "
370 "firmware state.\n");
373 if (buffer
[0] != 0xeb || buffer
[1] != 0xaa || buffer
[2] != 0x55) {
374 snd_printk(KERN_ERR PREFIX
"unknown device firmware state "
375 "received from device: ");
376 for (i
= 0; i
< 8; i
++)
377 snd_printk("%02x ", buffer
[i
]);
381 /* do we need fpga loader ezusb firmware? */
382 if (buffer
[3] == 0x01) {
383 ret
= usb6fire_fw_ezusb_upload(intf
,
384 "6fire/dmx6firel2.ihx", 0, NULL
, 0);
389 /* do we need fpga firmware and application ezusb firmware? */
390 else if (buffer
[3] == 0x02) {
391 ret
= usb6fire_fw_check(buffer
+ 4);
394 ret
= usb6fire_fw_fpga_upload(intf
, "6fire/dmx6firecf.bin");
397 memcpy(buffer
, ep_w_max_packet_size
,
398 sizeof(ep_w_max_packet_size
));
399 ret
= usb6fire_fw_ezusb_upload(intf
, "6fire/dmx6fireap.ihx",
400 0x0003, buffer
, sizeof(ep_w_max_packet_size
));
406 else if (buffer
[3] == 0x03)
407 return usb6fire_fw_check(buffer
+ 4);
410 snd_printk(KERN_ERR PREFIX
"unknown device firmware state "
411 "received from device: ");
412 for (i
= 0; i
< 8; i
++)
413 snd_printk("%02x ", buffer
[i
]);