3 * Copyright (C) 2008 Advanced Micro Devices, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <libpayload-config.h>
30 #include <libpayload.h>
33 struct console_output_driver
*console_out
;
34 struct console_input_driver
*console_in
;
35 static console_input_type last_getchar_input_type
;
37 static int output_driver_exists(struct console_output_driver
*out
)
39 struct console_output_driver
*head
= console_out
;
50 static int input_driver_exists(struct console_input_driver
*in
)
52 struct console_input_driver
*head
= console_in
;
63 void console_add_output_driver(struct console_output_driver
*out
)
65 die_if(!out
->putchar
&& !out
->write
, "Need at least one output func\n");
66 /* Check if this driver was already added to the console list */
67 if (output_driver_exists(out
))
69 out
->next
= console_out
;
73 void console_add_input_driver(struct console_input_driver
*in
)
75 /* Check if this driver was already added to the console list */
76 if (input_driver_exists(in
))
78 /* Flush out the driver input buffer. */
81 in
->next
= console_in
;
86 * For when you really need to silence an output driver (e.g. to avoid ugly
87 * recursions). Takes the pointer of either of the two output functions, since
88 * the struct console_output_driver itself is often static and inaccessible.
90 int console_remove_output_driver(void *function
)
92 struct console_output_driver
**out
;
93 for (out
= &console_out
; *out
; out
= &(*out
)->next
)
94 if ((*out
)->putchar
== function
|| (*out
)->write
== function
) {
102 void console_init(void)
104 #if CONFIG(LP_VIDEO_CONSOLE)
105 video_console_init();
107 #if CONFIG(LP_SERIAL_CONSOLE)
108 serial_console_init();
110 #if CONFIG(LP_PC_KEYBOARD)
113 #if CONFIG(LP_CBMEM_CONSOLE)
114 cbmem_console_init();
118 void console_write(const void *buffer
, size_t count
)
121 struct console_output_driver
*out
;
122 for (out
= console_out
; out
!= 0; out
= out
->next
)
124 out
->write(buffer
, count
);
126 for (ptr
= buffer
; (void *)ptr
< buffer
+ count
; ptr
++)
130 int putchar(unsigned int i
)
132 unsigned char c
= (unsigned char)i
;
133 console_write(&c
, 1);
137 int puts(const char *s
)
139 size_t size
= strlen(s
);
141 console_write(s
, size
);
152 struct console_input_driver
*in
;
153 for (in
= console_in
; in
!= 0; in
= in
->next
)
160 * This returns an ASCII value - the two getchar functions
161 * cook the respective input from the device.
169 struct console_input_driver
*in
;
170 for (in
= console_in
; in
!= 0; in
= in
->next
)
171 if (in
->havechar()) {
172 last_getchar_input_type
= in
->input_type
;
173 return in
->getchar();
178 int getchar_timeout(int *ms
)
194 console_input_type
last_key_input_type(void)
196 return last_getchar_input_type
;