Skip to content

Commit 576e5fb

Browse files
committed
rust: of: Add parse_phandle_by_name()
Signed-off-by: Janne Grunau <j@jannau.net>
1 parent 2d71bbc commit 576e5fb

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

rust/kernel/of.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,41 @@ impl Node {
214214
}
215215
}
216216

217+
/// Parse a phandle property and return the Node referenced at a given name, if any.
218+
///
219+
/// Used only for phandle properties with no arguments.
220+
#[allow(unused_variables)]
221+
pub fn parse_phandle_by_name(
222+
&self,
223+
prop: &CStr,
224+
propnames: &CStr,
225+
name: &CStr,
226+
) -> Option<Node> {
227+
#[cfg(not(CONFIG_OF))]
228+
{
229+
None
230+
}
231+
#[cfg(CONFIG_OF)]
232+
// SAFETY: `raw_node` is valid per the type invariant. `of_parse_phandle` returns an
233+
// owned reference.
234+
unsafe {
235+
let index = bindings::of_property_match_string(
236+
self.raw_node,
237+
propnames.as_char_ptr(),
238+
name.as_char_ptr(),
239+
);
240+
if index < 0 {
241+
return None;
242+
};
243+
244+
Node::from_raw(bindings::of_parse_phandle(
245+
self.raw_node,
246+
prop.as_char_ptr(),
247+
index.try_into().ok()?,
248+
))
249+
}
250+
}
251+
217252
#[allow(unused_variables)]
218253
/// Look up a node property by name, returning a `Property` object if found.
219254
pub fn find_property(&self, propname: &CStr) -> Option<Property<'_>> {

0 commit comments

Comments
 (0)