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