Home | History | Annotate | Line # | Download | only in hil
hilkbd.c revision 1.2
      1  1.2  tsutsui /*	$NetBSD: hilkbd.c,v 1.2 2011/02/15 11:05:51 tsutsui Exp $	*/
      2  1.1  tsutsui /*	$OpenBSD: hilkbd.c,v 1.14 2009/01/21 21:53:59 grange Exp $	*/
      3  1.1  tsutsui /*
      4  1.1  tsutsui  * Copyright (c) 2003, Miodrag Vallat.
      5  1.1  tsutsui  * All rights reserved.
      6  1.1  tsutsui  *
      7  1.1  tsutsui  * Redistribution and use in source and binary forms, with or without
      8  1.1  tsutsui  * modification, are permitted provided that the following conditions
      9  1.1  tsutsui  * are met:
     10  1.1  tsutsui  * 1. Redistributions of source code must retain the above copyright
     11  1.1  tsutsui  *    notice, this list of conditions and the following disclaimer.
     12  1.1  tsutsui  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  tsutsui  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  tsutsui  *    documentation and/or other materials provided with the distribution.
     15  1.1  tsutsui  *
     16  1.1  tsutsui  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  tsutsui  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     18  1.1  tsutsui  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     19  1.1  tsutsui  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     20  1.1  tsutsui  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     21  1.1  tsutsui  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     22  1.1  tsutsui  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1  tsutsui  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     24  1.1  tsutsui  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     25  1.1  tsutsui  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  tsutsui  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  tsutsui  *
     28  1.1  tsutsui  */
     29  1.1  tsutsui 
     30  1.1  tsutsui #include "opt_wsdisplay_compat.h"
     31  1.1  tsutsui 
     32  1.1  tsutsui #include <sys/param.h>
     33  1.1  tsutsui #include <sys/systm.h>
     34  1.1  tsutsui #include <sys/device.h>
     35  1.1  tsutsui #include <sys/ioctl.h>
     36  1.1  tsutsui #include <sys/kernel.h>
     37  1.1  tsutsui #include <sys/callout.h>
     38  1.1  tsutsui #include <sys/bus.h>
     39  1.1  tsutsui #include <sys/cpu.h>
     40  1.1  tsutsui 
     41  1.1  tsutsui #include <machine/autoconf.h>
     42  1.1  tsutsui 
     43  1.1  tsutsui #include <dev/hil/hilreg.h>
     44  1.1  tsutsui #include <dev/hil/hilvar.h>
     45  1.1  tsutsui #include <dev/hil/hildevs.h>
     46  1.1  tsutsui 
     47  1.1  tsutsui #include <dev/wscons/wsconsio.h>
     48  1.1  tsutsui #include <dev/wscons/wskbdvar.h>
     49  1.1  tsutsui #include <dev/wscons/wsksymdef.h>
     50  1.1  tsutsui #include <dev/wscons/wsksymvar.h>
     51  1.1  tsutsui 
     52  1.1  tsutsui #include <dev/hil/hilkbdmap.h>
     53  1.1  tsutsui 
     54  1.1  tsutsui struct hilkbd_softc {
     55  1.1  tsutsui 	struct hildev_softc sc_hildev;
     56  1.1  tsutsui 
     57  1.1  tsutsui 	int		sc_numleds;
     58  1.1  tsutsui 	int		sc_ledstate;
     59  1.1  tsutsui 	int		sc_enabled;
     60  1.1  tsutsui 	int		sc_console;
     61  1.1  tsutsui 	int		sc_lastarrow;
     62  1.1  tsutsui 
     63  1.1  tsutsui 	device_t	sc_wskbddev;
     64  1.1  tsutsui 
     65  1.1  tsutsui #ifdef WSDISPLAY_COMPAT_RAWKBD
     66  1.1  tsutsui 	int		sc_rawkbd;
     67  1.1  tsutsui 	int		sc_nrep;
     68  1.1  tsutsui 	char		sc_rep[HILBUFSIZE * 2];
     69  1.1  tsutsui 	struct callout	sc_rawrepeat_ch;
     70  1.1  tsutsui #define	REP_DELAY1	400
     71  1.1  tsutsui #define	REP_DELAYN	100
     72  1.1  tsutsui #endif
     73  1.1  tsutsui };
     74  1.1  tsutsui 
     75  1.2  tsutsui static int	hilkbdprobe(device_t, cfdata_t, void *);
     76  1.2  tsutsui static void	hilkbdattach(device_t, device_t, void *);
     77  1.2  tsutsui static int	hilkbddetach(device_t, int);
     78  1.1  tsutsui 
     79  1.1  tsutsui CFATTACH_DECL_NEW(hilkbd, sizeof(struct hilkbd_softc),
     80  1.1  tsutsui     hilkbdprobe, hilkbdattach, hilkbddetach, NULL);
     81  1.1  tsutsui 
     82  1.2  tsutsui static int	hilkbd_enable(void *, int);
     83  1.2  tsutsui static void	hilkbd_set_leds(void *, int);
     84  1.2  tsutsui static int	hilkbd_ioctl(void *, u_long, void *, int, struct lwp *);
     85  1.1  tsutsui 
     86  1.2  tsutsui static const struct wskbd_accessops hilkbd_accessops = {
     87  1.1  tsutsui 	hilkbd_enable,
     88  1.1  tsutsui 	hilkbd_set_leds,
     89  1.1  tsutsui 	hilkbd_ioctl,
     90  1.1  tsutsui };
     91  1.1  tsutsui 
     92  1.2  tsutsui static void	hilkbd_cngetc(void *, u_int *, int *);
     93  1.2  tsutsui static void	hilkbd_cnpollc(void *, int);
     94  1.2  tsutsui static void	hilkbd_cnbell(void *, u_int, u_int, u_int);
     95  1.1  tsutsui 
     96  1.2  tsutsui static const struct wskbd_consops hilkbd_consops = {
     97  1.1  tsutsui 	hilkbd_cngetc,
     98  1.1  tsutsui 	hilkbd_cnpollc,
     99  1.1  tsutsui 	hilkbd_cnbell,
    100  1.1  tsutsui };
    101  1.1  tsutsui 
    102  1.2  tsutsui static struct wskbd_mapdata hilkbd_keymapdata = {
    103  1.1  tsutsui 	hilkbd_keydesctab,
    104  1.1  tsutsui #ifdef HILKBD_LAYOUT
    105  1.1  tsutsui 	HILKBD_LAYOUT,
    106  1.1  tsutsui #else
    107  1.1  tsutsui 	KB_US,
    108  1.1  tsutsui #endif
    109  1.1  tsutsui };
    110  1.1  tsutsui 
    111  1.2  tsutsui static struct wskbd_mapdata hilkbd_keymapdata_ps2 = {
    112  1.1  tsutsui 	hilkbd_keydesctab_ps2,
    113  1.1  tsutsui #ifdef HILKBD_LAYOUT
    114  1.1  tsutsui 	HILKBD_LAYOUT,
    115  1.1  tsutsui #else
    116  1.1  tsutsui 	KB_US,
    117  1.1  tsutsui #endif
    118  1.1  tsutsui };
    119  1.1  tsutsui 
    120  1.2  tsutsui static void	hilkbd_bell(struct hil_softc *, u_int, u_int, u_int);
    121  1.2  tsutsui static void	hilkbd_callback(struct hildev_softc *, u_int, uint8_t *);
    122  1.2  tsutsui static void	hilkbd_decode(struct hilkbd_softc *, uint8_t, u_int *, int *,
    123  1.2  tsutsui 		    int);
    124  1.2  tsutsui static int	hilkbd_is_console(int);
    125  1.2  tsutsui static void	hilkbd_rawrepeat(void *);
    126  1.1  tsutsui 
    127  1.2  tsutsui static int	seen_hilkbd_console;
    128  1.1  tsutsui 
    129  1.1  tsutsui int
    130  1.1  tsutsui hilkbdprobe(device_t parent, cfdata_t cf, void *aux)
    131  1.1  tsutsui {
    132  1.1  tsutsui 	struct hil_attach_args *ha = aux;
    133  1.1  tsutsui 
    134  1.1  tsutsui 	if (ha->ha_type != HIL_DEVICE_KEYBOARD &&
    135  1.1  tsutsui 	    ha->ha_type != HIL_DEVICE_BUTTONBOX)
    136  1.2  tsutsui 		return 0;
    137  1.1  tsutsui 
    138  1.2  tsutsui 	return 1;
    139  1.1  tsutsui }
    140  1.1  tsutsui 
    141  1.1  tsutsui void
    142  1.1  tsutsui hilkbdattach(device_t parent, device_t self, void *aux)
    143  1.1  tsutsui {
    144  1.1  tsutsui 	struct hilkbd_softc *sc = device_private(self);
    145  1.1  tsutsui 	struct hil_attach_args *ha = aux;
    146  1.1  tsutsui 	struct wskbddev_attach_args a;
    147  1.2  tsutsui 	uint8_t layoutcode;
    148  1.1  tsutsui 	int ps2;
    149  1.1  tsutsui 
    150  1.1  tsutsui 	sc->sc_hildev.sc_dev = self;
    151  1.1  tsutsui 	sc->hd_code = ha->ha_code;
    152  1.1  tsutsui 	sc->hd_type = ha->ha_type;
    153  1.1  tsutsui 	sc->hd_infolen = ha->ha_infolen;
    154  1.1  tsutsui 	memcpy(sc->hd_info, ha->ha_info, ha->ha_infolen);
    155  1.1  tsutsui 	sc->hd_fn = hilkbd_callback;
    156  1.1  tsutsui 
    157  1.1  tsutsui 	if (ha->ha_type == HIL_DEVICE_KEYBOARD) {
    158  1.1  tsutsui 		/*
    159  1.1  tsutsui 		 * Determine the keyboard language configuration, but don't
    160  1.1  tsutsui 		 * override a user-specified setting.
    161  1.1  tsutsui 		 */
    162  1.1  tsutsui 		layoutcode = ha->ha_id & (MAXHILKBDLAYOUT - 1);
    163  1.1  tsutsui #ifndef HILKBD_LAYOUT
    164  1.1  tsutsui 		if (layoutcode < MAXHILKBDLAYOUT &&
    165  1.1  tsutsui 		    hilkbd_layouts[layoutcode] != -1)
    166  1.1  tsutsui 			hilkbd_keymapdata.layout =
    167  1.1  tsutsui 			hilkbd_keymapdata_ps2.layout =
    168  1.1  tsutsui 			    hilkbd_layouts[layoutcode];
    169  1.1  tsutsui #endif
    170  1.1  tsutsui 
    171  1.2  tsutsui 		aprint_normal(", layout %x", layoutcode);
    172  1.1  tsutsui 	}
    173  1.1  tsutsui 
    174  1.1  tsutsui 	/*
    175  1.1  tsutsui 	 * Interpret the identification bytes, if any
    176  1.1  tsutsui 	 */
    177  1.1  tsutsui 	if (ha->ha_infolen > 2 && (ha->ha_info[1] & HIL_IOB) != 0) {
    178  1.1  tsutsui 		/* HILIOB_PROMPT is not always reported... */
    179  1.1  tsutsui 		sc->sc_numleds = (ha->ha_info[2] & HILIOB_PMASK) >> 4;
    180  1.1  tsutsui 		if (sc->sc_numleds != 0)
    181  1.2  tsutsui 			aprint_normal(", %d leds", sc->sc_numleds);
    182  1.1  tsutsui 	}
    183  1.1  tsutsui 
    184  1.2  tsutsui 	aprint_normal("\n");
    185  1.1  tsutsui 
    186  1.1  tsutsui 	/*
    187  1.1  tsutsui 	 * Red lettered keyboards come in two flavours, the old one
    188  1.1  tsutsui 	 * with only one control key, to the left of the escape key,
    189  1.1  tsutsui 	 * and the modern one which has a PS/2 like layout, and leds.
    190  1.1  tsutsui 	 *
    191  1.1  tsutsui 	 * Unfortunately for us, they use the same device ID range.
    192  1.1  tsutsui 	 * We'll differentiate them by looking at the leds property.
    193  1.1  tsutsui 	 */
    194  1.1  tsutsui 	ps2 = (sc->sc_numleds != 0);
    195  1.1  tsutsui 
    196  1.1  tsutsui #ifdef WSDISPLAY_COMPAT_RAWKBD
    197  1.1  tsutsui 	callout_init(&sc->sc_rawrepeat_ch, 0);
    198  1.1  tsutsui 	callout_setfunc(&sc->sc_rawrepeat_ch, hilkbd_rawrepeat, sc);
    199  1.1  tsutsui #endif
    200  1.1  tsutsui 
    201  1.1  tsutsui 	/* Do not consider button boxes as console devices. */
    202  1.1  tsutsui 	if (ha->ha_type == HIL_DEVICE_BUTTONBOX)
    203  1.1  tsutsui 		a.console = 0;
    204  1.1  tsutsui 	else
    205  1.1  tsutsui 		a.console = hilkbd_is_console(ha->ha_console);
    206  1.1  tsutsui 	a.keymap = ps2 ? &hilkbd_keymapdata_ps2 : &hilkbd_keymapdata;
    207  1.1  tsutsui 	a.accessops = &hilkbd_accessops;
    208  1.1  tsutsui 	a.accesscookie = sc;
    209  1.1  tsutsui 
    210  1.1  tsutsui 	if (a.console) {
    211  1.1  tsutsui 		sc->sc_console = sc->sc_enabled = 1;
    212  1.1  tsutsui 		wskbd_cnattach(&hilkbd_consops, sc, a.keymap);
    213  1.1  tsutsui 	} else {
    214  1.1  tsutsui 		sc->sc_console = sc->sc_enabled = 0;
    215  1.1  tsutsui 	}
    216  1.1  tsutsui 
    217  1.1  tsutsui 	sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
    218  1.1  tsutsui 
    219  1.1  tsutsui 	/*
    220  1.1  tsutsui 	 * If this is an old keyboard with a numeric pad but no ``num lock''
    221  1.1  tsutsui 	 * key, simulate it being pressed so that the keyboard runs in
    222  1.1  tsutsui 	 * numeric mode.
    223  1.1  tsutsui 	 */
    224  1.1  tsutsui 	if (!ps2 && sc->sc_wskbddev != NULL) {
    225  1.1  tsutsui 		wskbd_input(sc->sc_wskbddev, WSCONS_EVENT_KEY_DOWN, 80);
    226  1.1  tsutsui 		wskbd_input(sc->sc_wskbddev, WSCONS_EVENT_KEY_UP, 80);
    227  1.1  tsutsui 	}
    228  1.1  tsutsui }
    229  1.1  tsutsui 
    230  1.1  tsutsui int
    231  1.2  tsutsui hilkbddetach(device_t self, int flags)
    232  1.1  tsutsui {
    233  1.1  tsutsui 	struct hilkbd_softc *sc = device_private(self);
    234  1.1  tsutsui 
    235  1.1  tsutsui 	/*
    236  1.1  tsutsui 	 * Handle console keyboard for the best. It should have been set
    237  1.1  tsutsui 	 * as the first device in the loop anyways.
    238  1.1  tsutsui 	 */
    239  1.1  tsutsui 	if (sc->sc_console) {
    240  1.1  tsutsui 		wskbd_cndetach();
    241  1.1  tsutsui 		seen_hilkbd_console = 0;
    242  1.1  tsutsui 	}
    243  1.1  tsutsui 
    244  1.1  tsutsui 	if (sc->sc_wskbddev != NULL)
    245  1.1  tsutsui 		return config_detach(sc->sc_wskbddev, flags);
    246  1.1  tsutsui 
    247  1.2  tsutsui 	return 0;
    248  1.1  tsutsui }
    249  1.1  tsutsui 
    250  1.1  tsutsui int
    251  1.1  tsutsui hilkbd_enable(void *v, int on)
    252  1.1  tsutsui {
    253  1.1  tsutsui 	struct hilkbd_softc *sc = v;
    254  1.1  tsutsui 
    255  1.1  tsutsui 	if (on) {
    256  1.1  tsutsui 		if (sc->sc_enabled)
    257  1.2  tsutsui 			return EBUSY;
    258  1.1  tsutsui 	} else {
    259  1.1  tsutsui 		if (sc->sc_console)
    260  1.2  tsutsui 			return EBUSY;
    261  1.1  tsutsui 	}
    262  1.1  tsutsui 
    263  1.1  tsutsui 	sc->sc_enabled = on;
    264  1.1  tsutsui 
    265  1.2  tsutsui 	return 0;
    266  1.1  tsutsui }
    267  1.1  tsutsui 
    268  1.1  tsutsui void
    269  1.1  tsutsui hilkbd_set_leds(void *v, int leds)
    270  1.1  tsutsui {
    271  1.1  tsutsui 	struct hilkbd_softc *sc = v;
    272  1.1  tsutsui 	struct hildev_softc *hdsc = &sc->sc_hildev;
    273  1.1  tsutsui 	int changemask;
    274  1.1  tsutsui 
    275  1.1  tsutsui 	if (sc->sc_numleds == 0)
    276  1.1  tsutsui 		return;
    277  1.1  tsutsui 
    278  1.1  tsutsui 	changemask = leds ^ sc->sc_ledstate;
    279  1.1  tsutsui 	if (changemask == 0)
    280  1.1  tsutsui 		return;
    281  1.1  tsutsui 
    282  1.1  tsutsui 	/* We do not handle more than 3 leds here */
    283  1.1  tsutsui 	if (changemask & WSKBD_LED_SCROLL)
    284  1.1  tsutsui 		send_hildev_cmd(hdsc,
    285  1.1  tsutsui 		    (leds & WSKBD_LED_SCROLL) ? HIL_PROMPT1 : HIL_ACK1,
    286  1.1  tsutsui 		    NULL, NULL);
    287  1.1  tsutsui 	if (changemask & WSKBD_LED_NUM)
    288  1.1  tsutsui 		send_hildev_cmd(hdsc,
    289  1.1  tsutsui 		    (leds & WSKBD_LED_NUM) ? HIL_PROMPT2 : HIL_ACK2,
    290  1.1  tsutsui 		    NULL, NULL);
    291  1.1  tsutsui 	if (changemask & WSKBD_LED_CAPS)
    292  1.1  tsutsui 		send_hildev_cmd(hdsc,
    293  1.1  tsutsui 		    (leds & WSKBD_LED_CAPS) ? HIL_PROMPT3 : HIL_ACK3,
    294  1.1  tsutsui 		    NULL, NULL);
    295  1.1  tsutsui 
    296  1.1  tsutsui 	sc->sc_ledstate = leds;
    297  1.1  tsutsui }
    298  1.1  tsutsui 
    299  1.1  tsutsui int
    300  1.1  tsutsui hilkbd_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
    301  1.1  tsutsui {
    302  1.1  tsutsui 	struct hilkbd_softc *sc = v;
    303  1.1  tsutsui 
    304  1.1  tsutsui 	switch (cmd) {
    305  1.1  tsutsui 	case WSKBDIO_GTYPE:
    306  1.1  tsutsui 		*(int *)data = WSKBD_TYPE_HIL;
    307  1.1  tsutsui 		return 0;
    308  1.1  tsutsui 	case WSKBDIO_SETLEDS:
    309  1.1  tsutsui 		hilkbd_set_leds(v, *(int *)data);
    310  1.1  tsutsui 		return 0;
    311  1.1  tsutsui 	case WSKBDIO_GETLEDS:
    312  1.1  tsutsui 		*(int *)data = sc->sc_ledstate;
    313  1.1  tsutsui 		return 0;
    314  1.1  tsutsui #ifdef WSDISPLAY_COMPAT_RAWKBD
    315  1.1  tsutsui 	case WSKBDIO_SETMODE:
    316  1.1  tsutsui 		sc->sc_rawkbd = *(int *)data == WSKBD_RAW;
    317  1.1  tsutsui 		callout_stop(&sc->sc_rawrepeat_ch);
    318  1.1  tsutsui 		return 0;
    319  1.1  tsutsui #endif
    320  1.1  tsutsui 	case WSKBDIO_COMPLEXBELL:
    321  1.1  tsutsui #define	d ((struct wskbd_bell_data *)data)
    322  1.1  tsutsui 		hilkbd_bell(device_private(device_parent(sc->sc_hildev.sc_dev)),
    323  1.1  tsutsui 		    d->pitch, d->period, d->volume);
    324  1.1  tsutsui #undef d
    325  1.1  tsutsui 		return 0;
    326  1.1  tsutsui 	}
    327  1.1  tsutsui 
    328  1.1  tsutsui 	return EPASSTHROUGH;
    329  1.1  tsutsui }
    330  1.1  tsutsui 
    331  1.1  tsutsui void
    332  1.1  tsutsui hilkbd_cngetc(void *v, u_int *type, int *data)
    333  1.1  tsutsui {
    334  1.1  tsutsui 	struct hilkbd_softc *sc = v;
    335  1.1  tsutsui 	struct hildev_softc *hdsc = &sc->sc_hildev;
    336  1.2  tsutsui 	uint8_t c, stat;
    337  1.1  tsutsui 
    338  1.1  tsutsui 	for (;;) {
    339  1.1  tsutsui 		while (hil_poll_data(hdsc, &stat, &c) != 0)
    340  1.1  tsutsui 			;
    341  1.1  tsutsui 
    342  1.1  tsutsui 		/*
    343  1.1  tsutsui 		 * Disregard keyboard data packet header.
    344  1.1  tsutsui 		 * Note that no key generates it, so we're safe.
    345  1.1  tsutsui 		 */
    346  1.1  tsutsui 		if (c != HIL_KBDBUTTON)
    347  1.1  tsutsui 			break;
    348  1.1  tsutsui 	}
    349  1.1  tsutsui 
    350  1.1  tsutsui 	hilkbd_decode(sc, c, type, data, HIL_KBDBUTTON);
    351  1.1  tsutsui }
    352  1.1  tsutsui 
    353  1.1  tsutsui void
    354  1.1  tsutsui hilkbd_cnpollc(void *v, int on)
    355  1.1  tsutsui {
    356  1.1  tsutsui 	struct hilkbd_softc *sc = v;
    357  1.1  tsutsui 
    358  1.1  tsutsui 	hil_set_poll(device_private(device_parent(sc->sc_hildev.sc_dev)), on);
    359  1.1  tsutsui }
    360  1.1  tsutsui 
    361  1.1  tsutsui void
    362  1.1  tsutsui hilkbd_cnbell(void *v, u_int pitch, u_int period, u_int volume)
    363  1.1  tsutsui {
    364  1.1  tsutsui 	struct hilkbd_softc *sc = v;
    365  1.1  tsutsui 
    366  1.1  tsutsui 	hilkbd_bell(device_private(device_parent(sc->sc_hildev.sc_dev)),
    367  1.1  tsutsui 	    pitch, period, volume);
    368  1.1  tsutsui }
    369  1.1  tsutsui 
    370  1.1  tsutsui void
    371  1.1  tsutsui hilkbd_bell(struct hil_softc *sc, u_int pitch, u_int period, u_int volume)
    372  1.1  tsutsui {
    373  1.2  tsutsui 	uint8_t buf[2];
    374  1.1  tsutsui 
    375  1.1  tsutsui 	/* XXX there could be at least a pitch -> HIL pitch conversion here */
    376  1.1  tsutsui #define	BELLDUR		80	/* tone duration in msec (10-2560) */
    377  1.1  tsutsui #define	BELLFREQ	8	/* tone frequency (0-63) */
    378  1.1  tsutsui 	buf[0] = ar_format(period - 10);
    379  1.1  tsutsui 	buf[1] = BELLFREQ;
    380  1.1  tsutsui 	send_hil_cmd(sc, HIL_SETTONE, buf, 2, NULL);
    381  1.1  tsutsui }
    382  1.1  tsutsui 
    383  1.1  tsutsui void
    384  1.2  tsutsui hilkbd_callback(struct hildev_softc *hdsc, u_int buflen, uint8_t *buf)
    385  1.1  tsutsui {
    386  1.1  tsutsui 	struct hilkbd_softc *sc = device_private(hdsc->sc_dev);
    387  1.1  tsutsui 	u_int type;
    388  1.1  tsutsui 	int kbdtype, key;
    389  1.1  tsutsui 	int i, s;
    390  1.1  tsutsui 
    391  1.1  tsutsui 	/*
    392  1.1  tsutsui 	 * Ignore packet if we don't need it
    393  1.1  tsutsui 	 */
    394  1.1  tsutsui 	if (sc->sc_enabled == 0)
    395  1.1  tsutsui 		return;
    396  1.1  tsutsui 
    397  1.1  tsutsui 	if (buflen == 0)
    398  1.1  tsutsui 		return;
    399  1.1  tsutsui 	switch ((kbdtype = *buf & HIL_KBDDATA)) {
    400  1.1  tsutsui 	case HIL_BUTTONBOX:
    401  1.1  tsutsui 	case HIL_KBDBUTTON:
    402  1.1  tsutsui 		break;
    403  1.1  tsutsui 	default:
    404  1.1  tsutsui 		return;
    405  1.1  tsutsui 	}
    406  1.1  tsutsui 
    407  1.1  tsutsui #ifdef WSDISPLAY_COMPAT_RAWKBD
    408  1.1  tsutsui 	if (sc->sc_rawkbd) {
    409  1.2  tsutsui 		uint8_t cbuf[HILBUFSIZE * 2];
    410  1.1  tsutsui 		int c, j, npress;
    411  1.1  tsutsui 
    412  1.1  tsutsui 		npress = j = 0;
    413  1.1  tsutsui 		for (i = 1, buf++; i < buflen; i++) {
    414  1.1  tsutsui 			hilkbd_decode(sc, *buf++, &type, &key, kbdtype);
    415  1.1  tsutsui 			c = hilkbd_raw[key];
    416  1.1  tsutsui 			if (c == 0)
    417  1.1  tsutsui 				continue;
    418  1.1  tsutsui 			/* fake extended scancode if necessary */
    419  1.1  tsutsui 			if (c & 0x80)
    420  1.1  tsutsui 				cbuf[j++] = 0xe0;
    421  1.1  tsutsui 			cbuf[j] = c & 0x7f;
    422  1.1  tsutsui 			if (type == WSCONS_EVENT_KEY_UP)
    423  1.1  tsutsui 				cbuf[j] |= 0x80;
    424  1.1  tsutsui 			else {
    425  1.1  tsutsui 				/* remember pressed keys for autorepeat */
    426  1.1  tsutsui 				if (c & 0x80)
    427  1.1  tsutsui 					sc->sc_rep[npress++] = 0xe0;
    428  1.1  tsutsui 				sc->sc_rep[npress++] = c & 0x7f;
    429  1.1  tsutsui 			}
    430  1.1  tsutsui 			j++;
    431  1.1  tsutsui 		}
    432  1.1  tsutsui 
    433  1.1  tsutsui 		s = spltty();
    434  1.1  tsutsui 		wskbd_rawinput(sc->sc_wskbddev, cbuf, j);
    435  1.1  tsutsui 		splx(s);
    436  1.1  tsutsui 		callout_stop(&sc->sc_rawrepeat_ch);
    437  1.1  tsutsui 		sc->sc_nrep = npress;
    438  1.1  tsutsui 		if (npress != 0) {
    439  1.1  tsutsui 			callout_schedule(&sc->sc_rawrepeat_ch,
    440  1.1  tsutsui 			    mstohz(REP_DELAY1));
    441  1.1  tsutsui 		}
    442  1.1  tsutsui 	} else
    443  1.1  tsutsui #endif
    444  1.1  tsutsui 	{
    445  1.1  tsutsui 		s = spltty();
    446  1.1  tsutsui 		for (i = 1, buf++; i < buflen; i++) {
    447  1.1  tsutsui 			hilkbd_decode(sc, *buf++, &type, &key, kbdtype);
    448  1.1  tsutsui 			if (sc->sc_wskbddev != NULL)
    449  1.1  tsutsui 				wskbd_input(sc->sc_wskbddev, type, key);
    450  1.1  tsutsui 		}
    451  1.1  tsutsui 		splx(s);
    452  1.1  tsutsui 	}
    453  1.1  tsutsui }
    454  1.1  tsutsui 
    455  1.1  tsutsui void
    456  1.2  tsutsui hilkbd_decode(struct hilkbd_softc *sc, uint8_t data, u_int *type, int *key,
    457  1.1  tsutsui     int kbdtype)
    458  1.1  tsutsui {
    459  1.2  tsutsui 
    460  1.1  tsutsui 	if (kbdtype == HIL_BUTTONBOX) {
    461  1.1  tsutsui 		if (data == 0x02)	/* repeat arrow */
    462  1.1  tsutsui 			data = sc->sc_lastarrow;
    463  1.1  tsutsui 		else if (data >= 0xf8)
    464  1.1  tsutsui 			sc->sc_lastarrow = data;
    465  1.1  tsutsui 	}
    466  1.1  tsutsui 
    467  1.1  tsutsui 	*type = (data & 1) ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
    468  1.1  tsutsui 	*key = data >> 1;
    469  1.1  tsutsui }
    470  1.1  tsutsui 
    471  1.1  tsutsui int
    472  1.1  tsutsui hilkbd_is_console(int hil_is_console)
    473  1.1  tsutsui {
    474  1.2  tsutsui 
    475  1.1  tsutsui 	/* if not first hil keyboard, then not the console */
    476  1.1  tsutsui 	if (seen_hilkbd_console)
    477  1.2  tsutsui 		return 0;
    478  1.1  tsutsui 
    479  1.1  tsutsui 	/* if PDC console does not match hil bus path, then not the console */
    480  1.1  tsutsui 	if (hil_is_console == 0)
    481  1.2  tsutsui 		return 0;
    482  1.1  tsutsui 
    483  1.1  tsutsui 	seen_hilkbd_console = 1;
    484  1.2  tsutsui 	return 1;
    485  1.1  tsutsui }
    486  1.1  tsutsui 
    487  1.1  tsutsui #ifdef WSDISPLAY_COMPAT_RAWKBD
    488  1.1  tsutsui void
    489  1.1  tsutsui hilkbd_rawrepeat(void *v)
    490  1.1  tsutsui {
    491  1.1  tsutsui 	struct hilkbd_softc *sc = v;
    492  1.1  tsutsui 	int s;
    493  1.1  tsutsui 
    494  1.1  tsutsui 	s = spltty();
    495  1.1  tsutsui 	wskbd_rawinput(sc->sc_wskbddev, sc->sc_rep, sc->sc_nrep);
    496  1.1  tsutsui 	splx(s);
    497  1.1  tsutsui 	callout_schedule(&sc->sc_rawrepeat_ch, mstohz(REP_DELAYN));
    498  1.1  tsutsui }
    499  1.1  tsutsui #endif
    500