libusb: add debug message on adapter not found due to wrong serial
[openocd.git] / src / jtag / drivers / libusb0_common.c
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
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 #include "log.h"
24 #include "libusb0_common.h"
25
26 static bool jtag_libusb_match(struct jtag_libusb_device *dev,
27 const uint16_t vids[], const uint16_t pids[])
28 {
29 for (unsigned i = 0; vids[i]; i++) {
30 if (dev->descriptor.idVendor == vids[i] &&
31 dev->descriptor.idProduct == pids[i]) {
32 return true;
33 }
34 }
35 return false;
36 }
37
38 /* Returns true if the string descriptor indexed by str_index in device matches string */
39 static bool string_descriptor_equal(usb_dev_handle *device, uint8_t str_index,
40 const char *string)
41 {
42 int retval;
43 bool matched;
44 char desc_string[256+1]; /* Max size of string descriptor */
45
46 if (str_index == 0)
47 return false;
48
49 retval = usb_get_string_simple(device, str_index,
50 desc_string, sizeof(desc_string)-1);
51 if (retval < 0) {
52 LOG_ERROR("usb_get_string_simple() failed with %d", retval);
53 return false;
54 }
55
56 /* Null terminate descriptor string in case it needs to be logged. */
57 desc_string[sizeof(desc_string)-1] = '\0';
58
59 matched = strncmp(string, desc_string, sizeof(desc_string)) == 0;
60 if (!matched)
61 LOG_DEBUG("Device serial number '%s' doesn't match requested serial '%s'",
62 desc_string, string);
63 return matched;
64 }
65
66 int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[],
67 const char *serial,
68 struct jtag_libusb_device_handle **out)
69 {
70 int retval = ERROR_FAIL;
71 bool serial_mismatch = false;
72 struct jtag_libusb_device_handle *libusb_handle;
73 usb_init();
74
75 usb_find_busses();
76 usb_find_devices();
77
78 struct usb_bus *busses = usb_get_busses();
79 for (struct usb_bus *bus = busses; bus; bus = bus->next) {
80 for (struct usb_device *dev = bus->devices;
81 dev; dev = dev->next) {
82 if (!jtag_libusb_match(dev, vids, pids))
83 continue;
84
85 libusb_handle = usb_open(dev);
86 if (NULL == libusb_handle) {
87 LOG_ERROR("usb_open() failed with %s", usb_strerror());
88 continue;
89 }
90
91 /* Device must be open to use libusb_get_string_descriptor_ascii. */
92 if (serial != NULL &&
93 !string_descriptor_equal(libusb_handle, dev->descriptor.iSerialNumber, serial)) {
94 serial_mismatch = true;
95 usb_close(libusb_handle);
96 continue;
97 }
98 *out = libusb_handle;
99 retval = ERROR_OK;
100 serial_mismatch = false;
101 break;
102 }
103 }
104
105 if (serial_mismatch)
106 LOG_INFO("No device matches the serial string");
107
108 return retval;
109 }
110
111 void jtag_libusb_close(jtag_libusb_device_handle *dev)
112 {
113 /* Close device */
114 usb_close(dev);
115 }
116
117 int jtag_libusb_control_transfer(jtag_libusb_device_handle *dev, uint8_t requestType,
118 uint8_t request, uint16_t wValue, uint16_t wIndex, char *bytes,
119 uint16_t size, unsigned int timeout)
120 {
121 int transferred = 0;
122
123 transferred = usb_control_msg(dev, requestType, request, wValue, wIndex,
124 bytes, size, timeout);
125
126 if (transferred < 0)
127 transferred = 0;
128
129 return transferred;
130 }
131
132 int jtag_libusb_bulk_write(jtag_libusb_device_handle *dev, int ep, char *bytes,
133 int size, int timeout)
134 {
135 return usb_bulk_write(dev, ep, bytes, size, timeout);
136 }
137
138 int jtag_libusb_bulk_read(jtag_libusb_device_handle *dev, int ep, char *bytes,
139 int size, int timeout)
140 {
141 return usb_bulk_read(dev, ep, bytes, size, timeout);
142 }
143
144 int jtag_libusb_set_configuration(jtag_libusb_device_handle *devh,
145 int configuration)
146 {
147 struct jtag_libusb_device *udev = jtag_libusb_get_device(devh);
148
149 return usb_set_configuration(devh,
150 udev->config[configuration].bConfigurationValue);
151 }
152
153 int jtag_libusb_choose_interface(struct jtag_libusb_device_handle *devh,
154 unsigned int *usb_read_ep,
155 unsigned int *usb_write_ep,
156 int bclass, int subclass, int protocol, int trans_type)
157 {
158 struct jtag_libusb_device *udev = jtag_libusb_get_device(devh);
159 struct usb_interface *iface = udev->config->interface;
160 struct usb_interface_descriptor *desc = iface->altsetting;
161
162 *usb_read_ep = *usb_write_ep = 0;
163
164 for (int i = 0; i < desc->bNumEndpoints; i++) {
165 if ((bclass > 0 && desc->bInterfaceClass != bclass) ||
166 (subclass > 0 && desc->bInterfaceSubClass != subclass) ||
167 (protocol > 0 && desc->bInterfaceProtocol != protocol) ||
168 (trans_type > 0 && (desc->endpoint[i].bmAttributes & 0x3) != trans_type))
169 continue;
170
171 uint8_t epnum = desc->endpoint[i].bEndpointAddress;
172 bool is_input = epnum & 0x80;
173 LOG_DEBUG("usb ep %s %02x", is_input ? "in" : "out", epnum);
174 if (is_input)
175 *usb_read_ep = epnum;
176 else
177 *usb_write_ep = epnum;
178
179 if (*usb_read_ep && *usb_write_ep) {
180 LOG_DEBUG("Claiming interface %d", (int)desc->bInterfaceNumber);
181 usb_claim_interface(devh, (int)desc->bInterfaceNumber);
182 return ERROR_OK;
183 }
184 }
185
186 return ERROR_FAIL;
187 }
188
189 int jtag_libusb_get_pid(struct jtag_libusb_device *dev, uint16_t *pid)
190 {
191 if (!dev)
192 return ERROR_FAIL;
193
194 *pid = dev->descriptor.idProduct;
195 return ERROR_OK;
196 }