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$");
31
32 #include <sys/types.h>
33
34 #include "bootstrap.h"
35 #include "openfirm.h"
36
37 static void ofw_cons_probe(struct console *cp);
38 static int ofw_cons_init(int);
39 void ofw_cons_putchar(int);
40 int ofw_cons_getchar(void);
41 int ofw_cons_poll(void);
42
43 static ihandle_t stdin;
44 static ihandle_t stdout;
45
46 struct console ofwconsole = {
47 .c_name = "ofw",
48 .c_desc = "Open Firmware console",
49 .c_flags = 0,
50 .c_probe = ofw_cons_probe,
51 .c_init = ofw_cons_init,
52 .c_out = ofw_cons_putchar,
53 .c_in = ofw_cons_getchar,
54 .c_ready = ofw_cons_poll,
55 .c_private = NULL
56 };
57
58 static void
ofw_cons_probe(struct console * cp)59 ofw_cons_probe(struct console *cp)
60 {
61
62 OF_getprop(chosen, "stdin", &stdin, sizeof(stdin));
63 OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
64 cp->c_flags |= C_PRESENTIN|C_PRESENTOUT;
65 }
66
67 static int
ofw_cons_init(int arg)68 ofw_cons_init(int arg)
69 {
70 return 0;
71 }
72
73 void
ofw_cons_putchar(int c)74 ofw_cons_putchar(int c)
75 {
76 char cbuf;
77
78 if (c == '\n') {
79 cbuf = '\r';
80 OF_write(stdout, &cbuf, 1);
81 }
82
83 cbuf = c;
84 OF_write(stdout, &cbuf, 1);
85 }
86
87 static int saved_char = -1;
88
89 int
ofw_cons_getchar()90 ofw_cons_getchar()
91 {
92 unsigned char ch = '\0';
93 int l;
94
95 if (saved_char != -1) {
96 l = saved_char;
97 saved_char = -1;
98 return l;
99 }
100
101 if (OF_read(stdin, &ch, 1) > 0)
102 return (ch);
103
104 return (-1);
105 }
106
107 int
ofw_cons_poll()108 ofw_cons_poll()
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