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