emi26.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 nofw;
  79. err = request_ihex_firmware(&bitstream_fw, "emi26/bitstream.fw",
  80. &dev->dev);
  81. if (err)
  82. goto nofw;
  83. err = request_ihex_firmware(&firmware_fw, "emi26/firmware.fw",
  84. &dev->dev);
  85. if (err) {
  86. nofw:
  87. dev_err(&dev->dev, "%s - request_firmware() failed\n",
  88. __func__);
  89. goto wraperr;
  90. }
  91. /* Assert reset (stop the CPU in the EMI) */
  92. err = emi26_set_reset(dev,1);
  93. if (err < 0)
  94. goto wraperr;
  95. rec = (const struct ihex_binrec *)loader_fw->data;
  96. /* 1. We need to put the loader for the FPGA into the EZ-USB */
  97. while (rec) {
  98. err = emi26_writememory(dev, be32_to_cpu(rec->addr),
  99. rec->data, be16_to_cpu(rec->len),
  100. ANCHOR_LOAD_INTERNAL);
  101. if (err < 0)
  102. goto wraperr;
  103. rec = ihex_next_binrec(rec);
  104. }
  105. /* De-assert reset (let the CPU run) */
  106. err = emi26_set_reset(dev,0);
  107. if (err < 0)
  108. goto wraperr;
  109. msleep(250); /* let device settle */
  110. /* 2. We upload the FPGA firmware into the EMI
  111. * Note: collect up to 1023 (yes!) bytes and send them with
  112. * a single request. This is _much_ faster! */
  113. rec = (const struct ihex_binrec *)bitstream_fw->data;
  114. do {
  115. i = 0;
  116. addr = be32_to_cpu(rec->addr);
  117. /* intel hex records are terminated with type 0 element */
  118. while (rec && (i + be16_to_cpu(rec->len) < FW_LOAD_SIZE)) {
  119. memcpy(buf + i, rec->data, be16_to_cpu(rec->len));
  120. i += be16_to_cpu(rec->len);
  121. rec = ihex_next_binrec(rec);
  122. }
  123. err = emi26_writememory(dev, addr, buf, i, ANCHOR_LOAD_FPGA);
  124. if (err < 0)
  125. goto wraperr;
  126. } while (rec);
  127. /* Assert reset (stop the CPU in the EMI) */
  128. err = emi26_set_reset(dev,1);
  129. if (err < 0)
  130. goto wraperr;
  131. /* 3. We need to put the loader for the firmware into the EZ-USB (again...) */
  132. for (rec = (const struct ihex_binrec *)loader_fw->data;
  133. rec; rec = ihex_next_binrec(rec)) {
  134. err = emi26_writememory(dev, be32_to_cpu(rec->addr),
  135. rec->data, be16_to_cpu(rec->len),
  136. ANCHOR_LOAD_INTERNAL);
  137. if (err < 0)
  138. goto wraperr;
  139. }
  140. msleep(250); /* let device settle */
  141. /* De-assert reset (let the CPU run) */
  142. err = emi26_set_reset(dev,0);
  143. if (err < 0)
  144. goto wraperr;
  145. /* 4. We put the part of the firmware that lies in the external RAM into the EZ-USB */
  146. for (rec = (const struct ihex_binrec *)firmware_fw->data;
  147. rec; rec = ihex_next_binrec(rec)) {
  148. if (!INTERNAL_RAM(be32_to_cpu(rec->addr))) {
  149. err = emi26_writememory(dev, be32_to_cpu(rec->addr),
  150. rec->data, be16_to_cpu(rec->len),
  151. ANCHOR_LOAD_EXTERNAL);
  152. if (err < 0)
  153. goto wraperr;
  154. }
  155. }
  156. /* Assert reset (stop the CPU in the EMI) */
  157. err = emi26_set_reset(dev,1);
  158. if (err < 0)
  159. goto wraperr;
  160. for (rec = (const struct ihex_binrec *)firmware_fw->data;
  161. rec; rec = ihex_next_binrec(rec)) {
  162. if (INTERNAL_RAM(be32_to_cpu(rec->addr))) {
  163. err = emi26_writememory(dev, be32_to_cpu(rec->addr),
  164. rec->data, be16_to_cpu(rec->len),
  165. ANCHOR_LOAD_INTERNAL);
  166. if (err < 0)
  167. goto wraperr;
  168. }
  169. }
  170. /* De-assert reset (let the CPU run) */
  171. err = emi26_set_reset(dev,0);
  172. if (err < 0)
  173. goto wraperr;
  174. msleep(250); /* let device settle */
  175. /* return 1 to fail the driver inialization
  176. * and give real driver change to load */
  177. err = 1;
  178. wraperr:
  179. if (err < 0)
  180. dev_err(&dev->dev,"%s - error loading firmware: error = %d\n",
  181. __func__, err);
  182. release_firmware(loader_fw);
  183. release_firmware(bitstream_fw);
  184. release_firmware(firmware_fw);
  185. kfree(buf);
  186. return err;
  187. }
  188. static const struct usb_device_id id_table[] = {
  189. { USB_DEVICE(EMI26_VENDOR_ID, EMI26_PRODUCT_ID) },
  190. { USB_DEVICE(EMI26_VENDOR_ID, EMI26B_PRODUCT_ID) },
  191. { } /* Terminating entry */
  192. };
  193. MODULE_DEVICE_TABLE (usb, id_table);
  194. static int emi26_probe(struct usb_interface *intf, const struct usb_device_id *id)
  195. {
  196. struct usb_device *dev = interface_to_usbdev(intf);
  197. dev_info(&intf->dev, "%s start\n", __func__);
  198. emi26_load_firmware(dev);
  199. /* do not return the driver context, let real audio driver do that */
  200. return -EIO;
  201. }
  202. static void emi26_disconnect(struct usb_interface *intf)
  203. {
  204. }
  205. static struct usb_driver emi26_driver = {
  206. .name = "emi26 - firmware loader",
  207. .probe = emi26_probe,
  208. .disconnect = emi26_disconnect,
  209. .id_table = id_table,
  210. };
  211. module_usb_driver(emi26_driver);
  212. MODULE_AUTHOR("Tapio Laxström");
  213. MODULE_DESCRIPTION("Emagic EMI 2|6 firmware loader.");
  214. MODULE_LICENSE("GPL");
  215. MODULE_FIRMWARE("emi26/loader.fw");
  216. MODULE_FIRMWARE("emi26/bitstream.fw");
  217. MODULE_FIRMWARE("emi26/firmware.fw");
  218. /* vi:ai:syntax=c:sw=8:ts=8:tw=80
  219. */