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