[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-Id: <20260108085545.333676-1-pengfuyuan@kylinos.cn>
Date: Thu, 8 Jan 2026 16:55:45 +0800
From: pengfuyuan <pengfuyuan@...inos.cn>
To: Greg Kroah-Hartman <gregkh@...uxfoundation.org>
Cc: "Rafael J . Wysocki" <rafael@...nel.org>,
Danilo Krummrich <dakr@...nel.org>,
Miguel Ojeda <ojeda@...nel.org>,
Boqun Feng <boqun.feng@...il.com>,
Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Benno Lossin <lossin@...nel.org>,
Andreas Hindborg <a.hindborg@...nel.org>,
Alice Ryhl <aliceryhl@...gle.com>,
Trevor Gross <tmgross@...ch.edu>,
rust-for-linux@...r.kernel.org,
linux-kernel@...r.kernel.org,
pengfuyuan <pengfuyuan@...inos.cn>
Subject: [PATCH] rust: device: add platdata accessors
Implement generic accessors for the platform data of a device.
Platform data is typically set by platform code when creating the device
and points to platform-specific data structures. The accessor provides
type-safe access to this data without requiring unsafe code at the call
site.
The accessor is implemented for Device<Bound>, allowing drivers to access
platform data during probe() and other device lifecycle callbacks. Unlike
drvdata, platform data is managed by platform code and has a lifetime
tied to the device itself.
Signed-off-by: pengfuyuan <pengfuyuan@...inos.cn>
---
rust/kernel/device.rs | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
index c79be2e2bfe3..e16f8eaa52e0 100644
--- a/rust/kernel/device.rs
+++ b/rust/kernel/device.rs
@@ -325,6 +325,31 @@ pub fn drvdata<T: 'static>(&self) -> Result<Pin<&T>> {
// - We've just checked that the type of the driver's private data is in fact `T`.
Ok(unsafe { self.drvdata_unchecked() })
}
+
+ /// Access the platform data for this device.
+ ///
+ /// The lifetime of the platform data is tied to the device's lifetime.
+ /// Returns a reference to the platform data of type `T`, or [`ENOENT`] if no platform data
+ /// is set.
+ ///
+ /// # Type Safety
+ ///
+ /// This function does not perform runtime type checking. The caller must ensure that the
+ /// platform data structure actually matches the type `T` for the specific platform device.
+ /// Incorrect type usage will result in undefined behavior.
+ pub fn platdata<T>(&self) -> Result<&T> {
+ // SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`.
+ let ptr = unsafe { (*self.as_raw()).platform_data };
+
+ if ptr.is_null() {
+ return Err(ENOENT);
+ }
+
+ // SAFETY:
+ // - `ptr` is not null, so it points to valid memory.
+ // - The caller must ensure that the platform data structure matches type `T`.
+ Ok(unsafe { &*ptr.cast::<T>() })
+ }
}
impl<Ctx: DeviceContext> Device<Ctx> {
--
2.25.1
Powered by blists - more mailing lists