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