[RFC PATCH 4/9] rust: usb: add usb::Interface::number()
From: Mike Lothian
Date: Wed Jun 17 2026 - 11:24:02 EST
Add a safe accessor returning an interface's bInterfaceNumber (from its
current alternate setting). A USB function driver's probe() is called once
per matching interface; a multi-interface device therefore needs to know
which interface it was handed so it can drive its protocol on the right one
and stay idle on the others. This exposes that number to Rust drivers.
Signed-off-by: Mike Lothian <mike@xxxxxxxxxxxxxx>
Assisted-by: Claude:claude-opus-4-8 [Claude-Code]
---
rust/kernel/usb.rs | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/rust/kernel/usb.rs b/rust/kernel/usb.rs
index 73637e3467f1..683e8b52d6d1 100644
--- a/rust/kernel/usb.rs
+++ b/rust/kernel/usb.rs
@@ -357,6 +357,17 @@ impl<Ctx: device::DeviceContext> Interface<Ctx> {
fn as_raw(&self) -> *mut bindings::usb_interface {
self.0.get()
}
+
+ /// Returns the interface number (`bInterfaceNumber`) of this interface's
+ /// current alternate setting.
+ pub fn number(&self) -> u8 {
+ // SAFETY: `self.as_raw()` is a valid `struct usb_interface` pointer by the
+ // type invariant, and a bound interface always has a `cur_altsetting`.
+ unsafe {
+ let altsetting = (*self.as_raw()).cur_altsetting;
+ (*altsetting).desc.bInterfaceNumber
+ }
+ }
}
// SAFETY: `usb::Interface` is a transparent wrapper of `struct usb_interface`.
--
2.54.0