138465Smsmith/* $NetBSD: prom.c,v 1.3 1997/09/06 14:03:58 drochner Exp $ */
238465Smsmith
3139738Simp/*-
438465Smsmith * Mach Operating System
538465Smsmith * Copyright (c) 1992 Carnegie Mellon University
638465Smsmith * All Rights Reserved.
738465Smsmith *
838465Smsmith * Permission to use, copy, modify and distribute this software and its
938465Smsmith * documentation is hereby granted, provided that both the copyright
1038465Smsmith * notice and this permission notice appear in all copies of the
1138465Smsmith * software, derivative works or modified versions, and any portions
1238465Smsmith * thereof, and that both notices appear in supporting documentation.
1338465Smsmith *
1438465Smsmith * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
1538465Smsmith * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
1638465Smsmith * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
1738465Smsmith *
1838465Smsmith * Carnegie Mellon requests users of this software to return to
1938465Smsmith *
2038465Smsmith *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
2138465Smsmith *  School of Computer Science
2238465Smsmith *  Carnegie Mellon University
2338465Smsmith *  Pittsburgh PA 15213-3890
2438465Smsmith *
2538465Smsmith * any improvements or extensions that they make and grant Carnegie Mellon
2638465Smsmith * the rights to redistribute these changes.
2738465Smsmith */
2838465Smsmith
29124140Sobrien#include <sys/cdefs.h>
30124140Sobrien__FBSDID("$FreeBSD$");
31124140Sobrien
3238465Smsmith#include <sys/types.h>
3338465Smsmith
3438465Smsmith#include "bootstrap.h"
3567227Sobrien#include "openfirm.h"
3638465Smsmith
3767227Sobrienstatic void ofw_cons_probe(struct console *cp);
3867227Sobrienstatic int ofw_cons_init(int);
3967227Sobrienvoid ofw_cons_putchar(int);
4067227Sobrienint ofw_cons_getchar(void);
4167227Sobrienint ofw_cons_poll(void);
4238465Smsmith
4367227Sobrienstatic ihandle_t stdin;
4467227Sobrienstatic ihandle_t stdout;
4567227Sobrien
4667227Sobrienstruct console ofwconsole = {
4767227Sobrien	"ofw",
48133862Smarius	"Open Firmware console",
4967227Sobrien	0,
5067227Sobrien	ofw_cons_probe,
5167227Sobrien	ofw_cons_init,
5267227Sobrien	ofw_cons_putchar,
5367227Sobrien	ofw_cons_getchar,
5467227Sobrien	ofw_cons_poll,
5538465Smsmith};
5638465Smsmith
5767227Sobrienstatic void
5867227Sobrienofw_cons_probe(struct console *cp)
5938465Smsmith{
6038465Smsmith
6167227Sobrien	OF_getprop(chosen, "stdin", &stdin, sizeof(stdin));
6267227Sobrien	OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
6367227Sobrien	cp->c_flags |= C_PRESENTIN|C_PRESENTOUT;
6438465Smsmith}
6538465Smsmith
6638465Smsmithstatic int
6767227Sobrienofw_cons_init(int arg)
6838465Smsmith{
6967227Sobrien	return 0;
7038465Smsmith}
7138465Smsmith
7238465Smsmithvoid
7367227Sobrienofw_cons_putchar(int c)
7438465Smsmith{
7567227Sobrien	char cbuf;
7638465Smsmith
7767227Sobrien	if (c == '\n') {
7867227Sobrien		cbuf = '\r';
7967227Sobrien		OF_write(stdout, &cbuf, 1);
8067227Sobrien	}
8167227Sobrien
8267227Sobrien	cbuf = c;
8367227Sobrien	OF_write(stdout, &cbuf, 1);
8438465Smsmith}
8538465Smsmith
8638465Smsmithstatic int saved_char = -1;
8738465Smsmith
8838465Smsmithint
8967227Sobrienofw_cons_getchar()
9038465Smsmith{
9167227Sobrien	unsigned char ch = '\0';
9267227Sobrien	int l;
9338465Smsmith
9467227Sobrien	if (saved_char != -1) {
9567227Sobrien		l = saved_char;
9667227Sobrien		saved_char = -1;
9767227Sobrien		return l;
9867227Sobrien	}
9938465Smsmith
100176068Sgrehan	if (OF_read(stdin, &ch, 1) > 0)
101176068Sgrehan		return (ch);
102176068Sgrehan
103176068Sgrehan	return (-1);
10438465Smsmith}
10538465Smsmith
10638465Smsmithint
10767227Sobrienofw_cons_poll()
10838465Smsmith{
10967227Sobrien	unsigned char ch;
11038465Smsmith
11167227Sobrien	if (saved_char != -1)
11267227Sobrien		return 1;
11338465Smsmith
11491113Sjake	if (OF_read(stdin, &ch, 1) > 0) {
11567227Sobrien		saved_char = ch;
11667227Sobrien		return 1;
11767227Sobrien	}
11838465Smsmith
11967227Sobrien	return 0;
12038465Smsmith}
121