Skip to content

Commit d9e5958

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 1f4c815 commit d9e5958

1 file changed

Lines changed: 24 additions & 1 deletion

File tree

rust/kernel/device.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
77
use crate::{
88
bindings,
9-
error::{Error, Result},
9+
error::{code::*, Error, Result},
1010
str::CStr,
1111
types::{ARef, NotThreadSafe, Opaque},
1212
};
@@ -267,6 +267,29 @@ impl<Ctx: DeviceContext> Device<Ctx> {
267267
_not_send: NotThreadSafe,
268268
}
269269
}
270+
271+
/// ensure Device is bound
272+
pub fn is_bound(&self) -> Option<Guard<'_, Ctx>> {
273+
let guard = self.lock();
274+
if !unsafe { bindings::device_is_bound(self.as_raw()) } {
275+
return None;
276+
}
277+
Some(guard)
278+
}
279+
280+
/// excute closure while the device is bound
281+
pub fn while_bound_with<F, U>(&self, f: F) -> Result<U>
282+
where
283+
F: FnOnce(&Device<Bound>) -> Result<U>,
284+
{
285+
let _guard = self.lock();
286+
if unsafe { !bindings::device_is_bound(self.as_raw()) } {
287+
return Err(ENODEV);
288+
}
289+
let ptr: *const Self = self;
290+
let ptr = ptr.cast::<Device<Bound>>();
291+
f(unsafe { &*ptr })
292+
}
270293
}
271294

272295
/// A lock guard.

0 commit comments

Comments
 (0)