xref: /netbsd-src/sys/dev/bluetooth/btms.c (revision c0179c282a5968435315a82f4128c61372c68fc3)
1 /*	$NetBSD: btms.c,v 1.5 2006/11/16 01:32:48 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.5 2006/11/16 01:32:48 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 #define BTMS_HASW		(1 << 2)	/* has W direction */
85 
86 /* autoconf(9) methods */
87 static int	btms_match(struct device *, struct cfdata *, void *);
88 static void	btms_attach(struct device *, struct device *, void *);
89 static int	btms_detach(struct device *, int);
90 
91 CFATTACH_DECL(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, caddr_t, 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(struct device *parent, struct cfdata *match,
115     void *aux)
116 {
117 	struct bthidev_attach_args *ba = aux;
118 
119 	if (hid_is_collection(ba->ba_desc, ba->ba_dlen, ba->ba_id,
120 			    HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)))
121 		return 1;
122 
123 	return 0;
124 }
125 
126 static void
127 btms_attach(struct device *parent, struct device *self, void *aux)
128 {
129 	struct btms_softc *sc = (struct btms_softc *)self;
130 	struct bthidev_attach_args *ba = aux;
131 	struct wsmousedev_attach_args wsma;
132 	struct hid_location *zloc;
133 	uint32_t flags;
134 	int i, hl;
135 
136 	ba->ba_input = btms_input;
137 
138 	/* control the horizontal */
139 	hl = hid_locate(ba->ba_desc,
140 			ba->ba_dlen,
141 			HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
142 			ba->ba_id,
143 			hid_input,
144 			&sc->sc_loc_x,
145 			&flags);
146 
147 	if (hl == 0 || NOTMOUSE(flags)) {
148 		printf("\n%s: X report 0x%04x not supported\n",
149 		       sc->sc_hidev.sc_dev.dv_xname, flags);
150 
151 		return;
152 	}
153 
154 	/* control the vertical */
155 	hl = hid_locate(ba->ba_desc,
156 			ba->ba_dlen,
157 			HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
158 			ba->ba_id,
159 			hid_input,
160 			&sc->sc_loc_y,
161 			&flags);
162 
163 	if (hl == 0 || NOTMOUSE(flags)) {
164 		printf("\n%s: Y report 0x%04x not supported\n",
165 			sc->sc_hidev.sc_dev.dv_xname, flags);
166 
167 		return;
168 	}
169 
170 	/* Try the wheel first as the Z activator since it's tradition. */
171 	hl = hid_locate(ba->ba_desc,
172 			ba->ba_dlen,
173 			HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_WHEEL),
174 			ba->ba_id,
175 			hid_input,
176 			&sc->sc_loc_z,
177 			&flags);
178 
179 	zloc = &sc->sc_loc_z;
180 	if (hl) {
181 		if (NOTMOUSE(flags)) {
182 			printf("\n%s: Wheel report 0x%04x not supported\n",
183 				sc->sc_hidev.sc_dev.dv_xname, flags);
184 
185 			/* ignore Bad Z coord */
186 			sc->sc_loc_z.size = 0;
187 		} else {
188 			sc->sc_flags |= BTMS_HASZ;
189 			/* Wheels need the Z axis reversed. */
190 			sc->sc_flags ^= BTMS_REVZ;
191 			/* Put Z on the W coordinate */
192 			zloc = &sc->sc_loc_w;
193 		}
194 	}
195 
196 	hl = hid_locate(ba->ba_desc,
197 			ba->ba_dlen,
198 			HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Z),
199 			ba->ba_id,
200 			hid_input,
201 			zloc,
202 			&flags);
203 
204 	/*
205 	 * The horizontal component of the scrollball can also be given by
206 	 * Application Control Pan in the Consumer page, so if we didnt see
207 	 * any Z then check that.
208 	 */
209 	if (!hl) {
210 		hl = hid_locate(ba->ba_desc,
211 				ba->ba_dlen,
212 				HID_USAGE2(HUP_CONSUMER, HUC_AC_PAN),
213 				ba->ba_id,
214 				hid_input,
215 				zloc,
216 				&flags);
217 	}
218 
219 	if (hl) {
220 		if (NOTMOUSE(flags))
221 			zloc->size = 0;	/* ignore Z */
222 		else {
223 			if (sc->sc_flags & BTMS_HASZ)
224 				sc->sc_flags |= BTMS_HASW;
225 			else
226 				sc->sc_flags |= BTMS_HASZ;
227 		}
228 	}
229 
230 	for (i = 1 ; i <= MAX_BUTTONS ; i++) {
231 		hl = hid_locate(ba->ba_desc,
232 				ba->ba_dlen,
233 				HID_USAGE2(HUP_BUTTON, i),
234 				ba->ba_id,
235 				hid_input,
236 				&sc->sc_loc_button[i - 1],
237 				NULL);
238 
239 		if (hl == 0)
240 			break;
241 	}
242 	sc->sc_num_buttons = i - 1;
243 
244 	aprint_normal(": %d button%s%s%s%s.\n",
245 			sc->sc_num_buttons,
246 			sc->sc_num_buttons == 1 ? "" : "s",
247 			sc->sc_flags & BTMS_HASW ? ", W" : "",
248 			sc->sc_flags & BTMS_HASZ ? " and Z dir" : "",
249 			sc->sc_flags & BTMS_HASW ? "s" : "");
250 
251 	wsma.accessops = &btms_wsmouse_accessops;
252 	wsma.accesscookie = sc;
253 
254 	sc->sc_wsmouse = config_found((struct device *)sc, &wsma, wsmousedevprint);
255 }
256 
257 static int
258 btms_detach(struct device *self, int flags)
259 {
260 	struct btms_softc *sc = (struct btms_softc *)self;
261 	int err = 0;
262 
263 	if (sc->sc_wsmouse != NULL) {
264 		err = config_detach(sc->sc_wsmouse, flags);
265 		sc->sc_wsmouse = NULL;
266 	}
267 
268 	return err;
269 }
270 
271 /*****************************************************************************
272  *
273  *	wsmouse(4) accessops
274  */
275 
276 static int
277 btms_wsmouse_enable(void *self)
278 {
279 	struct btms_softc *sc = (struct btms_softc *)self;
280 
281 	if (sc->sc_enabled)
282 		return EBUSY;
283 
284 	sc->sc_enabled = 1;
285 	return 0;
286 }
287 
288 static int
289 btms_wsmouse_ioctl(void *self, unsigned long cmd, caddr_t data,
290     int flag, struct lwp *l)
291 {
292 	/* struct btms_softc *sc = (struct btms_softc *)self; */
293 
294 	switch (cmd) {
295 	case WSMOUSEIO_GTYPE:
296 		*(uint *)data = WSMOUSE_TYPE_BLUETOOTH;
297 		break;
298 
299 	default:
300 		return EPASSTHROUGH;
301 	}
302 
303 	return 0;
304 }
305 
306 static void
307 btms_wsmouse_disable(void *self)
308 {
309 	struct btms_softc *sc = (struct btms_softc *)self;
310 
311 	sc->sc_enabled = 0;
312 }
313 
314 /*****************************************************************************
315  *
316  *	btms input routine, called from our parent
317  */
318 
319 static void
320 btms_input(struct bthidev *self, uint8_t *data, int len)
321 {
322 	struct btms_softc *sc = (struct btms_softc *)self;
323 	int dx, dy, dz, dw;
324 	uint32_t buttons;
325 	int i, s;
326 
327 	if (sc->sc_wsmouse == NULL || sc->sc_enabled == 0)
328 		return;
329 
330 	dx =  hid_get_data(data, &sc->sc_loc_x);
331 	dy = -hid_get_data(data, &sc->sc_loc_y);
332 	dz =  hid_get_data(data, &sc->sc_loc_z);
333 	dw =  hid_get_data(data, &sc->sc_loc_w);
334 
335 	if (sc->sc_flags & BTMS_REVZ)
336 		dz = -dz;
337 
338 	buttons = 0;
339 	for (i = 0 ; i < sc->sc_num_buttons ; i++)
340 		if (hid_get_data(data, &sc->sc_loc_button[i]))
341 			buttons |= BUTTON(i);
342 
343 	if (dx != 0 || dy != 0 || dz != 0 || dw != 0 || buttons != sc->sc_buttons) {
344 		sc->sc_buttons = buttons;
345 
346 		s = spltty();
347 		wsmouse_input(sc->sc_wsmouse,
348 				buttons,
349 				dx, dy, dz, dw,
350 				WSMOUSE_INPUT_DELTA);
351 		splx(s);
352 	}
353 }
354