Skip to content

Commit 4d03277

Browse files
Danilo Krummrichgregkh
authored andcommitted
rust: device: implement device context marker
Some bus device functions should only be called from bus callbacks, such as probe(), remove(), resume(), suspend(), etc. To ensure this add device context marker structs, that can be used as generics for bus device implementations. Reviewed-by: Benno Lossin <benno.lossin@proton.me> Suggested-by: Benno Lossin <benno.lossin@proton.me> Signed-off-by: Danilo Krummrich <dakr@kernel.org> Acked-by: Boqun Feng <boqun.feng@gmail.com> Link: https://lore.kernel.org/r/20250314160932.100165-3-dakr@kernel.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent d1f6d6c commit 4d03277

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

rust/kernel/device.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,32 @@ unsafe impl Send for Device {}
209209
// synchronization in `struct device`.
210210
unsafe impl Sync for Device {}
211211

212+
/// Marker trait for the context of a bus specific device.
213+
///
214+
/// Some functions of a bus specific device should only be called from a certain context, i.e. bus
215+
/// callbacks, such as `probe()`.
216+
///
217+
/// This is the marker trait for structures representing the context of a bus specific device.
218+
pub trait DeviceContext: private::Sealed {}
219+
220+
/// The [`Normal`] context is the context of a bus specific device when it is not an argument of
221+
/// any bus callback.
222+
pub struct Normal;
223+
224+
/// The [`Core`] context is the context of a bus specific device when it is supplied as argument of
225+
/// any of the bus callbacks, such as `probe()`.
226+
pub struct Core;
227+
228+
mod private {
229+
pub trait Sealed {}
230+
231+
impl Sealed for super::Core {}
232+
impl Sealed for super::Normal {}
233+
}
234+
235+
impl DeviceContext for Core {}
236+
impl DeviceContext for Normal {}
237+
212238
#[doc(hidden)]
213239
#[macro_export]
214240
macro_rules! dev_printk {

0 commit comments

Comments
 (0)