blob: 08b8e9fb177e8880a155b05b37b936a72ae0eea4 [file] [log] [blame]
Julius Werner03020da2018-11-27 22:10:56 -08001/*
Zelaleme8dadb12020-02-05 14:12:39 -06002 * Copyright (c) 2018-2020, ARM Limited and Contributors. All rights reserved.
Julius Werner03020da2018-11-27 22:10:56 -08003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Julius Werner03020da2018-11-27 22:10:56 -08007#include <assert.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00008
Julius Werner03020da2018-11-27 22:10:56 -08009#include <drivers/console.h>
10
11console_t *console_list;
12uint8_t console_state = CONSOLE_FLAG_BOOT;
13
Ambroise Vincenta88a35d2019-02-14 09:48:21 +000014IMPORT_SYM(console_t *, __STACKS_START__, stacks_start)
15IMPORT_SYM(console_t *, __STACKS_END__, stacks_end)
16
Julius Werner03020da2018-11-27 22:10:56 -080017int console_register(console_t *console)
18{
Julius Werner03020da2018-11-27 22:10:56 -080019 /* Assert that the struct is not on the stack (common mistake). */
20 assert((console < stacks_start) || (console >= stacks_end));
Ambroise Vincent29d5c382019-04-18 11:36:42 +010021
22 /* Check that we won't make a circle in the list. */
23 if (console_is_registered(console) == 1)
24 return 1;
Julius Werner03020da2018-11-27 22:10:56 -080025
26 console->next = console_list;
27 console_list = console;
28
29 /* Return 1 for convenient tail-calling from console_xxx_register(). */
30 return 1;
31}
32
33console_t *console_unregister(console_t *to_be_deleted)
34{
35 console_t **ptr;
36
37 assert(to_be_deleted != NULL);
38
39 for (ptr = &console_list; *ptr != NULL; ptr = &(*ptr)->next)
40 if (*ptr == to_be_deleted) {
41 *ptr = (*ptr)->next;
42 return to_be_deleted;
43 }
44
45 return NULL;
46}
47
48int console_is_registered(console_t *to_find)
49{
50 console_t *console;
51
52 assert(to_find != NULL);
53
54 for (console = console_list; console != NULL; console = console->next)
55 if (console == to_find)
56 return 1;
57
58 return 0;
59}
60
61void console_switch_state(unsigned int new_state)
62{
63 console_state = new_state;
64}
65
66void console_set_scope(console_t *console, unsigned int scope)
67{
68 assert(console != NULL);
69
70 console->flags = (console->flags & ~CONSOLE_FLAG_SCOPE_MASK) | scope;
71}
72
Masahiro Yamada269a8ad2019-07-23 12:32:58 +090073static int do_putc(int c, console_t *console)
74{
75 int ret;
76
77 if ((c == '\n') &&
78 ((console->flags & CONSOLE_FLAG_TRANSLATE_CRLF) != 0)) {
79 ret = console->putc('\r', console);
80 if (ret < 0)
81 return ret;
82 }
83
84 return console->putc(c, console);
85}
86
Julius Werner03020da2018-11-27 22:10:56 -080087int console_putc(int c)
88{
89 int err = ERROR_NO_VALID_CONSOLE;
90 console_t *console;
91
92 for (console = console_list; console != NULL; console = console->next)
Zelaleme8dadb12020-02-05 14:12:39 -060093 if ((console->flags & console_state) && (console->putc != NULL)) {
Masahiro Yamada269a8ad2019-07-23 12:32:58 +090094 int ret = do_putc(c, console);
Julius Werner03020da2018-11-27 22:10:56 -080095 if ((err == ERROR_NO_VALID_CONSOLE) || (ret < err))
96 err = ret;
97 }
98
99 return err;
100}
101
102int console_getc(void)
103{
104 int err = ERROR_NO_VALID_CONSOLE;
105 console_t *console;
106
107 do { /* Keep polling while at least one console works correctly. */
108 for (console = console_list; console != NULL;
109 console = console->next)
Zelaleme8dadb12020-02-05 14:12:39 -0600110 if ((console->flags & console_state) && (console->getc != NULL)) {
Julius Werner03020da2018-11-27 22:10:56 -0800111 int ret = console->getc(console);
112 if (ret >= 0)
113 return ret;
114 if (err != ERROR_NO_PENDING_CHAR)
115 err = ret;
116 }
117 } while (err == ERROR_NO_PENDING_CHAR);
118
119 return err;
120}
121
Jimmy Brisson39f9eee2020-08-05 13:44:05 -0500122void console_flush(void)
Julius Werner03020da2018-11-27 22:10:56 -0800123{
Julius Werner03020da2018-11-27 22:10:56 -0800124 console_t *console;
125
126 for (console = console_list; console != NULL; console = console->next)
Zelaleme8dadb12020-02-05 14:12:39 -0600127 if ((console->flags & console_state) && (console->flush != NULL)) {
Jimmy Brisson39f9eee2020-08-05 13:44:05 -0500128 console->flush(console);
Julius Werner03020da2018-11-27 22:10:56 -0800129 }
Julius Werner03020da2018-11-27 22:10:56 -0800130}