Home | History | Annotate | Line # | Download | only in wscons
wskbd.c revision 1.34
      1 /* $NetBSD: wskbd.c,v 1.34 1999/12/21 12:02:04 drochner Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1996, 1997 Christopher G. Demetriou.  All rights reserved.
      5  *
      6  * Keysym translator:
      7  * Contributed to The NetBSD Foundation by Juergen Hannken-Illjes.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *      This product includes software developed by Christopher G. Demetriou
     20  *	for the NetBSD Project.
     21  * 4. The name of the author may not be used to endorse or promote products
     22  *    derived from this software without specific prior written permission
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     27  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     29  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     33  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34  */
     35 
     36 static const char _copyright[] __attribute__ ((unused)) =
     37     "Copyright (c) 1996, 1997 Christopher G. Demetriou.  All rights reserved.";
     38 static const char _rcsid[] __attribute__ ((unused)) =
     39     "$NetBSD: wskbd.c,v 1.34 1999/12/21 12:02:04 drochner Exp $";
     40 
     41 /*
     42  * Copyright (c) 1992, 1993
     43  *	The Regents of the University of California.  All rights reserved.
     44  *
     45  * This software was developed by the Computer Systems Engineering group
     46  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     47  * contributed to Berkeley.
     48  *
     49  * All advertising materials mentioning features or use of this software
     50  * must display the following acknowledgement:
     51  *	This product includes software developed by the University of
     52  *	California, Lawrence Berkeley Laboratory.
     53  *
     54  * Redistribution and use in source and binary forms, with or without
     55  * modification, are permitted provided that the following conditions
     56  * are met:
     57  * 1. Redistributions of source code must retain the above copyright
     58  *    notice, this list of conditions and the following disclaimer.
     59  * 2. Redistributions in binary form must reproduce the above copyright
     60  *    notice, this list of conditions and the following disclaimer in the
     61  *    documentation and/or other materials provided with the distribution.
     62  * 3. All advertising materials mentioning features or use of this software
     63  *    must display the following acknowledgement:
     64  *	This product includes software developed by the University of
     65  *	California, Berkeley and its contributors.
     66  * 4. Neither the name of the University nor the names of its contributors
     67  *    may be used to endorse or promote products derived from this software
     68  *    without specific prior written permission.
     69  *
     70  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     71  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     72  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     73  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     74  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     75  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     76  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     77  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     78  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     79  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     80  * SUCH DAMAGE.
     81  *
     82  *	@(#)kbd.c	8.2 (Berkeley) 10/30/93
     83  */
     84 
     85 /*
     86  * Keyboard driver (/dev/wskbd*).  Translates incoming bytes to ASCII or
     87  * to `wscons_events' and passes them up to the appropriate reader.
     88  */
     89 
     90 #include "opt_ddb.h"
     91 
     92 #include <sys/param.h>
     93 #include <sys/conf.h>
     94 #include <sys/device.h>
     95 #include <sys/ioctl.h>
     96 #include <sys/kernel.h>
     97 #include <sys/proc.h>
     98 #include <sys/syslog.h>
     99 #include <sys/systm.h>
    100 #include <sys/malloc.h>
    101 #include <sys/tty.h>
    102 #include <sys/signalvar.h>
    103 #include <sys/errno.h>
    104 #include <sys/fcntl.h>
    105 #include <sys/vnode.h>
    106 
    107 #include <dev/wscons/wsconsio.h>
    108 #include <dev/wscons/wskbdvar.h>
    109 #include <dev/wscons/wsksymdef.h>
    110 #include <dev/wscons/wsksymvar.h>
    111 #include <dev/wscons/wseventvar.h>
    112 #include <dev/wscons/wscons_callbacks.h>
    113 
    114 #include "opt_wsdisplay_compat.h"
    115 
    116 #include "wsdisplay.h"
    117 #include "wsmux.h"
    118 
    119 #ifdef WSKBD_DEBUG
    120 #define DPRINTF(x)	if (wskbddebug) printf x
    121 int	wskbddebug = 0;
    122 #else
    123 #define DPRINTF(x)
    124 #endif
    125 
    126 #if NWSMUX > 0 || NWSDISPLAY > 0
    127 #include <dev/wscons/wsmuxvar.h>
    128 #endif
    129 
    130 struct wskbd_internal {
    131 	const struct wskbd_mapdata *t_keymap;
    132 
    133 	const struct wskbd_consops *t_consops;
    134 	void	*t_consaccesscookie;
    135 
    136 	int	t_modifiers;
    137 	int	t_composelen;		/* remaining entries in t_composebuf */
    138 	keysym_t t_composebuf[2];
    139 
    140 	int t_flags;
    141 #define WSKFL_METAESC 1
    142 
    143 #define MAXKEYSYMSPERKEY 2 /* ESC <key> at max */
    144 	keysym_t t_symbols[MAXKEYSYMSPERKEY];
    145 
    146 	struct wskbd_softc *t_sc;	/* back pointer */
    147 };
    148 
    149 struct wskbd_softc {
    150 	struct device	sc_dv;
    151 
    152 	struct wskbd_internal *id;
    153 
    154 	const struct wskbd_accessops *sc_accessops;
    155 	void *sc_accesscookie;
    156 
    157 	int	sc_ledstate;
    158 
    159 	int	sc_ready;		/* accepting events */
    160 	struct wseventvar sc_events;	/* event queue state */
    161 
    162 	int	sc_isconsole;
    163 #if NWSDISPLAY > 0
    164 	struct device	*sc_displaydv;
    165 #endif
    166 
    167 	struct wskbd_bell_data sc_bell_data;
    168 	struct wskbd_keyrepeat_data sc_keyrepeat_data;
    169 
    170 	int	sc_repeating;		/* we've called timeout() */
    171 
    172 	int	sc_translating;		/* xlate to chars for emulation */
    173 
    174 	int	sc_maplen;		/* number of entries in sc_map */
    175 	struct wscons_keymap *sc_map;	/* current translation map */
    176 	kbd_t sc_layout; /* current layout */
    177 
    178 	int		sc_refcnt;
    179 	u_char		sc_dying;	/* device is being detached */
    180 
    181 #if NWSMUX > 0 || NWSDISPLAY > 0
    182 	struct wsmux_softc *sc_mux;
    183 #endif
    184 };
    185 
    186 #define MOD_SHIFT_L		(1 << 0)
    187 #define MOD_SHIFT_R		(1 << 1)
    188 #define MOD_SHIFTLOCK		(1 << 2)
    189 #define MOD_CAPSLOCK		(1 << 3)
    190 #define MOD_CONTROL_L		(1 << 4)
    191 #define MOD_CONTROL_R		(1 << 5)
    192 #define MOD_META_L		(1 << 6)
    193 #define MOD_META_R		(1 << 7)
    194 #define MOD_MODESHIFT		(1 << 8)
    195 #define MOD_NUMLOCK		(1 << 9)
    196 #define MOD_COMPOSE		(1 << 10)
    197 #define MOD_HOLDSCREEN		(1 << 11)
    198 #define MOD_COMMAND		(1 << 12)
    199 #define MOD_COMMAND1		(1 << 13)
    200 #define MOD_COMMAND2		(1 << 14)
    201 
    202 #define MOD_ANYSHIFT		(MOD_SHIFT_L | MOD_SHIFT_R | MOD_SHIFTLOCK)
    203 #define MOD_ANYCONTROL		(MOD_CONTROL_L | MOD_CONTROL_R)
    204 #define MOD_ANYMETA		(MOD_META_L | MOD_META_R)
    205 
    206 #define MOD_ONESET(id, mask)	(((id)->t_modifiers & (mask)) != 0)
    207 #define MOD_ALLSET(id, mask)	(((id)->t_modifiers & (mask)) == (mask))
    208 
    209 int	wskbd_match __P((struct device *, struct cfdata *, void *));
    210 void	wskbd_attach __P((struct device *, struct device *, void *));
    211 int	wskbd_detach __P((struct device *, int));
    212 int	wskbd_activate __P((struct device *, enum devact));
    213 
    214 static int wskbd_displayioctl
    215 	    __P((struct device *, u_long, caddr_t, int, struct proc *p));
    216 int	wskbd_set_display __P((struct device *, struct wsmux_softc *));
    217 
    218 static inline void update_leds __P((struct wskbd_internal *));
    219 static inline void update_modifier __P((struct wskbd_internal *, u_int, int, int));
    220 static int internal_command __P((struct wskbd_softc *, u_int *, keysym_t));
    221 static int wskbd_translate __P((struct wskbd_internal *, u_int, int));
    222 static int wskbd_enable __P((struct wskbd_softc *, int));
    223 #if NWSDISPLAY > 0
    224 static void wskbd_holdscreen __P((struct wskbd_softc *, int));
    225 #endif
    226 
    227 int	wskbd_do_ioctl __P((struct wskbd_softc *, u_long, caddr_t,
    228 			    int, struct proc *));
    229 
    230 int	wskbddoclose __P((struct device *, int, int, struct proc *));
    231 int	wskbddoioctl __P((struct device *, u_long, caddr_t, int,
    232 			  struct proc *));
    233 
    234 struct cfattach wskbd_ca = {
    235 	sizeof (struct wskbd_softc), wskbd_match, wskbd_attach,
    236 	wskbd_detach, wskbd_activate
    237 };
    238 
    239 extern struct cfdriver wskbd_cd;
    240 
    241 #ifndef WSKBD_DEFAULT_BELL_PITCH
    242 #define	WSKBD_DEFAULT_BELL_PITCH	1500	/* 1500Hz */
    243 #endif
    244 #ifndef WSKBD_DEFAULT_BELL_PERIOD
    245 #define	WSKBD_DEFAULT_BELL_PERIOD	100	/* 100ms */
    246 #endif
    247 #ifndef WSKBD_DEFAULT_BELL_VOLUME
    248 #define	WSKBD_DEFAULT_BELL_VOLUME	50	/* 50% volume */
    249 #endif
    250 
    251 struct wskbd_bell_data wskbd_default_bell_data = {
    252 	WSKBD_BELL_DOALL,
    253 	WSKBD_DEFAULT_BELL_PITCH,
    254 	WSKBD_DEFAULT_BELL_PERIOD,
    255 	WSKBD_DEFAULT_BELL_VOLUME,
    256 };
    257 
    258 #ifndef WSKBD_DEFAULT_KEYREPEAT_DEL1
    259 #define	WSKBD_DEFAULT_KEYREPEAT_DEL1	400	/* 400ms to start repeating */
    260 #endif
    261 #ifndef WSKBD_DEFAULT_KEYREPEAT_DELN
    262 #define	WSKBD_DEFAULT_KEYREPEAT_DELN	100	/* 100ms to between repeats */
    263 #endif
    264 
    265 struct wskbd_keyrepeat_data wskbd_default_keyrepeat_data = {
    266 	WSKBD_KEYREPEAT_DOALL,
    267 	WSKBD_DEFAULT_KEYREPEAT_DEL1,
    268 	WSKBD_DEFAULT_KEYREPEAT_DELN,
    269 };
    270 
    271 cdev_decl(wskbd);
    272 
    273 #if NWSMUX > 0 || NWSDISPLAY > 0
    274 struct wsmuxops wskbd_muxops = {
    275 	wskbdopen, wskbddoclose, wskbddoioctl, wskbd_displayioctl,
    276 	wskbd_set_display
    277 };
    278 #endif
    279 
    280 #if NWSDISPLAY > 0
    281 static void wskbd_repeat __P((void *v));
    282 #endif
    283 
    284 static int wskbd_console_initted;
    285 static struct wskbd_softc *wskbd_console_device;
    286 static struct wskbd_internal wskbd_console_data;
    287 
    288 static void wskbd_update_layout __P((struct wskbd_internal *, kbd_t));
    289 
    290 static void
    291 wskbd_update_layout(id, enc)
    292 	struct wskbd_internal *id;
    293 	kbd_t enc;
    294 {
    295 
    296 	if (enc & KB_METAESC)
    297 		id->t_flags |= WSKFL_METAESC;
    298 	else
    299 		id->t_flags &= ~WSKFL_METAESC;
    300 }
    301 
    302 /*
    303  * Print function (for parent devices).
    304  */
    305 int
    306 wskbddevprint(aux, pnp)
    307 	void *aux;
    308 	const char *pnp;
    309 {
    310 #if 0
    311 	struct wskbddev_attach_args *ap = aux;
    312 #endif
    313 
    314 	if (pnp)
    315 		printf("wskbd at %s", pnp);
    316 #if 0
    317 	printf(" console %d", ap->console);
    318 #endif
    319 
    320 	return (UNCONF);
    321 }
    322 
    323 int
    324 wskbd_match(parent, match, aux)
    325 	struct device *parent;
    326 	struct cfdata *match;
    327 	void *aux;
    328 {
    329 	struct wskbddev_attach_args *ap = aux;
    330 
    331 	if (match->wskbddevcf_console != WSKBDDEVCF_CONSOLE_UNK) {
    332 		/*
    333 		 * If console-ness of device specified, either match
    334 		 * exactly (at high priority), or fail.
    335 		 */
    336 		if (match->wskbddevcf_console != 0 && ap->console != 0)
    337 			return (10);
    338 		else
    339 			return (0);
    340 	}
    341 
    342 	/* If console-ness unspecified, it wins. */
    343 	return (1);
    344 }
    345 
    346 void
    347 wskbd_attach(parent, self, aux)
    348 	struct device *parent, *self;
    349 	void *aux;
    350 {
    351 	struct wskbd_softc *sc = (struct wskbd_softc *)self;
    352 	struct wskbddev_attach_args *ap = aux;
    353 #if NWSMUX > 0 || NWSDISPLAY > 0
    354 	int mux;
    355 #endif
    356 
    357 #if NWSDISPLAY > 0
    358 	sc->sc_displaydv = NULL;
    359 #endif
    360 	sc->sc_isconsole = ap->console;
    361 
    362 #if NWSMUX > 0 || NWSDISPLAY > 0
    363 	mux = sc->sc_dv.dv_cfdata->wskbddevcf_mux;
    364 	if (sc->sc_isconsole && mux != WSKBDDEVCF_MUX_DEFAULT) {
    365 		printf(" (mux %d ignored for console)", mux);
    366 		mux = WSKBDDEVCF_MUX_DEFAULT;
    367 	}
    368 	if (mux != WSKBDDEVCF_MUX_DEFAULT)
    369 		printf(" mux %d", mux);
    370 #endif
    371 
    372 	if (ap->console) {
    373 		sc->id = &wskbd_console_data;
    374 	} else {
    375 		sc->id = malloc(sizeof(struct wskbd_internal),
    376 				M_DEVBUF, M_WAITOK);
    377 		bzero(sc->id, sizeof(struct wskbd_internal));
    378 		sc->id->t_keymap = ap->keymap;
    379 		wskbd_update_layout(sc->id, ap->keymap->layout);
    380 	}
    381 
    382 	sc->id->t_sc = sc;
    383 
    384 	sc->sc_accessops = ap->accessops;
    385 	sc->sc_accesscookie = ap->accesscookie;
    386 	sc->sc_ready = 0;				/* sanity */
    387 	sc->sc_repeating = 0;
    388 	sc->sc_translating = 1;
    389 	sc->sc_ledstate = -1; /* force update */
    390 
    391 	if (wskbd_load_keymap(sc->id->t_keymap,
    392 			      &sc->sc_map, &sc->sc_maplen) != 0)
    393 		panic("cannot load keymap");
    394 
    395 	sc->sc_layout = sc->id->t_keymap->layout;
    396 
    397 	/* set default bell and key repeat data */
    398 	sc->sc_bell_data = wskbd_default_bell_data;
    399 	sc->sc_keyrepeat_data = wskbd_default_keyrepeat_data;
    400 
    401 	if (ap->console) {
    402 		KASSERT(wskbd_console_initted);
    403 		KASSERT(wskbd_console_device == NULL);
    404 
    405 		wskbd_console_device = sc;
    406 
    407 		printf(": console keyboard");
    408 
    409 #if NWSDISPLAY > 0
    410 		if ((sc->sc_displaydv = wsdisplay_set_console_kbd(self)))
    411 			printf(", using %s", sc->sc_displaydv->dv_xname);
    412 #endif
    413 	}
    414 	printf("\n");
    415 
    416 #if NWSMUX > 0
    417 	if (mux != WSKBDDEVCF_MUX_DEFAULT)
    418 		wsmux_attach(mux, WSMUX_KBD, &sc->sc_dv, &sc->sc_events,
    419 			     &sc->sc_mux, &wskbd_muxops);
    420 #endif
    421 
    422 }
    423 
    424 void
    425 wskbd_cnattach(consops, conscookie, mapdata)
    426 	const struct wskbd_consops *consops;
    427 	void *conscookie;
    428 	const struct wskbd_mapdata *mapdata;
    429 {
    430 	KASSERT(!wskbd_console_initted);
    431 
    432 	wskbd_console_data.t_keymap = mapdata;
    433 	wskbd_update_layout(&wskbd_console_data, mapdata->layout);
    434 
    435 	wskbd_console_data.t_consops = consops;
    436 	wskbd_console_data.t_consaccesscookie = conscookie;
    437 
    438 #if NWSDISPLAY > 0
    439 	wsdisplay_set_cons_kbd(wskbd_cngetc, wskbd_cnpollc);
    440 #endif
    441 
    442 	wskbd_console_initted = 1;
    443 }
    444 
    445 void
    446 wskbd_cndetach()
    447 {
    448 	KASSERT(wskbd_console_initted);
    449 
    450 	wskbd_console_data.t_keymap = 0;
    451 
    452 	wskbd_console_data.t_consops = 0;
    453 	wskbd_console_data.t_consaccesscookie = 0;
    454 
    455 #if NWSDISPLAY > 0
    456 	wsdisplay_unset_cons_kbd();
    457 #endif
    458 
    459 	wskbd_console_initted = 0;
    460 }
    461 
    462 #if NWSDISPLAY > 0
    463 static void
    464 wskbd_repeat(v)
    465 	void *v;
    466 {
    467 	struct wskbd_softc *sc = (struct wskbd_softc *)v;
    468 	int s = spltty();
    469 
    470 	if (!sc->sc_repeating) {
    471 		/*
    472 		 * race condition: a "key up" event came in when wskbd_repeat()
    473 		 * was already called but not yet spltty()'d
    474 		 */
    475 		splx(s);
    476 		return;
    477 	}
    478 	if (sc->sc_displaydv != NULL) {
    479 		int i;
    480 		for (i = 0; i < sc->sc_repeating; i++)
    481 			wsdisplay_kbdinput(sc->sc_displaydv,
    482 					   sc->id->t_symbols[i]);
    483 	}
    484 	timeout(wskbd_repeat, sc,
    485 		(hz * sc->sc_keyrepeat_data.delN) / 1000);
    486 	splx(s);
    487 }
    488 #endif
    489 
    490 int
    491 wskbd_activate(self, act)
    492 	struct device *self;
    493 	enum devact act;
    494 {
    495 	/* XXX should we do something more? */
    496 	return (0);
    497 }
    498 
    499 /*
    500  * Detach a keyboard.  To keep track of users of the softc we keep
    501  * a reference count that's incremented while inside, e.g., read.
    502  * If the keyboard is active and the reference count is > 0 (0 is the
    503  * normal state) we post an event and then wait for the process
    504  * that had the reference to wake us up again.  Then we blow away the
    505  * vnode and return (which will deallocate the softc).
    506  */
    507 int
    508 wskbd_detach(self, flags)
    509 	struct device  *self;
    510 	int flags;
    511 {
    512 	struct wskbd_softc *sc = (struct wskbd_softc *)self;
    513 	struct wseventvar *evar;
    514 	int maj, mn;
    515 	int s;
    516 #if NWSMUX > 0
    517 	int mux;
    518 
    519 	mux = sc->sc_dv.dv_cfdata->wskbddevcf_mux;
    520 	if (mux != WSMOUSEDEVCF_MUX_DEFAULT)
    521 		wsmux_detach(mux, &sc->sc_dv);
    522 #endif
    523 
    524 	evar = &sc->sc_events;
    525 	if (evar->io) {
    526 		s = spltty();
    527 		if (--sc->sc_refcnt >= 0) {
    528 			/* Wake everyone by generating a dummy event. */
    529 			if (++evar->put >= WSEVENT_QSIZE)
    530 				evar->put = 0;
    531 			WSEVENT_WAKEUP(evar);
    532 			/* Wait for processes to go away. */
    533 			if (tsleep(sc, PZERO, "wskdet", hz * 60))
    534 				printf("wskbd_detach: %s didn't detach\n",
    535 				       sc->sc_dv.dv_xname);
    536 		}
    537 		splx(s);
    538 	}
    539 
    540 	/* locate the major number */
    541 	for (maj = 0; maj < nchrdev; maj++)
    542 		if (cdevsw[maj].d_open == wskbdopen)
    543 			break;
    544 
    545 	/* Nuke the vnodes for any open instances. */
    546 	mn = self->dv_unit;
    547 	vdevgone(maj, mn, mn, VCHR);
    548 
    549 	return (0);
    550 }
    551 
    552 void
    553 wskbd_input(dev, type, value)
    554 	struct device *dev;
    555 	u_int type;
    556 	int value;
    557 {
    558 	struct wskbd_softc *sc = (struct wskbd_softc *)dev;
    559 	struct wscons_event *ev;
    560 	struct wseventvar *evar;
    561 	struct timeval xxxtime;
    562 #if NWSDISPLAY > 0
    563 	int num, i;
    564 #endif
    565 	int put;
    566 
    567 #if NWSDISPLAY > 0
    568 	if (sc->sc_repeating) {
    569 		sc->sc_repeating = 0;
    570 		untimeout(wskbd_repeat, sc);
    571 	}
    572 
    573 	/*
    574 	 * If /dev/wskbd is not connected in event mode translate and
    575 	 * send upstream.
    576 	 */
    577 	if (sc->sc_translating) {
    578 		num = wskbd_translate(sc->id, type, value);
    579 		if (num > 0) {
    580 			if (sc->sc_displaydv != NULL) {
    581 				for (i = 0; i < num; i++)
    582 					wsdisplay_kbdinput(sc->sc_displaydv,
    583 						sc->id->t_symbols[i]);
    584 			}
    585 
    586 			sc->sc_repeating = num;
    587 			timeout(wskbd_repeat, sc,
    588 				(hz * sc->sc_keyrepeat_data.del1) / 1000);
    589 		}
    590 		return;
    591 	}
    592 #endif
    593 
    594 	/*
    595 	 * Keyboard is generating events.  Turn this keystroke into an
    596 	 * event and put it in the queue.  If the queue is full, the
    597 	 * keystroke is lost (sorry!).
    598 	 */
    599 
    600 	/* no one to receive; punt!*/
    601 	if (!sc->sc_ready)
    602 		return;
    603 
    604 #if NWSMUX > 0
    605 	if (sc->sc_mux)
    606 		evar = &sc->sc_mux->sc_events;
    607 	else
    608 #endif
    609 		evar = &sc->sc_events;
    610 
    611 	put = evar->put;
    612 	ev = &evar->q[put];
    613 	put = (put + 1) % WSEVENT_QSIZE;
    614 	if (put == evar->get) {
    615 		log(LOG_WARNING, "%s: event queue overflow\n",
    616 		    sc->sc_dv.dv_xname);
    617 		return;
    618 	}
    619 	ev->type = type;
    620 	ev->value = value;
    621 	microtime(&xxxtime);
    622 	TIMEVAL_TO_TIMESPEC(&xxxtime, &ev->time);
    623 	evar->put = put;
    624 	WSEVENT_WAKEUP(evar);
    625 }
    626 
    627 #ifdef WSDISPLAY_COMPAT_RAWKBD
    628 void
    629 wskbd_rawinput(dev, buf, len)
    630 	struct device *dev;
    631 	char *buf;
    632 	int len;
    633 {
    634 #if NWSDISPLAY > 0
    635 	struct wskbd_softc *sc = (struct wskbd_softc *)dev;
    636 	int i;
    637 
    638 	for (i = 0; i < len; i++)
    639 		wsdisplay_kbdinput(sc->sc_displaydv, buf[i]);
    640 	/* this is KS_GROUP_Ascii */
    641 #endif
    642 }
    643 #endif /* WSDISPLAY_COMPAT_RAWKBD */
    644 
    645 #if NWSDISPLAY > 0
    646 static void
    647 wskbd_holdscreen(sc, hold)
    648 	struct wskbd_softc *sc;
    649 	int hold;
    650 {
    651 	int new_state;
    652 
    653 	if (sc->sc_displaydv != NULL) {
    654 		wsdisplay_kbdholdscreen(sc->sc_displaydv, hold);
    655 		new_state = sc->sc_ledstate;
    656 		if (hold)
    657 			new_state |= WSKBD_LED_SCROLL;
    658 		else
    659 			new_state &= ~WSKBD_LED_SCROLL;
    660 		if (new_state != sc->sc_ledstate) {
    661 			(*sc->sc_accessops->set_leds)(sc->sc_accesscookie,
    662 						      new_state);
    663 			sc->sc_ledstate = new_state;
    664 		}
    665 	}
    666 }
    667 #endif
    668 
    669 static int
    670 wskbd_enable(sc, on)
    671 	struct wskbd_softc *sc;
    672 	int on;
    673 {
    674 	int res;
    675 
    676 	/* XXX reference count? */
    677 	if (!on && (!sc->sc_translating
    678 #if NWSDISPLAY > 0
    679 		    || sc->sc_displaydv
    680 #endif
    681 		))
    682 		return (EBUSY);
    683 
    684 	res = (*sc->sc_accessops->enable)(sc->sc_accesscookie, on);
    685 	return (res);
    686 }
    687 
    688 int
    689 wskbdopen(dev, flags, mode, p)
    690 	dev_t dev;
    691 	int flags, mode;
    692 	struct proc *p;
    693 {
    694 	struct wskbd_softc *sc;
    695 	int unit;
    696 
    697 	unit = minor(dev);
    698 	if (unit >= wskbd_cd.cd_ndevs ||	/* make sure it was attached */
    699 	    (sc = wskbd_cd.cd_devs[unit]) == NULL)
    700 		return (ENXIO);
    701 
    702 	if (sc->sc_dying)
    703 		return (EIO);
    704 
    705 	if (!(flags & FREAD)) {
    706 		/* Not opening for read, only ioctl is available. */
    707 		return (0);
    708 	}
    709 
    710 #if NWSMUX > 0
    711 	if (sc->sc_mux)
    712 		return (EBUSY);
    713 #endif
    714 
    715 	if (sc->sc_events.io)			/* and that it's not in use */
    716 		return (EBUSY);
    717 
    718 	sc->sc_events.io = p;
    719 	wsevent_init(&sc->sc_events);		/* may cause sleep */
    720 
    721 	sc->sc_translating = 0;
    722 	sc->sc_ready = 1;			/* start accepting events */
    723 
    724 	wskbd_enable(sc, 1);
    725 	return (0);
    726 }
    727 
    728 int
    729 wskbdclose(dev, flags, mode, p)
    730 	dev_t dev;
    731 	int flags, mode;
    732 	struct proc *p;
    733 {
    734 	return (wskbddoclose(wskbd_cd.cd_devs[minor(dev)], flags, mode, p));
    735 }
    736 
    737 int
    738 wskbddoclose(dv, flags, mode, p)
    739 	struct device *dv;
    740 	int flags, mode;
    741 	struct proc *p;
    742 {
    743 	struct wskbd_softc *sc = (struct wskbd_softc *)dv;
    744 
    745 	if (!(flags & FREAD)) {
    746 		/* Nothing to do, because open didn't do anything. */
    747 		return (0);
    748 	}
    749 
    750 	sc->sc_ready = 0;			/* stop accepting events */
    751 	sc->sc_translating = 1;
    752 
    753 	wsevent_fini(&sc->sc_events);
    754 	sc->sc_events.io = NULL;
    755 
    756 	wskbd_enable(sc, 0);
    757 	return (0);
    758 }
    759 
    760 int
    761 wskbdread(dev, uio, flags)
    762 	dev_t dev;
    763 	struct uio *uio;
    764 	int flags;
    765 {
    766 	struct wskbd_softc *sc = wskbd_cd.cd_devs[minor(dev)];
    767 	int error;
    768 
    769 	if (sc->sc_dying)
    770 		return (EIO);
    771 
    772 	sc->sc_refcnt++;
    773 	error = wsevent_read(&sc->sc_events, uio, flags);
    774 	if (--sc->sc_refcnt < 0) {
    775 		wakeup(sc);
    776 		error = EIO;
    777 	}
    778 	return (error);
    779 }
    780 
    781 int
    782 wskbdioctl(dev, cmd, data, flag, p)
    783 	dev_t dev;
    784 	u_long cmd;
    785 	caddr_t data;
    786 	int flag;
    787 	struct proc *p;
    788 {
    789 	return (wskbddoioctl(wskbd_cd.cd_devs[minor(dev)], cmd, data, flag,p));
    790 }
    791 
    792 /* A wrapper around the ioctl() workhorse to make reference counting easy. */
    793 int
    794 wskbddoioctl(dv, cmd, data, flag, p)
    795 	struct device *dv;
    796 	u_long cmd;
    797 	caddr_t data;
    798 	int flag;
    799 	struct proc *p;
    800 {
    801 	struct wskbd_softc *sc = (struct wskbd_softc *)dv;
    802 	int error;
    803 
    804 	sc->sc_refcnt++;
    805 	error = wskbd_do_ioctl(sc, cmd, data, flag, p);
    806 	if (--sc->sc_refcnt < 0)
    807 		wakeup(sc);
    808 	return (error);
    809 }
    810 
    811 int
    812 wskbd_do_ioctl(sc, cmd, data, flag, p)
    813 	struct wskbd_softc *sc;
    814 	u_long cmd;
    815 	caddr_t data;
    816 	int flag;
    817 	struct proc *p;
    818 {
    819 	int error;
    820 
    821 	/*
    822 	 * Try the generic ioctls that the wskbd interface supports.
    823 	 */
    824 	switch (cmd) {
    825 	case FIONBIO:		/* we will remove this someday (soon???) */
    826 		return (0);
    827 
    828 	case FIOASYNC:
    829 		sc->sc_events.async = *(int *)data != 0;
    830 		return (0);
    831 
    832 	case TIOCSPGRP:
    833 		if (*(int *)data != sc->sc_events.io->p_pgid)
    834 			return (EPERM);
    835 		return (0);
    836 	}
    837 
    838 	/*
    839 	 * Try the keyboard driver for WSKBDIO ioctls.  It returns -1
    840 	 * if it didn't recognize the request.
    841 	 */
    842 	error = wskbd_displayioctl((struct device *)sc, cmd, data, flag, p);
    843 	return (error != -1 ? error : ENOTTY);
    844 }
    845 
    846 /*
    847  * WSKBDIO ioctls, handled in both emulation mode and in ``raw'' mode.
    848  * Some of these have no real effect in raw mode, however.
    849  */
    850 static int
    851 wskbd_displayioctl(dev, cmd, data, flag, p)
    852 	struct device *dev;
    853 	u_long cmd;
    854 	caddr_t data;
    855 	int flag;
    856 	struct proc *p;
    857 {
    858 	struct wskbd_softc *sc = (struct wskbd_softc *)dev;
    859 	struct wskbd_bell_data *ubdp, *kbdp;
    860 	struct wskbd_keyrepeat_data *ukdp, *kkdp;
    861 	struct wskbd_map_data *umdp;
    862 	struct wskbd_mapdata md;
    863 	kbd_t enc;
    864 	void *buf;
    865 	int len, error;
    866 
    867 	switch (cmd) {
    868 #define	SETBELL(dstp, srcp, dfltp)					\
    869     do {								\
    870 	(dstp)->pitch = ((srcp)->which & WSKBD_BELL_DOPITCH) ?		\
    871 	    (srcp)->pitch : (dfltp)->pitch;				\
    872 	(dstp)->period = ((srcp)->which & WSKBD_BELL_DOPERIOD) ?	\
    873 	    (srcp)->period : (dfltp)->period;				\
    874 	(dstp)->volume = ((srcp)->which & WSKBD_BELL_DOVOLUME) ?	\
    875 	    (srcp)->volume : (dfltp)->volume;				\
    876 	(dstp)->which = WSKBD_BELL_DOALL;				\
    877     } while (0)
    878 
    879 	case WSKBDIO_BELL:
    880 		if ((flag & FWRITE) == 0)
    881 			return (EACCES);
    882 		return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
    883 		    WSKBDIO_COMPLEXBELL, (caddr_t)&sc->sc_bell_data, flag, p));
    884 
    885 	case WSKBDIO_COMPLEXBELL:
    886 		if ((flag & FWRITE) == 0)
    887 			return (EACCES);
    888 		ubdp = (struct wskbd_bell_data *)data;
    889 		SETBELL(ubdp, ubdp, &sc->sc_bell_data);
    890 		return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
    891 		    WSKBDIO_COMPLEXBELL, (caddr_t)ubdp, flag, p));
    892 
    893 	case WSKBDIO_SETBELL:
    894 		if ((flag & FWRITE) == 0)
    895 			return (EACCES);
    896 		kbdp = &sc->sc_bell_data;
    897 setbell:
    898 		ubdp = (struct wskbd_bell_data *)data;
    899 		SETBELL(kbdp, ubdp, kbdp);
    900 		return (0);
    901 
    902 	case WSKBDIO_GETBELL:
    903 		kbdp = &sc->sc_bell_data;
    904 getbell:
    905 		ubdp = (struct wskbd_bell_data *)data;
    906 		SETBELL(ubdp, kbdp, kbdp);
    907 		return (0);
    908 
    909 	case WSKBDIO_SETDEFAULTBELL:
    910 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    911 			return (error);
    912 		kbdp = &wskbd_default_bell_data;
    913 		goto setbell;
    914 
    915 
    916 	case WSKBDIO_GETDEFAULTBELL:
    917 		kbdp = &wskbd_default_bell_data;
    918 		goto getbell;
    919 
    920 #undef SETBELL
    921 
    922 #define	SETKEYREPEAT(dstp, srcp, dfltp)					\
    923     do {								\
    924 	(dstp)->del1 = ((srcp)->which & WSKBD_KEYREPEAT_DODEL1) ?	\
    925 	    (srcp)->del1 : (dfltp)->del1;				\
    926 	(dstp)->delN = ((srcp)->which & WSKBD_KEYREPEAT_DODELN) ?	\
    927 	    (srcp)->delN : (dfltp)->delN;				\
    928 	(dstp)->which = WSKBD_KEYREPEAT_DOALL;				\
    929     } while (0)
    930 
    931 	case WSKBDIO_SETKEYREPEAT:
    932 		if ((flag & FWRITE) == 0)
    933 			return (EACCES);
    934 		kkdp = &sc->sc_keyrepeat_data;
    935 setkeyrepeat:
    936 		ukdp = (struct wskbd_keyrepeat_data *)data;
    937 		SETKEYREPEAT(kkdp, ukdp, kkdp);
    938 		return (0);
    939 
    940 	case WSKBDIO_GETKEYREPEAT:
    941 		kkdp = &sc->sc_keyrepeat_data;
    942 getkeyrepeat:
    943 		ukdp = (struct wskbd_keyrepeat_data *)data;
    944 		SETKEYREPEAT(ukdp, kkdp, kkdp);
    945 		return (0);
    946 
    947 	case WSKBDIO_SETDEFAULTKEYREPEAT:
    948 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    949 			return (error);
    950 		kkdp = &wskbd_default_keyrepeat_data;
    951 		goto setkeyrepeat;
    952 
    953 
    954 	case WSKBDIO_GETDEFAULTKEYREPEAT:
    955 		kkdp = &wskbd_default_keyrepeat_data;
    956 		goto getkeyrepeat;
    957 
    958 #undef SETKEYREPEAT
    959 
    960 	case WSKBDIO_SETMAP:
    961 		if ((flag & FWRITE) == 0)
    962 			return (EACCES);
    963 		umdp = (struct wskbd_map_data *)data;
    964 		len = umdp->maplen*sizeof(struct wscons_keymap);
    965 		buf = malloc(len, M_TEMP, M_WAITOK);
    966 		error = copyin(umdp->map, buf, len);
    967 		if (error == 0) {
    968 			wskbd_init_keymap(umdp->maplen,
    969 					  &sc->sc_map, &sc->sc_maplen);
    970 			memcpy(sc->sc_map, buf, len);
    971 			/* drop the variant bits handled by the map */
    972 			sc->sc_layout = KB_USER |
    973 			      (KB_VARIANT(sc->sc_layout) & KB_HANDLEDBYWSKBD);
    974 			wskbd_update_layout(sc->id, sc->sc_layout);
    975 		}
    976 		free(buf, M_TEMP);
    977 		return(error);
    978 
    979 	case WSKBDIO_GETMAP:
    980 		umdp = (struct wskbd_map_data *)data;
    981 		if (umdp->maplen > sc->sc_maplen)
    982 			umdp->maplen = sc->sc_maplen;
    983 		error = copyout(sc->sc_map, umdp->map,
    984 				umdp->maplen*sizeof(struct wscons_keymap));
    985 		return(error);
    986 
    987 	case WSKBDIO_GETENCODING:
    988 		*((kbd_t *) data) = sc->sc_layout;
    989 		return(0);
    990 
    991 	case WSKBDIO_SETENCODING:
    992 		if ((flag & FWRITE) == 0)
    993 			return (EACCES);
    994 		enc = *((kbd_t *)data);
    995 		if (KB_ENCODING(enc) == KB_USER) {
    996 			/* user map must already be loaded */
    997 			if (KB_ENCODING(sc->sc_layout) != KB_USER)
    998 				return (EINVAL);
    999 			/* map variants make no sense */
   1000 			if (KB_VARIANT(enc) & ~KB_HANDLEDBYWSKBD)
   1001 				return (EINVAL);
   1002 		} else {
   1003 			md = *(sc->id->t_keymap); /* structure assignment */
   1004 			md.layout = enc;
   1005 			error = wskbd_load_keymap(&md, &sc->sc_map,
   1006 						  &sc->sc_maplen);
   1007 			if (error)
   1008 				return (error);
   1009 		}
   1010 		sc->sc_layout = enc;
   1011 		wskbd_update_layout(sc->id, enc);
   1012 		return (0);
   1013 	}
   1014 
   1015 	/*
   1016 	 * Try the keyboard driver for WSKBDIO ioctls.  It returns -1
   1017 	 * if it didn't recognize the request, and in turn we return
   1018 	 * -1 if we didn't recognize the request.
   1019 	 */
   1020 /* printf("kbdaccess\n"); */
   1021 	error = (*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd, data,
   1022 					   flag, p);
   1023 #ifdef WSDISPLAY_COMPAT_RAWKBD
   1024 	if (!error && cmd == WSKBDIO_SETMODE && *(int *)data == WSKBD_RAW) {
   1025 		int s = spltty();
   1026 		sc->id->t_modifiers &= ~(MOD_SHIFT_L | MOD_SHIFT_R
   1027 					 | MOD_CONTROL_L | MOD_CONTROL_R
   1028 					 | MOD_META_L | MOD_META_R
   1029 					 | MOD_COMMAND
   1030 					 | MOD_COMMAND1 | MOD_COMMAND2);
   1031 #if NWSDISPLAY > 0
   1032 		if (sc->sc_repeating) {
   1033 			sc->sc_repeating = 0;
   1034 			untimeout(wskbd_repeat, sc);
   1035 		}
   1036 #endif
   1037 		splx(s);
   1038 	}
   1039 #endif
   1040 	return (error);
   1041 }
   1042 
   1043 int
   1044 wskbdpoll(dev, events, p)
   1045 	dev_t dev;
   1046 	int events;
   1047 	struct proc *p;
   1048 {
   1049 	struct wskbd_softc *sc = wskbd_cd.cd_devs[minor(dev)];
   1050 
   1051 	return (wsevent_poll(&sc->sc_events, events, p));
   1052 }
   1053 
   1054 #if NWSDISPLAY > 0
   1055 
   1056 int
   1057 wskbd_pickfree()
   1058 {
   1059 	int i;
   1060 	struct wskbd_softc *sc;
   1061 
   1062 	for (i = 0; i < wskbd_cd.cd_ndevs; i++) {
   1063 		if ((sc = wskbd_cd.cd_devs[i]) == NULL)
   1064 			continue;
   1065 		if (sc->sc_displaydv == NULL)
   1066 			return (i);
   1067 	}
   1068 	return (-1);
   1069 }
   1070 
   1071 struct device *
   1072 wskbd_set_console_display(displaydv, muxsc)
   1073 	struct device *displaydv;
   1074 	struct wsmux_softc *muxsc;
   1075 {
   1076 	struct wskbd_softc *sc = wskbd_console_device;
   1077 
   1078 	if (!sc)
   1079 		return (0);
   1080 	sc->sc_displaydv = displaydv;
   1081 	(void)wsmux_attach_sc(muxsc, WSMUX_KBD, &sc->sc_dv, &sc->sc_events,
   1082 			      &sc->sc_mux, &wskbd_muxops);
   1083 	return (&sc->sc_dv);
   1084 }
   1085 
   1086 int
   1087 wskbd_set_display(dv, muxsc)
   1088 	struct device *dv;
   1089 	struct wsmux_softc *muxsc;
   1090 {
   1091 	struct wskbd_softc *sc = (struct wskbd_softc *)dv;
   1092 	struct device *displaydv = muxsc ? muxsc->sc_displaydv : 0;
   1093 	struct device *odisplaydv;
   1094 	int error;
   1095 
   1096 	DPRINTF(("wskbd_set_display: %s mux=%p disp=%p odisp=%p cons=%d\n",
   1097 		 dv->dv_xname, muxsc, sc->sc_displaydv, displaydv,
   1098 		 sc->sc_isconsole));
   1099 
   1100 	if (sc->sc_isconsole)
   1101 		return (EBUSY);
   1102 
   1103 	if (displaydv) {
   1104 		if (sc->sc_displaydv)
   1105 			return (EBUSY);
   1106 	} else {
   1107 		if (sc->sc_displaydv == NULL)
   1108 			return (ENXIO);
   1109 	}
   1110 
   1111 	odisplaydv = sc->sc_displaydv;
   1112 	sc->sc_displaydv = displaydv;
   1113 
   1114 	error = wskbd_enable(sc, displaydv != NULL);
   1115 	if (error) {
   1116 		sc->sc_displaydv = odisplaydv;
   1117 		return (error);
   1118 	}
   1119 
   1120 	if (displaydv)
   1121 		printf("%s: connecting to %s\n",
   1122 		       sc->sc_dv.dv_xname, displaydv->dv_xname);
   1123 	else
   1124 		printf("%s: disconnecting from %s\n",
   1125 		       sc->sc_dv.dv_xname, odisplaydv->dv_xname);
   1126 
   1127 	return (0);
   1128 }
   1129 
   1130 int
   1131 wskbd_add_mux(unit, muxsc)
   1132 	int unit;
   1133 	struct wsmux_softc *muxsc;
   1134 {
   1135 	struct wskbd_softc *sc;
   1136 
   1137 	DPRINTF(("wskbd_add_mux: %d %s %p\n", unit, muxsc->sc_dv.dv_xname,
   1138 		 muxsc->sc_displaydv));
   1139 	if (unit < 0 || unit >= wskbd_cd.cd_ndevs ||
   1140 	    (sc = wskbd_cd.cd_devs[unit]) == NULL)
   1141 		return (ENXIO);
   1142 
   1143 	if (sc->sc_mux || sc->sc_events.io)
   1144 		return (EBUSY);
   1145 
   1146 	return (wsmux_attach_sc(muxsc, WSMUX_KBD, &sc->sc_dv, &sc->sc_events,
   1147 				&sc->sc_mux, &wskbd_muxops));
   1148 }
   1149 
   1150 int
   1151 wskbd_rem_mux(unit, muxsc)
   1152 	int unit;
   1153 	struct wsmux_softc *muxsc;
   1154 {
   1155 	struct wskbd_softc *sc;
   1156 
   1157 	DPRINTF(("wskbd_rem_mux: %d %s\n", unit, muxsc->sc_dv.dv_xname));
   1158 	if (unit < 0 || unit >= wskbd_cd.cd_ndevs ||
   1159 	    (sc = wskbd_cd.cd_devs[unit]) == NULL)
   1160 		return (ENXIO);
   1161 
   1162 	return (wsmux_detach_sc(muxsc, &sc->sc_dv));
   1163 }
   1164 
   1165 #endif /* NWSDISPLAY > 0 */
   1166 
   1167 /*
   1168  * Console interface.
   1169  */
   1170 int
   1171 wskbd_cngetc(dev)
   1172 	dev_t dev;
   1173 {
   1174 	static int num = 0;
   1175 	static int pos;
   1176 	u_int type;
   1177 	int data;
   1178 	keysym_t ks;
   1179 
   1180 	if (!wskbd_console_initted)
   1181 		return 0;
   1182 
   1183 	if (wskbd_console_device != NULL &&
   1184 	    !wskbd_console_device->sc_translating)
   1185 		return 0;
   1186 
   1187 	for(;;) {
   1188 		if (num-- > 0) {
   1189 			ks = wskbd_console_data.t_symbols[pos++];
   1190 			if (KS_GROUP(ks) == KS_GROUP_Ascii)
   1191 				return (KS_VALUE(ks));
   1192 		} else {
   1193 			(*wskbd_console_data.t_consops->getc)
   1194 				(wskbd_console_data.t_consaccesscookie,
   1195 				 &type, &data);
   1196 			num = wskbd_translate(&wskbd_console_data, type, data);
   1197 			pos = 0;
   1198 		}
   1199 	}
   1200 }
   1201 
   1202 void
   1203 wskbd_cnpollc(dev, poll)
   1204 	dev_t dev;
   1205 	int poll;
   1206 {
   1207 
   1208 	if (!wskbd_console_initted)
   1209 		return;
   1210 
   1211 	if (wskbd_console_device != NULL &&
   1212 	    !wskbd_console_device->sc_translating)
   1213 		return;
   1214 
   1215 	(*wskbd_console_data.t_consops->pollc)
   1216 	    (wskbd_console_data.t_consaccesscookie, poll);
   1217 }
   1218 
   1219 static inline void
   1220 update_leds(id)
   1221 	struct wskbd_internal *id;
   1222 {
   1223 	int new_state;
   1224 
   1225 	new_state = 0;
   1226 	if (id->t_modifiers & (MOD_SHIFTLOCK | MOD_CAPSLOCK))
   1227 		new_state |= WSKBD_LED_CAPS;
   1228 	if (id->t_modifiers & MOD_NUMLOCK)
   1229 		new_state |= WSKBD_LED_NUM;
   1230 	if (id->t_modifiers & MOD_COMPOSE)
   1231 		new_state |= WSKBD_LED_COMPOSE;
   1232 	if (id->t_modifiers & MOD_HOLDSCREEN)
   1233 		new_state |= WSKBD_LED_SCROLL;
   1234 
   1235 	if (id->t_sc && new_state != id->t_sc->sc_ledstate) {
   1236 		(*id->t_sc->sc_accessops->set_leds)
   1237 		    (id->t_sc->sc_accesscookie, new_state);
   1238 		id->t_sc->sc_ledstate = new_state;
   1239 	}
   1240 }
   1241 
   1242 static inline void
   1243 update_modifier(id, type, toggle, mask)
   1244 	struct wskbd_internal *id;
   1245 	u_int type;
   1246 	int toggle;
   1247 	int mask;
   1248 {
   1249 	if (toggle) {
   1250 		if (type == WSCONS_EVENT_KEY_DOWN)
   1251 			id->t_modifiers ^= mask;
   1252 	} else {
   1253 		if (type == WSCONS_EVENT_KEY_DOWN)
   1254 			id->t_modifiers |= mask;
   1255 		else
   1256 			id->t_modifiers &= ~mask;
   1257 	}
   1258 }
   1259 
   1260 static int
   1261 internal_command(sc, type, ksym)
   1262 	struct wskbd_softc *sc;
   1263 	u_int *type;
   1264 	keysym_t ksym;
   1265 {
   1266 	switch (ksym) {
   1267 	case KS_Cmd:
   1268 		update_modifier(sc->id, *type, 0, MOD_COMMAND);
   1269 		break;
   1270 
   1271 	case KS_Cmd1:
   1272 		update_modifier(sc->id, *type, 0, MOD_COMMAND1);
   1273 		break;
   1274 
   1275 	case KS_Cmd2:
   1276 		update_modifier(sc->id, *type, 0, MOD_COMMAND2);
   1277 		break;
   1278 	}
   1279 
   1280 	if (*type != WSCONS_EVENT_KEY_DOWN ||
   1281 	    (! MOD_ONESET(sc->id, MOD_COMMAND) &&
   1282 	     ! MOD_ALLSET(sc->id, MOD_COMMAND1 | MOD_COMMAND2)))
   1283 		return (0);
   1284 
   1285 	switch (ksym) {
   1286 #ifdef DDB
   1287 	case KS_Cmd_Debugger:
   1288 		if (sc->sc_isconsole)
   1289 			console_debugger();
   1290 		/* discard this key (ddb discarded command modifiers) */
   1291 		*type = WSCONS_EVENT_KEY_UP;
   1292 		return (1);
   1293 #endif
   1294 
   1295 #if NWSDISPLAY > 0
   1296 	case KS_Cmd_Screen0:
   1297 	case KS_Cmd_Screen1:
   1298 	case KS_Cmd_Screen2:
   1299 	case KS_Cmd_Screen3:
   1300 	case KS_Cmd_Screen4:
   1301 	case KS_Cmd_Screen5:
   1302 	case KS_Cmd_Screen6:
   1303 	case KS_Cmd_Screen7:
   1304 	case KS_Cmd_Screen8:
   1305 	case KS_Cmd_Screen9:
   1306 		wsdisplay_switch(sc->sc_displaydv, ksym - KS_Cmd_Screen0, 0);
   1307 		return (1);
   1308 	case KS_Cmd_ResetEmul:
   1309 		wsdisplay_reset(sc->sc_displaydv, WSDISPLAY_RESETEMUL);
   1310 		return (1);
   1311 	case KS_Cmd_ResetClose:
   1312 		wsdisplay_reset(sc->sc_displaydv, WSDISPLAY_RESETCLOSE);
   1313 		return (1);
   1314 #endif
   1315 	}
   1316 	return (0);
   1317 }
   1318 
   1319 static int
   1320 wskbd_translate(id, type, value)
   1321 	struct wskbd_internal *id;
   1322 	u_int type;
   1323 	int value;
   1324 {
   1325 	struct wskbd_softc *sc = id->t_sc;
   1326 	keysym_t ksym, res, *group;
   1327 	struct wscons_keymap kpbuf, *kp;
   1328 	int iscommand = 0;
   1329 
   1330 	if (type == WSCONS_EVENT_ALL_KEYS_UP) {
   1331 		id->t_modifiers &= ~(MOD_SHIFT_L | MOD_SHIFT_R
   1332 				| MOD_CONTROL_L | MOD_CONTROL_R
   1333 				| MOD_META_L | MOD_META_R
   1334 				| MOD_MODESHIFT
   1335 				| MOD_COMMAND | MOD_COMMAND1 | MOD_COMMAND2);
   1336 		update_leds(id);
   1337 		return (0);
   1338 	}
   1339 
   1340 	if (sc != NULL) {
   1341 		if (value < 0 || value >= sc->sc_maplen) {
   1342 #ifdef DEBUG
   1343 			printf("wskbd_translate: keycode %d out of range\n",
   1344 			       value);
   1345 #endif
   1346 			return (0);
   1347 		}
   1348 		kp = sc->sc_map + value;
   1349 	} else {
   1350 		kp = &kpbuf;
   1351 		wskbd_get_mapentry(id->t_keymap, value, kp);
   1352 	}
   1353 
   1354 	/* if this key has a command, process it first */
   1355 	if (sc != NULL && kp->command != KS_voidSymbol)
   1356 		iscommand = internal_command(sc, &type, kp->command);
   1357 
   1358 	/* Now update modifiers */
   1359 	switch (kp->group1[0]) {
   1360 	case KS_Shift_L:
   1361 		update_modifier(id, type, 0, MOD_SHIFT_L);
   1362 		break;
   1363 
   1364 	case KS_Shift_R:
   1365 		update_modifier(id, type, 0, MOD_SHIFT_R);
   1366 		break;
   1367 
   1368 	case KS_Shift_Lock:
   1369 		update_modifier(id, type, 1, MOD_SHIFTLOCK);
   1370 		break;
   1371 
   1372 	case KS_Caps_Lock:
   1373 		update_modifier(id, type, 1, MOD_CAPSLOCK);
   1374 		break;
   1375 
   1376 	case KS_Control_L:
   1377 		update_modifier(id, type, 0, MOD_CONTROL_L);
   1378 		break;
   1379 
   1380 	case KS_Control_R:
   1381 		update_modifier(id, type, 0, MOD_CONTROL_R);
   1382 		break;
   1383 
   1384 	case KS_Alt_L:
   1385 		update_modifier(id, type, 0, MOD_META_L);
   1386 		break;
   1387 
   1388 	case KS_Alt_R:
   1389 		update_modifier(id, type, 0, MOD_META_R);
   1390 		break;
   1391 
   1392 	case KS_Mode_switch:
   1393 		update_modifier(id, type, 0, MOD_MODESHIFT);
   1394 		break;
   1395 
   1396 	case KS_Num_Lock:
   1397 		update_modifier(id, type, 1, MOD_NUMLOCK);
   1398 		break;
   1399 
   1400 #if NWSDISPLAY > 0
   1401 	case KS_Hold_Screen:
   1402 		if (sc != NULL) {
   1403 			update_modifier(id, type, 1, MOD_HOLDSCREEN);
   1404 			wskbd_holdscreen(sc, id->t_modifiers & MOD_HOLDSCREEN);
   1405 		}
   1406 		break;
   1407 #endif
   1408 	}
   1409 
   1410 	/* If this is a key release or we are in command mode, we are done */
   1411 	if (type != WSCONS_EVENT_KEY_DOWN || iscommand) {
   1412 		update_leds(id);
   1413 		return (0);
   1414 	}
   1415 
   1416 	/* Get the keysym */
   1417 	if (id->t_modifiers & MOD_MODESHIFT)
   1418 		group = & kp->group2[0];
   1419 	else
   1420 		group = & kp->group1[0];
   1421 
   1422 	if ((id->t_modifiers & MOD_NUMLOCK) != 0 &&
   1423 	    KS_GROUP(group[1]) == KS_GROUP_Keypad) {
   1424 		if (MOD_ONESET(id, MOD_ANYSHIFT))
   1425 			ksym = group[0];
   1426 		else
   1427 			ksym = group[1];
   1428 	} else if (! MOD_ONESET(id, MOD_ANYSHIFT | MOD_CAPSLOCK)) {
   1429 		ksym = group[0];
   1430 	} else if (MOD_ONESET(id, MOD_CAPSLOCK)) {
   1431 		if (! MOD_ONESET(id, MOD_SHIFT_L | MOD_SHIFT_R))
   1432 			ksym = group[0];
   1433 		else
   1434 			ksym = group[1];
   1435 		if (ksym >= KS_a && ksym <= KS_z)
   1436 			ksym += KS_A - KS_a;
   1437 		else if (ksym >= KS_agrave && ksym <= KS_thorn &&
   1438 			 ksym != KS_division)
   1439 			ksym += KS_Agrave - KS_agrave;
   1440 	} else if (MOD_ONESET(id, MOD_ANYSHIFT)) {
   1441 		ksym = group[1];
   1442 	} else {
   1443 		ksym = group[0];
   1444 	}
   1445 
   1446 	/* Process compose sequence and dead accents */
   1447 	res = KS_voidSymbol;
   1448 
   1449 	switch (KS_GROUP(ksym)) {
   1450 	case KS_GROUP_Ascii:
   1451 	case KS_GROUP_Keypad:
   1452 	case KS_GROUP_Function:
   1453 		res = ksym;
   1454 		break;
   1455 
   1456 	case KS_GROUP_Mod:
   1457 		if (ksym == KS_Multi_key) {
   1458 			update_modifier(id, 1, 0, MOD_COMPOSE);
   1459 			id->t_composelen = 2;
   1460 		}
   1461 		break;
   1462 
   1463 	case KS_GROUP_Dead:
   1464 		if (id->t_composelen == 0) {
   1465 			update_modifier(id, 1, 0, MOD_COMPOSE);
   1466 			id->t_composelen = 1;
   1467 			id->t_composebuf[0] = ksym;
   1468 		} else
   1469 			res = ksym;
   1470 		break;
   1471 	}
   1472 
   1473 	if (res == KS_voidSymbol) {
   1474 		update_leds(id);
   1475 		return (0);
   1476 	}
   1477 
   1478 	if (id->t_composelen > 0) {
   1479 		id->t_composebuf[2 - id->t_composelen] = res;
   1480 		if (--id->t_composelen == 0) {
   1481 			res = wskbd_compose_value(id->t_composebuf);
   1482 			update_modifier(id, 0, 0, MOD_COMPOSE);
   1483 		} else {
   1484 			return (0);
   1485 		}
   1486 	}
   1487 
   1488 	update_leds(id);
   1489 
   1490 	/* We are done, return the symbol */
   1491 	if (KS_GROUP(res) == KS_GROUP_Ascii) {
   1492 		if (MOD_ONESET(id, MOD_ANYCONTROL)) {
   1493 			if ((res >= KS_at && res <= KS_z) || res == KS_space)
   1494 				res = res & 0x1f;
   1495 			else if (res == KS_2)
   1496 				res = 0x00;
   1497 			else if (res >= KS_3 && res <= KS_7)
   1498 				res = KS_Escape + (res - KS_3);
   1499 			else if (res == KS_8)
   1500 				res = KS_Delete;
   1501 		}
   1502 		if (MOD_ONESET(id, MOD_ANYMETA)) {
   1503 			if (id->t_flags & WSKFL_METAESC) {
   1504 				id->t_symbols[0] = KS_Escape;
   1505 				id->t_symbols[1] = res;
   1506 				return (2);
   1507 			} else
   1508 				res |= 0x80;
   1509 		}
   1510 	}
   1511 
   1512 	id->t_symbols[0] = res;
   1513 	return (1);
   1514 }
   1515