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