diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs index 0ec3dd66813834..424075c4c32e8f 100644 --- a/rust/kernel/device.rs +++ b/rust/kernel/device.rs @@ -8,7 +8,7 @@ use crate::{clk::Clk, error::from_kernel_err_ptr}; use crate::{ - bindings, c_str, c_types, + bindings, revocable::{Revocable, RevocableGuard}, str::CStr, sync::{NeedsLockClass, RevocableMutex, RevocableMutexGuard, UniqueRef}, @@ -20,6 +20,9 @@ use core::{ pin::Pin, }; +#[cfg(CONFIG_PRINTK)] +use crate::{c_str, c_types}; + /// A raw device. /// /// # Safety @@ -138,10 +141,12 @@ pub unsafe trait RawDevice { /// /// Callers must ensure that `klevel` is null-terminated; in particular, one of the /// `KERN_*`constants, for example, `KERN_CRIT`, `KERN_ALERT`, etc. + #[cfg_attr(not(CONFIG_PRINTK), allow(unused_variables))] unsafe fn printk(&self, klevel: &[u8], msg: fmt::Arguments<'_>) { // SAFETY: `klevel` is null-terminated and one of the kernel constants. `self.raw_device` // is valid because `self` is valid. The "%pA" format string expects a pointer to // `fmt::Arguments`, which is what we're passing as the last argument. + #[cfg(CONFIG_PRINTK)] unsafe { bindings::_dev_printk( klevel as *const _ as *const c_types::c_char, diff --git a/rust/kernel/print.rs b/rust/kernel/print.rs index e75fe088f284e9..dba4ef10c72243 100644 --- a/rust/kernel/print.rs +++ b/rust/kernel/print.rs @@ -8,8 +8,13 @@ use core::fmt; -use crate::c_types::{c_char, c_void}; -use crate::{bindings, str::Formatter}; +use crate::{ + c_types::{c_char, c_void}, + str::Formatter, +}; + +#[cfg(CONFIG_PRINTK)] +use crate::bindings; // Called from `vsprintf` with format specifier `%pA`. #[no_mangle] @@ -93,12 +98,14 @@ pub mod format_strings { /// /// [`_printk`]: ../../../../include/linux/_printk.h #[doc(hidden)] +#[cfg_attr(not(CONFIG_PRINTK), allow(unused_variables))] pub unsafe fn call_printk( format_string: &[u8; format_strings::LENGTH], module_name: &[u8], args: fmt::Arguments<'_>, ) { // `_printk` does not seem to fail in any path. + #[cfg(CONFIG_PRINTK)] unsafe { bindings::_printk( format_string.as_ptr() as _, @@ -114,10 +121,12 @@ pub unsafe fn call_printk( /// /// [`_printk`]: ../../../../include/linux/printk.h #[doc(hidden)] +#[cfg_attr(not(CONFIG_PRINTK), allow(unused_variables))] pub fn call_printk_cont(args: fmt::Arguments<'_>) { // `_printk` does not seem to fail in any path. // // SAFETY: The format string is fixed. + #[cfg(CONFIG_PRINTK)] unsafe { bindings::_printk( format_strings::CONT.as_ptr() as _,