xref: /freebsd/sys/dev/usb/input/ums.c (revision 884a2a699669ec61e2366e3e358342dbc94be24a)
1 /*-
2  * Copyright (c) 1998 The NetBSD Foundation, Inc.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to The NetBSD Foundation
6  * by Lennart Augustsson (lennart@augustsson.net) at
7  * Carlstedt Research & Technology.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 /*
35  * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
36  */
37 
38 #include <sys/stdint.h>
39 #include <sys/stddef.h>
40 #include <sys/param.h>
41 #include <sys/queue.h>
42 #include <sys/types.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/bus.h>
46 #include <sys/module.h>
47 #include <sys/lock.h>
48 #include <sys/mutex.h>
49 #include <sys/condvar.h>
50 #include <sys/sysctl.h>
51 #include <sys/sx.h>
52 #include <sys/unistd.h>
53 #include <sys/callout.h>
54 #include <sys/malloc.h>
55 #include <sys/priv.h>
56 #include <sys/conf.h>
57 #include <sys/fcntl.h>
58 #include <sys/sbuf.h>
59 
60 #include <dev/usb/usb.h>
61 #include <dev/usb/usbdi.h>
62 #include <dev/usb/usbdi_util.h>
63 #include <dev/usb/usbhid.h>
64 #include "usbdevs.h"
65 
66 #define	USB_DEBUG_VAR ums_debug
67 #include <dev/usb/usb_debug.h>
68 
69 #include <dev/usb/quirk/usb_quirk.h>
70 
71 #include <sys/ioccom.h>
72 #include <sys/filio.h>
73 #include <sys/tty.h>
74 #include <sys/mouse.h>
75 
76 #ifdef USB_DEBUG
77 static int ums_debug = 0;
78 
79 SYSCTL_NODE(_hw_usb, OID_AUTO, ums, CTLFLAG_RW, 0, "USB ums");
80 SYSCTL_INT(_hw_usb_ums, OID_AUTO, debug, CTLFLAG_RW,
81     &ums_debug, 0, "Debug level");
82 #endif
83 
84 #define	MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
85 #define	MOUSE_FLAGS (HIO_RELATIVE)
86 
87 #define	UMS_BUF_SIZE      8		/* bytes */
88 #define	UMS_IFQ_MAXLEN   50		/* units */
89 #define	UMS_BUTTON_MAX   31		/* exclusive, must be less than 32 */
90 #define	UMS_BUT(i) ((i) < 3 ? (((i) + 2) % 3) : (i))
91 #define	UMS_INFO_MAX	  2		/* maximum number of HID sets */
92 
93 enum {
94 	UMS_INTR_DT,
95 	UMS_N_TRANSFER,
96 };
97 
98 struct ums_info {
99 	struct hid_location sc_loc_w;
100 	struct hid_location sc_loc_x;
101 	struct hid_location sc_loc_y;
102 	struct hid_location sc_loc_z;
103 	struct hid_location sc_loc_t;
104 	struct hid_location sc_loc_btn[UMS_BUTTON_MAX];
105 
106 	uint32_t sc_flags;
107 #define	UMS_FLAG_X_AXIS     0x0001
108 #define	UMS_FLAG_Y_AXIS     0x0002
109 #define	UMS_FLAG_Z_AXIS     0x0004
110 #define	UMS_FLAG_T_AXIS     0x0008
111 #define	UMS_FLAG_SBU        0x0010	/* spurious button up events */
112 #define	UMS_FLAG_REVZ	    0x0020	/* Z-axis is reversed */
113 #define	UMS_FLAG_W_AXIS     0x0040
114 
115 	uint8_t	sc_iid_w;
116 	uint8_t	sc_iid_x;
117 	uint8_t	sc_iid_y;
118 	uint8_t	sc_iid_z;
119 	uint8_t	sc_iid_t;
120 	uint8_t	sc_iid_btn[UMS_BUTTON_MAX];
121 	uint8_t	sc_buttons;
122 };
123 
124 struct ums_softc {
125 	struct usb_fifo_sc sc_fifo;
126 	struct mtx sc_mtx;
127 	struct usb_callout sc_callout;
128 	struct ums_info sc_info[UMS_INFO_MAX];
129 
130 	mousehw_t sc_hw;
131 	mousemode_t sc_mode;
132 	mousestatus_t sc_status;
133 
134 	struct usb_xfer *sc_xfer[UMS_N_TRANSFER];
135 
136 	int sc_pollrate;
137 
138 	uint8_t	sc_buttons;
139 	uint8_t	sc_iid;
140 	uint8_t	sc_temp[64];
141 };
142 
143 static void ums_put_queue_timeout(void *__sc);
144 
145 static usb_callback_t ums_intr_callback;
146 
147 static device_probe_t ums_probe;
148 static device_attach_t ums_attach;
149 static device_detach_t ums_detach;
150 
151 static usb_fifo_cmd_t ums_start_read;
152 static usb_fifo_cmd_t ums_stop_read;
153 static usb_fifo_open_t ums_open;
154 static usb_fifo_close_t ums_close;
155 static usb_fifo_ioctl_t ums_ioctl;
156 
157 static void	ums_put_queue(struct ums_softc *, int32_t, int32_t,
158 		    int32_t, int32_t, int32_t);
159 static int	ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS);
160 
161 static struct usb_fifo_methods ums_fifo_methods = {
162 	.f_open = &ums_open,
163 	.f_close = &ums_close,
164 	.f_ioctl = &ums_ioctl,
165 	.f_start_read = &ums_start_read,
166 	.f_stop_read = &ums_stop_read,
167 	.basename[0] = "ums",
168 };
169 
170 static void
171 ums_put_queue_timeout(void *__sc)
172 {
173 	struct ums_softc *sc = __sc;
174 
175 	mtx_assert(&sc->sc_mtx, MA_OWNED);
176 
177 	ums_put_queue(sc, 0, 0, 0, 0, 0);
178 }
179 
180 static void
181 ums_intr_callback(struct usb_xfer *xfer, usb_error_t error)
182 {
183 	struct ums_softc *sc = usbd_xfer_softc(xfer);
184 	struct ums_info *info = &sc->sc_info[0];
185 	struct usb_page_cache *pc;
186 	uint8_t *buf = sc->sc_temp;
187 	int32_t buttons = 0;
188 	int32_t buttons_found = 0;
189 	int32_t dw = 0;
190 	int32_t dx = 0;
191 	int32_t dy = 0;
192 	int32_t dz = 0;
193 	int32_t dt = 0;
194 	uint8_t i;
195 	uint8_t id;
196 	int len;
197 
198 	usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
199 
200 	switch (USB_GET_STATE(xfer)) {
201 	case USB_ST_TRANSFERRED:
202 		DPRINTFN(6, "sc=%p actlen=%d\n", sc, len);
203 
204 		if (len > sizeof(sc->sc_temp)) {
205 			DPRINTFN(6, "truncating large packet to %zu bytes\n",
206 			    sizeof(sc->sc_temp));
207 			len = sizeof(sc->sc_temp);
208 		}
209 		if (len == 0)
210 			goto tr_setup;
211 
212 		pc = usbd_xfer_get_frame(xfer, 0);
213 		usbd_copy_out(pc, 0, buf, len);
214 
215 		DPRINTFN(6, "data = %02x %02x %02x %02x "
216 		    "%02x %02x %02x %02x\n",
217 		    (len > 0) ? buf[0] : 0, (len > 1) ? buf[1] : 0,
218 		    (len > 2) ? buf[2] : 0, (len > 3) ? buf[3] : 0,
219 		    (len > 4) ? buf[4] : 0, (len > 5) ? buf[5] : 0,
220 		    (len > 6) ? buf[6] : 0, (len > 7) ? buf[7] : 0);
221 
222 		if (sc->sc_iid) {
223 			id = *buf;
224 
225 			len--;
226 			buf++;
227 
228 		} else {
229 			id = 0;
230 			if (sc->sc_info[0].sc_flags & UMS_FLAG_SBU) {
231 				if ((*buf == 0x14) || (*buf == 0x15)) {
232 					goto tr_setup;
233 				}
234 			}
235 		}
236 
237 	repeat:
238 		if ((info->sc_flags & UMS_FLAG_W_AXIS) &&
239 		    (id == info->sc_iid_w))
240 			dw += hid_get_data(buf, len, &info->sc_loc_w);
241 
242 		if ((info->sc_flags & UMS_FLAG_X_AXIS) &&
243 		    (id == info->sc_iid_x))
244 			dx += hid_get_data(buf, len, &info->sc_loc_x);
245 
246 		if ((info->sc_flags & UMS_FLAG_Y_AXIS) &&
247 		    (id == info->sc_iid_y))
248 			dy = -hid_get_data(buf, len, &info->sc_loc_y);
249 
250 		if ((info->sc_flags & UMS_FLAG_Z_AXIS) &&
251 		    (id == info->sc_iid_z)) {
252 			int32_t temp;
253 			temp = hid_get_data(buf, len, &info->sc_loc_z);
254 			if (info->sc_flags & UMS_FLAG_REVZ)
255 				temp = -temp;
256 			dz -= temp;
257 		}
258 
259 		if ((info->sc_flags & UMS_FLAG_T_AXIS) &&
260 		    (id == info->sc_iid_t))
261 			dt -= hid_get_data(buf, len, &info->sc_loc_t);
262 
263 		for (i = 0; i < info->sc_buttons; i++) {
264 			uint32_t mask;
265 			mask = 1UL << UMS_BUT(i);
266 			/* check for correct button ID */
267 			if (id != info->sc_iid_btn[i])
268 				continue;
269 			/* check for button pressed */
270 			if (hid_get_data(buf, len, &info->sc_loc_btn[i]))
271 				buttons |= mask;
272 			/* register button mask */
273 			buttons_found |= mask;
274 		}
275 
276 		if (++info != &sc->sc_info[UMS_INFO_MAX])
277 			goto repeat;
278 
279 		/* keep old button value(s) for non-detected buttons */
280 		buttons |= sc->sc_status.button & ~buttons_found;
281 
282 		if (dx || dy || dz || dt || dw ||
283 		    (buttons != sc->sc_status.button)) {
284 
285 			DPRINTFN(6, "x:%d y:%d z:%d t:%d w:%d buttons:0x%08x\n",
286 			    dx, dy, dz, dt, dw, buttons);
287 
288 			/* translate T-axis into button presses until further */
289 			if (dt > 0)
290 				buttons |= 1UL << 3;
291 			else if (dt < 0)
292 				buttons |= 1UL << 4;
293 
294 			sc->sc_status.button = buttons;
295 			sc->sc_status.dx += dx;
296 			sc->sc_status.dy += dy;
297 			sc->sc_status.dz += dz;
298 			/*
299 			 * sc->sc_status.dt += dt;
300 			 * no way to export this yet
301 			 */
302 
303 			/*
304 		         * The Qtronix keyboard has a built in PS/2
305 		         * port for a mouse.  The firmware once in a
306 		         * while posts a spurious button up
307 		         * event. This event we ignore by doing a
308 		         * timeout for 50 msecs.  If we receive
309 		         * dx=dy=dz=buttons=0 before we add the event
310 		         * to the queue.  In any other case we delete
311 		         * the timeout event.
312 		         */
313 			if ((sc->sc_info[0].sc_flags & UMS_FLAG_SBU) &&
314 			    (dx == 0) && (dy == 0) && (dz == 0) && (dt == 0) &&
315 			    (dw == 0) && (buttons == 0)) {
316 
317 				usb_callout_reset(&sc->sc_callout, hz / 20,
318 				    &ums_put_queue_timeout, sc);
319 			} else {
320 
321 				usb_callout_stop(&sc->sc_callout);
322 
323 				ums_put_queue(sc, dx, dy, dz, dt, buttons);
324 			}
325 		}
326 	case USB_ST_SETUP:
327 tr_setup:
328 		/* check if we can put more data into the FIFO */
329 		if (usb_fifo_put_bytes_max(
330 		    sc->sc_fifo.fp[USB_FIFO_RX]) != 0) {
331 			usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
332 			usbd_transfer_submit(xfer);
333 		}
334 		break;
335 
336 	default:			/* Error */
337 		if (error != USB_ERR_CANCELLED) {
338 			/* try clear stall first */
339 			usbd_xfer_set_stall(xfer);
340 			goto tr_setup;
341 		}
342 		break;
343 	}
344 }
345 
346 static const struct usb_config ums_config[UMS_N_TRANSFER] = {
347 
348 	[UMS_INTR_DT] = {
349 		.type = UE_INTERRUPT,
350 		.endpoint = UE_ADDR_ANY,
351 		.direction = UE_DIR_IN,
352 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
353 		.bufsize = 0,	/* use wMaxPacketSize */
354 		.callback = &ums_intr_callback,
355 	},
356 };
357 
358 static int
359 ums_probe(device_t dev)
360 {
361 	struct usb_attach_arg *uaa = device_get_ivars(dev);
362 	void *d_ptr;
363 	int error;
364 	uint16_t d_len;
365 
366 	DPRINTFN(11, "\n");
367 
368 	if (uaa->usb_mode != USB_MODE_HOST)
369 		return (ENXIO);
370 
371 	if (uaa->info.bInterfaceClass != UICLASS_HID)
372 		return (ENXIO);
373 
374 	if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
375 	    (uaa->info.bInterfaceProtocol == UIPROTO_MOUSE))
376 		return (BUS_PROBE_DEFAULT);
377 
378 	error = usbd_req_get_hid_desc(uaa->device, NULL,
379 	    &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
380 
381 	if (error)
382 		return (ENXIO);
383 
384 	if (hid_is_collection(d_ptr, d_len,
385 	    HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)))
386 		error = BUS_PROBE_DEFAULT;
387 	else
388 		error = ENXIO;
389 
390 	free(d_ptr, M_TEMP);
391 	return (error);
392 }
393 
394 static void
395 ums_hid_parse(struct ums_softc *sc, device_t dev, const uint8_t *buf,
396     uint16_t len, uint8_t index)
397 {
398 	struct ums_info *info = &sc->sc_info[index];
399 	uint32_t flags;
400 	uint8_t i;
401 	uint8_t j;
402 
403 	if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
404 	    hid_input, index, &info->sc_loc_x, &flags, &info->sc_iid_x)) {
405 
406 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
407 			info->sc_flags |= UMS_FLAG_X_AXIS;
408 		}
409 	}
410 	if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
411 	    hid_input, index, &info->sc_loc_y, &flags, &info->sc_iid_y)) {
412 
413 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
414 			info->sc_flags |= UMS_FLAG_Y_AXIS;
415 		}
416 	}
417 	/* Try the wheel first as the Z activator since it's tradition. */
418 	if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
419 	    HUG_WHEEL), hid_input, index, &info->sc_loc_z, &flags,
420 	    &info->sc_iid_z) ||
421 	    hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
422 	    HUG_TWHEEL), hid_input, index, &info->sc_loc_z, &flags,
423 	    &info->sc_iid_z)) {
424 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
425 			info->sc_flags |= UMS_FLAG_Z_AXIS;
426 		}
427 		/*
428 		 * We might have both a wheel and Z direction, if so put
429 		 * put the Z on the W coordinate.
430 		 */
431 		if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
432 		    HUG_Z), hid_input, index, &info->sc_loc_w, &flags,
433 		    &info->sc_iid_w)) {
434 
435 			if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
436 				info->sc_flags |= UMS_FLAG_W_AXIS;
437 			}
438 		}
439 	} else if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
440 	    HUG_Z), hid_input, index, &info->sc_loc_z, &flags,
441 	    &info->sc_iid_z)) {
442 
443 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
444 			info->sc_flags |= UMS_FLAG_Z_AXIS;
445 		}
446 	}
447 	/*
448 	 * The Microsoft Wireless Intellimouse 2.0 reports it's wheel
449 	 * using 0x0048, which is HUG_TWHEEL, and seems to expect you
450 	 * to know that the byte after the wheel is the tilt axis.
451 	 * There are no other HID axis descriptors other than X,Y and
452 	 * TWHEEL
453 	 */
454 	if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
455 	    HUG_TWHEEL), hid_input, index, &info->sc_loc_t,
456 	    &flags, &info->sc_iid_t)) {
457 
458 		info->sc_loc_t.pos += 8;
459 
460 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
461 			info->sc_flags |= UMS_FLAG_T_AXIS;
462 		}
463 	} else if (hid_locate(buf, len, HID_USAGE2(HUP_CONSUMER,
464 		HUC_AC_PAN), hid_input, index, &info->sc_loc_t,
465 		&flags, &info->sc_iid_t)) {
466 
467 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS)
468 			info->sc_flags |= UMS_FLAG_T_AXIS;
469 	}
470 	/* figure out the number of buttons */
471 
472 	for (i = 0; i < UMS_BUTTON_MAX; i++) {
473 		if (!hid_locate(buf, len, HID_USAGE2(HUP_BUTTON, (i + 1)),
474 		    hid_input, index, &info->sc_loc_btn[i], NULL,
475 		    &info->sc_iid_btn[i])) {
476 			break;
477 		}
478 	}
479 
480 	/* detect other buttons */
481 
482 	for (j = 0; (i < UMS_BUTTON_MAX) && (j < 2); i++, j++) {
483 		if (!hid_locate(buf, len, HID_USAGE2(HUP_MICROSOFT, (j + 1)),
484 		    hid_input, index, &info->sc_loc_btn[i], NULL,
485 		    &info->sc_iid_btn[i])) {
486 			break;
487 		}
488 	}
489 
490 	info->sc_buttons = i;
491 
492 	if (i > sc->sc_buttons)
493 		sc->sc_buttons = i;
494 
495 	if (info->sc_flags == 0)
496 		return;
497 
498 	/* announce information about the mouse */
499 	device_printf(dev, "%d buttons and [%s%s%s%s%s] coordinates ID=%u\n",
500 	    (info->sc_buttons),
501 	    (info->sc_flags & UMS_FLAG_X_AXIS) ? "X" : "",
502 	    (info->sc_flags & UMS_FLAG_Y_AXIS) ? "Y" : "",
503 	    (info->sc_flags & UMS_FLAG_Z_AXIS) ? "Z" : "",
504 	    (info->sc_flags & UMS_FLAG_T_AXIS) ? "T" : "",
505 	    (info->sc_flags & UMS_FLAG_W_AXIS) ? "W" : "",
506 	    info->sc_iid_x);
507 }
508 
509 static int
510 ums_attach(device_t dev)
511 {
512 	struct usb_attach_arg *uaa = device_get_ivars(dev);
513 	struct ums_softc *sc = device_get_softc(dev);
514 	struct ums_info *info;
515 	void *d_ptr = NULL;
516 	int isize;
517 	int err;
518 	uint16_t d_len;
519 	uint8_t i;
520 #ifdef USB_DEBUG
521 	uint8_t j;
522 #endif
523 
524 	DPRINTFN(11, "sc=%p\n", sc);
525 
526 	device_set_usb_desc(dev);
527 
528 	mtx_init(&sc->sc_mtx, "ums lock", NULL, MTX_DEF | MTX_RECURSE);
529 
530 	usb_callout_init_mtx(&sc->sc_callout, &sc->sc_mtx, 0);
531 
532 	/*
533          * Force the report (non-boot) protocol.
534          *
535          * Mice without boot protocol support may choose not to implement
536          * Set_Protocol at all; Ignore any error.
537          */
538 	err = usbd_req_set_protocol(uaa->device, NULL,
539 	    uaa->info.bIfaceIndex, 1);
540 
541 	err = usbd_transfer_setup(uaa->device,
542 	    &uaa->info.bIfaceIndex, sc->sc_xfer, ums_config,
543 	    UMS_N_TRANSFER, sc, &sc->sc_mtx);
544 
545 	if (err) {
546 		DPRINTF("error=%s\n", usbd_errstr(err));
547 		goto detach;
548 	}
549 
550 	/* Get HID descriptor */
551 	err = usbd_req_get_hid_desc(uaa->device, NULL, &d_ptr,
552 	    &d_len, M_TEMP, uaa->info.bIfaceIndex);
553 
554 	if (err) {
555 		device_printf(dev, "error reading report description\n");
556 		goto detach;
557 	}
558 
559 	isize = hid_report_size(d_ptr, d_len, hid_input, &sc->sc_iid);
560 
561 	/*
562 	 * The Microsoft Wireless Notebook Optical Mouse seems to be in worse
563 	 * shape than the Wireless Intellimouse 2.0, as its X, Y, wheel, and
564 	 * all of its other button positions are all off. It also reports that
565 	 * it has two addional buttons and a tilt wheel.
566 	 */
567 	if (usb_test_quirk(uaa, UQ_MS_BAD_CLASS)) {
568 
569 		sc->sc_iid = 0;
570 
571 		info = &sc->sc_info[0];
572 		info->sc_flags = (UMS_FLAG_X_AXIS |
573 		    UMS_FLAG_Y_AXIS |
574 		    UMS_FLAG_Z_AXIS |
575 		    UMS_FLAG_SBU);
576 		info->sc_buttons = 3;
577 		isize = 5;
578 		/* 1st byte of descriptor report contains garbage */
579 		info->sc_loc_x.pos = 16;
580 		info->sc_loc_x.size = 8;
581 		info->sc_loc_y.pos = 24;
582 		info->sc_loc_y.size = 8;
583 		info->sc_loc_z.pos = 32;
584 		info->sc_loc_z.size = 8;
585 		info->sc_loc_btn[0].pos = 8;
586 		info->sc_loc_btn[0].size = 1;
587 		info->sc_loc_btn[1].pos = 9;
588 		info->sc_loc_btn[1].size = 1;
589 		info->sc_loc_btn[2].pos = 10;
590 		info->sc_loc_btn[2].size = 1;
591 
592 		/* Announce device */
593 		device_printf(dev, "3 buttons and [XYZ] "
594 		    "coordinates ID=0\n");
595 
596 	} else {
597 		/* Search the HID descriptor and announce device */
598 		for (i = 0; i < UMS_INFO_MAX; i++) {
599 			ums_hid_parse(sc, dev, d_ptr, d_len, i);
600 		}
601 	}
602 
603 	if (usb_test_quirk(uaa, UQ_MS_REVZ)) {
604 		info = &sc->sc_info[0];
605 		/* Some wheels need the Z axis reversed. */
606 		info->sc_flags |= UMS_FLAG_REVZ;
607 	}
608 	if (isize > usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT])) {
609 		DPRINTF("WARNING: report size, %d bytes, is larger "
610 		    "than interrupt size, %d bytes!\n", isize,
611 		    usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT]));
612 	}
613 	free(d_ptr, M_TEMP);
614 	d_ptr = NULL;
615 
616 #ifdef USB_DEBUG
617 	for (j = 0; j < UMS_INFO_MAX; j++) {
618 		info = &sc->sc_info[j];
619 
620 		DPRINTF("sc=%p, index=%d\n", sc, j);
621 		DPRINTF("X\t%d/%d id=%d\n", info->sc_loc_x.pos,
622 		    info->sc_loc_x.size, info->sc_iid_x);
623 		DPRINTF("Y\t%d/%d id=%d\n", info->sc_loc_y.pos,
624 		    info->sc_loc_y.size, info->sc_iid_y);
625 		DPRINTF("Z\t%d/%d id=%d\n", info->sc_loc_z.pos,
626 		    info->sc_loc_z.size, info->sc_iid_z);
627 		DPRINTF("T\t%d/%d id=%d\n", info->sc_loc_t.pos,
628 		    info->sc_loc_t.size, info->sc_iid_t);
629 		DPRINTF("W\t%d/%d id=%d\n", info->sc_loc_w.pos,
630 		    info->sc_loc_w.size, info->sc_iid_w);
631 
632 		for (i = 0; i < info->sc_buttons; i++) {
633 			DPRINTF("B%d\t%d/%d id=%d\n",
634 			    i + 1, info->sc_loc_btn[i].pos,
635 			    info->sc_loc_btn[i].size, info->sc_iid_btn[i]);
636 		}
637 	}
638 	DPRINTF("size=%d, id=%d\n", isize, sc->sc_iid);
639 #endif
640 
641 	if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
642 		sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
643 	else
644 		sc->sc_hw.buttons = sc->sc_buttons;
645 
646 	sc->sc_hw.iftype = MOUSE_IF_USB;
647 	sc->sc_hw.type = MOUSE_MOUSE;
648 	sc->sc_hw.model = MOUSE_MODEL_GENERIC;
649 	sc->sc_hw.hwid = 0;
650 
651 	sc->sc_mode.protocol = MOUSE_PROTO_MSC;
652 	sc->sc_mode.rate = -1;
653 	sc->sc_mode.resolution = MOUSE_RES_UNKNOWN;
654 	sc->sc_mode.accelfactor = 0;
655 	sc->sc_mode.level = 0;
656 	sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
657 	sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
658 	sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
659 
660 	err = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
661 	    &ums_fifo_methods, &sc->sc_fifo,
662 	    device_get_unit(dev), 0 - 1, uaa->info.bIfaceIndex,
663   	    UID_ROOT, GID_OPERATOR, 0644);
664 	if (err) {
665 		goto detach;
666 	}
667 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
668 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
669 	    OID_AUTO, "parseinfo", CTLTYPE_STRING|CTLFLAG_RD,
670 	    sc, 0, ums_sysctl_handler_parseinfo,
671 	    "", "Dump of parsed HID report descriptor");
672 
673 	return (0);
674 
675 detach:
676 	if (d_ptr) {
677 		free(d_ptr, M_TEMP);
678 	}
679 	ums_detach(dev);
680 	return (ENOMEM);
681 }
682 
683 static int
684 ums_detach(device_t self)
685 {
686 	struct ums_softc *sc = device_get_softc(self);
687 
688 	DPRINTF("sc=%p\n", sc);
689 
690 	usb_fifo_detach(&sc->sc_fifo);
691 
692 	usbd_transfer_unsetup(sc->sc_xfer, UMS_N_TRANSFER);
693 
694 	usb_callout_drain(&sc->sc_callout);
695 
696 	mtx_destroy(&sc->sc_mtx);
697 
698 	return (0);
699 }
700 
701 static void
702 ums_start_read(struct usb_fifo *fifo)
703 {
704 	struct ums_softc *sc = usb_fifo_softc(fifo);
705 	int rate;
706 
707 	/* Check if we should override the default polling interval */
708 	rate = sc->sc_pollrate;
709 	/* Range check rate */
710 	if (rate > 1000)
711 		rate = 1000;
712 	/* Check for set rate */
713 	if ((rate > 0) && (sc->sc_xfer[UMS_INTR_DT] != NULL)) {
714 		DPRINTF("Setting pollrate = %d\n", rate);
715 		/* Stop current transfer, if any */
716 		usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
717 		/* Set new interval */
718 		usbd_xfer_set_interval(sc->sc_xfer[UMS_INTR_DT], 1000 / rate);
719 		/* Only set pollrate once */
720 		sc->sc_pollrate = 0;
721 	}
722 
723 	usbd_transfer_start(sc->sc_xfer[UMS_INTR_DT]);
724 }
725 
726 static void
727 ums_stop_read(struct usb_fifo *fifo)
728 {
729 	struct ums_softc *sc = usb_fifo_softc(fifo);
730 
731 	usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
732 	usb_callout_stop(&sc->sc_callout);
733 }
734 
735 
736 #if ((MOUSE_SYS_PACKETSIZE != 8) || \
737      (MOUSE_MSC_PACKETSIZE != 5))
738 #error "Software assumptions are not met. Please update code."
739 #endif
740 
741 static void
742 ums_put_queue(struct ums_softc *sc, int32_t dx, int32_t dy,
743     int32_t dz, int32_t dt, int32_t buttons)
744 {
745 	uint8_t buf[8];
746 
747 	if (1) {
748 
749 		if (dx > 254)
750 			dx = 254;
751 		if (dx < -256)
752 			dx = -256;
753 		if (dy > 254)
754 			dy = 254;
755 		if (dy < -256)
756 			dy = -256;
757 		if (dz > 126)
758 			dz = 126;
759 		if (dz < -128)
760 			dz = -128;
761 		if (dt > 126)
762 			dt = 126;
763 		if (dt < -128)
764 			dt = -128;
765 
766 		buf[0] = sc->sc_mode.syncmask[1];
767 		buf[0] |= (~buttons) & MOUSE_MSC_BUTTONS;
768 		buf[1] = dx >> 1;
769 		buf[2] = dy >> 1;
770 		buf[3] = dx - (dx >> 1);
771 		buf[4] = dy - (dy >> 1);
772 
773 		if (sc->sc_mode.level == 1) {
774 			buf[5] = dz >> 1;
775 			buf[6] = dz - (dz >> 1);
776 			buf[7] = (((~buttons) >> 3) & MOUSE_SYS_EXTBUTTONS);
777 		}
778 		usb_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf,
779 		    sc->sc_mode.packetsize, 1);
780 
781 	} else {
782 		DPRINTF("Buffer full, discarded packet\n");
783 	}
784 }
785 
786 static void
787 ums_reset_buf(struct ums_softc *sc)
788 {
789 	/* reset read queue */
790 	usb_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]);
791 }
792 
793 static int
794 ums_open(struct usb_fifo *fifo, int fflags)
795 {
796 	struct ums_softc *sc = usb_fifo_softc(fifo);
797 
798 	DPRINTFN(2, "\n");
799 
800 	if (fflags & FREAD) {
801 
802 		/* reset status */
803 
804 		sc->sc_status.flags = 0;
805 		sc->sc_status.button = 0;
806 		sc->sc_status.obutton = 0;
807 		sc->sc_status.dx = 0;
808 		sc->sc_status.dy = 0;
809 		sc->sc_status.dz = 0;
810 		/* sc->sc_status.dt = 0; */
811 
812 		if (usb_fifo_alloc_buffer(fifo,
813 		    UMS_BUF_SIZE, UMS_IFQ_MAXLEN)) {
814 			return (ENOMEM);
815 		}
816 	}
817 	return (0);
818 }
819 
820 static void
821 ums_close(struct usb_fifo *fifo, int fflags)
822 {
823 	if (fflags & FREAD) {
824 		usb_fifo_free_buffer(fifo);
825 	}
826 }
827 
828 static int
829 ums_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
830 {
831 	struct ums_softc *sc = usb_fifo_softc(fifo);
832 	mousemode_t mode;
833 	int error = 0;
834 
835 	DPRINTFN(2, "\n");
836 
837 	mtx_lock(&sc->sc_mtx);
838 
839 	switch (cmd) {
840 	case MOUSE_GETHWINFO:
841 		*(mousehw_t *)addr = sc->sc_hw;
842 		break;
843 
844 	case MOUSE_GETMODE:
845 		*(mousemode_t *)addr = sc->sc_mode;
846 		break;
847 
848 	case MOUSE_SETMODE:
849 		mode = *(mousemode_t *)addr;
850 
851 		if (mode.level == -1) {
852 			/* don't change the current setting */
853 		} else if ((mode.level < 0) || (mode.level > 1)) {
854 			error = EINVAL;
855 			goto done;
856 		} else {
857 			sc->sc_mode.level = mode.level;
858 		}
859 
860 		/* store polling rate */
861 		sc->sc_pollrate = mode.rate;
862 
863 		if (sc->sc_mode.level == 0) {
864 			if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
865 				sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
866 			else
867 				sc->sc_hw.buttons = sc->sc_buttons;
868 			sc->sc_mode.protocol = MOUSE_PROTO_MSC;
869 			sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
870 			sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
871 			sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
872 		} else if (sc->sc_mode.level == 1) {
873 			if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
874 				sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
875 			else
876 				sc->sc_hw.buttons = sc->sc_buttons;
877 			sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
878 			sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
879 			sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
880 			sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
881 		}
882 		ums_reset_buf(sc);
883 		break;
884 
885 	case MOUSE_GETLEVEL:
886 		*(int *)addr = sc->sc_mode.level;
887 		break;
888 
889 	case MOUSE_SETLEVEL:
890 		if (*(int *)addr < 0 || *(int *)addr > 1) {
891 			error = EINVAL;
892 			goto done;
893 		}
894 		sc->sc_mode.level = *(int *)addr;
895 
896 		if (sc->sc_mode.level == 0) {
897 			if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
898 				sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
899 			else
900 				sc->sc_hw.buttons = sc->sc_buttons;
901 			sc->sc_mode.protocol = MOUSE_PROTO_MSC;
902 			sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
903 			sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
904 			sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
905 		} else if (sc->sc_mode.level == 1) {
906 			if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
907 				sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
908 			else
909 				sc->sc_hw.buttons = sc->sc_buttons;
910 			sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
911 			sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
912 			sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
913 			sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
914 		}
915 		ums_reset_buf(sc);
916 		break;
917 
918 	case MOUSE_GETSTATUS:{
919 			mousestatus_t *status = (mousestatus_t *)addr;
920 
921 			*status = sc->sc_status;
922 			sc->sc_status.obutton = sc->sc_status.button;
923 			sc->sc_status.button = 0;
924 			sc->sc_status.dx = 0;
925 			sc->sc_status.dy = 0;
926 			sc->sc_status.dz = 0;
927 			/* sc->sc_status.dt = 0; */
928 
929 			if (status->dx || status->dy || status->dz /* || status->dt */ ) {
930 				status->flags |= MOUSE_POSCHANGED;
931 			}
932 			if (status->button != status->obutton) {
933 				status->flags |= MOUSE_BUTTONSCHANGED;
934 			}
935 			break;
936 		}
937 	default:
938 		error = ENOTTY;
939 	}
940 
941 done:
942 	mtx_unlock(&sc->sc_mtx);
943 	return (error);
944 }
945 
946 static int
947 ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS)
948 {
949 	struct ums_softc *sc = arg1;
950 	struct ums_info *info;
951 	struct sbuf *sb;
952 	int i, j, err, had_output;
953 
954 	sb = sbuf_new_auto();
955 	for (i = 0, had_output = 0; i < UMS_INFO_MAX; i++) {
956 		info = &sc->sc_info[i];
957 
958 		/* Don't emit empty info */
959 		if ((info->sc_flags &
960 		    (UMS_FLAG_X_AXIS | UMS_FLAG_Y_AXIS | UMS_FLAG_Z_AXIS |
961 		     UMS_FLAG_T_AXIS | UMS_FLAG_W_AXIS)) == 0 &&
962 		    info->sc_buttons == 0)
963 			continue;
964 
965 		if (had_output)
966 			sbuf_printf(sb, "\n");
967 		had_output = 1;
968 		sbuf_printf(sb, "i%d:", i + 1);
969 		if (info->sc_flags & UMS_FLAG_X_AXIS)
970 			sbuf_printf(sb, " X:r%d, p%d, s%d;",
971 			    (int)info->sc_iid_x,
972 			    (int)info->sc_loc_x.pos,
973 			    (int)info->sc_loc_x.size);
974 		if (info->sc_flags & UMS_FLAG_Y_AXIS)
975 			sbuf_printf(sb, " Y:r%d, p%d, s%d;",
976 			    (int)info->sc_iid_y,
977 			    (int)info->sc_loc_y.pos,
978 			    (int)info->sc_loc_y.size);
979 		if (info->sc_flags & UMS_FLAG_Z_AXIS)
980 			sbuf_printf(sb, " Z:r%d, p%d, s%d;",
981 			    (int)info->sc_iid_z,
982 			    (int)info->sc_loc_z.pos,
983 			    (int)info->sc_loc_z.size);
984 		if (info->sc_flags & UMS_FLAG_T_AXIS)
985 			sbuf_printf(sb, " T:r%d, p%d, s%d;",
986 			    (int)info->sc_iid_t,
987 			    (int)info->sc_loc_t.pos,
988 			    (int)info->sc_loc_t.size);
989 		if (info->sc_flags & UMS_FLAG_W_AXIS)
990 			sbuf_printf(sb, " W:r%d, p%d, s%d;",
991 			    (int)info->sc_iid_w,
992 			    (int)info->sc_loc_w.pos,
993 			    (int)info->sc_loc_w.size);
994 
995 		for (j = 0; j < info->sc_buttons; j++) {
996 			sbuf_printf(sb, " B%d:r%d, p%d, s%d;", j + 1,
997 			    (int)info->sc_iid_btn[j],
998 			    (int)info->sc_loc_btn[j].pos,
999 			    (int)info->sc_loc_btn[j].size);
1000 		}
1001 	}
1002 	sbuf_finish(sb);
1003 	err = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
1004 	sbuf_delete(sb);
1005 
1006 	return (err);
1007 }
1008 
1009 static devclass_t ums_devclass;
1010 
1011 static device_method_t ums_methods[] = {
1012 	DEVMETHOD(device_probe, ums_probe),
1013 	DEVMETHOD(device_attach, ums_attach),
1014 	DEVMETHOD(device_detach, ums_detach),
1015 	{0, 0}
1016 };
1017 
1018 static driver_t ums_driver = {
1019 	.name = "ums",
1020 	.methods = ums_methods,
1021 	.size = sizeof(struct ums_softc),
1022 };
1023 
1024 DRIVER_MODULE(ums, uhub, ums_driver, ums_devclass, NULL, 0);
1025 MODULE_DEPEND(ums, usb, 1, 1, 1);
1026 MODULE_VERSION(ums, 1);
1027