Skip to content

Commit 339e976

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 52c1011 commit 339e976

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
@@ -65,6 +65,8 @@
6565
#include <linux/mdio.h>
6666
#include <linux/mm.h>
6767
#include <linux/miscdevice.h>
68+
#include <linux/of.h>
69+
#include <linux/of_address.h>
6870
#include <linux/of_device.h>
6971
#include <linux/pci.h>
7072
#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
@@ -4,6 +4,7 @@
44
//!
55
//! C header: [`include/linux/device.h`](srctree/include/linux/device.h)
66
7+
use crate::of;
78
use crate::{
89
bindings, fmt,
910
prelude::*,
@@ -368,6 +369,13 @@ impl<Ctx: DeviceContext> Device<Ctx> {
368369
unsafe { &*ptr.cast() }
369370
}
370371

372+
/// Gets the OpenFirmware node attached to this device
373+
pub fn of_node(&self) -> Option<of::Node> {
374+
let ptr = self.0.get();
375+
// SAFETY: This is safe as long as of_node is NULL or valid.
376+
unsafe { of::Node::get_from_raw((*ptr).of_node) }
377+
}
378+
371379
/// Prints an emergency-level message (level 0) prefixed with device information.
372380
///
373381
/// More details are available from [`dev_emerg`].

0 commit comments

Comments
 (0)