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