[PATCH 5/7] rust: i2c: remove the unbind() callback
From: Danilo Krummrich
Date: Wed Nov 11 2026 - 20:16:26 EST
The driver model uses native Rust lifetimes to let driver data retain
device resources and a Device<Bound> reference for the duration of the
binding. Both remain accessible from the destructor, so a separate
unbind() callback is no longer needed.
Remove i2c::Driver::unbind() and the adapter's remove callback and
document that device teardown belongs in Drop or PinnedDrop.
Signed-off-by: Danilo Krummrich <dakr@xxxxxxxxxx>
---
rust/kernel/i2c.rs | 31 ++++---------------------------
samples/rust/rust_driver_i2c.rs | 18 ++++++++++++------
2 files changed, 16 insertions(+), 33 deletions(-)
diff --git a/rust/kernel/i2c.rs b/rust/kernel/i2c.rs
index b4ff25c830fc..b15e73bf827a 100644
--- a/rust/kernel/i2c.rs
+++ b/rust/kernel/i2c.rs
@@ -123,7 +123,6 @@ unsafe fn register(
unsafe {
(*idrv.get()).driver.name = name.as_char_ptr();
(*idrv.get()).probe = Some(Self::probe_callback);
- (*idrv.get()).remove = Some(Self::remove_callback);
(*idrv.get()).shutdown = Some(Self::shutdown_callback);
(*idrv.get()).id_table = i2c_table;
(*idrv.get()).driver.of_match_table = of_table;
@@ -161,18 +160,6 @@ extern "C" fn probe_callback(idev: *mut bindings::i2c_client) -> kernel::ffi::c_
})
}
- extern "C" fn remove_callback(idev: *mut bindings::i2c_client) {
- // SAFETY: `idev` is a valid pointer to a `struct i2c_client`.
- let idev = unsafe { &*idev.cast::<I2cClient<device::CoreInternal<'_>>>() };
-
- // SAFETY: `remove_callback` is only ever called after a successful call to
- // `probe_callback`, hence it's guaranteed that `I2cClient::set_drvdata()` has been called
- // and stored a `Pin<KBox<T::Data<'_>>>`.
- let data = unsafe { idev.as_ref().drvdata_borrow::<T::Data<'_>>() };
-
- T::unbind(idev, data);
- }
-
extern "C" fn shutdown_callback(idev: *mut bindings::i2c_client) {
// SAFETY: `shutdown_callback` is only ever called for a valid `idev`
let idev = unsafe { &*idev.cast::<I2cClient<device::CoreInternal<'_>>>() };
@@ -312,6 +299,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 a
+ /// [`I2cClient<device::Bound>`] reference for use during teardown.
type Data<'bound>: Send + 'bound;
/// The table of device ids supported by the driver.
@@ -346,20 +337,6 @@ fn probe<'bound>(
fn shutdown<'bound>(dev: &'bound I2cClient<device::Core<'_>>, this: Pin<&Self::Data<'bound>>) {
let _ = (dev, this);
}
-
- /// I2C driver unbind.
- ///
- /// Called when the [`I2cClient`] is unbound from its bound [`Driver`]. Implementing this
- /// callback is optional.
- ///
- /// This callback serves as a place for drivers to perform teardown operations that require a
- /// `&Device<Core>` or `&Device<Bound>` reference. For instance, drivers may try to perform I/O
- /// operations to gracefully tear down the device.
- ///
- /// Otherwise, release operations for driver resources should be performed in `Drop`.
- fn unbind<'bound>(dev: &'bound I2cClient<device::Core<'_>>, this: Pin<&Self::Data<'bound>>) {
- let _ = (dev, this);
- }
}
/// The i2c adapter representation.
diff --git a/samples/rust/rust_driver_i2c.rs b/samples/rust/rust_driver_i2c.rs
index a0df0c6097c4..4f5047ab4e84 100644
--- a/samples/rust/rust_driver_i2c.rs
+++ b/samples/rust/rust_driver_i2c.rs
@@ -12,6 +12,10 @@
struct SampleDriver;
+struct SampleDriverData<'bound> {
+ idev: &'bound i2c::I2cClient,
+}
+
kernel::acpi_device_table! {
ACPI_TABLE,
<SampleDriver as i2c::Driver>::IdInfo,
@@ -32,7 +36,7 @@
impl i2c::Driver for SampleDriver {
type IdInfo = u32;
- type Data<'bound> = Self;
+ type Data<'bound> = SampleDriverData<'bound>;
const ACPI_ID_TABLE: Option<acpi::IdTable<Self::IdInfo>> = Some(&ACPI_TABLE);
const I2C_ID_TABLE: Option<i2c::IdTable<Self::IdInfo>> = Some(&I2C_TABLE);
@@ -41,7 +45,7 @@ impl i2c::Driver for SampleDriver {
fn probe<'bound>(
idev: &'bound i2c::I2cClient<Core<'_>>,
info: Option<&'bound Self::IdInfo>,
- ) -> impl PinInit<Self, Error> + 'bound {
+ ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound {
let dev = idev.as_ref();
dev_info!(dev, "Probe Rust I2C driver sample.\n");
@@ -50,15 +54,17 @@ fn probe<'bound>(
dev_info!(dev, "Probed with info: '{}'.\n", info);
}
- Ok(Self)
+ Ok(SampleDriverData { idev })
}
- fn shutdown<'bound>(idev: &'bound i2c::I2cClient<Core<'_>>, _this: Pin<&Self>) {
+ fn shutdown<'bound>(idev: &'bound i2c::I2cClient<Core<'_>>, _this: Pin<&Self::Data<'bound>>) {
dev_info!(idev.as_ref(), "Shutdown Rust I2C driver sample.\n");
}
+}
- fn unbind<'bound>(idev: &'bound i2c::I2cClient<Core<'_>>, _this: Pin<&Self>) {
- dev_info!(idev.as_ref(), "Unbind Rust I2C driver sample.\n");
+impl Drop for SampleDriverData<'_> {
+ fn drop(&mut self) {
+ dev_info!(self.idev, "Unbind Rust I2C driver sample.\n");
}
}
--
2.56.0