OpenOCD
cmsis_dap_usb_bulk.c
Go to the documentation of this file.
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 <helper/system.h>
28 #include <libusb.h>
29 #include <helper/log.h>
30 #include <helper/replacements.h>
31 
32 #include "cmsis_dap.h"
33 
35  struct libusb_context *usb_ctx;
36  struct libusb_device_handle *dev_handle;
37  unsigned int ep_out;
38  unsigned int ep_in;
39  int interface;
40 };
41 
42 static int cmsis_dap_usb_interface = -1;
43 
44 static void cmsis_dap_usb_close(struct cmsis_dap *dap);
45 static int cmsis_dap_usb_alloc(struct cmsis_dap *dap, unsigned int pkt_sz);
46 
47 static int cmsis_dap_usb_open(struct cmsis_dap *dap, uint16_t vids[], uint16_t pids[], const char *serial)
48 {
49  int err;
50  struct libusb_context *ctx;
51  struct libusb_device **device_list;
52 
53  err = libusb_init(&ctx);
54  if (err) {
55  LOG_ERROR("libusb initialization failed: %s", libusb_strerror(err));
56  return ERROR_FAIL;
57  }
58 
59  int num_devices = libusb_get_device_list(ctx, &device_list);
60  if (num_devices < 0) {
61  LOG_ERROR("could not enumerate USB devices: %s", libusb_strerror(num_devices));
62  libusb_exit(ctx);
63  return ERROR_FAIL;
64  }
65 
66  for (int i = 0; i < num_devices; i++) {
67  struct libusb_device *dev = device_list[i];
68  struct libusb_device_descriptor dev_desc;
69 
70  err = libusb_get_device_descriptor(dev, &dev_desc);
71  if (err) {
72  LOG_ERROR("could not get device descriptor for device %d: %s", i, libusb_strerror(err));
73  continue;
74  }
75 
76  /* Match VID/PID */
77 
78  bool id_match = false;
79  bool id_filter = vids[0] || pids[0];
80  for (int id = 0; vids[id] || pids[id]; id++) {
81  id_match = !vids[id] || dev_desc.idVendor == vids[id];
82  id_match &= !pids[id] || dev_desc.idProduct == pids[id];
83 
84  if (id_match)
85  break;
86  }
87 
88  if (id_filter && !id_match)
89  continue;
90 
91  /* Don't continue if we asked for a serial number and the device doesn't have one */
92  if (dev_desc.iSerialNumber == 0 && serial && serial[0])
93  continue;
94 
95  struct libusb_device_handle *dev_handle = NULL;
96  err = libusb_open(dev, &dev_handle);
97  if (err) {
98  /* It's to be expected that most USB devices can't be opened
99  * so only report an error if it was explicitly selected
100  */
101  if (id_filter) {
102  LOG_ERROR("could not open device 0x%04x:0x%04x: %s",
103  dev_desc.idVendor, dev_desc.idProduct, libusb_strerror(err));
104  } else {
105  LOG_DEBUG("could not open device 0x%04x:0x%04x: %s",
106  dev_desc.idVendor, dev_desc.idProduct, libusb_strerror(err));
107  }
108  continue;
109  }
110 
111  /* Match serial number */
112 
113  bool serial_match = false;
114  char dev_serial[256] = {0};
115  if (dev_desc.iSerialNumber > 0) {
116  err = libusb_get_string_descriptor_ascii(
117  dev_handle, dev_desc.iSerialNumber,
118  (uint8_t *)dev_serial, sizeof(dev_serial));
119 
120  if (err < 0) {
121  const char *msg = "could not read serial number for device 0x%04x:0x%04x: %s";
122  if (serial)
123  LOG_WARNING(msg, dev_desc.idVendor, dev_desc.idProduct,
124  libusb_strerror(err));
125  else
126  LOG_DEBUG(msg, dev_desc.idVendor, dev_desc.idProduct,
127  libusb_strerror(err));
128  } else if (serial && strncmp(dev_serial, serial, sizeof(dev_serial)) == 0) {
129  serial_match = true;
130  }
131  }
132 
133  if (serial && !serial_match) {
134  libusb_close(dev_handle);
135  continue;
136  }
137 
138  /* Find the CMSIS-DAP string in product string */
139 
140  bool cmsis_dap_in_product_str = false;
141  char product_string[256] = {0};
142  if (dev_desc.iProduct > 0) {
143  err = libusb_get_string_descriptor_ascii(
144  dev_handle, dev_desc.iProduct,
145  (uint8_t *)product_string, sizeof(product_string));
146  if (err < 0) {
147  LOG_WARNING("could not read product string for device 0x%04x:0x%04x: %s",
148  dev_desc.idVendor, dev_desc.idProduct, libusb_strerror(err));
149  } else if (strstr(product_string, "CMSIS-DAP")) {
150  LOG_DEBUG("found product string of 0x%04x:0x%04x '%s'",
151  dev_desc.idVendor, dev_desc.idProduct, product_string);
152  cmsis_dap_in_product_str = true;
153  }
154  }
155 
156  bool device_identified_reliably = cmsis_dap_in_product_str
157  || serial_match || id_match;
158 
159  /* Find the CMSIS-DAP interface */
160 
161  for (int config = 0; config < dev_desc.bNumConfigurations; config++) {
162  struct libusb_config_descriptor *config_desc;
163  err = libusb_get_config_descriptor(dev, config, &config_desc);
164  if (err) {
165  LOG_ERROR("could not get configuration descriptor %d for device 0x%04x:0x%04x: %s",
166  config, dev_desc.idVendor, dev_desc.idProduct, libusb_strerror(err));
167  continue;
168  }
169 
170  LOG_DEBUG("enumerating interfaces of 0x%04x:0x%04x",
171  dev_desc.idVendor, dev_desc.idProduct);
172  int config_num = config_desc->bConfigurationValue;
173  const struct libusb_interface_descriptor *intf_desc_candidate = NULL;
174  const struct libusb_interface_descriptor *intf_desc_found = NULL;
175 
176  for (int interface = 0; interface < config_desc->bNumInterfaces; interface++) {
177  const struct libusb_interface_descriptor *intf_desc = &config_desc->interface[interface].altsetting[0];
178  int interface_num = intf_desc->bInterfaceNumber;
179 
180  /* Skip this interface if another one was requested explicitly */
181  if (cmsis_dap_usb_interface != -1 && cmsis_dap_usb_interface != interface_num)
182  continue;
183 
184  /* CMSIS-DAP v2 spec says:
185  *
186  * CMSIS-DAP with default V2 configuration uses WinUSB and is therefore faster.
187  * Optionally support for streaming SWO trace is provided via an additional USB endpoint.
188  *
189  * The WinUSB configuration requires custom class support with the interface setting
190  * Class Code: 0xFF (Vendor specific)
191  * Subclass: 0x00
192  * Protocol code: 0x00
193  *
194  * Depending on the configuration it uses the following USB endpoints which should be configured
195  * in the interface descriptor in this order:
196  * - Endpoint 1: Bulk Out – used for commands received from host PC.
197  * - Endpoint 2: Bulk In – used for responses send to host PC.
198  * - Endpoint 3: Bulk In (optional) – used for streaming SWO trace (if enabled with SWO_STREAM).
199  */
200 
201  /* Search for "CMSIS-DAP" in the interface string */
202  bool cmsis_dap_in_interface_str = false;
203  if (intf_desc->iInterface != 0) {
204 
205  char interface_str[256] = {0};
206 
207  err = libusb_get_string_descriptor_ascii(
208  dev_handle, intf_desc->iInterface,
209  (uint8_t *)interface_str, sizeof(interface_str));
210  if (err < 0) {
211  LOG_DEBUG("could not read interface string %d for device 0x%04x:0x%04x: %s",
212  intf_desc->iInterface,
213  dev_desc.idVendor, dev_desc.idProduct,
214  libusb_strerror(err));
215  } else if (strstr(interface_str, "CMSIS-DAP")) {
216  cmsis_dap_in_interface_str = true;
217  LOG_DEBUG("found interface %d string '%s'",
218  interface_num, interface_str);
219  }
220  }
221 
222  /* Bypass the following check if this interface was explicitly requested. */
223  if (cmsis_dap_usb_interface == -1) {
224  if (!cmsis_dap_in_product_str && !cmsis_dap_in_interface_str)
225  continue;
226  }
227 
228  /* check endpoints */
229  if (intf_desc->bNumEndpoints < 2) {
230  LOG_DEBUG("skipping interface %d, has only %d endpoints",
231  interface_num, intf_desc->bNumEndpoints);
232  continue;
233  }
234 
235  if ((intf_desc->endpoint[0].bmAttributes & 3) != LIBUSB_TRANSFER_TYPE_BULK ||
236  (intf_desc->endpoint[0].bEndpointAddress & 0x80) != LIBUSB_ENDPOINT_OUT) {
237  LOG_DEBUG("skipping interface %d, endpoint[0] is not bulk out",
238  interface_num);
239  continue;
240  }
241 
242  if ((intf_desc->endpoint[1].bmAttributes & 3) != LIBUSB_TRANSFER_TYPE_BULK ||
243  (intf_desc->endpoint[1].bEndpointAddress & 0x80) != LIBUSB_ENDPOINT_IN) {
244  LOG_DEBUG("skipping interface %d, endpoint[1] is not bulk in",
245  interface_num);
246  continue;
247  }
248 
249  /* We can rely on the interface is really CMSIS-DAP if
250  * - we've seen CMSIS-DAP in the interface string
251  * - config asked explicitly for an interface number
252  * - the device has only one interface
253  * The later two cases should be honored only if we know
254  * we are on the right device */
255  bool intf_identified_reliably = cmsis_dap_in_interface_str
256  || (device_identified_reliably &&
258  || config_desc->bNumInterfaces == 1));
259 
260  if (intf_desc->bInterfaceClass != LIBUSB_CLASS_VENDOR_SPEC ||
261  intf_desc->bInterfaceSubClass != 0 || intf_desc->bInterfaceProtocol != 0) {
262  /* If the interface is reliably identified
263  * then we need not insist on setting USB class, subclass and protocol
264  * exactly as the specification requires.
265  * Just filter out the well known classes, mainly CDC and MSC.
266  * At least KitProg3 uses class 0 contrary to the specification */
267  if (intf_identified_reliably &&
268  (intf_desc->bInterfaceClass == 0 || intf_desc->bInterfaceClass > 0x12)) {
269  LOG_WARNING("Using CMSIS-DAPv2 interface %d with wrong class %" PRId8
270  " subclass %" PRId8 " or protocol %" PRId8,
271  interface_num,
272  intf_desc->bInterfaceClass,
273  intf_desc->bInterfaceSubClass,
274  intf_desc->bInterfaceProtocol);
275  } else {
276  LOG_DEBUG("skipping interface %d, class %" PRId8
277  " subclass %" PRId8 " protocol %" PRId8,
278  interface_num,
279  intf_desc->bInterfaceClass,
280  intf_desc->bInterfaceSubClass,
281  intf_desc->bInterfaceProtocol);
282  continue;
283 
284  }
285  }
286 
287  if (intf_identified_reliably) {
288  /* That's the one! */
289  intf_desc_found = intf_desc;
290  break;
291  }
292 
293  if (!intf_desc_candidate && device_identified_reliably) {
294  /* This interface looks suitable for CMSIS-DAP. Store the pointer to it
295  * and keep searching for another one with CMSIS-DAP in interface string */
296  intf_desc_candidate = intf_desc;
297  }
298  }
299 
300  if (!intf_desc_found) {
301  /* We were not able to identify reliably which interface is CMSIS-DAP.
302  * Let's use the first suitable if we found one */
303  intf_desc_found = intf_desc_candidate;
304  }
305 
306  if (!intf_desc_found) {
307  libusb_free_config_descriptor(config_desc);
308  continue;
309  }
310 
311  /* We've chosen an interface, connect to it */
312  int interface_num = intf_desc_found->bInterfaceNumber;
313  int packet_size = intf_desc_found->endpoint[0].wMaxPacketSize;
314  int ep_out = intf_desc_found->endpoint[0].bEndpointAddress;
315  int ep_in = intf_desc_found->endpoint[1].bEndpointAddress;
316 
317  libusb_free_config_descriptor(config_desc);
318  libusb_free_device_list(device_list, true);
319 
320  LOG_INFO("Using CMSIS-DAPv2 interface with VID:PID=0x%04x:0x%04x, serial=%s",
321  dev_desc.idVendor, dev_desc.idProduct, dev_serial);
322 
323  int current_config;
324  err = libusb_get_configuration(dev_handle, &current_config);
325  if (err) {
326  LOG_ERROR("could not find current configuration: %s", libusb_strerror(err));
327  libusb_close(dev_handle);
328  libusb_exit(ctx);
329  return ERROR_FAIL;
330  }
331 
332  if (config_num != current_config) {
333  err = libusb_set_configuration(dev_handle, config_num);
334  if (err) {
335  LOG_ERROR("could not set configuration: %s", libusb_strerror(err));
336  libusb_close(dev_handle);
337  libusb_exit(ctx);
338  return ERROR_FAIL;
339  }
340  }
341 
342  err = libusb_claim_interface(dev_handle, interface_num);
343  if (err)
344  LOG_WARNING("could not claim interface: %s", libusb_strerror(err));
345 
346  dap->bdata = malloc(sizeof(struct cmsis_dap_backend_data));
347  if (!dap->bdata) {
348  LOG_ERROR("unable to allocate memory");
349  libusb_release_interface(dev_handle, interface_num);
350  libusb_close(dev_handle);
351  libusb_exit(ctx);
352  return ERROR_FAIL;
353  }
354 
355  dap->packet_size = packet_size;
356  dap->packet_buffer_size = packet_size;
357  dap->bdata->usb_ctx = ctx;
358  dap->bdata->dev_handle = dev_handle;
359  dap->bdata->ep_out = ep_out;
360  dap->bdata->ep_in = ep_in;
361  dap->bdata->interface = interface_num;
362 
363  dap->packet_buffer = malloc(dap->packet_buffer_size);
364  if (!dap->packet_buffer) {
365  LOG_ERROR("unable to allocate memory");
366  cmsis_dap_usb_close(dap);
367  return ERROR_FAIL;
368  }
369 
370  dap->command = dap->packet_buffer;
371  dap->response = dap->packet_buffer;
372 
373  return ERROR_OK;
374  }
375 
376  libusb_close(dev_handle);
377  }
378 
379  libusb_free_device_list(device_list, true);
380 
381  libusb_exit(ctx);
382  return ERROR_FAIL;
383 }
384 
385 static void cmsis_dap_usb_close(struct cmsis_dap *dap)
386 {
387  libusb_release_interface(dap->bdata->dev_handle, dap->bdata->interface);
388  libusb_close(dap->bdata->dev_handle);
389  libusb_exit(dap->bdata->usb_ctx);
390  free(dap->bdata);
391  dap->bdata = NULL;
392  free(dap->packet_buffer);
393  dap->packet_buffer = NULL;
394 }
395 
396 static int cmsis_dap_usb_read(struct cmsis_dap *dap, int timeout_ms)
397 {
398  int transferred = 0;
399  int err;
400 
401  err = libusb_bulk_transfer(dap->bdata->dev_handle, dap->bdata->ep_in,
402  dap->packet_buffer, dap->packet_size, &transferred, timeout_ms);
403  if (err) {
404  if (err == LIBUSB_ERROR_TIMEOUT) {
405  return ERROR_TIMEOUT_REACHED;
406  } else {
407  LOG_ERROR("error reading data: %s", libusb_strerror(err));
408  return ERROR_FAIL;
409  }
410  }
411 
412  memset(&dap->packet_buffer[transferred], 0, dap->packet_buffer_size - transferred);
413 
414  return transferred;
415 }
416 
417 static int cmsis_dap_usb_write(struct cmsis_dap *dap, int txlen, int timeout_ms)
418 {
419  int transferred = 0;
420  int err;
421 
422  /* skip the first byte that is only used by the HID backend */
423  err = libusb_bulk_transfer(dap->bdata->dev_handle, dap->bdata->ep_out,
424  dap->packet_buffer, txlen, &transferred, timeout_ms);
425  if (err) {
426  if (err == LIBUSB_ERROR_TIMEOUT) {
427  return ERROR_TIMEOUT_REACHED;
428  } else {
429  LOG_ERROR("error writing data: %s", libusb_strerror(err));
430  return ERROR_FAIL;
431  }
432  }
433 
434  return transferred;
435 }
436 
437 static int cmsis_dap_usb_alloc(struct cmsis_dap *dap, unsigned int pkt_sz)
438 {
439  uint8_t *buf = malloc(pkt_sz);
440  if (!buf) {
441  LOG_ERROR("unable to allocate CMSIS-DAP packet buffer");
442  return ERROR_FAIL;
443  }
444 
445  dap->packet_buffer = buf;
446  dap->packet_size = pkt_sz;
447  dap->packet_buffer_size = pkt_sz;
448 
449  dap->command = dap->packet_buffer;
450  dap->response = dap->packet_buffer;
451 
452  return ERROR_OK;
453 }
454 
455 COMMAND_HANDLER(cmsis_dap_handle_usb_interface_command)
456 {
457  if (CMD_ARGC == 1)
459  else
460  LOG_ERROR("expected exactly one argument to cmsis_dap_usb_interface <interface_number>");
461 
462  return ERROR_OK;
463 }
464 
466  {
467  .name = "interface",
468  .handler = &cmsis_dap_handle_usb_interface_command,
469  .mode = COMMAND_CONFIG,
470  .help = "set the USB interface number to use (for USB bulk backend only)",
471  .usage = "<interface_number>",
472  },
474 };
475 
477  .name = "usb_bulk",
478  .open = cmsis_dap_usb_open,
479  .close = cmsis_dap_usb_close,
480  .read = cmsis_dap_usb_read,
481  .write = cmsis_dap_usb_write,
482  .packet_buffer_alloc = cmsis_dap_usb_alloc,
483 };
char * serial
Definition: adapter.c:47
COMMAND_HANDLER(cmsis_dap_handle_usb_interface_command)
static int cmsis_dap_usb_write(struct cmsis_dap *dap, int txlen, int timeout_ms)
const struct cmsis_dap_backend cmsis_dap_usb_backend
static int cmsis_dap_usb_alloc(struct cmsis_dap *dap, unsigned int pkt_sz)
const struct command_registration cmsis_dap_usb_subcommand_handlers[]
static int cmsis_dap_usb_interface
static int cmsis_dap_usb_open(struct cmsis_dap *dap, uint16_t vids[], uint16_t pids[], const char *serial)
static int cmsis_dap_usb_read(struct cmsis_dap *dap, int timeout_ms)
static void cmsis_dap_usb_close(struct cmsis_dap *dap)
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:155
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:150
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
Definition: command.h:425
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:247
@ COMMAND_CONFIG
Definition: command.h:41
#define LOG_WARNING(expr ...)
Definition: log.h:120
#define ERROR_FAIL
Definition: log.h:161
#define LOG_ERROR(expr ...)
Definition: log.h:123
#define ERROR_TIMEOUT_REACHED
Definition: log.h:164
#define LOG_INFO(expr ...)
Definition: log.h:117
#define LOG_DEBUG(expr ...)
Definition: log.h:109
#define ERROR_OK
Definition: log.h:155
static unsigned int ep_out
Definition: openjtag.c:120
static unsigned int ep_in
Definition: openjtag.c:120
char id[RTT_CB_MAX_ID_LENGTH]
Control block identifier.
Definition: rtt/rtt.c:32
struct libusb_context * usb_ctx
struct libusb_device_handle * dev_handle
const char * name
Definition: cmsis_dap.h:28
struct cmsis_dap_backend_data * bdata
Definition: cmsis_dap.h:13
uint8_t * response
Definition: cmsis_dap.h:20
uint16_t packet_size
Definition: cmsis_dap.h:15
uint8_t * command
Definition: cmsis_dap.h:19
uint8_t * packet_buffer
Definition: cmsis_dap.h:17
uint16_t packet_buffer_size
Definition: cmsis_dap.h:18
const char * name
Definition: command.h:229
#define NULL
Definition: usb.h:16