firmware.c 12 KB

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