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.
 
 
 
 
 
 

249 lines
7.4 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 "libusb1_common.h"
  24. static struct libusb_context *jtag_libusb_context; /**< Libusb context **/
  25. static libusb_device **devs; /**< The usb device list **/
  26. static bool jtag_libusb_match(struct libusb_device_descriptor *dev_desc,
  27. const uint16_t vids[], const uint16_t pids[])
  28. {
  29. for (unsigned i = 0; vids[i]; i++) {
  30. if (dev_desc->idVendor == vids[i] &&
  31. dev_desc->idProduct == pids[i]) {
  32. return true;
  33. }
  34. }
  35. return false;
  36. }
  37. /* Returns true if the string descriptor indexed by str_index in device matches string */
  38. static bool string_descriptor_equal(libusb_device_handle *device, uint8_t str_index,
  39. const char *string)
  40. {
  41. int retval;
  42. bool matched;
  43. char desc_string[256+1]; /* Max size of string descriptor */
  44. if (str_index == 0)
  45. return false;
  46. retval = libusb_get_string_descriptor_ascii(device, str_index,
  47. (unsigned char *)desc_string, sizeof(desc_string)-1);
  48. if (retval < 0) {
  49. LOG_ERROR("libusb_get_string_descriptor_ascii() failed with %d", retval);
  50. return false;
  51. }
  52. /* Null terminate descriptor string in case it needs to be logged. */
  53. desc_string[sizeof(desc_string)-1] = '\0';
  54. matched = strncmp(string, desc_string, sizeof(desc_string)) == 0;
  55. if (!matched)
  56. LOG_DEBUG("Device serial number '%s' doesn't match requested serial '%s'",
  57. desc_string, string);
  58. return matched;
  59. }
  60. int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[],
  61. const char *serial,
  62. struct jtag_libusb_device_handle **out)
  63. {
  64. int cnt, idx, errCode;
  65. int retval = -ENODEV;
  66. struct jtag_libusb_device_handle *libusb_handle = NULL;
  67. if (libusb_init(&jtag_libusb_context) < 0)
  68. return -ENODEV;
  69. cnt = libusb_get_device_list(jtag_libusb_context, &devs);
  70. for (idx = 0; idx < cnt; idx++) {
  71. struct libusb_device_descriptor dev_desc;
  72. if (libusb_get_device_descriptor(devs[idx], &dev_desc) != 0)
  73. continue;
  74. if (!jtag_libusb_match(&dev_desc, vids, pids))
  75. continue;
  76. errCode = libusb_open(devs[idx], &libusb_handle);
  77. if (errCode) {
  78. LOG_ERROR("libusb_open() failed with %s",
  79. libusb_error_name(errCode));
  80. continue;
  81. }
  82. /* Device must be open to use libusb_get_string_descriptor_ascii. */
  83. if (serial != NULL &&
  84. !string_descriptor_equal(libusb_handle, dev_desc.iSerialNumber, serial)) {
  85. libusb_close(libusb_handle);
  86. continue;
  87. }
  88. /* Success. */
  89. *out = libusb_handle;
  90. retval = 0;
  91. break;
  92. }
  93. if (cnt >= 0)
  94. libusb_free_device_list(devs, 1);
  95. return retval;
  96. }
  97. void jtag_libusb_close(jtag_libusb_device_handle *dev)
  98. {
  99. /* Close device */
  100. libusb_close(dev);
  101. libusb_exit(jtag_libusb_context);
  102. }
  103. int jtag_libusb_control_transfer(jtag_libusb_device_handle *dev, uint8_t requestType,
  104. uint8_t request, uint16_t wValue, uint16_t wIndex, char *bytes,
  105. uint16_t size, unsigned int timeout)
  106. {
  107. int transferred = 0;
  108. transferred = libusb_control_transfer(dev, requestType, request, wValue, wIndex,
  109. (unsigned char *)bytes, size, timeout);
  110. if (transferred < 0)
  111. transferred = 0;
  112. return transferred;
  113. }
  114. int jtag_libusb_bulk_write(jtag_libusb_device_handle *dev, int ep, char *bytes,
  115. int size, int timeout)
  116. {
  117. int transferred = 0;
  118. libusb_bulk_transfer(dev, ep, (unsigned char *)bytes, size,
  119. &transferred, timeout);
  120. return transferred;
  121. }
  122. int jtag_libusb_bulk_read(jtag_libusb_device_handle *dev, int ep, char *bytes,
  123. int size, int timeout)
  124. {
  125. int transferred = 0;
  126. libusb_bulk_transfer(dev, ep, (unsigned char *)bytes, size,
  127. &transferred, timeout);
  128. return transferred;
  129. }
  130. int jtag_libusb_set_configuration(jtag_libusb_device_handle *devh,
  131. int configuration)
  132. {
  133. struct jtag_libusb_device *udev = jtag_libusb_get_device(devh);
  134. int retCode = -99;
  135. struct libusb_config_descriptor *config = NULL;
  136. int current_config = -1;
  137. retCode = libusb_get_configuration(devh, &current_config);
  138. if (retCode != 0)
  139. return retCode;
  140. retCode = libusb_get_config_descriptor(udev, configuration, &config);
  141. if (retCode != 0 || config == NULL)
  142. return retCode;
  143. /* Only change the configuration if it is not already set to the
  144. same one. Otherwise this issues a lightweight reset and hangs
  145. LPC-Link2 with JLink firmware. */
  146. if (current_config != config->bConfigurationValue)
  147. retCode = libusb_set_configuration(devh, config->bConfigurationValue);
  148. libusb_free_config_descriptor(config);
  149. return retCode;
  150. }
  151. int jtag_libusb_choose_interface(struct jtag_libusb_device_handle *devh,
  152. unsigned int *usb_read_ep,
  153. unsigned int *usb_write_ep,
  154. int bclass, int subclass, int protocol)
  155. {
  156. struct jtag_libusb_device *udev = jtag_libusb_get_device(devh);
  157. const struct libusb_interface *inter;
  158. const struct libusb_interface_descriptor *interdesc;
  159. const struct libusb_endpoint_descriptor *epdesc;
  160. struct libusb_config_descriptor *config;
  161. *usb_read_ep = *usb_write_ep = 0;
  162. libusb_get_config_descriptor(udev, 0, &config);
  163. for (int i = 0; i < (int)config->bNumInterfaces; i++) {
  164. inter = &config->interface[i];
  165. interdesc = &inter->altsetting[0];
  166. for (int k = 0;
  167. k < (int)interdesc->bNumEndpoints; k++) {
  168. if ((bclass > 0 && interdesc->bInterfaceClass != bclass) ||
  169. (subclass > 0 && interdesc->bInterfaceSubClass != subclass) ||
  170. (protocol > 0 && interdesc->bInterfaceProtocol != protocol))
  171. continue;
  172. epdesc = &interdesc->endpoint[k];
  173. uint8_t epnum = epdesc->bEndpointAddress;
  174. bool is_input = epnum & 0x80;
  175. LOG_DEBUG("usb ep %s %02x",
  176. is_input ? "in" : "out", epnum);
  177. if (is_input)
  178. *usb_read_ep = epnum;
  179. else
  180. *usb_write_ep = epnum;
  181. if (*usb_read_ep && *usb_write_ep) {
  182. LOG_DEBUG("Claiming interface %d", (int)interdesc->bInterfaceNumber);
  183. libusb_claim_interface(devh, (int)interdesc->bInterfaceNumber);
  184. libusb_free_config_descriptor(config);
  185. return ERROR_OK;
  186. }
  187. }
  188. }
  189. libusb_free_config_descriptor(config);
  190. return ERROR_FAIL;
  191. }
  192. int jtag_libusb_get_pid(struct jtag_libusb_device *dev, uint16_t *pid)
  193. {
  194. struct libusb_device_descriptor dev_desc;
  195. if (libusb_get_device_descriptor(dev, &dev_desc) == 0) {
  196. *pid = dev_desc.idProduct;
  197. return ERROR_OK;
  198. }
  199. return ERROR_FAIL;
  200. }