X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=src%2Fstlink-usb.c;h=98654129eccdca57b5a20ebaca2b5d525f48d873;hb=refs%2Fheads%2Fdebian;hp=6125ec3fa570491f67f060f3c7180bf9fdbce0e0;hpb=db5f0297bf4840b85d37e3460e705f5344928a98;p=fw%2Fstlink diff --git a/src/stlink-usb.c b/src/stlink-usb.c index 6125ec3..9865412 100644 --- a/src/stlink-usb.c +++ b/src/stlink-usb.c @@ -743,6 +743,10 @@ stlink_t *stlink_open_usb(enum ugly_loglevel verbose, bool reset, char serial[16 int devBus =0; int devAddr=0; + /* @TODO: Reading a environment variable in a usb open function is not very nice, this + should be refactored and moved into the CLI tools, and instead of giving USB_BUS:USB_ADDR a real stlink + serial string should be passed to this function. Probably people are using this but this is very odd because + as programmer can change to multiple busses and it is better to detect them based on serial. */ char *device = getenv("STLINK_DEVICE"); if (device) { char *c = strchr(device,':'); @@ -756,31 +760,38 @@ stlink_t *stlink_open_usb(enum ugly_loglevel verbose, bool reset, char serial[16 ILOG("bus %03d dev %03d\n",devBus, devAddr); } - while (cnt--){ + while (cnt--) { libusb_get_device_descriptor( list[cnt], &desc ); if (desc.idVendor != USB_ST_VID) continue; - if (devBus && devAddr) - if ((libusb_get_bus_number(list[cnt])!=devBus) || (libusb_get_device_address(list[cnt])!=devAddr)) + if (devBus && devAddr) { + if ((libusb_get_bus_number(list[cnt]) != devBus) + || (libusb_get_device_address(list[cnt]) != devAddr)) { continue; - - if ( (desc.idProduct == USB_STLINK_32L_PID) || (desc.idProduct == USB_STLINK_NUCLEO_PID) ) { - if ((serial != NULL)) { - struct libusb_device_handle *handle; - - libusb_open(list[cnt], &handle); - sl->serial_size = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, (unsigned char *)sl->serial, sizeof(sl->serial)); - libusb_close(handle); - if (sl->serial_size < 0) - continue; - else if (memcmp(serial, &sl->serial, sl->serial_size) == 0) - break; - } else { - break; } } + if ((desc.idProduct == USB_STLINK_32L_PID) || (desc.idProduct == USB_STLINK_NUCLEO_PID)) { + struct libusb_device_handle *handle; + + libusb_open(list[cnt], &handle); + sl->serial_size = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, + (unsigned char *)sl->serial, sizeof(sl->serial)); + libusb_close(handle); + + if ((serial == NULL) || (*serial == 0)) + break; + + if (sl->serial_size < 0) + continue; + + if (memcmp(serial, &sl->serial, sl->serial_size) == 0) + break; + + continue; + } + if (desc.idProduct == USB_STLINK_PID) { slu->protocoll = 1; break; @@ -825,7 +836,7 @@ stlink_t *stlink_open_usb(enum ugly_loglevel verbose, bool reset, char serial[16 } if (libusb_claim_interface(slu->usb_handle, 0)) { - WLOG("libusb_claim_interface() failed\n"); + WLOG("Stlink usb device found, but unable to claim (probably already in use?)\n"); goto on_libusb_error; } @@ -933,7 +944,11 @@ static size_t stlink_probe_usb_devs(libusb_device **devs, stlink_t **sldevs[]) { WLOG("failed to get libusb device descriptor\n"); break; } - libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, (unsigned char *)&serial, sizeof(serial)); + + ret = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, (unsigned char *)&serial, sizeof(serial)); + if (ret < 0) + *serial = NULL; + libusb_close(handle); stlink_t *sl = NULL;