firmware.c 10 KB

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