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