From fdecc3622738580f047fbda0c265d86e06175691 Mon Sep 17 00:00:00 2001 From: Robin Getz Date: Mon, 20 Feb 2023 10:02:15 -0500 Subject: [PATCH] usb: don't error during a scan on linux when there are no usb hosts. libiio scans via libusb when the usb backend is built in. For many embedded linux targets that support On The Go (OTG) the USB port can be configured as device or host. Sometimes, end users may want to scan for local devices when the OTG is configured as a device (and there aren't any USB hosts). The issue with this is libusb returns an error when there are not any hosts. So - on linux - catch that, and override the error handling. New functionality is when Linux's OTG USB is in device mode, usb scans can still take place. This fixes issue #925 (iio_attr -a fails on plutoSDR). Signed-off-by: Robin Getz --- usb.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/usb.c b/usb.c index e12fb332f..82ab7365b 100644 --- a/usb.c +++ b/usb.c @@ -11,6 +11,9 @@ #include "iiod-client.h" #include +#ifdef __linux__ +#include +#endif #include #include #include @@ -1253,8 +1256,20 @@ int usb_context_scan(struct iio_scan_result *scan_result, const char *args) return ret; ret = libusb_init(&ctx); - if (ret < 0) + if (ret < 0) { +#ifdef __linux__ + /* When Linux's OTG USB is in device mode, and there are no hosts, + * libusb_init() is expected to fail, but we shouldn't treat that + * as a hard failure - only that there are no devices. + */ + DIR* dir = opendir("/dev/bus/usb/"); + if (dir) + closedir(dir); + else if (errno == ENOENT) + return 0; +#endif return -(int) libusb_to_errno(ret); + } ret = (int) libusb_get_device_list(ctx, &device_list); if (ret < 0) {