From: Lyude Paul <lyude@redhat.com>
To: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: "Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
"Rafael J. Wysocki" <rafael@kernel.org>,
"Danilo Krummrich" <dakr@kernel.org>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <benno.lossin@proton.me>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>
Subject: [PATCH 1/2] rust/kernel: Add platform::Device::from_raw()
Date: Wed, 22 Jan 2025 18:49:21 -0500 [thread overview]
Message-ID: <20250122235340.2145383-2-lyude@redhat.com> (raw)
In-Reply-To: <20250122235340.2145383-1-lyude@redhat.com>
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@redhat.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
next prev parent reply other threads:[~2025-01-22 23:54 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-22 23:49 [PATCH 0/2] rust/kernel: Add bindings for manually creating devices Lyude Paul
2025-01-22 23:49 ` Lyude Paul [this message]
2025-01-28 14:35 ` [PATCH 1/2] rust/kernel: Add platform::Device::from_raw() Alice Ryhl
2025-01-22 23:49 ` [PATCH 2/2] rust/kernel: Add platform::ModuleDevice Lyude Paul
2025-01-23 6:23 ` Greg Kroah-Hartman
2025-01-23 10:21 ` Danilo Krummrich
2025-01-23 14:17 ` Greg Kroah-Hartman
2025-01-24 10:52 ` Danilo Krummrich
2025-01-30 21:28 ` [PATCH] WIP: drivers/base: Add virtual_device_create() Lyude Paul
2025-01-30 21:58 ` Lyude Paul
2025-02-01 8:32 ` Greg Kroah-Hartman
2025-01-31 3:34 ` kernel test robot
2025-01-31 8:00 ` Greg Kroah-Hartman
2025-01-31 16:40 ` Greg Kroah-Hartman
2025-01-31 18:43 ` Danilo Krummrich
2025-02-01 8:00 ` Greg Kroah-Hartman
2025-02-03 9:39 ` [RFC] driver core: add a virtual bus for use when a simple device/bus is needed Greg Kroah-Hartman
2025-02-03 10:02 ` Greg Kroah-Hartman
2025-02-03 11:01 ` Danilo Krummrich
2025-02-03 11:25 ` Greg Kroah-Hartman
2025-02-03 14:33 ` Greg Kroah-Hartman
2025-02-03 15:32 ` Simona Vetter
2025-02-03 15:38 ` Greg Kroah-Hartman
2025-02-03 22:45 ` Lyude Paul
2025-02-03 21:13 ` Danilo Krummrich
2025-02-04 6:05 ` Greg Kroah-Hartman
2025-02-03 9:45 ` [PATCH] WIP: drivers/base: Add virtual_device_create() Simona Vetter
2025-02-03 9:51 ` Greg Kroah-Hartman
2025-01-31 16:42 ` Simona Vetter
2025-01-31 10:43 ` Andy Shevchenko
2025-01-24 0:33 ` [PATCH 2/2] rust/kernel: Add platform::ModuleDevice Lyude Paul
2025-01-24 11:02 ` Danilo Krummrich
2025-01-31 16:41 ` Simona Vetter
2025-01-24 21:19 ` Lyude Paul
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250122235340.2145383-2-lyude@redhat.com \
--to=lyude@redhat.com \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ojeda@kernel.org \
--cc=rafael@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox