emi62.c 9.0 KB

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