From 377f513da146b4e1227689cae613a261b8fbd74c Mon Sep 17 00:00:00 2001 From: Chris Dickens Date: Thu, 17 Sep 2015 22:27:20 -0700 Subject: [PATCH] linux: Filter hotplug events by DEVTYPE of "usb_device" Many different hotplug events can occur for the "usb" subsystem (e.g. attaching/detaching a class driver), but libusb only needs to care about events where the device type is "usb_device". All other events cause needless attempts to lookup the device address, which will fail as the paths for such events do not correspond to actual USB device nodes. Signed-off-by: Chris Dickens --- libusb/os/linux_netlink.c | 7 +++++++ libusb/os/linux_udev.c | 3 ++- libusb/version_nano.h | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/libusb/os/linux_netlink.c b/libusb/os/linux_netlink.c index 7a30561..72d3589 100644 --- a/libusb/os/linux_netlink.c +++ b/libusb/os/linux_netlink.c @@ -222,6 +222,13 @@ static int linux_netlink_parse(char *buffer, size_t len, int *detached, const ch return -1; } + /* check that this is an actual usb device */ + tmp = netlink_message_parse(buffer, len, "DEVTYPE"); + if (NULL == tmp || 0 != strcmp(tmp, "usb_device")) { + /* not usb. ignore */ + return -1; + } + tmp = netlink_message_parse(buffer, len, "BUSNUM"); if (NULL == tmp) { /* no bus number. try "DEVICE" */ diff --git a/libusb/os/linux_udev.c b/libusb/os/linux_udev.c index 6577240..b63db98 100644 --- a/libusb/os/linux_udev.c +++ b/libusb/os/linux_udev.c @@ -69,7 +69,7 @@ int linux_udev_start_event_monitor(void) goto err_free_ctx; } - r = udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "usb", 0); + r = udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "usb", "usb_device"); if (r) { usbi_err(NULL, "could not initialize udev monitor filter for \"usb\" subsystem"); goto err_free_monitor; @@ -266,6 +266,7 @@ int linux_udev_scan_devices(struct libusb_context *ctx) } udev_enumerate_add_match_subsystem(enumerator, "usb"); + udev_enumerate_add_match_property(enumerator, "DEVTYPE", "usb_device"); udev_enumerate_scan_devices(enumerator); devices = udev_enumerate_get_list_entry(enumerator); diff --git a/libusb/version_nano.h b/libusb/version_nano.h index 3adf53e..112b99f 100644 --- a/libusb/version_nano.h +++ b/libusb/version_nano.h @@ -1 +1 @@ -#define LIBUSB_NANO 11007 +#define LIBUSB_NANO 11008 -- 2.34.1