[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250816-rust-overlay-abs-v2-1-48a2c8921df2@beagleboard.org>
Date: Sat, 16 Aug 2025 11:27:45 +0530
From: Ayush Singh <ayush@...gleboard.org>
To: Jason Kridner <jkridner@...gleboard.org>,
Deepak Khatri <lorforlinux@...gleboard.org>,
Robert Nelson <robertcnelson@...gleboard.org>,
Miguel Ojeda <ojeda@...nel.org>, Dhruva Gole <d-gole@...com>,
Alex Gaynor <alex.gaynor@...il.com>, Boqun Feng <boqun.feng@...il.com>,
Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Andreas Hindborg <a.hindborg@...nel.org>, Alice Ryhl <aliceryhl@...gle.com>,
Trevor Gross <tmgross@...ch.edu>, Danilo Krummrich <dakr@...nel.org>,
Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
"Rafael J. Wysocki" <rafael@...nel.org>, Rob Herring <robh@...nel.org>,
Saravana Kannan <saravanak@...gle.com>, Benno Lossin <lossin@...nel.org>,
Benno Lossin <lossin@...nel.org>
Cc: rust-for-linux@...r.kernel.org, linux-kernel@...r.kernel.org,
devicetree@...r.kernel.org, Ayush Singh <ayush@...gleboard.org>
Subject: [PATCH v2 1/2] rust: kernel: of: Add DeviceNode abstraction
Allow getting struct device_node from struct fwnode_handle. This is
required when applying devicetree overlays using the
`of_overlay_fdt_apply`.
Signed-off-by: Ayush Singh <ayush@...gleboard.org>
---
rust/bindings/bindings_helper.h | 1 +
rust/helpers/of.c | 5 +++++
rust/kernel/device/property.rs | 18 ++++++++++++++++++
rust/kernel/of.rs | 12 ++++++++++++
4 files changed, 36 insertions(+)
diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h
index 84d60635e8a9baef1f1a1b2752dc0fa044f8542f..f35764bf28647f6d567afcbe073c37b30a3b0284 100644
--- a/rust/bindings/bindings_helper.h
+++ b/rust/bindings/bindings_helper.h
@@ -58,6 +58,7 @@
#include <linux/jump_label.h>
#include <linux/mdio.h>
#include <linux/miscdevice.h>
+#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/pci.h>
#include <linux/phy.h>
diff --git a/rust/helpers/of.c b/rust/helpers/of.c
index 86b51167c913f96b626e55adb51bdac2a9f04f56..fba7a26dfd45a6d4350aa2a3e5b43ddbcd1b2c70 100644
--- a/rust/helpers/of.c
+++ b/rust/helpers/of.c
@@ -6,3 +6,8 @@ bool rust_helper_is_of_node(const struct fwnode_handle *fwnode)
{
return is_of_node(fwnode);
}
+
+struct device_node *rust_helper_to_of_node(const struct fwnode_handle *fwnode)
+{
+ return to_of_node(fwnode);
+}
diff --git a/rust/kernel/device/property.rs b/rust/kernel/device/property.rs
index 49ee12a906dbadbe932e207fc7a0d1d622125f5f..e19c1aebd5d5a7f0eedf8ef264d36b943eaf1fc5 100644
--- a/rust/kernel/device/property.rs
+++ b/rust/kernel/device/property.rs
@@ -68,6 +68,24 @@ pub fn is_of_node(&self) -> bool {
unsafe { bindings::is_of_node(self.as_raw()) }
}
+ /// Returns `DeviceNode` if `&self` is an OF node.
+ pub fn to_of_node(&self) -> Option<&crate::of::DeviceNode> {
+ // SAFETY: The type invariant of `Self` guarantees that `self.as_raw() is a pointer to a
+ // valid `struct fwnode_handle`.
+ let of_node = unsafe { bindings::to_of_node(self.as_raw()) };
+
+ if of_node.is_null() {
+ None
+ } else {
+ // SAFETY: `of_node` is valid. Its lifetime is tied to `&self`. We
+ // return a reference instead of an `ARef<DeviceNode>` because `to_of_node()`
+ // doesn't increment the refcount. It is safe to cast from a
+ // `struct device_node*` to a `*const DeviceNode` because `DeviceNode` is
+ // defined as a `#[repr(transparent)]` wrapper around `device_node`.
+ Some(unsafe { &*of_node.cast() })
+ }
+ }
+
/// Returns an object that implements [`Display`](core::fmt::Display) for
/// printing the name of a node.
///
diff --git a/rust/kernel/of.rs b/rust/kernel/of.rs
index b76b35265df2ea6b8b6dcf3d3dab5519d6092bf9..74d42dce06f0e9f67a0e5bd1287117b4966d4af9 100644
--- a/rust/kernel/of.rs
+++ b/rust/kernel/of.rs
@@ -6,6 +6,7 @@
bindings,
device_id::{RawDeviceId, RawDeviceIdIndex},
prelude::*,
+ types::Opaque
};
/// IdTable type for OF drivers.
@@ -63,3 +64,14 @@ macro_rules! of_device_table {
$crate::module_device_table!("of", $module_table_name, $table_name);
};
}
+
+/// Devicetree device node
+#[repr(transparent)]
+pub struct DeviceNode(Opaque<bindings::device_node>);
+
+impl DeviceNode {
+ /// Obtain the raw `struct device_node *`.
+ pub fn as_raw(&self) -> *mut bindings::device_node {
+ self.0.get()
+ }
+}
--
2.50.1
Powered by blists - more mailing lists