Message ID | 20240229235905.569705-1-code@elbertmai.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-87713-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2097:b0:108:e6aa:91d0 with SMTP id gs23csp754488dyb; Thu, 29 Feb 2024 16:00:09 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWDpFbRKJWSwmye4tdGTYMQiNq9AzQSR80gpHy7qFHhH3yKIritQ8aWN00+i0+lCkUxFq9xEfpRTej4Jl9Aq07i1Aj/ug== X-Google-Smtp-Source: AGHT+IFKyizXDC8AlIEm7cfrn1D2UcjbuOH1HZGiAQd/UUZ4I8LMpSofDljdyabZvXO9HLAl0GND X-Received: by 2002:a05:6512:308f:b0:513:24b8:90cb with SMTP id z15-20020a056512308f00b0051324b890cbmr115949lfd.0.1709251209642; Thu, 29 Feb 2024 16:00:09 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709251209; cv=pass; d=google.com; s=arc-20160816; b=n5PIzVKLWj3e73O2BrctbfDf+NZNXK0IkVGCPh918IADq8CZWS68uyzC2qnzUJ0isq ietpFpOgWG0fMSpXl4P9wz2CYdG7vwSc+qs4ZHDflrvvXezO8Fosn0cKLK5dIsPFlGoN sfdoe5JbyLTo4lN2ciURzL0GF04GCzUBLMiJVNv/cVD4PDdHN2gtxTbbuMKHCn2aYOK/ 9p6Y9YpEUMKkJAh+L0flk7lNN0zVSPpYpN0vItFrcWqtpZBzOFFcv4mzwBYjrg0u/7XJ vMgJ4CLG1bz1cNBY0yOkArDDUMVEl3Rsa6kgLj+Zt9C1kcW66G7zl1qUqbrlNkrWj2Hw 9GEg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:feedback-id:dkim-signature:dkim-signature; bh=jKxTKTyzfcSXs003eimMjGbwTLAMzN33LwizADlAvJc=; fh=1WZnTMc8qSxRFBYxWPPEhxxvVTQoXiZE9yLHr/mwVZE=; b=UMyqb9utsassK9xfghQJ7CORcZ0xxANlp32t76lJhHRBqYMyVN3BSxqHRtB8jOoDqK JPPBifd7bvGWKtn12cPsGDLzMUh1Ttl9KQCbH3iPs8r5nvuUccjmbCwZSfn8IAcMec2H boYsnyLybC6E6kuy+YCxrWwIo9EgsNZ5bNxu8XN2xq3bCAUQHIhEZCiIm60fQN7mGmk5 qzQdDCYoExH5nH+YFh5QVHi2Nl7/bQx8J1UxMMk5WMZ5XjvilhP19fz/ot/ISayev/DU icaobuf/W1aGmLtQoP7HKoQtP7G4V42I7ralyd4iP9TCa/OR7KfarOuM4cOCUneHWRKv /zYQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@elbertmai.com header.s=purelymail1 header.b=U3sIgO3l; dkim=pass header.i=@purelymail.com header.s=purelymail1 header.b="bn/qr6Eb"; arc=pass (i=1 spf=pass spfdomain=elbertmai.com dkim=pass dkdomain=elbertmai.com dkim=pass dkdomain=purelymail.com dmarc=pass fromdomain=elbertmai.com); spf=pass (google.com: domain of linux-kernel+bounces-87713-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-87713-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=elbertmai.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id b5-20020a056402084500b005665c07c408si978451edz.17.2024.02.29.16.00.09 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Feb 2024 16:00:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-87713-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@elbertmai.com header.s=purelymail1 header.b=U3sIgO3l; dkim=pass header.i=@purelymail.com header.s=purelymail1 header.b="bn/qr6Eb"; arc=pass (i=1 spf=pass spfdomain=elbertmai.com dkim=pass dkdomain=elbertmai.com dkim=pass dkdomain=purelymail.com dmarc=pass fromdomain=elbertmai.com); spf=pass (google.com: domain of linux-kernel+bounces-87713-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-87713-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=elbertmai.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 146F11F22DD3 for <ouuuleilei@gmail.com>; Fri, 1 Mar 2024 00:00:09 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1BA1B13E7E3; Thu, 29 Feb 2024 23:59:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=elbertmai.com header.i=@elbertmai.com header.b="U3sIgO3l"; dkim=pass (2048-bit key) header.d=purelymail.com header.i=@purelymail.com header.b="bn/qr6Eb" Received: from sendmail.purelymail.com (sendmail.purelymail.com [34.202.193.197]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B648344374 for <linux-kernel@vger.kernel.org>; Thu, 29 Feb 2024 23:59:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=34.202.193.197 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709251192; cv=none; b=JvAtlPOTXHQAPTGXRtkfv3eCk6gR4Jn+HJG1/9G1sV/Y2mdJzH/nz20m7hgpOT8hfWVkBY+thBOJEfgMcn5Bihf94BhtqSbG0mSpRsFoU7qcevFst/i21I8F9gljxFcHn8bKcVobvk5VqJS1o+oZyZ4Mlu28OyLHV17UyOcDkWo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709251192; c=relaxed/simple; bh=OOOCsGxQH/wxU/CG8sZCXZFdEfx3IZ9cD1UMgPqoejI=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version:Content-Type; b=tAAaQg8HeDgODtPOdfp5MczQfPV/RQaw23Knajne7Zag6k9AKiTMHvFnjyix4REbbEsOuKBrTRUdSVFh+U33KimOerWVchiQL7+ab9KaeHXeqeFbstssIIVKU5S9xnRm9uR+rrTj4cAgqSJzLw4ucUv0EwoqJbKb474ZIPguaKU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=elbertmai.com; spf=pass smtp.mailfrom=elbertmai.com; dkim=pass (2048-bit key) header.d=elbertmai.com header.i=@elbertmai.com header.b=U3sIgO3l; dkim=pass (2048-bit key) header.d=purelymail.com header.i=@purelymail.com header.b=bn/qr6Eb; arc=none smtp.client-ip=34.202.193.197 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=elbertmai.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=elbertmai.com Authentication-Results: purelymail.com; auth=pass DKIM-Signature: a=rsa-sha256; b=U3sIgO3lqTzN/z5hQyfzPw4s9+yl5E1EGxOdfTty0MSKjQ6d2FjCTouDqFDDUUqCWMQ+PPy2rhcDKy5qh9UYD6Vyaexsi/qN0O7Y35bNgfwziQRG7N/O5l1v9/86CqRr7TuqbaZzDwKxK00iGx8A+6oOCGBHD38x+swMLVUorvZPMDjYczSm+rKgiiS4h4TpqXo49czACtt0LflITl6DbSVMsr8KoY1N+iWQ9Us3Isjrz4hcewT4JK0mmDffSxiSsiLLxCDWEPKgYBVsjzmWW7IqVdCFdAzm01uY22rY3v4TATqs0YngMTxgYfmNgM34ryX+gQbKEx8Y0jPuosercw==; s=purelymail1; d=elbertmai.com; v=1; bh=OOOCsGxQH/wxU/CG8sZCXZFdEfx3IZ9cD1UMgPqoejI=; h=Received:From:To:Subject; DKIM-Signature: a=rsa-sha256; b=bn/qr6Ebpw63GwRPkSkgkHh2wf3tAJtJY7NPyPSTPM/OqoLUHRV1xuiuHvJOKgD/MjNX+dnMYO+vnNb3nvcZycNjSW0dODcVl/3yQdphUcpwhyB14hS7Omxul4L/IxlnXkbilCpFF6JxTyk8uArA2kxxYn29qR82d+a/EhJmx3aOnRq5c+cCHQ45L0BG9BNCZJxbtTAeBd9tKqsN4c5QKyprAkWPYiWcEhTm84pk7FvcPGDg/jPvFsMq6VK8LZN8R7AzjYe/b8n4pgiPhJR6bEiKMzPoUbNByMhOeq4Dt3Njb/TlNqhHuiGJ7KY90ocFvPEWzUPBOFv1NEhiLYZzhQ==; s=purelymail1; d=purelymail.com; v=1; bh=OOOCsGxQH/wxU/CG8sZCXZFdEfx3IZ9cD1UMgPqoejI=; h=Feedback-ID:Received:From:To:Subject; Feedback-ID: 5995:1482:null:purelymail X-Pm-Original-To: linux-kernel@vger.kernel.org Received: by smtp.purelymail.com (Purelymail SMTP) with ESMTPSA id -265359410; (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384); Thu, 29 Feb 2024 23:59:22 +0000 (UTC) From: Elbert Mai <code@elbertmai.com> To: gregkh@linuxfoundation.org, linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org Cc: Elbert Mai <code@elbertmai.com> Subject: [PATCH] usb: Export BOS descriptor to sysfs Date: Thu, 29 Feb 2024 15:59:05 -0800 Message-Id: <20240229235905.569705-1-code@elbertmai.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-MIME-Autoconverted: from 8bit to quoted-printable by Purelymail Content-Type: text/plain; charset=UTF-8 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792279795972317103 X-GMAIL-MSGID: 1792279795972317103 |
Series |
usb: Export BOS descriptor to sysfs
|
|
Commit Message
Elbert Mai
Feb. 29, 2024, 11:59 p.m. UTC
Motivation
----------
The kernel already retrieves and caches the binary device object store
(BOS) descriptor from USB devices it enumerates. Export this descriptor to
userspace via sysfs, so users do not need to open the USB device with the
correct permissions and requesting the descriptor themselves.
A BOS descriptor contains a set of device capability descriptors. One that
is of interest to users is the platform descriptor. This contains a 128-bit
UUID and arbitrary data. The descriptor allows parties outside of USB-IF to
add additional metadata about a device in a standards-compliant manner.
Notable examples include the WebUSB and Microsoft OS 2.0 descriptors. Of
course, there could be more. By exporting the entire BOS descriptor we can
handle these and all future device capabilities. In addition, tools like
udev can match rules on device capabilities in the BOS without requiring
additional I/O with the USB device.
Implementation
--------------
Add bos_descriptor file to sysfs. This is a binary file and it works the
same way as the existing descriptors file. The file exists even if a device
does not have a BOS descriptor (the file will be empty in this case). This
allows users to detect if the kernel supports reading the BOS via sysfs and
fall back to direct USB I/O if needed.
Signed-off-by: Elbert Mai <code@elbertmai.com>
---
Documentation/ABI/testing/sysfs-bus-usb | 9 +++++++
drivers/usb/core/sysfs.c | 35 ++++++++++++++++++++++++-
2 files changed, 43 insertions(+), 1 deletion(-)
Comments
On Thu, Feb 29, 2024 at 03:59:05PM -0800, Elbert Mai wrote: > Motivation > ---------- > > The kernel already retrieves and caches the binary device object store > (BOS) descriptor from USB devices it enumerates. Export this descriptor to > userspace via sysfs, so users do not need to open the USB device with the > correct permissions and requesting the descriptor themselves. > > A BOS descriptor contains a set of device capability descriptors. One that > is of interest to users is the platform descriptor. This contains a 128-bit > UUID and arbitrary data. The descriptor allows parties outside of USB-IF to > add additional metadata about a device in a standards-compliant manner. > > Notable examples include the WebUSB and Microsoft OS 2.0 descriptors. Of > course, there could be more. By exporting the entire BOS descriptor we can > handle these and all future device capabilities. In addition, tools like > udev can match rules on device capabilities in the BOS without requiring > additional I/O with the USB device. But this requires that userspace parse the raw descriptor, right? Why not also export the descriptor information in simpler form through different sysfs files as well so that scripts can read this information easily? That's not to say we don't also want this binary file, just maybe add more? > Implementation > -------------- > > Add bos_descriptor file to sysfs. This is a binary file and it works the > same way as the existing descriptors file. The file exists even if a device > does not have a BOS descriptor (the file will be empty in this case). This > allows users to detect if the kernel supports reading the BOS via sysfs and > fall back to direct USB I/O if needed. This is nice, but can you only create the file if it actually is present? Use the is_visible callback to determine this, having an "empty" binary sysfs file isn't that nice. Other tiny review comments below: > Signed-off-by: Elbert Mai <code@elbertmai.com> > --- > Documentation/ABI/testing/sysfs-bus-usb | 9 +++++++ > drivers/usb/core/sysfs.c | 35 ++++++++++++++++++++++++- > 2 files changed, 43 insertions(+), 1 deletion(-) > > diff --git a/Documentation/ABI/testing/sysfs-bus-usb b/Documentation/ABI/testing/sysfs-bus-usb > index 614d216dff1d..bfffaa752a13 100644 > --- a/Documentation/ABI/testing/sysfs-bus-usb > +++ b/Documentation/ABI/testing/sysfs-bus-usb > @@ -293,3 +293,12 @@ Description: > USB 3.2 adds Dual-lane support, 2 rx and 2 tx -lanes over Type-C. > Inter-Chip SSIC devices support asymmetric lanes up to 4 lanes per > direction. Devices before USB 3.2 are single lane (tx_lanes = 1) > + > +What: /sys/bus/usb/devices/.../bos_descriptor > +Date: March 2024 > +Contact: Elbert Mai <code@elbertmai.com> > +Description: > + Binary file containing the cached binary device object store (BOS) > + descriptor of the device. This file is empty if the BOS descriptor > + is not present. The kernel will not request a BOS descriptor from > + the device if its bcdUSB value is less than 0x0201. > diff --git a/drivers/usb/core/sysfs.c b/drivers/usb/core/sysfs.c > index a2ca38e25e0c..208d2f8cde2d 100644 > --- a/drivers/usb/core/sysfs.c > +++ b/drivers/usb/core/sysfs.c > @@ -901,7 +901,7 @@ read_descriptors(struct file *filp, struct kobject *kobj, > srclen = sizeof(struct usb_device_descriptor); > } else { > src = udev->rawdescriptors[cfgno]; > - srclen = __le16_to_cpu(udev->config[cfgno].desc. > + srclen = le16_to_cpu(udev->config[cfgno].desc. Why is this line changed? > wTotalLength); > } > if (off < srclen) { > @@ -923,6 +923,34 @@ static struct bin_attribute dev_bin_attr_descriptors = { > .size = 18 + 65535, /* dev descr + max-size raw descriptor */ > }; > > +static ssize_t > +read_bos_descriptor(struct file *filp, struct kobject *kobj, > + struct bin_attribute *attr, > + char *buf, loff_t off, size_t count) > +{ > + struct device *dev = kobj_to_dev(kobj); > + struct usb_device *udev = to_usb_device(dev); > + struct usb_host_bos *bos = udev->bos; > + struct usb_bos_descriptor *desc; > + size_t desclen, n = 0; > + > + if (bos) { > + desc = bos->desc; > + desclen = le16_to_cpu(desc->wTotalLength); > + if (off < desclen) { > + n = min(count, desclen - (size_t) off); > + memcpy(buf, (void *) desc + off, n); > + } > + } > + return n; If bos is not present an error should return, not 0, right? > +} > + > +static struct bin_attribute dev_bin_attr_bos_descriptor = { > + .attr = {.name = "bos_descriptor", .mode = 0444}, > + .read = read_bos_descriptor, Isn't there a mscro for all of this? > + .size = 65535, /* max-size BOS descriptor */ Do we know the size of the descriptor at runtime? Ideally we would set it to be that, not an unknown size that way userspace knows what needs to be read. But that's not really a big deal, if it's not possible, as the other USB binary descriptor does much the same. > +}; This should go into an attribute group with the other descriptor, making the rest of the patch not needed and simpler. > + > /* > * Show & store the current value of authorized_default > */ > @@ -1042,6 +1070,10 @@ int usb_create_sysfs_dev_files(struct usb_device *udev) > if (retval) > goto error; > > + retval = device_create_bin_file(dev, &dev_bin_attr_bos_descriptor); > + if (retval) > + goto error; > + long-term I'd like to get rid of the individual "add and remove" sysfs files in this function, and just use the default groups instead so the driver core handles it. It's not an issue here for this change, but if you can find a way to work on that as part of a patch series here, I think that would be really good (if possible, not a requirement.) thanks! greg k-h
On 2024-03-01 06:18, Greg KH wrote: > On Thu, Feb 29, 2024 at 03:59:05PM -0800, Elbert Mai wrote: >> Motivation >> ---------- >> >> The kernel already retrieves and caches the binary device object store >> (BOS) descriptor from USB devices it enumerates. Export this >> descriptor to >> userspace via sysfs, so users do not need to open the USB device with >> the >> correct permissions and requesting the descriptor themselves. >> >> A BOS descriptor contains a set of device capability descriptors. One >> that >> is of interest to users is the platform descriptor. This contains a >> 128-bit >> UUID and arbitrary data. The descriptor allows parties outside of >> USB-IF to >> add additional metadata about a device in a standards-compliant >> manner. >> >> Notable examples include the WebUSB and Microsoft OS 2.0 descriptors. >> Of >> course, there could be more. By exporting the entire BOS descriptor we >> can >> handle these and all future device capabilities. In addition, tools >> like >> udev can match rules on device capabilities in the BOS without >> requiring >> additional I/O with the USB device. > > But this requires that userspace parse the raw descriptor, right? Why > not also export the descriptor information in simpler form through > different sysfs files as well so that scripts can read this information > easily? > > That's not to say we don't also want this binary file, just maybe add > more? I've thought about this for a while, but IMHO it doesn't seem possible to present the BOS in a simpler form like how it's done for the device and configuration descriptors. The BOS was explicitly designed to be flexible and extensible, and it's hard to make a "rich" sysfs interface around that. We could have a platform_uuids file that lists all platform UUIDs in the BOS. These could be presented in RFC 4122 string format, each separated by some delimiter. This would make it possible to write a udev rule that matches a particular UUID via a glob pattern. This seems like a rather inelegant way of handling this, IMHO. With regards to parsing raw descriptors in userspace, this already happens at least with udev. About a dozen files in /usr/lib/udev/rules.d invoke a built-in program called "usb_id", which opens the descriptors binary file (among other files in sysfs) and parses it for interface information. I think users and scripts interested in reading the BOS are best served with userspace tools that can parse the BOS and present it in a convenient textual format of their choosing. Having bos_descriptor in sysfs makes this possible without needing elevated permissions or USB I/O. But I could be mistaken in this regard. >> Implementation >> -------------- >> >> Add bos_descriptor file to sysfs. This is a binary file and it works >> the >> same way as the existing descriptors file. The file exists even if a >> device >> does not have a BOS descriptor (the file will be empty in this case). >> This >> allows users to detect if the kernel supports reading the BOS via >> sysfs and >> fall back to direct USB I/O if needed. > > This is nice, but can you only create the file if it actually is > present? Use the is_visible callback to determine this, having an > "empty" binary sysfs file isn't that nice. Of course, I can make that happen. > Other tiny review comments below: > >> Signed-off-by: Elbert Mai <code@elbertmai.com> >> --- >> Documentation/ABI/testing/sysfs-bus-usb | 9 +++++++ >> drivers/usb/core/sysfs.c | 35 >> ++++++++++++++++++++++++- >> 2 files changed, 43 insertions(+), 1 deletion(-) >> >> diff --git a/Documentation/ABI/testing/sysfs-bus-usb >> b/Documentation/ABI/testing/sysfs-bus-usb >> index 614d216dff1d..bfffaa752a13 100644 >> --- a/Documentation/ABI/testing/sysfs-bus-usb >> +++ b/Documentation/ABI/testing/sysfs-bus-usb >> @@ -293,3 +293,12 @@ Description: >> USB 3.2 adds Dual-lane support, 2 rx and 2 tx -lanes over Type-C. >> Inter-Chip SSIC devices support asymmetric lanes up to 4 lanes per >> direction. Devices before USB 3.2 are single lane (tx_lanes = 1) >> + >> +What: /sys/bus/usb/devices/.../bos_descriptor >> +Date: March 2024 >> +Contact: Elbert Mai <code@elbertmai.com> >> +Description: >> + Binary file containing the cached binary device object store (BOS) >> + descriptor of the device. This file is empty if the BOS descriptor >> + is not present. The kernel will not request a BOS descriptor from >> + the device if its bcdUSB value is less than 0x0201. >> diff --git a/drivers/usb/core/sysfs.c b/drivers/usb/core/sysfs.c >> index a2ca38e25e0c..208d2f8cde2d 100644 >> --- a/drivers/usb/core/sysfs.c >> +++ b/drivers/usb/core/sysfs.c >> @@ -901,7 +901,7 @@ read_descriptors(struct file *filp, struct kobject >> *kobj, >> srclen = sizeof(struct usb_device_descriptor); >> } else { >> src = udev->rawdescriptors[cfgno]; >> - srclen = __le16_to_cpu(udev->config[cfgno].desc. >> + srclen = le16_to_cpu(udev->config[cfgno].desc. > > Why is this line changed? Consistency with the rest of the file. This was the only instance of __le16_to_cpu, other lines used le16_to_cpu (no underscores). >> wTotalLength); >> } >> if (off < srclen) { >> @@ -923,6 +923,34 @@ static struct bin_attribute >> dev_bin_attr_descriptors = { >> .size = 18 + 65535, /* dev descr + max-size raw descriptor */ >> }; >> >> +static ssize_t >> +read_bos_descriptor(struct file *filp, struct kobject *kobj, >> + struct bin_attribute *attr, >> + char *buf, loff_t off, size_t count) >> +{ >> + struct device *dev = kobj_to_dev(kobj); >> + struct usb_device *udev = to_usb_device(dev); >> + struct usb_host_bos *bos = udev->bos; >> + struct usb_bos_descriptor *desc; >> + size_t desclen, n = 0; >> + >> + if (bos) { >> + desc = bos->desc; >> + desclen = le16_to_cpu(desc->wTotalLength); >> + if (off < desclen) { >> + n = min(count, desclen - (size_t) off); >> + memcpy(buf, (void *) desc + off, n); >> + } >> + } >> + return n; > > If bos is not present an error should return, not 0, right? The interface_show function in sysfs.c returns 0 if the string describing the interface is not present, so I did it the same way. Should I instead return -ENOENT or some other specific error code? >> +} >> + >> +static struct bin_attribute dev_bin_attr_bos_descriptor = { >> + .attr = {.name = "bos_descriptor", .mode = 0444}, >> + .read = read_bos_descriptor, > > Isn't there a mscro for all of this? It turns out there is, called BIN_ATTR_RO in in linux/sysfs.h. I'm not sure why the original code didn't use it for read_descriptors. I can change both instances to shorten the code. >> + .size = 65535, /* max-size BOS descriptor */ > > Do we know the size of the descriptor at runtime? Ideally we would set > it to be that, not an unknown size that way userspace knows what needs > to be read. But that's not really a big deal, if it's not possible, as > the other USB binary descriptor does much the same. We do know the size via wTotalLength, but that varies between USB devices and dev_bin_attr_bos_descriptor is a static variable. So not really possible unless we dynamically allocate all the attribute structs. >> +}; > > This should go into an attribute group with the other descriptor, > making > the rest of the patch not needed and simpler. Will do. I'm in favor of simpler patches. >> + >> /* >> * Show & store the current value of authorized_default >> */ >> @@ -1042,6 +1070,10 @@ int usb_create_sysfs_dev_files(struct >> usb_device *udev) >> if (retval) >> goto error; >> >> + retval = device_create_bin_file(dev, &dev_bin_attr_bos_descriptor); >> + if (retval) >> + goto error; >> + > > long-term I'd like to get rid of the individual "add and remove" sysfs > files in this function, and just use the default groups instead so the > driver core handles it. It's not an issue here for this change, but if > you can find a way to work on that as part of a patch series here, I > think that would be really good (if possible, not a requirement.) I'll see what I can do. I'll go for at least the low-hanging fruit. > thanks! > > greg k-h And thank you for taking the time to review this patch! -Elbert
diff --git a/Documentation/ABI/testing/sysfs-bus-usb b/Documentation/ABI/testing/sysfs-bus-usb index 614d216dff1d..bfffaa752a13 100644 --- a/Documentation/ABI/testing/sysfs-bus-usb +++ b/Documentation/ABI/testing/sysfs-bus-usb @@ -293,3 +293,12 @@ Description: USB 3.2 adds Dual-lane support, 2 rx and 2 tx -lanes over Type-C. Inter-Chip SSIC devices support asymmetric lanes up to 4 lanes per direction. Devices before USB 3.2 are single lane (tx_lanes = 1) + +What: /sys/bus/usb/devices/.../bos_descriptor +Date: March 2024 +Contact: Elbert Mai <code@elbertmai.com> +Description: + Binary file containing the cached binary device object store (BOS) + descriptor of the device. This file is empty if the BOS descriptor + is not present. The kernel will not request a BOS descriptor from + the device if its bcdUSB value is less than 0x0201. diff --git a/drivers/usb/core/sysfs.c b/drivers/usb/core/sysfs.c index a2ca38e25e0c..208d2f8cde2d 100644 --- a/drivers/usb/core/sysfs.c +++ b/drivers/usb/core/sysfs.c @@ -901,7 +901,7 @@ read_descriptors(struct file *filp, struct kobject *kobj, srclen = sizeof(struct usb_device_descriptor); } else { src = udev->rawdescriptors[cfgno]; - srclen = __le16_to_cpu(udev->config[cfgno].desc. + srclen = le16_to_cpu(udev->config[cfgno].desc. wTotalLength); } if (off < srclen) { @@ -923,6 +923,34 @@ static struct bin_attribute dev_bin_attr_descriptors = { .size = 18 + 65535, /* dev descr + max-size raw descriptor */ }; +static ssize_t +read_bos_descriptor(struct file *filp, struct kobject *kobj, + struct bin_attribute *attr, + char *buf, loff_t off, size_t count) +{ + struct device *dev = kobj_to_dev(kobj); + struct usb_device *udev = to_usb_device(dev); + struct usb_host_bos *bos = udev->bos; + struct usb_bos_descriptor *desc; + size_t desclen, n = 0; + + if (bos) { + desc = bos->desc; + desclen = le16_to_cpu(desc->wTotalLength); + if (off < desclen) { + n = min(count, desclen - (size_t) off); + memcpy(buf, (void *) desc + off, n); + } + } + return n; +} + +static struct bin_attribute dev_bin_attr_bos_descriptor = { + .attr = {.name = "bos_descriptor", .mode = 0444}, + .read = read_bos_descriptor, + .size = 65535, /* max-size BOS descriptor */ +}; + /* * Show & store the current value of authorized_default */ @@ -1042,6 +1070,10 @@ int usb_create_sysfs_dev_files(struct usb_device *udev) if (retval) goto error; + retval = device_create_bin_file(dev, &dev_bin_attr_bos_descriptor); + if (retval) + goto error; + retval = add_persist_attributes(dev); if (retval) goto error; @@ -1071,6 +1103,7 @@ void usb_remove_sysfs_dev_files(struct usb_device *udev) remove_power_attributes(dev); remove_persist_attributes(dev); + device_remove_bin_file(dev, &dev_bin_attr_bos_descriptor); device_remove_bin_file(dev, &dev_bin_attr_descriptors); }