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