Home | History | Annotate | Line # | Download | only in dev
kd.c revision 1.21
      1 /*	$NetBSD: kd.c,v 1.21 1996/11/20 18:56:55 gwr 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 REGENTS OR CONTRIBUTORS BE
     30  * 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/autoconf.h>
     58 #include <machine/mon.h>
     59 #include <machine/psl.h>
     60 
     61 #include <dev/cons.h>
     62 #include <dev/sun/kbd_xlate.h>
     63 
     64 #define	KDMAJOR 1
     65 #define PUT_WSIZE	64
     66 
     67 cdev_decl(kd);	/* open, close, read, write, ioctl, stop, ... */
     68 
     69 struct kd_softc {
     70 	struct	device kd_dev;		/* required first: base device */
     71 	struct  tty *kd_tty;
     72 };
     73 
     74 /*
     75  * There is no point in pretending there might be
     76  * more than one keyboard/display device.
     77  */
     78 struct kd_softc kd_softc;
     79 
     80 static int kdparam(struct tty *, struct termios *);
     81 static void kdstart(struct tty *);
     82 
     83 int kd_is_console;
     84 
     85 /*
     86  * This is called by kbd_attach()
     87  * XXX - Make this a proper child of kbd?
     88  */
     89 void
     90 kd_init(unit)
     91 	int unit;
     92 {
     93 	struct kd_softc *kd;
     94 	struct tty *tp;
     95 
     96 	if (unit != 0)
     97 		return;
     98 	kd = &kd_softc; 	/* XXX */
     99 
    100 	tp = ttymalloc();
    101 	tp->t_oproc = kdstart;
    102 	tp->t_param = kdparam;
    103 	tp->t_dev = makedev(KDMAJOR, unit);
    104 	tty_attach(tp);
    105 
    106 	kd->kd_tty = tp;
    107 
    108 	return;
    109 }
    110 
    111 struct tty *
    112 kdtty(dev)
    113 	dev_t dev;
    114 {
    115 	struct kd_softc *kd;
    116 
    117 	kd = &kd_softc; 	/* XXX */
    118 	return (kd->kd_tty);
    119 }
    120 
    121 int
    122 kdopen(dev, flag, mode, p)
    123 	dev_t dev;
    124 	int flag, mode;
    125 	struct proc *p;
    126 {
    127 	struct kd_softc *kd;
    128 	int error, s, unit;
    129 	struct tty *tp;
    130 
    131 	unit = minor(dev);
    132 	if (unit != 0)
    133 		return ENXIO;
    134 	kd = &kd_softc; 	/* XXX */
    135 	tp = kd->kd_tty;
    136 
    137 	if ((error = kbd_iopen(unit)) != 0) {
    138 #ifdef	DIAGNOSTIC
    139 		printf("kd: kbd_iopen, error=%d\n", error);
    140 #endif
    141 		return (error);
    142 	}
    143 
    144 	/* It's simpler to do this up here. */
    145 	if (((tp->t_state & (TS_ISOPEN | TS_XCLUDE))
    146 	     ==             (TS_ISOPEN | TS_XCLUDE))
    147 	    && (p->p_ucred->cr_uid != 0) )
    148 	{
    149 		return (EBUSY);
    150 	}
    151 
    152 	s = spltty();
    153 
    154 	if ((tp->t_state & TS_ISOPEN) == 0) {
    155 		/* First open. */
    156 		ttychars(tp);
    157 		tp->t_iflag = TTYDEF_IFLAG;
    158 		tp->t_oflag = TTYDEF_OFLAG;
    159 		tp->t_cflag = TTYDEF_CFLAG;
    160 		tp->t_lflag = TTYDEF_LFLAG;
    161 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    162 		(void) kdparam(tp, &tp->t_termios);
    163 		ttsetwater(tp);
    164 		/* Flush pending input?  Clear translator? */
    165 		/* This (pseudo)device always has SOFTCAR */
    166 		tp->t_state |= TS_CARR_ON;
    167 	}
    168 
    169 	splx(s);
    170 
    171 	return ((*linesw[tp->t_line].l_open)(dev, tp));
    172 }
    173 
    174 int
    175 kdclose(dev, flag, mode, p)
    176 	dev_t dev;
    177 	int flag, mode;
    178 	struct proc *p;
    179 {
    180 	struct kd_softc *kd;
    181 	struct tty *tp;
    182 
    183 	kd = &kd_softc; 	/* XXX */
    184 	tp = kd->kd_tty;
    185 
    186 	/* XXX This is for cons.c. */
    187 	if ((tp->t_state & TS_ISOPEN) == 0)
    188 		return 0;
    189 
    190 	(*linesw[tp->t_line].l_close)(tp, flag);
    191 	ttyclose(tp);
    192 	return (0);
    193 }
    194 
    195 int
    196 kdread(dev, uio, flag)
    197 	dev_t dev;
    198 	struct uio *uio;
    199 	int flag;
    200 {
    201 	struct kd_softc *kd;
    202 	struct tty *tp;
    203 
    204 	kd = &kd_softc; 	/* XXX */
    205 	tp = kd->kd_tty;
    206 
    207 	return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
    208 }
    209 
    210 int
    211 kdwrite(dev, uio, flag)
    212 	dev_t dev;
    213 	struct uio *uio;
    214 	int flag;
    215 {
    216 	struct kd_softc *kd;
    217 	struct tty *tp;
    218 
    219 	kd = &kd_softc; 	/* XXX */
    220 	tp = kd->kd_tty;
    221 
    222 	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
    223 }
    224 
    225 int
    226 kdioctl(dev, cmd, data, flag, p)
    227 	dev_t dev;
    228 	u_long cmd;
    229 	caddr_t data;
    230 	int flag;
    231 	struct proc *p;
    232 {
    233 	struct kd_softc *kd;
    234 	struct tty *tp;
    235 	int error;
    236 
    237 	kd = &kd_softc; 	/* XXX */
    238 	tp = kd->kd_tty;
    239 
    240 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
    241 	if (error >= 0)
    242 		return error;
    243 	error = ttioctl(tp, cmd, data, flag, p);
    244 	if (error >= 0)
    245 		return error;
    246 
    247 	/* Handle any ioctl commands specific to kbd/display. */
    248 	/* XXX - Send KB* ioctls to kbd module? */
    249 	/* XXX - Send FB* ioctls to fb module?  */
    250 
    251 	return ENOTTY;
    252 }
    253 
    254 
    255 static int
    256 kdparam(tp, t)
    257 	struct tty *tp;
    258 	struct termios *t;
    259 {
    260 	/* XXX - These are ignored... */
    261 	tp->t_ispeed = t->c_ispeed;
    262 	tp->t_ospeed = t->c_ospeed;
    263 	tp->t_cflag = t->c_cflag;
    264 	return 0;
    265 }
    266 
    267 
    268 void
    269 kdstop(tp, flag)
    270 	struct tty *tp;
    271 	int flag;
    272 {
    273 
    274 }
    275 
    276 static void kd_later(void*);
    277 static void kd_putfb(struct tty *);
    278 
    279 void
    280 kdstart(tp)
    281 	struct tty *tp;
    282 {
    283 	struct clist *cl;
    284 	register int s;
    285 
    286 	s = spltty();
    287 	if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
    288 		goto out;
    289 
    290 	cl = &tp->t_outq;
    291 	if (cl->c_cc) {
    292 		if (kd_is_console) {
    293 			tp->t_state |= TS_BUSY;
    294 			if ((s & PSL_IPL) == 0) {
    295 				/* called at level zero - update screen now. */
    296 				(void) splsoftclock();
    297 				kd_putfb(tp);
    298 				(void) spltty();
    299 				tp->t_state &= ~TS_BUSY;
    300 			} else {
    301 				/* called at interrupt level - do it later */
    302 				timeout(kd_later, (void*)tp, 0);
    303 			}
    304 		} else {
    305 			/*
    306 			 * This driver uses the PROM for writing the screen,
    307 			 * and that only works if this is the console device.
    308 			 * If this is not the console, just flush the output.
    309 			 * Sorry.  (In that case, use xdm instead of getty.)
    310 			 */
    311 			ndflush(cl, cl->c_cc);
    312 		}
    313 	}
    314 	if (cl->c_cc <= tp->t_lowat) {
    315 		if (tp->t_state & TS_ASLEEP) {
    316 			tp->t_state &= ~TS_ASLEEP;
    317 			wakeup((caddr_t)cl);
    318 		}
    319 		selwakeup(&tp->t_wsel);
    320 	}
    321 out:
    322 	splx(s);
    323 }
    324 
    325 /*
    326  * Timeout function to do delayed writes to the screen.
    327  * Called at splsoftclock when requested by kdstart.
    328  */
    329 static void
    330 kd_later(tpaddr)
    331 	void *tpaddr;
    332 {
    333 	struct tty *tp = tpaddr;
    334 	register int s;
    335 
    336 	kd_putfb(tp);
    337 
    338 	s = spltty();
    339 	tp->t_state &= ~TS_BUSY;
    340 	(*linesw[tp->t_line].l_start)(tp);
    341 	splx(s);
    342 }
    343 
    344 /*
    345  * Put text on the screen using the PROM monitor.
    346  * This can take a while, so to avoid missing
    347  * interrupts, this is called at splsoftclock.
    348  */
    349 static void kd_putfb(tp)
    350 	struct tty *tp;
    351 {
    352 	char buf[PUT_WSIZE];
    353 	struct clist *cl = &tp->t_outq;
    354 	char *p, *end;
    355 	int len;
    356 
    357 	while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) {
    358 		/* PROM will barf if high bits are set. */
    359 		p = buf;
    360 		end = buf + len;
    361 		while (p < end)
    362 			*p++ &= 0x7f;
    363 		(romVectorPtr->fbWriteStr)(buf, len);
    364 	}
    365 }
    366 
    367 /*
    368  * Our "interrupt" routine for input. This is called by
    369  * the keyboard driver (dev/sun/kbd.c) at spltty.
    370  */
    371 void
    372 kd_input(c)
    373 	int c;
    374 {
    375 	struct kd_softc *kd = &kd_softc;
    376 	struct tty *tp;
    377 
    378 	/* XXX: Make sure the device is open. */
    379 	tp = kd->kd_tty;
    380 	if (tp == NULL)
    381 		return;
    382     if ((tp->t_state & TS_ISOPEN) == 0)
    383 		return;
    384 
    385 	(*linesw[tp->t_line].l_rint)(c, tp);
    386 }
    387 
    388 
    389 /****************************************************************
    390  * kd console support
    391  ****************************************************************/
    392 
    393 extern void *zs_conschan;
    394 extern int zs_getc();
    395 extern void nullcnprobe();
    396 cons_decl(kd);
    397 
    398 /* The debugger gets its own key translation state. */
    399 static struct kbd_state kdcn_state;
    400 
    401 void
    402 kdcninit(cn)
    403 	struct consdev *cn;
    404 {
    405 	struct kbd_state *ks = &kdcn_state;
    406 
    407 	mon_printf("console on kd0 (keyboard/display)\n");
    408 
    409 	/* This prepares kbd_translate() */
    410 	ks->kbd_id = KBD_MIN_TYPE;
    411 	kbd_xlate_init(ks);
    412 
    413 	/* Indicate that it is OK to use the PROM fbwrite */
    414 	kd_is_console = 1;
    415 }
    416 
    417 int
    418 kdcngetc(dev)
    419 	dev_t dev;
    420 {
    421 	struct kbd_state *ks = &kdcn_state;
    422 	int code, class, data, keysym;
    423 
    424 	for (;;) {
    425 		code = zs_getc(zs_conschan);
    426 		keysym = kbd_code_to_keysym(ks, code);
    427 		class = KEYSYM_CLASS(keysym);
    428 
    429 		switch (class) {
    430 		case KEYSYM_ASCII:
    431 			goto out;
    432 
    433 		case KEYSYM_CLRMOD:
    434 		case KEYSYM_SETMOD:
    435 			data = (keysym & 0x1F);
    436 			/* Only allow ctrl or shift. */
    437 			if (data > KBMOD_SHIFT_R)
    438 				break;
    439 			data = 1 << data;
    440 			if (class == KEYSYM_SETMOD)
    441 				ks->kbd_modbits |= data;
    442 			else
    443 				ks->kbd_modbits &= ~data;
    444 			break;
    445 
    446 		case KEYSYM_ALL_UP:
    447 			/* No toggle keys here. */
    448 			ks->kbd_modbits = 0;
    449 			break;
    450 
    451 		default:	/* ignore all other keysyms */
    452 			break;
    453 		}
    454 	}
    455 out:
    456 	return (keysym);
    457 }
    458 
    459 void
    460 kdcnputc(dev, c)
    461 	dev_t dev;
    462 	int c;
    463 {
    464 	(romVectorPtr->fbWriteChar)(c & 0x7f);
    465 }
    466 
    467 extern void fb_unblank();
    468 void kdcnpollc(dev, on)
    469 	dev_t dev;
    470 	int on;
    471 {
    472 	struct kbd_state *ks = &kdcn_state;
    473 
    474 	if (on) {
    475 		/* Entering debugger. */
    476 		fb_unblank();
    477 		/* Clear shift keys too. */
    478 		ks->kbd_modbits = 0;
    479 	} else {
    480 		/* Resuming kernel. */
    481 	}
    482 }
    483 
    484 struct consdev consdev_kd = {
    485 	nullcnprobe,
    486 	kdcninit,
    487 	kdcngetc,
    488 	kdcnputc,
    489 	kdcnpollc,
    490 	makedev(KDMAJOR, 0),
    491 	CN_INTERNAL
    492 };
    493 
    494