Skip to content

Commit

Permalink
usb: don't error during a scan on linux when there are no usb hosts.
Browse files Browse the repository at this point in the history
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 <rgetz@mathworks.com>
  • Loading branch information
rgetz authored and pcercuei committed Feb 22, 2023
1 parent daddd3f commit fdecc36
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion usb.c
Expand Up @@ -11,6 +11,9 @@
#include "iiod-client.h"

#include <ctype.h>
#ifdef __linux__
#include <dirent.h>
#endif
#include <errno.h>
#include <libusb.h>
#include <stdbool.h>
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit fdecc36

Please sign in to comment.