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