Skip to content

Commit b5d158b

Browse files
authored
Merge pull request #669 from ojeda/printk
rust: kernel: support `!CONFIG_PRINTK`
2 parents 5bded08 + 47d226f commit b5d158b

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

rust/kernel/device.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use crate::{clk::Clk, error::from_kernel_err_ptr};
99

1010
use crate::{
11-
bindings, c_str, c_types,
11+
bindings,
1212
revocable::{Revocable, RevocableGuard},
1313
str::CStr,
1414
sync::{NeedsLockClass, RevocableMutex, RevocableMutexGuard, UniqueRef},
@@ -20,6 +20,9 @@ use core::{
2020
pin::Pin,
2121
};
2222

23+
#[cfg(CONFIG_PRINTK)]
24+
use crate::{c_str, c_types};
25+
2326
/// A raw device.
2427
///
2528
/// # Safety
@@ -138,10 +141,12 @@ pub unsafe trait RawDevice {
138141
///
139142
/// Callers must ensure that `klevel` is null-terminated; in particular, one of the
140143
/// `KERN_*`constants, for example, `KERN_CRIT`, `KERN_ALERT`, etc.
144+
#[cfg_attr(not(CONFIG_PRINTK), allow(unused_variables))]
141145
unsafe fn printk(&self, klevel: &[u8], msg: fmt::Arguments<'_>) {
142146
// SAFETY: `klevel` is null-terminated and one of the kernel constants. `self.raw_device`
143147
// is valid because `self` is valid. The "%pA" format string expects a pointer to
144148
// `fmt::Arguments`, which is what we're passing as the last argument.
149+
#[cfg(CONFIG_PRINTK)]
145150
unsafe {
146151
bindings::_dev_printk(
147152
klevel as *const _ as *const c_types::c_char,

rust/kernel/print.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@
88
99
use core::fmt;
1010

11-
use crate::c_types::{c_char, c_void};
12-
use crate::{bindings, str::Formatter};
11+
use crate::{
12+
c_types::{c_char, c_void},
13+
str::Formatter,
14+
};
15+
16+
#[cfg(CONFIG_PRINTK)]
17+
use crate::bindings;
1318

1419
// Called from `vsprintf` with format specifier `%pA`.
1520
#[no_mangle]
@@ -93,12 +98,14 @@ pub mod format_strings {
9398
///
9499
/// [`_printk`]: ../../../../include/linux/_printk.h
95100
#[doc(hidden)]
101+
#[cfg_attr(not(CONFIG_PRINTK), allow(unused_variables))]
96102
pub unsafe fn call_printk(
97103
format_string: &[u8; format_strings::LENGTH],
98104
module_name: &[u8],
99105
args: fmt::Arguments<'_>,
100106
) {
101107
// `_printk` does not seem to fail in any path.
108+
#[cfg(CONFIG_PRINTK)]
102109
unsafe {
103110
bindings::_printk(
104111
format_string.as_ptr() as _,
@@ -114,10 +121,12 @@ pub unsafe fn call_printk(
114121
///
115122
/// [`_printk`]: ../../../../include/linux/printk.h
116123
#[doc(hidden)]
124+
#[cfg_attr(not(CONFIG_PRINTK), allow(unused_variables))]
117125
pub fn call_printk_cont(args: fmt::Arguments<'_>) {
118126
// `_printk` does not seem to fail in any path.
119127
//
120128
// SAFETY: The format string is fixed.
129+
#[cfg(CONFIG_PRINTK)]
121130
unsafe {
122131
bindings::_printk(
123132
format_strings::CONT.as_ptr() as _,

0 commit comments

Comments
 (0)