emi26.c 7.7 KB

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