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