Home | History | Annotate | Line # | Download | only in dev
kd.c revision 1.16.2.1
      1 /*	$NetBSD: kd.c,v 1.16.2.1 2007/10/23 20:14:39 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.16.2.1 2007/10/23 20:14:39 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 	callout_setfunc(&tp->t_rstrt_ch, kd_later, tp);
    139 	tp->t_oproc = kdstart;
    140 	tp->t_param = kdparam;
    141 	tp->t_dev = makedev(cdevsw_lookup_major(&kd_cdevsw), 0);
    142 
    143 	tty_attach(tp);
    144 	kd->kd_tty = tp;
    145 
    146 	/*
    147 	 * get the console struct winsize.
    148 	 */
    149 	if (kd_is_console) {
    150 		fbconstty = tp;
    151 #ifdef RASTERCONSOLE
    152 		kd->rows = fbrcons_rows();
    153 		kd->cols = fbrcons_cols();
    154 		rcons_ttyinit(tp);
    155 #endif
    156 	}
    157 
    158 	/* else, consult the PROM */
    159 	switch (prom_version()) {
    160 #ifdef	PROM_OLDMON
    161 	case PROM_OLDMON:
    162 		if ((ep = (struct eeprom *)eeprom_va) == NULL)
    163 			break;
    164 		if (kd->rows == 0)
    165 			kd->rows = (u_short)ep->eeTtyRows;
    166 		if (kd->cols == 0)
    167 			kd->cols = (u_short)ep->eeTtyCols;
    168 		break;
    169 #endif	/* PROM_OLDMON */
    170 #ifdef	notyet /* PROM_OBP_V2 */
    171 	case PROM_OBP_V0:
    172 	case PROM_OBP_V2:
    173 	case PROM_OBP_V3:
    174 	case PROM_OPENFIRM:
    175 
    176 		if (kd->rows == 0 &&
    177 		    (prop = PROM_getpropstring(optionsnode, "screen-#rows"))) {
    178 			i = 0;
    179 			while (*prop != '\0')
    180 				i = i * 10 + *prop++ - '0';
    181 			kd->rows = (unsigned short)i;
    182 		}
    183 		if (kd->cols == 0 &&
    184 		    (prop = PROM_getpropstring(optionsnode, "screen-#columns"))) {
    185 			i = 0;
    186 			while (*prop != '\0')
    187 				i = i * 10 + *prop++ - '0';
    188 			kd->cols = (unsigned short)i;
    189 		}
    190 		break;
    191 #endif	/* PROM_OBP_V2 */
    192 	}
    193 	return;
    194 }
    195 
    196 struct tty *
    197 kdtty(dev_t dev)
    198 {
    199 	struct kd_softc *kd;
    200 
    201 	kd = &kd_softc; 	/* XXX */
    202 	return (kd->kd_tty);
    203 }
    204 
    205 int
    206 kdopen(dev_t dev, int flag, int mode, struct lwp *l)
    207 {
    208 	struct kd_softc *kd;
    209 	int error, s, unit;
    210 	struct tty *tp;
    211 static	int firstopen = 1;
    212 
    213 	unit = minor(dev);
    214 	if (unit != 0)
    215 		return ENXIO;
    216 	kd = &kd_softc; 	/* XXX */
    217 
    218 	if (firstopen) {
    219 		kd_init(kd);
    220 		firstopen = 0;
    221 	}
    222 	tp = kd->kd_tty;
    223 
    224 	/* It's simpler to do this up here. */
    225 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    226 		return (EBUSY);
    227 
    228 	s = spltty();
    229 
    230 	if ((tp->t_state & TS_ISOPEN) == 0) {
    231 		/* First open. */
    232 
    233 		/* Notify the input device that serves us */
    234 		struct cons_channel *cc = kd->kd_in;
    235 		if (cc != NULL &&
    236 		    (error = (*cc->cc_iopen)(cc)) != 0) {
    237 			splx(s);
    238 			return (error);
    239 		}
    240 
    241 		ttychars(tp);
    242 		tp->t_iflag = TTYDEF_IFLAG;
    243 		tp->t_oflag = TTYDEF_OFLAG;
    244 		tp->t_cflag = TTYDEF_CFLAG;
    245 		tp->t_lflag = TTYDEF_LFLAG;
    246 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    247 		(void) kdparam(tp, &tp->t_termios);
    248 		ttsetwater(tp);
    249 		tp->t_winsize.ws_row = kd->rows;
    250 		tp->t_winsize.ws_col = kd->cols;
    251 		/* Flush pending input?  Clear translator? */
    252 		/* This (pseudo)device always has SOFTCAR */
    253 		tp->t_state |= TS_CARR_ON;
    254 	}
    255 
    256 	splx(s);
    257 
    258 	return ((*tp->t_linesw->l_open)(dev, tp));
    259 }
    260 
    261 int
    262 kdclose(dev_t dev, int flag, int mode, struct lwp *l)
    263 {
    264 	struct kd_softc *kd;
    265 	struct tty *tp;
    266 	struct cons_channel *cc;
    267 
    268 	kd = &kd_softc; 	/* XXX */
    269 	tp = kd->kd_tty;
    270 
    271 	/* XXX This is for cons.c. */
    272 	if ((tp->t_state & TS_ISOPEN) == 0)
    273 		return 0;
    274 
    275 	(*tp->t_linesw->l_close)(tp, flag);
    276 	ttyclose(tp);
    277 
    278 	if ((cc = kd->kd_in) != NULL)
    279 		(void)(*cc->cc_iclose)(cc);
    280 
    281 	return (0);
    282 }
    283 
    284 int
    285 kdread(dev_t dev, struct uio *uio, int flag)
    286 {
    287 	struct kd_softc *kd;
    288 	struct tty *tp;
    289 
    290 	kd = &kd_softc; 	/* XXX */
    291 	tp = kd->kd_tty;
    292 
    293 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
    294 }
    295 
    296 int
    297 kdwrite(dev_t dev, struct uio *uio, int flag)
    298 {
    299 	struct kd_softc *kd;
    300 	struct tty *tp;
    301 
    302 	kd = &kd_softc; 	/* XXX */
    303 	tp = kd->kd_tty;
    304 
    305 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    306 }
    307 
    308 int
    309 kdpoll(dev_t dev, int events, struct lwp *l)
    310 {
    311 	struct kd_softc *kd;
    312 	struct tty *tp;
    313 
    314 	kd = &kd_softc; 	/* XXX */
    315 	tp = kd->kd_tty;
    316 
    317 	return ((*tp->t_linesw->l_poll)(tp, events, l));
    318 }
    319 
    320 int
    321 kdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    322 {
    323 	struct kd_softc *kd;
    324 	struct tty *tp;
    325 	int error;
    326 
    327 	kd = &kd_softc; 	/* XXX */
    328 	tp = kd->kd_tty;
    329 
    330 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    331 	if (error != EPASSTHROUGH)
    332 		return error;
    333 
    334 	error = ttioctl(tp, cmd, data, flag, l);
    335 	if (error != EPASSTHROUGH)
    336 		return error;
    337 
    338 	/* Handle any ioctl commands specific to kbd/display. */
    339 	/* XXX - Send KB* ioctls to kbd module? */
    340 	/* XXX - Send FB* ioctls to fb module?  */
    341 
    342 	return EPASSTHROUGH;
    343 }
    344 
    345 static int
    346 kdparam(struct tty *tp, 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(struct tty *tp)
    361 {
    362 	struct clist *cl;
    363 	int s1, s2;
    364 
    365 	s1 = splsoftclock();
    366 	s2 = 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 (is_spl0(s1)) {
    375 				/* called at level zero - update screen now. */
    376 				splx(s2);
    377 				kd_putfb(tp);
    378 				s2 = spltty();
    379 				tp->t_state &= ~TS_BUSY;
    380 			} else {
    381 				/* called at interrupt level - do it later */
    382 				callout_schedule(&tp->t_rstrt_ch, 0);
    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((void *)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