emi26.c 7.6 KB

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