1/*	$NetBSD$	*/
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$");
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
93struct 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 */
117static int	btms_match(device_t, cfdata_t, void *);
118static void	btms_attach(device_t, device_t, void *);
119static int	btms_detach(device_t, int);
120
121CFATTACH_DECL_NEW(btms, sizeof(struct btms_softc),
122    btms_match, btms_attach, btms_detach, NULL);
123
124/* wsmouse(4) accessops */
125static int	btms_wsmouse_enable(void *);
126static int	btms_wsmouse_ioctl(void *, unsigned long, void *, int, struct lwp *);
127static void	btms_wsmouse_disable(void *);
128
129static const struct wsmouse_accessops btms_wsmouse_accessops = {
130	btms_wsmouse_enable,
131	btms_wsmouse_ioctl,
132	btms_wsmouse_disable,
133};
134
135/* bthid methods */
136static void btms_input(struct bthidev *, uint8_t *, int);
137
138/*****************************************************************************
139 *
140 *	btms autoconf(9) routines
141 */
142
143static int
144btms_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
155static void
156btms_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	pmf_device_register(self, NULL, NULL);
281}
282
283static int
284btms_detach(device_t self, int flags)
285{
286	struct btms_softc *sc = device_private(self);
287	int err = 0;
288
289	pmf_device_deregister(self);
290
291	if (sc->sc_wsmouse != NULL) {
292		err = config_detach(sc->sc_wsmouse, flags);
293		sc->sc_wsmouse = NULL;
294	}
295
296	return err;
297}
298
299/*****************************************************************************
300 *
301 *	wsmouse(4) accessops
302 */
303
304static int
305btms_wsmouse_enable(void *self)
306{
307	struct btms_softc *sc = self;
308
309	if (sc->sc_enabled)
310		return EBUSY;
311
312	sc->sc_enabled = 1;
313	return 0;
314}
315
316static int
317btms_wsmouse_ioctl(void *self, unsigned long cmd, void *data,
318    int flag, struct lwp *l)
319{
320	/* struct btms_softc *sc = self; */
321
322	switch (cmd) {
323	case WSMOUSEIO_GTYPE:
324		*(uint *)data = WSMOUSE_TYPE_BLUETOOTH;
325		break;
326
327	default:
328		return EPASSTHROUGH;
329	}
330
331	return 0;
332}
333
334static void
335btms_wsmouse_disable(void *self)
336{
337	struct btms_softc *sc = self;
338
339	sc->sc_enabled = 0;
340}
341
342/*****************************************************************************
343 *
344 *	btms input routine, called from our parent
345 */
346
347static void
348btms_input(struct bthidev *self, uint8_t *data, int len)
349{
350	struct btms_softc *sc = (struct btms_softc *)self;
351	int dx, dy, dz, dw;
352	uint32_t buttons;
353	int i, s;
354
355	if (sc->sc_wsmouse == NULL || sc->sc_enabled == 0)
356		return;
357
358	dx =  hid_get_data(data, &sc->sc_loc_x);
359	dy = -hid_get_data(data, &sc->sc_loc_y);
360	dz =  hid_get_data(data, &sc->sc_loc_z);
361	dw =  hid_get_data(data, &sc->sc_loc_w);
362
363	if (sc->sc_flags & BTMS_REVZ)
364		dz = -dz;
365
366	buttons = 0;
367	for (i = 0 ; i < sc->sc_num_buttons ; i++)
368		if (hid_get_data(data, &sc->sc_loc_button[i]))
369			buttons |= BUTTON(i);
370
371	if (dx != 0 || dy != 0 || dz != 0 || dw != 0 || buttons != sc->sc_buttons) {
372		sc->sc_buttons = buttons;
373
374		s = spltty();
375		wsmouse_input(sc->sc_wsmouse,
376				buttons,
377				dx, dy, dz, dw,
378				WSMOUSE_INPUT_DELTA);
379		splx(s);
380	}
381}
382