tcl/target: Switch Renesas R-Car Gen2 boards to new config
[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 int jtag_libusb_error(int err)
27 {
28 switch (err) {
29 case 0:
30 return ERROR_OK;
31 case -ETIMEDOUT:
32 return ERROR_TIMEOUT_REACHED;
33 default:
34 return ERROR_FAIL;
35 }
36 }
37
38 static bool jtag_libusb_match(struct jtag_libusb_device *dev,
39 const uint16_t vids[], const uint16_t pids[])
40 {
41 for (unsigned i = 0; vids[i]; i++) {
42 if (dev->descriptor.idVendor == vids[i] &&
43 dev->descriptor.idProduct == pids[i]) {
44 return true;
45 }
46 }
47 return false;
48 }
49
50 /* Returns true if the string descriptor indexed by str_index in device matches string */
51 static bool string_descriptor_equal(usb_dev_handle *device, uint8_t str_index,
52 const char *string)
53 {
54 int retval;
55 bool matched;
56 char desc_string[256+1]; /* Max size of string descriptor */
57
58 if (str_index == 0)
59 return false;
60
61 retval = usb_get_string_simple(device, str_index,
62 desc_string, sizeof(desc_string)-1);
63 if (retval < 0) {
64 LOG_ERROR("usb_get_string_simple() failed with %d", retval);
65 return false;
66 }
67
68 /* Null terminate descriptor string in case it needs to be logged. */
69 desc_string[sizeof(desc_string)-1] = '\0';
70
71 matched = strncmp(string, desc_string, sizeof(desc_string)) == 0;
72 if (!matched)
73 LOG_DEBUG("Device serial number '%s' doesn't match requested serial '%s'",
74 desc_string, string);
75 return matched;
76 }
77
78 int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[],
79 const char *serial,
80 struct jtag_libusb_device_handle **out)
81 {
82 int retval = ERROR_FAIL;
83 bool serial_mismatch = false;
84 struct jtag_libusb_device_handle *libusb_handle;
85 usb_init();
86
87 usb_find_busses();
88 usb_find_devices();
89
90 struct usb_bus *busses = usb_get_busses();
91 for (struct usb_bus *bus = busses; bus; bus = bus->next) {
92 for (struct usb_device *dev = bus->devices;
93 dev; dev = dev->next) {
94 if (!jtag_libusb_match(dev, vids, pids))
95 continue;
96
97 libusb_handle = usb_open(dev);
98 if (NULL == libusb_handle) {
99 LOG_ERROR("usb_open() failed with %s", usb_strerror());
100 continue;
101 }
102
103 /* Device must be open to use libusb_get_string_descriptor_ascii. */
104 if (serial != NULL &&
105 !string_descriptor_equal(libusb_handle, dev->descriptor.iSerialNumber, serial)) {
106 serial_mismatch = true;
107 usb_close(libusb_handle);
108 continue;
109 }
110 *out = libusb_handle;
111 retval = ERROR_OK;
112 serial_mismatch = false;
113 break;
114 }
115 }
116
117 if (serial_mismatch)
118 LOG_INFO("No device matches the serial string");
119
120 return retval;
121 }
122
123 void jtag_libusb_close(jtag_libusb_device_handle *dev)
124 {
125 /* Close device */
126 usb_close(dev);
127 }
128
129 int jtag_libusb_control_transfer(jtag_libusb_device_handle *dev, uint8_t requestType,
130 uint8_t request, uint16_t wValue, uint16_t wIndex, char *bytes,
131 uint16_t size, unsigned int timeout)
132 {
133 int transferred = 0;
134
135 transferred = usb_control_msg(dev, requestType, request, wValue, wIndex,
136 bytes, size, timeout);
137
138 if (transferred < 0)
139 transferred = 0;
140
141 return transferred;
142 }
143
144 int jtag_libusb_bulk_write(jtag_libusb_device_handle *dev, int ep, char *bytes,
145 int size, int timeout, int *transferred)
146 {
147 int ret;
148
149 *transferred = 0;
150
151 ret = usb_bulk_write(dev, ep, bytes, size, timeout);
152
153 if (ret < 0) {
154 LOG_ERROR("usb_bulk_write error: %i", ret);
155 return jtag_libusb_error(ret);
156 }
157
158 return ERROR_OK;
159 }
160
161 int jtag_libusb_bulk_read(jtag_libusb_device_handle *dev, int ep, char *bytes,
162 int size, int timeout, int *transferred)
163 {
164 int ret;
165
166 *transferred = 0;
167
168 ret = usb_bulk_read(dev, ep, bytes, size, timeout);
169
170 if (ret < 0) {
171 LOG_ERROR("usb_bulk_read error: %i", ret);
172 return jtag_libusb_error(ret);
173 }
174
175 return ERROR_OK;
176 }
177
178 int jtag_libusb_set_configuration(jtag_libusb_device_handle *devh,
179 int configuration)
180 {
181 struct jtag_libusb_device *udev = jtag_libusb_get_device(devh);
182
183 return usb_set_configuration(devh,
184 udev->config[configuration].bConfigurationValue);
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 struct usb_interface *iface = udev->config->interface;
194 struct usb_interface_descriptor *desc = iface->altsetting;
195
196 *usb_read_ep = *usb_write_ep = 0;
197
198 for (int i = 0; i < desc->bNumEndpoints; i++) {
199 if ((bclass > 0 && desc->bInterfaceClass != bclass) ||
200 (subclass > 0 && desc->bInterfaceSubClass != subclass) ||
201 (protocol > 0 && desc->bInterfaceProtocol != protocol) ||
202 (trans_type > 0 && (desc->endpoint[i].bmAttributes & 0x3) != trans_type))
203 continue;
204
205 uint8_t epnum = desc->endpoint[i].bEndpointAddress;
206 bool is_input = epnum & 0x80;
207 LOG_DEBUG("usb ep %s %02x", is_input ? "in" : "out", epnum);
208 if (is_input)
209 *usb_read_ep = epnum;
210 else
211 *usb_write_ep = epnum;
212
213 if (*usb_read_ep && *usb_write_ep) {
214 LOG_DEBUG("Claiming interface %d", (int)desc->bInterfaceNumber);
215 usb_claim_interface(devh, (int)desc->bInterfaceNumber);
216 return ERROR_OK;
217 }
218 }
219
220 return ERROR_FAIL;
221 }
222
223 int jtag_libusb_get_pid(struct jtag_libusb_device *dev, uint16_t *pid)
224 {
225 if (!dev)
226 return ERROR_FAIL;
227
228 *pid = dev->descriptor.idProduct;
229 return ERROR_OK;
230 }

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)