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