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