emi62.c 8.8 KB

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