jlink: add support for Jlink-OB (0x0105) devices
[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 jtag_libusb_device *dev,
32 const uint16_t vids[], const uint16_t pids[])
33 {
34 struct libusb_device_descriptor dev_desc;
35
36 for (unsigned i = 0; vids[i]; i++) {
37 if (libusb_get_device_descriptor(dev, &dev_desc) == 0) {
38 if (dev_desc.idVendor == vids[i] &&
39 dev_desc.idProduct == pids[i])
40 return true;
41 }
42 }
43 return false;
44 }
45
46 int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[],
47 struct jtag_libusb_device_handle **out)
48 {
49 int cnt, idx, errCode;
50
51 if (libusb_init(&jtag_libusb_context) < 0)
52 return -ENODEV;
53
54 cnt = libusb_get_device_list(jtag_libusb_context, &devs);
55
56 for (idx = 0; idx < cnt; idx++) {
57 if (!jtag_libusb_match(devs[idx], vids, pids))
58 continue;
59
60 errCode = libusb_open(devs[idx], out);
61
62 /** Free the device list **/
63 libusb_free_device_list(devs, 1);
64
65 if (errCode) {
66 LOG_ERROR("libusb_open() failed with %s",
67 libusb_error_name(errCode));
68 return errCode;
69 }
70 return 0;
71 }
72 return -ENODEV;
73 }
74
75 void jtag_libusb_close(jtag_libusb_device_handle *dev)
76 {
77 /* Close device */
78 libusb_close(dev);
79
80 libusb_exit(jtag_libusb_context);
81 }
82
83 int jtag_libusb_control_transfer(jtag_libusb_device_handle *dev, uint8_t requestType,
84 uint8_t request, uint16_t wValue, uint16_t wIndex, char *bytes,
85 uint16_t size, unsigned int timeout)
86 {
87 int transferred = 0;
88
89 transferred = libusb_control_transfer(dev, requestType, request, wValue, wIndex,
90 (unsigned char *)bytes, size, timeout);
91
92 if (transferred < 0)
93 transferred = 0;
94
95 return transferred;
96 }
97
98 int jtag_libusb_bulk_write(jtag_libusb_device_handle *dev, int ep, char *bytes,
99 int size, int timeout)
100 {
101 int transferred = 0;
102
103 libusb_bulk_transfer(dev, ep, (unsigned char *)bytes, size,
104 &transferred, timeout);
105 return transferred;
106 }
107
108 int jtag_libusb_bulk_read(jtag_libusb_device_handle *dev, int ep, char *bytes,
109 int size, int timeout)
110 {
111 int transferred = 0;
112
113 libusb_bulk_transfer(dev, ep, (unsigned char *)bytes, size,
114 &transferred, timeout);
115 return transferred;
116 }
117
118 int jtag_libusb_set_configuration(jtag_libusb_device_handle *devh,
119 int configuration)
120 {
121 struct jtag_libusb_device *udev = jtag_libusb_get_device(devh);
122 int retCode = -99;
123
124 struct libusb_config_descriptor *config = NULL;
125
126 libusb_get_config_descriptor(udev, configuration, &config);
127 retCode = libusb_set_configuration(devh, config->bConfigurationValue);
128
129 libusb_free_config_descriptor(config);
130
131 return retCode;
132 }
133
134 int jtag_libusb_get_endpoints(struct jtag_libusb_device *udev,
135 unsigned int *usb_read_ep,
136 unsigned int *usb_write_ep)
137 {
138 const struct libusb_interface *inter;
139 const struct libusb_interface_descriptor *interdesc;
140 const struct libusb_endpoint_descriptor *epdesc;
141 struct libusb_config_descriptor *config;
142
143 libusb_get_config_descriptor(udev, 0, &config);
144 for (int i = 0; i < (int)config->bNumInterfaces; i++) {
145 inter = &config->interface[i];
146
147 for (int j = 0; j < inter->num_altsetting; j++) {
148 interdesc = &inter->altsetting[j];
149 for (int k = 0;
150 k < (int)interdesc->bNumEndpoints; k++) {
151 epdesc = &interdesc->endpoint[k];
152
153 uint8_t epnum = epdesc->bEndpointAddress;
154 bool is_input = epnum & 0x80;
155 LOG_DEBUG("usb ep %s %02x",
156 is_input ? "in" : "out", epnum);
157
158 if (is_input)
159 *usb_read_ep = epnum;
160 else
161 *usb_write_ep = epnum;
162 }
163 }
164 }
165 libusb_free_config_descriptor(config);
166
167 return 0;
168 }
169
170 int jtag_libusb_get_pid(struct jtag_libusb_device *dev, uint16_t *pid)
171 {
172 struct libusb_device_descriptor dev_desc;
173
174 if (libusb_get_device_descriptor(dev, &dev_desc) == 0) {
175 *pid = dev_desc.idProduct;
176
177 return 0;
178 }
179
180 return -ENODEV;
181 }

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)