emi26.c 7.6 KB

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