diff --git a/Documentation/ABI/testing/sysfs-bus-usb-billboard b/Documentation/ABI/testing/sysfs-bus-usb-billboard new file mode 100644 index 0000000000000..c43a112c31c77 --- /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 9d5077f609bbb..99d7180fd7978 100644 --- a/arch/arm64/configs/asahi.config +++ b/arch/arm64/configs/asahi.config @@ -124,6 +124,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..ce77093b3a089 --- /dev/null +++ b/drivers/usb/misc/usb-billboard.c @@ -0,0 +1,434 @@ +// 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; + struct bb_mode_attr attrs[BB_NR_MODE_ATTRS]; +}; + +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); +}; + +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, +}; + +/* Per-mode attributes, billboard/altmode_ */ + +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", +}; + +/* + * 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) +{ + 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; + } + } + + bb->group.name = "billboard"; + bb->group.attrs = attrs; + + return device_add_group(dev, &bb->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 billboard_disconnect() has removed + * the sysfs group that prints them. + */ +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 = bb_add_group(bb); + 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) +{ + 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[] = { + { 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