emi62.c 9.0 KB

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