ezusb.c 4.3 KB

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