Skip to content

RFC rust: usb: abstractions towards the port of usbsevseg.c to Rust

Alexandru Radovici driver-core linux-usb lkml rust-for-linux 2 versions – In review

Get series with b4
b4 am [email protected] change-id 20260801-rust-usb_control_msg-ee34e002b818

Versions newest first

  1. v2 RFC Latest4 patches 1 replyIn review
  2. v1 RFC2 patches 6 repliesSuperseded

Changes in v2 from the cover letter

- add sysfs abstractions
- add DEVICE_GROUPS abstractions to usb::Driver using sysfs
- rename the control_ methods correctly
- use Duration instead of i32 for control functions timeout
- fix control function 0 value timeout from "for ever" to USB_MAX_SYNCHRONOUS_TIMEOUT
- add #[inline] to short functions
- use u16::from_le to read wMaxPacketSize
- rename Generic generic type to Dir
- impl max_nak_rate for HostEndpoint<Bidirectional, Control>
- make data &mut [u8] for control_message_receive
- Link to v1: https://lore.kernel.org/r/[email protected]

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/4rust: usb: add endpoint abstractionrust/kernel/usb.rs rust/kernel/usb/endpoint.rs+641 -11 reply
2/4rust: usb: add control message send and receiverust/kernel/usb.rs rust/kernel/usb/control.rs+368 -00 replies
3/4rust: sysfs: add abstractions for device attributesrust/kernel/device.rs rust/kernel/lib.rs rust/kernel/sysfs.rs+605 -10 replies
4/4rust: usb: allow drivers to expose sysfs attributesrust/kernel/usb.rs samples/rust/rust_driver_usb.rs+111 -30 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v2 yet.

Read the v2 thread

Cover letter [PATCH RFC v2 0/4] rust: usb: abstractions towards the port of usbsevseg.c to Rust

My end goal is to port the usbsevseg.c USB driver to Rust. That driver is small enough to make a useful first target; the remaining piece it needs is sysfs attribute support for USB devices, which I plan to send separately.

Porting the driver requires:
- retrieving endpoints and sending control messages API (this series)
- enabling sysfs attributes API for USB devices (future patch)

These patches add the USB and sysfs abstractions needed to port drivers/usb/misc/usbsevseg.c to Rust: retrieving an interface's endpoints, and sending and receiving control messages.

Patch 1 adds AlternateEndpoint, along with AlternateSetting and the accessors needed to reach one. Patch 2 adds the control transfer methods and the Request type describing a setup packet. Patch 3 adds the sysfs abstractions. Patch 4 adds the sysfs abstractions to usb::Driver.

The part I would most like feedback on is the typestate design in patch
1. HostEndpoint is generic over a direction and a transfer type, both sealed 1-ZST markers, so a function can require e.g. &HostEndpoint<In, Bulk> and skip checking the descriptor itself. Control endpoints get a Bidirectional marker rather than a direction, since bit 7 of bEndpointAddress is ignored for them. I am not sure whether this earns its complexity for the drivers we expect, or whether plain run-time check would be better.

Only as_control(), as_in() and as_out() are implemented so far; as_bulk(), as_interrupt() and as_isochronous() are skiped for now as no current user needs them.

I am not sure about the soundness of the sysfs absgtractions, especially the lifetime annotations of DEVICE_GROUPS.
Feedback for this would be greatly appreciated.

---
Changes in v2:
- add sysfs abstractions
- add DEVICE_GROUPS abstractions to usb::Driver using sysfs
- rename the control_ methods correctly
- use Duration instead of i32 for control functions timeout
- fix control function 0 value timeout from "for ever" to USB_MAX_SYNCHRONOUS_TIMEOUT
- add #[inline] to short functions
- use u16::from_le to read wMaxPacketSize
- rename Generic generic type to Dir
- impl max_nak_rate for HostEndpoint<Bidirectional, Control>
- make data &mut [u8] for control_message_receive
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Alexandru Radovici (4):
rust: usb: add endpoint abstraction
rust: usb: add control message send and receive
rust: sysfs: add abstractions for device attributes
rust: usb: allow drivers to expose sysfs attributes

 rust/kernel/device.rs           |   2 +-
 rust/kernel/lib.rs              |   2 +
 rust/kernel/sysfs.rs            | 602 ++++++++++++++++++++++++++++++++++++++++
 rust/kernel/usb.rs              | 227 ++++++++++++++-
 rust/kernel/usb/control.rs      | 367 ++++++++++++++++++++++++
 rust/kernel/usb/endpoint.rs     | 516 ++++++++++++++++++++++++++++++++++
 samples/rust/rust_driver_usb.rs |  14 +
 7 files changed, 1725 insertions(+), 5 deletions(-)

---
base-commit: 8ba098e6b6ff0db8edf28528d1552be261af30d4
change-id: 20260801-rust-usb_control_msg-ee34e002b818

Best regards,

Signature · 1 line
Alexandru Radovici <[email protected]>