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