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 #define PRINTK_INFO_SUBSYSTEM_LEN	16
25 #define PRINTK_INFO_DEVICE_LEN		48
26 
27 struct dev_printk_info {
28 	char subsystem[PRINTK_INFO_SUBSYSTEM_LEN];
29 	char device[PRINTK_INFO_DEVICE_LEN];
30 };
31 
32 #ifdef CONFIG_PRINTK
33 
34 __printf(3, 0) __cold
35 int dev_vprintk_emit(int level, const struct device *dev,
36 		     const char *fmt, va_list args);
37 __printf(3, 4) __cold
38 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...);
39 
40 __printf(3, 4) __cold
41 void _dev_printk(const char *level, const struct device *dev,
42 		 const char *fmt, ...);
43 __printf(2, 3) __cold
44 void _dev_emerg(const struct device *dev, const char *fmt, ...);
45 __printf(2, 3) __cold
46 void _dev_alert(const struct device *dev, const char *fmt, ...);
47 __printf(2, 3) __cold
48 void _dev_crit(const struct device *dev, const char *fmt, ...);
49 __printf(2, 3) __cold
50 void _dev_err(const struct device *dev, const char *fmt, ...);
51 __printf(2, 3) __cold
52 void _dev_warn(const struct device *dev, const char *fmt, ...);
53 __printf(2, 3) __cold
54 void _dev_notice(const struct device *dev, const char *fmt, ...);
55 __printf(2, 3) __cold
56 void _dev_info(const struct device *dev, const char *fmt, ...);
57 
58 #else
59 
60 static inline __printf(3, 0)
dev_vprintk_emit(int level,const struct device * dev,const char * fmt,va_list args)61 int dev_vprintk_emit(int level, const struct device *dev,
62 		     const char *fmt, va_list args)
63 { return 0; }
64 static inline __printf(3, 4)
dev_printk_emit(int level,const struct device * dev,const char * fmt,...)65 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
66 { return 0; }
67 
__dev_printk(const char * level,const struct device * dev,struct va_format * vaf)68 static inline void __dev_printk(const char *level, const struct device *dev,
69 				struct va_format *vaf)
70 {}
71 static inline __printf(3, 4)
_dev_printk(const char * level,const struct device * dev,const char * fmt,...)72 void _dev_printk(const char *level, const struct device *dev,
73 		 const char *fmt, ...)
74 {}
75 
76 static inline __printf(2, 3)
_dev_emerg(const struct device * dev,const char * fmt,...)77 void _dev_emerg(const struct device *dev, const char *fmt, ...)
78 {}
79 static inline __printf(2, 3)
_dev_crit(const struct device * dev,const char * fmt,...)80 void _dev_crit(const struct device *dev, const char *fmt, ...)
81 {}
82 static inline __printf(2, 3)
_dev_alert(const struct device * dev,const char * fmt,...)83 void _dev_alert(const struct device *dev, const char *fmt, ...)
84 {}
85 static inline __printf(2, 3)
_dev_err(const struct device * dev,const char * fmt,...)86 void _dev_err(const struct device *dev, const char *fmt, ...)
87 {}
88 static inline __printf(2, 3)
_dev_warn(const struct device * dev,const char * fmt,...)89 void _dev_warn(const struct device *dev, const char *fmt, ...)
90 {}
91 static inline __printf(2, 3)
_dev_notice(const struct device * dev,const char * fmt,...)92 void _dev_notice(const struct device *dev, const char *fmt, ...)
93 {}
94 static inline __printf(2, 3)
_dev_info(const struct device * dev,const char * fmt,...)95 void _dev_info(const struct device *dev, const char *fmt, ...)
96 {}
97 
98 #endif
99 
100 /*
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.
103  */
104 #define dev_printk_index_emit(level, fmt, ...) \
105 	printk_index_subsys_emit("%s %s: ", level, fmt)
106 
107 #define dev_printk_index_wrap(_p_func, level, dev, fmt, ...)		\
108 	({								\
109 		dev_printk_index_emit(level, fmt);			\
110 		_p_func(dev, fmt, ##__VA_ARGS__);			\
111 	})
112 
113 /*
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.
119  *
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.
122  *
123  * We also can't use dev_printk_index_wrap directly, because we have a separate
124  * level to process.
125  */
126 #define dev_printk(level, dev, fmt, ...)				\
127 	({								\
128 		dev_printk_index_emit(level, fmt);			\
129 		_dev_printk(level, dev, fmt, ##__VA_ARGS__);		\
130 	})
131 
132 /*
133  * #defines for all the dev_<level> macros to prefix with whatever
134  * possible use of #define dev_fmt(fmt) ...
135  */
136 
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__)
151 
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__)
156 #elif defined(DEBUG)
157 #define dev_dbg(dev, fmt, ...)						\
158 	dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__)
159 #else
160 #define dev_dbg(dev, fmt, ...)						\
161 ({									\
162 	if (0)								\
163 		dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \
164 })
165 #endif
166 
167 #ifdef CONFIG_PRINTK
168 #define dev_level_once(dev_level, dev, fmt, ...)			\
169 do {									\
170 	static bool __print_once __read_mostly;				\
171 									\
172 	if (!__print_once) {						\
173 		__print_once = true;					\
174 		dev_level(dev, fmt, ##__VA_ARGS__);			\
175 	}								\
176 } while (0)
177 #else
178 #define dev_level_once(dev_level, dev, fmt, ...)			\
179 do {									\
180 	if (0)								\
181 		dev_level(dev, fmt, ##__VA_ARGS__);			\
182 } while (0)
183 #endif
184 
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__)
201 
202 #define dev_level_ratelimited(dev_level, dev, fmt, ...)			\
203 do {									\
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__);			\
209 } while (0)
210 
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, ...)				\
229 do {									\
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) &&				\
235 	    __ratelimit(&_rs))						\
236 		__dynamic_dev_dbg(&descriptor, dev, dev_fmt(fmt),	\
237 				  ##__VA_ARGS__);			\
238 } while (0)
239 #elif defined(DEBUG)
240 #define dev_dbg_ratelimited(dev, fmt, ...)				\
241 do {									\
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__); \
247 } while (0)
248 #else
249 #define dev_dbg_ratelimited(dev, fmt, ...)				\
250 do {									\
251 	if (0)								\
252 		dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \
253 } while (0)
254 #endif
255 
256 #ifdef VERBOSE_DEBUG
257 #define dev_vdbg	dev_dbg
258 #else
259 #define dev_vdbg(dev, fmt, ...)						\
260 ({									\
261 	if (0)								\
262 		dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \
263 })
264 #endif
265 
266 /*
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.
269  */
270 #define dev_WARN(dev, format, arg...) \
271 	WARN(1, "%s %s: " format, dev_driver_string(dev), dev_name(dev), ## arg)
272 
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)
276 
277 __printf(3, 4) int dev_err_probe(const struct device *dev, int err, const char *fmt, ...);
278 
279 #endif /* _DEVICE_PRINTK_H_ */
280