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, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
20 ***************************************************************************/
26 #include "libusb0_common.h"
28 static bool jtag_libusb_match(struct jtag_libusb_device *dev,
29 const uint16_t vids[], const uint16_t pids[])
31 for (unsigned i = 0; vids[i]; i++) {
32 if (dev->descriptor.idVendor == vids[i] &&
33 dev->descriptor.idProduct == pids[i]) {
40 /* Returns true if the string descriptor indexed by str_index in device matches string */
41 static bool string_descriptor_equal(usb_dev_handle *device, uint8_t str_index,
46 char desc_string[256+1]; /* Max size of string descriptor */
51 retval = usb_get_string_simple(device, str_index,
52 desc_string, sizeof(desc_string)-1);
54 LOG_ERROR("usb_get_string_simple() failed with %d", retval);
58 /* Null terminate descriptor string in case it needs to be logged. */
59 desc_string[sizeof(desc_string)-1] = '\0';
61 matched = strncmp(string, desc_string, sizeof(desc_string)) == 0;
63 LOG_DEBUG("Device serial number '%s' doesn't match requested serial '%s'",
68 int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[],
70 struct jtag_libusb_device_handle **out)
73 struct jtag_libusb_device_handle *libusb_handle;
79 struct usb_bus *busses = usb_get_busses();
80 for (struct usb_bus *bus = busses; bus; bus = bus->next) {
81 for (struct usb_device *dev = bus->devices;
82 dev; dev = dev->next) {
83 if (!jtag_libusb_match(dev, vids, pids))
86 libusb_handle = usb_open(dev);
87 if (NULL == libusb_handle) {
92 /* Device must be open to use libusb_get_string_descriptor_ascii. */
94 !string_descriptor_equal(libusb_handle, dev->descriptor.iSerialNumber, serial)) {
95 usb_close(libusb_handle);
106 void jtag_libusb_close(jtag_libusb_device_handle *dev)
112 int jtag_libusb_control_transfer(jtag_libusb_device_handle *dev, uint8_t requestType,
113 uint8_t request, uint16_t wValue, uint16_t wIndex, char *bytes,
114 uint16_t size, unsigned int timeout)
118 transferred = usb_control_msg(dev, requestType, request, wValue, wIndex,
119 bytes, size, timeout);
127 int jtag_libusb_bulk_write(jtag_libusb_device_handle *dev, int ep, char *bytes,
128 int size, int timeout)
130 return usb_bulk_write(dev, ep, bytes, size, timeout);
133 int jtag_libusb_bulk_read(jtag_libusb_device_handle *dev, int ep, char *bytes,
134 int size, int timeout)
136 return usb_bulk_read(dev, ep, bytes, size, timeout);
139 int jtag_libusb_set_configuration(jtag_libusb_device_handle *devh,
142 struct jtag_libusb_device *udev = jtag_libusb_get_device(devh);
144 return usb_set_configuration(devh,
145 udev->config[configuration].bConfigurationValue);
148 int jtag_libusb_choose_interface(struct jtag_libusb_device_handle *devh,
149 unsigned int *usb_read_ep,
150 unsigned int *usb_write_ep,
151 int bclass, int subclass, int protocol)
153 struct jtag_libusb_device *udev = jtag_libusb_get_device(devh);
154 struct usb_interface *iface = udev->config->interface;
155 struct usb_interface_descriptor *desc = iface->altsetting;
157 *usb_read_ep = *usb_write_ep = 0;
159 for (int i = 0; i < desc->bNumEndpoints; i++) {
160 if ((bclass > 0 && desc->bInterfaceClass != bclass) ||
161 (subclass > 0 && desc->bInterfaceSubClass != subclass) ||
162 (protocol > 0 && desc->bInterfaceProtocol != protocol))
165 uint8_t epnum = desc->endpoint[i].bEndpointAddress;
166 bool is_input = epnum & 0x80;
167 LOG_DEBUG("usb ep %s %02x", is_input ? "in" : "out", epnum);
169 *usb_read_ep = epnum;
171 *usb_write_ep = epnum;
173 if (*usb_read_ep && *usb_write_ep) {
174 LOG_DEBUG("Claiming interface %d", (int)desc->bInterfaceNumber);
175 usb_claim_interface(devh, (int)desc->bInterfaceNumber);
183 int jtag_libusb_get_pid(struct jtag_libusb_device *dev, uint16_t *pid)
188 *pid = dev->descriptor.idProduct;