2 * Linux driver for TerraTec DMX 6Fire USB
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);
223 "error requesting ezusb firmware %s.\n", fwname);
226 ret = usb6fire_fw_ihex_init(fw, rec);
229 release_firmware(fw);
231 "error validating ezusb 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);
241 "unable to upload ezusb firmware %s: begin message.\n",
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);
253 "unable to upload ezusb firmware %s: data urb.\n",
259 release_firmware(fw);
261 if (postdata) { /* write data after firmware has been uploaded */
262 ret = usb6fire_fw_ezusb_write(device, 0xa0, postaddr,
266 "unable to upload ezusb firmware %s: post urb.\n",
272 data = 0x00; /* resume ezusb cpu */
273 ret = usb6fire_fw_ezusb_write(device, 0xa0, 0xe600, &data, 1);
276 "unable to upload ezusb firmware %s: end message.\n",
283 static int usb6fire_fw_fpga_upload(
284 struct usb_interface *intf, const char *fwname)
288 struct usb_device *device = interface_to_usbdev(intf);
289 u8 *buffer = kmalloc(FPGA_BUFSIZE, GFP_KERNEL);
292 const struct firmware *fw;
297 ret = request_firmware(&fw, fwname, &device->dev);
299 dev_err(&intf->dev, "unable to get fpga firmware %s.\n",
306 end = fw->data + fw->size;
308 ret = usb6fire_fw_ezusb_write(device, 8, 0, NULL, 0);
311 release_firmware(fw);
313 "unable to upload fpga firmware: begin urb.\n");
318 for (i = 0; c != end && i < FPGA_BUFSIZE; i++, c++)
319 buffer[i] = bitrev8((u8)*c);
321 ret = usb6fire_fw_fpga_write(device, buffer, i);
323 release_firmware(fw);
326 "unable to upload fpga firmware: fw urb.\n");
330 release_firmware(fw);
333 ret = usb6fire_fw_ezusb_write(device, 9, 0, NULL, 0);
336 "unable to upload fpga firmware: end urb.\n");
342 /* check, if the firmware version the devices has currently loaded
343 * is known by this driver. 'version' needs to have 4 bytes version
345 static int usb6fire_fw_check(struct usb_interface *intf, const u8 *version)
349 for (i = 0; i < ARRAY_SIZE(known_fw_versions); i++)
350 if (!memcmp(version, known_fw_versions + i, 2))
353 dev_err(&intf->dev, "invalid firmware version in device: %4ph. "
354 "please reconnect to power. if this failure "
355 "still happens, check your firmware installation.",
360 int usb6fire_fw_init(struct usb_interface *intf)
364 struct usb_device *device = interface_to_usbdev(intf);
365 /* buffer: 8 receiving bytes from device and
366 * sizeof(EP_W_MAX_PACKET_SIZE) bytes for non-const copy */
369 ret = usb6fire_fw_ezusb_read(device, 1, 0, buffer, 8);
372 "unable to receive device firmware state.\n");
375 if (buffer[0] != 0xeb || buffer[1] != 0xaa || buffer[2] != 0x55) {
377 "unknown device firmware state received from device:");
378 for (i = 0; i < 8; i++)
379 printk(KERN_CONT "%02x ", buffer[i]);
380 printk(KERN_CONT "\n");
383 /* do we need fpga loader ezusb firmware? */
384 if (buffer[3] == 0x01) {
385 ret = usb6fire_fw_ezusb_upload(intf,
386 "6fire/dmx6firel2.ihx", 0, NULL, 0);
391 /* do we need fpga firmware and application ezusb firmware? */
392 else if (buffer[3] == 0x02) {
393 ret = usb6fire_fw_check(intf, buffer + 4);
396 ret = usb6fire_fw_fpga_upload(intf, "6fire/dmx6firecf.bin");
399 memcpy(buffer, ep_w_max_packet_size,
400 sizeof(ep_w_max_packet_size));
401 ret = usb6fire_fw_ezusb_upload(intf, "6fire/dmx6fireap.ihx",
402 0x0003, buffer, sizeof(ep_w_max_packet_size));
408 else if (buffer[3] == 0x03)
409 return usb6fire_fw_check(intf, buffer + 4);
413 "unknown device firmware state received from device: ");
414 for (i = 0; i < 8; i++)
415 printk(KERN_CONT "%02x ", buffer[i]);
416 printk(KERN_CONT "\n");