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. snd_printk(KERN_ERR PREFIX "unable to upload ezusb "
  244. "firmware %s: end message.\n", fwname);
  245. return ret;
  246. }
  247. return 0;
  248. }
  249. static int usb6fire_fw_fpga_upload(
  250. struct usb_interface *intf, const char *fwname)
  251. {
  252. int ret;
  253. int i;
  254. struct usb_device *device = interface_to_usbdev(intf);
  255. u8 *buffer = kmalloc(FPGA_BUFSIZE, GFP_KERNEL);
  256. const char *c;
  257. const char *end;
  258. const struct firmware *fw;
  259. if (!buffer)
  260. return -ENOMEM;
  261. ret = request_firmware(&fw, fwname, &device->dev);
  262. if (ret < 0) {
  263. snd_printk(KERN_ERR PREFIX "unable to get fpga firmware %s.\n",
  264. fwname);
  265. kfree(buffer);
  266. return -EIO;
  267. }
  268. c = fw->data;
  269. end = fw->data + fw->size;
  270. ret = usb6fire_fw_ezusb_write(device, 8, 0, NULL, 0);
  271. if (ret < 0) {
  272. kfree(buffer);
  273. release_firmware(fw);
  274. snd_printk(KERN_ERR PREFIX "unable to upload fpga firmware: "
  275. "begin urb.\n");
  276. return ret;
  277. }
  278. while (c != end) {
  279. for (i = 0; c != end && i < FPGA_BUFSIZE; i++, c++)
  280. buffer[i] = byte_rev_table[(u8) *c];
  281. ret = usb6fire_fw_fpga_write(device, buffer, i);
  282. if (ret < 0) {
  283. release_firmware(fw);
  284. kfree(buffer);
  285. snd_printk(KERN_ERR PREFIX "unable to upload fpga "
  286. "firmware: fw urb.\n");
  287. return ret;
  288. }
  289. }
  290. release_firmware(fw);
  291. kfree(buffer);
  292. ret = usb6fire_fw_ezusb_write(device, 9, 0, NULL, 0);
  293. if (ret < 0) {
  294. snd_printk(KERN_ERR PREFIX "unable to upload fpga firmware: "
  295. "end urb.\n");
  296. return ret;
  297. }
  298. return 0;
  299. }
  300. /* check, if the firmware version the devices has currently loaded
  301. * is known by this driver. 'version' needs to have 4 bytes version
  302. * info data. */
  303. static int usb6fire_fw_check(u8 *version)
  304. {
  305. int i;
  306. for (i = 0; i < ARRAY_SIZE(known_fw_versions); i++)
  307. if (!memcmp(version, known_fw_versions + i, 4))
  308. return 0;
  309. snd_printk(KERN_ERR PREFIX "invalid fimware version in device: "
  310. "%02x %02x %02x %02x. "
  311. "please reconnect to power. if this failure "
  312. "still happens, check your firmware installation.",
  313. version[0], version[1], version[2], version[3]);
  314. return -EINVAL;
  315. }
  316. int usb6fire_fw_init(struct usb_interface *intf)
  317. {
  318. int i;
  319. int ret;
  320. struct usb_device *device = interface_to_usbdev(intf);
  321. /* buffer: 8 receiving bytes from device and
  322. * sizeof(EP_W_MAX_PACKET_SIZE) bytes for non-const copy */
  323. u8 buffer[12];
  324. ret = usb6fire_fw_ezusb_read(device, 1, 0, buffer, 8);
  325. if (ret < 0) {
  326. snd_printk(KERN_ERR PREFIX "unable to receive device "
  327. "firmware state.\n");
  328. return ret;
  329. }
  330. if (buffer[0] != 0xeb || buffer[1] != 0xaa || buffer[2] != 0x55) {
  331. snd_printk(KERN_ERR PREFIX "unknown device firmware state "
  332. "received from device: ");
  333. for (i = 0; i < 8; i++)
  334. snd_printk("%02x ", buffer[i]);
  335. snd_printk("\n");
  336. return -EIO;
  337. }
  338. /* do we need fpga loader ezusb firmware? */
  339. if (buffer[3] == 0x01) {
  340. ret = usb6fire_fw_ezusb_upload(intf,
  341. "6fire/dmx6firel2.ihx", 0, NULL, 0);
  342. if (ret < 0)
  343. return ret;
  344. return FW_NOT_READY;
  345. }
  346. /* do we need fpga firmware and application ezusb firmware? */
  347. else if (buffer[3] == 0x02) {
  348. ret = usb6fire_fw_check(buffer + 4);
  349. if (ret < 0)
  350. return ret;
  351. ret = usb6fire_fw_fpga_upload(intf, "6fire/dmx6firecf.bin");
  352. if (ret < 0)
  353. return ret;
  354. memcpy(buffer, ep_w_max_packet_size,
  355. sizeof(ep_w_max_packet_size));
  356. ret = usb6fire_fw_ezusb_upload(intf, "6fire/dmx6fireap.ihx",
  357. 0x0003, buffer, sizeof(ep_w_max_packet_size));
  358. if (ret < 0)
  359. return ret;
  360. return FW_NOT_READY;
  361. }
  362. /* all fw loaded? */
  363. else if (buffer[3] == 0x03)
  364. return usb6fire_fw_check(buffer + 4);
  365. /* unknown data? */
  366. else {
  367. snd_printk(KERN_ERR PREFIX "unknown device firmware state "
  368. "received from device: ");
  369. for (i = 0; i < 8; i++)
  370. snd_printk("%02x ", buffer[i]);
  371. snd_printk("\n");
  372. return -EIO;
  373. }
  374. return 0;
  375. }