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