From nobody Mon Jun 22 13:36:59 2026 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4gkTl013pfz6hCRY for ; Mon, 22 Jun 2026 13:37:00 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "YR1" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 4gkTkz5r7wz3w65 for ; Mon, 22 Jun 2026 13:36:59 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1782135419; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=KCYNLqCVhcOJ7veWqEUFjIjv2WzGSdVVpO8n1/ZJWzs=; b=TFpk3BF2teWzt1zqT4iQxmwz5GVJU7q1bs2Z4Ib7sesXo10XG4APkrNTR0QeXLd+o7ktuL 2Vio5JU4NBq7VwDfr/BJYx1BTI7ydULh1d2t6swkpiNf2Oe1VAg1R7JDny0KW2+to8BcZL Pdakq8EusDJ8ngvk4tvWvu/UsPT5PSCBVNSFd3OuUViaGj2CwGX25GVqPnp0be7GuxvQaU zhj7AMVi+A2CsJgQyokdBoXw4cJXBUvt0G7aGOSUaJJoZMYy4RDK4g+UgOVMVJQ/KIKigh A6BtxA8xZcY7fF7VB0nszDbD5AjJTiBKx1YOZ4Ue0Qgx728XJzd3WiszI+enQA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1782135419; a=rsa-sha256; cv=none; b=X0bCKR6LCHNcw7sR51R43P3anQipR0X1nAHak7Rp+qdkBd7gqkfVFcwm+By+53MgCTPEyt 4TS+gUk1g+TriVTbn/UXA+P9SlhQZnHZJqjOpuI2XyoX4PWo4H7QO3hT1AIwYbXfSkgGHu yMqHq9U4SnYG63fFWczSIU/k2pduV0rdt8LmZe36Sk2Ccv1tvJALjZEqYQLktfSn0TQQmc BJbsJVj6I7FmX/GNVJNqIG0iyjHit7gbeLhUNgKIuOk9X67FJbuHS6kcnMPu6FhKFprbiz AboyEGnThTXn/3WY4eU0JJMmKxCOAFYgODIXrJGmFwXo/1TOjs8OBvO3bMY35w== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1782135419; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=KCYNLqCVhcOJ7veWqEUFjIjv2WzGSdVVpO8n1/ZJWzs=; b=Jh5I3xc7gKEok/0dMQIJYiKregCNIqjMgOeqzN6Ry39l1Eo2tYegKX2sCv/9I7Hjqcz0+m KXYIAeaTV49zMgxdzhOUwK/D28gK9Z2MPssqHBGsj8GSjdVROqRKgDm3ghpBqUZN0Mc1uZ 00Nr7+1H0BZwV5hh8h0cWWKMCX3CEdALdOuP/oDpZb82i8pDM9gbaW2V7B2OHNkSeAmetm ykj70aLpO5QQxezba8vObl0nnR6d187Z4lmqxlvrIgsTZHgEujIghF261ZW6Qxso4xVePY vGQIsuFGcpbRAHsUQD9EQVircgsFKNw7Wuh4oM3AAWSLekMvjt+BSxBcfOcGWg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4gkTkz4XV0zqkD for ; Mon, 22 Jun 2026 13:36:59 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 39461 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Mon, 22 Jun 2026 13:36:59 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: ShengYi Hung Subject: git: 2879c818e553 - main - libusb: implement libusb_set_option List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: aokblast X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 2879c818e5535e9c9dc592714c7d4ef0c1c9e8c1 Auto-Submitted: auto-generated Date: Mon, 22 Jun 2026 13:36:59 +0000 Message-Id: <6a393a7b.39461.1ed3577b@gitrepo.freebsd.org> The branch main has been updated by aokblast: URL: https://cgit.FreeBSD.org/src/commit/?id=2879c818e5535e9c9dc592714c7d4ef0c1c9e8c1 commit 2879c818e5535e9c9dc592714c7d4ef0c1c9e8c1 Author: ShengYi Hung AuthorDate: 2025-06-11 19:07:39 +0000 Commit: ShengYi Hung 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 #include #include +#include #include #include #include @@ -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))