/src/barebox/common/console.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-or-later |
2 | | /* |
3 | | * (C) Copyright 2000 |
4 | | * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it |
5 | | */ |
6 | | |
7 | | #include <security/config.h> |
8 | | #include <common.h> |
9 | | #include <stdarg.h> |
10 | | #include <malloc.h> |
11 | | #include <param.h> |
12 | | #include <console.h> |
13 | | #include <term.h> |
14 | | #include <driver.h> |
15 | | #include <fs.h> |
16 | | #include <of.h> |
17 | | #include <init.h> |
18 | | #include <clock.h> |
19 | | #include <kfifo.h> |
20 | | #include <module.h> |
21 | | #include <sched.h> |
22 | | #include <ratp_bb.h> |
23 | | #include <readkey.h> |
24 | | #include <magicvar.h> |
25 | | #include <globalvar.h> |
26 | | #include <linux/list.h> |
27 | | #include <linux/stringify.h> |
28 | | #include <debug_ll.h> |
29 | | #include <security/config.h> |
30 | | |
31 | | LIST_HEAD(console_list); |
32 | | EXPORT_SYMBOL(console_list); |
33 | | |
34 | 32 | #define CONSOLE_UNINITIALIZED 0 |
35 | 32 | #define CONSOLE_INITIALIZED_BUFFER 1 |
36 | 134 | #define CONSOLE_INIT_FULL 2 |
37 | | |
38 | | #define to_console_dev(d) container_of(d, struct console_device, class_dev) |
39 | | |
40 | | static int initialized = 0; |
41 | | |
42 | 64 | #define CONSOLE_BUFFER_SIZE 1024 |
43 | | |
44 | | static char console_input_buffer[CONSOLE_BUFFER_SIZE]; |
45 | | static char console_output_buffer[CONSOLE_BUFFER_SIZE]; |
46 | | |
47 | | static struct kfifo __console_input_fifo; |
48 | | static struct kfifo __console_output_fifo; |
49 | | static struct kfifo *console_input_fifo = &__console_input_fifo; |
50 | | static struct kfifo *console_output_fifo = &__console_output_fifo; |
51 | | |
52 | | int console_open(struct console_device *cdev, unsigned activate) |
53 | 32 | { |
54 | 32 | int ret; |
55 | | |
56 | 32 | if (cdev->open && !cdev->open_count) { |
57 | 0 | ret = cdev->open(cdev, activate); |
58 | 0 | if (ret) |
59 | 0 | return ret; |
60 | 0 | } |
61 | | |
62 | 32 | cdev->open_count++; |
63 | | |
64 | 32 | return 0; |
65 | 32 | } |
66 | | |
67 | | int console_close(struct console_device *cdev) |
68 | 0 | { |
69 | 0 | int ret; |
70 | |
|
71 | 0 | if (!cdev->open_count) |
72 | 0 | return -EBADFD; |
73 | | |
74 | 0 | cdev->open_count--; |
75 | |
|
76 | 0 | if (cdev->close && !cdev->open_count) { |
77 | 0 | ret = cdev->close(cdev); |
78 | 0 | if (ret) |
79 | 0 | return ret; |
80 | 0 | } |
81 | | |
82 | 0 | return 0; |
83 | 0 | } |
84 | | |
85 | | int console_set_active(struct console_device *cdev, unsigned flag) |
86 | 32 | { |
87 | 32 | unsigned flag_new = flag & ~cdev->f_active; |
88 | 32 | int ret; |
89 | | |
90 | 32 | if (!cdev->getc) |
91 | 0 | flag &= ~CONSOLE_STDIN; |
92 | 32 | if (!cdev->putc) |
93 | 0 | flag &= ~(CONSOLE_STDOUT | CONSOLE_STDERR); |
94 | | |
95 | 32 | if (!flag && cdev->f_active && cdev->flush) |
96 | 0 | cdev->flush(cdev); |
97 | | |
98 | 32 | if (flag == cdev->f_active) |
99 | 0 | return 0; |
100 | | |
101 | 32 | if (!flag) { |
102 | 0 | ret = console_close(cdev); |
103 | 0 | if (ret) |
104 | 0 | return ret; |
105 | 32 | } else { |
106 | 32 | ret = console_open(cdev, flag); |
107 | 32 | if (ret) |
108 | 0 | return ret; |
109 | 32 | } |
110 | | |
111 | 32 | cdev->f_active = flag; |
112 | | |
113 | 32 | if (initialized < CONSOLE_INIT_FULL) { |
114 | 32 | char ch; |
115 | 32 | initialized = CONSOLE_INIT_FULL; |
116 | 32 | puts_ll("Switch to console ["); |
117 | 32 | puts_ll(dev_name(&cdev->class_dev)); |
118 | 32 | puts_ll("]\n"); |
119 | 32 | barebox_banner(); |
120 | 32 | while (kfifo_getc(console_output_fifo, &ch) == 0) |
121 | 0 | console_putc(CONSOLE_DEV_STDOUT, ch); |
122 | 32 | } else if (IS_ENABLED(CONFIG_BANNER) && cdev->puts && |
123 | 0 | flag_new == CONSOLE_STDIOE) { |
124 | 0 | cdev->puts(cdev, version_string, strlen(version_string)); |
125 | 0 | cdev->puts(cdev, "\n\n", 2); |
126 | 0 | } |
127 | | |
128 | 32 | return 0; |
129 | 32 | } |
130 | | |
131 | | unsigned console_get_active(struct console_device *cdev) |
132 | 0 | { |
133 | 0 | return cdev->f_active; |
134 | 0 | } |
135 | | |
136 | | static int console_active_set(struct param_d *param, void *priv) |
137 | 0 | { |
138 | 0 | struct console_device *cdev = priv; |
139 | 0 | unsigned int flag = 0; |
140 | 0 | int ret; |
141 | |
|
142 | 0 | if (cdev->active_string) { |
143 | 0 | if (strchr(cdev->active_string, 'i')) |
144 | 0 | flag |= CONSOLE_STDIN; |
145 | 0 | if (strchr(cdev->active_string, 'o')) |
146 | 0 | flag |= CONSOLE_STDOUT; |
147 | 0 | if (strchr(cdev->active_string, 'e')) |
148 | 0 | flag |= CONSOLE_STDERR; |
149 | 0 | } |
150 | |
|
151 | 0 | ret = console_set_active(cdev, flag); |
152 | 0 | if (ret) |
153 | 0 | return ret; |
154 | | |
155 | 0 | return 0; |
156 | 0 | } |
157 | | |
158 | | static int console_active_get(struct param_d *param, void *priv) |
159 | 0 | { |
160 | 0 | struct console_device *cdev = priv; |
161 | 0 | unsigned int flag = cdev->f_active; |
162 | |
|
163 | 0 | free(cdev->active_string); |
164 | 0 | cdev->active_string = basprintf("%s%s%s", |
165 | 0 | flag & CONSOLE_STDIN ? "i" : "", |
166 | 0 | flag & CONSOLE_STDOUT ? "o" : "", |
167 | 0 | flag & CONSOLE_STDERR ? "e" : ""); |
168 | 0 | return 0; |
169 | 0 | } |
170 | | |
171 | | int console_set_baudrate(struct console_device *cdev, unsigned baudrate) |
172 | 0 | { |
173 | 0 | int ret; |
174 | 0 | unsigned char c; |
175 | |
|
176 | 0 | if (cdev->baudrate == baudrate) |
177 | 0 | return 0; |
178 | | |
179 | 0 | if (!cdev->setbrg) |
180 | 0 | return -ENOSYS; |
181 | | |
182 | | /* |
183 | | * If the device is already active, change its baudrate. |
184 | | * The baudrate of an inactive device will be set at activation time. |
185 | | */ |
186 | 0 | if (cdev->f_active) { |
187 | 0 | printf("## Switch baudrate on console %s to %d bps and press ENTER ...\n", |
188 | 0 | dev_name(&cdev->class_dev), baudrate); |
189 | 0 | mdelay(50); |
190 | 0 | } |
191 | |
|
192 | 0 | ret = cdev->setbrg(cdev, baudrate); |
193 | 0 | if (ret) |
194 | 0 | return ret; |
195 | | |
196 | 0 | if (cdev->f_active) { |
197 | 0 | mdelay(50); |
198 | 0 | do { |
199 | 0 | c = getchar(); |
200 | 0 | } while (c != '\r' && c != '\n'); |
201 | 0 | } |
202 | |
|
203 | 0 | cdev->baudrate = baudrate; |
204 | 0 | cdev->baudrate_param = baudrate; |
205 | |
|
206 | 0 | return 0; |
207 | 0 | } |
208 | | |
209 | | unsigned console_get_baudrate(struct console_device *cdev) |
210 | 0 | { |
211 | 0 | return cdev->baudrate; |
212 | 0 | } |
213 | | |
214 | | static int console_baudrate_set(struct param_d *param, void *priv) |
215 | 0 | { |
216 | 0 | struct console_device *cdev = priv; |
217 | |
|
218 | 0 | return console_set_baudrate(cdev, cdev->baudrate_param); |
219 | 0 | } |
220 | | |
221 | | static int console_size_get(struct param_d *param, void *priv) |
222 | 0 | { |
223 | 0 | struct console_device *cdev = priv; |
224 | 0 | int rows = 0, cols = 0; |
225 | | |
226 | | /* Parameter will just report 0x0 on errors) */ |
227 | 0 | (void)term_cdev_get_size(cdev, &cols, &rows); |
228 | |
|
229 | 0 | free(cdev->term_size); |
230 | 0 | cdev->term_size = xasprintf("%ux%u", cols, rows); |
231 | |
|
232 | 0 | return 0; |
233 | 0 | } |
234 | | |
235 | | static void console_init_early(void) |
236 | 32 | { |
237 | 32 | kfifo_init(console_input_fifo, console_input_buffer, |
238 | 32 | CONSOLE_BUFFER_SIZE); |
239 | 32 | kfifo_init(console_output_fifo, console_output_buffer, |
240 | 32 | CONSOLE_BUFFER_SIZE); |
241 | | |
242 | 32 | initialized = CONSOLE_INITIALIZED_BUFFER; |
243 | 32 | } |
244 | | |
245 | | static void console_add_earlycon_param(struct console_device *cdev, unsigned baudrate) |
246 | 32 | { |
247 | 32 | if (!cdev->linux_earlycon_name) |
248 | 32 | return; |
249 | | |
250 | 0 | dev_add_param_fixed(&cdev->class_dev, "linux.bootargs.earlycon", |
251 | 0 | "earlycon=%s,0x%lx", cdev->linux_earlycon_name, |
252 | 0 | (ulong)cdev->phys_base); |
253 | 0 | } |
254 | | |
255 | | void console_set_stdoutpath(struct console_device *cdev, unsigned baudrate) |
256 | 0 | { |
257 | 0 | int id; |
258 | 0 | char *str; |
259 | |
|
260 | 0 | if (!cdev->linux_console_name) |
261 | 0 | return; |
262 | | |
263 | 0 | id = of_alias_get_id(cdev->dev->of_node, "serial"); |
264 | 0 | if (id < 0) |
265 | 0 | return; |
266 | | |
267 | 0 | str = basprintf("console=%s%d,%dn8", cdev->linux_console_name, id, baudrate); |
268 | |
|
269 | 0 | globalvar_add_simple("linux.bootargs.console", str); |
270 | |
|
271 | 0 | free(str); |
272 | 0 | } |
273 | | |
274 | | struct console_device *of_console_by_stdout_path(void) |
275 | 0 | { |
276 | 0 | struct device_node *stdout_np; |
277 | |
|
278 | 0 | stdout_np = of_get_stdoutpath(NULL); |
279 | 0 | if (!stdout_np) |
280 | 0 | return NULL; |
281 | | |
282 | 0 | for_each_console(console) { |
283 | 0 | if (console->dev && dev_of_node(console->dev) == stdout_np) |
284 | 0 | return console; |
285 | 0 | } |
286 | | |
287 | 0 | return NULL; |
288 | 0 | } |
289 | | |
290 | | static int __console_puts(struct console_device *cdev, const char *s, |
291 | | size_t nbytes) |
292 | 70 | { |
293 | 70 | size_t i; |
294 | | |
295 | 412 | for (i = 0; i < nbytes; i++) { |
296 | 342 | if (*s == '\n') { |
297 | 134 | cdev->putc(cdev, '\r'); |
298 | 134 | if (IS_ENABLED(CONFIG_CONSOLE_FLUSH_LINE_BREAK) && cdev->flush) |
299 | 0 | cdev->flush(cdev); |
300 | 134 | } |
301 | | |
302 | 342 | cdev->putc(cdev, *s); |
303 | 342 | s++; |
304 | 342 | } |
305 | 70 | return i; |
306 | 70 | } |
307 | | |
308 | | static int fops_open(struct cdev *cdev, unsigned long flags) |
309 | 0 | { |
310 | 0 | struct console_device *priv = cdev->priv; |
311 | |
|
312 | 0 | if ((flags & (O_WRONLY | O_RDWR)) && !priv->puts ) |
313 | 0 | return -EPERM; |
314 | | |
315 | 0 | return console_open(priv, 0); |
316 | 0 | } |
317 | | |
318 | | static int fops_close(struct cdev *dev) |
319 | 0 | { |
320 | 0 | struct console_device *priv = dev->priv; |
321 | |
|
322 | 0 | return console_close(priv); |
323 | 0 | } |
324 | | |
325 | | static int fops_flush(struct cdev *dev) |
326 | 0 | { |
327 | 0 | struct console_device *priv = dev->priv; |
328 | |
|
329 | 0 | if (priv->flush) |
330 | 0 | priv->flush(priv); |
331 | |
|
332 | 0 | return 0; |
333 | 0 | } |
334 | | |
335 | | static ssize_t fops_write(struct cdev* dev, const void* buf, size_t count, |
336 | | loff_t offset, ulong flags) |
337 | 0 | { |
338 | 0 | struct console_device *priv = dev->priv; |
339 | |
|
340 | 0 | priv->puts(priv, buf, count); |
341 | |
|
342 | 0 | return count; |
343 | 0 | } |
344 | | |
345 | | int console_register(struct console_device *newcdev) |
346 | 32 | { |
347 | 32 | struct device_node *serdev_node = console_is_serdev_node(newcdev); |
348 | 32 | struct device *dev = &newcdev->class_dev; |
349 | 32 | int activate = 0, ret; |
350 | 32 | unsigned of_baudrate = 0, baudrate = CONFIG_BAUDRATE; |
351 | | |
352 | 32 | if (!serdev_node && initialized == CONSOLE_UNINITIALIZED) |
353 | 32 | console_init_early(); |
354 | | |
355 | 32 | if (newcdev->devname) { |
356 | 0 | dev->id = newcdev->devid; |
357 | 0 | dev_set_name(dev, "%s", newcdev->devname); |
358 | 32 | } else { |
359 | 32 | dev->id = DEVICE_ID_DYNAMIC; |
360 | 32 | dev_set_name(dev, "cs"); |
361 | 32 | } |
362 | | |
363 | 32 | if (newcdev->dev) |
364 | 32 | dev->parent = newcdev->dev; |
365 | 32 | platform_device_register(dev); |
366 | | |
367 | 32 | if (!newcdev->devname) |
368 | 32 | newcdev->devname = xstrdup(dev_name(dev)); |
369 | | |
370 | 32 | newcdev->open_count = 0; |
371 | | |
372 | | /* |
373 | | * If our console device is a serdev, we skip the creation of |
374 | | * corresponding entry in /dev as well as registration in |
375 | | * console_list and just go straight to populating child |
376 | | * devices. |
377 | | */ |
378 | 32 | if (serdev_node) |
379 | 0 | return of_platform_populate(serdev_node, NULL, dev); |
380 | | |
381 | 32 | if (newcdev->dev && of_device_is_stdout_path(newcdev->dev, &baudrate)) { |
382 | 0 | activate = CONSOLE_STDIOE; |
383 | 0 | console_set_stdoutpath(newcdev, baudrate); |
384 | 0 | } |
385 | | |
386 | | /* Honour the previous baudrate if it is set to a non-zero value */ |
387 | 32 | of_property_read_u32(dev->of_node, "current-speed", &of_baudrate); |
388 | 32 | if (of_baudrate) |
389 | 0 | baudrate = of_baudrate; |
390 | | |
391 | 32 | console_add_earlycon_param(newcdev, baudrate); |
392 | | |
393 | 32 | if (newcdev->setbrg) { |
394 | 0 | ret = newcdev->setbrg(newcdev, baudrate); |
395 | 0 | if (ret) |
396 | 0 | return ret; |
397 | 0 | newcdev->baudrate_param = baudrate; |
398 | 0 | dev_add_param_uint32(dev, "baudrate", console_baudrate_set, |
399 | 0 | NULL, &newcdev->baudrate_param, "%u", newcdev); |
400 | 0 | } |
401 | | |
402 | 32 | newcdev->baudrate = baudrate; |
403 | | |
404 | 32 | if (newcdev->putc && !newcdev->puts) |
405 | 32 | newcdev->puts = __console_puts; |
406 | | |
407 | 32 | dev_add_param_string(dev, "active", console_active_set, console_active_get, |
408 | 32 | &newcdev->active_string, newcdev); |
409 | | |
410 | 32 | dev_add_param_string(dev, "terminal.size", NULL, console_size_get, |
411 | 32 | &newcdev->term_size, newcdev); |
412 | | |
413 | 32 | if (IS_ENABLED(CONFIG_CONSOLE_ACTIVATE_FIRST)) { |
414 | 0 | if (list_empty(&console_list)) |
415 | 0 | activate = CONSOLE_STDIOE; |
416 | 32 | } else if (IS_ENABLED(CONFIG_CONSOLE_ACTIVATE_ALL)) { |
417 | 32 | activate = CONSOLE_STDIOE; |
418 | 32 | } |
419 | | |
420 | 32 | list_add_tail(&newcdev->list, &console_list); |
421 | | |
422 | 32 | if (IS_ENABLED(CONFIG_CONSOLE_DISABLE_INPUT)) |
423 | 0 | activate &= ~CONSOLE_STDIN; |
424 | | |
425 | 32 | if (activate) |
426 | 32 | console_set_active(newcdev, activate); |
427 | | |
428 | | /* expose console as device in fs */ |
429 | 32 | newcdev->devfs.name = basprintf("%s%d", newcdev->class_dev.name, |
430 | 32 | newcdev->class_dev.id); |
431 | 32 | newcdev->devfs.priv = newcdev; |
432 | 32 | newcdev->devfs.dev = dev; |
433 | 32 | newcdev->devfs.ops = &newcdev->fops; |
434 | 32 | newcdev->devfs.flags = DEVFS_IS_CHARACTER_DEV; |
435 | 32 | newcdev->fops.open = fops_open; |
436 | 32 | newcdev->fops.close = fops_close; |
437 | 32 | newcdev->fops.flush = fops_flush; |
438 | 32 | newcdev->fops.write = fops_write; |
439 | | |
440 | 32 | ret = devfs_create(&newcdev->devfs); |
441 | | |
442 | 32 | if (ret) { |
443 | 0 | pr_err("devfs entry creation failed: %pe\n", ERR_PTR(ret)); |
444 | 0 | return ret; |
445 | 0 | } |
446 | | |
447 | 32 | return 0; |
448 | 32 | } |
449 | | EXPORT_SYMBOL(console_register); |
450 | | |
451 | | int console_unregister(struct console_device *cdev) |
452 | 0 | { |
453 | 0 | struct device *dev = &cdev->class_dev; |
454 | 0 | int status; |
455 | | |
456 | | /* |
457 | | * We don't do any sophisticated serdev device de-population |
458 | | * and instead claim this console busy, preventing its |
459 | | * de-initialization, 'till the very end of our execution. |
460 | | */ |
461 | 0 | if (console_is_serdev_node(cdev)) |
462 | 0 | return -EBUSY; |
463 | | |
464 | | /* the device file is still open, e.g. by a 9P mount */ |
465 | 0 | status = devfs_remove(&cdev->devfs); |
466 | 0 | if (status) |
467 | 0 | return status; |
468 | | |
469 | 0 | list_del(&cdev->list); |
470 | 0 | if (list_empty(&console_list)) |
471 | 0 | initialized = CONSOLE_UNINITIALIZED; |
472 | |
|
473 | 0 | status = unregister_device(dev); |
474 | 0 | if (!status) |
475 | 0 | memset(cdev, 0, sizeof(*cdev)); |
476 | 0 | return status; |
477 | 0 | } |
478 | | EXPORT_SYMBOL(console_unregister); |
479 | | |
480 | | static __maybe_unused int console_activate_all_fallback(void) |
481 | 0 | { |
482 | 0 | int activate = CONSOLE_STDIOE; |
483 | 0 |
|
484 | 0 | for_each_console(cdev) { |
485 | 0 | if (cdev->f_active & (CONSOLE_STDOUT | CONSOLE_STDERR)) |
486 | 0 | return 0; |
487 | 0 | } |
488 | 0 |
|
489 | 0 | if (IS_ENABLED(CONFIG_CONSOLE_DISABLE_INPUT)) |
490 | 0 | activate &= ~CONSOLE_STDIN; |
491 | 0 |
|
492 | 0 | for_each_console(cdev) |
493 | 0 | console_set_active(cdev, activate); |
494 | 0 |
|
495 | 0 | /* |
496 | 0 | * This is last resort, so the user is not kept in the dark. |
497 | 0 | * Writing to all consoles is a bad idea as the devices at the |
498 | 0 | * other side might get confused by it, thus the error log level. |
499 | 0 | */ |
500 | 0 | pr_err("No consoles were activated. Activating all consoles as fallback!\n"); |
501 | 0 |
|
502 | 0 | return 0; |
503 | 0 | } |
504 | | #ifdef CONFIG_CONSOLE_ACTIVATE_ALL_FALLBACK |
505 | | postenvironment_initcall(console_activate_all_fallback); |
506 | | #endif |
507 | | |
508 | | static int getc_raw(void) |
509 | 0 | { |
510 | 0 | int active = 0; |
511 | |
|
512 | 0 | while (1) { |
513 | 0 | for_each_console(cdev) { |
514 | 0 | if (!(cdev->f_active & CONSOLE_STDIN)) |
515 | 0 | continue; |
516 | 0 | active = 1; |
517 | 0 | if (cdev->tstc(cdev)) { |
518 | 0 | int ch = cdev->getc(cdev); |
519 | |
|
520 | 0 | if (IS_ENABLED(CONFIG_RATP) && ch == 0x01 && |
521 | 0 | IS_ALLOWED(SCONFIG_RATP)) { |
522 | 0 | barebox_ratp(cdev); |
523 | 0 | return -1; |
524 | 0 | } |
525 | | |
526 | 0 | return ch; |
527 | 0 | } |
528 | 0 | } |
529 | 0 | if (!active) |
530 | | /* no active console found. bail out */ |
531 | 0 | return -1; |
532 | 0 | } |
533 | 0 | } |
534 | | |
535 | | static int tstc_raw(void) |
536 | 0 | { |
537 | 0 | if (!IS_ALLOWED(SCONFIG_CONSOLE_INPUT)) |
538 | 0 | return 0; |
539 | | |
540 | 0 | for_each_console(cdev) { |
541 | 0 | if (!(cdev->f_active & CONSOLE_STDIN)) |
542 | 0 | continue; |
543 | 0 | if (cdev->tstc(cdev)) |
544 | 0 | return 1; |
545 | 0 | } |
546 | | |
547 | 0 | return 0; |
548 | 0 | } |
549 | | |
550 | | int getchar(void) |
551 | 0 | { |
552 | 0 | unsigned char ch; |
553 | 0 | uint64_t start; |
554 | |
|
555 | 0 | if (!IS_ALLOWED(SCONFIG_CONSOLE_INPUT)) |
556 | 0 | return -1; |
557 | | |
558 | | /* |
559 | | * For 100us we read the characters from the serial driver |
560 | | * into a kfifo. This helps us not to lose characters |
561 | | * in small hardware fifos. |
562 | | */ |
563 | 0 | start = get_time_ns(); |
564 | 0 | while (1) { |
565 | 0 | if (tstc_raw()) { |
566 | 0 | int c = getc_raw(); |
567 | |
|
568 | 0 | if (c < 0) |
569 | 0 | break; |
570 | | |
571 | 0 | kfifo_putc(console_input_fifo, c); |
572 | |
|
573 | 0 | start = get_time_ns(); |
574 | 0 | } |
575 | | |
576 | 0 | if (is_timeout_interruptible(start, 100 * USECOND) && |
577 | 0 | kfifo_len(console_input_fifo)) |
578 | 0 | break; |
579 | 0 | } |
580 | |
|
581 | 0 | if (!kfifo_len(console_input_fifo)) |
582 | 0 | return -1; |
583 | | |
584 | 0 | kfifo_getc(console_input_fifo, &ch); |
585 | |
|
586 | 0 | return ch; |
587 | 0 | } |
588 | | EXPORT_SYMBOL(getchar); |
589 | | |
590 | | int tstc(void) |
591 | 0 | { |
592 | 0 | return kfifo_len(console_input_fifo) || tstc_raw(); |
593 | 0 | } |
594 | | EXPORT_SYMBOL(tstc); |
595 | | |
596 | | int pollchar(ktime_t duration) |
597 | 0 | { |
598 | 0 | ktime_t start = get_time_ns(); |
599 | |
|
600 | 0 | while (!tstc()) { |
601 | 0 | if (is_timeout(start, duration)) |
602 | 0 | return -ETIMEDOUT; |
603 | 0 | } |
604 | | |
605 | 0 | return getchar(); |
606 | 0 | } |
607 | | EXPORT_SYMBOL(pollchar); |
608 | | |
609 | | /** |
610 | | * console_ctrlc - check the console input for a Ctrl-C |
611 | | * |
612 | | * Unlike reading the input with getchar(), this keeps all other characters |
613 | | * queued, so that keys typed while a command runs aren't lost. As on a tty, |
614 | | * a Ctrl-C discards all queued input. |
615 | | * |
616 | | * Return: true if Ctrl-C was pressed |
617 | | */ |
618 | | bool console_ctrlc(void) |
619 | 0 | { |
620 | 0 | struct kfifo *fifo = console_input_fifo; |
621 | 0 | unsigned int i; |
622 | |
|
623 | 0 | while (tstc_raw()) { |
624 | 0 | int c = getc_raw(); |
625 | |
|
626 | 0 | if (c < 0) |
627 | 0 | break; |
628 | | |
629 | 0 | kfifo_putc(fifo, c); |
630 | 0 | } |
631 | | |
632 | | /* Check full FIFO: earlier getchar() may also have queued a Ctrl-C */ |
633 | 0 | for (i = fifo->out; i != fifo->in; i++) { |
634 | 0 | if (fifo->buffer[i & (fifo->size - 1)] == CTL_CH('c')) { |
635 | 0 | kfifo_reset(fifo); |
636 | 0 | return true; |
637 | 0 | } |
638 | 0 | } |
639 | | |
640 | 0 | return false; |
641 | 0 | } |
642 | | |
643 | | int console_putc(struct console_device *con, char c) |
644 | 0 | { |
645 | 0 | bool crlf = c == '\n'; |
646 | |
|
647 | 0 | if (crlf) |
648 | 0 | console_putbin(con, "\r\n", 2); |
649 | 0 | else |
650 | 0 | console_putbin(con, &c, 1); |
651 | |
|
652 | 0 | return 1 + crlf; |
653 | 0 | } |
654 | | EXPORT_SYMBOL(console_putc); |
655 | | |
656 | | int console_puts(struct console_device *con, const char *str) |
657 | 140 | { |
658 | 140 | const char *s = str; |
659 | 140 | unsigned int ch; |
660 | 140 | int n = 0; |
661 | | |
662 | 140 | ch = console_dev_is_std(con); |
663 | 140 | if (!ch) |
664 | 70 | return con->puts(con, str, strlen(str)); |
665 | | |
666 | 70 | if (initialized == CONSOLE_INIT_FULL) { |
667 | 140 | for_each_console(cdev) { |
668 | 70 | if (cdev->f_active & ch) |
669 | 70 | n = console_puts(cdev, str); |
670 | 70 | } |
671 | 140 | return n; |
672 | 70 | } |
673 | | |
674 | 0 | while (*s) { |
675 | 0 | n += console_putc(con, *s); |
676 | 0 | s++; |
677 | 0 | } |
678 | 0 | return n; |
679 | 70 | } |
680 | | EXPORT_SYMBOL(console_puts); |
681 | | |
682 | | void console_putbin(struct console_device *con, const u8 *str, size_t len) |
683 | 0 | { |
684 | 0 | unsigned int ch; |
685 | |
|
686 | 0 | ch = console_dev_is_std(con); |
687 | 0 | if (!ch) { |
688 | 0 | for (size_t i = 0; i < len; i++) |
689 | 0 | con->putc(con, str[i]); |
690 | 0 | return; |
691 | 0 | } |
692 | | |
693 | 0 | switch (initialized) { |
694 | 0 | case CONSOLE_UNINITIALIZED: |
695 | 0 | console_init_early(); |
696 | 0 | fallthrough; |
697 | 0 | case CONSOLE_INITIALIZED_BUFFER: |
698 | 0 | kfifo_put(console_output_fifo, str, len); |
699 | 0 | for (size_t i = 0; i < len; i++) |
700 | 0 | putc_ll(str[i]); |
701 | 0 | return; |
702 | 0 | case CONSOLE_INIT_FULL: |
703 | 0 | for_each_console(cdev) { |
704 | 0 | if (cdev->f_active & ch) |
705 | 0 | console_putbin(cdev, str, len); |
706 | 0 | } |
707 | 0 | return; |
708 | 0 | default: |
709 | | /* If we have problems inititalizing our data |
710 | | * get them early |
711 | | */ |
712 | 0 | hang(); |
713 | 0 | } |
714 | 0 | } |
715 | | EXPORT_SYMBOL(console_putbin); |
716 | | |
717 | | void console_flush(void) |
718 | 0 | { |
719 | 0 | for_each_console(cdev) { |
720 | 0 | if (cdev->flush) |
721 | 0 | cdev->flush(cdev); |
722 | 0 | } |
723 | 0 | } |
724 | | EXPORT_SYMBOL(console_flush); |
725 | | |
726 | | BAREBOX_MAGICVAR(global.linux.bootargs.console, |
727 | | "console= argument for Linux from the stdout-path property in /chosen node"); |