emi26.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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", __func__, reset_bit);
  65. /* printk(KERN_DEBUG "%s - %d", __func__, 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", __func__, -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", __func__, 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", __func__, err);
  97. goto wraperr;
  98. }
  99. }
  100. /* De-assert reset (let the CPU run) */
  101. err = emi26_set_reset(dev,0);
  102. if (err < 0) {
  103. err("%s - error loading firmware: error = %d", __func__, err);
  104. goto wraperr;
  105. }
  106. msleep(250); /* let device settle */
  107. /* 2. We upload the FPGA firmware into the EMI
  108. * Note: collect up to 1023 (yes!) bytes and send them with
  109. * a single request. This is _much_ faster! */
  110. do {
  111. i = 0;
  112. addr = g_bitstream[pos].address;
  113. /* intel hex records are terminated with type 0 element */
  114. while ((g_bitstream[pos].type == 0) && (i + g_bitstream[pos].length < FW_LOAD_SIZE)) {
  115. memcpy(buf + i, g_bitstream[pos].data, g_bitstream[pos].length);
  116. i += g_bitstream[pos].length;
  117. pos++;
  118. }
  119. err = emi26_writememory(dev, addr, buf, i, ANCHOR_LOAD_FPGA);
  120. if (err < 0) {
  121. err("%s - error loading firmware: error = %d", __func__, err);
  122. goto wraperr;
  123. }
  124. } while (i > 0);
  125. /* Assert reset (stop the CPU in the EMI) */
  126. err = emi26_set_reset(dev,1);
  127. if (err < 0) {
  128. err("%s - error loading firmware: error = %d", __func__, err);
  129. goto wraperr;
  130. }
  131. /* 3. We need to put the loader for the firmware into the EZ-USB (again...) */
  132. for (i=0; g_Loader[i].type == 0; i++) {
  133. err = emi26_writememory(dev, g_Loader[i].address, g_Loader[i].data, g_Loader[i].length, ANCHOR_LOAD_INTERNAL);
  134. if (err < 0) {
  135. err("%s - error loading firmware: error = %d", __func__, err);
  136. goto wraperr;
  137. }
  138. }
  139. msleep(250); /* let device settle */
  140. /* De-assert reset (let the CPU run) */
  141. err = emi26_set_reset(dev,0);
  142. if (err < 0) {
  143. err("%s - error loading firmware: error = %d", __func__, err);
  144. goto wraperr;
  145. }
  146. /* 4. We put the part of the firmware that lies in the external RAM into the EZ-USB */
  147. for (i=0; g_Firmware[i].type == 0; i++) {
  148. if (!INTERNAL_RAM(g_Firmware[i].address)) {
  149. err = emi26_writememory(dev, g_Firmware[i].address, g_Firmware[i].data, g_Firmware[i].length, ANCHOR_LOAD_EXTERNAL);
  150. if (err < 0) {
  151. err("%s - error loading firmware: error = %d", __func__, err);
  152. goto wraperr;
  153. }
  154. }
  155. }
  156. /* Assert reset (stop the CPU in the EMI) */
  157. err = emi26_set_reset(dev,1);
  158. if (err < 0) {
  159. err("%s - error loading firmware: error = %d", __func__, err);
  160. goto wraperr;
  161. }
  162. for (i=0; g_Firmware[i].type == 0; i++) {
  163. if (INTERNAL_RAM(g_Firmware[i].address)) {
  164. err = emi26_writememory(dev, g_Firmware[i].address, g_Firmware[i].data, g_Firmware[i].length, ANCHOR_LOAD_INTERNAL);
  165. if (err < 0) {
  166. err("%s - error loading firmware: error = %d", __func__, err);
  167. goto wraperr;
  168. }
  169. }
  170. }
  171. /* De-assert reset (let the CPU run) */
  172. err = emi26_set_reset(dev,0);
  173. if (err < 0) {
  174. err("%s - error loading firmware: error = %d", __func__, err);
  175. goto wraperr;
  176. }
  177. msleep(250); /* let device settle */
  178. /* return 1 to fail the driver inialization
  179. * and give real driver change to load */
  180. err = 1;
  181. wraperr:
  182. kfree(buf);
  183. return err;
  184. }
  185. static struct usb_device_id id_table [] = {
  186. { USB_DEVICE(EMI26_VENDOR_ID, EMI26_PRODUCT_ID) },
  187. { USB_DEVICE(EMI26_VENDOR_ID, EMI26B_PRODUCT_ID) },
  188. { } /* Terminating entry */
  189. };
  190. MODULE_DEVICE_TABLE (usb, id_table);
  191. static int emi26_probe(struct usb_interface *intf, const struct usb_device_id *id)
  192. {
  193. struct usb_device *dev = interface_to_usbdev(intf);
  194. info("%s start", __func__);
  195. emi26_load_firmware(dev);
  196. /* do not return the driver context, let real audio driver do that */
  197. return -EIO;
  198. }
  199. static void emi26_disconnect(struct usb_interface *intf)
  200. {
  201. }
  202. static struct usb_driver emi26_driver = {
  203. .name = "emi26 - firmware loader",
  204. .probe = emi26_probe,
  205. .disconnect = emi26_disconnect,
  206. .id_table = id_table,
  207. };
  208. static int __init emi26_init (void)
  209. {
  210. return usb_register(&emi26_driver);
  211. }
  212. static void __exit emi26_exit (void)
  213. {
  214. usb_deregister (&emi26_driver);
  215. }
  216. module_init(emi26_init);
  217. module_exit(emi26_exit);
  218. MODULE_AUTHOR("Tapio Laxström");
  219. MODULE_DESCRIPTION("Emagic EMI 2|6 firmware loader.");
  220. MODULE_LICENSE("GPL");
  221. /* vi:ai:syntax=c:sw=8:ts=8:tw=80
  222. */