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