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 * Dummy dev_printk for disabled debugging statements to use whilst maintaining
134 * gcc's format checking.
135 */
136 #define dev_no_printk(level, dev, fmt, ...) \
137 ({ \
138 if (0) \
139 _dev_printk(level, dev, fmt, ##__VA_ARGS__); \
140 })
141
142 /*
143 * #defines for all the dev_<level> macros to prefix with whatever
144 * possible use of #define dev_fmt(fmt) ...
145 */
146
147 #define dev_emerg(dev, fmt, ...) \
148 dev_printk_index_wrap(_dev_emerg, KERN_EMERG, dev, dev_fmt(fmt), ##__VA_ARGS__)
149 #define dev_crit(dev, fmt, ...) \
150 dev_printk_index_wrap(_dev_crit, KERN_CRIT, dev, dev_fmt(fmt), ##__VA_ARGS__)
151 #define dev_alert(dev, fmt, ...) \
152 dev_printk_index_wrap(_dev_alert, KERN_ALERT, dev, dev_fmt(fmt), ##__VA_ARGS__)
153 #define dev_err(dev, fmt, ...) \
154 dev_printk_index_wrap(_dev_err, KERN_ERR, dev, dev_fmt(fmt), ##__VA_ARGS__)
155 #define dev_warn(dev, fmt, ...) \
156 dev_printk_index_wrap(_dev_warn, KERN_WARNING, dev, dev_fmt(fmt), ##__VA_ARGS__)
157 #define dev_notice(dev, fmt, ...) \
158 dev_printk_index_wrap(_dev_notice, KERN_NOTICE, dev, dev_fmt(fmt), ##__VA_ARGS__)
159 #define dev_info(dev, fmt, ...) \
160 dev_printk_index_wrap(_dev_info, KERN_INFO, dev, dev_fmt(fmt), ##__VA_ARGS__)
161
162 #if defined(CONFIG_DYNAMIC_DEBUG) || \
163 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
164 #define dev_dbg(dev, fmt, ...) \
165 dynamic_dev_dbg(dev, dev_fmt(fmt), ##__VA_ARGS__)
166 #elif defined(DEBUG)
167 #define dev_dbg(dev, fmt, ...) \
168 dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__)
169 #else
170 #define dev_dbg(dev, fmt, ...) \
171 dev_no_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__)
172 #endif
173
174 #ifdef CONFIG_PRINTK
175 #define dev_level_once(dev_level, dev, fmt, ...) \
176 do { \
177 static bool __print_once __read_mostly; \
178 \
179 if (!__print_once) { \
180 __print_once = true; \
181 dev_level(dev, fmt, ##__VA_ARGS__); \
182 } \
183 } while (0)
184 #else
185 #define dev_level_once(dev_level, dev, fmt, ...) \
186 do { \
187 if (0) \
188 dev_level(dev, fmt, ##__VA_ARGS__); \
189 } while (0)
190 #endif
191
192 #define dev_emerg_once(dev, fmt, ...) \
193 dev_level_once(dev_emerg, dev, fmt, ##__VA_ARGS__)
194 #define dev_alert_once(dev, fmt, ...) \
195 dev_level_once(dev_alert, dev, fmt, ##__VA_ARGS__)
196 #define dev_crit_once(dev, fmt, ...) \
197 dev_level_once(dev_crit, dev, fmt, ##__VA_ARGS__)
198 #define dev_err_once(dev, fmt, ...) \
199 dev_level_once(dev_err, dev, fmt, ##__VA_ARGS__)
200 #define dev_warn_once(dev, fmt, ...) \
201 dev_level_once(dev_warn, dev, fmt, ##__VA_ARGS__)
202 #define dev_notice_once(dev, fmt, ...) \
203 dev_level_once(dev_notice, dev, fmt, ##__VA_ARGS__)
204 #define dev_info_once(dev, fmt, ...) \
205 dev_level_once(dev_info, dev, fmt, ##__VA_ARGS__)
206 #define dev_dbg_once(dev, fmt, ...) \
207 dev_level_once(dev_dbg, dev, fmt, ##__VA_ARGS__)
208
209 #define dev_level_ratelimited(dev_level, dev, fmt, ...) \
210 do { \
211 static DEFINE_RATELIMIT_STATE(_rs, \
212 DEFAULT_RATELIMIT_INTERVAL, \
213 DEFAULT_RATELIMIT_BURST); \
214 if (__ratelimit(&_rs)) \
215 dev_level(dev, fmt, ##__VA_ARGS__); \
216 } while (0)
217
218 #define dev_emerg_ratelimited(dev, fmt, ...) \
219 dev_level_ratelimited(dev_emerg, dev, fmt, ##__VA_ARGS__)
220 #define dev_alert_ratelimited(dev, fmt, ...) \
221 dev_level_ratelimited(dev_alert, dev, fmt, ##__VA_ARGS__)
222 #define dev_crit_ratelimited(dev, fmt, ...) \
223 dev_level_ratelimited(dev_crit, dev, fmt, ##__VA_ARGS__)
224 #define dev_err_ratelimited(dev, fmt, ...) \
225 dev_level_ratelimited(dev_err, dev, fmt, ##__VA_ARGS__)
226 #define dev_warn_ratelimited(dev, fmt, ...) \
227 dev_level_ratelimited(dev_warn, dev, fmt, ##__VA_ARGS__)
228 #define dev_notice_ratelimited(dev, fmt, ...) \
229 dev_level_ratelimited(dev_notice, dev, fmt, ##__VA_ARGS__)
230 #define dev_info_ratelimited(dev, fmt, ...) \
231 dev_level_ratelimited(dev_info, dev, fmt, ##__VA_ARGS__)
232 #if defined(CONFIG_DYNAMIC_DEBUG) || \
233 (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
234 /* descriptor check is first to prevent flooding with "callbacks suppressed" */
235 #define dev_dbg_ratelimited(dev, fmt, ...) \
236 do { \
237 static DEFINE_RATELIMIT_STATE(_rs, \
238 DEFAULT_RATELIMIT_INTERVAL, \
239 DEFAULT_RATELIMIT_BURST); \
240 DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \
241 if (DYNAMIC_DEBUG_BRANCH(descriptor) && \
242 __ratelimit(&_rs)) \
243 __dynamic_dev_dbg(&descriptor, dev, dev_fmt(fmt), \
244 ##__VA_ARGS__); \
245 } while (0)
246 #elif defined(DEBUG)
247 #define dev_dbg_ratelimited(dev, fmt, ...) \
248 do { \
249 static DEFINE_RATELIMIT_STATE(_rs, \
250 DEFAULT_RATELIMIT_INTERVAL, \
251 DEFAULT_RATELIMIT_BURST); \
252 if (__ratelimit(&_rs)) \
253 dev_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__); \
254 } while (0)
255 #else
256 #define dev_dbg_ratelimited(dev, fmt, ...) \
257 dev_no_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__)
258 #endif
259
260 #ifdef VERBOSE_DEBUG
261 #define dev_vdbg dev_dbg
262 #else
263 #define dev_vdbg(dev, fmt, ...) \
264 dev_no_printk(KERN_DEBUG, dev, dev_fmt(fmt), ##__VA_ARGS__)
265 #endif
266
267 /*
268 * dev_WARN*() acts like dev_printk(), but with the key difference of
269 * using WARN/WARN_ONCE to include file/line information and a backtrace.
270 */
271 #define dev_WARN(dev, format, arg...) \
272 WARN(1, "%s %s: " format, dev_driver_string(dev), dev_name(dev), ## arg)
273
274 #define dev_WARN_ONCE(dev, condition, format, arg...) \
275 WARN_ONCE(condition, "%s %s: " format, \
276 dev_driver_string(dev), dev_name(dev), ## arg)
277
278 __printf(3, 4) int dev_err_probe(const struct device *dev, int err, const char *fmt, ...);
279
280 /* Simple helper for dev_err_probe() when ERR_PTR() is to be returned. */
281 #define dev_err_ptr_probe(dev, ___err, fmt, ...) \
282 ERR_PTR(dev_err_probe(dev, ___err, fmt, ##__VA_ARGS__))
283
284 /* Simple helper for dev_err_probe() when ERR_CAST() is to be returned. */
285 #define dev_err_cast_probe(dev, ___err_ptr, fmt, ...) \
286 ERR_PTR(dev_err_probe(dev, PTR_ERR(___err_ptr), fmt, ##__VA_ARGS__))
287
288 #endif /* _DEVICE_PRINTK_H_ */
289