Skip to content

Commit 71ab965

Browse files
nbdd0121jannau
authored andcommitted
rust: device: support dev_printk on all devices
Currently, `dev_*` only works on the core `Device`, but not on any other bus or class device objects. This causes a pattern of `dev_info!(pdev.as_ref())` which is not ideal. This adds support of using these devices directly with `dev_*` macros, by adding `AsRef` call inside the macro. To make sure we can still use just `kernel::device::Device`, as `AsRef` implementation is added for it; this is typical for types that is designed to use with `AsRef` anyway, for example, `str` implements `AsRef<str>` and `Path` implements `AsRef<Path>`. Signed-off-by: Gary Guo <gary@garyguo.net> Link: https://patch.msgid.link/20260123175854.176735-1-gary@kernel.org Signed-off-by: Danilo Krummrich <dakr@kernel.org>
1 parent c819da3 commit 71ab965

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

rust/kernel/device.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,13 @@ impl DeviceContext for Core {}
596596
impl DeviceContext for CoreInternal {}
597597
impl DeviceContext for Normal {}
598598

599+
impl<Ctx: DeviceContext> AsRef<Device<Ctx>> for Device<Ctx> {
600+
#[inline]
601+
fn as_ref(&self) -> &Device<Ctx> {
602+
self
603+
}
604+
}
605+
599606
/// Convert device references to bus device references.
600607
///
601608
/// Bus devices can implement this trait to allow abstractions to provide the bus device in
@@ -715,7 +722,7 @@ macro_rules! impl_device_context_into_aref {
715722
macro_rules! dev_printk {
716723
($method:ident, $dev:expr, $($f:tt)*) => {
717724
{
718-
($dev).$method($crate::prelude::fmt!($($f)*));
725+
$crate::device::Device::$method($dev.as_ref(), $crate::prelude::fmt!($($f)*))
719726
}
720727
}
721728
}

0 commit comments

Comments
 (0)