rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ayush Singh <ayush@beagleboard.org>
To: "Jason Kridner" <jkridner@beagleboard.org>,
	"Deepak Khatri" <lorforlinux@beagleboard.org>,
	"Robert Nelson" <robertcnelson@beagleboard.org>,
	"Miguel Ojeda" <ojeda@kernel.org>, "Dhruva Gole" <d-gole@ti.com>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	"Rob Herring" <robh@kernel.org>,
	"Saravana Kannan" <saravanak@google.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Benno Lossin" <lossin@kernel.org>
Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
	 devicetree@vger.kernel.org, Ayush Singh <ayush@beagleboard.org>
Subject: [PATCH v2 1/2] rust: kernel: of: Add DeviceNode abstraction
Date: Sat, 16 Aug 2025 11:27:45 +0530	[thread overview]
Message-ID: <20250816-rust-overlay-abs-v2-1-48a2c8921df2@beagleboard.org> (raw)
In-Reply-To: <20250816-rust-overlay-abs-v2-0-48a2c8921df2@beagleboard.org>

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@beagleboard.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


  reply	other threads:[~2025-08-16  5:58 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-16  5:57 [PATCH v2 0/2] rust: Add abstractions for applying devicetree overlays Ayush Singh
2025-08-16  5:57 ` Ayush Singh [this message]
2025-08-16  5:57 ` [PATCH v2 2/2] rust: kernel: of: Add overlay id abstraction Ayush Singh
2025-08-17  5:50   ` kernel test robot
2025-08-16 12:42 ` [PATCH v2 0/2] rust: Add abstractions for applying devicetree overlays Miguel Ojeda

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250816-rust-overlay-abs-v2-1-48a2c8921df2@beagleboard.org \
    --to=ayush@beagleboard.org \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=d-gole@ti.com \
    --cc=dakr@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=gary@garyguo.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=jkridner@beagleboard.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lorforlinux@beagleboard.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rafael@kernel.org \
    --cc=robertcnelson@beagleboard.org \
    --cc=robh@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=saravanak@google.com \
    --cc=tmgross@umich.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).