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.
 
 
 
 
 
 

371 lines
10 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 <jtag/drivers/jtag_usb_common.h>
  23. #include "libusb_helper.h"
  24. #include "log.h"
  25. /*
  26. * comment from libusb:
  27. * As per the USB 3.0 specs, the current maximum limit for the depth is 7.
  28. */
  29. #define MAX_USB_PORTS 7
  30. static struct libusb_context *jtag_libusb_context; /**< Libusb context **/
  31. static struct libusb_device **devs; /**< The usb device list **/
  32. static int jtag_libusb_error(int err)
  33. {
  34. switch (err) {
  35. case LIBUSB_SUCCESS:
  36. return ERROR_OK;
  37. case LIBUSB_ERROR_TIMEOUT:
  38. return ERROR_TIMEOUT_REACHED;
  39. case LIBUSB_ERROR_IO:
  40. case LIBUSB_ERROR_INVALID_PARAM:
  41. case LIBUSB_ERROR_ACCESS:
  42. case LIBUSB_ERROR_NO_DEVICE:
  43. case LIBUSB_ERROR_NOT_FOUND:
  44. case LIBUSB_ERROR_BUSY:
  45. case LIBUSB_ERROR_OVERFLOW:
  46. case LIBUSB_ERROR_PIPE:
  47. case LIBUSB_ERROR_INTERRUPTED:
  48. case LIBUSB_ERROR_NO_MEM:
  49. case LIBUSB_ERROR_NOT_SUPPORTED:
  50. case LIBUSB_ERROR_OTHER:
  51. return ERROR_FAIL;
  52. default:
  53. return ERROR_FAIL;
  54. }
  55. }
  56. static bool jtag_libusb_match_ids(struct libusb_device_descriptor *dev_desc,
  57. const uint16_t vids[], const uint16_t pids[])
  58. {
  59. for (unsigned i = 0; vids[i]; i++) {
  60. if (dev_desc->idVendor == vids[i] &&
  61. dev_desc->idProduct == pids[i]) {
  62. return true;
  63. }
  64. }
  65. return false;
  66. }
  67. #ifdef HAVE_LIBUSB_GET_PORT_NUMBERS
  68. static bool jtag_libusb_location_equal(struct libusb_device *device)
  69. {
  70. uint8_t port_path[MAX_USB_PORTS];
  71. uint8_t dev_bus;
  72. int path_len;
  73. path_len = libusb_get_port_numbers(device, port_path, MAX_USB_PORTS);
  74. if (path_len == LIBUSB_ERROR_OVERFLOW) {
  75. LOG_WARNING("cannot determine path to usb device! (more than %i ports in path)\n",
  76. MAX_USB_PORTS);
  77. return false;
  78. }
  79. dev_bus = libusb_get_bus_number(device);
  80. return jtag_usb_location_equal(dev_bus, port_path, path_len);
  81. }
  82. #else /* HAVE_LIBUSB_GET_PORT_NUMBERS */
  83. static bool jtag_libusb_location_equal(struct libusb_device *device)
  84. {
  85. return true;
  86. }
  87. #endif /* HAVE_LIBUSB_GET_PORT_NUMBERS */
  88. /* Returns true if the string descriptor indexed by str_index in device matches string */
  89. static bool string_descriptor_equal(struct libusb_device_handle *device, uint8_t str_index,
  90. const char *string)
  91. {
  92. int retval;
  93. bool matched;
  94. char desc_string[256+1]; /* Max size of string descriptor */
  95. if (str_index == 0)
  96. return false;
  97. retval = libusb_get_string_descriptor_ascii(device, str_index,
  98. (unsigned char *)desc_string, sizeof(desc_string)-1);
  99. if (retval < 0) {
  100. LOG_ERROR("libusb_get_string_descriptor_ascii() failed with %d", retval);
  101. return false;
  102. }
  103. /* Null terminate descriptor string in case it needs to be logged. */
  104. desc_string[sizeof(desc_string)-1] = '\0';
  105. matched = strncmp(string, desc_string, sizeof(desc_string)) == 0;
  106. if (!matched)
  107. LOG_DEBUG("Device serial number '%s' doesn't match requested serial '%s'",
  108. desc_string, string);
  109. return matched;
  110. }
  111. static bool jtag_libusb_match_serial(struct libusb_device_handle *device,
  112. struct libusb_device_descriptor *dev_desc, const char *serial,
  113. adapter_get_alternate_serial_fn adapter_get_alternate_serial)
  114. {
  115. if (string_descriptor_equal(device, dev_desc->iSerialNumber, serial))
  116. return true;
  117. /* check the alternate serial helper */
  118. if (!adapter_get_alternate_serial)
  119. return false;
  120. /* get the alternate serial */
  121. char *alternate_serial = adapter_get_alternate_serial(device, dev_desc);
  122. /* check possible failures */
  123. if (!alternate_serial)
  124. return false;
  125. /* then compare and free the alternate serial */
  126. bool match = false;
  127. if (strcmp(serial, alternate_serial) == 0)
  128. match = true;
  129. else
  130. LOG_DEBUG("Device alternate serial number '%s' doesn't match requested serial '%s'",
  131. alternate_serial, serial);
  132. free(alternate_serial);
  133. return match;
  134. }
  135. int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[],
  136. const char *serial,
  137. struct libusb_device_handle **out,
  138. adapter_get_alternate_serial_fn adapter_get_alternate_serial)
  139. {
  140. int cnt, idx, err_code;
  141. int retval = ERROR_FAIL;
  142. bool serial_mismatch = false;
  143. struct libusb_device_handle *libusb_handle = NULL;
  144. if (libusb_init(&jtag_libusb_context) < 0)
  145. return ERROR_FAIL;
  146. cnt = libusb_get_device_list(jtag_libusb_context, &devs);
  147. for (idx = 0; idx < cnt; idx++) {
  148. struct libusb_device_descriptor dev_desc;
  149. if (libusb_get_device_descriptor(devs[idx], &dev_desc) != 0)
  150. continue;
  151. if (!jtag_libusb_match_ids(&dev_desc, vids, pids))
  152. continue;
  153. if (jtag_usb_get_location() && !jtag_libusb_location_equal(devs[idx]))
  154. continue;
  155. err_code = libusb_open(devs[idx], &libusb_handle);
  156. if (err_code) {
  157. LOG_ERROR("libusb_open() failed with %s",
  158. libusb_error_name(err_code));
  159. continue;
  160. }
  161. /* Device must be open to use libusb_get_string_descriptor_ascii. */
  162. if (serial &&
  163. !jtag_libusb_match_serial(libusb_handle, &dev_desc, serial, adapter_get_alternate_serial)) {
  164. serial_mismatch = true;
  165. libusb_close(libusb_handle);
  166. continue;
  167. }
  168. /* Success. */
  169. *out = libusb_handle;
  170. retval = ERROR_OK;
  171. serial_mismatch = false;
  172. break;
  173. }
  174. if (cnt >= 0)
  175. libusb_free_device_list(devs, 1);
  176. if (serial_mismatch)
  177. LOG_INFO("No device matches the serial string");
  178. if (retval != ERROR_OK)
  179. libusb_exit(jtag_libusb_context);
  180. return retval;
  181. }
  182. void jtag_libusb_close(struct libusb_device_handle *dev)
  183. {
  184. /* Close device */
  185. libusb_close(dev);
  186. libusb_exit(jtag_libusb_context);
  187. }
  188. int jtag_libusb_control_transfer(struct libusb_device_handle *dev, uint8_t request_type,
  189. uint8_t request, uint16_t value, uint16_t index, char *bytes,
  190. uint16_t size, unsigned int timeout)
  191. {
  192. int transferred = 0;
  193. transferred = libusb_control_transfer(dev, request_type, request, value, index,
  194. (unsigned char *)bytes, size, timeout);
  195. if (transferred < 0)
  196. transferred = 0;
  197. return transferred;
  198. }
  199. int jtag_libusb_bulk_write(struct libusb_device_handle *dev, int ep, char *bytes,
  200. int size, int timeout, int *transferred)
  201. {
  202. int ret;
  203. *transferred = 0;
  204. ret = libusb_bulk_transfer(dev, ep, (unsigned char *)bytes, size,
  205. transferred, timeout);
  206. if (ret != LIBUSB_SUCCESS) {
  207. LOG_ERROR("libusb_bulk_write error: %s", libusb_error_name(ret));
  208. return jtag_libusb_error(ret);
  209. }
  210. return ERROR_OK;
  211. }
  212. int jtag_libusb_bulk_read(struct libusb_device_handle *dev, int ep, char *bytes,
  213. int size, int timeout, int *transferred)
  214. {
  215. int ret;
  216. *transferred = 0;
  217. ret = libusb_bulk_transfer(dev, ep, (unsigned char *)bytes, size,
  218. transferred, timeout);
  219. if (ret != LIBUSB_SUCCESS) {
  220. LOG_ERROR("libusb_bulk_read error: %s", libusb_error_name(ret));
  221. return jtag_libusb_error(ret);
  222. }
  223. return ERROR_OK;
  224. }
  225. int jtag_libusb_set_configuration(struct libusb_device_handle *devh,
  226. int configuration)
  227. {
  228. struct libusb_device *udev = libusb_get_device(devh);
  229. int retval = -99;
  230. struct libusb_config_descriptor *config = NULL;
  231. int current_config = -1;
  232. retval = libusb_get_configuration(devh, &current_config);
  233. if (retval != 0)
  234. return retval;
  235. retval = libusb_get_config_descriptor(udev, configuration, &config);
  236. if (retval != 0 || !config)
  237. return retval;
  238. /* Only change the configuration if it is not already set to the
  239. same one. Otherwise this issues a lightweight reset and hangs
  240. LPC-Link2 with JLink firmware. */
  241. if (current_config != config->bConfigurationValue)
  242. retval = libusb_set_configuration(devh, config->bConfigurationValue);
  243. libusb_free_config_descriptor(config);
  244. return retval;
  245. }
  246. int jtag_libusb_choose_interface(struct libusb_device_handle *devh,
  247. unsigned int *usb_read_ep,
  248. unsigned int *usb_write_ep,
  249. int bclass, int subclass, int protocol, int trans_type)
  250. {
  251. struct libusb_device *udev = libusb_get_device(devh);
  252. const struct libusb_interface *inter;
  253. const struct libusb_interface_descriptor *interdesc;
  254. const struct libusb_endpoint_descriptor *epdesc;
  255. struct libusb_config_descriptor *config;
  256. *usb_read_ep = *usb_write_ep = 0;
  257. libusb_get_config_descriptor(udev, 0, &config);
  258. for (int i = 0; i < (int)config->bNumInterfaces; i++) {
  259. inter = &config->interface[i];
  260. interdesc = &inter->altsetting[0];
  261. for (int k = 0;
  262. k < (int)interdesc->bNumEndpoints; k++) {
  263. if ((bclass > 0 && interdesc->bInterfaceClass != bclass) ||
  264. (subclass > 0 && interdesc->bInterfaceSubClass != subclass) ||
  265. (protocol > 0 && interdesc->bInterfaceProtocol != protocol))
  266. continue;
  267. epdesc = &interdesc->endpoint[k];
  268. if (trans_type > 0 && (epdesc->bmAttributes & 0x3) != trans_type)
  269. continue;
  270. uint8_t epnum = epdesc->bEndpointAddress;
  271. bool is_input = epnum & 0x80;
  272. LOG_DEBUG("usb ep %s %02x",
  273. is_input ? "in" : "out", epnum);
  274. if (is_input)
  275. *usb_read_ep = epnum;
  276. else
  277. *usb_write_ep = epnum;
  278. if (*usb_read_ep && *usb_write_ep) {
  279. LOG_DEBUG("Claiming interface %d", (int)interdesc->bInterfaceNumber);
  280. libusb_claim_interface(devh, (int)interdesc->bInterfaceNumber);
  281. libusb_free_config_descriptor(config);
  282. return ERROR_OK;
  283. }
  284. }
  285. }
  286. libusb_free_config_descriptor(config);
  287. return ERROR_FAIL;
  288. }
  289. int jtag_libusb_get_pid(struct libusb_device *dev, uint16_t *pid)
  290. {
  291. struct libusb_device_descriptor dev_desc;
  292. if (libusb_get_device_descriptor(dev, &dev_desc) == 0) {
  293. *pid = dev_desc.idProduct;
  294. return ERROR_OK;
  295. }
  296. return ERROR_FAIL;
  297. }
  298. int jtag_libusb_handle_events_completed(int *completed)
  299. {
  300. return libusb_handle_events_completed(jtag_libusb_context, completed);
  301. }