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