emi62.c 7.7 KB

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