Home | History | Annotate | Line # | Download | only in bluetooth
      1  1.21   thorpej /*	$NetBSD: btkbd.c,v 1.21 2021/08/07 16:19:09 thorpej Exp $	*/
      2  1.10      cube 
      3  1.10      cube /*
      4  1.10      cube  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  1.10      cube  * All rights reserved.
      6  1.10      cube  *
      7  1.10      cube  * This code is derived from software contributed to The NetBSD Foundation
      8  1.10      cube  * by Lennart Augustsson (lennart (at) augustsson.net) at
      9  1.10      cube  * Carlstedt Research & Technology.
     10  1.10      cube  *
     11  1.10      cube  * Redistribution and use in source and binary forms, with or without
     12  1.10      cube  * modification, are permitted provided that the following conditions
     13  1.10      cube  * are met:
     14  1.10      cube  * 1. Redistributions of source code must retain the above copyright
     15  1.10      cube  *    notice, this list of conditions and the following disclaimer.
     16  1.10      cube  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.10      cube  *    notice, this list of conditions and the following disclaimer in the
     18  1.10      cube  *    documentation and/or other materials provided with the distribution.
     19  1.10      cube  *
     20  1.10      cube  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  1.10      cube  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  1.10      cube  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  1.10      cube  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  1.10      cube  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  1.10      cube  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  1.10      cube  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  1.10      cube  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  1.10      cube  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  1.10      cube  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  1.10      cube  * POSSIBILITY OF SUCH DAMAGE.
     31  1.10      cube  */
     32   1.1   gdamore 
     33   1.1   gdamore /*-
     34   1.1   gdamore  * Copyright (c) 2006 Itronix Inc.
     35   1.1   gdamore  * All rights reserved.
     36   1.1   gdamore  *
     37   1.1   gdamore  * Written by Iain Hibbert for Itronix Inc.
     38   1.1   gdamore  *
     39   1.1   gdamore  * Redistribution and use in source and binary forms, with or without
     40   1.1   gdamore  * modification, are permitted provided that the following conditions
     41   1.1   gdamore  * are met:
     42   1.1   gdamore  * 1. Redistributions of source code must retain the above copyright
     43   1.1   gdamore  *    notice, this list of conditions and the following disclaimer.
     44   1.1   gdamore  * 2. Redistributions in binary form must reproduce the above copyright
     45   1.1   gdamore  *    notice, this list of conditions and the following disclaimer in the
     46   1.1   gdamore  *    documentation and/or other materials provided with the distribution.
     47   1.1   gdamore  * 3. The name of Itronix Inc. may not be used to endorse
     48   1.1   gdamore  *    or promote products derived from this software without specific
     49   1.1   gdamore  *    prior written permission.
     50   1.1   gdamore  *
     51   1.1   gdamore  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
     52   1.1   gdamore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     53   1.1   gdamore  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     54   1.1   gdamore  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
     55   1.1   gdamore  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     56   1.1   gdamore  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     57   1.1   gdamore  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     58   1.1   gdamore  * ON ANY THEORY OF LIABILITY, WHETHER IN
     59   1.1   gdamore  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     60   1.1   gdamore  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     61   1.1   gdamore  * POSSIBILITY OF SUCH DAMAGE.
     62   1.1   gdamore  */
     63   1.1   gdamore 
     64   1.1   gdamore /*
     65   1.1   gdamore  * based on dev/usb/ukbd.c
     66   1.1   gdamore  */
     67   1.1   gdamore 
     68   1.1   gdamore #include <sys/cdefs.h>
     69  1.21   thorpej __KERNEL_RCSID(0, "$NetBSD: btkbd.c,v 1.21 2021/08/07 16:19:09 thorpej Exp $");
     70   1.1   gdamore 
     71   1.1   gdamore #include <sys/param.h>
     72   1.1   gdamore #include <sys/callout.h>
     73   1.1   gdamore #include <sys/conf.h>
     74   1.1   gdamore #include <sys/device.h>
     75   1.1   gdamore #include <sys/kernel.h>
     76   1.1   gdamore #include <sys/proc.h>
     77   1.1   gdamore #include <sys/systm.h>
     78   1.1   gdamore 
     79   1.1   gdamore #include <netbt/bluetooth.h>
     80   1.1   gdamore 
     81   1.1   gdamore #include <dev/bluetooth/bthid.h>
     82   1.1   gdamore #include <dev/bluetooth/bthidev.h>
     83   1.1   gdamore 
     84  1.18    bouyer #include <dev/hid/hid.h>
     85   1.1   gdamore #include <dev/usb/usb.h>
     86   1.1   gdamore #include <dev/usb/usbhid.h>
     87   1.1   gdamore 
     88   1.1   gdamore #include <dev/wscons/wsconsio.h>
     89   1.1   gdamore #include <dev/wscons/wskbdvar.h>
     90   1.1   gdamore #include <dev/wscons/wsksymdef.h>
     91   1.1   gdamore #include <dev/wscons/wsksymvar.h>
     92   1.1   gdamore 
     93   1.1   gdamore #include "opt_wsdisplay_compat.h"
     94   1.1   gdamore #include "opt_btkbd.h"
     95   1.1   gdamore 
     96   1.1   gdamore #define	MAXKEYCODE		6
     97   1.1   gdamore #define MAXMOD			8	/* max 32 */
     98   1.1   gdamore #define MAXKEYS			(MAXMOD + (2 * MAXKEYCODE))
     99   1.1   gdamore 
    100   1.1   gdamore struct btkbd_data {
    101   1.1   gdamore 	uint32_t		modifiers;
    102   1.1   gdamore 	uint8_t			keycode[MAXKEYCODE];
    103   1.1   gdamore };
    104   1.1   gdamore 
    105   1.1   gdamore struct btkbd_mod {
    106   1.1   gdamore 	uint32_t		mask;
    107   1.1   gdamore 	uint8_t			key;
    108   1.1   gdamore };
    109   1.1   gdamore 
    110   1.1   gdamore struct btkbd_softc {
    111   1.1   gdamore 	struct bthidev		 sc_hidev;	/* device+ */
    112   1.8    plunky 	device_t		 sc_wskbd;	/* child */
    113   1.1   gdamore 	int			 sc_enabled;
    114   1.1   gdamore 
    115   1.1   gdamore 	int			(*sc_output)	/* output method */
    116   1.1   gdamore 				(struct bthidev *, uint8_t *, int);
    117   1.1   gdamore 
    118   1.1   gdamore 	/* stored data */
    119   1.1   gdamore 	struct btkbd_data	 sc_odata;
    120   1.1   gdamore 	struct btkbd_data	 sc_ndata;
    121   1.1   gdamore 
    122   1.1   gdamore 	/* input reports */
    123   1.1   gdamore 	int			 sc_nmod;
    124   1.1   gdamore 	struct hid_location	 sc_modloc[MAXMOD];
    125   1.1   gdamore 	struct btkbd_mod	 sc_mods[MAXMOD];
    126   1.1   gdamore 
    127   1.1   gdamore 	int			 sc_nkeycode;
    128   1.1   gdamore 	struct hid_location	 sc_keycodeloc;
    129   1.1   gdamore 
    130   1.1   gdamore 	/* output reports */
    131   1.1   gdamore 	struct hid_location	 sc_numloc;
    132   1.1   gdamore 	struct hid_location	 sc_capsloc;
    133   1.1   gdamore 	struct hid_location	 sc_scroloc;
    134   1.1   gdamore 	int			 sc_leds;
    135   1.1   gdamore 
    136   1.1   gdamore #ifdef WSDISPLAY_COMPAT_RAWKBD
    137   1.1   gdamore 	int			 sc_rawkbd;
    138   1.1   gdamore #ifdef BTKBD_REPEAT
    139   1.9    plunky 	callout_t		 sc_repeat;
    140   1.1   gdamore 	int			 sc_nrep;
    141   1.1   gdamore 	char			 sc_rep[MAXKEYS];
    142   1.1   gdamore #endif
    143   1.1   gdamore #endif
    144   1.1   gdamore };
    145   1.1   gdamore 
    146   1.1   gdamore /* autoconf(9) methods */
    147  1.11    cegger static int	btkbd_match(device_t, cfdata_t, void *);
    148   1.8    plunky static void	btkbd_attach(device_t, device_t, void *);
    149   1.8    plunky static int	btkbd_detach(device_t, int);
    150   1.1   gdamore 
    151   1.8    plunky CFATTACH_DECL_NEW(btkbd, sizeof(struct btkbd_softc),
    152   1.1   gdamore     btkbd_match, btkbd_attach, btkbd_detach, NULL);
    153   1.1   gdamore 
    154   1.1   gdamore /* wskbd(4) accessops */
    155   1.1   gdamore static int	btkbd_enable(void *, int);
    156   1.1   gdamore static void	btkbd_set_leds(void *, int);
    157   1.6  christos static int	btkbd_ioctl(void *, unsigned long, void *, int, struct lwp *);
    158   1.1   gdamore 
    159   1.1   gdamore static const struct wskbd_accessops btkbd_accessops = {
    160   1.1   gdamore 	btkbd_enable,
    161   1.1   gdamore 	btkbd_set_leds,
    162   1.1   gdamore 	btkbd_ioctl
    163   1.1   gdamore };
    164   1.1   gdamore 
    165   1.1   gdamore /* wskbd(4) keymap data */
    166  1.18    bouyer extern const struct wscons_keydesc hidkbd_keydesctab[];
    167   1.1   gdamore 
    168   1.1   gdamore const struct wskbd_mapdata btkbd_keymapdata = {
    169  1.18    bouyer 	hidkbd_keydesctab,
    170   1.1   gdamore #if defined(BTKBD_LAYOUT)
    171   1.1   gdamore 	BTKBD_LAYOUT,
    172   1.1   gdamore #elif defined(PCKBD_LAYOUT)
    173   1.1   gdamore 	PCKBD_LAYOUT,
    174   1.1   gdamore #else
    175   1.1   gdamore 	KB_US,
    176   1.1   gdamore #endif
    177   1.1   gdamore };
    178   1.1   gdamore 
    179   1.1   gdamore /* bthid methods */
    180   1.1   gdamore static void btkbd_input(struct bthidev *, uint8_t *, int);
    181   1.1   gdamore 
    182   1.1   gdamore /* internal prototypes */
    183   1.2      tron static const char *btkbd_parse_desc(struct btkbd_softc *, int, const void *, int);
    184   1.1   gdamore 
    185   1.1   gdamore #ifdef WSDISPLAY_COMPAT_RAWKBD
    186   1.1   gdamore #ifdef BTKBD_REPEAT
    187   1.1   gdamore static void btkbd_repeat(void *);
    188   1.1   gdamore #endif
    189   1.1   gdamore #endif
    190   1.1   gdamore 
    191   1.1   gdamore /*****************************************************************************
    192   1.1   gdamore  *
    193   1.1   gdamore  *	btkbd autoconf(9) routines
    194   1.1   gdamore  */
    195   1.1   gdamore 
    196   1.1   gdamore static int
    197  1.11    cegger btkbd_match(device_t self, cfdata_t cfdata, void *aux)
    198   1.1   gdamore {
    199   1.1   gdamore 	struct bthidev_attach_args *ba = aux;
    200   1.1   gdamore 
    201   1.1   gdamore 	if (hid_is_collection(ba->ba_desc, ba->ba_dlen, ba->ba_id,
    202   1.1   gdamore 			HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD)))
    203   1.1   gdamore 		return 1;
    204   1.1   gdamore 
    205   1.1   gdamore 	return 0;
    206   1.1   gdamore }
    207   1.1   gdamore 
    208   1.1   gdamore static void
    209   1.8    plunky btkbd_attach(device_t parent, device_t self, void *aux)
    210   1.1   gdamore {
    211   1.8    plunky 	struct btkbd_softc *sc = device_private(self);
    212   1.1   gdamore 	struct bthidev_attach_args *ba = aux;
    213   1.1   gdamore 	struct wskbddev_attach_args wska;
    214   1.1   gdamore 	const char *parserr;
    215   1.1   gdamore 
    216   1.1   gdamore 	sc->sc_output = ba->ba_output;
    217   1.1   gdamore 	ba->ba_input = btkbd_input;
    218   1.1   gdamore 
    219   1.1   gdamore 	parserr = btkbd_parse_desc(sc, ba->ba_id, ba->ba_desc, ba->ba_dlen);
    220   1.1   gdamore 	if (parserr != NULL) {
    221   1.1   gdamore 		aprint_error("%s\n", parserr);
    222   1.1   gdamore 		return;
    223   1.1   gdamore 	}
    224   1.1   gdamore 
    225   1.1   gdamore 	aprint_normal("\n");
    226   1.1   gdamore 
    227   1.1   gdamore #ifdef WSDISPLAY_COMPAT_RAWKBD
    228   1.1   gdamore #ifdef BTKBD_REPEAT
    229   1.7        ad 	callout_init(&sc->sc_repeat, 0);
    230   1.1   gdamore 	callout_setfunc(&sc->sc_repeat, btkbd_repeat, sc);
    231   1.1   gdamore #endif
    232   1.1   gdamore #endif
    233   1.1   gdamore 
    234   1.1   gdamore 	wska.console = 0;
    235   1.1   gdamore 	wska.keymap = &btkbd_keymapdata;
    236   1.1   gdamore 	wska.accessops = &btkbd_accessops;
    237   1.1   gdamore 	wska.accesscookie = sc;
    238   1.1   gdamore 
    239  1.21   thorpej 	sc->sc_wskbd = config_found(self, &wska, wskbddevprint, CFARGS_NONE);
    240  1.14    plunky 
    241  1.14    plunky 	pmf_device_register(self, NULL, NULL);
    242   1.1   gdamore }
    243   1.1   gdamore 
    244   1.1   gdamore static int
    245   1.8    plunky btkbd_detach(device_t self, int flags)
    246   1.1   gdamore {
    247   1.8    plunky 	struct btkbd_softc *sc = device_private(self);
    248   1.1   gdamore 	int err = 0;
    249   1.1   gdamore 
    250  1.14    plunky 	pmf_device_deregister(self);
    251  1.14    plunky 
    252   1.3    plunky #ifdef WSDISPLAY_COMPAT_RAWKBD
    253   1.3    plunky #ifdef BTKBD_REPEAT
    254  1.17     ozaki 	callout_halt(&sc->sc_repeat, NULL);
    255   1.9    plunky 	callout_destroy(&sc->sc_repeat);
    256   1.3    plunky #endif
    257   1.3    plunky #endif
    258   1.3    plunky 
    259   1.1   gdamore 	if (sc->sc_wskbd != NULL) {
    260   1.1   gdamore 		err = config_detach(sc->sc_wskbd, flags);
    261   1.1   gdamore 		sc->sc_wskbd = NULL;
    262   1.1   gdamore 	}
    263   1.1   gdamore 
    264   1.1   gdamore 	return err;
    265   1.1   gdamore }
    266   1.1   gdamore 
    267   1.1   gdamore static const char *
    268   1.2      tron btkbd_parse_desc(struct btkbd_softc *sc, int id, const void *desc, int dlen)
    269   1.1   gdamore {
    270   1.1   gdamore 	struct hid_data *d;
    271   1.1   gdamore 	struct hid_item h;
    272   1.1   gdamore 	int imod;
    273   1.1   gdamore 
    274   1.1   gdamore 	imod = 0;
    275   1.1   gdamore 	sc->sc_nkeycode = 0;
    276   1.1   gdamore 	d = hid_start_parse(desc, dlen, hid_input);
    277   1.1   gdamore 	while (hid_get_item(d, &h)) {
    278   1.1   gdamore 		if (h.kind != hid_input || (h.flags & HIO_CONST) ||
    279   1.1   gdamore 		    HID_GET_USAGE_PAGE(h.usage) != HUP_KEYBOARD ||
    280   1.1   gdamore 		    h.report_ID != id)
    281   1.1   gdamore 			continue;
    282   1.1   gdamore 
    283   1.1   gdamore 		if (h.flags & HIO_VARIABLE) {
    284  1.19      maxv 			if (h.loc.size != 1) {
    285  1.19      maxv 				hid_end_parse(d);
    286   1.1   gdamore 				return ("bad modifier size");
    287  1.19      maxv 			}
    288   1.1   gdamore 
    289   1.1   gdamore 			/* Single item */
    290   1.1   gdamore 			if (imod < MAXMOD) {
    291   1.1   gdamore 				sc->sc_modloc[imod] = h.loc;
    292   1.1   gdamore 				sc->sc_mods[imod].mask = 1 << imod;
    293   1.1   gdamore 				sc->sc_mods[imod].key = HID_GET_USAGE(h.usage);
    294   1.1   gdamore 				imod++;
    295  1.19      maxv 			} else {
    296  1.19      maxv 				hid_end_parse(d);
    297   1.1   gdamore 				return ("too many modifier keys");
    298  1.19      maxv 			}
    299   1.1   gdamore 		} else {
    300   1.1   gdamore 			/* Array */
    301  1.19      maxv 			if (h.loc.size != 8) {
    302  1.19      maxv 				hid_end_parse(d);
    303   1.1   gdamore 				return ("key code size != 8");
    304  1.19      maxv 			}
    305  1.19      maxv 			if (h.loc.count > MAXKEYCODE) {
    306  1.19      maxv 				hid_end_parse(d);
    307   1.1   gdamore 				return ("too many key codes");
    308  1.19      maxv 			}
    309  1.19      maxv 			if (h.loc.pos % 8 != 0) {
    310  1.19      maxv 				hid_end_parse(d);
    311   1.1   gdamore 				return ("key codes not on byte boundary");
    312  1.19      maxv 			}
    313  1.19      maxv 			if (sc->sc_nkeycode != 0) {
    314  1.19      maxv 				hid_end_parse(d);
    315   1.1   gdamore 				return ("multiple key code arrays\n");
    316  1.19      maxv 			}
    317   1.1   gdamore 			sc->sc_keycodeloc = h.loc;
    318   1.1   gdamore 			sc->sc_nkeycode = h.loc.count;
    319   1.1   gdamore 		}
    320   1.1   gdamore 	}
    321   1.1   gdamore 	sc->sc_nmod = imod;
    322   1.1   gdamore 	hid_end_parse(d);
    323   1.1   gdamore 
    324   1.1   gdamore 	hid_locate(desc, dlen, HID_USAGE2(HUP_LEDS, HUD_LED_NUM_LOCK),
    325   1.1   gdamore 		   id, hid_output, &sc->sc_numloc, NULL);
    326   1.1   gdamore 
    327   1.1   gdamore 	hid_locate(desc, dlen, HID_USAGE2(HUP_LEDS, HUD_LED_CAPS_LOCK),
    328   1.1   gdamore 		   id, hid_output, &sc->sc_capsloc, NULL);
    329   1.1   gdamore 
    330   1.1   gdamore 	hid_locate(desc, dlen, HID_USAGE2(HUP_LEDS, HUD_LED_SCROLL_LOCK),
    331   1.1   gdamore 		   id, hid_output, &sc->sc_scroloc, NULL);
    332   1.1   gdamore 
    333   1.1   gdamore 	return (NULL);
    334   1.1   gdamore }
    335   1.1   gdamore 
    336   1.1   gdamore /*****************************************************************************
    337   1.1   gdamore  *
    338   1.1   gdamore  *	wskbd(9) accessops
    339   1.1   gdamore  */
    340   1.1   gdamore 
    341   1.1   gdamore static int
    342  1.15       chs btkbd_enable(void *cookie, int on)
    343   1.1   gdamore {
    344  1.15       chs 	struct btkbd_softc *sc = cookie;
    345   1.1   gdamore 
    346   1.1   gdamore 	sc->sc_enabled = on;
    347   1.1   gdamore 	return 0;
    348   1.1   gdamore }
    349   1.1   gdamore 
    350   1.1   gdamore static void
    351  1.15       chs btkbd_set_leds(void *cookie, int leds)
    352   1.1   gdamore {
    353  1.15       chs 	struct btkbd_softc *sc = cookie;
    354   1.1   gdamore 	uint8_t report;
    355   1.1   gdamore 
    356   1.1   gdamore 	if (sc->sc_leds == leds)
    357   1.1   gdamore 		return;
    358   1.1   gdamore 
    359   1.1   gdamore 	sc->sc_leds = leds;
    360   1.1   gdamore 
    361   1.1   gdamore 	/*
    362   1.1   gdamore 	 * This is not totally correct, since we did not check the
    363   1.1   gdamore 	 * report size from the descriptor but for keyboards it should
    364   1.1   gdamore 	 * just be a single byte with the relevant bits set.
    365   1.1   gdamore 	 */
    366   1.1   gdamore 	report = 0;
    367   1.1   gdamore 	if ((leds & WSKBD_LED_SCROLL) && sc->sc_scroloc.size == 1)
    368   1.1   gdamore 		report |= 1 << sc->sc_scroloc.pos;
    369   1.1   gdamore 
    370   1.1   gdamore 	if ((leds & WSKBD_LED_NUM) && sc->sc_numloc.size == 1)
    371   1.1   gdamore 		report |= 1 << sc->sc_numloc.pos;
    372   1.1   gdamore 
    373   1.1   gdamore 	if ((leds & WSKBD_LED_CAPS) && sc->sc_capsloc.size == 1)
    374   1.1   gdamore 		report |= 1 << sc->sc_capsloc.pos;
    375   1.1   gdamore 
    376   1.1   gdamore 	if (sc->sc_output)
    377   1.1   gdamore 		(*sc->sc_output)(&sc->sc_hidev, &report, sizeof(report));
    378   1.1   gdamore }
    379   1.1   gdamore 
    380   1.1   gdamore static int
    381  1.15       chs btkbd_ioctl(void *cookie, unsigned long cmd, void *data, int flag,
    382   1.5  christos     struct lwp *l)
    383   1.1   gdamore {
    384  1.15       chs 	struct btkbd_softc *sc = cookie;
    385   1.1   gdamore 
    386   1.1   gdamore 	switch (cmd) {
    387   1.1   gdamore 	case WSKBDIO_GTYPE:
    388   1.1   gdamore 		*(int *)data = WSKBD_TYPE_BLUETOOTH;
    389   1.1   gdamore 		break;
    390   1.1   gdamore 
    391   1.1   gdamore 	case WSKBDIO_SETLEDS:
    392   1.1   gdamore 		btkbd_set_leds(sc, *(int *)data);
    393   1.1   gdamore 		break;
    394   1.1   gdamore 
    395   1.1   gdamore 	case WSKBDIO_GETLEDS:
    396   1.1   gdamore 		*(int *)data = sc->sc_leds;
    397   1.1   gdamore 		break;
    398   1.1   gdamore 
    399   1.1   gdamore #ifdef WSDISPLAY_COMPAT_RAWKBD
    400   1.1   gdamore 	case WSKBDIO_SETMODE:
    401   1.1   gdamore 		sc->sc_rawkbd = (*(int *)data == WSKBD_RAW);
    402   1.1   gdamore #ifdef BTKBD_REPEAT
    403   1.1   gdamore 		callout_stop(&sc->sc_repeat);
    404   1.1   gdamore #endif
    405   1.1   gdamore 		break;
    406   1.1   gdamore #endif
    407   1.1   gdamore 
    408   1.1   gdamore 	default:
    409   1.1   gdamore 		return EPASSTHROUGH;
    410   1.1   gdamore 	}
    411   1.1   gdamore 
    412   1.1   gdamore 	return 0;
    413   1.1   gdamore }
    414   1.1   gdamore 
    415   1.1   gdamore /*****************************************************************************
    416   1.1   gdamore  *
    417   1.1   gdamore  *	btkbd input routine, called from our parent
    418   1.1   gdamore  */
    419   1.1   gdamore 
    420   1.1   gdamore #ifdef WSDISPLAY_COMPAT_RAWKBD
    421   1.1   gdamore #define NN 0			/* no translation */
    422   1.1   gdamore /*
    423   1.1   gdamore  * Translate USB keycodes to US keyboard XT scancodes.
    424   1.1   gdamore  * Scancodes >= 0x80 represent EXTENDED keycodes.
    425   1.1   gdamore  *
    426   1.1   gdamore  * See http://www.microsoft.com/HWDEV/TECH/input/Scancode.asp
    427   1.1   gdamore  */
    428   1.1   gdamore static const u_int8_t btkbd_trtab[256] = {
    429   1.1   gdamore       NN,   NN,   NN,   NN, 0x1e, 0x30, 0x2e, 0x20, /* 00 - 07 */
    430   1.1   gdamore     0x12, 0x21, 0x22, 0x23, 0x17, 0x24, 0x25, 0x26, /* 08 - 0f */
    431   1.1   gdamore     0x32, 0x31, 0x18, 0x19, 0x10, 0x13, 0x1f, 0x14, /* 10 - 17 */
    432   1.1   gdamore     0x16, 0x2f, 0x11, 0x2d, 0x15, 0x2c, 0x02, 0x03, /* 18 - 1f */
    433   1.1   gdamore     0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, /* 20 - 27 */
    434   1.1   gdamore     0x1c, 0x01, 0x0e, 0x0f, 0x39, 0x0c, 0x0d, 0x1a, /* 28 - 2f */
    435   1.1   gdamore     0x1b, 0x2b, 0x2b, 0x27, 0x28, 0x29, 0x33, 0x34, /* 30 - 37 */
    436   1.1   gdamore     0x35, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, /* 38 - 3f */
    437   1.1   gdamore     0x41, 0x42, 0x43, 0x44, 0x57, 0x58, 0xaa, 0x46, /* 40 - 47 */
    438   1.1   gdamore     0x7f, 0xd2, 0xc7, 0xc9, 0xd3, 0xcf, 0xd1, 0xcd, /* 48 - 4f */
    439   1.1   gdamore     0xcb, 0xd0, 0xc8, 0x45, 0xb5, 0x37, 0x4a, 0x4e, /* 50 - 57 */
    440   1.1   gdamore     0x9c, 0x4f, 0x50, 0x51, 0x4b, 0x4c, 0x4d, 0x47, /* 58 - 5f */
    441   1.1   gdamore     0x48, 0x49, 0x52, 0x53, 0x56, 0xdd,   NN, 0x59, /* 60 - 67 */
    442   1.1   gdamore     0x5d, 0x5e, 0x5f,   NN,   NN,   NN,   NN,   NN, /* 68 - 6f */
    443   1.1   gdamore       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* 70 - 77 */
    444   1.1   gdamore       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* 78 - 7f */
    445   1.1   gdamore       NN,   NN,   NN,   NN,   NN, 0x7e,   NN, 0x73, /* 80 - 87 */
    446   1.1   gdamore     0x70, 0x7d, 0x79, 0x7b, 0x5c,   NN,   NN,   NN, /* 88 - 8f */
    447   1.1   gdamore       NN,   NN, 0x78, 0x77, 0x76,   NN,   NN,   NN, /* 90 - 97 */
    448   1.1   gdamore       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* 98 - 9f */
    449   1.1   gdamore       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* a0 - a7 */
    450   1.1   gdamore       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* a8 - af */
    451   1.1   gdamore       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* b0 - b7 */
    452   1.1   gdamore       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* b8 - bf */
    453   1.1   gdamore       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* c0 - c7 */
    454   1.1   gdamore       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* c8 - cf */
    455   1.1   gdamore       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* d0 - d7 */
    456   1.1   gdamore       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* d8 - df */
    457   1.1   gdamore     0x1d, 0x2a, 0x38, 0xdb, 0x9d, 0x36, 0xb8, 0xdc, /* e0 - e7 */
    458   1.1   gdamore       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* e8 - ef */
    459   1.1   gdamore       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* f0 - f7 */
    460   1.1   gdamore       NN,   NN,   NN,   NN,   NN,   NN,   NN,   NN, /* f8 - ff */
    461   1.1   gdamore };
    462   1.1   gdamore #endif
    463   1.1   gdamore 
    464   1.1   gdamore #define KEY_ERROR	0x01
    465   1.1   gdamore #define PRESS		0x000
    466   1.1   gdamore #define RELEASE		0x100
    467   1.1   gdamore #define CODEMASK	0x0ff
    468   1.1   gdamore #define ADDKEY(c)	ibuf[nkeys++] = (c)
    469   1.1   gdamore #define REP_DELAY1	400
    470   1.1   gdamore #define REP_DELAYN	100
    471   1.1   gdamore 
    472   1.1   gdamore static void
    473  1.15       chs btkbd_input(struct bthidev *hidev, uint8_t *data, int len)
    474   1.1   gdamore {
    475  1.15       chs 	struct btkbd_softc *sc = (struct btkbd_softc *)hidev;
    476   1.1   gdamore 	struct btkbd_data *ud = &sc->sc_ndata;
    477   1.1   gdamore 	uint16_t ibuf[MAXKEYS];
    478   1.1   gdamore 	uint32_t mod, omod;
    479   1.1   gdamore 	int nkeys, i, j;
    480   1.1   gdamore 	int key;
    481   1.1   gdamore 	int s;
    482   1.1   gdamore 
    483   1.1   gdamore 	if (sc->sc_wskbd == NULL || sc->sc_enabled == 0)
    484   1.1   gdamore 		return;
    485   1.1   gdamore 
    486   1.1   gdamore 	/* extract key modifiers */
    487   1.1   gdamore 	ud->modifiers = 0;
    488   1.1   gdamore 	for (i = 0 ; i < sc->sc_nmod ; i++)
    489   1.1   gdamore 		if (hid_get_data(data, &sc->sc_modloc[i]))
    490   1.1   gdamore 			ud->modifiers |= sc->sc_mods[i].mask;
    491   1.1   gdamore 
    492   1.1   gdamore 	/* extract keycodes */
    493   1.1   gdamore 	memcpy(ud->keycode, data + (sc->sc_keycodeloc.pos / 8),
    494   1.1   gdamore 	       sc->sc_nkeycode);
    495   1.1   gdamore 
    496   1.1   gdamore 	if (ud->keycode[0] == KEY_ERROR)
    497   1.1   gdamore 		return;		/* ignore  */
    498   1.1   gdamore 
    499   1.1   gdamore 	nkeys = 0;
    500   1.1   gdamore 	mod = ud->modifiers;
    501   1.1   gdamore 	omod = sc->sc_odata.modifiers;
    502   1.1   gdamore 	if (mod != omod)
    503   1.1   gdamore 		for (i = 0 ; i < sc->sc_nmod ; i++)
    504   1.1   gdamore 			if ((mod & sc->sc_mods[i].mask) !=
    505   1.1   gdamore 			    (omod & sc->sc_mods[i].mask))
    506   1.1   gdamore 				ADDKEY(sc->sc_mods[i].key |
    507   1.1   gdamore 				       (mod & sc->sc_mods[i].mask
    508   1.1   gdamore 					  ? PRESS : RELEASE));
    509   1.1   gdamore 
    510   1.1   gdamore 	if (memcmp(ud->keycode, sc->sc_odata.keycode, sc->sc_nkeycode) != 0) {
    511   1.1   gdamore 		/* Check for released keys. */
    512   1.1   gdamore 		for (i = 0 ; i < sc->sc_nkeycode ; i++) {
    513   1.1   gdamore 			key = sc->sc_odata.keycode[i];
    514   1.1   gdamore 			if (key == 0)
    515   1.1   gdamore 				continue;
    516   1.1   gdamore 
    517   1.1   gdamore 			for (j = 0 ; j < sc->sc_nkeycode ; j++)
    518   1.1   gdamore 				if (key == ud->keycode[j])
    519   1.1   gdamore 					goto rfound;
    520   1.1   gdamore 
    521   1.1   gdamore 			ADDKEY(key | RELEASE);
    522   1.1   gdamore 
    523   1.1   gdamore 		rfound:
    524   1.1   gdamore 			;
    525   1.1   gdamore 		}
    526   1.1   gdamore 
    527   1.1   gdamore 		/* Check for pressed keys. */
    528   1.1   gdamore 		for (i = 0 ; i < sc->sc_nkeycode ; i++) {
    529   1.1   gdamore 			key = ud->keycode[i];
    530   1.1   gdamore 			if (key == 0)
    531   1.1   gdamore 				continue;
    532   1.1   gdamore 
    533   1.1   gdamore 			for (j = 0; j < sc->sc_nkeycode; j++)
    534   1.1   gdamore 				if (key == sc->sc_odata.keycode[j])
    535   1.1   gdamore 					goto pfound;
    536   1.1   gdamore 
    537   1.1   gdamore 			ADDKEY(key | PRESS);
    538   1.1   gdamore 		pfound:
    539   1.1   gdamore 			;
    540   1.1   gdamore 		}
    541   1.1   gdamore 	}
    542   1.1   gdamore 	sc->sc_odata = *ud;
    543   1.1   gdamore 
    544   1.1   gdamore 	if (nkeys == 0)
    545   1.1   gdamore 		return;
    546   1.1   gdamore 
    547   1.1   gdamore #ifdef WSDISPLAY_COMPAT_RAWKBD
    548   1.1   gdamore 	if (sc->sc_rawkbd) {
    549   1.1   gdamore 		u_char cbuf[MAXKEYS * 2];
    550   1.1   gdamore 		int c;
    551  1.16    martin #ifdef BTKBD_REPEAT
    552  1.16    martin 		int npress = 0;
    553  1.16    martin #endif
    554   1.1   gdamore 
    555  1.16    martin 		for (i = j = 0 ; i < nkeys ; i++) {
    556   1.1   gdamore 			key = ibuf[i];
    557   1.1   gdamore 			c = btkbd_trtab[key & CODEMASK];
    558   1.1   gdamore 			if (c == NN)
    559   1.1   gdamore 				continue;
    560   1.1   gdamore 
    561   1.1   gdamore 			if (c & 0x80)
    562   1.1   gdamore 				cbuf[j++] = 0xe0;
    563   1.1   gdamore 
    564   1.1   gdamore 			cbuf[j] = c & 0x7f;
    565   1.1   gdamore 			if (key & RELEASE)
    566   1.1   gdamore 				cbuf[j] |= 0x80;
    567   1.1   gdamore #ifdef BTKBD_REPEAT
    568   1.1   gdamore 			else {
    569   1.1   gdamore 				/* remember pressed keys for autorepeat */
    570   1.1   gdamore 				if (c & 0x80)
    571   1.1   gdamore 					sc->sc_rep[npress++] = 0xe0;
    572   1.1   gdamore 
    573   1.1   gdamore 				sc->sc_rep[npress++] = c & 0x7f;
    574   1.1   gdamore 			}
    575   1.1   gdamore #endif
    576   1.1   gdamore 
    577   1.1   gdamore 			j++;
    578   1.1   gdamore 		}
    579   1.1   gdamore 
    580   1.1   gdamore 		s = spltty();
    581   1.1   gdamore 		wskbd_rawinput(sc->sc_wskbd, cbuf, j);
    582   1.1   gdamore 		splx(s);
    583   1.1   gdamore #ifdef BTKBD_REPEAT
    584   1.1   gdamore 		callout_stop(&sc->sc_repeat);
    585   1.1   gdamore 		if (npress != 0) {
    586   1.1   gdamore 			sc->sc_nrep = npress;
    587   1.1   gdamore 			callout_schedule(&sc->sc_repeat, hz * REP_DELAY1 / 1000);
    588   1.1   gdamore 		}
    589   1.1   gdamore #endif
    590   1.1   gdamore 		return;
    591   1.1   gdamore 	}
    592   1.1   gdamore #endif
    593   1.1   gdamore 
    594   1.1   gdamore 	s = spltty();
    595   1.1   gdamore 	for (i = 0 ; i < nkeys ; i++) {
    596   1.1   gdamore 		key = ibuf[i];
    597   1.1   gdamore 		wskbd_input(sc->sc_wskbd,
    598   1.1   gdamore 		    key & RELEASE ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN,
    599   1.1   gdamore 		    key & CODEMASK);
    600   1.1   gdamore 	}
    601   1.1   gdamore 	splx(s);
    602   1.1   gdamore }
    603   1.1   gdamore 
    604   1.1   gdamore #ifdef WSDISPLAY_COMPAT_RAWKBD
    605   1.1   gdamore #ifdef BTKBD_REPEAT
    606   1.1   gdamore static void
    607   1.1   gdamore btkbd_repeat(void *arg)
    608   1.1   gdamore {
    609   1.1   gdamore 	struct btkbd_softc *sc = arg;
    610   1.1   gdamore 	int s;
    611   1.1   gdamore 
    612   1.1   gdamore 	s = spltty();
    613   1.1   gdamore 	wskbd_rawinput(sc->sc_wskbd, sc->sc_rep, sc->sc_nrep);
    614   1.1   gdamore 	splx(s);
    615   1.1   gdamore 	callout_schedule(&sc->sc_repeat, hz * REP_DELAYN / 1000);
    616   1.1   gdamore }
    617   1.1   gdamore #endif
    618   1.1   gdamore #endif
    619