firmware.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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 "firmware.h"
  18. #include "chip.h"
  19. MODULE_FIRMWARE("6fire/dmx6firel2.ihx");
  20. MODULE_FIRMWARE("6fire/dmx6fireap.ihx");
  21. MODULE_FIRMWARE("6fire/dmx6firecf.bin");
  22. enum {
  23. FPGA_BUFSIZE = 512, FPGA_EP = 2
  24. };
  25. static const u8 BIT_REVERSE_TABLE[256] = {
  26. 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, 0x10, 0x90, 0x50,
  27. 0xd0, 0x30, 0xb0, 0x70, 0xf0, 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8,
  28. 0x68, 0xe8, 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8, 0x04,
  29. 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4, 0x14, 0x94, 0x54, 0xd4,
  30. 0x34, 0xb4, 0x74, 0xf4, 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c,
  31. 0xec, 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc, 0x02, 0x82,
  32. 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2, 0x12, 0x92, 0x52, 0xd2, 0x32,
  33. 0xb2, 0x72, 0xf2, 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
  34. 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa, 0x06, 0x86, 0x46,
  35. 0xc6, 0x26, 0xa6, 0x66, 0xe6, 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6,
  36. 0x76, 0xf6, 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee, 0x1e,
  37. 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe, 0x01, 0x81, 0x41, 0xc1,
  38. 0x21, 0xa1, 0x61, 0xe1, 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71,
  39. 0xf1, 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9, 0x19, 0x99,
  40. 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9, 0x05, 0x85, 0x45, 0xc5, 0x25,
  41. 0xa5, 0x65, 0xe5, 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
  42. 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed, 0x1d, 0x9d, 0x5d,
  43. 0xdd, 0x3d, 0xbd, 0x7d, 0xfd, 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3,
  44. 0x63, 0xe3, 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3, 0x0b,
  45. 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb, 0x1b, 0x9b, 0x5b, 0xdb,
  46. 0x3b, 0xbb, 0x7b, 0xfb, 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67,
  47. 0xe7, 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7, 0x0f, 0x8f,
  48. 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, 0x1f, 0x9f, 0x5f, 0xdf, 0x3f,
  49. 0xbf, 0x7f, 0xff };
  50. /*
  51. * wMaxPacketSize of pcm endpoints.
  52. * keep synced with rates_in_packet_size and rates_out_packet_size in pcm.c
  53. * fpp: frames per isopacket
  54. *
  55. * CAUTION: keep sizeof <= buffer[] in usb6fire_fw_init
  56. */
  57. static const u8 ep_w_max_packet_size[] = {
  58. 0xe4, 0x00, 0xe4, 0x00, /* alt 1: 228 EP2 and EP6 (7 fpp) */
  59. 0xa4, 0x01, 0xa4, 0x01, /* alt 2: 420 EP2 and EP6 (13 fpp)*/
  60. 0x94, 0x01, 0x5c, 0x02 /* alt 3: 404 EP2 and 604 EP6 (25 fpp) */
  61. };
  62. static const u8 known_fw_versions[][4] = {
  63. { 0x03, 0x01, 0x0b, 0x00 }
  64. };
  65. struct ihex_record {
  66. u16 address;
  67. u8 len;
  68. u8 data[256];
  69. char error; /* true if an error occured parsing this record */
  70. u8 max_len; /* maximum record length in whole ihex */
  71. /* private */
  72. const char *txt_data;
  73. unsigned int txt_length;
  74. unsigned int txt_offset; /* current position in txt_data */
  75. };
  76. static u8 usb6fire_fw_ihex_nibble(const u8 n)
  77. {
  78. if (n >= '0' && n <= '9')
  79. return n - '0';
  80. else if (n >= 'A' && n <= 'F')
  81. return n - ('A' - 10);
  82. else if (n >= 'a' && n <= 'f')
  83. return n - ('a' - 10);
  84. return 0;
  85. }
  86. static u8 usb6fire_fw_ihex_hex(const u8 *data, u8 *crc)
  87. {
  88. u8 val = (usb6fire_fw_ihex_nibble(data[0]) << 4) |
  89. usb6fire_fw_ihex_nibble(data[1]);
  90. *crc += val;
  91. return val;
  92. }
  93. /*
  94. * returns true if record is available, false otherwise.
  95. * iff an error occured, false will be returned and record->error will be true.
  96. */
  97. static bool usb6fire_fw_ihex_next_record(struct ihex_record *record)
  98. {
  99. u8 crc = 0;
  100. u8 type;
  101. int i;
  102. record->error = false;
  103. /* find begin of record (marked by a colon) */
  104. while (record->txt_offset < record->txt_length
  105. && record->txt_data[record->txt_offset] != ':')
  106. record->txt_offset++;
  107. if (record->txt_offset == record->txt_length)
  108. return false;
  109. /* number of characters needed for len, addr and type entries */
  110. record->txt_offset++;
  111. if (record->txt_offset + 8 > record->txt_length) {
  112. record->error = true;
  113. return false;
  114. }
  115. record->len = usb6fire_fw_ihex_hex(record->txt_data +
  116. record->txt_offset, &crc);
  117. record->txt_offset += 2;
  118. record->address = usb6fire_fw_ihex_hex(record->txt_data +
  119. record->txt_offset, &crc) << 8;
  120. record->txt_offset += 2;
  121. record->address |= usb6fire_fw_ihex_hex(record->txt_data +
  122. record->txt_offset, &crc);
  123. record->txt_offset += 2;
  124. type = usb6fire_fw_ihex_hex(record->txt_data +
  125. record->txt_offset, &crc);
  126. record->txt_offset += 2;
  127. /* number of characters needed for data and crc entries */
  128. if (record->txt_offset + 2 * (record->len + 1) > record->txt_length) {
  129. record->error = true;
  130. return false;
  131. }
  132. for (i = 0; i < record->len; i++) {
  133. record->data[i] = usb6fire_fw_ihex_hex(record->txt_data
  134. + record->txt_offset, &crc);
  135. record->txt_offset += 2;
  136. }
  137. usb6fire_fw_ihex_hex(record->txt_data + record->txt_offset, &crc);
  138. if (crc) {
  139. record->error = true;
  140. return false;
  141. }
  142. if (type == 1 || !record->len) /* eof */
  143. return false;
  144. else if (type == 0)
  145. return true;
  146. else {
  147. record->error = true;
  148. return false;
  149. }
  150. }
  151. static int usb6fire_fw_ihex_init(const struct firmware *fw,
  152. struct ihex_record *record)
  153. {
  154. record->txt_data = fw->data;
  155. record->txt_length = fw->size;
  156. record->txt_offset = 0;
  157. record->max_len = 0;
  158. /* read all records, if loop ends, record->error indicates,
  159. * whether ihex is valid. */
  160. while (usb6fire_fw_ihex_next_record(record))
  161. record->max_len = max(record->len, record->max_len);
  162. if (record->error)
  163. return -EINVAL;
  164. record->txt_offset = 0;
  165. return 0;
  166. }
  167. static int usb6fire_fw_ezusb_write(struct usb_device *device,
  168. int type, int value, char *data, int len)
  169. {
  170. int ret;
  171. ret = usb_control_msg(device, usb_sndctrlpipe(device, 0), type,
  172. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  173. value, 0, data, len, HZ);
  174. if (ret < 0)
  175. return ret;
  176. else if (ret != len)
  177. return -EIO;
  178. return 0;
  179. }
  180. static int usb6fire_fw_ezusb_read(struct usb_device *device,
  181. int type, int value, char *data, int len)
  182. {
  183. int ret = usb_control_msg(device, usb_rcvctrlpipe(device, 0), type,
  184. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, value,
  185. 0, data, len, HZ);
  186. if (ret < 0)
  187. return ret;
  188. else if (ret != len)
  189. return -EIO;
  190. return 0;
  191. }
  192. static int usb6fire_fw_fpga_write(struct usb_device *device,
  193. char *data, int len)
  194. {
  195. int actual_len;
  196. int ret;
  197. ret = usb_bulk_msg(device, usb_sndbulkpipe(device, FPGA_EP), data, len,
  198. &actual_len, HZ);
  199. if (ret < 0)
  200. return ret;
  201. else if (actual_len != len)
  202. return -EIO;
  203. return 0;
  204. }
  205. static int usb6fire_fw_ezusb_upload(
  206. struct usb_interface *intf, const char *fwname,
  207. unsigned int postaddr, u8 *postdata, unsigned int postlen)
  208. {
  209. int ret;
  210. u8 data;
  211. struct usb_device *device = interface_to_usbdev(intf);
  212. const struct firmware *fw = 0;
  213. struct ihex_record *rec = kmalloc(sizeof(struct ihex_record),
  214. GFP_KERNEL);
  215. if (!rec)
  216. return -ENOMEM;
  217. ret = request_firmware(&fw, fwname, &device->dev);
  218. if (ret < 0) {
  219. kfree(rec);
  220. snd_printk(KERN_ERR PREFIX "error requesting ezusb "
  221. "firmware %s.\n", fwname);
  222. return ret;
  223. }
  224. ret = usb6fire_fw_ihex_init(fw, rec);
  225. if (ret < 0) {
  226. kfree(rec);
  227. snd_printk(KERN_ERR PREFIX "error validating ezusb "
  228. "firmware %s.\n", fwname);
  229. return ret;
  230. }
  231. /* upload firmware image */
  232. data = 0x01; /* stop ezusb cpu */
  233. ret = usb6fire_fw_ezusb_write(device, 0xa0, 0xe600, &data, 1);
  234. if (ret < 0) {
  235. kfree(rec);
  236. release_firmware(fw);
  237. snd_printk(KERN_ERR PREFIX "unable to upload ezusb "
  238. "firmware %s: begin message.\n", fwname);
  239. return ret;
  240. }
  241. while (usb6fire_fw_ihex_next_record(rec)) { /* write firmware */
  242. ret = usb6fire_fw_ezusb_write(device, 0xa0, rec->address,
  243. rec->data, rec->len);
  244. if (ret < 0) {
  245. kfree(rec);
  246. release_firmware(fw);
  247. snd_printk(KERN_ERR PREFIX "unable to upload ezusb "
  248. "firmware %s: data urb.\n", fwname);
  249. return ret;
  250. }
  251. }
  252. release_firmware(fw);
  253. kfree(rec);
  254. if (postdata) { /* write data after firmware has been uploaded */
  255. ret = usb6fire_fw_ezusb_write(device, 0xa0, postaddr,
  256. postdata, postlen);
  257. if (ret < 0) {
  258. snd_printk(KERN_ERR PREFIX "unable to upload ezusb "
  259. "firmware %s: post urb.\n", fwname);
  260. return ret;
  261. }
  262. }
  263. data = 0x00; /* resume ezusb cpu */
  264. ret = usb6fire_fw_ezusb_write(device, 0xa0, 0xe600, &data, 1);
  265. if (ret < 0) {
  266. release_firmware(fw);
  267. snd_printk(KERN_ERR PREFIX "unable to upload ezusb "
  268. "firmware %s: end message.\n", fwname);
  269. return ret;
  270. }
  271. return 0;
  272. }
  273. static int usb6fire_fw_fpga_upload(
  274. struct usb_interface *intf, const char *fwname)
  275. {
  276. int ret;
  277. int i;
  278. struct usb_device *device = interface_to_usbdev(intf);
  279. u8 *buffer = kmalloc(FPGA_BUFSIZE, GFP_KERNEL);
  280. const char *c;
  281. const char *end;
  282. const struct firmware *fw;
  283. if (!buffer)
  284. return -ENOMEM;
  285. ret = request_firmware(&fw, fwname, &device->dev);
  286. if (ret < 0) {
  287. snd_printk(KERN_ERR PREFIX "unable to get fpga firmware %s.\n",
  288. fwname);
  289. kfree(buffer);
  290. return -EIO;
  291. }
  292. c = fw->data;
  293. end = fw->data + fw->size;
  294. ret = usb6fire_fw_ezusb_write(device, 8, 0, NULL, 0);
  295. if (ret < 0) {
  296. kfree(buffer);
  297. release_firmware(fw);
  298. snd_printk(KERN_ERR PREFIX "unable to upload fpga firmware: "
  299. "begin urb.\n");
  300. return ret;
  301. }
  302. while (c != end) {
  303. for (i = 0; c != end && i < FPGA_BUFSIZE; i++, c++)
  304. buffer[i] = BIT_REVERSE_TABLE[(u8) *c];
  305. ret = usb6fire_fw_fpga_write(device, buffer, i);
  306. if (ret < 0) {
  307. release_firmware(fw);
  308. kfree(buffer);
  309. snd_printk(KERN_ERR PREFIX "unable to upload fpga "
  310. "firmware: fw urb.\n");
  311. return ret;
  312. }
  313. }
  314. release_firmware(fw);
  315. kfree(buffer);
  316. ret = usb6fire_fw_ezusb_write(device, 9, 0, NULL, 0);
  317. if (ret < 0) {
  318. snd_printk(KERN_ERR PREFIX "unable to upload fpga firmware: "
  319. "end urb.\n");
  320. return ret;
  321. }
  322. return 0;
  323. }
  324. /* check, if the firmware version the devices has currently loaded
  325. * is known by this driver. 'version' needs to have 4 bytes version
  326. * info data. */
  327. static int usb6fire_fw_check(u8 *version)
  328. {
  329. int i;
  330. for (i = 0; i < ARRAY_SIZE(known_fw_versions); i++)
  331. if (!memcmp(version, known_fw_versions + i, 4))
  332. return 0;
  333. snd_printk(KERN_ERR PREFIX "invalid fimware version in device: "
  334. "%02x %02x %02x %02x. "
  335. "please reconnect to power. if this failure "
  336. "still happens, check your firmware installation.",
  337. version[0], version[1], version[2], version[3]);
  338. return -EINVAL;
  339. }
  340. int usb6fire_fw_init(struct usb_interface *intf)
  341. {
  342. int i;
  343. int ret;
  344. struct usb_device *device = interface_to_usbdev(intf);
  345. /* buffer: 8 receiving bytes from device and
  346. * sizeof(EP_W_MAX_PACKET_SIZE) bytes for non-const copy */
  347. u8 buffer[12];
  348. ret = usb6fire_fw_ezusb_read(device, 1, 0, buffer, 8);
  349. if (ret < 0) {
  350. snd_printk(KERN_ERR PREFIX "unable to receive device "
  351. "firmware state.\n");
  352. return ret;
  353. }
  354. if (buffer[0] != 0xeb || buffer[1] != 0xaa || buffer[2] != 0x55) {
  355. snd_printk(KERN_ERR PREFIX "unknown device firmware state "
  356. "received from device: ");
  357. for (i = 0; i < 8; i++)
  358. snd_printk("%02x ", buffer[i]);
  359. snd_printk("\n");
  360. return -EIO;
  361. }
  362. /* do we need fpga loader ezusb firmware? */
  363. if (buffer[3] == 0x01) {
  364. ret = usb6fire_fw_ezusb_upload(intf,
  365. "6fire/dmx6firel2.ihx", 0, NULL, 0);
  366. if (ret < 0)
  367. return ret;
  368. return FW_NOT_READY;
  369. }
  370. /* do we need fpga firmware and application ezusb firmware? */
  371. else if (buffer[3] == 0x02) {
  372. ret = usb6fire_fw_check(buffer + 4);
  373. if (ret < 0)
  374. return ret;
  375. ret = usb6fire_fw_fpga_upload(intf, "6fire/dmx6firecf.bin");
  376. if (ret < 0)
  377. return ret;
  378. memcpy(buffer, ep_w_max_packet_size,
  379. sizeof(ep_w_max_packet_size));
  380. ret = usb6fire_fw_ezusb_upload(intf, "6fire/dmx6fireap.ihx",
  381. 0x0003, buffer, sizeof(ep_w_max_packet_size));
  382. if (ret < 0)
  383. return ret;
  384. return FW_NOT_READY;
  385. }
  386. /* all fw loaded? */
  387. else if (buffer[3] == 0x03)
  388. return usb6fire_fw_check(buffer + 4);
  389. /* unknown data? */
  390. else {
  391. snd_printk(KERN_ERR PREFIX "unknown device firmware state "
  392. "received from device: ");
  393. for (i = 0; i < 8; i++)
  394. snd_printk("%02x ", buffer[i]);
  395. snd_printk("\n");
  396. return -EIO;
  397. }
  398. return 0;
  399. }