You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

189 lines
5.7 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2009 by Zachary T Welch <zw@superlucidity.net> *
  3. * *
  4. * Copyright (C) 2011 by Mauro Gamba <maurillo71@gmail.com> *
  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; either version 2 of the License, or *
  9. * (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU General Public License *
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  18. ***************************************************************************/
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include "log.h"
  23. #include "libusb0_common.h"
  24. static bool jtag_libusb_match(struct jtag_libusb_device *dev,
  25. const uint16_t vids[], const uint16_t pids[])
  26. {
  27. for (unsigned i = 0; vids[i]; i++) {
  28. if (dev->descriptor.idVendor == vids[i] &&
  29. dev->descriptor.idProduct == pids[i]) {
  30. return true;
  31. }
  32. }
  33. return false;
  34. }
  35. /* Returns true if the string descriptor indexed by str_index in device matches string */
  36. static bool string_descriptor_equal(usb_dev_handle *device, uint8_t str_index,
  37. const char *string)
  38. {
  39. int retval;
  40. bool matched;
  41. char desc_string[256+1]; /* Max size of string descriptor */
  42. if (str_index == 0)
  43. return false;
  44. retval = usb_get_string_simple(device, str_index,
  45. desc_string, sizeof(desc_string)-1);
  46. if (retval < 0) {
  47. LOG_ERROR("usb_get_string_simple() failed with %d", retval);
  48. return false;
  49. }
  50. /* Null terminate descriptor string in case it needs to be logged. */
  51. desc_string[sizeof(desc_string)-1] = '\0';
  52. matched = strncmp(string, desc_string, sizeof(desc_string)) == 0;
  53. if (!matched)
  54. LOG_DEBUG("Device serial number '%s' doesn't match requested serial '%s'",
  55. desc_string, string);
  56. return matched;
  57. }
  58. int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[],
  59. const char *serial,
  60. struct jtag_libusb_device_handle **out)
  61. {
  62. int retval = -ENODEV;
  63. struct jtag_libusb_device_handle *libusb_handle;
  64. usb_init();
  65. usb_find_busses();
  66. usb_find_devices();
  67. struct usb_bus *busses = usb_get_busses();
  68. for (struct usb_bus *bus = busses; bus; bus = bus->next) {
  69. for (struct usb_device *dev = bus->devices;
  70. dev; dev = dev->next) {
  71. if (!jtag_libusb_match(dev, vids, pids))
  72. continue;
  73. libusb_handle = usb_open(dev);
  74. if (NULL == libusb_handle) {
  75. retval = -errno;
  76. continue;
  77. }
  78. /* Device must be open to use libusb_get_string_descriptor_ascii. */
  79. if (serial != NULL &&
  80. !string_descriptor_equal(libusb_handle, dev->descriptor.iSerialNumber, serial)) {
  81. usb_close(libusb_handle);
  82. continue;
  83. }
  84. *out = libusb_handle;
  85. retval = 0;
  86. break;
  87. }
  88. }
  89. return retval;
  90. }
  91. void jtag_libusb_close(jtag_libusb_device_handle *dev)
  92. {
  93. /* Close device */
  94. usb_close(dev);
  95. }
  96. int jtag_libusb_control_transfer(jtag_libusb_device_handle *dev, uint8_t requestType,
  97. uint8_t request, uint16_t wValue, uint16_t wIndex, char *bytes,
  98. uint16_t size, unsigned int timeout)
  99. {
  100. int transferred = 0;
  101. transferred = usb_control_msg(dev, requestType, request, wValue, wIndex,
  102. bytes, size, timeout);
  103. if (transferred < 0)
  104. transferred = 0;
  105. return transferred;
  106. }
  107. int jtag_libusb_bulk_write(jtag_libusb_device_handle *dev, int ep, char *bytes,
  108. int size, int timeout)
  109. {
  110. return usb_bulk_write(dev, ep, bytes, size, timeout);
  111. }
  112. int jtag_libusb_bulk_read(jtag_libusb_device_handle *dev, int ep, char *bytes,
  113. int size, int timeout)
  114. {
  115. return usb_bulk_read(dev, ep, bytes, size, timeout);
  116. }
  117. int jtag_libusb_set_configuration(jtag_libusb_device_handle *devh,
  118. int configuration)
  119. {
  120. struct jtag_libusb_device *udev = jtag_libusb_get_device(devh);
  121. return usb_set_configuration(devh,
  122. udev->config[configuration].bConfigurationValue);
  123. }
  124. int jtag_libusb_choose_interface(struct jtag_libusb_device_handle *devh,
  125. unsigned int *usb_read_ep,
  126. unsigned int *usb_write_ep,
  127. int bclass, int subclass, int protocol)
  128. {
  129. struct jtag_libusb_device *udev = jtag_libusb_get_device(devh);
  130. struct usb_interface *iface = udev->config->interface;
  131. struct usb_interface_descriptor *desc = iface->altsetting;
  132. *usb_read_ep = *usb_write_ep = 0;
  133. for (int i = 0; i < desc->bNumEndpoints; i++) {
  134. if ((bclass > 0 && desc->bInterfaceClass != bclass) ||
  135. (subclass > 0 && desc->bInterfaceSubClass != subclass) ||
  136. (protocol > 0 && desc->bInterfaceProtocol != protocol))
  137. continue;
  138. uint8_t epnum = desc->endpoint[i].bEndpointAddress;
  139. bool is_input = epnum & 0x80;
  140. LOG_DEBUG("usb ep %s %02x", is_input ? "in" : "out", epnum);
  141. if (is_input)
  142. *usb_read_ep = epnum;
  143. else
  144. *usb_write_ep = epnum;
  145. if (*usb_read_ep && *usb_write_ep) {
  146. LOG_DEBUG("Claiming interface %d", (int)desc->bInterfaceNumber);
  147. usb_claim_interface(devh, (int)desc->bInterfaceNumber);
  148. return ERROR_OK;
  149. }
  150. }
  151. return ERROR_FAIL;
  152. }
  153. int jtag_libusb_get_pid(struct jtag_libusb_device *dev, uint16_t *pid)
  154. {
  155. if (!dev)
  156. return ERROR_FAIL;
  157. *pid = dev->descriptor.idProduct;
  158. return ERROR_OK;
  159. }