git: be95871347a8 - main - libusb: refactor the process of setting option when init the context.

From: ShengYi Hung <aokblast_at_FreeBSD.org>
Date: Mon, 22 Jun 2026 13:37:00 UTC
The branch main has been updated by aokblast:

URL: https://cgit.FreeBSD.org/src/commit/?id=be95871347a85f6feac59d036f62182fce847f23

commit be95871347a85f6feac59d036f62182fce847f23
Author:     ShengYi Hung <aokblast@FreeBSD.org>
AuthorDate: 2025-07-09 09:36:07 +0000
Commit:     ShengYi Hung <aokblast@FreeBSD.org>
CommitDate: 2026-06-22 13:36:47 +0000

    libusb: refactor the process of setting option when init the context.
    
    From
    https://github.com/libusb/libusb/commit/6622f386f52807dac76c8a260c98aa02c311bc93#diff-c1f9bc250077d41456a3e580fca0ddf5d8c25b741bff6d9b9505990a8b70b254R2358.
    We are able to set all of the option from the init_context. To address
    this, we modify the process of setting option in init_context to make it be more clear and then
    adapat libusb_set_option inside to all other option.
    
    Reviewed by:    adrian
    Sponsored By:   The FreeBSD Foundation
    Differential Revision: https://reviews.freebsd.org/D51224
---
 lib/libusb/libusb.3   |  2 +-
 lib/libusb/libusb.h   |  7 ++-----
 lib/libusb/libusb10.c | 41 ++++++++++++++++++++++++-----------------
 3 files changed, 27 insertions(+), 23 deletions(-)

diff --git a/lib/libusb/libusb.3 b/lib/libusb/libusb.3
index 52b83c700571..7b977103eb59 100644
--- a/lib/libusb/libusb.3
+++ b/lib/libusb/libusb.3
@@ -118,7 +118,7 @@ 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
+.It Va LIBUSB_OPTION_NO_DEVICE_DISCOVERY
 disable the USB enumeration.
 .It Va LIBUSB_OPTION_LOG_CB
 customize default callback function with
diff --git a/lib/libusb/libusb.h b/lib/libusb/libusb.h
index 5f8af0eae6cc..00255291ebd5 100644
--- a/lib/libusb/libusb.h
+++ b/lib/libusb/libusb.h
@@ -303,11 +303,6 @@ 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;
@@ -330,6 +325,8 @@ struct libusb_init_option {
 };
 
 typedef struct libusb_context libusb_context;
+typedef void (*libusb_log_cb)(libusb_context *ctx, enum libusb_log_level,
+    const char *str);
 typedef struct libusb_device libusb_device;
 typedef struct libusb_device_handle libusb_device_handle;
 typedef struct libusb_pollfd libusb_pollfd;
diff --git a/lib/libusb/libusb10.c b/lib/libusb/libusb10.c
index 9c0f4edb9513..c00d8ec8be8b 100644
--- a/lib/libusb/libusb10.c
+++ b/lib/libusb/libusb10.c
@@ -208,6 +208,10 @@ libusb_init_context(libusb_context **context,
 	ctx->devd_pipe = -1;
 
 	debug = getenv("LIBUSB_DEBUG");
+	ctx->log_cb = NULL;
+	ctx->no_discovery = false;
+	ctx->debug = LIBUSB_LOG_LEVEL_NONE;
+
 	if (debug != NULL) {
 		/*
 		 * If LIBUSB_DEBUG is set, we'll honor that first and
@@ -227,24 +231,29 @@ libusb_init_context(libusb_context **context,
 			 */
 			ctx->debug = 0;
 		}
-	} else {
-		/*
-		 * If the LIBUSB_OPTION_LOG_LEVEL is set, honor that.
-		 */
-		for (int i = 0; i != num_options; i++) {
-			if (option[i].option != LIBUSB_OPTION_LOG_LEVEL)
-				continue;
+	}
 
-			ctx->debug = (int)option[i].value.ival;
-			if ((int64_t)ctx->debug == option[i].value.ival) {
-				ctx->debug_fixed = 1;
-			} else {
-				free(ctx);
-				return (LIBUSB_ERROR_INVALID_PARAM);
-			}
-		}
+	/*
+	 * Set the default from default context then override by options
+	 */
+	if (usbi_default_context) {
+		CTX_LOCK(usbi_default_context);
+		if (usbi_default_context->no_discovery)
+			libusb_set_option(ctx,
+			    LIBUSB_OPTION_NO_DEVICE_DISCOVERY);
+		/* libusb_set_option will check if debug is fixed by the
+		   environment variable. If it is fixed, the override will not
+		   take effect */
+		libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL,
+		    usbi_default_context->debug);
+		libusb_set_option(ctx, LIBUSB_OPTION_LOG_CB,
+		    usbi_default_context->log_cb);
+		CTX_UNLOCK(usbi_default_context);
 	}
 
+	for (int i = 0; i < num_options; i++)
+		libusb_set_option(ctx, option[i].option, option[i].value);
+
 	TAILQ_INIT(&ctx->pollfds);
 	TAILQ_INIT(&ctx->tr_done);
 	TAILQ_INIT(&ctx->hotplug_cbh);
@@ -294,8 +303,6 @@ 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) {