50 struct libusb_context *ctx;
51 struct libusb_device **device_list;
53 err = libusb_init(&ctx);
55 LOG_ERROR(
"libusb initialization failed: %s", libusb_strerror(err));
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));
66 for (
int i = 0; i < num_devices; i++) {
67 struct libusb_device *dev = device_list[i];
68 struct libusb_device_descriptor dev_desc;
70 err = libusb_get_device_descriptor(dev, &dev_desc);
72 LOG_ERROR(
"could not get device descriptor for device %d: %s", i, libusb_strerror(err));
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];
88 if (id_filter && !id_match)
95 struct libusb_device_handle *dev_handle =
NULL;
96 err = libusb_open(dev, &dev_handle);
102 LOG_ERROR(
"could not open device 0x%04x:0x%04x: %s",
103 dev_desc.idVendor, dev_desc.idProduct, libusb_strerror(err));
105 LOG_DEBUG(
"could not open device 0x%04x:0x%04x: %s",
106 dev_desc.idVendor, dev_desc.idProduct, libusb_strerror(err));
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));
121 const char *msg =
"could not read serial number for device 0x%04x:0x%04x: %s";
123 LOG_WARNING(msg, dev_desc.idVendor, dev_desc.idProduct,
124 libusb_strerror(err));
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) {
133 if (
serial && !serial_match) {
134 libusb_close(dev_handle);
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));
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;
156 bool device_identified_reliably = cmsis_dap_in_product_str
157 || serial_match || id_match;
162 struct libusb_config_descriptor *config_desc;
163 err = libusb_get_config_descriptor(dev,
config, &config_desc);
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));
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;
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;
202 bool cmsis_dap_in_interface_str =
false;
203 if (intf_desc->iInterface != 0) {
205 char interface_str[256] = {0};
207 err = libusb_get_string_descriptor_ascii(
208 dev_handle, intf_desc->iInterface,
209 (uint8_t *)interface_str,
sizeof(interface_str));
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);
224 if (!cmsis_dap_in_product_str && !cmsis_dap_in_interface_str)
229 if (intf_desc->bNumEndpoints < 2) {
230 LOG_DEBUG(
"skipping interface %d, has only %d endpoints",
231 interface_num, intf_desc->bNumEndpoints);
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",
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",
255 bool intf_identified_reliably = cmsis_dap_in_interface_str
256 || (device_identified_reliably &&
258 || config_desc->bNumInterfaces == 1));
260 if (intf_desc->bInterfaceClass != LIBUSB_CLASS_VENDOR_SPEC ||
261 intf_desc->bInterfaceSubClass != 0 || intf_desc->bInterfaceProtocol != 0) {
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,
272 intf_desc->bInterfaceClass,
273 intf_desc->bInterfaceSubClass,
274 intf_desc->bInterfaceProtocol);
276 LOG_DEBUG(
"skipping interface %d, class %" PRId8
277 " subclass %" PRId8
" protocol %" PRId8,
279 intf_desc->bInterfaceClass,
280 intf_desc->bInterfaceSubClass,
281 intf_desc->bInterfaceProtocol);
287 if (intf_identified_reliably) {
289 intf_desc_found = intf_desc;
293 if (!intf_desc_candidate && device_identified_reliably) {
296 intf_desc_candidate = intf_desc;
300 if (!intf_desc_found) {
303 intf_desc_found = intf_desc_candidate;
306 if (!intf_desc_found) {
307 libusb_free_config_descriptor(config_desc);
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;
317 libusb_free_config_descriptor(config_desc);
318 libusb_free_device_list(device_list,
true);
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);
324 err = libusb_get_configuration(dev_handle, ¤t_config);
326 LOG_ERROR(
"could not find current configuration: %s", libusb_strerror(err));
327 libusb_close(dev_handle);
332 if (config_num != current_config) {
333 err = libusb_set_configuration(dev_handle, config_num);
335 LOG_ERROR(
"could not set configuration: %s", libusb_strerror(err));
336 libusb_close(dev_handle);
342 err = libusb_claim_interface(dev_handle, interface_num);
344 LOG_WARNING(
"could not claim interface: %s", libusb_strerror(err));
349 libusb_release_interface(dev_handle, interface_num);
350 libusb_close(dev_handle);
376 libusb_close(dev_handle);
379 libusb_free_device_list(device_list,
true);
404 if (err == LIBUSB_ERROR_TIMEOUT) {
407 LOG_ERROR(
"error reading data: %s", libusb_strerror(err));
426 if (err == LIBUSB_ERROR_TIMEOUT) {
429 LOG_ERROR(
"error writing data: %s", libusb_strerror(err));
439 uint8_t *buf = malloc(pkt_sz);
441 LOG_ERROR(
"unable to allocate CMSIS-DAP packet buffer");
460 LOG_ERROR(
"expected exactly one argument to cmsis_dap_usb_interface <interface_number>");
468 .handler = &cmsis_dap_handle_usb_interface_command,
470 .help =
"set the USB interface number to use (for USB bulk backend only)",
471 .usage =
"<interface_number>",
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...
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
#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...
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
static struct device_config config
#define LOG_WARNING(expr ...)
#define LOG_ERROR(expr ...)
#define ERROR_TIMEOUT_REACHED
#define LOG_INFO(expr ...)
#define LOG_DEBUG(expr ...)
static unsigned int ep_out
static unsigned int ep_in
char id[RTT_CB_MAX_ID_LENGTH]
Control block identifier.
struct libusb_context * usb_ctx
struct libusb_device_handle * dev_handle
struct cmsis_dap_backend_data * bdata
uint16_t packet_buffer_size