Julius Werner | 03020da | 2018-11-27 22:10:56 -0800 | [diff] [blame] | 1 | /* |
Yann Gautier | 0d5ca3d | 2022-12-09 14:30:55 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2023, ARM Limited and Contributors. All rights reserved. |
Julius Werner | 03020da | 2018-11-27 22:10:56 -0800 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Julius Werner | 03020da | 2018-11-27 22:10:56 -0800 | [diff] [blame] | 7 | #include <assert.h> |
Claus Pedersen | 785e66c | 2022-09-12 22:42:58 +0000 | [diff] [blame] | 8 | #include <stddef.h> |
Claus Pedersen | 63f7586 | 2022-09-12 23:47:10 +0000 | [diff] [blame] | 9 | #include <stdlib.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 10 | |
Julius Werner | 03020da | 2018-11-27 22:10:56 -0800 | [diff] [blame] | 11 | #include <drivers/console.h> |
| 12 | |
| 13 | console_t *console_list; |
Yann Gautier | 0d5ca3d | 2022-12-09 14:30:55 +0100 | [diff] [blame] | 14 | static uint8_t console_state = CONSOLE_FLAG_BOOT; |
Julius Werner | 03020da | 2018-11-27 22:10:56 -0800 | [diff] [blame] | 15 | |
Ambroise Vincent | a88a35d | 2019-02-14 09:48:21 +0000 | [diff] [blame] | 16 | IMPORT_SYM(console_t *, __STACKS_START__, stacks_start) |
| 17 | IMPORT_SYM(console_t *, __STACKS_END__, stacks_end) |
| 18 | |
Julius Werner | 03020da | 2018-11-27 22:10:56 -0800 | [diff] [blame] | 19 | int console_register(console_t *console) |
| 20 | { |
Julius Werner | 03020da | 2018-11-27 22:10:56 -0800 | [diff] [blame] | 21 | /* Assert that the struct is not on the stack (common mistake). */ |
| 22 | assert((console < stacks_start) || (console >= stacks_end)); |
Ambroise Vincent | 29d5c38 | 2019-04-18 11:36:42 +0100 | [diff] [blame] | 23 | |
| 24 | /* Check that we won't make a circle in the list. */ |
| 25 | if (console_is_registered(console) == 1) |
| 26 | return 1; |
Julius Werner | 03020da | 2018-11-27 22:10:56 -0800 | [diff] [blame] | 27 | |
| 28 | console->next = console_list; |
| 29 | console_list = console; |
| 30 | |
| 31 | /* Return 1 for convenient tail-calling from console_xxx_register(). */ |
| 32 | return 1; |
| 33 | } |
| 34 | |
| 35 | console_t *console_unregister(console_t *to_be_deleted) |
| 36 | { |
| 37 | console_t **ptr; |
| 38 | |
| 39 | assert(to_be_deleted != NULL); |
| 40 | |
| 41 | for (ptr = &console_list; *ptr != NULL; ptr = &(*ptr)->next) |
| 42 | if (*ptr == to_be_deleted) { |
| 43 | *ptr = (*ptr)->next; |
| 44 | return to_be_deleted; |
| 45 | } |
| 46 | |
| 47 | return NULL; |
| 48 | } |
| 49 | |
| 50 | int console_is_registered(console_t *to_find) |
| 51 | { |
| 52 | console_t *console; |
| 53 | |
| 54 | assert(to_find != NULL); |
| 55 | |
| 56 | for (console = console_list; console != NULL; console = console->next) |
| 57 | if (console == to_find) |
| 58 | return 1; |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | void console_switch_state(unsigned int new_state) |
| 64 | { |
| 65 | console_state = new_state; |
| 66 | } |
| 67 | |
| 68 | void console_set_scope(console_t *console, unsigned int scope) |
| 69 | { |
| 70 | assert(console != NULL); |
| 71 | |
| 72 | console->flags = (console->flags & ~CONSOLE_FLAG_SCOPE_MASK) | scope; |
| 73 | } |
| 74 | |
Masahiro Yamada | 269a8ad | 2019-07-23 12:32:58 +0900 | [diff] [blame] | 75 | static int do_putc(int c, console_t *console) |
| 76 | { |
| 77 | int ret; |
| 78 | |
| 79 | if ((c == '\n') && |
| 80 | ((console->flags & CONSOLE_FLAG_TRANSLATE_CRLF) != 0)) { |
| 81 | ret = console->putc('\r', console); |
| 82 | if (ret < 0) |
| 83 | return ret; |
| 84 | } |
| 85 | |
| 86 | return console->putc(c, console); |
| 87 | } |
| 88 | |
Julius Werner | 03020da | 2018-11-27 22:10:56 -0800 | [diff] [blame] | 89 | int console_putc(int c) |
| 90 | { |
| 91 | int err = ERROR_NO_VALID_CONSOLE; |
| 92 | console_t *console; |
| 93 | |
| 94 | for (console = console_list; console != NULL; console = console->next) |
Zelalem | e8dadb1 | 2020-02-05 14:12:39 -0600 | [diff] [blame] | 95 | if ((console->flags & console_state) && (console->putc != NULL)) { |
Masahiro Yamada | 269a8ad | 2019-07-23 12:32:58 +0900 | [diff] [blame] | 96 | int ret = do_putc(c, console); |
Julius Werner | 03020da | 2018-11-27 22:10:56 -0800 | [diff] [blame] | 97 | if ((err == ERROR_NO_VALID_CONSOLE) || (ret < err)) |
| 98 | err = ret; |
| 99 | } |
Julius Werner | 03020da | 2018-11-27 22:10:56 -0800 | [diff] [blame] | 100 | return err; |
| 101 | } |
| 102 | |
Claus Pedersen | 63f7586 | 2022-09-12 23:47:10 +0000 | [diff] [blame] | 103 | int putchar(int c) |
| 104 | { |
| 105 | if (console_putc(c) == 0) |
| 106 | return c; |
| 107 | else |
| 108 | return EOF; |
| 109 | } |
| 110 | |
Sandrine Bailleux | f57e203 | 2023-10-11 08:38:00 +0200 | [diff] [blame] | 111 | #if ENABLE_CONSOLE_GETC |
Julius Werner | 03020da | 2018-11-27 22:10:56 -0800 | [diff] [blame] | 112 | int console_getc(void) |
| 113 | { |
| 114 | int err = ERROR_NO_VALID_CONSOLE; |
| 115 | console_t *console; |
| 116 | |
| 117 | do { /* Keep polling while at least one console works correctly. */ |
| 118 | for (console = console_list; console != NULL; |
| 119 | console = console->next) |
Zelalem | e8dadb1 | 2020-02-05 14:12:39 -0600 | [diff] [blame] | 120 | if ((console->flags & console_state) && (console->getc != NULL)) { |
Julius Werner | 03020da | 2018-11-27 22:10:56 -0800 | [diff] [blame] | 121 | int ret = console->getc(console); |
| 122 | if (ret >= 0) |
| 123 | return ret; |
| 124 | if (err != ERROR_NO_PENDING_CHAR) |
| 125 | err = ret; |
| 126 | } |
| 127 | } while (err == ERROR_NO_PENDING_CHAR); |
| 128 | |
| 129 | return err; |
| 130 | } |
Sandrine Bailleux | f57e203 | 2023-10-11 08:38:00 +0200 | [diff] [blame] | 131 | #endif |
Julius Werner | 03020da | 2018-11-27 22:10:56 -0800 | [diff] [blame] | 132 | |
Jimmy Brisson | 39f9eee | 2020-08-05 13:44:05 -0500 | [diff] [blame] | 133 | void console_flush(void) |
Julius Werner | 03020da | 2018-11-27 22:10:56 -0800 | [diff] [blame] | 134 | { |
Julius Werner | 03020da | 2018-11-27 22:10:56 -0800 | [diff] [blame] | 135 | console_t *console; |
| 136 | |
| 137 | for (console = console_list; console != NULL; console = console->next) |
Zelalem | e8dadb1 | 2020-02-05 14:12:39 -0600 | [diff] [blame] | 138 | if ((console->flags & console_state) && (console->flush != NULL)) { |
Jimmy Brisson | 39f9eee | 2020-08-05 13:44:05 -0500 | [diff] [blame] | 139 | console->flush(console); |
Julius Werner | 03020da | 2018-11-27 22:10:56 -0800 | [diff] [blame] | 140 | } |
Julius Werner | 03020da | 2018-11-27 22:10:56 -0800 | [diff] [blame] | 141 | } |