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