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