[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250122235340.2145383-2-lyude@redhat.com>
Date: Wed, 22 Jan 2025 18:49:21 -0500
From: Lyude Paul <lyude@...hat.com>
To: rust-for-linux@...r.kernel.org,
linux-kernel@...r.kernel.org
Cc: Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
"Rafael J. Wysocki" <rafael@...nel.org>,
Danilo Krummrich <dakr@...nel.org>,
Miguel Ojeda <ojeda@...nel.org>,
Alex Gaynor <alex.gaynor@...il.com>,
Boqun Feng <boqun.feng@...il.com>,
Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Benno Lossin <benno.lossin@...ton.me>,
Andreas Hindborg <a.hindborg@...nel.org>,
Alice Ryhl <aliceryhl@...gle.com>,
Trevor Gross <tmgross@...ch.edu>
Subject: [PATCH 1/2] rust/kernel: Add platform::Device::from_raw()
Just a convenience method to convert from a raw struct platform_device
pointer into a platform::Device object, which we'll be using in the next
commit.
Signed-off-by: Lyude Paul <lyude@...hat.com>
---
rust/kernel/platform.rs | 17 ++++++++++++++++-
1 file changed, 16 insertions(+), 1 deletion(-)
diff --git a/rust/kernel/platform.rs b/rust/kernel/platform.rs
index 50e6b04218132..75dc7824eccf4 100644
--- a/rust/kernel/platform.rs
+++ b/rust/kernel/platform.rs
@@ -14,7 +14,7 @@
ThisModule,
};
-use core::ptr::addr_of_mut;
+use core::ptr::{NonNull, addr_of_mut};
/// An adapter for the registration of platform drivers.
pub struct Adapter<T: Driver>(T);
@@ -186,6 +186,21 @@ unsafe fn from_dev(dev: ARef<device::Device>) -> Self {
Self(dev)
}
+ /// Convert a raw pointer to a `struct platform_device` into a `Device`.
+ ///
+ /// # Safety
+ ///
+ /// * `pdev` must be a valid pointer to a `bindings::platform_device`.
+ /// * The caller must be guaranteed to hold at least one reference to `pdev`.
+ unsafe fn from_raw(pdev: *mut bindings::platform_device) -> Self {
+ // SAFETY:
+ // * Our safety contract ensures `pdev` is a valid pointer which we hold at least one
+ // reference to.
+ // * struct device and `device::Device` have equivalent data layouts via the
+ // `device::Device` type invariants.
+ Self(unsafe { ARef::from_raw(NonNull::new_unchecked(addr_of_mut!((*pdev).dev).cast())) })
+ }
+
fn as_raw(&self) -> *mut bindings::platform_device {
// SAFETY: By the type invariant `self.0.as_raw` is a pointer to the `struct device`
// embedded in `struct platform_device`.
--
2.47.1
Powered by blists - more mailing lists