jtag/drivers/cmsis_dap_bulk: use asynchronous libusb transfer
[openocd.git] / src / jtag / drivers / cmsis_dap_usb_hid.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2
3 /***************************************************************************
4 * Copyright (C) 2018 by Mickaƫl Thomas *
5 * mickael9@gmail.com *
6 * *
7 * Copyright (C) 2016 by Maksym Hilliaka *
8 * oter@frozen-team.com *
9 * *
10 * Copyright (C) 2016 by Phillip Pearson *
11 * pp@myelin.co.nz *
12 * *
13 * Copyright (C) 2014 by Paul Fertser *
14 * fercerpav@gmail.com *
15 * *
16 * Copyright (C) 2013 by mike brown *
17 * mike@theshedworks.org.uk *
18 * *
19 * Copyright (C) 2013 by Spencer Oliver *
20 * spen@spen-soft.co.uk *
21 ***************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <string.h>
28 #include <hidapi.h>
29 #include <helper/log.h>
30
31 #include "cmsis_dap.h"
32
33 struct cmsis_dap_backend_data {
34 hid_device *dev_handle;
35 };
36
37 static void cmsis_dap_hid_close(struct cmsis_dap *dap);
38 static int cmsis_dap_hid_alloc(struct cmsis_dap *dap, unsigned int pkt_sz);
39 static void cmsis_dap_hid_free(struct cmsis_dap *dap);
40
41 static int cmsis_dap_hid_open(struct cmsis_dap *dap, uint16_t vids[], uint16_t pids[], const char *serial)
42 {
43 hid_device *dev = NULL;
44 int i;
45 struct hid_device_info *devs, *cur_dev;
46 unsigned short target_vid, target_pid;
47
48 target_vid = 0;
49 target_pid = 0;
50
51 if (hid_init() != 0) {
52 LOG_ERROR("unable to open HIDAPI");
53 return ERROR_FAIL;
54 }
55
56 /*
57 * The CMSIS-DAP specification stipulates:
58 * "The Product String must contain "CMSIS-DAP" somewhere in the string. This is used by the
59 * debuggers to identify a CMSIS-DAP compliant Debug Unit that is connected to a host computer."
60 */
61 devs = hid_enumerate(0x0, 0x0);
62 cur_dev = devs;
63 while (cur_dev) {
64 bool found = false;
65
66 if (vids[0] == 0) {
67 if (!cur_dev->product_string) {
68 LOG_DEBUG("Cannot read product string of device 0x%x:0x%x",
69 cur_dev->vendor_id, cur_dev->product_id);
70 } else if (wcsstr(cur_dev->product_string, L"CMSIS-DAP")) {
71 /* if the user hasn't specified VID:PID *and*
72 * product string contains "CMSIS-DAP", pick it
73 */
74 found = true;
75 }
76 } else {
77 /* otherwise, exhaustively compare against all VID:PID in list */
78 for (i = 0; vids[i] || pids[i]; i++) {
79 if ((vids[i] == cur_dev->vendor_id) && (pids[i] == cur_dev->product_id))
80 found = true;
81 }
82 }
83
84 /* LPC-LINK2 has cmsis-dap on interface 0 and other HID functions on other interfaces */
85 if (cur_dev->vendor_id == 0x1fc9 && cur_dev->product_id == 0x0090 && cur_dev->interface_number != 0)
86 found = false;
87
88 if (found) {
89 /* check serial number matches if given */
90 if (!serial)
91 break;
92
93 if (cur_dev->serial_number) {
94 size_t len = (strlen(serial) + 1) * sizeof(wchar_t);
95 wchar_t *wserial = malloc(len);
96 mbstowcs(wserial, serial, len);
97
98 if (wcscmp(wserial, cur_dev->serial_number) == 0) {
99 free(wserial);
100 break;
101 } else {
102 free(wserial);
103 wserial = NULL;
104 }
105 }
106 }
107
108 cur_dev = cur_dev->next;
109 }
110
111 if (cur_dev) {
112 target_vid = cur_dev->vendor_id;
113 target_pid = cur_dev->product_id;
114 }
115
116 if (target_vid == 0 && target_pid == 0) {
117 hid_free_enumeration(devs);
118 return ERROR_FAIL;
119 }
120
121 dap->bdata = malloc(sizeof(struct cmsis_dap_backend_data));
122 if (!dap->bdata) {
123 LOG_ERROR("unable to allocate memory");
124 return ERROR_FAIL;
125 }
126
127 dev = hid_open_path(cur_dev->path);
128 hid_free_enumeration(devs);
129
130 if (!dev) {
131 LOG_ERROR("unable to open CMSIS-DAP device 0x%x:0x%x", target_vid, target_pid);
132 return ERROR_FAIL;
133 }
134
135 /* allocate default packet buffer, may be changed later.
136 * currently with HIDAPI we have no way of getting the output report length
137 * without this info we cannot communicate with the adapter.
138 * For the moment we have to hard code the packet size */
139
140 unsigned int packet_size = 64;
141
142 /* atmel cmsis-dap uses 512 byte reports */
143 /* except when it doesn't e.g. with mEDBG on SAMD10 Xplained
144 * board */
145 /* TODO: HID report descriptor should be parsed instead of
146 * hardcoding a match by VID */
147 if (target_vid == 0x03eb && target_pid != 0x2145 && target_pid != 0x2175)
148 packet_size = 512;
149
150 dap->bdata->dev_handle = dev;
151
152 int retval = cmsis_dap_hid_alloc(dap, packet_size);
153 if (retval != ERROR_OK) {
154 cmsis_dap_hid_close(dap);
155 return ERROR_FAIL;
156 }
157
158 dap->command = dap->packet_buffer + REPORT_ID_SIZE;
159 dap->response = dap->packet_buffer;
160 return ERROR_OK;
161 }
162
163 static void cmsis_dap_hid_close(struct cmsis_dap *dap)
164 {
165 hid_close(dap->bdata->dev_handle);
166 hid_exit();
167 free(dap->bdata);
168 dap->bdata = NULL;
169 cmsis_dap_hid_free(dap);
170 }
171
172 static int cmsis_dap_hid_read(struct cmsis_dap *dap, int transfer_timeout_ms,
173 struct timeval *wait_timeout)
174 {
175 int timeout_ms;
176 if (wait_timeout)
177 timeout_ms = wait_timeout->tv_usec / 1000 + wait_timeout->tv_sec * 1000;
178 else
179 timeout_ms = transfer_timeout_ms;
180
181 int retval = hid_read_timeout(dap->bdata->dev_handle,
182 dap->packet_buffer, dap->packet_buffer_size,
183 timeout_ms);
184 if (retval == 0) {
185 return ERROR_TIMEOUT_REACHED;
186 } else if (retval == -1) {
187 LOG_ERROR("error reading data: %ls", hid_error(dap->bdata->dev_handle));
188 return ERROR_FAIL;
189 }
190
191 return retval;
192 }
193
194 static int cmsis_dap_hid_write(struct cmsis_dap *dap, int txlen, int timeout_ms)
195 {
196 (void) timeout_ms;
197
198 dap->packet_buffer[0] = 0; /* HID report number */
199
200 /* Pad the rest of the TX buffer with 0's */
201 memset(dap->command + txlen, 0, dap->packet_size - txlen);
202
203 /* write data to device */
204 int retval = hid_write(dap->bdata->dev_handle, dap->packet_buffer, dap->packet_buffer_size);
205 if (retval == -1) {
206 LOG_ERROR("error writing data: %ls", hid_error(dap->bdata->dev_handle));
207 return ERROR_FAIL;
208 }
209
210 return retval;
211 }
212
213 static int cmsis_dap_hid_alloc(struct cmsis_dap *dap, unsigned int pkt_sz)
214 {
215 unsigned int packet_buffer_size = pkt_sz + REPORT_ID_SIZE;
216 uint8_t *buf = malloc(packet_buffer_size);
217 if (!buf) {
218 LOG_ERROR("unable to allocate CMSIS-DAP packet buffer");
219 return ERROR_FAIL;
220 }
221
222 dap->packet_buffer = buf;
223 dap->packet_size = pkt_sz;
224 dap->packet_usable_size = pkt_sz;
225 dap->packet_buffer_size = packet_buffer_size;
226
227 dap->command = dap->packet_buffer + REPORT_ID_SIZE;
228 dap->response = dap->packet_buffer;
229
230 return ERROR_OK;
231 }
232
233 static void cmsis_dap_hid_free(struct cmsis_dap *dap)
234 {
235 free(dap->packet_buffer);
236 dap->packet_buffer = NULL;
237 }
238
239 const struct cmsis_dap_backend cmsis_dap_hid_backend = {
240 .name = "hid",
241 .open = cmsis_dap_hid_open,
242 .close = cmsis_dap_hid_close,
243 .read = cmsis_dap_hid_read,
244 .write = cmsis_dap_hid_write,
245 .packet_buffer_alloc = cmsis_dap_hid_alloc,
246 .packet_buffer_free = cmsis_dap_hid_free,
247 };

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)