hosted/stlinkv2: Fix !found causing hosted to continue on anyway, and improved the error reporting from the device finder loop

This commit is contained in:
dragonmux 2022-04-14 14:19:44 -04:00 committed by Rachel Mant
parent 4287f1ba0e
commit ba8ed132a8

View File

@ -510,21 +510,26 @@ int stlink_init(bmp_info_t *info)
bool found = false; bool found = false;
while ((dev = devs[i++]) != NULL) { while ((dev = devs[i++]) != NULL) {
struct libusb_device_descriptor desc; struct libusb_device_descriptor desc;
int r = libusb_get_device_descriptor(dev, &desc); int result = libusb_get_device_descriptor(dev, &desc);
if (r < 0) { if (result != LIBUSB_SUCCESS) {
DEBUG_WARN("libusb_get_device_descriptor failed %s", DEBUG_WARN("libusb_get_device_descriptor failed %s",
libusb_strerror(r)); libusb_strerror(result));
return -1; return -1;
} }
if ((desc.idVendor != info->vid) || if (desc.idVendor != info->vid ||
(desc.idProduct != info->pid) || desc.idProduct != info->pid) {
(libusb_open(dev, &sl->ul_libusb_device_handle) continue;
!= LIBUSB_SUCCESS)) { }
if ((result = libusb_open(dev, &sl->ul_libusb_device_handle)) != LIBUSB_SUCCESS)
{
DEBUG_WARN("Failed to open STLink device %#06x:%#06x - %s",
desc.idVendor, desc.idProduct, libusb_strerror(result));
DEBUG_WARN("Are you sure the permissions on the device are set correctly?");
continue; continue;
} }
char serial[64]; char serial[64];
if (desc.iSerialNumber) { if (desc.iSerialNumber) {
const int result = libusb_get_string_descriptor_ascii(sl->ul_libusb_device_handle, int result = libusb_get_string_descriptor_ascii(sl->ul_libusb_device_handle,
desc.iSerialNumber, (uint8_t *)serial, sizeof(serial)); desc.iSerialNumber, (uint8_t *)serial, sizeof(serial));
/* If the call fails and it's not because the device gave us STALL, continue to the next one */ /* If the call fails and it's not because the device gave us STALL, continue to the next one */
if (result < 0 && result != LIBUSB_ERROR_PIPE) { if (result < 0 && result != LIBUSB_ERROR_PIPE) {
@ -546,7 +551,7 @@ int stlink_init(bmp_info_t *info)
} }
libusb_free_device_list(devs, 1); libusb_free_device_list(devs, 1);
if (!found) if (!found)
return 0; return 1;
if (info->vid != VENDOR_ID_STLINK) if (info->vid != VENDOR_ID_STLINK)
return 0; return 0;
switch (info->pid) { switch (info->pid) {