b44b7861b7b70f20747430d453a5eaded02d8764
[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, 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 "libusb0_common.h"
27
28 static bool jtag_libusb_match(struct jtag_libusb_device *dev,
29 const uint16_t vids[], const uint16_t pids[])
30 {
31 for (unsigned i = 0; vids[i]; i++) {
32 if (dev->descriptor.idVendor == vids[i] &&
33 dev->descriptor.idProduct == pids[i]) {
34 return true;
35 }
36 }
37 return false;
38 }
39
40 /* Returns true if the string descriptor indexed by str_index in device matches string */
41 static bool string_descriptor_equal(usb_dev_handle *device, uint8_t str_index,
42 const char *string)
43 {
44 int retval;
45 bool matched;
46 char desc_string[256+1]; /* Max size of string descriptor */
47
48 if (str_index == 0)
49 return false;
50
51 retval = usb_get_string_simple(device, str_index,
52 desc_string, sizeof(desc_string)-1);
53 if (retval < 0) {
54 LOG_ERROR("usb_get_string_simple() failed with %d", retval);
55 return false;
56 }
57
58 /* Null terminate descriptor string in case it needs to be logged. */
59 desc_string[sizeof(desc_string)-1] = '\0';
60
61 matched = strncmp(string, desc_string, sizeof(desc_string)) == 0;
62 if (!matched)
63 LOG_DEBUG("Device serial number '%s' doesn't match requested serial '%s'",
64 desc_string, string);
65 return matched;
66 }
67
68 int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[],
69 const char *serial,
70 struct jtag_libusb_device_handle **out)
71 {
72 int retval = -ENODEV;
73 struct jtag_libusb_device_handle *libusb_handle;
74 usb_init();
75
76 usb_find_busses();
77 usb_find_devices();
78
79 struct usb_bus *busses = usb_get_busses();
80 for (struct usb_bus *bus = busses; bus; bus = bus->next) {
81 for (struct usb_device *dev = bus->devices;
82 dev; dev = dev->next) {
83 if (!jtag_libusb_match(dev, vids, pids))
84 continue;
85
86 libusb_handle = usb_open(dev);
87 if (NULL == libusb_handle) {
88 retval = -errno;
89 continue;
90 }
91
92 /* Device must be open to use libusb_get_string_descriptor_ascii. */
93 if (serial != NULL &&
94 !string_descriptor_equal(libusb_handle, dev->descriptor.iSerialNumber, serial)) {
95 usb_close(libusb_handle);
96 continue;
97 }
98 *out = libusb_handle;
99 retval = 0;
100 break;
101 }
102 }
103 return retval;
104 }
105
106 void jtag_libusb_close(jtag_libusb_device_handle *dev)
107 {
108 /* Close device */
109 usb_close(dev);
110 }
111
112 int jtag_libusb_control_transfer(jtag_libusb_device_handle *dev, uint8_t requestType,
113 uint8_t request, uint16_t wValue, uint16_t wIndex, char *bytes,
114 uint16_t size, unsigned int timeout)
115 {
116 int transferred = 0;
117
118 transferred = usb_control_msg(dev, requestType, request, wValue, wIndex,
119 bytes, size, timeout);
120
121 if (transferred < 0)
122 transferred = 0;
123
124 return transferred;
125 }
126
127 int jtag_libusb_bulk_write(jtag_libusb_device_handle *dev, int ep, char *bytes,
128 int size, int timeout)
129 {
130 return usb_bulk_write(dev, ep, bytes, size, timeout);
131 }
132
133 int jtag_libusb_bulk_read(jtag_libusb_device_handle *dev, int ep, char *bytes,
134 int size, int timeout)
135 {
136 return usb_bulk_read(dev, ep, bytes, size, timeout);
137 }
138
139 int jtag_libusb_set_configuration(jtag_libusb_device_handle *devh,
140 int configuration)
141 {
142 struct jtag_libusb_device *udev = jtag_libusb_get_device(devh);
143
144 return usb_set_configuration(devh,
145 udev->config[configuration].bConfigurationValue);
146 }
147
148 int jtag_libusb_get_endpoints(struct jtag_libusb_device *udev,
149 unsigned int *usb_read_ep,
150 unsigned int *usb_write_ep)
151 {
152 struct usb_interface *iface = udev->config->interface;
153 struct usb_interface_descriptor *desc = iface->altsetting;
154
155 for (int i = 0; i < desc->bNumEndpoints; i++) {
156 uint8_t epnum = desc->endpoint[i].bEndpointAddress;
157 bool is_input = epnum & 0x80;
158 LOG_DEBUG("usb ep %s %02x", is_input ? "in" : "out", epnum);
159 if (is_input)
160 *usb_read_ep = epnum;
161 else
162 *usb_write_ep = epnum;
163 }
164
165 return 0;
166 }
167
168 int jtag_libusb_get_pid(struct jtag_libusb_device *dev, uint16_t *pid)
169 {
170 if (!dev)
171 return ERROR_FAIL;
172
173 *pid = dev->descriptor.idProduct;
174 return ERROR_OK;
175 }

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)