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