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