vt: vt_ioctl: fix VT_DISALLOCATE freeing in-use virtual console
[linux/fpc-iii.git] / drivers / gpu / drm / drm_print.c
blob0e7fc3e7dfb48878671cbf709063e1489485186b
1 /*
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.
22 * Authors:
23 * Rob Clark <robdclark@gmail.com>
26 #define DEBUG /* for pr_debug() */
28 #include <stdarg.h>
29 #include <linux/seq_file.h>
30 #include <drm/drmP.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;
36 ssize_t len;
38 if (!iterator->remain)
39 return;
41 if (iterator->offset < iterator->start) {
42 ssize_t copy;
44 len = strlen(str);
46 if (iterator->offset + len <= iterator->start) {
47 iterator->offset += len;
48 return;
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;
62 } else {
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;
78 size_t len;
79 char *buf;
81 if (!iterator->remain)
82 return;
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;
90 return;
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;
103 return;
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);
111 if (!buf)
112 return;
114 snprintf(buf, len + 1, "%pV", vaf);
115 __drm_puts_coredump(p, (const char *) buf);
117 kfree(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
148 * @str: const string
150 * Allow &drm_printer types that have a constant string
151 * option to use it.
153 void drm_puts(struct drm_printer *p, const char *str)
155 if (p->puts)
156 p->puts(p, str);
157 else
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
165 * @f: format string
167 void drm_printf(struct drm_printer *p, const char *f, ...)
169 va_list args;
171 va_start(args, f);
172 drm_vprintf(p, f, &args);
173 va_end(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;
181 va_list args;
183 va_start(args, format);
184 vaf.fmt = format;
185 vaf.va = &args;
187 if (dev)
188 dev_printk(level, dev, "[" DRM_NAME ":%ps] %pV",
189 __builtin_return_address(0), &vaf);
190 else
191 printk("%s" "[" DRM_NAME ":%ps] %pV",
192 level, __builtin_return_address(0), &vaf);
194 va_end(args);
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;
202 va_list args;
204 if (!(drm_debug & category))
205 return;
207 va_start(args, format);
208 vaf.fmt = format;
209 vaf.va = &args;
211 if (dev)
212 dev_printk(KERN_DEBUG, dev, "[" DRM_NAME ":%ps] %pV",
213 __builtin_return_address(0), &vaf);
214 else
215 printk(KERN_DEBUG "[" DRM_NAME ":%ps] %pV",
216 __builtin_return_address(0), &vaf);
218 va_end(args);
220 EXPORT_SYMBOL(drm_dev_dbg);
222 void drm_dbg(unsigned int category, const char *format, ...)
224 struct va_format vaf;
225 va_list args;
227 if (!(drm_debug & category))
228 return;
230 va_start(args, format);
231 vaf.fmt = format;
232 vaf.va = &args;
234 printk(KERN_DEBUG "[" DRM_NAME ":%ps] %pV",
235 __builtin_return_address(0), &vaf);
237 va_end(args);
239 EXPORT_SYMBOL(drm_dbg);
241 void drm_err(const char *format, ...)
243 struct va_format vaf;
244 va_list args;
246 va_start(args, format);
247 vaf.fmt = format;
248 vaf.va = &args;
250 printk(KERN_ERR "[" DRM_NAME ":%ps] *ERROR* %pV",
251 __builtin_return_address(0), &vaf);
253 va_end(args);
255 EXPORT_SYMBOL(drm_err);