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