emi62.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. /* thanks to drivers/usb/serial/keyspan_pda.c code */
  44. static int emi62_writememory(struct usb_device *dev, int address,
  45. const unsigned char *data, int length,
  46. __u8 request)
  47. {
  48. int result;
  49. unsigned char *buffer = kmemdup(data, length, GFP_KERNEL);
  50. if (!buffer) {
  51. dev_err(&dev->dev, "kmalloc(%d) failed.\n", length);
  52. return -ENOMEM;
  53. }
  54. /* Note: usb_control_msg returns negative value on error or length of the
  55. * data that was written! */
  56. result = usb_control_msg (dev, usb_sndctrlpipe(dev, 0), request, 0x40, address, 0, buffer, length, 300);
  57. kfree (buffer);
  58. return result;
  59. }
  60. /* thanks to drivers/usb/serial/keyspan_pda.c code */
  61. static int emi62_set_reset (struct usb_device *dev, unsigned char reset_bit)
  62. {
  63. int response;
  64. dev_info(&dev->dev, "%s - %d\n", __func__, reset_bit);
  65. response = emi62_writememory (dev, CPUCS_REG, &reset_bit, 1, 0xa0);
  66. if (response < 0)
  67. dev_err(&dev->dev, "set_reset (%d) failed\n", reset_bit);
  68. return response;
  69. }
  70. #define FW_LOAD_SIZE 1023
  71. static int emi62_load_firmware (struct usb_device *dev)
  72. {
  73. const struct firmware *loader_fw = NULL;
  74. const struct firmware *bitstream_fw = NULL;
  75. const struct firmware *firmware_fw = NULL;
  76. const struct ihex_binrec *rec;
  77. int err = -ENOMEM;
  78. int i;
  79. __u32 addr; /* Address to write */
  80. __u8 *buf;
  81. dev_dbg(&dev->dev, "load_firmware\n");
  82. buf = kmalloc(FW_LOAD_SIZE, GFP_KERNEL);
  83. if (!buf)
  84. goto wraperr;
  85. err = request_ihex_firmware(&loader_fw, "emi62/loader.fw", &dev->dev);
  86. if (err)
  87. goto nofw;
  88. err = request_ihex_firmware(&bitstream_fw, "emi62/bitstream.fw",
  89. &dev->dev);
  90. if (err)
  91. goto nofw;
  92. err = request_ihex_firmware(&firmware_fw, FIRMWARE_FW, &dev->dev);
  93. if (err) {
  94. nofw:
  95. goto wraperr;
  96. }
  97. /* Assert reset (stop the CPU in the EMI) */
  98. err = emi62_set_reset(dev,1);
  99. if (err < 0)
  100. goto wraperr;
  101. rec = (const struct ihex_binrec *)loader_fw->data;
  102. /* 1. We need to put the loader for the FPGA into the EZ-USB */
  103. while (rec) {
  104. err = emi62_writememory(dev, be32_to_cpu(rec->addr),
  105. rec->data, be16_to_cpu(rec->len),
  106. ANCHOR_LOAD_INTERNAL);
  107. if (err < 0)
  108. goto wraperr;
  109. rec = ihex_next_binrec(rec);
  110. }
  111. /* De-assert reset (let the CPU run) */
  112. err = emi62_set_reset(dev,0);
  113. if (err < 0)
  114. goto wraperr;
  115. msleep(250); /* let device settle */
  116. /* 2. We upload the FPGA firmware into the EMI
  117. * Note: collect up to 1023 (yes!) bytes and send them with
  118. * a single request. This is _much_ faster! */
  119. rec = (const struct ihex_binrec *)bitstream_fw->data;
  120. do {
  121. i = 0;
  122. addr = be32_to_cpu(rec->addr);
  123. /* intel hex records are terminated with type 0 element */
  124. while (rec && (i + be16_to_cpu(rec->len) < FW_LOAD_SIZE)) {
  125. memcpy(buf + i, rec->data, be16_to_cpu(rec->len));
  126. i += be16_to_cpu(rec->len);
  127. rec = ihex_next_binrec(rec);
  128. }
  129. err = emi62_writememory(dev, addr, buf, i, ANCHOR_LOAD_FPGA);
  130. if (err < 0)
  131. goto wraperr;
  132. } while (rec);
  133. /* Assert reset (stop the CPU in the EMI) */
  134. err = emi62_set_reset(dev,1);
  135. if (err < 0)
  136. goto wraperr;
  137. /* 3. We need to put the loader for the firmware into the EZ-USB (again...) */
  138. for (rec = (const struct ihex_binrec *)loader_fw->data;
  139. rec; rec = ihex_next_binrec(rec)) {
  140. err = emi62_writememory(dev, be32_to_cpu(rec->addr),
  141. rec->data, be16_to_cpu(rec->len),
  142. ANCHOR_LOAD_INTERNAL);
  143. if (err < 0)
  144. goto wraperr;
  145. }
  146. /* De-assert reset (let the CPU run) */
  147. err = emi62_set_reset(dev,0);
  148. if (err < 0)
  149. goto wraperr;
  150. msleep(250); /* let device settle */
  151. /* 4. We put the part of the firmware that lies in the external RAM into the EZ-USB */
  152. for (rec = (const struct ihex_binrec *)firmware_fw->data;
  153. rec; rec = ihex_next_binrec(rec)) {
  154. if (!INTERNAL_RAM(be32_to_cpu(rec->addr))) {
  155. err = emi62_writememory(dev, be32_to_cpu(rec->addr),
  156. rec->data, be16_to_cpu(rec->len),
  157. ANCHOR_LOAD_EXTERNAL);
  158. if (err < 0)
  159. goto wraperr;
  160. }
  161. }
  162. /* Assert reset (stop the CPU in the EMI) */
  163. err = emi62_set_reset(dev,1);
  164. if (err < 0)
  165. goto wraperr;
  166. for (rec = (const struct ihex_binrec *)firmware_fw->data;
  167. rec; rec = ihex_next_binrec(rec)) {
  168. if (INTERNAL_RAM(be32_to_cpu(rec->addr))) {
  169. err = emi62_writememory(dev, be32_to_cpu(rec->addr),
  170. rec->data, be16_to_cpu(rec->len),
  171. ANCHOR_LOAD_EXTERNAL);
  172. if (err < 0)
  173. goto wraperr;
  174. }
  175. }
  176. /* De-assert reset (let the CPU run) */
  177. err = emi62_set_reset(dev,0);
  178. if (err < 0)
  179. goto wraperr;
  180. msleep(250); /* let device settle */
  181. release_firmware(loader_fw);
  182. release_firmware(bitstream_fw);
  183. release_firmware(firmware_fw);
  184. kfree(buf);
  185. /* return 1 to fail the driver inialization
  186. * and give real driver change to load */
  187. return 1;
  188. wraperr:
  189. if (err < 0)
  190. dev_err(&dev->dev,"%s - error loading firmware: error = %d\n",
  191. __func__, err);
  192. release_firmware(loader_fw);
  193. release_firmware(bitstream_fw);
  194. release_firmware(firmware_fw);
  195. kfree(buf);
  196. dev_err(&dev->dev, "Error\n");
  197. return err;
  198. }
  199. static const struct usb_device_id id_table[] = {
  200. { USB_DEVICE(EMI62_VENDOR_ID, EMI62_PRODUCT_ID) },
  201. { } /* Terminating entry */
  202. };
  203. MODULE_DEVICE_TABLE (usb, id_table);
  204. static int emi62_probe(struct usb_interface *intf, const struct usb_device_id *id)
  205. {
  206. struct usb_device *dev = interface_to_usbdev(intf);
  207. dev_dbg(&intf->dev, "emi62_probe\n");
  208. dev_info(&intf->dev, "%s start\n", __func__);
  209. emi62_load_firmware(dev);
  210. /* do not return the driver context, let real audio driver do that */
  211. return -EIO;
  212. }
  213. static void emi62_disconnect(struct usb_interface *intf)
  214. {
  215. }
  216. static struct usb_driver emi62_driver = {
  217. .name = "emi62 - firmware loader",
  218. .probe = emi62_probe,
  219. .disconnect = emi62_disconnect,
  220. .id_table = id_table,
  221. };
  222. module_usb_driver(emi62_driver);
  223. MODULE_AUTHOR("Tapio Laxström");
  224. MODULE_DESCRIPTION("Emagic EMI 6|2m firmware loader.");
  225. MODULE_LICENSE("GPL");
  226. MODULE_FIRMWARE("emi62/loader.fw");
  227. MODULE_FIRMWARE("emi62/bitstream.fw");
  228. MODULE_FIRMWARE(FIRMWARE_FW);
  229. /* vi:ai:syntax=c:sw=8:ts=8:tw=80
  230. */