Skip to content

Commit c6576be

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

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
@@ -211,6 +211,41 @@ impl Node {
211211
}
212212
}
213213

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

0 commit comments

Comments
 (0)