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, see <http://www.gnu.org/licenses/>. *
18 ***************************************************************************/
24 #include "libusb1_common.h"
26 static struct libusb_context
*jtag_libusb_context
; /**< Libusb context **/
27 static libusb_device
**devs
; /**< The usb device list **/
29 static bool jtag_libusb_match(struct libusb_device_descriptor
*dev_desc
,
30 const uint16_t vids
[], const uint16_t pids
[])
32 for (unsigned i
= 0; vids
[i
]; i
++) {
33 if (dev_desc
->idVendor
== vids
[i
] &&
34 dev_desc
->idProduct
== pids
[i
]) {
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
,
47 char desc_string
[256+1]; /* Max size of string descriptor */
52 retval
= libusb_get_string_descriptor_ascii(device
, str_index
,
53 (unsigned char *)desc_string
, sizeof(desc_string
)-1);
55 LOG_ERROR("libusb_get_string_descriptor_ascii() failed with %d", retval
);
59 /* Null terminate descriptor string in case it needs to be logged. */
60 desc_string
[sizeof(desc_string
)-1] = '\0';
62 matched
= strncmp(string
, desc_string
, sizeof(desc_string
)) == 0;
64 LOG_DEBUG("Device serial number '%s' doesn't match requested serial '%s'",
69 int jtag_libusb_open(const uint16_t vids
[], const uint16_t pids
[],
71 struct jtag_libusb_device_handle
**out
)
73 int cnt
, idx
, errCode
;
74 int retval
= ERROR_FAIL
;
75 bool serial_mismatch
= false;
76 struct jtag_libusb_device_handle
*libusb_handle
= NULL
;
78 if (libusb_init(&jtag_libusb_context
) < 0)
81 cnt
= libusb_get_device_list(jtag_libusb_context
, &devs
);
83 for (idx
= 0; idx
< cnt
; idx
++) {
84 struct libusb_device_descriptor dev_desc
;
86 if (libusb_get_device_descriptor(devs
[idx
], &dev_desc
) != 0)
89 if (!jtag_libusb_match(&dev_desc
, vids
, pids
))
92 errCode
= libusb_open(devs
[idx
], &libusb_handle
);
95 LOG_ERROR("libusb_open() failed with %s",
96 libusb_error_name(errCode
));
100 /* Device must be open to use libusb_get_string_descriptor_ascii. */
101 if (serial
!= NULL
&&
102 !string_descriptor_equal(libusb_handle
, dev_desc
.iSerialNumber
, serial
)) {
103 serial_mismatch
= true;
104 libusb_close(libusb_handle
);
109 *out
= libusb_handle
;
111 serial_mismatch
= false;
115 libusb_free_device_list(devs
, 1);
118 LOG_INFO("No device matches the serial string");
123 void jtag_libusb_close(jtag_libusb_device_handle
*dev
)
128 libusb_exit(jtag_libusb_context
);
131 int jtag_libusb_control_transfer(jtag_libusb_device_handle
*dev
, uint8_t requestType
,
132 uint8_t request
, uint16_t wValue
, uint16_t wIndex
, char *bytes
,
133 uint16_t size
, unsigned int timeout
)
137 transferred
= libusb_control_transfer(dev
, requestType
, request
, wValue
, wIndex
,
138 (unsigned char *)bytes
, size
, timeout
);
146 int jtag_libusb_bulk_write(jtag_libusb_device_handle
*dev
, int ep
, char *bytes
,
147 int size
, int timeout
)
151 libusb_bulk_transfer(dev
, ep
, (unsigned char *)bytes
, size
,
152 &transferred
, timeout
);
156 int jtag_libusb_bulk_read(jtag_libusb_device_handle
*dev
, int ep
, char *bytes
,
157 int size
, int timeout
)
161 libusb_bulk_transfer(dev
, ep
, (unsigned char *)bytes
, size
,
162 &transferred
, timeout
);
166 int jtag_libusb_set_configuration(jtag_libusb_device_handle
*devh
,
169 struct jtag_libusb_device
*udev
= jtag_libusb_get_device(devh
);
172 struct libusb_config_descriptor
*config
= NULL
;
173 int current_config
= -1;
175 retCode
= libusb_get_configuration(devh
, ¤t_config
);
179 retCode
= libusb_get_config_descriptor(udev
, configuration
, &config
);
180 if (retCode
!= 0 || config
== NULL
)
183 /* Only change the configuration if it is not already set to the
184 same one. Otherwise this issues a lightweight reset and hangs
185 LPC-Link2 with JLink firmware. */
186 if (current_config
!= config
->bConfigurationValue
)
187 retCode
= libusb_set_configuration(devh
, config
->bConfigurationValue
);
189 libusb_free_config_descriptor(config
);
194 int jtag_libusb_choose_interface(struct jtag_libusb_device_handle
*devh
,
195 unsigned int *usb_read_ep
,
196 unsigned int *usb_write_ep
,
197 int bclass
, int subclass
, int protocol
, int trans_type
)
199 struct jtag_libusb_device
*udev
= jtag_libusb_get_device(devh
);
200 const struct libusb_interface
*inter
;
201 const struct libusb_interface_descriptor
*interdesc
;
202 const struct libusb_endpoint_descriptor
*epdesc
;
203 struct libusb_config_descriptor
*config
;
205 *usb_read_ep
= *usb_write_ep
= 0;
207 libusb_get_config_descriptor(udev
, 0, &config
);
208 for (int i
= 0; i
< (int)config
->bNumInterfaces
; i
++) {
209 inter
= &config
->interface
[i
];
211 interdesc
= &inter
->altsetting
[0];
213 k
< (int)interdesc
->bNumEndpoints
; k
++) {
214 if ((bclass
> 0 && interdesc
->bInterfaceClass
!= bclass
) ||
215 (subclass
> 0 && interdesc
->bInterfaceSubClass
!= subclass
) ||
216 (protocol
> 0 && interdesc
->bInterfaceProtocol
!= protocol
))
219 epdesc
= &interdesc
->endpoint
[k
];
220 if (trans_type
> 0 && (epdesc
->bmAttributes
& 0x3) != trans_type
)
223 uint8_t epnum
= epdesc
->bEndpointAddress
;
224 bool is_input
= epnum
& 0x80;
225 LOG_DEBUG("usb ep %s %02x",
226 is_input
? "in" : "out", epnum
);
229 *usb_read_ep
= epnum
;
231 *usb_write_ep
= epnum
;
233 if (*usb_read_ep
&& *usb_write_ep
) {
234 LOG_DEBUG("Claiming interface %d", (int)interdesc
->bInterfaceNumber
);
235 libusb_claim_interface(devh
, (int)interdesc
->bInterfaceNumber
);
236 libusb_free_config_descriptor(config
);
241 libusb_free_config_descriptor(config
);
246 int jtag_libusb_get_pid(struct jtag_libusb_device
*dev
, uint16_t *pid
)
248 struct libusb_device_descriptor dev_desc
;
250 if (libusb_get_device_descriptor(dev
, &dev_desc
) == 0) {
251 *pid
= dev_desc
.idProduct
;
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)