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