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