emi26.c 7.7 KB

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