Skip to content

Commit 0a1d754

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 71a2ceb commit 0a1d754

5 files changed

Lines changed: 530 additions & 0 deletions

File tree

rust/bindings/bindings_helper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
#include <linux/jump_label.h>
5454
#include <linux/mdio.h>
5555
#include <linux/miscdevice.h>
56+
#include <linux/of.h>
57+
#include <linux/of_address.h>
5658
#include <linux/of_device.h>
5759
#include <linux/pci.h>
5860
#include <linux/phy.h>

rust/helpers/helpers.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "jump_label.c"
2525
#include "kunit.c"
2626
#include "mutex.c"
27+
#include "of.c"
2728
#include "page.c"
2829
#include "platform.c"
2930
#include "pci.c"

rust/helpers/of.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
12+
#ifdef CONFIG_OF
13+
bool rust_helper_of_node_is_root(const struct device_node *np)
14+
{
15+
return of_node_is_root(np);
16+
}
17+
#endif
18+
19+
struct device_node *rust_helper_of_parse_phandle(const struct device_node *np,
20+
const char *phandle_name,
21+
int index)
22+
{
23+
return of_parse_phandle(np, phandle_name, index);
24+
}

rust/kernel/device.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use crate::{
88
bindings,
99
error::{code::*, Error, Result},
10+
of,
1011
str::CStr,
1112
types::{ARef, NotThreadSafe, Opaque},
1213
};
@@ -102,6 +103,13 @@ impl<Ctx: DeviceContext> Device<Ctx> {
102103
unsafe { &*ptr.cast() }
103104
}
104105

106+
/// Gets the OpenFirmware node attached to this device
107+
pub fn of_node(&self) -> Option<of::Node> {
108+
let ptr = self.0.get();
109+
// SAFETY: This is safe as long as of_node is NULL or valid.
110+
unsafe { of::Node::get_from_raw((*ptr).of_node) }
111+
}
112+
105113
/// Prints an emergency-level message (level 0) prefixed with device information.
106114
///
107115
/// More details are available from [`dev_emerg`].

0 commit comments

Comments
 (0)