ofw_console.c revision 139738
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/cdefs.h>
30__FBSDID("$FreeBSD: head/sys/boot/ofw/libofw/ofw_console.c 139738 2005-01-05 22:16:58Z imp $");
31
32#include <sys/types.h>
33
34#include "bootstrap.h"
35#include "openfirm.h"
36
37int console;
38
39static void ofw_cons_probe(struct console *cp);
40static int ofw_cons_init(int);
41void ofw_cons_putchar(int);
42int ofw_cons_getchar(void);
43int ofw_cons_poll(void);
44
45static ihandle_t stdin;
46static ihandle_t stdout;
47
48struct console ofwconsole = {
49	"ofw",
50	"Open Firmware console",
51	0,
52	ofw_cons_probe,
53	ofw_cons_init,
54	ofw_cons_putchar,
55	ofw_cons_getchar,
56	ofw_cons_poll,
57};
58
59static void
60ofw_cons_probe(struct console *cp)
61{
62	phandle_t chosen;
63
64	if ((chosen = OF_finddevice("/chosen")) == -1)
65		OF_exit();
66	OF_getprop(chosen, "stdin", &stdin, sizeof(stdin));
67	OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
68	cp->c_flags |= C_PRESENTIN|C_PRESENTOUT;
69}
70
71static int
72ofw_cons_init(int arg)
73{
74	return 0;
75}
76
77void
78ofw_cons_putchar(int c)
79{
80	char cbuf;
81
82	if (c == '\n') {
83		cbuf = '\r';
84		OF_write(stdout, &cbuf, 1);
85	}
86
87	cbuf = c;
88	OF_write(stdout, &cbuf, 1);
89}
90
91static int saved_char = -1;
92
93int
94ofw_cons_getchar()
95{
96	unsigned char ch = '\0';
97	int l;
98
99	if (saved_char != -1) {
100		l = saved_char;
101		saved_char = -1;
102		return l;
103	}
104
105	while ((l = OF_read(stdin, &ch, 1)) != 1)
106		if (l != -2 && l != 0)
107			return -1;
108	return ch;
109}
110
111int
112ofw_cons_poll()
113{
114	unsigned char ch;
115	int l;
116
117	if (saved_char != -1)
118		return 1;
119
120	if (OF_read(stdin, &ch, 1) > 0) {
121		saved_char = ch;
122		return 1;
123	}
124
125	return 0;
126}
127