[PATCH v4 2/9] rust: device: Enable accessing the FwNode of a Device
From: Remo Senekowitsch
Date: Sun May 04 2025 - 13:39:07 EST
The FwNode is an abstraction over the C struct fwnode_handle. It will be
used to access all device properties.
Signed-off-by: Remo Senekowitsch <remo@xxxxxxxxxxx>
---
rust/kernel/device/mod.rs | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git a/rust/kernel/device/mod.rs b/rust/kernel/device/mod.rs
index d8619d4485fb4..b4b7056eb80f8 100644
--- a/rust/kernel/device/mod.rs
+++ b/rust/kernel/device/mod.rs
@@ -186,6 +186,21 @@ unsafe fn printk(&self, klevel: &[u8], msg: fmt::Arguments<'_>) {
};
}
+ /// Obtain the [`FwNode`](property::FwNode) corresponding to the device.
+ pub fn fwnode(&self) -> Option<&property::FwNode> {
+ // SAFETY: `self` is valid.
+ let fwnode_handle = unsafe { bindings::__dev_fwnode(self.as_raw()) };
+ if fwnode_handle.is_null() {
+ return None;
+ }
+ // SAFETY: `fwnode_handle` is valid. Its lifetime is tied to `&self`. We
+ // return a reference instead of an `ARef<FwNode>` because `dev_fwnode()`
+ // doesn't increment the refcount. It is safe to cast from a
+ // `struct fwnode_handle*` to a `*const FwNode` because `FwNode` is
+ // defined as a `#[repr(transparent)]` wrapper around `fwnode_handle`.
+ Some(unsafe { &*fwnode_handle.cast() })
+ }
+
/// Checks if property is present or not.
pub fn property_present(&self, name: &CStr) -> bool {
// SAFETY: By the invariant of `CStr`, `name` is null-terminated.
--
2.49.0