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