Home | History | Annotate | Line # | Download | only in bluetooth
btms.c revision 1.2
      1 /*	$NetBSD: btms.c,v 1.2 2006/10/12 01:30:55 christos 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.2 2006/10/12 01:30:55 christos 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 	struct device		*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 
     85 /* autoconf(9) methods */
     86 static int	btms_match(struct device *, struct cfdata *, void *);
     87 static void	btms_attach(struct device *, struct device *, void *);
     88 static int	btms_detach(struct device *, int);
     89 
     90 CFATTACH_DECL(btms, sizeof(struct btms_softc),
     91     btms_match, btms_attach, btms_detach, NULL);
     92 
     93 /* wsmouse(4) accessops */
     94 static int	btms_wsmouse_enable(void *);
     95 static int	btms_wsmouse_ioctl(void *, unsigned long, caddr_t, int, struct lwp *);
     96 static void	btms_wsmouse_disable(void *);
     97 
     98 static const struct wsmouse_accessops btms_wsmouse_accessops = {
     99 	btms_wsmouse_enable,
    100 	btms_wsmouse_ioctl,
    101 	btms_wsmouse_disable,
    102 };
    103 
    104 /* bthid methods */
    105 static void btms_input(struct bthidev *, uint8_t *, int);
    106 
    107 /*****************************************************************************
    108  *
    109  *	btms autoconf(9) routines
    110  */
    111 
    112 static int
    113 btms_match(struct device *parent __unused, struct cfdata *match __unused,
    114     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(struct device *parent __unused, struct device *self, void *aux)
    127 {
    128 	struct btms_softc *sc = (struct btms_softc *)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 		printf("\n%s: X report 0x%04x not supported\n",
    148 		       sc->sc_hidev.sc_dev.dv_xname, flags);
    149 
    150 		return;
    151 	}
    152 
    153 	/* control the vertical */
    154 	hl = hid_locate(ba->ba_desc,
    155 			ba->ba_dlen,
    156 			HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
    157 			ba->ba_id,
    158 			hid_input,
    159 			&sc->sc_loc_y,
    160 			&flags);
    161 
    162 	if (hl == 0 || NOTMOUSE(flags)) {
    163 		printf("\n%s: Y report 0x%04x not supported\n",
    164 			sc->sc_hidev.sc_dev.dv_xname, flags);
    165 
    166 		return;
    167 	}
    168 
    169 	/* Try the wheel first as the Z activator since it's tradition. */
    170 	hl = hid_locate(ba->ba_desc,
    171 			ba->ba_dlen,
    172 			HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_WHEEL),
    173 			ba->ba_id,
    174 			hid_input,
    175 			&sc->sc_loc_z,
    176 			&flags);
    177 
    178 	zloc = &sc->sc_loc_z;
    179 	if (hl) {
    180 		if (NOTMOUSE(flags)) {
    181 			printf("\n%s: Wheel report 0x%04x not supported\n",
    182 				sc->sc_hidev.sc_dev.dv_xname, flags);
    183 
    184 			/* ignore Bad Z coord */
    185 			sc->sc_loc_z.size = 0;
    186 		} else {
    187 			sc->sc_flags |= BTMS_HASZ;
    188 			/* Wheels need the Z axis reversed. */
    189 			sc->sc_flags ^= BTMS_REVZ;
    190 			/* Put Z on the W coordinate */
    191 			zloc = &sc->sc_loc_w;
    192 		}
    193 	}
    194 
    195 	hl = hid_locate(ba->ba_desc,
    196 			ba->ba_dlen,
    197 			HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Z),
    198 			ba->ba_id,
    199 			hid_input,
    200 			zloc,
    201 			&flags);
    202 
    203 	if (hl) {
    204 		if (NOTMOUSE(flags))
    205 			zloc->size = 0;	/* ignore Z */
    206 		else
    207 			sc->sc_flags |= BTMS_HASZ;
    208 	}
    209 
    210 	for (i = 1 ; i <= MAX_BUTTONS ; i++) {
    211 		hl = hid_locate(ba->ba_desc,
    212 				ba->ba_dlen,
    213 				HID_USAGE2(HUP_BUTTON, i),
    214 				ba->ba_id,
    215 				hid_input,
    216 				&sc->sc_loc_button[i - 1],
    217 				NULL);
    218 
    219 		if (hl == 0)
    220 			break;
    221 	}
    222 	sc->sc_num_buttons = i - 1;
    223 
    224 	aprint_normal(": %d button%s%s.\n",
    225 			sc->sc_num_buttons,
    226 			sc->sc_num_buttons == 1 ? "" : "s",
    227 			sc->sc_flags & BTMS_HASZ ? " and Z dir" : "");
    228 
    229 	wsma.accessops = &btms_wsmouse_accessops;
    230 	wsma.accesscookie = sc;
    231 
    232 	sc->sc_wsmouse = config_found((struct device *)sc, &wsma, wsmousedevprint);
    233 }
    234 
    235 static int
    236 btms_detach(struct device *self, int flags)
    237 {
    238 	struct btms_softc *sc = (struct btms_softc *)self;
    239 	int err = 0;
    240 
    241 	if (sc->sc_wsmouse != NULL) {
    242 		err = config_detach(sc->sc_wsmouse, flags);
    243 		sc->sc_wsmouse = NULL;
    244 	}
    245 
    246 	return err;
    247 }
    248 
    249 /*****************************************************************************
    250  *
    251  *	wsmouse(4) accessops
    252  */
    253 
    254 static int
    255 btms_wsmouse_enable(void *self)
    256 {
    257 	struct btms_softc *sc = (struct btms_softc *)self;
    258 
    259 	if (sc->sc_enabled)
    260 		return EBUSY;
    261 
    262 	sc->sc_enabled = 1;
    263 	return 0;
    264 }
    265 
    266 static int
    267 btms_wsmouse_ioctl(void *self __unused, unsigned long cmd, caddr_t data,
    268     int flag __unused, struct lwp *l __unused)
    269 {
    270 	/* struct btms_softc *sc = (struct btms_softc *)self; */
    271 
    272 	switch (cmd) {
    273 	case WSMOUSEIO_GTYPE:
    274 		*(uint *)data = WSMOUSE_TYPE_BLUETOOTH;
    275 		break;
    276 
    277 	default:
    278 		return EPASSTHROUGH;
    279 	}
    280 
    281 	return 0;
    282 }
    283 
    284 static void
    285 btms_wsmouse_disable(void *self)
    286 {
    287 	struct btms_softc *sc = (struct btms_softc *)self;
    288 
    289 	sc->sc_enabled = 0;
    290 }
    291 
    292 /*****************************************************************************
    293  *
    294  *	btms input routine, called from our parent
    295  */
    296 
    297 static void
    298 btms_input(struct bthidev *self, uint8_t *data, int len __unused)
    299 {
    300 	struct btms_softc *sc = (struct btms_softc *)self;
    301 	int dx, dy, dz, dw;
    302 	uint32_t buttons;
    303 	int i, s;
    304 
    305 	if (sc->sc_wsmouse == NULL || sc->sc_enabled == 0)
    306 		return;
    307 
    308 	dx =  hid_get_data(data, &sc->sc_loc_x);
    309 	dy = -hid_get_data(data, &sc->sc_loc_y);
    310 	dz =  hid_get_data(data, &sc->sc_loc_z);
    311 	dw =  hid_get_data(data, &sc->sc_loc_w);
    312 
    313 	if (sc->sc_flags & BTMS_REVZ)
    314 		dz = -dz;
    315 
    316 	buttons = 0;
    317 	for (i = 0 ; i < sc->sc_num_buttons ; i++)
    318 		if (hid_get_data(data, &sc->sc_loc_button[i]))
    319 			buttons |= BUTTON(i);
    320 
    321 	if (dx != 0 || dy != 0 || dz != 0 || dw != 0 || buttons != sc->sc_buttons) {
    322 		sc->sc_buttons = buttons;
    323 
    324 		s = spltty();
    325 		wsmouse_input_xyzw(sc->sc_wsmouse, buttons,
    326 				   dx, dy, dz, dw,
    327 				   WSMOUSE_INPUT_DELTA);
    328 		splx(s);
    329 	}
    330 }
    331