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

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)