Home | History | Annotate | Line # | Download | only in dev
kd.c revision 1.2
      1 /*	$NetBSD: kd.c,v 1.2 1997/12/03 22:32:29 mjacob 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/bsd_openprom.h>
     58 #include <machine/eeprom.h>
     59 #include <machine/psl.h>
     60 #include <machine/cpu.h>
     61 #include <machine/kbd.h>
     62 #if defined(SUN4)
     63 #include <machine/oldmon.h>
     64 #endif
     65 #include <machine/autoconf.h>
     66 #include <machine/conf.h>
     67 
     68 #ifdef RASTERCONSOLE
     69 #include <machine/fbio.h>
     70 #include <machine/fbvar.h>
     71 #endif
     72 
     73 
     74 #include <dev/cons.h>
     75 #include <dev/sun/kbd_xlate.h>
     76 #include <sparc/dev/cons.h>
     77 
     78 extern void fb_unblank __P((void)); /* XXX */
     79 struct	tty *fbconstty = 0;	/* tty structure for frame buffer console */
     80 int cnrom __P((void));
     81 void cnrint __P((void));
     82 
     83 #define	KDMAJOR 1
     84 #define PUT_WSIZE	64
     85 
     86 struct kd_softc {
     87 	struct	device kd_dev;		/* required first: base device */
     88 	struct  tty *kd_tty;
     89 	int rows, cols;
     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 
    102 int	rom_console_input;	/* when set, hardclock calls cnrom() */
    103 int	cons_ocount;		/* output byte count */
    104 
    105 /* Now talking directly to the zs, so this is not needed. */
    106 int
    107 cnrom()
    108 {
    109   return (0);
    110 }
    111 void
    112 cnrint()
    113 {
    114 }
    115 
    116 /*
    117  * This is called by kbd_attach()
    118  * XXX - Make this a proper child of kbd?
    119  */
    120 void
    121 kd_init(unit)
    122 	int unit;
    123 {
    124 	struct kd_softc *kd;
    125 	struct tty *tp;
    126 
    127 	if (unit != 0)
    128 		return;
    129 	kd = &kd_softc; 	/* XXX */
    130 
    131 	tp = ttymalloc();
    132 	tp->t_oproc = kdstart;
    133 	tp->t_param = kdparam;
    134 	tp->t_dev = makedev(KDMAJOR, unit);
    135 
    136 #if 1	/* XXX - Why? */
    137 	clalloc(&tp->t_rawq, 1024, 1);
    138 	clalloc(&tp->t_canq, 1024, 1);
    139 	/* output queue doesn't need quoting */
    140 	clalloc(&tp->t_outq, 1024, 0);
    141 #endif
    142 
    143 	tty_attach(tp);
    144 	kd->kd_tty = tp;
    145 
    146 	/*
    147 	 * get the console struct winsize.
    148 	 */
    149 	if (kd_is_console) {
    150 		fbconstty = tp;
    151 #ifdef RASTERCONSOLE
    152 		kd->rows = fbrcons_rows();
    153 		kd->cols = fbrcons_cols();
    154 #endif
    155 	}
    156 
    157 	if (CPU_ISSUN4COR4M) {
    158 		int i;
    159 		char *prop;
    160 
    161 		if (kd->rows == 0 &&
    162 		    (prop = getpropstring(optionsnode, "screen-#rows"))) {
    163 			i = 0;
    164 			while (*prop != '\0')
    165 				i = i * 10 + *prop++ - '0';
    166 			kd->rows = (unsigned short)i;
    167 		}
    168 		if (kd->cols == 0 &&
    169 		    (prop = getpropstring(optionsnode, "screen-#columns"))) {
    170 			i = 0;
    171 			while (*prop != '\0')
    172 				i = i * 10 + *prop++ - '0';
    173 			kd->cols = (unsigned short)i;
    174 		}
    175 	}
    176 	if (CPU_ISSUN4) {
    177 		struct eeprom *ep = (struct eeprom *)eeprom_va;
    178 
    179 		if (ep) {
    180 			if (kd->rows == 0)
    181 				kd->rows = (u_short)ep->eeTtyRows;
    182 			if (kd->cols == 0)
    183 				kd->cols = (u_short)ep->eeTtyCols;
    184 		}
    185 	}
    186 
    187 	return;
    188 }
    189 
    190 struct tty *
    191 kdtty(dev)
    192 	dev_t dev;
    193 {
    194 	struct kd_softc *kd;
    195 
    196 	kd = &kd_softc; 	/* XXX */
    197 	return (kd->kd_tty);
    198 }
    199 
    200 int
    201 kdopen(dev, flag, mode, p)
    202 	dev_t dev;
    203 	int flag, mode;
    204 	struct proc *p;
    205 {
    206 	struct kd_softc *kd;
    207 	int error, s, unit;
    208 	struct tty *tp;
    209 
    210 	unit = minor(dev);
    211 	if (unit != 0)
    212 		return ENXIO;
    213 	kd = &kd_softc; 	/* XXX */
    214 	tp = kd->kd_tty;
    215 
    216 	if ((error = kbd_iopen(unit)) != 0) {
    217 #ifdef	DIAGNOSTIC
    218 		printf("kd: kbd_iopen, error=%d\n", error);
    219 #endif
    220 		return (error);
    221 	}
    222 
    223 	/* It's simpler to do this up here. */
    224 	if (((tp->t_state & (TS_ISOPEN | TS_XCLUDE))
    225 	     ==             (TS_ISOPEN | TS_XCLUDE))
    226 	    && (p->p_ucred->cr_uid != 0) )
    227 	{
    228 		return (EBUSY);
    229 	}
    230 
    231 	s = spltty();
    232 
    233 	if ((tp->t_state & TS_ISOPEN) == 0) {
    234 		/* First open. */
    235 		ttychars(tp);
    236 		tp->t_iflag = TTYDEF_IFLAG;
    237 		tp->t_oflag = TTYDEF_OFLAG;
    238 		tp->t_cflag = TTYDEF_CFLAG;
    239 		tp->t_lflag = TTYDEF_LFLAG;
    240 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    241 		(void) kdparam(tp, &tp->t_termios);
    242 		ttsetwater(tp);
    243 		tp->t_winsize.ws_row = kd->rows;
    244 		tp->t_winsize.ws_col = kd->cols;
    245 		/* Flush pending input?  Clear translator? */
    246 		/* This (pseudo)device always has SOFTCAR */
    247 		tp->t_state |= TS_CARR_ON;
    248 	}
    249 
    250 	splx(s);
    251 
    252 	return ((*linesw[tp->t_line].l_open)(dev, tp));
    253 }
    254 
    255 int
    256 kdclose(dev, flag, mode, p)
    257 	dev_t dev;
    258 	int flag, mode;
    259 	struct proc *p;
    260 {
    261 	struct kd_softc *kd;
    262 	struct tty *tp;
    263 
    264 	kd = &kd_softc; 	/* XXX */
    265 	tp = kd->kd_tty;
    266 
    267 	/* XXX This is for cons.c. */
    268 	if ((tp->t_state & TS_ISOPEN) == 0)
    269 		return 0;
    270 
    271 	(*linesw[tp->t_line].l_close)(tp, flag);
    272 	ttyclose(tp);
    273 	return (0);
    274 }
    275 
    276 int
    277 kdread(dev, uio, flag)
    278 	dev_t dev;
    279 	struct uio *uio;
    280 	int flag;
    281 {
    282 	struct kd_softc *kd;
    283 	struct tty *tp;
    284 
    285 	kd = &kd_softc; 	/* XXX */
    286 	tp = kd->kd_tty;
    287 
    288 	return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
    289 }
    290 
    291 int
    292 kdwrite(dev, uio, flag)
    293 	dev_t dev;
    294 	struct uio *uio;
    295 	int flag;
    296 {
    297 	struct kd_softc *kd;
    298 	struct tty *tp;
    299 
    300 	kd = &kd_softc; 	/* XXX */
    301 	tp = kd->kd_tty;
    302 
    303 	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
    304 }
    305 
    306 int
    307 kdioctl(dev, cmd, data, flag, p)
    308 	dev_t dev;
    309 	u_long cmd;
    310 	caddr_t data;
    311 	int flag;
    312 	struct proc *p;
    313 {
    314 	struct kd_softc *kd;
    315 	struct tty *tp;
    316 	int error;
    317 
    318 	kd = &kd_softc; 	/* XXX */
    319 	tp = kd->kd_tty;
    320 
    321 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
    322 	if (error >= 0)
    323 		return error;
    324 	error = ttioctl(tp, cmd, data, flag, p);
    325 	if (error >= 0)
    326 		return error;
    327 
    328 	/* Handle any ioctl commands specific to kbd/display. */
    329 	/* XXX - Send KB* ioctls to kbd module? */
    330 	/* XXX - Send FB* ioctls to fb module?  */
    331 
    332 	return ENOTTY;
    333 }
    334 
    335 void
    336 kdstop(tp, flag)
    337 	struct tty *tp;
    338 	int flag;
    339 {
    340 
    341 }
    342 
    343 
    344 static int
    345 kdparam(tp, t)
    346 	struct tty *tp;
    347 	struct termios *t;
    348 {
    349 	/* XXX - These are ignored... */
    350 	tp->t_ispeed = t->c_ispeed;
    351 	tp->t_ospeed = t->c_ospeed;
    352 	tp->t_cflag = t->c_cflag;
    353 	return 0;
    354 }
    355 
    356 
    357 static void kd_later(void*);
    358 static void kd_putfb(struct tty *);
    359 
    360 static void
    361 kdstart(tp)
    362 	struct tty *tp;
    363 {
    364 	struct clist *cl;
    365 	register int s;
    366 
    367 	s = spltty();
    368 	if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
    369 		goto out;
    370 
    371 	cl = &tp->t_outq;
    372 	if (cl->c_cc) {
    373 		if (kd_is_console) {
    374 			tp->t_state |= TS_BUSY;
    375 			if ((s & PSR_PIL) == 0) {
    376 				/* called at level zero - update screen now. */
    377 				(void) splsoftclock();
    378 				kd_putfb(tp);
    379 				(void) spltty();
    380 				tp->t_state &= ~TS_BUSY;
    381 			} else {
    382 				/* called at interrupt level - do it later */
    383 				timeout(kd_later, (void*)tp, 0);
    384 			}
    385 		} else {
    386 			/*
    387 			 * This driver uses the PROM for writing the screen,
    388 			 * and that only works if this is the console device.
    389 			 * If this is not the console, just flush the output.
    390 			 * Sorry.  (In that case, use xdm instead of getty.)
    391 			 */
    392 			ndflush(cl, cl->c_cc);
    393 		}
    394 	}
    395 	if (cl->c_cc <= tp->t_lowat) {
    396 		if (tp->t_state & TS_ASLEEP) {
    397 			tp->t_state &= ~TS_ASLEEP;
    398 			wakeup((caddr_t)cl);
    399 		}
    400 		selwakeup(&tp->t_wsel);
    401 	}
    402 out:
    403 	splx(s);
    404 }
    405 
    406 /*
    407  * Timeout function to do delayed writes to the screen.
    408  * Called at splsoftclock when requested by kdstart.
    409  */
    410 static void
    411 kd_later(tpaddr)
    412 	void *tpaddr;
    413 {
    414 	struct tty *tp = tpaddr;
    415 	register int s;
    416 
    417 	kd_putfb(tp);
    418 
    419 	s = spltty();
    420 	tp->t_state &= ~TS_BUSY;
    421 	(*linesw[tp->t_line].l_start)(tp);
    422 	splx(s);
    423 }
    424 
    425 /*
    426  * Put text on the screen using the PROM monitor.
    427  * This can take a while, so to avoid missing
    428  * interrupts, this is called at splsoftclock.
    429  */
    430 static void
    431 kd_putfb(tp)
    432 	struct tty *tp;
    433 {
    434 	char buf[PUT_WSIZE];
    435 	struct clist *cl = &tp->t_outq;
    436 	char *p, *end;
    437 	int len;
    438 
    439 	while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) {
    440 		/* PROM will barf if high bits are set. */
    441 		p = buf;
    442 		end = buf + len;
    443 		while (p < end)
    444 			*p++ &= 0x7f;
    445 		/* Now let the PROM print it. */
    446 		if (promvec->pv_romvec_vers > 2) {
    447 			(*promvec->pv_v2devops.v2_write)
    448 				(*promvec->pv_v2bootargs.v2_fd1, buf, len);
    449 		} else
    450 			(*promvec->pv_putstr)(buf, len);
    451 	}
    452 }
    453 
    454 /*
    455  * Our "interrupt" routine for input. This is called by
    456  * the keyboard driver (dev/sun/kbd.c) at spltty.
    457  */
    458 void
    459 kd_input(c)
    460 	int c;
    461 {
    462 	struct kd_softc *kd = &kd_softc;
    463 	struct tty *tp;
    464 
    465 	/* XXX: Make sure the device is open. */
    466 	tp = kd->kd_tty;
    467 	if (tp == NULL)
    468 		return;
    469 	if ((tp->t_state & TS_ISOPEN) == 0)
    470 		return;
    471 
    472 	(*linesw[tp->t_line].l_rint)(c, tp);
    473 }
    474 
    475 
    476 /****************************************************************
    477  * kd console support
    478  ****************************************************************/
    479 
    480 /* The debugger gets its own key translation state. */
    481 static struct kbd_state kdcn_state;
    482 
    483 static void kdcnprobe __P((struct consdev *));
    484 static void kdcninit __P((struct consdev *));
    485 static int  kdcngetc __P((dev_t));
    486 static void kdcnputc __P((dev_t, int));
    487 static void kdcnpollc __P((dev_t, int));
    488 
    489 struct consdev consdev_kd = {
    490 	kdcnprobe,
    491 	kdcninit,
    492 	kdcngetc,
    493 	kdcnputc,
    494 	kdcnpollc,
    495 };
    496 
    497 /* We never call this. */
    498 static void
    499 kdcnprobe(cn)
    500 	struct consdev *cn;
    501 {
    502 }
    503 
    504 static void
    505 kdcninit(cn)
    506 	struct consdev *cn;
    507 {
    508 	struct kbd_state *ks = &kdcn_state;
    509 
    510 	cn->cn_dev = makedev(KDMAJOR, 0);
    511 	cn->cn_pri = CN_INTERNAL;
    512 
    513 	/* This prepares kbd_translate() */
    514 	ks->kbd_id = KBD_MIN_TYPE;
    515 	kbd_xlate_init(ks);
    516 
    517 	/* Indicate that it is OK to use the PROM fbwrite */
    518 	kd_is_console = 1;
    519 }
    520 
    521 static int
    522 kdcngetc(dev)
    523 	dev_t dev;
    524 {
    525 	struct kbd_state *ks = &kdcn_state;
    526 	int code, class, data, keysym;
    527 
    528 	for (;;) {
    529 		code = zs_getc(zs_conschan);
    530 		keysym = kbd_code_to_keysym(ks, code);
    531 		class = KEYSYM_CLASS(keysym);
    532 
    533 		switch (class) {
    534 		case KEYSYM_ASCII:
    535 			goto out;
    536 
    537 		case KEYSYM_CLRMOD:
    538 		case KEYSYM_SETMOD:
    539 			data = (keysym & 0x1F);
    540 			/* Only allow ctrl or shift. */
    541 			if (data > KBMOD_SHIFT_R)
    542 				break;
    543 			data = 1 << data;
    544 			if (class == KEYSYM_SETMOD)
    545 				ks->kbd_modbits |= data;
    546 			else
    547 				ks->kbd_modbits &= ~data;
    548 			break;
    549 
    550 		case KEYSYM_ALL_UP:
    551 			/* No toggle keys here. */
    552 			ks->kbd_modbits = 0;
    553 			break;
    554 
    555 		default:	/* ignore all other keysyms */
    556 			break;
    557 		}
    558 	}
    559 out:
    560 	return (keysym);
    561 }
    562 
    563 static void
    564 kdcnputc(dev, c)
    565 	dev_t dev;
    566 	int c;
    567 {
    568 	char c0 = (c & 0x7f);
    569 
    570 	if (promvec->pv_romvec_vers > 2)
    571 		(*promvec->pv_v2devops.v2_write)
    572 			(*promvec->pv_v2bootargs.v2_fd1, &c0, 1);
    573 	else
    574 		(*promvec->pv_putchar)(c);
    575 }
    576 
    577 static void
    578 kdcnpollc(dev, on)
    579 	dev_t dev;
    580 	int on;
    581 {
    582 	struct kbd_state *ks = &kdcn_state;
    583 
    584 	if (on) {
    585 		/* Entering debugger. */
    586 		fb_unblank();
    587 		/* Clear shift keys too. */
    588 		ks->kbd_modbits = 0;
    589 	} else {
    590 		/* Resuming kernel. */
    591 	}
    592 }
    593 
    594