From 3a3558d7e1b7e3af4faacf72c07d576afec86772 Mon Sep 17 00:00:00 2001 From: ALTracer <11005378+ALTracer@users.noreply.github.com> Date: Sun, 18 Aug 2024 22:27:44 +0300 Subject: [PATCH] swolisten: Add support for walking USB descriptors --- scripts/swolisten.c | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/scripts/swolisten.c b/scripts/swolisten.c index f7408fd74bc..7f056504dbc 100644 --- a/scripts/swolisten.c +++ b/scripts/swolisten.c @@ -40,8 +40,7 @@ #define VID (0x1d50) #define PID (0x6018) -#define INTERFACE (5) -#define ENDPOINT (0x85) +#define IFACE_STR "Black Magic Trace Capture" #define TRANSFER_SIZE (64) #define NUM_FIFOS 32 @@ -417,6 +416,7 @@ int usbFeeder(void) unsigned char cbw[TRANSFER_SIZE]; libusb_device_handle *handle; libusb_device *dev; + struct libusb_config_descriptor *config; int size; while (1) @@ -435,10 +435,36 @@ int usbFeeder(void) if (!(dev = libusb_get_device(handle))) continue; - if (libusb_claim_interface (handle, INTERFACE)<0) + uint8_t bmp_interface = 0; + uint8_t bmp_endpoint = 0; + + if (libusb_get_active_config_descriptor(dev, &config) < 0) + continue; + + int interface_found = 0; + for (uint8_t if_num = 0; if_num < config->bNumInterfaces && !interface_found; if_num++) { + for (int alt_num = 0; alt_num < config->interface[if_num].num_altsetting && !interface_found; alt_num++) { + const struct libusb_interface_descriptor *i = &config->interface[if_num].altsetting[alt_num]; + char itfString[256] = {0}; + + int ret = libusb_get_string_descriptor_ascii(handle, i->iInterface, (unsigned char*)itfString, 256); + if (ret < 0) + continue; + if (strcmp (itfString, IFACE_STR) != 0) + continue; + + bmp_interface = i->bInterfaceNumber; + bmp_endpoint = i->endpoint[0].bEndpointAddress; + interface_found = 1; + } + } + + libusb_free_config_descriptor(config); + + if (libusb_claim_interface (handle, bmp_interface)<0) continue; - while (0==libusb_bulk_transfer(handle, ENDPOINT, cbw, TRANSFER_SIZE, &size, 10)) + while (0==libusb_bulk_transfer(handle, bmp_endpoint, cbw, TRANSFER_SIZE, &size, 10)) { unsigned char *c=cbw; if (options.dump)