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