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 * #defines for all the dev_<level> macros to prefix with whatever
102 * possible use of #define dev_fmt(fmt) ...
105 #define dev_emerg(dev, fmt, ...) \
106 _dev_emerg(dev, dev_fmt(fmt), ##__VA_ARGS__)
107 #define dev_crit(dev, fmt, ...) \
108 _dev_crit(dev, dev_fmt(fmt), ##__VA_ARGS__)
109 #define dev_alert(dev, fmt, ...) \
110 _dev_alert(dev, dev_fmt(fmt), ##__VA_ARGS__)
111 #define dev_err(dev, fmt, ...) \
112 _dev_err(dev, dev_fmt(fmt), ##__VA_ARGS__)
113 #define dev_warn(dev, fmt, ...) \
114 _dev_warn(dev, dev_fmt(fmt), ##__VA_ARGS__)
115 #define dev_notice(dev, fmt, ...) \
116 _dev_notice(dev, dev_fmt(fmt), ##__VA_ARGS__)
117 #define dev_info(dev, fmt, ...) \
118 _dev_info(dev, dev_fmt(fmt), ##__VA_ARGS__)
120 #if defined(CONFIG_DYNAMIC_DEBUG) || \
121 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
122 #define dev_dbg(dev, fmt, ...) \
123 dynamic_dev_dbg(dev, dev_fmt(fmt), ##__VA_ARGS__)
125 #define dev_dbg(dev, fmt, ...) \
126 dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__)
128 #define dev_dbg(dev, fmt, ...) \
131 dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \
136 #define dev_level_once(dev_level, dev, fmt, ...) \
138 static bool __print_once __read_mostly; \
140 if (!__print_once) { \
141 __print_once = true; \
142 dev_level(dev, fmt, ##__VA_ARGS__); \
146 #define dev_level_once(dev_level, dev, fmt, ...) \
149 dev_level(dev, fmt, ##__VA_ARGS__); \
153 #define dev_emerg_once(dev, fmt, ...) \
154 dev_level_once(dev_emerg, dev, fmt, ##__VA_ARGS__)
155 #define dev_alert_once(dev, fmt, ...) \
156 dev_level_once(dev_alert, dev, fmt, ##__VA_ARGS__)
157 #define dev_crit_once(dev, fmt, ...) \
158 dev_level_once(dev_crit, dev, fmt, ##__VA_ARGS__)
159 #define dev_err_once(dev, fmt, ...) \
160 dev_level_once(dev_err, dev, fmt, ##__VA_ARGS__)
161 #define dev_warn_once(dev, fmt, ...) \
162 dev_level_once(dev_warn, dev, fmt, ##__VA_ARGS__)
163 #define dev_notice_once(dev, fmt, ...) \
164 dev_level_once(dev_notice, dev, fmt, ##__VA_ARGS__)
165 #define dev_info_once(dev, fmt, ...) \
166 dev_level_once(dev_info, dev, fmt, ##__VA_ARGS__)
167 #define dev_dbg_once(dev, fmt, ...) \
168 dev_level_once(dev_dbg, dev, fmt, ##__VA_ARGS__)
170 #define dev_level_ratelimited(dev_level, dev, fmt, ...) \
172 static DEFINE_RATELIMIT_STATE(_rs, \
173 DEFAULT_RATELIMIT_INTERVAL, \
174 DEFAULT_RATELIMIT_BURST); \
175 if (__ratelimit(&_rs)) \
176 dev_level(dev, fmt, ##__VA_ARGS__); \
179 #define dev_emerg_ratelimited(dev, fmt, ...) \
180 dev_level_ratelimited(dev_emerg, dev, fmt, ##__VA_ARGS__)
181 #define dev_alert_ratelimited(dev, fmt, ...) \
182 dev_level_ratelimited(dev_alert, dev, fmt, ##__VA_ARGS__)
183 #define dev_crit_ratelimited(dev, fmt, ...) \
184 dev_level_ratelimited(dev_crit, dev, fmt, ##__VA_ARGS__)
185 #define dev_err_ratelimited(dev, fmt, ...) \
186 dev_level_ratelimited(dev_err, dev, fmt, ##__VA_ARGS__)
187 #define dev_warn_ratelimited(dev, fmt, ...) \
188 dev_level_ratelimited(dev_warn, dev, fmt, ##__VA_ARGS__)
189 #define dev_notice_ratelimited(dev, fmt, ...) \
190 dev_level_ratelimited(dev_notice, dev, fmt, ##__VA_ARGS__)
191 #define dev_info_ratelimited(dev, fmt, ...) \
192 dev_level_ratelimited(dev_info, dev, fmt, ##__VA_ARGS__)
193 #if defined(CONFIG_DYNAMIC_DEBUG) || \
194 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
195 /* descriptor check is first to prevent flooding with "callbacks suppressed" */
196 #define dev_dbg_ratelimited(dev, fmt, ...) \
198 static DEFINE_RATELIMIT_STATE(_rs, \
199 DEFAULT_RATELIMIT_INTERVAL, \
200 DEFAULT_RATELIMIT_BURST); \
201 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \
202 if (DYNAMIC_DEBUG_BRANCH(descriptor) && \
204 __dynamic_dev_dbg(&descriptor, dev, dev_fmt(fmt), \
208 #define dev_dbg_ratelimited(dev, fmt, ...) \
210 static DEFINE_RATELIMIT_STATE(_rs, \
211 DEFAULT_RATELIMIT_INTERVAL, \
212 DEFAULT_RATELIMIT_BURST); \
213 if (__ratelimit(&_rs)) \
214 dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \
217 #define dev_dbg_ratelimited(dev, fmt, ...) \
220 dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \
225 #define dev_vdbg dev_dbg
227 #define dev_vdbg(dev, fmt, ...) \
230 dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \
235 * dev_WARN*() acts like dev_printk(), but with the key difference of
236 * using WARN/WARN_ONCE to include file/line information and a backtrace.
238 #define dev_WARN(dev, format, arg...) \
239 WARN(1, "%s %s: " format, dev_driver_string(dev), dev_name(dev), ## arg)
241 #define dev_WARN_ONCE(dev, condition, format, arg...) \
242 WARN_ONCE(condition, "%s %s: " format, \
243 dev_driver_string(dev), dev_name(dev), ## arg)
245 #endif /* _DEVICE_PRINTK_H_ */