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