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