2 * Copyright (C) 2016 Red Hat
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
23 * Rob Clark <robdclark@gmail.com>
26 #define DEBUG /* for pr_debug() */
29 #include <linux/seq_file.h>
31 #include <drm/drm_print.h>
33 void __drm_puts_coredump(struct drm_printer
*p
, const char *str
)
35 struct drm_print_iterator
*iterator
= p
->arg
;
38 if (!iterator
->remain
)
41 if (iterator
->offset
< iterator
->start
) {
46 if (iterator
->offset
+ len
<= iterator
->start
) {
47 iterator
->offset
+= len
;
51 copy
= len
- (iterator
->start
- iterator
->offset
);
53 if (copy
> iterator
->remain
)
54 copy
= iterator
->remain
;
56 /* Copy out the bit of the string that we need */
57 memcpy(iterator
->data
,
58 str
+ (iterator
->start
- iterator
->offset
), copy
);
60 iterator
->offset
= iterator
->start
+ copy
;
61 iterator
->remain
-= copy
;
63 ssize_t pos
= iterator
->offset
- iterator
->start
;
65 len
= min_t(ssize_t
, strlen(str
), iterator
->remain
);
67 memcpy(iterator
->data
+ pos
, str
, len
);
69 iterator
->offset
+= len
;
70 iterator
->remain
-= len
;
73 EXPORT_SYMBOL(__drm_puts_coredump
);
75 void __drm_printfn_coredump(struct drm_printer
*p
, struct va_format
*vaf
)
77 struct drm_print_iterator
*iterator
= p
->arg
;
81 if (!iterator
->remain
)
84 /* Figure out how big the string will be */
85 len
= snprintf(NULL
, 0, "%pV", vaf
);
87 /* This is the easiest path, we've already advanced beyond the offset */
88 if (iterator
->offset
+ len
<= iterator
->start
) {
89 iterator
->offset
+= len
;
93 /* Then check if we can directly copy into the target buffer */
94 if ((iterator
->offset
>= iterator
->start
) && (len
< iterator
->remain
)) {
95 ssize_t pos
= iterator
->offset
- iterator
->start
;
97 snprintf(((char *) iterator
->data
) + pos
,
98 iterator
->remain
, "%pV", vaf
);
100 iterator
->offset
+= len
;
101 iterator
->remain
-= len
;
107 * Finally, hit the slow path and make a temporary string to copy over
108 * using _drm_puts_coredump
110 buf
= kmalloc(len
+ 1, GFP_KERNEL
| __GFP_NOWARN
| __GFP_NORETRY
);
114 snprintf(buf
, len
+ 1, "%pV", vaf
);
115 __drm_puts_coredump(p
, (const char *) buf
);
119 EXPORT_SYMBOL(__drm_printfn_coredump
);
121 void __drm_puts_seq_file(struct drm_printer
*p
, const char *str
)
123 seq_puts(p
->arg
, str
);
125 EXPORT_SYMBOL(__drm_puts_seq_file
);
127 void __drm_printfn_seq_file(struct drm_printer
*p
, struct va_format
*vaf
)
129 seq_printf(p
->arg
, "%pV", vaf
);
131 EXPORT_SYMBOL(__drm_printfn_seq_file
);
133 void __drm_printfn_info(struct drm_printer
*p
, struct va_format
*vaf
)
135 dev_info(p
->arg
, "[" DRM_NAME
"] %pV", vaf
);
137 EXPORT_SYMBOL(__drm_printfn_info
);
139 void __drm_printfn_debug(struct drm_printer
*p
, struct va_format
*vaf
)
141 pr_debug("%s %pV", p
->prefix
, vaf
);
143 EXPORT_SYMBOL(__drm_printfn_debug
);
146 * drm_puts - print a const string to a &drm_printer stream
147 * @p: the &drm printer
150 * Allow &drm_printer types that have a constant string
153 void drm_puts(struct drm_printer
*p
, const char *str
)
158 drm_printf(p
, "%s", str
);
160 EXPORT_SYMBOL(drm_puts
);
163 * drm_printf - print to a &drm_printer stream
164 * @p: the &drm_printer
167 void drm_printf(struct drm_printer
*p
, const char *f
, ...)
172 drm_vprintf(p
, f
, &args
);
175 EXPORT_SYMBOL(drm_printf
);
177 void drm_dev_printk(const struct device
*dev
, const char *level
,
178 const char *format
, ...)
180 struct va_format vaf
;
183 va_start(args
, format
);
188 dev_printk(level
, dev
, "[" DRM_NAME
":%ps] %pV",
189 __builtin_return_address(0), &vaf
);
191 printk("%s" "[" DRM_NAME
":%ps] %pV",
192 level
, __builtin_return_address(0), &vaf
);
196 EXPORT_SYMBOL(drm_dev_printk
);
198 void drm_dev_dbg(const struct device
*dev
, unsigned int category
,
199 const char *format
, ...)
201 struct va_format vaf
;
204 if (!(drm_debug
& category
))
207 va_start(args
, format
);
212 dev_printk(KERN_DEBUG
, dev
, "[" DRM_NAME
":%ps] %pV",
213 __builtin_return_address(0), &vaf
);
215 printk(KERN_DEBUG
"[" DRM_NAME
":%ps] %pV",
216 __builtin_return_address(0), &vaf
);
220 EXPORT_SYMBOL(drm_dev_dbg
);
222 void drm_dbg(unsigned int category
, const char *format
, ...)
224 struct va_format vaf
;
227 if (!(drm_debug
& category
))
230 va_start(args
, format
);
234 printk(KERN_DEBUG
"[" DRM_NAME
":%ps] %pV",
235 __builtin_return_address(0), &vaf
);
239 EXPORT_SYMBOL(drm_dbg
);
241 void drm_err(const char *format
, ...)
243 struct va_format vaf
;
246 va_start(args
, format
);
250 printk(KERN_ERR
"[" DRM_NAME
":%ps] *ERROR* %pV",
251 __builtin_return_address(0), &vaf
);
255 EXPORT_SYMBOL(drm_err
);