1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
36
41
42
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
77
78
79
84
85
86
87
88
89
90
94
95
96
97
98
99
106
107
108
109
110
111
112
113
114
115
116
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
150
151
152
156
157
158
159
160
161
174
175
176
177
178
179
180
181
182
183
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
239
240
241
242
246
247
248
253
254
260
261
267
268
274
275
276
277
278
279
280
281
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
311
312
317
318
319
324
325
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
350
351
360
361
362
363
364
365
366
373
374
375
376
377
378
379
380
381
382
383
388
389
390
391
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
420
421
422
423
424
425
426
427
428
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
465
466
467
471
472
473
474
475
476
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
539
546
550
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
665
666
667
668
669
670
671
672
673
674
675
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <helper/system.h>
#include <libusb.h>
#include <helper/log.h>
#include <helper/replacements.h>
#include <jtag/jtag.h>
#include "cmsis_dap.h"
#include "libusb_helper.h"
7 includes
enum {
CMSIS_DAP_TRANSFER_PENDING = 0,
CMSIS_DAP_TRANSFER_IDLE,
CMSIS_DAP_TRANSFER_COMPLETED
...};
struct cmsis_dap_bulk_transfer {
struct libusb_transfer *transfer;
uint8_t *buffer;
int status;
int transferred;
...};
struct cmsis_dap_backend_data {
struct libusb_context *usb_ctx;
struct libusb_device_handle *dev_handle;
unsigned int ep_out;
unsigned int ep_in;
int interface;
struct cmsis_dap_bulk_transfer command_transfers[MAX_PENDING_REQUESTS];
struct cmsis_dap_bulk_transfer response_transfers[MAX_PENDING_REQUESTS];
...};
static int cmsis_dap_usb_interface = -1;
static void cmsis_dap_usb_close(struct cmsis_dap *dap);
static int cmsis_dap_usb_alloc(struct cmsis_dap *dap, unsigned int pkt_sz);
static void cmsis_dap_usb_free(struct cmsis_dap *dap);
static int cmsis_dap_usb_open(struct cmsis_dap *dap, uint16_t vids[], uint16_t pids[], const char *serial)
{
int err;
struct libusb_context *ctx;
struct libusb_device **device_list;
err = libusb_init(&ctx);
if (err) {
LOG_ERROR("libusb initialization failed: %s", libusb_strerror(err));
return ERROR_FAIL;
}if (err) { ... }
int num_devices = libusb_get_device_list(ctx, &device_list);
if (num_devices < 0) {
LOG_ERROR("could not enumerate USB devices: %s", libusb_strerror(num_devices));
libusb_exit(ctx);
return ERROR_FAIL;
}if (num_devices < 0) { ... }
for (int i = 0; i < num_devices; i++) {
struct libusb_device *dev = device_list[i];
struct libusb_device_descriptor dev_desc;
err = libusb_get_device_descriptor(dev, &dev_desc);
if (err) {
LOG_ERROR("could not get device descriptor for device %d: %s", i, libusb_strerror(err));
continue;
}if (err) { ... }
bool id_match = false;
bool id_filter = vids[0] || pids[0];
for (int id = 0; vids[id] || pids[id]; id++) {
id_match = !vids[id] || dev_desc.idVendor == vids[id];
id_match &= !pids[id] || dev_desc.idProduct == pids[id];
if (id_match)
break;
}for (int id = 0; vids[id] || pids[id]; id++) { ... }
if (id_filter && !id_match)
continue;
if (dev_desc.iSerialNumber == 0 && serial && serial[0])
continue;
struct libusb_device_handle *dev_handle = NULL;
err = libusb_open(dev, &dev_handle);
if (err) {
/* ... */
if (id_filter) {
LOG_ERROR("could not open device 0x%04x:0x%04x: %s",
dev_desc.idVendor, dev_desc.idProduct, libusb_strerror(err));
}if (id_filter) { ... } else {
LOG_DEBUG("could not open device 0x%04x:0x%04x: %s",
dev_desc.idVendor, dev_desc.idProduct, libusb_strerror(err));
}else { ... }
continue;
}if (err) { ... }
bool serial_match = false;
char dev_serial[256] = {0};
if (dev_desc.iSerialNumber > 0) {
err = libusb_get_string_descriptor_ascii(
dev_handle, dev_desc.iSerialNumber,
(uint8_t *)dev_serial, sizeof(dev_serial));
if (err < 0) {
const char *msg = "could not read serial number for device 0x%04x:0x%04x: %s";
if (serial)
LOG_WARNING(msg, dev_desc.idVendor, dev_desc.idProduct,
libusb_strerror(err));
else
LOG_DEBUG(msg, dev_desc.idVendor, dev_desc.idProduct,
libusb_strerror(err));
}if (err < 0) { ... } else if (serial && strncmp(dev_serial, serial, sizeof(dev_serial)) == 0) {
serial_match = true;
}else if (serial && strncmp(dev_serial, serial, sizeof(dev_serial)) == 0) { ... }
}if (dev_desc.iSerialNumber > 0) { ... }
if (serial && !serial_match) {
libusb_close(dev_handle);
continue;
}if (serial && !serial_match) { ... }
bool cmsis_dap_in_product_str = false;
char product_string[256] = {0};
if (dev_desc.iProduct > 0) {
err = libusb_get_string_descriptor_ascii(
dev_handle, dev_desc.iProduct,
(uint8_t *)product_string, sizeof(product_string));
if (err < 0) {
LOG_WARNING("could not read product string for device 0x%04x:0x%04x: %s",
dev_desc.idVendor, dev_desc.idProduct, libusb_strerror(err));
}if (err < 0) { ... } else if (strstr(product_string, "CMSIS-DAP")) {
LOG_DEBUG("found product string of 0x%04x:0x%04x '%s'",
dev_desc.idVendor, dev_desc.idProduct, product_string);
cmsis_dap_in_product_str = true;
}else if (strstr(product_string, "CMSIS-DAP")) { ... }
}if (dev_desc.iProduct > 0) { ... }
bool device_identified_reliably = cmsis_dap_in_product_str
|| serial_match || id_match;
for (int config = 0; config < dev_desc.bNumConfigurations; config++) {
struct libusb_config_descriptor *config_desc;
err = libusb_get_config_descriptor(dev, config, &config_desc);
if (err) {
LOG_ERROR("could not get configuration descriptor %d for device 0x%04x:0x%04x: %s",
config, dev_desc.idVendor, dev_desc.idProduct, libusb_strerror(err));
continue;
}if (err) { ... }
LOG_DEBUG("enumerating interfaces of 0x%04x:0x%04x",
dev_desc.idVendor, dev_desc.idProduct);
int config_num = config_desc->bConfigurationValue;
const struct libusb_interface_descriptor *intf_desc_candidate = NULL;
const struct libusb_interface_descriptor *intf_desc_found = NULL;
for (int interface = 0; interface < config_desc->bNumInterfaces; interface++) {
const struct libusb_interface_descriptor *intf_desc = &config_desc->interface[interface].altsetting[0];
int interface_num = intf_desc->bInterfaceNumber;
if (cmsis_dap_usb_interface != -1 && cmsis_dap_usb_interface != interface_num)
continue;
/* ... */
bool cmsis_dap_in_interface_str = false;
if (intf_desc->iInterface != 0) {
char interface_str[256] = {0};
err = libusb_get_string_descriptor_ascii(
dev_handle, intf_desc->iInterface,
(uint8_t *)interface_str, sizeof(interface_str));
if (err < 0) {
LOG_DEBUG("could not read interface string %d for device 0x%04x:0x%04x: %s",
intf_desc->iInterface,
dev_desc.idVendor, dev_desc.idProduct,
libusb_strerror(err));
}if (err < 0) { ... } else if (strstr(interface_str, "CMSIS-DAP")) {
cmsis_dap_in_interface_str = true;
LOG_DEBUG("found interface %d string '%s'",
interface_num, interface_str);
}else if (strstr(interface_str, "CMSIS-DAP")) { ... }
}if (intf_desc->iInterface != 0) { ... }
if (cmsis_dap_usb_interface == -1) {
if (!cmsis_dap_in_product_str && !cmsis_dap_in_interface_str)
continue;
}if (cmsis_dap_usb_interface == -1) { ... }
if (intf_desc->bNumEndpoints < 2) {
LOG_DEBUG("skipping interface %d, has only %d endpoints",
interface_num, intf_desc->bNumEndpoints);
continue;
}if (intf_desc->bNumEndpoints < 2) { ... }
if ((intf_desc->endpoint[0].bmAttributes & 3) != LIBUSB_TRANSFER_TYPE_BULK ||
(intf_desc->endpoint[0].bEndpointAddress & 0x80) != LIBUSB_ENDPOINT_OUT) {
LOG_DEBUG("skipping interface %d, endpoint[0] is not bulk out",
interface_num);
continue;
}if ((intf_desc->endpoint[0].bmAttributes & 3) != LIBUSB_TRANSFER_TYPE_BULK || (intf_desc->endpoint[0].bEndpointAddress & 0x80) != LIBUSB_ENDPOINT_OUT) { ... }
if ((intf_desc->endpoint[1].bmAttributes & 3) != LIBUSB_TRANSFER_TYPE_BULK ||
(intf_desc->endpoint[1].bEndpointAddress & 0x80) != LIBUSB_ENDPOINT_IN) {
LOG_DEBUG("skipping interface %d, endpoint[1] is not bulk in",
interface_num);
continue;
}if ((intf_desc->endpoint[1].bmAttributes & 3) != LIBUSB_TRANSFER_TYPE_BULK || (intf_desc->endpoint[1].bEndpointAddress & 0x80) != LIBUSB_ENDPOINT_IN) { ... }
/* ... */
bool intf_identified_reliably = cmsis_dap_in_interface_str
|| (device_identified_reliably &&
(cmsis_dap_usb_interface != -1
|| config_desc->bNumInterfaces == 1));
if (intf_desc->bInterfaceClass != LIBUSB_CLASS_VENDOR_SPEC ||
intf_desc->bInterfaceSubClass != 0 || intf_desc->bInterfaceProtocol != 0) {
/* ... */
if (intf_identified_reliably &&
(intf_desc->bInterfaceClass == 0 || intf_desc->bInterfaceClass > 0x12)) {
LOG_WARNING("Using CMSIS-DAPv2 interface %d with wrong class %" PRId8
" subclass %" PRId8 " or protocol %" PRId8,
interface_num,
intf_desc->bInterfaceClass,
intf_desc->bInterfaceSubClass,
intf_desc->bInterfaceProtocol);
}if (intf_identified_reliably && (intf_desc->bInterfaceClass == 0 || intf_desc->bInterfaceClass > 0x12)) { ... } else {
LOG_DEBUG("skipping interface %d, class %" PRId8
" subclass %" PRId8 " protocol %" PRId8,
interface_num,
intf_desc->bInterfaceClass,
intf_desc->bInterfaceSubClass,
intf_desc->bInterfaceProtocol);
continue;
}else { ... }
}if (intf_desc->bInterfaceClass != LIBUSB_CLASS_VENDOR_SPEC || intf_desc->bInterfaceSubClass != 0 || intf_desc->bInterfaceProtocol != 0) { ... }
if (intf_identified_reliably) {
intf_desc_found = intf_desc;
break;
}if (intf_identified_reliably) { ... }
if (!intf_desc_candidate && device_identified_reliably) {
/* ... */
intf_desc_candidate = intf_desc;
}if (!intf_desc_candidate && device_identified_reliably) { ... }
}for (int interface = 0; interface < config_desc->bNumInterfaces; interface++) { ... }
if (!intf_desc_found) {
/* ... */
intf_desc_found = intf_desc_candidate;
}if (!intf_desc_found) { ... }
if (!intf_desc_found) {
libusb_free_config_descriptor(config_desc);
continue;
}if (!intf_desc_found) { ... }
int interface_num = intf_desc_found->bInterfaceNumber;
int packet_size = intf_desc_found->endpoint[0].wMaxPacketSize;
int ep_out = intf_desc_found->endpoint[0].bEndpointAddress;
int ep_in = intf_desc_found->endpoint[1].bEndpointAddress;
libusb_free_config_descriptor(config_desc);
libusb_free_device_list(device_list, true);
LOG_INFO("Using CMSIS-DAPv2 interface with VID:PID=0x%04x:0x%04x, serial=%s",
dev_desc.idVendor, dev_desc.idProduct, dev_serial);
int current_config;
err = libusb_get_configuration(dev_handle, ¤t_config);
if (err) {
LOG_ERROR("could not find current configuration: %s", libusb_strerror(err));
libusb_close(dev_handle);
libusb_exit(ctx);
return ERROR_FAIL;
}if (err) { ... }
if (config_num != current_config) {
err = libusb_set_configuration(dev_handle, config_num);
if (err) {
LOG_ERROR("could not set configuration: %s", libusb_strerror(err));
libusb_close(dev_handle);
libusb_exit(ctx);
return ERROR_FAIL;
}if (err) { ... }
}if (config_num != current_config) { ... }
err = libusb_claim_interface(dev_handle, interface_num);
if (err)
LOG_WARNING("could not claim interface: %s", libusb_strerror(err));
dap->bdata = calloc(1, sizeof(struct cmsis_dap_backend_data));
if (!dap->bdata) {
LOG_ERROR("unable to allocate memory");
libusb_release_interface(dev_handle, interface_num);
libusb_close(dev_handle);
libusb_exit(ctx);
return ERROR_FAIL;
}if (!dap->bdata) { ... }
dap->bdata->usb_ctx = ctx;
dap->bdata->dev_handle = dev_handle;
dap->bdata->ep_out = ep_out;
dap->bdata->ep_in = ep_in;
dap->bdata->interface = interface_num;
for (unsigned int idx = 0; idx < MAX_PENDING_REQUESTS; idx++) {
dap->bdata->command_transfers[idx].status = CMSIS_DAP_TRANSFER_IDLE;
dap->bdata->command_transfers[idx].transfer = libusb_alloc_transfer(0);
if (!dap->bdata->command_transfers[idx].transfer) {
LOG_ERROR("unable to allocate USB transfer");
cmsis_dap_usb_close(dap);
return ERROR_FAIL;
}if (!dap->bdata->command_transfers[idx].transfer) { ... }
dap->bdata->response_transfers[idx].status = CMSIS_DAP_TRANSFER_IDLE;
dap->bdata->response_transfers[idx].transfer = libusb_alloc_transfer(0);
if (!dap->bdata->response_transfers[idx].transfer) {
LOG_ERROR("unable to allocate USB transfer");
cmsis_dap_usb_close(dap);
return ERROR_FAIL;
}if (!dap->bdata->response_transfers[idx].transfer) { ... }
}for (unsigned int idx = 0; idx < MAX_PENDING_REQUESTS; idx++) { ... }
err = cmsis_dap_usb_alloc(dap, packet_size);
if (err != ERROR_OK)
cmsis_dap_usb_close(dap);
return err;
}for (int config = 0; config < dev_desc.bNumConfigurations; config++) { ... }
libusb_close(dev_handle);
}for (int i = 0; i < num_devices; i++) { ... }
libusb_free_device_list(device_list, true);
libusb_exit(ctx);
return ERROR_FAIL;
}{ ... }
static void cmsis_dap_usb_close(struct cmsis_dap *dap)
{
for (unsigned int i = 0; i < MAX_PENDING_REQUESTS; i++) {
libusb_free_transfer(dap->bdata->command_transfers[i].transfer);
libusb_free_transfer(dap->bdata->response_transfers[i].transfer);
}for (unsigned int i = 0; i < MAX_PENDING_REQUESTS; i++) { ... }
cmsis_dap_usb_free(dap);
libusb_release_interface(dap->bdata->dev_handle, dap->bdata->interface);
libusb_close(dap->bdata->dev_handle);
libusb_exit(dap->bdata->usb_ctx);
free(dap->bdata);
dap->bdata = NULL;
}{ ... }
static void LIBUSB_CALL cmsis_dap_usb_callback(struct libusb_transfer *transfer)
{
struct cmsis_dap_bulk_transfer *tr;
tr = (struct cmsis_dap_bulk_transfer *)transfer->user_data;
if (transfer->status == LIBUSB_TRANSFER_COMPLETED) {
tr->status = CMSIS_DAP_TRANSFER_COMPLETED;
tr->transferred = transfer->actual_length;
}if (transfer->status == LIBUSB_TRANSFER_COMPLETED) { ... } else if (transfer->status == LIBUSB_TRANSFER_TIMED_OUT) {
tr->status = ERROR_TIMEOUT_REACHED;
}else if (transfer->status == LIBUSB_TRANSFER_TIMED_OUT) { ... } else {
tr->status = ERROR_JTAG_DEVICE_ERROR;
}else { ... }
}cmsis_dap_usb_callback (struct libusb_transfer *transfer) { ... }
static int cmsis_dap_usb_read(struct cmsis_dap *dap, int transfer_timeout_ms,
struct timeval *wait_timeout)
{
int transferred = 0;
int err;
struct cmsis_dap_bulk_transfer *tr;
tr = &dap->bdata->response_transfers[dap->pending_fifo_get_idx];
if (tr->status == CMSIS_DAP_TRANSFER_IDLE) {
libusb_fill_bulk_transfer(tr->transfer,
dap->bdata->dev_handle, dap->bdata->ep_in,
tr->buffer, dap->packet_size,
&cmsis_dap_usb_callback, tr,
transfer_timeout_ms);
LOG_DEBUG_IO("submit read @ %u", dap->pending_fifo_get_idx);
tr->status = CMSIS_DAP_TRANSFER_PENDING;
err = libusb_submit_transfer(tr->transfer);
if (err) {
tr->status = CMSIS_DAP_TRANSFER_IDLE;
LOG_ERROR("error submitting USB read: %s", libusb_strerror(err));
return ERROR_FAIL;
}if (err) { ... }
}if (tr->status == CMSIS_DAP_TRANSFER_IDLE) { ... }
struct timeval tv = {
.tv_sec = transfer_timeout_ms / 1000,
.tv_usec = transfer_timeout_ms % 1000 * 1000
...};
while (tr->status == CMSIS_DAP_TRANSFER_PENDING) {
err = libusb_handle_events_timeout_completed(dap->bdata->usb_ctx,
wait_timeout ? wait_timeout : &tv,
&tr->status);
if (err) {
LOG_ERROR("error handling USB events: %s", libusb_strerror(err));
return ERROR_FAIL;
}if (err) { ... }
if (wait_timeout)
break;
}while (tr->status == CMSIS_DAP_TRANSFER_PENDING) { ... }
if (tr->status < 0 || tr->status == CMSIS_DAP_TRANSFER_COMPLETED) {
struct cmsis_dap_bulk_transfer *tr_cmd;
tr_cmd = &dap->bdata->command_transfers[dap->pending_fifo_get_idx];
if (tr_cmd->status < 0) {
err = tr_cmd->status;
tr_cmd->status = CMSIS_DAP_TRANSFER_IDLE;
if (err != ERROR_TIMEOUT_REACHED)
LOG_ERROR("error writing USB data");
else
LOG_DEBUG("command write USB timeout @ %u", dap->pending_fifo_get_idx);
return err;
}if (tr_cmd->status < 0) { ... }
if (tr_cmd->status == CMSIS_DAP_TRANSFER_COMPLETED)
tr_cmd->status = CMSIS_DAP_TRANSFER_IDLE;
}if (tr->status < 0 || tr->status == CMSIS_DAP_TRANSFER_COMPLETED) { ... }
if (tr->status < 0) {
err = tr->status;
tr->status = CMSIS_DAP_TRANSFER_IDLE;
if (err != ERROR_TIMEOUT_REACHED)
LOG_ERROR("error reading USB data");
else
LOG_DEBUG("USB timeout @ %u", dap->pending_fifo_get_idx);
return err;
}if (tr->status < 0) { ... }
if (tr->status == CMSIS_DAP_TRANSFER_COMPLETED) {
transferred = tr->transferred;
LOG_DEBUG_IO("completed read @ %u, transferred %i",
dap->pending_fifo_get_idx, transferred);
memcpy(dap->packet_buffer, tr->buffer, transferred);
memset(&dap->packet_buffer[transferred], 0, dap->packet_buffer_size - transferred);
tr->status = CMSIS_DAP_TRANSFER_IDLE;
}if (tr->status == CMSIS_DAP_TRANSFER_COMPLETED) { ... }
return transferred;
}cmsis_dap_usb_read (struct cmsis_dap *dap, int transfer_timeout_ms, struct timeval *wait_timeout) { ... }
static int cmsis_dap_usb_write(struct cmsis_dap *dap, int txlen, int timeout_ms)
{
int err;
struct cmsis_dap_bulk_transfer *tr;
tr = &dap->bdata->command_transfers[dap->pending_fifo_put_idx];
if (tr->status == CMSIS_DAP_TRANSFER_PENDING) {
LOG_ERROR("busy command USB transfer at %u", dap->pending_fifo_put_idx);
struct timeval tv = {
.tv_sec = timeout_ms / 1000,
.tv_usec = timeout_ms % 1000 * 1000
...};
libusb_handle_events_timeout_completed(dap->bdata->usb_ctx, &tv, &tr->status);
}if (tr->status == CMSIS_DAP_TRANSFER_PENDING) { ... }
if (tr->status < 0) {
if (tr->status != ERROR_TIMEOUT_REACHED)
LOG_ERROR("error writing USB data, late detect");
else
LOG_DEBUG("USB write timeout @ %u, late detect", dap->pending_fifo_get_idx);
tr->status = CMSIS_DAP_TRANSFER_IDLE;
}if (tr->status < 0) { ... }
if (tr->status == CMSIS_DAP_TRANSFER_COMPLETED) {
LOG_ERROR("USB write: late transfer competed");
tr->status = CMSIS_DAP_TRANSFER_IDLE;
}if (tr->status == CMSIS_DAP_TRANSFER_COMPLETED) { ... }
if (tr->status != CMSIS_DAP_TRANSFER_IDLE) {
libusb_cancel_transfer(tr->transfer);
return ERROR_JTAG_DEVICE_ERROR;
}if (tr->status != CMSIS_DAP_TRANSFER_IDLE) { ... }
memcpy(tr->buffer, dap->packet_buffer, txlen);
libusb_fill_bulk_transfer(tr->transfer,
dap->bdata->dev_handle, dap->bdata->ep_out,
tr->buffer, txlen,
&cmsis_dap_usb_callback, tr,
timeout_ms);
LOG_DEBUG_IO("submit write @ %u", dap->pending_fifo_put_idx);
tr->status = CMSIS_DAP_TRANSFER_PENDING;
err = libusb_submit_transfer(tr->transfer);
if (err) {
if (err == LIBUSB_ERROR_BUSY)
libusb_cancel_transfer(tr->transfer);
else
tr->status = CMSIS_DAP_TRANSFER_IDLE;
LOG_ERROR("error submitting USB write: %s", libusb_strerror(err));
return ERROR_FAIL;
}if (err) { ... }
return ERROR_OK;
}cmsis_dap_usb_write (struct cmsis_dap *dap, int txlen, int timeout_ms) { ... }
static int cmsis_dap_usb_alloc(struct cmsis_dap *dap, unsigned int pkt_sz)
{
dap->packet_buffer = malloc(pkt_sz);
if (!dap->packet_buffer) {
LOG_ERROR("unable to allocate CMSIS-DAP packet buffer");
return ERROR_FAIL;
}if (!dap->packet_buffer) { ... }
dap->packet_size = pkt_sz;
dap->packet_buffer_size = pkt_sz;
dap->packet_usable_size = pkt_sz - 1;
dap->command = dap->packet_buffer;
dap->response = dap->packet_buffer;
struct cmsis_dap_backend_data *bdata = dap->bdata;
for (unsigned int i = 0; i < MAX_PENDING_REQUESTS; i++) {
bdata->command_transfers[i].buffer =
oocd_libusb_dev_mem_alloc(bdata->dev_handle, pkt_sz);
bdata->response_transfers[i].buffer =
oocd_libusb_dev_mem_alloc(bdata->dev_handle, pkt_sz);
if (!bdata->command_transfers[i].buffer
|| !bdata->response_transfers[i].buffer) {
LOG_ERROR("unable to allocate CMSIS-DAP pending packet buffer");
return ERROR_FAIL;
}if (!bdata->command_transfers[i].buffer || !bdata->response_transfers[i].buffer) { ... }
}for (unsigned int i = 0; i < MAX_PENDING_REQUESTS; i++) { ... }
return ERROR_OK;
}cmsis_dap_usb_alloc (struct cmsis_dap *dap, unsigned int pkt_sz) { ... }
static void cmsis_dap_usb_free(struct cmsis_dap *dap)
{
struct cmsis_dap_backend_data *bdata = dap->bdata;
for (unsigned int i = 0; i < MAX_PENDING_REQUESTS; i++) {
oocd_libusb_dev_mem_free(bdata->dev_handle,
bdata->command_transfers[i].buffer, dap->packet_size);
oocd_libusb_dev_mem_free(bdata->dev_handle,
bdata->response_transfers[i].buffer, dap->packet_size);
bdata->command_transfers[i].buffer = NULL;
bdata->response_transfers[i].buffer = NULL;
}for (unsigned int i = 0; i < MAX_PENDING_REQUESTS; i++) { ... }
free(dap->packet_buffer);
dap->packet_buffer = NULL;
dap->command = NULL;
dap->response = NULL;
}cmsis_dap_usb_free (struct cmsis_dap *dap) { ... }
static void cmsis_dap_usb_cancel_all(struct cmsis_dap *dap)
{
for (unsigned int i = 0; i < MAX_PENDING_REQUESTS; i++) {
if (dap->bdata->command_transfers[i].status == CMSIS_DAP_TRANSFER_PENDING)
libusb_cancel_transfer(dap->bdata->command_transfers[i].transfer);
if (dap->bdata->response_transfers[i].status == CMSIS_DAP_TRANSFER_PENDING)
libusb_cancel_transfer(dap->bdata->response_transfers[i].transfer);
dap->bdata->command_transfers[i].status = CMSIS_DAP_TRANSFER_IDLE;
dap->bdata->response_transfers[i].status = CMSIS_DAP_TRANSFER_IDLE;
}for (unsigned int i = 0; i < MAX_PENDING_REQUESTS; i++) { ... }
}cmsis_dap_usb_cancel_all (struct cmsis_dap *dap) { ... }
COMMAND_HANDLER(cmsis_dap_handle_usb_interface_command)
{
if (CMD_ARGC == 1)
COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], cmsis_dap_usb_interface);
else
LOG_ERROR("expected exactly one argument to cmsis_dap_usb_interface <interface_number>");
return ERROR_OK;
}COMMAND_HANDLER (cmsis_dap_handle_usb_interface_command) { ... }
const struct command_registration cmsis_dap_usb_subcommand_handlers[] = {
{
.name = "interface",
.handler = &cmsis_dap_handle_usb_interface_command,
.mode = COMMAND_CONFIG,
.help = "set the USB interface number to use (for USB bulk backend only)",
.usage = "<interface_number>",
...},
COMMAND_REGISTRATION_DONE
...};
const struct cmsis_dap_backend cmsis_dap_usb_backend = {
.name = "usb_bulk",
.open = cmsis_dap_usb_open,
.close = cmsis_dap_usb_close,
.read = cmsis_dap_usb_read,
.write = cmsis_dap_usb_write,
.packet_buffer_alloc = cmsis_dap_usb_alloc,
.packet_buffer_free = cmsis_dap_usb_free,
.cancel_all = cmsis_dap_usb_cancel_all,
...};