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