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