Home | History | Annotate | Line # | Download | only in bluetooth
      1  1.15   thorpej /*	$NetBSD: btms.c,v 1.15 2021/08/07 16:19:09 thorpej Exp $	*/
      2   1.8      cube 
      3   1.8      cube /*
      4   1.8      cube  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5   1.8      cube  * All rights reserved.
      6   1.8      cube  *
      7   1.8      cube  * This code is derived from software contributed to The NetBSD Foundation
      8   1.8      cube  * by Lennart Augustsson (lennart (at) augustsson.net) at
      9   1.8      cube  * Carlstedt Research & Technology.
     10   1.8      cube  *
     11   1.8      cube  * Redistribution and use in source and binary forms, with or without
     12   1.8      cube  * modification, are permitted provided that the following conditions
     13   1.8      cube  * are met:
     14   1.8      cube  * 1. Redistributions of source code must retain the above copyright
     15   1.8      cube  *    notice, this list of conditions and the following disclaimer.
     16   1.8      cube  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.8      cube  *    notice, this list of conditions and the following disclaimer in the
     18   1.8      cube  *    documentation and/or other materials provided with the distribution.
     19   1.8      cube  *
     20   1.8      cube  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21   1.8      cube  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22   1.8      cube  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23   1.8      cube  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24   1.8      cube  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   1.8      cube  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   1.8      cube  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.8      cube  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   1.8      cube  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   1.8      cube  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   1.8      cube  * POSSIBILITY OF SUCH DAMAGE.
     31   1.8      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/ums.c
     66   1.1   gdamore  */
     67   1.1   gdamore 
     68   1.1   gdamore #include <sys/cdefs.h>
     69  1.15   thorpej __KERNEL_RCSID(0, "$NetBSD: btms.c,v 1.15 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/conf.h>
     73   1.1   gdamore #include <sys/device.h>
     74   1.1   gdamore #include <sys/proc.h>
     75   1.1   gdamore #include <sys/systm.h>
     76   1.1   gdamore 
     77   1.1   gdamore #include <netbt/bluetooth.h>
     78   1.1   gdamore 
     79   1.1   gdamore #include <dev/bluetooth/bthid.h>
     80   1.1   gdamore #include <dev/bluetooth/bthidev.h>
     81   1.1   gdamore 
     82  1.13    bouyer #include <dev/hid/hid.h>
     83   1.1   gdamore #include <dev/usb/usb.h>
     84   1.1   gdamore #include <dev/usb/usbhid.h>
     85   1.1   gdamore 
     86   1.1   gdamore #include <dev/wscons/wsconsio.h>
     87   1.1   gdamore #include <dev/wscons/wsmousevar.h>
     88   1.1   gdamore 
     89  1.12    nonaka #ifdef BTMS_DEBUG
     90  1.12    nonaka int btms_debug = 0;
     91  1.12    nonaka #define	BTMSDBG(s)	if (btms_debug) printf s
     92  1.12    nonaka #define	BTMSDBGN(n,s)	if (btms_debug > (n)) printf s
     93  1.12    nonaka #else
     94  1.12    nonaka #define	BTMSDBG(s)
     95  1.12    nonaka #define	BTMSDBGN(n,s)
     96  1.12    nonaka #endif
     97  1.12    nonaka 
     98   1.1   gdamore #define MAX_BUTTONS	31
     99   1.1   gdamore #define BUTTON(n)	(1 << (((n) == 1 || (n) == 2) ? 3 - (n) : (n)))
    100   1.1   gdamore #define NOTMOUSE(f)	(((f) & (HIO_CONST | HIO_RELATIVE)) != HIO_RELATIVE)
    101   1.1   gdamore 
    102   1.1   gdamore struct btms_softc {
    103   1.1   gdamore 	struct bthidev		 sc_hidev;	/* device+ */
    104   1.1   gdamore 
    105   1.7    plunky 	device_t		 sc_wsmouse;	/* child */
    106   1.1   gdamore 	int			 sc_enabled;
    107   1.1   gdamore 	uint16_t		 sc_flags;
    108   1.1   gdamore 
    109   1.1   gdamore 	/* locators */
    110   1.1   gdamore 	struct hid_location	 sc_loc_x;
    111   1.1   gdamore 	struct hid_location	 sc_loc_y;
    112   1.1   gdamore 	struct hid_location	 sc_loc_z;
    113   1.1   gdamore 	struct hid_location	 sc_loc_w;
    114   1.1   gdamore 	struct hid_location	 sc_loc_button[MAX_BUTTONS];
    115   1.1   gdamore 
    116   1.1   gdamore 	int			 sc_num_buttons;
    117   1.1   gdamore 	uint32_t		 sc_buttons;
    118   1.1   gdamore };
    119   1.1   gdamore 
    120   1.1   gdamore /* sc_flags */
    121   1.1   gdamore #define BTMS_REVZ		(1 << 0)	/* reverse Z direction */
    122   1.1   gdamore #define BTMS_HASZ		(1 << 1)	/* has Z direction */
    123   1.3    plunky #define BTMS_HASW		(1 << 2)	/* has W direction */
    124   1.1   gdamore 
    125   1.1   gdamore /* autoconf(9) methods */
    126   1.9    cegger static int	btms_match(device_t, cfdata_t, void *);
    127   1.7    plunky static void	btms_attach(device_t, device_t, void *);
    128   1.7    plunky static int	btms_detach(device_t, int);
    129   1.1   gdamore 
    130   1.7    plunky CFATTACH_DECL_NEW(btms, sizeof(struct btms_softc),
    131   1.1   gdamore     btms_match, btms_attach, btms_detach, NULL);
    132   1.1   gdamore 
    133   1.1   gdamore /* wsmouse(4) accessops */
    134   1.1   gdamore static int	btms_wsmouse_enable(void *);
    135   1.6  christos static int	btms_wsmouse_ioctl(void *, unsigned long, void *, int, struct lwp *);
    136   1.1   gdamore static void	btms_wsmouse_disable(void *);
    137   1.1   gdamore 
    138   1.1   gdamore static const struct wsmouse_accessops btms_wsmouse_accessops = {
    139   1.1   gdamore 	btms_wsmouse_enable,
    140   1.1   gdamore 	btms_wsmouse_ioctl,
    141   1.1   gdamore 	btms_wsmouse_disable,
    142   1.1   gdamore };
    143   1.1   gdamore 
    144   1.1   gdamore /* bthid methods */
    145   1.1   gdamore static void btms_input(struct bthidev *, uint8_t *, int);
    146   1.1   gdamore 
    147  1.12    nonaka #ifdef BTMS_DEBUG
    148  1.12    nonaka static void	btms_print_device(struct btms_softc *);
    149  1.12    nonaka #endif
    150  1.12    nonaka 
    151  1.12    nonaka /*
    152  1.12    nonaka  * quirks
    153  1.12    nonaka  */
    154  1.12    nonaka static const struct btms_quirk {
    155  1.12    nonaka 	int		vendor;
    156  1.12    nonaka 	int		product;
    157  1.12    nonaka 
    158  1.12    nonaka 	uint32_t	flags;
    159  1.12    nonaka #define	BTMS_QUIRK_ELECOM	__BIT(0)
    160  1.12    nonaka } btms_quirk_table[] = {
    161  1.12    nonaka 	/* ELECOM M-XG2BB */
    162  1.12    nonaka 	{ 0x056e, 0x00d2, BTMS_QUIRK_ELECOM },
    163  1.12    nonaka };
    164  1.12    nonaka 
    165  1.12    nonaka static uint32_t
    166  1.12    nonaka btms_lookup_quirk_flags(int vendor, int product)
    167  1.12    nonaka {
    168  1.12    nonaka 	const struct btms_quirk *q;
    169  1.12    nonaka 	int i;
    170  1.12    nonaka 
    171  1.12    nonaka 	for (i = 0; i < __arraycount(btms_quirk_table); ++i) {
    172  1.12    nonaka 		q = &btms_quirk_table[i];
    173  1.12    nonaka 		if (vendor == q->vendor && product == q->product)
    174  1.12    nonaka 			return q->flags;
    175  1.12    nonaka 	}
    176  1.12    nonaka 	return 0;
    177  1.12    nonaka }
    178  1.12    nonaka 
    179  1.12    nonaka static void btms_fixup_elecom(struct bthidev_attach_args *,struct btms_softc *);
    180  1.12    nonaka 
    181   1.1   gdamore /*****************************************************************************
    182   1.1   gdamore  *
    183   1.1   gdamore  *	btms autoconf(9) routines
    184   1.1   gdamore  */
    185   1.1   gdamore 
    186   1.1   gdamore static int
    187   1.9    cegger btms_match(device_t parent, cfdata_t match, void *aux)
    188   1.1   gdamore {
    189   1.1   gdamore 	struct bthidev_attach_args *ba = aux;
    190   1.1   gdamore 
    191   1.1   gdamore 	if (hid_is_collection(ba->ba_desc, ba->ba_dlen, ba->ba_id,
    192   1.1   gdamore 			    HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)))
    193   1.1   gdamore 		return 1;
    194   1.1   gdamore 
    195   1.1   gdamore 	return 0;
    196   1.1   gdamore }
    197   1.1   gdamore 
    198   1.1   gdamore static void
    199   1.7    plunky btms_attach(device_t parent, device_t self, void *aux)
    200   1.1   gdamore {
    201   1.7    plunky 	struct btms_softc *sc = device_private(self);
    202   1.1   gdamore 	struct bthidev_attach_args *ba = aux;
    203   1.1   gdamore 	struct wsmousedev_attach_args wsma;
    204   1.1   gdamore 	struct hid_location *zloc;
    205  1.12    nonaka 	uint32_t flags, quirks;
    206   1.1   gdamore 	int i, hl;
    207   1.1   gdamore 
    208   1.1   gdamore 	ba->ba_input = btms_input;
    209   1.1   gdamore 
    210  1.12    nonaka 	quirks = btms_lookup_quirk_flags(ba->ba_vendor, ba->ba_product);
    211  1.12    nonaka 
    212   1.1   gdamore 	/* control the horizontal */
    213   1.1   gdamore 	hl = hid_locate(ba->ba_desc,
    214   1.1   gdamore 			ba->ba_dlen,
    215   1.1   gdamore 			HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
    216   1.1   gdamore 			ba->ba_id,
    217   1.1   gdamore 			hid_input,
    218   1.1   gdamore 			&sc->sc_loc_x,
    219   1.1   gdamore 			&flags);
    220   1.1   gdamore 
    221   1.1   gdamore 	if (hl == 0 || NOTMOUSE(flags)) {
    222   1.7    plunky 		aprint_error("X report 0x%04x not supported\n", flags);
    223   1.1   gdamore 		return;
    224   1.1   gdamore 	}
    225   1.1   gdamore 
    226   1.1   gdamore 	/* control the vertical */
    227   1.1   gdamore 	hl = hid_locate(ba->ba_desc,
    228   1.1   gdamore 			ba->ba_dlen,
    229   1.1   gdamore 			HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
    230   1.1   gdamore 			ba->ba_id,
    231   1.1   gdamore 			hid_input,
    232   1.1   gdamore 			&sc->sc_loc_y,
    233   1.1   gdamore 			&flags);
    234   1.1   gdamore 
    235   1.1   gdamore 	if (hl == 0 || NOTMOUSE(flags)) {
    236   1.7    plunky 		aprint_error("Y report 0x%04x not supported\n", flags);
    237   1.1   gdamore 		return;
    238   1.1   gdamore 	}
    239   1.1   gdamore 
    240   1.1   gdamore 	/* Try the wheel first as the Z activator since it's tradition. */
    241   1.1   gdamore 	hl = hid_locate(ba->ba_desc,
    242   1.1   gdamore 			ba->ba_dlen,
    243   1.1   gdamore 			HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_WHEEL),
    244   1.1   gdamore 			ba->ba_id,
    245   1.1   gdamore 			hid_input,
    246   1.1   gdamore 			&sc->sc_loc_z,
    247   1.1   gdamore 			&flags);
    248   1.1   gdamore 
    249   1.1   gdamore 	zloc = &sc->sc_loc_z;
    250   1.1   gdamore 	if (hl) {
    251   1.1   gdamore 		if (NOTMOUSE(flags)) {
    252   1.7    plunky 			aprint_error("Wheel report 0x%04x ignored\n", flags);
    253   1.1   gdamore 
    254   1.1   gdamore 			/* ignore Bad Z coord */
    255   1.1   gdamore 			sc->sc_loc_z.size = 0;
    256   1.1   gdamore 		} else {
    257   1.1   gdamore 			sc->sc_flags |= BTMS_HASZ;
    258   1.1   gdamore 			/* Wheels need the Z axis reversed. */
    259   1.1   gdamore 			sc->sc_flags ^= BTMS_REVZ;
    260   1.1   gdamore 			/* Put Z on the W coordinate */
    261   1.1   gdamore 			zloc = &sc->sc_loc_w;
    262   1.1   gdamore 		}
    263   1.1   gdamore 	}
    264   1.1   gdamore 
    265   1.1   gdamore 	hl = hid_locate(ba->ba_desc,
    266   1.1   gdamore 			ba->ba_dlen,
    267   1.1   gdamore 			HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Z),
    268   1.1   gdamore 			ba->ba_id,
    269   1.1   gdamore 			hid_input,
    270   1.1   gdamore 			zloc,
    271   1.1   gdamore 			&flags);
    272   1.1   gdamore 
    273   1.3    plunky 	/*
    274   1.3    plunky 	 * The horizontal component of the scrollball can also be given by
    275   1.3    plunky 	 * Application Control Pan in the Consumer page, so if we didnt see
    276   1.3    plunky 	 * any Z then check that.
    277   1.3    plunky 	 */
    278   1.3    plunky 	if (!hl) {
    279   1.3    plunky 		hl = hid_locate(ba->ba_desc,
    280   1.3    plunky 				ba->ba_dlen,
    281   1.3    plunky 				HID_USAGE2(HUP_CONSUMER, HUC_AC_PAN),
    282   1.3    plunky 				ba->ba_id,
    283   1.3    plunky 				hid_input,
    284   1.3    plunky 				zloc,
    285   1.3    plunky 				&flags);
    286   1.3    plunky 	}
    287   1.3    plunky 
    288   1.1   gdamore 	if (hl) {
    289   1.1   gdamore 		if (NOTMOUSE(flags))
    290   1.1   gdamore 			zloc->size = 0;	/* ignore Z */
    291   1.3    plunky 		else {
    292   1.3    plunky 			if (sc->sc_flags & BTMS_HASZ)
    293   1.3    plunky 				sc->sc_flags |= BTMS_HASW;
    294   1.3    plunky 			else
    295   1.3    plunky 				sc->sc_flags |= BTMS_HASZ;
    296   1.3    plunky 		}
    297   1.1   gdamore 	}
    298   1.1   gdamore 
    299   1.1   gdamore 	for (i = 1 ; i <= MAX_BUTTONS ; i++) {
    300   1.1   gdamore 		hl = hid_locate(ba->ba_desc,
    301   1.1   gdamore 				ba->ba_dlen,
    302   1.1   gdamore 				HID_USAGE2(HUP_BUTTON, i),
    303   1.1   gdamore 				ba->ba_id,
    304   1.1   gdamore 				hid_input,
    305   1.1   gdamore 				&sc->sc_loc_button[i - 1],
    306   1.1   gdamore 				NULL);
    307   1.1   gdamore 
    308   1.1   gdamore 		if (hl == 0)
    309   1.1   gdamore 			break;
    310   1.1   gdamore 	}
    311   1.1   gdamore 	sc->sc_num_buttons = i - 1;
    312   1.1   gdamore 
    313  1.12    nonaka 	if (ISSET(quirks, BTMS_QUIRK_ELECOM))
    314  1.12    nonaka 		btms_fixup_elecom(ba, sc);
    315  1.12    nonaka 
    316   1.3    plunky 	aprint_normal(": %d button%s%s%s%s.\n",
    317   1.1   gdamore 			sc->sc_num_buttons,
    318   1.1   gdamore 			sc->sc_num_buttons == 1 ? "" : "s",
    319   1.3    plunky 			sc->sc_flags & BTMS_HASW ? ", W" : "",
    320   1.3    plunky 			sc->sc_flags & BTMS_HASZ ? " and Z dir" : "",
    321   1.3    plunky 			sc->sc_flags & BTMS_HASW ? "s" : "");
    322  1.12    nonaka #ifdef BTMS_DEBUG
    323  1.12    nonaka 	if (btms_debug)
    324  1.12    nonaka 		btms_print_device(sc);
    325  1.12    nonaka #endif
    326   1.1   gdamore 
    327   1.1   gdamore 	wsma.accessops = &btms_wsmouse_accessops;
    328   1.1   gdamore 	wsma.accesscookie = sc;
    329   1.1   gdamore 
    330  1.15   thorpej 	sc->sc_wsmouse = config_found(self, &wsma, wsmousedevprint, CFARGS_NONE);
    331  1.10    plunky 
    332  1.10    plunky 	pmf_device_register(self, NULL, NULL);
    333   1.1   gdamore }
    334   1.1   gdamore 
    335   1.1   gdamore static int
    336   1.7    plunky btms_detach(device_t self, int flags)
    337   1.1   gdamore {
    338   1.7    plunky 	struct btms_softc *sc = device_private(self);
    339   1.1   gdamore 	int err = 0;
    340   1.1   gdamore 
    341  1.10    plunky 	pmf_device_deregister(self);
    342  1.10    plunky 
    343   1.1   gdamore 	if (sc->sc_wsmouse != NULL) {
    344   1.1   gdamore 		err = config_detach(sc->sc_wsmouse, flags);
    345   1.1   gdamore 		sc->sc_wsmouse = NULL;
    346   1.1   gdamore 	}
    347   1.1   gdamore 
    348   1.1   gdamore 	return err;
    349   1.1   gdamore }
    350   1.1   gdamore 
    351   1.1   gdamore /*****************************************************************************
    352   1.1   gdamore  *
    353   1.1   gdamore  *	wsmouse(4) accessops
    354   1.1   gdamore  */
    355   1.1   gdamore 
    356   1.1   gdamore static int
    357  1.11       chs btms_wsmouse_enable(void *cookie)
    358   1.1   gdamore {
    359  1.11       chs 	struct btms_softc *sc = cookie;
    360   1.1   gdamore 
    361   1.1   gdamore 	if (sc->sc_enabled)
    362   1.1   gdamore 		return EBUSY;
    363   1.1   gdamore 
    364   1.1   gdamore 	sc->sc_enabled = 1;
    365   1.1   gdamore 	return 0;
    366   1.1   gdamore }
    367   1.1   gdamore 
    368   1.1   gdamore static int
    369  1.11       chs btms_wsmouse_ioctl(void *cookie, unsigned long cmd, void *data,
    370   1.5  christos     int flag, struct lwp *l)
    371   1.1   gdamore {
    372   1.1   gdamore 
    373   1.1   gdamore 	switch (cmd) {
    374   1.1   gdamore 	case WSMOUSEIO_GTYPE:
    375   1.1   gdamore 		*(uint *)data = WSMOUSE_TYPE_BLUETOOTH;
    376   1.1   gdamore 		break;
    377   1.1   gdamore 
    378   1.1   gdamore 	default:
    379   1.1   gdamore 		return EPASSTHROUGH;
    380   1.1   gdamore 	}
    381   1.1   gdamore 
    382   1.1   gdamore 	return 0;
    383   1.1   gdamore }
    384   1.1   gdamore 
    385   1.1   gdamore static void
    386  1.11       chs btms_wsmouse_disable(void *cookie)
    387   1.1   gdamore {
    388  1.11       chs 	struct btms_softc *sc = cookie;
    389   1.1   gdamore 
    390   1.1   gdamore 	sc->sc_enabled = 0;
    391   1.1   gdamore }
    392   1.1   gdamore 
    393   1.1   gdamore /*****************************************************************************
    394   1.1   gdamore  *
    395   1.1   gdamore  *	btms input routine, called from our parent
    396   1.1   gdamore  */
    397   1.1   gdamore 
    398   1.1   gdamore static void
    399  1.11       chs btms_input(struct bthidev *hidev, uint8_t *data, int len)
    400   1.1   gdamore {
    401  1.11       chs 	struct btms_softc *sc = (struct btms_softc *)hidev;
    402   1.1   gdamore 	int dx, dy, dz, dw;
    403   1.1   gdamore 	uint32_t buttons;
    404   1.1   gdamore 	int i, s;
    405   1.1   gdamore 
    406   1.1   gdamore 	if (sc->sc_wsmouse == NULL || sc->sc_enabled == 0)
    407   1.1   gdamore 		return;
    408   1.1   gdamore 
    409  1.12    nonaka #ifdef BTMS_DEBUG
    410  1.12    nonaka 	if (btms_debug > 9) {
    411  1.12    nonaka 		printf("%s: data: ", __func__);
    412  1.12    nonaka 		for (i = 0; i < len; ++i) {
    413  1.12    nonaka 			printf("%02x", data[i]);
    414  1.12    nonaka 		}
    415  1.12    nonaka 		printf("\n");
    416  1.12    nonaka 	}
    417  1.12    nonaka #endif
    418  1.12    nonaka 
    419   1.1   gdamore 	dx =  hid_get_data(data, &sc->sc_loc_x);
    420   1.1   gdamore 	dy = -hid_get_data(data, &sc->sc_loc_y);
    421   1.1   gdamore 	dz =  hid_get_data(data, &sc->sc_loc_z);
    422   1.1   gdamore 	dw =  hid_get_data(data, &sc->sc_loc_w);
    423   1.1   gdamore 
    424   1.1   gdamore 	if (sc->sc_flags & BTMS_REVZ)
    425   1.1   gdamore 		dz = -dz;
    426   1.1   gdamore 
    427   1.1   gdamore 	buttons = 0;
    428   1.1   gdamore 	for (i = 0 ; i < sc->sc_num_buttons ; i++)
    429   1.1   gdamore 		if (hid_get_data(data, &sc->sc_loc_button[i]))
    430   1.1   gdamore 			buttons |= BUTTON(i);
    431   1.1   gdamore 
    432  1.12    nonaka 	BTMSDBGN(9,("%s: dx=%d, dy=%d, dz=%d, dw=%d, buttons=0x%08x\n",
    433  1.12    nonaka 	    __func__, dx, dy, dz, dw, buttons));
    434   1.1   gdamore 	if (dx != 0 || dy != 0 || dz != 0 || dw != 0 || buttons != sc->sc_buttons) {
    435   1.1   gdamore 		sc->sc_buttons = buttons;
    436   1.1   gdamore 
    437   1.1   gdamore 		s = spltty();
    438   1.4    plunky 		wsmouse_input(sc->sc_wsmouse,
    439   1.4    plunky 				buttons,
    440   1.4    plunky 				dx, dy, dz, dw,
    441   1.4    plunky 				WSMOUSE_INPUT_DELTA);
    442   1.1   gdamore 		splx(s);
    443   1.1   gdamore 	}
    444   1.1   gdamore }
    445  1.12    nonaka 
    446  1.12    nonaka #ifdef BTMS_DEBUG
    447  1.12    nonaka static void
    448  1.12    nonaka btms_print_device(struct btms_softc *sc)
    449  1.12    nonaka {
    450  1.12    nonaka 	int i;
    451  1.12    nonaka 
    452  1.12    nonaka 	printf("btms: X: pos=%d, size=%d\n",
    453  1.12    nonaka 	    sc->sc_loc_x.pos, sc->sc_loc_x.size);
    454  1.12    nonaka 	printf("btms: Y: pos=%d, size=%d\n",
    455  1.12    nonaka 	    sc->sc_loc_y.pos, sc->sc_loc_y.size);
    456  1.12    nonaka 	if (sc->sc_flags & BTMS_HASZ) {
    457  1.12    nonaka 		printf("btms: Z: pos=%d, size=%d%s\n",
    458  1.12    nonaka 		    sc->sc_loc_z.pos, sc->sc_loc_z.size,
    459  1.12    nonaka 		    ((sc->sc_flags & BTMS_REVZ) ? ", REVZ" : ""));
    460  1.12    nonaka 	}
    461  1.12    nonaka 	if (sc->sc_flags & BTMS_HASW) {
    462  1.12    nonaka 		printf("btms: W: pos=%d, size=%d\n",
    463  1.12    nonaka 		    sc->sc_loc_w.pos, sc->sc_loc_w.size);
    464  1.12    nonaka 	}
    465  1.12    nonaka 
    466  1.12    nonaka 	for (i = 0; i < sc->sc_num_buttons; ++i) {
    467  1.12    nonaka 		printf("btms: button%d: pos=%d, size=%d\n", i,
    468  1.12    nonaka 		    sc->sc_loc_button[i].pos, sc->sc_loc_button[i].size);
    469  1.12    nonaka 	}
    470  1.12    nonaka }
    471  1.12    nonaka #endif
    472  1.12    nonaka 
    473  1.12    nonaka /*****************************************************************************
    474  1.12    nonaka  *
    475  1.12    nonaka  * fixup routines
    476  1.12    nonaka  */
    477  1.12    nonaka static void
    478  1.12    nonaka btms_fixup_elecom(struct bthidev_attach_args *ba, struct btms_softc *sc)
    479  1.12    nonaka {
    480  1.12    nonaka 
    481  1.12    nonaka 	switch (ba->ba_product) {
    482  1.12    nonaka 	case 0x00d2:	/* M-XG2BB */
    483  1.12    nonaka 		/* invalid Wheel and AC_Pan */
    484  1.12    nonaka 		BTMSDBG(("%s: fixup ELECOM M-XG2BB\n", __func__));
    485  1.12    nonaka 		sc->sc_loc_z.pos = 40;
    486  1.12    nonaka 		sc->sc_loc_z.size = 8;
    487  1.12    nonaka 		sc->sc_loc_w.pos = 0;
    488  1.12    nonaka 		sc->sc_loc_w.size = 0;
    489  1.12    nonaka 		sc->sc_flags = BTMS_HASZ | BTMS_REVZ;
    490  1.12    nonaka 		break;
    491  1.12    nonaka 	}
    492  1.12    nonaka }
    493