xref: /openbsd-src/sys/dev/usb/ums.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: ums.c,v 1.29 2008/06/26 05:42:19 ray Exp $ */
2 /*	$NetBSD: ums.c,v 1.60 2003/03/11 16:44:00 augustss Exp $	*/
3 
4 /*
5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Lennart Augustsson (lennart@augustsson.net) at
10  * Carlstedt Research & Technology.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
36  */
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/device.h>
42 #include <sys/ioctl.h>
43 #include <sys/tty.h>
44 #include <sys/file.h>
45 #include <sys/selinfo.h>
46 #include <sys/proc.h>
47 #include <sys/vnode.h>
48 #include <sys/poll.h>
49 
50 #include <dev/usb/usb.h>
51 #include <dev/usb/usbhid.h>
52 
53 #include <dev/usb/usbdi.h>
54 #include <dev/usb/usbdi_util.h>
55 #include <dev/usb/usbdevs.h>
56 #include <dev/usb/usb_quirks.h>
57 #include <dev/usb/uhidev.h>
58 #include <dev/usb/hid.h>
59 
60 #include <dev/wscons/wsconsio.h>
61 #include <dev/wscons/wsmousevar.h>
62 
63 #ifdef USB_DEBUG
64 #define DPRINTF(x)	do { if (umsdebug) printf x; } while (0)
65 #define DPRINTFN(n,x)	do { if (umsdebug>(n)) printf x; } while (0)
66 int	umsdebug = 0;
67 #else
68 #define DPRINTF(x)
69 #define DPRINTFN(n,x)
70 #endif
71 
72 #define UMS_BUT(i) ((i) == 1 || (i) == 2 ? 3 - (i) : i)
73 
74 #define UMSUNIT(s)	(minor(s))
75 
76 #define MAX_BUTTONS	16	/* must not exceed size of sc_buttons */
77 
78 struct ums_softc {
79 	struct uhidev sc_hdev;
80 
81 	struct hid_location sc_loc_x, sc_loc_y, sc_loc_z, sc_loc_w;
82 	struct hid_location sc_loc_btn[MAX_BUTTONS];
83 
84 	int sc_enabled;
85 
86 	int flags;		/* device configuration */
87 #define UMS_Z		0x01	/* Z direction available */
88 #define UMS_SPUR_BUT_UP	0x02	/* spurious button up events */
89 #define UMS_REVZ	0x04	/* Z-axis is reversed */
90 #define UMS_W		0x08	/* W direction available */
91 #define UMS_REVW	0x10	/* W-axis is reversed */
92 #define UMS_LEADINGBYTE	0x20	/* Unknown leading byte */
93 
94 	int nbuttons;
95 
96 	u_int32_t sc_buttons;	/* mouse button status */
97 	struct device *sc_wsmousedev;
98 
99 	char			sc_dying;
100 };
101 
102 #define MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
103 #define MOUSE_FLAGS (HIO_RELATIVE)
104 
105 void ums_intr(struct uhidev *addr, void *ibuf, u_int len);
106 
107 int	ums_enable(void *);
108 void	ums_disable(void *);
109 int	ums_ioctl(void *, u_long, caddr_t, int, struct proc *);
110 
111 const struct wsmouse_accessops ums_accessops = {
112 	ums_enable,
113 	ums_ioctl,
114 	ums_disable,
115 };
116 
117 int ums_match(struct device *, void *, void *);
118 void ums_attach(struct device *, struct device *, void *);
119 int ums_detach(struct device *, int);
120 int ums_activate(struct device *, enum devact);
121 
122 struct cfdriver ums_cd = {
123 	NULL, "ums", DV_DULL
124 };
125 
126 const struct cfattach ums_ca = {
127 	sizeof(struct ums_softc),
128 	ums_match,
129 	ums_attach,
130 	ums_detach,
131 	ums_activate,
132 };
133 
134 int
135 ums_match(struct device *parent, void *match, void *aux)
136 {
137 	struct usb_attach_arg *uaa = aux;
138 	struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)uaa;
139 	int size;
140 	void *desc;
141 
142 	uhidev_get_report_desc(uha->parent, &desc, &size);
143 	if (!hid_is_collection(desc, size, uha->reportid,
144 			       HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)))
145 		return (UMATCH_NONE);
146 
147 	return (UMATCH_IFACECLASS);
148 }
149 
150 void
151 ums_attach(struct device *parent, struct device *self, void *aux)
152 {
153 	struct ums_softc *sc = (struct ums_softc *)self;
154 	struct usb_attach_arg *uaa = aux;
155 	struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)uaa;
156 	struct wsmousedev_attach_args a;
157 	int size;
158 	void *desc;
159 	u_int32_t flags, quirks;
160 	int i, wheel, twheel;
161 	struct hid_location loc_btn;
162 
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 	if (quirks & UQ_MS_LEADING_BYTE)
173 		sc->flags |= UMS_LEADINGBYTE;
174 
175 	uhidev_get_report_desc(uha->parent, &desc, &size);
176 
177 	if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
178 	       uha->reportid, hid_input, &sc->sc_loc_x, &flags)) {
179 		printf("\n%s: mouse has no X report\n",
180 		       sc->sc_hdev.sc_dev.dv_xname);
181 		return;
182 	}
183 	if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
184 		printf("\n%s: X report 0x%04x not supported\n",
185 		       sc->sc_hdev.sc_dev.dv_xname, flags);
186 		return;
187 	}
188 
189 	if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
190 	       uha->reportid, hid_input, &sc->sc_loc_y, &flags)) {
191 		printf("\n%s: mouse has no Y report\n",
192 		       sc->sc_hdev.sc_dev.dv_xname);
193 		return;
194 	}
195 	if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
196 		printf("\n%s: Y report 0x%04x not supported\n",
197 		       sc->sc_hdev.sc_dev.dv_xname, flags);
198 		return;
199 	}
200 
201 	/*
202 	 * Try to guess the Z activator: check WHEEL, TWHEEL, and Z,
203 	 * in that order.
204 	 */
205 
206 	wheel = hid_locate(desc, size,
207 	    HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_WHEEL),
208 	    uha->reportid, hid_input, &sc->sc_loc_z, &flags);
209 	if (wheel == 0)
210 		twheel = hid_locate(desc, size,
211 		    HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_TWHEEL),
212 		    uha->reportid, hid_input, &sc->sc_loc_z, &flags);
213 	else
214 		twheel = 0;
215 
216 	if (wheel || twheel) {
217 		if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
218 			DPRINTF(("\n%s: Wheel report 0x%04x not supported\n",
219 				sc->sc_hdev.sc_dev.dv_xname, flags));
220 			sc->sc_loc_z.size = 0; /* Bad Z coord, ignore it */
221 		} else {
222 			sc->flags |= UMS_Z;
223 			/* Wheels need the Z axis reversed. */
224 			sc->flags ^= UMS_REVZ;
225 		}
226 		/*
227 		 * We might have both a wheel and Z direction; in this case,
228 		 * report the Z direction on the W axis.
229 		*/
230 		if (hid_locate(desc, size,
231 		    HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Z),
232 		    uha->reportid, hid_input, &sc->sc_loc_w, &flags)) {
233 			if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
234 				DPRINTF(("\n%s: Z report 0x%04x not supported\n",
235 					sc->sc_hdev.sc_dev.dv_xname, flags));
236 				/* Bad Z coord, ignore it */
237 				sc->sc_loc_w.size = 0;
238 			}
239 			else
240 				sc->flags |= UMS_W;
241 		}
242 	} else if (hid_locate(desc, size,
243 	    HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Z),
244 	    uha->reportid, hid_input, &sc->sc_loc_z, &flags)) {
245 		if ((flags & MOUSE_FLAGS_MASK) != MOUSE_FLAGS) {
246 			DPRINTF(("\n%s: Z report 0x%04x not supported\n",
247 				sc->sc_hdev.sc_dev.dv_xname, flags));
248 			sc->sc_loc_z.size = 0; /* Bad Z coord, ignore it */
249 		} else {
250 			sc->flags |= UMS_Z;
251 		}
252 	}
253 
254 	/*
255 	 * The Microsoft Wireless Intellimouse 2.0 reports its wheel
256 	 * using 0x0048 (I've called it HUG_TWHEEL) and seems to expect
257 	 * us to know that the byte after the wheel is the tilt axis.
258 	 * There are no other HID axis descriptors other than X, Y and
259 	 * TWHEEL, so we report TWHEEL on the W axis.
260 	 */
261 	if (twheel) {
262 		sc->sc_loc_w = sc->sc_loc_z;
263 		sc->sc_loc_w.pos = sc->sc_loc_w.pos + 8;
264 		sc->flags |= UMS_W | UMS_LEADINGBYTE;
265 		/* Wheels need their axis reversed. */
266 		sc->flags ^= UMS_REVW;
267 	}
268 
269 	/* figure out the number of buttons */
270 	for (i = 1; i <= MAX_BUTTONS; i++)
271 		if (!hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
272 			uha->reportid, hid_input, &loc_btn, 0))
273 			break;
274 	sc->nbuttons = i - 1;
275 
276 	/*
277 	 * The Microsoft Wireless Notebook Optical Mouse seems to be in worse
278 	 * shape than the Wireless Intellimouse 2.0, as its X, Y, wheel, and
279 	 * all of its other button positions are all off. It also reports that
280 	 * it has two addional buttons and a tilt wheel.
281 	 */
282 	if (quirks & UQ_MS_BAD_CLASS) {
283 		/* UMS_LEADINGBYTE cleared on purpose */
284 		sc->flags = UMS_Z | UMS_SPUR_BUT_UP;
285 		sc->nbuttons = 3;
286 		/* XXX change sc_hdev isize to 5? */
287 		/* 1st byte of descriptor report contains garbage */
288 		sc->sc_loc_x.pos = 16;
289 		sc->sc_loc_y.pos = 24;
290 		sc->sc_loc_z.pos = 32;
291 		sc->sc_loc_btn[0].pos = 8;
292 		sc->sc_loc_btn[1].pos = 9;
293 		sc->sc_loc_btn[2].pos = 10;
294 	}
295 
296 	/*
297 	 * The Microsoft Wireless Notebook Optical Mouse 3000 Model 1049 has
298 	 * five Report IDs: 19, 23, 24, 17, 18 (in the order they appear in
299 	 * report descriptor), it seems that report 17 contains the necessary
300 	 * mouse information (3-buttons, X, Y, wheel) so we specify it
301 	 * manually.
302 	 */
303 	if (uaa->vendor == USB_VENDOR_MICROSOFT &&
304 	    uaa->product == USB_PRODUCT_MICROSOFT_WLNOTEBOOK3) {
305 		sc->flags = UMS_Z;
306 		sc->nbuttons = 3;
307 		/* XXX change sc_hdev isize to 5? */
308 		sc->sc_loc_x.pos = 8;
309 		sc->sc_loc_y.pos = 16;
310 		sc->sc_loc_z.pos = 24;
311 		sc->sc_loc_btn[0].pos = 0;
312 		sc->sc_loc_btn[1].pos = 1;
313 		sc->sc_loc_btn[2].pos = 2;
314 	}
315 
316 	printf(": %d button%s",
317 	    sc->nbuttons, sc->nbuttons <= 1 ? "" : "s");
318 	switch (sc->flags & (UMS_Z | UMS_W)) {
319 	case UMS_Z:
320 		printf(", Z dir");
321 		break;
322 	case UMS_W:
323 		printf(", W dir");
324 		break;
325 	case UMS_Z | UMS_W:
326 		printf(", Z and W dir");
327 		break;
328 	}
329 	printf("\n");
330 
331 	for (i = 1; i <= sc->nbuttons; i++)
332 		hid_locate(desc, size, HID_USAGE2(HUP_BUTTON, i),
333 			   uha->reportid, hid_input,
334 			   &sc->sc_loc_btn[i-1], 0);
335 
336 #ifdef USB_DEBUG
337 	DPRINTF(("ums_attach: sc=%p\n", sc));
338 	DPRINTF(("ums_attach: X\t%d/%d\n",
339 		 sc->sc_loc_x.pos, sc->sc_loc_x.size));
340 	DPRINTF(("ums_attach: Y\t%d/%d\n",
341 		 sc->sc_loc_y.pos, sc->sc_loc_y.size));
342 	if (sc->flags & UMS_Z)
343 		DPRINTF(("ums_attach: Z\t%d/%d\n",
344 			 sc->sc_loc_z.pos, sc->sc_loc_z.size));
345 	if (sc->flags & UMS_W)
346 		DPRINTF(("ums_attach: W\t%d/%d\n",
347 			 sc->sc_loc_w.pos, sc->sc_loc_w.size));
348 	for (i = 1; i <= sc->nbuttons; i++) {
349 		DPRINTF(("ums_attach: B%d\t%d/%d\n",
350 			 i, sc->sc_loc_btn[i-1].pos,sc->sc_loc_btn[i-1].size));
351 	}
352 #endif
353 
354 	a.accessops = &ums_accessops;
355 	a.accesscookie = sc;
356 
357 	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
358 }
359 
360 int
361 ums_activate(struct device *self, enum devact act)
362 {
363 	struct ums_softc *sc = (struct ums_softc *)self;
364 	int rv = 0;
365 
366 	switch (act) {
367 	case DVACT_ACTIVATE:
368 		break;
369 
370 	case DVACT_DEACTIVATE:
371 		if (sc->sc_wsmousedev != NULL)
372 			rv = config_deactivate(sc->sc_wsmousedev);
373 		sc->sc_dying = 1;
374 		break;
375 	}
376 	return (rv);
377 }
378 
379 int
380 ums_detach(struct device *self, int flags)
381 {
382 	struct ums_softc *sc = (struct ums_softc *)self;
383 	int rv = 0;
384 
385 	DPRINTF(("ums_detach: sc=%p flags=%d\n", sc, flags));
386 
387 	/* No need to do reference counting of ums, wsmouse has all the goo. */
388 	if (sc->sc_wsmousedev != NULL)
389 		rv = config_detach(sc->sc_wsmousedev, flags);
390 
391 	return (rv);
392 }
393 
394 void
395 ums_intr(struct uhidev *addr, void *buf, u_int len)
396 {
397 	struct ums_softc *sc = (struct ums_softc *)addr;
398 	u_char *ibuf = (u_char *)buf;
399 	int dx, dy, dz, dw;
400 	u_int32_t buttons = 0;
401 	int i;
402 	int s;
403 
404 	DPRINTFN(5,("ums_intr: len=%d\n", len));
405 
406 	/*
407 	 * The Microsoft Wireless Intellimouse 2.0 sends one extra leading
408 	 * byte of data compared to most USB mice.  This byte frequently
409 	 * switches from 0x01 (usual state) to 0x02.  It may be used to
410 	 * report non-standard events (such as battery life).  However,
411 	 * at the same time, it generates a left click event on the
412 	 * button byte, where there shouldn't be any.  We simply discard
413 	 * the packet in this case.
414 	 *
415 	 * This problem affects the MS Wireless Notebook Optical Mouse, too.
416 	 * However, the leading byte for this mouse is normally 0x11, and
417 	 * the phantom mouse click occurs when it's 0x14.
418 	 */
419 	if (sc->flags & UMS_LEADINGBYTE) {
420 		if (*ibuf++ == 0x02)
421 			return;
422 		/* len--; */
423 	} else if (sc->flags & UMS_SPUR_BUT_UP) {
424 		if (*ibuf == 0x14 || *ibuf == 0x15)
425 			return;
426 	}
427 
428 	dx =  hid_get_data(ibuf, &sc->sc_loc_x);
429 	dy = -hid_get_data(ibuf, &sc->sc_loc_y);
430 	dz =  hid_get_data(ibuf, &sc->sc_loc_z);
431 	dw =  hid_get_data(ibuf, &sc->sc_loc_w);
432 	if (sc->flags & UMS_REVZ)
433 		dz = -dz;
434 	if (sc->flags & UMS_REVW)
435 		dw = -dw;
436 	for (i = 0; i < sc->nbuttons; i++)
437 		if (hid_get_data(ibuf, &sc->sc_loc_btn[i]))
438 			buttons |= (1 << UMS_BUT(i));
439 
440 	if (dx != 0 || dy != 0 || dz != 0 || dw != 0 ||
441 	    buttons != sc->sc_buttons) {
442 		DPRINTFN(10, ("ums_intr: x:%d y:%d z:%d w:%d buttons:0x%x\n",
443 			dx, dy, dz, dw, buttons));
444 		sc->sc_buttons = buttons;
445 		if (sc->sc_wsmousedev != NULL) {
446 			s = spltty();
447 			wsmouse_input(sc->sc_wsmousedev, buttons,
448 			    dx, dy, dz, dw, WSMOUSE_INPUT_DELTA);
449 			splx(s);
450 		}
451 	}
452 }
453 
454 int
455 ums_enable(void *v)
456 {
457 	struct ums_softc *sc = v;
458 
459 	DPRINTFN(1,("ums_enable: sc=%p\n", sc));
460 
461 	if (sc->sc_dying)
462 		return (EIO);
463 
464 	if (sc->sc_enabled)
465 		return (EBUSY);
466 
467 	sc->sc_enabled = 1;
468 	sc->sc_buttons = 0;
469 
470 	return (uhidev_open(&sc->sc_hdev));
471 }
472 
473 void
474 ums_disable(void *v)
475 {
476 	struct ums_softc *sc = v;
477 
478 	DPRINTFN(1,("ums_disable: sc=%p\n", sc));
479 #ifdef DIAGNOSTIC
480 	if (!sc->sc_enabled) {
481 		printf("ums_disable: not enabled\n");
482 		return;
483 	}
484 #endif
485 
486 	sc->sc_enabled = 0;
487 	uhidev_close(&sc->sc_hdev);
488 }
489 
490 int
491 ums_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
492 
493 {
494 	switch (cmd) {
495 	case WSMOUSEIO_GTYPE:
496 		*(u_int *)data = WSMOUSE_TYPE_USB;
497 		return (0);
498 	}
499 
500 	return (-1);
501 }
502