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