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