[PATCH 6/7] rust: usb: remove the disconnect() callback

From: Danilo Krummrich

Date: Wed Nov 11 2026 - 20:09:55 EST


The driver model uses native Rust lifetimes to let driver data retain
device resources and an Interface<Bound> reference for the duration of
the binding. Both remain accessible from the destructor, so a separate
disconnect() callback is no longer needed.

Remove usb::Driver::disconnect() and document that device teardown
belongs in Drop or PinnedDrop.

Signed-off-by: Danilo Krummrich <dakr@xxxxxxxxxx>
---
rust/kernel/usb.rs | 36 ++++++---------------------------
samples/rust/rust_driver_usb.rs | 11 +++++-----
2 files changed, 12 insertions(+), 35 deletions(-)

diff --git a/rust/kernel/usb.rs b/rust/kernel/usb.rs
index 993760ff332b..2126378abc95 100644
--- a/rust/kernel/usb.rs
+++ b/rust/kernel/usb.rs
@@ -98,22 +98,8 @@ extern "C" fn probe_callback(
})
}

- extern "C" fn disconnect_callback(intf: *mut bindings::usb_interface) {
- // SAFETY: The USB core only ever calls the disconnect callback with a valid pointer to a
- // `struct usb_interface`.
- //
- // INVARIANT: `intf` is valid for the duration of `disconnect_callback()`.
- let intf = unsafe { &*intf.cast::<Interface<device::CoreInternal<'_>>>() };
-
- let dev: &device::Device<device::CoreInternal<'_>> = intf.as_ref();
-
- // SAFETY: `disconnect_callback` is only ever called after a successful call to
- // `probe_callback`, hence it's guaranteed that `Device::set_drvdata()` has been called
- // and stored a `Pin<KBox<T::Data<'_>>>`.
- let data = unsafe { dev.drvdata_borrow::<T::Data<'_>>() };
-
- T::disconnect(intf, data);
- }
+ // The USB core calls `disconnect` unconditionally.
+ extern "C" fn disconnect_callback(_intf: *mut bindings::usb_interface) {}
}

/// Abstraction for the USB device ID structure, i.e. [`struct usb_device_id`].
@@ -278,12 +264,6 @@ macro_rules! usb_device_table {
/// ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound {
/// Err(ENODEV)
/// }
-///
-/// fn disconnect<'bound>(
-/// _interface: &'bound usb::Interface<Core<'_>>,
-/// _data: Pin<&Self::Data<'bound>>,
-/// ) {
-/// }
/// }
///```
pub trait Driver {
@@ -291,6 +271,10 @@ pub trait Driver {
type IdInfo: 'static;

/// The type of the driver's bus device private data.
+ ///
+ /// Device teardown should be performed in [`Drop`] or [`PinnedDrop`](pin_init::PinnedDrop)
+ /// for this type. The `'bound` lifetime allows the data to retain device resources and an
+ /// [`Interface<device::Bound>`] reference for use during teardown.
type Data<'bound>: Send + 'bound;

/// The table of device ids supported by the driver.
@@ -305,14 +289,6 @@ fn probe<'bound>(
id: &DeviceId,
id_info: Option<&'bound Self::IdInfo>,
) -> impl PinInit<Self::Data<'bound>, Error> + 'bound;
-
- /// USB driver disconnect.
- ///
- /// Called when the USB interface is about to be unbound from this driver.
- fn disconnect<'bound>(
- interface: &'bound Interface<device::Core<'_>>,
- data: Pin<&Self::Data<'bound>>,
- );
}

/// A USB interface.
diff --git a/samples/rust/rust_driver_usb.rs b/samples/rust/rust_driver_usb.rs
index 7ef04e177c80..1349fa1a66d1 100644
--- a/samples/rust/rust_driver_usb.rs
+++ b/samples/rust/rust_driver_usb.rs
@@ -14,7 +14,7 @@
};

struct SampleDriver {
- _intf: ARef<usb::Interface>,
+ intf: ARef<usb::Interface>,
}

kernel::usb_device_table!(
@@ -36,12 +36,13 @@ fn probe<'bound>(
let dev: &device::Device<Core<'_>> = intf.as_ref();
dev_info!(dev, "Rust USB driver sample probed\n");

- Ok(Self { _intf: intf.into() })
+ Ok(Self { intf: intf.into() })
}
+}

- fn disconnect<'bound>(intf: &'bound usb::Interface<Core<'_>>, _data: Pin<&Self>) {
- let dev: &device::Device<Core<'_>> = intf.as_ref();
- dev_info!(dev, "Rust USB driver sample disconnected\n");
+impl Drop for SampleDriver {
+ fn drop(&mut self) {
+ dev_info!(self.intf, "Rust USB driver sample disconnected\n");
}
}

--
2.56.0