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