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