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