ezusb.c 4.5 KB

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