Skip to content

Commit d32e4c2

Browse files
author
Danilo Krummrich
committed
rust: device: implement device context for Device
Analogous to bus specific device, implement the DeviceContext generic for generic devices. This is used for APIs that work with generic devices (such as Devres) to evaluate the device's context. Reviewed-by: Benno Lossin <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Danilo Krummrich <[email protected]>
1 parent fbb92b6 commit d32e4c2

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

rust/kernel/device.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
str::CStr,
1010
types::{ARef, Opaque},
1111
};
12-
use core::{fmt, ptr};
12+
use core::{fmt, marker::PhantomData, ptr};
1313

1414
#[cfg(CONFIG_PRINTK)]
1515
use crate::c_str;
@@ -42,7 +42,7 @@ use crate::c_str;
4242
/// `bindings::device::release` is valid to be called from any thread, hence `ARef<Device>` can be
4343
/// dropped from any thread.
4444
#[repr(transparent)]
45-
pub struct Device(Opaque<bindings::device>);
45+
pub struct Device<Ctx: DeviceContext = Normal>(Opaque<bindings::device>, PhantomData<Ctx>);
4646

4747
impl Device {
4848
/// Creates a new reference-counted abstraction instance of an existing `struct device` pointer.
@@ -59,7 +59,9 @@ impl Device {
5959
// SAFETY: By the safety requirements ptr is valid
6060
unsafe { Self::as_ref(ptr) }.into()
6161
}
62+
}
6263

64+
impl<Ctx: DeviceContext> Device<Ctx> {
6365
/// Obtain the raw `struct device *`.
6466
pub(crate) fn as_raw(&self) -> *mut bindings::device {
6567
self.0.get()
@@ -189,6 +191,11 @@ impl Device {
189191
}
190192
}
191193

194+
// SAFETY: `Device` is a transparent wrapper of a type that doesn't depend on `Device`'s generic
195+
// argument.
196+
kernel::impl_device_context_deref!(unsafe { Device });
197+
kernel::impl_device_context_into_aref!(Device);
198+
192199
// SAFETY: Instances of `Device` are always reference-counted.
193200
unsafe impl crate::types::AlwaysRefCounted for Device {
194201
fn inc_ref(&self) {

0 commit comments

Comments
 (0)