Home | History | Annotate | Line # | Download | only in bluetooth
btms.c revision 1.8
      1  1.8      cube /*	$NetBSD: btms.c,v 1.8 2008/09/09 03:54:56 cube Exp $	*/
      2  1.8      cube 
      3  1.8      cube /*
      4  1.8      cube  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  1.8      cube  * All rights reserved.
      6  1.8      cube  *
      7  1.8      cube  * This code is derived from software contributed to The NetBSD Foundation
      8  1.8      cube  * by Lennart Augustsson (lennart (at) augustsson.net) at
      9  1.8      cube  * Carlstedt Research & Technology.
     10  1.8      cube  *
     11  1.8      cube  * Redistribution and use in source and binary forms, with or without
     12  1.8      cube  * modification, are permitted provided that the following conditions
     13  1.8      cube  * are met:
     14  1.8      cube  * 1. Redistributions of source code must retain the above copyright
     15  1.8      cube  *    notice, this list of conditions and the following disclaimer.
     16  1.8      cube  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.8      cube  *    notice, this list of conditions and the following disclaimer in the
     18  1.8      cube  *    documentation and/or other materials provided with the distribution.
     19  1.8      cube  *
     20  1.8      cube  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  1.8      cube  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  1.8      cube  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  1.8      cube  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  1.8      cube  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  1.8      cube  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  1.8      cube  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  1.8      cube  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  1.8      cube  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  1.8      cube  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  1.8      cube  * POSSIBILITY OF SUCH DAMAGE.
     31  1.8      cube  */
     32  1.1   gdamore 
     33  1.1   gdamore /*-
     34  1.1   gdamore  * Copyright (c) 2006 Itronix Inc.
     35  1.1   gdamore  * All rights reserved.
     36  1.1   gdamore  *
     37  1.1   gdamore  * Written by Iain Hibbert for Itronix Inc.
     38  1.1   gdamore  *
     39  1.1   gdamore  * Redistribution and use in source and binary forms, with or without
     40  1.1   gdamore  * modification, are permitted provided that the following conditions
     41  1.1   gdamore  * are met:
     42  1.1   gdamore  * 1. Redistributions of source code must retain the above copyright
     43  1.1   gdamore  *    notice, this list of conditions and the following disclaimer.
     44  1.1   gdamore  * 2. Redistributions in binary form must reproduce the above copyright
     45  1.1   gdamore  *    notice, this list of conditions and the following disclaimer in the
     46  1.1   gdamore  *    documentation and/or other materials provided with the distribution.
     47  1.1   gdamore  * 3. The name of Itronix Inc. may not be used to endorse
     48  1.1   gdamore  *    or promote products derived from this software without specific
     49  1.1   gdamore  *    prior written permission.
     50  1.1   gdamore  *
     51  1.1   gdamore  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
     52  1.1   gdamore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     53  1.1   gdamore  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     54  1.1   gdamore  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
     55  1.1   gdamore  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     56  1.1   gdamore  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     57  1.1   gdamore  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     58  1.1   gdamore  * ON ANY THEORY OF LIABILITY, WHETHER IN
     59  1.1   gdamore  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     60  1.1   gdamore  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     61  1.1   gdamore  * POSSIBILITY OF SUCH DAMAGE.
     62  1.1   gdamore  */
     63  1.1   gdamore 
     64  1.1   gdamore /*
     65  1.1   gdamore  * based on dev/usb/ums.c
     66  1.1   gdamore  */
     67  1.1   gdamore 
     68  1.1   gdamore #include <sys/cdefs.h>
     69  1.8      cube __KERNEL_RCSID(0, "$NetBSD: btms.c,v 1.8 2008/09/09 03:54:56 cube Exp $");
     70  1.1   gdamore 
     71  1.1   gdamore #include <sys/param.h>
     72  1.1   gdamore #include <sys/conf.h>
     73  1.1   gdamore #include <sys/device.h>
     74  1.1   gdamore #include <sys/proc.h>
     75  1.1   gdamore #include <sys/systm.h>
     76  1.1   gdamore 
     77  1.1   gdamore #include <netbt/bluetooth.h>
     78  1.1   gdamore 
     79  1.1   gdamore #include <dev/bluetooth/bthid.h>
     80  1.1   gdamore #include <dev/bluetooth/bthidev.h>
     81  1.1   gdamore 
     82  1.1   gdamore #include <dev/usb/hid.h>
     83  1.1   gdamore #include <dev/usb/usb.h>
     84  1.1   gdamore #include <dev/usb/usbhid.h>
     85  1.1   gdamore 
     86  1.1   gdamore #include <dev/wscons/wsconsio.h>
     87  1.1   gdamore #include <dev/wscons/wsmousevar.h>
     88  1.1   gdamore 
     89  1.1   gdamore #define MAX_BUTTONS	31
     90  1.1   gdamore #define BUTTON(n)	(1 << (((n) == 1 || (n) == 2) ? 3 - (n) : (n)))
     91  1.1   gdamore #define NOTMOUSE(f)	(((f) & (HIO_CONST | HIO_RELATIVE)) != HIO_RELATIVE)
     92  1.1   gdamore 
     93  1.1   gdamore struct btms_softc {
     94  1.1   gdamore 	struct bthidev		 sc_hidev;	/* device+ */
     95  1.1   gdamore 
     96  1.7    plunky 	device_t		 sc_wsmouse;	/* child */
     97  1.1   gdamore 	int			 sc_enabled;
     98  1.1   gdamore 	uint16_t		 sc_flags;
     99  1.1   gdamore 
    100  1.1   gdamore 	/* locators */
    101  1.1   gdamore 	struct hid_location	 sc_loc_x;
    102  1.1   gdamore 	struct hid_location	 sc_loc_y;
    103  1.1   gdamore 	struct hid_location	 sc_loc_z;
    104  1.1   gdamore 	struct hid_location	 sc_loc_w;
    105  1.1   gdamore 	struct hid_location	 sc_loc_button[MAX_BUTTONS];
    106  1.1   gdamore 
    107  1.1   gdamore 	int			 sc_num_buttons;
    108  1.1   gdamore 	uint32_t		 sc_buttons;
    109  1.1   gdamore };
    110  1.1   gdamore 
    111  1.1   gdamore /* sc_flags */
    112  1.1   gdamore #define BTMS_REVZ		(1 << 0)	/* reverse Z direction */
    113  1.1   gdamore #define BTMS_HASZ		(1 << 1)	/* has Z direction */
    114  1.3    plunky #define BTMS_HASW		(1 << 2)	/* has W direction */
    115  1.1   gdamore 
    116  1.1   gdamore /* autoconf(9) methods */
    117  1.7    plunky static int	btms_match(device_t, struct cfdata *, void *);
    118  1.7    plunky static void	btms_attach(device_t, device_t, void *);
    119  1.7    plunky static int	btms_detach(device_t, int);
    120  1.1   gdamore 
    121  1.7    plunky CFATTACH_DECL_NEW(btms, sizeof(struct btms_softc),
    122  1.1   gdamore     btms_match, btms_attach, btms_detach, NULL);
    123  1.1   gdamore 
    124  1.1   gdamore /* wsmouse(4) accessops */
    125  1.1   gdamore static int	btms_wsmouse_enable(void *);
    126  1.6  christos static int	btms_wsmouse_ioctl(void *, unsigned long, void *, int, struct lwp *);
    127  1.1   gdamore static void	btms_wsmouse_disable(void *);
    128  1.1   gdamore 
    129  1.1   gdamore static const struct wsmouse_accessops btms_wsmouse_accessops = {
    130  1.1   gdamore 	btms_wsmouse_enable,
    131  1.1   gdamore 	btms_wsmouse_ioctl,
    132  1.1   gdamore 	btms_wsmouse_disable,
    133  1.1   gdamore };
    134  1.1   gdamore 
    135  1.1   gdamore /* bthid methods */
    136  1.1   gdamore static void btms_input(struct bthidev *, uint8_t *, int);
    137  1.1   gdamore 
    138  1.1   gdamore /*****************************************************************************
    139  1.1   gdamore  *
    140  1.1   gdamore  *	btms autoconf(9) routines
    141  1.1   gdamore  */
    142  1.1   gdamore 
    143  1.1   gdamore static int
    144  1.7    plunky btms_match(device_t parent, struct cfdata *match, void *aux)
    145  1.1   gdamore {
    146  1.1   gdamore 	struct bthidev_attach_args *ba = aux;
    147  1.1   gdamore 
    148  1.1   gdamore 	if (hid_is_collection(ba->ba_desc, ba->ba_dlen, ba->ba_id,
    149  1.1   gdamore 			    HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)))
    150  1.1   gdamore 		return 1;
    151  1.1   gdamore 
    152  1.1   gdamore 	return 0;
    153  1.1   gdamore }
    154  1.1   gdamore 
    155  1.1   gdamore static void
    156  1.7    plunky btms_attach(device_t parent, device_t self, void *aux)
    157  1.1   gdamore {
    158  1.7    plunky 	struct btms_softc *sc = device_private(self);
    159  1.1   gdamore 	struct bthidev_attach_args *ba = aux;
    160  1.1   gdamore 	struct wsmousedev_attach_args wsma;
    161  1.1   gdamore 	struct hid_location *zloc;
    162  1.1   gdamore 	uint32_t flags;
    163  1.1   gdamore 	int i, hl;
    164  1.1   gdamore 
    165  1.1   gdamore 	ba->ba_input = btms_input;
    166  1.1   gdamore 
    167  1.1   gdamore 	/* control the horizontal */
    168  1.1   gdamore 	hl = hid_locate(ba->ba_desc,
    169  1.1   gdamore 			ba->ba_dlen,
    170  1.1   gdamore 			HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
    171  1.1   gdamore 			ba->ba_id,
    172  1.1   gdamore 			hid_input,
    173  1.1   gdamore 			&sc->sc_loc_x,
    174  1.1   gdamore 			&flags);
    175  1.1   gdamore 
    176  1.1   gdamore 	if (hl == 0 || NOTMOUSE(flags)) {
    177  1.7    plunky 		aprint_error("X report 0x%04x not supported\n", flags);
    178  1.1   gdamore 		return;
    179  1.1   gdamore 	}
    180  1.1   gdamore 
    181  1.1   gdamore 	/* control the vertical */
    182  1.1   gdamore 	hl = hid_locate(ba->ba_desc,
    183  1.1   gdamore 			ba->ba_dlen,
    184  1.1   gdamore 			HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
    185  1.1   gdamore 			ba->ba_id,
    186  1.1   gdamore 			hid_input,
    187  1.1   gdamore 			&sc->sc_loc_y,
    188  1.1   gdamore 			&flags);
    189  1.1   gdamore 
    190  1.1   gdamore 	if (hl == 0 || NOTMOUSE(flags)) {
    191  1.7    plunky 		aprint_error("Y report 0x%04x not supported\n", flags);
    192  1.1   gdamore 		return;
    193  1.1   gdamore 	}
    194  1.1   gdamore 
    195  1.1   gdamore 	/* Try the wheel first as the Z activator since it's tradition. */
    196  1.1   gdamore 	hl = hid_locate(ba->ba_desc,
    197  1.1   gdamore 			ba->ba_dlen,
    198  1.1   gdamore 			HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_WHEEL),
    199  1.1   gdamore 			ba->ba_id,
    200  1.1   gdamore 			hid_input,
    201  1.1   gdamore 			&sc->sc_loc_z,
    202  1.1   gdamore 			&flags);
    203  1.1   gdamore 
    204  1.1   gdamore 	zloc = &sc->sc_loc_z;
    205  1.1   gdamore 	if (hl) {
    206  1.1   gdamore 		if (NOTMOUSE(flags)) {
    207  1.7    plunky 			aprint_error("Wheel report 0x%04x ignored\n", flags);
    208  1.1   gdamore 
    209  1.1   gdamore 			/* ignore Bad Z coord */
    210  1.1   gdamore 			sc->sc_loc_z.size = 0;
    211  1.1   gdamore 		} else {
    212  1.1   gdamore 			sc->sc_flags |= BTMS_HASZ;
    213  1.1   gdamore 			/* Wheels need the Z axis reversed. */
    214  1.1   gdamore 			sc->sc_flags ^= BTMS_REVZ;
    215  1.1   gdamore 			/* Put Z on the W coordinate */
    216  1.1   gdamore 			zloc = &sc->sc_loc_w;
    217  1.1   gdamore 		}
    218  1.1   gdamore 	}
    219  1.1   gdamore 
    220  1.1   gdamore 	hl = hid_locate(ba->ba_desc,
    221  1.1   gdamore 			ba->ba_dlen,
    222  1.1   gdamore 			HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Z),
    223  1.1   gdamore 			ba->ba_id,
    224  1.1   gdamore 			hid_input,
    225  1.1   gdamore 			zloc,
    226  1.1   gdamore 			&flags);
    227  1.1   gdamore 
    228  1.3    plunky 	/*
    229  1.3    plunky 	 * The horizontal component of the scrollball can also be given by
    230  1.3    plunky 	 * Application Control Pan in the Consumer page, so if we didnt see
    231  1.3    plunky 	 * any Z then check that.
    232  1.3    plunky 	 */
    233  1.3    plunky 	if (!hl) {
    234  1.3    plunky 		hl = hid_locate(ba->ba_desc,
    235  1.3    plunky 				ba->ba_dlen,
    236  1.3    plunky 				HID_USAGE2(HUP_CONSUMER, HUC_AC_PAN),
    237  1.3    plunky 				ba->ba_id,
    238  1.3    plunky 				hid_input,
    239  1.3    plunky 				zloc,
    240  1.3    plunky 				&flags);
    241  1.3    plunky 	}
    242  1.3    plunky 
    243  1.1   gdamore 	if (hl) {
    244  1.1   gdamore 		if (NOTMOUSE(flags))
    245  1.1   gdamore 			zloc->size = 0;	/* ignore Z */
    246  1.3    plunky 		else {
    247  1.3    plunky 			if (sc->sc_flags & BTMS_HASZ)
    248  1.3    plunky 				sc->sc_flags |= BTMS_HASW;
    249  1.3    plunky 			else
    250  1.3    plunky 				sc->sc_flags |= BTMS_HASZ;
    251  1.3    plunky 		}
    252  1.1   gdamore 	}
    253  1.1   gdamore 
    254  1.1   gdamore 	for (i = 1 ; i <= MAX_BUTTONS ; i++) {
    255  1.1   gdamore 		hl = hid_locate(ba->ba_desc,
    256  1.1   gdamore 				ba->ba_dlen,
    257  1.1   gdamore 				HID_USAGE2(HUP_BUTTON, i),
    258  1.1   gdamore 				ba->ba_id,
    259  1.1   gdamore 				hid_input,
    260  1.1   gdamore 				&sc->sc_loc_button[i - 1],
    261  1.1   gdamore 				NULL);
    262  1.1   gdamore 
    263  1.1   gdamore 		if (hl == 0)
    264  1.1   gdamore 			break;
    265  1.1   gdamore 	}
    266  1.1   gdamore 	sc->sc_num_buttons = i - 1;
    267  1.1   gdamore 
    268  1.3    plunky 	aprint_normal(": %d button%s%s%s%s.\n",
    269  1.1   gdamore 			sc->sc_num_buttons,
    270  1.1   gdamore 			sc->sc_num_buttons == 1 ? "" : "s",
    271  1.3    plunky 			sc->sc_flags & BTMS_HASW ? ", W" : "",
    272  1.3    plunky 			sc->sc_flags & BTMS_HASZ ? " and Z dir" : "",
    273  1.3    plunky 			sc->sc_flags & BTMS_HASW ? "s" : "");
    274  1.1   gdamore 
    275  1.1   gdamore 	wsma.accessops = &btms_wsmouse_accessops;
    276  1.1   gdamore 	wsma.accesscookie = sc;
    277  1.1   gdamore 
    278  1.7    plunky 	sc->sc_wsmouse = config_found(self, &wsma, wsmousedevprint);
    279  1.1   gdamore }
    280  1.1   gdamore 
    281  1.1   gdamore static int
    282  1.7    plunky btms_detach(device_t self, int flags)
    283  1.1   gdamore {
    284  1.7    plunky 	struct btms_softc *sc = device_private(self);
    285  1.1   gdamore 	int err = 0;
    286  1.1   gdamore 
    287  1.1   gdamore 	if (sc->sc_wsmouse != NULL) {
    288  1.1   gdamore 		err = config_detach(sc->sc_wsmouse, flags);
    289  1.1   gdamore 		sc->sc_wsmouse = NULL;
    290  1.1   gdamore 	}
    291  1.1   gdamore 
    292  1.1   gdamore 	return err;
    293  1.1   gdamore }
    294  1.1   gdamore 
    295  1.1   gdamore /*****************************************************************************
    296  1.1   gdamore  *
    297  1.1   gdamore  *	wsmouse(4) accessops
    298  1.1   gdamore  */
    299  1.1   gdamore 
    300  1.1   gdamore static int
    301  1.1   gdamore btms_wsmouse_enable(void *self)
    302  1.1   gdamore {
    303  1.7    plunky 	struct btms_softc *sc = self;
    304  1.1   gdamore 
    305  1.1   gdamore 	if (sc->sc_enabled)
    306  1.1   gdamore 		return EBUSY;
    307  1.1   gdamore 
    308  1.1   gdamore 	sc->sc_enabled = 1;
    309  1.1   gdamore 	return 0;
    310  1.1   gdamore }
    311  1.1   gdamore 
    312  1.1   gdamore static int
    313  1.6  christos btms_wsmouse_ioctl(void *self, unsigned long cmd, void *data,
    314  1.5  christos     int flag, struct lwp *l)
    315  1.1   gdamore {
    316  1.7    plunky 	/* struct btms_softc *sc = self; */
    317  1.1   gdamore 
    318  1.1   gdamore 	switch (cmd) {
    319  1.1   gdamore 	case WSMOUSEIO_GTYPE:
    320  1.1   gdamore 		*(uint *)data = WSMOUSE_TYPE_BLUETOOTH;
    321  1.1   gdamore 		break;
    322  1.1   gdamore 
    323  1.1   gdamore 	default:
    324  1.1   gdamore 		return EPASSTHROUGH;
    325  1.1   gdamore 	}
    326  1.1   gdamore 
    327  1.1   gdamore 	return 0;
    328  1.1   gdamore }
    329  1.1   gdamore 
    330  1.1   gdamore static void
    331  1.1   gdamore btms_wsmouse_disable(void *self)
    332  1.1   gdamore {
    333  1.7    plunky 	struct btms_softc *sc = self;
    334  1.1   gdamore 
    335  1.1   gdamore 	sc->sc_enabled = 0;
    336  1.1   gdamore }
    337  1.1   gdamore 
    338  1.1   gdamore /*****************************************************************************
    339  1.1   gdamore  *
    340  1.1   gdamore  *	btms input routine, called from our parent
    341  1.1   gdamore  */
    342  1.1   gdamore 
    343  1.1   gdamore static void
    344  1.5  christos btms_input(struct bthidev *self, uint8_t *data, int len)
    345  1.1   gdamore {
    346  1.1   gdamore 	struct btms_softc *sc = (struct btms_softc *)self;
    347  1.1   gdamore 	int dx, dy, dz, dw;
    348  1.1   gdamore 	uint32_t buttons;
    349  1.1   gdamore 	int i, s;
    350  1.1   gdamore 
    351  1.1   gdamore 	if (sc->sc_wsmouse == NULL || sc->sc_enabled == 0)
    352  1.1   gdamore 		return;
    353  1.1   gdamore 
    354  1.1   gdamore 	dx =  hid_get_data(data, &sc->sc_loc_x);
    355  1.1   gdamore 	dy = -hid_get_data(data, &sc->sc_loc_y);
    356  1.1   gdamore 	dz =  hid_get_data(data, &sc->sc_loc_z);
    357  1.1   gdamore 	dw =  hid_get_data(data, &sc->sc_loc_w);
    358  1.1   gdamore 
    359  1.1   gdamore 	if (sc->sc_flags & BTMS_REVZ)
    360  1.1   gdamore 		dz = -dz;
    361  1.1   gdamore 
    362  1.1   gdamore 	buttons = 0;
    363  1.1   gdamore 	for (i = 0 ; i < sc->sc_num_buttons ; i++)
    364  1.1   gdamore 		if (hid_get_data(data, &sc->sc_loc_button[i]))
    365  1.1   gdamore 			buttons |= BUTTON(i);
    366  1.1   gdamore 
    367  1.1   gdamore 	if (dx != 0 || dy != 0 || dz != 0 || dw != 0 || buttons != sc->sc_buttons) {
    368  1.1   gdamore 		sc->sc_buttons = buttons;
    369  1.1   gdamore 
    370  1.1   gdamore 		s = spltty();
    371  1.4    plunky 		wsmouse_input(sc->sc_wsmouse,
    372  1.4    plunky 				buttons,
    373  1.4    plunky 				dx, dy, dz, dw,
    374  1.4    plunky 				WSMOUSE_INPUT_DELTA);
    375  1.1   gdamore 		splx(s);
    376  1.1   gdamore 	}
    377  1.1   gdamore }
    378