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