From 92d4db7262439010d4aae11d2a8d1010dc782abe Mon Sep 17 00:00:00 2001 From: Stefan Rueger Date: Sat, 3 Aug 2024 16:14:27 +0100 Subject: [PATCH] Provide -P usb:vid:pid for USB hid i/f and allow long -P usb:serno --- src/usb_hidapi.c | 78 ++++++++++++++++++++++++++++++++---------------- src/usb_libusb.c | 50 ++++++++++++------------------- 2 files changed, 72 insertions(+), 56 deletions(-) diff --git a/src/usb_hidapi.c b/src/usb_hidapi.c index 55bbe6bf..fea15f40 100644 --- a/src/usb_hidapi.c +++ b/src/usb_hidapi.c @@ -17,7 +17,7 @@ */ /* - * USB interface via libhidapi for avrdude. + * USB interface via libhidapi for avrdude; it's used for jtag3 programmers */ #include @@ -45,9 +45,9 @@ * to pass the desired USB device ID. */ static int usbhid_open(const char *port, union pinfo pinfo, union filedescriptor *fd) { - hid_device *dev; - char *serno, *cp2; - size_t x; + hid_device *dev = NULL; + char serno[64], *s; + const char *serp, *vidp, *pidp; unsigned char usbbuf[USBDEV_MAX_XFER_3 + 1]; if (fd->usb.max_xfer == 0) @@ -56,31 +56,38 @@ static int usbhid_open(const char *port, union pinfo pinfo, union filedescriptor /* * The syntax for usb devices is defined as: * - * -P usb[:serialnumber] + * -P usb:vid:pid + * -P usb:serialnumber + * -P usb * - * See if we've got a serial number passed here. The serial number - * might contain colons which we remove below, and we compare it - * right-to-left, so only the least significant nibbles need to be - * specified. + * First check for a valid vid:pid pair, then see if there is a serial number + * passed here. The serial number might contain colons which are removed; + * comparison is right-to-left, so only the least significant nibbles need to + * be specified. */ - if ((serno = strchr(port, ':')) != NULL) { - /* First, drop all colons there if any */ - cp2 = ++serno; - while ((cp2 = strchr(cp2, ':')) != NULL) { - x = strlen(cp2) - 1; - memmove(cp2, cp2 + 1, x); - cp2[x] = '\0'; + if((vidp = strchr(port, ':')) && (pidp = strchr(vidp+1, ':'))) { + int vid, pid; + + if(sscanf(vidp+1, "%x", &vid) == 1 && sscanf(pidp+1, "%x", &pid) == 1) { + if((dev = hid_open(vid, pid, NULL))) { + pmsg_notice2("USB device with VID: 0x%04x and PID: 0x%04x\n", vid, pid); + pinfo.usbinfo.vid = vid; + pinfo.usbinfo.pid = pid; + } } + } - if (strlen(serno) > 12) { - pmsg_error("invalid serial number %s\n", serno); - return -1; - } + if(!dev && (serp = vidp) && *++serp) { + // First, get a copy of the serial number w/out colons + for(s = serno; *serp && s < serno + sizeof serno - 1; serp++) + if(*serp != ':') + *s++ = *serp; + *s = 0; - wchar_t wserno[15]; - mbstowcs(wserno, serno, 15); - size_t serlen = strlen(serno); + wchar_t wserno[sizeof serno] = {0}; + mbstowcs(wserno, serno, sizeof serno); + size_t serlen = s - serno; /* * Now, try finding all devices matching VID:PID, and compare @@ -117,7 +124,7 @@ static int usbhid_open(const char *port, union pinfo pinfo, union filedescriptor pmsg_error("found device, but hid_open_path() failed\n"); return -1; } - } else { + } else if(!dev) { dev = hid_open(pinfo.usbinfo.vid, pinfo.usbinfo.pid, NULL); if (dev == NULL) { @@ -142,6 +149,26 @@ static int usbhid_open(const char *port, union pinfo pinfo, union filedescriptor fd->usb.handle = dev; + /* + * If a device contains the string "CMSIS-DAP" anywhere in its product name + * string, it claims to be a CMSIS-DAP spec HID device. HID interrupt packets + * can only be 64 byte (Full-Speed) or 512 byte (High-Speed) in length and + * cannot be any other value. Standard-Speed (8 bytes) will not work. + */ + wchar_t ps[256]; + if (hid_get_product_string(dev, ps, 255) == 0) { + size_t n = wcstombs(NULL, ps, 0) + 1; + if (n) { + char cn[255]; + if (wcstombs(cn, ps, n) != (size_t) -1 && str_contains(cn, "CMSIS-DAP")) { + // The JTAGICE3 running CMSIS-DAP doesn't use a separate endpoint for event reception + fd->usb.eep = 0; + fd->usb.max_xfer = 64; + pmsg_debug("usbhid_open(): product string: %s\n", cn); + } + } + } + /* * Try finding out the endpoint size. Alas, libhidapi doesn't * provide us with an API function for that, nor for the report @@ -168,7 +195,8 @@ static int usbhid_open(const char *port, union pinfo pinfo, union filedescriptor * be incremented by one, as the report ID will be omitted by the * hidapi library. */ - if (pinfo.usbinfo.vid == USB_VENDOR_ATMEL) { + + if(pinfo.usbinfo.vid == USB_VENDOR_ATMEL || pinfo.usbinfo.vid == USB_VENDOR_MICROCHIP) { pmsg_debug("%s(): probing for max packet size\n", __func__); memset(usbbuf, 0, sizeof usbbuf); usbbuf[0] = 0; /* no HID reports used */ diff --git a/src/usb_libusb.c b/src/usb_libusb.c index 86cadefa..487810ef 100644 --- a/src/usb_libusb.c +++ b/src/usb_libusb.c @@ -61,10 +61,10 @@ static int usbdev_open(const char *port, union pinfo pinfo, union filedescriptor struct usb_bus *bus; struct usb_device *dev; usb_dev_handle *udev; - char *serno, *cp2; + char *s, serno[64] = {0}; + const char *serp; int i; int iface; - size_t x; /* * The syntax for usb devices is defined as: @@ -76,24 +76,13 @@ static int usbdev_open(const char *port, union pinfo pinfo, union filedescriptor * right-to-left, so only the least significant nibbles need to be * specified. */ - if ((serno = strchr(port, ':')) != NULL) - { - /* first, drop all colons there if any */ - cp2 = ++serno; - - while ((cp2 = strchr(cp2, ':')) != NULL) - { - x = strlen(cp2) - 1; - memmove(cp2, cp2 + 1, x); - cp2[x] = '\0'; - } - - if (strlen(serno) > 12) - { - pmsg_error("invalid serial number %s\n", serno); - return -1; - } - } + if((serp = strchr(port, ':')) && *++serp) { + // First, get a copy of the serial number w/out colons + for(s = serno; *serp && s < serno + sizeof serno - 1; serp++) + if(*serp != ':') + *s++ = *serp; + *s = 0; + } if (fd->usb.max_xfer == 0) fd->usb.max_xfer = USBDEV_MAX_XFER_MKII; @@ -126,10 +115,9 @@ static int usbdev_open(const char *port, union pinfo pinfo, union filedescriptor * particular serial number, so we could * continue anyway. */ - if (serno != NULL) + if(*serno) return -1; /* no chance */ - else - strcpy(string, "[unknown]"); + strcpy(string, "[unknown]"); } if(serdev) serdev->usbsn = cache_string(string); @@ -173,20 +161,20 @@ static int usbdev_open(const char *port, union pinfo pinfo, union filedescriptor fd->usb.wep = 0x02; } - pmsg_notice2("%s(): found %s, serno: %s\n", __func__, product, string); - if (serno != NULL) + pmsg_notice2("%s(): found %s, serno: %s\n", __func__, product, string); + if (*serno) { /* * See if the serial number requested by the * user matches what we found, matching * right-to-left. */ - x = strlen(string) - strlen(serno); - if (!str_caseeq(string + x, serno)) + int x = strlen(string) - strlen(serno); + if (x < 0 || !str_caseeq(string + x, serno)) { - pmsg_debug("%s(): serial number does not match\n", __func__); + pmsg_debug("%s(): serial number does not match\n", __func__); usb_close(udev); - continue; + continue; } } @@ -292,8 +280,8 @@ static int usbdev_open(const char *port, union pinfo pinfo, union filedescriptor } if ((pinfo.usbinfo.flags & PINFO_FL_SILENT) == 0) - pmsg_notice("%s(): did not find any%s USB device %s (0x%04x:0x%04x)\n", __func__, - serno? " (matching)": "", port, (unsigned)pinfo.usbinfo.vid, (unsigned)pinfo.usbinfo.pid); + pmsg_notice("%s(): did not find any%s USB device %s (0x%04x:0x%04x)\n", __func__, + *serno? " (matching)": "", port, (unsigned) pinfo.usbinfo.vid, (unsigned)pinfo.usbinfo.pid); return -1; }