From 4e799223574155c3f472937f10ef8c6c5ebcb0f8 Mon Sep 17 00:00:00 2001 From: Chris Dickens Date: Thu, 28 Dec 2017 00:40:14 -0800 Subject: [PATCH] linux_usbfs: libusb_init() should succeed if no devices are present When using sysfs to scan for devices, libusb_init() will fail if there are no USB devices present. There is no reason for this behavior, so this commit modifies the logic to only return an error if one or more devices are present but none could be successfully enumerated. Closes #301 Signed-off-by: Chris Dickens --- libusb/os/linux_usbfs.c | 16 ++++++++++++---- libusb/version_nano.h | 2 +- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/libusb/os/linux_usbfs.c b/libusb/os/linux_usbfs.c index 96fe07c79..31ff219e7 100644 --- a/libusb/os/linux_usbfs.c +++ b/libusb/os/linux_usbfs.c @@ -1299,11 +1299,12 @@ static int sysfs_get_device_list(struct libusb_context *ctx) { DIR *devices = opendir(SYSFS_DEVICE_PATH); struct dirent *entry; - int r = LIBUSB_ERROR_IO; + int num_devices = 0; + int num_enumerated = 0; if (!devices) { usbi_err(ctx, "opendir devices failed errno=%d", errno); - return r; + return LIBUSB_ERROR_IO; } while ((entry = readdir(devices))) { @@ -1311,16 +1312,23 @@ static int sysfs_get_device_list(struct libusb_context *ctx) || strchr(entry->d_name, ':')) continue; + num_devices++; + if (sysfs_scan_device(ctx, entry->d_name)) { usbi_dbg("failed to enumerate dir entry %s", entry->d_name); continue; } - r = 0; + num_enumerated++; } closedir(devices); - return r; + + /* successful if at least one device was enumerated or no devices were found */ + if (num_enumerated || !num_devices) + return LIBUSB_SUCCESS; + else + return LIBUSB_ERROR_IO; } static int linux_default_scan_devices (struct libusb_context *ctx) diff --git a/libusb/version_nano.h b/libusb/version_nano.h index 423c3df52..61232e844 100644 --- a/libusb/version_nano.h +++ b/libusb/version_nano.h @@ -1 +1 @@ -#define LIBUSB_NANO 11232 +#define LIBUSB_NANO 11233