Skip to content

Commit 2dbf189

Browse files
committed
rust: device: Allow access to bound device
TODO: ensure this can't be called with devices with Core/Bound context as the those will deadlock. Maybe use trylock? Signed-off-by: Janne Grunau <j@jannau.net>
1 parent 86d6c6d commit 2dbf189

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

rust/kernel/device.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,29 @@ impl<Ctx: DeviceContext> Device<Ctx> {
409409
_not_send: NotThreadSafe,
410410
}
411411
}
412+
413+
/// ensure Device is bound
414+
pub fn is_bound(&self) -> Option<Guard<'_, Ctx>> {
415+
let guard = self.lock();
416+
if !unsafe { bindings::device_is_bound(self.as_raw()) } {
417+
return None;
418+
}
419+
Some(guard)
420+
}
421+
422+
/// excute closure while the device is bound
423+
pub fn while_bound_with<F, U>(&self, f: F) -> Result<U>
424+
where
425+
F: FnOnce(&Device<Bound>) -> Result<U>,
426+
{
427+
let _guard = self.lock();
428+
if unsafe { !bindings::device_is_bound(self.as_raw()) } {
429+
return Err(ENODEV);
430+
}
431+
let ptr: *const Self = self;
432+
let ptr = ptr.cast::<Device<Bound>>();
433+
f(unsafe { &*ptr })
434+
}
412435
}
413436

414437
/// A lock guard.

0 commit comments

Comments
 (0)