Add CMSIS-DAP v2 support
[fw/openocd] / 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 #define PACKET_SIZE       (64 + 1)      /* 64 bytes plus report id */
44
45 struct cmsis_dap_backend_data {
46         hid_device *dev_handle;
47 };
48
49 static int cmsis_dap_hid_open(struct cmsis_dap *dap, uint16_t vids[], uint16_t pids[], char *serial)
50 {
51         hid_device *dev = NULL;
52         int i;
53         struct hid_device_info *devs, *cur_dev;
54         unsigned short target_vid, target_pid;
55
56         target_vid = 0;
57         target_pid = 0;
58
59         if (hid_init() != 0) {
60                 LOG_ERROR("unable to open HIDAPI");
61                 return ERROR_FAIL;
62         }
63
64         /*
65          * The CMSIS-DAP specification stipulates:
66          * "The Product String must contain "CMSIS-DAP" somewhere in the string. This is used by the
67          * debuggers to identify a CMSIS-DAP compliant Debug Unit that is connected to a host computer."
68          */
69         devs = hid_enumerate(0x0, 0x0);
70         cur_dev = devs;
71         while (NULL != cur_dev) {
72                 bool found = false;
73
74                 if (0 == vids[0]) {
75                         if (NULL == cur_dev->product_string) {
76                                 LOG_DEBUG("Cannot read product string of device 0x%x:0x%x",
77                                           cur_dev->vendor_id, cur_dev->product_id);
78                         } else if (wcsstr(cur_dev->product_string, L"CMSIS-DAP")) {
79                                 /* if the user hasn't specified VID:PID *and*
80                                  * product string contains "CMSIS-DAP", pick it
81                                  */
82                                 found = true;
83                         }
84                 } else {
85                         /* otherwise, exhaustively compare against all VID:PID in list */
86                         for (i = 0; vids[i] || pids[i]; i++) {
87                                 if ((vids[i] == cur_dev->vendor_id) && (pids[i] == cur_dev->product_id))
88                                         found = true;
89                         }
90                 }
91
92                 /* LPC-LINK2 has cmsis-dap on interface 0 and other HID functions on other interfaces */
93                 if (cur_dev->vendor_id == 0x1fc9 && cur_dev->product_id == 0x0090 && cur_dev->interface_number != 0)
94                         found = false;
95
96                 if (found) {
97                         /* check serial number matches if given */
98                         if (serial == NULL)
99                                 break;
100
101                         if (cur_dev->serial_number != NULL) {
102                                 size_t len = (strlen(serial) + 1) * sizeof(wchar_t);
103                                 wchar_t *wserial = malloc(len);
104                                 mbstowcs(wserial, serial, len);
105
106                                 if (wcscmp(wserial, cur_dev->serial_number) == 0) {
107                                         free(wserial);
108                                         break;
109                                 } else {
110                                         free(wserial);
111                                         wserial = NULL;
112                                 }
113                         }
114                 }
115
116                 cur_dev = cur_dev->next;
117         }
118
119         if (NULL != cur_dev) {
120                 target_vid = cur_dev->vendor_id;
121                 target_pid = cur_dev->product_id;
122         }
123
124         if (target_vid == 0 && target_pid == 0) {
125                 hid_free_enumeration(devs);
126                 return ERROR_FAIL;
127         }
128
129         dap->bdata = malloc(sizeof(struct cmsis_dap_backend_data));
130         if (dap->bdata == NULL) {
131                 LOG_ERROR("unable to allocate memory");
132                 return ERROR_FAIL;
133         }
134
135         dev = hid_open_path(cur_dev->path);
136         hid_free_enumeration(devs);
137
138         if (dev == NULL) {
139                 LOG_ERROR("unable to open CMSIS-DAP device 0x%x:0x%x", target_vid, target_pid);
140                 return ERROR_FAIL;
141         }
142
143         /* allocate default packet buffer, may be changed later.
144          * currently with HIDAPI we have no way of getting the output report length
145          * without this info we cannot communicate with the adapter.
146          * For the moment we have to hard code the packet size */
147
148         dap->packet_size = PACKET_SIZE;
149
150         /* atmel cmsis-dap uses 512 byte reports */
151         /* except when it doesn't e.g. with mEDBG on SAMD10 Xplained
152          * board */
153         /* TODO: HID report descriptor should be parsed instead of
154          * hardcoding a match by VID */
155         if (target_vid == 0x03eb && target_pid != 0x2145 && target_pid != 0x2175)
156                 dap->packet_size = 512 + 1;
157
158         dap->bdata->dev_handle = dev;
159
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 }
170
171 static int cmsis_dap_hid_read(struct cmsis_dap *dap, int timeout_ms)
172 {
173         int retval = hid_read_timeout(dap->bdata->dev_handle, dap->packet_buffer, dap->packet_size, timeout_ms);
174
175         if (retval == 0) {
176                 return ERROR_TIMEOUT_REACHED;
177         } else if (retval == -1) {
178                 LOG_ERROR("error reading data: %ls", hid_error(dap->bdata->dev_handle));
179                 return ERROR_FAIL;
180         }
181
182         return retval;
183 }
184
185 static int cmsis_dap_hid_write(struct cmsis_dap *dap, int txlen, int timeout_ms)
186 {
187         (void) timeout_ms;
188
189         /* Pad the rest of the TX buffer with 0's */
190         memset(dap->packet_buffer + txlen, 0, dap->packet_size - txlen);
191
192         /* write data to device */
193         int retval = hid_write(dap->bdata->dev_handle, dap->packet_buffer, dap->packet_size);
194         if (retval == -1) {
195                 LOG_ERROR("error writing data: %ls", hid_error(dap->bdata->dev_handle));
196                 return ERROR_FAIL;
197         }
198
199         return retval;
200 }
201
202 const struct cmsis_dap_backend cmsis_dap_hid_backend = {
203         .name = "hid",
204         .open = cmsis_dap_hid_open,
205         .close = cmsis_dap_hid_close,
206         .read = cmsis_dap_hid_read,
207         .write = cmsis_dap_hid_write,
208 };