xref: /netbsd-src/sys/dev/bluetooth/btms.c (revision c7fb772b85b2b5d4cfb282f868f454b4701534fd)
1*c7fb772bSthorpej /*	$NetBSD: btms.c,v 1.15 2021/08/07 16:19:09 thorpej Exp $	*/
265ce3552Scube 
365ce3552Scube /*
465ce3552Scube  * Copyright (c) 1998 The NetBSD Foundation, Inc.
565ce3552Scube  * All rights reserved.
665ce3552Scube  *
765ce3552Scube  * This code is derived from software contributed to The NetBSD Foundation
865ce3552Scube  * by Lennart Augustsson (lennart@augustsson.net) at
965ce3552Scube  * Carlstedt Research & Technology.
1065ce3552Scube  *
1165ce3552Scube  * Redistribution and use in source and binary forms, with or without
1265ce3552Scube  * modification, are permitted provided that the following conditions
1365ce3552Scube  * are met:
1465ce3552Scube  * 1. Redistributions of source code must retain the above copyright
1565ce3552Scube  *    notice, this list of conditions and the following disclaimer.
1665ce3552Scube  * 2. Redistributions in binary form must reproduce the above copyright
1765ce3552Scube  *    notice, this list of conditions and the following disclaimer in the
1865ce3552Scube  *    documentation and/or other materials provided with the distribution.
1965ce3552Scube  *
2065ce3552Scube  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2165ce3552Scube  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2265ce3552Scube  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2365ce3552Scube  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2465ce3552Scube  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2565ce3552Scube  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2665ce3552Scube  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2765ce3552Scube  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2865ce3552Scube  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2965ce3552Scube  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3065ce3552Scube  * POSSIBILITY OF SUCH DAMAGE.
3165ce3552Scube  */
32a5c89047Sgdamore 
33a5c89047Sgdamore /*-
34a5c89047Sgdamore  * Copyright (c) 2006 Itronix Inc.
35a5c89047Sgdamore  * All rights reserved.
36a5c89047Sgdamore  *
37a5c89047Sgdamore  * Written by Iain Hibbert for Itronix Inc.
38a5c89047Sgdamore  *
39a5c89047Sgdamore  * Redistribution and use in source and binary forms, with or without
40a5c89047Sgdamore  * modification, are permitted provided that the following conditions
41a5c89047Sgdamore  * are met:
42a5c89047Sgdamore  * 1. Redistributions of source code must retain the above copyright
43a5c89047Sgdamore  *    notice, this list of conditions and the following disclaimer.
44a5c89047Sgdamore  * 2. Redistributions in binary form must reproduce the above copyright
45a5c89047Sgdamore  *    notice, this list of conditions and the following disclaimer in the
46a5c89047Sgdamore  *    documentation and/or other materials provided with the distribution.
47a5c89047Sgdamore  * 3. The name of Itronix Inc. may not be used to endorse
48a5c89047Sgdamore  *    or promote products derived from this software without specific
49a5c89047Sgdamore  *    prior written permission.
50a5c89047Sgdamore  *
51a5c89047Sgdamore  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
52a5c89047Sgdamore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
53a5c89047Sgdamore  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
54a5c89047Sgdamore  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
55a5c89047Sgdamore  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
56a5c89047Sgdamore  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
57a5c89047Sgdamore  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
58a5c89047Sgdamore  * ON ANY THEORY OF LIABILITY, WHETHER IN
59a5c89047Sgdamore  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
60a5c89047Sgdamore  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
61a5c89047Sgdamore  * POSSIBILITY OF SUCH DAMAGE.
62a5c89047Sgdamore  */
63a5c89047Sgdamore 
64a5c89047Sgdamore /*
65a5c89047Sgdamore  * based on dev/usb/ums.c
66a5c89047Sgdamore  */
67a5c89047Sgdamore 
68a5c89047Sgdamore #include <sys/cdefs.h>
69*c7fb772bSthorpej __KERNEL_RCSID(0, "$NetBSD: btms.c,v 1.15 2021/08/07 16:19:09 thorpej Exp $");
70a5c89047Sgdamore 
71a5c89047Sgdamore #include <sys/param.h>
72a5c89047Sgdamore #include <sys/conf.h>
73a5c89047Sgdamore #include <sys/device.h>
74a5c89047Sgdamore #include <sys/proc.h>
75a5c89047Sgdamore #include <sys/systm.h>
76a5c89047Sgdamore 
77a5c89047Sgdamore #include <netbt/bluetooth.h>
78a5c89047Sgdamore 
79a5c89047Sgdamore #include <dev/bluetooth/bthid.h>
80a5c89047Sgdamore #include <dev/bluetooth/bthidev.h>
81a5c89047Sgdamore 
82404d04cdSbouyer #include <dev/hid/hid.h>
83a5c89047Sgdamore #include <dev/usb/usb.h>
84a5c89047Sgdamore #include <dev/usb/usbhid.h>
85a5c89047Sgdamore 
86a5c89047Sgdamore #include <dev/wscons/wsconsio.h>
87a5c89047Sgdamore #include <dev/wscons/wsmousevar.h>
88a5c89047Sgdamore 
89fa5e93a1Snonaka #ifdef BTMS_DEBUG
90fa5e93a1Snonaka int btms_debug = 0;
91fa5e93a1Snonaka #define	BTMSDBG(s)	if (btms_debug) printf s
92fa5e93a1Snonaka #define	BTMSDBGN(n,s)	if (btms_debug > (n)) printf s
93fa5e93a1Snonaka #else
94fa5e93a1Snonaka #define	BTMSDBG(s)
95fa5e93a1Snonaka #define	BTMSDBGN(n,s)
96fa5e93a1Snonaka #endif
97fa5e93a1Snonaka 
98a5c89047Sgdamore #define MAX_BUTTONS	31
99a5c89047Sgdamore #define BUTTON(n)	(1 << (((n) == 1 || (n) == 2) ? 3 - (n) : (n)))
100a5c89047Sgdamore #define NOTMOUSE(f)	(((f) & (HIO_CONST | HIO_RELATIVE)) != HIO_RELATIVE)
101a5c89047Sgdamore 
102a5c89047Sgdamore struct btms_softc {
103a5c89047Sgdamore 	struct bthidev		 sc_hidev;	/* device+ */
104a5c89047Sgdamore 
105edb74239Splunky 	device_t		 sc_wsmouse;	/* child */
106a5c89047Sgdamore 	int			 sc_enabled;
107a5c89047Sgdamore 	uint16_t		 sc_flags;
108a5c89047Sgdamore 
109a5c89047Sgdamore 	/* locators */
110a5c89047Sgdamore 	struct hid_location	 sc_loc_x;
111a5c89047Sgdamore 	struct hid_location	 sc_loc_y;
112a5c89047Sgdamore 	struct hid_location	 sc_loc_z;
113a5c89047Sgdamore 	struct hid_location	 sc_loc_w;
114a5c89047Sgdamore 	struct hid_location	 sc_loc_button[MAX_BUTTONS];
115a5c89047Sgdamore 
116a5c89047Sgdamore 	int			 sc_num_buttons;
117a5c89047Sgdamore 	uint32_t		 sc_buttons;
118a5c89047Sgdamore };
119a5c89047Sgdamore 
120a5c89047Sgdamore /* sc_flags */
121a5c89047Sgdamore #define BTMS_REVZ		(1 << 0)	/* reverse Z direction */
122a5c89047Sgdamore #define BTMS_HASZ		(1 << 1)	/* has Z direction */
123e500250bSplunky #define BTMS_HASW		(1 << 2)	/* has W direction */
124a5c89047Sgdamore 
125a5c89047Sgdamore /* autoconf(9) methods */
12632c49409Scegger static int	btms_match(device_t, cfdata_t, void *);
127edb74239Splunky static void	btms_attach(device_t, device_t, void *);
128edb74239Splunky static int	btms_detach(device_t, int);
129a5c89047Sgdamore 
130edb74239Splunky CFATTACH_DECL_NEW(btms, sizeof(struct btms_softc),
131a5c89047Sgdamore     btms_match, btms_attach, btms_detach, NULL);
132a5c89047Sgdamore 
133a5c89047Sgdamore /* wsmouse(4) accessops */
134a5c89047Sgdamore static int	btms_wsmouse_enable(void *);
13553524e44Schristos static int	btms_wsmouse_ioctl(void *, unsigned long, void *, int, struct lwp *);
136a5c89047Sgdamore static void	btms_wsmouse_disable(void *);
137a5c89047Sgdamore 
138a5c89047Sgdamore static const struct wsmouse_accessops btms_wsmouse_accessops = {
139a5c89047Sgdamore 	btms_wsmouse_enable,
140a5c89047Sgdamore 	btms_wsmouse_ioctl,
141a5c89047Sgdamore 	btms_wsmouse_disable,
142a5c89047Sgdamore };
143a5c89047Sgdamore 
144a5c89047Sgdamore /* bthid methods */
145a5c89047Sgdamore static void btms_input(struct bthidev *, uint8_t *, int);
146a5c89047Sgdamore 
147fa5e93a1Snonaka #ifdef BTMS_DEBUG
148fa5e93a1Snonaka static void	btms_print_device(struct btms_softc *);
149fa5e93a1Snonaka #endif
150fa5e93a1Snonaka 
151fa5e93a1Snonaka /*
152fa5e93a1Snonaka  * quirks
153fa5e93a1Snonaka  */
154fa5e93a1Snonaka static const struct btms_quirk {
155fa5e93a1Snonaka 	int		vendor;
156fa5e93a1Snonaka 	int		product;
157fa5e93a1Snonaka 
158fa5e93a1Snonaka 	uint32_t	flags;
159fa5e93a1Snonaka #define	BTMS_QUIRK_ELECOM	__BIT(0)
160fa5e93a1Snonaka } btms_quirk_table[] = {
161fa5e93a1Snonaka 	/* ELECOM M-XG2BB */
162fa5e93a1Snonaka 	{ 0x056e, 0x00d2, BTMS_QUIRK_ELECOM },
163fa5e93a1Snonaka };
164fa5e93a1Snonaka 
165fa5e93a1Snonaka static uint32_t
btms_lookup_quirk_flags(int vendor,int product)166fa5e93a1Snonaka btms_lookup_quirk_flags(int vendor, int product)
167fa5e93a1Snonaka {
168fa5e93a1Snonaka 	const struct btms_quirk *q;
169fa5e93a1Snonaka 	int i;
170fa5e93a1Snonaka 
171fa5e93a1Snonaka 	for (i = 0; i < __arraycount(btms_quirk_table); ++i) {
172fa5e93a1Snonaka 		q = &btms_quirk_table[i];
173fa5e93a1Snonaka 		if (vendor == q->vendor && product == q->product)
174fa5e93a1Snonaka 			return q->flags;
175fa5e93a1Snonaka 	}
176fa5e93a1Snonaka 	return 0;
177fa5e93a1Snonaka }
178fa5e93a1Snonaka 
179fa5e93a1Snonaka static void btms_fixup_elecom(struct bthidev_attach_args *,struct btms_softc *);
180fa5e93a1Snonaka 
181a5c89047Sgdamore /*****************************************************************************
182a5c89047Sgdamore  *
183a5c89047Sgdamore  *	btms autoconf(9) routines
184a5c89047Sgdamore  */
185a5c89047Sgdamore 
186a5c89047Sgdamore static int
btms_match(device_t parent,cfdata_t match,void * aux)18732c49409Scegger btms_match(device_t parent, cfdata_t match, void *aux)
188a5c89047Sgdamore {
189a5c89047Sgdamore 	struct bthidev_attach_args *ba = aux;
190a5c89047Sgdamore 
191a5c89047Sgdamore 	if (hid_is_collection(ba->ba_desc, ba->ba_dlen, ba->ba_id,
192a5c89047Sgdamore 			    HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)))
193a5c89047Sgdamore 		return 1;
194a5c89047Sgdamore 
195a5c89047Sgdamore 	return 0;
196a5c89047Sgdamore }
197a5c89047Sgdamore 
198a5c89047Sgdamore static void
btms_attach(device_t parent,device_t self,void * aux)199edb74239Splunky btms_attach(device_t parent, device_t self, void *aux)
200a5c89047Sgdamore {
201edb74239Splunky 	struct btms_softc *sc = device_private(self);
202a5c89047Sgdamore 	struct bthidev_attach_args *ba = aux;
203a5c89047Sgdamore 	struct wsmousedev_attach_args wsma;
204a5c89047Sgdamore 	struct hid_location *zloc;
205fa5e93a1Snonaka 	uint32_t flags, quirks;
206a5c89047Sgdamore 	int i, hl;
207a5c89047Sgdamore 
208a5c89047Sgdamore 	ba->ba_input = btms_input;
209a5c89047Sgdamore 
210fa5e93a1Snonaka 	quirks = btms_lookup_quirk_flags(ba->ba_vendor, ba->ba_product);
211fa5e93a1Snonaka 
212a5c89047Sgdamore 	/* control the horizontal */
213a5c89047Sgdamore 	hl = hid_locate(ba->ba_desc,
214a5c89047Sgdamore 			ba->ba_dlen,
215a5c89047Sgdamore 			HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
216a5c89047Sgdamore 			ba->ba_id,
217a5c89047Sgdamore 			hid_input,
218a5c89047Sgdamore 			&sc->sc_loc_x,
219a5c89047Sgdamore 			&flags);
220a5c89047Sgdamore 
221a5c89047Sgdamore 	if (hl == 0 || NOTMOUSE(flags)) {
222edb74239Splunky 		aprint_error("X report 0x%04x not supported\n", flags);
223a5c89047Sgdamore 		return;
224a5c89047Sgdamore 	}
225a5c89047Sgdamore 
226a5c89047Sgdamore 	/* control the vertical */
227a5c89047Sgdamore 	hl = hid_locate(ba->ba_desc,
228a5c89047Sgdamore 			ba->ba_dlen,
229a5c89047Sgdamore 			HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
230a5c89047Sgdamore 			ba->ba_id,
231a5c89047Sgdamore 			hid_input,
232a5c89047Sgdamore 			&sc->sc_loc_y,
233a5c89047Sgdamore 			&flags);
234a5c89047Sgdamore 
235a5c89047Sgdamore 	if (hl == 0 || NOTMOUSE(flags)) {
236edb74239Splunky 		aprint_error("Y report 0x%04x not supported\n", flags);
237a5c89047Sgdamore 		return;
238a5c89047Sgdamore 	}
239a5c89047Sgdamore 
240a5c89047Sgdamore 	/* Try the wheel first as the Z activator since it's tradition. */
241a5c89047Sgdamore 	hl = hid_locate(ba->ba_desc,
242a5c89047Sgdamore 			ba->ba_dlen,
243a5c89047Sgdamore 			HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_WHEEL),
244a5c89047Sgdamore 			ba->ba_id,
245a5c89047Sgdamore 			hid_input,
246a5c89047Sgdamore 			&sc->sc_loc_z,
247a5c89047Sgdamore 			&flags);
248a5c89047Sgdamore 
249a5c89047Sgdamore 	zloc = &sc->sc_loc_z;
250a5c89047Sgdamore 	if (hl) {
251a5c89047Sgdamore 		if (NOTMOUSE(flags)) {
252edb74239Splunky 			aprint_error("Wheel report 0x%04x ignored\n", flags);
253a5c89047Sgdamore 
254a5c89047Sgdamore 			/* ignore Bad Z coord */
255a5c89047Sgdamore 			sc->sc_loc_z.size = 0;
256a5c89047Sgdamore 		} else {
257a5c89047Sgdamore 			sc->sc_flags |= BTMS_HASZ;
258a5c89047Sgdamore 			/* Wheels need the Z axis reversed. */
259a5c89047Sgdamore 			sc->sc_flags ^= BTMS_REVZ;
260a5c89047Sgdamore 			/* Put Z on the W coordinate */
261a5c89047Sgdamore 			zloc = &sc->sc_loc_w;
262a5c89047Sgdamore 		}
263a5c89047Sgdamore 	}
264a5c89047Sgdamore 
265a5c89047Sgdamore 	hl = hid_locate(ba->ba_desc,
266a5c89047Sgdamore 			ba->ba_dlen,
267a5c89047Sgdamore 			HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Z),
268a5c89047Sgdamore 			ba->ba_id,
269a5c89047Sgdamore 			hid_input,
270a5c89047Sgdamore 			zloc,
271a5c89047Sgdamore 			&flags);
272a5c89047Sgdamore 
273e500250bSplunky 	/*
274e500250bSplunky 	 * The horizontal component of the scrollball can also be given by
275e500250bSplunky 	 * Application Control Pan in the Consumer page, so if we didnt see
276e500250bSplunky 	 * any Z then check that.
277e500250bSplunky 	 */
278e500250bSplunky 	if (!hl) {
279e500250bSplunky 		hl = hid_locate(ba->ba_desc,
280e500250bSplunky 				ba->ba_dlen,
281e500250bSplunky 				HID_USAGE2(HUP_CONSUMER, HUC_AC_PAN),
282e500250bSplunky 				ba->ba_id,
283e500250bSplunky 				hid_input,
284e500250bSplunky 				zloc,
285e500250bSplunky 				&flags);
286e500250bSplunky 	}
287e500250bSplunky 
288a5c89047Sgdamore 	if (hl) {
289a5c89047Sgdamore 		if (NOTMOUSE(flags))
290a5c89047Sgdamore 			zloc->size = 0;	/* ignore Z */
291e500250bSplunky 		else {
292e500250bSplunky 			if (sc->sc_flags & BTMS_HASZ)
293e500250bSplunky 				sc->sc_flags |= BTMS_HASW;
294a5c89047Sgdamore 			else
295a5c89047Sgdamore 				sc->sc_flags |= BTMS_HASZ;
296a5c89047Sgdamore 		}
297e500250bSplunky 	}
298a5c89047Sgdamore 
299a5c89047Sgdamore 	for (i = 1 ; i <= MAX_BUTTONS ; i++) {
300a5c89047Sgdamore 		hl = hid_locate(ba->ba_desc,
301a5c89047Sgdamore 				ba->ba_dlen,
302a5c89047Sgdamore 				HID_USAGE2(HUP_BUTTON, i),
303a5c89047Sgdamore 				ba->ba_id,
304a5c89047Sgdamore 				hid_input,
305a5c89047Sgdamore 				&sc->sc_loc_button[i - 1],
306a5c89047Sgdamore 				NULL);
307a5c89047Sgdamore 
308a5c89047Sgdamore 		if (hl == 0)
309a5c89047Sgdamore 			break;
310a5c89047Sgdamore 	}
311a5c89047Sgdamore 	sc->sc_num_buttons = i - 1;
312a5c89047Sgdamore 
313fa5e93a1Snonaka 	if (ISSET(quirks, BTMS_QUIRK_ELECOM))
314fa5e93a1Snonaka 		btms_fixup_elecom(ba, sc);
315fa5e93a1Snonaka 
316e500250bSplunky 	aprint_normal(": %d button%s%s%s%s.\n",
317a5c89047Sgdamore 			sc->sc_num_buttons,
318a5c89047Sgdamore 			sc->sc_num_buttons == 1 ? "" : "s",
319e500250bSplunky 			sc->sc_flags & BTMS_HASW ? ", W" : "",
320e500250bSplunky 			sc->sc_flags & BTMS_HASZ ? " and Z dir" : "",
321e500250bSplunky 			sc->sc_flags & BTMS_HASW ? "s" : "");
322fa5e93a1Snonaka #ifdef BTMS_DEBUG
323fa5e93a1Snonaka 	if (btms_debug)
324fa5e93a1Snonaka 		btms_print_device(sc);
325fa5e93a1Snonaka #endif
326a5c89047Sgdamore 
327a5c89047Sgdamore 	wsma.accessops = &btms_wsmouse_accessops;
328a5c89047Sgdamore 	wsma.accesscookie = sc;
329a5c89047Sgdamore 
330*c7fb772bSthorpej 	sc->sc_wsmouse = config_found(self, &wsma, wsmousedevprint, CFARGS_NONE);
3317e98c91dSplunky 
3327e98c91dSplunky 	pmf_device_register(self, NULL, NULL);
333a5c89047Sgdamore }
334a5c89047Sgdamore 
335a5c89047Sgdamore static int
btms_detach(device_t self,int flags)336edb74239Splunky btms_detach(device_t self, int flags)
337a5c89047Sgdamore {
338edb74239Splunky 	struct btms_softc *sc = device_private(self);
339a5c89047Sgdamore 	int err = 0;
340a5c89047Sgdamore 
3417e98c91dSplunky 	pmf_device_deregister(self);
3427e98c91dSplunky 
343a5c89047Sgdamore 	if (sc->sc_wsmouse != NULL) {
344a5c89047Sgdamore 		err = config_detach(sc->sc_wsmouse, flags);
345a5c89047Sgdamore 		sc->sc_wsmouse = NULL;
346a5c89047Sgdamore 	}
347a5c89047Sgdamore 
348a5c89047Sgdamore 	return err;
349a5c89047Sgdamore }
350a5c89047Sgdamore 
351a5c89047Sgdamore /*****************************************************************************
352a5c89047Sgdamore  *
353a5c89047Sgdamore  *	wsmouse(4) accessops
354a5c89047Sgdamore  */
355a5c89047Sgdamore 
356a5c89047Sgdamore static int
btms_wsmouse_enable(void * cookie)357cbab9cadSchs btms_wsmouse_enable(void *cookie)
358a5c89047Sgdamore {
359cbab9cadSchs 	struct btms_softc *sc = cookie;
360a5c89047Sgdamore 
361a5c89047Sgdamore 	if (sc->sc_enabled)
362a5c89047Sgdamore 		return EBUSY;
363a5c89047Sgdamore 
364a5c89047Sgdamore 	sc->sc_enabled = 1;
365a5c89047Sgdamore 	return 0;
366a5c89047Sgdamore }
367a5c89047Sgdamore 
368a5c89047Sgdamore static int
btms_wsmouse_ioctl(void * cookie,unsigned long cmd,void * data,int flag,struct lwp * l)369cbab9cadSchs btms_wsmouse_ioctl(void *cookie, unsigned long cmd, void *data,
370168cd830Schristos     int flag, struct lwp *l)
371a5c89047Sgdamore {
372a5c89047Sgdamore 
373a5c89047Sgdamore 	switch (cmd) {
374a5c89047Sgdamore 	case WSMOUSEIO_GTYPE:
375a5c89047Sgdamore 		*(uint *)data = WSMOUSE_TYPE_BLUETOOTH;
376a5c89047Sgdamore 		break;
377a5c89047Sgdamore 
378a5c89047Sgdamore 	default:
379a5c89047Sgdamore 		return EPASSTHROUGH;
380a5c89047Sgdamore 	}
381a5c89047Sgdamore 
382a5c89047Sgdamore 	return 0;
383a5c89047Sgdamore }
384a5c89047Sgdamore 
385a5c89047Sgdamore static void
btms_wsmouse_disable(void * cookie)386cbab9cadSchs btms_wsmouse_disable(void *cookie)
387a5c89047Sgdamore {
388cbab9cadSchs 	struct btms_softc *sc = cookie;
389a5c89047Sgdamore 
390a5c89047Sgdamore 	sc->sc_enabled = 0;
391a5c89047Sgdamore }
392a5c89047Sgdamore 
393a5c89047Sgdamore /*****************************************************************************
394a5c89047Sgdamore  *
395a5c89047Sgdamore  *	btms input routine, called from our parent
396a5c89047Sgdamore  */
397a5c89047Sgdamore 
398a5c89047Sgdamore static void
btms_input(struct bthidev * hidev,uint8_t * data,int len)399cbab9cadSchs btms_input(struct bthidev *hidev, uint8_t *data, int len)
400a5c89047Sgdamore {
401cbab9cadSchs 	struct btms_softc *sc = (struct btms_softc *)hidev;
402a5c89047Sgdamore 	int dx, dy, dz, dw;
403a5c89047Sgdamore 	uint32_t buttons;
404a5c89047Sgdamore 	int i, s;
405a5c89047Sgdamore 
406a5c89047Sgdamore 	if (sc->sc_wsmouse == NULL || sc->sc_enabled == 0)
407a5c89047Sgdamore 		return;
408a5c89047Sgdamore 
409fa5e93a1Snonaka #ifdef BTMS_DEBUG
410fa5e93a1Snonaka 	if (btms_debug > 9) {
411fa5e93a1Snonaka 		printf("%s: data: ", __func__);
412fa5e93a1Snonaka 		for (i = 0; i < len; ++i) {
413fa5e93a1Snonaka 			printf("%02x", data[i]);
414fa5e93a1Snonaka 		}
415fa5e93a1Snonaka 		printf("\n");
416fa5e93a1Snonaka 	}
417fa5e93a1Snonaka #endif
418fa5e93a1Snonaka 
419a5c89047Sgdamore 	dx =  hid_get_data(data, &sc->sc_loc_x);
420a5c89047Sgdamore 	dy = -hid_get_data(data, &sc->sc_loc_y);
421a5c89047Sgdamore 	dz =  hid_get_data(data, &sc->sc_loc_z);
422a5c89047Sgdamore 	dw =  hid_get_data(data, &sc->sc_loc_w);
423a5c89047Sgdamore 
424a5c89047Sgdamore 	if (sc->sc_flags & BTMS_REVZ)
425a5c89047Sgdamore 		dz = -dz;
426a5c89047Sgdamore 
427a5c89047Sgdamore 	buttons = 0;
428a5c89047Sgdamore 	for (i = 0 ; i < sc->sc_num_buttons ; i++)
429a5c89047Sgdamore 		if (hid_get_data(data, &sc->sc_loc_button[i]))
430a5c89047Sgdamore 			buttons |= BUTTON(i);
431a5c89047Sgdamore 
432fa5e93a1Snonaka 	BTMSDBGN(9,("%s: dx=%d, dy=%d, dz=%d, dw=%d, buttons=0x%08x\n",
433fa5e93a1Snonaka 	    __func__, dx, dy, dz, dw, buttons));
434a5c89047Sgdamore 	if (dx != 0 || dy != 0 || dz != 0 || dw != 0 || buttons != sc->sc_buttons) {
435a5c89047Sgdamore 		sc->sc_buttons = buttons;
436a5c89047Sgdamore 
437a5c89047Sgdamore 		s = spltty();
43857c0199dSplunky 		wsmouse_input(sc->sc_wsmouse,
43957c0199dSplunky 				buttons,
440a5c89047Sgdamore 				dx, dy, dz, dw,
441a5c89047Sgdamore 				WSMOUSE_INPUT_DELTA);
442a5c89047Sgdamore 		splx(s);
443a5c89047Sgdamore 	}
444a5c89047Sgdamore }
445fa5e93a1Snonaka 
446fa5e93a1Snonaka #ifdef BTMS_DEBUG
447fa5e93a1Snonaka static void
btms_print_device(struct btms_softc * sc)448fa5e93a1Snonaka btms_print_device(struct btms_softc *sc)
449fa5e93a1Snonaka {
450fa5e93a1Snonaka 	int i;
451fa5e93a1Snonaka 
452fa5e93a1Snonaka 	printf("btms: X: pos=%d, size=%d\n",
453fa5e93a1Snonaka 	    sc->sc_loc_x.pos, sc->sc_loc_x.size);
454fa5e93a1Snonaka 	printf("btms: Y: pos=%d, size=%d\n",
455fa5e93a1Snonaka 	    sc->sc_loc_y.pos, sc->sc_loc_y.size);
456fa5e93a1Snonaka 	if (sc->sc_flags & BTMS_HASZ) {
457fa5e93a1Snonaka 		printf("btms: Z: pos=%d, size=%d%s\n",
458fa5e93a1Snonaka 		    sc->sc_loc_z.pos, sc->sc_loc_z.size,
459fa5e93a1Snonaka 		    ((sc->sc_flags & BTMS_REVZ) ? ", REVZ" : ""));
460fa5e93a1Snonaka 	}
461fa5e93a1Snonaka 	if (sc->sc_flags & BTMS_HASW) {
462fa5e93a1Snonaka 		printf("btms: W: pos=%d, size=%d\n",
463fa5e93a1Snonaka 		    sc->sc_loc_w.pos, sc->sc_loc_w.size);
464fa5e93a1Snonaka 	}
465fa5e93a1Snonaka 
466fa5e93a1Snonaka 	for (i = 0; i < sc->sc_num_buttons; ++i) {
467fa5e93a1Snonaka 		printf("btms: button%d: pos=%d, size=%d\n", i,
468fa5e93a1Snonaka 		    sc->sc_loc_button[i].pos, sc->sc_loc_button[i].size);
469fa5e93a1Snonaka 	}
470fa5e93a1Snonaka }
471fa5e93a1Snonaka #endif
472fa5e93a1Snonaka 
473fa5e93a1Snonaka /*****************************************************************************
474fa5e93a1Snonaka  *
475fa5e93a1Snonaka  * fixup routines
476fa5e93a1Snonaka  */
477fa5e93a1Snonaka static void
btms_fixup_elecom(struct bthidev_attach_args * ba,struct btms_softc * sc)478fa5e93a1Snonaka btms_fixup_elecom(struct bthidev_attach_args *ba, struct btms_softc *sc)
479fa5e93a1Snonaka {
480fa5e93a1Snonaka 
481fa5e93a1Snonaka 	switch (ba->ba_product) {
482fa5e93a1Snonaka 	case 0x00d2:	/* M-XG2BB */
483fa5e93a1Snonaka 		/* invalid Wheel and AC_Pan */
484fa5e93a1Snonaka 		BTMSDBG(("%s: fixup ELECOM M-XG2BB\n", __func__));
485fa5e93a1Snonaka 		sc->sc_loc_z.pos = 40;
486fa5e93a1Snonaka 		sc->sc_loc_z.size = 8;
487fa5e93a1Snonaka 		sc->sc_loc_w.pos = 0;
488fa5e93a1Snonaka 		sc->sc_loc_w.size = 0;
489fa5e93a1Snonaka 		sc->sc_flags = BTMS_HASZ | BTMS_REVZ;
490fa5e93a1Snonaka 		break;
491fa5e93a1Snonaka 	}
492fa5e93a1Snonaka }
493