emi26.c 8.7 KB

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