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