From nobody Thu Feb 29 12:04:09 2024 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 4TlqdP6zTWz5C3D7; Thu, 29 Feb 2024 12:04:09 +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 4TlqdP6Qlqz4DC7; Thu, 29 Feb 2024 12:04:09 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1709208249; 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=sXaXplzDrW7cj3GiOB6ekuQNxoOgF67KWYcpfc4S69o=; b=MtRnOMxh292XMkXDvAjRLlCHSaByidFfi7EqATaeGgZOVg2go4XgRW2ky3gLUtdh4J8NcJ LjbSYNsj2g4wfAicyhk6OZAzjPvQ8MyKZKMA6keMot37DtItMqB7MdCyGzczR1zw0f7ZG1 tV8kTurx/rBNHScA0V1OZxZaWTtEqOvi/+l40QKfFB4XDY0C2X6S3gJAe+QKdF1a89ekOx dhSJkJcrkV+ZSzVboL2LxQiEahaiwJwx21ID9GZWv76jDhyXTKYMlB9JDPEHuMRT8NWQ85 PmreSitBcGe9SXLmvvVE/ADjYK+adB5d6OM/UN+TTVJ4EtYDCm1znA4Kk9o5FQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1709208249; a=rsa-sha256; cv=none; b=Aq+lv4EQzG7FaaU8lIQ7bSredTlm5dTti/gkwWhwg4NnLNUkaXiwOrrZHIQETGgw+dEgNK Bu++wJTfkjTrd5pSPA0nRrRRo09cNBtCh9ZW/6HVX53VSq4tc9GHddmCeCMgWr2gm6lMeH 0FQM6xR0DFRagTlT92WpwPnR/nQGFSMuekrDhAP2vUxJEkgvJ5xNdWPM7nTyXk0/30ETHJ BjAAUtttbpj5/paxEnoQhOo0HUWABu2yHXNCt5ir9MEGSz3rOMUcyn3YSG+nqYt4OdR7yR kAa/7auudhxCF3BNEHzU57r9GWIk5BzaZ2pG8/sOJyZyWGl1J05iQ1N48DflRQ== 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=1709208249; 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=sXaXplzDrW7cj3GiOB6ekuQNxoOgF67KWYcpfc4S69o=; b=lJzF4zgwduS1Yqpig09Ct2oh/PihhRv5zLU7FPR7l1cVtnlOXnQx/IyBuibG3ZSYRF2Mxy v+pBTkcnUpZeL6dEIw/lsHeaL0C3kk8YWXEyW3YYLF729a0htXVxWfgn1mxTEKTe8LWiQs IXUlVgSmuwPzDWMm03u+CNNoxcBXBx2T4RR2wf1+WSIkNNuXaCA+BBGnFUc7YJeO8rF8GF ynjEiri2qtqbvl9rggtWNmiZ+wgn461xtSdddD7+YMuGkstwMft6lGCzqdFPcJoNQ/b/oH uNqCMYXSvnj3I2ylsswwjqmL6j3KV04lsuBqywrNBdwcv73MunaANP6OdyIvfw== 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 4TlqdP62MDzh0N; Thu, 29 Feb 2024 12:04:09 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 41TC49B4083227; Thu, 29 Feb 2024 12:04:09 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 41TC492M083224; Thu, 29 Feb 2024 12:04:09 GMT (envelope-from git) Date: Thu, 29 Feb 2024 12:04:09 GMT Message-Id: <202402291204.41TC492M083224@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Baptiste Daroussin Subject: git: 8fe86d923f83 - main - usbconfig: allow to get usb vendors and products from the usb.ids database 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: bapt X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 8fe86d923f83ffdfed44f3b67b55287bf7cf3ce1 Auto-Submitted: auto-generated The branch main has been updated by bapt: URL: https://cgit.FreeBSD.org/src/commit/?id=8fe86d923f83ffdfed44f3b67b55287bf7cf3ce1 commit 8fe86d923f83ffdfed44f3b67b55287bf7cf3ce1 Author: Baptiste Daroussin AuthorDate: 2024-02-27 17:12:38 +0000 Commit: Baptiste Daroussin CommitDate: 2024-02-29 12:01:06 +0000 usbconfig: allow to get usb vendors and products from the usb.ids database Reviewed by: emaste, manu Differential Revision: https://reviews.freebsd.org/D3392 --- usr.sbin/usbconfig/dump.c | 150 ++++++++++++++++++++++++++++++++++++++++- usr.sbin/usbconfig/pathnames.h | 4 ++ 2 files changed, 153 insertions(+), 1 deletion(-) diff --git a/usr.sbin/usbconfig/dump.c b/usr.sbin/usbconfig/dump.c index d7ea8b93011a..36f026bfc2fb 100644 --- a/usr.sbin/usbconfig/dump.c +++ b/usr.sbin/usbconfig/dump.c @@ -2,6 +2,7 @@ * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. + * Copyright (c) 2024 Baptiste Daroussin * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -25,6 +26,8 @@ * SUCH DAMAGE. */ +#include + #include #include #include @@ -38,12 +41,28 @@ #include #include "dump.h" +#include "pathnames.h" #define DUMP0(n,type,field,...) dump_field(pdev, " ", #field, n->field); #define DUMP1(n,type,field,...) dump_field(pdev, " ", #field, n->field); #define DUMP2(n,type,field,...) dump_field(pdev, " ", #field, n->field); #define DUMP3(n,type,field,...) dump_field(pdev, " ", #field, n->field); +struct usb_product_info { + STAILQ_ENTRY(usb_product_info) link; + int id; + char *desc; +}; + +struct usb_vendor_info { + STAILQ_ENTRY(usb_vendor_info) link; + STAILQ_HEAD(,usb_product_info) devs; + int id; + char *desc; +}; + +STAILQ_HEAD(usb_vendors, usb_vendor_info); + const char * dump_mode(uint8_t value) { @@ -293,22 +312,151 @@ dump_iface(struct libusb20_device *pdev, } } +static struct usb_vendors * +load_vendors(void) +{ + const char *dbf; + FILE *db = NULL; + struct usb_vendor_info *cv; + struct usb_product_info *cd; + struct usb_vendors *usb_vendors; + char buf[1024], str[1024]; + char *ch; + int id; + + usb_vendors = malloc(sizeof(*usb_vendors)); + if (usb_vendors == NULL) + err(1, "out of memory"); + STAILQ_INIT(usb_vendors); + if ((dbf = getenv("USB_VENDOR_DATABASE")) != NULL) + db = fopen(dbf, "r"); + if (db == NULL) { + dbf = _PATH_LUSBVDB; + if ((db = fopen(dbf, "r")) == NULL) { + dbf = _PATH_USBVDB; + if ((db = fopen(dbf, "r")) == NULL) + return (usb_vendors); + } + } + cv = NULL; + cd = NULL; + + for (;;) { + if (fgets(buf, sizeof(buf), db) == NULL) + break; + + if ((ch = strchr(buf, '#')) != NULL) + *ch = '\0'; + if (ch == buf) + continue; + ch = strchr(buf, '\0') - 1; + while (ch > buf && isspace(*ch)) + *ch-- = '\0'; + if (ch <= buf) + continue; + + /* Can't handle subvendor / subdevice entries yet */ + if (buf[0] == '\t' && buf[1] == '\t') + continue; + + /* Check for vendor entry */ + if (buf[0] != '\t' && sscanf(buf, "%04x %[^\n]", &id, str) == 2) { + if ((id == 0) || (strlen(str) < 1)) + continue; + if ((cv = malloc(sizeof(struct usb_vendor_info))) == NULL) + err(1, "out of memory"); + if ((cv->desc = strdup(str)) == NULL) + err(1, "out of memory"); + cv->id = id; + STAILQ_INIT(&cv->devs); + STAILQ_INSERT_TAIL(usb_vendors, cv, link); + continue; + } + + /* Check for device entry */ + if (buf[0] == '\t' && sscanf(buf + 1, "%04x %[^\n]", &id, str) == 2) { + if ((id == 0) || (strlen(str) < 1)) + continue; + if (cv == NULL) + continue; + if ((cd = malloc(sizeof(struct usb_product_info))) == NULL) + err(1, "out of memory"); + if ((cd->desc = strdup(str)) == NULL) + err(1, "out of memory"); + cd->id = id; + STAILQ_INSERT_TAIL(&cv->devs, cd, link); + continue; + } + } + if (ferror(db)) + err(1, "error reading the usb id db"); + + fclose(db); + /* cleanup */ + return (usb_vendors); +} + +static char * +_device_desc(struct libusb20_device *pdev) +{ + static struct usb_vendors *usb_vendors = NULL; + char *desc = NULL; + const char *vendor = NULL, *product = NULL; + uint16_t vid = libusb20_dev_get_device_desc(pdev)->idVendor; + uint16_t pid = libusb20_dev_get_device_desc(pdev)->idProduct; + struct usb_vendor_info *vi; + struct usb_product_info *pi; + + if (usb_vendors == NULL) + usb_vendors = load_vendors(); + + STAILQ_FOREACH(vi, usb_vendors, link) { + if (vi->id == vid) { + vendor = vi->desc; + break; + } + } + if (vi != NULL) { + STAILQ_FOREACH(pi, &vi->devs, link) { + if (pi->id == pid) { + product = pi->desc; + break; + } + } + } + if (vendor == NULL || product == NULL) + return (NULL); + + asprintf(&desc, "ugen%u.%u: <%s %s> at usbus%u", + libusb20_dev_get_bus_number(pdev), + libusb20_dev_get_address(pdev), + product, vendor, + libusb20_dev_get_bus_number(pdev)); + + + return (desc); +} + void dump_device_info(struct libusb20_device *pdev, uint8_t show_ifdrv) { char buf[128]; uint8_t n; unsigned int usage; + char *desc; usage = libusb20_dev_get_power_usage(pdev); + desc = _device_desc(pdev); + printf("%s, cfg=%u md=%s spd=%s pwr=%s (%umA)\n", - libusb20_dev_get_desc(pdev), + desc ? desc : libusb20_dev_get_desc(pdev), libusb20_dev_get_config_index(pdev), dump_mode(libusb20_dev_get_mode(pdev)), dump_speed(libusb20_dev_get_speed(pdev)), dump_power_mode(libusb20_dev_get_power_mode(pdev)), usage); + free(desc); if (!show_ifdrv) return; diff --git a/usr.sbin/usbconfig/pathnames.h b/usr.sbin/usbconfig/pathnames.h new file mode 100644 index 000000000000..66053a9a87c5 --- /dev/null +++ b/usr.sbin/usbconfig/pathnames.h @@ -0,0 +1,4 @@ +#include + +#define _PATH_USBVDB "/usr/share/misc/usb_vendors" +#define _PATH_LUSBVDB _PATH_LOCALBASE "/share/usbids/usb.ids"