emi26.c 7.4 KB

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