Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F164763729
D48300.id148858.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
4 KB
Referenced Files
None
Subscribers
None
D48300.id148858.diff
View Options
diff --git a/lib/libusb/libusb10.h b/lib/libusb/libusb10.h
--- a/lib/libusb/libusb10.h
+++ b/lib/libusb/libusb10.h
@@ -30,6 +30,12 @@
#ifndef LIBUSB_GLOBAL_INCLUDE_FILE
#include <sys/queue.h>
+#include <netlink/netlink.h>
+#include <netlink/netlink_generic.h>
+#include <netlink/netlink_snl.h>
+#include <netlink/netlink_snl_generic.h>
+#include <netlink/netlink_sysevent.h>
+
#endif
#define GET_CONTEXT(ctx) (((ctx) == NULL) ? usbi_default_context : (ctx))
@@ -90,12 +96,22 @@
TAILQ_HEAD(libusb_device_head, libusb_device);
+typedef enum {
+ usb_event_none,
+ usb_event_scan,
+ usb_event_devd,
+ usb_event_netlink
+} usb_event_mode_t;
+
struct libusb_context {
int debug;
int debug_fixed;
int ctrl_pipe[2];
int tr_done_ref;
int tr_done_gen;
+ usb_event_mode_t usb_event_mode;
+ int devd_pipe;
+ struct snl_state ss;
pthread_mutex_t ctx_lock;
pthread_mutex_t hotplug_lock;
diff --git a/lib/libusb/libusb10.c b/lib/libusb/libusb10.c
--- a/lib/libusb/libusb10.c
+++ b/lib/libusb/libusb10.c
@@ -155,6 +155,7 @@
return (LIBUSB_ERROR_INVALID_PARAM);
memset(ctx, 0, sizeof(*ctx));
+ ctx->devd_pipe = -1;
debug = getenv("LIBUSB_DEBUG");
if (debug != NULL) {
diff --git a/lib/libusb/libusb10_hotplug.c b/lib/libusb/libusb10_hotplug.c
--- a/lib/libusb/libusb10_hotplug.c
+++ b/lib/libusb/libusb10_hotplug.c
@@ -23,6 +23,7 @@
* SUCH DAMAGE.
*/
+#include <netlink/netlink_snl_generic.h>
#ifdef LIBUSB_GLOBAL_INCLUDE_FILE
#include LIBUSB_GLOBAL_INCLUDE_FILE
#else
@@ -39,6 +40,10 @@
#include <sys/ioctl.h>
#include <sys/queue.h>
#include <sys/endian.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <sys/module.h>
+#include <sys/linker.h>
#endif
#define libusb_device_handle libusb20_device
@@ -49,6 +54,61 @@
#include "libusb.h"
#include "libusb10.h"
+#define DEVDPIPE "/var/run/devd.seqpacket.pipe"
+
+static bool
+netlink_init(libusb_context *ctx)
+{
+ struct _getfamily_attrs attrs;
+
+ if (modfind("nlsysevent") < 0)
+ kldload("nlsysevent");
+ if (modfind("nlsysevent") < 0)
+ return (false);
+ if (!snl_init(&ctx->ss, NETLINK_GENERIC))
+ return (false);
+
+ if (!snl_get_genl_family_info(&ctx->ss, "nlsysevent", &attrs))
+ return (false);
+
+ for (unsigned int i = 0; i < attrs.mcast_groups.num_groups; i++) {
+ if (strcmp(attrs.mcast_groups.groups[i]->mcast_grp_name,
+ "USB") == 0) {
+ if (setsockopt(ctx->ss.fd, SOL_NETLINK,
+ NETLINK_ADD_MEMBERSHIP,
+ &attrs.mcast_groups.groups[i]->mcast_grp_id,
+ sizeof(attrs.mcast_groups.groups[i]->mcast_grp_id))
+ == -1) {
+ return (false);
+ }
+ }
+ }
+ ctx->usb_event_mode = usb_event_netlink;
+ return (true);
+}
+
+static bool
+devd_init(libusb_context *ctx)
+{
+ struct sockaddr_un devd_addr;
+
+ bzero(&devd_addr, sizeof(devd_addr));
+ if ((ctx->devd_pipe = socket(PF_LOCAL, SOCK_SEQPACKET|SOCK_NONBLOCK, 0)) < 0)
+ return (false);
+
+ devd_addr.sun_family = PF_LOCAL;
+ strlcpy(devd_addr.sun_path, DEVDPIPE, sizeof(devd_addr.sun_path));
+ if (connect(ctx->devd_pipe, (struct sockaddr *)&devd_addr,
+ sizeof(devd_addr)) == -1) {
+ close(ctx->devd_pipe);
+ ctx->devd_pipe = -1;
+ return (false);
+ }
+
+ ctx->usb_event_mode = usb_event_devd;
+ return (true);
+}
+
static int
libusb_hotplug_equal(libusb_device *_adev, libusb_device *_bdev)
{
@@ -105,6 +165,7 @@
static void *
libusb_hotplug_scan(void *arg)
{
+ struct pollfd pfd[0];
struct libusb_device_head hotplug_devs;
libusb_hotplug_callback_handle acbh;
libusb_hotplug_callback_handle bcbh;
@@ -112,9 +173,24 @@
libusb_device *temp;
libusb_device *adev;
libusb_device *bdev;
+ int timeout = 4000;
+ int nfds;
+ if (ctx->usb_event_mode == usb_event_devd) {
+ pfd[0].fd = ctx->devd_pipe;
+ pfd[0].events = POLLIN;
+ pfd[0].revents = 0;
+ nfds = 1;
+ } else if (ctx->usb_event_mode == usb_event_netlink) {
+ pfd[0].fd = ctx->ss.fd;
+ pfd[0].events = POLLIN;
+ pfd[0].revents = 0;
+ nfds = 1;
+ } else {
+ nfds = 0;
+ }
for (;;) {
- usleep(4000000);
+ (void)poll(pfd, nfds, timeout);
HOTPLUG_LOCK(ctx);
if (ctx->hotplug_handler == NO_THREAD) {
@@ -122,6 +198,10 @@
TAILQ_REMOVE(&ctx->hotplug_devs, adev, hotplug_entry);
libusb_unref_device(adev);
}
+ if (ctx->usb_event_mode == usb_event_devd)
+ close(ctx->devd_pipe);
+ else if (ctx->usb_event_mode == usb_event_netlink)
+ close(ctx->ss.fd);
HOTPLUG_UNLOCK(ctx);
break;
}
@@ -192,6 +272,13 @@
ctx = GET_CONTEXT(ctx);
+ if (ctx->usb_event_mode == usb_event_none) {
+ HOTPLUG_LOCK(ctx);
+ if (!netlink_init(ctx) && !devd_init(ctx))
+ ctx->usb_event_mode = usb_event_scan;
+ HOTPLUG_UNLOCK(ctx);
+ }
+
if (ctx == NULL || cb_fn == NULL || events == 0 ||
vendor_id < -1 || vendor_id > 0xffff ||
product_id < -1 || product_id > 0xffff ||
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Tue, Aug 4, 7:30 PM (4 h, 42 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
35970567
Default Alt Text
D48300.id148858.diff (4 KB)
Attached To
Mode
D48300: libusb: hotplug, use events instead of a timer when possible
Attached
Detach File
Event Timeline
Log In to Comment