xref: /freebsd/stand/kboot/kboot/hostcons.c (revision f989ebd4dea2ad45f3dd491d86f821a524a0360f)
1 /*-
2  * Copyright (C) 2014 Nathan Whitehorn
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include <sys/types.h>
27 #include "bootstrap.h"
28 #include "host_syscall.h"
29 #include "termios.h"
30 
31 static void hostcons_probe(struct console *cp);
32 static int hostcons_init(int arg);
33 static void hostcons_putchar(int c);
34 static int hostcons_getchar(void);
35 static int hostcons_poll(void);
36 
37 struct console hostconsole = {
38 	.c_name = "host",
39 	.c_desc = "Host Console",
40 	.c_probe = hostcons_probe,
41 	.c_init = hostcons_init,
42 	.c_out = hostcons_putchar,
43 	.c_in = hostcons_getchar,
44 	.c_ready = hostcons_poll,
45 };
46 
47 static struct host_termios old_settings;
48 
49 static void
hostcons_probe(struct console * cp)50 hostcons_probe(struct console *cp)
51 {
52 
53 	cp->c_flags |= C_PRESENTIN|C_PRESENTOUT;
54 }
55 
56 static int
hostcons_init(int arg)57 hostcons_init(int arg)
58 {
59 	struct host_termios new_settings;
60 
61 	host_tcgetattr(0, &old_settings);
62 	new_settings = old_settings;
63 	host_cfmakeraw(&new_settings);
64 	host_tcsetattr(0, HOST_TCSANOW, &new_settings);
65 
66 	return (0);
67 }
68 
69 static void
hostcons_putchar(int c)70 hostcons_putchar(int c)
71 {
72 	uint8_t ch = c;
73 
74 	host_write(1, &ch, 1);
75 }
76 
77 static int
hostcons_getchar(void)78 hostcons_getchar(void)
79 {
80 	uint8_t ch;
81 	int rv;
82 
83 	rv = host_read(0, &ch, 1);
84 	if (rv == 1)
85 		return (ch);
86 	return (-1);
87 }
88 
89 static int
hostcons_poll(void)90 hostcons_poll(void)
91 {
92 	struct host_timeval tv = {0,0};
93 	long fds = 1 << 0;
94 	int ret;
95 
96 	ret = host_select(32, &fds, NULL, NULL, &tv);
97 	return (ret > 0);
98 }
99