ezusb.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * EZ-USB specific functions used by some of the USB to Serial drivers.
  3. *
  4. * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License version
  8. * 2 as published by the Free Software Foundation.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/slab.h>
  13. #include <linux/module.h>
  14. #include <linux/usb.h>
  15. #include <linux/firmware.h>
  16. #include <linux/ihex.h>
  17. #include <linux/usb/ezusb.h>
  18. struct ezusb_fx_type {
  19. /* EZ-USB Control and Status Register. Bit 0 controls 8051 reset */
  20. unsigned short cpucs_reg;
  21. unsigned short max_internal_adress;
  22. };
  23. static struct ezusb_fx_type ezusb_fx1 = {
  24. .cpucs_reg = 0x7F92,
  25. .max_internal_adress = 0x1B3F,
  26. };
  27. /* Commands for writing to memory */
  28. #define WRITE_INT_RAM 0xA0
  29. #define WRITE_EXT_RAM 0xA3
  30. static int ezusb_writememory(struct usb_device *dev, int address,
  31. unsigned char *data, int length, __u8 request)
  32. {
  33. int result;
  34. unsigned char *transfer_buffer;
  35. if (!dev)
  36. return -ENODEV;
  37. transfer_buffer = kmemdup(data, length, GFP_KERNEL);
  38. if (!transfer_buffer) {
  39. dev_err(&dev->dev, "%s - kmalloc(%d) failed.\n",
  40. __func__, length);
  41. return -ENOMEM;
  42. }
  43. result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
  44. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  45. address, 0, transfer_buffer, length, 3000);
  46. kfree(transfer_buffer);
  47. return result;
  48. }
  49. static int ezusb_set_reset(struct usb_device *dev, unsigned short cpucs_reg,
  50. unsigned char reset_bit)
  51. {
  52. int response = ezusb_writememory(dev, cpucs_reg, &reset_bit, 1, WRITE_INT_RAM);
  53. if (response < 0)
  54. dev_err(&dev->dev, "%s-%d failed: %d\n",
  55. __func__, reset_bit, response);
  56. return response;
  57. }
  58. int ezusb_fx1_set_reset(struct usb_device *dev, unsigned char reset_bit)
  59. {
  60. return ezusb_set_reset(dev, ezusb_fx1.cpucs_reg, reset_bit);
  61. }
  62. EXPORT_SYMBOL_GPL(ezusb_fx1_set_reset);
  63. static int ezusb_ihex_firmware_download(struct usb_device *dev,
  64. struct ezusb_fx_type fx,
  65. const char *firmware_path)
  66. {
  67. int ret = -ENOENT;
  68. const struct firmware *firmware = NULL;
  69. const struct ihex_binrec *record;
  70. if (request_ihex_firmware(&firmware, firmware_path,
  71. &dev->dev)) {
  72. dev_err(&dev->dev,
  73. "%s - request \"%s\" failed\n",
  74. __func__, firmware_path);
  75. goto out;
  76. }
  77. ret = ezusb_set_reset(dev, fx.cpucs_reg, 0);
  78. if (ret < 0)
  79. goto out;
  80. record = (const struct ihex_binrec *)firmware->data;
  81. for (; record; record = ihex_next_binrec(record)) {
  82. if (be32_to_cpu(record->addr) > fx.max_internal_adress) {
  83. ret = ezusb_writememory(dev, be32_to_cpu(record->addr),
  84. (unsigned char *)record->data,
  85. be16_to_cpu(record->len), WRITE_EXT_RAM);
  86. if (ret < 0) {
  87. dev_err(&dev->dev, "%s - ezusb_writememory "
  88. "failed writing internal memory "
  89. "(%d %04X %p %d)\n", __func__, ret,
  90. be32_to_cpu(record->addr), record->data,
  91. be16_to_cpu(record->len));
  92. goto out;
  93. }
  94. }
  95. }
  96. ret = ezusb_set_reset(dev, fx.cpucs_reg, 1);
  97. if (ret < 0)
  98. goto out;
  99. record = (const struct ihex_binrec *)firmware->data;
  100. for (; record; record = ihex_next_binrec(record)) {
  101. if (be32_to_cpu(record->addr) <= fx.max_internal_adress) {
  102. ret = ezusb_writememory(dev, be32_to_cpu(record->addr),
  103. (unsigned char *)record->data,
  104. be16_to_cpu(record->len), WRITE_INT_RAM);
  105. if (ret < 0) {
  106. dev_err(&dev->dev, "%s - ezusb_writememory "
  107. "failed writing external memory "
  108. "(%d %04X %p %d)\n", __func__, ret,
  109. be32_to_cpu(record->addr), record->data,
  110. be16_to_cpu(record->len));
  111. goto out;
  112. }
  113. }
  114. }
  115. ret = ezusb_set_reset(dev, fx.cpucs_reg, 0);
  116. out:
  117. release_firmware(firmware);
  118. return ret;
  119. }
  120. int ezusb_fx1_ihex_firmware_download(struct usb_device *dev,
  121. const char *firmware_path)
  122. {
  123. return ezusb_ihex_firmware_download(dev, ezusb_fx1, firmware_path);
  124. }
  125. EXPORT_SYMBOL_GPL(ezusb_fx1_ihex_firmware_download);
  126. #if 0
  127. /*
  128. * Once someone one needs these fx2 functions, uncomment them
  129. * and add them to ezusb.h and all should be good.
  130. */
  131. static struct ezusb_fx_type ezusb_fx2 = {
  132. .cpucs_reg = 0xE600,
  133. .max_internal_adress = 0x3FFF,
  134. };
  135. int ezusb_fx2_set_reset(struct usb_device *dev, unsigned char reset_bit)
  136. {
  137. return ezusb_set_reset(dev, ezusb_fx2.cpucs_reg, reset_bit);
  138. }
  139. EXPORT_SYMBOL_GPL(ezusb_fx2_set_reset);
  140. int ezusb_fx2_ihex_firmware_download(struct usb_device *dev,
  141. const char *firmware_path)
  142. {
  143. return ezusb_ihex_firmware_download(dev, ezusb_fx2, firmware_path);
  144. }
  145. EXPORT_SYMBOL_GPL(ezusb_fx2_ihex_firmware_download);
  146. #endif
  147. MODULE_LICENSE("GPL");