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