Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / dev / bluetooth / btms.c
blobaefb96bcbd851aab813ed4779316d2bebfba1698
1 /* $NetBSD: btms.c,v 1.8 2008/09/09 03:54:56 cube Exp $ */
3 /*
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
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
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.
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.
33 /*-
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
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.
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>
72 #include <sys/conf.h>
73 #include <sys/device.h>
74 #include <sys/proc.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)
93 struct btms_softc {
94 struct bthidev sc_hidev; /* device+ */
96 device_t sc_wsmouse; /* child */
97 int sc_enabled;
98 uint16_t sc_flags;
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];
107 int sc_num_buttons;
108 uint32_t sc_buttons;
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 */
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 = {
130 btms_wsmouse_enable,
131 btms_wsmouse_ioctl,
132 btms_wsmouse_disable,
135 /* bthid methods */
136 static void btms_input(struct bthidev *, uint8_t *, int);
138 /*****************************************************************************
140 * btms autoconf(9) routines
143 static int
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)))
150 return 1;
152 return 0;
155 static void
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;
162 uint32_t flags;
163 int i, hl;
165 ba->ba_input = btms_input;
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);
176 if (hl == 0 || NOTMOUSE(flags)) {
177 aprint_error("X report 0x%04x not supported\n", flags);
178 return;
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);
190 if (hl == 0 || NOTMOUSE(flags)) {
191 aprint_error("Y report 0x%04x not supported\n", flags);
192 return;
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);
204 zloc = &sc->sc_loc_z;
205 if (hl) {
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;
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;
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);
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.
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);
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;
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);
263 if (hl == 0)
264 break;
266 sc->sc_num_buttons = i - 1;
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" : "");
275 wsma.accessops = &btms_wsmouse_accessops;
276 wsma.accesscookie = sc;
278 sc->sc_wsmouse = config_found(self, &wsma, wsmousedevprint);
281 static int
282 btms_detach(device_t self, int flags)
284 struct btms_softc *sc = device_private(self);
285 int err = 0;
287 if (sc->sc_wsmouse != NULL) {
288 err = config_detach(sc->sc_wsmouse, flags);
289 sc->sc_wsmouse = NULL;
292 return err;
295 /*****************************************************************************
297 * wsmouse(4) accessops
300 static int
301 btms_wsmouse_enable(void *self)
303 struct btms_softc *sc = self;
305 if (sc->sc_enabled)
306 return EBUSY;
308 sc->sc_enabled = 1;
309 return 0;
312 static int
313 btms_wsmouse_ioctl(void *self, unsigned long cmd, void *data,
314 int flag, struct lwp *l)
316 /* struct btms_softc *sc = self; */
318 switch (cmd) {
319 case WSMOUSEIO_GTYPE:
320 *(uint *)data = WSMOUSE_TYPE_BLUETOOTH;
321 break;
323 default:
324 return EPASSTHROUGH;
327 return 0;
330 static void
331 btms_wsmouse_disable(void *self)
333 struct btms_softc *sc = self;
335 sc->sc_enabled = 0;
338 /*****************************************************************************
340 * btms input routine, called from our parent
343 static void
344 btms_input(struct bthidev *self, uint8_t *data, int len)
346 struct btms_softc *sc = (struct btms_softc *)self;
347 int dx, dy, dz, dw;
348 uint32_t buttons;
349 int i, s;
351 if (sc->sc_wsmouse == NULL || sc->sc_enabled == 0)
352 return;
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)
360 dz = -dz;
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);
367 if (dx != 0 || dy != 0 || dz != 0 || dw != 0 || buttons != sc->sc_buttons) {
368 sc->sc_buttons = buttons;
370 s = spltty();
371 wsmouse_input(sc->sc_wsmouse,
372 buttons,
373 dx, dy, dz, dw,
374 WSMOUSE_INPUT_DELTA);
375 splx(s);