// SPDX-License-Identifier: GPL-2.012//! Generic devices that are part of the kernel's driver model.3//!4//! C header: [`include/linux/device.h`](srctree/include/linux/device.h)56use crate::{7bindings, fmt,8sync::aref::ARef,9types::{ForeignOwnable, Opaque},10};11use core::{marker::PhantomData, ptr};1213#[cfg(CONFIG_PRINTK)]14use crate::c_str;1516pub mod property;1718/// The core representation of a device in the kernel's driver model.19///20/// This structure represents the Rust abstraction for a C `struct device`. A [`Device`] can either21/// exist as temporary reference (see also [`Device::from_raw`]), which is only valid within a22/// certain scope or as [`ARef<Device>`], owning a dedicated reference count.23///24/// # Device Types25///26/// A [`Device`] can represent either a bus device or a class device.27///28/// ## Bus Devices29///30/// A bus device is a [`Device`] that is associated with a physical or virtual bus. Examples of31/// buses include PCI, USB, I2C, and SPI. Devices attached to a bus are registered with a specific32/// bus type, which facilitates matching devices with appropriate drivers based on IDs or other33/// identifying information. Bus devices are visible in sysfs under `/sys/bus/<bus-name>/devices/`.34///35/// ## Class Devices36///37/// A class device is a [`Device`] that is associated with a logical category of functionality38/// rather than a physical bus. Examples of classes include block devices, network interfaces, sound39/// cards, and input devices. Class devices are grouped under a common class and exposed to40/// userspace via entries in `/sys/class/<class-name>/`.41///42/// # Device Context43///44/// [`Device`] references are generic over a [`DeviceContext`], which represents the type state of45/// a [`Device`].46///47/// As the name indicates, this type state represents the context of the scope the [`Device`]48/// reference is valid in. For instance, the [`Bound`] context guarantees that the [`Device`] is49/// bound to a driver for the entire duration of the existence of a [`Device<Bound>`] reference.50///51/// Other [`DeviceContext`] types besides [`Bound`] are [`Normal`], [`Core`] and [`CoreInternal`].52///53/// Unless selected otherwise [`Device`] defaults to the [`Normal`] [`DeviceContext`], which by54/// itself has no additional requirements.55///56/// It is always up to the caller of [`Device::from_raw`] to select the correct [`DeviceContext`]57/// type for the corresponding scope the [`Device`] reference is created in.58///59/// All [`DeviceContext`] types other than [`Normal`] are intended to be used with60/// [bus devices](#bus-devices) only.61///62/// # Implementing Bus Devices63///64/// This section provides a guideline to implement bus specific devices, such as [`pci::Device`] or65/// [`platform::Device`].66///67/// A bus specific device should be defined as follows.68///69/// ```ignore70/// #[repr(transparent)]71/// pub struct Device<Ctx: device::DeviceContext = device::Normal>(72/// Opaque<bindings::bus_device_type>,73/// PhantomData<Ctx>,74/// );75/// ```76///77/// Since devices are reference counted, [`AlwaysRefCounted`] should be implemented for `Device`78/// (i.e. `Device<Normal>`). Note that [`AlwaysRefCounted`] must not be implemented for any other79/// [`DeviceContext`], since all other device context types are only valid within a certain scope.80///81/// In order to be able to implement the [`DeviceContext`] dereference hierarchy, bus device82/// implementations should call the [`impl_device_context_deref`] macro as shown below.83///84/// ```ignore85/// // SAFETY: `Device` is a transparent wrapper of a type that doesn't depend on `Device`'s86/// // generic argument.87/// kernel::impl_device_context_deref!(unsafe { Device });88/// ```89///90/// In order to convert from a any [`Device<Ctx>`] to [`ARef<Device>`], bus devices can implement91/// the following macro call.92///93/// ```ignore94/// kernel::impl_device_context_into_aref!(Device);95/// ```96///97/// Bus devices should also implement the following [`AsRef`] implementation, such that users can98/// easily derive a generic [`Device`] reference.99///100/// ```ignore101/// impl<Ctx: device::DeviceContext> AsRef<device::Device<Ctx>> for Device<Ctx> {102/// fn as_ref(&self) -> &device::Device<Ctx> {103/// ...104/// }105/// }106/// ```107///108/// # Implementing Class Devices109///110/// Class device implementations require less infrastructure and depend slightly more on the111/// specific subsystem.112///113/// An example implementation for a class device could look like this.114///115/// ```ignore116/// #[repr(C)]117/// pub struct Device<T: class::Driver> {118/// dev: Opaque<bindings::class_device_type>,119/// data: T::Data,120/// }121/// ```122///123/// This class device uses the sub-classing pattern to embed the driver's private data within the124/// allocation of the class device. For this to be possible the class device is generic over the125/// class specific `Driver` trait implementation.126///127/// Just like any device, class devices are reference counted and should hence implement128/// [`AlwaysRefCounted`] for `Device`.129///130/// Class devices should also implement the following [`AsRef`] implementation, such that users can131/// easily derive a generic [`Device`] reference.132///133/// ```ignore134/// impl<T: class::Driver> AsRef<device::Device> for Device<T> {135/// fn as_ref(&self) -> &device::Device {136/// ...137/// }138/// }139/// ```140///141/// An example for a class device implementation is142#[cfg_attr(CONFIG_DRM = "y", doc = "[`drm::Device`](kernel::drm::Device).")]143#[cfg_attr(not(CONFIG_DRM = "y"), doc = "`drm::Device`.")]144///145/// # Invariants146///147/// A `Device` instance represents a valid `struct device` created by the C portion of the kernel.148///149/// Instances of this type are always reference-counted, that is, a call to `get_device` ensures150/// that the allocation remains valid at least until the matching call to `put_device`.151///152/// `bindings::device::release` is valid to be called from any thread, hence `ARef<Device>` can be153/// dropped from any thread.154///155/// [`AlwaysRefCounted`]: kernel::types::AlwaysRefCounted156/// [`impl_device_context_deref`]: kernel::impl_device_context_deref157/// [`pci::Device`]: kernel::pci::Device158/// [`platform::Device`]: kernel::platform::Device159#[repr(transparent)]160pub struct Device<Ctx: DeviceContext = Normal>(Opaque<bindings::device>, PhantomData<Ctx>);161162impl Device {163/// Creates a new reference-counted abstraction instance of an existing `struct device` pointer.164///165/// # Safety166///167/// Callers must ensure that `ptr` is valid, non-null, and has a non-zero reference count,168/// i.e. it must be ensured that the reference count of the C `struct device` `ptr` points to169/// can't drop to zero, for the duration of this function call.170///171/// It must also be ensured that `bindings::device::release` can be called from any thread.172/// While not officially documented, this should be the case for any `struct device`.173pub unsafe fn get_device(ptr: *mut bindings::device) -> ARef<Self> {174// SAFETY: By the safety requirements ptr is valid175unsafe { Self::from_raw(ptr) }.into()176}177178/// Convert a [`&Device`](Device) into a [`&Device<Bound>`](Device<Bound>).179///180/// # Safety181///182/// The caller is responsible to ensure that the returned [`&Device<Bound>`](Device<Bound>)183/// only lives as long as it can be guaranteed that the [`Device`] is actually bound.184pub unsafe fn as_bound(&self) -> &Device<Bound> {185let ptr = core::ptr::from_ref(self);186187// CAST: By the safety requirements the caller is responsible to guarantee that the188// returned reference only lives as long as the device is actually bound.189let ptr = ptr.cast();190191// SAFETY:192// - `ptr` comes from `from_ref(self)` above, hence it's guaranteed to be valid.193// - Any valid `Device` pointer is also a valid pointer for `Device<Bound>`.194unsafe { &*ptr }195}196}197198impl Device<CoreInternal> {199/// Store a pointer to the bound driver's private data.200pub fn set_drvdata(&self, data: impl ForeignOwnable) {201// SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`.202unsafe { bindings::dev_set_drvdata(self.as_raw(), data.into_foreign().cast()) }203}204205/// Take ownership of the private data stored in this [`Device`].206///207/// # Safety208///209/// - Must only be called once after a preceding call to [`Device::set_drvdata`].210/// - The type `T` must match the type of the `ForeignOwnable` previously stored by211/// [`Device::set_drvdata`].212pub unsafe fn drvdata_obtain<T: ForeignOwnable>(&self) -> T {213// SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`.214let ptr = unsafe { bindings::dev_get_drvdata(self.as_raw()) };215216// SAFETY:217// - By the safety requirements of this function, `ptr` comes from a previous call to218// `into_foreign()`.219// - `dev_get_drvdata()` guarantees to return the same pointer given to `dev_set_drvdata()`220// in `into_foreign()`.221unsafe { T::from_foreign(ptr.cast()) }222}223224/// Borrow the driver's private data bound to this [`Device`].225///226/// # Safety227///228/// - Must only be called after a preceding call to [`Device::set_drvdata`] and before229/// [`Device::drvdata_obtain`].230/// - The type `T` must match the type of the `ForeignOwnable` previously stored by231/// [`Device::set_drvdata`].232pub unsafe fn drvdata_borrow<T: ForeignOwnable>(&self) -> T::Borrowed<'_> {233// SAFETY: By the type invariants, `self.as_raw()` is a valid pointer to a `struct device`.234let ptr = unsafe { bindings::dev_get_drvdata(self.as_raw()) };235236// SAFETY:237// - By the safety requirements of this function, `ptr` comes from a previous call to238// `into_foreign()`.239// - `dev_get_drvdata()` guarantees to return the same pointer given to `dev_set_drvdata()`240// in `into_foreign()`.241unsafe { T::borrow(ptr.cast()) }242}243}244245impl<Ctx: DeviceContext> Device<Ctx> {246/// Obtain the raw `struct device *`.247pub(crate) fn as_raw(&self) -> *mut bindings::device {248self.0.get()249}250251/// Returns a reference to the parent device, if any.252#[cfg_attr(not(CONFIG_AUXILIARY_BUS), expect(dead_code))]253pub(crate) fn parent(&self) -> Option<&Self> {254// SAFETY:255// - By the type invariant `self.as_raw()` is always valid.256// - The parent device is only ever set at device creation.257let parent = unsafe { (*self.as_raw()).parent };258259if parent.is_null() {260None261} else {262// SAFETY:263// - Since `parent` is not NULL, it must be a valid pointer to a `struct device`.264// - `parent` is valid for the lifetime of `self`, since a `struct device` holds a265// reference count of its parent.266Some(unsafe { Self::from_raw(parent) })267}268}269270/// Convert a raw C `struct device` pointer to a `&'a Device`.271///272/// # Safety273///274/// Callers must ensure that `ptr` is valid, non-null, and has a non-zero reference count,275/// i.e. it must be ensured that the reference count of the C `struct device` `ptr` points to276/// can't drop to zero, for the duration of this function call and the entire duration when the277/// returned reference exists.278pub unsafe fn from_raw<'a>(ptr: *mut bindings::device) -> &'a Self {279// SAFETY: Guaranteed by the safety requirements of the function.280unsafe { &*ptr.cast() }281}282283/// Prints an emergency-level message (level 0) prefixed with device information.284///285/// More details are available from [`dev_emerg`].286///287/// [`dev_emerg`]: crate::dev_emerg288pub fn pr_emerg(&self, args: fmt::Arguments<'_>) {289// SAFETY: `klevel` is null-terminated, uses one of the kernel constants.290unsafe { self.printk(bindings::KERN_EMERG, args) };291}292293/// Prints an alert-level message (level 1) prefixed with device information.294///295/// More details are available from [`dev_alert`].296///297/// [`dev_alert`]: crate::dev_alert298pub fn pr_alert(&self, args: fmt::Arguments<'_>) {299// SAFETY: `klevel` is null-terminated, uses one of the kernel constants.300unsafe { self.printk(bindings::KERN_ALERT, args) };301}302303/// Prints a critical-level message (level 2) prefixed with device information.304///305/// More details are available from [`dev_crit`].306///307/// [`dev_crit`]: crate::dev_crit308pub fn pr_crit(&self, args: fmt::Arguments<'_>) {309// SAFETY: `klevel` is null-terminated, uses one of the kernel constants.310unsafe { self.printk(bindings::KERN_CRIT, args) };311}312313/// Prints an error-level message (level 3) prefixed with device information.314///315/// More details are available from [`dev_err`].316///317/// [`dev_err`]: crate::dev_err318pub fn pr_err(&self, args: fmt::Arguments<'_>) {319// SAFETY: `klevel` is null-terminated, uses one of the kernel constants.320unsafe { self.printk(bindings::KERN_ERR, args) };321}322323/// Prints a warning-level message (level 4) prefixed with device information.324///325/// More details are available from [`dev_warn`].326///327/// [`dev_warn`]: crate::dev_warn328pub fn pr_warn(&self, args: fmt::Arguments<'_>) {329// SAFETY: `klevel` is null-terminated, uses one of the kernel constants.330unsafe { self.printk(bindings::KERN_WARNING, args) };331}332333/// Prints a notice-level message (level 5) prefixed with device information.334///335/// More details are available from [`dev_notice`].336///337/// [`dev_notice`]: crate::dev_notice338pub fn pr_notice(&self, args: fmt::Arguments<'_>) {339// SAFETY: `klevel` is null-terminated, uses one of the kernel constants.340unsafe { self.printk(bindings::KERN_NOTICE, args) };341}342343/// Prints an info-level message (level 6) prefixed with device information.344///345/// More details are available from [`dev_info`].346///347/// [`dev_info`]: crate::dev_info348pub fn pr_info(&self, args: fmt::Arguments<'_>) {349// SAFETY: `klevel` is null-terminated, uses one of the kernel constants.350unsafe { self.printk(bindings::KERN_INFO, args) };351}352353/// Prints a debug-level message (level 7) prefixed with device information.354///355/// More details are available from [`dev_dbg`].356///357/// [`dev_dbg`]: crate::dev_dbg358pub fn pr_dbg(&self, args: fmt::Arguments<'_>) {359if cfg!(debug_assertions) {360// SAFETY: `klevel` is null-terminated, uses one of the kernel constants.361unsafe { self.printk(bindings::KERN_DEBUG, args) };362}363}364365/// Prints the provided message to the console.366///367/// # Safety368///369/// Callers must ensure that `klevel` is null-terminated; in particular, one of the370/// `KERN_*`constants, for example, `KERN_CRIT`, `KERN_ALERT`, etc.371#[cfg_attr(not(CONFIG_PRINTK), allow(unused_variables))]372unsafe fn printk(&self, klevel: &[u8], msg: fmt::Arguments<'_>) {373// SAFETY: `klevel` is null-terminated and one of the kernel constants. `self.as_raw`374// is valid because `self` is valid. The "%pA" format string expects a pointer to375// `fmt::Arguments`, which is what we're passing as the last argument.376#[cfg(CONFIG_PRINTK)]377unsafe {378bindings::_dev_printk(379klevel.as_ptr().cast::<crate::ffi::c_char>(),380self.as_raw(),381c_str!("%pA").as_char_ptr(),382core::ptr::from_ref(&msg).cast::<crate::ffi::c_void>(),383)384};385}386387/// Obtain the [`FwNode`](property::FwNode) corresponding to this [`Device`].388pub fn fwnode(&self) -> Option<&property::FwNode> {389// SAFETY: `self` is valid.390let fwnode_handle = unsafe { bindings::__dev_fwnode(self.as_raw()) };391if fwnode_handle.is_null() {392return None;393}394// SAFETY: `fwnode_handle` is valid. Its lifetime is tied to `&self`. We395// return a reference instead of an `ARef<FwNode>` because `dev_fwnode()`396// doesn't increment the refcount. It is safe to cast from a397// `struct fwnode_handle*` to a `*const FwNode` because `FwNode` is398// defined as a `#[repr(transparent)]` wrapper around `fwnode_handle`.399Some(unsafe { &*fwnode_handle.cast() })400}401}402403// SAFETY: `Device` is a transparent wrapper of a type that doesn't depend on `Device`'s generic404// argument.405kernel::impl_device_context_deref!(unsafe { Device });406kernel::impl_device_context_into_aref!(Device);407408// SAFETY: Instances of `Device` are always reference-counted.409unsafe impl crate::sync::aref::AlwaysRefCounted for Device {410fn inc_ref(&self) {411// SAFETY: The existence of a shared reference guarantees that the refcount is non-zero.412unsafe { bindings::get_device(self.as_raw()) };413}414415unsafe fn dec_ref(obj: ptr::NonNull<Self>) {416// SAFETY: The safety requirements guarantee that the refcount is non-zero.417unsafe { bindings::put_device(obj.cast().as_ptr()) }418}419}420421// SAFETY: As by the type invariant `Device` can be sent to any thread.422unsafe impl Send for Device {}423424// SAFETY: `Device` can be shared among threads because all immutable methods are protected by the425// synchronization in `struct device`.426unsafe impl Sync for Device {}427428/// Marker trait for the context or scope of a bus specific device.429///430/// [`DeviceContext`] is a marker trait for types representing the context of a bus specific431/// [`Device`].432///433/// The specific device context types are: [`CoreInternal`], [`Core`], [`Bound`] and [`Normal`].434///435/// [`DeviceContext`] types are hierarchical, which means that there is a strict hierarchy that436/// defines which [`DeviceContext`] type can be derived from another. For instance, any437/// [`Device<Core>`] can dereference to a [`Device<Bound>`].438///439/// The following enumeration illustrates the dereference hierarchy of [`DeviceContext`] types.440///441/// - [`CoreInternal`] => [`Core`] => [`Bound`] => [`Normal`]442///443/// Bus devices can automatically implement the dereference hierarchy by using444/// [`impl_device_context_deref`].445///446/// Note that the guarantee for a [`Device`] reference to have a certain [`DeviceContext`] comes447/// from the specific scope the [`Device`] reference is valid in.448///449/// [`impl_device_context_deref`]: kernel::impl_device_context_deref450pub trait DeviceContext: private::Sealed {}451452/// The [`Normal`] context is the default [`DeviceContext`] of any [`Device`].453///454/// The normal context does not indicate any specific context. Any `Device<Ctx>` is also a valid455/// [`Device<Normal>`]. It is the only [`DeviceContext`] for which it is valid to implement456/// [`AlwaysRefCounted`] for.457///458/// [`AlwaysRefCounted`]: kernel::types::AlwaysRefCounted459pub struct Normal;460461/// The [`Core`] context is the context of a bus specific device when it appears as argument of462/// any bus specific callback, such as `probe()`.463///464/// The core context indicates that the [`Device<Core>`] reference's scope is limited to the bus465/// callback it appears in. It is intended to be used for synchronization purposes. Bus device466/// implementations can implement methods for [`Device<Core>`], such that they can only be called467/// from bus callbacks.468pub struct Core;469470/// Semantically the same as [`Core`], but reserved for internal usage of the corresponding bus471/// abstraction.472///473/// The internal core context is intended to be used in exactly the same way as the [`Core`]474/// context, with the difference that this [`DeviceContext`] is internal to the corresponding bus475/// abstraction.476///477/// This context mainly exists to share generic [`Device`] infrastructure that should only be called478/// from bus callbacks with bus abstractions, but without making them accessible for drivers.479pub struct CoreInternal;480481/// The [`Bound`] context is the [`DeviceContext`] of a bus specific device when it is guaranteed to482/// be bound to a driver.483///484/// The bound context indicates that for the entire duration of the lifetime of a [`Device<Bound>`]485/// reference, the [`Device`] is guaranteed to be bound to a driver.486///487/// Some APIs, such as [`dma::CoherentAllocation`] or [`Devres`] rely on the [`Device`] to be bound,488/// which can be proven with the [`Bound`] device context.489///490/// Any abstraction that can guarantee a scope where the corresponding bus device is bound, should491/// provide a [`Device<Bound>`] reference to its users for this scope. This allows users to benefit492/// from optimizations for accessing device resources, see also [`Devres::access`].493///494/// [`Devres`]: kernel::devres::Devres495/// [`Devres::access`]: kernel::devres::Devres::access496/// [`dma::CoherentAllocation`]: kernel::dma::CoherentAllocation497pub struct Bound;498499mod private {500pub trait Sealed {}501502impl Sealed for super::Bound {}503impl Sealed for super::Core {}504impl Sealed for super::CoreInternal {}505impl Sealed for super::Normal {}506}507508impl DeviceContext for Bound {}509impl DeviceContext for Core {}510impl DeviceContext for CoreInternal {}511impl DeviceContext for Normal {}512513/// # Safety514///515/// The type given as `$device` must be a transparent wrapper of a type that doesn't depend on the516/// generic argument of `$device`.517#[doc(hidden)]518#[macro_export]519macro_rules! __impl_device_context_deref {520(unsafe { $device:ident, $src:ty => $dst:ty }) => {521impl ::core::ops::Deref for $device<$src> {522type Target = $device<$dst>;523524fn deref(&self) -> &Self::Target {525let ptr: *const Self = self;526527// CAST: `$device<$src>` and `$device<$dst>` transparently wrap the same type by the528// safety requirement of the macro.529let ptr = ptr.cast::<Self::Target>();530531// SAFETY: `ptr` was derived from `&self`.532unsafe { &*ptr }533}534}535};536}537538/// Implement [`core::ops::Deref`] traits for allowed [`DeviceContext`] conversions of a (bus539/// specific) device.540///541/// # Safety542///543/// The type given as `$device` must be a transparent wrapper of a type that doesn't depend on the544/// generic argument of `$device`.545#[macro_export]546macro_rules! impl_device_context_deref {547(unsafe { $device:ident }) => {548// SAFETY: This macro has the exact same safety requirement as549// `__impl_device_context_deref!`.550::kernel::__impl_device_context_deref!(unsafe {551$device,552$crate::device::CoreInternal => $crate::device::Core553});554555// SAFETY: This macro has the exact same safety requirement as556// `__impl_device_context_deref!`.557::kernel::__impl_device_context_deref!(unsafe {558$device,559$crate::device::Core => $crate::device::Bound560});561562// SAFETY: This macro has the exact same safety requirement as563// `__impl_device_context_deref!`.564::kernel::__impl_device_context_deref!(unsafe {565$device,566$crate::device::Bound => $crate::device::Normal567});568};569}570571#[doc(hidden)]572#[macro_export]573macro_rules! __impl_device_context_into_aref {574($src:ty, $device:tt) => {575impl ::core::convert::From<&$device<$src>> for $crate::sync::aref::ARef<$device> {576fn from(dev: &$device<$src>) -> Self {577(&**dev).into()578}579}580};581}582583/// Implement [`core::convert::From`], such that all `&Device<Ctx>` can be converted to an584/// `ARef<Device>`.585#[macro_export]586macro_rules! impl_device_context_into_aref {587($device:tt) => {588::kernel::__impl_device_context_into_aref!($crate::device::CoreInternal, $device);589::kernel::__impl_device_context_into_aref!($crate::device::Core, $device);590::kernel::__impl_device_context_into_aref!($crate::device::Bound, $device);591};592}593594#[doc(hidden)]595#[macro_export]596macro_rules! dev_printk {597($method:ident, $dev:expr, $($f:tt)*) => {598{599($dev).$method($crate::prelude::fmt!($($f)*));600}601}602}603604/// Prints an emergency-level message (level 0) prefixed with device information.605///606/// This level should be used if the system is unusable.607///608/// Equivalent to the kernel's `dev_emerg` macro.609///610/// Mimics the interface of [`std::print!`]. More information about the syntax is available from611/// [`core::fmt`] and [`std::format!`].612///613/// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html614/// [`std::format!`]: https://doc.rust-lang.org/std/macro.format.html615///616/// # Examples617///618/// ```619/// # use kernel::device::Device;620///621/// fn example(dev: &Device) {622/// dev_emerg!(dev, "hello {}\n", "there");623/// }624/// ```625#[macro_export]626macro_rules! dev_emerg {627($($f:tt)*) => { $crate::dev_printk!(pr_emerg, $($f)*); }628}629630/// Prints an alert-level message (level 1) prefixed with device information.631///632/// This level should be used if action must be taken immediately.633///634/// Equivalent to the kernel's `dev_alert` macro.635///636/// Mimics the interface of [`std::print!`]. More information about the syntax is available from637/// [`core::fmt`] and [`std::format!`].638///639/// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html640/// [`std::format!`]: https://doc.rust-lang.org/std/macro.format.html641///642/// # Examples643///644/// ```645/// # use kernel::device::Device;646///647/// fn example(dev: &Device) {648/// dev_alert!(dev, "hello {}\n", "there");649/// }650/// ```651#[macro_export]652macro_rules! dev_alert {653($($f:tt)*) => { $crate::dev_printk!(pr_alert, $($f)*); }654}655656/// Prints a critical-level message (level 2) prefixed with device information.657///658/// This level should be used in critical conditions.659///660/// Equivalent to the kernel's `dev_crit` macro.661///662/// Mimics the interface of [`std::print!`]. More information about the syntax is available from663/// [`core::fmt`] and [`std::format!`].664///665/// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html666/// [`std::format!`]: https://doc.rust-lang.org/std/macro.format.html667///668/// # Examples669///670/// ```671/// # use kernel::device::Device;672///673/// fn example(dev: &Device) {674/// dev_crit!(dev, "hello {}\n", "there");675/// }676/// ```677#[macro_export]678macro_rules! dev_crit {679($($f:tt)*) => { $crate::dev_printk!(pr_crit, $($f)*); }680}681682/// Prints an error-level message (level 3) prefixed with device information.683///684/// This level should be used in error conditions.685///686/// Equivalent to the kernel's `dev_err` macro.687///688/// Mimics the interface of [`std::print!`]. More information about the syntax is available from689/// [`core::fmt`] and [`std::format!`].690///691/// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html692/// [`std::format!`]: https://doc.rust-lang.org/std/macro.format.html693///694/// # Examples695///696/// ```697/// # use kernel::device::Device;698///699/// fn example(dev: &Device) {700/// dev_err!(dev, "hello {}\n", "there");701/// }702/// ```703#[macro_export]704macro_rules! dev_err {705($($f:tt)*) => { $crate::dev_printk!(pr_err, $($f)*); }706}707708/// Prints a warning-level message (level 4) prefixed with device information.709///710/// This level should be used in warning conditions.711///712/// Equivalent to the kernel's `dev_warn` macro.713///714/// Mimics the interface of [`std::print!`]. More information about the syntax is available from715/// [`core::fmt`] and [`std::format!`].716///717/// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html718/// [`std::format!`]: https://doc.rust-lang.org/std/macro.format.html719///720/// # Examples721///722/// ```723/// # use kernel::device::Device;724///725/// fn example(dev: &Device) {726/// dev_warn!(dev, "hello {}\n", "there");727/// }728/// ```729#[macro_export]730macro_rules! dev_warn {731($($f:tt)*) => { $crate::dev_printk!(pr_warn, $($f)*); }732}733734/// Prints a notice-level message (level 5) prefixed with device information.735///736/// This level should be used in normal but significant conditions.737///738/// Equivalent to the kernel's `dev_notice` macro.739///740/// Mimics the interface of [`std::print!`]. More information about the syntax is available from741/// [`core::fmt`] and [`std::format!`].742///743/// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html744/// [`std::format!`]: https://doc.rust-lang.org/std/macro.format.html745///746/// # Examples747///748/// ```749/// # use kernel::device::Device;750///751/// fn example(dev: &Device) {752/// dev_notice!(dev, "hello {}\n", "there");753/// }754/// ```755#[macro_export]756macro_rules! dev_notice {757($($f:tt)*) => { $crate::dev_printk!(pr_notice, $($f)*); }758}759760/// Prints an info-level message (level 6) prefixed with device information.761///762/// This level should be used for informational messages.763///764/// Equivalent to the kernel's `dev_info` macro.765///766/// Mimics the interface of [`std::print!`]. More information about the syntax is available from767/// [`core::fmt`] and [`std::format!`].768///769/// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html770/// [`std::format!`]: https://doc.rust-lang.org/std/macro.format.html771///772/// # Examples773///774/// ```775/// # use kernel::device::Device;776///777/// fn example(dev: &Device) {778/// dev_info!(dev, "hello {}\n", "there");779/// }780/// ```781#[macro_export]782macro_rules! dev_info {783($($f:tt)*) => { $crate::dev_printk!(pr_info, $($f)*); }784}785786/// Prints a debug-level message (level 7) prefixed with device information.787///788/// This level should be used for debug messages.789///790/// Equivalent to the kernel's `dev_dbg` macro, except that it doesn't support dynamic debug yet.791///792/// Mimics the interface of [`std::print!`]. More information about the syntax is available from793/// [`core::fmt`] and [`std::format!`].794///795/// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html796/// [`std::format!`]: https://doc.rust-lang.org/std/macro.format.html797///798/// # Examples799///800/// ```801/// # use kernel::device::Device;802///803/// fn example(dev: &Device) {804/// dev_dbg!(dev, "hello {}\n", "there");805/// }806/// ```807#[macro_export]808macro_rules! dev_dbg {809($($f:tt)*) => { $crate::dev_printk!(pr_dbg, $($f)*); }810}811812813