ums.c revision 1.85 1 /* $NetBSD: ums.c,v 1.85 2013/01/05 01:30:17 christos Exp $ */
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 (at) 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 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: ums.c,v 1.85 2013/01/05 01:30:17 christos Exp $");
39
40 #ifdef _KERNEL_OPT
41 #include "opt_usb.h"
42 #endif
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/device.h>
49 #include <sys/ioctl.h>
50 #include <sys/file.h>
51 #include <sys/select.h>
52 #include <sys/proc.h>
53 #include <sys/vnode.h>
54 #include <sys/poll.h>
55
56 #include <dev/usb/usb.h>
57 #include <dev/usb/usbhid.h>
58
59 #include <dev/usb/usbdi.h>
60 #include <dev/usb/usbdi_util.h>
61 #include <dev/usb/usbdevs.h>
62 #include <dev/usb/usb_quirks.h>
63 #include <dev/usb/uhidev.h>
64 #include <dev/usb/hid.h>
65
66 #include <dev/wscons/wsconsio.h>
67 #include <dev/wscons/wsmousevar.h>
68
69 #ifdef USB_DEBUG
70 #define DPRINTF(x) if (umsdebug) printf x
71 #define DPRINTFN(n,x) if (umsdebug>(n)) printf x
72 int umsdebug = 0;
73 #else
74 #define DPRINTF(x)
75 #define DPRINTFN(n,x)
76 #endif
77
78 #define UMS_BUT(i) ((i) == 1 || (i) == 2 ? 3 - (i) : i)
79
80 #define UMSUNIT(s) (minor(s))
81
82 #define PS2LBUTMASK x01
83 #define PS2RBUTMASK x02
84 #define PS2MBUTMASK x04
85 #define PS2BUTMASK 0x0f
86
87 #define MAX_BUTTONS 31 /* must not exceed size of sc_buttons */
88
89 struct ums_softc {
90 struct uhidev sc_hdev;
91
92 struct hid_location sc_loc_x, sc_loc_y, sc_loc_z, sc_loc_w;
93 struct hid_location sc_loc_btn[MAX_BUTTONS];
94
95 int sc_enabled;
96
97 u_int flags; /* device configuration */
98 #define UMS_Z 0x001 /* z direction available */
99 #define UMS_SPUR_BUT_UP 0x002 /* spurious button up events */
100 #define UMS_REVZ 0x004 /* Z-axis is reversed */
101 #define UMS_W 0x008 /* w direction/tilt available */
102 #define UMS_ABS 0x010 /* absolute position, touchpanel */
103 #define UMS_TIP_SWITCH 0x020 /* digitizer tip switch */
104 #define UMS_SEC_TIP_SWITCH 0x040 /* digitizer secondary tip switch */
105 #define UMS_BARREL_SWITCH 0x080 /* digitizer barrel switch */
106 #define UMS_ERASER 0x100 /* digitizer eraser */
107
108 int nbuttons;
109
110 u_int32_t sc_buttons; /* mouse button status */
111 device_t sc_wsmousedev;
112
113 char sc_dying;
114 };
115
116 static const struct {
117 u_int feature;
118 u_int flag;
119 } digbut[] = {
120 { HUD_TIP_SWITCH, UMS_TIP_SWITCH },
121 { HUD_SEC_TIP_SWITCH, UMS_SEC_TIP_SWITCH },
122 { HUD_BARREL_SWITCH, UMS_BARREL_SWITCH },
123 { HUD_ERASER, UMS_ERASER },
124 };
125
126 #define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
127
128 Static void ums_intr(struct uhidev *addr, void *ibuf, u_int len);
129
130 Static int ums_enable(void *);
131 Static void ums_disable(void *);
132 Static int ums_ioctl(void *, u_long, void *, int, struct lwp * );
133
134 const struct wsmouse_accessops ums_accessops = {
135 ums_enable,
136 ums_ioctl,
137 ums_disable,
138 };
139
140 int ums_match(device_t, cfdata_t, void *);
141 void ums_attach(device_t, device_t, void *);
142 void ums_childdet(device_t, device_t);
143 int ums_detach(device_t, int);
144 int ums_activate(device_t, enum devact);
145 extern struct cfdriver ums_cd;
146 CFATTACH_DECL2_NEW(ums, sizeof(struct ums_softc), ums_match, ums_attach,
147 ums_detach, ums_activate, NULL, ums_childdet);
148
149 int
150 ums_match(device_t parent, cfdata_t match, void *aux)
151 {
152 struct uhidev_attach_arg *uha = aux;
153 int size;
154 void *desc;
155
156 /*
157 * Some (older) Griffin PowerMate knobs may masquerade as a
158 * mouse, avoid treating them as such, they have only one axis.
159 */
160 if (uha->uaa->vendor == USB_VENDOR_GRIFFIN &&
161 uha->uaa->product == USB_PRODUCT_GRIFFIN_POWERMATE)
162 return (UMATCH_NONE);
163
164 uhidev_get_report_desc(uha->parent, &desc, &size);
165 if (!hid_is_collection(desc, size, uha->reportid,
166 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)) &&
167 !hid_is_collection(desc, size, uha->reportid,
168 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_POINTER)) &&
169 !hid_is_collection(desc, size, uha->reportid,
170 HID_USAGE2(HUP_DIGITIZERS, 0x0002)))
171 return (UMATCH_NONE);
172
173 return (UMATCH_IFACECLASS);
174 }
175
176 void
177 ums_attach(device_t parent, device_t self, void *aux)
178 {
179 struct ums_softc *sc = device_private(self);
180 struct uhidev_attach_arg *uha = aux;
181 struct wsmousedev_attach_args a;
182 int size;
183 void *desc;
184 u_int32_t flags, quirks;
185 int i, hl;
186 struct hid_location *zloc;
187 bool isdigitizer;
188
189 aprint_naive("\n");
190
191 sc->sc_hdev.sc_dev = self;
192 sc->sc_hdev.sc_intr = ums_intr;
193 sc->sc_hdev.sc_parent = uha->parent;
194 sc->sc_hdev.sc_report_id = uha->reportid;
195
196 quirks = usbd_get_quirks(uha->parent->sc_udev)->uq_flags;
197 if (quirks & UQ_MS_REVZ)
198 sc->flags |= UMS_REVZ;
199 if (quirks & UQ_SPUR_BUT_UP)
200 sc->flags |= UMS_SPUR_BUT_UP;
201
202 uhidev_get_report_desc(uha->parent, &desc, &size);
203
204 isdigitizer = hid_is_collection(desc, size, uha->reportid,
205 HID_USAGE2(HUP_DIGITIZERS, 0x0002));
206
207 if (!pmf_device_register(self, NULL, NULL))
208 aprint_error_dev(self, "couldn't establish power handler\n");
209
210 if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
211 uha->reportid, hid_input, &sc->sc_loc_x, &flags)) {
212 aprint_error("\n%s: mouse has no X report\n",
213 device_xname(sc->sc_hdev.sc_dev));
214 return;
215 }
216 switch (flags & MOUSE_FLAGS_MASK) {
217 case 0:
218 sc->flags |= UMS_ABS;
219 break;
220 case HIO_RELATIVE:
221 break;
222 default:
223 aprint_error("\n%s: X report 0x%04x not supported\n",
224 device_xname(sc->sc_hdev.sc_dev), flags);
225 return;
226 }
227
228 if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
229 uha->reportid, hid_input, &sc->sc_loc_y, &flags)) {
230 aprint_error("\n%s: mouse has no Y report\n",
231 device_xname(sc->sc_hdev.sc_dev));
232 return;
233 }
234 switch (flags & MOUSE_FLAGS_MASK) {
235 case 0:
236 sc->flags |= UMS_ABS;
237 break;
238 case HIO_RELATIVE:
239 break;
240 default:
241 aprint_error("\n%s: Y report 0x%04x not supported\n",
242 device_xname(sc->sc_hdev.sc_dev), flags);
243 return;
244 }
245
246 /* Try the wheel first as the Z activator since it's tradition. */
247 hl = hid_locate(desc,
248 size,
249 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_WHEEL),
250 uha->reportid,
251 hid_input,
252 &sc->sc_loc_z,
253 &flags);
254
255 zloc = &sc->sc_loc_z;
256 if (hl) {
257 if ((flags & MOUSE_FLAGS_MASK) != HIO_RELATIVE) {
258 aprint_verbose("\n%s: Wheel report 0x%04x not "
259 "supported\n", device_xname(sc->sc_hdev.sc_dev),
260 flags);
261 sc->sc_loc_z.size = 0; /* Bad Z coord, ignore it */
262 } else {
263 sc->flags |= UMS_Z;
264 /* Wheels need the Z axis reversed. */
265 sc->flags ^= UMS_REVZ;
266 /* Put Z on the W coordinate */
267 zloc = &sc->sc_loc_w;
268 }
269 }
270
271 hl = hid_locate(desc,
272 size,
273 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Z),
274 uha->reportid,
275 hid_input,
276 zloc,
277 &flags);
278
279 /*
280 * The horizontal component of the scrollball can also be given by
281 * Application Control Pan in the Consumer page, so if we didnt see
282 * any Z then check that.
283 */
284 if (!hl) {
285 hl = hid_locate(desc,
286 size,
287 HID_USAGE2(HUP_CONSUMER, HUC_AC_PAN),
288 uha->reportid,
289 hid_input,
290 zloc,
291 &flags);
292 }
293
294 if (hl) {
295 if ((flags & MOUSE_FLAGS_MASK) != HIO_RELATIVE) {
296 aprint_verbose("\n%s: Z report 0x%04x not supported\n",
297 device_xname(sc->sc_hdev.sc_dev), flags);
298 zloc->size = 0; /* Bad Z coord, ignore it */
299 } else {
300 if (sc->flags & UMS_Z)
301 sc->flags |= UMS_W;
302 else
303 sc->flags |= UMS_Z;
304 }
305 }
306
307 /*
308 * The Microsoft Wireless Laser Mouse 6000 v2.0 reports a bad
309 * position for the wheel and wheel tilt controls -- should be
310 * in bytes 3 & 4 of the report. Fix this if necessary.
311 */
312 if (uha->uaa->vendor == USB_VENDOR_MICROSOFT &&
313 (uha->uaa->product == USB_PRODUCT_MICROSOFT_24GHZ_XCVR10 ||
314 uha->uaa->product == USB_PRODUCT_MICROSOFT_24GHZ_XCVR20)) {
315 if ((sc->flags & UMS_Z) && sc->sc_loc_z.pos == 0)
316 sc->sc_loc_z.pos = 24;
317 if ((sc->flags & UMS_W) && sc->sc_loc_w.pos == 0)
318 sc->sc_loc_w.pos = sc->sc_loc_z.pos + 8;
319 }
320
321 /* figure out the number of buttons */
322 for (i = 1; i <= MAX_BUTTONS; i++)
323 if (!hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
324 uha->reportid, hid_input, &sc->sc_loc_btn[i - 1], 0))
325 break;
326
327 if (isdigitizer) {
328 for (size_t j = 0; j < __arraycount(digbut); j++) {
329 if (hid_locate(desc, size, HID_USAGE2(HUP_DIGITIZERS,
330 digbut[j].feature), uha->reportid, hid_input,
331 &sc->sc_loc_btn[i - 1], 0)) {
332 if (i <= MAX_BUTTONS) {
333 i++;
334 sc->flags |= digbut[j].flag;
335 } else
336 aprint_error_dev(self,
337 "ran out of buttons\n");
338 }
339 }
340 }
341 sc->nbuttons = i - 1;
342
343 aprint_normal(": %d button%s%s%s%s%s%s%s%s%s\n",
344 sc->nbuttons, sc->nbuttons == 1 ? "" : "s",
345 sc->flags & UMS_W ? ", W" : "",
346 sc->flags & UMS_Z ? " and Z dir" : "",
347 sc->flags & UMS_W ? "s" : "",
348 isdigitizer ? " digitizer" : "",
349 sc->flags & UMS_TIP_SWITCH ? ", tip" : "",
350 sc->flags & UMS_SEC_TIP_SWITCH ? ", sec tip" : "",
351 sc->flags & UMS_BARREL_SWITCH ? ", barrel" : "",
352 sc->flags & UMS_ERASER ? ", eraser" : "");
353
354 #ifdef USB_DEBUG
355 DPRINTF(("ums_attach: sc=%p\n", sc));
356 DPRINTF(("ums_attach: X\t%d/%d\n",
357 sc->sc_loc_x.pos, sc->sc_loc_x.size));
358 DPRINTF(("ums_attach: Y\t%d/%d\n",
359 sc->sc_loc_y.pos, sc->sc_loc_y.size));
360 if (sc->flags & UMS_Z)
361 DPRINTF(("ums_attach: Z\t%d/%d\n",
362 sc->sc_loc_z.pos, sc->sc_loc_z.size));
363 if (sc->flags & UMS_W)
364 DPRINTF(("ums_attach: W\t%d/%d\n",
365 sc->sc_loc_w.pos, sc->sc_loc_w.size));
366 for (i = 1; i <= sc->nbuttons; i++) {
367 DPRINTF(("ums_attach: B%d\t%d/%d\n",
368 i, sc->sc_loc_btn[i-1].pos,sc->sc_loc_btn[i-1].size));
369 }
370 #endif
371
372 a.accessops = &ums_accessops;
373 a.accesscookie = sc;
374
375 sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
376
377 return;
378 }
379
380 int
381 ums_activate(device_t self, enum devact act)
382 {
383 struct ums_softc *sc = device_private(self);
384
385 switch (act) {
386 case DVACT_DEACTIVATE:
387 sc->sc_dying = 1;
388 return 0;
389 default:
390 return EOPNOTSUPP;
391 }
392 }
393
394 void
395 ums_childdet(device_t self, device_t child)
396 {
397 struct ums_softc *sc = device_private(self);
398
399 KASSERT(sc->sc_wsmousedev == child);
400 sc->sc_wsmousedev = NULL;
401 }
402
403 int
404 ums_detach(device_t self, int flags)
405 {
406 struct ums_softc *sc = device_private(self);
407 int rv = 0;
408
409 DPRINTF(("ums_detach: sc=%p flags=%d\n", sc, flags));
410
411 /* No need to do reference counting of ums, wsmouse has all the goo. */
412 if (sc->sc_wsmousedev != NULL)
413 rv = config_detach(sc->sc_wsmousedev, flags);
414
415 pmf_device_deregister(self);
416
417 return (rv);
418 }
419
420 void
421 ums_intr(struct uhidev *addr, void *ibuf, u_int len)
422 {
423 struct ums_softc *sc = (struct ums_softc *)addr;
424 int dx, dy, dz, dw;
425 u_int32_t buttons = 0;
426 int i, flags, s;
427
428 DPRINTFN(5,("ums_intr: len=%d\n", len));
429
430 flags = WSMOUSE_INPUT_DELTA; /* equals 0 */
431
432 dx = hid_get_data(ibuf, &sc->sc_loc_x);
433 if (sc->flags & UMS_ABS) {
434 flags |= (WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y);
435 dy = hid_get_data(ibuf, &sc->sc_loc_y);
436 } else
437 dy = -hid_get_data(ibuf, &sc->sc_loc_y);
438 dz = hid_get_data(ibuf, &sc->sc_loc_z);
439 dw = hid_get_data(ibuf, &sc->sc_loc_w);
440
441 if (sc->flags & UMS_REVZ)
442 dz = -dz;
443 for (i = 0; i < sc->nbuttons; i++)
444 if (hid_get_data(ibuf, &sc->sc_loc_btn[i]))
445 buttons |= (1 << UMS_BUT(i));
446
447 if (dx != 0 || dy != 0 || dz != 0 || dw != 0 ||
448 buttons != sc->sc_buttons) {
449 DPRINTFN(10, ("ums_intr: x:%d y:%d z:%d w:%d buttons:0x%x\n",
450 dx, dy, dz, dw, buttons));
451 sc->sc_buttons = buttons;
452 if (sc->sc_wsmousedev != NULL) {
453 s = spltty();
454 wsmouse_input(sc->sc_wsmousedev, buttons, dx, dy, dz,
455 dw, flags);
456 splx(s);
457 }
458 }
459 }
460
461 Static int
462 ums_enable(void *v)
463 {
464 struct ums_softc *sc = v;
465
466 DPRINTFN(1,("ums_enable: sc=%p\n", sc));
467
468 if (sc->sc_dying)
469 return (EIO);
470
471 if (sc->sc_enabled)
472 return (EBUSY);
473
474 sc->sc_enabled = 1;
475 sc->sc_buttons = 0;
476
477 return (uhidev_open(&sc->sc_hdev));
478 }
479
480 Static void
481 ums_disable(void *v)
482 {
483 struct ums_softc *sc = v;
484
485 DPRINTFN(1,("ums_disable: sc=%p\n", sc));
486 #ifdef DIAGNOSTIC
487 if (!sc->sc_enabled) {
488 printf("ums_disable: not enabled\n");
489 return;
490 }
491 #endif
492
493 sc->sc_enabled = 0;
494 uhidev_close(&sc->sc_hdev);
495 }
496
497 Static int
498 ums_ioctl(void *v, u_long cmd, void *data, int flag,
499 struct lwp * p)
500
501 {
502 struct ums_softc *sc = v;
503
504 switch (cmd) {
505 case WSMOUSEIO_GTYPE:
506 if (sc->flags & UMS_ABS)
507 *(u_int *)data = WSMOUSE_TYPE_TPANEL;
508 else
509 *(u_int *)data = WSMOUSE_TYPE_USB;
510 return (0);
511 }
512
513 return (EPASSTHROUGH);
514 }
515