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