emi62.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * Emagic EMI 2|6 usb audio interface firmware loader.
  3. * Copyright (C) 2002
  4. * Tapio Laxström (tapio.laxstrom@iptime.fi)
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License, as published by
  8. * the Free Software Foundation, version 2.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/errno.h>
  12. #include <linux/slab.h>
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/usb.h>
  16. #include <linux/delay.h>
  17. #include <linux/firmware.h>
  18. #include <linux/ihex.h>
  19. /* include firmware (variables)*/
  20. /* FIXME: This is quick and dirty solution! */
  21. #define SPDIF /* if you want SPDIF comment next line */
  22. //#undef SPDIF /* if you want MIDI uncomment this line */
  23. #ifdef SPDIF
  24. #define FIRMWARE_FW "emi62/spdif.fw"
  25. #else
  26. #define FIRMWARE_FW "emi62/midi.fw"
  27. #endif
  28. #define EMI62_VENDOR_ID 0x086a /* Emagic Soft-und Hardware GmBH */
  29. #define EMI62_PRODUCT_ID 0x0110 /* EMI 6|2m without firmware */
  30. #define ANCHOR_LOAD_INTERNAL 0xA0 /* Vendor specific request code for Anchor Upload/Download (This one is implemented in the core) */
  31. #define ANCHOR_LOAD_EXTERNAL 0xA3 /* This command is not implemented in the core. Requires firmware */
  32. #define ANCHOR_LOAD_FPGA 0xA5 /* This command is not implemented in the core. Requires firmware. Emagic extension */
  33. #define MAX_INTERNAL_ADDRESS 0x1B3F /* This is the highest internal RAM address for the AN2131Q */
  34. #define CPUCS_REG 0x7F92 /* EZ-USB Control and Status Register. Bit 0 controls 8051 reset */
  35. #define INTERNAL_RAM(address) (address <= MAX_INTERNAL_ADDRESS)
  36. static int emi62_writememory(struct usb_device *dev, int address,
  37. const unsigned char *data, int length,
  38. __u8 bRequest);
  39. static int emi62_set_reset(struct usb_device *dev, unsigned char reset_bit);
  40. static int emi62_load_firmware (struct usb_device *dev);
  41. static int emi62_probe(struct usb_interface *intf, const struct usb_device_id *id);
  42. static void emi62_disconnect(struct usb_interface *intf);
  43. static int __init emi62_init (void);
  44. static void __exit emi62_exit (void);
  45. /* thanks to drivers/usb/serial/keyspan_pda.c code */
  46. static int emi62_writememory(struct usb_device *dev, int address,
  47. const unsigned char *data, int length,
  48. __u8 request)
  49. {
  50. int result;
  51. unsigned char *buffer = kmemdup(data, length, GFP_KERNEL);
  52. if (!buffer) {
  53. err("emi62: kmalloc(%d) failed.", length);
  54. return -ENOMEM;
  55. }
  56. /* Note: usb_control_msg returns negative value on error or length of the
  57. * data that was written! */
  58. result = usb_control_msg (dev, usb_sndctrlpipe(dev, 0), request, 0x40, address, 0, buffer, length, 300);
  59. kfree (buffer);
  60. return result;
  61. }
  62. /* thanks to drivers/usb/serial/keyspan_pda.c code */
  63. static int emi62_set_reset (struct usb_device *dev, unsigned char reset_bit)
  64. {
  65. int response;
  66. dev_info(&dev->dev, "%s - %d\n", __func__, reset_bit);
  67. response = emi62_writememory (dev, CPUCS_REG, &reset_bit, 1, 0xa0);
  68. if (response < 0) {
  69. err("emi62: set_reset (%d) failed", reset_bit);
  70. }
  71. return response;
  72. }
  73. #define FW_LOAD_SIZE 1023
  74. static int emi62_load_firmware (struct usb_device *dev)
  75. {
  76. const struct firmware *loader_fw = NULL;
  77. const struct firmware *bitstream_fw = NULL;
  78. const struct firmware *firmware_fw = NULL;
  79. const struct ihex_binrec *rec;
  80. int err;
  81. int i;
  82. __u32 addr; /* Address to write */
  83. __u8 *buf;
  84. dev_dbg(&dev->dev, "load_firmware\n");
  85. buf = kmalloc(FW_LOAD_SIZE, GFP_KERNEL);
  86. if (!buf) {
  87. err( "%s - error loading firmware: error = %d", __func__, -ENOMEM);
  88. err = -ENOMEM;
  89. goto wraperr;
  90. }
  91. err = request_ihex_firmware(&loader_fw, "emi62/loader.fw", &dev->dev);
  92. if (err)
  93. goto nofw;
  94. err = request_ihex_firmware(&bitstream_fw, "emi62/bitstream.fw",
  95. &dev->dev);
  96. if (err)
  97. goto nofw;
  98. err = request_ihex_firmware(&firmware_fw, FIRMWARE_FW, &dev->dev);
  99. if (err) {
  100. nofw:
  101. err( "%s - request_firmware() failed", __func__);
  102. goto wraperr;
  103. }
  104. /* Assert reset (stop the CPU in the EMI) */
  105. err = emi62_set_reset(dev,1);
  106. if (err < 0) {
  107. err("%s - error loading firmware: error = %d", __func__, err);
  108. goto wraperr;
  109. }
  110. rec = (const struct ihex_binrec *)loader_fw->data;
  111. /* 1. We need to put the loader for the FPGA into the EZ-USB */
  112. while (rec) {
  113. err = emi62_writememory(dev, be32_to_cpu(rec->addr),
  114. rec->data, be16_to_cpu(rec->len),
  115. ANCHOR_LOAD_INTERNAL);
  116. if (err < 0) {
  117. err("%s - error loading firmware: error = %d", __func__, err);
  118. goto wraperr;
  119. }
  120. rec = ihex_next_binrec(rec);
  121. }
  122. /* De-assert reset (let the CPU run) */
  123. err = emi62_set_reset(dev,0);
  124. if (err < 0) {
  125. err("%s - error loading firmware: error = %d", __func__, err);
  126. goto wraperr;
  127. }
  128. msleep(250); /* let device settle */
  129. /* 2. We upload the FPGA firmware into the EMI
  130. * Note: collect up to 1023 (yes!) bytes and send them with
  131. * a single request. This is _much_ faster! */
  132. rec = (const struct ihex_binrec *)bitstream_fw->data;
  133. do {
  134. i = 0;
  135. addr = be32_to_cpu(rec->addr);
  136. /* intel hex records are terminated with type 0 element */
  137. while (rec && (i + be16_to_cpu(rec->len) < FW_LOAD_SIZE)) {
  138. memcpy(buf + i, rec->data, be16_to_cpu(rec->len));
  139. i += be16_to_cpu(rec->len);
  140. rec = ihex_next_binrec(rec);
  141. }
  142. err = emi62_writememory(dev, addr, buf, i, ANCHOR_LOAD_FPGA);
  143. if (err < 0) {
  144. err("%s - error loading firmware: error = %d", __func__, err);
  145. goto wraperr;
  146. }
  147. } while (rec);
  148. /* Assert reset (stop the CPU in the EMI) */
  149. err = emi62_set_reset(dev,1);
  150. if (err < 0) {
  151. err("%s - error loading firmware: error = %d", __func__, err);
  152. goto wraperr;
  153. }
  154. /* 3. We need to put the loader for the firmware into the EZ-USB (again...) */
  155. for (rec = (const struct ihex_binrec *)loader_fw->data;
  156. rec; rec = ihex_next_binrec(rec)) {
  157. err = emi62_writememory(dev, be32_to_cpu(rec->addr),
  158. rec->data, be16_to_cpu(rec->len),
  159. ANCHOR_LOAD_INTERNAL);
  160. if (err < 0) {
  161. err("%s - error loading firmware: error = %d", __func__, err);
  162. goto wraperr;
  163. }
  164. }
  165. /* De-assert reset (let the CPU run) */
  166. err = emi62_set_reset(dev,0);
  167. if (err < 0) {
  168. err("%s - error loading firmware: error = %d", __func__, err);
  169. goto wraperr;
  170. }
  171. msleep(250); /* let device settle */
  172. /* 4. We put the part of the firmware that lies in the external RAM into the EZ-USB */
  173. for (rec = (const struct ihex_binrec *)firmware_fw->data;
  174. rec; rec = ihex_next_binrec(rec)) {
  175. if (!INTERNAL_RAM(be32_to_cpu(rec->addr))) {
  176. err = emi62_writememory(dev, be32_to_cpu(rec->addr),
  177. rec->data, be16_to_cpu(rec->len),
  178. ANCHOR_LOAD_EXTERNAL);
  179. if (err < 0) {
  180. err("%s - error loading firmware: error = %d", __func__, err);
  181. goto wraperr;
  182. }
  183. }
  184. }
  185. /* Assert reset (stop the CPU in the EMI) */
  186. err = emi62_set_reset(dev,1);
  187. if (err < 0) {
  188. err("%s - error loading firmware: error = %d", __func__, err);
  189. goto wraperr;
  190. }
  191. for (rec = (const struct ihex_binrec *)firmware_fw->data;
  192. rec; rec = ihex_next_binrec(rec)) {
  193. if (INTERNAL_RAM(be32_to_cpu(rec->addr))) {
  194. err = emi62_writememory(dev, be32_to_cpu(rec->addr),
  195. rec->data, be16_to_cpu(rec->len),
  196. ANCHOR_LOAD_EXTERNAL);
  197. if (err < 0) {
  198. err("%s - error loading firmware: error = %d", __func__, err);
  199. goto wraperr;
  200. }
  201. }
  202. }
  203. /* De-assert reset (let the CPU run) */
  204. err = emi62_set_reset(dev,0);
  205. if (err < 0) {
  206. err("%s - error loading firmware: error = %d", __func__, err);
  207. goto wraperr;
  208. }
  209. msleep(250); /* let device settle */
  210. release_firmware(loader_fw);
  211. release_firmware(bitstream_fw);
  212. release_firmware(firmware_fw);
  213. kfree(buf);
  214. /* return 1 to fail the driver inialization
  215. * and give real driver change to load */
  216. return 1;
  217. wraperr:
  218. release_firmware(loader_fw);
  219. release_firmware(bitstream_fw);
  220. release_firmware(firmware_fw);
  221. kfree(buf);
  222. dev_err(&dev->dev, "Error\n");
  223. return err;
  224. }
  225. static const struct usb_device_id id_table[] __devinitconst = {
  226. { USB_DEVICE(EMI62_VENDOR_ID, EMI62_PRODUCT_ID) },
  227. { } /* Terminating entry */
  228. };
  229. MODULE_DEVICE_TABLE (usb, id_table);
  230. static int emi62_probe(struct usb_interface *intf, const struct usb_device_id *id)
  231. {
  232. struct usb_device *dev = interface_to_usbdev(intf);
  233. dev_dbg(&intf->dev, "emi62_probe\n");
  234. dev_info(&intf->dev, "%s start\n", __func__);
  235. emi62_load_firmware(dev);
  236. /* do not return the driver context, let real audio driver do that */
  237. return -EIO;
  238. }
  239. static void emi62_disconnect(struct usb_interface *intf)
  240. {
  241. }
  242. static struct usb_driver emi62_driver = {
  243. .name = "emi62 - firmware loader",
  244. .probe = emi62_probe,
  245. .disconnect = emi62_disconnect,
  246. .id_table = id_table,
  247. };
  248. static int __init emi62_init (void)
  249. {
  250. int retval;
  251. retval = usb_register (&emi62_driver);
  252. if (retval)
  253. printk(KERN_ERR "adi-emi: registration failed\n");
  254. return retval;
  255. }
  256. static void __exit emi62_exit (void)
  257. {
  258. usb_deregister (&emi62_driver);
  259. }
  260. module_init(emi62_init);
  261. module_exit(emi62_exit);
  262. MODULE_AUTHOR("Tapio Laxström");
  263. MODULE_DESCRIPTION("Emagic EMI 6|2m firmware loader.");
  264. MODULE_LICENSE("GPL");
  265. MODULE_FIRMWARE("emi62/loader.fw");
  266. MODULE_FIRMWARE("emi62/bitstream.fw");
  267. MODULE_FIRMWARE(FIRMWARE_FW);
  268. /* vi:ai:syntax=c:sw=8:ts=8:tw=80
  269. */