emi26.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. static int __init emi26_init (void);
  38. static void __exit emi26_exit (void);
  39. /* thanks to drivers/usb/serial/keyspan_pda.c code */
  40. static int emi26_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. err("emi26: kmalloc(%d) failed.", 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 emi26_set_reset (struct usb_device *dev, unsigned char reset_bit)
  58. {
  59. int response;
  60. info("%s - %d", __func__, reset_bit);
  61. /* printk(KERN_DEBUG "%s - %d", __func__, reset_bit); */
  62. response = emi26_writememory (dev, CPUCS_REG, &reset_bit, 1, 0xa0);
  63. if (response < 0) {
  64. err("emi26: set_reset (%d) failed", reset_bit);
  65. }
  66. return response;
  67. }
  68. #define FW_LOAD_SIZE 1023
  69. static int emi26_load_firmware (struct usb_device *dev)
  70. {
  71. const struct firmware *loader_fw = NULL;
  72. const struct firmware *bitstream_fw = NULL;
  73. const struct firmware *firmware_fw = NULL;
  74. const struct ihex_binrec *rec;
  75. int err;
  76. int i;
  77. __u32 addr; /* Address to write */
  78. __u8 *buf;
  79. buf = kmalloc(FW_LOAD_SIZE, GFP_KERNEL);
  80. if (!buf) {
  81. err( "%s - error loading firmware: error = %d", __func__, -ENOMEM);
  82. err = -ENOMEM;
  83. goto wraperr;
  84. }
  85. err = request_ihex_firmware(&loader_fw, "emi26/loader.fw", &dev->dev);
  86. if (err)
  87. goto nofw;
  88. err = request_ihex_firmware(&bitstream_fw, "emi26/bitstream.fw",
  89. &dev->dev);
  90. if (err)
  91. goto nofw;
  92. err = request_ihex_firmware(&firmware_fw, "emi26/firmware.fw",
  93. &dev->dev);
  94. if (err) {
  95. nofw:
  96. err( "%s - request_firmware() failed", __func__);
  97. goto wraperr;
  98. }
  99. /* Assert reset (stop the CPU in the EMI) */
  100. err = emi26_set_reset(dev,1);
  101. if (err < 0) {
  102. err( "%s - error loading firmware: error = %d", __func__, err);
  103. goto wraperr;
  104. }
  105. rec = (const struct ihex_binrec *)loader_fw->data;
  106. /* 1. We need to put the loader for the FPGA into the EZ-USB */
  107. while (rec) {
  108. err = emi26_writememory(dev, be32_to_cpu(rec->addr),
  109. rec->data, be16_to_cpu(rec->len),
  110. ANCHOR_LOAD_INTERNAL);
  111. if (err < 0) {
  112. err("%s - error loading firmware: error = %d", __func__, err);
  113. goto wraperr;
  114. }
  115. rec = ihex_next_binrec(rec);
  116. }
  117. /* De-assert reset (let the CPU run) */
  118. err = emi26_set_reset(dev,0);
  119. if (err < 0) {
  120. err("%s - error loading firmware: error = %d", __func__, err);
  121. goto wraperr;
  122. }
  123. msleep(250); /* let device settle */
  124. /* 2. We upload the FPGA firmware into the EMI
  125. * Note: collect up to 1023 (yes!) bytes and send them with
  126. * a single request. This is _much_ faster! */
  127. rec = (const struct ihex_binrec *)bitstream_fw->data;
  128. do {
  129. i = 0;
  130. addr = be32_to_cpu(rec->addr);
  131. /* intel hex records are terminated with type 0 element */
  132. while (rec && (i + be16_to_cpu(rec->len) < FW_LOAD_SIZE)) {
  133. memcpy(buf + i, rec->data, be16_to_cpu(rec->len));
  134. i += be16_to_cpu(rec->len);
  135. rec = ihex_next_binrec(rec);
  136. }
  137. err = emi26_writememory(dev, addr, buf, i, ANCHOR_LOAD_FPGA);
  138. if (err < 0) {
  139. err("%s - error loading firmware: error = %d", __func__, err);
  140. goto wraperr;
  141. }
  142. } while (i > 0);
  143. /* Assert reset (stop the CPU in the EMI) */
  144. err = emi26_set_reset(dev,1);
  145. if (err < 0) {
  146. err("%s - error loading firmware: error = %d", __func__, err);
  147. goto wraperr;
  148. }
  149. /* 3. We need to put the loader for the firmware into the EZ-USB (again...) */
  150. for (rec = (const struct ihex_binrec *)loader_fw->data;
  151. rec; rec = ihex_next_binrec(rec)) {
  152. err = emi26_writememory(dev, be32_to_cpu(rec->addr),
  153. rec->data, be16_to_cpu(rec->len),
  154. ANCHOR_LOAD_INTERNAL);
  155. if (err < 0) {
  156. err("%s - error loading firmware: error = %d", __func__, err);
  157. goto wraperr;
  158. }
  159. }
  160. msleep(250); /* let device settle */
  161. /* De-assert reset (let the CPU run) */
  162. err = emi26_set_reset(dev,0);
  163. if (err < 0) {
  164. err("%s - error loading firmware: error = %d", __func__, err);
  165. goto wraperr;
  166. }
  167. /* 4. We put the part of the firmware that lies in the external RAM into the EZ-USB */
  168. for (rec = (const struct ihex_binrec *)firmware_fw->data;
  169. rec; rec = ihex_next_binrec(rec)) {
  170. if (!INTERNAL_RAM(be32_to_cpu(rec->addr))) {
  171. err = emi26_writememory(dev, be32_to_cpu(rec->addr),
  172. rec->data, be16_to_cpu(rec->len),
  173. ANCHOR_LOAD_EXTERNAL);
  174. if (err < 0) {
  175. err("%s - error loading firmware: error = %d", __func__, err);
  176. goto wraperr;
  177. }
  178. }
  179. }
  180. /* Assert reset (stop the CPU in the EMI) */
  181. err = emi26_set_reset(dev,1);
  182. if (err < 0) {
  183. err("%s - error loading firmware: error = %d", __func__, err);
  184. goto wraperr;
  185. }
  186. for (rec = (const struct ihex_binrec *)firmware_fw->data;
  187. rec; rec = ihex_next_binrec(rec)) {
  188. if (INTERNAL_RAM(be32_to_cpu(rec->addr))) {
  189. err = emi26_writememory(dev, be32_to_cpu(rec->addr),
  190. rec->data, be16_to_cpu(rec->len),
  191. ANCHOR_LOAD_INTERNAL);
  192. if (err < 0) {
  193. err("%s - error loading firmware: error = %d", __func__, err);
  194. goto wraperr;
  195. }
  196. }
  197. }
  198. /* De-assert reset (let the CPU run) */
  199. err = emi26_set_reset(dev,0);
  200. if (err < 0) {
  201. err("%s - error loading firmware: error = %d", __func__, err);
  202. goto wraperr;
  203. }
  204. msleep(250); /* let device settle */
  205. /* return 1 to fail the driver inialization
  206. * and give real driver change to load */
  207. err = 1;
  208. wraperr:
  209. release_firmware(loader_fw);
  210. release_firmware(bitstream_fw);
  211. release_firmware(firmware_fw);
  212. kfree(buf);
  213. return err;
  214. }
  215. static struct usb_device_id id_table [] = {
  216. { USB_DEVICE(EMI26_VENDOR_ID, EMI26_PRODUCT_ID) },
  217. { USB_DEVICE(EMI26_VENDOR_ID, EMI26B_PRODUCT_ID) },
  218. { } /* Terminating entry */
  219. };
  220. MODULE_DEVICE_TABLE (usb, id_table);
  221. static int emi26_probe(struct usb_interface *intf, const struct usb_device_id *id)
  222. {
  223. struct usb_device *dev = interface_to_usbdev(intf);
  224. info("%s start", __func__);
  225. emi26_load_firmware(dev);
  226. /* do not return the driver context, let real audio driver do that */
  227. return -EIO;
  228. }
  229. static void emi26_disconnect(struct usb_interface *intf)
  230. {
  231. }
  232. static struct usb_driver emi26_driver = {
  233. .name = "emi26 - firmware loader",
  234. .probe = emi26_probe,
  235. .disconnect = emi26_disconnect,
  236. .id_table = id_table,
  237. };
  238. static int __init emi26_init (void)
  239. {
  240. return usb_register(&emi26_driver);
  241. }
  242. static void __exit emi26_exit (void)
  243. {
  244. usb_deregister (&emi26_driver);
  245. }
  246. module_init(emi26_init);
  247. module_exit(emi26_exit);
  248. MODULE_AUTHOR("Tapio Laxström");
  249. MODULE_DESCRIPTION("Emagic EMI 2|6 firmware loader.");
  250. MODULE_LICENSE("GPL");
  251. MODULE_FIRMWARE("emi26/loader.fw");
  252. MODULE_FIRMWARE("emi26/bitstream.fw");
  253. MODULE_FIRMWARE("emi26/firmware.fw");
  254. /* vi:ai:syntax=c:sw=8:ts=8:tw=80
  255. */