Skip to content

Commit 7c844a1

Browse files
senekorjannau
authored andcommitted
rust: device: Add property_get_reference_args
Allow Rust code to read reference args from device properties. The wrapper type `FwNodeReferenceArgs` allows callers to access the buffer of read args safely. Tested-by: Dirk Behme <dirk.behme@de.bosch.com> Signed-off-by: Remo Senekowitsch <remo@buenzli.dev>
1 parent 885cba8 commit 7c844a1

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

rust/kernel/device/property.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,64 @@ impl FwNode {
246246
Some(next)
247247
})
248248
}
249+
250+
/// Finds a reference with arguments.
251+
pub fn property_get_reference_args(
252+
&self,
253+
prop: &CStr,
254+
nargs: NArgs<'_>,
255+
index: u32,
256+
) -> Result<FwNodeReferenceArgs> {
257+
let mut out_args = FwNodeReferenceArgs::default();
258+
259+
let (nargs_prop, nargs) = match nargs {
260+
NArgs::Prop(nargs_prop) => (nargs_prop.as_char_ptr(), 0),
261+
NArgs::N(nargs) => (ptr::null(), nargs),
262+
};
263+
264+
// SAFETY:
265+
// - `self.0.get()` is valid.
266+
// - `prop.as_char_ptr()` is valid and zero-terminated.
267+
// - `nargs_prop` is valid and zero-terminated if `nargs`
268+
// is zero, otherwise it is allowed to be a null-pointer.
269+
let ret = unsafe {
270+
bindings::fwnode_property_get_reference_args(
271+
self.0.get(),
272+
prop.as_char_ptr(),
273+
nargs_prop,
274+
nargs,
275+
index,
276+
&mut out_args.0,
277+
)
278+
};
279+
to_result(ret)?;
280+
281+
Ok(out_args)
282+
}
283+
}
284+
285+
/// The return value of [`FwNode::property_get_reference_args`].
286+
#[repr(transparent)]
287+
#[derive(Copy, Clone, Default)]
288+
pub struct FwNodeReferenceArgs(bindings::fwnode_reference_args);
289+
290+
impl FwNodeReferenceArgs {
291+
/// Returns the slice of reference arguments.
292+
pub fn as_slice(&self) -> &[u64] {
293+
// SAFETY: As per the safety invariant of `FwNodeReferenceArgs`, `nargs`
294+
// is the number of elements in `args` that is valid.
295+
unsafe { core::slice::from_raw_parts(self.0.args.as_ptr(), self.0.nargs as usize) }
296+
}
297+
298+
/// Returns the number of reference arguments.
299+
pub fn len(&self) -> usize {
300+
self.0.nargs as usize
301+
}
302+
303+
/// Returns `true` if there are no reference arguments.
304+
pub fn is_empty(&self) -> bool {
305+
self.0.nargs == 0
306+
}
249307
}
250308

251309
// SAFETY: Instances of `FwNode` are always reference-counted.
@@ -412,6 +470,15 @@ impl_property_for_int! {
412470
i64: fwnode_property_read_u64_array,
413471
}
414472

473+
/// The number of arguments of a reference.
474+
pub enum NArgs<'a> {
475+
/// The name of the property of the reference indicating the number of
476+
/// arguments.
477+
Prop(&'a CStr),
478+
/// The known number of arguments.
479+
N(u32),
480+
}
481+
415482
/// A helper for reading device properties.
416483
///
417484
/// Use [`Self::required_by`] if a missing property is considered a bug and

0 commit comments

Comments
 (0)