The branch main has been updated by aokblast: URL: https://cgit.FreeBSD.org/src/commit/?id=2879c818e5535e9c9dc592714c7d4ef0c1c9e8c1
commit 2879c818e5535e9c9dc592714c7d4ef0c1c9e8c1 Author: ShengYi Hung <[email protected]> AuthorDate: 2025-06-11 19:07:39 +0000 Commit: ShengYi Hung <[email protected]> CommitDate: 2026-06-22 13:36:47 +0000 libusb: implement libusb_set_option Implement libusb_set_option for API compatibility of libusb upstream The implementation status of each option is as following: LIBUSB_OPTION_LOG_LEVEL: just like libusb_set_debug LIBUSB_OPTION_LOG_CB: add callback support for DPRINTF LIBUSB_OPTION_NO_DEVICE_DISCOVERY: disable initialization of devd and netlink when register. Also, create no thread when registration of callback happens. LIBUSB_OPTION_USE_USBDK: no needed as USBDK is for Windows Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D50818 --- lib/libusb/libusb.3 | 24 +++++++++++++ lib/libusb/libusb.h | 9 ++++- lib/libusb/libusb10.c | 78 ++++++++++++++++++++++++++++++++++++++++++- lib/libusb/libusb10.h | 2 ++ lib/libusb/libusb10_hotplug.c | 3 ++ 5 files changed, 114 insertions(+), 2 deletions(-) diff --git a/lib/libusb/libusb.3 b/lib/libusb/libusb.3 index 9b46470ed13d..52b83c700571 100644 --- a/lib/libusb/libusb.3 +++ b/lib/libusb/libusb.3 @@ -101,6 +101,30 @@ and .Fn libusb_attach_kernel_driver . .El .Pp +.Ft int +.Fn libusb_set_option "libusb_context *ctx" "enum libusb_option option" "...value" +This function sets the +.Fa option +from the given +.Fa value . +When the +.Fa ctx +is NULL, the option will be used as the default option for later created +.Fa ctx . +The available options are: +.Bl -tag -width LIBUSB_OPTION -offset indent +.It Va LIBUSB_OPTION_LOG_LEVEL +set the option level to the given +.Fa value . +.It Va LIBUSB_OPTION_USE_USBDK +use USBDK as a libusb backend (Windows only) +.It Va LIBUSB_OPTION_NO_DEVICE_DISCOVER +disable the USB enumeration. +.It Va LIBUSB_OPTION_LOG_CB +customize default callback function with +.Fa value . +.El +.Pp .Ft const char * .Fn libusb_strerror "int code" Get the ASCII representation of the error given by the diff --git a/lib/libusb/libusb.h b/lib/libusb/libusb.h index 5a33dd39d85e..5f8af0eae6cc 100644 --- a/lib/libusb/libusb.h +++ b/lib/libusb/libusb.h @@ -291,7 +291,8 @@ enum libusb_option { LIBUSB_OPTION_USE_USBDK = 1, LIBUSB_OPTION_NO_DEVICE_DISCOVERY = 2, LIBUSB_OPTION_WEAK_AUTHORITY = 2, - LIBUSB_OPTION_MAX = 3, + LIBUSB_OPTION_LOG_CB = 3, + LIBUSB_OPTION_MAX = 4, }; /* libusb structures */ @@ -302,6 +303,11 @@ struct libusb_transfer; struct libusb_device_handle; struct libusb_hotplug_callback_handle_struct; +typedef struct libusb_context libusb_context; + +typedef void (*libusb_log_cb)(libusb_context *ctx, enum libusb_log_level, + const char *str); + struct libusb_pollfd { int fd; short events; @@ -668,6 +674,7 @@ int libusb_alloc_streams(libusb_device_handle *dev, uint32_t num_streams, unsign int libusb_free_streams(libusb_device_handle *dev, unsigned char *endpoints, int num_endpoints); void libusb_transfer_set_stream_id(struct libusb_transfer *transfer, uint32_t stream_id); uint32_t libusb_transfer_get_stream_id(struct libusb_transfer *transfer); +int libusb_set_option(libusb_context *ctx, enum libusb_option option, ...); #if 0 { /* indent fix */ diff --git a/lib/libusb/libusb10.c b/lib/libusb/libusb10.c index 289b56842b5d..9c0f4edb9513 100644 --- a/lib/libusb/libusb10.c +++ b/lib/libusb/libusb10.c @@ -37,6 +37,7 @@ #include <poll.h> #include <pthread.h> #include <signal.h> +#include <stdbool.h> #include <stdarg.h> #include <stdio.h> #include <stdlib.h> @@ -293,6 +294,8 @@ libusb_init_context(libusb_context **context, } libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->event, POLLIN); + ctx->log_cb = NULL; + ctx->no_discovery = false; pthread_mutex_lock(&default_context_lock); if (usbi_default_context == NULL) { @@ -1897,7 +1900,11 @@ libusb_log_va_args(struct libusb_context *ctx, enum libusb_log_level level, snprintf(new_fmt, sizeof(new_fmt), "%s: %s\n", log_prefix[level], fmt); vsnprintf(buffer, sizeof(buffer), new_fmt, args); - fputs(buffer, stdout); + + if (ctx->log_cb != NULL) + ctx->log_cb(ctx, level, buffer); + else + fputs(buffer, stdout); va_end(args); } @@ -1947,3 +1954,72 @@ libusb_wrap_sys_device(libusb_context *ctx, intptr_t sys_dev, { return (LIBUSB_ERROR_NOT_SUPPORTED); } + +int +libusb_set_option(libusb_context *ctx, enum libusb_option option, ...) +{ + int err = LIBUSB_SUCCESS; + enum libusb_log_level level; + va_list args; + libusb_log_cb callback; + + ctx = GET_CONTEXT(ctx); + va_start(args, option); + + switch (option) { + case LIBUSB_OPTION_LOG_LEVEL: + level = va_arg(args, enum libusb_log_level); + if (level < LIBUSB_LOG_LEVEL_NONE || + level > LIBUSB_LOG_LEVEL_DEBUG) { + err = LIBUSB_ERROR_INVALID_PARAM; + goto end; + } + break; + case LIBUSB_OPTION_LOG_CB: + callback = va_arg(args, libusb_log_cb); + break; + } + + if (option >= LIBUSB_OPTION_MAX) { + err = LIBUSB_ERROR_INVALID_PARAM; + goto end; + } + + /* + * When it is default context, the context will be accessed by multiple + * instances of libusb_context that will later be taken as the default + * value when new instances are created. + */ + if (ctx == usbi_default_context) + CTX_LOCK(ctx); + + switch (option) { + case LIBUSB_OPTION_LOG_LEVEL: + if (ctx->debug_fixed) + break; + ctx->debug = level; + break; + case LIBUSB_OPTION_LOG_CB: + ctx->log_cb = callback; + break; + case LIBUSB_OPTION_NO_DEVICE_DISCOVERY: + ctx->no_discovery = true; + break; + /* + * We don't handle USBDK as it is a windows + * backend specified SDK + */ + case LIBUSB_OPTION_USE_USBDK: + break; + default: + err = LIBUSB_ERROR_INVALID_PARAM; + break; + } + + if (ctx == usbi_default_context) + CTX_UNLOCK(ctx); + +end: + va_end(args); + return (err); +} diff --git a/lib/libusb/libusb10.h b/lib/libusb/libusb10.h index eced364ef857..cd90531d1a7b 100644 --- a/lib/libusb/libusb10.h +++ b/lib/libusb/libusb10.h @@ -118,6 +118,8 @@ struct libusb_context { libusb_pollfd_added_cb fd_added_cb; libusb_pollfd_removed_cb fd_removed_cb; void *fd_cb_user_data; + libusb_log_cb log_cb; + int no_discovery; }; struct libusb_device { diff --git a/lib/libusb/libusb10_hotplug.c b/lib/libusb/libusb10_hotplug.c index 359c818b5720..8b8de5019e4e 100644 --- a/lib/libusb/libusb10_hotplug.c +++ b/lib/libusb/libusb10_hotplug.c @@ -348,6 +348,9 @@ int libusb_hotplug_register_callback(libusb_context *ctx, ctx = GET_CONTEXT(ctx); + if (ctx->no_discovery) + return (LIBUSB_SUCCESS); + if (ctx->usb_event_mode == usb_event_none) { HOTPLUG_LOCK(ctx); if (!netlink_init(ctx) && !devd_init(ctx))
