firmware.c 10 KB

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