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