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