1 /***************************************************************************
2 * Copyright (C) 2009 by Zachary T Welch <zw@superlucidity.net> *
4 * Copyright (C) 2011 by Mauro Gamba <maurillo71@gmail.com> *
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. *
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. *
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 ***************************************************************************/
23 #include <jtag/drivers/jtag_usb_common.h>
24 #include "libusb1_common.h"
28 * comment from libusb:
29 * As per the USB 3.0 specs, the current maximum limit for the depth is 7.
31 #define MAX_USB_PORTS 7
33 static struct libusb_context *jtag_libusb_context; /**< Libusb context **/
34 static libusb_device **devs; /**< The usb device list **/
36 static int jtag_libusb_error(int err)
41 case LIBUSB_ERROR_TIMEOUT:
42 return ERROR_TIMEOUT_REACHED;
44 case LIBUSB_ERROR_INVALID_PARAM:
45 case LIBUSB_ERROR_ACCESS:
46 case LIBUSB_ERROR_NO_DEVICE:
47 case LIBUSB_ERROR_NOT_FOUND:
48 case LIBUSB_ERROR_BUSY:
49 case LIBUSB_ERROR_OVERFLOW:
50 case LIBUSB_ERROR_PIPE:
51 case LIBUSB_ERROR_INTERRUPTED:
52 case LIBUSB_ERROR_NO_MEM:
53 case LIBUSB_ERROR_NOT_SUPPORTED:
54 case LIBUSB_ERROR_OTHER:
61 static bool jtag_libusb_match(struct libusb_device_descriptor *dev_desc,
62 const uint16_t vids[], const uint16_t pids[])
64 for (unsigned i = 0; vids[i]; i++) {
65 if (dev_desc->idVendor == vids[i] &&
66 dev_desc->idProduct == pids[i]) {
73 #ifdef HAVE_LIBUSB_GET_PORT_NUMBERS
74 static bool jtag_libusb_location_equal(libusb_device *device)
76 uint8_t port_path[MAX_USB_PORTS];
80 path_len = libusb_get_port_numbers(device, port_path, MAX_USB_PORTS);
81 if (path_len == LIBUSB_ERROR_OVERFLOW) {
82 LOG_WARNING("cannot determine path to usb device! (more than %i ports in path)\n",
86 dev_bus = libusb_get_bus_number(device);
88 return jtag_usb_location_equal(dev_bus, port_path, path_len);
90 #else /* HAVE_LIBUSB_GET_PORT_NUMBERS */
91 static bool jtag_libusb_location_equal(libusb_device *device)
95 #endif /* HAVE_LIBUSB_GET_PORT_NUMBERS */
98 /* Returns true if the string descriptor indexed by str_index in device matches string */
99 static bool string_descriptor_equal(libusb_device_handle *device, uint8_t str_index,
104 char desc_string[256+1]; /* Max size of string descriptor */
109 retval = libusb_get_string_descriptor_ascii(device, str_index,
110 (unsigned char *)desc_string, sizeof(desc_string)-1);
112 LOG_ERROR("libusb_get_string_descriptor_ascii() failed with %d", retval);
116 /* Null terminate descriptor string in case it needs to be logged. */
117 desc_string[sizeof(desc_string)-1] = '\0';
119 matched = strncmp(string, desc_string, sizeof(desc_string)) == 0;
121 LOG_DEBUG("Device serial number '%s' doesn't match requested serial '%s'",
122 desc_string, string);
126 int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[],
128 struct jtag_libusb_device_handle **out)
130 int cnt, idx, errCode;
131 int retval = ERROR_FAIL;
132 bool serial_mismatch = false;
133 struct jtag_libusb_device_handle *libusb_handle = NULL;
135 if (libusb_init(&jtag_libusb_context) < 0)
138 cnt = libusb_get_device_list(jtag_libusb_context, &devs);
140 for (idx = 0; idx < cnt; idx++) {
141 struct libusb_device_descriptor dev_desc;
143 if (libusb_get_device_descriptor(devs[idx], &dev_desc) != 0)
146 if (!jtag_libusb_match(&dev_desc, vids, pids))
149 if (jtag_usb_get_location() && !jtag_libusb_location_equal(devs[idx]))
152 errCode = libusb_open(devs[idx], &libusb_handle);
155 LOG_ERROR("libusb_open() failed with %s",
156 libusb_error_name(errCode));
160 /* Device must be open to use libusb_get_string_descriptor_ascii. */
161 if (serial != NULL &&
162 !string_descriptor_equal(libusb_handle, dev_desc.iSerialNumber, serial)) {
163 serial_mismatch = true;
164 libusb_close(libusb_handle);
169 *out = libusb_handle;
171 serial_mismatch = false;
175 libusb_free_device_list(devs, 1);
178 LOG_INFO("No device matches the serial string");
183 void jtag_libusb_close(jtag_libusb_device_handle *dev)
188 libusb_exit(jtag_libusb_context);
191 int jtag_libusb_control_transfer(jtag_libusb_device_handle *dev, uint8_t requestType,
192 uint8_t request, uint16_t wValue, uint16_t wIndex, char *bytes,
193 uint16_t size, unsigned int timeout)
197 transferred = libusb_control_transfer(dev, requestType, request, wValue, wIndex,
198 (unsigned char *)bytes, size, timeout);
206 int jtag_libusb_bulk_write(jtag_libusb_device_handle *dev, int ep, char *bytes,
207 int size, int timeout, int *transferred)
213 ret = libusb_bulk_transfer(dev, ep, (unsigned char *)bytes, size,
214 transferred, timeout);
215 if (ret != LIBUSB_SUCCESS) {
216 LOG_ERROR("libusb_bulk_write error: %s", libusb_error_name(ret));
217 return jtag_libusb_error(ret);
223 int jtag_libusb_bulk_read(jtag_libusb_device_handle *dev, int ep, char *bytes,
224 int size, int timeout, int *transferred)
230 ret = libusb_bulk_transfer(dev, ep, (unsigned char *)bytes, size,
231 transferred, timeout);
232 if (ret != LIBUSB_SUCCESS) {
233 LOG_ERROR("libusb_bulk_read error: %s", libusb_error_name(ret));
234 return jtag_libusb_error(ret);
240 int jtag_libusb_set_configuration(jtag_libusb_device_handle *devh,
243 struct jtag_libusb_device *udev = jtag_libusb_get_device(devh);
246 struct libusb_config_descriptor *config = NULL;
247 int current_config = -1;
249 retCode = libusb_get_configuration(devh, ¤t_config);
253 retCode = libusb_get_config_descriptor(udev, configuration, &config);
254 if (retCode != 0 || config == NULL)
257 /* Only change the configuration if it is not already set to the
258 same one. Otherwise this issues a lightweight reset and hangs
259 LPC-Link2 with JLink firmware. */
260 if (current_config != config->bConfigurationValue)
261 retCode = libusb_set_configuration(devh, config->bConfigurationValue);
263 libusb_free_config_descriptor(config);
268 int jtag_libusb_choose_interface(struct jtag_libusb_device_handle *devh,
269 unsigned int *usb_read_ep,
270 unsigned int *usb_write_ep,
271 int bclass, int subclass, int protocol, int trans_type)
273 struct jtag_libusb_device *udev = jtag_libusb_get_device(devh);
274 const struct libusb_interface *inter;
275 const struct libusb_interface_descriptor *interdesc;
276 const struct libusb_endpoint_descriptor *epdesc;
277 struct libusb_config_descriptor *config;
279 *usb_read_ep = *usb_write_ep = 0;
281 libusb_get_config_descriptor(udev, 0, &config);
282 for (int i = 0; i < (int)config->bNumInterfaces; i++) {
283 inter = &config->interface[i];
285 interdesc = &inter->altsetting[0];
287 k < (int)interdesc->bNumEndpoints; k++) {
288 if ((bclass > 0 && interdesc->bInterfaceClass != bclass) ||
289 (subclass > 0 && interdesc->bInterfaceSubClass != subclass) ||
290 (protocol > 0 && interdesc->bInterfaceProtocol != protocol))
293 epdesc = &interdesc->endpoint[k];
294 if (trans_type > 0 && (epdesc->bmAttributes & 0x3) != trans_type)
297 uint8_t epnum = epdesc->bEndpointAddress;
298 bool is_input = epnum & 0x80;
299 LOG_DEBUG("usb ep %s %02x",
300 is_input ? "in" : "out", epnum);
303 *usb_read_ep = epnum;
305 *usb_write_ep = epnum;
307 if (*usb_read_ep && *usb_write_ep) {
308 LOG_DEBUG("Claiming interface %d", (int)interdesc->bInterfaceNumber);
309 libusb_claim_interface(devh, (int)interdesc->bInterfaceNumber);
310 libusb_free_config_descriptor(config);
315 libusb_free_config_descriptor(config);
320 int jtag_libusb_get_pid(struct jtag_libusb_device *dev, uint16_t *pid)
322 struct libusb_device_descriptor dev_desc;
324 if (libusb_get_device_descriptor(dev, &dev_desc) == 0) {
325 *pid = dev_desc.idProduct;