[RFC PATCH v2 0/7] virtio-usb: add dual-role virtio USB driver
From: Igor Skalkin
Date: Tue Dec 01 2026 - 12:36:10 EST
This series adds a driver for the virtio USB device: a dual-role
paravirtualized USB controller. Each port of the device is either a
USB host controller port, backed by a per-port HS+SS usb_hcd pair, or
a USB device controller (UDC). Dual-role ports can switch between the
two roles at runtime, and the switch is exposed through the USB role
switch class. This is a device-driven role switch, not USB OTG:
there is no HNP/SRP, ID pin or OTG descriptor involved.
The device is described in the virtio-usb specification proposal,
currently at RFC v3 on virtio-comment:
https://lore.kernel.org/virtio-comment/20260611130826.287372-1-vasilii.ianikeev@xxxxxxxxxxxxxxxx/T/
This series is an RFC because the specification is still under
review. The protocol header in patch 2 follows spec RFC v3.
The driver was tested against two device backends: a proprietary one
and one based on QEMU. The QEMU backend is currently under review and
will be sent upstream soon.
Series layout:
- 1/7 syncs the device ID (49) from the virtio specification into
virtio_ids.h. The driver does not build without it.
- 2/7 adds the protocol header and the driver skeleton.
- 3/7 and 4/7 add the host and the device role.
- 5/7 adds the per-port role query, which is needed when the device
offers both roles.
- 6/7 adds runtime host/device role switching.
- 7/7 lets the device supply a name prefix for a port's UDC.
Changes since v1:
- Marked as RFC, since the specification is still under review.
- Rebased onto v7.4-rc5. The virtio_ids.h patch is now part of the
series (1/7).
- Series restructured: v1 6/8 ("rework endpoint lifecycle to an
async split-phase state machine") and v1 7/8 ("add SuperSpeed
device-role support") are folded into the device role patch
(4/7). The OTG patches are renamed to "per-port role query" (5/7)
and "host/device role switch" (6/7).
- checkpatch, sparse and kernel-doc cleanups throughout.
[2/7] protocol header and skeleton
- uapi enums converted to explicit #defines.
- OTG naming replaced by role naming: VIRTIO_USB_VQ_ROLE_*,
VIRTIO_USB_CMD_ROLE_GET/SET, struct virtio_usb_role_set carrying
the target role, struct virtio_usb_role_info,
VIRTIO_USB_EVT_ROLE_CHANGE.
- Host port event field port_id renamed to port.
- VIRTIO_USB_SPEED_WIRELESS dropped; VIRTIO_USB_USB_SPEED_SUPER
renamed to VIRTIO_USB_SPEED_SUPER.
- uapi header relicensed from BSD-3-Clause to GPL-2.0-or-later
WITH Linux-syscall-note, like the rest of the driver.
[3/7] host role
- The cmd_timeout_ms module parameter is removed; the command
timeout is the constant VIRTIO_USB_CMD_TIMEOUT_MS (1 s).
- Each data submission gets a tag from a per-device 64-bit counter,
and HOST_CANCEL refers to that tag instead of the URB address,
which could match a reused allocation.
- Fixed a race where dequeue ran before the URB was on the data
virtqueue: the HOST_CANCEL matched nothing on the device, so the
URB was never cancelled. Dequeue now only records the cancel, and
enqueue sends it once the URB is queued.
- skip_phy_initialization is now also set on the SuperSpeed HCD.
[4/7] device role
- Folds in v1 6/8: an explicit endpoint state machine, with
workqueues and a pending request queue, so that the
usb_ep_ops/usb_gadget_ops callbacks never sleep. No gadget core
change is needed.
- Folds in v1 7/8: max_speed is taken from the BIND event, EP0
maxpacket is set for SS/SSP, and the SS endpoint companion
descriptor is validated and sent with EP_ENABLE.
- DEV_CANCEL uses a per-submission tag instead of the request
address.
- Fixed the same early-cancel race as on the host side. A stale
cancel is dropped if the request has completed and been queued
again.
- ep_queue() on a halted endpoint returns the error without giving
the request back, which avoids recursion with gadget drivers that
resubmit from their completion handler.
- Events with non-zero reserved padding are rejected.
- UNBIND flushes the endpoint work before freeing the endpoints
(use-after-free fix).
[5/7] per-port role query (was: OTG role query)
- otg.c/otg.h renamed to role_switch.c/role_switch.h, otg_* to
virtio_usb_rsw_*, is_otg to is_dual_role.
- The ROLE_* queues are created only when both roles are
negotiated.
[6/7] host/device role switch (was: OTG role-switching)
- Role switches work in both directions from the guest. The role
switch set callback sends ROLE_SET with the target role, then
reads the role back with ROLE_GET and follows it, even if
ROLE_SET failed. v1 could only request device-to-host,
asynchronously, and carried no target role.
- ROLE_CHANGE events and guest requests share one resync path,
serialized by a per-port mutex.
- Host port events that arrive before the HCD is installed are
stored per port and replayed once it is. v1 dropped them.
- On remove, the role switches are unregistered and the role event
work is cancelled before the queues are stopped.
- Probe runs the event workers once after setting started, so
events delivered during probe are not lost.
[7/7] UDC name prefix
- No functional change.
v1: https://lore.kernel.org/all/20260924160907.145405-1-igor.skalkin@xxxxxxxxxxxxxxxx/
Aiswarya Cyriac (3):
virtio-usb: add protocol header and skeleton dual-role driver
virtio-usb: add host role (USB Host Controller) support
virtio-usb: add device role (USB Device Controller) support
Igor Skalkin (4):
uapi: virtio_ids: reserve ID for virtio USB device
virtio-usb: add per-port role query support
virtio-usb: add host/device role switch support
virtio-usb: support a guest UDC name prefix from the bind event
MAINTAINERS | 10 +
drivers/usb/Kconfig | 2 +
drivers/usb/Makefile | 1 +
drivers/usb/virtio_usb/Kconfig | 15 +
drivers/usb/virtio_usb/Makefile | 9 +
drivers/usb/virtio_usb/controller.c | 437 ++++++
drivers/usb/virtio_usb/controller.h | 185 +++
drivers/usb/virtio_usb/device.c | 2055 ++++++++++++++++++++++++++
drivers/usb/virtio_usb/device.h | 126 ++
drivers/usb/virtio_usb/host.c | 1555 +++++++++++++++++++
drivers/usb/virtio_usb/host.h | 244 +++
drivers/usb/virtio_usb/role_switch.c | 502 +++++++
drivers/usb/virtio_usb/role_switch.h | 40 +
drivers/usb/virtio_usb/vq_common.c | 747 ++++++++++
drivers/usb/virtio_usb/vq_common.h | 163 ++
include/uapi/linux/virtio_ids.h | 1 +
include/uapi/linux/virtio_usb.h | 321 ++++
17 files changed, 6413 insertions(+)
create mode 100644 drivers/usb/virtio_usb/Kconfig
create mode 100644 drivers/usb/virtio_usb/Makefile
create mode 100644 drivers/usb/virtio_usb/controller.c
create mode 100644 drivers/usb/virtio_usb/controller.h
create mode 100644 drivers/usb/virtio_usb/device.c
create mode 100644 drivers/usb/virtio_usb/device.h
create mode 100644 drivers/usb/virtio_usb/host.c
create mode 100644 drivers/usb/virtio_usb/host.h
create mode 100644 drivers/usb/virtio_usb/role_switch.c
create mode 100644 drivers/usb/virtio_usb/role_switch.h
create mode 100644 drivers/usb/virtio_usb/vq_common.c
create mode 100644 drivers/usb/virtio_usb/vq_common.h
create mode 100644 include/uapi/linux/virtio_usb.h
base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
--
2.49.0