Skip to content

Commit d2e44dd

Browse files
hoshinolinajannau
authored andcommitted
rust: of: Add OF node abstraction
This abstraction enables Rust drivers to walk Device Tree nodes and query their properties. Signed-off-by: Asahi Lina <lina@asahilina.net>
1 parent 657c512 commit d2e44dd

5 files changed

Lines changed: 487 additions & 1 deletion

File tree

rust/bindings/bindings_helper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include <linux/lockdep.h>
2626
#include <linux/mdio.h>
2727
#include <linux/miscdevice.h>
28+
#include <linux/of.h>
29+
#include <linux/of_address.h>
2830
#include <linux/of_device.h>
2931
#include <linux/pci.h>
3032
#include <linux/phy.h>

rust/helpers/helpers.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "kunit.c"
2222
#include "lockdep.c"
2323
#include "mutex.c"
24+
#include "of.c"
2425
#include "page.c"
2526
#include "platform.c"
2627
#include "pci.c"

rust/helpers/of.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <linux/of.h>
4+
#include <linux/of_device.h>
5+
6+
const struct of_device_id *rust_helper_of_match_device(
7+
const struct of_device_id *matches, const struct device *dev)
8+
{
9+
return of_match_device(matches, dev);
10+
}
11+
bool rust_helper_of_node_is_root(const struct device_node *np)
12+
{
13+
return of_node_is_root(np);
14+
}
15+
16+
struct device_node *rust_helper_of_parse_phandle(const struct device_node *np,
17+
const char *phandle_name,
18+
int index)
19+
{
20+
return of_parse_phandle(np, phandle_name, index);
21+
}

rust/kernel/device.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! C header: [`include/linux/device.h`](srctree/include/linux/device.h)
66
77
use crate::{
8-
bindings,
8+
bindings, of,
99
str::CStr,
1010
types::{ARef, Opaque},
1111
};
@@ -89,6 +89,13 @@ impl Device {
8989
unsafe { &*ptr.cast() }
9090
}
9191

92+
/// Gets the OpenFirmware node attached to this device
93+
pub fn of_node(&self) -> Option<of::Node> {
94+
let ptr = self.0.get();
95+
// SAFETY: This is safe as long as of_node is NULL or valid.
96+
unsafe { of::Node::get_from_raw((*ptr).of_node) }
97+
}
98+
9299
/// Prints an emergency-level message (level 0) prefixed with device information.
93100
///
94101
/// More details are available from [`dev_emerg`].

0 commit comments

Comments
 (0)