1 /* $NetBSD: btms.c,v 1.8 2008/09/09 03:54:56 cube Exp $ */
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart@augustsson.net) at
9 * Carlstedt Research & Technology.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
34 * Copyright (c) 2006 Itronix Inc.
35 * All rights reserved.
37 * Written by Iain Hibbert for Itronix Inc.
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
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.
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.
65 * based on dev/usb/ums.c
68 #include <sys/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: btms.c,v 1.8 2008/09/09 03:54:56 cube Exp $");
71 #include <sys/param.h>
73 #include <sys/device.h>
75 #include <sys/systm.h>
77 #include <netbt/bluetooth.h>
79 #include <dev/bluetooth/bthid.h>
80 #include <dev/bluetooth/bthidev.h>
82 #include <dev/usb/hid.h>
83 #include <dev/usb/usb.h>
84 #include <dev/usb/usbhid.h>
86 #include <dev/wscons/wsconsio.h>
87 #include <dev/wscons/wsmousevar.h>
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)
94 struct bthidev sc_hidev
; /* device+ */
96 device_t sc_wsmouse
; /* child */
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
];
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 */
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);
121 CFATTACH_DECL_NEW(btms
, sizeof(struct btms_softc
),
122 btms_match
, btms_attach
, btms_detach
, NULL
);
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 *);
129 static const struct wsmouse_accessops btms_wsmouse_accessops
= {
132 btms_wsmouse_disable
,
136 static void btms_input(struct bthidev
*, uint8_t *, int);
138 /*****************************************************************************
140 * btms autoconf(9) routines
144 btms_match(device_t parent
, cfdata_t match
, void *aux
)
146 struct bthidev_attach_args
*ba
= aux
;
148 if (hid_is_collection(ba
->ba_desc
, ba
->ba_dlen
, ba
->ba_id
,
149 HID_USAGE2(HUP_GENERIC_DESKTOP
, HUG_MOUSE
)))
156 btms_attach(device_t parent
, device_t self
, void *aux
)
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
;
165 ba
->ba_input
= btms_input
;
167 /* control the horizontal */
168 hl
= hid_locate(ba
->ba_desc
,
170 HID_USAGE2(HUP_GENERIC_DESKTOP
, HUG_X
),
176 if (hl
== 0 || NOTMOUSE(flags
)) {
177 aprint_error("X report 0x%04x not supported\n", flags
);
181 /* control the vertical */
182 hl
= hid_locate(ba
->ba_desc
,
184 HID_USAGE2(HUP_GENERIC_DESKTOP
, HUG_Y
),
190 if (hl
== 0 || NOTMOUSE(flags
)) {
191 aprint_error("Y report 0x%04x not supported\n", flags
);
195 /* Try the wheel first as the Z activator since it's tradition. */
196 hl
= hid_locate(ba
->ba_desc
,
198 HID_USAGE2(HUP_GENERIC_DESKTOP
, HUG_WHEEL
),
204 zloc
= &sc
->sc_loc_z
;
206 if (NOTMOUSE(flags
)) {
207 aprint_error("Wheel report 0x%04x ignored\n", flags
);
209 /* ignore Bad Z coord */
210 sc
->sc_loc_z
.size
= 0;
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
;
220 hl
= hid_locate(ba
->ba_desc
,
222 HID_USAGE2(HUP_GENERIC_DESKTOP
, HUG_Z
),
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.
234 hl
= hid_locate(ba
->ba_desc
,
236 HID_USAGE2(HUP_CONSUMER
, HUC_AC_PAN
),
245 zloc
->size
= 0; /* ignore Z */
247 if (sc
->sc_flags
& BTMS_HASZ
)
248 sc
->sc_flags
|= BTMS_HASW
;
250 sc
->sc_flags
|= BTMS_HASZ
;
254 for (i
= 1 ; i
<= MAX_BUTTONS
; i
++) {
255 hl
= hid_locate(ba
->ba_desc
,
257 HID_USAGE2(HUP_BUTTON
, i
),
260 &sc
->sc_loc_button
[i
- 1],
266 sc
->sc_num_buttons
= i
- 1;
268 aprint_normal(": %d button%s%s%s%s.\n",
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" : "");
275 wsma
.accessops
= &btms_wsmouse_accessops
;
276 wsma
.accesscookie
= sc
;
278 sc
->sc_wsmouse
= config_found(self
, &wsma
, wsmousedevprint
);
282 btms_detach(device_t self
, int flags
)
284 struct btms_softc
*sc
= device_private(self
);
287 if (sc
->sc_wsmouse
!= NULL
) {
288 err
= config_detach(sc
->sc_wsmouse
, flags
);
289 sc
->sc_wsmouse
= NULL
;
295 /*****************************************************************************
297 * wsmouse(4) accessops
301 btms_wsmouse_enable(void *self
)
303 struct btms_softc
*sc
= self
;
313 btms_wsmouse_ioctl(void *self
, unsigned long cmd
, void *data
,
314 int flag
, struct lwp
*l
)
316 /* struct btms_softc *sc = self; */
319 case WSMOUSEIO_GTYPE
:
320 *(uint
*)data
= WSMOUSE_TYPE_BLUETOOTH
;
331 btms_wsmouse_disable(void *self
)
333 struct btms_softc
*sc
= self
;
338 /*****************************************************************************
340 * btms input routine, called from our parent
344 btms_input(struct bthidev
*self
, uint8_t *data
, int len
)
346 struct btms_softc
*sc
= (struct btms_softc
*)self
;
351 if (sc
->sc_wsmouse
== NULL
|| sc
->sc_enabled
== 0)
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
);
359 if (sc
->sc_flags
& BTMS_REVZ
)
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
);
367 if (dx
!= 0 || dy
!= 0 || dz
!= 0 || dw
!= 0 || buttons
!= sc
->sc_buttons
) {
368 sc
->sc_buttons
= buttons
;
371 wsmouse_input(sc
->sc_wsmouse
,
374 WSMOUSE_INPUT_DELTA
);