Skip to content

Commit

Permalink
rust: of: Add OF node abstraction
Browse files Browse the repository at this point in the history
This abstraction enables Rust drivers to walk Device Tree nodes and
query their properties.

Signed-off-by: Asahi Lina <lina@asahilina.net>
  • Loading branch information
asahilina committed May 19, 2023
1 parent 38ed18e commit c6d2380
Show file tree
Hide file tree
Showing 4 changed files with 485 additions and 2 deletions.
3 changes: 3 additions & 0 deletions rust/bindings/bindings_helper.h
Expand Up @@ -10,6 +10,9 @@
#include <linux/device.h>
#include <linux/io-pgtable.h>
#include <linux/ktime.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_device.h>
#include <linux/platform_device.h>
#include <linux/refcount.h>
#include <linux/wait.h>
Expand Down
14 changes: 14 additions & 0 deletions rust/helpers.c
Expand Up @@ -337,6 +337,20 @@ const struct of_device_id *rust_helper_of_match_device(
}
EXPORT_SYMBOL_GPL(rust_helper_of_match_device);

bool rust_helper_of_node_is_root(const struct device_node *np)
{
return of_node_is_root(np);
}
EXPORT_SYMBOL_GPL(rust_helper_of_node_is_root);

struct device_node *rust_helper_of_parse_phandle(const struct device_node *np,
const char *phandle_name,
int index)
{
return of_parse_phandle(np, phandle_name, index);
}
EXPORT_SYMBOL_GPL(rust_helper_of_parse_phandle);

/*
* We use `bindgen`'s `--size_t-is-usize` option to bind the C `size_t` type
* as the Rust `usize` type, so we can use it in contexts where Rust
Expand Down
9 changes: 8 additions & 1 deletion rust/kernel/device.rs
Expand Up @@ -8,7 +8,7 @@ use crate::{
bindings,
error::Result,
macros::pin_data,
pin_init, pr_crit,
of, pin_init, pr_crit,
str::CStr,
sync::{lock::mutex, lock::Guard, LockClassKey, Mutex, UniqueArc},
};
Expand Down Expand Up @@ -52,6 +52,13 @@ pub unsafe trait RawDevice {
unsafe { CStr::from_char_ptr(name) }
}

/// Gets the OpenFirmware node attached to this device
fn of_node(&self) -> Option<of::Node> {
let ptr = self.raw_device();

unsafe { of::Node::get_from_raw((*ptr).of_node) }
}

/// Prints an emergency-level message (level 0) prefixed with device information.
///
/// More details are available from [`dev_emerg`].
Expand Down

0 comments on commit c6d2380

Please sign in to comment.