emi26.c 8.5 KB

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