xref: /linux/include/linux/dev_printk.h (revision 132db93572821ec2fdf81e354cc40f558faf7e4f)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * dev_printk.h - printk messages helpers for devices
4  *
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.
8  *
9  */
10 
11 #ifndef _DEVICE_PRINTK_H_
12 #define _DEVICE_PRINTK_H_
13 
14 #include <linux/compiler.h>
15 #include <linux/types.h>
16 #include <linux/ratelimit.h>
17 
18 #ifndef dev_fmt
19 #define dev_fmt(fmt) fmt
20 #endif
21 
22 struct device;
23 
24 #ifdef CONFIG_PRINTK
25 
26 __printf(3, 0) __cold
27 int dev_vprintk_emit(int level, const struct device *dev,
28 		     const char *fmt, va_list args);
29 __printf(3, 4) __cold
30 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...);
31 
32 __printf(3, 4) __cold
33 void dev_printk(const char *level, const struct device *dev,
34 		const char *fmt, ...);
35 __printf(2, 3) __cold
36 void _dev_emerg(const struct device *dev, const char *fmt, ...);
37 __printf(2, 3) __cold
38 void _dev_alert(const struct device *dev, const char *fmt, ...);
39 __printf(2, 3) __cold
40 void _dev_crit(const struct device *dev, const char *fmt, ...);
41 __printf(2, 3) __cold
42 void _dev_err(const struct device *dev, const char *fmt, ...);
43 __printf(2, 3) __cold
44 void _dev_warn(const struct device *dev, const char *fmt, ...);
45 __printf(2, 3) __cold
46 void _dev_notice(const struct device *dev, const char *fmt, ...);
47 __printf(2, 3) __cold
48 void _dev_info(const struct device *dev, const char *fmt, ...);
49 
50 #else
51 
52 static inline __printf(3, 0)
53 int dev_vprintk_emit(int level, const struct device *dev,
54 		     const char *fmt, va_list args)
55 { return 0; }
56 static inline __printf(3, 4)
57 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
58 { return 0; }
59 
60 static inline void __dev_printk(const char *level, const struct device *dev,
61 				struct va_format *vaf)
62 {}
63 static inline __printf(3, 4)
64 void dev_printk(const char *level, const struct device *dev,
65 		 const char *fmt, ...)
66 {}
67 
68 static inline __printf(2, 3)
69 void _dev_emerg(const struct device *dev, const char *fmt, ...)
70 {}
71 static inline __printf(2, 3)
72 void _dev_crit(const struct device *dev, const char *fmt, ...)
73 {}
74 static inline __printf(2, 3)
75 void _dev_alert(const struct device *dev, const char *fmt, ...)
76 {}
77 static inline __printf(2, 3)
78 void _dev_err(const struct device *dev, const char *fmt, ...)
79 {}
80 static inline __printf(2, 3)
81 void _dev_warn(const struct device *dev, const char *fmt, ...)
82 {}
83 static inline __printf(2, 3)
84 void _dev_notice(const struct device *dev, const char *fmt, ...)
85 {}
86 static inline __printf(2, 3)
87 void _dev_info(const struct device *dev, const char *fmt, ...)
88 {}
89 
90 #endif
91 
92 /*
93  * #defines for all the dev_<level> macros to prefix with whatever
94  * possible use of #define dev_fmt(fmt) ...
95  */
96 
97 #define dev_emerg(dev, fmt, ...)					\
98 	_dev_emerg(dev, dev_fmt(fmt), ##__VA_ARGS__)
99 #define dev_crit(dev, fmt, ...)						\
100 	_dev_crit(dev, dev_fmt(fmt), ##__VA_ARGS__)
101 #define dev_alert(dev, fmt, ...)					\
102 	_dev_alert(dev, dev_fmt(fmt), ##__VA_ARGS__)
103 #define dev_err(dev, fmt, ...)						\
104 	_dev_err(dev, dev_fmt(fmt), ##__VA_ARGS__)
105 #define dev_warn(dev, fmt, ...)						\
106 	_dev_warn(dev, dev_fmt(fmt), ##__VA_ARGS__)
107 #define dev_notice(dev, fmt, ...)					\
108 	_dev_notice(dev, dev_fmt(fmt), ##__VA_ARGS__)
109 #define dev_info(dev, fmt, ...)						\
110 	_dev_info(dev, dev_fmt(fmt), ##__VA_ARGS__)
111 
112 #if defined(CONFIG_DYNAMIC_DEBUG) || \
113 	(defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
114 #define dev_dbg(dev, fmt, ...)						\
115 	dynamic_dev_dbg(dev, dev_fmt(fmt), ##__VA_ARGS__)
116 #elif defined(DEBUG)
117 #define dev_dbg(dev, fmt, ...)						\
118 	dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__)
119 #else
120 #define dev_dbg(dev, fmt, ...)						\
121 ({									\
122 	if (0)								\
123 		dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \
124 })
125 #endif
126 
127 #ifdef CONFIG_PRINTK
128 #define dev_level_once(dev_level, dev, fmt, ...)			\
129 do {									\
130 	static bool __print_once __read_mostly;				\
131 									\
132 	if (!__print_once) {						\
133 		__print_once = true;					\
134 		dev_level(dev, fmt, ##__VA_ARGS__);			\
135 	}								\
136 } while (0)
137 #else
138 #define dev_level_once(dev_level, dev, fmt, ...)			\
139 do {									\
140 	if (0)								\
141 		dev_level(dev, fmt, ##__VA_ARGS__);			\
142 } while (0)
143 #endif
144 
145 #define dev_emerg_once(dev, fmt, ...)					\
146 	dev_level_once(dev_emerg, dev, fmt, ##__VA_ARGS__)
147 #define dev_alert_once(dev, fmt, ...)					\
148 	dev_level_once(dev_alert, dev, fmt, ##__VA_ARGS__)
149 #define dev_crit_once(dev, fmt, ...)					\
150 	dev_level_once(dev_crit, dev, fmt, ##__VA_ARGS__)
151 #define dev_err_once(dev, fmt, ...)					\
152 	dev_level_once(dev_err, dev, fmt, ##__VA_ARGS__)
153 #define dev_warn_once(dev, fmt, ...)					\
154 	dev_level_once(dev_warn, dev, fmt, ##__VA_ARGS__)
155 #define dev_notice_once(dev, fmt, ...)					\
156 	dev_level_once(dev_notice, dev, fmt, ##__VA_ARGS__)
157 #define dev_info_once(dev, fmt, ...)					\
158 	dev_level_once(dev_info, dev, fmt, ##__VA_ARGS__)
159 #define dev_dbg_once(dev, fmt, ...)					\
160 	dev_level_once(dev_dbg, dev, fmt, ##__VA_ARGS__)
161 
162 #define dev_level_ratelimited(dev_level, dev, fmt, ...)			\
163 do {									\
164 	static DEFINE_RATELIMIT_STATE(_rs,				\
165 				      DEFAULT_RATELIMIT_INTERVAL,	\
166 				      DEFAULT_RATELIMIT_BURST);		\
167 	if (__ratelimit(&_rs))						\
168 		dev_level(dev, fmt, ##__VA_ARGS__);			\
169 } while (0)
170 
171 #define dev_emerg_ratelimited(dev, fmt, ...)				\
172 	dev_level_ratelimited(dev_emerg, dev, fmt, ##__VA_ARGS__)
173 #define dev_alert_ratelimited(dev, fmt, ...)				\
174 	dev_level_ratelimited(dev_alert, dev, fmt, ##__VA_ARGS__)
175 #define dev_crit_ratelimited(dev, fmt, ...)				\
176 	dev_level_ratelimited(dev_crit, dev, fmt, ##__VA_ARGS__)
177 #define dev_err_ratelimited(dev, fmt, ...)				\
178 	dev_level_ratelimited(dev_err, dev, fmt, ##__VA_ARGS__)
179 #define dev_warn_ratelimited(dev, fmt, ...)				\
180 	dev_level_ratelimited(dev_warn, dev, fmt, ##__VA_ARGS__)
181 #define dev_notice_ratelimited(dev, fmt, ...)				\
182 	dev_level_ratelimited(dev_notice, dev, fmt, ##__VA_ARGS__)
183 #define dev_info_ratelimited(dev, fmt, ...)				\
184 	dev_level_ratelimited(dev_info, dev, fmt, ##__VA_ARGS__)
185 #if defined(CONFIG_DYNAMIC_DEBUG) || \
186 	(defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
187 /* descriptor check is first to prevent flooding with "callbacks suppressed" */
188 #define dev_dbg_ratelimited(dev, fmt, ...)				\
189 do {									\
190 	static DEFINE_RATELIMIT_STATE(_rs,				\
191 				      DEFAULT_RATELIMIT_INTERVAL,	\
192 				      DEFAULT_RATELIMIT_BURST);		\
193 	DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt);			\
194 	if (DYNAMIC_DEBUG_BRANCH(descriptor) &&				\
195 	    __ratelimit(&_rs))						\
196 		__dynamic_dev_dbg(&descriptor, dev, dev_fmt(fmt),	\
197 				  ##__VA_ARGS__);			\
198 } while (0)
199 #elif defined(DEBUG)
200 #define dev_dbg_ratelimited(dev, fmt, ...)				\
201 do {									\
202 	static DEFINE_RATELIMIT_STATE(_rs,				\
203 				      DEFAULT_RATELIMIT_INTERVAL,	\
204 				      DEFAULT_RATELIMIT_BURST);		\
205 	if (__ratelimit(&_rs))						\
206 		dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \
207 } while (0)
208 #else
209 #define dev_dbg_ratelimited(dev, fmt, ...)				\
210 do {									\
211 	if (0)								\
212 		dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \
213 } while (0)
214 #endif
215 
216 #ifdef VERBOSE_DEBUG
217 #define dev_vdbg	dev_dbg
218 #else
219 #define dev_vdbg(dev, fmt, ...)						\
220 ({									\
221 	if (0)								\
222 		dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \
223 })
224 #endif
225 
226 /*
227  * dev_WARN*() acts like dev_printk(), but with the key difference of
228  * using WARN/WARN_ONCE to include file/line information and a backtrace.
229  */
230 #define dev_WARN(dev, format, arg...) \
231 	WARN(1, "%s %s: " format, dev_driver_string(dev), dev_name(dev), ## arg);
232 
233 #define dev_WARN_ONCE(dev, condition, format, arg...) \
234 	WARN_ONCE(condition, "%s %s: " format, \
235 			dev_driver_string(dev), dev_name(dev), ## arg)
236 
237 #endif /* _DEVICE_PRINTK_H_ */
238