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