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