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