* [PATCH 0/2] rust: Add abstractions for applying devicetree overlays
@ 2025-04-22 12:32 Ayush Singh
2025-04-22 12:32 ` [PATCH 1/2] rust: kernel: of: Add DeviceNode abstraction Ayush Singh
2025-04-22 12:32 ` [PATCH 2/2] rust: kernel: of: Add overlay id abstraction Ayush Singh
0 siblings, 2 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
The following patch series adds abstractions required to apply
devicetree overlays from Rust code. To see how the bindings look in
usage, see my working tree [0] for a connector driver I am working on.
Open Questions
***************
1. Should `DeviceNode` be Opaque?
Since this structure is never read/constructed from Rust side, maybe it's
better off as Opaque.
2. Removing overlay on drop
I my usecase (see [0]), I will only ever have 1 active overlay in the
driver, which I will be removing dynamically. So removing overlay on
drop works for it. But maybe there are some usecases I am missing.
3. Only enable bindings when CONFIG_OF_OVERLAY is set?
The kernel header currently seems to provide blank implementations of
these methods when `CONFIG_OF_OVERLAY` is not enabled. But I am not sure
what is rust-for-linux policy here.
[0]: https://github.com/Ayush1325/linux/commits/b4/beagle-cape/
Signed-off-by: Ayush Singh <ayush@beagleboard.org>
---
Ayush Singh (2):
rust: kernel: of: Add DeviceNode abstraction
rust: kernel: of: Add overlay id abstraction
rust/bindings/bindings_helper.h | 1 +
rust/kernel/device.rs | 5 ++++
rust/kernel/of.rs | 53 ++++++++++++++++++++++++++++++++++++++++-
3 files changed, 58 insertions(+), 1 deletion(-)
---
base-commit: bc8aa6cdadcc00862f2b5720e5de2e17f696a081
change-id: 20250417-rust-overlay-abs-36aac8b9752a
Best regards,
--
Ayush Singh <ayush@beagleboard.org>
^ permalink raw reply [flat|nested] 3+ messages in thread
* [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
end of thread, other threads:[~2025-04-22 12:33 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH 2/2] rust: kernel: of: Add overlay id abstraction Ayush Singh
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).