From: Gary Guo Date: Fri, 23 Jan 2026 17:58:38 +0000 (+0000) Subject: rust: device: support `dev_printk` on all devices X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a38cd1fea98990e20021823cea251e6cb088eeab;p=thirdparty%2Fkernel%2Flinux.git 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` and `Path` implements `AsRef`. Signed-off-by: Gary Guo Link: https://patch.msgid.link/20260123175854.176735-1-gary@kernel.org Signed-off-by: Danilo Krummrich --- diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs index cb2348934676..a9cbed2e204c 100644 --- a/rust/kernel/device.rs +++ b/rust/kernel/device.rs @@ -599,6 +599,13 @@ impl DeviceContext for Core {} impl DeviceContext for CoreInternal {} impl DeviceContext for Normal {} +impl AsRef> for Device { + #[inline] + fn as_ref(&self) -> &Device { + self + } +} + /// Convert device references to bus device references. /// /// Bus devices can implement this trait to allow abstractions to provide the bus device in @@ -718,7 +725,7 @@ macro_rules! impl_device_context_into_aref { macro_rules! dev_printk { ($method:ident, $dev:expr, $($f:tt)*) => { { - ($dev).$method($crate::prelude::fmt!($($f)*)); + $crate::device::Device::$method($dev.as_ref(), $crate::prelude::fmt!($($f)*)) } } }