emi26.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 = kmalloc (length, GFP_KERNEL);
  50. if (!buffer) {
  51. err("emi26: kmalloc(%d) failed.", length);
  52. return -ENOMEM;
  53. }
  54. memcpy (buffer, data, length);
  55. /* Note: usb_control_msg returns negative value on error or length of the
  56. * data that was written! */
  57. result = usb_control_msg (dev, usb_sndctrlpipe(dev, 0), request, 0x40, address, 0, buffer, length, 300);
  58. kfree (buffer);
  59. return result;
  60. }
  61. /* thanks to drivers/usb/serial/keyspan_pda.c code */
  62. static int emi26_set_reset (struct usb_device *dev, unsigned char reset_bit)
  63. {
  64. int response;
  65. info("%s - %d", __FUNCTION__, reset_bit);
  66. /* printk(KERN_DEBUG "%s - %d", __FUNCTION__, reset_bit); */
  67. response = emi26_writememory (dev, CPUCS_REG, &reset_bit, 1, 0xa0);
  68. if (response < 0) {
  69. err("emi26: set_reset (%d) failed", reset_bit);
  70. }
  71. return response;
  72. }
  73. #define FW_LOAD_SIZE 1023
  74. static int emi26_load_firmware (struct usb_device *dev)
  75. {
  76. int err;
  77. int i;
  78. int pos = 0; /* Position in hex record */
  79. __u32 addr; /* Address to write */
  80. __u8 *buf;
  81. buf = kmalloc(FW_LOAD_SIZE, GFP_KERNEL);
  82. if (!buf) {
  83. err( "%s - error loading firmware: error = %d", __FUNCTION__, -ENOMEM);
  84. err = -ENOMEM;
  85. goto wraperr;
  86. }
  87. /* Assert reset (stop the CPU in the EMI) */
  88. err = emi26_set_reset(dev,1);
  89. if (err < 0) {
  90. err( "%s - error loading firmware: error = %d", __FUNCTION__, err);
  91. goto wraperr;
  92. }
  93. /* 1. We need to put the loader for the FPGA into the EZ-USB */
  94. for (i=0; g_Loader[i].type == 0; i++) {
  95. err = emi26_writememory(dev, g_Loader[i].address, g_Loader[i].data, g_Loader[i].length, ANCHOR_LOAD_INTERNAL);
  96. if (err < 0) {
  97. err("%s - error loading firmware: error = %d", __FUNCTION__, err);
  98. goto wraperr;
  99. }
  100. }
  101. /* De-assert reset (let the CPU run) */
  102. err = emi26_set_reset(dev,0);
  103. msleep(250); /* let device settle */
  104. /* 2. We upload the FPGA firmware into the EMI
  105. * Note: collect up to 1023 (yes!) bytes and send them with
  106. * a single request. This is _much_ faster! */
  107. do {
  108. i = 0;
  109. addr = g_bitstream[pos].address;
  110. /* intel hex records are terminated with type 0 element */
  111. while ((g_bitstream[pos].type == 0) && (i + g_bitstream[pos].length < FW_LOAD_SIZE)) {
  112. memcpy(buf + i, g_bitstream[pos].data, g_bitstream[pos].length);
  113. i += g_bitstream[pos].length;
  114. pos++;
  115. }
  116. err = emi26_writememory(dev, addr, buf, i, ANCHOR_LOAD_FPGA);
  117. if (err < 0) {
  118. err("%s - error loading firmware: error = %d", __FUNCTION__, err);
  119. goto wraperr;
  120. }
  121. } while (i > 0);
  122. /* Assert reset (stop the CPU in the EMI) */
  123. err = emi26_set_reset(dev,1);
  124. if (err < 0) {
  125. err("%s - error loading firmware: error = %d", __FUNCTION__, err);
  126. goto wraperr;
  127. }
  128. /* 3. We need to put the loader for the firmware into the EZ-USB (again...) */
  129. for (i=0; g_Loader[i].type == 0; i++) {
  130. err = emi26_writememory(dev, g_Loader[i].address, g_Loader[i].data, g_Loader[i].length, ANCHOR_LOAD_INTERNAL);
  131. if (err < 0) {
  132. err("%s - error loading firmware: error = %d", __FUNCTION__, err);
  133. goto wraperr;
  134. }
  135. }
  136. msleep(250); /* let device settle */
  137. /* De-assert reset (let the CPU run) */
  138. err = emi26_set_reset(dev,0);
  139. if (err < 0) {
  140. err("%s - error loading firmware: error = %d", __FUNCTION__, err);
  141. goto wraperr;
  142. }
  143. /* 4. We put the part of the firmware that lies in the external RAM into the EZ-USB */
  144. for (i=0; g_Firmware[i].type == 0; i++) {
  145. if (!INTERNAL_RAM(g_Firmware[i].address)) {
  146. err = emi26_writememory(dev, g_Firmware[i].address, g_Firmware[i].data, g_Firmware[i].length, ANCHOR_LOAD_EXTERNAL);
  147. if (err < 0) {
  148. err("%s - error loading firmware: error = %d", __FUNCTION__, err);
  149. goto wraperr;
  150. }
  151. }
  152. }
  153. /* Assert reset (stop the CPU in the EMI) */
  154. err = emi26_set_reset(dev,1);
  155. if (err < 0) {
  156. err("%s - error loading firmware: error = %d", __FUNCTION__, err);
  157. goto wraperr;
  158. }
  159. for (i=0; g_Firmware[i].type == 0; i++) {
  160. if (INTERNAL_RAM(g_Firmware[i].address)) {
  161. err = emi26_writememory(dev, g_Firmware[i].address, g_Firmware[i].data, g_Firmware[i].length, ANCHOR_LOAD_INTERNAL);
  162. if (err < 0) {
  163. err("%s - error loading firmware: error = %d", __FUNCTION__, err);
  164. goto wraperr;
  165. }
  166. }
  167. }
  168. /* De-assert reset (let the CPU run) */
  169. err = emi26_set_reset(dev,0);
  170. if (err < 0) {
  171. err("%s - error loading firmware: error = %d", __FUNCTION__, err);
  172. goto wraperr;
  173. }
  174. msleep(250); /* let device settle */
  175. /* return 1 to fail the driver inialization
  176. * and give real driver change to load */
  177. err = 1;
  178. wraperr:
  179. kfree(buf);
  180. return err;
  181. }
  182. static struct usb_device_id id_table [] = {
  183. { USB_DEVICE(EMI26_VENDOR_ID, EMI26_PRODUCT_ID) },
  184. { USB_DEVICE(EMI26_VENDOR_ID, EMI26B_PRODUCT_ID) },
  185. { } /* Terminating entry */
  186. };
  187. MODULE_DEVICE_TABLE (usb, id_table);
  188. static int emi26_probe(struct usb_interface *intf, const struct usb_device_id *id)
  189. {
  190. struct usb_device *dev = interface_to_usbdev(intf);
  191. info("%s start", __FUNCTION__);
  192. emi26_load_firmware(dev);
  193. /* do not return the driver context, let real audio driver do that */
  194. return -EIO;
  195. }
  196. static void emi26_disconnect(struct usb_interface *intf)
  197. {
  198. }
  199. static struct usb_driver emi26_driver = {
  200. .name = "emi26 - firmware loader",
  201. .probe = emi26_probe,
  202. .disconnect = emi26_disconnect,
  203. .id_table = id_table,
  204. };
  205. static int __init emi26_init (void)
  206. {
  207. return usb_register(&emi26_driver);
  208. }
  209. static void __exit emi26_exit (void)
  210. {
  211. usb_deregister (&emi26_driver);
  212. }
  213. module_init(emi26_init);
  214. module_exit(emi26_exit);
  215. MODULE_AUTHOR("tapio laxström");
  216. MODULE_DESCRIPTION("Emagic EMI 2|6 firmware loader.");
  217. MODULE_LICENSE("GPL");
  218. /* vi:ai:syntax=c:sw=8:ts=8:tw=80
  219. */