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