1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Linux driver for TerraTec DMX 6Fire USB
8 * Created: Jan 01, 2011
9 * Copyright: (C) Torsten Schenk
12 #include <linux/firmware.h>
13 #include <linux/module.h>
14 #include <linux/bitrev.h>
15 #include <linux/kernel.h>
20 MODULE_FIRMWARE("6fire/dmx6firel2.ihx");
21 MODULE_FIRMWARE("6fire/dmx6fireap.ihx");
22 MODULE_FIRMWARE("6fire/dmx6firecf.bin");
25 FPGA_BUFSIZE = 512, FPGA_EP = 2
29 * wMaxPacketSize of pcm endpoints.
30 * keep synced with rates_in_packet_size and rates_out_packet_size in pcm.c
31 * fpp: frames per isopacket
33 * CAUTION: keep sizeof <= buffer[] in usb6fire_fw_init
35 static const u8 ep_w_max_packet_size[] = {
36 0xe4, 0x00, 0xe4, 0x00, /* alt 1: 228 EP2 and EP6 (7 fpp) */
37 0xa4, 0x01, 0xa4, 0x01, /* alt 2: 420 EP2 and EP6 (13 fpp)*/
38 0x94, 0x01, 0x5c, 0x02 /* alt 3: 404 EP2 and 604 EP6 (25 fpp) */
41 static const u8 known_fw_versions[][2] = {
49 char error; /* true if an error occurred parsing this record */
51 u8 max_len; /* maximum record length in whole ihex */
55 unsigned int txt_length;
56 unsigned int txt_offset; /* current position in txt_data */
59 static u8 usb6fire_fw_ihex_hex(const u8 *data, u8 *crc)
64 hval = hex_to_bin(data[0]);
68 hval = hex_to_bin(data[1]);
77 * returns true if record is available, false otherwise.
78 * iff an error occurred, false will be returned and record->error will be true.
80 static bool usb6fire_fw_ihex_next_record(struct ihex_record *record)
86 record->error = false;
88 /* find begin of record (marked by a colon) */
89 while (record->txt_offset < record->txt_length
90 && record->txt_data[record->txt_offset] != ':')
92 if (record->txt_offset == record->txt_length)
95 /* number of characters needed for len, addr and type entries */
97 if (record->txt_offset + 8 > record->txt_length) {
102 record->len = usb6fire_fw_ihex_hex(record->txt_data +
103 record->txt_offset, &crc);
104 record->txt_offset += 2;
105 record->address = usb6fire_fw_ihex_hex(record->txt_data +
106 record->txt_offset, &crc) << 8;
107 record->txt_offset += 2;
108 record->address |= usb6fire_fw_ihex_hex(record->txt_data +
109 record->txt_offset, &crc);
110 record->txt_offset += 2;
111 type = usb6fire_fw_ihex_hex(record->txt_data +
112 record->txt_offset, &crc);
113 record->txt_offset += 2;
115 /* number of characters needed for data and crc entries */
116 if (record->txt_offset + 2 * (record->len + 1) > record->txt_length) {
117 record->error = true;
120 for (i = 0; i < record->len; i++) {
121 record->data[i] = usb6fire_fw_ihex_hex(record->txt_data
122 + record->txt_offset, &crc);
123 record->txt_offset += 2;
125 usb6fire_fw_ihex_hex(record->txt_data + record->txt_offset, &crc);
127 record->error = true;
131 if (type == 1 || !record->len) /* eof */
136 record->error = true;
141 static int usb6fire_fw_ihex_init(const struct firmware *fw,
142 struct ihex_record *record)
144 record->txt_data = fw->data;
145 record->txt_length = fw->size;
146 record->txt_offset = 0;
148 /* read all records, if loop ends, record->error indicates,
149 * whether ihex is valid. */
150 while (usb6fire_fw_ihex_next_record(record))
151 record->max_len = max(record->len, record->max_len);
154 record->txt_offset = 0;
158 static int usb6fire_fw_ezusb_write(struct usb_device *device,
159 int type, int value, char *data, int len)
161 return usb_control_msg_send(device, 0, type,
162 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
163 value, 0, data, len, HZ, GFP_KERNEL);
166 static int usb6fire_fw_ezusb_read(struct usb_device *device,
167 int type, int value, char *data, int len)
169 return usb_control_msg_recv(device, 0, type,
170 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
171 value, 0, data, len, HZ, GFP_KERNEL);
174 static int usb6fire_fw_fpga_write(struct usb_device *device,
180 ret = usb_bulk_msg(device, usb_sndbulkpipe(device, FPGA_EP), data, len,
184 else if (actual_len != len)
189 static int usb6fire_fw_ezusb_upload(
190 struct usb_interface *intf, const char *fwname,
191 unsigned int postaddr, u8 *postdata, unsigned int postlen)
195 struct usb_device *device = interface_to_usbdev(intf);
196 const struct firmware *fw = NULL;
197 struct ihex_record *rec = kmalloc(sizeof(struct ihex_record),
203 ret = request_firmware(&fw, fwname, &device->dev);
207 "error requesting ezusb firmware %s.\n", fwname);
210 ret = usb6fire_fw_ihex_init(fw, rec);
213 release_firmware(fw);
215 "error validating ezusb firmware %s.\n", fwname);
218 /* upload firmware image */
219 data = 0x01; /* stop ezusb cpu */
220 ret = usb6fire_fw_ezusb_write(device, 0xa0, 0xe600, &data, 1);
223 release_firmware(fw);
225 "unable to upload ezusb firmware %s: begin message.\n",
230 while (usb6fire_fw_ihex_next_record(rec)) { /* write firmware */
231 ret = usb6fire_fw_ezusb_write(device, 0xa0, rec->address,
232 rec->data, rec->len);
235 release_firmware(fw);
237 "unable to upload ezusb firmware %s: data urb.\n",
243 release_firmware(fw);
245 if (postdata) { /* write data after firmware has been uploaded */
246 ret = usb6fire_fw_ezusb_write(device, 0xa0, postaddr,
250 "unable to upload ezusb firmware %s: post urb.\n",
256 data = 0x00; /* resume ezusb cpu */
257 ret = usb6fire_fw_ezusb_write(device, 0xa0, 0xe600, &data, 1);
260 "unable to upload ezusb firmware %s: end message.\n",
267 static int usb6fire_fw_fpga_upload(
268 struct usb_interface *intf, const char *fwname)
272 struct usb_device *device = interface_to_usbdev(intf);
273 u8 *buffer = kmalloc(FPGA_BUFSIZE, GFP_KERNEL);
276 const struct firmware *fw;
281 ret = request_firmware(&fw, fwname, &device->dev);
283 dev_err(&intf->dev, "unable to get fpga firmware %s.\n",
290 end = fw->data + fw->size;
292 ret = usb6fire_fw_ezusb_write(device, 8, 0, NULL, 0);
295 release_firmware(fw);
297 "unable to upload fpga firmware: begin urb.\n");
302 for (i = 0; c != end && i < FPGA_BUFSIZE; i++, c++)
303 buffer[i] = bitrev8((u8)*c);
305 ret = usb6fire_fw_fpga_write(device, buffer, i);
307 release_firmware(fw);
310 "unable to upload fpga firmware: fw urb.\n");
314 release_firmware(fw);
317 ret = usb6fire_fw_ezusb_write(device, 9, 0, NULL, 0);
320 "unable to upload fpga firmware: end urb.\n");
326 /* check, if the firmware version the devices has currently loaded
327 * is known by this driver. 'version' needs to have 4 bytes version
329 static int usb6fire_fw_check(struct usb_interface *intf, const u8 *version)
333 for (i = 0; i < ARRAY_SIZE(known_fw_versions); i++)
334 if (!memcmp(version, known_fw_versions + i, 2))
337 dev_err(&intf->dev, "invalid firmware version in device: %4ph. "
338 "please reconnect to power. if this failure "
339 "still happens, check your firmware installation.",
344 int usb6fire_fw_init(struct usb_interface *intf)
348 struct usb_device *device = interface_to_usbdev(intf);
349 /* buffer: 8 receiving bytes from device and
350 * sizeof(EP_W_MAX_PACKET_SIZE) bytes for non-const copy */
353 ret = usb6fire_fw_ezusb_read(device, 1, 0, buffer, 8);
356 "unable to receive device firmware state.\n");
359 if (buffer[0] != 0xeb || buffer[1] != 0xaa || buffer[2] != 0x55) {
361 "unknown device firmware state received from device:");
362 for (i = 0; i < 8; i++)
363 printk(KERN_CONT "%02x ", buffer[i]);
364 printk(KERN_CONT "\n");
367 /* do we need fpga loader ezusb firmware? */
368 if (buffer[3] == 0x01) {
369 ret = usb6fire_fw_ezusb_upload(intf,
370 "6fire/dmx6firel2.ihx", 0, NULL, 0);
375 /* do we need fpga firmware and application ezusb firmware? */
376 else if (buffer[3] == 0x02) {
377 ret = usb6fire_fw_check(intf, buffer + 4);
380 ret = usb6fire_fw_fpga_upload(intf, "6fire/dmx6firecf.bin");
383 memcpy(buffer, ep_w_max_packet_size,
384 sizeof(ep_w_max_packet_size));
385 ret = usb6fire_fw_ezusb_upload(intf, "6fire/dmx6fireap.ihx",
386 0x0003, buffer, sizeof(ep_w_max_packet_size));
392 else if (buffer[3] == 0x03)
393 return usb6fire_fw_check(intf, buffer + 4);
397 "unknown device firmware state received from device: ");
398 for (i = 0; i < 8; i++)
399 printk(KERN_CONT "%02x ", buffer[i]);
400 printk(KERN_CONT "\n");