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