* [PATCH 1/2] rust: kernel: of: Add DeviceNode abstraction
2025-04-22 12:32 [PATCH 0/2] rust: Add abstractions for applying devicetree overlays Ayush Singh
@ 2025-04-22 12:32 ` Ayush Singh
2025-04-22 12:32 ` [PATCH 2/2] rust: kernel: of: Add overlay id abstraction Ayush Singh
1 sibling, 0 replies; 3+ messages in thread
From: Ayush Singh @ 2025-04-22 12:32 UTC (permalink / raw)
To: Jason Kridner, Deepak Khatri, Robert Nelson, Miguel Ojeda,
Dhruva Gole, Alex Gaynor, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Trevor Gross, Danilo Krummrich, Greg Kroah-Hartman,
Rafael J. Wysocki, Rob Herring, Saravana Kannan
Cc: rust-for-linux, linux-kernel, devicetree, Ayush Singh
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
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH 2/2] rust: kernel: of: Add overlay id abstraction
2025-04-22 12:32 [PATCH 0/2] rust: Add abstractions for applying devicetree overlays Ayush Singh
2025-04-22 12:32 ` [PATCH 1/2] rust: kernel: of: Add DeviceNode abstraction Ayush Singh
@ 2025-04-22 12:32 ` Ayush Singh
1 sibling, 0 replies; 3+ messages in thread
From: Ayush Singh @ 2025-04-22 12:32 UTC (permalink / raw)
To: Jason Kridner, Deepak Khatri, Robert Nelson, Miguel Ojeda,
Dhruva Gole, Alex Gaynor, Boqun Feng, Gary Guo,
Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
Trevor Gross, Danilo Krummrich, Greg Kroah-Hartman,
Rafael J. Wysocki, Rob Herring, Saravana Kannan
Cc: rust-for-linux, linux-kernel, devicetree, Ayush Singh
Allow applying devicetree overlays from Rust.
The overlay is removed on Drop.
Signed-off-by: Ayush Singh <ayush@beagleboard.org>
---
rust/kernel/of.rs | 29 +++++++++++++++++++++++++++++
1 file changed, 29 insertions(+)
diff --git a/rust/kernel/of.rs b/rust/kernel/of.rs
index db4ec26ff7de024f5e513a2223cfd841f57b1434..76dbb118d2dc21d1174e0a1ca8f8610d1d176119 100644
--- a/rust/kernel/of.rs
+++ b/rust/kernel/of.rs
@@ -80,3 +80,32 @@ pub fn as_raw(&self) -> *mut bindings::device_node {
self.0.get()
}
}
+
+/// Devicetree overlay present in livetree.
+///
+/// The overlay is removed on Drop
+pub struct OvcsId(kernel::ffi::c_int);
+
+impl OvcsId {
+ /// Create and apply an overlay changeset to the live tree.
+ pub fn of_overlay_fdt_apply(overlay: &[u8], base: &DeviceNode) -> Result<Self> {
+ let mut ovcs_id: kernel::ffi::c_int = 0;
+
+ crate::error::to_result(unsafe {
+ bindings::of_overlay_fdt_apply(
+ overlay.as_ptr().cast(),
+ overlay.len().try_into().unwrap(),
+ &mut ovcs_id,
+ base.as_raw(),
+ )
+ })?;
+
+ Ok(Self(ovcs_id))
+ }
+}
+
+impl Drop for OvcsId {
+ fn drop(&mut self) {
+ unsafe { bindings::of_overlay_remove(&mut self.0) };
+ }
+}
--
2.49.0
^ permalink raw reply related [flat|nested] 3+ messages in thread