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