aboutsummaryrefslogtreecommitdiffstats
path: root/rust/kernel/platform.rs
diff options
context:
space:
mode:
authorDanilo Krummrich <dakr@kernel.org>2025-06-21 21:43:29 +0200
committerDanilo Krummrich <dakr@kernel.org>2025-07-09 00:04:33 +0200
commitf0a68a912c673d8899d863c2f01f1ef7006e0b11 (patch)
treed37c10badd87a0144a433624a16f3969febace09 /rust/kernel/platform.rs
parentrust: device: add drvdata accessors (diff)
downloadlinux-f0a68a912c673d8899d863c2f01f1ef7006e0b11.tar.gz
linux-f0a68a912c673d8899d863c2f01f1ef7006e0b11.zip
rust: platform: use generic device drvdata accessors
Take advantage of the generic drvdata accessors of the generic Device type. While at it, use from_result() instead of match. Link: https://lore.kernel.org/r/20250621195118.124245-4-dakr@kernel.org Signed-off-by: Danilo Krummrich <dakr@kernel.org>
Diffstat (limited to 'rust/kernel/platform.rs')
-rw-r--r--rust/kernel/platform.rs36
1 files changed, 17 insertions, 19 deletions
diff --git a/rust/kernel/platform.rs b/rust/kernel/platform.rs
index 7484d7e5c2cf..e9e95c5bb02b 100644
--- a/rust/kernel/platform.rs
+++ b/rust/kernel/platform.rs
@@ -6,10 +6,10 @@
use crate::{
acpi, bindings, container_of, device, driver,
- error::{to_result, Result},
+ error::{from_result, to_result, Result},
of,
prelude::*,
- types::{ForeignOwnable, Opaque},
+ types::Opaque,
ThisModule,
};
@@ -66,30 +66,28 @@ impl<T: Driver + 'static> Adapter<T> {
// `struct platform_device`.
//
// INVARIANT: `pdev` is valid for the duration of `probe_callback()`.
- let pdev = unsafe { &*pdev.cast::<Device<device::Core>>() };
-
+ let pdev = unsafe { &*pdev.cast::<Device<device::CoreInternal>>() };
let info = <Self as driver::Adapter>::id_info(pdev.as_ref());
- match T::probe(pdev, info) {
- Ok(data) => {
- // Let the `struct platform_device` own a reference of the driver's private data.
- // SAFETY: By the type invariant `pdev.as_raw` returns a valid pointer to a
- // `struct platform_device`.
- unsafe { bindings::platform_set_drvdata(pdev.as_raw(), data.into_foreign() as _) };
- }
- Err(err) => return Error::to_errno(err),
- }
- 0
+ from_result(|| {
+ let data = T::probe(pdev, info)?;
+
+ pdev.as_ref().set_drvdata(data);
+ Ok(0)
+ })
}
extern "C" fn remove_callback(pdev: *mut bindings::platform_device) {
- // SAFETY: `pdev` is a valid pointer to a `struct platform_device`.
- let ptr = unsafe { bindings::platform_get_drvdata(pdev) }.cast();
+ // SAFETY: The platform bus only ever calls the remove callback with a valid pointer to a
+ // `struct platform_device`.
+ //
+ // INVARIANT: `pdev` is valid for the duration of `probe_callback()`.
+ let pdev = unsafe { &*pdev.cast::<Device<device::CoreInternal>>() };
// SAFETY: `remove_callback` is only ever called after a successful call to
- // `probe_callback`, hence it's guaranteed that `ptr` points to a valid and initialized
- // `KBox<T>` pointer created through `KBox::into_foreign`.
- let _ = unsafe { KBox::<T>::from_foreign(ptr) };
+ // `probe_callback`, hence it's guaranteed that `Device::set_drvdata()` has been called
+ // and stored a `Pin<KBox<T>>`.
+ let _ = unsafe { pdev.as_ref().drvdata_obtain::<Pin<KBox<T>>>() };
}
}