emi62.c 8.6 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. #include <linux/kernel.h>
  11. #include <linux/errno.h>
  12. #include <linux/slab.h>
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/usb.h>
  16. #include <linux/delay.h>
  17. #include <linux/firmware.h>
  18. #include <linux/ihex.h>
  19. /* include firmware (variables)*/
  20. /* FIXME: This is quick and dirty solution! */
  21. #define SPDIF /* if you want SPDIF comment next line */
  22. //#undef SPDIF /* if you want MIDI uncomment this line */
  23. #ifdef SPDIF
  24. #define FIRMWARE_FW "emi62/spdif.fw"
  25. #else
  26. #define FIRMWARE_FW "emi62/midi.fw"
  27. #endif
  28. #define EMI62_VENDOR_ID 0x086a /* Emagic Soft-und Hardware GmBH */
  29. #define EMI62_PRODUCT_ID 0x0110 /* EMI 6|2m without firmware */
  30. #define ANCHOR_LOAD_INTERNAL 0xA0 /* Vendor specific request code for Anchor Upload/Download (This one is implemented in the core) */
  31. #define ANCHOR_LOAD_EXTERNAL 0xA3 /* This command is not implemented in the core. Requires firmware */
  32. #define ANCHOR_LOAD_FPGA 0xA5 /* This command is not implemented in the core. Requires firmware. Emagic extension */
  33. #define MAX_INTERNAL_ADDRESS 0x1B3F /* This is the highest internal RAM address for the AN2131Q */
  34. #define CPUCS_REG 0x7F92 /* EZ-USB Control and Status Register. Bit 0 controls 8051 reset */
  35. #define INTERNAL_RAM(address) (address <= MAX_INTERNAL_ADDRESS)
  36. static int emi62_writememory(struct usb_device *dev, int address,
  37. const unsigned char *data, int length,
  38. __u8 bRequest);
  39. static int emi62_set_reset(struct usb_device *dev, unsigned char reset_bit);
  40. static int emi62_load_firmware (struct usb_device *dev);
  41. static int emi62_probe(struct usb_interface *intf, const struct usb_device_id *id);
  42. static void emi62_disconnect(struct usb_interface *intf);
  43. /* thanks to drivers/usb/serial/keyspan_pda.c code */
  44. static int emi62_writememory(struct usb_device *dev, int address,
  45. const unsigned char *data, int length,
  46. __u8 request)
  47. {
  48. int result;
  49. unsigned char *buffer = kmemdup(data, length, GFP_KERNEL);
  50. if (!buffer) {
  51. err("emi62: kmalloc(%d) failed.", length);
  52. return -ENOMEM;
  53. }
  54. /* Note: usb_control_msg returns negative value on error or length of the
  55. * data that was written! */
  56. result = usb_control_msg (dev, usb_sndctrlpipe(dev, 0), request, 0x40, address, 0, buffer, length, 300);
  57. kfree (buffer);
  58. return result;
  59. }
  60. /* thanks to drivers/usb/serial/keyspan_pda.c code */
  61. static int emi62_set_reset (struct usb_device *dev, unsigned char reset_bit)
  62. {
  63. int response;
  64. dev_info(&dev->dev, "%s - %d\n", __func__, reset_bit);
  65. response = emi62_writememory (dev, CPUCS_REG, &reset_bit, 1, 0xa0);
  66. if (response < 0) {
  67. err("emi62: set_reset (%d) failed", reset_bit);
  68. }
  69. return response;
  70. }
  71. #define FW_LOAD_SIZE 1023
  72. static int emi62_load_firmware (struct usb_device *dev)
  73. {
  74. const struct firmware *loader_fw = NULL;
  75. const struct firmware *bitstream_fw = NULL;
  76. const struct firmware *firmware_fw = NULL;
  77. const struct ihex_binrec *rec;
  78. int err;
  79. int i;
  80. __u32 addr; /* Address to write */
  81. __u8 *buf;
  82. dev_dbg(&dev->dev, "load_firmware\n");
  83. buf = kmalloc(FW_LOAD_SIZE, GFP_KERNEL);
  84. if (!buf) {
  85. err( "%s - error loading firmware: error = %d", __func__, -ENOMEM);
  86. err = -ENOMEM;
  87. goto wraperr;
  88. }
  89. err = request_ihex_firmware(&loader_fw, "emi62/loader.fw", &dev->dev);
  90. if (err)
  91. goto nofw;
  92. err = request_ihex_firmware(&bitstream_fw, "emi62/bitstream.fw",
  93. &dev->dev);
  94. if (err)
  95. goto nofw;
  96. err = request_ihex_firmware(&firmware_fw, FIRMWARE_FW, &dev->dev);
  97. if (err) {
  98. nofw:
  99. err( "%s - request_firmware() failed", __func__);
  100. goto wraperr;
  101. }
  102. /* Assert reset (stop the CPU in the EMI) */
  103. err = emi62_set_reset(dev,1);
  104. if (err < 0) {
  105. err("%s - error loading firmware: error = %d", __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 = emi62_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 = emi62_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 = emi62_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 = emi62_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 = emi62_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. /* De-assert reset (let the CPU run) */
  164. err = emi62_set_reset(dev,0);
  165. if (err < 0) {
  166. err("%s - error loading firmware: error = %d", __func__, err);
  167. goto wraperr;
  168. }
  169. msleep(250); /* let device settle */
  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 = emi62_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 = emi62_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 = emi62_writememory(dev, be32_to_cpu(rec->addr),
  193. rec->data, be16_to_cpu(rec->len),
  194. ANCHOR_LOAD_EXTERNAL);
  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 = emi62_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. release_firmware(loader_fw);
  209. release_firmware(bitstream_fw);
  210. release_firmware(firmware_fw);
  211. kfree(buf);
  212. /* return 1 to fail the driver inialization
  213. * and give real driver change to load */
  214. return 1;
  215. wraperr:
  216. release_firmware(loader_fw);
  217. release_firmware(bitstream_fw);
  218. release_firmware(firmware_fw);
  219. kfree(buf);
  220. dev_err(&dev->dev, "Error\n");
  221. return err;
  222. }
  223. static const struct usb_device_id id_table[] = {
  224. { USB_DEVICE(EMI62_VENDOR_ID, EMI62_PRODUCT_ID) },
  225. { } /* Terminating entry */
  226. };
  227. MODULE_DEVICE_TABLE (usb, id_table);
  228. static int emi62_probe(struct usb_interface *intf, const struct usb_device_id *id)
  229. {
  230. struct usb_device *dev = interface_to_usbdev(intf);
  231. dev_dbg(&intf->dev, "emi62_probe\n");
  232. dev_info(&intf->dev, "%s start\n", __func__);
  233. emi62_load_firmware(dev);
  234. /* do not return the driver context, let real audio driver do that */
  235. return -EIO;
  236. }
  237. static void emi62_disconnect(struct usb_interface *intf)
  238. {
  239. }
  240. static struct usb_driver emi62_driver = {
  241. .name = "emi62 - firmware loader",
  242. .probe = emi62_probe,
  243. .disconnect = emi62_disconnect,
  244. .id_table = id_table,
  245. };
  246. module_usb_driver(emi62_driver);
  247. MODULE_AUTHOR("Tapio Laxström");
  248. MODULE_DESCRIPTION("Emagic EMI 6|2m firmware loader.");
  249. MODULE_LICENSE("GPL");
  250. MODULE_FIRMWARE("emi62/loader.fw");
  251. MODULE_FIRMWARE("emi62/bitstream.fw");
  252. MODULE_FIRMWARE(FIRMWARE_FW);
  253. /* vi:ai:syntax=c:sw=8:ts=8:tw=80
  254. */