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 * #defines for all the dev_<level> macros to prefix with whatever
134 * possible use of #define dev_fmt(fmt) ...
137 #define dev_emerg(dev, fmt, ...) \
138 dev_printk_index_wrap(_dev_emerg, KERN_EMERG, dev, dev_fmt(fmt), ##__VA_ARGS__)
139 #define dev_crit(dev, fmt, ...) \
140 dev_printk_index_wrap(_dev_crit, KERN_CRIT, dev, dev_fmt(fmt), ##__VA_ARGS__)
141 #define dev_alert(dev, fmt, ...) \
142 dev_printk_index_wrap(_dev_alert, KERN_ALERT, dev, dev_fmt(fmt), ##__VA_ARGS__)
143 #define dev_err(dev, fmt, ...) \
144 dev_printk_index_wrap(_dev_err, KERN_ERR, dev, dev_fmt(fmt), ##__VA_ARGS__)
145 #define dev_warn(dev, fmt, ...) \
146 dev_printk_index_wrap(_dev_warn, KERN_WARNING, dev, dev_fmt(fmt), ##__VA_ARGS__)
147 #define dev_notice(dev, fmt, ...) \
148 dev_printk_index_wrap(_dev_notice, KERN_NOTICE, dev, dev_fmt(fmt), ##__VA_ARGS__)
149 #define dev_info(dev, fmt, ...) \
150 dev_printk_index_wrap(_dev_info, KERN_INFO, dev, dev_fmt(fmt), ##__VA_ARGS__)
152 #if defined(CONFIG_DYNAMIC_DEBUG) || \
153 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
154 #define dev_dbg(dev, fmt, ...) \
155 dynamic_dev_dbg(dev, dev_fmt(fmt), ##__VA_ARGS__)
157 #define dev_dbg(dev, fmt, ...) \
158 dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__)
160 #define dev_dbg(dev, fmt, ...) \
163 dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \
168 #define dev_level_once(dev_level, dev, fmt, ...) \
170 static bool __print_once __read_mostly; \
172 if (!__print_once) { \
173 __print_once = true; \
174 dev_level(dev, fmt, ##__VA_ARGS__); \
178 #define dev_level_once(dev_level, dev, fmt, ...) \
181 dev_level(dev, fmt, ##__VA_ARGS__); \
185 #define dev_emerg_once(dev, fmt, ...) \
186 dev_level_once(dev_emerg, dev, fmt, ##__VA_ARGS__)
187 #define dev_alert_once(dev, fmt, ...) \
188 dev_level_once(dev_alert, dev, fmt, ##__VA_ARGS__)
189 #define dev_crit_once(dev, fmt, ...) \
190 dev_level_once(dev_crit, dev, fmt, ##__VA_ARGS__)
191 #define dev_err_once(dev, fmt, ...) \
192 dev_level_once(dev_err, dev, fmt, ##__VA_ARGS__)
193 #define dev_warn_once(dev, fmt, ...) \
194 dev_level_once(dev_warn, dev, fmt, ##__VA_ARGS__)
195 #define dev_notice_once(dev, fmt, ...) \
196 dev_level_once(dev_notice, dev, fmt, ##__VA_ARGS__)
197 #define dev_info_once(dev, fmt, ...) \
198 dev_level_once(dev_info, dev, fmt, ##__VA_ARGS__)
199 #define dev_dbg_once(dev, fmt, ...) \
200 dev_level_once(dev_dbg, dev, fmt, ##__VA_ARGS__)
202 #define dev_level_ratelimited(dev_level, dev, fmt, ...) \
204 static DEFINE_RATELIMIT_STATE(_rs, \
205 DEFAULT_RATELIMIT_INTERVAL, \
206 DEFAULT_RATELIMIT_BURST); \
207 if (__ratelimit(&_rs)) \
208 dev_level(dev, fmt, ##__VA_ARGS__); \
211 #define dev_emerg_ratelimited(dev, fmt, ...) \
212 dev_level_ratelimited(dev_emerg, dev, fmt, ##__VA_ARGS__)
213 #define dev_alert_ratelimited(dev, fmt, ...) \
214 dev_level_ratelimited(dev_alert, dev, fmt, ##__VA_ARGS__)
215 #define dev_crit_ratelimited(dev, fmt, ...) \
216 dev_level_ratelimited(dev_crit, dev, fmt, ##__VA_ARGS__)
217 #define dev_err_ratelimited(dev, fmt, ...) \
218 dev_level_ratelimited(dev_err, dev, fmt, ##__VA_ARGS__)
219 #define dev_warn_ratelimited(dev, fmt, ...) \
220 dev_level_ratelimited(dev_warn, dev, fmt, ##__VA_ARGS__)
221 #define dev_notice_ratelimited(dev, fmt, ...) \
222 dev_level_ratelimited(dev_notice, dev, fmt, ##__VA_ARGS__)
223 #define dev_info_ratelimited(dev, fmt, ...) \
224 dev_level_ratelimited(dev_info, dev, fmt, ##__VA_ARGS__)
225 #if defined(CONFIG_DYNAMIC_DEBUG) || \
226 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
227 /* descriptor check is first to prevent flooding with "callbacks suppressed" */
228 #define dev_dbg_ratelimited(dev, fmt, ...) \
230 static DEFINE_RATELIMIT_STATE(_rs, \
231 DEFAULT_RATELIMIT_INTERVAL, \
232 DEFAULT_RATELIMIT_BURST); \
233 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \
234 if (DYNAMIC_DEBUG_BRANCH(descriptor) && \
236 __dynamic_dev_dbg(&descriptor, dev, dev_fmt(fmt), \
240 #define dev_dbg_ratelimited(dev, fmt, ...) \
242 static DEFINE_RATELIMIT_STATE(_rs, \
243 DEFAULT_RATELIMIT_INTERVAL, \
244 DEFAULT_RATELIMIT_BURST); \
245 if (__ratelimit(&_rs)) \
246 dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \
249 #define dev_dbg_ratelimited(dev, fmt, ...) \
252 dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \
257 #define dev_vdbg dev_dbg
259 #define dev_vdbg(dev, fmt, ...) \
262 dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \
267 * dev_WARN*() acts like dev_printk(), but with the key difference of
268 * using WARN/WARN_ONCE to include file/line information and a backtrace.
270 #define dev_WARN(dev, format, arg...) \
271 WARN(1, "%s %s: " format, dev_driver_string(dev), dev_name(dev), ## arg)
273 #define dev_WARN_ONCE(dev, condition, format, arg...) \
274 WARN_ONCE(condition, "%s %s: " format, \
275 dev_driver_string(dev), dev_name(dev), ## arg)
277 __printf(3, 4) int dev_err_probe(const struct device *dev, int err, const char *fmt, ...);
279 #endif /* _DEVICE_PRINTK_H_ */