[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250422-rust-overlay-abs-v1-1-85779c1b853d@beagleboard.org>
Date: Tue, 22 Apr 2025 18:02:09 +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>,
Benno Lossin <benno.lossin@...ton.me>,
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>
Cc: rust-for-linux@...r.kernel.org, linux-kernel@...r.kernel.org,
devicetree@...r.kernel.org, Ayush Singh <ayush@...gleboard.org>
Subject: [PATCH 1/2] rust: kernel: of: Add DeviceNode abstraction
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@...gleboard.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
Powered by blists - more mailing lists