summaryrefslogtreecommitdiff
path: root/rust/kernel
diff options
context:
space:
mode:
authorRemo Senekowitsch <remo@buenzli.dev>2025-06-11 12:29:01 +0200
committerDanilo Krummrich <dakr@kernel.org>2025-06-13 00:58:51 +0200
commit658f23b59251e15cc9263cfe5157d5757a293017 (patch)
tree4204707c1e17d82247be85fca47d8f260742d7ed /rust/kernel
parenta2801affa7103862d549050401a9f53b3365fca4 (diff)
rust: device: Enable accessing the FwNode of a Device
Subsequent patches will add methods for reading properties to FwNode. The first step to accessing these methods will be to access the "root" FwNode of a Device. Add the method `fwnode` to `Device`. Tested-by: Dirk Behme <dirk.behme@de.bosch.com> Signed-off-by: Remo Senekowitsch <remo@buenzli.dev> Link: https://lore.kernel.org/r/20250611102908.212514-3-remo@buenzli.dev Signed-off-by: Danilo Krummrich <dakr@kernel.org>
Diffstat (limited to 'rust/kernel')
-rw-r--r--rust/kernel/device.rs15
1 files changed, 15 insertions, 0 deletions
diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
index d6237827a936..48d45af1cfb2 100644
--- a/rust/kernel/device.rs
+++ b/rust/kernel/device.rs
@@ -205,6 +205,21 @@ impl<Ctx: DeviceContext> Device<Ctx> {
};
}
+ /// Obtain the [`FwNode`](property::FwNode) corresponding to this [`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.