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
34static void ofw_cons_probe(struct console *cp);
35static int ofw_cons_init(int);
36void ofw_cons_putchar(int);
37int ofw_cons_getchar(void);
38int ofw_cons_poll(void);
39
40static ihandle_t stdin;
41static ihandle_t stdout;
42
43struct console ofwconsole = {
44	"ofw",
45	"Open Firmware console",
46	0,
47	ofw_cons_probe,
48	ofw_cons_init,
49	ofw_cons_putchar,
50	ofw_cons_getchar,
51	ofw_cons_poll,
52};
53
54static void
55ofw_cons_probe(struct console *cp)
56{
57
58	OF_getprop(chosen, "stdin", &stdin, sizeof(stdin));
59	OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
60	cp->c_flags |= C_PRESENTIN|C_PRESENTOUT;
61}
62
63static int
64ofw_cons_init(int arg)
65{
66	return 0;
67}
68
69void
70ofw_cons_putchar(int c)
71{
72	char cbuf;
73
74	if (c == '\n') {
75		cbuf = '\r';
76		OF_write(stdout, &cbuf, 1);
77	}
78
79	cbuf = c;
80	OF_write(stdout, &cbuf, 1);
81}
82
83static int saved_char = -1;
84
85int
86ofw_cons_getchar(void)
87{
88	unsigned char ch = '\0';
89	int l;
90
91	if (saved_char != -1) {
92		l = saved_char;
93		saved_char = -1;
94		return l;
95	}
96
97	/* At least since version 4.0.0, QEMU became bug-compatible
98	 * with PowerVM's vty, by inserting a \0 after every \r.
99	 * As this confuses loader's interpreter and as a \0 coming
100	 * from the console doesn't seem reasonable, it's filtered here. */
101	if (OF_read(stdin, &ch, 1) > 0 && ch != '\0')
102		return (ch);
103
104	return (-1);
105}
106
107int
108ofw_cons_poll(void)
109{
110	unsigned char ch;
111
112	if (saved_char != -1)
113		return 1;
114
115	if (OF_read(stdin, &ch, 1) > 0) {
116		saved_char = ch;
117		return 1;
118	}
119
120	return 0;
121}
122