Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

rust: platform: impl TryFrom<&Device> for &platform::Device

Implement TryFrom<&device::Device> for &Device.

This allows us to get a &platform::Device from a generic &Device in a safe
way; the conversion fails if the device' bus type does not match with
the platform bus type.

Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Benno Lossin <benno.lossin@proton.me>
Link: https://lore.kernel.org/r/20250321214826.140946-4-dakr@kernel.org
[ Support device context types, use dev_is_platform() helper. - Danilo ]
Signed-off-by: Danilo Krummrich <dakr@kernel.org>

+26 -1
+5
rust/helpers/platform.c
··· 11 11 { 12 12 platform_set_drvdata(pdev, data); 13 13 } 14 + 15 + bool rust_helper_dev_is_platform(const struct device *dev) 16 + { 17 + return dev_is_platform(dev); 18 + }
+21 -1
rust/kernel/platform.rs
··· 5 5 //! C header: [`include/linux/platform_device.h`](srctree/include/linux/platform_device.h) 6 6 7 7 use crate::{ 8 - bindings, device, driver, 8 + bindings, container_of, device, driver, 9 9 error::{to_result, Result}, 10 10 of, 11 11 prelude::*, ··· 215 215 216 216 // SAFETY: `dev` points to a valid `struct device`. 217 217 unsafe { device::Device::as_ref(dev) } 218 + } 219 + } 220 + 221 + impl<Ctx: device::DeviceContext> TryFrom<&device::Device<Ctx>> for &Device<Ctx> { 222 + type Error = kernel::error::Error; 223 + 224 + fn try_from(dev: &device::Device<Ctx>) -> Result<Self, Self::Error> { 225 + // SAFETY: By the type invariant of `Device`, `dev.as_raw()` is a valid pointer to a 226 + // `struct device`. 227 + if !unsafe { bindings::dev_is_platform(dev.as_raw()) } { 228 + return Err(EINVAL); 229 + } 230 + 231 + // SAFETY: We've just verified that the bus type of `dev` equals 232 + // `bindings::platform_bus_type`, hence `dev` must be embedded in a valid 233 + // `struct platform_device` as guaranteed by the corresponding C code. 234 + let pdev = unsafe { container_of!(dev.as_raw(), bindings::platform_device, dev) }; 235 + 236 + // SAFETY: `pdev` is a valid pointer to a `struct platform_device`. 237 + Ok(unsafe { &*pdev.cast() }) 218 238 } 219 239 } 220 240