blob: 62c25e74f0e52542b06acd2f57e47164d408d56d [file] [log] [blame]
Torsten Schenkc6d43ba2011-01-24 18:45:30 +01001/*
2 * Linux driver for TerraTec DMX 6Fire USB
3 *
4 * Firmware loader
5 *
Torsten Schenkc6d43ba2011-01-24 18:45:30 +01006 * Author: Torsten Schenk <torsten.schenk@zoho.com>
7 * Created: Jan 01, 2011
Torsten Schenkc6d43ba2011-01-24 18:45:30 +01008 * Copyright: (C) Torsten Schenk
9 *
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.
14 */
15
16#include <linux/firmware.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040017#include <linux/module.h>
Daniel Mack8ae95722011-04-23 20:56:43 +020018#include <linux/bitrev.h>
Andy Shevchenko49957f32011-09-23 14:32:11 +030019#include <linux/kernel.h>
Torsten Schenkc6d43ba2011-01-24 18:45:30 +010020
21#include "firmware.h"
22#include "chip.h"
23
24MODULE_FIRMWARE("6fire/dmx6firel2.ihx");
25MODULE_FIRMWARE("6fire/dmx6fireap.ihx");
26MODULE_FIRMWARE("6fire/dmx6firecf.bin");
27
28enum {
29 FPGA_BUFSIZE = 512, FPGA_EP = 2
30};
31
Torsten Schenkc6d43ba2011-01-24 18:45:30 +010032/*
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
36 *
37 * CAUTION: keep sizeof <= buffer[] in usb6fire_fw_init
38 */
39static 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) */
43};
44
Torsten Schenkd47333d2013-05-23 13:38:29 +020045static const u8 known_fw_versions[][2] = {
46 { 0x03, 0x01 }
Torsten Schenkb84610b2011-04-04 11:49:57 +020047};
48
Torsten Schenkc6d43ba2011-01-24 18:45:30 +010049struct ihex_record {
50 u16 address;
51 u8 len;
52 u8 data[256];
Lucas De Marchi25985ed2011-03-30 22:57:33 -030053 char error; /* true if an error occurred parsing this record */
Torsten Schenkc6d43ba2011-01-24 18:45:30 +010054
55 u8 max_len; /* maximum record length in whole ihex */
56
57 /* private */
58 const char *txt_data;
59 unsigned int txt_length;
60 unsigned int txt_offset; /* current position in txt_data */
61};
62
Torsten Schenkc6d43ba2011-01-24 18:45:30 +010063static u8 usb6fire_fw_ihex_hex(const u8 *data, u8 *crc)
64{
Andy Shevchenko49957f32011-09-23 14:32:11 +030065 u8 val = 0;
66 int hval;
67
68 hval = hex_to_bin(data[0]);
69 if (hval >= 0)
70 val |= (hval << 4);
71
72 hval = hex_to_bin(data[1]);
73 if (hval >= 0)
74 val |= hval;
75
Torsten Schenkc6d43ba2011-01-24 18:45:30 +010076 *crc += val;
77 return val;
78}
79
80/*
81 * returns true if record is available, false otherwise.
Lucas De Marchi25985ed2011-03-30 22:57:33 -030082 * iff an error occurred, false will be returned and record->error will be true.
Torsten Schenkc6d43ba2011-01-24 18:45:30 +010083 */
84static bool usb6fire_fw_ihex_next_record(struct ihex_record *record)
85{
86 u8 crc = 0;
87 u8 type;
88 int i;
89
90 record->error = false;
91
92 /* find begin of record (marked by a colon) */
93 while (record->txt_offset < record->txt_length
94 && record->txt_data[record->txt_offset] != ':')
95 record->txt_offset++;
96 if (record->txt_offset == record->txt_length)
97 return false;
98
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;
103 return false;
104 }
105
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;
118
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;
122 return false;
123 }
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;
128 }
129 usb6fire_fw_ihex_hex(record->txt_data + record->txt_offset, &crc);
130 if (crc) {
131 record->error = true;
132 return false;
133 }
134
135 if (type == 1 || !record->len) /* eof */
136 return false;
137 else if (type == 0)
138 return true;
139 else {
140 record->error = true;
141 return false;
142 }
143}
144
145static int usb6fire_fw_ihex_init(const struct firmware *fw,
146 struct ihex_record *record)
147{
148 record->txt_data = fw->data;
149 record->txt_length = fw->size;
150 record->txt_offset = 0;
151 record->max_len = 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);
156 if (record->error)
157 return -EINVAL;
158 record->txt_offset = 0;
159 return 0;
160}
161
162static int usb6fire_fw_ezusb_write(struct usb_device *device,
163 int type, int value, char *data, int len)
164{
165 int ret;
166
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);
170 if (ret < 0)
171 return ret;
172 else if (ret != len)
173 return -EIO;
174 return 0;
175}
176
177static int usb6fire_fw_ezusb_read(struct usb_device *device,
178 int type, int value, char *data, int len)
179{
180 int ret = usb_control_msg(device, usb_rcvctrlpipe(device, 0), type,
181 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, value,
182 0, data, len, HZ);
183 if (ret < 0)
184 return ret;
185 else if (ret != len)
186 return -EIO;
187 return 0;
188}
189
190static int usb6fire_fw_fpga_write(struct usb_device *device,
191 char *data, int len)
192{
193 int actual_len;
194 int ret;
195
196 ret = usb_bulk_msg(device, usb_sndbulkpipe(device, FPGA_EP), data, len,
197 &actual_len, HZ);
198 if (ret < 0)
199 return ret;
200 else if (actual_len != len)
201 return -EIO;
202 return 0;
203}
204
205static int usb6fire_fw_ezusb_upload(
206 struct usb_interface *intf, const char *fwname,
207 unsigned int postaddr, u8 *postdata, unsigned int postlen)
208{
209 int ret;
210 u8 data;
211 struct usb_device *device = interface_to_usbdev(intf);
Daniel Mack0b1d8e02012-06-16 16:58:36 +0200212 const struct firmware *fw = NULL;
Torsten Schenkc6d43ba2011-01-24 18:45:30 +0100213 struct ihex_record *rec = kmalloc(sizeof(struct ihex_record),
214 GFP_KERNEL);
215
216 if (!rec)
217 return -ENOMEM;
218
219 ret = request_firmware(&fw, fwname, &device->dev);
220 if (ret < 0) {
221 kfree(rec);
Takashi Iwaie3b37572014-02-26 15:51:04 +0100222 dev_err(&intf->dev,
223 "error requesting ezusb firmware %s.\n", fwname);
Torsten Schenkc6d43ba2011-01-24 18:45:30 +0100224 return ret;
225 }
226 ret = usb6fire_fw_ihex_init(fw, rec);
227 if (ret < 0) {
228 kfree(rec);
Jesper Juhlbf0be0e2011-05-30 12:49:01 +0200229 release_firmware(fw);
Takashi Iwaie3b37572014-02-26 15:51:04 +0100230 dev_err(&intf->dev,
231 "error validating ezusb firmware %s.\n", fwname);
Torsten Schenkc6d43ba2011-01-24 18:45:30 +0100232 return ret;
233 }
234 /* upload firmware image */
235 data = 0x01; /* stop ezusb cpu */
236 ret = usb6fire_fw_ezusb_write(device, 0xa0, 0xe600, &data, 1);
237 if (ret < 0) {
238 kfree(rec);
239 release_firmware(fw);
Takashi Iwaie3b37572014-02-26 15:51:04 +0100240 dev_err(&intf->dev,
241 "unable to upload ezusb firmware %s: begin message.\n",
242 fwname);
Torsten Schenkc6d43ba2011-01-24 18:45:30 +0100243 return ret;
244 }
245
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);
249 if (ret < 0) {
250 kfree(rec);
251 release_firmware(fw);
Takashi Iwaie3b37572014-02-26 15:51:04 +0100252 dev_err(&intf->dev,
253 "unable to upload ezusb firmware %s: data urb.\n",
254 fwname);
Torsten Schenkc6d43ba2011-01-24 18:45:30 +0100255 return ret;
256 }
257 }
258
259 release_firmware(fw);
260 kfree(rec);
261 if (postdata) { /* write data after firmware has been uploaded */
262 ret = usb6fire_fw_ezusb_write(device, 0xa0, postaddr,
263 postdata, postlen);
264 if (ret < 0) {
Takashi Iwaie3b37572014-02-26 15:51:04 +0100265 dev_err(&intf->dev,
266 "unable to upload ezusb firmware %s: post urb.\n",
267 fwname);
Torsten Schenkc6d43ba2011-01-24 18:45:30 +0100268 return ret;
269 }
270 }
271
272 data = 0x00; /* resume ezusb cpu */
273 ret = usb6fire_fw_ezusb_write(device, 0xa0, 0xe600, &data, 1);
274 if (ret < 0) {
Takashi Iwaie3b37572014-02-26 15:51:04 +0100275 dev_err(&intf->dev,
276 "unable to upload ezusb firmware %s: end message.\n",
277 fwname);
Torsten Schenkc6d43ba2011-01-24 18:45:30 +0100278 return ret;
279 }
280 return 0;
281}
282
283static int usb6fire_fw_fpga_upload(
284 struct usb_interface *intf, const char *fwname)
285{
286 int ret;
287 int i;
288 struct usb_device *device = interface_to_usbdev(intf);
289 u8 *buffer = kmalloc(FPGA_BUFSIZE, GFP_KERNEL);
290 const char *c;
291 const char *end;
292 const struct firmware *fw;
293
294 if (!buffer)
295 return -ENOMEM;
296
297 ret = request_firmware(&fw, fwname, &device->dev);
298 if (ret < 0) {
Takashi Iwaie3b37572014-02-26 15:51:04 +0100299 dev_err(&intf->dev, "unable to get fpga firmware %s.\n",
Torsten Schenkc6d43ba2011-01-24 18:45:30 +0100300 fwname);
301 kfree(buffer);
302 return -EIO;
303 }
304
305 c = fw->data;
306 end = fw->data + fw->size;
307
308 ret = usb6fire_fw_ezusb_write(device, 8, 0, NULL, 0);
309 if (ret < 0) {
310 kfree(buffer);
311 release_firmware(fw);
Takashi Iwaie3b37572014-02-26 15:51:04 +0100312 dev_err(&intf->dev,
313 "unable to upload fpga firmware: begin urb.\n");
Torsten Schenkc6d43ba2011-01-24 18:45:30 +0100314 return ret;
315 }
316
317 while (c != end) {
318 for (i = 0; c != end && i < FPGA_BUFSIZE; i++, c++)
Joe Perches9547c092014-10-28 14:22:49 -0700319 buffer[i] = bitrev8((u8)*c);
Torsten Schenkc6d43ba2011-01-24 18:45:30 +0100320
321 ret = usb6fire_fw_fpga_write(device, buffer, i);
322 if (ret < 0) {
323 release_firmware(fw);
324 kfree(buffer);
Takashi Iwaie3b37572014-02-26 15:51:04 +0100325 dev_err(&intf->dev,
326 "unable to upload fpga firmware: fw urb.\n");
Torsten Schenkc6d43ba2011-01-24 18:45:30 +0100327 return ret;
328 }
329 }
330 release_firmware(fw);
331 kfree(buffer);
332
333 ret = usb6fire_fw_ezusb_write(device, 9, 0, NULL, 0);
334 if (ret < 0) {
Takashi Iwaie3b37572014-02-26 15:51:04 +0100335 dev_err(&intf->dev,
336 "unable to upload fpga firmware: end urb.\n");
Torsten Schenkc6d43ba2011-01-24 18:45:30 +0100337 return ret;
338 }
339 return 0;
340}
341
Torsten Schenkb84610b2011-04-04 11:49:57 +0200342/* check, if the firmware version the devices has currently loaded
343 * is known by this driver. 'version' needs to have 4 bytes version
344 * info data. */
Takashi Iwaie3b37572014-02-26 15:51:04 +0100345static int usb6fire_fw_check(struct usb_interface *intf, const u8 *version)
Torsten Schenkb84610b2011-04-04 11:49:57 +0200346{
347 int i;
348
349 for (i = 0; i < ARRAY_SIZE(known_fw_versions); i++)
Torsten Schenkd47333d2013-05-23 13:38:29 +0200350 if (!memcmp(version, known_fw_versions + i, 2))
Torsten Schenkb84610b2011-04-04 11:49:57 +0200351 return 0;
352
Takashi Iwaie3b37572014-02-26 15:51:04 +0100353 dev_err(&intf->dev, "invalid fimware version in device: %4ph. "
Torsten Schenkb84610b2011-04-04 11:49:57 +0200354 "please reconnect to power. if this failure "
355 "still happens, check your firmware installation.",
Andy Shevchenko663819f2013-08-07 17:55:14 +0300356 version);
Torsten Schenkb84610b2011-04-04 11:49:57 +0200357 return -EINVAL;
358}
359
Torsten Schenkc6d43ba2011-01-24 18:45:30 +0100360int usb6fire_fw_init(struct usb_interface *intf)
361{
362 int i;
363 int ret;
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 */
367 u8 buffer[12];
368
369 ret = usb6fire_fw_ezusb_read(device, 1, 0, buffer, 8);
370 if (ret < 0) {
Takashi Iwaie3b37572014-02-26 15:51:04 +0100371 dev_err(&intf->dev,
372 "unable to receive device firmware state.\n");
Torsten Schenkc6d43ba2011-01-24 18:45:30 +0100373 return ret;
374 }
Torsten Schenkb84610b2011-04-04 11:49:57 +0200375 if (buffer[0] != 0xeb || buffer[1] != 0xaa || buffer[2] != 0x55) {
Takashi Iwaie3b37572014-02-26 15:51:04 +0100376 dev_err(&intf->dev,
377 "unknown device firmware state received from device:");
Torsten Schenkc6d43ba2011-01-24 18:45:30 +0100378 for (i = 0; i < 8; i++)
Takashi Iwaie3b37572014-02-26 15:51:04 +0100379 printk(KERN_CONT "%02x ", buffer[i]);
380 printk(KERN_CONT "\n");
Torsten Schenkc6d43ba2011-01-24 18:45:30 +0100381 return -EIO;
382 }
383 /* do we need fpga loader ezusb firmware? */
Torsten Schenkb84610b2011-04-04 11:49:57 +0200384 if (buffer[3] == 0x01) {
Torsten Schenkc6d43ba2011-01-24 18:45:30 +0100385 ret = usb6fire_fw_ezusb_upload(intf,
386 "6fire/dmx6firel2.ihx", 0, NULL, 0);
387 if (ret < 0)
388 return ret;
389 return FW_NOT_READY;
390 }
391 /* do we need fpga firmware and application ezusb firmware? */
Torsten Schenkb84610b2011-04-04 11:49:57 +0200392 else if (buffer[3] == 0x02) {
Takashi Iwaie3b37572014-02-26 15:51:04 +0100393 ret = usb6fire_fw_check(intf, buffer + 4);
Torsten Schenkb84610b2011-04-04 11:49:57 +0200394 if (ret < 0)
395 return ret;
Torsten Schenkc6d43ba2011-01-24 18:45:30 +0100396 ret = usb6fire_fw_fpga_upload(intf, "6fire/dmx6firecf.bin");
397 if (ret < 0)
398 return ret;
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));
403 if (ret < 0)
404 return ret;
405 return FW_NOT_READY;
406 }
407 /* all fw loaded? */
Torsten Schenkb84610b2011-04-04 11:49:57 +0200408 else if (buffer[3] == 0x03)
Takashi Iwaie3b37572014-02-26 15:51:04 +0100409 return usb6fire_fw_check(intf, buffer + 4);
Torsten Schenkc6d43ba2011-01-24 18:45:30 +0100410 /* unknown data? */
411 else {
Takashi Iwaie3b37572014-02-26 15:51:04 +0100412 dev_err(&intf->dev,
413 "unknown device firmware state received from device: ");
Torsten Schenkc6d43ba2011-01-24 18:45:30 +0100414 for (i = 0; i < 8; i++)
Takashi Iwaie3b37572014-02-26 15:51:04 +0100415 printk(KERN_CONT "%02x ", buffer[i]);
416 printk(KERN_CONT "\n");
Torsten Schenkc6d43ba2011-01-24 18:45:30 +0100417 return -EIO;
418 }
419 return 0;
420}
421