2 * Linux driver for TerraTec DMX 6Fire USB
6 * Author: Torsten Schenk <torsten.schenk@zoho.com>
7 * Created: Jan 01, 2011
8 * Copyright: (C) Torsten Schenk
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
16 #include <linux/firmware.h>
17 #include <linux/module.h>
18 #include <linux/bitrev.h>
19 #include <linux/kernel.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
[][2] = {
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_hex(const u8
*data
, u8
*crc
)
68 hval
= hex_to_bin(data
[0]);
72 hval
= hex_to_bin(data
[1]);
81 * returns true if record is available, false otherwise.
82 * iff an error occurred, false will be returned and record->error will be true.
84 static bool usb6fire_fw_ihex_next_record(struct ihex_record
*record
)
90 record
->error
= false;
92 /* find begin of record (marked by a colon) */
93 while (record
->txt_offset
< record
->txt_length
94 && record
->txt_data
[record
->txt_offset
] != ':')
96 if (record
->txt_offset
== record
->txt_length
)
99 /* number of characters needed for len, addr and type entries */
100 record
->txt_offset
++;
101 if (record
->txt_offset
+ 8 > record
->txt_length
) {
102 record
->error
= true;
106 record
->len
= usb6fire_fw_ihex_hex(record
->txt_data
+
107 record
->txt_offset
, &crc
);
108 record
->txt_offset
+= 2;
109 record
->address
= usb6fire_fw_ihex_hex(record
->txt_data
+
110 record
->txt_offset
, &crc
) << 8;
111 record
->txt_offset
+= 2;
112 record
->address
|= usb6fire_fw_ihex_hex(record
->txt_data
+
113 record
->txt_offset
, &crc
);
114 record
->txt_offset
+= 2;
115 type
= usb6fire_fw_ihex_hex(record
->txt_data
+
116 record
->txt_offset
, &crc
);
117 record
->txt_offset
+= 2;
119 /* number of characters needed for data and crc entries */
120 if (record
->txt_offset
+ 2 * (record
->len
+ 1) > record
->txt_length
) {
121 record
->error
= true;
124 for (i
= 0; i
< record
->len
; i
++) {
125 record
->data
[i
] = usb6fire_fw_ihex_hex(record
->txt_data
126 + record
->txt_offset
, &crc
);
127 record
->txt_offset
+= 2;
129 usb6fire_fw_ihex_hex(record
->txt_data
+ record
->txt_offset
, &crc
);
131 record
->error
= true;
135 if (type
== 1 || !record
->len
) /* eof */
140 record
->error
= true;
145 static int usb6fire_fw_ihex_init(const struct firmware
*fw
,
146 struct ihex_record
*record
)
148 record
->txt_data
= fw
->data
;
149 record
->txt_length
= fw
->size
;
150 record
->txt_offset
= 0;
152 /* read all records, if loop ends, record->error indicates,
153 * whether ihex is valid. */
154 while (usb6fire_fw_ihex_next_record(record
))
155 record
->max_len
= max(record
->len
, record
->max_len
);
158 record
->txt_offset
= 0;
162 static int usb6fire_fw_ezusb_write(struct usb_device
*device
,
163 int type
, int value
, char *data
, int len
)
167 ret
= usb_control_msg(device
, usb_sndctrlpipe(device
, 0), type
,
168 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
169 value
, 0, data
, len
, HZ
);
177 static int usb6fire_fw_ezusb_read(struct usb_device
*device
,
178 int type
, int value
, char *data
, int len
)
180 int ret
= usb_control_msg(device
, usb_rcvctrlpipe(device
, 0), type
,
181 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
, value
,
190 static int usb6fire_fw_fpga_write(struct usb_device
*device
,
196 ret
= usb_bulk_msg(device
, usb_sndbulkpipe(device
, FPGA_EP
), data
, len
,
200 else if (actual_len
!= len
)
205 static int usb6fire_fw_ezusb_upload(
206 struct usb_interface
*intf
, const char *fwname
,
207 unsigned int postaddr
, u8
*postdata
, unsigned int postlen
)
211 struct usb_device
*device
= interface_to_usbdev(intf
);
212 const struct firmware
*fw
= NULL
;
213 struct ihex_record
*rec
= kmalloc(sizeof(struct ihex_record
),
219 ret
= request_firmware(&fw
, fwname
, &device
->dev
);
222 snd_printk(KERN_ERR PREFIX
"error requesting ezusb "
223 "firmware %s.\n", fwname
);
226 ret
= usb6fire_fw_ihex_init(fw
, rec
);
229 release_firmware(fw
);
230 snd_printk(KERN_ERR PREFIX
"error validating ezusb "
231 "firmware %s.\n", fwname
);
234 /* upload firmware image */
235 data
= 0x01; /* stop ezusb cpu */
236 ret
= usb6fire_fw_ezusb_write(device
, 0xa0, 0xe600, &data
, 1);
239 release_firmware(fw
);
240 snd_printk(KERN_ERR PREFIX
"unable to upload ezusb "
241 "firmware %s: begin message.\n", fwname
);
245 while (usb6fire_fw_ihex_next_record(rec
)) { /* write firmware */
246 ret
= usb6fire_fw_ezusb_write(device
, 0xa0, rec
->address
,
247 rec
->data
, rec
->len
);
250 release_firmware(fw
);
251 snd_printk(KERN_ERR PREFIX
"unable to upload ezusb "
252 "firmware %s: data urb.\n", fwname
);
257 release_firmware(fw
);
259 if (postdata
) { /* write data after firmware has been uploaded */
260 ret
= usb6fire_fw_ezusb_write(device
, 0xa0, postaddr
,
263 snd_printk(KERN_ERR PREFIX
"unable to upload ezusb "
264 "firmware %s: post urb.\n", fwname
);
269 data
= 0x00; /* resume ezusb cpu */
270 ret
= usb6fire_fw_ezusb_write(device
, 0xa0, 0xe600, &data
, 1);
272 snd_printk(KERN_ERR PREFIX
"unable to upload ezusb "
273 "firmware %s: end message.\n", fwname
);
279 static int usb6fire_fw_fpga_upload(
280 struct usb_interface
*intf
, const char *fwname
)
284 struct usb_device
*device
= interface_to_usbdev(intf
);
285 u8
*buffer
= kmalloc(FPGA_BUFSIZE
, GFP_KERNEL
);
288 const struct firmware
*fw
;
293 ret
= request_firmware(&fw
, fwname
, &device
->dev
);
295 snd_printk(KERN_ERR PREFIX
"unable to get fpga firmware %s.\n",
302 end
= fw
->data
+ fw
->size
;
304 ret
= usb6fire_fw_ezusb_write(device
, 8, 0, NULL
, 0);
307 release_firmware(fw
);
308 snd_printk(KERN_ERR PREFIX
"unable to upload fpga firmware: "
314 for (i
= 0; c
!= end
&& i
< FPGA_BUFSIZE
; i
++, c
++)
315 buffer
[i
] = byte_rev_table
[(u8
) *c
];
317 ret
= usb6fire_fw_fpga_write(device
, buffer
, i
);
319 release_firmware(fw
);
321 snd_printk(KERN_ERR PREFIX
"unable to upload fpga "
322 "firmware: fw urb.\n");
326 release_firmware(fw
);
329 ret
= usb6fire_fw_ezusb_write(device
, 9, 0, NULL
, 0);
331 snd_printk(KERN_ERR PREFIX
"unable to upload fpga firmware: "
338 /* check, if the firmware version the devices has currently loaded
339 * is known by this driver. 'version' needs to have 4 bytes version
341 static int usb6fire_fw_check(u8
*version
)
345 for (i
= 0; i
< ARRAY_SIZE(known_fw_versions
); i
++)
346 if (!memcmp(version
, known_fw_versions
+ i
, 2))
349 snd_printk(KERN_ERR PREFIX
"invalid fimware version in device: %4ph. "
350 "please reconnect to power. if this failure "
351 "still happens, check your firmware installation.",
356 int usb6fire_fw_init(struct usb_interface
*intf
)
360 struct usb_device
*device
= interface_to_usbdev(intf
);
361 /* buffer: 8 receiving bytes from device and
362 * sizeof(EP_W_MAX_PACKET_SIZE) bytes for non-const copy */
365 ret
= usb6fire_fw_ezusb_read(device
, 1, 0, buffer
, 8);
367 snd_printk(KERN_ERR PREFIX
"unable to receive device "
368 "firmware state.\n");
371 if (buffer
[0] != 0xeb || buffer
[1] != 0xaa || buffer
[2] != 0x55) {
372 snd_printk(KERN_ERR PREFIX
"unknown device firmware state "
373 "received from device: ");
374 for (i
= 0; i
< 8; i
++)
375 snd_printk("%02x ", buffer
[i
]);
379 /* do we need fpga loader ezusb firmware? */
380 if (buffer
[3] == 0x01) {
381 ret
= usb6fire_fw_ezusb_upload(intf
,
382 "6fire/dmx6firel2.ihx", 0, NULL
, 0);
387 /* do we need fpga firmware and application ezusb firmware? */
388 else if (buffer
[3] == 0x02) {
389 ret
= usb6fire_fw_check(buffer
+ 4);
392 ret
= usb6fire_fw_fpga_upload(intf
, "6fire/dmx6firecf.bin");
395 memcpy(buffer
, ep_w_max_packet_size
,
396 sizeof(ep_w_max_packet_size
));
397 ret
= usb6fire_fw_ezusb_upload(intf
, "6fire/dmx6fireap.ihx",
398 0x0003, buffer
, sizeof(ep_w_max_packet_size
));
404 else if (buffer
[3] == 0x03)
405 return usb6fire_fw_check(buffer
+ 4);
408 snd_printk(KERN_ERR PREFIX
"unknown device firmware state "
409 "received from device: ");
410 for (i
= 0; i
< 8; i
++)
411 snd_printk("%02x ", buffer
[i
]);