From nobody Sun Apr 30 06:57:40 2023 X-Original-To: dev-commits-src-all@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 4Q8HGY38Zfz48s8w; Sun, 30 Apr 2023 06:57:41 +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 "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Q8HGY1B4Zz3MBj; Sun, 30 Apr 2023 06:57:41 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1682837861; 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=s/R7bSmBG47ymeyhlJCcEmhSFyXsBH3cXPcbX4U/XEg=; b=siuf2S9VS9TEJwEogkDHJiLi6iV0m7tuVUJvmX/ExiDYh7ehm1Bdv8TwnPUVUGPZ5Wwjs0 vtdz2h1PR2LnC4hw8XZEyFA6sYo4U9yAGvFpHDkTeqG9fAQ3bu5PjJAkvrFZUWKHNekaOm oattUe1EQH6KwVwb7gcv/Ku3MPrK7UrRTivb/kpB+wS917hyxrMoDc8u2ozpI8fj9ADXM6 o2yDhvOwrIz6b0FqmoOlQrODEnK4Yma43U6bt2cZ2pX7kl4CLZ3ZTwto3hnheO/H0p61VU Og4pRK8U0ITGwIiySiNwQkgjci7pPsbuvH6R7A07SXIYgzikAQ7puLvhLGjqmg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1682837861; 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=s/R7bSmBG47ymeyhlJCcEmhSFyXsBH3cXPcbX4U/XEg=; b=CKm0OKmB2kHYChHoNEvEQN785hl4M1K+VImUF+rkOgd2BoW1hXiwMLtL6VUXbrWg5MrmP+ YCCuRIFEs/hyEQyYUXNUb+AnQ4p3ENazMWi4SCpuDhASLfyI/BeS2K4bxHB40eMiCjR0wb nW+EwOVV6DCTsgJHZ6YVKomAugjg6VyVzDgkc+LxXNDhKG8bdt8iWvT6+18fX5RfSLlJR3 6Khvpu5wPtGfVR8lSz5J2kEz3LXqNl1GrZMD8EHN2k3IsIwL2tJ12k8rhZL0kpl5Fe2ZOZ j/ADJSO+csBqn7nM4W9uViP6AOmhx83E0Re1agBtaybMAIs/yUyojcTeuAmxMw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1682837861; a=rsa-sha256; cv=none; b=hvWqQaHp97s9hNg1ej3V10be7+saL/RQ7K312rbOrCk/EN+f8WOcyhyF+QMRmYYqDie6JM J6LyqLEULkuNNzOCVg6tCO0QGmjcUyMq+f7FaMC10LunG5G/3LZZHfgiyVHPXayOK1Jilh BtwSOtIEe/sJG4qRfJ+SW01N96He8Mg68HJRSd8SS8RfGn5rrwKL4TONKchycOJ4FDIRvz mxDKRkdAoxyOFnl5JJCFk34wRNVgPlr/5+5gvik0T12pksPIqNyRCvQPQOOJoBNNHPllIv yjgykDEW+ZxTTgmyhXsANjUq11cOf0J/1IqNLDY4pgsS2bFqRZkK5kqKXc7DDA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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 did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Q8HGY0FrPz1Cg9; Sun, 30 Apr 2023 06:57:41 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 33U6ve7c071289; Sun, 30 Apr 2023 06:57:40 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 33U6veuo071288; Sun, 30 Apr 2023 06:57:40 GMT (envelope-from git) Date: Sun, 30 Apr 2023 06:57:40 GMT Message-Id: <202304300657.33U6veuo071288@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Hans Petter Selasky Subject: git: 8ff5099a7932 - stable/13 - libusb(3): Implement libusb_init_context() and the needed structures and definitions. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: hselasky X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 8ff5099a793207af0d6080b01df40392c3c5b51f Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by hselasky: URL: https://cgit.FreeBSD.org/src/commit/?id=8ff5099a793207af0d6080b01df40392c3c5b51f commit 8ff5099a793207af0d6080b01df40392c3c5b51f Author: Hans Petter Selasky AuthorDate: 2023-01-26 12:56:51 +0000 Commit: Hans Petter Selasky CommitDate: 2023-04-30 06:56:15 +0000 libusb(3): Implement libusb_init_context() and the needed structures and definitions. Differential Revision: https://reviews.freebsd.org/D38212 Sponsored by: NVIDIA Networking (cherry picked from commit 4c6bcffd04f9d0b6cb57af0ffcc9be3098fe950c) --- lib/libusb/Makefile | 1 + lib/libusb/libusb.3 | 27 +++++++++++++++++++++------ lib/libusb/libusb.h | 16 ++++++++++++++++ lib/libusb/libusb10.c | 34 +++++++++++++++++++++++++++++++--- 4 files changed, 69 insertions(+), 9 deletions(-) diff --git a/lib/libusb/Makefile b/lib/libusb/Makefile index 3982fda478fb..1648664b8815 100644 --- a/lib/libusb/Makefile +++ b/lib/libusb/Makefile @@ -66,6 +66,7 @@ CFLAGS+= -I ../../sys # LibUSB v1.0 MLINKS += libusb.3 libusb_get_version.3 MLINKS += libusb.3 libusb_init.3 +MLINKS += libusb.3 libusb_init_context.3 MLINKS += libusb.3 libusb_exit.3 MLINKS += libusb.3 libusb_has_capability.3 MLINKS += libusb.3 libusb_strerror.3 diff --git a/lib/libusb/libusb.3 b/lib/libusb/libusb.3 index d4c638d986e4..fe56639fa59f 100644 --- a/lib/libusb/libusb.3 +++ b/lib/libusb/libusb.3 @@ -24,7 +24,7 @@ .\" .\" $FreeBSD$ .\" -.Dd October, 2, 2022 +.Dd January, 26, 2023 .Dt LIBUSB 3 .Os .Sh NAME @@ -47,11 +47,26 @@ This function returns version information about LibUSB. .Pp .Ft int .Fn libusb_init "libusb_context **ctx" -This function initialises libusb. -It must be called at the beginning -of the program, before other libusb routines are used. -This function returns 0 on success or LIBUSB_ERROR on -failure. +Call this function before any other libusb v1.0 API function, to +initialise a valid libusb v1.0 context. +If the +.Fa ctx +argument is non-NULL, a pointer to the libusb context is stored at +the given location. +This function returns 0 upon success or LIBUSB_ERROR on failure. +.Pp +.Ft int +.Fn libusb_init_context "libusb_context **ctx" "const struct libusb_init_option []" "int num_options" +Call this function before any other libusb v1.0 API function, to +initialise a valid libusb v1.0 context. +If the +.Fa ctx +argument is non-NULL, a pointer to the libusb context is stored at +the given location. +Additional options, like the USB debug level, may be given using the +second and third argument. +If no options are needed, simply use libusb_init(). +This function returns 0 upon success or a LIBUSB_ERROR value on failure. .Pp .Ft void .Fn libusb_exit "libusb_context *ctx" diff --git a/lib/libusb/libusb.h b/lib/libusb/libusb.h index 9eaee671b8b3..28fbece4955a 100644 --- a/lib/libusb/libusb.h +++ b/lib/libusb/libusb.h @@ -266,6 +266,14 @@ typedef enum { LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT = 2, } libusb_hotplug_event; +enum libusb_option { + LIBUSB_OPTION_LOG_LEVEL = 0, + LIBUSB_OPTION_USE_USBDK = 1, + LIBUSB_OPTION_NO_DEVICE_DISCOVERY = 2, + LIBUSB_OPTION_WEAK_AUTHORITY = 2, + LIBUSB_OPTION_MAX = 3, +}; + /* libusb structures */ struct libusb_context; @@ -288,6 +296,13 @@ struct libusb_version { const char *describe; }; +struct libusb_init_option { + enum libusb_option option; + union { + int64_t ival; + } value; +}; + typedef struct libusb_context libusb_context; typedef struct libusb_device libusb_device; typedef struct libusb_device_handle libusb_device_handle; @@ -465,6 +480,7 @@ const struct libusb_version *libusb_get_version(void); const char *libusb_strerror(int code); const char *libusb_error_name(int code); int libusb_init(libusb_context ** context); +int libusb_init_context(libusb_context **, const struct libusb_init_option [], int num_options); void libusb_exit(struct libusb_context *ctx); int libusb_has_capability(uint32_t capability); diff --git a/lib/libusb/libusb10.c b/lib/libusb/libusb10.c index 1fc89fc409f0..793152a30910 100644 --- a/lib/libusb/libusb10.c +++ b/lib/libusb/libusb10.c @@ -3,7 +3,7 @@ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2009 Sylvestre Gallon. All rights reserved. - * Copyright (c) 2009 Hans Petter Selasky. All rights reserved. + * Copyright (c) 2009-2023 Hans Petter Selasky * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -135,12 +135,22 @@ libusb_interrupt_event_handler(libusb_context *ctx) int libusb_init(libusb_context **context) +{ + return (libusb_init_context(context, NULL, 0)); +} + +int +libusb_init_context(libusb_context **context, + const struct libusb_init_option option[], int num_options) { struct libusb_context *ctx; pthread_condattr_t attr; char *debug, *ep; int ret; + if (num_options < 0) + return (LIBUSB_ERROR_INVALID_PARAM); + ctx = malloc(sizeof(*ctx)); if (!ctx) return (LIBUSB_ERROR_INVALID_PARAM); @@ -150,8 +160,9 @@ libusb_init(libusb_context **context) debug = getenv("LIBUSB_DEBUG"); if (debug != NULL) { /* - * If LIBUSB_DEBUG is set, we'll honor that and use it to - * override libusb_set_debug calls. + * If LIBUSB_DEBUG is set, we'll honor that first and + * use it to override any future libusb_set_debug() + * calls or init options. */ errno = 0; ctx->debug = strtol(debug, &ep, 10); @@ -166,7 +177,24 @@ libusb_init(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); + } + } } + TAILQ_INIT(&ctx->pollfds); TAILQ_INIT(&ctx->tr_done); TAILQ_INIT(&ctx->hotplug_cbh);