@@ -301,16 +301,7 @@ static const char *find_usbfs_path(void)
301301 }
302302 }
303303
304- /* On udev based systems without any usb-devices /dev/bus/usb will not
305- * exist. So if we've not found anything and we're using udev for hotplug
306- * simply assume /dev/bus/usb rather then making libusb_init fail.
307- * Make the same assumption for Android where SELinux policies might block us
308- * from reading /dev on newer devices. */
309- #if defined(HAVE_LIBUDEV ) || defined(__ANDROID__ )
310- return USB_DEVTMPFS_PATH ;
311- #else
312304 return NULL ;
313- #endif
314305}
315306
316307static int get_kernel_version (struct libusb_context * ctx ,
@@ -378,12 +369,16 @@ static int op_init(struct libusb_context *ctx)
378369
379370 usbfs_path = find_usbfs_path ();
380371 if (!usbfs_path ) {
381- usbi_err (ctx , "could not find usbfs" );
382- return LIBUSB_ERROR_OTHER ;
372+ /* On udev based systems without any usb devices /dev/bus/usb will not
373+ * exist. On android and other systems SELinux policies might block us
374+ * from reading /dev. Nonetheless it's the default for most modern linux
375+ systems including those using udev alternatives such as busybox's mdev. */
376+ usbfs_path = USB_DEVTMPFS_PATH ;
377+ usbi_dbg (ctx , "could not find usbfs, defaulting to %s" , usbfs_path );
378+ } else {
379+ usbi_dbg (ctx , "found usbfs at %s" , usbfs_path );
383380 }
384381
385- usbi_dbg (ctx , "found usbfs at %s" , usbfs_path );
386-
387382 if (!max_iso_packet_len ) {
388383 if (kernel_version_ge (& kversion , 5 , 2 , 0 ))
389384 max_iso_packet_len = 98304 ;
@@ -1302,6 +1297,10 @@ static int usbfs_get_device_list(struct libusb_context *ctx)
13021297 buses = opendir (USB_DEVTMPFS_PATH );
13031298
13041299 if (!buses ) {
1300+ if (!usbdev_names && errno == ENOENT ) {
1301+ /* The path does not exist if there are no devices plugged in */
1302+ return LIBUSB_SUCCESS ;
1303+ }
13051304 usbi_err (ctx , "opendir buses failed, errno=%d" , errno );
13061305 return LIBUSB_ERROR_IO ;
13071306 }
@@ -1379,10 +1378,10 @@ static int linux_default_scan_devices(struct libusb_context *ctx)
13791378 * any autosuspended USB devices. however, sysfs is not available
13801379 * everywhere, so we need a usbfs fallback too.
13811380 */
1382- if (sysfs_available )
1383- return sysfs_get_device_list ( ctx ) ;
1384- else
1385- return usbfs_get_device_list (ctx );
1381+ if (sysfs_available && sysfs_get_device_list ( ctx ) == LIBUSB_SUCCESS )
1382+ return LIBUSB_SUCCESS ;
1383+
1384+ return usbfs_get_device_list (ctx );
13861385}
13871386#endif
13881387
0 commit comments