Home | History | Annotate | Line # | Download | only in wscons
wskbd.c revision 1.59
      1 /* $NetBSD: wskbd.c,v 1.59 2001/11/22 00:57:15 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 /*
     37  * Copyright (c) 1992, 1993
     38  *	The Regents of the University of California.  All rights reserved.
     39  *
     40  * This software was developed by the Computer Systems Engineering group
     41  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     42  * contributed to Berkeley.
     43  *
     44  * All advertising materials mentioning features or use of this software
     45  * must display the following acknowledgement:
     46  *	This product includes software developed by the University of
     47  *	California, Lawrence Berkeley Laboratory.
     48  *
     49  * Redistribution and use in source and binary forms, with or without
     50  * modification, are permitted provided that the following conditions
     51  * are met:
     52  * 1. Redistributions of source code must retain the above copyright
     53  *    notice, this list of conditions and the following disclaimer.
     54  * 2. Redistributions in binary form must reproduce the above copyright
     55  *    notice, this list of conditions and the following disclaimer in the
     56  *    documentation and/or other materials provided with the distribution.
     57  * 3. All advertising materials mentioning features or use of this software
     58  *    must display the following acknowledgement:
     59  *	This product includes software developed by the University of
     60  *	California, Berkeley and its contributors.
     61  * 4. Neither the name of the University nor the names of its contributors
     62  *    may be used to endorse or promote products derived from this software
     63  *    without specific prior written permission.
     64  *
     65  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     66  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     67  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     68  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     69  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     70  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     71  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     72  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     73  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     74  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     75  * SUCH DAMAGE.
     76  *
     77  *	@(#)kbd.c	8.2 (Berkeley) 10/30/93
     78  */
     79 
     80 /*
     81  * Keyboard driver (/dev/wskbd*).  Translates incoming bytes to ASCII or
     82  * to `wscons_events' and passes them up to the appropriate reader.
     83  */
     84 
     85 #include <sys/cdefs.h>
     86 __KERNEL_RCSID(0, "$NetBSD: wskbd.c,v 1.59 2001/11/22 00:57:15 augustss Exp $");
     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 		wsdisplay_set_console_kbd(&sc->sc_base); /* sets me_dispv */
    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 #ifdef DIAGNOSTIC
    611 	if (evar->q == NULL) {
    612 		printf("wskbd_input: evar->q=NULL\n");
    613 		return;
    614 	}
    615 #endif
    616 
    617 	put = evar->put;
    618 	ev = &evar->q[put];
    619 	put = (put + 1) % WSEVENT_QSIZE;
    620 	if (put == evar->get) {
    621 		log(LOG_WARNING, "%s: event queue overflow\n",
    622 		    sc->sc_base.me_dv.dv_xname);
    623 		return;
    624 	}
    625 	ev->type = type;
    626 	ev->value = value;
    627 	microtime(&thistime);
    628 	TIMEVAL_TO_TIMESPEC(&thistime, &ev->time);
    629 	evar->put = put;
    630 	WSEVENT_WAKEUP(evar);
    631 }
    632 
    633 #ifdef WSDISPLAY_COMPAT_RAWKBD
    634 void
    635 wskbd_rawinput(struct device *dev, u_char *buf, int len)
    636 {
    637 #if NWSDISPLAY > 0
    638 	struct wskbd_softc *sc = (struct wskbd_softc *)dev;
    639 	int i;
    640 
    641 	if (sc->sc_base.me_dispdv != NULL)
    642 		for (i = 0; i < len; i++)
    643 			wsdisplay_kbdinput(sc->sc_base.me_dispdv, buf[i]);
    644 	/* this is KS_GROUP_Ascii */
    645 #endif
    646 }
    647 #endif /* WSDISPLAY_COMPAT_RAWKBD */
    648 
    649 #if NWSDISPLAY > 0
    650 static void
    651 wskbd_holdscreen(struct wskbd_softc *sc, int hold)
    652 {
    653 	int new_state;
    654 
    655 	if (sc->sc_base.me_dispdv != NULL) {
    656 		wsdisplay_kbdholdscreen(sc->sc_base.me_dispdv, hold);
    657 		new_state = sc->sc_ledstate;
    658 		if (hold)
    659 			new_state |= WSKBD_LED_SCROLL;
    660 		else
    661 			new_state &= ~WSKBD_LED_SCROLL;
    662 		if (new_state != sc->sc_ledstate) {
    663 			(*sc->sc_accessops->set_leds)(sc->sc_accesscookie,
    664 						      new_state);
    665 			sc->sc_ledstate = new_state;
    666 		}
    667 	}
    668 }
    669 #endif
    670 
    671 static int
    672 wskbd_enable(struct wskbd_softc *sc, int on)
    673 {
    674 	int error;
    675 
    676 #if 0
    677 /* I don't understand the purpose of this code.  And it seems to
    678  * break things, so it's out.  -- Lennart
    679  */
    680 	if (!on && (!sc->sc_translating
    681 #if NWSDISPLAY > 0
    682 		    || sc->sc_base.me_dispdv
    683 #endif
    684 		))
    685 		return (EBUSY);
    686 #endif
    687 #if NWSDISPLAY > 0
    688 	if (sc->sc_base.me_dispdv != NULL)
    689 		return (0);
    690 #endif
    691 
    692 	error = (*sc->sc_accessops->enable)(sc->sc_accesscookie, on);
    693 	DPRINTF(("wskbd_enable: sc=%p on=%d res=%d\n", sc, on, error));
    694 	return (error);
    695 }
    696 
    697 #if NWSMUX > 0
    698 int
    699 wskbd_mux_open(struct wsevsrc *me, struct wseventvar *evp)
    700 {
    701 	struct wskbd_softc *sc = (struct wskbd_softc *)me;
    702 
    703 	if (sc->sc_dying)
    704 		return (EIO);
    705 
    706 	if (sc->sc_base.me_evp != NULL)
    707 		return (EBUSY);
    708 
    709 	return (wskbd_do_open(sc, evp));
    710 }
    711 #endif
    712 
    713 int
    714 wskbdopen(dev_t dev, int flags, int mode, struct proc *p)
    715 {
    716 	struct wskbd_softc *sc;
    717 	struct wseventvar *evar;
    718 	int unit, error;
    719 
    720 	unit = minor(dev);
    721 	if (unit >= wskbd_cd.cd_ndevs ||	/* make sure it was attached */
    722 	    (sc = wskbd_cd.cd_devs[unit]) == NULL)
    723 		return (ENXIO);
    724 
    725 #if NWSMUX > 0
    726 	DPRINTF(("wskbdopen: %s mux=%p p=%p\n", sc->sc_base.me_dv.dv_xname,
    727 		 sc->sc_base.me_parent, p));
    728 #endif
    729 
    730 	if (sc->sc_dying)
    731 		return (EIO);
    732 
    733 	if ((flags & (FREAD | FWRITE)) == FWRITE)
    734 		/* Not opening for read, only ioctl is available. */
    735 		return (0);
    736 
    737 #if NWSMUX > 0
    738 	if (sc->sc_base.me_parent != NULL) {
    739 		/* Grab the keyboard out of the greedy hands of the mux. */
    740 		DPRINTF(("wskbdopen: detach\n"));
    741 		wsmux_detach_sc(&sc->sc_base);
    742 	}
    743 #endif
    744 
    745 	if (sc->sc_base.me_evp != NULL)
    746 		return (EBUSY);
    747 
    748 	evar = &sc->sc_base.me_evar;
    749 	wsevent_init(evar);
    750 	evar->io = p;
    751 
    752 	error = wskbd_do_open(sc, evar);
    753 	if (error) {
    754 		DPRINTF(("wskbdopen: %s open failed\n",
    755 			 sc->sc_base.me_dv.dv_xname));
    756 		sc->sc_base.me_evp = NULL;
    757 		wsevent_fini(evar);
    758 	}
    759 	return (error);
    760 }
    761 
    762 int
    763 wskbd_do_open(struct wskbd_softc *sc, struct wseventvar *evp)
    764 {
    765 	sc->sc_base.me_evp = evp;
    766 	sc->sc_translating = 0;
    767 
    768 	return (wskbd_enable(sc, 1));
    769 }
    770 
    771 int
    772 wskbdclose(dev_t dev, int flags, int mode, struct proc *p)
    773 {
    774 	struct wskbd_softc *sc =
    775 	    (struct wskbd_softc *)wskbd_cd.cd_devs[minor(dev)];
    776 	struct wseventvar *evar = sc->sc_base.me_evp;
    777 
    778 	if (evar == NULL)
    779 		/* not open for read */
    780 		return (0);
    781 
    782 	sc->sc_base.me_evp = NULL;
    783 	sc->sc_translating = 1;
    784 	(void)wskbd_enable(sc, 0);
    785 	wsevent_fini(evar);
    786 
    787 	return (0);
    788 }
    789 
    790 #if NWSMUX > 0
    791 int
    792 wskbd_mux_close(struct wsevsrc *me)
    793 {
    794 	struct wskbd_softc *sc = (struct wskbd_softc *)me;
    795 
    796 	sc->sc_base.me_evp = NULL;
    797 	sc->sc_translating = 1;
    798 	(void)wskbd_enable(sc, 0);
    799 
    800 	return (0);
    801 }
    802 #endif
    803 
    804 int
    805 wskbdread(dev_t dev, struct uio *uio, int flags)
    806 {
    807 	struct wskbd_softc *sc = wskbd_cd.cd_devs[minor(dev)];
    808 	int error;
    809 
    810 	if (sc->sc_dying)
    811 		return (EIO);
    812 
    813 #ifdef DIAGNOSTIC
    814 	if (sc->sc_base.me_evp == NULL) {
    815 		printf("wskbdread: evp == NULL\n");
    816 		return (EINVAL);
    817 	}
    818 #endif
    819 
    820 	sc->sc_refcnt++;
    821 	error = wsevent_read(sc->sc_base.me_evp, uio, flags);
    822 	if (--sc->sc_refcnt < 0) {
    823 		wakeup(sc);
    824 		error = EIO;
    825 	}
    826 	return (error);
    827 }
    828 
    829 int
    830 wskbdioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
    831 {
    832 	return (wskbd_do_ioctl(wskbd_cd.cd_devs[minor(dev)], cmd, data, flag,p));
    833 }
    834 
    835 /* A wrapper around the ioctl() workhorse to make reference counting easy. */
    836 int
    837 wskbd_do_ioctl(struct device *dv, u_long cmd, caddr_t data, int flag,
    838 	struct proc *p)
    839 {
    840 	struct wskbd_softc *sc = (struct wskbd_softc *)dv;
    841 	int error;
    842 
    843 	sc->sc_refcnt++;
    844 	error = wskbd_do_ioctl_sc(sc, cmd, data, flag, p);
    845 	if (--sc->sc_refcnt < 0)
    846 		wakeup(sc);
    847 	return (error);
    848 }
    849 
    850 int
    851 wskbd_do_ioctl_sc(struct wskbd_softc *sc, u_long cmd, caddr_t data, int flag,
    852 		  struct proc *p)
    853 {
    854 	int error;
    855 
    856 	/*
    857 	 * Try the generic ioctls that the wskbd interface supports.
    858 	 */
    859 	switch (cmd) {
    860 	case FIONBIO:		/* we will remove this someday (soon???) */
    861 		return (0);
    862 
    863 	case FIOASYNC:
    864 		if (sc->sc_base.me_evp == NULL)
    865 			return (EINVAL);
    866 		sc->sc_base.me_evp->async = *(int *)data != 0;
    867 		return (0);
    868 
    869 	case TIOCSPGRP:
    870 		if (sc->sc_base.me_evp == NULL)
    871 			return (EINVAL);
    872 		if (*(int *)data != sc->sc_base.me_evp->io->p_pgid)
    873 			return (EPERM);
    874 		return (0);
    875 	}
    876 
    877 	/*
    878 	 * Try the keyboard driver for WSKBDIO ioctls.  It returns -1
    879 	 * if it didn't recognize the request.
    880 	 */
    881 	error = wskbd_displayioctl(&sc->sc_base.me_dv, cmd, data, flag, p);
    882 	return (error != -1 ? error : ENOTTY);
    883 }
    884 
    885 /*
    886  * WSKBDIO ioctls, handled in both emulation mode and in ``raw'' mode.
    887  * Some of these have no real effect in raw mode, however.
    888  */
    889 static int
    890 wskbd_displayioctl(struct device *dev, u_long cmd, caddr_t data, int flag,
    891 	struct proc *p)
    892 {
    893 	struct wskbd_softc *sc = (struct wskbd_softc *)dev;
    894 	struct wskbd_bell_data *ubdp, *kbdp;
    895 	struct wskbd_keyrepeat_data *ukdp, *kkdp;
    896 	struct wskbd_map_data *umdp;
    897 	struct wskbd_mapdata md;
    898 	kbd_t enc;
    899 	void *buf;
    900 	int len, error;
    901 
    902 	switch (cmd) {
    903 #define	SETBELL(dstp, srcp, dfltp)					\
    904     do {								\
    905 	(dstp)->pitch = ((srcp)->which & WSKBD_BELL_DOPITCH) ?		\
    906 	    (srcp)->pitch : (dfltp)->pitch;				\
    907 	(dstp)->period = ((srcp)->which & WSKBD_BELL_DOPERIOD) ?	\
    908 	    (srcp)->period : (dfltp)->period;				\
    909 	(dstp)->volume = ((srcp)->which & WSKBD_BELL_DOVOLUME) ?	\
    910 	    (srcp)->volume : (dfltp)->volume;				\
    911 	(dstp)->which = WSKBD_BELL_DOALL;				\
    912     } while (0)
    913 
    914 	case WSKBDIO_BELL:
    915 		if ((flag & FWRITE) == 0)
    916 			return (EACCES);
    917 		return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
    918 		    WSKBDIO_COMPLEXBELL, (caddr_t)&sc->sc_bell_data, flag, p));
    919 
    920 	case WSKBDIO_COMPLEXBELL:
    921 		if ((flag & FWRITE) == 0)
    922 			return (EACCES);
    923 		ubdp = (struct wskbd_bell_data *)data;
    924 		SETBELL(ubdp, ubdp, &sc->sc_bell_data);
    925 		return ((*sc->sc_accessops->ioctl)(sc->sc_accesscookie,
    926 		    WSKBDIO_COMPLEXBELL, (caddr_t)ubdp, flag, p));
    927 
    928 	case WSKBDIO_SETBELL:
    929 		if ((flag & FWRITE) == 0)
    930 			return (EACCES);
    931 		kbdp = &sc->sc_bell_data;
    932 setbell:
    933 		ubdp = (struct wskbd_bell_data *)data;
    934 		SETBELL(kbdp, ubdp, kbdp);
    935 		return (0);
    936 
    937 	case WSKBDIO_GETBELL:
    938 		kbdp = &sc->sc_bell_data;
    939 getbell:
    940 		ubdp = (struct wskbd_bell_data *)data;
    941 		SETBELL(ubdp, kbdp, kbdp);
    942 		return (0);
    943 
    944 	case WSKBDIO_SETDEFAULTBELL:
    945 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    946 			return (error);
    947 		kbdp = &wskbd_default_bell_data;
    948 		goto setbell;
    949 
    950 
    951 	case WSKBDIO_GETDEFAULTBELL:
    952 		kbdp = &wskbd_default_bell_data;
    953 		goto getbell;
    954 
    955 #undef SETBELL
    956 
    957 #define	SETKEYREPEAT(dstp, srcp, dfltp)					\
    958     do {								\
    959 	(dstp)->del1 = ((srcp)->which & WSKBD_KEYREPEAT_DODEL1) ?	\
    960 	    (srcp)->del1 : (dfltp)->del1;				\
    961 	(dstp)->delN = ((srcp)->which & WSKBD_KEYREPEAT_DODELN) ?	\
    962 	    (srcp)->delN : (dfltp)->delN;				\
    963 	(dstp)->which = WSKBD_KEYREPEAT_DOALL;				\
    964     } while (0)
    965 
    966 	case WSKBDIO_SETKEYREPEAT:
    967 		if ((flag & FWRITE) == 0)
    968 			return (EACCES);
    969 		kkdp = &sc->sc_keyrepeat_data;
    970 setkeyrepeat:
    971 		ukdp = (struct wskbd_keyrepeat_data *)data;
    972 		SETKEYREPEAT(kkdp, ukdp, kkdp);
    973 		return (0);
    974 
    975 	case WSKBDIO_GETKEYREPEAT:
    976 		kkdp = &sc->sc_keyrepeat_data;
    977 getkeyrepeat:
    978 		ukdp = (struct wskbd_keyrepeat_data *)data;
    979 		SETKEYREPEAT(ukdp, kkdp, kkdp);
    980 		return (0);
    981 
    982 	case WSKBDIO_SETDEFAULTKEYREPEAT:
    983 		if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    984 			return (error);
    985 		kkdp = &wskbd_default_keyrepeat_data;
    986 		goto setkeyrepeat;
    987 
    988 
    989 	case WSKBDIO_GETDEFAULTKEYREPEAT:
    990 		kkdp = &wskbd_default_keyrepeat_data;
    991 		goto getkeyrepeat;
    992 
    993 #undef SETKEYREPEAT
    994 
    995 	case WSKBDIO_SETMAP:
    996 		if ((flag & FWRITE) == 0)
    997 			return (EACCES);
    998 		umdp = (struct wskbd_map_data *)data;
    999 		if (umdp->maplen > WSKBDIO_MAXMAPLEN)
   1000 			return (EINVAL);
   1001 
   1002 		len = umdp->maplen*sizeof(struct wscons_keymap);
   1003 		buf = malloc(len, M_TEMP, M_WAITOK);
   1004 		error = copyin(umdp->map, buf, len);
   1005 		if (error == 0) {
   1006 			wskbd_init_keymap(umdp->maplen,
   1007 					  &sc->sc_map, &sc->sc_maplen);
   1008 			memcpy(sc->sc_map, buf, len);
   1009 			/* drop the variant bits handled by the map */
   1010 			sc->sc_layout = KB_USER |
   1011 			      (KB_VARIANT(sc->sc_layout) & KB_HANDLEDBYWSKBD);
   1012 			wskbd_update_layout(sc->id, sc->sc_layout);
   1013 		}
   1014 		free(buf, M_TEMP);
   1015 		return(error);
   1016 
   1017 	case WSKBDIO_GETMAP:
   1018 		umdp = (struct wskbd_map_data *)data;
   1019 		if (umdp->maplen > sc->sc_maplen)
   1020 			umdp->maplen = sc->sc_maplen;
   1021 		error = copyout(sc->sc_map, umdp->map,
   1022 				umdp->maplen*sizeof(struct wscons_keymap));
   1023 		return(error);
   1024 
   1025 	case WSKBDIO_GETENCODING:
   1026 		*((kbd_t *) data) = sc->sc_layout;
   1027 		return(0);
   1028 
   1029 	case WSKBDIO_SETENCODING:
   1030 		if ((flag & FWRITE) == 0)
   1031 			return (EACCES);
   1032 		enc = *((kbd_t *)data);
   1033 		if (KB_ENCODING(enc) == KB_USER) {
   1034 			/* user map must already be loaded */
   1035 			if (KB_ENCODING(sc->sc_layout) != KB_USER)
   1036 				return (EINVAL);
   1037 			/* map variants make no sense */
   1038 			if (KB_VARIANT(enc) & ~KB_HANDLEDBYWSKBD)
   1039 				return (EINVAL);
   1040 		} else {
   1041 			md = *(sc->id->t_keymap); /* structure assignment */
   1042 			md.layout = enc;
   1043 			error = wskbd_load_keymap(&md, &sc->sc_map,
   1044 						  &sc->sc_maplen);
   1045 			if (error)
   1046 				return (error);
   1047 		}
   1048 		sc->sc_layout = enc;
   1049 		wskbd_update_layout(sc->id, enc);
   1050 		return (0);
   1051 	}
   1052 
   1053 	/*
   1054 	 * Try the keyboard driver for WSKBDIO ioctls.  It returns -1
   1055 	 * if it didn't recognize the request, and in turn we return
   1056 	 * -1 if we didn't recognize the request.
   1057 	 */
   1058 /* printf("kbdaccess\n"); */
   1059 	error = (*sc->sc_accessops->ioctl)(sc->sc_accesscookie, cmd, data,
   1060 					   flag, p);
   1061 #ifdef WSDISPLAY_COMPAT_RAWKBD
   1062 	if (!error && cmd == WSKBDIO_SETMODE && *(int *)data == WSKBD_RAW) {
   1063 		int s = spltty();
   1064 		sc->id->t_modifiers &= ~(MOD_SHIFT_L | MOD_SHIFT_R
   1065 					 | MOD_CONTROL_L | MOD_CONTROL_R
   1066 					 | MOD_META_L | MOD_META_R
   1067 					 | MOD_COMMAND
   1068 					 | MOD_COMMAND1 | MOD_COMMAND2);
   1069 #if NWSDISPLAY > 0
   1070 		if (sc->sc_repeating) {
   1071 			sc->sc_repeating = 0;
   1072 			callout_stop(&sc->sc_repeat_ch);
   1073 		}
   1074 #endif
   1075 		splx(s);
   1076 	}
   1077 #endif
   1078 	return (error);
   1079 }
   1080 
   1081 int
   1082 wskbdpoll(dev_t dev, int events, struct proc *p)
   1083 {
   1084 	struct wskbd_softc *sc = wskbd_cd.cd_devs[minor(dev)];
   1085 
   1086 	if (sc->sc_base.me_evp == NULL)
   1087 		return (EINVAL);
   1088 	return (wsevent_poll(sc->sc_base.me_evp, events, p));
   1089 }
   1090 
   1091 #if NWSDISPLAY > 0
   1092 
   1093 int
   1094 wskbd_pickfree(void)
   1095 {
   1096 	int i;
   1097 	struct wskbd_softc *sc;
   1098 
   1099 	for (i = 0; i < wskbd_cd.cd_ndevs; i++) {
   1100 		if ((sc = wskbd_cd.cd_devs[i]) == NULL)
   1101 			continue;
   1102 		if (sc->sc_base.me_dispdv == NULL)
   1103 			return (i);
   1104 	}
   1105 	return (-1);
   1106 }
   1107 
   1108 struct wsevsrc *
   1109 wskbd_set_console_display(struct device *displaydv, struct wsevsrc *me)
   1110 {
   1111 	struct wskbd_softc *sc = wskbd_console_device;
   1112 
   1113 	if (sc == NULL)
   1114 		return (NULL);
   1115 	sc->sc_base.me_dispdv = displaydv;
   1116 #if NWSMUX > 0
   1117 	(void)wsmux_attach_sc((struct wsmux_softc *)me, &sc->sc_base);
   1118 #endif
   1119 	return (&sc->sc_base);
   1120 }
   1121 
   1122 int
   1123 wskbd_set_display(struct device *dv, struct wsevsrc *me)
   1124 {
   1125 	struct wskbd_softc *sc = (struct wskbd_softc *)dv;
   1126 	struct device *displaydv = me != NULL ? me->me_dispdv : NULL;
   1127 	struct device *odisplaydv;
   1128 	int error;
   1129 
   1130 	DPRINTF(("wskbd_set_display: %s me=%p odisp=%p disp=%p cons=%d\n",
   1131 		 dv->dv_xname, me, sc->sc_base.me_dispdv, displaydv,
   1132 		 sc->sc_isconsole));
   1133 
   1134 	if (sc->sc_isconsole)
   1135 		return (EBUSY);
   1136 
   1137 	if (displaydv != NULL) {
   1138 		if (sc->sc_base.me_dispdv != NULL)
   1139 			return (EBUSY);
   1140 	} else {
   1141 		if (sc->sc_base.me_dispdv == NULL)
   1142 			return (ENXIO);
   1143 	}
   1144 
   1145 	odisplaydv = sc->sc_base.me_dispdv;
   1146 	sc->sc_base.me_dispdv = NULL;
   1147 	error = wskbd_enable(sc, displaydv != NULL);
   1148 	sc->sc_base.me_dispdv = displaydv;
   1149 	if (error) {
   1150 		sc->sc_base.me_dispdv = odisplaydv;
   1151 		return (error);
   1152 	}
   1153 
   1154 	if (displaydv)
   1155 		printf("%s: connecting to %s\n",
   1156 		       sc->sc_base.me_dv.dv_xname, displaydv->dv_xname);
   1157 	else
   1158 		printf("%s: disconnecting from %s\n",
   1159 		       sc->sc_base.me_dv.dv_xname, odisplaydv->dv_xname);
   1160 
   1161 	return (0);
   1162 }
   1163 
   1164 #endif /* NWSDISPLAY > 0 */
   1165 
   1166 #if NWSMUX > 0
   1167 int
   1168 wskbd_add_mux(int unit, struct wsmux_softc *muxsc)
   1169 {
   1170 	struct wskbd_softc *sc;
   1171 
   1172 	if (unit < 0 || unit >= wskbd_cd.cd_ndevs ||
   1173 	    (sc = wskbd_cd.cd_devs[unit]) == NULL)
   1174 		return (ENXIO);
   1175 
   1176 	if (sc->sc_base.me_parent != NULL || sc->sc_base.me_evp != NULL)
   1177 		return (EBUSY);
   1178 
   1179 	return (wsmux_attach_sc(muxsc, &sc->sc_base));
   1180 }
   1181 #endif
   1182 
   1183 /*
   1184  * Console interface.
   1185  */
   1186 int
   1187 wskbd_cngetc(dev_t dev)
   1188 {
   1189 	static int num = 0;
   1190 	static int pos;
   1191 	u_int type;
   1192 	int data;
   1193 	keysym_t ks;
   1194 
   1195 	if (!wskbd_console_initted)
   1196 		return 0;
   1197 
   1198 	if (wskbd_console_device != NULL &&
   1199 	    !wskbd_console_device->sc_translating)
   1200 		return 0;
   1201 
   1202 	for(;;) {
   1203 		if (num-- > 0) {
   1204 			ks = wskbd_console_data.t_symbols[pos++];
   1205 			if (KS_GROUP(ks) == KS_GROUP_Ascii)
   1206 				return (KS_VALUE(ks));
   1207 		} else {
   1208 			(*wskbd_console_data.t_consops->getc)
   1209 				(wskbd_console_data.t_consaccesscookie,
   1210 				 &type, &data);
   1211 			num = wskbd_translate(&wskbd_console_data, type, data);
   1212 			pos = 0;
   1213 		}
   1214 	}
   1215 }
   1216 
   1217 void
   1218 wskbd_cnpollc(dev_t dev, int poll)
   1219 {
   1220 
   1221 	if (!wskbd_console_initted)
   1222 		return;
   1223 
   1224 	if (wskbd_console_device != NULL &&
   1225 	    !wskbd_console_device->sc_translating)
   1226 		return;
   1227 
   1228 	(*wskbd_console_data.t_consops->pollc)
   1229 	    (wskbd_console_data.t_consaccesscookie, poll);
   1230 }
   1231 
   1232 void
   1233 wskbd_cnbell(dev_t dev, u_int pitch, u_int period, u_int volume)
   1234 {
   1235 
   1236 	if (!wskbd_console_initted)
   1237 		return;
   1238 
   1239 	if (wskbd_console_data.t_consops->bell != NULL)
   1240 		(*wskbd_console_data.t_consops->bell)
   1241 		    (wskbd_console_data.t_consaccesscookie, pitch, period,
   1242 			volume);
   1243 }
   1244 
   1245 static inline void
   1246 update_leds(struct wskbd_internal *id)
   1247 {
   1248 	int new_state;
   1249 
   1250 	new_state = 0;
   1251 	if (id->t_modifiers & (MOD_SHIFTLOCK | MOD_CAPSLOCK))
   1252 		new_state |= WSKBD_LED_CAPS;
   1253 	if (id->t_modifiers & MOD_NUMLOCK)
   1254 		new_state |= WSKBD_LED_NUM;
   1255 	if (id->t_modifiers & MOD_COMPOSE)
   1256 		new_state |= WSKBD_LED_COMPOSE;
   1257 	if (id->t_modifiers & MOD_HOLDSCREEN)
   1258 		new_state |= WSKBD_LED_SCROLL;
   1259 
   1260 	if (id->t_sc && new_state != id->t_sc->sc_ledstate) {
   1261 		(*id->t_sc->sc_accessops->set_leds)
   1262 		    (id->t_sc->sc_accesscookie, new_state);
   1263 		id->t_sc->sc_ledstate = new_state;
   1264 	}
   1265 }
   1266 
   1267 static inline void
   1268 update_modifier(struct wskbd_internal *id, u_int type, int toggle, int mask)
   1269 {
   1270 	if (toggle) {
   1271 		if (type == WSCONS_EVENT_KEY_DOWN)
   1272 			id->t_modifiers ^= mask;
   1273 	} else {
   1274 		if (type == WSCONS_EVENT_KEY_DOWN)
   1275 			id->t_modifiers |= mask;
   1276 		else
   1277 			id->t_modifiers &= ~mask;
   1278 	}
   1279 }
   1280 
   1281 #if NWSDISPLAY > 0
   1282 static void
   1283 change_displayparam(struct wskbd_softc *sc, int param, int updown,
   1284 	int wraparound)
   1285 {
   1286 	int res;
   1287 	struct wsdisplay_param dp;
   1288 
   1289 	if (sc->sc_base.me_dispdv == NULL)
   1290 		return;
   1291 
   1292 	dp.param = param;
   1293 	res = wsdisplay_param(sc->sc_base.me_dispdv, WSDISPLAYIO_GETPARAM, &dp);
   1294 
   1295 	if (res == EINVAL)
   1296 		return; /* no such parameter */
   1297 
   1298 	dp.curval += updown;
   1299 	if (dp.max < dp.curval)
   1300 		dp.curval = wraparound ? dp.min : dp.max;
   1301 	else
   1302 	if (dp.curval < dp.min)
   1303 		dp.curval = wraparound ? dp.max : dp.min;
   1304 	wsdisplay_param(sc->sc_base.me_dispdv, WSDISPLAYIO_SETPARAM, &dp);
   1305 }
   1306 #endif
   1307 
   1308 static int
   1309 internal_command(struct wskbd_softc *sc, u_int *type, keysym_t ksym,
   1310 	keysym_t ksym2)
   1311 {
   1312 	switch (ksym) {
   1313 	case KS_Cmd:
   1314 		update_modifier(sc->id, *type, 0, MOD_COMMAND);
   1315 		ksym = ksym2;
   1316 		break;
   1317 
   1318 	case KS_Cmd1:
   1319 		update_modifier(sc->id, *type, 0, MOD_COMMAND1);
   1320 		break;
   1321 
   1322 	case KS_Cmd2:
   1323 		update_modifier(sc->id, *type, 0, MOD_COMMAND2);
   1324 		break;
   1325 	}
   1326 
   1327 	if (*type != WSCONS_EVENT_KEY_DOWN ||
   1328 	    (! MOD_ONESET(sc->id, MOD_COMMAND) &&
   1329 	     ! MOD_ALLSET(sc->id, MOD_COMMAND1 | MOD_COMMAND2)))
   1330 		return (0);
   1331 
   1332 	switch (ksym) {
   1333 #if defined(DDB) || defined(KGDB)
   1334 	case KS_Cmd_Debugger:
   1335 		if (sc->sc_isconsole) {
   1336 #ifdef DDB
   1337 			console_debugger();
   1338 #endif
   1339 #ifdef KGDB
   1340 			kgdb_connect(1);
   1341 #endif
   1342 		}
   1343 		/* discard this key (ddb discarded command modifiers) */
   1344 		*type = WSCONS_EVENT_KEY_UP;
   1345 		return (1);
   1346 #endif
   1347 
   1348 #if NWSDISPLAY > 0
   1349 	case KS_Cmd_Screen0:
   1350 	case KS_Cmd_Screen1:
   1351 	case KS_Cmd_Screen2:
   1352 	case KS_Cmd_Screen3:
   1353 	case KS_Cmd_Screen4:
   1354 	case KS_Cmd_Screen5:
   1355 	case KS_Cmd_Screen6:
   1356 	case KS_Cmd_Screen7:
   1357 	case KS_Cmd_Screen8:
   1358 	case KS_Cmd_Screen9:
   1359 		wsdisplay_switch(sc->sc_base.me_dispdv, ksym - KS_Cmd_Screen0, 0);
   1360 		return (1);
   1361 	case KS_Cmd_ResetEmul:
   1362 		wsdisplay_reset(sc->sc_base.me_dispdv, WSDISPLAY_RESETEMUL);
   1363 		return (1);
   1364 	case KS_Cmd_ResetClose:
   1365 		wsdisplay_reset(sc->sc_base.me_dispdv, WSDISPLAY_RESETCLOSE);
   1366 		return (1);
   1367 	case KS_Cmd_BacklightOn:
   1368 	case KS_Cmd_BacklightOff:
   1369 	case KS_Cmd_BacklightToggle:
   1370 		change_displayparam(sc, WSDISPLAYIO_PARAM_BACKLIGHT,
   1371 				    ksym == KS_Cmd_BacklightOff ? -1 : 1,
   1372 				    ksym == KS_Cmd_BacklightToggle ? 1 : 0);
   1373 		return (1);
   1374 	case KS_Cmd_BrightnessUp:
   1375 	case KS_Cmd_BrightnessDown:
   1376 	case KS_Cmd_BrightnessRotate:
   1377 		change_displayparam(sc, WSDISPLAYIO_PARAM_BRIGHTNESS,
   1378 				    ksym == KS_Cmd_BrightnessDown ? -1 : 1,
   1379 				    ksym == KS_Cmd_BrightnessRotate ? 1 : 0);
   1380 		return (1);
   1381 	case KS_Cmd_ContrastUp:
   1382 	case KS_Cmd_ContrastDown:
   1383 	case KS_Cmd_ContrastRotate:
   1384 		change_displayparam(sc, WSDISPLAYIO_PARAM_CONTRAST,
   1385 				    ksym == KS_Cmd_ContrastDown ? -1 : 1,
   1386 				    ksym == KS_Cmd_ContrastRotate ? 1 : 0);
   1387 		return (1);
   1388 #endif
   1389 	}
   1390 	return (0);
   1391 }
   1392 
   1393 static int
   1394 wskbd_translate(struct wskbd_internal *id, u_int type, int value)
   1395 {
   1396 	struct wskbd_softc *sc = id->t_sc;
   1397 	keysym_t ksym, res, *group;
   1398 	struct wscons_keymap kpbuf, *kp;
   1399 	int iscommand = 0;
   1400 
   1401 	if (type == WSCONS_EVENT_ALL_KEYS_UP) {
   1402 		id->t_modifiers &= ~(MOD_SHIFT_L | MOD_SHIFT_R
   1403 				| MOD_CONTROL_L | MOD_CONTROL_R
   1404 				| MOD_META_L | MOD_META_R
   1405 				| MOD_MODESHIFT
   1406 				| MOD_COMMAND | MOD_COMMAND1 | MOD_COMMAND2);
   1407 		update_leds(id);
   1408 		return (0);
   1409 	}
   1410 
   1411 	if (sc != NULL) {
   1412 		if (value < 0 || value >= sc->sc_maplen) {
   1413 #ifdef DEBUG
   1414 			printf("wskbd_translate: keycode %d out of range\n",
   1415 			       value);
   1416 #endif
   1417 			return (0);
   1418 		}
   1419 		kp = sc->sc_map + value;
   1420 	} else {
   1421 		kp = &kpbuf;
   1422 		wskbd_get_mapentry(id->t_keymap, value, kp);
   1423 	}
   1424 
   1425 	/* if this key has a command, process it first */
   1426 	if (sc != NULL && kp->command != KS_voidSymbol)
   1427 		iscommand = internal_command(sc, &type, kp->command,
   1428 					     kp->group1[0]);
   1429 
   1430 	/* Now update modifiers */
   1431 	switch (kp->group1[0]) {
   1432 	case KS_Shift_L:
   1433 		update_modifier(id, type, 0, MOD_SHIFT_L);
   1434 		break;
   1435 
   1436 	case KS_Shift_R:
   1437 		update_modifier(id, type, 0, MOD_SHIFT_R);
   1438 		break;
   1439 
   1440 	case KS_Shift_Lock:
   1441 		update_modifier(id, type, 1, MOD_SHIFTLOCK);
   1442 		break;
   1443 
   1444 	case KS_Caps_Lock:
   1445 		update_modifier(id, type, 1, MOD_CAPSLOCK);
   1446 		break;
   1447 
   1448 	case KS_Control_L:
   1449 		update_modifier(id, type, 0, MOD_CONTROL_L);
   1450 		break;
   1451 
   1452 	case KS_Control_R:
   1453 		update_modifier(id, type, 0, MOD_CONTROL_R);
   1454 		break;
   1455 
   1456 	case KS_Alt_L:
   1457 		update_modifier(id, type, 0, MOD_META_L);
   1458 		break;
   1459 
   1460 	case KS_Alt_R:
   1461 		update_modifier(id, type, 0, MOD_META_R);
   1462 		break;
   1463 
   1464 	case KS_Mode_switch:
   1465 		update_modifier(id, type, 0, MOD_MODESHIFT);
   1466 		break;
   1467 
   1468 	case KS_Num_Lock:
   1469 		update_modifier(id, type, 1, MOD_NUMLOCK);
   1470 		break;
   1471 
   1472 #if NWSDISPLAY > 0
   1473 	case KS_Hold_Screen:
   1474 		if (sc != NULL) {
   1475 			update_modifier(id, type, 1, MOD_HOLDSCREEN);
   1476 			wskbd_holdscreen(sc, id->t_modifiers & MOD_HOLDSCREEN);
   1477 		}
   1478 		break;
   1479 #endif
   1480 	}
   1481 
   1482 	/* If this is a key release or we are in command mode, we are done */
   1483 	if (type != WSCONS_EVENT_KEY_DOWN || iscommand) {
   1484 		update_leds(id);
   1485 		return (0);
   1486 	}
   1487 
   1488 	/* Get the keysym */
   1489 	if (id->t_modifiers & MOD_MODESHIFT)
   1490 		group = & kp->group2[0];
   1491 	else
   1492 		group = & kp->group1[0];
   1493 
   1494 	if ((id->t_modifiers & MOD_NUMLOCK) != 0 &&
   1495 	    KS_GROUP(group[1]) == KS_GROUP_Keypad) {
   1496 		if (MOD_ONESET(id, MOD_ANYSHIFT))
   1497 			ksym = group[0];
   1498 		else
   1499 			ksym = group[1];
   1500 	} else if (! MOD_ONESET(id, MOD_ANYSHIFT | MOD_CAPSLOCK)) {
   1501 		ksym = group[0];
   1502 	} else if (MOD_ONESET(id, MOD_CAPSLOCK)) {
   1503 		if (! MOD_ONESET(id, MOD_SHIFT_L | MOD_SHIFT_R))
   1504 			ksym = group[0];
   1505 		else
   1506 			ksym = group[1];
   1507 		if (ksym >= KS_a && ksym <= KS_z)
   1508 			ksym += KS_A - KS_a;
   1509 		else if (ksym >= KS_agrave && ksym <= KS_thorn &&
   1510 			 ksym != KS_division)
   1511 			ksym += KS_Agrave - KS_agrave;
   1512 	} else if (MOD_ONESET(id, MOD_ANYSHIFT)) {
   1513 		ksym = group[1];
   1514 	} else {
   1515 		ksym = group[0];
   1516 	}
   1517 
   1518 	/* Process compose sequence and dead accents */
   1519 	res = KS_voidSymbol;
   1520 
   1521 	switch (KS_GROUP(ksym)) {
   1522 	case KS_GROUP_Ascii:
   1523 	case KS_GROUP_Keypad:
   1524 	case KS_GROUP_Function:
   1525 		res = ksym;
   1526 		break;
   1527 
   1528 	case KS_GROUP_Mod:
   1529 		if (ksym == KS_Multi_key) {
   1530 			update_modifier(id, 1, 0, MOD_COMPOSE);
   1531 			id->t_composelen = 2;
   1532 		}
   1533 		break;
   1534 
   1535 	case KS_GROUP_Dead:
   1536 		if (id->t_composelen == 0) {
   1537 			update_modifier(id, 1, 0, MOD_COMPOSE);
   1538 			id->t_composelen = 1;
   1539 			id->t_composebuf[0] = ksym;
   1540 		} else
   1541 			res = ksym;
   1542 		break;
   1543 	}
   1544 
   1545 	if (res == KS_voidSymbol) {
   1546 		update_leds(id);
   1547 		return (0);
   1548 	}
   1549 
   1550 	if (id->t_composelen > 0) {
   1551 		id->t_composebuf[2 - id->t_composelen] = res;
   1552 		if (--id->t_composelen == 0) {
   1553 			res = wskbd_compose_value(id->t_composebuf);
   1554 			update_modifier(id, 0, 0, MOD_COMPOSE);
   1555 		} else {
   1556 			return (0);
   1557 		}
   1558 	}
   1559 
   1560 	update_leds(id);
   1561 
   1562 	/* We are done, return the symbol */
   1563 	if (KS_GROUP(res) == KS_GROUP_Ascii) {
   1564 		if (MOD_ONESET(id, MOD_ANYCONTROL)) {
   1565 			if ((res >= KS_at && res <= KS_z) || res == KS_space)
   1566 				res = res & 0x1f;
   1567 			else if (res == KS_2)
   1568 				res = 0x00;
   1569 			else if (res >= KS_3 && res <= KS_7)
   1570 				res = KS_Escape + (res - KS_3);
   1571 			else if (res == KS_8)
   1572 				res = KS_Delete;
   1573 		}
   1574 		if (MOD_ONESET(id, MOD_ANYMETA)) {
   1575 			if (id->t_flags & WSKFL_METAESC) {
   1576 				id->t_symbols[0] = KS_Escape;
   1577 				id->t_symbols[1] = res;
   1578 				return (2);
   1579 			} else
   1580 				res |= 0x80;
   1581 		}
   1582 	}
   1583 
   1584 	id->t_symbols[0] = res;
   1585 	return (1);
   1586 }
   1587