Home | History | Annotate | Line # | Download | only in dev
kd.c revision 1.35
      1 /*	$NetBSD: kd.c,v 1.35 2001/05/02 10:32:20 scw 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 ((*tp->t_linesw->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 	(*tp->t_linesw->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 ((*tp->t_linesw->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 ((*tp->t_linesw->l_write)(tp, uio, flag));
    238 }
    239 
    240 int
    241 kdpoll(dev, events, p)
    242 	dev_t dev;
    243 	int events;
    244 	struct proc *p;
    245 {
    246 	struct kd_softc *kd;
    247 	struct tty *tp;
    248 
    249 	kd = &kd_softc; 	/* XXX */
    250 	tp = kd->kd_tty;
    251 
    252 	return ((*tp->t_linesw->l_poll)(tp, events, p));
    253 }
    254 
    255 int
    256 kdioctl(dev, cmd, data, flag, p)
    257 	dev_t dev;
    258 	u_long cmd;
    259 	caddr_t data;
    260 	int flag;
    261 	struct proc *p;
    262 {
    263 	struct kd_softc *kd;
    264 	struct tty *tp;
    265 	int error;
    266 
    267 	kd = &kd_softc; 	/* XXX */
    268 	tp = kd->kd_tty;
    269 
    270 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
    271 	if (error >= 0)
    272 		return error;
    273 	error = ttioctl(tp, cmd, data, flag, p);
    274 	if (error >= 0)
    275 		return error;
    276 
    277 	/* Handle any ioctl commands specific to kbd/display. */
    278 	/* XXX - Send KB* ioctls to kbd module? */
    279 	/* XXX - Send FB* ioctls to fb module?  */
    280 
    281 	return ENOTTY;
    282 }
    283 
    284 void
    285 kdstop(tp, flag)
    286 	struct tty *tp;
    287 	int flag;
    288 {
    289 
    290 }
    291 
    292 
    293 static int
    294 kdparam(tp, t)
    295 	struct tty *tp;
    296 	struct termios *t;
    297 {
    298 	/* XXX - These are ignored... */
    299 	tp->t_ispeed = t->c_ispeed;
    300 	tp->t_ospeed = t->c_ospeed;
    301 	tp->t_cflag = t->c_cflag;
    302 	return 0;
    303 }
    304 
    305 
    306 static void kd_later(void*);
    307 static void kd_putfb(struct tty *);
    308 
    309 static void
    310 kdstart(tp)
    311 	struct tty *tp;
    312 {
    313 	struct clist *cl;
    314 	register int s;
    315 
    316 	s = spltty();
    317 	if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
    318 		goto out;
    319 
    320 	cl = &tp->t_outq;
    321 	if (cl->c_cc) {
    322 		if (kd_is_console) {
    323 			tp->t_state |= TS_BUSY;
    324 			if ((s & PSL_IPL) == 0) {
    325 				/* called at level zero - update screen now. */
    326 				(void) spllowersoftclock();
    327 				kd_putfb(tp);
    328 				(void) spltty();
    329 				tp->t_state &= ~TS_BUSY;
    330 			} else {
    331 				/* called at interrupt level - do it later */
    332 				callout_reset(&tp->t_rstrt_ch, 0,
    333 				    kd_later, tp);
    334 			}
    335 		} else {
    336 			/*
    337 			 * This driver uses the PROM for writing the screen,
    338 			 * and that only works if this is the console device.
    339 			 * If this is not the console, just flush the output.
    340 			 * Sorry.  (In that case, use xdm instead of getty.)
    341 			 */
    342 			ndflush(cl, cl->c_cc);
    343 		}
    344 	}
    345 	if (cl->c_cc <= tp->t_lowat) {
    346 		if (tp->t_state & TS_ASLEEP) {
    347 			tp->t_state &= ~TS_ASLEEP;
    348 			wakeup((caddr_t)cl);
    349 		}
    350 		selwakeup(&tp->t_wsel);
    351 	}
    352 out:
    353 	splx(s);
    354 }
    355 
    356 /*
    357  * Timeout function to do delayed writes to the screen.
    358  * Called at splsoftclock when requested by kdstart.
    359  */
    360 static void
    361 kd_later(tpaddr)
    362 	void *tpaddr;
    363 {
    364 	struct tty *tp = tpaddr;
    365 	register int s;
    366 
    367 	kd_putfb(tp);
    368 
    369 	s = spltty();
    370 	tp->t_state &= ~TS_BUSY;
    371 	(*tp->t_linesw->l_start)(tp);
    372 	splx(s);
    373 }
    374 
    375 /*
    376  * Put text on the screen using the PROM monitor.
    377  * This can take a while, so to avoid missing
    378  * interrupts, this is called at splsoftclock.
    379  */
    380 static void
    381 kd_putfb(tp)
    382 	struct tty *tp;
    383 {
    384 	char buf[PUT_WSIZE];
    385 	struct clist *cl = &tp->t_outq;
    386 	char *p, *end;
    387 	int len;
    388 
    389 	while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) {
    390 		/* PROM will barf if high bits are set. */
    391 		p = buf;
    392 		end = buf + len;
    393 		while (p < end)
    394 			*p++ &= 0x7f;
    395 		(romVectorPtr->fbWriteStr)(buf, len);
    396 	}
    397 }
    398 
    399 void
    400 cons_attach_input(cc, cn)
    401 	struct cons_channel *cc;
    402 	struct consdev *cn;
    403 {
    404 	struct kd_softc *kd = &kd_softc;
    405 
    406 	kd->kd_in = cc;
    407 	cc->cc_upstream = kd_cons_input;
    408 }
    409 
    410 /*
    411  * Default PROM-based console input stream
    412  */
    413 static int kd_rom_iopen __P((struct cons_channel *));
    414 static int kd_rom_iclose __P((struct cons_channel *));
    415 
    416 static struct cons_channel prom_cons_channel;
    417 
    418 int
    419 kd_rom_iopen(cc)
    420 	struct cons_channel *cc;
    421 {
    422 	/* No-op */
    423 	return (0);
    424 }
    425 
    426 int
    427 kd_rom_iclose(cc)
    428 	struct cons_channel *cc;
    429 {
    430 	/* No-op */
    431 	return (0);
    432 }
    433 
    434 /*
    435  * Our "interrupt" routine for input. This is called by
    436  * the keyboard driver (dev/sun/kbd.c) at spltty.
    437  */
    438 void
    439 kd_cons_input(c)
    440 	int c;
    441 {
    442 	struct kd_softc *kd = &kd_softc;
    443 	struct tty *tp;
    444 
    445 	/* XXX: Make sure the device is open. */
    446 	tp = kd->kd_tty;
    447 	if (tp == NULL)
    448 		return;
    449 	if ((tp->t_state & TS_ISOPEN) == 0)
    450 		return;
    451 
    452 	(*tp->t_linesw->l_rint)(c, tp);
    453 }
    454 
    455 
    456 /****************************************************************
    457  * kd console support
    458  ****************************************************************/
    459 
    460 /* The debugger gets its own key translation state. */
    461 static struct kbd_state kdcn_state;
    462 
    463 static void kdcnprobe __P((struct consdev *));
    464 static void kdcninit __P((struct consdev *));
    465 static int  kdcngetc __P((dev_t));
    466 static void kdcnputc __P((dev_t, int));
    467 static void kdcnpollc __P((dev_t, int));
    468 
    469 struct consdev consdev_kd = {
    470 	kdcnprobe,
    471 	kdcninit,
    472 	kdcngetc,
    473 	kdcnputc,
    474 	kdcnpollc,
    475 	NULL,
    476 };
    477 
    478 /* We never call this. */
    479 static void
    480 kdcnprobe(cn)
    481 	struct consdev *cn;
    482 {
    483 }
    484 
    485 static void
    486 kdcninit(cn)
    487 	struct consdev *cn;
    488 {
    489 	struct kbd_state *ks = &kdcn_state;
    490 
    491 	cn->cn_dev = makedev(KDMAJOR, 0);
    492 	cn->cn_pri = CN_INTERNAL;
    493 
    494 	/* This prepares kbd_translate() */
    495 	ks->kbd_id = KBD_MIN_TYPE;
    496 	kbd_xlate_init(ks);
    497 
    498 	/* Set up initial PROM input channel for /dev/console */
    499 	prom_cons_channel.cc_dev = NULL;
    500 	prom_cons_channel.cc_iopen = kd_rom_iopen;
    501 	prom_cons_channel.cc_iclose = kd_rom_iclose;
    502 	cons_attach_input(&prom_cons_channel, cn);
    503 
    504 	/* Indicate that it is OK to use the PROM fbwrite */
    505 	kd_is_console = 1;
    506 }
    507 
    508 static int
    509 kdcngetc(dev)
    510 	dev_t dev;
    511 {
    512 	struct kbd_state *ks = &kdcn_state;
    513 	int code, class, data, keysym;
    514 
    515 	for (;;) {
    516 		code = zs_getc(zs_conschan);
    517 		keysym = kbd_code_to_keysym(ks, code);
    518 		class = KEYSYM_CLASS(keysym);
    519 
    520 		switch (class) {
    521 		case KEYSYM_ASCII:
    522 			goto out;
    523 
    524 		case KEYSYM_CLRMOD:
    525 		case KEYSYM_SETMOD:
    526 			data = (keysym & 0x1F);
    527 			/* Only allow ctrl or shift. */
    528 			if (data > KBMOD_SHIFT_R)
    529 				break;
    530 			data = 1 << data;
    531 			if (class == KEYSYM_SETMOD)
    532 				ks->kbd_modbits |= data;
    533 			else
    534 				ks->kbd_modbits &= ~data;
    535 			break;
    536 
    537 		case KEYSYM_ALL_UP:
    538 			/* No toggle keys here. */
    539 			ks->kbd_modbits = 0;
    540 			break;
    541 
    542 		default:	/* ignore all other keysyms */
    543 			break;
    544 		}
    545 	}
    546 out:
    547 	return (keysym);
    548 }
    549 
    550 static void
    551 kdcnputc(dev, c)
    552 	dev_t dev;
    553 	int c;
    554 {
    555 	(romVectorPtr->fbWriteChar)(c & 0x7f);
    556 }
    557 
    558 static void
    559 kdcnpollc(dev, on)
    560 	dev_t dev;
    561 	int on;
    562 {
    563 	struct kbd_state *ks = &kdcn_state;
    564 
    565 	if (on) {
    566 		/* Entering debugger. */
    567 #if NFB > 0
    568 		fb_unblank();
    569 #endif
    570 		/* Clear shift keys too. */
    571 		ks->kbd_modbits = 0;
    572 	} else {
    573 		/* Resuming kernel. */
    574 	}
    575 }
    576 
    577