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