| 1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
/* |
| 3 |
* Linux driver for TerraTec DMX 6Fire USB |
| 4 |
* |
| 5 |
* Firmware loader |
| 6 |
* |
| 7 |
* Author: Torsten Schenk <[email protected]> |
| 8 |
* Created: Jan 01, 2011 |
| 9 |
* Copyright: (C) Torsten Schenk |
| 10 |
*/ |
| 11 |
|
| 12 |
#include <linux/firmware.h> |
| 13 |
#include <linux/module.h> |
| 14 |
#include <linux/bitrev.h> |
| 15 |
#include <linux/hex.h> |
| 16 |
#include <linux/kernel.h> |
| 17 |
|
| 18 |
#include "firmware.h" |
| 19 |
#include "chip.h" |
| 20 |
|
| 21 |
MODULE_FIRMWARE("6fire/dmx6firel2.ihx"); |
| 22 |
MODULE_FIRMWARE("6fire/dmx6fireap.ihx"); |
| 23 |
MODULE_FIRMWARE("6fire/dmx6firecf.bin"); |
| 24 |
|
| 25 |
enum { |
| 26 |
FPGA_BUFSIZE = 512, FPGA_EP = 2 |
| 27 |
}; |
| 28 |
|
| 29 |
/* |
| 30 |
* wMaxPacketSize of pcm endpoints. |
| 31 |
* keep synced with rates_in_packet_size and rates_out_packet_size in pcm.c |
| 32 |
* fpp: frames per isopacket |
| 33 |
* |
| 34 |
* CAUTION: keep sizeof <= buffer[] in usb6fire_fw_init |
| 35 |
*/ |
| 36 |
static const u8 ep_w_max_packet_size[] = { |
| 37 |
0xe4, 0x00, 0xe4, 0x00, /* alt 1: 228 EP2 and EP6 (7 fpp) */ |
| 38 |
0xa4, 0x01, 0xa4, 0x01, /* alt 2: 420 EP2 and EP6 (13 fpp)*/ |
| 39 |
0x94, 0x01, 0x5c, 0x02 /* alt 3: 404 EP2 and 604 EP6 (25 fpp) */ |
| 40 |
}; |
| 41 |
|
| 42 |
static const u8 known_fw_versions[][2] = { |
| 43 |
{ 0x03, 0x01 } |
| 44 |
}; |
| 45 |
|
| 46 |
struct ihex_record { |
| 47 |
u16 address; |
| 48 |
u8 len; |
| 49 |
u8 data[256]; |
| 50 |
char error; /* true if an error occurred parsing this record */ |
| 51 |
|
| 52 |
u8 max_len; /* maximum record length in whole ihex */ |
| 53 |
|
| 54 |
/* private */ |
| 55 |
const char *txt_data; |
| 56 |
unsigned int txt_length; |
| 57 |
unsigned int txt_offset; /* current position in txt_data */ |
| 58 |
}; |
| 59 |
|
| 60 |
static u8 usb6fire_fw_ihex_hex(const u8 *data, u8 *crc) |
| 61 |
{ |
| 62 |
u8 val = 0; |
| 63 |
int hval; |
| 64 |
|
| 65 |
hval = hex_to_bin(data[0]); |
| 66 |
if (hval >= 0) |
| 67 |
val |= (hval << 4); |
| 68 |
|
| 69 |
hval = hex_to_bin(data[1]); |
| 70 |
if (hval >= 0) |
| 71 |
val |= hval; |
| 72 |
|
| 73 |
*crc += val; |
| 74 |
return val; |
| 75 |
} |
| 76 |
|
| 77 |
/* |
| 78 |
* returns true if record is available, false otherwise. |
| 79 |
* iff an error occurred, false will be returned and record->error will be true. |
| 80 |
*/ |
| 81 |
static bool usb6fire_fw_ihex_next_record(struct ihex_record *record) |
| 82 |
{ |
| 83 |
u8 crc = 0; |
| 84 |
u8 type; |
| 85 |
int i; |
| 86 |
|
| 87 |
record->error = false; |
| 88 |
|
| 89 |
/* find begin of record (marked by a colon) */ |
| 90 |
while (record->txt_offset < record->txt_length |
| 91 |
&& record->txt_data[record->txt_offset] != ':') |
| 92 |
record->txt_offset++; |
| 93 |
if (record->txt_offset == record->txt_length) |
| 94 |
return false; |
| 95 |
|
| 96 |
/* number of characters needed for len, addr and type entries */ |
| 97 |
record->txt_offset++; |
| 98 |
if (record->txt_offset + 8 > record->txt_length) { |
| 99 |
record->error = true; |
| 100 |
return false; |
| 101 |
} |
| 102 |
|
| 103 |
record->len = usb6fire_fw_ihex_hex(record->txt_data + |
| 104 |
record->txt_offset, &crc); |
| 105 |
record->txt_offset += 2; |
| 106 |
record->address = usb6fire_fw_ihex_hex(record->txt_data + |
| 107 |
record->txt_offset, &crc) << 8; |
| 108 |
record->txt_offset += 2; |
| 109 |
record->address |= usb6fire_fw_ihex_hex(record->txt_data + |
| 110 |
record->txt_offset, &crc); |
| 111 |
record->txt_offset += 2; |
| 112 |
type = usb6fire_fw_ihex_hex(record->txt_data + |
| 113 |
record->txt_offset, &crc); |
| 114 |
record->txt_offset += 2; |
| 115 |
|
| 116 |
/* number of characters needed for data and crc entries */ |
| 117 |
if (record->txt_offset + 2 * (record->len + 1) > record->txt_length) { |
| 118 |
record->error = true; |
| 119 |
return false; |
| 120 |
} |
| 121 |
for (i = 0; i < record->len; i++) { |
| 122 |
record->data[i] = usb6fire_fw_ihex_hex(record->txt_data |
| 123 |
+ record->txt_offset, &crc); |
| 124 |
record->txt_offset += 2; |
| 125 |
} |
| 126 |
usb6fire_fw_ihex_hex(record->txt_data + record->txt_offset, &crc); |
| 127 |
if (crc) { |
| 128 |
record->error = true; |
| 129 |
return false; |
| 130 |
} |
| 131 |
|
| 132 |
if (type == 1 || !record->len) /* eof */ |
| 133 |
return false; |
| 134 |
else if (type == 0) |
| 135 |
return true; |
| 136 |
else { |
| 137 |
record->error = true; |
| 138 |
return false; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
static int usb6fire_fw_ihex_init(const struct firmware *fw, |
| 143 |
struct ihex_record *record) |
| 144 |
{ |
| 145 |
record->txt_data = fw->data; |
| 146 |
record->txt_length = fw->size; |
| 147 |
record->txt_offset = 0; |
| 148 |
record->max_len = 0; |
| 149 |
/* read all records, if loop ends, record->error indicates, |
| 150 |
* whether ihex is valid. */ |
| 151 |
while (usb6fire_fw_ihex_next_record(record)) |
| 152 |
record->max_len = max(record->len, record->max_len); |
| 153 |
if (record->error) |
| 154 |
return -EINVAL; |
| 155 |
record->txt_offset = 0; |
| 156 |
return 0; |
| 157 |
} |
| 158 |
|
| 159 |
static int usb6fire_fw_ezusb_write(struct usb_device *device, |
| 160 |
int type, int value, char *data, int len) |
| 161 |
{ |
| 162 |
return usb_control_msg_send(device, 0, type, |
| 163 |
USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 164 |
value, 0, data, len, 1000, GFP_KERNEL); |
| 165 |
} |
| 166 |
|
| 167 |
static int usb6fire_fw_ezusb_read(struct usb_device *device, |
| 168 |
int type, int value, char *data, int len) |
| 169 |
{ |
| 170 |
return usb_control_msg_recv(device, 0, type, |
| 171 |
USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 172 |
value, 0, data, len, 1000, GFP_KERNEL); |
| 173 |
} |
| 174 |
|
| 175 |
static int usb6fire_fw_fpga_write(struct usb_device *device, |
| 176 |
char *data, int len) |
| 177 |
{ |
| 178 |
int actual_len; |
| 179 |
int ret; |
| 180 |
|
| 181 |
ret = usb_bulk_msg(device, usb_sndbulkpipe(device, FPGA_EP), data, len, |
| 182 |
&actual_len, 1000); |
| 183 |
if (ret < 0) |
| 184 |
return ret; |
| 185 |
else if (actual_len != len) |
| 186 |
return -EIO; |
| 187 |
return 0; |
| 188 |
} |
| 189 |
|
| 190 |
static int usb6fire_fw_ezusb_upload( |
| 191 |
struct usb_interface *intf, const char *fwname, |
| 192 |
unsigned int postaddr, u8 *postdata, unsigned int postlen) |
| 193 |
{ |
| 194 |
int ret; |
| 195 |
u8 data; |
| 196 |
struct usb_device *device = interface_to_usbdev(intf); |
| 197 |
const struct firmware *fw = NULL; |
| 198 |
struct ihex_record *rec = kmalloc_obj(struct ihex_record); |
| 199 |
|
| 200 |
if (!rec) |
| 201 |
return -ENOMEM; |
| 202 |
|
| 203 |
ret = request_firmware(&fw, fwname, &device->dev); |
| 204 |
if (ret < 0) { |
| 205 |
kfree(rec); |
| 206 |
dev_err(&intf->dev, |
| 207 |
"error requesting ezusb firmware %s.\n", fwname); |
| 208 |
return ret; |
| 209 |
} |
| 210 |
ret = usb6fire_fw_ihex_init(fw, rec); |
| 211 |
if (ret < 0) { |
| 212 |
kfree(rec); |
| 213 |
release_firmware(fw); |
| 214 |
dev_err(&intf->dev, |
| 215 |
"error validating ezusb firmware %s.\n", fwname); |
| 216 |
return ret; |
| 217 |
} |
| 218 |
/* upload firmware image */ |
| 219 |
data = 0x01; /* stop ezusb cpu */ |
| 220 |
ret = usb6fire_fw_ezusb_write(device, 0xa0, 0xe600, &data, 1); |
| 221 |
if (ret) { |
| 222 |
kfree(rec); |
| 223 |
release_firmware(fw); |
| 224 |
dev_err(&intf->dev, |
| 225 |
"unable to upload ezusb firmware %s: begin message.\n", |
| 226 |
fwname); |
| 227 |
return ret; |
| 228 |
} |
| 229 |
|
| 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); |
| 233 |
if (ret) { |
| 234 |
kfree(rec); |
| 235 |
release_firmware(fw); |
| 236 |
dev_err(&intf->dev, |
| 237 |
"unable to upload ezusb firmware %s: data urb.\n", |
| 238 |
fwname); |
| 239 |
return ret; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
release_firmware(fw); |
| 244 |
kfree(rec); |
| 245 |
if (postdata) { /* write data after firmware has been uploaded */ |
| 246 |
ret = usb6fire_fw_ezusb_write(device, 0xa0, postaddr, |
| 247 |
postdata, postlen); |
| 248 |
if (ret) { |
| 249 |
dev_err(&intf->dev, |
| 250 |
"unable to upload ezusb firmware %s: post urb.\n", |
| 251 |
fwname); |
| 252 |
return ret; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
data = 0x00; /* resume ezusb cpu */ |
| 257 |
ret = usb6fire_fw_ezusb_write(device, 0xa0, 0xe600, &data, 1); |
| 258 |
if (ret) { |
| 259 |
dev_err(&intf->dev, |
| 260 |
"unable to upload ezusb firmware %s: end message.\n", |
| 261 |
fwname); |
| 262 |
return ret; |
| 263 |
} |
| 264 |
return 0; |
| 265 |
} |
| 266 |
|
| 267 |
static int usb6fire_fw_fpga_upload( |
| 268 |
struct usb_interface *intf, const char *fwname) |
| 269 |
{ |
| 270 |
int ret; |
| 271 |
int i; |
| 272 |
struct usb_device *device = interface_to_usbdev(intf); |
| 273 |
u8 *buffer = kmalloc(FPGA_BUFSIZE, GFP_KERNEL); |
| 274 |
const char *c; |
| 275 |
const char *end; |
| 276 |
const struct firmware *fw; |
| 277 |
|
| 278 |
if (!buffer) |
| 279 |
return -ENOMEM; |
| 280 |
|
| 281 |
ret = request_firmware(&fw, fwname, &device->dev); |
| 282 |
if (ret < 0) { |
| 283 |
dev_err(&intf->dev, "unable to get fpga firmware %s.\n", |
| 284 |
fwname); |
| 285 |
kfree(buffer); |
| 286 |
return -EIO; |
| 287 |
} |
| 288 |
|
| 289 |
c = fw->data; |
| 290 |
end = fw->data + fw->size; |
| 291 |
|
| 292 |
ret = usb6fire_fw_ezusb_write(device, 8, 0, NULL, 0); |
| 293 |
if (ret) { |
| 294 |
kfree(buffer); |
| 295 |
release_firmware(fw); |
| 296 |
dev_err(&intf->dev, |
| 297 |
"unable to upload fpga firmware: begin urb.\n"); |
| 298 |
return ret; |
| 299 |
} |
| 300 |
|
| 301 |
while (c != end) { |
| 302 |
for (i = 0; c != end && i < FPGA_BUFSIZE; i++, c++) |
| 303 |
buffer[i] = bitrev8((u8)*c); |
| 304 |
|
| 305 |
ret = usb6fire_fw_fpga_write(device, buffer, i); |
| 306 |
if (ret < 0) { |
| 307 |
release_firmware(fw); |
| 308 |
kfree(buffer); |
| 309 |
dev_err(&intf->dev, |
| 310 |
"unable to upload fpga firmware: fw urb.\n"); |
| 311 |
return ret; |
| 312 |
} |
| 313 |
} |
| 314 |
release_firmware(fw); |
| 315 |
kfree(buffer); |
| 316 |
|
| 317 |
ret = usb6fire_fw_ezusb_write(device, 9, 0, NULL, 0); |
| 318 |
if (ret) { |
| 319 |
dev_err(&intf->dev, |
| 320 |
"unable to upload fpga firmware: end urb.\n"); |
| 321 |
return ret; |
| 322 |
} |
| 323 |
return 0; |
| 324 |
} |
| 325 |
|
| 326 |
/* check, if the firmware version the devices has currently loaded |
| 327 |
* is known by this driver. 'version' needs to have 4 bytes version |
| 328 |
* info data. */ |
| 329 |
static int usb6fire_fw_check(struct usb_interface *intf, const u8 *version) |
| 330 |
{ |
| 331 |
int i; |
| 332 |
|
| 333 |
for (i = 0; i < ARRAY_SIZE(known_fw_versions); i++) |
| 334 |
if (!memcmp(version, known_fw_versions + i, 2)) |
| 335 |
return 0; |
| 336 |
|
| 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.", |
| 340 |
version); |
| 341 |
return -EINVAL; |
| 342 |
} |
| 343 |
|
| 344 |
int usb6fire_fw_init(struct usb_interface *intf) |
| 345 |
{ |
| 346 |
int i; |
| 347 |
int ret; |
| 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 */ |
| 351 |
u8 buffer[12]; |
| 352 |
|
| 353 |
ret = usb6fire_fw_ezusb_read(device, 1, 0, buffer, 8); |
| 354 |
if (ret) { |
| 355 |
dev_err(&intf->dev, |
| 356 |
"unable to receive device firmware state.\n"); |
| 357 |
return ret; |
| 358 |
} |
| 359 |
if (buffer[0] != 0xeb || buffer[1] != 0xaa || buffer[2] != 0x55) { |
| 360 |
dev_err(&intf->dev, |
| 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"); |
| 365 |
return -EIO; |
| 366 |
} |
| 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); |
| 371 |
if (ret < 0) |
| 372 |
return ret; |
| 373 |
return FW_NOT_READY; |
| 374 |
} |
| 375 |
/* do we need fpga firmware and application ezusb firmware? */ |
| 376 |
else if (buffer[3] == 0x02) { |
| 377 |
ret = usb6fire_fw_check(intf, buffer + 4); |
| 378 |
if (ret < 0) |
| 379 |
return ret; |
| 380 |
ret = usb6fire_fw_fpga_upload(intf, "6fire/dmx6firecf.bin"); |
| 381 |
if (ret < 0) |
| 382 |
return ret; |
| 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)); |
| 387 |
if (ret < 0) |
| 388 |
return ret; |
| 389 |
return FW_NOT_READY; |
| 390 |
} |
| 391 |
/* all fw loaded? */ |
| 392 |
else if (buffer[3] == 0x03) |
| 393 |
return usb6fire_fw_check(intf, buffer + 4); |
| 394 |
/* unknown data? */ |
| 395 |
else { |
| 396 |
dev_err(&intf->dev, |
| 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"); |
| 401 |
return -EIO; |
| 402 |
} |
| 403 |
return 0; |
| 404 |
} |
| 405 |
|