Home | History | Annotate | Line # | Download | only in bluetooth
btmagic.c revision 1.10
      1  1.10     rtr /*	$NetBSD: btmagic.c,v 1.10 2014/07/31 03:39:35 rtr Exp $	*/
      2   1.1  plunky 
      3   1.1  plunky /*-
      4   1.1  plunky  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5   1.1  plunky  * All rights reserved.
      6   1.1  plunky  *
      7   1.1  plunky  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1  plunky  * by Iain Hibbert.
      9   1.1  plunky  *
     10   1.1  plunky  * This code is derived from software contributed to The NetBSD Foundation
     11   1.1  plunky  * by Lennart Augustsson (lennart (at) augustsson.net) at
     12   1.1  plunky  * Carlstedt Research & Technology.
     13   1.1  plunky  *
     14   1.1  plunky  * Redistribution and use in source and binary forms, with or without
     15   1.1  plunky  * modification, are permitted provided that the following conditions
     16   1.1  plunky  * are met:
     17   1.1  plunky  * 1. Redistributions of source code must retain the above copyright
     18   1.1  plunky  *    notice, this list of conditions and the following disclaimer.
     19   1.1  plunky  * 2. Redistributions in binary form must reproduce the above copyright
     20   1.1  plunky  *    notice, this list of conditions and the following disclaimer in the
     21   1.1  plunky  *    documentation and/or other materials provided with the distribution.
     22   1.1  plunky  *
     23   1.1  plunky  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     24   1.1  plunky  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25   1.1  plunky  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26   1.1  plunky  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     27   1.1  plunky  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28   1.1  plunky  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29   1.1  plunky  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30   1.1  plunky  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31   1.1  plunky  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32   1.1  plunky  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33   1.1  plunky  * POSSIBILITY OF SUCH DAMAGE.
     34   1.1  plunky  */
     35   1.1  plunky /*-
     36   1.1  plunky  * Copyright (c) 2006 Itronix Inc.
     37   1.1  plunky  * All rights reserved.
     38   1.1  plunky  *
     39   1.1  plunky  * Written by Iain Hibbert for Itronix Inc.
     40   1.1  plunky  *
     41   1.1  plunky  * Redistribution and use in source and binary forms, with or without
     42   1.1  plunky  * modification, are permitted provided that the following conditions
     43   1.1  plunky  * are met:
     44   1.1  plunky  * 1. Redistributions of source code must retain the above copyright
     45   1.1  plunky  *    notice, this list of conditions and the following disclaimer.
     46   1.1  plunky  * 2. Redistributions in binary form must reproduce the above copyright
     47   1.1  plunky  *    notice, this list of conditions and the following disclaimer in the
     48   1.1  plunky  *    documentation and/or other materials provided with the distribution.
     49   1.1  plunky  * 3. The name of Itronix Inc. may not be used to endorse
     50   1.1  plunky  *    or promote products derived from this software without specific
     51   1.1  plunky  *    prior written permission.
     52   1.1  plunky  *
     53   1.1  plunky  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
     54   1.1  plunky  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     55   1.1  plunky  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     56   1.1  plunky  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
     57   1.1  plunky  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     58   1.1  plunky  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     59   1.1  plunky  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     60   1.1  plunky  * ON ANY THEORY OF LIABILITY, WHETHER IN
     61   1.1  plunky  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     62   1.1  plunky  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     63   1.1  plunky  * POSSIBILITY OF SUCH DAMAGE.
     64   1.1  plunky  */
     65   1.1  plunky 
     66   1.1  plunky 
     67   1.1  plunky /*****************************************************************************
     68   1.1  plunky  *
     69   1.1  plunky  *		Apple Bluetooth Magic Mouse driver
     70   1.1  plunky  *
     71   1.1  plunky  * The Apple Magic Mouse is a HID device but it doesn't provide a proper HID
     72   1.1  plunky  * descriptor, and requires extra initializations to enable the proprietary
     73   1.1  plunky  * touch reports. We match against the vendor-id and product-id and provide
     74   1.1  plunky  * our own Bluetooth connection handling as the bthidev driver does not cater
     75   1.1  plunky  * for such complications.
     76   1.1  plunky  *
     77   1.1  plunky  * This driver interprets the touch reports only as far as emulating a
     78   1.1  plunky  * middle mouse button and providing horizontal and vertical scroll action.
     79   1.1  plunky  * Full gesture support would be more complicated and is left as an exercise
     80   1.1  plunky  * for the reader.
     81   1.1  plunky  *
     82   1.1  plunky  * Credit for decoding the proprietary touch reports goes to Michael Poole
     83   1.1  plunky  * who wrote the Linux hid-magicmouse input driver.
     84   1.1  plunky  *
     85   1.1  plunky  *****************************************************************************/
     86   1.1  plunky 
     87   1.1  plunky #include <sys/cdefs.h>
     88  1.10     rtr __KERNEL_RCSID(0, "$NetBSD: btmagic.c,v 1.10 2014/07/31 03:39:35 rtr Exp $");
     89   1.1  plunky 
     90   1.1  plunky #include <sys/param.h>
     91   1.1  plunky #include <sys/conf.h>
     92   1.1  plunky #include <sys/device.h>
     93   1.1  plunky #include <sys/fcntl.h>
     94   1.1  plunky #include <sys/kernel.h>
     95   1.1  plunky #include <sys/malloc.h>
     96   1.1  plunky #include <sys/mbuf.h>
     97   1.1  plunky #include <sys/proc.h>
     98   1.1  plunky #include <sys/socketvar.h>
     99   1.1  plunky #include <sys/systm.h>
    100   1.1  plunky #include <sys/sysctl.h>
    101   1.1  plunky 
    102   1.1  plunky #include <prop/proplib.h>
    103   1.1  plunky 
    104   1.1  plunky #include <netbt/bluetooth.h>
    105   1.1  plunky #include <netbt/l2cap.h>
    106   1.1  plunky 
    107   1.1  plunky #include <dev/bluetooth/btdev.h>
    108   1.1  plunky #include <dev/bluetooth/bthid.h>
    109   1.1  plunky #include <dev/bluetooth/bthidev.h>
    110   1.1  plunky 
    111   1.1  plunky #include <dev/usb/hid.h>
    112   1.1  plunky #include <dev/usb/usb.h>
    113   1.1  plunky #include <dev/usb/usbdevs.h>
    114   1.1  plunky 
    115   1.1  plunky #include <dev/wscons/wsconsio.h>
    116   1.1  plunky #include <dev/wscons/wsmousevar.h>
    117   1.1  plunky 
    118   1.1  plunky #undef	DPRINTF
    119   1.1  plunky #ifdef	BTMAGIC_DEBUG
    120   1.1  plunky #define	DPRINTF(sc, ...) do {				\
    121   1.1  plunky 	printf("%s: ", device_xname((sc)->sc_dev));	\
    122   1.1  plunky 	printf(__VA_ARGS__);				\
    123   1.1  plunky 	printf("\n");					\
    124   1.1  plunky } while (/*CONSTCOND*/0)
    125   1.1  plunky #else
    126   1.1  plunky #define	DPRINTF(...)	(void)0
    127   1.1  plunky #endif
    128   1.1  plunky 
    129   1.1  plunky struct btmagic_softc {
    130   1.1  plunky 	bdaddr_t		sc_laddr;	/* local address */
    131   1.1  plunky 	bdaddr_t		sc_raddr;	/* remote address */
    132   1.1  plunky 	struct sockopt		sc_mode;	/* link mode */
    133   1.1  plunky 
    134   1.1  plunky 	device_t		sc_dev;
    135   1.1  plunky 	uint16_t		sc_state;
    136   1.1  plunky 	uint16_t		sc_flags;
    137   1.1  plunky 
    138   1.1  plunky 	callout_t		sc_timeout;
    139   1.1  plunky 
    140   1.1  plunky 	/* control */
    141   1.1  plunky 	struct l2cap_channel	*sc_ctl;
    142   1.1  plunky 	struct l2cap_channel	*sc_ctl_l;
    143   1.1  plunky 
    144   1.1  plunky 	/* interrupt */
    145   1.1  plunky 	struct l2cap_channel	*sc_int;
    146   1.1  plunky 	struct l2cap_channel	*sc_int_l;
    147   1.1  plunky 
    148   1.1  plunky 	/* wsmouse child */
    149   1.1  plunky 	device_t		sc_wsmouse;
    150   1.1  plunky 	int			sc_enabled;
    151   1.1  plunky 
    152   1.1  plunky 	/* config */
    153   1.1  plunky 	int			sc_resolution;	/* for soft scaling */
    154   1.1  plunky 	int			sc_firm;	/* firm touch threshold */
    155   1.1  plunky 	int			sc_dist;	/* scroll distance threshold */
    156   1.1  plunky 	int			sc_scale;	/* scroll descaling */
    157   1.1  plunky 	struct sysctllog	*sc_log;	/* sysctl teardown log */
    158   1.1  plunky 
    159   1.1  plunky 	/* remainders */
    160   1.1  plunky 	int			sc_rx;
    161   1.1  plunky 	int			sc_ry;
    162   1.1  plunky 	int			sc_rz;
    163   1.1  plunky 	int			sc_rw;
    164   1.1  plunky 
    165   1.1  plunky 	/* previous touches */
    166   1.1  plunky 	uint32_t		sc_smask;	/* scrolling */
    167   1.1  plunky 	int			sc_az[16];
    168   1.1  plunky 	int			sc_aw[16];
    169   1.1  plunky 
    170   1.1  plunky 	/* previous mouse buttons */
    171   1.1  plunky 	uint32_t		sc_mb;
    172   1.1  plunky };
    173   1.1  plunky 
    174   1.1  plunky /* sc_flags */
    175   1.1  plunky #define BTMAGIC_CONNECTING	__BIT(0) /* we are connecting */
    176   1.1  plunky #define BTMAGIC_ENABLED		__BIT(1) /* touch reports enabled */
    177   1.1  plunky 
    178   1.1  plunky /* sc_state */
    179   1.1  plunky #define BTMAGIC_CLOSED		0
    180   1.1  plunky #define BTMAGIC_WAIT_CTL	1
    181   1.1  plunky #define BTMAGIC_WAIT_INT	2
    182   1.1  plunky #define BTMAGIC_OPEN		3
    183   1.1  plunky 
    184   1.1  plunky /* autoconf(9) glue */
    185   1.1  plunky static int  btmagic_match(device_t, cfdata_t, void *);
    186   1.1  plunky static void btmagic_attach(device_t, device_t, void *);
    187   1.1  plunky static int  btmagic_detach(device_t, int);
    188   1.1  plunky static int  btmagic_listen(struct btmagic_softc *);
    189   1.1  plunky static int  btmagic_connect(struct btmagic_softc *);
    190   1.1  plunky static int  btmagic_sysctl_resolution(SYSCTLFN_PROTO);
    191   1.1  plunky static int  btmagic_sysctl_scale(SYSCTLFN_PROTO);
    192   1.1  plunky 
    193   1.1  plunky CFATTACH_DECL_NEW(btmagic, sizeof(struct btmagic_softc),
    194   1.1  plunky     btmagic_match, btmagic_attach, btmagic_detach, NULL);
    195   1.1  plunky 
    196   1.1  plunky /* wsmouse(4) accessops */
    197   1.1  plunky static int btmagic_wsmouse_enable(void *);
    198   1.1  plunky static int btmagic_wsmouse_ioctl(void *, unsigned long, void *, int, struct lwp *);
    199   1.1  plunky static void btmagic_wsmouse_disable(void *);
    200   1.1  plunky 
    201   1.1  plunky static const struct wsmouse_accessops btmagic_wsmouse_accessops = {
    202   1.1  plunky 	btmagic_wsmouse_enable,
    203   1.1  plunky 	btmagic_wsmouse_ioctl,
    204   1.1  plunky 	btmagic_wsmouse_disable,
    205   1.1  plunky };
    206   1.1  plunky 
    207   1.1  plunky /* bluetooth(9) protocol methods for L2CAP */
    208   1.1  plunky static void  btmagic_connecting(void *);
    209   1.1  plunky static void  btmagic_ctl_connected(void *);
    210   1.1  plunky static void  btmagic_int_connected(void *);
    211   1.1  plunky static void  btmagic_ctl_disconnected(void *, int);
    212   1.1  plunky static void  btmagic_int_disconnected(void *, int);
    213   1.1  plunky static void *btmagic_ctl_newconn(void *, struct sockaddr_bt *, struct sockaddr_bt *);
    214   1.1  plunky static void *btmagic_int_newconn(void *, struct sockaddr_bt *, struct sockaddr_bt *);
    215   1.1  plunky static void  btmagic_complete(void *, int);
    216   1.1  plunky static void  btmagic_linkmode(void *, int);
    217   1.1  plunky static void  btmagic_input(void *, struct mbuf *);
    218   1.1  plunky static void  btmagic_input_basic(struct btmagic_softc *, uint8_t *, size_t);
    219   1.1  plunky static void  btmagic_input_magic(struct btmagic_softc *, uint8_t *, size_t);
    220   1.1  plunky 
    221   1.1  plunky static const struct btproto btmagic_ctl_proto = {
    222   1.1  plunky 	btmagic_connecting,
    223   1.1  plunky 	btmagic_ctl_connected,
    224   1.1  plunky 	btmagic_ctl_disconnected,
    225   1.1  plunky 	btmagic_ctl_newconn,
    226   1.1  plunky 	btmagic_complete,
    227   1.1  plunky 	btmagic_linkmode,
    228   1.1  plunky 	btmagic_input,
    229   1.1  plunky };
    230   1.1  plunky 
    231   1.1  plunky static const struct btproto btmagic_int_proto = {
    232   1.1  plunky 	btmagic_connecting,
    233   1.1  plunky 	btmagic_int_connected,
    234   1.1  plunky 	btmagic_int_disconnected,
    235   1.1  plunky 	btmagic_int_newconn,
    236   1.1  plunky 	btmagic_complete,
    237   1.1  plunky 	btmagic_linkmode,
    238   1.1  plunky 	btmagic_input,
    239   1.1  plunky };
    240   1.1  plunky 
    241   1.1  plunky /* btmagic internals */
    242   1.1  plunky static void btmagic_timeout(void *);
    243   1.1  plunky static int  btmagic_ctl_send(struct btmagic_softc *, const uint8_t *, size_t);
    244   1.1  plunky static void btmagic_enable(struct btmagic_softc *);
    245   1.1  plunky static void btmagic_check_battery(struct btmagic_softc *);
    246   1.1  plunky static int  btmagic_scale(int, int *, int);
    247   1.1  plunky 
    248   1.1  plunky 
    249   1.1  plunky /*****************************************************************************
    250   1.1  plunky  *
    251   1.1  plunky  *	Magic Mouse autoconf(9) routines
    252   1.1  plunky  */
    253   1.1  plunky 
    254   1.1  plunky static int
    255   1.1  plunky btmagic_match(device_t self, cfdata_t cfdata, void *aux)
    256   1.1  plunky {
    257   1.1  plunky 	uint16_t v, p;
    258   1.1  plunky 
    259   1.1  plunky 	if (prop_dictionary_get_uint16(aux, BTDEVvendor, &v)
    260   1.1  plunky 	    && prop_dictionary_get_uint16(aux, BTDEVproduct, &p)
    261   1.1  plunky 	    && v == USB_VENDOR_APPLE
    262   1.1  plunky 	    && p == USB_PRODUCT_APPLE_MAGICMOUSE)
    263   1.1  plunky 		return 2;	/* trump bthidev(4) */
    264   1.1  plunky 
    265   1.1  plunky 	return 0;
    266   1.1  plunky }
    267   1.1  plunky 
    268   1.1  plunky static void
    269   1.1  plunky btmagic_attach(device_t parent, device_t self, void *aux)
    270   1.1  plunky {
    271   1.1  plunky 	struct btmagic_softc *sc = device_private(self);
    272   1.1  plunky 	struct wsmousedev_attach_args wsma;
    273   1.1  plunky 	const struct sysctlnode *node;
    274   1.1  plunky 	prop_object_t obj;
    275   1.5  plunky 	int err;
    276   1.1  plunky 
    277   1.1  plunky 	/*
    278   1.1  plunky 	 * Init softc
    279   1.1  plunky 	 */
    280   1.1  plunky 	sc->sc_dev = self;
    281   1.1  plunky 	sc->sc_state = BTMAGIC_CLOSED;
    282   1.1  plunky 	callout_init(&sc->sc_timeout, 0);
    283   1.1  plunky 	callout_setfunc(&sc->sc_timeout, btmagic_timeout, sc);
    284   1.1  plunky 	sockopt_init(&sc->sc_mode, BTPROTO_L2CAP, SO_L2CAP_LM, 0);
    285   1.1  plunky 
    286   1.1  plunky 	/*
    287   1.1  plunky 	 * extract config from proplist
    288   1.1  plunky 	 */
    289   1.1  plunky 	obj = prop_dictionary_get(aux, BTDEVladdr);
    290   1.1  plunky 	bdaddr_copy(&sc->sc_laddr, prop_data_data_nocopy(obj));
    291   1.1  plunky 
    292   1.1  plunky 	obj = prop_dictionary_get(aux, BTDEVraddr);
    293   1.1  plunky 	bdaddr_copy(&sc->sc_raddr, prop_data_data_nocopy(obj));
    294   1.1  plunky 
    295   1.1  plunky 	obj = prop_dictionary_get(aux, BTDEVmode);
    296   1.1  plunky 	if (prop_object_type(obj) == PROP_TYPE_STRING) {
    297   1.1  plunky 		if (prop_string_equals_cstring(obj, BTDEVauth))
    298   1.1  plunky 			sockopt_setint(&sc->sc_mode, L2CAP_LM_AUTH);
    299   1.1  plunky 		else if (prop_string_equals_cstring(obj, BTDEVencrypt))
    300   1.1  plunky 			sockopt_setint(&sc->sc_mode, L2CAP_LM_ENCRYPT);
    301   1.1  plunky 		else if (prop_string_equals_cstring(obj, BTDEVsecure))
    302   1.1  plunky 			sockopt_setint(&sc->sc_mode, L2CAP_LM_SECURE);
    303   1.1  plunky 		else  {
    304   1.1  plunky 			aprint_error(" unknown %s\n", BTDEVmode);
    305   1.1  plunky 			return;
    306   1.1  plunky 		}
    307   1.1  plunky 
    308   1.1  plunky 		aprint_verbose(" %s %s", BTDEVmode,
    309   1.1  plunky 		    prop_string_cstring_nocopy(obj));
    310   1.4  plunky 	} else
    311   1.4  plunky 		sockopt_setint(&sc->sc_mode, 0);
    312   1.1  plunky 
    313   1.1  plunky 	aprint_normal(": 3 buttons, W and Z dirs\n");
    314   1.1  plunky 	aprint_naive("\n");
    315   1.1  plunky 
    316   1.1  plunky 	/*
    317   1.1  plunky 	 * set defaults
    318   1.1  plunky 	 */
    319   1.1  plunky 	sc->sc_resolution = 650;
    320   1.1  plunky 	sc->sc_firm = 6;
    321   1.1  plunky 	sc->sc_dist = 130;
    322   1.1  plunky 	sc->sc_scale = 20;
    323   1.1  plunky 
    324   1.1  plunky 	sysctl_createv(&sc->sc_log, 0, NULL, &node,
    325   1.1  plunky 		0,
    326   1.1  plunky 		CTLTYPE_NODE, device_xname(self),
    327   1.1  plunky 		NULL,
    328   1.1  plunky 		NULL, 0,
    329   1.1  plunky 		NULL, 0,
    330   1.1  plunky 		CTL_HW,
    331   1.1  plunky 		CTL_CREATE, CTL_EOL);
    332   1.1  plunky 
    333   1.1  plunky 	if (node != NULL) {
    334   1.1  plunky 		sysctl_createv(&sc->sc_log, 0, NULL, NULL,
    335   1.1  plunky 			CTLFLAG_READWRITE,
    336   1.1  plunky 			CTLTYPE_INT, "soft_resolution",
    337   1.1  plunky 			NULL,
    338   1.1  plunky 			btmagic_sysctl_resolution, 0,
    339   1.3     dsl 			(void *)sc, 0,
    340   1.1  plunky 			CTL_HW, node->sysctl_num,
    341   1.1  plunky 			CTL_CREATE, CTL_EOL);
    342   1.1  plunky 
    343   1.1  plunky 		sysctl_createv(&sc->sc_log, 0, NULL, NULL,
    344   1.1  plunky 			CTLFLAG_READWRITE,
    345   1.1  plunky 			CTLTYPE_INT, "firm_touch_threshold",
    346   1.1  plunky 			NULL,
    347   1.1  plunky 			NULL, 0,
    348   1.1  plunky 			&sc->sc_firm, sizeof(sc->sc_firm),
    349   1.1  plunky 			CTL_HW, node->sysctl_num,
    350   1.1  plunky 			CTL_CREATE, CTL_EOL);
    351   1.1  plunky 
    352   1.1  plunky 		sysctl_createv(&sc->sc_log, 0, NULL, NULL,
    353   1.1  plunky 			CTLFLAG_READWRITE,
    354   1.1  plunky 			CTLTYPE_INT, "scroll_distance_threshold",
    355   1.1  plunky 			NULL,
    356   1.1  plunky 			NULL, 0,
    357   1.1  plunky 			&sc->sc_dist, sizeof(sc->sc_dist),
    358   1.1  plunky 			CTL_HW, node->sysctl_num,
    359   1.1  plunky 			CTL_CREATE, CTL_EOL);
    360   1.1  plunky 
    361   1.1  plunky 		sysctl_createv(&sc->sc_log, 0, NULL, NULL,
    362   1.1  plunky 			CTLFLAG_READWRITE,
    363   1.1  plunky 			CTLTYPE_INT, "scroll_downscale_factor",
    364   1.1  plunky 			NULL,
    365   1.1  plunky 			btmagic_sysctl_scale, 0,
    366   1.3     dsl 			(void *)sc, 0,
    367   1.1  plunky 			CTL_HW, node->sysctl_num,
    368   1.1  plunky 			CTL_CREATE, CTL_EOL);
    369   1.1  plunky 	}
    370   1.1  plunky 
    371   1.1  plunky 	/*
    372   1.1  plunky 	 * attach the wsmouse
    373   1.1  plunky 	 */
    374   1.1  plunky 	wsma.accessops = &btmagic_wsmouse_accessops;
    375   1.1  plunky 	wsma.accesscookie = self;
    376   1.1  plunky 	sc->sc_wsmouse = config_found(self, &wsma, wsmousedevprint);
    377   1.1  plunky 	if (sc->sc_wsmouse == NULL) {
    378   1.1  plunky 		aprint_error_dev(self, "failed to attach wsmouse\n");
    379   1.1  plunky 		return;
    380   1.1  plunky 	}
    381   1.1  plunky 
    382   1.2  plunky 	pmf_device_register(self, NULL, NULL);
    383   1.2  plunky 
    384   1.1  plunky 	/*
    385   1.1  plunky 	 * start bluetooth connections
    386   1.1  plunky 	 */
    387   1.1  plunky 	mutex_enter(bt_lock);
    388   1.5  plunky 	if ((err = btmagic_listen(sc)) != 0)
    389   1.5  plunky 		aprint_error_dev(self, "failed to listen (%d)\n", err);
    390   1.1  plunky 	btmagic_connect(sc);
    391   1.1  plunky 	mutex_exit(bt_lock);
    392   1.1  plunky }
    393   1.1  plunky 
    394   1.1  plunky static int
    395   1.1  plunky btmagic_detach(device_t self, int flags)
    396   1.1  plunky {
    397   1.1  plunky 	struct btmagic_softc *sc = device_private(self);
    398   1.1  plunky 	int err = 0;
    399   1.1  plunky 
    400   1.1  plunky 	mutex_enter(bt_lock);
    401   1.1  plunky 
    402   1.1  plunky 	/* release interrupt listen */
    403   1.1  plunky 	if (sc->sc_int_l != NULL) {
    404   1.7   rmind 		l2cap_detach_pcb(&sc->sc_int_l);
    405   1.1  plunky 		sc->sc_int_l = NULL;
    406   1.1  plunky 	}
    407   1.1  plunky 
    408   1.1  plunky 	/* release control listen */
    409   1.1  plunky 	if (sc->sc_ctl_l != NULL) {
    410   1.7   rmind 		l2cap_detach_pcb(&sc->sc_ctl_l);
    411   1.1  plunky 		sc->sc_ctl_l = NULL;
    412   1.1  plunky 	}
    413   1.1  plunky 
    414   1.1  plunky 	/* close interrupt channel */
    415   1.1  plunky 	if (sc->sc_int != NULL) {
    416  1.10     rtr 		l2cap_disconnect_pcb(sc->sc_int, 0);
    417   1.7   rmind 		l2cap_detach_pcb(&sc->sc_int);
    418   1.1  plunky 		sc->sc_int = NULL;
    419   1.1  plunky 	}
    420   1.1  plunky 
    421   1.1  plunky 	/* close control channel */
    422   1.1  plunky 	if (sc->sc_ctl != NULL) {
    423  1.10     rtr 		l2cap_disconnect_pcb(sc->sc_ctl, 0);
    424   1.7   rmind 		l2cap_detach_pcb(&sc->sc_ctl);
    425   1.1  plunky 		sc->sc_ctl = NULL;
    426   1.1  plunky 	}
    427   1.1  plunky 
    428   1.1  plunky 	callout_halt(&sc->sc_timeout, bt_lock);
    429   1.1  plunky 	callout_destroy(&sc->sc_timeout);
    430   1.1  plunky 
    431   1.1  plunky 	mutex_exit(bt_lock);
    432   1.1  plunky 
    433   1.2  plunky 	pmf_device_deregister(self);
    434   1.2  plunky 
    435   1.1  plunky 	sockopt_destroy(&sc->sc_mode);
    436   1.1  plunky 
    437   1.1  plunky 	sysctl_teardown(&sc->sc_log);
    438   1.1  plunky 
    439   1.1  plunky 	if (sc->sc_wsmouse != NULL) {
    440   1.1  plunky 		err = config_detach(sc->sc_wsmouse, flags);
    441   1.1  plunky 		sc->sc_wsmouse = NULL;
    442   1.1  plunky 	}
    443   1.1  plunky 
    444   1.1  plunky 	return err;
    445   1.1  plunky }
    446   1.1  plunky 
    447   1.1  plunky /*
    448   1.1  plunky  * listen for our device
    449   1.1  plunky  *
    450   1.1  plunky  * bt_lock is held
    451   1.1  plunky  */
    452   1.1  plunky static int
    453   1.1  plunky btmagic_listen(struct btmagic_softc *sc)
    454   1.1  plunky {
    455   1.1  plunky 	struct sockaddr_bt sa;
    456   1.1  plunky 	int err;
    457   1.1  plunky 
    458   1.1  plunky 	memset(&sa, 0, sizeof(sa));
    459   1.1  plunky 	sa.bt_len = sizeof(sa);
    460   1.1  plunky 	sa.bt_family = AF_BLUETOOTH;
    461   1.1  plunky 	bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
    462   1.1  plunky 
    463   1.1  plunky 	/*
    464   1.1  plunky 	 * Listen on control PSM
    465   1.1  plunky 	 */
    466   1.7   rmind 	err = l2cap_attach_pcb(&sc->sc_ctl_l, &btmagic_ctl_proto, sc);
    467   1.1  plunky 	if (err)
    468   1.1  plunky 		return err;
    469   1.1  plunky 
    470   1.1  plunky 	err = l2cap_setopt(sc->sc_ctl_l, &sc->sc_mode);
    471   1.1  plunky 	if (err)
    472   1.1  plunky 		return err;
    473   1.1  plunky 
    474   1.1  plunky 	sa.bt_psm = L2CAP_PSM_HID_CNTL;
    475   1.8     rtr 	err = l2cap_bind_pcb(sc->sc_ctl_l, &sa);
    476   1.1  plunky 	if (err)
    477   1.1  plunky 		return err;
    478   1.1  plunky 
    479   1.8     rtr 	err = l2cap_listen_pcb(sc->sc_ctl_l);
    480   1.1  plunky 	if (err)
    481   1.1  plunky 		return err;
    482   1.1  plunky 
    483   1.1  plunky 	/*
    484   1.1  plunky 	 * Listen on interrupt PSM
    485   1.1  plunky 	 */
    486   1.7   rmind 	err = l2cap_attach_pcb(&sc->sc_int_l, &btmagic_int_proto, sc);
    487   1.1  plunky 	if (err)
    488   1.1  plunky 		return err;
    489   1.1  plunky 
    490   1.1  plunky 	err = l2cap_setopt(sc->sc_int_l, &sc->sc_mode);
    491   1.1  plunky 	if (err)
    492   1.1  plunky 		return err;
    493   1.1  plunky 
    494   1.1  plunky 	sa.bt_psm = L2CAP_PSM_HID_INTR;
    495   1.8     rtr 	err = l2cap_bind_pcb(sc->sc_int_l, &sa);
    496   1.1  plunky 	if (err)
    497   1.1  plunky 		return err;
    498   1.1  plunky 
    499   1.8     rtr 	err = l2cap_listen_pcb(sc->sc_int_l);
    500   1.1  plunky 	if (err)
    501   1.1  plunky 		return err;
    502   1.1  plunky 
    503   1.1  plunky 	sc->sc_state = BTMAGIC_WAIT_CTL;
    504   1.1  plunky 	return 0;
    505   1.1  plunky }
    506   1.1  plunky 
    507   1.1  plunky /*
    508   1.1  plunky  * start connecting to our device
    509   1.1  plunky  *
    510   1.1  plunky  * bt_lock is held
    511   1.1  plunky  */
    512   1.1  plunky static int
    513   1.1  plunky btmagic_connect(struct btmagic_softc *sc)
    514   1.1  plunky {
    515   1.1  plunky 	struct sockaddr_bt sa;
    516   1.1  plunky 	int err;
    517   1.1  plunky 
    518   1.1  plunky 	memset(&sa, 0, sizeof(sa));
    519   1.1  plunky 	sa.bt_len = sizeof(sa);
    520   1.1  plunky 	sa.bt_family = AF_BLUETOOTH;
    521   1.1  plunky 
    522   1.7   rmind 	err = l2cap_attach_pcb(&sc->sc_ctl, &btmagic_ctl_proto, sc);
    523   1.1  plunky 	if (err) {
    524   1.1  plunky 		printf("%s: l2cap_attach failed (%d)\n",
    525   1.1  plunky 		    device_xname(sc->sc_dev), err);
    526   1.1  plunky 		return err;
    527   1.1  plunky 	}
    528   1.1  plunky 
    529   1.1  plunky 	err = l2cap_setopt(sc->sc_ctl, &sc->sc_mode);
    530   1.5  plunky 	if (err) {
    531   1.5  plunky 		printf("%s: l2cap_setopt failed (%d)\n",
    532   1.5  plunky 		    device_xname(sc->sc_dev), err);
    533   1.1  plunky 		return err;
    534   1.5  plunky 	}
    535   1.1  plunky 
    536   1.1  plunky 	bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
    537   1.8     rtr 	err = l2cap_bind_pcb(sc->sc_ctl, &sa);
    538   1.1  plunky 	if (err) {
    539   1.8     rtr 		printf("%s: l2cap_bind_pcb failed (%d)\n",
    540   1.1  plunky 		    device_xname(sc->sc_dev), err);
    541   1.1  plunky 		return err;
    542   1.1  plunky 	}
    543   1.1  plunky 
    544   1.1  plunky 	sa.bt_psm = L2CAP_PSM_HID_CNTL;
    545   1.1  plunky 	bdaddr_copy(&sa.bt_bdaddr, &sc->sc_raddr);
    546   1.9     rtr 	err = l2cap_connect_pcb(sc->sc_ctl, &sa);
    547   1.1  plunky 	if (err) {
    548   1.9     rtr 		printf("%s: l2cap_connect_pcb failed (%d)\n",
    549   1.1  plunky 		    device_xname(sc->sc_dev), err);
    550   1.1  plunky 		return err;
    551   1.1  plunky 	}
    552   1.1  plunky 
    553   1.1  plunky 	SET(sc->sc_flags, BTMAGIC_CONNECTING);
    554   1.1  plunky 	sc->sc_state = BTMAGIC_WAIT_CTL;
    555   1.1  plunky 	return 0;
    556   1.1  plunky }
    557   1.1  plunky 
    558   1.1  plunky /* validate soft_resolution */
    559   1.1  plunky static int
    560   1.1  plunky btmagic_sysctl_resolution(SYSCTLFN_ARGS)
    561   1.1  plunky {
    562   1.1  plunky 	struct sysctlnode node;
    563   1.1  plunky 	struct btmagic_softc *sc;
    564   1.1  plunky 	int t, error;
    565   1.1  plunky 
    566   1.1  plunky 	node = *rnode;
    567   1.1  plunky 	sc = node.sysctl_data;
    568   1.1  plunky 
    569   1.1  plunky 	t = sc->sc_resolution;
    570   1.1  plunky 	node.sysctl_data = &t;
    571   1.1  plunky 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    572   1.1  plunky 	if (error || newp == NULL)
    573   1.1  plunky 		return error;
    574   1.1  plunky 
    575   1.1  plunky 	if (t < 100 || t > 4000 || (t / sc->sc_scale) == 0)
    576   1.1  plunky 		return EINVAL;
    577   1.1  plunky 
    578   1.1  plunky 	sc->sc_resolution = t;
    579   1.1  plunky 	DPRINTF(sc, "sc_resolution = %u", t);
    580   1.1  plunky 	return 0;
    581   1.1  plunky }
    582   1.1  plunky 
    583   1.1  plunky /* validate scroll_downscale_factor */
    584   1.1  plunky static int
    585   1.1  plunky btmagic_sysctl_scale(SYSCTLFN_ARGS)
    586   1.1  plunky {
    587   1.1  plunky 	struct sysctlnode node;
    588   1.1  plunky 	struct btmagic_softc *sc;
    589   1.1  plunky 	int t, error;
    590   1.1  plunky 
    591   1.1  plunky 	node = *rnode;
    592   1.1  plunky 	sc = node.sysctl_data;
    593   1.1  plunky 
    594   1.1  plunky 	t = sc->sc_scale;
    595   1.1  plunky 	node.sysctl_data = &t;
    596   1.1  plunky 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    597   1.1  plunky 	if (error || newp == NULL)
    598   1.1  plunky 		return error;
    599   1.1  plunky 
    600   1.1  plunky 	if (t < 1 || t > 40 || (sc->sc_resolution / t) == 0)
    601   1.1  plunky 		return EINVAL;
    602   1.1  plunky 
    603   1.1  plunky 	sc->sc_scale = t;
    604   1.1  plunky 	DPRINTF(sc, "sc_scale = %u", t);
    605   1.1  plunky 	return 0;
    606   1.1  plunky }
    607   1.1  plunky 
    608   1.1  plunky /*****************************************************************************
    609   1.1  plunky  *
    610   1.1  plunky  *	wsmouse(4) accessops
    611   1.1  plunky  */
    612   1.1  plunky 
    613   1.1  plunky static int
    614   1.1  plunky btmagic_wsmouse_enable(void *self)
    615   1.1  plunky {
    616   1.1  plunky 	struct btmagic_softc *sc = device_private(self);
    617   1.1  plunky 
    618   1.1  plunky 	if (sc->sc_enabled)
    619   1.1  plunky 		return EBUSY;
    620   1.1  plunky 
    621   1.1  plunky 	sc->sc_enabled = 1;
    622   1.1  plunky 	DPRINTF(sc, "enable");
    623   1.1  plunky 	return 0;
    624   1.1  plunky }
    625   1.1  plunky 
    626   1.1  plunky static int
    627   1.1  plunky btmagic_wsmouse_ioctl(void *self, unsigned long cmd, void *data,
    628   1.1  plunky     int flag, struct lwp *l)
    629   1.1  plunky {
    630   1.1  plunky 	/* struct btmagic_softc *sc = device_private(self); */
    631   1.1  plunky 	int err;
    632   1.1  plunky 
    633   1.1  plunky 	switch (cmd) {
    634   1.1  plunky 	case WSMOUSEIO_GTYPE:
    635   1.1  plunky 		*(uint *)data = WSMOUSE_TYPE_BLUETOOTH;
    636   1.1  plunky 		err = 0;
    637   1.1  plunky 		break;
    638   1.1  plunky 
    639   1.1  plunky 	default:
    640   1.1  plunky 		err = EPASSTHROUGH;
    641   1.1  plunky 		break;
    642   1.1  plunky 	}
    643   1.1  plunky 
    644   1.1  plunky 	return err;
    645   1.1  plunky }
    646   1.1  plunky 
    647   1.1  plunky static void
    648   1.1  plunky btmagic_wsmouse_disable(void *self)
    649   1.1  plunky {
    650   1.1  plunky 	struct btmagic_softc *sc = device_private(self);
    651   1.1  plunky 
    652   1.1  plunky 	DPRINTF(sc, "disable");
    653   1.1  plunky 	sc->sc_enabled = 0;
    654   1.1  plunky }
    655   1.1  plunky 
    656   1.1  plunky 
    657   1.1  plunky /*****************************************************************************
    658   1.1  plunky  *
    659   1.1  plunky  *	setup routines
    660   1.1  plunky  */
    661   1.1  plunky 
    662   1.1  plunky static void
    663   1.1  plunky btmagic_timeout(void *arg)
    664   1.1  plunky {
    665   1.1  plunky 	struct btmagic_softc *sc = arg;
    666   1.1  plunky 
    667   1.1  plunky 	mutex_enter(bt_lock);
    668   1.1  plunky 	callout_ack(&sc->sc_timeout);
    669   1.1  plunky 
    670   1.1  plunky 	switch (sc->sc_state) {
    671   1.1  plunky 	case BTMAGIC_CLOSED:
    672   1.1  plunky 		if (sc->sc_int != NULL) {
    673  1.10     rtr 			l2cap_disconnect_pcb(sc->sc_int, 0);
    674   1.1  plunky 			break;
    675   1.1  plunky 		}
    676   1.1  plunky 
    677   1.1  plunky 		if (sc->sc_ctl != NULL) {
    678  1.10     rtr 			l2cap_disconnect_pcb(sc->sc_ctl, 0);
    679   1.1  plunky 			break;
    680   1.1  plunky 		}
    681   1.1  plunky 		break;
    682   1.1  plunky 
    683   1.1  plunky 	case BTMAGIC_OPEN:
    684   1.1  plunky 		if (!ISSET(sc->sc_flags, BTMAGIC_ENABLED)) {
    685   1.1  plunky 			btmagic_enable(sc);
    686   1.1  plunky 			break;
    687   1.1  plunky 		}
    688   1.1  plunky 
    689   1.1  plunky 		btmagic_check_battery(sc);
    690   1.1  plunky 		break;
    691   1.1  plunky 
    692   1.1  plunky 	case BTMAGIC_WAIT_CTL:
    693   1.1  plunky 	case BTMAGIC_WAIT_INT:
    694   1.1  plunky 	default:
    695   1.1  plunky 		break;
    696   1.1  plunky 	}
    697   1.1  plunky 	mutex_exit(bt_lock);
    698   1.1  plunky }
    699   1.1  plunky 
    700   1.1  plunky /*
    701   1.1  plunky  * Send report on control channel
    702   1.1  plunky  *
    703   1.1  plunky  * bt_lock is held
    704   1.1  plunky  */
    705   1.1  plunky static int
    706   1.1  plunky btmagic_ctl_send(struct btmagic_softc *sc, const uint8_t *data, size_t len)
    707   1.1  plunky {
    708   1.1  plunky 	struct mbuf *m;
    709   1.1  plunky 
    710   1.1  plunky 	if (len > MLEN)
    711   1.1  plunky 		return EINVAL;
    712   1.1  plunky 
    713   1.1  plunky 	m = m_gethdr(M_DONTWAIT, MT_DATA);
    714   1.1  plunky 	if (m == NULL)
    715   1.1  plunky 		return ENOMEM;
    716   1.1  plunky 
    717   1.1  plunky #ifdef BTMAGIC_DEBUG
    718   1.1  plunky 	printf("%s: send", device_xname(sc->sc_dev));
    719   1.1  plunky 	for (size_t i = 0; i < len; i++)
    720   1.1  plunky 		printf(" 0x%02x", data[i]);
    721   1.1  plunky 	printf("\n");
    722   1.1  plunky #endif
    723   1.1  plunky 
    724   1.1  plunky 	memcpy(mtod(m, uint8_t *), data, len);
    725   1.1  plunky 	m->m_pkthdr.len = m->m_len = len;
    726   1.1  plunky 	return l2cap_send(sc->sc_ctl, m);
    727   1.1  plunky }
    728   1.1  plunky 
    729   1.1  plunky /*
    730   1.1  plunky  * Enable touch reports by sending the following report
    731   1.1  plunky  *
    732   1.1  plunky  *	 SET_REPORT(FEATURE, 0xd7) = 0x01
    733   1.1  plunky  *
    734   1.1  plunky  * bt_lock is held
    735   1.1  plunky  */
    736   1.1  plunky static void
    737   1.1  plunky btmagic_enable(struct btmagic_softc *sc)
    738   1.1  plunky {
    739   1.1  plunky 	static const uint8_t rep[] = { 0x53, 0xd7, 0x01 };
    740   1.1  plunky 
    741   1.1  plunky 	if (btmagic_ctl_send(sc, rep, sizeof(rep)) != 0) {
    742   1.1  plunky 		printf("%s: cannot enable touch reports\n",
    743   1.1  plunky 		    device_xname(sc->sc_dev));
    744   1.1  plunky 
    745   1.1  plunky 		return;
    746   1.1  plunky 	}
    747   1.1  plunky 
    748   1.1  plunky 	SET(sc->sc_flags, BTMAGIC_ENABLED);
    749   1.1  plunky }
    750   1.1  plunky 
    751   1.1  plunky /*
    752   1.1  plunky  * Request the battery level by sending the following report
    753   1.1  plunky  *
    754   1.1  plunky  *	GET_REPORT(FEATURE, 0x47)
    755   1.1  plunky  *
    756   1.1  plunky  * bt_lock is held
    757   1.1  plunky  */
    758   1.1  plunky static void
    759   1.1  plunky btmagic_check_battery(struct btmagic_softc *sc)
    760   1.1  plunky {
    761   1.1  plunky 	static const uint8_t rep[] = { 0x43, 0x47 };
    762   1.1  plunky 
    763   1.1  plunky 	if (btmagic_ctl_send(sc, rep, sizeof(rep)) != 0)
    764   1.1  plunky 		printf("%s: cannot request battery level\n",
    765   1.1  plunky 		    device_xname(sc->sc_dev));
    766   1.1  plunky }
    767   1.1  plunky 
    768   1.1  plunky /*
    769   1.1  plunky  * the Magic Mouse has a base resolution of 1300dpi which is rather flighty. We
    770   1.1  plunky  * scale the output to the requested resolution, taking care to account for the
    771   1.1  plunky  * remainders to prevent loss of small deltas.
    772   1.1  plunky  */
    773   1.1  plunky static int
    774   1.1  plunky btmagic_scale(int delta, int *remainder, int resolution)
    775   1.1  plunky {
    776   1.1  plunky 	int new;
    777   1.1  plunky 
    778   1.1  plunky 	delta += *remainder;
    779   1.1  plunky 	new = delta * resolution / 1300;
    780   1.1  plunky 	*remainder = delta - new * 1300 / resolution;
    781   1.1  plunky 	return new;
    782   1.1  plunky }
    783   1.1  plunky 
    784   1.1  plunky 
    785   1.1  plunky /*****************************************************************************
    786   1.1  plunky  *
    787   1.1  plunky  *	bluetooth(9) callback methods for L2CAP
    788   1.1  plunky  *
    789   1.1  plunky  *	All these are called from Bluetooth Protocol code, holding bt_lock.
    790   1.1  plunky  */
    791   1.1  plunky 
    792   1.1  plunky static void
    793   1.1  plunky btmagic_connecting(void *arg)
    794   1.1  plunky {
    795   1.1  plunky 
    796   1.1  plunky 	/* dont care */
    797   1.1  plunky }
    798   1.1  plunky 
    799   1.1  plunky static void
    800   1.1  plunky btmagic_ctl_connected(void *arg)
    801   1.1  plunky {
    802   1.1  plunky 	struct sockaddr_bt sa;
    803   1.1  plunky 	struct btmagic_softc *sc = arg;
    804   1.1  plunky 	int err;
    805   1.1  plunky 
    806   1.1  plunky 	if (sc->sc_state != BTMAGIC_WAIT_CTL)
    807   1.1  plunky 		return;
    808   1.1  plunky 
    809   1.1  plunky 	KASSERT(sc->sc_ctl != NULL);
    810   1.1  plunky 	KASSERT(sc->sc_int == NULL);
    811   1.1  plunky 
    812   1.1  plunky 	if (ISSET(sc->sc_flags, BTMAGIC_CONNECTING)) {
    813   1.1  plunky 		/* initiate connect on interrupt PSM */
    814   1.7   rmind 		err = l2cap_attach_pcb(&sc->sc_int, &btmagic_int_proto, sc);
    815   1.1  plunky 		if (err)
    816   1.1  plunky 			goto fail;
    817   1.1  plunky 
    818   1.1  plunky 		err = l2cap_setopt(sc->sc_int, &sc->sc_mode);
    819   1.1  plunky 		if (err)
    820   1.1  plunky 			goto fail;
    821   1.1  plunky 
    822   1.1  plunky 		memset(&sa, 0, sizeof(sa));
    823   1.1  plunky 		sa.bt_len = sizeof(sa);
    824   1.1  plunky 		sa.bt_family = AF_BLUETOOTH;
    825   1.1  plunky 		bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
    826   1.1  plunky 
    827   1.8     rtr 		err = l2cap_bind_pcb(sc->sc_int, &sa);
    828   1.1  plunky 		if (err)
    829   1.1  plunky 			goto fail;
    830   1.1  plunky 
    831   1.1  plunky 		sa.bt_psm = L2CAP_PSM_HID_INTR;
    832   1.1  plunky 		bdaddr_copy(&sa.bt_bdaddr, &sc->sc_raddr);
    833   1.9     rtr 		err = l2cap_connect_pcb(sc->sc_int, &sa);
    834   1.1  plunky 		if (err)
    835   1.1  plunky 			goto fail;
    836   1.1  plunky 	}
    837   1.1  plunky 
    838   1.1  plunky 	sc->sc_state = BTMAGIC_WAIT_INT;
    839   1.1  plunky 	return;
    840   1.1  plunky 
    841   1.1  plunky fail:
    842   1.7   rmind 	l2cap_detach_pcb(&sc->sc_ctl);
    843   1.1  plunky 	sc->sc_ctl = NULL;
    844   1.1  plunky 
    845   1.1  plunky 	printf("%s: connect failed (%d)\n", device_xname(sc->sc_dev), err);
    846   1.1  plunky }
    847   1.1  plunky 
    848   1.1  plunky static void
    849   1.1  plunky btmagic_int_connected(void *arg)
    850   1.1  plunky {
    851   1.1  plunky 	struct btmagic_softc *sc = arg;
    852   1.1  plunky 
    853   1.1  plunky 	if (sc->sc_state != BTMAGIC_WAIT_INT)
    854   1.1  plunky 		return;
    855   1.1  plunky 
    856   1.1  plunky 	KASSERT(sc->sc_ctl != NULL);
    857   1.1  plunky 	KASSERT(sc->sc_int != NULL);
    858   1.1  plunky 
    859   1.1  plunky 	printf("%s: connected\n", device_xname(sc->sc_dev));
    860   1.1  plunky 	CLR(sc->sc_flags, BTMAGIC_CONNECTING);
    861   1.1  plunky 	sc->sc_state = BTMAGIC_OPEN;
    862   1.1  plunky 
    863   1.1  plunky 	/* trigger the setup */
    864   1.1  plunky 	CLR(sc->sc_flags, BTMAGIC_ENABLED);
    865   1.1  plunky 	callout_schedule(&sc->sc_timeout, hz);
    866   1.1  plunky }
    867   1.1  plunky 
    868   1.1  plunky /*
    869   1.1  plunky  * Disconnected
    870   1.1  plunky  *
    871   1.1  plunky  * Depending on our state, this could mean several things, but essentially
    872   1.1  plunky  * we are lost. If both channels are closed, schedule another connection.
    873   1.1  plunky  */
    874   1.1  plunky static void
    875   1.1  plunky btmagic_ctl_disconnected(void *arg, int err)
    876   1.1  plunky {
    877   1.1  plunky 	struct btmagic_softc *sc = arg;
    878   1.1  plunky 
    879   1.1  plunky 	if (sc->sc_ctl != NULL) {
    880   1.7   rmind 		l2cap_detach_pcb(&sc->sc_ctl);
    881   1.1  plunky 		sc->sc_ctl = NULL;
    882   1.1  plunky 	}
    883   1.1  plunky 
    884   1.1  plunky 	if (sc->sc_int == NULL) {
    885   1.5  plunky 		printf("%s: disconnected (%d)\n", device_xname(sc->sc_dev), err);
    886   1.1  plunky 		CLR(sc->sc_flags, BTMAGIC_CONNECTING);
    887   1.1  plunky 		sc->sc_state = BTMAGIC_WAIT_CTL;
    888   1.1  plunky 	} else {
    889   1.1  plunky 		/*
    890   1.1  plunky 		 * The interrupt channel should have been closed first,
    891   1.1  plunky 		 * but its potentially unsafe to detach that from here.
    892   1.1  plunky 		 * Give them a second to do the right thing or let the
    893   1.1  plunky 		 * callout handle it.
    894   1.1  plunky 		 */
    895   1.1  plunky 		sc->sc_state = BTMAGIC_CLOSED;
    896   1.1  plunky 		callout_schedule(&sc->sc_timeout, hz);
    897   1.1  plunky 	}
    898   1.1  plunky }
    899   1.1  plunky 
    900   1.1  plunky static void
    901   1.1  plunky btmagic_int_disconnected(void *arg, int err)
    902   1.1  plunky {
    903   1.1  plunky 	struct btmagic_softc *sc = arg;
    904   1.1  plunky 
    905   1.1  plunky 	if (sc->sc_int != NULL) {
    906   1.7   rmind 		l2cap_detach_pcb(&sc->sc_int);
    907   1.1  plunky 		sc->sc_int = NULL;
    908   1.1  plunky 	}
    909   1.1  plunky 
    910   1.1  plunky 	if (sc->sc_ctl == NULL) {
    911   1.5  plunky 		printf("%s: disconnected (%d)\n", device_xname(sc->sc_dev), err);
    912   1.1  plunky 		CLR(sc->sc_flags, BTMAGIC_CONNECTING);
    913   1.1  plunky 		sc->sc_state = BTMAGIC_WAIT_CTL;
    914   1.1  plunky 	} else {
    915   1.1  plunky 		/*
    916   1.1  plunky 		 * The control channel should be closing also, allow
    917   1.1  plunky 		 * them a chance to do that before we force it.
    918   1.1  plunky 		 */
    919   1.1  plunky 		sc->sc_state = BTMAGIC_CLOSED;
    920   1.1  plunky 		callout_schedule(&sc->sc_timeout, hz);
    921   1.1  plunky 	}
    922   1.1  plunky }
    923   1.1  plunky 
    924   1.1  plunky /*
    925   1.1  plunky  * New Connections
    926   1.1  plunky  *
    927   1.1  plunky  * We give a new L2CAP handle back if this matches the BDADDR we are
    928   1.1  plunky  * listening for and we are in the right state. btmagic_connected will
    929   1.1  plunky  * be called when the connection is open, so nothing else to do here
    930   1.1  plunky  */
    931   1.1  plunky static void *
    932   1.1  plunky btmagic_ctl_newconn(void *arg, struct sockaddr_bt *laddr,
    933   1.1  plunky     struct sockaddr_bt *raddr)
    934   1.1  plunky {
    935   1.1  plunky 	struct btmagic_softc *sc = arg;
    936   1.1  plunky 
    937   1.1  plunky 	if (bdaddr_same(&raddr->bt_bdaddr, &sc->sc_raddr) == 0)
    938   1.1  plunky 		return NULL;
    939   1.1  plunky 
    940   1.1  plunky 	if (sc->sc_state != BTMAGIC_WAIT_CTL
    941   1.1  plunky 	    || ISSET(sc->sc_flags, BTMAGIC_CONNECTING)
    942   1.1  plunky 	    || sc->sc_ctl != NULL
    943   1.1  plunky 	    || sc->sc_int != NULL) {
    944   1.1  plunky 		DPRINTF(sc, "reject ctl newconn %s%s%s%s",
    945   1.1  plunky 		    (sc->sc_state == BTMAGIC_WAIT_CTL) ? " (WAITING)": "",
    946   1.1  plunky 		    ISSET(sc->sc_flags, BTMAGIC_CONNECTING) ? " (CONNECTING)" : "",
    947   1.1  plunky 		    (sc->sc_ctl != NULL) ? " (GOT CONTROL)" : "",
    948   1.1  plunky 		    (sc->sc_int != NULL) ? " (GOT INTERRUPT)" : "");
    949   1.1  plunky 
    950   1.1  plunky 		return NULL;
    951   1.1  plunky 	}
    952   1.1  plunky 
    953   1.7   rmind 	l2cap_attach_pcb(&sc->sc_ctl, &btmagic_ctl_proto, sc);
    954   1.1  plunky 	return sc->sc_ctl;
    955   1.1  plunky }
    956   1.1  plunky 
    957   1.1  plunky static void *
    958   1.1  plunky btmagic_int_newconn(void *arg, struct sockaddr_bt *laddr,
    959   1.1  plunky     struct sockaddr_bt *raddr)
    960   1.1  plunky {
    961   1.1  plunky 	struct btmagic_softc *sc = arg;
    962   1.1  plunky 
    963   1.1  plunky 	if (bdaddr_same(&raddr->bt_bdaddr, &sc->sc_raddr) == 0)
    964   1.1  plunky 		return NULL;
    965   1.1  plunky 
    966   1.1  plunky 	if (sc->sc_state != BTMAGIC_WAIT_INT
    967   1.1  plunky 	    || ISSET(sc->sc_flags, BTMAGIC_CONNECTING)
    968   1.1  plunky 	    || sc->sc_ctl == NULL
    969   1.1  plunky 	    || sc->sc_int != NULL) {
    970   1.1  plunky 		DPRINTF(sc, "reject int newconn %s%s%s%s",
    971   1.1  plunky 		    (sc->sc_state == BTMAGIC_WAIT_INT) ? " (WAITING)": "",
    972   1.1  plunky 		    ISSET(sc->sc_flags, BTMAGIC_CONNECTING) ? " (CONNECTING)" : "",
    973   1.1  plunky 		    (sc->sc_ctl == NULL) ? " (NO CONTROL)" : "",
    974   1.1  plunky 		    (sc->sc_int != NULL) ? " (GOT INTERRUPT)" : "");
    975   1.1  plunky 
    976   1.1  plunky 		return NULL;
    977   1.1  plunky 	}
    978   1.1  plunky 
    979   1.7   rmind 	l2cap_attach_pcb(&sc->sc_int, &btmagic_int_proto, sc);
    980   1.1  plunky 	return sc->sc_int;
    981   1.1  plunky }
    982   1.1  plunky 
    983   1.1  plunky static void
    984   1.1  plunky btmagic_complete(void *arg, int count)
    985   1.1  plunky {
    986   1.1  plunky 
    987   1.1  plunky 	/* dont care */
    988   1.1  plunky }
    989   1.1  plunky 
    990   1.1  plunky static void
    991   1.1  plunky btmagic_linkmode(void *arg, int new)
    992   1.1  plunky {
    993   1.1  plunky 	struct btmagic_softc *sc = arg;
    994   1.1  plunky 	int mode;
    995   1.1  plunky 
    996   1.1  plunky 	(void)sockopt_getint(&sc->sc_mode, &mode);
    997   1.1  plunky 
    998   1.1  plunky 	if (ISSET(mode, L2CAP_LM_AUTH) && !ISSET(new, L2CAP_LM_AUTH))
    999   1.1  plunky 		printf("%s: auth failed\n", device_xname(sc->sc_dev));
   1000   1.1  plunky 	else if (ISSET(mode, L2CAP_LM_ENCRYPT) && !ISSET(new, L2CAP_LM_ENCRYPT))
   1001   1.1  plunky 		printf("%s: encrypt off\n", device_xname(sc->sc_dev));
   1002   1.1  plunky 	else if (ISSET(mode, L2CAP_LM_SECURE) && !ISSET(new, L2CAP_LM_SECURE))
   1003   1.1  plunky 		printf("%s: insecure\n", device_xname(sc->sc_dev));
   1004   1.1  plunky 	else
   1005   1.1  plunky 		return;
   1006   1.1  plunky 
   1007   1.1  plunky 	if (sc->sc_int != NULL)
   1008  1.10     rtr 		l2cap_disconnect_pcb(sc->sc_int, 0);
   1009   1.1  plunky 
   1010   1.1  plunky 	if (sc->sc_ctl != NULL)
   1011  1.10     rtr 		l2cap_disconnect_pcb(sc->sc_ctl, 0);
   1012   1.1  plunky }
   1013   1.1  plunky 
   1014   1.1  plunky /*
   1015   1.1  plunky  * Receive transaction from the mouse. We don't differentiate between
   1016   1.1  plunky  * interrupt and control channel here, there is no need.
   1017   1.1  plunky  */
   1018   1.1  plunky static void
   1019   1.1  plunky btmagic_input(void *arg, struct mbuf *m)
   1020   1.1  plunky {
   1021   1.1  plunky 	struct btmagic_softc *sc = arg;
   1022   1.1  plunky 	uint8_t *data;
   1023   1.1  plunky 	size_t len;
   1024   1.1  plunky 
   1025   1.1  plunky 	if (sc->sc_state != BTMAGIC_OPEN
   1026   1.1  plunky 	    || sc->sc_wsmouse == NULL
   1027   1.1  plunky 	    || sc->sc_enabled == 0)
   1028   1.1  plunky 		goto release;
   1029   1.1  plunky 
   1030   1.1  plunky 	if (m->m_pkthdr.len > m->m_len)
   1031   1.1  plunky 		printf("%s: truncating input\n", device_xname(sc->sc_dev));
   1032   1.1  plunky 
   1033   1.1  plunky 	data = mtod(m, uint8_t *);
   1034   1.1  plunky 	len = m->m_len;
   1035   1.1  plunky 
   1036   1.1  plunky 	if (len < 1)
   1037   1.1  plunky 		goto release;
   1038   1.1  plunky 
   1039   1.1  plunky 	switch (BTHID_TYPE(data[0])) {
   1040   1.1  plunky 	case BTHID_HANDSHAKE:
   1041   1.1  plunky 		DPRINTF(sc, "Handshake: 0x%x", BTHID_HANDSHAKE_PARAM(data[0]));
   1042   1.1  plunky 		callout_schedule(&sc->sc_timeout, hz);
   1043   1.1  plunky 		break;
   1044   1.1  plunky 
   1045   1.1  plunky 	case BTHID_DATA:
   1046   1.1  plunky 		if (len < 2)
   1047   1.1  plunky 			break;
   1048   1.1  plunky 
   1049   1.1  plunky 		switch (data[1]) {
   1050   1.1  plunky 		case 0x10: /* Basic mouse (input) */
   1051   1.1  plunky 			btmagic_input_basic(sc, data + 2, len - 2);
   1052   1.1  plunky 			break;
   1053   1.1  plunky 
   1054   1.1  plunky 		case 0x29: /* Magic touch (input) */
   1055   1.1  plunky 			btmagic_input_magic(sc, data + 2, len - 2);
   1056   1.1  plunky 			break;
   1057   1.1  plunky 
   1058   1.1  plunky 		case 0x30: /* Battery status (input) */
   1059   1.1  plunky 			if (len != 3)
   1060   1.1  plunky 				break;
   1061   1.1  plunky 
   1062   1.1  plunky 			printf("%s: Battery ", device_xname(sc->sc_dev));
   1063   1.1  plunky 			switch (data[2]) {
   1064   1.1  plunky 			case 0:	printf("Ok\n");			break;
   1065   1.1  plunky 			case 1:	printf("Warning\n");		break;
   1066   1.1  plunky 			case 2:	printf("Critical\n");		break;
   1067   1.1  plunky 			default: printf("0x%02x\n", data[2]);	break;
   1068   1.1  plunky 			}
   1069   1.1  plunky 			break;
   1070   1.1  plunky 
   1071   1.1  plunky 		case 0x47: /* Battery strength (feature) */
   1072   1.1  plunky 			if (len != 3)
   1073   1.1  plunky 				break;
   1074   1.1  plunky 
   1075   1.1  plunky 			printf("%s: Battery %d%%\n", device_xname(sc->sc_dev),
   1076   1.1  plunky 			    data[2]);
   1077   1.1  plunky 			break;
   1078   1.1  plunky 
   1079   1.1  plunky 		case 0x61: /* Surface detection (input) */
   1080   1.1  plunky 			if (len != 3)
   1081   1.1  plunky 				break;
   1082   1.1  plunky 
   1083   1.1  plunky 			DPRINTF(sc, "Mouse %s",
   1084   1.1  plunky 			    (data[2] == 0 ? "lowered" : "raised"));
   1085   1.1  plunky 			break;
   1086   1.1  plunky 
   1087   1.1  plunky 		case 0x60: /* unknown (input) */
   1088   1.1  plunky 		case 0xf0: /* unknown (feature) */
   1089   1.1  plunky 		case 0xf1: /* unknown (feature) */
   1090   1.1  plunky 		default:
   1091   1.1  plunky #if BTMAGIC_DEBUG
   1092   1.1  plunky 			printf("%s: recv", device_xname(sc->sc_dev));
   1093   1.1  plunky 			for (size_t i = 0; i < len; i++)
   1094   1.1  plunky 				printf(" 0x%02x", data[i]);
   1095   1.1  plunky 			printf("\n");
   1096   1.1  plunky #endif
   1097   1.1  plunky 			break;
   1098   1.1  plunky 		}
   1099   1.1  plunky 		break;
   1100   1.1  plunky 
   1101   1.1  plunky 	default:
   1102   1.1  plunky 		DPRINTF(sc, "transaction (type 0x%x)", BTHID_TYPE(data[0]));
   1103   1.1  plunky 		break;
   1104   1.1  plunky 	}
   1105   1.1  plunky 
   1106   1.1  plunky release:
   1107   1.1  plunky 	m_freem(m);
   1108   1.1  plunky }
   1109   1.1  plunky 
   1110   1.1  plunky /*
   1111   1.1  plunky  * parse the Basic report (0x10), which according to the provided
   1112   1.1  plunky  * HID descriptor is in the following format
   1113   1.1  plunky  *
   1114   1.1  plunky  *	button 1	1-bit
   1115   1.1  plunky  *	button 2	1-bit
   1116   1.1  plunky  *	padding		6-bits
   1117   1.1  plunky  *	dX		16-bits (signed)
   1118   1.1  plunky  *	dY		16-bits (signed)
   1119   1.1  plunky  *
   1120   1.1  plunky  * Even when the magic touch reports are enabled, the basic report is
   1121   1.1  plunky  * sent for mouse move events where no touches are detected.
   1122   1.1  plunky  */
   1123   1.1  plunky static const struct {
   1124   1.1  plunky 	struct hid_location button1;
   1125   1.1  plunky 	struct hid_location button2;
   1126   1.1  plunky 	struct hid_location dX;
   1127   1.1  plunky 	struct hid_location dY;
   1128   1.1  plunky } basic = {
   1129   1.1  plunky 	.button1 = { .pos =  0, .size = 1 },
   1130   1.1  plunky 	.button2 = { .pos =  1, .size = 1 },
   1131   1.1  plunky 	.dX = { .pos =  8, .size = 16 },
   1132   1.1  plunky 	.dY = { .pos = 24, .size = 16 },
   1133   1.1  plunky };
   1134   1.1  plunky 
   1135   1.1  plunky static void
   1136   1.1  plunky btmagic_input_basic(struct btmagic_softc *sc, uint8_t *data, size_t len)
   1137   1.1  plunky {
   1138   1.1  plunky 	int dx, dy;
   1139   1.1  plunky 	uint32_t mb;
   1140   1.1  plunky 	int s;
   1141   1.1  plunky 
   1142   1.1  plunky 	if (len != 5)
   1143   1.1  plunky 		return;
   1144   1.1  plunky 
   1145   1.1  plunky 	dx = hid_get_data(data, &basic.dX);
   1146   1.1  plunky 	dx = btmagic_scale(dx, &sc->sc_rx, sc->sc_resolution);
   1147   1.1  plunky 
   1148   1.1  plunky 	dy = hid_get_data(data, &basic.dY);
   1149   1.1  plunky 	dy = btmagic_scale(dy, &sc->sc_ry, sc->sc_resolution);
   1150   1.1  plunky 
   1151   1.1  plunky 	mb = 0;
   1152   1.1  plunky 	if (hid_get_udata(data, &basic.button1))
   1153   1.1  plunky 		mb |= __BIT(0);
   1154   1.1  plunky 	if (hid_get_udata(data, &basic.button2))
   1155   1.1  plunky 		mb |= __BIT(2);
   1156   1.1  plunky 
   1157   1.1  plunky 	if (dx != 0 || dy != 0 || mb != sc->sc_mb) {
   1158   1.1  plunky 		sc->sc_mb = mb;
   1159   1.1  plunky 
   1160   1.1  plunky 		s = spltty();
   1161   1.1  plunky 		wsmouse_input(sc->sc_wsmouse, mb,
   1162   1.1  plunky 		    dx, -dy, 0, 0, WSMOUSE_INPUT_DELTA);
   1163   1.1  plunky 		splx(s);
   1164   1.1  plunky 	}
   1165   1.1  plunky }
   1166   1.1  plunky 
   1167   1.1  plunky /*
   1168   1.1  plunky  * the Magic touch report (0x29), according to the Linux driver
   1169   1.1  plunky  * written by Michael Poole, is variable length starting with the
   1170   1.1  plunky  * fixed 40-bit header
   1171   1.1  plunky  *
   1172   1.1  plunky  *	dX lsb		8-bits (signed)
   1173   1.1  plunky  *	dY lsb		8-bits (signed)
   1174   1.1  plunky  *	button 1	1-bit
   1175   1.1  plunky  *	button 2	1-bit
   1176   1.1  plunky  *	dX msb		2-bits (signed)
   1177   1.1  plunky  *	dY msb		2-bits (signed)
   1178   1.1  plunky  *	timestamp	18-bits
   1179   1.1  plunky  *
   1180   1.1  plunky  * followed by (up to 5?) touch reports of 64-bits each
   1181   1.1  plunky  *
   1182   1.1  plunky  *	abs W		12-bits (signed)
   1183   1.1  plunky  *	abs Z		12-bits (signed)
   1184   1.1  plunky  *	axis major	8-bits
   1185   1.1  plunky  *	axis minor	8-bits
   1186   1.1  plunky  *	pressure	6-bits
   1187   1.1  plunky  *	id		4-bits
   1188   1.1  plunky  *	angle		6-bits	(from E(0)->N(32)->W(64))
   1189   1.1  plunky  *	unknown		4-bits
   1190   1.1  plunky  *	phase		4-bits
   1191   1.1  plunky  */
   1192   1.1  plunky 
   1193   1.1  plunky static const struct {
   1194   1.1  plunky 	struct hid_location dXl;
   1195   1.1  plunky 	struct hid_location dYl;
   1196   1.1  plunky 	struct hid_location button1;
   1197   1.1  plunky 	struct hid_location button2;
   1198   1.1  plunky 	struct hid_location dXm;
   1199   1.1  plunky 	struct hid_location dYm;
   1200   1.1  plunky 	struct hid_location timestamp;
   1201   1.1  plunky } magic = {
   1202   1.1  plunky 	.dXl = { .pos = 0, .size = 8 },
   1203   1.1  plunky 	.dYl = { .pos = 8, .size = 8 },
   1204   1.1  plunky 	.button1 = { .pos = 16, .size = 1 },
   1205   1.1  plunky 	.button2 = { .pos = 17, .size = 1 },
   1206   1.1  plunky 	.dXm = { .pos = 18, .size = 2 },
   1207   1.1  plunky 	.dYm = { .pos = 20, .size = 2 },
   1208   1.1  plunky 	.timestamp = { .pos = 22, .size = 18 },
   1209   1.1  plunky };
   1210   1.1  plunky 
   1211   1.1  plunky static const struct {
   1212   1.1  plunky 	struct hid_location aW;
   1213   1.1  plunky 	struct hid_location aZ;
   1214   1.1  plunky 	struct hid_location major;
   1215   1.1  plunky 	struct hid_location minor;
   1216   1.1  plunky 	struct hid_location pressure;
   1217   1.1  plunky 	struct hid_location id;
   1218   1.1  plunky 	struct hid_location angle;
   1219   1.1  plunky 	struct hid_location unknown;
   1220   1.1  plunky 	struct hid_location phase;
   1221   1.1  plunky } touch = {
   1222   1.1  plunky 	.aW = { .pos = 0, .size = 12 },
   1223   1.1  plunky 	.aZ = { .pos = 12, .size = 12 },
   1224   1.1  plunky 	.major = { .pos = 24, .size = 8 },
   1225   1.1  plunky 	.minor = { .pos = 32, .size = 8 },
   1226   1.1  plunky 	.pressure = { .pos = 40, .size = 6 },
   1227   1.1  plunky 	.id = { .pos = 46, .size = 4 },
   1228   1.1  plunky 	.angle = { .pos = 50, .size = 6 },
   1229   1.1  plunky 	.unknown = { .pos = 56, .size = 4 },
   1230   1.1  plunky 	.phase = { .pos = 60, .size = 4 },
   1231   1.1  plunky };
   1232   1.1  plunky 
   1233   1.1  plunky /*
   1234   1.1  plunky  * the phase of the touch starts at 0x01 as the finger is first detected
   1235   1.1  plunky  * approaching the mouse, increasing to 0x04 while the finger is touching,
   1236   1.1  plunky  * then increases towards 0x07 as the finger is lifted, and we get 0x00
   1237   1.1  plunky  * when the touch is cancelled. The values below seem to be produced for
   1238   1.1  plunky  * every touch, the others less consistently depending on how fast the
   1239   1.1  plunky  * approach or departure is.
   1240   1.1  plunky  *
   1241   1.1  plunky  * In fact we ignore touches unless they are in the steady 0x04 phase.
   1242   1.1  plunky  */
   1243   1.1  plunky #define BTMAGIC_PHASE_START	0x3
   1244   1.1  plunky #define BTMAGIC_PHASE_CONT	0x4
   1245   1.1  plunky #define BTMAGIC_PHASE_END	0x7
   1246   1.1  plunky #define BTMAGIC_PHASE_CANCEL	0x0
   1247   1.1  plunky 
   1248   1.1  plunky static void
   1249   1.1  plunky btmagic_input_magic(struct btmagic_softc *sc, uint8_t *data, size_t len)
   1250   1.1  plunky {
   1251   1.1  plunky 	uint32_t mb;
   1252   1.1  plunky 	int dx, dy, dz, dw;
   1253   1.1  plunky 	int id, nf, az, aw, tz, tw;
   1254   1.1  plunky 	int s;
   1255   1.1  plunky 
   1256   1.1  plunky 	if (((len - 5) % 8) != 0)
   1257   1.1  plunky 		return;
   1258   1.1  plunky 
   1259   1.1  plunky 	dx = (hid_get_data(data, &magic.dXm) << 8)
   1260   1.1  plunky 	    | (hid_get_data(data, &magic.dXl) & 0xff);
   1261   1.1  plunky 	dx = btmagic_scale(dx, &sc->sc_rx, sc->sc_resolution);
   1262   1.1  plunky 
   1263   1.1  plunky 	dy = (hid_get_data(data, &magic.dYm) << 8)
   1264   1.1  plunky 	    | (hid_get_data(data, &magic.dYl) & 0xff);
   1265   1.1  plunky 	dy = btmagic_scale(dy, &sc->sc_ry, sc->sc_resolution);
   1266   1.1  plunky 
   1267   1.1  plunky 	mb = 0;
   1268   1.1  plunky 	if (hid_get_udata(data, &magic.button1))
   1269   1.1  plunky 		mb |= __BIT(0);
   1270   1.1  plunky 	if (hid_get_udata(data, &magic.button2))
   1271   1.1  plunky 		mb |= __BIT(2);
   1272   1.1  plunky 
   1273   1.1  plunky 	nf = 0;
   1274   1.1  plunky 	dz = 0;
   1275   1.1  plunky 	dw = 0;
   1276   1.1  plunky 	len = (len - 5) / 8;
   1277   1.1  plunky 	for (data += 5; len-- > 0; data += 8) {
   1278   1.1  plunky 		id = hid_get_udata(data, &touch.id);
   1279   1.1  plunky 		az = hid_get_data(data, &touch.aZ);
   1280   1.1  plunky 		aw = hid_get_data(data, &touch.aW);
   1281   1.1  plunky 
   1282   1.1  plunky 		/*
   1283   1.1  plunky 		 * scrolling is triggered by an established touch moving
   1284   1.1  plunky 		 * beyond a minimum distance from its start point and is
   1285   1.1  plunky 		 * cancelled as the touch starts to fade.
   1286   1.1  plunky 		 *
   1287   1.1  plunky 		 * Multiple touches may be scrolling simultaneously, the
   1288   1.1  plunky 		 * effect is cumulative.
   1289   1.1  plunky 		 */
   1290   1.1  plunky 
   1291   1.1  plunky 		switch (hid_get_udata(data, &touch.phase)) {
   1292   1.1  plunky 		case BTMAGIC_PHASE_CONT:
   1293   1.1  plunky 			tz = az - sc->sc_az[id];
   1294   1.1  plunky 			tw = aw - sc->sc_aw[id];
   1295   1.1  plunky 
   1296   1.1  plunky 			if (ISSET(sc->sc_smask, id)) {
   1297   1.1  plunky 				/* scrolling finger */
   1298   1.1  plunky 				dz += btmagic_scale(tz, &sc->sc_rz,
   1299   1.1  plunky 				    sc->sc_resolution / sc->sc_scale);
   1300   1.1  plunky 				dw += btmagic_scale(tw, &sc->sc_rw,
   1301   1.1  plunky 				    sc->sc_resolution / sc->sc_scale);
   1302   1.1  plunky 			} else if (abs(tz) > sc->sc_dist
   1303   1.1  plunky 			    || abs(tw) > sc->sc_dist) {
   1304   1.1  plunky 				/* new scrolling finger */
   1305   1.1  plunky 				if (sc->sc_smask == 0) {
   1306   1.1  plunky 					sc->sc_rz = 0;
   1307   1.1  plunky 					sc->sc_rw = 0;
   1308   1.1  plunky 				}
   1309   1.1  plunky 
   1310   1.1  plunky 				SET(sc->sc_smask, id);
   1311   1.1  plunky 			} else {
   1312   1.1  plunky 				/* not scrolling finger */
   1313   1.1  plunky 				az = sc->sc_az[id];
   1314   1.1  plunky 				aw = sc->sc_aw[id];
   1315   1.1  plunky 			}
   1316   1.1  plunky 
   1317   1.1  plunky 			/* count firm touches for middle-click */
   1318   1.1  plunky 			if (hid_get_udata(data, &touch.pressure) > sc->sc_firm)
   1319   1.1  plunky 				nf++;
   1320   1.1  plunky 
   1321   1.1  plunky 			break;
   1322   1.1  plunky 
   1323   1.1  plunky 		default:
   1324   1.1  plunky 			CLR(sc->sc_smask, id);
   1325   1.1  plunky 			break;
   1326   1.1  plunky 		}
   1327   1.1  plunky 
   1328   1.1  plunky 		sc->sc_az[id] = az;
   1329   1.1  plunky 		sc->sc_aw[id] = aw;
   1330   1.1  plunky 	}
   1331   1.1  plunky 
   1332   1.1  plunky 	/*
   1333   1.1  plunky 	 * The mouse only has one click detector, and says left or right but
   1334   1.1  plunky 	 * never both. We convert multiple firm touches while clicking into
   1335   1.1  plunky 	 * a middle button press, and cancel any scroll effects while click
   1336   1.1  plunky 	 * is active.
   1337   1.1  plunky 	 */
   1338   1.1  plunky 	if (mb != 0) {
   1339   1.1  plunky 		if (sc->sc_mb != 0)
   1340   1.1  plunky 			mb = sc->sc_mb;
   1341   1.1  plunky 		else if (nf > 1)
   1342   1.1  plunky 			mb = __BIT(1);
   1343   1.1  plunky 
   1344   1.1  plunky 		sc->sc_smask = 0;
   1345   1.1  plunky 		dz = 0;
   1346   1.1  plunky 		dw = 0;
   1347   1.1  plunky 	}
   1348   1.1  plunky 
   1349   1.1  plunky 	if (dx != 0 || dy != 0 || dz != 0 || dw != 0 || mb != sc->sc_mb) {
   1350   1.1  plunky 		sc->sc_mb = mb;
   1351   1.1  plunky 
   1352   1.1  plunky 		s = spltty();
   1353   1.1  plunky 		wsmouse_input(sc->sc_wsmouse, mb,
   1354   1.1  plunky 		    dx, -dy, -dz, dw, WSMOUSE_INPUT_DELTA);
   1355   1.1  plunky 		splx(s);
   1356   1.1  plunky 	}
   1357   1.1  plunky }
   1358