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