From nobody Thu Jun 12 16:56:50 2025 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 4bJ7wg2H5bz5y5DL; Thu, 12 Jun 2025 16:56:51 +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 "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4bJ7wg1FKNz3bmw; Thu, 12 Jun 2025 16:56:51 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1749747411; 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=jCOr9cc2E43OMtFykINokOFFIt6YxUZI3bi7q5j5HyQ=; b=inbIpeFf2emKVJeSoQDousokAzNCtyaYCADovImW1j+4E+x5fPtVVDOww8wWLKWETFgW1w K4BrO//mFsisZrE43UNpZhzZm7F8K7nUdfErwaIVB5RoRKXci1YGPLExnw5rAF/M6m9bsv sP6jZvoyaXQ0+aJ0HEr9rYiYaBHagi0O0ZkGWOFkskh+GRfIvI1+LvNrjTRuusuXiw9jcF AE/EeNx2edi8KeH7uNF8IVy6Hm4L6C0PVGlPUzvNQotCVBtkmzf/4gG6cvufDXw7mKI4G9 kEn1t+T7YXafbXswqZiDACb/VsKgoDImt7FSEXdTTZB5la3fomG+aIOm3dHp7A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1749747411; 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=jCOr9cc2E43OMtFykINokOFFIt6YxUZI3bi7q5j5HyQ=; b=ERkjOR5kcVS0m8NLssKkghH5izpH8Tsz72akG2FQAvlO2sPY6ufoYhL8pJVD3JNM6P8U1T nkfmS7w+fmyVno00oWQ50ogqnmnymPhfD9O5gSBS8jaazoxbyFIz6hO55uHfxH8oENZZmF Mz1BUL8bojMqXwoWA7FqbBFQINgQv0vg6e23xfVKVEHWPM7kGufw2ZIMuSbGfYNy6FHOhg jHdRXx+L9WsTkoyl0EY9/mwy4W5QsSpNCK0K6/IsLpYX9PC1bsomiTbhTGyTaPYv7OSXsL csdv+h/Y9W1kBUmXxNU0MP4GV6XIclizMm+yZeCfXyEUiG6wkWwA/q4G7SNizQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1749747411; a=rsa-sha256; cv=none; b=rML3Cpb3BylPW3AlGml6WOJvS+j2kRSCnSCTlAt0Lsd0jrfE53fQ59Vpd4Q739Mw/GqjbG cmrJaeXjeGhCZOZp83k8GLcsr75fzu5wtsoBtlmwKf+5PoeeSmC3ujiLZTEQLpFrokiow9 oDEw1OTBrBfqKb8j4tQMGhTOtuKAdsn4Z110elnL10ZNGaMZHDhj3upPCuGeOksdSYDeoR isNwZ+M8SxvcDVrURpZCv28LExT2UjcRVIewn+syb6lH/Qjr9J9rr6W5lDBk9P/KxE8slx m0EfyPIi3kRMlKU8Lskm63pcohbbQDAuGyRIoUiiK7Y4dJkbP25GRI6GYzKuIQ== 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 4bJ7wg0YVtz5QD; Thu, 12 Jun 2025 16:56:51 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 55CGuoDY036564; Thu, 12 Jun 2025 16:56:50 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 55CGuoa9036561; Thu, 12 Jun 2025 16:56:50 GMT (envelope-from git) Date: Thu, 12 Jun 2025 16:56:50 GMT Message-Id: <202506121656.55CGuoa9036561@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Kyle Evans Subject: git: 4c556a4e8d14 - main - libusb: implement `libusb_get_parent` 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 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kevans X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 4c556a4e8d149169a7d8e029aee7f3d017fd4e7f Auto-Submitted: auto-generated The branch main has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=4c556a4e8d149169a7d8e029aee7f3d017fd4e7f commit 4c556a4e8d149169a7d8e029aee7f3d017fd4e7f Author: Aymeric Wibo AuthorDate: 2025-06-12 16:54:13 +0000 Commit: Kyle Evans CommitDate: 2025-06-12 16:56:31 +0000 libusb: implement `libusb_get_parent` Newer versions of drivers such as libwacom (graphics tablets) or libfprint (fingerprint scanners) call g_usb_device_get_parent. This in turn calls libusb_get_parent on platforms which implement it, and returns NULL on platforms that don't. This patch implements this function on FreeBSD. Reviewed by: bapt, kevans Differential Revision: https://reviews.freebsd.org/D46992 --- lib/libusb/libusb.3 | 9 +++++++-- lib/libusb/libusb.h | 1 + lib/libusb/libusb10.c | 34 ++++++++++++++++++++++++++++++++-- lib/libusb/libusb10.h | 1 + 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/lib/libusb/libusb.3 b/lib/libusb/libusb.3 index 1ca0e677d96f..a9a99f307a86 100644 --- a/lib/libusb/libusb.3 +++ b/lib/libusb/libusb.3 @@ -22,7 +22,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd May 16, 2025 +.Dd June 12, 2025 .Dt LIBUSB 3 .Os .Sh NAME @@ -117,7 +117,7 @@ This function does not return NULL. .Fn libusb_set_debug "libusb_context *ctx" "int level" Set the debug level to .Fa level . -.Pp +.Sh DEVICE HANDLING AND ENUMERATION .Ft ssize_t .Fn libusb_get_device_list "libusb_context *ctx" "libusb_device ***list" Populate @@ -220,6 +220,11 @@ Close a device handle. Get the device contained by devh. Returns NULL on error. .Pp +.Ft libusb_device * +.Fn libusb_get_parent "libusb_device *dev" +Get dev's parent device. +Returns NULL if the device has no parent (i.e. is a root device). +.Pp .Ft int .Fn libusb_get_configuration "libusb_device_handle *devh" "int *config" Returns the value of the current configuration. diff --git a/lib/libusb/libusb.h b/lib/libusb/libusb.h index cdac94b17c64..0aad29aa4ecc 100644 --- a/lib/libusb/libusb.h +++ b/lib/libusb/libusb.h @@ -503,6 +503,7 @@ int libusb_open(libusb_device * dev, libusb_device_handle ** devh); libusb_device_handle *libusb_open_device_with_vid_pid(libusb_context * ctx, uint16_t vendor_id, uint16_t product_id); void libusb_close(libusb_device_handle * devh); libusb_device *libusb_get_device(libusb_device_handle * devh); +libusb_device *libusb_get_parent(libusb_device * dev); int libusb_get_configuration(libusb_device_handle * devh, int *config); int libusb_set_configuration(libusb_device_handle * devh, int configuration); int libusb_claim_interface(libusb_device_handle * devh, int interface_number); diff --git a/lib/libusb/libusb10.c b/lib/libusb/libusb10.c index b442a1f489a0..3e81f234a735 100644 --- a/lib/libusb/libusb10.c +++ b/lib/libusb/libusb10.c @@ -3,6 +3,7 @@ * * Copyright (c) 2009 Sylvestre Gallon. All rights reserved. * Copyright (c) 2009-2023 Hans Petter Selasky + * Copyright (c) 2024 Aymeric Wibo * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -311,9 +312,9 @@ ssize_t libusb_get_device_list(libusb_context *ctx, libusb_device ***list) { struct libusb20_backend *usb_backend; - struct libusb20_device *pdev; + struct libusb20_device *pdev, *parent_dev; struct libusb_device *dev; - int i; + int i, j, k; ctx = GET_CONTEXT(ctx); @@ -365,6 +366,9 @@ libusb_get_device_list(libusb_context *ctx, libusb_device ***list) /* set context we belong to */ dev->ctx = ctx; + /* assume we have no parent by default */ + dev->parent_dev = NULL; + /* link together the two structures */ dev->os_priv = pdev; pdev->privLuData = dev; @@ -374,6 +378,25 @@ libusb_get_device_list(libusb_context *ctx, libusb_device ***list) } (*list)[i] = NULL; + /* for each device, find its parent */ + for (j = 0; j < i; j++) { + pdev = (*list)[j]->os_priv; + + for (k = 0; k < i; k++) { + if (k == j) + continue; + + parent_dev = (*list)[k]->os_priv; + + if (parent_dev->bus_number != pdev->bus_number) + continue; + if (parent_dev->device_address == pdev->parent_address) { + (*list)[j]->parent_dev = libusb_ref_device((*list)[k]); + break; + } + } + } + libusb20_be_free(usb_backend); return (i); } @@ -540,6 +563,7 @@ libusb_unref_device(libusb_device *dev) CTX_UNLOCK(dev->ctx); if (dev->refcnt == 0) { + libusb_unref_device(dev->parent_dev); libusb20_dev_free(dev->os_priv); free(dev); } @@ -816,6 +840,12 @@ libusb_set_interface_alt_setting(struct libusb20_device *pdev, return (err ? LIBUSB_ERROR_OTHER : 0); } +libusb_device * +libusb_get_parent(libusb_device *dev) +{ + return (dev->parent_dev); +} + static struct libusb20_transfer * libusb10_get_transfer(struct libusb20_device *pdev, uint8_t endpoint, uint8_t xfer_index) diff --git a/lib/libusb/libusb10.h b/lib/libusb/libusb10.h index f34ea25740d6..70b5525df537 100644 --- a/lib/libusb/libusb10.h +++ b/lib/libusb/libusb10.h @@ -142,6 +142,7 @@ struct libusb_device { struct libusb_super_pollfd dev_poll; struct libusb_context *ctx; + struct libusb_device *parent_dev; TAILQ_ENTRY(libusb_device) hotplug_entry;