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