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