Skip to content

Commit 77b7ece

Browse files
committed
rust: of: Add Node::address_as_resource()
Signed-off-by: Asahi Lina <lina@asahilina.net>
1 parent dfafc6f commit 77b7ece

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

rust/kernel/of.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//!
55
//! C header: [`include/linux/of_*.h`](srctree/include/linux/of_*.h)
66
7-
use crate::{bindings, device_id::RawDeviceId, prelude::*};
7+
use crate::{bindings, device_id::RawDeviceId, error::to_result, io_mem::Resource, prelude::*};
88
// Note: Most OF functions turn into inline dummies with CONFIG_OF(_*) disabled.
99
// We have to either add config conditionals to helpers.c or here; let's do it
1010
// here for now. In the future, once bindgen can auto-generate static inline
@@ -255,6 +255,24 @@ impl Node {
255255
}
256256
}
257257

258+
pub fn address_as_resource(&self, index: usize) -> Result<Resource> {
259+
#[cfg(not(CONFIG_OF))]
260+
{
261+
Err(EINVAL)
262+
}
263+
#[cfg(CONFIG_OF)]
264+
{
265+
let mut res = core::mem::MaybeUninit::<bindings::resource>::uninit();
266+
// SAFETY: This function is safe to call as long as the arguments are valid pointers.
267+
let ret = unsafe {
268+
bindings::of_address_to_resource(self.raw_node, index.try_into()?, res.as_mut_ptr())
269+
};
270+
to_result(ret)?;
271+
// SAFETY: We have checked the return value above, so the resource must be initialized now.
272+
Resource::new_from_resource(&unsafe { res.assume_init() }).ok_or(EINVAL)
273+
}
274+
}
275+
258276
#[allow(unused_variables)]
259277
/// Look up a node property by name, returning a `Property` object if found.
260278
pub fn find_property(&self, propname: &CStr) -> Option<Property<'_>> {

0 commit comments

Comments
 (0)