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