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>,
	"Benno Lossin" <benno.lossin@proton.me>,
	"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>
Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
	 devicetree@vger.kernel.org, Ayush Singh <ayush@beagleboard.org>
Subject: [PATCH 1/2] rust: kernel: of: Add DeviceNode abstraction
Date: Tue, 22 Apr 2025 18:02:09 +0530	[thread overview]
Message-ID: <20250422-rust-overlay-abs-v1-1-85779c1b853d@beagleboard.org> (raw)
In-Reply-To: <20250422-rust-overlay-abs-v1-0-85779c1b853d@beagleboard.org>

Allow getting struct device_node from struct device. This is required
when applying devicetree overlays using the `of_overlay_fdt_apply`.

This structure is never read/constructed from Rust side.

Signed-off-by: Ayush Singh <ayush@beagleboard.org>
---
 rust/bindings/bindings_helper.h |  1 +
 rust/kernel/device.rs           |  5 +++++
 rust/kernel/of.rs               | 24 +++++++++++++++++++++++-
 3 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h
index ab37e1d35c70d52e69b754bf855bc19911d156d8..95ccd7772006f17c126acc70776b7a19f5768683 100644
--- a/rust/bindings/bindings_helper.h
+++ b/rust/bindings/bindings_helper.h
@@ -23,6 +23,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/kernel/device.rs b/rust/kernel/device.rs
index 21b343a1dc4d2b4ba75c3886ba954be53ada88c2..77ef5addb76f67e952ccf7fadcda59ad72975670 100644
--- a/rust/kernel/device.rs
+++ b/rust/kernel/device.rs
@@ -187,6 +187,11 @@ pub fn property_present(&self, name: &CStr) -> bool {
         // SAFETY: By the invariant of `CStr`, `name` is null-terminated.
         unsafe { bindings::device_property_present(self.as_raw().cast_const(), name.as_char_ptr()) }
     }
+
+    /// Get refernce to the device_node property
+    pub fn device_node(&self) -> &kernel::of::DeviceNode {
+        unsafe { kernel::of::DeviceNode::as_ref((*self.as_raw()).of_node) }
+    }
 }
 
 // SAFETY: Instances of `Device` are always reference-counted.
diff --git a/rust/kernel/of.rs b/rust/kernel/of.rs
index 04f2d8ef29cb953907ae26b5b2ebce926b6feef2..db4ec26ff7de024f5e513a2223cfd841f57b1434 100644
--- a/rust/kernel/of.rs
+++ b/rust/kernel/of.rs
@@ -2,7 +2,7 @@
 
 //! Device Tree / Open Firmware abstractions.
 
-use crate::{bindings, device_id::RawDeviceId, prelude::*};
+use crate::{bindings, device_id::RawDeviceId, prelude::*, types::Opaque};
 
 /// IdTable type for OF drivers.
 pub type IdTable<T> = &'static dyn kernel::device_id::IdTable<DeviceId, T>;
@@ -58,3 +58,25 @@ 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 {
+    /// Convert a raw C `struct device_node` pointer to a `&'a DeviceNode`.
+    ///
+    /// # Safety
+    ///
+    /// Callers must ensure that `ptr` is valid, non-null. for the duration of this function call
+    /// and the entire duration when the returned reference exists.
+    pub unsafe fn as_ref<'a>(ptr: *mut bindings::device_node) -> &'a Self {
+        // SAFETY: Guaranteed by the safety requirements of the function.
+        unsafe { &*ptr.cast() }
+    }
+
+    /// Obtain the raw `struct device_node *`.
+    pub fn as_raw(&self) -> *mut bindings::device_node {
+        self.0.get()
+    }
+}

-- 
2.49.0


  reply	other threads:[~2025-04-22 12:33 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-22 12:32 [PATCH 0/2] rust: Add abstractions for applying devicetree overlays Ayush Singh
2025-04-22 12:32 ` Ayush Singh [this message]
2025-04-22 12:32 ` [PATCH 2/2] rust: kernel: of: Add overlay id abstraction Ayush Singh

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=20250422-rust-overlay-abs-v1-1-85779c1b853d@beagleboard.org \
    --to=ayush@beagleboard.org \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=benno.lossin@proton.me \
    --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=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).