From 9cb5bd5f1cf70cad1d20bce43f556320ea419005 Mon Sep 17 00:00:00 2001 From: hselasky Date: Wed, 22 Jun 2016 10:38:41 +0000 Subject: Implement libusb_hotplug_register_callback() and libusb_hotplug_deregister_callback() for the LibUSB v1.0 API and update the libusb(3) manual page. Approved by: re (kib) Requested by: swills MFC after: 1 week --- lib/libusb/Makefile | 3 + lib/libusb/libusb.3 | 43 +++++++- lib/libusb/libusb.h | 22 ++++ lib/libusb/libusb10.c | 27 +++++ lib/libusb/libusb10.h | 18 ++++ lib/libusb/libusb10_hotplug.c | 237 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 349 insertions(+), 1 deletion(-) create mode 100644 lib/libusb/libusb10_hotplug.c diff --git a/lib/libusb/Makefile b/lib/libusb/Makefile index bdeaef6..adc5179 100644 --- a/lib/libusb/Makefile +++ b/lib/libusb/Makefile @@ -32,6 +32,7 @@ SRCS+= libusb01.c INCS+= libusb.h SRCS+= libusb10.c SRCS+= libusb10_desc.c +SRCS+= libusb10_hotplug.c SRCS+= libusb10_io.c .if defined(COMPAT_32BIT) @@ -142,6 +143,8 @@ MLINKS += libusb.3 libusb_handle_events_locked.3 MLINKS += libusb.3 libusb_get_next_timeout.3 MLINKS += libusb.3 libusb_set_pollfd_notifiers.3 MLINKS += libusb.3 libusb_get_pollfds.3 +MLINKS += libusb.3 libusb_hotplug_register_callback.3 +MLINKS += libusb.3 libusb_hotplug_deregister_callback.3 # LibUSB v0.1 MLINKS += libusb.3 usb_open.3 diff --git a/lib/libusb/libusb.3 b/lib/libusb/libusb.3 index 0ccb95c..4950a25 100644 --- a/lib/libusb/libusb.3 +++ b/lib/libusb/libusb.3 @@ -26,7 +26,7 @@ .\" .\" $FreeBSD$ .\" -.Dd June 17, 2016 +.Dd June 22, 2016 .Dt LIBUSB 3 .Os .Sh NAME @@ -642,6 +642,47 @@ that libusb uses as an event source. Retrive a list of file descriptors that should be polled by your main loop as libusb event sources. Returns a NULL-terminated list on success or NULL on failure. +.Pp +.Ft int +.Fn libusb_hotplug_register_callback "libusb_context *ctx" "libusb_hotplug_event events" "libusb_hotplug_flag flags" "int vendor_id" "int product_id" "int dev_class" "libusb_hotplug_callback_fn cb_fn" "void *user_data" "libusb_hotplug_callback_handle *handle" +This function registers a hotplug filter. +The +.Fa events +argument select which events makes the hotplug filter trigger. +Available event values are LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED and LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT. +One or more events must be specified. +The +.Fa vendor_id , +.Fa product_id +and +.Fa dev_class +arguments can be set to LIBUSB_HOTPLUG_MATCH_ANY to match any value in the USB device descriptor. +Else the specified value is used for matching. +If the +.Fa flags +argument is set to LIBUSB_HOTPLUG_ENUMERATE, all currently attached and matching USB devices will be passed to the hotplug filter, given by the +.Fa cb_fn +argument. +Else the +.Fa flags +argument should be set to LIBUSB_HOTPLUG_NO_FLAGS. +This function returns 0 upon success or a LIBUSB_ERROR code on failure. +.Pp +.Ft int +.Fn libusb_hotplug_callback_fn "libusb_context *ctx" "libusb_device *device" "libusb_hotplug_event event" "void *user_data" +The hotplug filter function. +If this function returns non-zero, the filter is removed. +Else the filter is kept and can receive more events. +The +.Fa user_data +argument is the same as given when the filter was registered. +The +.Fa event +argument can be either of LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED or LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT. +.Pp +.Ft void +.Fn libusb_hotplug_deregister_callback "libusb_context *ctx" "libusb_hotplug_callback_handle handle" +This function unregisters a hotplug filter. .Sh LIBUSB VERSION 0.1 COMPATIBILITY The library is also compliant with LibUSB version 0.1.12. .Pp diff --git a/lib/libusb/libusb.h b/lib/libusb/libusb.h index d446830..1738301 100644 --- a/lib/libusb/libusb.h +++ b/lib/libusb/libusb.h @@ -236,12 +236,25 @@ enum libusb_debug_level { LIBUSB_DEBUG_TRANSFER=2, }; +#define LIBUSB_HOTPLUG_MATCH_ANY -1 + +typedef enum { + LIBUSB_HOTPLUG_NO_FLAGS = 0, + LIBUSB_HOTPLUG_ENUMERATE = 1 << 0, +} libusb_hotplug_flag; + +typedef enum { + LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED = 1, + LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT = 2, +} libusb_hotplug_event; + /* libusb structures */ struct libusb_context; struct libusb_device; struct libusb_transfer; struct libusb_device_handle; +struct libusb_hotplug_callback_handle; struct libusb_pollfd { int fd; @@ -263,6 +276,7 @@ typedef struct libusb_device_handle libusb_device_handle; typedef struct libusb_pollfd libusb_pollfd; typedef void (*libusb_pollfd_added_cb) (int fd, short events, void *user_data); typedef void (*libusb_pollfd_removed_cb) (int fd, void *user_data); +typedef struct libusb_hotplug_callback_handle *libusb_hotplug_callback_handle; typedef struct libusb_device_descriptor { uint8_t bLength; @@ -539,6 +553,14 @@ int libusb_interrupt_transfer(libusb_device_handle * devh, uint8_t endpoint, uin uint16_t libusb_cpu_to_le16(uint16_t x); uint16_t libusb_le16_to_cpu(uint16_t x); +/* Hotplug support */ + +typedef int (*libusb_hotplug_callback_fn)(libusb_context *ctx, + libusb_device *device, libusb_hotplug_event event, void *user_data); + +int libusb_hotplug_register_callback(libusb_context *ctx, libusb_hotplug_event events, libusb_hotplug_flag flags, int vendor_id, int product_id, int dev_class, libusb_hotplug_callback_fn cb_fn, void *user_data, libusb_hotplug_callback_handle *handle); +void libusb_hotplug_deregister_callback(libusb_context *ctx, libusb_hotplug_callback_handle handle); + #if 0 { /* indent fix */ #endif diff --git a/lib/libusb/libusb10.c b/lib/libusb/libusb10.c index 7b336c9..c6eb771 100644 --- a/lib/libusb/libusb10.c +++ b/lib/libusb/libusb10.c @@ -134,24 +134,34 @@ libusb_init(libusb_context **context) } TAILQ_INIT(&ctx->pollfds); TAILQ_INIT(&ctx->tr_done); + TAILQ_INIT(&ctx->hotplug_cbh); + TAILQ_INIT(&ctx->hotplug_devs); if (pthread_mutex_init(&ctx->ctx_lock, NULL) != 0) { free(ctx); return (LIBUSB_ERROR_NO_MEM); } + if (pthread_mutex_init(&ctx->hotplug_lock, NULL) != 0) { + pthread_mutex_destroy(&ctx->ctx_lock); + free(ctx); + return (LIBUSB_ERROR_NO_MEM); + } if (pthread_condattr_init(&attr) != 0) { pthread_mutex_destroy(&ctx->ctx_lock); + pthread_mutex_destroy(&ctx->hotplug_lock); free(ctx); return (LIBUSB_ERROR_NO_MEM); } if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC) != 0) { pthread_mutex_destroy(&ctx->ctx_lock); + pthread_mutex_destroy(&ctx->hotplug_lock); pthread_condattr_destroy(&attr); free(ctx); return (LIBUSB_ERROR_OTHER); } if (pthread_cond_init(&ctx->ctx_cond, &attr) != 0) { pthread_mutex_destroy(&ctx->ctx_lock); + pthread_mutex_destroy(&ctx->hotplug_lock); pthread_condattr_destroy(&attr); free(ctx); return (LIBUSB_ERROR_NO_MEM); @@ -159,10 +169,12 @@ libusb_init(libusb_context **context) pthread_condattr_destroy(&attr); ctx->ctx_handler = NO_THREAD; + ctx->hotplug_handler = NO_THREAD; ret = pipe(ctx->ctrl_pipe); if (ret < 0) { pthread_mutex_destroy(&ctx->ctx_lock); + pthread_mutex_destroy(&ctx->hotplug_lock); pthread_cond_destroy(&ctx->ctx_cond); free(ctx); return (LIBUSB_ERROR_OTHER); @@ -195,12 +207,27 @@ libusb_exit(libusb_context *ctx) if (ctx == NULL) return; + /* stop hotplug thread, if any */ + + if (ctx->hotplug_handler != NO_THREAD) { + pthread_t td; + void *ptr; + + HOTPLUG_LOCK(ctx); + td = ctx->hotplug_handler; + ctx->hotplug_handler = NO_THREAD; + HOTPLUG_UNLOCK(ctx); + + pthread_join(td, &ptr); + } + /* XXX cleanup devices */ libusb10_remove_pollfd(ctx, &ctx->ctx_poll); close(ctx->ctrl_pipe[0]); close(ctx->ctrl_pipe[1]); pthread_mutex_destroy(&ctx->ctx_lock); + pthread_mutex_destroy(&ctx->hotplug_lock); pthread_cond_destroy(&ctx->ctx_cond); pthread_mutex_lock(&default_context_lock); diff --git a/lib/libusb/libusb10.h b/lib/libusb/libusb10.h index f1e5460..d2df6e0 100644 --- a/lib/libusb/libusb10.h +++ b/lib/libusb/libusb10.h @@ -36,6 +36,8 @@ #define CTX_LOCK(ctx) pthread_mutex_lock(&(ctx)->ctx_lock) #define CTX_TRYLOCK(ctx) pthread_mutex_trylock(&(ctx)->ctx_lock) #define CTX_UNLOCK(ctx) pthread_mutex_unlock(&(ctx)->ctx_lock) +#define HOTPLUG_LOCK(ctx) pthread_mutex_lock(&(ctx)->hotplug_lock) +#define HOTPLUG_UNLOCK(ctx) pthread_mutex_unlock(&(ctx)->hotplug_lock) #define DPRINTF(ctx, dbg, format, args...) do { \ if ((ctx)->debug == dbg) { \ @@ -72,6 +74,16 @@ struct libusb_super_transfer { #define LIBUSB_SUPER_XFER_ST_PEND 1 }; +struct libusb_hotplug_callback_handle { + TAILQ_ENTRY(libusb_hotplug_callback_handle) entry; + int events; + int vendor; + int product; + int devclass; + libusb_hotplug_callback_fn fn; + void *user_data; +}; + struct libusb_context { int debug; int debug_fixed; @@ -80,12 +92,16 @@ struct libusb_context { int tr_done_gen; pthread_mutex_t ctx_lock; + pthread_mutex_t hotplug_lock; pthread_cond_t ctx_cond; + pthread_t hotplug_handler; pthread_t ctx_handler; #define NO_THREAD ((pthread_t)-1) TAILQ_HEAD(, libusb_super_pollfd) pollfds; TAILQ_HEAD(, libusb_super_transfer) tr_done; + TAILQ_HEAD(, libusb_hotplug_callback_handle) hotplug_cbh; + TAILQ_HEAD(, libusb_device) hotplug_devs; struct libusb_super_pollfd ctx_poll; @@ -103,6 +119,8 @@ struct libusb_device { struct libusb_context *ctx; + TAILQ_ENTRY(libusb_device) hotplug_entry; + TAILQ_HEAD(, libusb_super_transfer) tr_head; struct libusb20_device *os_priv; diff --git a/lib/libusb/libusb10_hotplug.c b/lib/libusb/libusb10_hotplug.c new file mode 100644 index 0000000..162cf2b --- /dev/null +++ b/lib/libusb/libusb10_hotplug.c @@ -0,0 +1,237 @@ +/* $FreeBSD$ */ +/*- + * Copyright (c) 2016 Hans Petter Selasky. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifdef LIBUSB_GLOBAL_INCLUDE_FILE +#include LIBUSB_GLOBAL_INCLUDE_FILE +#else +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#endif + +#define libusb_device_handle libusb20_device + +#include "libusb20.h" +#include "libusb20_desc.h" +#include "libusb20_int.h" +#include "libusb.h" +#include "libusb10.h" + +static int +libusb_hotplug_equal(libusb_device *_adev, libusb_device *_bdev) +{ + struct libusb20_device *adev = _adev->os_priv; + struct libusb20_device *bdev = _bdev->os_priv; + + if (adev->bus_number != bdev->bus_number) + return (0); + if (adev->device_address != bdev->device_address) + return (0); + if (memcmp(&adev->ddesc, &bdev->ddesc, sizeof(adev->ddesc))) + return (0); + if (memcmp(&adev->session_data, &bdev->session_data, sizeof(adev->session_data))) + return (0); + return (1); +} + +static int +libusb_hotplug_filter(libusb_context *ctx, libusb_hotplug_callback_handle pcbh, + libusb_device *dev, libusb_hotplug_event event) +{ + if (!(pcbh->events & event)) + return (0); + if (pcbh->vendor != LIBUSB_HOTPLUG_MATCH_ANY && + pcbh->vendor != libusb20_dev_get_device_desc(dev->os_priv)->idVendor) + return (0); + if (pcbh->product != LIBUSB_HOTPLUG_MATCH_ANY && + pcbh->product != libusb20_dev_get_device_desc(dev->os_priv)->idProduct) + return (0); + if (pcbh->devclass != LIBUSB_HOTPLUG_MATCH_ANY && + pcbh->devclass != libusb20_dev_get_device_desc(dev->os_priv)->bDeviceClass) + return (0); + return (pcbh->fn(ctx, dev, event, pcbh->user_data)); +} + +static void * +libusb_hotplug_scan(void *arg) +{ + TAILQ_HEAD(, libusb_device) hotplug_devs; + libusb_hotplug_callback_handle acbh; + libusb_hotplug_callback_handle bcbh; + libusb_context *ctx = arg; + libusb_device **ppdev; + libusb_device *temp; + libusb_device *adev; + libusb_device *bdev; + unsigned do_loop = 1; + ssize_t count; + ssize_t x; + + while (do_loop) { + usleep(4000000); + + HOTPLUG_LOCK(ctx); + + TAILQ_INIT(&hotplug_devs); + + if (ctx->hotplug_handler != NO_THREAD) { + count = libusb_get_device_list(ctx, &ppdev); + if (count < 0) + continue; + for (x = 0; x != count; x++) { + TAILQ_INSERT_TAIL(&hotplug_devs, ppdev[x], + hotplug_entry); + } + libusb_free_device_list(ppdev, 0); + } else { + do_loop = 0; + } + + /* figure out which devices are gone */ + TAILQ_FOREACH_SAFE(adev, &ctx->hotplug_devs, hotplug_entry, temp) { + TAILQ_FOREACH(bdev, &hotplug_devs, hotplug_entry) { + if (libusb_hotplug_equal(adev, bdev)) + break; + } + if (bdev == NULL) { + TAILQ_REMOVE(&ctx->hotplug_devs, adev, hotplug_entry); + TAILQ_FOREACH_SAFE(acbh, &ctx->hotplug_cbh, entry, bcbh) { + if (libusb_hotplug_filter(ctx, acbh, adev, + LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT) == 0) + continue; + TAILQ_REMOVE(&ctx->hotplug_cbh, acbh, entry); + free(acbh); + } + libusb_unref_device(adev); + } + } + + /* figure out which devices are new */ + TAILQ_FOREACH_SAFE(adev, &hotplug_devs, hotplug_entry, temp) { + TAILQ_FOREACH(bdev, &ctx->hotplug_devs, hotplug_entry) { + if (libusb_hotplug_equal(adev, bdev)) + break; + } + if (bdev == NULL) { + TAILQ_REMOVE(&hotplug_devs, adev, hotplug_entry); + TAILQ_INSERT_TAIL(&ctx->hotplug_devs, adev, hotplug_entry); + TAILQ_FOREACH_SAFE(acbh, &ctx->hotplug_cbh, entry, bcbh) { + if (libusb_hotplug_filter(ctx, acbh, adev, + LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED) == 0) + continue; + TAILQ_REMOVE(&ctx->hotplug_cbh, acbh, entry); + free(acbh); + } + } + } + HOTPLUG_UNLOCK(ctx); + + /* unref remaining devices */ + while ((adev = TAILQ_FIRST(&hotplug_devs)) != NULL) { + TAILQ_REMOVE(&hotplug_devs, adev, hotplug_entry); + libusb_unref_device(adev); + } + } + return (NULL); +} + +int libusb_hotplug_register_callback(libusb_context *ctx, + libusb_hotplug_event events, libusb_hotplug_flag flags, + int vendor_id, int product_id, int dev_class, + libusb_hotplug_callback_fn cb_fn, void *user_data, + libusb_hotplug_callback_handle *phandle) +{ + libusb_hotplug_callback_handle handle; + struct libusb_device *adev; + + ctx = GET_CONTEXT(ctx); + + if (ctx == NULL || cb_fn == NULL || events == 0 || + vendor_id < -1 || vendor_id > 0xffff || + product_id < -1 || product_id > 0xffff || + dev_class < -1 || dev_class > 0xff) + return (LIBUSB_ERROR_INVALID_PARAM); + + handle = malloc(sizeof(*handle)); + if (handle == NULL) + return (LIBUSB_ERROR_NO_MEM); + + HOTPLUG_LOCK(ctx); + if (ctx->hotplug_handler == NO_THREAD) { + if (pthread_create(&ctx->hotplug_handler, NULL, + &libusb_hotplug_scan, ctx) != 0) + ctx->hotplug_handler = NO_THREAD; + } + handle->events = events; + handle->vendor = vendor_id; + handle->product = product_id; + handle->devclass = dev_class; + handle->fn = cb_fn; + handle->user_data = user_data; + + if (flags & LIBUSB_HOTPLUG_ENUMERATE) { + TAILQ_FOREACH(adev, &ctx->hotplug_devs, hotplug_entry) { + if (libusb_hotplug_filter(ctx, handle, adev, + LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED) == 0) + continue; + free(handle); + handle = NULL; + break; + } + } + if (handle != NULL) + TAILQ_INSERT_TAIL(&ctx->hotplug_cbh, handle, entry); + HOTPLUG_UNLOCK(ctx); + + if (phandle != NULL) + *phandle = handle; + return (LIBUSB_SUCCESS); +} + +void libusb_hotplug_deregister_callback(libusb_context *ctx, + libusb_hotplug_callback_handle handle) +{ + ctx = GET_CONTEXT(ctx); + + if (ctx == NULL || handle == NULL) + return; + + HOTPLUG_LOCK(ctx); + TAILQ_REMOVE(&ctx->hotplug_cbh, handle, entry); + HOTPLUG_UNLOCK(ctx); + + free(handle); +} -- cgit v1.1