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 "libusb0_common.h"
26 static int jtag_libusb_error(int err
)
32 return ERROR_TIMEOUT_REACHED
;
38 static bool jtag_libusb_match(struct jtag_libusb_device
*dev
,
39 const uint16_t vids
[], const uint16_t pids
[])
41 for (unsigned i
= 0; vids
[i
]; i
++) {
42 if (dev
->descriptor
.idVendor
== vids
[i
] &&
43 dev
->descriptor
.idProduct
== pids
[i
]) {
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
,
56 char desc_string
[256+1]; /* Max size of string descriptor */
61 retval
= usb_get_string_simple(device
, str_index
,
62 desc_string
, sizeof(desc_string
)-1);
64 LOG_ERROR("usb_get_string_simple() failed with %d", retval
);
68 /* Null terminate descriptor string in case it needs to be logged. */
69 desc_string
[sizeof(desc_string
)-1] = '\0';
71 matched
= strncmp(string
, desc_string
, sizeof(desc_string
)) == 0;
73 LOG_DEBUG("Device serial number '%s' doesn't match requested serial '%s'",
78 int jtag_libusb_open(const uint16_t vids
[], const uint16_t pids
[],
80 struct jtag_libusb_device_handle
**out
)
82 int retval
= ERROR_FAIL
;
83 bool serial_mismatch
= false;
84 struct jtag_libusb_device_handle
*libusb_handle
;
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
))
97 libusb_handle
= usb_open(dev
);
98 if (NULL
== libusb_handle
) {
99 LOG_ERROR("usb_open() failed with %s", usb_strerror());
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
);
110 *out
= libusb_handle
;
112 serial_mismatch
= false;
118 LOG_INFO("No device matches the serial string");
123 void jtag_libusb_close(jtag_libusb_device_handle
*dev
)
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
)
135 transferred
= usb_control_msg(dev
, requestType
, request
, wValue
, wIndex
,
136 bytes
, size
, timeout
);
144 int jtag_libusb_bulk_write(jtag_libusb_device_handle
*dev
, int ep
, char *bytes
,
145 int size
, int timeout
, int *transferred
)
151 ret
= usb_bulk_write(dev
, ep
, bytes
, size
, timeout
);
154 LOG_ERROR("usb_bulk_write error: %i", ret
);
155 return jtag_libusb_error(ret
);
161 int jtag_libusb_bulk_read(jtag_libusb_device_handle
*dev
, int ep
, char *bytes
,
162 int size
, int timeout
, int *transferred
)
168 ret
= usb_bulk_read(dev
, ep
, bytes
, size
, timeout
);
171 LOG_ERROR("usb_bulk_read error: %i", ret
);
172 return jtag_libusb_error(ret
);
178 int jtag_libusb_set_configuration(jtag_libusb_device_handle
*devh
,
181 struct jtag_libusb_device
*udev
= jtag_libusb_get_device(devh
);
183 return usb_set_configuration(devh
,
184 udev
->config
[configuration
].bConfigurationValue
);
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
)
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
;
196 *usb_read_ep
= *usb_write_ep
= 0;
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
))
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
);
209 *usb_read_ep
= epnum
;
211 *usb_write_ep
= epnum
;
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
);
223 int jtag_libusb_get_pid(struct jtag_libusb_device
*dev
, uint16_t *pid
)
228 *pid
= dev
->descriptor
.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)