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