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