Home | History | Annotate | Line # | Download | only in bluetooth
btmagic.c revision 1.12
      1  1.12  bouyer /*	$NetBSD: btmagic.c,v 1.12 2015/04/06 17:45:31 bouyer 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.12  bouyer __KERNEL_RCSID(0, "$NetBSD: btmagic.c,v 1.12 2015/04/06 17:45:31 bouyer 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.12  bouyer 	uint32_t		sc_smask;	/* active(s) IDs */
    167  1.12  bouyer 	int			sc_nfingers;	/* number of active IDs */
    168  1.12  bouyer 	int			sc_ax[16];
    169  1.12  bouyer 	int			sc_ay[16];
    170   1.1  plunky 
    171   1.1  plunky 	/* previous mouse buttons */
    172  1.12  bouyer 	int			sc_mb_id; /* which ID selects the button */
    173   1.1  plunky 	uint32_t		sc_mb;
    174   1.1  plunky };
    175   1.1  plunky 
    176   1.1  plunky /* sc_flags */
    177   1.1  plunky #define BTMAGIC_CONNECTING	__BIT(0) /* we are connecting */
    178   1.1  plunky #define BTMAGIC_ENABLED		__BIT(1) /* touch reports enabled */
    179   1.1  plunky 
    180   1.1  plunky /* sc_state */
    181   1.1  plunky #define BTMAGIC_CLOSED		0
    182   1.1  plunky #define BTMAGIC_WAIT_CTL	1
    183   1.1  plunky #define BTMAGIC_WAIT_INT	2
    184   1.1  plunky #define BTMAGIC_OPEN		3
    185   1.1  plunky 
    186   1.1  plunky /* autoconf(9) glue */
    187   1.1  plunky static int  btmagic_match(device_t, cfdata_t, void *);
    188   1.1  plunky static void btmagic_attach(device_t, device_t, void *);
    189   1.1  plunky static int  btmagic_detach(device_t, int);
    190   1.1  plunky static int  btmagic_listen(struct btmagic_softc *);
    191   1.1  plunky static int  btmagic_connect(struct btmagic_softc *);
    192   1.1  plunky static int  btmagic_sysctl_resolution(SYSCTLFN_PROTO);
    193   1.1  plunky static int  btmagic_sysctl_scale(SYSCTLFN_PROTO);
    194   1.1  plunky 
    195   1.1  plunky CFATTACH_DECL_NEW(btmagic, sizeof(struct btmagic_softc),
    196   1.1  plunky     btmagic_match, btmagic_attach, btmagic_detach, NULL);
    197   1.1  plunky 
    198   1.1  plunky /* wsmouse(4) accessops */
    199   1.1  plunky static int btmagic_wsmouse_enable(void *);
    200   1.1  plunky static int btmagic_wsmouse_ioctl(void *, unsigned long, void *, int, struct lwp *);
    201   1.1  plunky static void btmagic_wsmouse_disable(void *);
    202   1.1  plunky 
    203   1.1  plunky static const struct wsmouse_accessops btmagic_wsmouse_accessops = {
    204   1.1  plunky 	btmagic_wsmouse_enable,
    205   1.1  plunky 	btmagic_wsmouse_ioctl,
    206   1.1  plunky 	btmagic_wsmouse_disable,
    207   1.1  plunky };
    208   1.1  plunky 
    209   1.1  plunky /* bluetooth(9) protocol methods for L2CAP */
    210   1.1  plunky static void  btmagic_connecting(void *);
    211   1.1  plunky static void  btmagic_ctl_connected(void *);
    212   1.1  plunky static void  btmagic_int_connected(void *);
    213   1.1  plunky static void  btmagic_ctl_disconnected(void *, int);
    214   1.1  plunky static void  btmagic_int_disconnected(void *, int);
    215   1.1  plunky static void *btmagic_ctl_newconn(void *, struct sockaddr_bt *, struct sockaddr_bt *);
    216   1.1  plunky static void *btmagic_int_newconn(void *, struct sockaddr_bt *, struct sockaddr_bt *);
    217   1.1  plunky static void  btmagic_complete(void *, int);
    218   1.1  plunky static void  btmagic_linkmode(void *, int);
    219   1.1  plunky static void  btmagic_input(void *, struct mbuf *);
    220   1.1  plunky static void  btmagic_input_basic(struct btmagic_softc *, uint8_t *, size_t);
    221  1.12  bouyer static void  btmagic_input_magicm(struct btmagic_softc *, uint8_t *, size_t);
    222  1.12  bouyer static void  btmagic_input_magict(struct btmagic_softc *, uint8_t *, size_t);
    223  1.12  bouyer 
    224  1.12  bouyer /* report types (data[1]) */
    225  1.12  bouyer #define BASIC_REPORT_ID		0x10
    226  1.12  bouyer #define TRACKPAD_REPORT_ID	0x28
    227  1.12  bouyer #define MOUSE_REPORT_ID		0x29
    228  1.12  bouyer #define BATT_STAT_REPORT_ID	0x30
    229  1.12  bouyer #define BATT_STRENGHT_REPORT_ID	0x47
    230  1.12  bouyer #define SURFACE_REPORT_ID	0x61
    231   1.1  plunky 
    232   1.1  plunky static const struct btproto btmagic_ctl_proto = {
    233   1.1  plunky 	btmagic_connecting,
    234   1.1  plunky 	btmagic_ctl_connected,
    235   1.1  plunky 	btmagic_ctl_disconnected,
    236   1.1  plunky 	btmagic_ctl_newconn,
    237   1.1  plunky 	btmagic_complete,
    238   1.1  plunky 	btmagic_linkmode,
    239   1.1  plunky 	btmagic_input,
    240   1.1  plunky };
    241   1.1  plunky 
    242   1.1  plunky static const struct btproto btmagic_int_proto = {
    243   1.1  plunky 	btmagic_connecting,
    244   1.1  plunky 	btmagic_int_connected,
    245   1.1  plunky 	btmagic_int_disconnected,
    246   1.1  plunky 	btmagic_int_newconn,
    247   1.1  plunky 	btmagic_complete,
    248   1.1  plunky 	btmagic_linkmode,
    249   1.1  plunky 	btmagic_input,
    250   1.1  plunky };
    251   1.1  plunky 
    252   1.1  plunky /* btmagic internals */
    253   1.1  plunky static void btmagic_timeout(void *);
    254   1.1  plunky static int  btmagic_ctl_send(struct btmagic_softc *, const uint8_t *, size_t);
    255   1.1  plunky static void btmagic_enable(struct btmagic_softc *);
    256   1.1  plunky static void btmagic_check_battery(struct btmagic_softc *);
    257   1.1  plunky static int  btmagic_scale(int, int *, int);
    258   1.1  plunky 
    259   1.1  plunky 
    260   1.1  plunky /*****************************************************************************
    261   1.1  plunky  *
    262   1.1  plunky  *	Magic Mouse autoconf(9) routines
    263   1.1  plunky  */
    264   1.1  plunky 
    265   1.1  plunky static int
    266   1.1  plunky btmagic_match(device_t self, cfdata_t cfdata, void *aux)
    267   1.1  plunky {
    268   1.1  plunky 	uint16_t v, p;
    269   1.1  plunky 
    270   1.1  plunky 	if (prop_dictionary_get_uint16(aux, BTDEVvendor, &v)
    271   1.1  plunky 	    && prop_dictionary_get_uint16(aux, BTDEVproduct, &p)
    272   1.1  plunky 	    && v == USB_VENDOR_APPLE
    273  1.12  bouyer 	    && (p == USB_PRODUCT_APPLE_MAGICMOUSE ||
    274  1.12  bouyer 		p == USB_PRODUCT_APPLE_MAGICTRACKPAD))
    275   1.1  plunky 		return 2;	/* trump bthidev(4) */
    276   1.1  plunky 
    277   1.1  plunky 	return 0;
    278   1.1  plunky }
    279   1.1  plunky 
    280   1.1  plunky static void
    281   1.1  plunky btmagic_attach(device_t parent, device_t self, void *aux)
    282   1.1  plunky {
    283   1.1  plunky 	struct btmagic_softc *sc = device_private(self);
    284   1.1  plunky 	struct wsmousedev_attach_args wsma;
    285   1.1  plunky 	const struct sysctlnode *node;
    286   1.1  plunky 	prop_object_t obj;
    287   1.5  plunky 	int err;
    288   1.1  plunky 
    289   1.1  plunky 	/*
    290   1.1  plunky 	 * Init softc
    291   1.1  plunky 	 */
    292   1.1  plunky 	sc->sc_dev = self;
    293   1.1  plunky 	sc->sc_state = BTMAGIC_CLOSED;
    294   1.1  plunky 	callout_init(&sc->sc_timeout, 0);
    295   1.1  plunky 	callout_setfunc(&sc->sc_timeout, btmagic_timeout, sc);
    296   1.1  plunky 	sockopt_init(&sc->sc_mode, BTPROTO_L2CAP, SO_L2CAP_LM, 0);
    297   1.1  plunky 
    298   1.1  plunky 	/*
    299   1.1  plunky 	 * extract config from proplist
    300   1.1  plunky 	 */
    301   1.1  plunky 	obj = prop_dictionary_get(aux, BTDEVladdr);
    302   1.1  plunky 	bdaddr_copy(&sc->sc_laddr, prop_data_data_nocopy(obj));
    303   1.1  plunky 
    304   1.1  plunky 	obj = prop_dictionary_get(aux, BTDEVraddr);
    305   1.1  plunky 	bdaddr_copy(&sc->sc_raddr, prop_data_data_nocopy(obj));
    306   1.1  plunky 
    307   1.1  plunky 	obj = prop_dictionary_get(aux, BTDEVmode);
    308   1.1  plunky 	if (prop_object_type(obj) == PROP_TYPE_STRING) {
    309   1.1  plunky 		if (prop_string_equals_cstring(obj, BTDEVauth))
    310   1.1  plunky 			sockopt_setint(&sc->sc_mode, L2CAP_LM_AUTH);
    311   1.1  plunky 		else if (prop_string_equals_cstring(obj, BTDEVencrypt))
    312   1.1  plunky 			sockopt_setint(&sc->sc_mode, L2CAP_LM_ENCRYPT);
    313   1.1  plunky 		else if (prop_string_equals_cstring(obj, BTDEVsecure))
    314   1.1  plunky 			sockopt_setint(&sc->sc_mode, L2CAP_LM_SECURE);
    315   1.1  plunky 		else  {
    316   1.1  plunky 			aprint_error(" unknown %s\n", BTDEVmode);
    317   1.1  plunky 			return;
    318   1.1  plunky 		}
    319   1.1  plunky 
    320   1.1  plunky 		aprint_verbose(" %s %s", BTDEVmode,
    321   1.1  plunky 		    prop_string_cstring_nocopy(obj));
    322   1.4  plunky 	} else
    323   1.4  plunky 		sockopt_setint(&sc->sc_mode, 0);
    324   1.1  plunky 
    325   1.1  plunky 	aprint_normal(": 3 buttons, W and Z dirs\n");
    326   1.1  plunky 	aprint_naive("\n");
    327   1.1  plunky 
    328   1.1  plunky 	/*
    329   1.1  plunky 	 * set defaults
    330   1.1  plunky 	 */
    331   1.1  plunky 	sc->sc_resolution = 650;
    332   1.1  plunky 	sc->sc_firm = 6;
    333   1.1  plunky 	sc->sc_dist = 130;
    334   1.1  plunky 	sc->sc_scale = 20;
    335   1.1  plunky 
    336   1.1  plunky 	sysctl_createv(&sc->sc_log, 0, NULL, &node,
    337   1.1  plunky 		0,
    338   1.1  plunky 		CTLTYPE_NODE, device_xname(self),
    339   1.1  plunky 		NULL,
    340   1.1  plunky 		NULL, 0,
    341   1.1  plunky 		NULL, 0,
    342   1.1  plunky 		CTL_HW,
    343   1.1  plunky 		CTL_CREATE, CTL_EOL);
    344   1.1  plunky 
    345   1.1  plunky 	if (node != NULL) {
    346   1.1  plunky 		sysctl_createv(&sc->sc_log, 0, NULL, NULL,
    347   1.1  plunky 			CTLFLAG_READWRITE,
    348   1.1  plunky 			CTLTYPE_INT, "soft_resolution",
    349   1.1  plunky 			NULL,
    350   1.1  plunky 			btmagic_sysctl_resolution, 0,
    351   1.3     dsl 			(void *)sc, 0,
    352   1.1  plunky 			CTL_HW, node->sysctl_num,
    353   1.1  plunky 			CTL_CREATE, CTL_EOL);
    354   1.1  plunky 
    355   1.1  plunky 		sysctl_createv(&sc->sc_log, 0, NULL, NULL,
    356   1.1  plunky 			CTLFLAG_READWRITE,
    357   1.1  plunky 			CTLTYPE_INT, "firm_touch_threshold",
    358   1.1  plunky 			NULL,
    359   1.1  plunky 			NULL, 0,
    360   1.1  plunky 			&sc->sc_firm, sizeof(sc->sc_firm),
    361   1.1  plunky 			CTL_HW, node->sysctl_num,
    362   1.1  plunky 			CTL_CREATE, CTL_EOL);
    363   1.1  plunky 
    364   1.1  plunky 		sysctl_createv(&sc->sc_log, 0, NULL, NULL,
    365   1.1  plunky 			CTLFLAG_READWRITE,
    366   1.1  plunky 			CTLTYPE_INT, "scroll_distance_threshold",
    367   1.1  plunky 			NULL,
    368   1.1  plunky 			NULL, 0,
    369   1.1  plunky 			&sc->sc_dist, sizeof(sc->sc_dist),
    370   1.1  plunky 			CTL_HW, node->sysctl_num,
    371   1.1  plunky 			CTL_CREATE, CTL_EOL);
    372   1.1  plunky 
    373   1.1  plunky 		sysctl_createv(&sc->sc_log, 0, NULL, NULL,
    374   1.1  plunky 			CTLFLAG_READWRITE,
    375   1.1  plunky 			CTLTYPE_INT, "scroll_downscale_factor",
    376   1.1  plunky 			NULL,
    377   1.1  plunky 			btmagic_sysctl_scale, 0,
    378   1.3     dsl 			(void *)sc, 0,
    379   1.1  plunky 			CTL_HW, node->sysctl_num,
    380   1.1  plunky 			CTL_CREATE, CTL_EOL);
    381   1.1  plunky 	}
    382   1.1  plunky 
    383   1.1  plunky 	/*
    384   1.1  plunky 	 * attach the wsmouse
    385   1.1  plunky 	 */
    386   1.1  plunky 	wsma.accessops = &btmagic_wsmouse_accessops;
    387   1.1  plunky 	wsma.accesscookie = self;
    388   1.1  plunky 	sc->sc_wsmouse = config_found(self, &wsma, wsmousedevprint);
    389   1.1  plunky 	if (sc->sc_wsmouse == NULL) {
    390   1.1  plunky 		aprint_error_dev(self, "failed to attach wsmouse\n");
    391   1.1  plunky 		return;
    392   1.1  plunky 	}
    393   1.1  plunky 
    394   1.2  plunky 	pmf_device_register(self, NULL, NULL);
    395   1.2  plunky 
    396   1.1  plunky 	/*
    397   1.1  plunky 	 * start bluetooth connections
    398   1.1  plunky 	 */
    399   1.1  plunky 	mutex_enter(bt_lock);
    400   1.5  plunky 	if ((err = btmagic_listen(sc)) != 0)
    401   1.5  plunky 		aprint_error_dev(self, "failed to listen (%d)\n", err);
    402   1.1  plunky 	btmagic_connect(sc);
    403   1.1  plunky 	mutex_exit(bt_lock);
    404   1.1  plunky }
    405   1.1  plunky 
    406   1.1  plunky static int
    407   1.1  plunky btmagic_detach(device_t self, int flags)
    408   1.1  plunky {
    409   1.1  plunky 	struct btmagic_softc *sc = device_private(self);
    410   1.1  plunky 	int err = 0;
    411   1.1  plunky 
    412   1.1  plunky 	mutex_enter(bt_lock);
    413   1.1  plunky 
    414   1.1  plunky 	/* release interrupt listen */
    415   1.1  plunky 	if (sc->sc_int_l != NULL) {
    416   1.7   rmind 		l2cap_detach_pcb(&sc->sc_int_l);
    417   1.1  plunky 		sc->sc_int_l = NULL;
    418   1.1  plunky 	}
    419   1.1  plunky 
    420   1.1  plunky 	/* release control listen */
    421   1.1  plunky 	if (sc->sc_ctl_l != NULL) {
    422   1.7   rmind 		l2cap_detach_pcb(&sc->sc_ctl_l);
    423   1.1  plunky 		sc->sc_ctl_l = NULL;
    424   1.1  plunky 	}
    425   1.1  plunky 
    426   1.1  plunky 	/* close interrupt channel */
    427   1.1  plunky 	if (sc->sc_int != NULL) {
    428  1.10     rtr 		l2cap_disconnect_pcb(sc->sc_int, 0);
    429   1.7   rmind 		l2cap_detach_pcb(&sc->sc_int);
    430   1.1  plunky 		sc->sc_int = NULL;
    431   1.1  plunky 	}
    432   1.1  plunky 
    433   1.1  plunky 	/* close control channel */
    434   1.1  plunky 	if (sc->sc_ctl != NULL) {
    435  1.10     rtr 		l2cap_disconnect_pcb(sc->sc_ctl, 0);
    436   1.7   rmind 		l2cap_detach_pcb(&sc->sc_ctl);
    437   1.1  plunky 		sc->sc_ctl = NULL;
    438   1.1  plunky 	}
    439   1.1  plunky 
    440   1.1  plunky 	callout_halt(&sc->sc_timeout, bt_lock);
    441   1.1  plunky 	callout_destroy(&sc->sc_timeout);
    442   1.1  plunky 
    443   1.1  plunky 	mutex_exit(bt_lock);
    444   1.1  plunky 
    445   1.2  plunky 	pmf_device_deregister(self);
    446   1.2  plunky 
    447   1.1  plunky 	sockopt_destroy(&sc->sc_mode);
    448   1.1  plunky 
    449   1.1  plunky 	sysctl_teardown(&sc->sc_log);
    450   1.1  plunky 
    451   1.1  plunky 	if (sc->sc_wsmouse != NULL) {
    452   1.1  plunky 		err = config_detach(sc->sc_wsmouse, flags);
    453   1.1  plunky 		sc->sc_wsmouse = NULL;
    454   1.1  plunky 	}
    455   1.1  plunky 
    456   1.1  plunky 	return err;
    457   1.1  plunky }
    458   1.1  plunky 
    459   1.1  plunky /*
    460   1.1  plunky  * listen for our device
    461   1.1  plunky  *
    462   1.1  plunky  * bt_lock is held
    463   1.1  plunky  */
    464   1.1  plunky static int
    465   1.1  plunky btmagic_listen(struct btmagic_softc *sc)
    466   1.1  plunky {
    467   1.1  plunky 	struct sockaddr_bt sa;
    468   1.1  plunky 	int err;
    469   1.1  plunky 
    470   1.1  plunky 	memset(&sa, 0, sizeof(sa));
    471   1.1  plunky 	sa.bt_len = sizeof(sa);
    472   1.1  plunky 	sa.bt_family = AF_BLUETOOTH;
    473   1.1  plunky 	bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
    474   1.1  plunky 
    475   1.1  plunky 	/*
    476   1.1  plunky 	 * Listen on control PSM
    477   1.1  plunky 	 */
    478   1.7   rmind 	err = l2cap_attach_pcb(&sc->sc_ctl_l, &btmagic_ctl_proto, sc);
    479   1.1  plunky 	if (err)
    480   1.1  plunky 		return err;
    481   1.1  plunky 
    482   1.1  plunky 	err = l2cap_setopt(sc->sc_ctl_l, &sc->sc_mode);
    483   1.1  plunky 	if (err)
    484   1.1  plunky 		return err;
    485   1.1  plunky 
    486   1.1  plunky 	sa.bt_psm = L2CAP_PSM_HID_CNTL;
    487   1.8     rtr 	err = l2cap_bind_pcb(sc->sc_ctl_l, &sa);
    488   1.1  plunky 	if (err)
    489   1.1  plunky 		return err;
    490   1.1  plunky 
    491   1.8     rtr 	err = l2cap_listen_pcb(sc->sc_ctl_l);
    492   1.1  plunky 	if (err)
    493   1.1  plunky 		return err;
    494   1.1  plunky 
    495   1.1  plunky 	/*
    496   1.1  plunky 	 * Listen on interrupt PSM
    497   1.1  plunky 	 */
    498   1.7   rmind 	err = l2cap_attach_pcb(&sc->sc_int_l, &btmagic_int_proto, sc);
    499   1.1  plunky 	if (err)
    500   1.1  plunky 		return err;
    501   1.1  plunky 
    502   1.1  plunky 	err = l2cap_setopt(sc->sc_int_l, &sc->sc_mode);
    503   1.1  plunky 	if (err)
    504   1.1  plunky 		return err;
    505   1.1  plunky 
    506   1.1  plunky 	sa.bt_psm = L2CAP_PSM_HID_INTR;
    507   1.8     rtr 	err = l2cap_bind_pcb(sc->sc_int_l, &sa);
    508   1.1  plunky 	if (err)
    509   1.1  plunky 		return err;
    510   1.1  plunky 
    511   1.8     rtr 	err = l2cap_listen_pcb(sc->sc_int_l);
    512   1.1  plunky 	if (err)
    513   1.1  plunky 		return err;
    514   1.1  plunky 
    515   1.1  plunky 	sc->sc_state = BTMAGIC_WAIT_CTL;
    516   1.1  plunky 	return 0;
    517   1.1  plunky }
    518   1.1  plunky 
    519   1.1  plunky /*
    520   1.1  plunky  * start connecting to our device
    521   1.1  plunky  *
    522   1.1  plunky  * bt_lock is held
    523   1.1  plunky  */
    524   1.1  plunky static int
    525   1.1  plunky btmagic_connect(struct btmagic_softc *sc)
    526   1.1  plunky {
    527   1.1  plunky 	struct sockaddr_bt sa;
    528   1.1  plunky 	int err;
    529   1.1  plunky 
    530   1.1  plunky 	memset(&sa, 0, sizeof(sa));
    531   1.1  plunky 	sa.bt_len = sizeof(sa);
    532   1.1  plunky 	sa.bt_family = AF_BLUETOOTH;
    533   1.1  plunky 
    534   1.7   rmind 	err = l2cap_attach_pcb(&sc->sc_ctl, &btmagic_ctl_proto, sc);
    535   1.1  plunky 	if (err) {
    536   1.1  plunky 		printf("%s: l2cap_attach failed (%d)\n",
    537   1.1  plunky 		    device_xname(sc->sc_dev), err);
    538   1.1  plunky 		return err;
    539   1.1  plunky 	}
    540   1.1  plunky 
    541   1.1  plunky 	err = l2cap_setopt(sc->sc_ctl, &sc->sc_mode);
    542   1.5  plunky 	if (err) {
    543   1.5  plunky 		printf("%s: l2cap_setopt failed (%d)\n",
    544   1.5  plunky 		    device_xname(sc->sc_dev), err);
    545   1.1  plunky 		return err;
    546   1.5  plunky 	}
    547   1.1  plunky 
    548   1.1  plunky 	bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
    549   1.8     rtr 	err = l2cap_bind_pcb(sc->sc_ctl, &sa);
    550   1.1  plunky 	if (err) {
    551   1.8     rtr 		printf("%s: l2cap_bind_pcb failed (%d)\n",
    552   1.1  plunky 		    device_xname(sc->sc_dev), err);
    553   1.1  plunky 		return err;
    554   1.1  plunky 	}
    555   1.1  plunky 
    556   1.1  plunky 	sa.bt_psm = L2CAP_PSM_HID_CNTL;
    557   1.1  plunky 	bdaddr_copy(&sa.bt_bdaddr, &sc->sc_raddr);
    558   1.9     rtr 	err = l2cap_connect_pcb(sc->sc_ctl, &sa);
    559   1.1  plunky 	if (err) {
    560   1.9     rtr 		printf("%s: l2cap_connect_pcb failed (%d)\n",
    561   1.1  plunky 		    device_xname(sc->sc_dev), err);
    562   1.1  plunky 		return err;
    563   1.1  plunky 	}
    564   1.1  plunky 
    565   1.1  plunky 	SET(sc->sc_flags, BTMAGIC_CONNECTING);
    566   1.1  plunky 	sc->sc_state = BTMAGIC_WAIT_CTL;
    567   1.1  plunky 	return 0;
    568   1.1  plunky }
    569   1.1  plunky 
    570   1.1  plunky /* validate soft_resolution */
    571   1.1  plunky static int
    572   1.1  plunky btmagic_sysctl_resolution(SYSCTLFN_ARGS)
    573   1.1  plunky {
    574   1.1  plunky 	struct sysctlnode node;
    575   1.1  plunky 	struct btmagic_softc *sc;
    576   1.1  plunky 	int t, error;
    577   1.1  plunky 
    578   1.1  plunky 	node = *rnode;
    579   1.1  plunky 	sc = node.sysctl_data;
    580   1.1  plunky 
    581   1.1  plunky 	t = sc->sc_resolution;
    582   1.1  plunky 	node.sysctl_data = &t;
    583   1.1  plunky 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    584   1.1  plunky 	if (error || newp == NULL)
    585   1.1  plunky 		return error;
    586   1.1  plunky 
    587   1.1  plunky 	if (t < 100 || t > 4000 || (t / sc->sc_scale) == 0)
    588   1.1  plunky 		return EINVAL;
    589   1.1  plunky 
    590   1.1  plunky 	sc->sc_resolution = t;
    591   1.1  plunky 	DPRINTF(sc, "sc_resolution = %u", t);
    592   1.1  plunky 	return 0;
    593   1.1  plunky }
    594   1.1  plunky 
    595   1.1  plunky /* validate scroll_downscale_factor */
    596   1.1  plunky static int
    597   1.1  plunky btmagic_sysctl_scale(SYSCTLFN_ARGS)
    598   1.1  plunky {
    599   1.1  plunky 	struct sysctlnode node;
    600   1.1  plunky 	struct btmagic_softc *sc;
    601   1.1  plunky 	int t, error;
    602   1.1  plunky 
    603   1.1  plunky 	node = *rnode;
    604   1.1  plunky 	sc = node.sysctl_data;
    605   1.1  plunky 
    606   1.1  plunky 	t = sc->sc_scale;
    607   1.1  plunky 	node.sysctl_data = &t;
    608   1.1  plunky 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    609   1.1  plunky 	if (error || newp == NULL)
    610   1.1  plunky 		return error;
    611   1.1  plunky 
    612   1.1  plunky 	if (t < 1 || t > 40 || (sc->sc_resolution / t) == 0)
    613   1.1  plunky 		return EINVAL;
    614   1.1  plunky 
    615   1.1  plunky 	sc->sc_scale = t;
    616   1.1  plunky 	DPRINTF(sc, "sc_scale = %u", t);
    617   1.1  plunky 	return 0;
    618   1.1  plunky }
    619   1.1  plunky 
    620   1.1  plunky /*****************************************************************************
    621   1.1  plunky  *
    622   1.1  plunky  *	wsmouse(4) accessops
    623   1.1  plunky  */
    624   1.1  plunky 
    625   1.1  plunky static int
    626   1.1  plunky btmagic_wsmouse_enable(void *self)
    627   1.1  plunky {
    628   1.1  plunky 	struct btmagic_softc *sc = device_private(self);
    629   1.1  plunky 
    630   1.1  plunky 	if (sc->sc_enabled)
    631   1.1  plunky 		return EBUSY;
    632   1.1  plunky 
    633   1.1  plunky 	sc->sc_enabled = 1;
    634   1.1  plunky 	DPRINTF(sc, "enable");
    635   1.1  plunky 	return 0;
    636   1.1  plunky }
    637   1.1  plunky 
    638   1.1  plunky static int
    639   1.1  plunky btmagic_wsmouse_ioctl(void *self, unsigned long cmd, void *data,
    640   1.1  plunky     int flag, struct lwp *l)
    641   1.1  plunky {
    642   1.1  plunky 	/* struct btmagic_softc *sc = device_private(self); */
    643   1.1  plunky 	int err;
    644   1.1  plunky 
    645   1.1  plunky 	switch (cmd) {
    646   1.1  plunky 	case WSMOUSEIO_GTYPE:
    647   1.1  plunky 		*(uint *)data = WSMOUSE_TYPE_BLUETOOTH;
    648   1.1  plunky 		err = 0;
    649   1.1  plunky 		break;
    650   1.1  plunky 
    651   1.1  plunky 	default:
    652   1.1  plunky 		err = EPASSTHROUGH;
    653   1.1  plunky 		break;
    654   1.1  plunky 	}
    655   1.1  plunky 
    656   1.1  plunky 	return err;
    657   1.1  plunky }
    658   1.1  plunky 
    659   1.1  plunky static void
    660   1.1  plunky btmagic_wsmouse_disable(void *self)
    661   1.1  plunky {
    662   1.1  plunky 	struct btmagic_softc *sc = device_private(self);
    663   1.1  plunky 
    664   1.1  plunky 	DPRINTF(sc, "disable");
    665   1.1  plunky 	sc->sc_enabled = 0;
    666   1.1  plunky }
    667   1.1  plunky 
    668   1.1  plunky 
    669   1.1  plunky /*****************************************************************************
    670   1.1  plunky  *
    671   1.1  plunky  *	setup routines
    672   1.1  plunky  */
    673   1.1  plunky 
    674   1.1  plunky static void
    675   1.1  plunky btmagic_timeout(void *arg)
    676   1.1  plunky {
    677   1.1  plunky 	struct btmagic_softc *sc = arg;
    678   1.1  plunky 
    679   1.1  plunky 	mutex_enter(bt_lock);
    680   1.1  plunky 	callout_ack(&sc->sc_timeout);
    681   1.1  plunky 
    682   1.1  plunky 	switch (sc->sc_state) {
    683   1.1  plunky 	case BTMAGIC_CLOSED:
    684   1.1  plunky 		if (sc->sc_int != NULL) {
    685  1.10     rtr 			l2cap_disconnect_pcb(sc->sc_int, 0);
    686   1.1  plunky 			break;
    687   1.1  plunky 		}
    688   1.1  plunky 
    689   1.1  plunky 		if (sc->sc_ctl != NULL) {
    690  1.10     rtr 			l2cap_disconnect_pcb(sc->sc_ctl, 0);
    691   1.1  plunky 			break;
    692   1.1  plunky 		}
    693   1.1  plunky 		break;
    694   1.1  plunky 
    695   1.1  plunky 	case BTMAGIC_OPEN:
    696   1.1  plunky 		if (!ISSET(sc->sc_flags, BTMAGIC_ENABLED)) {
    697   1.1  plunky 			btmagic_enable(sc);
    698   1.1  plunky 			break;
    699   1.1  plunky 		}
    700   1.1  plunky 
    701   1.1  plunky 		btmagic_check_battery(sc);
    702   1.1  plunky 		break;
    703   1.1  plunky 
    704   1.1  plunky 	case BTMAGIC_WAIT_CTL:
    705   1.1  plunky 	case BTMAGIC_WAIT_INT:
    706   1.1  plunky 	default:
    707   1.1  plunky 		break;
    708   1.1  plunky 	}
    709   1.1  plunky 	mutex_exit(bt_lock);
    710   1.1  plunky }
    711   1.1  plunky 
    712   1.1  plunky /*
    713   1.1  plunky  * Send report on control channel
    714   1.1  plunky  *
    715   1.1  plunky  * bt_lock is held
    716   1.1  plunky  */
    717   1.1  plunky static int
    718   1.1  plunky btmagic_ctl_send(struct btmagic_softc *sc, const uint8_t *data, size_t len)
    719   1.1  plunky {
    720   1.1  plunky 	struct mbuf *m;
    721   1.1  plunky 
    722   1.1  plunky 	if (len > MLEN)
    723   1.1  plunky 		return EINVAL;
    724   1.1  plunky 
    725   1.1  plunky 	m = m_gethdr(M_DONTWAIT, MT_DATA);
    726   1.1  plunky 	if (m == NULL)
    727   1.1  plunky 		return ENOMEM;
    728   1.1  plunky 
    729   1.1  plunky #ifdef BTMAGIC_DEBUG
    730   1.1  plunky 	printf("%s: send", device_xname(sc->sc_dev));
    731   1.1  plunky 	for (size_t i = 0; i < len; i++)
    732   1.1  plunky 		printf(" 0x%02x", data[i]);
    733   1.1  plunky 	printf("\n");
    734   1.1  plunky #endif
    735   1.1  plunky 
    736   1.1  plunky 	memcpy(mtod(m, uint8_t *), data, len);
    737   1.1  plunky 	m->m_pkthdr.len = m->m_len = len;
    738  1.11     rtr 	return l2cap_send_pcb(sc->sc_ctl, m);
    739   1.1  plunky }
    740   1.1  plunky 
    741   1.1  plunky /*
    742   1.1  plunky  * Enable touch reports by sending the following report
    743   1.1  plunky  *
    744   1.1  plunky  *	 SET_REPORT(FEATURE, 0xd7) = 0x01
    745   1.1  plunky  *
    746   1.1  plunky  * bt_lock is held
    747   1.1  plunky  */
    748   1.1  plunky static void
    749   1.1  plunky btmagic_enable(struct btmagic_softc *sc)
    750   1.1  plunky {
    751   1.1  plunky 	static const uint8_t rep[] = { 0x53, 0xd7, 0x01 };
    752   1.1  plunky 
    753   1.1  plunky 	if (btmagic_ctl_send(sc, rep, sizeof(rep)) != 0) {
    754   1.1  plunky 		printf("%s: cannot enable touch reports\n",
    755   1.1  plunky 		    device_xname(sc->sc_dev));
    756   1.1  plunky 
    757   1.1  plunky 		return;
    758   1.1  plunky 	}
    759   1.1  plunky 
    760   1.1  plunky 	SET(sc->sc_flags, BTMAGIC_ENABLED);
    761   1.1  plunky }
    762   1.1  plunky 
    763   1.1  plunky /*
    764   1.1  plunky  * Request the battery level by sending the following report
    765   1.1  plunky  *
    766   1.1  plunky  *	GET_REPORT(FEATURE, 0x47)
    767   1.1  plunky  *
    768   1.1  plunky  * bt_lock is held
    769   1.1  plunky  */
    770   1.1  plunky static void
    771   1.1  plunky btmagic_check_battery(struct btmagic_softc *sc)
    772   1.1  plunky {
    773   1.1  plunky 	static const uint8_t rep[] = { 0x43, 0x47 };
    774   1.1  plunky 
    775   1.1  plunky 	if (btmagic_ctl_send(sc, rep, sizeof(rep)) != 0)
    776   1.1  plunky 		printf("%s: cannot request battery level\n",
    777   1.1  plunky 		    device_xname(sc->sc_dev));
    778   1.1  plunky }
    779   1.1  plunky 
    780   1.1  plunky /*
    781   1.1  plunky  * the Magic Mouse has a base resolution of 1300dpi which is rather flighty. We
    782   1.1  plunky  * scale the output to the requested resolution, taking care to account for the
    783   1.1  plunky  * remainders to prevent loss of small deltas.
    784   1.1  plunky  */
    785   1.1  plunky static int
    786   1.1  plunky btmagic_scale(int delta, int *remainder, int resolution)
    787   1.1  plunky {
    788   1.1  plunky 	int new;
    789   1.1  plunky 
    790   1.1  plunky 	delta += *remainder;
    791   1.1  plunky 	new = delta * resolution / 1300;
    792   1.1  plunky 	*remainder = delta - new * 1300 / resolution;
    793   1.1  plunky 	return new;
    794   1.1  plunky }
    795   1.1  plunky 
    796   1.1  plunky 
    797   1.1  plunky /*****************************************************************************
    798   1.1  plunky  *
    799   1.1  plunky  *	bluetooth(9) callback methods for L2CAP
    800   1.1  plunky  *
    801   1.1  plunky  *	All these are called from Bluetooth Protocol code, holding bt_lock.
    802   1.1  plunky  */
    803   1.1  plunky 
    804   1.1  plunky static void
    805   1.1  plunky btmagic_connecting(void *arg)
    806   1.1  plunky {
    807   1.1  plunky 
    808   1.1  plunky 	/* dont care */
    809   1.1  plunky }
    810   1.1  plunky 
    811   1.1  plunky static void
    812   1.1  plunky btmagic_ctl_connected(void *arg)
    813   1.1  plunky {
    814   1.1  plunky 	struct sockaddr_bt sa;
    815   1.1  plunky 	struct btmagic_softc *sc = arg;
    816   1.1  plunky 	int err;
    817   1.1  plunky 
    818   1.1  plunky 	if (sc->sc_state != BTMAGIC_WAIT_CTL)
    819   1.1  plunky 		return;
    820   1.1  plunky 
    821   1.1  plunky 	KASSERT(sc->sc_ctl != NULL);
    822   1.1  plunky 	KASSERT(sc->sc_int == NULL);
    823   1.1  plunky 
    824   1.1  plunky 	if (ISSET(sc->sc_flags, BTMAGIC_CONNECTING)) {
    825   1.1  plunky 		/* initiate connect on interrupt PSM */
    826   1.7   rmind 		err = l2cap_attach_pcb(&sc->sc_int, &btmagic_int_proto, sc);
    827   1.1  plunky 		if (err)
    828   1.1  plunky 			goto fail;
    829   1.1  plunky 
    830   1.1  plunky 		err = l2cap_setopt(sc->sc_int, &sc->sc_mode);
    831   1.1  plunky 		if (err)
    832   1.1  plunky 			goto fail;
    833   1.1  plunky 
    834   1.1  plunky 		memset(&sa, 0, sizeof(sa));
    835   1.1  plunky 		sa.bt_len = sizeof(sa);
    836   1.1  plunky 		sa.bt_family = AF_BLUETOOTH;
    837   1.1  plunky 		bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
    838   1.1  plunky 
    839   1.8     rtr 		err = l2cap_bind_pcb(sc->sc_int, &sa);
    840   1.1  plunky 		if (err)
    841   1.1  plunky 			goto fail;
    842   1.1  plunky 
    843   1.1  plunky 		sa.bt_psm = L2CAP_PSM_HID_INTR;
    844   1.1  plunky 		bdaddr_copy(&sa.bt_bdaddr, &sc->sc_raddr);
    845   1.9     rtr 		err = l2cap_connect_pcb(sc->sc_int, &sa);
    846   1.1  plunky 		if (err)
    847   1.1  plunky 			goto fail;
    848   1.1  plunky 	}
    849   1.1  plunky 
    850   1.1  plunky 	sc->sc_state = BTMAGIC_WAIT_INT;
    851   1.1  plunky 	return;
    852   1.1  plunky 
    853   1.1  plunky fail:
    854   1.7   rmind 	l2cap_detach_pcb(&sc->sc_ctl);
    855   1.1  plunky 	sc->sc_ctl = NULL;
    856   1.1  plunky 
    857   1.1  plunky 	printf("%s: connect failed (%d)\n", device_xname(sc->sc_dev), err);
    858   1.1  plunky }
    859   1.1  plunky 
    860   1.1  plunky static void
    861   1.1  plunky btmagic_int_connected(void *arg)
    862   1.1  plunky {
    863   1.1  plunky 	struct btmagic_softc *sc = arg;
    864   1.1  plunky 
    865   1.1  plunky 	if (sc->sc_state != BTMAGIC_WAIT_INT)
    866   1.1  plunky 		return;
    867   1.1  plunky 
    868   1.1  plunky 	KASSERT(sc->sc_ctl != NULL);
    869   1.1  plunky 	KASSERT(sc->sc_int != NULL);
    870   1.1  plunky 
    871   1.1  plunky 	printf("%s: connected\n", device_xname(sc->sc_dev));
    872   1.1  plunky 	CLR(sc->sc_flags, BTMAGIC_CONNECTING);
    873   1.1  plunky 	sc->sc_state = BTMAGIC_OPEN;
    874   1.1  plunky 
    875   1.1  plunky 	/* trigger the setup */
    876   1.1  plunky 	CLR(sc->sc_flags, BTMAGIC_ENABLED);
    877   1.1  plunky 	callout_schedule(&sc->sc_timeout, hz);
    878   1.1  plunky }
    879   1.1  plunky 
    880   1.1  plunky /*
    881   1.1  plunky  * Disconnected
    882   1.1  plunky  *
    883   1.1  plunky  * Depending on our state, this could mean several things, but essentially
    884   1.1  plunky  * we are lost. If both channels are closed, schedule another connection.
    885   1.1  plunky  */
    886   1.1  plunky static void
    887   1.1  plunky btmagic_ctl_disconnected(void *arg, int err)
    888   1.1  plunky {
    889   1.1  plunky 	struct btmagic_softc *sc = arg;
    890   1.1  plunky 
    891   1.1  plunky 	if (sc->sc_ctl != NULL) {
    892   1.7   rmind 		l2cap_detach_pcb(&sc->sc_ctl);
    893   1.1  plunky 		sc->sc_ctl = NULL;
    894   1.1  plunky 	}
    895   1.1  plunky 
    896   1.1  plunky 	if (sc->sc_int == NULL) {
    897   1.5  plunky 		printf("%s: disconnected (%d)\n", device_xname(sc->sc_dev), err);
    898   1.1  plunky 		CLR(sc->sc_flags, BTMAGIC_CONNECTING);
    899   1.1  plunky 		sc->sc_state = BTMAGIC_WAIT_CTL;
    900   1.1  plunky 	} else {
    901   1.1  plunky 		/*
    902   1.1  plunky 		 * The interrupt channel should have been closed first,
    903   1.1  plunky 		 * but its potentially unsafe to detach that from here.
    904   1.1  plunky 		 * Give them a second to do the right thing or let the
    905   1.1  plunky 		 * callout handle it.
    906   1.1  plunky 		 */
    907   1.1  plunky 		sc->sc_state = BTMAGIC_CLOSED;
    908   1.1  plunky 		callout_schedule(&sc->sc_timeout, hz);
    909   1.1  plunky 	}
    910   1.1  plunky }
    911   1.1  plunky 
    912   1.1  plunky static void
    913   1.1  plunky btmagic_int_disconnected(void *arg, int err)
    914   1.1  plunky {
    915   1.1  plunky 	struct btmagic_softc *sc = arg;
    916   1.1  plunky 
    917   1.1  plunky 	if (sc->sc_int != NULL) {
    918   1.7   rmind 		l2cap_detach_pcb(&sc->sc_int);
    919   1.1  plunky 		sc->sc_int = NULL;
    920   1.1  plunky 	}
    921   1.1  plunky 
    922   1.1  plunky 	if (sc->sc_ctl == NULL) {
    923   1.5  plunky 		printf("%s: disconnected (%d)\n", device_xname(sc->sc_dev), err);
    924   1.1  plunky 		CLR(sc->sc_flags, BTMAGIC_CONNECTING);
    925   1.1  plunky 		sc->sc_state = BTMAGIC_WAIT_CTL;
    926   1.1  plunky 	} else {
    927   1.1  plunky 		/*
    928   1.1  plunky 		 * The control channel should be closing also, allow
    929   1.1  plunky 		 * them a chance to do that before we force it.
    930   1.1  plunky 		 */
    931   1.1  plunky 		sc->sc_state = BTMAGIC_CLOSED;
    932   1.1  plunky 		callout_schedule(&sc->sc_timeout, hz);
    933   1.1  plunky 	}
    934   1.1  plunky }
    935   1.1  plunky 
    936   1.1  plunky /*
    937   1.1  plunky  * New Connections
    938   1.1  plunky  *
    939   1.1  plunky  * We give a new L2CAP handle back if this matches the BDADDR we are
    940   1.1  plunky  * listening for and we are in the right state. btmagic_connected will
    941   1.1  plunky  * be called when the connection is open, so nothing else to do here
    942   1.1  plunky  */
    943   1.1  plunky static void *
    944   1.1  plunky btmagic_ctl_newconn(void *arg, struct sockaddr_bt *laddr,
    945   1.1  plunky     struct sockaddr_bt *raddr)
    946   1.1  plunky {
    947   1.1  plunky 	struct btmagic_softc *sc = arg;
    948   1.1  plunky 
    949   1.1  plunky 	if (bdaddr_same(&raddr->bt_bdaddr, &sc->sc_raddr) == 0)
    950   1.1  plunky 		return NULL;
    951   1.1  plunky 
    952   1.1  plunky 	if (sc->sc_state != BTMAGIC_WAIT_CTL
    953   1.1  plunky 	    || ISSET(sc->sc_flags, BTMAGIC_CONNECTING)
    954   1.1  plunky 	    || sc->sc_ctl != NULL
    955   1.1  plunky 	    || sc->sc_int != NULL) {
    956   1.1  plunky 		DPRINTF(sc, "reject ctl newconn %s%s%s%s",
    957   1.1  plunky 		    (sc->sc_state == BTMAGIC_WAIT_CTL) ? " (WAITING)": "",
    958   1.1  plunky 		    ISSET(sc->sc_flags, BTMAGIC_CONNECTING) ? " (CONNECTING)" : "",
    959   1.1  plunky 		    (sc->sc_ctl != NULL) ? " (GOT CONTROL)" : "",
    960   1.1  plunky 		    (sc->sc_int != NULL) ? " (GOT INTERRUPT)" : "");
    961   1.1  plunky 
    962   1.1  plunky 		return NULL;
    963   1.1  plunky 	}
    964   1.1  plunky 
    965   1.7   rmind 	l2cap_attach_pcb(&sc->sc_ctl, &btmagic_ctl_proto, sc);
    966   1.1  plunky 	return sc->sc_ctl;
    967   1.1  plunky }
    968   1.1  plunky 
    969   1.1  plunky static void *
    970   1.1  plunky btmagic_int_newconn(void *arg, struct sockaddr_bt *laddr,
    971   1.1  plunky     struct sockaddr_bt *raddr)
    972   1.1  plunky {
    973   1.1  plunky 	struct btmagic_softc *sc = arg;
    974   1.1  plunky 
    975   1.1  plunky 	if (bdaddr_same(&raddr->bt_bdaddr, &sc->sc_raddr) == 0)
    976   1.1  plunky 		return NULL;
    977   1.1  plunky 
    978   1.1  plunky 	if (sc->sc_state != BTMAGIC_WAIT_INT
    979   1.1  plunky 	    || ISSET(sc->sc_flags, BTMAGIC_CONNECTING)
    980   1.1  plunky 	    || sc->sc_ctl == NULL
    981   1.1  plunky 	    || sc->sc_int != NULL) {
    982   1.1  plunky 		DPRINTF(sc, "reject int newconn %s%s%s%s",
    983   1.1  plunky 		    (sc->sc_state == BTMAGIC_WAIT_INT) ? " (WAITING)": "",
    984   1.1  plunky 		    ISSET(sc->sc_flags, BTMAGIC_CONNECTING) ? " (CONNECTING)" : "",
    985   1.1  plunky 		    (sc->sc_ctl == NULL) ? " (NO CONTROL)" : "",
    986   1.1  plunky 		    (sc->sc_int != NULL) ? " (GOT INTERRUPT)" : "");
    987   1.1  plunky 
    988   1.1  plunky 		return NULL;
    989   1.1  plunky 	}
    990   1.1  plunky 
    991   1.7   rmind 	l2cap_attach_pcb(&sc->sc_int, &btmagic_int_proto, sc);
    992   1.1  plunky 	return sc->sc_int;
    993   1.1  plunky }
    994   1.1  plunky 
    995   1.1  plunky static void
    996   1.1  plunky btmagic_complete(void *arg, int count)
    997   1.1  plunky {
    998   1.1  plunky 
    999   1.1  plunky 	/* dont care */
   1000   1.1  plunky }
   1001   1.1  plunky 
   1002   1.1  plunky static void
   1003   1.1  plunky btmagic_linkmode(void *arg, int new)
   1004   1.1  plunky {
   1005   1.1  plunky 	struct btmagic_softc *sc = arg;
   1006   1.1  plunky 	int mode;
   1007   1.1  plunky 
   1008   1.1  plunky 	(void)sockopt_getint(&sc->sc_mode, &mode);
   1009   1.1  plunky 
   1010   1.1  plunky 	if (ISSET(mode, L2CAP_LM_AUTH) && !ISSET(new, L2CAP_LM_AUTH))
   1011   1.1  plunky 		printf("%s: auth failed\n", device_xname(sc->sc_dev));
   1012   1.1  plunky 	else if (ISSET(mode, L2CAP_LM_ENCRYPT) && !ISSET(new, L2CAP_LM_ENCRYPT))
   1013   1.1  plunky 		printf("%s: encrypt off\n", device_xname(sc->sc_dev));
   1014   1.1  plunky 	else if (ISSET(mode, L2CAP_LM_SECURE) && !ISSET(new, L2CAP_LM_SECURE))
   1015   1.1  plunky 		printf("%s: insecure\n", device_xname(sc->sc_dev));
   1016   1.1  plunky 	else
   1017   1.1  plunky 		return;
   1018   1.1  plunky 
   1019   1.1  plunky 	if (sc->sc_int != NULL)
   1020  1.10     rtr 		l2cap_disconnect_pcb(sc->sc_int, 0);
   1021   1.1  plunky 
   1022   1.1  plunky 	if (sc->sc_ctl != NULL)
   1023  1.10     rtr 		l2cap_disconnect_pcb(sc->sc_ctl, 0);
   1024   1.1  plunky }
   1025   1.1  plunky 
   1026   1.1  plunky /*
   1027   1.1  plunky  * Receive transaction from the mouse. We don't differentiate between
   1028   1.1  plunky  * interrupt and control channel here, there is no need.
   1029   1.1  plunky  */
   1030   1.1  plunky static void
   1031   1.1  plunky btmagic_input(void *arg, struct mbuf *m)
   1032   1.1  plunky {
   1033   1.1  plunky 	struct btmagic_softc *sc = arg;
   1034   1.1  plunky 	uint8_t *data;
   1035   1.1  plunky 	size_t len;
   1036   1.1  plunky 
   1037   1.1  plunky 	if (sc->sc_state != BTMAGIC_OPEN
   1038   1.1  plunky 	    || sc->sc_wsmouse == NULL
   1039   1.1  plunky 	    || sc->sc_enabled == 0)
   1040   1.1  plunky 		goto release;
   1041   1.1  plunky 
   1042   1.1  plunky 	if (m->m_pkthdr.len > m->m_len)
   1043   1.1  plunky 		printf("%s: truncating input\n", device_xname(sc->sc_dev));
   1044   1.1  plunky 
   1045   1.1  plunky 	data = mtod(m, uint8_t *);
   1046   1.1  plunky 	len = m->m_len;
   1047   1.1  plunky 
   1048   1.1  plunky 	if (len < 1)
   1049   1.1  plunky 		goto release;
   1050   1.1  plunky 
   1051   1.1  plunky 	switch (BTHID_TYPE(data[0])) {
   1052   1.1  plunky 	case BTHID_HANDSHAKE:
   1053   1.1  plunky 		DPRINTF(sc, "Handshake: 0x%x", BTHID_HANDSHAKE_PARAM(data[0]));
   1054   1.1  plunky 		callout_schedule(&sc->sc_timeout, hz);
   1055   1.1  plunky 		break;
   1056   1.1  plunky 
   1057   1.1  plunky 	case BTHID_DATA:
   1058   1.1  plunky 		if (len < 2)
   1059   1.1  plunky 			break;
   1060   1.1  plunky 
   1061   1.1  plunky 		switch (data[1]) {
   1062  1.12  bouyer 		case BASIC_REPORT_ID: /* Basic mouse (input) */
   1063   1.1  plunky 			btmagic_input_basic(sc, data + 2, len - 2);
   1064   1.1  plunky 			break;
   1065   1.1  plunky 
   1066  1.12  bouyer 		case TRACKPAD_REPORT_ID: /* Magic trackpad (input) */
   1067  1.12  bouyer 			btmagic_input_magict(sc, data + 2, len - 2);
   1068  1.12  bouyer 			break;
   1069  1.12  bouyer 		case MOUSE_REPORT_ID: /* Magic touch (input) */
   1070  1.12  bouyer 			btmagic_input_magicm(sc, data + 2, len - 2);
   1071   1.1  plunky 			break;
   1072   1.1  plunky 
   1073  1.12  bouyer 		case BATT_STAT_REPORT_ID: /* Battery status (input) */
   1074   1.1  plunky 			if (len != 3)
   1075   1.1  plunky 				break;
   1076   1.1  plunky 
   1077   1.1  plunky 			printf("%s: Battery ", device_xname(sc->sc_dev));
   1078   1.1  plunky 			switch (data[2]) {
   1079   1.1  plunky 			case 0:	printf("Ok\n");			break;
   1080   1.1  plunky 			case 1:	printf("Warning\n");		break;
   1081   1.1  plunky 			case 2:	printf("Critical\n");		break;
   1082   1.1  plunky 			default: printf("0x%02x\n", data[2]);	break;
   1083   1.1  plunky 			}
   1084   1.1  plunky 			break;
   1085   1.1  plunky 
   1086  1.12  bouyer 		case BATT_STRENGHT_REPORT_ID: /* Battery strength (feature) */
   1087   1.1  plunky 			if (len != 3)
   1088   1.1  plunky 				break;
   1089   1.1  plunky 
   1090   1.1  plunky 			printf("%s: Battery %d%%\n", device_xname(sc->sc_dev),
   1091   1.1  plunky 			    data[2]);
   1092   1.1  plunky 			break;
   1093   1.1  plunky 
   1094  1.12  bouyer 		case SURFACE_REPORT_ID: /* Surface detection (input) */
   1095   1.1  plunky 			if (len != 3)
   1096   1.1  plunky 				break;
   1097   1.1  plunky 
   1098   1.1  plunky 			DPRINTF(sc, "Mouse %s",
   1099   1.1  plunky 			    (data[2] == 0 ? "lowered" : "raised"));
   1100   1.1  plunky 			break;
   1101   1.1  plunky 
   1102   1.1  plunky 		case 0x60: /* unknown (input) */
   1103   1.1  plunky 		case 0xf0: /* unknown (feature) */
   1104   1.1  plunky 		case 0xf1: /* unknown (feature) */
   1105   1.1  plunky 		default:
   1106   1.1  plunky #if BTMAGIC_DEBUG
   1107   1.1  plunky 			printf("%s: recv", device_xname(sc->sc_dev));
   1108   1.1  plunky 			for (size_t i = 0; i < len; i++)
   1109   1.1  plunky 				printf(" 0x%02x", data[i]);
   1110   1.1  plunky 			printf("\n");
   1111   1.1  plunky #endif
   1112   1.1  plunky 			break;
   1113   1.1  plunky 		}
   1114   1.1  plunky 		break;
   1115   1.1  plunky 
   1116   1.1  plunky 	default:
   1117   1.1  plunky 		DPRINTF(sc, "transaction (type 0x%x)", BTHID_TYPE(data[0]));
   1118   1.1  plunky 		break;
   1119   1.1  plunky 	}
   1120   1.1  plunky 
   1121   1.1  plunky release:
   1122   1.1  plunky 	m_freem(m);
   1123   1.1  plunky }
   1124   1.1  plunky 
   1125   1.1  plunky /*
   1126   1.1  plunky  * parse the Basic report (0x10), which according to the provided
   1127   1.1  plunky  * HID descriptor is in the following format
   1128   1.1  plunky  *
   1129   1.1  plunky  *	button 1	1-bit
   1130   1.1  plunky  *	button 2	1-bit
   1131   1.1  plunky  *	padding		6-bits
   1132   1.1  plunky  *	dX		16-bits (signed)
   1133   1.1  plunky  *	dY		16-bits (signed)
   1134   1.1  plunky  *
   1135   1.1  plunky  * Even when the magic touch reports are enabled, the basic report is
   1136   1.1  plunky  * sent for mouse move events where no touches are detected.
   1137   1.1  plunky  */
   1138   1.1  plunky static const struct {
   1139   1.1  plunky 	struct hid_location button1;
   1140   1.1  plunky 	struct hid_location button2;
   1141   1.1  plunky 	struct hid_location dX;
   1142   1.1  plunky 	struct hid_location dY;
   1143   1.1  plunky } basic = {
   1144   1.1  plunky 	.button1 = { .pos =  0, .size = 1 },
   1145   1.1  plunky 	.button2 = { .pos =  1, .size = 1 },
   1146   1.1  plunky 	.dX = { .pos =  8, .size = 16 },
   1147   1.1  plunky 	.dY = { .pos = 24, .size = 16 },
   1148   1.1  plunky };
   1149   1.1  plunky 
   1150   1.1  plunky static void
   1151   1.1  plunky btmagic_input_basic(struct btmagic_softc *sc, uint8_t *data, size_t len)
   1152   1.1  plunky {
   1153   1.1  plunky 	int dx, dy;
   1154   1.1  plunky 	uint32_t mb;
   1155   1.1  plunky 	int s;
   1156   1.1  plunky 
   1157   1.1  plunky 	if (len != 5)
   1158   1.1  plunky 		return;
   1159   1.1  plunky 
   1160   1.1  plunky 	dx = hid_get_data(data, &basic.dX);
   1161   1.1  plunky 	dx = btmagic_scale(dx, &sc->sc_rx, sc->sc_resolution);
   1162   1.1  plunky 
   1163   1.1  plunky 	dy = hid_get_data(data, &basic.dY);
   1164   1.1  plunky 	dy = btmagic_scale(dy, &sc->sc_ry, sc->sc_resolution);
   1165   1.1  plunky 
   1166   1.1  plunky 	mb = 0;
   1167   1.1  plunky 	if (hid_get_udata(data, &basic.button1))
   1168   1.1  plunky 		mb |= __BIT(0);
   1169   1.1  plunky 	if (hid_get_udata(data, &basic.button2))
   1170   1.1  plunky 		mb |= __BIT(2);
   1171   1.1  plunky 
   1172   1.1  plunky 	if (dx != 0 || dy != 0 || mb != sc->sc_mb) {
   1173   1.1  plunky 		sc->sc_mb = mb;
   1174   1.1  plunky 
   1175   1.1  plunky 		s = spltty();
   1176   1.1  plunky 		wsmouse_input(sc->sc_wsmouse, mb,
   1177   1.1  plunky 		    dx, -dy, 0, 0, WSMOUSE_INPUT_DELTA);
   1178   1.1  plunky 		splx(s);
   1179   1.1  plunky 	}
   1180   1.1  plunky }
   1181   1.1  plunky 
   1182   1.1  plunky /*
   1183   1.1  plunky  * the Magic touch report (0x29), according to the Linux driver
   1184   1.1  plunky  * written by Michael Poole, is variable length starting with the
   1185   1.1  plunky  * fixed 40-bit header
   1186   1.1  plunky  *
   1187   1.1  plunky  *	dX lsb		8-bits (signed)
   1188   1.1  plunky  *	dY lsb		8-bits (signed)
   1189   1.1  plunky  *	button 1	1-bit
   1190   1.1  plunky  *	button 2	1-bit
   1191   1.1  plunky  *	dX msb		2-bits (signed)
   1192   1.1  plunky  *	dY msb		2-bits (signed)
   1193   1.1  plunky  *	timestamp	18-bits
   1194   1.1  plunky  *
   1195   1.1  plunky  * followed by (up to 5?) touch reports of 64-bits each
   1196   1.1  plunky  *
   1197   1.1  plunky  *	abs W		12-bits (signed)
   1198   1.1  plunky  *	abs Z		12-bits (signed)
   1199   1.1  plunky  *	axis major	8-bits
   1200   1.1  plunky  *	axis minor	8-bits
   1201   1.1  plunky  *	pressure	6-bits
   1202   1.1  plunky  *	id		4-bits
   1203   1.1  plunky  *	angle		6-bits	(from E(0)->N(32)->W(64))
   1204   1.1  plunky  *	unknown		4-bits
   1205   1.1  plunky  *	phase		4-bits
   1206   1.1  plunky  */
   1207   1.1  plunky 
   1208   1.1  plunky static const struct {
   1209   1.1  plunky 	struct hid_location dXl;
   1210   1.1  plunky 	struct hid_location dYl;
   1211   1.1  plunky 	struct hid_location button1;
   1212   1.1  plunky 	struct hid_location button2;
   1213   1.1  plunky 	struct hid_location dXm;
   1214   1.1  plunky 	struct hid_location dYm;
   1215   1.1  plunky 	struct hid_location timestamp;
   1216   1.1  plunky } magic = {
   1217   1.1  plunky 	.dXl = { .pos = 0, .size = 8 },
   1218   1.1  plunky 	.dYl = { .pos = 8, .size = 8 },
   1219   1.1  plunky 	.button1 = { .pos = 16, .size = 1 },
   1220   1.1  plunky 	.button2 = { .pos = 17, .size = 1 },
   1221   1.1  plunky 	.dXm = { .pos = 18, .size = 2 },
   1222   1.1  plunky 	.dYm = { .pos = 20, .size = 2 },
   1223   1.1  plunky 	.timestamp = { .pos = 22, .size = 18 },
   1224   1.1  plunky };
   1225   1.1  plunky 
   1226   1.1  plunky static const struct {
   1227   1.1  plunky 	struct hid_location aW;
   1228   1.1  plunky 	struct hid_location aZ;
   1229   1.1  plunky 	struct hid_location major;
   1230   1.1  plunky 	struct hid_location minor;
   1231   1.1  plunky 	struct hid_location pressure;
   1232   1.1  plunky 	struct hid_location id;
   1233   1.1  plunky 	struct hid_location angle;
   1234   1.1  plunky 	struct hid_location unknown;
   1235   1.1  plunky 	struct hid_location phase;
   1236   1.1  plunky } touch = {
   1237   1.1  plunky 	.aW = { .pos = 0, .size = 12 },
   1238   1.1  plunky 	.aZ = { .pos = 12, .size = 12 },
   1239   1.1  plunky 	.major = { .pos = 24, .size = 8 },
   1240   1.1  plunky 	.minor = { .pos = 32, .size = 8 },
   1241   1.1  plunky 	.pressure = { .pos = 40, .size = 6 },
   1242   1.1  plunky 	.id = { .pos = 46, .size = 4 },
   1243   1.1  plunky 	.angle = { .pos = 50, .size = 6 },
   1244   1.1  plunky 	.unknown = { .pos = 56, .size = 4 },
   1245   1.1  plunky 	.phase = { .pos = 60, .size = 4 },
   1246   1.1  plunky };
   1247   1.1  plunky 
   1248   1.1  plunky /*
   1249   1.1  plunky  * the phase of the touch starts at 0x01 as the finger is first detected
   1250   1.1  plunky  * approaching the mouse, increasing to 0x04 while the finger is touching,
   1251   1.1  plunky  * then increases towards 0x07 as the finger is lifted, and we get 0x00
   1252   1.1  plunky  * when the touch is cancelled. The values below seem to be produced for
   1253   1.1  plunky  * every touch, the others less consistently depending on how fast the
   1254   1.1  plunky  * approach or departure is.
   1255   1.1  plunky  *
   1256   1.1  plunky  * In fact we ignore touches unless they are in the steady 0x04 phase.
   1257   1.1  plunky  */
   1258   1.1  plunky #define BTMAGIC_PHASE_START	0x3
   1259   1.1  plunky #define BTMAGIC_PHASE_CONT	0x4
   1260   1.1  plunky #define BTMAGIC_PHASE_END	0x7
   1261   1.1  plunky #define BTMAGIC_PHASE_CANCEL	0x0
   1262   1.1  plunky 
   1263   1.1  plunky static void
   1264  1.12  bouyer btmagic_input_magicm(struct btmagic_softc *sc, uint8_t *data, size_t len)
   1265   1.1  plunky {
   1266   1.1  plunky 	uint32_t mb;
   1267   1.1  plunky 	int dx, dy, dz, dw;
   1268   1.1  plunky 	int id, nf, az, aw, tz, tw;
   1269   1.1  plunky 	int s;
   1270   1.1  plunky 
   1271   1.1  plunky 	if (((len - 5) % 8) != 0)
   1272   1.1  plunky 		return;
   1273   1.1  plunky 
   1274   1.1  plunky 	dx = (hid_get_data(data, &magic.dXm) << 8)
   1275   1.1  plunky 	    | (hid_get_data(data, &magic.dXl) & 0xff);
   1276   1.1  plunky 	dx = btmagic_scale(dx, &sc->sc_rx, sc->sc_resolution);
   1277   1.1  plunky 
   1278   1.1  plunky 	dy = (hid_get_data(data, &magic.dYm) << 8)
   1279   1.1  plunky 	    | (hid_get_data(data, &magic.dYl) & 0xff);
   1280   1.1  plunky 	dy = btmagic_scale(dy, &sc->sc_ry, sc->sc_resolution);
   1281   1.1  plunky 
   1282   1.1  plunky 	mb = 0;
   1283   1.1  plunky 	if (hid_get_udata(data, &magic.button1))
   1284   1.1  plunky 		mb |= __BIT(0);
   1285   1.1  plunky 	if (hid_get_udata(data, &magic.button2))
   1286   1.1  plunky 		mb |= __BIT(2);
   1287   1.1  plunky 
   1288   1.1  plunky 	nf = 0;
   1289   1.1  plunky 	dz = 0;
   1290   1.1  plunky 	dw = 0;
   1291   1.1  plunky 	len = (len - 5) / 8;
   1292   1.1  plunky 	for (data += 5; len-- > 0; data += 8) {
   1293   1.1  plunky 		id = hid_get_udata(data, &touch.id);
   1294   1.1  plunky 		az = hid_get_data(data, &touch.aZ);
   1295   1.1  plunky 		aw = hid_get_data(data, &touch.aW);
   1296   1.1  plunky 
   1297   1.1  plunky 		/*
   1298   1.1  plunky 		 * scrolling is triggered by an established touch moving
   1299   1.1  plunky 		 * beyond a minimum distance from its start point and is
   1300   1.1  plunky 		 * cancelled as the touch starts to fade.
   1301   1.1  plunky 		 *
   1302   1.1  plunky 		 * Multiple touches may be scrolling simultaneously, the
   1303   1.1  plunky 		 * effect is cumulative.
   1304   1.1  plunky 		 */
   1305   1.1  plunky 
   1306   1.1  plunky 		switch (hid_get_udata(data, &touch.phase)) {
   1307   1.1  plunky 		case BTMAGIC_PHASE_CONT:
   1308  1.12  bouyer #define sc_az sc_ay
   1309  1.12  bouyer #define sc_aw sc_ax
   1310   1.1  plunky 			tz = az - sc->sc_az[id];
   1311   1.1  plunky 			tw = aw - sc->sc_aw[id];
   1312   1.1  plunky 
   1313  1.12  bouyer 			if (ISSET(sc->sc_smask, __BIT(id))) {
   1314   1.1  plunky 				/* scrolling finger */
   1315   1.1  plunky 				dz += btmagic_scale(tz, &sc->sc_rz,
   1316   1.1  plunky 				    sc->sc_resolution / sc->sc_scale);
   1317   1.1  plunky 				dw += btmagic_scale(tw, &sc->sc_rw,
   1318   1.1  plunky 				    sc->sc_resolution / sc->sc_scale);
   1319   1.1  plunky 			} else if (abs(tz) > sc->sc_dist
   1320   1.1  plunky 			    || abs(tw) > sc->sc_dist) {
   1321   1.1  plunky 				/* new scrolling finger */
   1322   1.1  plunky 				if (sc->sc_smask == 0) {
   1323   1.1  plunky 					sc->sc_rz = 0;
   1324   1.1  plunky 					sc->sc_rw = 0;
   1325   1.1  plunky 				}
   1326   1.1  plunky 
   1327  1.12  bouyer 				SET(sc->sc_smask, __BIT(id));
   1328   1.1  plunky 			} else {
   1329   1.1  plunky 				/* not scrolling finger */
   1330   1.1  plunky 				az = sc->sc_az[id];
   1331   1.1  plunky 				aw = sc->sc_aw[id];
   1332   1.1  plunky 			}
   1333   1.1  plunky 
   1334   1.1  plunky 			/* count firm touches for middle-click */
   1335   1.1  plunky 			if (hid_get_udata(data, &touch.pressure) > sc->sc_firm)
   1336   1.1  plunky 				nf++;
   1337   1.1  plunky 
   1338   1.1  plunky 			break;
   1339   1.1  plunky 
   1340   1.1  plunky 		default:
   1341  1.12  bouyer 			CLR(sc->sc_smask, __BIT(id));
   1342   1.1  plunky 			break;
   1343   1.1  plunky 		}
   1344   1.1  plunky 
   1345   1.1  plunky 		sc->sc_az[id] = az;
   1346   1.1  plunky 		sc->sc_aw[id] = aw;
   1347  1.12  bouyer #undef sc_az
   1348  1.12  bouyer #undef sc_aw
   1349   1.1  plunky 	}
   1350   1.1  plunky 
   1351   1.1  plunky 	/*
   1352   1.1  plunky 	 * The mouse only has one click detector, and says left or right but
   1353   1.1  plunky 	 * never both. We convert multiple firm touches while clicking into
   1354   1.1  plunky 	 * a middle button press, and cancel any scroll effects while click
   1355   1.1  plunky 	 * is active.
   1356   1.1  plunky 	 */
   1357   1.1  plunky 	if (mb != 0) {
   1358   1.1  plunky 		if (sc->sc_mb != 0)
   1359   1.1  plunky 			mb = sc->sc_mb;
   1360   1.1  plunky 		else if (nf > 1)
   1361   1.1  plunky 			mb = __BIT(1);
   1362   1.1  plunky 
   1363   1.1  plunky 		sc->sc_smask = 0;
   1364   1.1  plunky 		dz = 0;
   1365   1.1  plunky 		dw = 0;
   1366   1.1  plunky 	}
   1367   1.1  plunky 
   1368   1.1  plunky 	if (dx != 0 || dy != 0 || dz != 0 || dw != 0 || mb != sc->sc_mb) {
   1369   1.1  plunky 		sc->sc_mb = mb;
   1370   1.1  plunky 
   1371   1.1  plunky 		s = spltty();
   1372   1.1  plunky 		wsmouse_input(sc->sc_wsmouse, mb,
   1373   1.1  plunky 		    dx, -dy, -dz, dw, WSMOUSE_INPUT_DELTA);
   1374   1.1  plunky 		splx(s);
   1375   1.1  plunky 	}
   1376   1.1  plunky }
   1377  1.12  bouyer 
   1378  1.12  bouyer /*
   1379  1.12  bouyer  * the Magic touch trackpad report (0x28), according to the Linux driver
   1380  1.12  bouyer  * written by Michael Poole and Chase Douglas, is variable length starting
   1381  1.12  bouyer  * with the fixed 24-bit header
   1382  1.12  bouyer  *
   1383  1.12  bouyer  *	button 1	1-bit
   1384  1.12  bouyer  *      unknown		5-bits
   1385  1.12  bouyer  *	timestamp	18-bits
   1386  1.12  bouyer  *
   1387  1.12  bouyer  * followed by (up to 5?) touch reports of 72-bits each
   1388  1.12  bouyer  *
   1389  1.12  bouyer  *	abs X		13-bits (signed)
   1390  1.12  bouyer  *	abs Y		13-bits (signed)
   1391  1.12  bouyer  * 	unknown		6-bits
   1392  1.12  bouyer  *	axis major	8-bits
   1393  1.12  bouyer  *	axis minor	8-bits
   1394  1.12  bouyer  *	pressure	6-bits
   1395  1.12  bouyer  *	id		4-bits
   1396  1.12  bouyer  *	angle		6-bits	(from E(0)->N(32)->W(64))
   1397  1.12  bouyer  *	unknown		4-bits
   1398  1.12  bouyer  *	phase		4-bits
   1399  1.12  bouyer  */
   1400  1.12  bouyer 
   1401  1.12  bouyer static const struct {
   1402  1.12  bouyer 	struct hid_location button;
   1403  1.12  bouyer 	struct hid_location timestamp;
   1404  1.12  bouyer } magict = {
   1405  1.12  bouyer 	.button = { .pos =  0, .size = 1 },
   1406  1.12  bouyer 	.timestamp = { .pos = 6, .size = 18 },
   1407  1.12  bouyer };
   1408  1.12  bouyer 
   1409  1.12  bouyer static const struct {
   1410  1.12  bouyer 	struct hid_location aX;
   1411  1.12  bouyer 	struct hid_location aY;
   1412  1.12  bouyer 	struct hid_location major;
   1413  1.12  bouyer 	struct hid_location minor;
   1414  1.12  bouyer 	struct hid_location pressure;
   1415  1.12  bouyer 	struct hid_location id;
   1416  1.12  bouyer 	struct hid_location angle;
   1417  1.12  bouyer 	struct hid_location unknown;
   1418  1.12  bouyer 	struct hid_location phase;
   1419  1.12  bouyer } toucht = {
   1420  1.12  bouyer 	.aX = { .pos = 0, .size = 13 },
   1421  1.12  bouyer 	.aY = { .pos = 13, .size = 13 },
   1422  1.12  bouyer 	.major = { .pos = 32, .size = 8 },
   1423  1.12  bouyer 	.minor = { .pos = 40, .size = 8 },
   1424  1.12  bouyer 	.pressure = { .pos = 48, .size = 6 },
   1425  1.12  bouyer 	.id = { .pos = 54, .size = 4 },
   1426  1.12  bouyer 	.angle = { .pos = 58, .size = 6 },
   1427  1.12  bouyer 	.unknown = { .pos = 64, .size = 4 },
   1428  1.12  bouyer 	.phase = { .pos = 68, .size = 4 },
   1429  1.12  bouyer };
   1430  1.12  bouyer 
   1431  1.12  bouyer /*
   1432  1.12  bouyer  * as for btmagic_input_magicm,
   1433  1.12  bouyer  * the phase of the touch starts at 0x01 as the finger is first detected
   1434  1.12  bouyer  * approaching the mouse, increasing to 0x04 while the finger is touching,
   1435  1.12  bouyer  * then increases towards 0x07 as the finger is lifted, and we get 0x00
   1436  1.12  bouyer  * when the touch is cancelled. The values below seem to be produced for
   1437  1.12  bouyer  * every touch, the others less consistently depending on how fast the
   1438  1.12  bouyer  * approach or departure is.
   1439  1.12  bouyer  *
   1440  1.12  bouyer  * In fact we ignore touches unless they are in the steady 0x04 phase.
   1441  1.12  bouyer  */
   1442  1.12  bouyer 
   1443  1.12  bouyer /* min and max values reported */
   1444  1.12  bouyer #define MAGICT_X_MIN	(-2910)
   1445  1.12  bouyer #define MAGICT_X_MAX	(3170)
   1446  1.12  bouyer #define MAGICT_Y_MIN	(-2565)
   1447  1.12  bouyer #define MAGICT_Y_MAX	(2455)
   1448  1.12  bouyer 
   1449  1.12  bouyer /*
   1450  1.12  bouyer  * area for detecting the buttons: divide in 3 areas on X,
   1451  1.12  bouyer  * below -1900 on y
   1452  1.12  bouyer  */
   1453  1.12  bouyer #define MAGICT_B_YMAX	(-1900)
   1454  1.12  bouyer #define MAGICT_B_XSIZE	((MAGICT_X_MAX - MAGICT_X_MIN) / 3)
   1455  1.12  bouyer #define MAGICT_B_X1MAX	(MAGICT_X_MIN + MAGICT_B_XSIZE)
   1456  1.12  bouyer #define MAGICT_B_X2MAX	(MAGICT_X_MIN + MAGICT_B_XSIZE * 2)
   1457  1.12  bouyer 
   1458  1.12  bouyer static void
   1459  1.12  bouyer btmagic_input_magict(struct btmagic_softc *sc, uint8_t *data, size_t len)
   1460  1.12  bouyer {
   1461  1.12  bouyer 	bool bpress;
   1462  1.12  bouyer 	uint32_t mb;
   1463  1.12  bouyer 	int id, ax, ay, tx, ty;
   1464  1.12  bouyer 	int dx, dy, dz, dw;
   1465  1.12  bouyer 	int s;
   1466  1.12  bouyer 
   1467  1.12  bouyer 	if (((len - 3) % 9) != 0)
   1468  1.12  bouyer 		return;
   1469  1.12  bouyer 
   1470  1.12  bouyer 	bpress = 0;
   1471  1.12  bouyer 	if (hid_get_udata(data, &magict.button))
   1472  1.12  bouyer 		bpress = 1;
   1473  1.12  bouyer 
   1474  1.12  bouyer 	dx = dy = dz = dw = 0;
   1475  1.12  bouyer 	mb = 0;
   1476  1.12  bouyer 
   1477  1.12  bouyer 	len = (len - 3) / 9;
   1478  1.12  bouyer 	for (data += 3; len-- > 0; data += 9) {
   1479  1.12  bouyer 		id = hid_get_udata(data, &toucht.id);
   1480  1.12  bouyer 		ax = hid_get_data(data, &toucht.aX);
   1481  1.12  bouyer 		ay = hid_get_data(data, &toucht.aY);
   1482  1.12  bouyer 
   1483  1.12  bouyer 		DPRINTF(sc,
   1484  1.12  bouyer 		    "btmagic_input_magicm: id %d ax %d ay %d phase %ld %s\n",
   1485  1.12  bouyer 		    id, ax, ay, hid_get_udata(data, &toucht.phase),
   1486  1.12  bouyer 		    bpress ? "button pressed" : "");
   1487  1.12  bouyer 
   1488  1.12  bouyer 		/*
   1489  1.12  bouyer 		 * a single touch is interpreted as a mouse move.
   1490  1.12  bouyer 		 * If a button is pressed, the touch in the button area
   1491  1.12  bouyer 		 * defined above defines the button; a second touch is
   1492  1.12  bouyer 		 * interpreted as a mouse move.
   1493  1.12  bouyer 		 */
   1494  1.12  bouyer 
   1495  1.12  bouyer 		switch (hid_get_udata(data, &toucht.phase)) {
   1496  1.12  bouyer 		case BTMAGIC_PHASE_CONT:
   1497  1.12  bouyer 			if (bpress) {
   1498  1.12  bouyer 				if (sc->sc_mb == 0 && ay < MAGICT_B_YMAX) {
   1499  1.12  bouyer 					/*
   1500  1.12  bouyer 					 * we have a new button press,
   1501  1.12  bouyer 					 * and this id tells which one
   1502  1.12  bouyer 					 */
   1503  1.12  bouyer 					if (ax < MAGICT_B_X1MAX)
   1504  1.12  bouyer 						mb = __BIT(0);
   1505  1.12  bouyer 					else if (ax > MAGICT_B_X2MAX)
   1506  1.12  bouyer 						mb = __BIT(2);
   1507  1.12  bouyer 					else
   1508  1.12  bouyer 						mb = __BIT(1);
   1509  1.12  bouyer 					sc->sc_mb_id = id;
   1510  1.12  bouyer 				} else {
   1511  1.12  bouyer 					/* keep previous state */
   1512  1.12  bouyer 					mb = sc->sc_mb;
   1513  1.12  bouyer 				}
   1514  1.12  bouyer 			} else {
   1515  1.12  bouyer 				/* no button pressed */
   1516  1.12  bouyer 				mb = 0;
   1517  1.12  bouyer 				sc->sc_mb_id = -1;
   1518  1.12  bouyer 			}
   1519  1.12  bouyer 			if (id == sc->sc_mb_id) {
   1520  1.12  bouyer 				/*
   1521  1.12  bouyer 				 * this id selects the button
   1522  1.12  bouyer 				 * ignore for move/scroll
   1523  1.12  bouyer 				 */
   1524  1.12  bouyer 				 continue;
   1525  1.12  bouyer 			}
   1526  1.12  bouyer 
   1527  1.12  bouyer 			tx = ax - sc->sc_ax[id];
   1528  1.12  bouyer 			ty = ay - sc->sc_ay[id];
   1529  1.12  bouyer 
   1530  1.12  bouyer 			if (ISSET(sc->sc_smask, __BIT(id))) {
   1531  1.12  bouyer 				if (sc->sc_nfingers == 1 || mb != 0) {
   1532  1.12  bouyer 					/* single finger moving */
   1533  1.12  bouyer 					dx += btmagic_scale(tx, &sc->sc_rx,
   1534  1.12  bouyer 					    sc->sc_resolution);
   1535  1.12  bouyer 					dy += btmagic_scale(ty, &sc->sc_ry,
   1536  1.12  bouyer 					    sc->sc_resolution);
   1537  1.12  bouyer 				} else {
   1538  1.12  bouyer 					/* scrolling fingers */
   1539  1.12  bouyer 					dz += btmagic_scale(ty, &sc->sc_rz,
   1540  1.12  bouyer 					    sc->sc_resolution / sc->sc_scale);
   1541  1.12  bouyer 					dw += btmagic_scale(tx, &sc->sc_rw,
   1542  1.12  bouyer 					    sc->sc_resolution / sc->sc_scale);
   1543  1.12  bouyer 				}
   1544  1.12  bouyer 			} else if (ay > MAGICT_B_YMAX) { /* new finger */
   1545  1.12  bouyer 				sc->sc_rx = 0;
   1546  1.12  bouyer 				sc->sc_ry = 0;
   1547  1.12  bouyer 				sc->sc_rz = 0;
   1548  1.12  bouyer 				sc->sc_rw = 0;
   1549  1.12  bouyer 
   1550  1.12  bouyer 				KASSERT(!ISSET(sc->sc_smask, __BIT(id)));
   1551  1.12  bouyer 				SET(sc->sc_smask, __BIT(id));
   1552  1.12  bouyer 				sc->sc_nfingers++;
   1553  1.12  bouyer 			}
   1554  1.12  bouyer 
   1555  1.12  bouyer 			break;
   1556  1.12  bouyer 		default:
   1557  1.12  bouyer 			if (ISSET(sc->sc_smask, __BIT(id))) {
   1558  1.12  bouyer 				CLR(sc->sc_smask, __BIT(id));
   1559  1.12  bouyer 				sc->sc_nfingers--;
   1560  1.12  bouyer 				KASSERT(sc->sc_nfingers >= 0);
   1561  1.12  bouyer 			}
   1562  1.12  bouyer 			break;
   1563  1.12  bouyer 		}
   1564  1.12  bouyer 
   1565  1.12  bouyer 		sc->sc_ax[id] = ax;
   1566  1.12  bouyer 		sc->sc_ay[id] = ay;
   1567  1.12  bouyer 	}
   1568  1.12  bouyer 
   1569  1.12  bouyer 	if (dx != 0 || dy != 0 || dz != 0 || dw != 0 || mb != sc->sc_mb) {
   1570  1.12  bouyer 		sc->sc_mb = mb;
   1571  1.12  bouyer 
   1572  1.12  bouyer 		s = spltty();
   1573  1.12  bouyer 		wsmouse_input(sc->sc_wsmouse, mb,
   1574  1.12  bouyer 		    dx, dy, -dz, dw, WSMOUSE_INPUT_DELTA);
   1575  1.12  bouyer 		splx(s);
   1576  1.12  bouyer 	}
   1577  1.12  bouyer }
   1578