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