kd.c revision 1.20.4.2 1 1.20.4.2 nathanw /* $NetBSD: kd.c,v 1.20.4.2 2002/04/01 07:42:42 nathanw Exp $ */
2 1.20.4.2 nathanw
3 1.20.4.2 nathanw /*-
4 1.20.4.2 nathanw * Copyright (c) 1996 The NetBSD Foundation, Inc.
5 1.20.4.2 nathanw * All rights reserved.
6 1.20.4.2 nathanw *
7 1.20.4.2 nathanw * This code is derived from software contributed to The NetBSD Foundation
8 1.20.4.2 nathanw * by Gordon W. Ross.
9 1.20.4.2 nathanw *
10 1.20.4.2 nathanw * Redistribution and use in source and binary forms, with or without
11 1.20.4.2 nathanw * modification, are permitted provided that the following conditions
12 1.20.4.2 nathanw * are met:
13 1.20.4.2 nathanw * 1. Redistributions of source code must retain the above copyright
14 1.20.4.2 nathanw * notice, this list of conditions and the following disclaimer.
15 1.20.4.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
16 1.20.4.2 nathanw * notice, this list of conditions and the following disclaimer in the
17 1.20.4.2 nathanw * documentation and/or other materials provided with the distribution.
18 1.20.4.2 nathanw * 3. All advertising materials mentioning features or use of this software
19 1.20.4.2 nathanw * must display the following acknowledgement:
20 1.20.4.2 nathanw * This product includes software developed by the NetBSD
21 1.20.4.2 nathanw * Foundation, Inc. and its contributors.
22 1.20.4.2 nathanw * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.20.4.2 nathanw * contributors may be used to endorse or promote products derived
24 1.20.4.2 nathanw * from this software without specific prior written permission.
25 1.20.4.2 nathanw *
26 1.20.4.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.20.4.2 nathanw * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.20.4.2 nathanw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.20.4.2 nathanw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.20.4.2 nathanw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.20.4.2 nathanw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.20.4.2 nathanw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.20.4.2 nathanw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.20.4.2 nathanw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.20.4.2 nathanw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.20.4.2 nathanw * POSSIBILITY OF SUCH DAMAGE.
37 1.20.4.2 nathanw */
38 1.20.4.2 nathanw
39 1.20.4.2 nathanw /*
40 1.20.4.2 nathanw * Console driver based on PROM primitives.
41 1.20.4.2 nathanw *
42 1.20.4.2 nathanw * This driver exists to provide a tty device that the indirect
43 1.20.4.2 nathanw * console driver can point to. It also provides a hook that
44 1.20.4.2 nathanw * allows another device to serve console input. This will normally
45 1.20.4.2 nathanw * be a keyboard driver (see sys/dev/sun/kbd.c)
46 1.20.4.2 nathanw */
47 1.20.4.2 nathanw
48 1.20.4.2 nathanw #include "opt_kgdb.h"
49 1.20.4.2 nathanw #include "fb.h"
50 1.20.4.2 nathanw
51 1.20.4.2 nathanw #include <sys/param.h>
52 1.20.4.2 nathanw #include <sys/proc.h>
53 1.20.4.2 nathanw #include <sys/systm.h>
54 1.20.4.2 nathanw #include <sys/kernel.h>
55 1.20.4.2 nathanw #include <sys/ioctl.h>
56 1.20.4.2 nathanw #include <sys/tty.h>
57 1.20.4.2 nathanw #include <sys/file.h>
58 1.20.4.2 nathanw #include <sys/conf.h>
59 1.20.4.2 nathanw #include <sys/device.h>
60 1.20.4.2 nathanw
61 1.20.4.2 nathanw #include <machine/bsd_openprom.h>
62 1.20.4.2 nathanw #include <machine/promlib.h>
63 1.20.4.2 nathanw #include <machine/eeprom.h>
64 1.20.4.2 nathanw #include <machine/psl.h>
65 1.20.4.2 nathanw #include <machine/cpu.h>
66 1.20.4.2 nathanw #include <machine/kbd.h>
67 1.20.4.2 nathanw #include <machine/autoconf.h>
68 1.20.4.2 nathanw #include <machine/conf.h>
69 1.20.4.2 nathanw
70 1.20.4.2 nathanw #if defined(RASTERCONSOLE) && NFB > 0
71 1.20.4.2 nathanw #include <dev/sun/fbio.h>
72 1.20.4.2 nathanw #include <dev/sun/fbvar.h>
73 1.20.4.2 nathanw #endif
74 1.20.4.2 nathanw
75 1.20.4.2 nathanw #include <dev/cons.h>
76 1.20.4.2 nathanw #include <sparc/dev/cons.h>
77 1.20.4.2 nathanw
78 1.20.4.2 nathanw #include <dev/sun/event_var.h>
79 1.20.4.2 nathanw #include <dev/sun/kbd_xlate.h>
80 1.20.4.2 nathanw #include <dev/sun/kbdvar.h>
81 1.20.4.2 nathanw
82 1.20.4.2 nathanw #define KDMAJOR 1
83 1.20.4.2 nathanw #define PUT_WSIZE 64
84 1.20.4.2 nathanw
85 1.20.4.2 nathanw struct kd_softc {
86 1.20.4.2 nathanw struct device kd_dev; /* required first: base device */
87 1.20.4.2 nathanw struct tty *kd_tty;
88 1.20.4.2 nathanw int rows, cols;
89 1.20.4.2 nathanw
90 1.20.4.2 nathanw /* Console input hook */
91 1.20.4.2 nathanw struct cons_channel *kd_in;
92 1.20.4.2 nathanw };
93 1.20.4.2 nathanw
94 1.20.4.2 nathanw /*
95 1.20.4.2 nathanw * There is no point in pretending there might be
96 1.20.4.2 nathanw * more than one keyboard/display device.
97 1.20.4.2 nathanw */
98 1.20.4.2 nathanw static struct kd_softc kd_softc;
99 1.20.4.2 nathanw
100 1.20.4.2 nathanw static int kdparam(struct tty *, struct termios *);
101 1.20.4.2 nathanw static void kdstart(struct tty *);
102 1.20.4.2 nathanw static void kd_init __P((struct kd_softc *));
103 1.20.4.2 nathanw static void kd_cons_input __P((int));
104 1.20.4.2 nathanw
105 1.20.4.2 nathanw /*
106 1.20.4.2 nathanw * Prepare the console tty; called on first open of /dev/console
107 1.20.4.2 nathanw */
108 1.20.4.2 nathanw void
109 1.20.4.2 nathanw kd_init(kd)
110 1.20.4.2 nathanw struct kd_softc *kd;
111 1.20.4.2 nathanw {
112 1.20.4.2 nathanw struct tty *tp;
113 1.20.4.2 nathanw
114 1.20.4.2 nathanw tp = ttymalloc();
115 1.20.4.2 nathanw tp->t_oproc = kdstart;
116 1.20.4.2 nathanw tp->t_param = kdparam;
117 1.20.4.2 nathanw tp->t_dev = makedev(KDMAJOR, 0);
118 1.20.4.2 nathanw
119 1.20.4.2 nathanw tty_attach(tp);
120 1.20.4.2 nathanw kd->kd_tty = tp;
121 1.20.4.2 nathanw
122 1.20.4.2 nathanw /*
123 1.20.4.2 nathanw * Get the console struct winsize.
124 1.20.4.2 nathanw */
125 1.20.4.2 nathanw #if defined(RASTERCONSOLE) && NFB > 0
126 1.20.4.2 nathanw /* If the raster console driver is attached, copy its size */
127 1.20.4.2 nathanw kd->rows = fbrcons_rows();
128 1.20.4.2 nathanw kd->cols = fbrcons_cols();
129 1.20.4.2 nathanw rcons_ttyinit(tp);
130 1.20.4.2 nathanw #endif
131 1.20.4.2 nathanw
132 1.20.4.2 nathanw /* else, consult the PROM */
133 1.20.4.2 nathanw switch (prom_version()) {
134 1.20.4.2 nathanw char *prop;
135 1.20.4.2 nathanw struct eeprom *ep;
136 1.20.4.2 nathanw case PROM_OLDMON:
137 1.20.4.2 nathanw if ((ep = (struct eeprom *)eeprom_va) == NULL)
138 1.20.4.2 nathanw break;
139 1.20.4.2 nathanw if (kd->rows == 0)
140 1.20.4.2 nathanw kd->rows = (u_short)ep->eeTtyRows;
141 1.20.4.2 nathanw if (kd->cols == 0)
142 1.20.4.2 nathanw kd->cols = (u_short)ep->eeTtyCols;
143 1.20.4.2 nathanw break;
144 1.20.4.2 nathanw case PROM_OBP_V0:
145 1.20.4.2 nathanw case PROM_OBP_V2:
146 1.20.4.2 nathanw case PROM_OBP_V3:
147 1.20.4.2 nathanw case PROM_OPENFIRM:
148 1.20.4.2 nathanw
149 1.20.4.2 nathanw if (kd->rows == 0 &&
150 1.20.4.2 nathanw (prop = PROM_getpropstring(optionsnode, "screen-#rows"))) {
151 1.20.4.2 nathanw int i = 0;
152 1.20.4.2 nathanw
153 1.20.4.2 nathanw while (*prop != '\0')
154 1.20.4.2 nathanw i = i * 10 + *prop++ - '0';
155 1.20.4.2 nathanw kd->rows = (unsigned short)i;
156 1.20.4.2 nathanw }
157 1.20.4.2 nathanw if (kd->cols == 0 &&
158 1.20.4.2 nathanw (prop = PROM_getpropstring(optionsnode, "screen-#columns"))) {
159 1.20.4.2 nathanw int i = 0;
160 1.20.4.2 nathanw
161 1.20.4.2 nathanw while (*prop != '\0')
162 1.20.4.2 nathanw i = i * 10 + *prop++ - '0';
163 1.20.4.2 nathanw kd->cols = (unsigned short)i;
164 1.20.4.2 nathanw }
165 1.20.4.2 nathanw break;
166 1.20.4.2 nathanw }
167 1.20.4.2 nathanw
168 1.20.4.2 nathanw return;
169 1.20.4.2 nathanw }
170 1.20.4.2 nathanw
171 1.20.4.2 nathanw struct tty *
172 1.20.4.2 nathanw kdtty(dev)
173 1.20.4.2 nathanw dev_t dev;
174 1.20.4.2 nathanw {
175 1.20.4.2 nathanw struct kd_softc *kd;
176 1.20.4.2 nathanw
177 1.20.4.2 nathanw kd = &kd_softc; /* XXX */
178 1.20.4.2 nathanw return (kd->kd_tty);
179 1.20.4.2 nathanw }
180 1.20.4.2 nathanw
181 1.20.4.2 nathanw int
182 1.20.4.2 nathanw kdopen(dev, flag, mode, p)
183 1.20.4.2 nathanw dev_t dev;
184 1.20.4.2 nathanw int flag, mode;
185 1.20.4.2 nathanw struct proc *p;
186 1.20.4.2 nathanw {
187 1.20.4.2 nathanw struct kd_softc *kd;
188 1.20.4.2 nathanw int error, s, unit;
189 1.20.4.2 nathanw struct tty *tp;
190 1.20.4.2 nathanw static int firstopen = 1;
191 1.20.4.2 nathanw
192 1.20.4.2 nathanw unit = minor(dev);
193 1.20.4.2 nathanw if (unit != 0)
194 1.20.4.2 nathanw return ENXIO;
195 1.20.4.2 nathanw
196 1.20.4.2 nathanw kd = &kd_softc; /* XXX */
197 1.20.4.2 nathanw if (firstopen) {
198 1.20.4.2 nathanw kd_init(kd);
199 1.20.4.2 nathanw firstopen = 0;
200 1.20.4.2 nathanw }
201 1.20.4.2 nathanw
202 1.20.4.2 nathanw tp = kd->kd_tty;
203 1.20.4.2 nathanw
204 1.20.4.2 nathanw /* It's simpler to do this up here. */
205 1.20.4.2 nathanw if (((tp->t_state & (TS_ISOPEN | TS_XCLUDE))
206 1.20.4.2 nathanw == (TS_ISOPEN | TS_XCLUDE))
207 1.20.4.2 nathanw && (p->p_ucred->cr_uid != 0) )
208 1.20.4.2 nathanw {
209 1.20.4.2 nathanw return (EBUSY);
210 1.20.4.2 nathanw }
211 1.20.4.2 nathanw
212 1.20.4.2 nathanw s = spltty();
213 1.20.4.2 nathanw if ((tp->t_state & TS_ISOPEN) == 0) {
214 1.20.4.2 nathanw /* First open. */
215 1.20.4.2 nathanw
216 1.20.4.2 nathanw /* Notify the input device that serves us */
217 1.20.4.2 nathanw struct cons_channel *cc = kd->kd_in;
218 1.20.4.2 nathanw if (cc != NULL &&
219 1.20.4.2 nathanw (error = (*cc->cc_iopen)(cc)) != 0) {
220 1.20.4.2 nathanw return (error);
221 1.20.4.2 nathanw }
222 1.20.4.2 nathanw
223 1.20.4.2 nathanw ttychars(tp);
224 1.20.4.2 nathanw tp->t_iflag = TTYDEF_IFLAG;
225 1.20.4.2 nathanw tp->t_oflag = TTYDEF_OFLAG;
226 1.20.4.2 nathanw tp->t_cflag = TTYDEF_CFLAG;
227 1.20.4.2 nathanw tp->t_lflag = TTYDEF_LFLAG;
228 1.20.4.2 nathanw tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
229 1.20.4.2 nathanw (void) kdparam(tp, &tp->t_termios);
230 1.20.4.2 nathanw ttsetwater(tp);
231 1.20.4.2 nathanw tp->t_winsize.ws_row = kd->rows;
232 1.20.4.2 nathanw tp->t_winsize.ws_col = kd->cols;
233 1.20.4.2 nathanw /* Flush pending input? Clear translator? */
234 1.20.4.2 nathanw /* This (pseudo)device always has SOFTCAR */
235 1.20.4.2 nathanw tp->t_state |= TS_CARR_ON;
236 1.20.4.2 nathanw }
237 1.20.4.2 nathanw
238 1.20.4.2 nathanw splx(s);
239 1.20.4.2 nathanw
240 1.20.4.2 nathanw return ((*tp->t_linesw->l_open)(dev, tp));
241 1.20.4.2 nathanw }
242 1.20.4.2 nathanw
243 1.20.4.2 nathanw int
244 1.20.4.2 nathanw kdclose(dev, flag, mode, p)
245 1.20.4.2 nathanw dev_t dev;
246 1.20.4.2 nathanw int flag, mode;
247 1.20.4.2 nathanw struct proc *p;
248 1.20.4.2 nathanw {
249 1.20.4.2 nathanw struct kd_softc *kd;
250 1.20.4.2 nathanw struct tty *tp;
251 1.20.4.2 nathanw struct cons_channel *cc;
252 1.20.4.2 nathanw
253 1.20.4.2 nathanw kd = &kd_softc; /* XXX */
254 1.20.4.2 nathanw tp = kd->kd_tty;
255 1.20.4.2 nathanw
256 1.20.4.2 nathanw /* XXX This is for cons.c. */
257 1.20.4.2 nathanw if ((tp->t_state & TS_ISOPEN) == 0)
258 1.20.4.2 nathanw return 0;
259 1.20.4.2 nathanw
260 1.20.4.2 nathanw (*tp->t_linesw->l_close)(tp, flag);
261 1.20.4.2 nathanw ttyclose(tp);
262 1.20.4.2 nathanw
263 1.20.4.2 nathanw if ((cc = kd->kd_in) != NULL)
264 1.20.4.2 nathanw (void)(*cc->cc_iclose)(cc);
265 1.20.4.2 nathanw
266 1.20.4.2 nathanw return (0);
267 1.20.4.2 nathanw }
268 1.20.4.2 nathanw
269 1.20.4.2 nathanw int
270 1.20.4.2 nathanw kdread(dev, uio, flag)
271 1.20.4.2 nathanw dev_t dev;
272 1.20.4.2 nathanw struct uio *uio;
273 1.20.4.2 nathanw int flag;
274 1.20.4.2 nathanw {
275 1.20.4.2 nathanw struct kd_softc *kd;
276 1.20.4.2 nathanw struct tty *tp;
277 1.20.4.2 nathanw
278 1.20.4.2 nathanw kd = &kd_softc; /* XXX */
279 1.20.4.2 nathanw tp = kd->kd_tty;
280 1.20.4.2 nathanw
281 1.20.4.2 nathanw return ((*tp->t_linesw->l_read)(tp, uio, flag));
282 1.20.4.2 nathanw }
283 1.20.4.2 nathanw
284 1.20.4.2 nathanw int
285 1.20.4.2 nathanw kdwrite(dev, uio, flag)
286 1.20.4.2 nathanw dev_t dev;
287 1.20.4.2 nathanw struct uio *uio;
288 1.20.4.2 nathanw int flag;
289 1.20.4.2 nathanw {
290 1.20.4.2 nathanw struct kd_softc *kd;
291 1.20.4.2 nathanw struct tty *tp;
292 1.20.4.2 nathanw
293 1.20.4.2 nathanw kd = &kd_softc; /* XXX */
294 1.20.4.2 nathanw tp = kd->kd_tty;
295 1.20.4.2 nathanw
296 1.20.4.2 nathanw return ((*tp->t_linesw->l_write)(tp, uio, flag));
297 1.20.4.2 nathanw }
298 1.20.4.2 nathanw
299 1.20.4.2 nathanw int
300 1.20.4.2 nathanw kdpoll(dev, events, p)
301 1.20.4.2 nathanw dev_t dev;
302 1.20.4.2 nathanw int events;
303 1.20.4.2 nathanw struct proc *p;
304 1.20.4.2 nathanw {
305 1.20.4.2 nathanw struct kd_softc *kd;
306 1.20.4.2 nathanw struct tty *tp;
307 1.20.4.2 nathanw
308 1.20.4.2 nathanw kd = &kd_softc; /* XXX */
309 1.20.4.2 nathanw tp = kd->kd_tty;
310 1.20.4.2 nathanw
311 1.20.4.2 nathanw return ((*tp->t_linesw->l_poll)(tp, events, p));
312 1.20.4.2 nathanw }
313 1.20.4.2 nathanw
314 1.20.4.2 nathanw int
315 1.20.4.2 nathanw kdioctl(dev, cmd, data, flag, p)
316 1.20.4.2 nathanw dev_t dev;
317 1.20.4.2 nathanw u_long cmd;
318 1.20.4.2 nathanw caddr_t data;
319 1.20.4.2 nathanw int flag;
320 1.20.4.2 nathanw struct proc *p;
321 1.20.4.2 nathanw {
322 1.20.4.2 nathanw struct kd_softc *kd;
323 1.20.4.2 nathanw struct tty *tp;
324 1.20.4.2 nathanw int error;
325 1.20.4.2 nathanw
326 1.20.4.2 nathanw kd = &kd_softc; /* XXX */
327 1.20.4.2 nathanw tp = kd->kd_tty;
328 1.20.4.2 nathanw
329 1.20.4.2 nathanw error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
330 1.20.4.2 nathanw if (error != EPASSTHROUGH)
331 1.20.4.2 nathanw return error;
332 1.20.4.2 nathanw
333 1.20.4.2 nathanw error = ttioctl(tp, cmd, data, flag, p);
334 1.20.4.2 nathanw if (error != EPASSTHROUGH)
335 1.20.4.2 nathanw return error;
336 1.20.4.2 nathanw
337 1.20.4.2 nathanw /* Handle any ioctl commands specific to kbd/display. */
338 1.20.4.2 nathanw /* XXX - Send KB* ioctls to kbd module? */
339 1.20.4.2 nathanw /* XXX - Send FB* ioctls to fb module? */
340 1.20.4.2 nathanw
341 1.20.4.2 nathanw return EPASSTHROUGH;
342 1.20.4.2 nathanw }
343 1.20.4.2 nathanw
344 1.20.4.2 nathanw void
345 1.20.4.2 nathanw kdstop(tp, flag)
346 1.20.4.2 nathanw struct tty *tp;
347 1.20.4.2 nathanw int flag;
348 1.20.4.2 nathanw {
349 1.20.4.2 nathanw
350 1.20.4.2 nathanw }
351 1.20.4.2 nathanw
352 1.20.4.2 nathanw
353 1.20.4.2 nathanw static int
354 1.20.4.2 nathanw kdparam(tp, t)
355 1.20.4.2 nathanw struct tty *tp;
356 1.20.4.2 nathanw struct termios *t;
357 1.20.4.2 nathanw {
358 1.20.4.2 nathanw /* XXX - These are ignored... */
359 1.20.4.2 nathanw tp->t_ispeed = t->c_ispeed;
360 1.20.4.2 nathanw tp->t_ospeed = t->c_ospeed;
361 1.20.4.2 nathanw tp->t_cflag = t->c_cflag;
362 1.20.4.2 nathanw return 0;
363 1.20.4.2 nathanw }
364 1.20.4.2 nathanw
365 1.20.4.2 nathanw
366 1.20.4.2 nathanw static void kd_later(void*);
367 1.20.4.2 nathanw static void kd_putfb(struct tty *);
368 1.20.4.2 nathanw
369 1.20.4.2 nathanw static void
370 1.20.4.2 nathanw kdstart(tp)
371 1.20.4.2 nathanw struct tty *tp;
372 1.20.4.2 nathanw {
373 1.20.4.2 nathanw struct clist *cl;
374 1.20.4.2 nathanw int s;
375 1.20.4.2 nathanw
376 1.20.4.2 nathanw s = spltty();
377 1.20.4.2 nathanw if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
378 1.20.4.2 nathanw goto out;
379 1.20.4.2 nathanw
380 1.20.4.2 nathanw cl = &tp->t_outq;
381 1.20.4.2 nathanw if (cl->c_cc) {
382 1.20.4.2 nathanw tp->t_state |= TS_BUSY;
383 1.20.4.2 nathanw if ((s & PSR_PIL) == 0) {
384 1.20.4.2 nathanw /* called at level zero - update screen now. */
385 1.20.4.2 nathanw (void) spllowersoftclock();
386 1.20.4.2 nathanw kd_putfb(tp);
387 1.20.4.2 nathanw (void) spltty();
388 1.20.4.2 nathanw tp->t_state &= ~TS_BUSY;
389 1.20.4.2 nathanw } else {
390 1.20.4.2 nathanw /* called at interrupt level - do it later */
391 1.20.4.2 nathanw callout_reset(&tp->t_rstrt_ch, 0, kd_later, tp);
392 1.20.4.2 nathanw }
393 1.20.4.2 nathanw }
394 1.20.4.2 nathanw if (cl->c_cc <= tp->t_lowat) {
395 1.20.4.2 nathanw if (tp->t_state & TS_ASLEEP) {
396 1.20.4.2 nathanw tp->t_state &= ~TS_ASLEEP;
397 1.20.4.2 nathanw wakeup((caddr_t)cl);
398 1.20.4.2 nathanw }
399 1.20.4.2 nathanw selwakeup(&tp->t_wsel);
400 1.20.4.2 nathanw }
401 1.20.4.2 nathanw out:
402 1.20.4.2 nathanw splx(s);
403 1.20.4.2 nathanw }
404 1.20.4.2 nathanw
405 1.20.4.2 nathanw /*
406 1.20.4.2 nathanw * Timeout function to do delayed writes to the screen.
407 1.20.4.2 nathanw * Called at splsoftclock when requested by kdstart.
408 1.20.4.2 nathanw */
409 1.20.4.2 nathanw static void
410 1.20.4.2 nathanw kd_later(arg)
411 1.20.4.2 nathanw void *arg;
412 1.20.4.2 nathanw {
413 1.20.4.2 nathanw struct tty *tp = arg;
414 1.20.4.2 nathanw int s;
415 1.20.4.2 nathanw
416 1.20.4.2 nathanw kd_putfb(tp);
417 1.20.4.2 nathanw
418 1.20.4.2 nathanw s = spltty();
419 1.20.4.2 nathanw tp->t_state &= ~TS_BUSY;
420 1.20.4.2 nathanw (*tp->t_linesw->l_start)(tp);
421 1.20.4.2 nathanw splx(s);
422 1.20.4.2 nathanw }
423 1.20.4.2 nathanw
424 1.20.4.2 nathanw /*
425 1.20.4.2 nathanw * Put text on the screen using the PROM monitor.
426 1.20.4.2 nathanw * This can take a while, so to avoid missing
427 1.20.4.2 nathanw * interrupts, this is called at splsoftclock.
428 1.20.4.2 nathanw */
429 1.20.4.2 nathanw static void
430 1.20.4.2 nathanw kd_putfb(tp)
431 1.20.4.2 nathanw struct tty *tp;
432 1.20.4.2 nathanw {
433 1.20.4.2 nathanw char buf[PUT_WSIZE];
434 1.20.4.2 nathanw struct clist *cl = &tp->t_outq;
435 1.20.4.2 nathanw char *p, *end;
436 1.20.4.2 nathanw int len;
437 1.20.4.2 nathanw
438 1.20.4.2 nathanw while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) {
439 1.20.4.2 nathanw /* PROM will barf if high bits are set. */
440 1.20.4.2 nathanw p = buf;
441 1.20.4.2 nathanw end = buf + len;
442 1.20.4.2 nathanw while (p < end)
443 1.20.4.2 nathanw *p++ &= 0x7f;
444 1.20.4.2 nathanw
445 1.20.4.2 nathanw /* Now let the PROM print it. */
446 1.20.4.2 nathanw prom_putstr(buf, len);
447 1.20.4.2 nathanw }
448 1.20.4.2 nathanw }
449 1.20.4.2 nathanw
450 1.20.4.2 nathanw /*
451 1.20.4.2 nathanw * Our "interrupt" routine for input. This is called by
452 1.20.4.2 nathanw * the keyboard driver (dev/sun/kbd.c) at spltty.
453 1.20.4.2 nathanw */
454 1.20.4.2 nathanw void
455 1.20.4.2 nathanw kd_cons_input(c)
456 1.20.4.2 nathanw int c;
457 1.20.4.2 nathanw {
458 1.20.4.2 nathanw struct kd_softc *kd = &kd_softc;
459 1.20.4.2 nathanw struct tty *tp;
460 1.20.4.2 nathanw
461 1.20.4.2 nathanw /* XXX: Make sure the device is open. */
462 1.20.4.2 nathanw tp = kd->kd_tty;
463 1.20.4.2 nathanw if (tp == NULL)
464 1.20.4.2 nathanw return;
465 1.20.4.2 nathanw if ((tp->t_state & TS_ISOPEN) == 0)
466 1.20.4.2 nathanw return;
467 1.20.4.2 nathanw
468 1.20.4.2 nathanw (*tp->t_linesw->l_rint)(c, tp);
469 1.20.4.2 nathanw }
470 1.20.4.2 nathanw
471 1.20.4.2 nathanw void
472 1.20.4.2 nathanw cons_attach_input(cc, cn)
473 1.20.4.2 nathanw struct cons_channel *cc;
474 1.20.4.2 nathanw struct consdev *cn;
475 1.20.4.2 nathanw {
476 1.20.4.2 nathanw struct kd_softc *kd = &kd_softc;
477 1.20.4.2 nathanw
478 1.20.4.2 nathanw kd->kd_in = cc;
479 1.20.4.2 nathanw cc->cc_upstream = kd_cons_input;
480 1.20.4.2 nathanw }
481 1.20.4.2 nathanw
482 1.20.4.2 nathanw void kd_attach_input(struct cons_channel *);
483 1.20.4.2 nathanw void
484 1.20.4.2 nathanw kd_attach_input(cc)
485 1.20.4.2 nathanw struct cons_channel *cc;
486 1.20.4.2 nathanw {
487 1.20.4.2 nathanw struct kd_softc *kd = &kd_softc;
488 1.20.4.2 nathanw
489 1.20.4.2 nathanw kd->kd_in = cc;
490 1.20.4.2 nathanw cc->cc_upstream = kd_cons_input;
491 1.20.4.2 nathanw }
492 1.20.4.2 nathanw
493 1.20.4.2 nathanw /*
494 1.20.4.2 nathanw * Default PROM-based console input stream
495 1.20.4.2 nathanw * Since the PROM does not notify us when data is available on the
496 1.20.4.2 nathanw * input channel these functions periodically poll the PROM.
497 1.20.4.2 nathanw */
498 1.20.4.2 nathanw static int kd_rom_iopen __P((struct cons_channel *));
499 1.20.4.2 nathanw static int kd_rom_iclose __P((struct cons_channel *));
500 1.20.4.2 nathanw static void kd_rom_intr __P((void *));
501 1.20.4.2 nathanw
502 1.20.4.2 nathanw static struct cons_channel prom_cons_channel;
503 1.20.4.2 nathanw
504 1.20.4.2 nathanw int
505 1.20.4.2 nathanw kd_rom_iopen(cc)
506 1.20.4.2 nathanw struct cons_channel *cc;
507 1.20.4.2 nathanw {
508 1.20.4.2 nathanw /* Poll for ROM input 4 times per second */
509 1.20.4.2 nathanw callout_reset(&cc->cc_callout, hz / 4, kd_rom_intr, cc);
510 1.20.4.2 nathanw return (0);
511 1.20.4.2 nathanw }
512 1.20.4.2 nathanw
513 1.20.4.2 nathanw int
514 1.20.4.2 nathanw kd_rom_iclose(cc)
515 1.20.4.2 nathanw struct cons_channel *cc;
516 1.20.4.2 nathanw {
517 1.20.4.2 nathanw
518 1.20.4.2 nathanw callout_stop(&cc->cc_callout);
519 1.20.4.2 nathanw return (0);
520 1.20.4.2 nathanw }
521 1.20.4.2 nathanw
522 1.20.4.2 nathanw /*
523 1.20.4.2 nathanw * "Interrupt" routine for input through ROM vectors
524 1.20.4.2 nathanw */
525 1.20.4.2 nathanw void
526 1.20.4.2 nathanw kd_rom_intr(arg)
527 1.20.4.2 nathanw void *arg;
528 1.20.4.2 nathanw {
529 1.20.4.2 nathanw struct cons_channel *cc = arg;
530 1.20.4.2 nathanw int s, c;
531 1.20.4.2 nathanw
532 1.20.4.2 nathanw /* Re-schedule */
533 1.20.4.2 nathanw callout_reset(&cc->cc_callout, hz / 4, kd_rom_intr, cc);
534 1.20.4.2 nathanw
535 1.20.4.2 nathanw s = spltty();
536 1.20.4.2 nathanw
537 1.20.4.2 nathanw while ((c = prom_peekchar()) >= 0)
538 1.20.4.2 nathanw (*cc->cc_upstream)(c);
539 1.20.4.2 nathanw
540 1.20.4.2 nathanw splx(s);
541 1.20.4.2 nathanw }
542 1.20.4.2 nathanw
543 1.20.4.2 nathanw /*****************************************************************/
544 1.20.4.2 nathanw
545 1.20.4.2 nathanw int prom_stdin_node;
546 1.20.4.2 nathanw int prom_stdout_node;
547 1.20.4.2 nathanw char prom_stdin_args[16];
548 1.20.4.2 nathanw char prom_stdout_args[16];
549 1.20.4.2 nathanw
550 1.20.4.2 nathanw extern void prom_cnprobe __P((struct consdev *));
551 1.20.4.2 nathanw static void prom_cninit __P((struct consdev *));
552 1.20.4.2 nathanw static int prom_cngetc __P((dev_t));
553 1.20.4.2 nathanw static void prom_cnputc __P((dev_t, int));
554 1.20.4.2 nathanw extern void prom_cnpollc __P((dev_t, int));
555 1.20.4.2 nathanw
556 1.20.4.2 nathanw /*
557 1.20.4.2 nathanw * The console is set to this one initially,
558 1.20.4.2 nathanw * which lets us use the PROM until consinit()
559 1.20.4.2 nathanw * is called to select a real console.
560 1.20.4.2 nathanw */
561 1.20.4.2 nathanw struct consdev consdev_prom = {
562 1.20.4.2 nathanw prom_cnprobe,
563 1.20.4.2 nathanw prom_cninit,
564 1.20.4.2 nathanw prom_cngetc,
565 1.20.4.2 nathanw prom_cnputc,
566 1.20.4.2 nathanw prom_cnpollc,
567 1.20.4.2 nathanw NULL,
568 1.20.4.2 nathanw };
569 1.20.4.2 nathanw
570 1.20.4.2 nathanw /*
571 1.20.4.2 nathanw * The console table pointer is statically initialized
572 1.20.4.2 nathanw * to point to the PROM table, so that early calls to printf will work.
573 1.20.4.2 nathanw */
574 1.20.4.2 nathanw struct consdev *cn_tab = &consdev_prom;
575 1.20.4.2 nathanw
576 1.20.4.2 nathanw void
577 1.20.4.2 nathanw prom_cnprobe(cn)
578 1.20.4.2 nathanw struct consdev *cn;
579 1.20.4.2 nathanw {
580 1.20.4.2 nathanw }
581 1.20.4.2 nathanw
582 1.20.4.2 nathanw static void
583 1.20.4.2 nathanw prom_cninit(cn)
584 1.20.4.2 nathanw struct consdev *cn;
585 1.20.4.2 nathanw {
586 1.20.4.2 nathanw }
587 1.20.4.2 nathanw
588 1.20.4.2 nathanw void
589 1.20.4.2 nathanw prom_cnpollc(dev, on)
590 1.20.4.2 nathanw dev_t dev;
591 1.20.4.2 nathanw int on;
592 1.20.4.2 nathanw {
593 1.20.4.2 nathanw
594 1.20.4.2 nathanw if (on) {
595 1.20.4.2 nathanw /* Entering debugger. */
596 1.20.4.2 nathanw #if NFB > 0
597 1.20.4.2 nathanw fb_unblank();
598 1.20.4.2 nathanw #endif
599 1.20.4.2 nathanw } else {
600 1.20.4.2 nathanw /* Resuming kernel. */
601 1.20.4.2 nathanw }
602 1.20.4.2 nathanw }
603 1.20.4.2 nathanw
604 1.20.4.2 nathanw
605 1.20.4.2 nathanw /*
606 1.20.4.2 nathanw * PROM console input putchar.
607 1.20.4.2 nathanw */
608 1.20.4.2 nathanw static int
609 1.20.4.2 nathanw prom_cngetc(dev)
610 1.20.4.2 nathanw dev_t dev;
611 1.20.4.2 nathanw {
612 1.20.4.2 nathanw int s, c;
613 1.20.4.2 nathanw
614 1.20.4.2 nathanw s = splhigh();
615 1.20.4.2 nathanw c = prom_getchar();
616 1.20.4.2 nathanw splx(s);
617 1.20.4.2 nathanw return (c);
618 1.20.4.2 nathanw }
619 1.20.4.2 nathanw
620 1.20.4.2 nathanw /*
621 1.20.4.2 nathanw * PROM console output putchar.
622 1.20.4.2 nathanw */
623 1.20.4.2 nathanw static void
624 1.20.4.2 nathanw prom_cnputc(dev, c)
625 1.20.4.2 nathanw dev_t dev;
626 1.20.4.2 nathanw int c;
627 1.20.4.2 nathanw {
628 1.20.4.2 nathanw
629 1.20.4.2 nathanw prom_putchar(c);
630 1.20.4.2 nathanw }
631 1.20.4.2 nathanw
632 1.20.4.2 nathanw
633 1.20.4.2 nathanw /*****************************************************************/
634 1.20.4.2 nathanw
635 1.20.4.2 nathanw static void prom_get_device_args __P((const char *, char *, unsigned int));
636 1.20.4.2 nathanw
637 1.20.4.2 nathanw void
638 1.20.4.2 nathanw prom_get_device_args(prop, args, sz)
639 1.20.4.2 nathanw const char *prop;
640 1.20.4.2 nathanw char *args;
641 1.20.4.2 nathanw unsigned int sz;
642 1.20.4.2 nathanw {
643 1.20.4.2 nathanw char *cp, buffer[128];
644 1.20.4.2 nathanw
645 1.20.4.2 nathanw cp = PROM_getpropstringA(findroot(), (char *)prop, buffer, sizeof buffer);
646 1.20.4.2 nathanw
647 1.20.4.2 nathanw /*
648 1.20.4.2 nathanw * Extract device-specific arguments from a PROM device path (if any)
649 1.20.4.2 nathanw */
650 1.20.4.2 nathanw cp = buffer + strlen(buffer);
651 1.20.4.2 nathanw while (cp >= buffer) {
652 1.20.4.2 nathanw if (*cp == ':') {
653 1.20.4.2 nathanw strncpy(args, cp+1, sz);
654 1.20.4.2 nathanw break;
655 1.20.4.2 nathanw }
656 1.20.4.2 nathanw cp--;
657 1.20.4.2 nathanw }
658 1.20.4.2 nathanw }
659 1.20.4.2 nathanw
660 1.20.4.2 nathanw /*
661 1.20.4.2 nathanw *
662 1.20.4.2 nathanw */
663 1.20.4.2 nathanw void
664 1.20.4.2 nathanw consinit()
665 1.20.4.2 nathanw {
666 1.20.4.2 nathanw int inSource, outSink;
667 1.20.4.2 nathanw
668 1.20.4.2 nathanw switch (prom_version()) {
669 1.20.4.2 nathanw case PROM_OLDMON:
670 1.20.4.2 nathanw case PROM_OBP_V0:
671 1.20.4.2 nathanw /* The stdio handles identify the device type */
672 1.20.4.2 nathanw inSource = prom_stdin();
673 1.20.4.2 nathanw outSink = prom_stdout();
674 1.20.4.2 nathanw break;
675 1.20.4.2 nathanw
676 1.20.4.2 nathanw case PROM_OBP_V2:
677 1.20.4.2 nathanw case PROM_OBP_V3:
678 1.20.4.2 nathanw case PROM_OPENFIRM:
679 1.20.4.2 nathanw
680 1.20.4.2 nathanw /* Save PROM arguments for device matching */
681 1.20.4.2 nathanw prom_get_device_args("stdin-path", prom_stdin_args,
682 1.20.4.2 nathanw sizeof(prom_stdin_args));
683 1.20.4.2 nathanw prom_get_device_args("stdout-path", prom_stdout_args,
684 1.20.4.2 nathanw sizeof(prom_stdout_args));
685 1.20.4.2 nathanw
686 1.20.4.2 nathanw /*
687 1.20.4.2 nathanw * Translate the STDIO package instance (`ihandle') -- that
688 1.20.4.2 nathanw * the PROM has already opened for us -- to a device tree
689 1.20.4.2 nathanw * node (i.e. a `phandle').
690 1.20.4.2 nathanw */
691 1.20.4.2 nathanw
692 1.20.4.2 nathanw prom_stdin_node = prom_instance_to_package(prom_stdin());
693 1.20.4.2 nathanw if (prom_stdin_node == 0)
694 1.20.4.2 nathanw printf("consinit: cannot convert stdin ihandle\n");
695 1.20.4.2 nathanw
696 1.20.4.2 nathanw prom_stdout_node = prom_instance_to_package(prom_stdout());
697 1.20.4.2 nathanw if (prom_stdout_node == 0) {
698 1.20.4.2 nathanw printf("consinit: cannot convert stdout ihandle\n");
699 1.20.4.2 nathanw break;
700 1.20.4.2 nathanw }
701 1.20.4.2 nathanw
702 1.20.4.2 nathanw break;
703 1.20.4.2 nathanw
704 1.20.4.2 nathanw default:
705 1.20.4.2 nathanw break;
706 1.20.4.2 nathanw }
707 1.20.4.2 nathanw
708 1.20.4.2 nathanw /* Wire up /dev/console */
709 1.20.4.2 nathanw cn_tab->cn_dev = makedev(KDMAJOR, 0);
710 1.20.4.2 nathanw cn_tab->cn_pri = CN_INTERNAL;
711 1.20.4.2 nathanw
712 1.20.4.2 nathanw /* Set up initial PROM input channel for /dev/console */
713 1.20.4.2 nathanw prom_cons_channel.cc_dev = NULL;
714 1.20.4.2 nathanw prom_cons_channel.cc_iopen = kd_rom_iopen;
715 1.20.4.2 nathanw prom_cons_channel.cc_iclose = kd_rom_iclose;
716 1.20.4.2 nathanw cons_attach_input(&prom_cons_channel, cn_tab);
717 1.20.4.2 nathanw
718 1.20.4.2 nathanw #ifdef KGDB
719 1.20.4.2 nathanw zs_kgdb_init(); /* XXX */
720 1.20.4.2 nathanw #endif
721 1.20.4.2 nathanw }
722