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