summaryrefslogtreecommitdiff
path: root/rust
diff options
context:
space:
mode:
authorDanilo Krummrich <dakr@kernel.org>2025-03-14 17:09:05 +0100
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2025-03-17 08:04:25 +0100
commit4d032779ab321baa1a430ff27791e5e0c98a0c2f (patch)
treed5746cee270a6caef6ad3e2d4de4cdc6752a20fb /rust
parentd1f6d6c537d488b1a8f04091c7751124985a0ab9 (diff)
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>
Diffstat (limited to 'rust')
-rw-r--r--rust/kernel/device.rs26
1 files changed, 26 insertions, 0 deletions
diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
index db2d9658ba47..21b343a1dc4d 100644
--- a/rust/kernel/device.rs
+++ b/rust/kernel/device.rs
@@ -209,6 +209,32 @@ unsafe impl Send for Device {}
// synchronization in `struct device`.
unsafe impl Sync for Device {}
+/// Marker trait for the context of a bus specific device.
+///
+/// Some functions of a bus specific device should only be called from a certain context, i.e. bus
+/// callbacks, such as `probe()`.
+///
+/// This is the marker trait for structures representing the context of a bus specific device.
+pub trait DeviceContext: private::Sealed {}
+
+/// The [`Normal`] context is the context of a bus specific device when it is not an argument of
+/// any bus callback.
+pub struct Normal;
+
+/// The [`Core`] context is the context of a bus specific device when it is supplied as argument of
+/// any of the bus callbacks, such as `probe()`.
+pub struct Core;
+
+mod private {
+ pub trait Sealed {}
+
+ impl Sealed for super::Core {}
+ impl Sealed for super::Normal {}
+}
+
+impl DeviceContext for Core {}
+impl DeviceContext for Normal {}
+
#[doc(hidden)]
#[macro_export]
macro_rules! dev_printk {