xref: /linux/include/linux/dev_printk.h (revision 02680c23d7b3febe45ea3d4f9818c2b2dc89020a)
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)
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)
65 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
66 { return 0; }
67 
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)
72 void dev_printk(const char *level, const struct device *dev,
73 		 const char *fmt, ...)
74 {}
75 
76 static inline __printf(2, 3)
77 void _dev_emerg(const struct device *dev, const char *fmt, ...)
78 {}
79 static inline __printf(2, 3)
80 void _dev_crit(const struct device *dev, const char *fmt, ...)
81 {}
82 static inline __printf(2, 3)
83 void _dev_alert(const struct device *dev, const char *fmt, ...)
84 {}
85 static inline __printf(2, 3)
86 void _dev_err(const struct device *dev, const char *fmt, ...)
87 {}
88 static inline __printf(2, 3)
89 void _dev_warn(const struct device *dev, const char *fmt, ...)
90 {}
91 static inline __printf(2, 3)
92 void _dev_notice(const struct device *dev, const char *fmt, ...)
93 {}
94 static inline __printf(2, 3)
95 void _dev_info(const struct device *dev, const char *fmt, ...)
96 {}
97 
98 #endif
99 
100 /*
101  * #defines for all the dev_<level> macros to prefix with whatever
102  * possible use of #define dev_fmt(fmt) ...
103  */
104 
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__)
119 
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__)
124 #elif defined(DEBUG)
125 #define dev_dbg(dev, fmt, ...)						\
126 	dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__)
127 #else
128 #define dev_dbg(dev, fmt, ...)						\
129 ({									\
130 	if (0)								\
131 		dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \
132 })
133 #endif
134 
135 #ifdef CONFIG_PRINTK
136 #define dev_level_once(dev_level, dev, fmt, ...)			\
137 do {									\
138 	static bool __print_once __read_mostly;				\
139 									\
140 	if (!__print_once) {						\
141 		__print_once = true;					\
142 		dev_level(dev, fmt, ##__VA_ARGS__);			\
143 	}								\
144 } while (0)
145 #else
146 #define dev_level_once(dev_level, dev, fmt, ...)			\
147 do {									\
148 	if (0)								\
149 		dev_level(dev, fmt, ##__VA_ARGS__);			\
150 } while (0)
151 #endif
152 
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__)
169 
170 #define dev_level_ratelimited(dev_level, dev, fmt, ...)			\
171 do {									\
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__);			\
177 } while (0)
178 
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, ...)				\
197 do {									\
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) &&				\
203 	    __ratelimit(&_rs))						\
204 		__dynamic_dev_dbg(&descriptor, dev, dev_fmt(fmt),	\
205 				  ##__VA_ARGS__);			\
206 } while (0)
207 #elif defined(DEBUG)
208 #define dev_dbg_ratelimited(dev, fmt, ...)				\
209 do {									\
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__); \
215 } while (0)
216 #else
217 #define dev_dbg_ratelimited(dev, fmt, ...)				\
218 do {									\
219 	if (0)								\
220 		dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \
221 } while (0)
222 #endif
223 
224 #ifdef VERBOSE_DEBUG
225 #define dev_vdbg	dev_dbg
226 #else
227 #define dev_vdbg(dev, fmt, ...)						\
228 ({									\
229 	if (0)								\
230 		dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \
231 })
232 #endif
233 
234 /*
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.
237  */
238 #define dev_WARN(dev, format, arg...) \
239 	WARN(1, "%s %s: " format, dev_driver_string(dev), dev_name(dev), ## arg);
240 
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)
244 
245 #endif /* _DEVICE_PRINTK_H_ */
246