diff options
| author | Danilo Krummrich <dakr@kernel.org> | 2025-10-16 14:55:28 +0200 |
|---|---|---|
| committer | Danilo Krummrich <dakr@kernel.org> | 2025-10-21 18:40:48 +0200 |
| commit | 0242623384c767b1156b61b67894b4ecf6682b8b (patch) | |
| tree | af5131064b1aac4ecdafe8884a95464429fab020 /rust/kernel/platform.rs | |
| parent | fc2b38de4c01710ecb9ebb9ecdce9a7bf433e9a8 (diff) | |
rust: driver: let probe() return impl PinInit<Self, Error>
The driver model defines the lifetime of the private data stored in (and
owned by) a bus device to be valid from when the driver is bound to a
device (i.e. from successful probe()) until the driver is unbound from
the device.
This is already taken care of by the Rust implementation of the driver
model. However, we still ask drivers to return a Result<Pin<KBox<Self>>>
from probe().
Unlike in C, where we do not have the concept of initializers, but
rather deal with uninitialized memory, drivers can just return an
impl PinInit<Self, Error> instead.
This contributes to more clarity to the fact that a driver returns it's
device private data in probe() and the Rust driver model owns the data,
manages the lifetime and - considering the lifetime - provides (safe)
accessors for the driver.
Hence, let probe() functions return an impl PinInit<Self, Error> instead
of Result<Pin<KBox<Self>>>.
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Acked-by: Viresh Kumar <viresh.kumar@linaro.org>
Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
Diffstat (limited to 'rust/kernel/platform.rs')
| -rw-r--r-- | rust/kernel/platform.rs | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/rust/kernel/platform.rs b/rust/kernel/platform.rs index 7205fe3416d3..043721fdb6d8 100644 --- a/rust/kernel/platform.rs +++ b/rust/kernel/platform.rs @@ -74,9 +74,9 @@ impl<T: Driver + 'static> Adapter<T> { let info = <Self as driver::Adapter>::id_info(pdev.as_ref()); from_result(|| { - let data = T::probe(pdev, info)?; + let data = T::probe(pdev, info); - pdev.as_ref().set_drvdata(data); + pdev.as_ref().set_drvdata(data)?; Ok(0) }) } @@ -166,7 +166,7 @@ macro_rules! module_platform_driver { /// fn probe( /// _pdev: &platform::Device<Core>, /// _id_info: Option<&Self::IdInfo>, -/// ) -> Result<Pin<KBox<Self>>> { +/// ) -> impl PinInit<Self, Error> { /// Err(ENODEV) /// } /// } @@ -190,8 +190,10 @@ pub trait Driver: Send { /// /// Called when a new platform device is added or discovered. /// Implementers should attempt to initialize the device here. - fn probe(dev: &Device<device::Core>, id_info: Option<&Self::IdInfo>) - -> Result<Pin<KBox<Self>>>; + fn probe( + dev: &Device<device::Core>, + id_info: Option<&Self::IdInfo>, + ) -> impl PinInit<Self, Error>; /// Platform driver unbind. /// |