1 // SPDX-License-Identifier: GPL-2.0
3 * dev_printk.h - printk messages helpers for devices
5 * Copyright (c) 2001-2003 Patrick Mochel <mochel@osdl.org>
6 * Copyright (c) 2004-2009 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (c) 2008-2009 Novell Inc.
11 #ifndef _DEVICE_PRINTK_H_
12 #define _DEVICE_PRINTK_H_
14 #include <linux/compiler.h>
15 #include <linux/types.h>
16 #include <linux/ratelimit.h>
19 #define dev_fmt(fmt) fmt
24 #define PRINTK_INFO_SUBSYSTEM_LEN 16
25 #define PRINTK_INFO_DEVICE_LEN 48
27 struct dev_printk_info
{
28 char subsystem
[PRINTK_INFO_SUBSYSTEM_LEN
];
29 char device
[PRINTK_INFO_DEVICE_LEN
];
35 int dev_vprintk_emit(int level
, const struct device
*dev
,
36 const char *fmt
, va_list args
);
38 int dev_printk_emit(int level
, const struct device
*dev
, const char *fmt
, ...);
41 void _dev_printk(const char *level
, const struct device
*dev
,
42 const char *fmt
, ...);
44 void _dev_emerg(const struct device
*dev
, const char *fmt
, ...);
46 void _dev_alert(const struct device
*dev
, const char *fmt
, ...);
48 void _dev_crit(const struct device
*dev
, const char *fmt
, ...);
50 void _dev_err(const struct device
*dev
, const char *fmt
, ...);
52 void _dev_warn(const struct device
*dev
, const char *fmt
, ...);
54 void _dev_notice(const struct device
*dev
, const char *fmt
, ...);
56 void _dev_info(const struct device
*dev
, const char *fmt
, ...);
60 static inline __printf(3, 0)
61 int dev_vprintk_emit(int level
, const struct device
*dev
,
62 const char *fmt
, va_list args
)
64 static inline __printf(3, 4)
65 int dev_printk_emit(int level
, const struct device
*dev
, const char *fmt
, ...)
68 static inline void __dev_printk(const char *level
, const struct device
*dev
,
69 struct va_format
*vaf
)
71 static inline __printf(3, 4)
72 void _dev_printk(const char *level
, const struct device
*dev
,
76 static inline __printf(2, 3)
77 void _dev_emerg(const struct device
*dev
, const char *fmt
, ...)
79 static inline __printf(2, 3)
80 void _dev_crit(const struct device
*dev
, const char *fmt
, ...)
82 static inline __printf(2, 3)
83 void _dev_alert(const struct device
*dev
, const char *fmt
, ...)
85 static inline __printf(2, 3)
86 void _dev_err(const struct device
*dev
, const char *fmt
, ...)
88 static inline __printf(2, 3)
89 void _dev_warn(const struct device
*dev
, const char *fmt
, ...)
91 static inline __printf(2, 3)
92 void _dev_notice(const struct device
*dev
, const char *fmt
, ...)
94 static inline __printf(2, 3)
95 void _dev_info(const struct device
*dev
, const char *fmt
, ...)
101 * Need to take variadic arguments even though we don't use them, as dev_fmt()
102 * may only just have been expanded and may result in multiple arguments.
104 #define dev_printk_index_emit(level, fmt, ...) \
105 printk_index_subsys_emit("%s %s: ", level, fmt)
107 #define dev_printk_index_wrap(_p_func, level, dev, fmt, ...) \
109 dev_printk_index_emit(level, fmt); \
110 _p_func(dev, fmt, ##__VA_ARGS__); \
114 * Some callsites directly call dev_printk rather than going through the
115 * dev_<level> infrastructure, so we need to emit here as well as inside those
116 * level-specific macros. Only one index entry will be produced, either way,
117 * since dev_printk's `fmt` isn't known at compile time if going through the
118 * dev_<level> macros.
120 * dev_fmt() isn't called for dev_printk when used directly, as it's used by
121 * the dev_<level> macros internally which already have dev_fmt() processed.
123 * We also can't use dev_printk_index_wrap directly, because we have a separate
126 #define dev_printk(level, dev, fmt, ...) \
128 dev_printk_index_emit(level, fmt); \
129 _dev_printk(level, dev, fmt, ##__VA_ARGS__); \
133 * Dummy dev_printk for disabled debugging statements to use whilst maintaining
134 * gcc's format checking.
136 #define dev_no_printk(level, dev, fmt, ...) \
139 _dev_printk(level, dev, fmt, ##__VA_ARGS__); \
143 * #defines for all the dev_<level> macros to prefix with whatever
144 * possible use of #define dev_fmt(fmt) ...
147 #define dev_emerg(dev, fmt, ...) \
148 dev_printk_index_wrap(_dev_emerg, KERN_EMERG, dev, dev_fmt(fmt), ##__VA_ARGS__)
149 #define dev_crit(dev, fmt, ...) \
150 dev_printk_index_wrap(_dev_crit, KERN_CRIT, dev, dev_fmt(fmt), ##__VA_ARGS__)
151 #define dev_alert(dev, fmt, ...) \
152 dev_printk_index_wrap(_dev_alert, KERN_ALERT, dev, dev_fmt(fmt), ##__VA_ARGS__)
153 #define dev_err(dev, fmt, ...) \
154 dev_printk_index_wrap(_dev_err, KERN_ERR, dev, dev_fmt(fmt), ##__VA_ARGS__)
155 #define dev_warn(dev, fmt, ...) \
156 dev_printk_index_wrap(_dev_warn, KERN_WARNING, dev, dev_fmt(fmt), ##__VA_ARGS__)
157 #define dev_notice(dev, fmt, ...) \
158 dev_printk_index_wrap(_dev_notice, KERN_NOTICE, dev, dev_fmt(fmt), ##__VA_ARGS__)
159 #define dev_info(dev, fmt, ...) \
160 dev_printk_index_wrap(_dev_info, KERN_INFO, dev, dev_fmt(fmt), ##__VA_ARGS__)
162 #if defined(CONFIG_DYNAMIC_DEBUG) || \
163 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
164 #define dev_dbg(dev, fmt, ...) \
165 dynamic_dev_dbg(dev, dev_fmt(fmt), ##__VA_ARGS__)
167 #define dev_dbg(dev, fmt, ...) \
168 dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__)
170 #define dev_dbg(dev, fmt, ...) \
171 dev_no_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__)
175 #define dev_level_once(dev_level, dev, fmt, ...) \
177 static bool __print_once __read_mostly; \
179 if (!__print_once) { \
180 __print_once = true; \
181 dev_level(dev, fmt, ##__VA_ARGS__); \
185 #define dev_level_once(dev_level, dev, fmt, ...) \
188 dev_level(dev, fmt, ##__VA_ARGS__); \
192 #define dev_emerg_once(dev, fmt, ...) \
193 dev_level_once(dev_emerg, dev, fmt, ##__VA_ARGS__)
194 #define dev_alert_once(dev, fmt, ...) \
195 dev_level_once(dev_alert, dev, fmt, ##__VA_ARGS__)
196 #define dev_crit_once(dev, fmt, ...) \
197 dev_level_once(dev_crit, dev, fmt, ##__VA_ARGS__)
198 #define dev_err_once(dev, fmt, ...) \
199 dev_level_once(dev_err, dev, fmt, ##__VA_ARGS__)
200 #define dev_warn_once(dev, fmt, ...) \
201 dev_level_once(dev_warn, dev, fmt, ##__VA_ARGS__)
202 #define dev_notice_once(dev, fmt, ...) \
203 dev_level_once(dev_notice, dev, fmt, ##__VA_ARGS__)
204 #define dev_info_once(dev, fmt, ...) \
205 dev_level_once(dev_info, dev, fmt, ##__VA_ARGS__)
206 #define dev_dbg_once(dev, fmt, ...) \
207 dev_level_once(dev_dbg, dev, fmt, ##__VA_ARGS__)
209 #define dev_level_ratelimited(dev_level, dev, fmt, ...) \
211 static DEFINE_RATELIMIT_STATE(_rs, \
212 DEFAULT_RATELIMIT_INTERVAL, \
213 DEFAULT_RATELIMIT_BURST); \
214 if (__ratelimit(&_rs)) \
215 dev_level(dev, fmt, ##__VA_ARGS__); \
218 #define dev_emerg_ratelimited(dev, fmt, ...) \
219 dev_level_ratelimited(dev_emerg, dev, fmt, ##__VA_ARGS__)
220 #define dev_alert_ratelimited(dev, fmt, ...) \
221 dev_level_ratelimited(dev_alert, dev, fmt, ##__VA_ARGS__)
222 #define dev_crit_ratelimited(dev, fmt, ...) \
223 dev_level_ratelimited(dev_crit, dev, fmt, ##__VA_ARGS__)
224 #define dev_err_ratelimited(dev, fmt, ...) \
225 dev_level_ratelimited(dev_err, dev, fmt, ##__VA_ARGS__)
226 #define dev_warn_ratelimited(dev, fmt, ...) \
227 dev_level_ratelimited(dev_warn, dev, fmt, ##__VA_ARGS__)
228 #define dev_notice_ratelimited(dev, fmt, ...) \
229 dev_level_ratelimited(dev_notice, dev, fmt, ##__VA_ARGS__)
230 #define dev_info_ratelimited(dev, fmt, ...) \
231 dev_level_ratelimited(dev_info, dev, fmt, ##__VA_ARGS__)
232 #if defined(CONFIG_DYNAMIC_DEBUG) || \
233 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
234 /* descriptor check is first to prevent flooding with "callbacks suppressed" */
235 #define dev_dbg_ratelimited(dev, fmt, ...) \
237 static DEFINE_RATELIMIT_STATE(_rs, \
238 DEFAULT_RATELIMIT_INTERVAL, \
239 DEFAULT_RATELIMIT_BURST); \
240 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \
241 if (DYNAMIC_DEBUG_BRANCH(descriptor) && \
243 __dynamic_dev_dbg(&descriptor, dev, dev_fmt(fmt), \
247 #define dev_dbg_ratelimited(dev, fmt, ...) \
249 static DEFINE_RATELIMIT_STATE(_rs, \
250 DEFAULT_RATELIMIT_INTERVAL, \
251 DEFAULT_RATELIMIT_BURST); \
252 if (__ratelimit(&_rs)) \
253 dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \
256 #define dev_dbg_ratelimited(dev, fmt, ...) \
257 dev_no_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__)
261 #define dev_vdbg dev_dbg
263 #define dev_vdbg(dev, fmt, ...) \
264 dev_no_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__)
268 * dev_WARN*() acts like dev_printk(), but with the key difference of
269 * using WARN/WARN_ONCE to include file/line information and a backtrace.
271 #define dev_WARN(dev, format, arg...) \
272 WARN(1, "%s %s: " format, dev_driver_string(dev), dev_name(dev), ## arg)
274 #define dev_WARN_ONCE(dev, condition, format, arg...) \
275 WARN_ONCE(condition, "%s %s: " format, \
276 dev_driver_string(dev), dev_name(dev), ## arg)
278 __printf(3, 4) int dev_err_probe(const struct device
*dev
, int err
, const char *fmt
, ...);
279 __printf(3, 4) int dev_warn_probe(const struct device
*dev
, int err
, const char *fmt
, ...);
281 /* Simple helper for dev_err_probe() when ERR_PTR() is to be returned. */
282 #define dev_err_ptr_probe(dev, ___err, fmt, ...) \
283 ERR_PTR(dev_err_probe(dev, ___err, fmt, ##__VA_ARGS__))
285 /* Simple helper for dev_err_probe() when ERR_CAST() is to be returned. */
286 #define dev_err_cast_probe(dev, ___err_ptr, fmt, ...) \
287 ERR_PTR(dev_err_probe(dev, PTR_ERR(___err_ptr), fmt, ##__VA_ARGS__))
289 #endif /* _DEVICE_PRINTK_H_ */