emi62.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 = kmemdup(data, length, GFP_KERNEL);
  56. if (!buffer) {
  57. err("emi62: kmalloc(%d) failed.", length);
  58. return -ENOMEM;
  59. }
  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. if (err < 0) {
  109. err("%s - error loading firmware: error = %d", __FUNCTION__, err);
  110. goto wraperr;
  111. }
  112. msleep(250); /* let device settle */
  113. /* 2. We upload the FPGA firmware into the EMI
  114. * Note: collect up to 1023 (yes!) bytes and send them with
  115. * a single request. This is _much_ faster! */
  116. do {
  117. i = 0;
  118. addr = g_emi62bs[pos].address;
  119. /* intel hex records are terminated with type 0 element */
  120. while ((g_emi62bs[pos].type == 0) && (i + g_emi62bs[pos].length < FW_LOAD_SIZE)) {
  121. memcpy(buf + i, g_emi62bs[pos].data, g_emi62bs[pos].length);
  122. i += g_emi62bs[pos].length;
  123. pos++;
  124. }
  125. err = emi62_writememory(dev, addr, buf, i, ANCHOR_LOAD_FPGA);
  126. if (err < 0) {
  127. err("%s - error loading firmware: error = %d", __FUNCTION__, err);
  128. goto wraperr;
  129. }
  130. } while (i > 0);
  131. /* Assert reset (stop the CPU in the EMI) */
  132. err = emi62_set_reset(dev,1);
  133. if (err < 0) {
  134. err("%s - error loading firmware: error = %d", __FUNCTION__, err);
  135. goto wraperr;
  136. }
  137. /* 3. We need to put the loader for the firmware into the EZ-USB (again...) */
  138. for (i=0; g_emi62_loader[i].type == 0; i++) {
  139. err = emi62_writememory(dev, g_emi62_loader[i].address, g_emi62_loader[i].data, g_emi62_loader[i].length, ANCHOR_LOAD_INTERNAL);
  140. if (err < 0) {
  141. err("%s - error loading firmware: error = %d", __FUNCTION__, err);
  142. goto wraperr;
  143. }
  144. }
  145. /* De-assert reset (let the CPU run) */
  146. err = emi62_set_reset(dev,0);
  147. if (err < 0) {
  148. err("%s - error loading firmware: error = %d", __FUNCTION__, err);
  149. goto wraperr;
  150. }
  151. msleep(250); /* let device settle */
  152. /* 4. We put the part of the firmware that lies in the external RAM into the EZ-USB */
  153. /* FIXME: quick and dirty ifdefs */
  154. #ifdef SPDIF
  155. for (i=0; g_HexSpdifFw62[i].type == 0; i++) {
  156. if (!INTERNAL_RAM(g_HexSpdifFw62[i].address)) {
  157. err = emi62_writememory(dev, g_HexSpdifFw62[i].address, g_HexSpdifFw62[i].data, g_HexSpdifFw62[i].length, ANCHOR_LOAD_EXTERNAL);
  158. if (err < 0) {
  159. err("%s - error loading firmware: error = %d", __FUNCTION__, err);
  160. goto wraperr;
  161. }
  162. }
  163. }
  164. #else /* MIDI */
  165. for (i=0; g_HexMidiFw62[i].type == 0; i++) {
  166. if (!INTERNAL_RAM(g_HexMidiFw62[i].address)) {
  167. err = emi62_writememory(dev, g_HexMidiFw62[i].address, g_HexMidiFw62[i].data, g_HexMidiFw62[i].length, ANCHOR_LOAD_EXTERNAL);
  168. if (err < 0) {
  169. err("%s - error loading firmware: error = %d\n", __FUNCTION__, err);
  170. goto wraperr;
  171. return err;
  172. }
  173. }
  174. }
  175. #endif
  176. /* Assert reset (stop the CPU in the EMI) */
  177. err = emi62_set_reset(dev,1);
  178. if (err < 0) {
  179. err("%s - error loading firmware: error = %d", __FUNCTION__, err);
  180. goto wraperr;
  181. }
  182. /* FIXME: quick and dirty ifdefs */
  183. #ifdef SPDIF
  184. for (i=0; g_HexSpdifFw62[i].type == 0; i++) {
  185. if (INTERNAL_RAM(g_HexSpdifFw62[i].address)) {
  186. err = emi62_writememory(dev, g_HexSpdifFw62[i].address, g_HexSpdifFw62[i].data, g_HexSpdifFw62[i].length, ANCHOR_LOAD_INTERNAL);
  187. if (err < 0) {
  188. err("%s - error loading firmware: error = %d", __FUNCTION__, err);
  189. goto wraperr;
  190. }
  191. }
  192. }
  193. #else /* MIDI */
  194. for (i=0; g_HexMidiFw62[i].type == 0; i++) {
  195. if (INTERNAL_RAM(g_HexMidiFw62[i].address)) {
  196. err = emi62_writememory(dev, g_HexMidiFw62[i].address, g_HexMidiFw62[i].data, g_HexMidiFw62[i].length, ANCHOR_LOAD_INTERNAL);
  197. if (err < 0) {
  198. err("%s - error loading firmware: error = %d\n", __FUNCTION__, err);
  199. goto wraperr;
  200. }
  201. }
  202. }
  203. #endif
  204. /* De-assert reset (let the CPU run) */
  205. err = emi62_set_reset(dev,0);
  206. if (err < 0) {
  207. err("%s - error loading firmware: error = %d", __FUNCTION__, err);
  208. goto wraperr;
  209. }
  210. msleep(250); /* let device settle */
  211. kfree(buf);
  212. /* return 1 to fail the driver inialization
  213. * and give real driver change to load */
  214. return 1;
  215. wraperr:
  216. kfree(buf);
  217. dev_err(&dev->dev, "Error\n");
  218. return err;
  219. }
  220. static __devinitdata struct usb_device_id id_table [] = {
  221. { USB_DEVICE(EMI62_VENDOR_ID, EMI62_PRODUCT_ID) },
  222. { } /* Terminating entry */
  223. };
  224. MODULE_DEVICE_TABLE (usb, id_table);
  225. static int emi62_probe(struct usb_interface *intf, const struct usb_device_id *id)
  226. {
  227. struct usb_device *dev = interface_to_usbdev(intf);
  228. dev_dbg(&intf->dev, "emi62_probe\n");
  229. info("%s start", __FUNCTION__);
  230. emi62_load_firmware(dev);
  231. /* do not return the driver context, let real audio driver do that */
  232. return -EIO;
  233. }
  234. static void emi62_disconnect(struct usb_interface *intf)
  235. {
  236. }
  237. static struct usb_driver emi62_driver = {
  238. .name = "emi62 - firmware loader",
  239. .probe = emi62_probe,
  240. .disconnect = emi62_disconnect,
  241. .id_table = id_table,
  242. };
  243. static int __init emi62_init (void)
  244. {
  245. int retval;
  246. retval = usb_register (&emi62_driver);
  247. if (retval)
  248. printk(KERN_ERR "adi-emi: registration failed\n");
  249. return retval;
  250. }
  251. static void __exit emi62_exit (void)
  252. {
  253. usb_deregister (&emi62_driver);
  254. }
  255. module_init(emi62_init);
  256. module_exit(emi62_exit);
  257. MODULE_AUTHOR("Tapio Laxström");
  258. MODULE_DESCRIPTION("Emagic EMI 6|2m firmware loader.");
  259. MODULE_LICENSE("GPL");
  260. /* vi:ai:syntax=c:sw=8:ts=8:tw=80
  261. */