From f94a3f95b9e456a616cd4f887aebca1d8f333575 Mon Sep 17 00:00:00 2001 From: Ryan Murray <81541646+RyanTheTide@users.noreply.github.com> Date: Thu, 19 Nov 2026 22:21:16 +1100 Subject: [PATCH 1/3] usb: misc: add a USB Billboard Device Class driver USB-C docks, adapters and displays that offer Alternate Modes expose a USB 2.0 Billboard device (class 0x11). Its BOS carries a Billboard Capability descriptor listing every mode the device container supports (SVID, mode index, optional string) and a two-bit bmConfigured result per mode telling the host whether the mode was entered, plus optional Billboard AUM Capability descriptors with each mode's VDO. Linux has no driver for the class, so these devices sit unbound in every dock (lsusb -t shows Driver=[none]) and a dock whose DisplayPort path failed says nothing. Bind to the Billboard interface and: - expose the descriptor in sysfs under /billboard/ (num_modes, preferred_mode, vconn_power, version, additional_failure_info, additional_info_url, and altmode/ with svid, mode, vdo, state, description), readable without root; - log one line per mode at bind time, at warning level for modes the device reports as attempted-but-not-entered or as an unspecified error. The descriptor layouts go into the uapi ch9.h so user space can share them. The capability is copied at probe, since a device reset replaces udev->bos. Nothing is sent to the device beyond the string descriptors the core already reads. This follows the 2024 debugfs posting by Niklas Neronin; review asked for the data outside debugfs and for the structs in uapi, which this does. It also fixes that posting's bmConfigured decode, which shifted by the mode index instead of twice the index. Tested on a Realtek RTS5411 dock billboard (0bda:5418, Billboard 1.21, one DisplayPort mode, configured). Link: https://lore.kernel.org/linux-usb/20240206125623.1208161-1-niklas.neronin@linux.intel.com/ Suggested-by: Niklas Neronin Signed-off-by: Ryan Murray --- .../ABI/testing/sysfs-bus-usb-billboard | 63 +++ arch/arm64/configs/asahi.config | 1 + drivers/usb/misc/Kconfig | 12 + drivers/usb/misc/Makefile | 1 + drivers/usb/misc/usb-billboard.c | 416 ++++++++++++++++++ include/uapi/linux/usb/ch9.h | 41 ++ 6 files changed, 534 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-bus-usb-billboard create mode 100644 drivers/usb/misc/usb-billboard.c diff --git a/Documentation/ABI/testing/sysfs-bus-usb-billboard b/Documentation/ABI/testing/sysfs-bus-usb-billboard new file mode 100644 index 0000000000000..fc939f60c353d --- /dev/null +++ b/Documentation/ABI/testing/sysfs-bus-usb-billboard @@ -0,0 +1,63 @@ +What: /sys/bus/usb/devices//billboard/num_modes +Date: November 2026 +Contact: linux-usb@vger.kernel.org +Description: + Number of Alternate Modes listed by the USB Billboard device + (bNumberOfAlternateModes). + +What: /sys/bus/usb/devices//billboard/preferred_mode +Date: November 2026 +Contact: linux-usb@vger.kernel.org +Description: + Index of the Alternate Mode the device prefers + (bPreferredAlternateMode). + +What: /sys/bus/usb/devices//billboard/vconn_power +Date: November 2026 +Contact: linux-usb@vger.kernel.org +Description: + VCONN power the device needs to run all its Alternate Modes: + "1W", "1.5W", "2W", "3W", "4W", "5W", "6W", "not-required" or + "reserved". + +What: /sys/bus/usb/devices//billboard/version +Date: November 2026 +Contact: linux-usb@vger.kernel.org +Description: + Billboard specification version the device implements + (bcdVersion), e.g. "1.21". + +What: /sys/bus/usb/devices//billboard/additional_failure_info +Date: November 2026 +Contact: linux-usb@vger.kernel.org +Description: + bAdditionalFailureInfo as reported by the device, in hex. + +What: /sys/bus/usb/devices//billboard/additional_info_url +Date: November 2026 +Contact: linux-usb@vger.kernel.org +Description: + The device's iAddtionalInfoURL string, empty if it has none. + +What: /sys/bus/usb/devices//billboard/altmode/svid +What: /sys/bus/usb/devices//billboard/altmode/mode +What: /sys/bus/usb/devices//billboard/altmode/vdo +What: /sys/bus/usb/devices//billboard/altmode/description +Date: November 2026 +Contact: linux-usb@vger.kernel.org +Description: + Alternate Mode as listed by the device: the Standard or + Vendor ID in hex (wSVID), the mode index within that SVID + (bAlternateMode), the mode's VDO from the Billboard AUM + Capability descriptor (only present when the device provides + one) and the iAlternateModeString string (empty if none). + +What: /sys/bus/usb/devices//billboard/altmode/state +Date: November 2026 +Contact: linux-usb@vger.kernel.org +Description: + Result of the host's attempt to enter Alternate Mode , from + bmConfigured: "configured", "not-attempted" (not attempted or + exited), "failed" (attempted but not entered) or "error" + (unspecified error). The driver also logs a warning for each + "failed" or "error" mode when it binds. diff --git a/arch/arm64/configs/asahi.config b/arch/arm64/configs/asahi.config index 9921de094ba9f..6e01176125b0b 100644 --- a/arch/arm64/configs/asahi.config +++ b/arch/arm64/configs/asahi.config @@ -113,6 +113,7 @@ CONFIG_SPI_HID_APPLE_OF=m CONFIG_SPI_HID_APPLE_CORE=m CONFIG_USB_DWC3_APPLE=m CONFIG_USB_VL122_HUB=m +CONFIG_USB_BILLBOARD=m CONFIG_TYPEC=m CONFIG_USB4=m CONFIG_USB4_APPLE_SOC=m diff --git a/drivers/usb/misc/Kconfig b/drivers/usb/misc/Kconfig index 8f5abd07655e8..47680f6ee11ff 100644 --- a/drivers/usb/misc/Kconfig +++ b/drivers/usb/misc/Kconfig @@ -156,6 +156,18 @@ config USB_APPLEDISPLAY Say Y here if you want to control the backlight of Apple Cinema Displays over USB. This driver provides a sysfs interface. +config USB_BILLBOARD + tristate "USB Billboard Device Class support" + help + Say Y here to support USB Billboard devices. A USB-C dock, adapter + or display that offers Alternate Modes (DisplayPort, Thunderbolt) + exposes a Billboard device listing those modes and whether the + host entered them. This driver shows that information in sysfs and + logs a warning for each mode the device reports as not entered. + + To compile this driver as a module, choose M here: the module will + be called usb-billboard. + config USB_QCOM_EUD tristate "Qualcomm Embedded USB Debugger(EUD) Driver" depends on ARCH_QCOM || COMPILE_TEST diff --git a/drivers/usb/misc/Makefile b/drivers/usb/misc/Makefile index 2e39cfa84fa0b..c5d6c21e17215 100644 --- a/drivers/usb/misc/Makefile +++ b/drivers/usb/misc/Makefile @@ -5,6 +5,7 @@ # obj-$(CONFIG_USB_ADUTUX) += adutux.o obj-$(CONFIG_USB_APPLEDISPLAY) += appledisplay.o +obj-$(CONFIG_USB_BILLBOARD) += usb-billboard.o obj-$(CONFIG_USB_CYPRESS_CY7C63) += cypress_cy7c63.o obj-$(CONFIG_USB_CYTHERM) += cytherm.o obj-$(CONFIG_USB_EMI26) += emi26.o diff --git a/drivers/usb/misc/usb-billboard.c b/drivers/usb/misc/usb-billboard.c new file mode 100644 index 0000000000000..337ddb1404001 --- /dev/null +++ b/drivers/usb/misc/usb-billboard.c @@ -0,0 +1,416 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * USB Billboard Device Class driver + * + * A USB-C device that offers Alternate Modes (DisplayPort, Thunderbolt, ...) + * exposes a USB 2.0 Billboard device. Its BOS descriptor lists the modes the + * device container supports and, for each one, whether the host managed to + * enter it. This driver binds to the Billboard interface, exposes that + * information in sysfs (readable without root, unlike debugfs) and logs a + * warning for every mode the device reports as not entered, so a dock or + * adapter whose display path failed is no longer silent. + * + * Reference: USB Billboard Device Class Specification, Revision 1.22. + * + * Based on the debugfs driver posted by Niklas Neronin in 2024, reworked + * around sysfs and the review comments on that posting. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* Revision 1.22 allows at most 0x34 Alternate or USB4 modes. */ +#define BB_MAX_MODES 52 + +/* Billboard Capability descriptor, VCONNPower. */ +#define BB_VCONN_NOT_REQUIRED BIT(15) +#define BB_VCONN_POWER_MASK GENMASK(2, 0) + +/* Billboard Capability descriptor, bmConfigured: two bits per mode. */ +enum bb_mode_state { + BB_STATE_ERROR = 0, /* unspecified error */ + BB_STATE_NOT_ATTEMPTED = 1, /* not attempted, or exited */ + BB_STATE_FAILED = 2, /* attempted, not entered */ + BB_STATE_CONFIGURED = 3, /* entered */ +}; + +static const char * const bb_state_names[] = { + [BB_STATE_ERROR] = "error", + [BB_STATE_NOT_ATTEMPTED] = "not-attempted", + [BB_STATE_FAILED] = "failed", + [BB_STATE_CONFIGURED] = "configured", +}; + +static const char * const bb_state_text[] = { + [BB_STATE_ERROR] = "unspecified error", + [BB_STATE_NOT_ATTEMPTED] = "not attempted or exited", + [BB_STATE_FAILED] = "attempted but not entered", + [BB_STATE_CONFIGURED] = "configured", +}; + +struct bb_mode; + +struct bb_mode_attr { + struct device_attribute attr; + struct bb_mode *mode; +}; + +enum { + BB_ATTR_SVID, + BB_ATTR_MODE, + BB_ATTR_VDO, + BB_ATTR_STATE, + BB_ATTR_DESC, + BB_NR_MODE_ATTRS +}; + +struct bb_mode { + u16 svid; + u8 mode; + bool has_vdo; + u32 vdo; + enum bb_mode_state state; + char *desc; + char name[16]; + struct bb_mode_attr attrs[BB_NR_MODE_ATTRS]; + struct attribute *attr_ptrs[BB_NR_MODE_ATTRS + 1]; + struct attribute_group group; +}; + +struct usb_billboard { + struct usb_interface *intf; + const struct usb_billboard_cap_descriptor *cap; + char *url; + unsigned int nr_modes; + struct bb_mode modes[] __counted_by(nr_modes); +}; + +static const char *bb_svid_name(u16 svid) +{ + switch (svid) { + case USB_TYPEC_DP_SID: + return "DisplayPort"; + case USB_TYPEC_TBT_SID: + return "Thunderbolt"; + default: + return NULL; + } +} + +static enum bb_mode_state bb_state(const struct usb_billboard_cap_descriptor *cap, + unsigned int i) +{ + return (cap->bmConfigured[i / 4] >> ((i % 4) * 2)) & 0x3; +} + +/* Device-wide attributes */ + +static ssize_t additional_info_url_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct usb_billboard *bb = usb_get_intfdata(to_usb_interface(dev)); + + return sysfs_emit(buf, "%s\n", bb->url ?: ""); +} +static DEVICE_ATTR_RO(additional_info_url); + +static ssize_t preferred_mode_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct usb_billboard *bb = usb_get_intfdata(to_usb_interface(dev)); + + return sysfs_emit(buf, "%u\n", bb->cap->bPreferredAlternateMode); +} +static DEVICE_ATTR_RO(preferred_mode); + +static ssize_t vconn_power_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + static const char * const watts[] = { + "1W", "1.5W", "2W", "3W", "4W", "5W", "6W", + }; + struct usb_billboard *bb = usb_get_intfdata(to_usb_interface(dev)); + u16 vconn = le16_to_cpu(bb->cap->VCONNPower); + unsigned int idx = vconn & BB_VCONN_POWER_MASK; + + if (vconn & BB_VCONN_NOT_REQUIRED) + return sysfs_emit(buf, "not-required\n"); + if (idx >= ARRAY_SIZE(watts)) + return sysfs_emit(buf, "reserved\n"); + return sysfs_emit(buf, "%s\n", watts[idx]); +} +static DEVICE_ATTR_RO(vconn_power); + +static ssize_t version_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct usb_billboard *bb = usb_get_intfdata(to_usb_interface(dev)); + u16 v = le16_to_cpu(bb->cap->bcdVersion); + + return sysfs_emit(buf, "%x.%02x\n", v >> 8, v & 0xff); +} +static DEVICE_ATTR_RO(version); + +static ssize_t additional_failure_info_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct usb_billboard *bb = usb_get_intfdata(to_usb_interface(dev)); + + return sysfs_emit(buf, "0x%02x\n", bb->cap->bAdditionalFailureInfo); +} +static DEVICE_ATTR_RO(additional_failure_info); + +static ssize_t num_modes_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct usb_billboard *bb = usb_get_intfdata(to_usb_interface(dev)); + + return sysfs_emit(buf, "%u\n", bb->nr_modes); +} +static DEVICE_ATTR_RO(num_modes); + +static struct attribute *bb_attrs[] = { + &dev_attr_additional_info_url.attr, + &dev_attr_preferred_mode.attr, + &dev_attr_vconn_power.attr, + &dev_attr_version.attr, + &dev_attr_additional_failure_info.attr, + &dev_attr_num_modes.attr, + NULL +}; + +static const struct attribute_group bb_group = { + .name = "billboard", + .attrs = bb_attrs, +}; + +/* Per-mode attributes, in billboard/altmodeN */ + +static ssize_t bb_mode_show(struct device *dev, struct device_attribute *attr, + char *buf) +{ + struct bb_mode_attr *ma = container_of(attr, struct bb_mode_attr, attr); + struct bb_mode *m = ma->mode; + + switch (ma - m->attrs) { + case BB_ATTR_SVID: + return sysfs_emit(buf, "%04x\n", m->svid); + case BB_ATTR_MODE: + return sysfs_emit(buf, "%u\n", m->mode); + case BB_ATTR_VDO: + return sysfs_emit(buf, "0x%08x\n", m->vdo); + case BB_ATTR_STATE: + return sysfs_emit(buf, "%s\n", bb_state_names[m->state]); + case BB_ATTR_DESC: + return sysfs_emit(buf, "%s\n", m->desc ?: ""); + } + return -EINVAL; +} + +static const char * const bb_mode_attr_names[BB_NR_MODE_ATTRS] = { + [BB_ATTR_SVID] = "svid", + [BB_ATTR_MODE] = "mode", + [BB_ATTR_VDO] = "vdo", + [BB_ATTR_STATE] = "state", + [BB_ATTR_DESC] = "description", +}; + +static int bb_add_mode_group(struct usb_billboard *bb, unsigned int i) +{ + struct bb_mode *m = &bb->modes[i]; + unsigned int a, n = 0; + + snprintf(m->name, sizeof(m->name), "altmode%u", i); + for (a = 0; a < BB_NR_MODE_ATTRS; a++) { + struct bb_mode_attr *ma = &m->attrs[a]; + + if (a == BB_ATTR_VDO && !m->has_vdo) + continue; + sysfs_attr_init(&ma->attr.attr); + ma->attr.attr.name = bb_mode_attr_names[a]; + ma->attr.attr.mode = 0444; + ma->attr.show = bb_mode_show; + ma->mode = m; + m->attr_ptrs[n++] = &ma->attr.attr; + } + m->attr_ptrs[n] = NULL; + m->group.name = m->name; + m->group.attrs = m->attr_ptrs; + + return devm_device_add_group(&bb->intf->dev, &m->group); +} + +/* Descriptor parsing */ + +/* + * Returns the Billboard capability (type USB_BILLBOARD_CAP_TYPE) or the + * Billboard AUM capability for mode @index (type USB_BILLBOARD_AUM_CAP_TYPE). + */ +static const void *bb_find_cap(struct usb_device *udev, u8 type, u8 index) +{ + const struct usb_bos_descriptor *bos; + const u8 *p, *end; + unsigned int n; + + if (!udev->bos) + return NULL; + + bos = udev->bos->desc; + p = (const u8 *)bos + bos->bLength; + end = (const u8 *)bos + le16_to_cpu(bos->wTotalLength); + + for (n = 0; n < bos->bNumDeviceCaps && p + 3 <= end; n++) { + const struct usb_dev_cap_header *hdr = (const void *)p; + + if (hdr->bLength < 3 || p + hdr->bLength > end) + break; + if (hdr->bDescriptorType == USB_DT_DEVICE_CAPABILITY && + hdr->bDevCapabilityType == type) { + if (type == USB_BILLBOARD_CAP_TYPE) + return p; + if (hdr->bLength >= USB_DT_BILLBOARD_AUM_CAP_SIZE && + ((const struct usb_billboard_aum_cap_descriptor *)p)->bIndex == index) + return p; + } + p += hdr->bLength; + } + return NULL; +} + +static void bb_kfree(void *p) +{ + kfree(p); +} + +/* + * Strings are released by devres after the sysfs groups that print them, + * because devres runs in reverse order and the strings are registered first. + */ +static char *bb_string(struct usb_interface *intf, u8 index) +{ + char *s; + + if (!index) + return NULL; + s = usb_cache_string(interface_to_usbdev(intf), index); + if (s && devm_add_action_or_reset(&intf->dev, bb_kfree, s)) + return NULL; + return s; +} + +static void bb_log_mode(struct usb_interface *intf, unsigned int i, + const struct bb_mode *m) +{ + const char *name = bb_svid_name(m->svid); + bool bad = m->state == BB_STATE_FAILED || m->state == BB_STATE_ERROR; + + dev_printk(bad ? KERN_WARNING : KERN_INFO, &intf->dev, + "alternate mode %u: SVID %04x%s%s mode %u%s%s: %s\n", + i, m->svid, name ? " " : "", name ?: "", m->mode, + m->desc ? ", " : "", m->desc ?: "", + bb_state_text[m->state]); +} + +static int billboard_probe(struct usb_interface *intf, + const struct usb_device_id *id) +{ + struct usb_device *udev = interface_to_usbdev(intf); + const struct usb_billboard_cap_descriptor *cap; + struct usb_billboard *bb; + unsigned int i, nr; + bool failed = false; + int ret; + + cap = bb_find_cap(udev, USB_BILLBOARD_CAP_TYPE, 0); + if (!cap) { + dev_warn(&intf->dev, "no Billboard capability descriptor\n"); + return -ENODEV; + } + + nr = cap->bLength >= USB_DT_BILLBOARD_CAP_SIZE(0) ? + cap->bNumberOfAlternateModes : 0; + if (!nr || nr > BB_MAX_MODES || cap->bLength < USB_DT_BILLBOARD_CAP_SIZE(nr)) { + dev_warn(&intf->dev, + "malformed Billboard capability (length %u, %u modes)\n", + cap->bLength, nr); + return -EINVAL; + } + + bb = devm_kzalloc(&intf->dev, struct_size(bb, modes, nr), GFP_KERNEL); + if (!bb) + return -ENOMEM; + bb->nr_modes = nr; + bb->intf = intf; + /* Keep a copy: a device reset replaces udev->bos. */ + cap = devm_kmemdup(&intf->dev, cap, cap->bLength, GFP_KERNEL); + if (!cap) + return -ENOMEM; + bb->cap = cap; + bb->url = bb_string(intf, cap->iAddtionalInfoURL); + + for (i = 0; i < nr; i++) { + const struct usb_billboard_aum_cap_descriptor *aum; + struct bb_mode *m = &bb->modes[i]; + + m->svid = le16_to_cpu(cap->aum[i].wSVID); + m->mode = cap->aum[i].bAlternateMode; + m->state = bb_state(cap, i); + m->desc = bb_string(intf, cap->aum[i].iAlternateModeString); + aum = bb_find_cap(udev, USB_BILLBOARD_AUM_CAP_TYPE, i); + if (aum) { + m->has_vdo = true; + m->vdo = le32_to_cpu(aum->dwAlternateModeVdo); + } + if (m->state == BB_STATE_FAILED || m->state == BB_STATE_ERROR) + failed = true; + } + + usb_set_intfdata(intf, bb); + + ret = devm_device_add_group(&intf->dev, &bb_group); + if (ret) + return ret; + for (i = 0; i < nr; i++) { + ret = bb_add_mode_group(bb, i); + if (ret) + return ret; + } + + for (i = 0; i < nr; i++) + bb_log_mode(intf, i, &bb->modes[i]); + if (failed && cap->bAdditionalFailureInfo) + dev_warn(&intf->dev, "additional failure info 0x%02x\n", + cap->bAdditionalFailureInfo); + + return 0; +} + +static void billboard_disconnect(struct usb_interface *intf) +{ + /* Everything is device-managed. */ +} + +static const struct usb_device_id billboard_ids[] = { + { USB_INTERFACE_INFO(USB_CLASS_BILLBOARD, 0, 0) }, + { } +}; +MODULE_DEVICE_TABLE(usb, billboard_ids); + +static struct usb_driver billboard_driver = { + .name = "usb-billboard", + .probe = billboard_probe, + .disconnect = billboard_disconnect, + .id_table = billboard_ids, +}; +module_usb_driver(billboard_driver); + +MODULE_DESCRIPTION("USB Billboard Device Class driver"); +MODULE_LICENSE("GPL"); diff --git a/include/uapi/linux/usb/ch9.h b/include/uapi/linux/usb/ch9.h index c3e5933783773..02341b89c8ceb 100644 --- a/include/uapi/linux/usb/ch9.h +++ b/include/uapi/linux/usb/ch9.h @@ -1143,6 +1143,47 @@ struct usb_ptm_cap_descriptor { } __attribute__((packed)); #define USB_DT_USB_PTM_ID_SIZE 3 + +/*-------------------------------------------------------------------------*/ + +/* + * Billboard Capability and Billboard AUM Capability descriptors + * (USB Billboard Device Class Specification, Revision 1.21). + * Field names follow the specification, including iAddtionalInfoURL. + */ +#define USB_BILLBOARD_CAP_TYPE 0x0d +#define USB_BILLBOARD_AUM_CAP_TYPE 0x0f + +struct usb_billboard_cap_descriptor { + __u8 bLength; + __u8 bDescriptorType; /* USB_DT_DEVICE_CAPABILITY */ + __u8 bDevCapabilityType; /* USB_BILLBOARD_CAP_TYPE */ + __u8 iAddtionalInfoURL; + __u8 bNumberOfAlternateModes; + __u8 bPreferredAlternateMode; + __le16 VCONNPower; + __u8 bmConfigured[32]; /* 2 bits per Alternate Mode */ + __le16 bcdVersion; + __u8 bAdditionalFailureInfo; + __u8 bReserved; + struct { + __le16 wSVID; + __u8 bAlternateMode; + __u8 iAlternateModeString; + } __attribute__((packed)) aum[]; +} __attribute__((packed)); + +#define USB_DT_BILLBOARD_CAP_SIZE(n) (44 + 4 * (n)) + +struct usb_billboard_aum_cap_descriptor { + __u8 bLength; + __u8 bDescriptorType; /* USB_DT_DEVICE_CAPABILITY */ + __u8 bDevCapabilityType; /* USB_BILLBOARD_AUM_CAP_TYPE */ + __u8 bIndex; + __le32 dwAlternateModeVdo; +} __attribute__((packed)); + +#define USB_DT_BILLBOARD_AUM_CAP_SIZE 8 /* * The size of the descriptor for the Sublink Speed Attribute Count * (SSAC) specified in bmAttributes[4:0]. SSAC is zero-based From f7205f4da3f74664706e47b7e5c5fd635dc85528 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 20 Nov 2026 00:46:09 +1100 Subject: [PATCH 2/3] usb: misc: usb-billboard: keep the per-mode files inside billboard/ Sysfs attribute groups cannot nest, so the per-mode "altmode" groups landed next to "billboard" on the interface instead of inside it. Build one "billboard" group holding the device-wide attributes and altmode_{svid,mode,vdo,state,description} for each mode, and update the ABI document to match. Signed-off-by: Ryan Murray --- .../ABI/testing/sysfs-bus-usb-billboard | 10 +-- drivers/usb/misc/usb-billboard.c | 81 ++++++++++--------- 2 files changed, 50 insertions(+), 41 deletions(-) diff --git a/Documentation/ABI/testing/sysfs-bus-usb-billboard b/Documentation/ABI/testing/sysfs-bus-usb-billboard index fc939f60c353d..c43a112c31c77 100644 --- a/Documentation/ABI/testing/sysfs-bus-usb-billboard +++ b/Documentation/ABI/testing/sysfs-bus-usb-billboard @@ -39,10 +39,10 @@ Contact: linux-usb@vger.kernel.org Description: The device's iAddtionalInfoURL string, empty if it has none. -What: /sys/bus/usb/devices//billboard/altmode/svid -What: /sys/bus/usb/devices//billboard/altmode/mode -What: /sys/bus/usb/devices//billboard/altmode/vdo -What: /sys/bus/usb/devices//billboard/altmode/description +What: /sys/bus/usb/devices//billboard/altmode_svid +What: /sys/bus/usb/devices//billboard/altmode_mode +What: /sys/bus/usb/devices//billboard/altmode_vdo +What: /sys/bus/usb/devices//billboard/altmode_description Date: November 2026 Contact: linux-usb@vger.kernel.org Description: @@ -52,7 +52,7 @@ Description: Capability descriptor (only present when the device provides one) and the iAlternateModeString string (empty if none). -What: /sys/bus/usb/devices//billboard/altmode/state +What: /sys/bus/usb/devices//billboard/altmode_state Date: November 2026 Contact: linux-usb@vger.kernel.org Description: diff --git a/drivers/usb/misc/usb-billboard.c b/drivers/usb/misc/usb-billboard.c index 337ddb1404001..0d36f9027f828 100644 --- a/drivers/usb/misc/usb-billboard.c +++ b/drivers/usb/misc/usb-billboard.c @@ -78,16 +78,14 @@ struct bb_mode { u32 vdo; enum bb_mode_state state; char *desc; - char name[16]; struct bb_mode_attr attrs[BB_NR_MODE_ATTRS]; - struct attribute *attr_ptrs[BB_NR_MODE_ATTRS + 1]; - struct attribute_group group; }; struct usb_billboard { struct usb_interface *intf; const struct usb_billboard_cap_descriptor *cap; char *url; + struct attribute_group group; unsigned int nr_modes; struct bb_mode modes[] __counted_by(nr_modes); }; @@ -184,15 +182,9 @@ static struct attribute *bb_attrs[] = { &dev_attr_version.attr, &dev_attr_additional_failure_info.attr, &dev_attr_num_modes.attr, - NULL -}; - -static const struct attribute_group bb_group = { - .name = "billboard", - .attrs = bb_attrs, }; -/* Per-mode attributes, in billboard/altmodeN */ +/* Per-mode attributes, billboard/altmode_ */ static ssize_t bb_mode_show(struct device *dev, struct device_attribute *attr, char *buf) @@ -223,29 +215,51 @@ static const char * const bb_mode_attr_names[BB_NR_MODE_ATTRS] = { [BB_ATTR_DESC] = "description", }; -static int bb_add_mode_group(struct usb_billboard *bb, unsigned int i) +/* + * Sysfs groups cannot nest, so everything lives in one "billboard" group: + * the device-wide attributes plus altmode_ for each mode. + */ +static int bb_add_group(struct usb_billboard *bb) { - struct bb_mode *m = &bb->modes[i]; - unsigned int a, n = 0; - - snprintf(m->name, sizeof(m->name), "altmode%u", i); - for (a = 0; a < BB_NR_MODE_ATTRS; a++) { - struct bb_mode_attr *ma = &m->attrs[a]; - - if (a == BB_ATTR_VDO && !m->has_vdo) - continue; - sysfs_attr_init(&ma->attr.attr); - ma->attr.attr.name = bb_mode_attr_names[a]; - ma->attr.attr.mode = 0444; - ma->attr.show = bb_mode_show; - ma->mode = m; - m->attr_ptrs[n++] = &ma->attr.attr; + struct device *dev = &bb->intf->dev; + struct attribute **attrs; + unsigned int i, a, n = 0; + + attrs = devm_kcalloc(dev, ARRAY_SIZE(bb_attrs) + + bb->nr_modes * BB_NR_MODE_ATTRS + 1, + sizeof(*attrs), GFP_KERNEL); + if (!attrs) + return -ENOMEM; + + for (a = 0; a < ARRAY_SIZE(bb_attrs); a++) + attrs[n++] = bb_attrs[a]; + + for (i = 0; i < bb->nr_modes; i++) { + struct bb_mode *m = &bb->modes[i]; + + for (a = 0; a < BB_NR_MODE_ATTRS; a++) { + struct bb_mode_attr *ma = &m->attrs[a]; + const char *name; + + if (a == BB_ATTR_VDO && !m->has_vdo) + continue; + name = devm_kasprintf(dev, GFP_KERNEL, "altmode%u_%s", + i, bb_mode_attr_names[a]); + if (!name) + return -ENOMEM; + sysfs_attr_init(&ma->attr.attr); + ma->attr.attr.name = name; + ma->attr.attr.mode = 0444; + ma->attr.show = bb_mode_show; + ma->mode = m; + attrs[n++] = &ma->attr.attr; + } } - m->attr_ptrs[n] = NULL; - m->group.name = m->name; - m->group.attrs = m->attr_ptrs; - return devm_device_add_group(&bb->intf->dev, &m->group); + bb->group.name = "billboard"; + bb->group.attrs = attrs; + + return devm_device_add_group(dev, &bb->group); } /* Descriptor parsing */ @@ -375,14 +389,9 @@ static int billboard_probe(struct usb_interface *intf, usb_set_intfdata(intf, bb); - ret = devm_device_add_group(&intf->dev, &bb_group); + ret = bb_add_group(bb); if (ret) return ret; - for (i = 0; i < nr; i++) { - ret = bb_add_mode_group(bb, i); - if (ret) - return ret; - } for (i = 0; i < nr; i++) bb_log_mode(intf, i, &bb->modes[i]); From 5cdb7b2489e6d67f1c1e136b4b4c4447cc2fa160 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 08:32:37 +1100 Subject: [PATCH 3/3] usb: misc: usb-billboard: remove the sysfs group before intfdata is cleared The device-wide attributes (version, num_modes, vconn_power, ...) look up their state with usb_get_intfdata(). The billboard group was added with devm_device_add_group(), so devres removed it only in device_unbind_cleanup(), after usb_unbind_interface() had already called usb_set_intfdata(intf, NULL). A read of billboard/version racing an unbind or an unplug could therefore dereference a NULL struct usb_billboard. Add the group with device_add_group() and remove it in billboard_disconnect(), which runs while the interface data is still set. Sysfs removal waits for show() calls already running, so once disconnect returns no reader can see the cleared pointer, and the devres-managed state (the struct, the attribute array, the names and the strings) is freed only after the files are gone. The driver core's dev_groups do not fit here because the group is built per device: its file names depend on the number of modes the device reports. Signed-off-by: Ryan Murray --- drivers/usb/misc/usb-billboard.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/drivers/usb/misc/usb-billboard.c b/drivers/usb/misc/usb-billboard.c index 0d36f9027f828..ce77093b3a089 100644 --- a/drivers/usb/misc/usb-billboard.c +++ b/drivers/usb/misc/usb-billboard.c @@ -218,6 +218,12 @@ static const char * const bb_mode_attr_names[BB_NR_MODE_ATTRS] = { /* * Sysfs groups cannot nest, so everything lives in one "billboard" group: * the device-wide attributes plus altmode_ for each mode. + * + * The group is not device-managed: devres would remove it only after + * usb_unbind_interface() has cleared the interface data that the + * device-wide attributes read. billboard_disconnect() removes it instead, + * while the data is still set; sysfs removal waits for running show() + * calls, so none can see the cleared pointer or the freed state. */ static int bb_add_group(struct usb_billboard *bb) { @@ -259,7 +265,7 @@ static int bb_add_group(struct usb_billboard *bb) bb->group.name = "billboard"; bb->group.attrs = attrs; - return devm_device_add_group(dev, &bb->group); + return device_add_group(dev, &bb->group); } /* Descriptor parsing */ @@ -305,8 +311,8 @@ static void bb_kfree(void *p) } /* - * Strings are released by devres after the sysfs groups that print them, - * because devres runs in reverse order and the strings are registered first. + * Strings are released by devres, after billboard_disconnect() has removed + * the sysfs group that prints them. */ static char *bb_string(struct usb_interface *intf, u8 index) { @@ -404,7 +410,10 @@ static int billboard_probe(struct usb_interface *intf, static void billboard_disconnect(struct usb_interface *intf) { - /* Everything is device-managed. */ + struct usb_billboard *bb = usb_get_intfdata(intf); + + /* Before the core clears intfdata; the rest is device-managed. */ + device_remove_group(&intf->dev, &bb->group); } static const struct usb_device_id billboard_ids[] = {