Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2016 Red Hat |
| 3 | * |
| 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: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | * |
| 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. |
| 21 | * |
| 22 | * Authors: |
| 23 | * Rob Clark <robdclark@gmail.com> |
| 24 | */ |
| 25 | |
| 26 | #define DEBUG /* for pr_debug() */ |
| 27 | |
| 28 | #include <stdarg.h> |
| 29 | #include <linux/seq_file.h> |
| 30 | #include <drm/drmP.h> |
| 31 | #include <drm/drm_print.h> |
| 32 | |
| 33 | void __drm_puts_coredump(struct drm_printer *p, const char *str) |
| 34 | { |
| 35 | struct drm_print_iterator *iterator = p->arg; |
| 36 | ssize_t len; |
| 37 | |
| 38 | if (!iterator->remain) |
| 39 | return; |
| 40 | |
| 41 | if (iterator->offset < iterator->start) { |
| 42 | ssize_t copy; |
| 43 | |
| 44 | len = strlen(str); |
| 45 | |
| 46 | if (iterator->offset + len <= iterator->start) { |
| 47 | iterator->offset += len; |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | copy = len - (iterator->start - iterator->offset); |
| 52 | |
| 53 | if (copy > iterator->remain) |
| 54 | copy = iterator->remain; |
| 55 | |
| 56 | /* Copy out the bit of the string that we need */ |
| 57 | memcpy(iterator->data, |
| 58 | str + (iterator->start - iterator->offset), copy); |
| 59 | |
| 60 | iterator->offset = iterator->start + copy; |
| 61 | iterator->remain -= copy; |
| 62 | } else { |
| 63 | ssize_t pos = iterator->offset - iterator->start; |
| 64 | |
| 65 | len = min_t(ssize_t, strlen(str), iterator->remain); |
| 66 | |
| 67 | memcpy(iterator->data + pos, str, len); |
| 68 | |
| 69 | iterator->offset += len; |
| 70 | iterator->remain -= len; |
| 71 | } |
| 72 | } |
| 73 | EXPORT_SYMBOL(__drm_puts_coredump); |
| 74 | |
| 75 | void __drm_printfn_coredump(struct drm_printer *p, struct va_format *vaf) |
| 76 | { |
| 77 | struct drm_print_iterator *iterator = p->arg; |
| 78 | size_t len; |
| 79 | char *buf; |
| 80 | |
| 81 | if (!iterator->remain) |
| 82 | return; |
| 83 | |
| 84 | /* Figure out how big the string will be */ |
| 85 | len = snprintf(NULL, 0, "%pV", vaf); |
| 86 | |
| 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; |
| 91 | } |
| 92 | |
| 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; |
| 96 | |
| 97 | snprintf(((char *) iterator->data) + pos, |
| 98 | iterator->remain, "%pV", vaf); |
| 99 | |
| 100 | iterator->offset += len; |
| 101 | iterator->remain -= len; |
| 102 | |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * Finally, hit the slow path and make a temporary string to copy over |
| 108 | * using _drm_puts_coredump |
| 109 | */ |
| 110 | buf = kmalloc(len + 1, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY); |
| 111 | if (!buf) |
| 112 | return; |
| 113 | |
| 114 | snprintf(buf, len + 1, "%pV", vaf); |
| 115 | __drm_puts_coredump(p, (const char *) buf); |
| 116 | |
| 117 | kfree(buf); |
| 118 | } |
| 119 | EXPORT_SYMBOL(__drm_printfn_coredump); |
| 120 | |
| 121 | void __drm_puts_seq_file(struct drm_printer *p, const char *str) |
| 122 | { |
| 123 | seq_puts(p->arg, str); |
| 124 | } |
| 125 | EXPORT_SYMBOL(__drm_puts_seq_file); |
| 126 | |
| 127 | void __drm_printfn_seq_file(struct drm_printer *p, struct va_format *vaf) |
| 128 | { |
| 129 | seq_printf(p->arg, "%pV", vaf); |
| 130 | } |
| 131 | EXPORT_SYMBOL(__drm_printfn_seq_file); |
| 132 | |
| 133 | void __drm_printfn_info(struct drm_printer *p, struct va_format *vaf) |
| 134 | { |
| 135 | dev_info(p->arg, "[" DRM_NAME "] %pV", vaf); |
| 136 | } |
| 137 | EXPORT_SYMBOL(__drm_printfn_info); |
| 138 | |
| 139 | void __drm_printfn_debug(struct drm_printer *p, struct va_format *vaf) |
| 140 | { |
| 141 | pr_debug("%s %pV", p->prefix, vaf); |
| 142 | } |
| 143 | EXPORT_SYMBOL(__drm_printfn_debug); |
| 144 | |
| 145 | /** |
| 146 | * drm_puts - print a const string to a &drm_printer stream |
| 147 | * @p: the &drm printer |
| 148 | * @str: const string |
| 149 | * |
| 150 | * Allow &drm_printer types that have a constant string |
| 151 | * option to use it. |
| 152 | */ |
| 153 | void drm_puts(struct drm_printer *p, const char *str) |
| 154 | { |
| 155 | if (p->puts) |
| 156 | p->puts(p, str); |
| 157 | else |
| 158 | drm_printf(p, "%s", str); |
| 159 | } |
| 160 | EXPORT_SYMBOL(drm_puts); |
| 161 | |
| 162 | /** |
| 163 | * drm_printf - print to a &drm_printer stream |
| 164 | * @p: the &drm_printer |
| 165 | * @f: format string |
| 166 | */ |
| 167 | void drm_printf(struct drm_printer *p, const char *f, ...) |
| 168 | { |
| 169 | va_list args; |
| 170 | |
| 171 | va_start(args, f); |
| 172 | drm_vprintf(p, f, &args); |
| 173 | va_end(args); |
| 174 | } |
| 175 | EXPORT_SYMBOL(drm_printf); |
| 176 | |
| 177 | void drm_dev_printk(const struct device *dev, const char *level, |
| 178 | const char *format, ...) |
| 179 | { |
| 180 | struct va_format vaf; |
| 181 | va_list args; |
| 182 | |
| 183 | va_start(args, format); |
| 184 | vaf.fmt = format; |
| 185 | vaf.va = &args; |
| 186 | |
| 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); |
| 193 | |
| 194 | va_end(args); |
| 195 | } |
| 196 | EXPORT_SYMBOL(drm_dev_printk); |
| 197 | |
| 198 | void drm_dev_dbg(const struct device *dev, unsigned int category, |
| 199 | const char *format, ...) |
| 200 | { |
| 201 | struct va_format vaf; |
| 202 | va_list args; |
| 203 | |
| 204 | if (!(drm_debug & category)) |
| 205 | return; |
| 206 | |
| 207 | va_start(args, format); |
| 208 | vaf.fmt = format; |
| 209 | vaf.va = &args; |
| 210 | |
| 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); |
| 217 | |
| 218 | va_end(args); |
| 219 | } |
| 220 | EXPORT_SYMBOL(drm_dev_dbg); |
| 221 | |
| 222 | void drm_dbg(unsigned int category, const char *format, ...) |
| 223 | { |
| 224 | struct va_format vaf; |
| 225 | va_list args; |
| 226 | |
| 227 | if (!(drm_debug & category)) |
| 228 | return; |
| 229 | |
| 230 | va_start(args, format); |
| 231 | vaf.fmt = format; |
| 232 | vaf.va = &args; |
| 233 | |
| 234 | printk(KERN_DEBUG "[" DRM_NAME ":%ps] %pV", |
| 235 | __builtin_return_address(0), &vaf); |
| 236 | |
| 237 | va_end(args); |
| 238 | } |
| 239 | EXPORT_SYMBOL(drm_dbg); |
| 240 | |
| 241 | void drm_err(const char *format, ...) |
| 242 | { |
| 243 | struct va_format vaf; |
| 244 | va_list args; |
| 245 | |
| 246 | va_start(args, format); |
| 247 | vaf.fmt = format; |
| 248 | vaf.va = &args; |
| 249 | |
| 250 | printk(KERN_ERR "[" DRM_NAME ":%ps] *ERROR* %pV", |
| 251 | __builtin_return_address(0), &vaf); |
| 252 | |
| 253 | va_end(args); |
| 254 | } |
| 255 | EXPORT_SYMBOL(drm_err); |