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