Skip to content

Commit a274a5c

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 3bfa894 commit a274a5c

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
@@ -497,6 +497,29 @@ impl<Ctx: DeviceContext> Device<Ctx> {
497497
_not_send: NotThreadSafe,
498498
}
499499
}
500+
501+
/// ensure Device is bound
502+
pub fn is_bound(&self) -> Option<Guard<'_, Ctx>> {
503+
let guard = self.lock();
504+
if !unsafe { bindings::device_is_bound(self.as_raw()) } {
505+
return None;
506+
}
507+
Some(guard)
508+
}
509+
510+
/// excute closure while the device is bound
511+
pub fn while_bound_with<F, U>(&self, f: F) -> Result<U>
512+
where
513+
F: FnOnce(&Device<Bound>) -> Result<U>,
514+
{
515+
let _guard = self.lock();
516+
if unsafe { !bindings::device_is_bound(self.as_raw()) } {
517+
return Err(ENODEV);
518+
}
519+
let ptr: *const Self = self;
520+
let ptr = ptr.cast::<Device<Bound>>();
521+
f(unsafe { &*ptr })
522+
}
500523
}
501524

502525
/// A lock guard.

0 commit comments

Comments
 (0)