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