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 #include <jtag/jtag.h> /* ERROR_JTAG_DEVICE_ERROR only */
32 
33 #include "cmsis_dap.h"
34 #include "libusb_helper.h"
35 
36 enum {
37  CMSIS_DAP_TRANSFER_PENDING = 0, /* must be 0, used in libusb_handle_events_completed */
40 };
41 
43  struct libusb_transfer *transfer;
44  uint8_t *buffer;
45  int status; /* either CMSIS_DAP_TRANSFER_ enum or error code */
47 };
48 
50  struct libusb_context *usb_ctx;
51  struct libusb_device_handle *dev_handle;
52  unsigned int ep_out;
53  unsigned int ep_in;
54  int interface;
55 
58 };
59 
60 static int cmsis_dap_usb_interface = -1;
61 
62 static void cmsis_dap_usb_close(struct cmsis_dap *dap);
63 static int cmsis_dap_usb_alloc(struct cmsis_dap *dap, unsigned int pkt_sz);
64 static void cmsis_dap_usb_free(struct cmsis_dap *dap);
65 
66 static int cmsis_dap_usb_open(struct cmsis_dap *dap, uint16_t vids[], uint16_t pids[], const char *serial)
67 {
68  int err;
69  struct libusb_context *ctx;
70  struct libusb_device **device_list;
71 
72  err = libusb_init(&ctx);
73  if (err) {
74  LOG_ERROR("libusb initialization failed: %s", libusb_strerror(err));
75  return ERROR_FAIL;
76  }
77 
78  int num_devices = libusb_get_device_list(ctx, &device_list);
79  if (num_devices < 0) {
80  LOG_ERROR("could not enumerate USB devices: %s", libusb_strerror(num_devices));
81  libusb_exit(ctx);
82  return ERROR_FAIL;
83  }
84 
85  for (int i = 0; i < num_devices; i++) {
86  struct libusb_device *dev = device_list[i];
87  struct libusb_device_descriptor dev_desc;
88 
89  err = libusb_get_device_descriptor(dev, &dev_desc);
90  if (err) {
91  LOG_ERROR("could not get device descriptor for device %d: %s", i, libusb_strerror(err));
92  continue;
93  }
94 
95  /* Match VID/PID */
96 
97  bool id_match = false;
98  bool id_filter = vids[0] || pids[0];
99  for (int id = 0; vids[id] || pids[id]; id++) {
100  id_match = !vids[id] || dev_desc.idVendor == vids[id];
101  id_match &= !pids[id] || dev_desc.idProduct == pids[id];
102 
103  if (id_match)
104  break;
105  }
106 
107  if (id_filter && !id_match)
108  continue;
109 
110  /* Don't continue if we asked for a serial number and the device doesn't have one */
111  if (dev_desc.iSerialNumber == 0 && serial && serial[0])
112  continue;
113 
114  struct libusb_device_handle *dev_handle = NULL;
115  err = libusb_open(dev, &dev_handle);
116  if (err) {
117  /* It's to be expected that most USB devices can't be opened
118  * so only report an error if it was explicitly selected
119  */
120  if (id_filter) {
121  LOG_ERROR("could not open device 0x%04x:0x%04x: %s",
122  dev_desc.idVendor, dev_desc.idProduct, libusb_strerror(err));
123  } else {
124  LOG_DEBUG("could not open device 0x%04x:0x%04x: %s",
125  dev_desc.idVendor, dev_desc.idProduct, libusb_strerror(err));
126  }
127  continue;
128  }
129 
130  /* Match serial number */
131 
132  bool serial_match = false;
133  char dev_serial[256] = {0};
134  if (dev_desc.iSerialNumber > 0) {
135  err = libusb_get_string_descriptor_ascii(
136  dev_handle, dev_desc.iSerialNumber,
137  (uint8_t *)dev_serial, sizeof(dev_serial));
138 
139  if (err < 0) {
140  const char *msg = "could not read serial number for device 0x%04x:0x%04x: %s";
141  if (serial)
142  LOG_WARNING(msg, dev_desc.idVendor, dev_desc.idProduct,
143  libusb_strerror(err));
144  else
145  LOG_DEBUG(msg, dev_desc.idVendor, dev_desc.idProduct,
146  libusb_strerror(err));
147  } else if (serial && strncmp(dev_serial, serial, sizeof(dev_serial)) == 0) {
148  serial_match = true;
149  }
150  }
151 
152  if (serial && !serial_match) {
153  libusb_close(dev_handle);
154  continue;
155  }
156 
157  /* Find the CMSIS-DAP string in product string */
158 
159  bool cmsis_dap_in_product_str = false;
160  char product_string[256] = {0};
161  if (dev_desc.iProduct > 0) {
162  err = libusb_get_string_descriptor_ascii(
163  dev_handle, dev_desc.iProduct,
164  (uint8_t *)product_string, sizeof(product_string));
165  if (err < 0) {
166  LOG_WARNING("could not read product string for device 0x%04x:0x%04x: %s",
167  dev_desc.idVendor, dev_desc.idProduct, libusb_strerror(err));
168  } else if (strstr(product_string, "CMSIS-DAP")) {
169  LOG_DEBUG("found product string of 0x%04x:0x%04x '%s'",
170  dev_desc.idVendor, dev_desc.idProduct, product_string);
171  cmsis_dap_in_product_str = true;
172  }
173  }
174 
175  bool device_identified_reliably = cmsis_dap_in_product_str
176  || serial_match || id_match;
177 
178  /* Find the CMSIS-DAP interface */
179 
180  for (int config = 0; config < dev_desc.bNumConfigurations; config++) {
181  struct libusb_config_descriptor *config_desc;
182  err = libusb_get_config_descriptor(dev, config, &config_desc);
183  if (err) {
184  LOG_ERROR("could not get configuration descriptor %d for device 0x%04x:0x%04x: %s",
185  config, dev_desc.idVendor, dev_desc.idProduct, libusb_strerror(err));
186  continue;
187  }
188 
189  LOG_DEBUG("enumerating interfaces of 0x%04x:0x%04x",
190  dev_desc.idVendor, dev_desc.idProduct);
191  int config_num = config_desc->bConfigurationValue;
192  const struct libusb_interface_descriptor *intf_desc_candidate = NULL;
193  const struct libusb_interface_descriptor *intf_desc_found = NULL;
194 
195  for (int interface = 0; interface < config_desc->bNumInterfaces; interface++) {
196  const struct libusb_interface_descriptor *intf_desc = &config_desc->interface[interface].altsetting[0];
197  int interface_num = intf_desc->bInterfaceNumber;
198 
199  /* Skip this interface if another one was requested explicitly */
200  if (cmsis_dap_usb_interface != -1 && cmsis_dap_usb_interface != interface_num)
201  continue;
202 
203  /* CMSIS-DAP v2 spec says:
204  *
205  * CMSIS-DAP with default V2 configuration uses WinUSB and is therefore faster.
206  * Optionally support for streaming SWO trace is provided via an additional USB endpoint.
207  *
208  * The WinUSB configuration requires custom class support with the interface setting
209  * Class Code: 0xFF (Vendor specific)
210  * Subclass: 0x00
211  * Protocol code: 0x00
212  *
213  * Depending on the configuration it uses the following USB endpoints which should be configured
214  * in the interface descriptor in this order:
215  * - Endpoint 1: Bulk Out – used for commands received from host PC.
216  * - Endpoint 2: Bulk In – used for responses send to host PC.
217  * - Endpoint 3: Bulk In (optional) – used for streaming SWO trace (if enabled with SWO_STREAM).
218  */
219 
220  /* Search for "CMSIS-DAP" in the interface string */
221  bool cmsis_dap_in_interface_str = false;
222  if (intf_desc->iInterface != 0) {
223 
224  char interface_str[256] = {0};
225 
226  err = libusb_get_string_descriptor_ascii(
227  dev_handle, intf_desc->iInterface,
228  (uint8_t *)interface_str, sizeof(interface_str));
229  if (err < 0) {
230  LOG_DEBUG("could not read interface string %d for device 0x%04x:0x%04x: %s",
231  intf_desc->iInterface,
232  dev_desc.idVendor, dev_desc.idProduct,
233  libusb_strerror(err));
234  } else if (strstr(interface_str, "CMSIS-DAP")) {
235  cmsis_dap_in_interface_str = true;
236  LOG_DEBUG("found interface %d string '%s'",
237  interface_num, interface_str);
238  }
239  }
240 
241  /* Bypass the following check if this interface was explicitly requested. */
242  if (cmsis_dap_usb_interface == -1) {
243  if (!cmsis_dap_in_product_str && !cmsis_dap_in_interface_str)
244  continue;
245  }
246 
247  /* check endpoints */
248  if (intf_desc->bNumEndpoints < 2) {
249  LOG_DEBUG("skipping interface %d, has only %d endpoints",
250  interface_num, intf_desc->bNumEndpoints);
251  continue;
252  }
253 
254  if ((intf_desc->endpoint[0].bmAttributes & 3) != LIBUSB_TRANSFER_TYPE_BULK ||
255  (intf_desc->endpoint[0].bEndpointAddress & 0x80) != LIBUSB_ENDPOINT_OUT) {
256  LOG_DEBUG("skipping interface %d, endpoint[0] is not bulk out",
257  interface_num);
258  continue;
259  }
260 
261  if ((intf_desc->endpoint[1].bmAttributes & 3) != LIBUSB_TRANSFER_TYPE_BULK ||
262  (intf_desc->endpoint[1].bEndpointAddress & 0x80) != LIBUSB_ENDPOINT_IN) {
263  LOG_DEBUG("skipping interface %d, endpoint[1] is not bulk in",
264  interface_num);
265  continue;
266  }
267 
268  /* We can rely on the interface is really CMSIS-DAP if
269  * - we've seen CMSIS-DAP in the interface string
270  * - config asked explicitly for an interface number
271  * - the device has only one interface
272  * The later two cases should be honored only if we know
273  * we are on the right device */
274  bool intf_identified_reliably = cmsis_dap_in_interface_str
275  || (device_identified_reliably &&
277  || config_desc->bNumInterfaces == 1));
278 
279  if (intf_desc->bInterfaceClass != LIBUSB_CLASS_VENDOR_SPEC ||
280  intf_desc->bInterfaceSubClass != 0 || intf_desc->bInterfaceProtocol != 0) {
281  /* If the interface is reliably identified
282  * then we need not insist on setting USB class, subclass and protocol
283  * exactly as the specification requires.
284  * Just filter out the well known classes, mainly CDC and MSC.
285  * At least KitProg3 uses class 0 contrary to the specification */
286  if (intf_identified_reliably &&
287  (intf_desc->bInterfaceClass == 0 || intf_desc->bInterfaceClass > 0x12)) {
288  LOG_WARNING("Using CMSIS-DAPv2 interface %d with wrong class %" PRId8
289  " subclass %" PRId8 " or protocol %" PRId8,
290  interface_num,
291  intf_desc->bInterfaceClass,
292  intf_desc->bInterfaceSubClass,
293  intf_desc->bInterfaceProtocol);
294  } else {
295  LOG_DEBUG("skipping interface %d, class %" PRId8
296  " subclass %" PRId8 " protocol %" PRId8,
297  interface_num,
298  intf_desc->bInterfaceClass,
299  intf_desc->bInterfaceSubClass,
300  intf_desc->bInterfaceProtocol);
301  continue;
302 
303  }
304  }
305 
306  if (intf_identified_reliably) {
307  /* That's the one! */
308  intf_desc_found = intf_desc;
309  break;
310  }
311 
312  if (!intf_desc_candidate && device_identified_reliably) {
313  /* This interface looks suitable for CMSIS-DAP. Store the pointer to it
314  * and keep searching for another one with CMSIS-DAP in interface string */
315  intf_desc_candidate = intf_desc;
316  }
317  }
318 
319  if (!intf_desc_found) {
320  /* We were not able to identify reliably which interface is CMSIS-DAP.
321  * Let's use the first suitable if we found one */
322  intf_desc_found = intf_desc_candidate;
323  }
324 
325  if (!intf_desc_found) {
326  libusb_free_config_descriptor(config_desc);
327  continue;
328  }
329 
330  /* We've chosen an interface, connect to it */
331  int interface_num = intf_desc_found->bInterfaceNumber;
332  int packet_size = intf_desc_found->endpoint[0].wMaxPacketSize;
333  int ep_out = intf_desc_found->endpoint[0].bEndpointAddress;
334  int ep_in = intf_desc_found->endpoint[1].bEndpointAddress;
335 
336  libusb_free_config_descriptor(config_desc);
337  libusb_free_device_list(device_list, true);
338 
339  LOG_INFO("Using CMSIS-DAPv2 interface with VID:PID=0x%04x:0x%04x, serial=%s",
340  dev_desc.idVendor, dev_desc.idProduct, dev_serial);
341 
342  int current_config;
343  err = libusb_get_configuration(dev_handle, &current_config);
344  if (err) {
345  LOG_ERROR("could not find current configuration: %s", libusb_strerror(err));
346  libusb_close(dev_handle);
347  libusb_exit(ctx);
348  return ERROR_FAIL;
349  }
350 
351  if (config_num != current_config) {
352  err = libusb_set_configuration(dev_handle, config_num);
353  if (err) {
354  LOG_ERROR("could not set configuration: %s", libusb_strerror(err));
355  libusb_close(dev_handle);
356  libusb_exit(ctx);
357  return ERROR_FAIL;
358  }
359  }
360 
361  err = libusb_claim_interface(dev_handle, interface_num);
362  if (err)
363  LOG_WARNING("could not claim interface: %s", libusb_strerror(err));
364 
365  dap->bdata = calloc(1, sizeof(struct cmsis_dap_backend_data));
366  if (!dap->bdata) {
367  LOG_ERROR("unable to allocate memory");
368  libusb_release_interface(dev_handle, interface_num);
369  libusb_close(dev_handle);
370  libusb_exit(ctx);
371  return ERROR_FAIL;
372  }
373 
374  dap->bdata->usb_ctx = ctx;
375  dap->bdata->dev_handle = dev_handle;
376  dap->bdata->ep_out = ep_out;
377  dap->bdata->ep_in = ep_in;
378  dap->bdata->interface = interface_num;
379 
380  for (unsigned int idx = 0; idx < MAX_PENDING_REQUESTS; idx++) {
382  dap->bdata->command_transfers[idx].transfer = libusb_alloc_transfer(0);
383  if (!dap->bdata->command_transfers[idx].transfer) {
384  LOG_ERROR("unable to allocate USB transfer");
385  cmsis_dap_usb_close(dap);
386  return ERROR_FAIL;
387  }
388 
390  dap->bdata->response_transfers[idx].transfer = libusb_alloc_transfer(0);
391  if (!dap->bdata->response_transfers[idx].transfer) {
392  LOG_ERROR("unable to allocate USB transfer");
393  cmsis_dap_usb_close(dap);
394  return ERROR_FAIL;
395  }
396  }
397 
398  err = cmsis_dap_usb_alloc(dap, packet_size);
399  if (err != ERROR_OK)
400  cmsis_dap_usb_close(dap);
401 
402  return err;
403  }
404 
405  libusb_close(dev_handle);
406  }
407 
408  libusb_free_device_list(device_list, true);
409 
410  libusb_exit(ctx);
411  return ERROR_FAIL;
412 }
413 
414 static void cmsis_dap_usb_close(struct cmsis_dap *dap)
415 {
416  for (unsigned int i = 0; i < MAX_PENDING_REQUESTS; i++) {
418  LOG_DEBUG("busy command USB transfer at %u", dap->pending_fifo_put_idx);
419  struct timeval tv = {
420  .tv_sec = 1,
421  .tv_usec = 1000
422  };
423  /* Complete pending commands */
424  int res = libusb_handle_events_timeout_completed(dap->bdata->usb_ctx, &tv, NULL);
425  if (res == 0)
426  libusb_free_transfer(dap->bdata->command_transfers[i].transfer);
427  } else {
428  libusb_free_transfer(dap->bdata->command_transfers[i].transfer);
429  }
430  libusb_free_transfer(dap->bdata->response_transfers[i].transfer);
431  }
432  cmsis_dap_usb_free(dap);
433  libusb_release_interface(dap->bdata->dev_handle, dap->bdata->interface);
434  libusb_close(dap->bdata->dev_handle);
435  libusb_exit(dap->bdata->usb_ctx);
436  free(dap->bdata);
437  dap->bdata = NULL;
438 }
439 
440 static void LIBUSB_CALL cmsis_dap_usb_callback(struct libusb_transfer *transfer)
441 {
442  struct cmsis_dap_bulk_transfer *tr;
443 
444  tr = (struct cmsis_dap_bulk_transfer *)transfer->user_data;
445  if (transfer->status == LIBUSB_TRANSFER_COMPLETED) {
447  tr->transferred = transfer->actual_length;
448  } else if (transfer->status == LIBUSB_TRANSFER_TIMED_OUT) {
450  } else {
452  }
453 }
454 
455 static int cmsis_dap_usb_read(struct cmsis_dap *dap, int transfer_timeout_ms,
456  enum cmsis_dap_blocking blocking)
457 {
458  int transferred = 0;
459  int err;
460  struct cmsis_dap_bulk_transfer *tr;
462 
463  if (tr->status == CMSIS_DAP_TRANSFER_IDLE) {
464  libusb_fill_bulk_transfer(tr->transfer,
465  dap->bdata->dev_handle, dap->bdata->ep_in,
466  tr->buffer, dap->packet_size,
468  transfer_timeout_ms);
469  LOG_DEBUG_IO("submit read @ %u", dap->pending_fifo_get_idx);
471  err = libusb_submit_transfer(tr->transfer);
472  if (err) {
474  LOG_ERROR("error submitting USB read: %s", libusb_strerror(err));
475  return ERROR_FAIL;
476  }
477  }
478 
479  struct timeval tv;
480  if (blocking == CMSIS_DAP_NON_BLOCKING) {
481  tv.tv_sec = 0;
482  tv.tv_usec = 0;
483  } else {
484  tv.tv_sec = transfer_timeout_ms / 1000;
485  tv.tv_usec = transfer_timeout_ms % 1000 * 1000;
486  }
487 
488  while (tr->status == CMSIS_DAP_TRANSFER_PENDING) {
489  err = libusb_handle_events_timeout_completed(dap->bdata->usb_ctx, &tv,
490  &tr->status);
491  if (err) {
492  LOG_ERROR("error handling USB events: %s", libusb_strerror(err));
493  return ERROR_FAIL;
494  }
495  if (tv.tv_sec == 0 && tv.tv_usec == 0)
496  break;
497  }
498 
499  if (tr->status < 0 || tr->status == CMSIS_DAP_TRANSFER_COMPLETED) {
500  /* Check related command request for an error */
501  struct cmsis_dap_bulk_transfer *tr_cmd;
502  tr_cmd = &dap->bdata->command_transfers[dap->pending_fifo_get_idx];
503  if (tr_cmd->status < 0) {
504  err = tr_cmd->status;
506  if (err != ERROR_TIMEOUT_REACHED)
507  LOG_ERROR("error writing USB data");
508  else
509  LOG_DEBUG("command write USB timeout @ %u", dap->pending_fifo_get_idx);
510 
511  return err;
512  }
513  if (tr_cmd->status == CMSIS_DAP_TRANSFER_COMPLETED)
515  }
516 
517  if (tr->status < 0) {
518  err = tr->status;
520  if (err != ERROR_TIMEOUT_REACHED)
521  LOG_ERROR("error reading USB data");
522  else
523  LOG_DEBUG("USB timeout @ %u", dap->pending_fifo_get_idx);
524 
525  return err;
526  }
527 
529  transferred = tr->transferred;
530  LOG_DEBUG_IO("completed read @ %u, transferred %i",
532  memcpy(dap->packet_buffer, tr->buffer, transferred);
533  memset(&dap->packet_buffer[transferred], 0, dap->packet_buffer_size - transferred);
535  }
536 
537  return transferred;
538 }
539 
540 static int cmsis_dap_usb_write(struct cmsis_dap *dap, int txlen, int timeout_ms)
541 {
542  int err;
543  struct cmsis_dap_bulk_transfer *tr;
544  tr = &dap->bdata->command_transfers[dap->pending_fifo_put_idx];
545 
546  if (tr->status == CMSIS_DAP_TRANSFER_PENDING) {
547  LOG_DEBUG_IO("busy command USB transfer at %u", dap->pending_fifo_put_idx);
548  struct timeval tv = {
549  .tv_sec = timeout_ms / 1000,
550  .tv_usec = timeout_ms % 1000 * 1000
551  };
552  libusb_handle_events_timeout_completed(dap->bdata->usb_ctx, &tv, &tr->status);
553  }
554  if (tr->status < 0) {
555  if (tr->status != ERROR_TIMEOUT_REACHED)
556  LOG_ERROR("error writing USB data, late detect");
557  else
558  LOG_DEBUG("USB write timeout @ %u, late detect", dap->pending_fifo_get_idx);
560  }
562  LOG_DEBUG_IO("USB write: late transfer competed");
564  }
565  if (tr->status != CMSIS_DAP_TRANSFER_IDLE) {
566  libusb_cancel_transfer(tr->transfer);
567  /* TODO: switch to less verbose errors and wait for USB working again */
569  }
570 
571  memcpy(tr->buffer, dap->packet_buffer, txlen);
572 
573  libusb_fill_bulk_transfer(tr->transfer,
574  dap->bdata->dev_handle, dap->bdata->ep_out,
575  tr->buffer, txlen,
577  timeout_ms);
578 
579  LOG_DEBUG_IO("submit write @ %u", dap->pending_fifo_put_idx);
581  err = libusb_submit_transfer(tr->transfer);
582  if (err) {
583  if (err == LIBUSB_ERROR_BUSY)
584  libusb_cancel_transfer(tr->transfer);
585  else
587 
588  LOG_ERROR("error submitting USB write: %s", libusb_strerror(err));
589  return ERROR_FAIL;
590  }
591 
592  return ERROR_OK;
593 }
594 
595 static int cmsis_dap_usb_alloc(struct cmsis_dap *dap, unsigned int pkt_sz)
596 {
597  dap->packet_buffer = malloc(pkt_sz);
598  if (!dap->packet_buffer) {
599  LOG_ERROR("unable to allocate CMSIS-DAP packet buffer");
600  return ERROR_FAIL;
601  }
602 
603  dap->packet_size = pkt_sz;
604  dap->packet_buffer_size = pkt_sz;
605  /* Prevent sending zero size USB packets */
606  dap->packet_usable_size = pkt_sz - 1;
607 
608  dap->command = dap->packet_buffer;
609  dap->response = dap->packet_buffer;
610 
611  struct cmsis_dap_backend_data *bdata = dap->bdata;
612  for (unsigned int i = 0; i < MAX_PENDING_REQUESTS; i++) {
613  bdata->command_transfers[i].buffer =
614  oocd_libusb_dev_mem_alloc(bdata->dev_handle, pkt_sz);
615 
616  bdata->response_transfers[i].buffer =
617  oocd_libusb_dev_mem_alloc(bdata->dev_handle, pkt_sz);
618 
619  if (!bdata->command_transfers[i].buffer
620  || !bdata->response_transfers[i].buffer) {
621  LOG_ERROR("unable to allocate CMSIS-DAP pending packet buffer");
622  return ERROR_FAIL;
623  }
624  }
625  return ERROR_OK;
626 }
627 
628 static void cmsis_dap_usb_free(struct cmsis_dap *dap)
629 {
630  struct cmsis_dap_backend_data *bdata = dap->bdata;
631 
632  for (unsigned int i = 0; i < MAX_PENDING_REQUESTS; i++) {
634  bdata->command_transfers[i].buffer, dap->packet_size);
636  bdata->response_transfers[i].buffer, dap->packet_size);
637  bdata->command_transfers[i].buffer = NULL;
638  bdata->response_transfers[i].buffer = NULL;
639  }
640 
641  free(dap->packet_buffer);
642  dap->packet_buffer = NULL;
643  dap->command = NULL;
644  dap->response = NULL;
645 }
646 
647 static void cmsis_dap_usb_cancel_all(struct cmsis_dap *dap)
648 {
649  for (unsigned int i = 0; i < MAX_PENDING_REQUESTS; i++) {
651  libusb_cancel_transfer(dap->bdata->command_transfers[i].transfer);
653  libusb_cancel_transfer(dap->bdata->response_transfers[i].transfer);
654 
657  }
658 }
659 
660 COMMAND_HANDLER(cmsis_dap_handle_usb_interface_command)
661 {
662  if (CMD_ARGC == 1)
664  else
665  LOG_ERROR("expected exactly one argument to cmsis_dap_usb_interface <interface_number>");
666 
667  return ERROR_OK;
668 }
669 
671  {
672  .name = "interface",
673  .handler = &cmsis_dap_handle_usb_interface_command,
674  .mode = COMMAND_CONFIG,
675  .help = "set the USB interface number to use (for USB bulk backend only)",
676  .usage = "<interface_number>",
677  },
679 };
680 
682  .name = "usb_bulk",
683  .open = cmsis_dap_usb_open,
684  .close = cmsis_dap_usb_close,
685  .read = cmsis_dap_usb_read,
686  .write = cmsis_dap_usb_write,
687  .packet_buffer_alloc = cmsis_dap_usb_alloc,
688  .packet_buffer_free = cmsis_dap_usb_free,
689  .cancel_all = cmsis_dap_usb_cancel_all,
690 };
char * serial
Definition: adapter.c:43
#define MAX_PENDING_REQUESTS
Definition: cmsis_dap.h:19
cmsis_dap_blocking
Definition: cmsis_dap.h:62
@ CMSIS_DAP_NON_BLOCKING
Definition: cmsis_dap.h:63
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 void LIBUSB_CALL cmsis_dap_usb_callback(struct libusb_transfer *transfer)
@ CMSIS_DAP_TRANSFER_COMPLETED
@ CMSIS_DAP_TRANSFER_PENDING
@ CMSIS_DAP_TRANSFER_IDLE
static int cmsis_dap_usb_alloc(struct cmsis_dap *dap, unsigned int pkt_sz)
static int cmsis_dap_usb_read(struct cmsis_dap *dap, int transfer_timeout_ms, enum cmsis_dap_blocking blocking)
static void cmsis_dap_usb_cancel_all(struct cmsis_dap *dap)
static void cmsis_dap_usb_free(struct cmsis_dap *dap)
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 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:156
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:151
#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:440
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:251
@ COMMAND_CONFIG
Definition: command.h:41
The JTAG interface can be implemented with a software or hardware fifo.
#define ERROR_JTAG_DEVICE_ERROR
Definition: jtag.h:558
uint8_t * oocd_libusb_dev_mem_alloc(libusb_device_handle *devh, size_t length)
Attempts to allocate a block of persistent DMA memory suitable for transfers against the USB device.
int oocd_libusb_dev_mem_free(libusb_device_handle *devh, uint8_t *buffer, size_t length)
Free device memory allocated with oocd_libusb_dev_mem_alloc().
#define LOG_DEBUG_IO(expr ...)
Definition: log.h:102
#define LOG_WARNING(expr ...)
Definition: log.h:130
#define ERROR_FAIL
Definition: log.h:174
#define LOG_ERROR(expr ...)
Definition: log.h:133
#define ERROR_TIMEOUT_REACHED
Definition: log.h:177
#define LOG_INFO(expr ...)
Definition: log.h:127
#define LOG_DEBUG(expr ...)
Definition: log.h:110
#define ERROR_OK
Definition: log.h:168
#define LIBUSB_CALL
Definition: mpsse.c:21
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
struct cmsis_dap_bulk_transfer response_transfers[MAX_PENDING_REQUESTS]
struct cmsis_dap_bulk_transfer command_transfers[MAX_PENDING_REQUESTS]
const char * name
Definition: cmsis_dap.h:68
struct libusb_transfer * transfer
unsigned int packet_buffer_size
Definition: cmsis_dap.h:32
unsigned int packet_usable_size
Definition: cmsis_dap.h:31
unsigned int pending_fifo_put_idx
Definition: cmsis_dap.h:51
unsigned int packet_size
Definition: cmsis_dap.h:30
struct cmsis_dap_backend_data * bdata
Definition: cmsis_dap.h:28
uint8_t * response
Definition: cmsis_dap.h:35
uint8_t * command
Definition: cmsis_dap.h:34
uint8_t * packet_buffer
Definition: cmsis_dap.h:33
unsigned int pending_fifo_get_idx
Definition: cmsis_dap.h:51
const char * name
Definition: command.h:234
long tv_sec
Definition: replacements.h:46
long tv_usec
Definition: replacements.h:47
#define NULL
Definition: usb.h:16