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