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