xref: /freebsd/stand/libofw/ofw_console.c (revision e9ac41698b2f322d55ccf9da50a3596edb2c1800)
1 /* $NetBSD: prom.c,v 1.3 1997/09/06 14:03:58 drochner Exp $ */
2 
3 /*-
4  * Mach Operating System
5  * Copyright (c) 1992 Carnegie Mellon University
6  * All Rights Reserved.
7  *
8  * Permission to use, copy, modify and distribute this software and its
9  * documentation is hereby granted, provided that both the copyright
10  * notice and this permission notice appear in all copies of the
11  * software, derivative works or modified versions, and any portions
12  * thereof, and that both notices appear in supporting documentation.
13  *
14  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17  *
18  * Carnegie Mellon requests users of this software to return to
19  *
20  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
21  *  School of Computer Science
22  *  Carnegie Mellon University
23  *  Pittsburgh PA 15213-3890
24  *
25  * any improvements or extensions that they make and grant Carnegie Mellon
26  * the rights to redistribute these changes.
27  */
28 
29 #include <sys/types.h>
30 
31 #include "bootstrap.h"
32 #include "openfirm.h"
33 
34 static void ofw_cons_probe(struct console *cp);
35 static int ofw_cons_init(int);
36 void ofw_cons_putchar(int);
37 int ofw_cons_getchar(void);
38 int ofw_cons_poll(void);
39 
40 static ihandle_t stdin;
41 static ihandle_t stdout;
42 
43 struct console ofwconsole = {
44 	.c_name = "ofw",
45 	.c_desc = "Open Firmware console",
46 	.c_probe = ofw_cons_probe,
47 	.c_init = ofw_cons_init,
48 	.c_out = ofw_cons_putchar,
49 	.c_in = ofw_cons_getchar,
50 	.c_ready = ofw_cons_poll,
51 };
52 
53 static void
54 ofw_cons_probe(struct console *cp)
55 {
56 
57 	OF_getprop(chosen, "stdin", &stdin, sizeof(stdin));
58 	OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
59 	cp->c_flags |= C_PRESENTIN|C_PRESENTOUT;
60 }
61 
62 static int
63 ofw_cons_init(int arg)
64 {
65 	return (0);
66 }
67 
68 void
69 ofw_cons_putchar(int c)
70 {
71 	char cbuf;
72 
73 	if (c == '\n') {
74 		cbuf = '\r';
75 		OF_write(stdout, &cbuf, 1);
76 	}
77 
78 	cbuf = c;
79 	OF_write(stdout, &cbuf, 1);
80 }
81 
82 static int saved_char = -1;
83 
84 int
85 ofw_cons_getchar(void)
86 {
87 	unsigned char ch = '\0';
88 	int l;
89 
90 	if (saved_char != -1) {
91 		l = saved_char;
92 		saved_char = -1;
93 		return (l);
94 	}
95 
96 	/* At least since version 4.0.0, QEMU became bug-compatible
97 	 * with PowerVM's vty, by inserting a \0 after every \r.
98 	 * As this confuses loader's interpreter and as a \0 coming
99 	 * from the console doesn't seem reasonable, it's filtered here. */
100 	if (OF_read(stdin, &ch, 1) > 0 && ch != '\0')
101 		return (ch);
102 
103 	return (-1);
104 }
105 
106 int
107 ofw_cons_poll(void)
108 {
109 	unsigned char ch;
110 
111 	if (saved_char != -1)
112 		return (1);
113 
114 	if (OF_read(stdin, &ch, 1) > 0) {
115 		saved_char = ch;
116 		return (1);
117 	}
118 
119 	return (0);
120 }
121