consinit.c revision 1.20 1 /* $NetBSD: consinit.c,v 1.20 2005/12/17 17:58:02 jdc Exp $ */
2
3 /*-
4 * Copyright (c) 1999 Eduardo E. Horvath
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: consinit.c,v 1.20 2005/12/17 17:58:02 jdc Exp $");
33
34 #include "opt_ddb.h"
35 #include "pcons.h"
36 #include "ukbd.h"
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/conf.h>
41 #include <sys/device.h>
42 #include <sys/file.h>
43 #include <sys/ioctl.h>
44 #include <sys/kernel.h>
45 #include <sys/proc.h>
46 #include <sys/tty.h>
47 #include <sys/time.h>
48 #include <sys/syslog.h>
49
50 #include <machine/autoconf.h>
51 #include <machine/openfirm.h>
52 #include <machine/bsd_openprom.h>
53 #include <machine/cpu.h>
54 #include <machine/eeprom.h>
55 #include <machine/psl.h>
56 #include <machine/z8530var.h>
57 #include <machine/sparc64.h>
58
59 #include <dev/cons.h>
60
61 #include <sparc64/dev/cons.h>
62
63 #include <dev/usb/ukbdvar.h>
64
65 static void prom_cnprobe __P((struct consdev *));
66 static void prom_cninit __P((struct consdev *));
67 int prom_cngetc __P((dev_t));
68 static void prom_cnputc __P((dev_t, int));
69 static void prom_cnpollc __P((dev_t, int));
70 static void prom_cnputc __P((dev_t, int));
71
72 /*
73 * The console is set to this one initially,
74 * which lets us use the PROM until consinit()
75 * is called to select a real console.
76 */
77 struct consdev consdev_prom = {
78 prom_cnprobe,
79 prom_cninit,
80 prom_cngetc,
81 prom_cnputc,
82 prom_cnpollc,
83 NULL,
84 };
85
86 /*
87 * The console table pointer is statically initialized
88 * to point to the PROM (output only) table, so that
89 * early calls to printf will work.
90 */
91 struct consdev *cn_tab = &consdev_prom;
92
93 void
94 prom_cnprobe(cd)
95 struct consdev *cd;
96 {
97 #if NPCONS > 0
98 int maj;
99 extern const struct cdevsw pcons_cdevsw;
100
101 maj = cdevsw_lookup_major(&pcons_cdevsw);
102 cd->cn_dev = makedev(maj, 0);
103 cd->cn_pri = CN_INTERNAL;
104 #endif
105 }
106
107 int
108 prom_cngetc(dev)
109 dev_t dev;
110 {
111 unsigned char ch = '\0';
112 int l;
113 #ifdef DDB
114 static int nplus = 0;
115 #endif
116
117 while ((l = prom_read(prom_stdin(), &ch, 1)) != 1)
118 /* void */;
119 #ifdef DDB
120 if (ch == '+') {
121 if (nplus++ > 3) Debugger();
122 } else nplus = 0;
123 #endif
124 if (ch == '\r')
125 ch = '\n';
126 return ch;
127 }
128
129 static void
130 prom_cninit(cn)
131 struct consdev *cn;
132 {
133 }
134
135 /*
136 * PROM console output putchar.
137 */
138 static void
139 prom_cnputc(dev, c)
140 dev_t dev;
141 int c;
142 {
143 int s;
144 char c0 = (c & 0x7f);
145
146 s = splhigh();
147 prom_write(prom_stdout(), &c0, 1);
148 splx(s);
149 }
150
151 void
152 prom_cnpollc(dev, on)
153 dev_t dev;
154 int on;
155 {
156 if (on) {
157 /* Entering debugger. */
158 #if NFB > 0
159 fb_unblank();
160 #endif
161 } else {
162 /* Resuming kernel. */
163 }
164 #if NPCONS > 0
165 pcons_cnpollc(dev, on);
166 #endif
167 }
168
169 /*****************************************************************/
170
171 #ifdef DEBUG
172 #define DBPRINT(x) prom_printf x
173 #else
174 #define DBPRINT(x)
175 #endif
176
177 int prom_stdin_node;
178 int prom_stdout_node;
179
180 /*
181 * This function replaces sys/dev/cninit.c
182 * Determine which device is the console using
183 * the PROM "input source" and "output sink".
184 */
185 void
186 consinit()
187 {
188 int chosen;
189 char buffer[128];
190 const char *consname = "unknown";
191
192 DBPRINT(("consinit()\r\n"));
193
194 if (cn_tab != &consdev_prom)
195 return;
196
197 chosen = prom_finddevice("/chosen");
198
199 if ((prom_stdin_node = prom_instance_to_package(prom_stdin())) == 0) {
200 printf("WARNING: no PROM stdin\n");
201 }
202 DBPRINT(("stdin node = %x\r\n", prom_stdin_node));
203
204 if ((prom_stdout_node = prom_instance_to_package(prom_stdout())) == 0)
205 printf("WARNING: no PROM stdout\n");
206 DBPRINT(("stdout package = %x\r\n", prom_stdout_node));
207
208 DBPRINT(("buffer @ %p\r\n", buffer));
209
210 if (prom_stdin_node != 0 &&
211 (prom_getproplen(prom_stdin_node, "keyboard") >= 0)) {
212 #if NUKBD > 0
213 if ((OF_instance_to_path(prom_stdin(), buffer, sizeof(buffer)) >= 0) &&
214 (strstr(buffer, "/usb@") != NULL)) {
215 /*
216 * If we have a USB keyboard, it will show up as (e.g.)
217 * /pci@1f,0/usb@c,3/keyboard@1 (Blade 100)
218 */
219 consname = "usb-keyboard/display";
220 ukbd_cnattach();
221 } else
222 #endif
223 consname = "sun-keyboard/display";
224 } else if (prom_stdin_node != 0 &&
225 (OF_instance_to_path(prom_stdin(), buffer, sizeof(buffer)) >= 0)) {
226 consname = buffer;
227 }
228 printf("console is %s\n", consname);
229
230 /* Initialize PROM console */
231 (*cn_tab->cn_probe)(cn_tab);
232 (*cn_tab->cn_init)(cn_tab);
233 }
234
235