ums.c revision 1.89 1 /* $NetBSD: ums.c,v 1.89 2016/04/23 10:15:32 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.89 2016/04/23 10:15:32 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 *, void *, u_int);
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->uiaa->uiaa_vendor == USB_VENDOR_GRIFFIN &&
156 uha->uiaa->uiaa_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 if (uha->uiaa->uiaa_vendor == USB_VENDOR_MICROSOFT) {
303 int fixpos;
304 /*
305 * The Microsoft Wireless Laser Mouse 6000 v2.0 and the
306 * Microsoft Comfort Mouse 2.0 report a bad position for
307 * the wheel and wheel tilt controls -- should be in bytes
308 * 3 & 4 of the report. Fix this if necessary.
309 */
310 switch (uha->uiaa->uiaa_product) {
311 case USB_PRODUCT_MICROSOFT_24GHZ_XCVR10:
312 case USB_PRODUCT_MICROSOFT_24GHZ_XCVR20:
313 fixpos = 24;
314 break;
315 case USB_PRODUCT_MICROSOFT_CM6000:
316 fixpos = 40;
317 break;
318 default:
319 fixpos = 0;
320 break;
321 }
322 if (fixpos) {
323 if ((sc->flags & UMS_Z) && sc->sc_loc_z.pos == 0)
324 sc->sc_loc_z.pos = fixpos;
325 if ((sc->flags & UMS_W) && sc->sc_loc_w.pos == 0)
326 sc->sc_loc_w.pos = sc->sc_loc_z.pos + 8;
327 }
328 }
329
330 /* figure out the number of buttons */
331 for (i = 1; i <= MAX_BUTTONS; i++)
332 if (!hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
333 uha->reportid, hid_input, &sc->sc_loc_btn[i - 1], 0))
334 break;
335
336 if (isdigitizer) {
337 for (size_t j = 0; j < __arraycount(digbut); j++) {
338 if (hid_locate(desc, size, HID_USAGE2(HUP_DIGITIZERS,
339 digbut[j].feature), uha->reportid, hid_input,
340 &sc->sc_loc_btn[i - 1], 0)) {
341 if (i <= MAX_BUTTONS) {
342 i++;
343 sc->flags |= digbut[j].flag;
344 } else
345 aprint_error_dev(self,
346 "ran out of buttons\n");
347 }
348 }
349 }
350 sc->nbuttons = i - 1;
351
352 aprint_normal(": %d button%s%s%s%s%s%s%s%s%s\n",
353 sc->nbuttons, sc->nbuttons == 1 ? "" : "s",
354 sc->flags & UMS_W ? ", W" : "",
355 sc->flags & UMS_Z ? " and Z dir" : "",
356 sc->flags & UMS_W ? "s" : "",
357 isdigitizer ? " digitizer" : "",
358 sc->flags & UMS_TIP_SWITCH ? ", tip" : "",
359 sc->flags & UMS_SEC_TIP_SWITCH ? ", sec tip" : "",
360 sc->flags & UMS_BARREL_SWITCH ? ", barrel" : "",
361 sc->flags & UMS_ERASER ? ", eraser" : "");
362
363 #ifdef UMS_DEBUG
364 DPRINTF(("ums_attach: sc=%p\n", sc));
365 DPRINTF(("ums_attach: X\t%d/%d\n",
366 sc->sc_loc_x.pos, sc->sc_loc_x.size));
367 DPRINTF(("ums_attach: Y\t%d/%d\n",
368 sc->sc_loc_y.pos, sc->sc_loc_y.size));
369 if (sc->flags & UMS_Z)
370 DPRINTF(("ums_attach: Z\t%d/%d\n",
371 sc->sc_loc_z.pos, sc->sc_loc_z.size));
372 if (sc->flags & UMS_W)
373 DPRINTF(("ums_attach: W\t%d/%d\n",
374 sc->sc_loc_w.pos, sc->sc_loc_w.size));
375 for (i = 1; i <= sc->nbuttons; i++) {
376 DPRINTF(("ums_attach: B%d\t%d/%d\n",
377 i, sc->sc_loc_btn[i-1].pos,sc->sc_loc_btn[i-1].size));
378 }
379 #endif
380
381 a.accessops = &ums_accessops;
382 a.accesscookie = sc;
383
384 sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
385
386 return;
387 }
388
389 int
390 ums_activate(device_t self, enum devact act)
391 {
392 struct ums_softc *sc = device_private(self);
393
394 switch (act) {
395 case DVACT_DEACTIVATE:
396 sc->sc_dying = 1;
397 return 0;
398 default:
399 return EOPNOTSUPP;
400 }
401 }
402
403 void
404 ums_childdet(device_t self, device_t child)
405 {
406 struct ums_softc *sc = device_private(self);
407
408 KASSERT(sc->sc_wsmousedev == child);
409 sc->sc_wsmousedev = NULL;
410 }
411
412 int
413 ums_detach(device_t self, int flags)
414 {
415 struct ums_softc *sc = device_private(self);
416 int rv = 0;
417
418 DPRINTF(("ums_detach: sc=%p flags=%d\n", sc, flags));
419
420 /* No need to do reference counting of ums, wsmouse has all the goo. */
421 if (sc->sc_wsmousedev != NULL)
422 rv = config_detach(sc->sc_wsmousedev, flags);
423
424 pmf_device_deregister(self);
425
426 return rv;
427 }
428
429 void
430 ums_intr(struct uhidev *addr, void *ibuf, u_int len)
431 {
432 struct ums_softc *sc = (struct ums_softc *)addr;
433 int dx, dy, dz, dw;
434 uint32_t buttons = 0;
435 int i, flags, s;
436
437 DPRINTFN(5,("ums_intr: len=%d\n", len));
438
439 flags = WSMOUSE_INPUT_DELTA; /* equals 0 */
440
441 dx = hid_get_data(ibuf, &sc->sc_loc_x);
442 if (sc->flags & UMS_ABS) {
443 flags |= (WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y);
444 dy = hid_get_data(ibuf, &sc->sc_loc_y);
445 } else
446 dy = -hid_get_data(ibuf, &sc->sc_loc_y);
447 dz = hid_get_data(ibuf, &sc->sc_loc_z);
448 dw = hid_get_data(ibuf, &sc->sc_loc_w);
449
450 if (sc->flags & UMS_REVZ)
451 dz = -dz;
452 for (i = 0; i < sc->nbuttons; i++)
453 if (hid_get_data(ibuf, &sc->sc_loc_btn[i]))
454 buttons |= (1 << UMS_BUT(i));
455
456 if (dx != 0 || dy != 0 || dz != 0 || dw != 0 ||
457 buttons != sc->sc_buttons) {
458 DPRINTFN(10, ("ums_intr: x:%d y:%d z:%d w:%d buttons:0x%x\n",
459 dx, dy, dz, dw, buttons));
460 sc->sc_buttons = buttons;
461 if (sc->sc_wsmousedev != NULL) {
462 s = spltty();
463 wsmouse_input(sc->sc_wsmousedev, buttons, dx, dy, dz,
464 dw, flags);
465 splx(s);
466 }
467 }
468 }
469
470 Static int
471 ums_enable(void *v)
472 {
473 struct ums_softc *sc = v;
474 int error;
475
476 DPRINTFN(1,("ums_enable: sc=%p\n", sc));
477
478 if (sc->sc_dying)
479 return EIO;
480
481 if (sc->sc_enabled)
482 return EBUSY;
483
484 sc->sc_enabled = 1;
485 sc->sc_buttons = 0;
486
487 error = uhidev_open(&sc->sc_hdev);
488 if (error)
489 sc->sc_enabled = 0;
490
491 return error;
492 }
493
494 Static void
495 ums_disable(void *v)
496 {
497 struct ums_softc *sc = v;
498
499 DPRINTFN(1,("ums_disable: sc=%p\n", sc));
500 #ifdef DIAGNOSTIC
501 if (!sc->sc_enabled) {
502 printf("ums_disable: not enabled\n");
503 return;
504 }
505 #endif
506
507 if (sc->sc_enabled) {
508 sc->sc_enabled = 0;
509 uhidev_close(&sc->sc_hdev);
510 }
511 }
512
513 Static int
514 ums_ioctl(void *v, u_long cmd, void *data, int flag,
515 struct lwp * p)
516
517 {
518 struct ums_softc *sc = v;
519
520 switch (cmd) {
521 case WSMOUSEIO_GTYPE:
522 if (sc->flags & UMS_ABS)
523 *(u_int *)data = WSMOUSE_TYPE_TPANEL;
524 else
525 *(u_int *)data = WSMOUSE_TYPE_USB;
526 return 0;
527 }
528
529 return EPASSTHROUGH;
530 }
531