Home | History | Annotate | Line # | Download | only in wscons
wskbd.c revision 1.42.2.3
      1 /* $NetBSD: wskbd.c,v 1.42.2.3 2001/10/01 12:46:42 fvdl 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.3 2001/10/01 12:46:42 fvdl 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 	if (sc->sc_displaydv != NULL)
    655 		for (i = 0; i < len; i++)
    656 			wsdisplay_kbdinput(sc->sc_displaydv, buf[i]);
    657 	/* this is KS_GROUP_Ascii */
    658 #endif
    659 }
    660 #endif /* WSDISPLAY_COMPAT_RAWKBD */
    661 
    662 #if NWSDISPLAY > 0
    663 static void
    664 wskbd_holdscreen(sc, hold)
    665 	struct wskbd_softc *sc;
    666 	int hold;
    667 {
    668 	int new_state;
    669 
    670 	if (sc->sc_displaydv != NULL) {
    671 		wsdisplay_kbdholdscreen(sc->sc_displaydv, hold);
    672 		new_state = sc->sc_ledstate;
    673 		if (hold)
    674 			new_state |= WSKBD_LED_SCROLL;
    675 		else
    676 			new_state &= ~WSKBD_LED_SCROLL;
    677 		if (new_state != sc->sc_ledstate) {
    678 			(*sc->sc_accessops->set_leds)(sc->sc_accesscookie,
    679 						      new_state);
    680 			sc->sc_ledstate = new_state;
    681 		}
    682 	}
    683 }
    684 #endif
    685 
    686 static int
    687 wskbd_enable(sc, on)
    688 	struct wskbd_softc *sc;
    689 	int on;
    690 {
    691 	int res;
    692 
    693 	/* XXX reference count? */
    694 	if (!on && (!sc->sc_translating
    695 #if NWSDISPLAY > 0
    696 		    || sc->sc_displaydv
    697 #endif
    698 		))
    699 		return (EBUSY);
    700 
    701 	res = (*sc->sc_accessops->enable)(sc->sc_accesscookie, on);
    702 	return (res);
    703 }
    704 
    705 int
    706 wskbdopen(devvp, flags, mode, p)
    707 	struct vnode *devvp;
    708 	int flags, mode;
    709 	struct proc *p;
    710 {
    711 	struct wskbd_softc *sc;
    712 	int unit;
    713 
    714 	unit = minor(vdev_rdev(devvp));
    715 	if (unit >= wskbd_cd.cd_ndevs ||	/* make sure it was attached */
    716 	    (sc = wskbd_cd.cd_devs[unit]) == NULL)
    717 		return (ENXIO);
    718 
    719 	if (sc->sc_dying)
    720 		return (EIO);
    721 
    722 	if (!(flags & FREAD)) {
    723 		/* Not opening for read, only ioctl is available. */
    724 		return (0);
    725 	}
    726 
    727 #if NWSMUX > 0
    728 	if (sc->sc_mux)
    729 		return (EBUSY);
    730 #endif
    731 
    732 	if (sc->sc_events.io)			/* and that it's not in use */
    733 		return (EBUSY);
    734 
    735 	vdev_setprivdata(devvp, sc);
    736 
    737 	sc->sc_events.io = p;
    738 	wsevent_init(&sc->sc_events);		/* may cause sleep */
    739 
    740 	sc->sc_translating = 0;
    741 	sc->sc_ready = 1;			/* start accepting events */
    742 
    743 	wskbd_enable(sc, 1);
    744 	return (0);
    745 }
    746 
    747 int
    748 wskbdclose(devvp, flags, mode, p)
    749 	struct vnode *devvp;
    750 	int flags, mode;
    751 	struct proc *p;
    752 {
    753 	return (wskbddoclose(vdev_privdata(devvp), flags, mode, p));
    754 }
    755 
    756 int
    757 wskbddoclose(dv, flags, mode, p)
    758 	struct device *dv;
    759 	int flags, mode;
    760 	struct proc *p;
    761 {
    762 	struct wskbd_softc *sc = (struct wskbd_softc *)dv;
    763 
    764 	if (!(flags & FREAD)) {
    765 		/* Nothing to do, because open didn't do anything. */
    766 		return (0);
    767 	}
    768 
    769 	sc->sc_ready = 0;			/* stop accepting events */
    770 	sc->sc_translating = 1;
    771 
    772 	wsevent_fini(&sc->sc_events);
    773 	sc->sc_events.io = NULL;
    774 
    775 	wskbd_enable(sc, 0);
    776 	return (0);
    777 }
    778 
    779 int
    780 wskbdread(devvp, uio, flags)
    781 	struct vnode *devvp;
    782 	struct uio *uio;
    783 	int flags;
    784 {
    785 	struct wskbd_softc *sc;
    786 	int error;
    787 
    788 	sc = vdev_privdata(devvp);
    789 
    790 	if (sc->sc_dying)
    791 		return (EIO);
    792 
    793 	sc->sc_refcnt++;
    794 	error = wsevent_read(&sc->sc_events, uio, flags);
    795 	if (--sc->sc_refcnt < 0) {
    796 		wakeup(sc);
    797 		error = EIO;
    798 	}
    799 	return (error);
    800 }
    801 
    802 int
    803 wskbdioctl(devvp, cmd, data, flag, p)
    804 	struct vnode *devvp;
    805 	u_long cmd;
    806 	caddr_t data;
    807 	int flag;
    808 	struct proc *p;
    809 {
    810 	return (wskbddoioctl(vdev_privdata(devvp), cmd, data, flag,p));
    811 }
    812 
    813 /* A wrapper around the ioctl() workhorse to make reference counting easy. */
    814 int
    815 wskbddoioctl(dv, cmd, data, flag, p)
    816 	struct device *dv;
    817 	u_long cmd;
    818 	caddr_t data;
    819 	int flag;
    820 	struct proc *p;
    821 {
    822 	struct wskbd_softc *sc = (struct wskbd_softc *)dv;
    823 	int error;
    824 
    825 	sc->sc_refcnt++;
    826 	error = wskbd_do_ioctl(sc, cmd, data, flag, p);
    827 	if (--sc->sc_refcnt < 0)
    828 		wakeup(sc);
    829 	return (error);
    830 }
    831 
    832 int
    833 wskbd_do_ioctl(sc, cmd, data, flag, p)
    834 	struct wskbd_softc *sc;
    835 	u_long cmd;
    836 	caddr_t data;
    837 	int flag;
    838 	struct proc *p;
    839 {
    840 	int error;
    841 
    842 	/*
    843 	 * Try the generic ioctls that the wskbd interface supports.
    844 	 */
    845 	switch (cmd) {
    846 	case FIONBIO:		/* we will remove this someday (soon???) */
    847 		return (0);
    848 
    849 	case FIOASYNC:
    850 		sc->sc_events.async = *(int *)data != 0;
    851 		return (0);
    852 
    853 	case TIOCSPGRP:
    854 		if (*(int *)data != sc->sc_events.io->p_pgid)
    855 			return (EPERM);
    856 		return (0);
    857 	}
    858 
    859 	/*
    860 	 * Try the keyboard driver for WSKBDIO ioctls.  It returns -1
    861 	 * if it didn't recognize the request.
    862 	 */
    863 	error = wskbd_displayioctl((struct device *)sc, cmd, data, flag, p);
    864 	return (error != -1 ? error : ENOTTY);
    865 }
    866 
    867 /*
    868  * WSKBDIO ioctls, handled in both emulation mode and in ``raw'' mode.
    869  * Some of these have no real effect in raw mode, however.
    870  */
    871 static int
    872 wskbd_displayioctl(dev, cmd, data, flag, p)
    873 	struct device *dev;
    874 	u_long cmd;
    875 	caddr_t data;
    876 	int flag;
    877 	struct proc *p;
    878 {
    879 	struct wskbd_softc *sc = (struct wskbd_softc *)dev;
    880 	struct wskbd_bell_data *ubdp, *kbdp;
    881 	struct wskbd_keyrepeat_data *ukdp, *kkdp;
    882 	struct wskbd_map_data *umdp;
    883 	struct wskbd_mapdata md;
    884 	kbd_t enc;
    885 	void *buf;
    886 	int len, error;
    887 
    888 	switch (cmd) {
    889 #define	SETBELL(dstp, srcp, dfltp)					\
    890     do {								\
    891 	(dstp)->pitch = ((srcp)->which & WSKBD_BELL_DOPITCH) ?		\
    892 	    (srcp)->pitch : (dfltp)->pitch;				\
    893 	(dstp)->period = ((srcp)->which & WSKBD_BELL_DOPERIOD) ?	\
    894 	    (srcp)->period : (dfltp)->period;				\
    895 	(dstp)->volume = ((srcp)->which & WSKBD_BELL_DOVOLUME) ?	\
    896 	    (srcp)->volume : (dfltp)->volume;				\
    897 	(dstp)->which = WSKBD_BELL_DOALL;				\
    898     } while (0)
    899 
    900 	case WSKBDIO_BELL:
    901 		if ((flag & FWRITE) == 0)
    902 			return (EACCES);
    903 		return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
    904 		    WSKBDIO_COMPLEXBELL, (caddr_t)&sc->sc_bell_data, flag, p));
    905 
    906 	case WSKBDIO_COMPLEXBELL:
    907 		if ((flag & FWRITE) == 0)
    908 			return (EACCES);
    909 		ubdp = (struct wskbd_bell_data *)data;
    910 		SETBELL(ubdp, ubdp, &sc->sc_bell_data);
    911 		return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
    912 		    WSKBDIO_COMPLEXBELL, (caddr_t)ubdp, flag, p));
    913 
    914 	case WSKBDIO_SETBELL:
    915 		if ((flag & FWRITE) == 0)
    916 			return (EACCES);
    917 		kbdp = &sc->sc_bell_data;
    918 setbell:
    919 		ubdp = (struct wskbd_bell_data *)data;
    920 		SETBELL(kbdp, ubdp, kbdp);
    921 		return (0);
    922 
    923 	case WSKBDIO_GETBELL:
    924 		kbdp = &sc->sc_bell_data;
    925 getbell:
    926 		ubdp = (struct wskbd_bell_data *)data;
    927 		SETBELL(ubdp, kbdp, kbdp);
    928 		return (0);
    929 
    930 	case WSKBDIO_SETDEFAULTBELL:
    931 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    932 			return (error);
    933 		kbdp = &wskbd_default_bell_data;
    934 		goto setbell;
    935 
    936 
    937 	case WSKBDIO_GETDEFAULTBELL:
    938 		kbdp = &wskbd_default_bell_data;
    939 		goto getbell;
    940 
    941 #undef SETBELL
    942 
    943 #define	SETKEYREPEAT(dstp, srcp, dfltp)					\
    944     do {								\
    945 	(dstp)->del1 = ((srcp)->which & WSKBD_KEYREPEAT_DODEL1) ?	\
    946 	    (srcp)->del1 : (dfltp)->del1;				\
    947 	(dstp)->delN = ((srcp)->which & WSKBD_KEYREPEAT_DODELN) ?	\
    948 	    (srcp)->delN : (dfltp)->delN;				\
    949 	(dstp)->which = WSKBD_KEYREPEAT_DOALL;				\
    950     } while (0)
    951 
    952 	case WSKBDIO_SETKEYREPEAT:
    953 		if ((flag & FWRITE) == 0)
    954 			return (EACCES);
    955 		kkdp = &sc->sc_keyrepeat_data;
    956 setkeyrepeat:
    957 		ukdp = (struct wskbd_keyrepeat_data *)data;
    958 		SETKEYREPEAT(kkdp, ukdp, kkdp);
    959 		return (0);
    960 
    961 	case WSKBDIO_GETKEYREPEAT:
    962 		kkdp = &sc->sc_keyrepeat_data;
    963 getkeyrepeat:
    964 		ukdp = (struct wskbd_keyrepeat_data *)data;
    965 		SETKEYREPEAT(ukdp, kkdp, kkdp);
    966 		return (0);
    967 
    968 	case WSKBDIO_SETDEFAULTKEYREPEAT:
    969 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    970 			return (error);
    971 		kkdp = &wskbd_default_keyrepeat_data;
    972 		goto setkeyrepeat;
    973 
    974 
    975 	case WSKBDIO_GETDEFAULTKEYREPEAT:
    976 		kkdp = &wskbd_default_keyrepeat_data;
    977 		goto getkeyrepeat;
    978 
    979 #undef SETKEYREPEAT
    980 
    981 	case WSKBDIO_SETMAP:
    982 		if ((flag & FWRITE) == 0)
    983 			return (EACCES);
    984 		umdp = (struct wskbd_map_data *)data;
    985 		if (umdp->maplen > WSKBDIO_MAXMAPLEN)
    986 			return (EINVAL);
    987 
    988 		len = umdp->maplen*sizeof(struct wscons_keymap);
    989 		buf = malloc(len, M_TEMP, M_WAITOK);
    990 		error = copyin(umdp->map, buf, len);
    991 		if (error == 0) {
    992 			wskbd_init_keymap(umdp->maplen,
    993 					  &sc->sc_map, &sc->sc_maplen);
    994 			memcpy(sc->sc_map, buf, len);
    995 			/* drop the variant bits handled by the map */
    996 			sc->sc_layout = KB_USER |
    997 			      (KB_VARIANT(sc->sc_layout) & KB_HANDLEDBYWSKBD);
    998 			wskbd_update_layout(sc->id, sc->sc_layout);
    999 		}
   1000 		free(buf, M_TEMP);
   1001 		return(error);
   1002 
   1003 	case WSKBDIO_GETMAP:
   1004 		umdp = (struct wskbd_map_data *)data;
   1005 		if (umdp->maplen > sc->sc_maplen)
   1006 			umdp->maplen = sc->sc_maplen;
   1007 		error = copyout(sc->sc_map, umdp->map,
   1008 				umdp->maplen*sizeof(struct wscons_keymap));
   1009 		return(error);
   1010 
   1011 	case WSKBDIO_GETENCODING:
   1012 		*((kbd_t *) data) = sc->sc_layout;
   1013 		return(0);
   1014 
   1015 	case WSKBDIO_SETENCODING:
   1016 		if ((flag & FWRITE) == 0)
   1017 			return (EACCES);
   1018 		enc = *((kbd_t *)data);
   1019 		if (KB_ENCODING(enc) == KB_USER) {
   1020 			/* user map must already be loaded */
   1021 			if (KB_ENCODING(sc->sc_layout) != KB_USER)
   1022 				return (EINVAL);
   1023 			/* map variants make no sense */
   1024 			if (KB_VARIANT(enc) & ~KB_HANDLEDBYWSKBD)
   1025 				return (EINVAL);
   1026 		} else {
   1027 			md = *(sc->id->t_keymap); /* structure assignment */
   1028 			md.layout = enc;
   1029 			error = wskbd_load_keymap(&md, &sc->sc_map,
   1030 						  &sc->sc_maplen);
   1031 			if (error)
   1032 				return (error);
   1033 		}
   1034 		sc->sc_layout = enc;
   1035 		wskbd_update_layout(sc->id, enc);
   1036 		return (0);
   1037 	}
   1038 
   1039 	/*
   1040 	 * Try the keyboard driver for WSKBDIO ioctls.  It returns -1
   1041 	 * if it didn't recognize the request, and in turn we return
   1042 	 * -1 if we didn't recognize the request.
   1043 	 */
   1044 /* printf("kbdaccess\n"); */
   1045 	error = (*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd, data,
   1046 					   flag, p);
   1047 #ifdef WSDISPLAY_COMPAT_RAWKBD
   1048 	if (!error && cmd == WSKBDIO_SETMODE && *(int *)data == WSKBD_RAW) {
   1049 		int s = spltty();
   1050 		sc->id->t_modifiers &= ~(MOD_SHIFT_L | MOD_SHIFT_R
   1051 					 | MOD_CONTROL_L | MOD_CONTROL_R
   1052 					 | MOD_META_L | MOD_META_R
   1053 					 | MOD_COMMAND
   1054 					 | MOD_COMMAND1 | MOD_COMMAND2);
   1055 #if NWSDISPLAY > 0
   1056 		if (sc->sc_repeating) {
   1057 			sc->sc_repeating = 0;
   1058 			callout_stop(&sc->sc_repeat_ch);
   1059 		}
   1060 #endif
   1061 		splx(s);
   1062 	}
   1063 #endif
   1064 	return (error);
   1065 }
   1066 
   1067 int
   1068 wskbdpoll(devvp, events, p)
   1069 	struct vnode *devvp;
   1070 	int events;
   1071 	struct proc *p;
   1072 {
   1073 	struct wskbd_softc *sc;
   1074 
   1075 	sc = vdev_privdata(devvp);
   1076 
   1077 	return (wsevent_poll(&sc->sc_events, events, p));
   1078 }
   1079 
   1080 #if NWSDISPLAY > 0
   1081 
   1082 int
   1083 wskbd_pickfree()
   1084 {
   1085 	int i;
   1086 	struct wskbd_softc *sc;
   1087 
   1088 	for (i = 0; i < wskbd_cd.cd_ndevs; i++) {
   1089 		if ((sc = wskbd_cd.cd_devs[i]) == NULL)
   1090 			continue;
   1091 		if (sc->sc_displaydv == NULL)
   1092 			return (i);
   1093 	}
   1094 	return (-1);
   1095 }
   1096 
   1097 struct device *
   1098 wskbd_set_console_display(displaydv, muxsc)
   1099 	struct device *displaydv;
   1100 	struct wsmux_softc *muxsc;
   1101 {
   1102 	struct wskbd_softc *sc = wskbd_console_device;
   1103 
   1104 	if (!sc)
   1105 		return (0);
   1106 	sc->sc_displaydv = displaydv;
   1107 	(void)wsmux_attach_sc(muxsc, WSMUX_KBD, &sc->sc_dv, &sc->sc_events,
   1108 			      &sc->sc_mux, &wskbd_muxops, wskbd_major);
   1109 	return (&sc->sc_dv);
   1110 }
   1111 
   1112 int
   1113 wskbd_set_display(dv, muxsc)
   1114 	struct device *dv;
   1115 	struct wsmux_softc *muxsc;
   1116 {
   1117 	struct wskbd_softc *sc = (struct wskbd_softc *)dv;
   1118 	struct device *displaydv = muxsc ? muxsc->sc_displaydv : 0;
   1119 	struct device *odisplaydv;
   1120 	int error;
   1121 
   1122 	DPRINTF(("wskbd_set_display: %s mux=%p disp=%p odisp=%p cons=%d\n",
   1123 		 dv->dv_xname, muxsc, sc->sc_displaydv, displaydv,
   1124 		 sc->sc_isconsole));
   1125 
   1126 	if (sc->sc_isconsole)
   1127 		return (EBUSY);
   1128 
   1129 	if (displaydv) {
   1130 		if (sc->sc_displaydv)
   1131 			return (EBUSY);
   1132 	} else {
   1133 		if (sc->sc_displaydv == NULL)
   1134 			return (ENXIO);
   1135 	}
   1136 
   1137 	odisplaydv = sc->sc_displaydv;
   1138 	sc->sc_displaydv = displaydv;
   1139 
   1140 	error = wskbd_enable(sc, displaydv != NULL);
   1141 	if (error) {
   1142 		sc->sc_displaydv = odisplaydv;
   1143 		return (error);
   1144 	}
   1145 
   1146 	if (displaydv)
   1147 		printf("%s: connecting to %s\n",
   1148 		       sc->sc_dv.dv_xname, displaydv->dv_xname);
   1149 	else
   1150 		printf("%s: disconnecting from %s\n",
   1151 		       sc->sc_dv.dv_xname, odisplaydv->dv_xname);
   1152 
   1153 	return (0);
   1154 }
   1155 
   1156 int
   1157 wskbd_add_mux(unit, muxsc)
   1158 	int unit;
   1159 	struct wsmux_softc *muxsc;
   1160 {
   1161 	struct wskbd_softc *sc;
   1162 
   1163 	DPRINTF(("wskbd_add_mux: %d %s %p\n", unit, muxsc->sc_dv.dv_xname,
   1164 		 muxsc->sc_displaydv));
   1165 	if (unit < 0 || unit >= wskbd_cd.cd_ndevs ||
   1166 	    (sc = wskbd_cd.cd_devs[unit]) == NULL)
   1167 		return (ENXIO);
   1168 
   1169 	if (sc->sc_mux || sc->sc_events.io)
   1170 		return (EBUSY);
   1171 
   1172 	return (wsmux_attach_sc(muxsc, WSMUX_KBD, &sc->sc_dv, &sc->sc_events,
   1173 				&sc->sc_mux, &wskbd_muxops, wskbd_major));
   1174 }
   1175 
   1176 int
   1177 wskbd_rem_mux(unit, muxsc)
   1178 	int unit;
   1179 	struct wsmux_softc *muxsc;
   1180 {
   1181 	struct wskbd_softc *sc;
   1182 
   1183 	DPRINTF(("wskbd_rem_mux: %d %s\n", unit, muxsc->sc_dv.dv_xname));
   1184 	if (unit < 0 || unit >= wskbd_cd.cd_ndevs ||
   1185 	    (sc = wskbd_cd.cd_devs[unit]) == NULL)
   1186 		return (ENXIO);
   1187 
   1188 	return (wsmux_detach_sc(muxsc, &sc->sc_dv));
   1189 }
   1190 
   1191 #endif /* NWSDISPLAY > 0 */
   1192 
   1193 /*
   1194  * Console interface.
   1195  */
   1196 int
   1197 wskbd_cngetc(dev)
   1198 	dev_t dev;
   1199 {
   1200 	static int num = 0;
   1201 	static int pos;
   1202 	u_int type;
   1203 	int data;
   1204 	keysym_t ks;
   1205 
   1206 	if (!wskbd_console_initted)
   1207 		return 0;
   1208 
   1209 	if (wskbd_console_device != NULL &&
   1210 	    !wskbd_console_device->sc_translating)
   1211 		return 0;
   1212 
   1213 	for(;;) {
   1214 		if (num-- > 0) {
   1215 			ks = wskbd_console_data.t_symbols[pos++];
   1216 			if (KS_GROUP(ks) == KS_GROUP_Ascii)
   1217 				return (KS_VALUE(ks));
   1218 		} else {
   1219 			(*wskbd_console_data.t_consops->getc)
   1220 				(wskbd_console_data.t_consaccesscookie,
   1221 				 &type, &data);
   1222 			num = wskbd_translate(&wskbd_console_data, type, data);
   1223 			pos = 0;
   1224 		}
   1225 	}
   1226 }
   1227 
   1228 void
   1229 wskbd_cnpollc(dev, poll)
   1230 	dev_t dev;
   1231 	int poll;
   1232 {
   1233 
   1234 	if (!wskbd_console_initted)
   1235 		return;
   1236 
   1237 	if (wskbd_console_device != NULL &&
   1238 	    !wskbd_console_device->sc_translating)
   1239 		return;
   1240 
   1241 	(*wskbd_console_data.t_consops->pollc)
   1242 	    (wskbd_console_data.t_consaccesscookie, poll);
   1243 }
   1244 
   1245 void
   1246 wskbd_cnbell(dev, pitch, period, volume)
   1247 	dev_t dev;
   1248 	u_int pitch, period, volume;
   1249 {
   1250 
   1251 	if (!wskbd_console_initted)
   1252 		return;
   1253 
   1254 	if (wskbd_console_data.t_consops->bell != NULL)
   1255 		(*wskbd_console_data.t_consops->bell)
   1256 		    (wskbd_console_data.t_consaccesscookie, pitch, period,
   1257 			volume);
   1258 }
   1259 
   1260 static inline void
   1261 update_leds(id)
   1262 	struct wskbd_internal *id;
   1263 {
   1264 	int new_state;
   1265 
   1266 	new_state = 0;
   1267 	if (id->t_modifiers & (MOD_SHIFTLOCK | MOD_CAPSLOCK))
   1268 		new_state |= WSKBD_LED_CAPS;
   1269 	if (id->t_modifiers & MOD_NUMLOCK)
   1270 		new_state |= WSKBD_LED_NUM;
   1271 	if (id->t_modifiers & MOD_COMPOSE)
   1272 		new_state |= WSKBD_LED_COMPOSE;
   1273 	if (id->t_modifiers & MOD_HOLDSCREEN)
   1274 		new_state |= WSKBD_LED_SCROLL;
   1275 
   1276 	if (id->t_sc && new_state != id->t_sc->sc_ledstate) {
   1277 		(*id->t_sc->sc_accessops->set_leds)
   1278 		    (id->t_sc->sc_accesscookie, new_state);
   1279 		id->t_sc->sc_ledstate = new_state;
   1280 	}
   1281 }
   1282 
   1283 static inline void
   1284 update_modifier(id, type, toggle, mask)
   1285 	struct wskbd_internal *id;
   1286 	u_int type;
   1287 	int toggle;
   1288 	int mask;
   1289 {
   1290 	if (toggle) {
   1291 		if (type == WSCONS_EVENT_KEY_DOWN)
   1292 			id->t_modifiers ^= mask;
   1293 	} else {
   1294 		if (type == WSCONS_EVENT_KEY_DOWN)
   1295 			id->t_modifiers |= mask;
   1296 		else
   1297 			id->t_modifiers &= ~mask;
   1298 	}
   1299 }
   1300 
   1301 #if NWSDISPLAY > 0
   1302 static void
   1303 change_displayparam(sc, param, updown, wraparound)
   1304 	struct wskbd_softc *sc;
   1305 	int param, updown, wraparound;
   1306 {
   1307 	int res;
   1308 	struct wsdisplay_param dp;
   1309 
   1310 	if (sc->sc_displaydv == NULL)
   1311 		return;
   1312 
   1313 	dp.param = param;
   1314 	res = wsdisplay_param(sc->sc_displaydv, WSDISPLAYIO_GETPARAM, &dp);
   1315 
   1316 	if (res == EINVAL)
   1317 		return; /* no such parameter */
   1318 
   1319 	dp.curval += updown;
   1320 	if (dp.max < dp.curval)
   1321 		dp.curval = wraparound ? dp.min : dp.max;
   1322 	else
   1323 	if (dp.curval < dp.min)
   1324 		dp.curval = wraparound ? dp.max : dp.min;
   1325 	wsdisplay_param(sc->sc_displaydv, WSDISPLAYIO_SETPARAM, &dp);
   1326 }
   1327 #endif
   1328 
   1329 static int
   1330 internal_command(sc, type, ksym, ksym2)
   1331 	struct wskbd_softc *sc;
   1332 	u_int *type;
   1333 	keysym_t ksym, ksym2;
   1334 {
   1335 	switch (ksym) {
   1336 	case KS_Cmd:
   1337 		update_modifier(sc->id, *type, 0, MOD_COMMAND);
   1338 		ksym = ksym2;
   1339 		break;
   1340 
   1341 	case KS_Cmd1:
   1342 		update_modifier(sc->id, *type, 0, MOD_COMMAND1);
   1343 		break;
   1344 
   1345 	case KS_Cmd2:
   1346 		update_modifier(sc->id, *type, 0, MOD_COMMAND2);
   1347 		break;
   1348 	}
   1349 
   1350 	if (*type != WSCONS_EVENT_KEY_DOWN ||
   1351 	    (! MOD_ONESET(sc->id, MOD_COMMAND) &&
   1352 	     ! MOD_ALLSET(sc->id, MOD_COMMAND1 | MOD_COMMAND2)))
   1353 		return (0);
   1354 
   1355 	switch (ksym) {
   1356 #if defined(DDB) || defined(KGDB)
   1357 	case KS_Cmd_Debugger:
   1358 		if (sc->sc_isconsole) {
   1359 #ifdef DDB
   1360 			console_debugger();
   1361 #endif
   1362 #ifdef KGDB
   1363 			kgdb_connect(1);
   1364 #endif
   1365 		}
   1366 		/* discard this key (ddb discarded command modifiers) */
   1367 		*type = WSCONS_EVENT_KEY_UP;
   1368 		return (1);
   1369 #endif
   1370 
   1371 #if NWSDISPLAY > 0
   1372 	case KS_Cmd_Screen0:
   1373 	case KS_Cmd_Screen1:
   1374 	case KS_Cmd_Screen2:
   1375 	case KS_Cmd_Screen3:
   1376 	case KS_Cmd_Screen4:
   1377 	case KS_Cmd_Screen5:
   1378 	case KS_Cmd_Screen6:
   1379 	case KS_Cmd_Screen7:
   1380 	case KS_Cmd_Screen8:
   1381 	case KS_Cmd_Screen9:
   1382 		wsdisplay_switch(sc->sc_displaydv, ksym - KS_Cmd_Screen0, 0);
   1383 		return (1);
   1384 	case KS_Cmd_ResetEmul:
   1385 		wsdisplay_reset(sc->sc_displaydv, WSDISPLAY_RESETEMUL);
   1386 		return (1);
   1387 	case KS_Cmd_ResetClose:
   1388 		wsdisplay_reset(sc->sc_displaydv, WSDISPLAY_RESETCLOSE);
   1389 		return (1);
   1390 	case KS_Cmd_BacklightOn:
   1391 	case KS_Cmd_BacklightOff:
   1392 	case KS_Cmd_BacklightToggle:
   1393 		change_displayparam(sc, WSDISPLAYIO_PARAM_BACKLIGHT,
   1394 				    ksym == KS_Cmd_BacklightOff ? -1 : 1,
   1395 				    ksym == KS_Cmd_BacklightToggle ? 1 : 0);
   1396 		return (1);
   1397 	case KS_Cmd_BrightnessUp:
   1398 	case KS_Cmd_BrightnessDown:
   1399 	case KS_Cmd_BrightnessRotate:
   1400 		change_displayparam(sc, WSDISPLAYIO_PARAM_BRIGHTNESS,
   1401 				    ksym == KS_Cmd_BrightnessDown ? -1 : 1,
   1402 				    ksym == KS_Cmd_BrightnessRotate ? 1 : 0);
   1403 		return (1);
   1404 	case KS_Cmd_ContrastUp:
   1405 	case KS_Cmd_ContrastDown:
   1406 	case KS_Cmd_ContrastRotate:
   1407 		change_displayparam(sc, WSDISPLAYIO_PARAM_CONTRAST,
   1408 				    ksym == KS_Cmd_ContrastDown ? -1 : 1,
   1409 				    ksym == KS_Cmd_ContrastRotate ? 1 : 0);
   1410 		return (1);
   1411 #endif
   1412 	}
   1413 	return (0);
   1414 }
   1415 
   1416 static int
   1417 wskbd_translate(id, type, value)
   1418 	struct wskbd_internal *id;
   1419 	u_int type;
   1420 	int value;
   1421 {
   1422 	struct wskbd_softc *sc = id->t_sc;
   1423 	keysym_t ksym, res, *group;
   1424 	struct wscons_keymap kpbuf, *kp;
   1425 	int iscommand = 0;
   1426 
   1427 	if (type == WSCONS_EVENT_ALL_KEYS_UP) {
   1428 		id->t_modifiers &= ~(MOD_SHIFT_L | MOD_SHIFT_R
   1429 				| MOD_CONTROL_L | MOD_CONTROL_R
   1430 				| MOD_META_L | MOD_META_R
   1431 				| MOD_MODESHIFT
   1432 				| MOD_COMMAND | MOD_COMMAND1 | MOD_COMMAND2);
   1433 		update_leds(id);
   1434 		return (0);
   1435 	}
   1436 
   1437 	if (sc != NULL) {
   1438 		if (value < 0 || value >= sc->sc_maplen) {
   1439 #ifdef DEBUG
   1440 			printf("wskbd_translate: keycode %d out of range\n",
   1441 			       value);
   1442 #endif
   1443 			return (0);
   1444 		}
   1445 		kp = sc->sc_map + value;
   1446 	} else {
   1447 		kp = &kpbuf;
   1448 		wskbd_get_mapentry(id->t_keymap, value, kp);
   1449 	}
   1450 
   1451 	/* if this key has a command, process it first */
   1452 	if (sc != NULL && kp->command != KS_voidSymbol)
   1453 		iscommand = internal_command(sc, &type, kp->command,
   1454 					     kp->group1[0]);
   1455 
   1456 	/* Now update modifiers */
   1457 	switch (kp->group1[0]) {
   1458 	case KS_Shift_L:
   1459 		update_modifier(id, type, 0, MOD_SHIFT_L);
   1460 		break;
   1461 
   1462 	case KS_Shift_R:
   1463 		update_modifier(id, type, 0, MOD_SHIFT_R);
   1464 		break;
   1465 
   1466 	case KS_Shift_Lock:
   1467 		update_modifier(id, type, 1, MOD_SHIFTLOCK);
   1468 		break;
   1469 
   1470 	case KS_Caps_Lock:
   1471 		update_modifier(id, type, 1, MOD_CAPSLOCK);
   1472 		break;
   1473 
   1474 	case KS_Control_L:
   1475 		update_modifier(id, type, 0, MOD_CONTROL_L);
   1476 		break;
   1477 
   1478 	case KS_Control_R:
   1479 		update_modifier(id, type, 0, MOD_CONTROL_R);
   1480 		break;
   1481 
   1482 	case KS_Alt_L:
   1483 		update_modifier(id, type, 0, MOD_META_L);
   1484 		break;
   1485 
   1486 	case KS_Alt_R:
   1487 		update_modifier(id, type, 0, MOD_META_R);
   1488 		break;
   1489 
   1490 	case KS_Mode_switch:
   1491 		update_modifier(id, type, 0, MOD_MODESHIFT);
   1492 		break;
   1493 
   1494 	case KS_Num_Lock:
   1495 		update_modifier(id, type, 1, MOD_NUMLOCK);
   1496 		break;
   1497 
   1498 #if NWSDISPLAY > 0
   1499 	case KS_Hold_Screen:
   1500 		if (sc != NULL) {
   1501 			update_modifier(id, type, 1, MOD_HOLDSCREEN);
   1502 			wskbd_holdscreen(sc, id->t_modifiers & MOD_HOLDSCREEN);
   1503 		}
   1504 		break;
   1505 #endif
   1506 	}
   1507 
   1508 	/* If this is a key release or we are in command mode, we are done */
   1509 	if (type != WSCONS_EVENT_KEY_DOWN || iscommand) {
   1510 		update_leds(id);
   1511 		return (0);
   1512 	}
   1513 
   1514 	/* Get the keysym */
   1515 	if (id->t_modifiers & MOD_MODESHIFT)
   1516 		group = & kp->group2[0];
   1517 	else
   1518 		group = & kp->group1[0];
   1519 
   1520 	if ((id->t_modifiers & MOD_NUMLOCK) != 0 &&
   1521 	    KS_GROUP(group[1]) == KS_GROUP_Keypad) {
   1522 		if (MOD_ONESET(id, MOD_ANYSHIFT))
   1523 			ksym = group[0];
   1524 		else
   1525 			ksym = group[1];
   1526 	} else if (! MOD_ONESET(id, MOD_ANYSHIFT | MOD_CAPSLOCK)) {
   1527 		ksym = group[0];
   1528 	} else if (MOD_ONESET(id, MOD_CAPSLOCK)) {
   1529 		if (! MOD_ONESET(id, MOD_SHIFT_L | MOD_SHIFT_R))
   1530 			ksym = group[0];
   1531 		else
   1532 			ksym = group[1];
   1533 		if (ksym >= KS_a && ksym <= KS_z)
   1534 			ksym += KS_A - KS_a;
   1535 		else if (ksym >= KS_agrave && ksym <= KS_thorn &&
   1536 			 ksym != KS_division)
   1537 			ksym += KS_Agrave - KS_agrave;
   1538 	} else if (MOD_ONESET(id, MOD_ANYSHIFT)) {
   1539 		ksym = group[1];
   1540 	} else {
   1541 		ksym = group[0];
   1542 	}
   1543 
   1544 	/* Process compose sequence and dead accents */
   1545 	res = KS_voidSymbol;
   1546 
   1547 	switch (KS_GROUP(ksym)) {
   1548 	case KS_GROUP_Ascii:
   1549 	case KS_GROUP_Keypad:
   1550 	case KS_GROUP_Function:
   1551 		res = ksym;
   1552 		break;
   1553 
   1554 	case KS_GROUP_Mod:
   1555 		if (ksym == KS_Multi_key) {
   1556 			update_modifier(id, 1, 0, MOD_COMPOSE);
   1557 			id->t_composelen = 2;
   1558 		}
   1559 		break;
   1560 
   1561 	case KS_GROUP_Dead:
   1562 		if (id->t_composelen == 0) {
   1563 			update_modifier(id, 1, 0, MOD_COMPOSE);
   1564 			id->t_composelen = 1;
   1565 			id->t_composebuf[0] = ksym;
   1566 		} else
   1567 			res = ksym;
   1568 		break;
   1569 	}
   1570 
   1571 	if (res == KS_voidSymbol) {
   1572 		update_leds(id);
   1573 		return (0);
   1574 	}
   1575 
   1576 	if (id->t_composelen > 0) {
   1577 		id->t_composebuf[2 - id->t_composelen] = res;
   1578 		if (--id->t_composelen == 0) {
   1579 			res = wskbd_compose_value(id->t_composebuf);
   1580 			update_modifier(id, 0, 0, MOD_COMPOSE);
   1581 		} else {
   1582 			return (0);
   1583 		}
   1584 	}
   1585 
   1586 	update_leds(id);
   1587 
   1588 	/* We are done, return the symbol */
   1589 	if (KS_GROUP(res) == KS_GROUP_Ascii) {
   1590 		if (MOD_ONESET(id, MOD_ANYCONTROL)) {
   1591 			if ((res >= KS_at && res <= KS_z) || res == KS_space)
   1592 				res = res & 0x1f;
   1593 			else if (res == KS_2)
   1594 				res = 0x00;
   1595 			else if (res >= KS_3 && res <= KS_7)
   1596 				res = KS_Escape + (res - KS_3);
   1597 			else if (res == KS_8)
   1598 				res = KS_Delete;
   1599 		}
   1600 		if (MOD_ONESET(id, MOD_ANYMETA)) {
   1601 			if (id->t_flags & WSKFL_METAESC) {
   1602 				id->t_symbols[0] = KS_Escape;
   1603 				id->t_symbols[1] = res;
   1604 				return (2);
   1605 			} else
   1606 				res |= 0x80;
   1607 		}
   1608 	}
   1609 
   1610 	id->t_symbols[0] = res;
   1611 	return (1);
   1612 }
   1613