From patchwork Thu Feb 29 23:59:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elbert Mai X-Patchwork-Id: 13577766 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 C7D0444374 for ; Thu, 29 Feb 2024 23:59:37 +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=1709251180; cv=none; b=obLLcxOi9RZU13CL8rh7B5l5iUml6Lo9DeZ0TnXIdOrlxnAhkn1vRU7Y59eePt/WenLYIW5kRlqJ6ZPkHtAO1n0pT+PkrB5BHmtkA45cREhp8ml4GxQooExeELcfE70mtgiaMqYgvcDGla3Jaa9azsYD1Bzj/ho5pVe5ie3umxw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709251180; c=relaxed/simple; bh=OOOCsGxQH/wxU/CG8sZCXZFdEfx3IZ9cD1UMgPqoejI=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version:Content-Type; b=GNSxXAqPBS4ou1OKBu5u43E2jgMNThAmjEcR1RT4Xo6v7QhYv5K95GveRuYmLk2h7KlMhWm9P12E3r2cF9zUqkIgb7alTIJVx3rdb1/eMbhx721VoajD1w2ibC4IVB1g+fE9fd/B4E3Opwy7AQkoUFHUQqJB5HDzodng0ZGERew= 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: 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" 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-usb@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 To: gregkh@linuxfoundation.org, linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org Cc: Elbert Mai 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-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MIME-Autoconverted: from 8bit to quoted-printable by Purelymail 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 --- 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 +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); }