Skip to content

Commit dbbb076

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 3ab99cc commit dbbb076

4 files changed

Lines changed: 527 additions & 0 deletions

File tree

rust/bindings/bindings_helper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
#include <linux/jump_label.h>
5959
#include <linux/mdio.h>
6060
#include <linux/miscdevice.h>
61+
#include <linux/of.h>
62+
#include <linux/of_address.h>
6163
#include <linux/of_device.h>
6264
#include <linux/pci.h>
6365
#include <linux/phy.h>

rust/helpers/of.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,29 @@
11
// SPDX-License-Identifier: GPL-2.0
22

33
#include <linux/of.h>
4+
#include <linux/of_device.h>
45

56
bool rust_helper_is_of_node(const struct fwnode_handle *fwnode)
67
{
78
return is_of_node(fwnode);
89
}
10+
11+
const struct of_device_id *rust_helper_of_match_device(
12+
const struct of_device_id *matches, const struct device *dev)
13+
{
14+
return of_match_device(matches, dev);
15+
}
16+
17+
#ifdef CONFIG_OF
18+
bool rust_helper_of_node_is_root(const struct device_node *np)
19+
{
20+
return of_node_is_root(np);
21+
}
22+
#endif
23+
24+
struct device_node *rust_helper_of_parse_phandle(const struct device_node *np,
25+
const char *phandle_name,
26+
int index)
27+
{
28+
return of_parse_phandle(np, phandle_name, index);
29+
}

rust/kernel/device.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
77
use crate::{
88
bindings,
9+
of,
910
types::{ARef, ForeignOwnable, NotThreadSafe, Opaque},
1011
};
1112
use core::{fmt, marker::PhantomData, ptr};
@@ -280,6 +281,13 @@ impl<Ctx: DeviceContext> Device<Ctx> {
280281
unsafe { &*ptr.cast() }
281282
}
282283

284+
/// Gets the OpenFirmware node attached to this device
285+
pub fn of_node(&self) -> Option<of::Node> {
286+
let ptr = self.0.get();
287+
// SAFETY: This is safe as long as of_node is NULL or valid.
288+
unsafe { of::Node::get_from_raw((*ptr).of_node) }
289+
}
290+
283291
/// Prints an emergency-level message (level 0) prefixed with device information.
284292
///
285293
/// More details are available from [`dev_emerg`].

0 commit comments

Comments
 (0)