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