ums.c revision 1.73.8.1 1 /* $NetBSD: ums.c,v 1.73.8.1 2009/11/27 08:54:13 sborrill 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.1 2009/11/27 08:54:13 sborrill 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
100 int nbuttons;
101
102 u_int32_t sc_buttons; /* mouse button status */
103 device_t sc_wsmousedev;
104
105 char sc_dying;
106 };
107
108 #define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
109 #define MOUSE_FLAGS (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 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
185 aprint_error("\n%s: X report 0x%04x not supported\n",
186 USBDEVNAME(sc->sc_hdev.sc_dev), flags);
187 USB_ATTACH_ERROR_RETURN;
188 }
189
190 if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
191 uha->reportid, hid_input, &sc->sc_loc_y, &flags)) {
192 aprint_error("\n%s: mouse has no Y report\n",
193 USBDEVNAME(sc->sc_hdev.sc_dev));
194 USB_ATTACH_ERROR_RETURN;
195 }
196 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
197 aprint_error("\n%s: Y report 0x%04x not supported\n",
198 USBDEVNAME(sc->sc_hdev.sc_dev), flags);
199 USB_ATTACH_ERROR_RETURN;
200 }
201
202 /* Try the wheel first as the Z activator since it's tradition. */
203 hl = hid_locate(desc,
204 size,
205 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_WHEEL),
206 uha->reportid,
207 hid_input,
208 &sc->sc_loc_z,
209 &flags);
210
211 zloc = &sc->sc_loc_z;
212 if (hl) {
213 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
214 aprint_verbose("\n%s: Wheel report 0x%04x not "
215 "supported\n", USBDEVNAME(sc->sc_hdev.sc_dev),
216 flags);
217 sc->sc_loc_z.size = 0; /* Bad Z coord, ignore it */
218 } else {
219 sc->flags |= UMS_Z;
220 /* Wheels need the Z axis reversed. */
221 sc->flags ^= UMS_REVZ;
222 /* Put Z on the W coordinate */
223 zloc = &sc->sc_loc_w;
224 }
225 }
226
227 hl = hid_locate(desc,
228 size,
229 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Z),
230 uha->reportid,
231 hid_input,
232 zloc,
233 &flags);
234
235 /*
236 * The horizontal component of the scrollball can also be given by
237 * Application Control Pan in the Consumer page, so if we didnt see
238 * any Z then check that.
239 */
240 if (!hl) {
241 hl = hid_locate(desc,
242 size,
243 HID_USAGE2(HUP_CONSUMER, HUC_AC_PAN),
244 uha->reportid,
245 hid_input,
246 zloc,
247 &flags);
248 }
249
250 if (hl) {
251 if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
252 aprint_verbose("\n%s: Z report 0x%04x not supported\n",
253 USBDEVNAME(sc->sc_hdev.sc_dev), flags);
254 zloc->size = 0; /* Bad Z coord, ignore it */
255 } else {
256 if (sc->flags & UMS_Z)
257 sc->flags |= UMS_W;
258 else
259 sc->flags |= UMS_Z;
260 }
261 }
262
263 /*
264 * The Microsoft Wireless Laser Mouse 6000 v2.0 reports a bad
265 * position for the wheel and wheel tilt controls -- should be
266 * in bytes 3 & 4 of the report. Fix this if necessary.
267 */
268 if (uha->uaa->vendor == USB_VENDOR_MICROSOFT &&
269 uha->uaa->product == USB_PRODUCT_MICROSOFT_24GHZ_XCVR) {
270 if ((sc->flags & UMS_Z) && sc->sc_loc_z.pos == 0)
271 sc->sc_loc_z.pos = 24;
272 if ((sc->flags & UMS_W) && sc->sc_loc_w.pos == 0)
273 sc->sc_loc_w.pos = sc->sc_loc_z.pos + 8;
274 }
275
276 /* figure out the number of buttons */
277 for (i = 1; i <= MAX_BUTTONS; i++)
278 if (!hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
279 uha->reportid, hid_input, &loc_btn, 0))
280 break;
281 sc->nbuttons = i - 1;
282
283 aprint_normal(": %d button%s%s%s%s\n",
284 sc->nbuttons, sc->nbuttons == 1 ? "" : "s",
285 sc->flags & UMS_W ? ", W" : "",
286 sc->flags & UMS_Z ? " and Z dir" : "",
287 sc->flags & UMS_W ? "s" : "");
288
289 for (i = 1; i <= sc->nbuttons; i++)
290 hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
291 uha->reportid, hid_input,
292 &sc->sc_loc_btn[i-1], 0);
293
294 #ifdef USB_DEBUG
295 DPRINTF(("ums_attach: sc=%p\n", sc));
296 DPRINTF(("ums_attach: X\t%d/%d\n",
297 sc->sc_loc_x.pos, sc->sc_loc_x.size));
298 DPRINTF(("ums_attach: Y\t%d/%d\n",
299 sc->sc_loc_y.pos, sc->sc_loc_y.size));
300 if (sc->flags & UMS_Z)
301 DPRINTF(("ums_attach: Z\t%d/%d\n",
302 sc->sc_loc_z.pos, sc->sc_loc_z.size));
303 if (sc->flags & UMS_W)
304 DPRINTF(("ums_attach: W\t%d/%d\n",
305 sc->sc_loc_w.pos, sc->sc_loc_w.size));
306 for (i = 1; i <= sc->nbuttons; i++) {
307 DPRINTF(("ums_attach: B%d\t%d/%d\n",
308 i, sc->sc_loc_btn[i-1].pos,sc->sc_loc_btn[i-1].size));
309 }
310 #endif
311
312 a.accessops = &ums_accessops;
313 a.accesscookie = sc;
314
315 sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
316
317 USB_ATTACH_SUCCESS_RETURN;
318 }
319
320 int
321 ums_activate(device_ptr_t self, enum devact act)
322 {
323 struct ums_softc *sc = device_private(self);
324 int rv = 0;
325
326 switch (act) {
327 case DVACT_ACTIVATE:
328 return (EOPNOTSUPP);
329
330 case DVACT_DEACTIVATE:
331 if (sc->sc_wsmousedev != NULL)
332 rv = config_deactivate(sc->sc_wsmousedev);
333 sc->sc_dying = 1;
334 break;
335 }
336 return (rv);
337 }
338
339 void
340 ums_childdet(device_t self, device_t child)
341 {
342 struct ums_softc *sc = device_private(self);
343
344 KASSERT(sc->sc_wsmousedev == child);
345 sc->sc_wsmousedev = NULL;
346 }
347
348 int
349 ums_detach(device_t self, int flags)
350 {
351 struct ums_softc *sc = device_private(self);
352 int rv = 0;
353
354 DPRINTF(("ums_detach: sc=%p flags=%d\n", sc, flags));
355
356 /* No need to do reference counting of ums, wsmouse has all the goo. */
357 if (sc->sc_wsmousedev != NULL)
358 rv = config_detach(sc->sc_wsmousedev, flags);
359
360 pmf_device_deregister(self);
361
362 return (rv);
363 }
364
365 void
366 ums_intr(struct uhidev *addr, void *ibuf, u_int len)
367 {
368 struct ums_softc *sc = (struct ums_softc *)addr;
369 int dx, dy, dz, dw;
370 u_int32_t buttons = 0;
371 int i;
372 int s;
373
374 DPRINTFN(5,("ums_intr: len=%d\n", len));
375
376 dx = hid_get_data(ibuf, &sc->sc_loc_x);
377 dy = -hid_get_data(ibuf, &sc->sc_loc_y);
378 dz = hid_get_data(ibuf, &sc->sc_loc_z);
379 dw = hid_get_data(ibuf, &sc->sc_loc_w);
380 if (sc->flags & UMS_REVZ)
381 dz = -dz;
382 for (i = 0; i < sc->nbuttons; i++)
383 if (hid_get_data(ibuf, &sc->sc_loc_btn[i]))
384 buttons |= (1 << UMS_BUT(i));
385
386 if (dx != 0 || dy != 0 || dz != 0 || dw != 0 ||
387 buttons != sc->sc_buttons) {
388 DPRINTFN(10, ("ums_intr: x:%d y:%d z:%d w:%d buttons:0x%x\n",
389 dx, dy, dz, dw, buttons));
390 sc->sc_buttons = buttons;
391 if (sc->sc_wsmousedev != NULL) {
392 s = spltty();
393 wsmouse_input(sc->sc_wsmousedev,
394 buttons,
395 dx, dy, dz, dw,
396 WSMOUSE_INPUT_DELTA);
397 splx(s);
398 }
399 }
400 }
401
402 Static int
403 ums_enable(void *v)
404 {
405 struct ums_softc *sc = v;
406
407 DPRINTFN(1,("ums_enable: sc=%p\n", sc));
408
409 if (sc->sc_dying)
410 return (EIO);
411
412 if (sc->sc_enabled)
413 return (EBUSY);
414
415 sc->sc_enabled = 1;
416 sc->sc_buttons = 0;
417
418 return (uhidev_open(&sc->sc_hdev));
419 }
420
421 Static void
422 ums_disable(void *v)
423 {
424 struct ums_softc *sc = v;
425
426 DPRINTFN(1,("ums_disable: sc=%p\n", sc));
427 #ifdef DIAGNOSTIC
428 if (!sc->sc_enabled) {
429 printf("ums_disable: not enabled\n");
430 return;
431 }
432 #endif
433
434 sc->sc_enabled = 0;
435 uhidev_close(&sc->sc_hdev);
436 }
437
438 Static int
439 ums_ioctl(void *v, u_long cmd, void *data, int flag,
440 struct lwp * p)
441
442 {
443 switch (cmd) {
444 case WSMOUSEIO_GTYPE:
445 *(u_int *)data = WSMOUSE_TYPE_USB;
446 return (0);
447 }
448
449 return (EPASSTHROUGH);
450 }
451