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

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)