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