Re: [PATCH 1/2] rust/kernel: Add platform::Device::from_raw()
From: Alice Ryhl
Date: Tue Jan 28 2025 - 09:35:47 EST
On Thu, Jan 23, 2025 at 12:54 AM Lyude Paul <lyude@xxxxxxxxxx> wrote:
>
> 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@xxxxxxxxxx>
> ---
> 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`.
I would probably say that the caller must *transfer ownership* of one
refcount or something like that.
Alice