xref: /freebsd/sys/dev/usb/input/ums.c (revision 7937bfbc0ca53fe7cdd0d54414f9296e273a518e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Lennart Augustsson (lennart@augustsson.net) at
9  * Carlstedt Research & Technology.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
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/hid/hid.h>
63 
64 #include <dev/usb/usb.h>
65 #include <dev/usb/usbdi.h>
66 #include <dev/usb/usbdi_util.h>
67 #include <dev/usb/usbhid.h>
68 #include "usbdevs.h"
69 
70 #define	USB_DEBUG_VAR ums_debug
71 #include <dev/usb/usb_debug.h>
72 
73 #include <dev/usb/quirk/usb_quirk.h>
74 
75 #ifdef EVDEV_SUPPORT
76 #include <dev/evdev/input.h>
77 #include <dev/evdev/evdev.h>
78 #endif
79 
80 #include <sys/ioccom.h>
81 #include <sys/filio.h>
82 #include <sys/mouse.h>
83 
84 #ifdef USB_DEBUG
85 static int ums_debug = 0;
86 
87 static SYSCTL_NODE(_hw_usb, OID_AUTO, ums, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
88     "USB ums");
89 SYSCTL_INT(_hw_usb_ums, OID_AUTO, debug, CTLFLAG_RWTUN,
90     &ums_debug, 0, "Debug level");
91 #endif
92 
93 #define	MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
94 #define	MOUSE_FLAGS (HIO_RELATIVE)
95 
96 #define	UMS_BUF_SIZE      8		/* bytes */
97 #define	UMS_IFQ_MAXLEN   50		/* units */
98 #define	UMS_BUTTON_MAX   31		/* exclusive, must be less than 32 */
99 #define	UMS_BUT(i) ((i) < 3 ? (((i) + 2) % 3) : (i))
100 #define	UMS_INFO_MAX	  2		/* maximum number of HID sets */
101 
102 enum {
103 	UMS_INTR_DT,
104 	UMS_N_TRANSFER,
105 };
106 
107 struct ums_info {
108 	struct hid_location sc_loc_w;
109 	struct hid_location sc_loc_x;
110 	struct hid_location sc_loc_y;
111 	struct hid_location sc_loc_z;
112 	struct hid_location sc_loc_t;
113 	struct hid_location sc_loc_btn[UMS_BUTTON_MAX];
114 
115 	uint32_t sc_flags;
116 #define	UMS_FLAG_X_AXIS     0x0001
117 #define	UMS_FLAG_Y_AXIS     0x0002
118 #define	UMS_FLAG_Z_AXIS     0x0004
119 #define	UMS_FLAG_T_AXIS     0x0008
120 #define	UMS_FLAG_SBU        0x0010	/* spurious button up events */
121 #define	UMS_FLAG_REVZ	    0x0020	/* Z-axis is reversed */
122 #define	UMS_FLAG_W_AXIS     0x0040
123 #define	UMS_FLAG_VBTN	    0x0080	/* Buttons in vendor usage page */
124 
125 	uint8_t	sc_iid_w;
126 	uint8_t	sc_iid_x;
127 	uint8_t	sc_iid_y;
128 	uint8_t	sc_iid_z;
129 	uint8_t	sc_iid_t;
130 	uint8_t	sc_iid_btn[UMS_BUTTON_MAX];
131 	uint8_t	sc_buttons;
132 };
133 
134 struct ums_softc {
135 	struct usb_fifo_sc sc_fifo;
136 	struct mtx sc_mtx;
137 	struct usb_callout sc_callout;
138 	struct ums_info sc_info[UMS_INFO_MAX];
139 
140 	mousehw_t sc_hw;
141 	mousemode_t sc_mode;
142 	mousestatus_t sc_status;
143 
144 	struct usb_xfer *sc_xfer[UMS_N_TRANSFER];
145 
146 	int sc_pollrate;
147 	int sc_fflags;
148 #ifdef EVDEV_SUPPORT
149 	int sc_evflags;
150 #define	UMS_EVDEV_OPENED	1
151 #endif
152 
153 	uint8_t	sc_buttons;
154 	uint8_t	sc_iid;
155 	uint8_t	sc_temp[64];
156 
157 #ifdef EVDEV_SUPPORT
158 	struct evdev_dev *sc_evdev;
159 #endif
160 };
161 
162 static void ums_put_queue_timeout(void *__sc);
163 
164 static usb_callback_t ums_intr_callback;
165 
166 static device_probe_t ums_probe;
167 static device_attach_t ums_attach;
168 static device_detach_t ums_detach;
169 
170 static usb_fifo_cmd_t ums_fifo_start_read;
171 static usb_fifo_cmd_t ums_fifo_stop_read;
172 static usb_fifo_open_t ums_fifo_open;
173 static usb_fifo_close_t ums_fifo_close;
174 static usb_fifo_ioctl_t ums_fifo_ioctl;
175 
176 #ifdef EVDEV_SUPPORT
177 static evdev_open_t ums_ev_open;
178 static evdev_close_t ums_ev_close;
179 static void ums_evdev_push(struct ums_softc *, int32_t, int32_t,
180     int32_t, int32_t, int32_t);
181 #endif
182 
183 static void	ums_start_rx(struct ums_softc *);
184 static void	ums_stop_rx(struct ums_softc *);
185 static void	ums_put_queue(struct ums_softc *, int32_t, int32_t,
186 		    int32_t, int32_t, int32_t);
187 static int	ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS);
188 
189 static struct usb_fifo_methods ums_fifo_methods = {
190 	.f_open = &ums_fifo_open,
191 	.f_close = &ums_fifo_close,
192 	.f_ioctl = &ums_fifo_ioctl,
193 	.f_start_read = &ums_fifo_start_read,
194 	.f_stop_read = &ums_fifo_stop_read,
195 	.basename[0] = "ums",
196 };
197 
198 #ifdef EVDEV_SUPPORT
199 static const struct evdev_methods ums_evdev_methods = {
200 	.ev_open = &ums_ev_open,
201 	.ev_close = &ums_ev_close,
202 };
203 #endif
204 
205 static void
206 ums_put_queue_timeout(void *__sc)
207 {
208 	struct ums_softc *sc = __sc;
209 
210 	mtx_assert(&sc->sc_mtx, MA_OWNED);
211 
212 	ums_put_queue(sc, 0, 0, 0, 0, 0);
213 #ifdef EVDEV_SUPPORT
214 	ums_evdev_push(sc, 0, 0, 0, 0, 0);
215 #endif
216 }
217 
218 static void
219 ums_intr_callback(struct usb_xfer *xfer, usb_error_t error)
220 {
221 	struct ums_softc *sc = usbd_xfer_softc(xfer);
222 	struct ums_info *info = &sc->sc_info[0];
223 	struct usb_page_cache *pc;
224 	uint8_t *buf = sc->sc_temp;
225 	int32_t buttons = 0;
226 	int32_t buttons_found = 0;
227 #ifdef EVDEV_SUPPORT
228 	int32_t buttons_reported = 0;
229 #endif
230 	int32_t dw = 0;
231 	int32_t dx = 0;
232 	int32_t dy = 0;
233 	int32_t dz = 0;
234 	int32_t dt = 0;
235 	uint8_t i;
236 	uint8_t id;
237 	int len;
238 
239 	usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
240 
241 	switch (USB_GET_STATE(xfer)) {
242 	case USB_ST_TRANSFERRED:
243 		DPRINTFN(6, "sc=%p actlen=%d\n", sc, len);
244 
245 		if (len > (int)sizeof(sc->sc_temp)) {
246 			DPRINTFN(6, "truncating large packet to %zu bytes\n",
247 			    sizeof(sc->sc_temp));
248 			len = sizeof(sc->sc_temp);
249 		}
250 		if (len == 0)
251 			goto tr_setup;
252 
253 		pc = usbd_xfer_get_frame(xfer, 0);
254 		usbd_copy_out(pc, 0, buf, len);
255 
256 		DPRINTFN(6, "data = %02x %02x %02x %02x "
257 		    "%02x %02x %02x %02x\n",
258 		    (len > 0) ? buf[0] : 0, (len > 1) ? buf[1] : 0,
259 		    (len > 2) ? buf[2] : 0, (len > 3) ? buf[3] : 0,
260 		    (len > 4) ? buf[4] : 0, (len > 5) ? buf[5] : 0,
261 		    (len > 6) ? buf[6] : 0, (len > 7) ? buf[7] : 0);
262 
263 		if (sc->sc_iid) {
264 			id = *buf;
265 
266 			len--;
267 			buf++;
268 
269 		} else {
270 			id = 0;
271 			if (sc->sc_info[0].sc_flags & UMS_FLAG_SBU) {
272 				if ((*buf == 0x14) || (*buf == 0x15)) {
273 					goto tr_setup;
274 				}
275 			}
276 		}
277 
278 	repeat:
279 		if ((info->sc_flags & UMS_FLAG_W_AXIS) &&
280 		    (id == info->sc_iid_w))
281 			dw += hid_get_data(buf, len, &info->sc_loc_w);
282 
283 		if ((info->sc_flags & UMS_FLAG_X_AXIS) &&
284 		    (id == info->sc_iid_x))
285 			dx += hid_get_data(buf, len, &info->sc_loc_x);
286 
287 		if ((info->sc_flags & UMS_FLAG_Y_AXIS) &&
288 		    (id == info->sc_iid_y))
289 			dy -= hid_get_data(buf, len, &info->sc_loc_y);
290 
291 		if ((info->sc_flags & UMS_FLAG_Z_AXIS) &&
292 		    (id == info->sc_iid_z)) {
293 			int32_t temp;
294 			temp = hid_get_data(buf, len, &info->sc_loc_z);
295 			if (info->sc_flags & UMS_FLAG_REVZ)
296 				temp = -temp;
297 			dz -= temp;
298 		}
299 
300 		if ((info->sc_flags & UMS_FLAG_T_AXIS) &&
301 		    (id == info->sc_iid_t)) {
302 			dt += hid_get_data(buf, len, &info->sc_loc_t);
303 			/* T-axis is translated into button presses */
304 			buttons_found |= (1UL << 5) | (1UL << 6);
305 		}
306 
307 		for (i = 0; i < info->sc_buttons; i++) {
308 			uint32_t mask;
309 			mask = 1UL << UMS_BUT(i);
310 			/* check for correct button ID */
311 			if (id != info->sc_iid_btn[i])
312 				continue;
313 			/* check for button pressed */
314 			if (hid_get_data(buf, len, &info->sc_loc_btn[i]))
315 				buttons |= mask;
316 			/* register button mask */
317 			buttons_found |= mask;
318 		}
319 
320 		if (++info != &sc->sc_info[UMS_INFO_MAX])
321 			goto repeat;
322 
323 		/* keep old button value(s) for non-detected buttons */
324 		buttons |= sc->sc_status.button & ~buttons_found;
325 
326 #ifdef EVDEV_SUPPORT
327 		buttons_reported = buttons;
328 #endif
329 
330 		if (dx || dy || dz || dt || dw ||
331 		    (buttons != sc->sc_status.button)) {
332 			DPRINTFN(6, "x:%d y:%d z:%d t:%d w:%d buttons:0x%08x\n",
333 			    dx, dy, dz, dt, dw, buttons);
334 
335 			/* translate T-axis into button presses until further */
336 			if (dt > 0) {
337 				ums_put_queue(sc, 0, 0, 0, 0, buttons);
338 				buttons |= 1UL << 6;
339 			} else if (dt < 0) {
340 				ums_put_queue(sc, 0, 0, 0, 0, buttons);
341 				buttons |= 1UL << 5;
342 			}
343 
344 			sc->sc_status.button = buttons;
345 			sc->sc_status.dx += dx;
346 			sc->sc_status.dy += dy;
347 			sc->sc_status.dz += dz;
348 			/*
349 			 * sc->sc_status.dt += dt;
350 			 * no way to export this yet
351 			 */
352 
353 			/*
354 		         * The Qtronix keyboard has a built in PS/2
355 		         * port for a mouse.  The firmware once in a
356 		         * while posts a spurious button up
357 		         * event. This event we ignore by doing a
358 		         * timeout for 50 msecs.  If we receive
359 		         * dx=dy=dz=buttons=0 before we add the event
360 		         * to the queue.  In any other case we delete
361 		         * the timeout event.
362 		         */
363 			if ((sc->sc_info[0].sc_flags & UMS_FLAG_SBU) &&
364 			    (dx == 0) && (dy == 0) && (dz == 0) && (dt == 0) &&
365 			    (dw == 0) && (buttons == 0)) {
366 				usb_callout_reset(&sc->sc_callout, hz / 20,
367 				    &ums_put_queue_timeout, sc);
368 			} else {
369 				usb_callout_stop(&sc->sc_callout);
370 
371 				ums_put_queue(sc, dx, dy, dz, dt, buttons);
372 #ifdef EVDEV_SUPPORT
373 				ums_evdev_push(sc, dx, dy, dz, dt,
374 				    buttons_reported);
375 #endif
376 			}
377 		}
378 	case USB_ST_SETUP:
379 tr_setup:
380 		/* check if we can put more data into the FIFO */
381 		if (usb_fifo_put_bytes_max(sc->sc_fifo.fp[USB_FIFO_RX]) == 0) {
382 #ifdef EVDEV_SUPPORT
383 			if ((sc->sc_evflags & UMS_EVDEV_OPENED) == 0)
384 				break;
385 #else
386 			break;
387 #endif
388 		}
389 
390 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
391 		usbd_transfer_submit(xfer);
392 		break;
393 
394 	default:			/* Error */
395 		if (error != USB_ERR_CANCELLED) {
396 			/* try clear stall first */
397 			usbd_xfer_set_stall(xfer);
398 			goto tr_setup;
399 		}
400 		break;
401 	}
402 }
403 
404 static const struct usb_config ums_config[UMS_N_TRANSFER] = {
405 	[UMS_INTR_DT] = {
406 		.type = UE_INTERRUPT,
407 		.endpoint = UE_ADDR_ANY,
408 		.direction = UE_DIR_IN,
409 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
410 		.bufsize = 0,	/* use wMaxPacketSize */
411 		.callback = &ums_intr_callback,
412 	},
413 };
414 
415 /* A match on these entries will load ums */
416 static const STRUCT_USB_HOST_ID __used ums_devs[] = {
417 	{USB_IFACE_CLASS(UICLASS_HID),
418 	 USB_IFACE_SUBCLASS(UISUBCLASS_BOOT),
419 	 USB_IFACE_PROTOCOL(UIPROTO_MOUSE),},
420 };
421 
422 static int
423 ums_probe(device_t dev)
424 {
425 	struct usb_attach_arg *uaa = device_get_ivars(dev);
426 	void *d_ptr;
427 	int error;
428 	uint16_t d_len;
429 
430 	DPRINTFN(11, "\n");
431 
432 	if (uaa->usb_mode != USB_MODE_HOST)
433 		return (ENXIO);
434 
435 	if (uaa->info.bInterfaceClass != UICLASS_HID)
436 		return (ENXIO);
437 
438 	if (usb_test_quirk(uaa, UQ_UMS_IGNORE))
439 		return (ENXIO);
440 
441 	if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
442 	    (uaa->info.bInterfaceProtocol == UIPROTO_MOUSE))
443 		return (BUS_PROBE_DEFAULT);
444 
445 	error = usbd_req_get_hid_desc(uaa->device, NULL,
446 	    &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
447 
448 	if (error)
449 		return (ENXIO);
450 
451 	if (hid_is_mouse(d_ptr, d_len))
452 		error = BUS_PROBE_DEFAULT;
453 	else
454 		error = ENXIO;
455 
456 	free(d_ptr, M_TEMP);
457 	return (error);
458 }
459 
460 static void
461 ums_hid_parse(struct ums_softc *sc, device_t dev, const uint8_t *buf,
462     uint16_t len, uint8_t index)
463 {
464 	struct ums_info *info = &sc->sc_info[index];
465 	uint32_t flags;
466 	uint8_t i;
467 	uint8_t j;
468 
469 	if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
470 	    hid_input, index, &info->sc_loc_x, &flags, &info->sc_iid_x)) {
471 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
472 			info->sc_flags |= UMS_FLAG_X_AXIS;
473 		}
474 	}
475 	if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
476 	    hid_input, index, &info->sc_loc_y, &flags, &info->sc_iid_y)) {
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 			if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
499 				info->sc_flags |= UMS_FLAG_W_AXIS;
500 			}
501 		}
502 	} else if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
503 	    HUG_Z), hid_input, index, &info->sc_loc_z, &flags,
504 	    &info->sc_iid_z)) {
505 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
506 			info->sc_flags |= UMS_FLAG_Z_AXIS;
507 		}
508 	}
509 	/*
510 	 * The Microsoft Wireless Intellimouse 2.0 reports it's wheel
511 	 * using 0x0048, which is HUG_TWHEEL, and seems to expect you
512 	 * to know that the byte after the wheel is the tilt axis.
513 	 * There are no other HID axis descriptors other than X,Y and
514 	 * TWHEEL
515 	 */
516 	if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
517 	    HUG_TWHEEL), hid_input, index, &info->sc_loc_t,
518 	    &flags, &info->sc_iid_t)) {
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 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS)
528 			info->sc_flags |= UMS_FLAG_T_AXIS;
529 	}
530 	/* figure out the number of buttons */
531 
532 	for (i = 0; i < UMS_BUTTON_MAX; i++) {
533 		if (!hid_locate(buf, len, HID_USAGE2(HUP_BUTTON, (i + 1)),
534 		    hid_input, index, &info->sc_loc_btn[i], NULL,
535 		    &info->sc_iid_btn[i])) {
536 			break;
537 		}
538 	}
539 
540 	/* detect other buttons */
541 	if (info->sc_flags & UMS_FLAG_VBTN) {
542 		for (j = 0; (i < UMS_BUTTON_MAX) && (j < 2); i++, j++) {
543 			if (!hid_locate(buf, len, HID_USAGE2(HUP_MICROSOFT,
544 			    (j + 1)), hid_input, index, &info->sc_loc_btn[i],
545 			    NULL, &info->sc_iid_btn[i])) {
546 				break;
547 			}
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_max(d_ptr, d_len, hid_input, &sc->sc_iid);
621 
622 	if (usb_test_quirk(uaa, UQ_MS_VENDOR_BTN))
623 		for (i = 0; i < UMS_INFO_MAX; i++)
624 			sc->sc_info[i].sc_flags |= UMS_FLAG_VBTN;
625 
626 	/*
627 	 * The Microsoft Wireless Notebook Optical Mouse seems to be in worse
628 	 * shape than the Wireless Intellimouse 2.0, as its X, Y, wheel, and
629 	 * all of its other button positions are all off. It also reports that
630 	 * it has two additional buttons and a tilt wheel.
631 	 */
632 	if (usb_test_quirk(uaa, UQ_MS_BAD_CLASS)) {
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",
750 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
751 	    sc, 0, ums_sysctl_handler_parseinfo, "",
752 	    "Dump of parsed HID report descriptor");
753 
754 	return (0);
755 
756 detach:
757 	if (d_ptr) {
758 		free(d_ptr, M_TEMP);
759 	}
760 	ums_detach(dev);
761 	return (ENOMEM);
762 }
763 
764 static int
765 ums_detach(device_t self)
766 {
767 	struct ums_softc *sc = device_get_softc(self);
768 
769 	DPRINTF("sc=%p\n", sc);
770 
771 	usb_fifo_detach(&sc->sc_fifo);
772 
773 #ifdef EVDEV_SUPPORT
774 	evdev_free(sc->sc_evdev);
775 #endif
776 
777 	usbd_transfer_unsetup(sc->sc_xfer, UMS_N_TRANSFER);
778 
779 	usb_callout_drain(&sc->sc_callout);
780 
781 	mtx_destroy(&sc->sc_mtx);
782 
783 	return (0);
784 }
785 
786 static void
787 ums_reset(struct ums_softc *sc)
788 {
789 
790 	/* reset all USB mouse parameters */
791 
792 	if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
793 		sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
794 	else
795 		sc->sc_hw.buttons = sc->sc_buttons;
796 
797 	sc->sc_hw.iftype = MOUSE_IF_USB;
798 	sc->sc_hw.type = MOUSE_MOUSE;
799 	sc->sc_hw.model = MOUSE_MODEL_GENERIC;
800 	sc->sc_hw.hwid = 0;
801 
802 	sc->sc_mode.protocol = MOUSE_PROTO_MSC;
803 	sc->sc_mode.rate = -1;
804 	sc->sc_mode.resolution = MOUSE_RES_UNKNOWN;
805 	sc->sc_mode.accelfactor = 0;
806 	sc->sc_mode.level = 0;
807 	sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
808 	sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
809 	sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
810 
811 	/* reset status */
812 
813 	sc->sc_status.flags = 0;
814 	sc->sc_status.button = 0;
815 	sc->sc_status.obutton = 0;
816 	sc->sc_status.dx = 0;
817 	sc->sc_status.dy = 0;
818 	sc->sc_status.dz = 0;
819 	/* sc->sc_status.dt = 0; */
820 }
821 
822 static void
823 ums_start_rx(struct ums_softc *sc)
824 {
825 	int rate;
826 
827 	/* Check if we should override the default polling interval */
828 	rate = sc->sc_pollrate;
829 	/* Range check rate */
830 	if (rate > 1000)
831 		rate = 1000;
832 	/* Check for set rate */
833 	if ((rate > 0) && (sc->sc_xfer[UMS_INTR_DT] != NULL)) {
834 		DPRINTF("Setting pollrate = %d\n", rate);
835 		/* Stop current transfer, if any */
836 		usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
837 		/* Set new interval */
838 		usbd_xfer_set_interval(sc->sc_xfer[UMS_INTR_DT], 1000 / rate);
839 		/* Only set pollrate once */
840 		sc->sc_pollrate = 0;
841 	}
842 
843 	usbd_transfer_start(sc->sc_xfer[UMS_INTR_DT]);
844 }
845 
846 static void
847 ums_stop_rx(struct ums_softc *sc)
848 {
849 	usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
850 	usb_callout_stop(&sc->sc_callout);
851 }
852 
853 static void
854 ums_fifo_start_read(struct usb_fifo *fifo)
855 {
856 	struct ums_softc *sc = usb_fifo_softc(fifo);
857 
858 	ums_start_rx(sc);
859 }
860 
861 static void
862 ums_fifo_stop_read(struct usb_fifo *fifo)
863 {
864 	struct ums_softc *sc = usb_fifo_softc(fifo);
865 
866 #ifdef EVDEV_SUPPORT
867 	if ((sc->sc_evflags & UMS_EVDEV_OPENED) == 0)
868 #endif
869 		ums_stop_rx(sc);
870 }
871 
872 #if ((MOUSE_SYS_PACKETSIZE != 8) || \
873      (MOUSE_MSC_PACKETSIZE != 5))
874 #error "Software assumptions are not met. Please update code."
875 #endif
876 
877 static void
878 ums_put_queue(struct ums_softc *sc, int32_t dx, int32_t dy,
879     int32_t dz, int32_t dt, int32_t buttons)
880 {
881 	uint8_t buf[8];
882 
883 #ifdef EVDEV_SUPPORT
884 	if (evdev_is_grabbed(sc->sc_evdev))
885 		return;
886 #endif
887 	if (1) {
888 		if (dx > 254)
889 			dx = 254;
890 		if (dx < -256)
891 			dx = -256;
892 		if (dy > 254)
893 			dy = 254;
894 		if (dy < -256)
895 			dy = -256;
896 		if (dz > 126)
897 			dz = 126;
898 		if (dz < -128)
899 			dz = -128;
900 		if (dt > 126)
901 			dt = 126;
902 		if (dt < -128)
903 			dt = -128;
904 
905 		buf[0] = sc->sc_mode.syncmask[1];
906 		buf[0] |= (~buttons) & MOUSE_MSC_BUTTONS;
907 		buf[1] = dx >> 1;
908 		buf[2] = dy >> 1;
909 		buf[3] = dx - (dx >> 1);
910 		buf[4] = dy - (dy >> 1);
911 
912 		if (sc->sc_mode.level == 1) {
913 			buf[5] = dz >> 1;
914 			buf[6] = dz - (dz >> 1);
915 			buf[7] = (((~buttons) >> 3) & MOUSE_SYS_EXTBUTTONS);
916 		}
917 		usb_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf,
918 		    sc->sc_mode.packetsize, 1);
919 	} else {
920 		DPRINTF("Buffer full, discarded packet\n");
921 	}
922 }
923 
924 #ifdef EVDEV_SUPPORT
925 static void
926 ums_evdev_push(struct ums_softc *sc, int32_t dx, int32_t dy,
927     int32_t dz, int32_t dt, int32_t buttons)
928 {
929 	if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) {
930 		/* Push evdev event */
931 		evdev_push_rel(sc->sc_evdev, REL_X, dx);
932 		evdev_push_rel(sc->sc_evdev, REL_Y, -dy);
933 		evdev_push_rel(sc->sc_evdev, REL_WHEEL, -dz);
934 		evdev_push_rel(sc->sc_evdev, REL_HWHEEL, dt);
935 		evdev_push_mouse_btn(sc->sc_evdev,
936 		    (buttons & ~MOUSE_STDBUTTONS) |
937 		    (buttons & (1 << 2) ? MOUSE_BUTTON1DOWN : 0) |
938 		    (buttons & (1 << 1) ? MOUSE_BUTTON2DOWN : 0) |
939 		    (buttons & (1 << 0) ? MOUSE_BUTTON3DOWN : 0));
940 		evdev_sync(sc->sc_evdev);
941 	}
942 }
943 #endif
944 
945 static void
946 ums_reset_buf(struct ums_softc *sc)
947 {
948 	/* reset read queue, must be called locked */
949 	usb_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]);
950 }
951 
952 #ifdef EVDEV_SUPPORT
953 static int
954 ums_ev_open(struct evdev_dev *evdev)
955 {
956 	struct ums_softc *sc = evdev_get_softc(evdev);
957 
958 	mtx_assert(&sc->sc_mtx, MA_OWNED);
959 
960 	sc->sc_evflags |= UMS_EVDEV_OPENED;
961 
962 	if (sc->sc_fflags == 0) {
963 		ums_reset(sc);
964 	}
965 	ums_start_rx(sc);
966 
967 	return (0);
968 }
969 
970 static int
971 ums_ev_close(struct evdev_dev *evdev)
972 {
973 	struct ums_softc *sc = evdev_get_softc(evdev);
974 
975 	mtx_assert(&sc->sc_mtx, MA_OWNED);
976 
977 	sc->sc_evflags &= ~UMS_EVDEV_OPENED;
978 
979 	if (sc->sc_fflags == 0)
980 		ums_stop_rx(sc);
981 
982 	return (0);
983 }
984 #endif
985 
986 static int
987 ums_fifo_open(struct usb_fifo *fifo, int fflags)
988 {
989 	struct ums_softc *sc = usb_fifo_softc(fifo);
990 
991 	DPRINTFN(2, "\n");
992 
993 	/* check for duplicate open, should not happen */
994 	if (sc->sc_fflags & fflags)
995 		return (EBUSY);
996 
997 	/* check for first open */
998 #ifdef EVDEV_SUPPORT
999 	if (sc->sc_fflags == 0 && (sc->sc_evflags & UMS_EVDEV_OPENED) == 0)
1000 		ums_reset(sc);
1001 #else
1002 	if (sc->sc_fflags == 0)
1003 		ums_reset(sc);
1004 #endif
1005 
1006 	if (fflags & FREAD) {
1007 		/* allocate RX buffer */
1008 		if (usb_fifo_alloc_buffer(fifo,
1009 		    UMS_BUF_SIZE, UMS_IFQ_MAXLEN)) {
1010 			return (ENOMEM);
1011 		}
1012 	}
1013 
1014 	sc->sc_fflags |= fflags & (FREAD | FWRITE);
1015 	return (0);
1016 }
1017 
1018 static void
1019 ums_fifo_close(struct usb_fifo *fifo, int fflags)
1020 {
1021 	struct ums_softc *sc = usb_fifo_softc(fifo);
1022 
1023 	DPRINTFN(2, "\n");
1024 
1025 	if (fflags & FREAD)
1026 		usb_fifo_free_buffer(fifo);
1027 
1028 	sc->sc_fflags &= ~(fflags & (FREAD | FWRITE));
1029 }
1030 
1031 static int
1032 ums_fifo_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1033 {
1034 	struct ums_softc *sc = usb_fifo_softc(fifo);
1035 	mousemode_t mode;
1036 	int error = 0;
1037 
1038 	DPRINTFN(2, "\n");
1039 
1040 	mtx_lock(&sc->sc_mtx);
1041 
1042 	switch (cmd) {
1043 	case MOUSE_GETHWINFO:
1044 		*(mousehw_t *)addr = sc->sc_hw;
1045 		break;
1046 
1047 	case MOUSE_GETMODE:
1048 		*(mousemode_t *)addr = sc->sc_mode;
1049 		break;
1050 
1051 	case MOUSE_SETMODE:
1052 		mode = *(mousemode_t *)addr;
1053 
1054 		if (mode.level == -1) {
1055 			/* don't change the current setting */
1056 		} else if ((mode.level < 0) || (mode.level > 1)) {
1057 			error = EINVAL;
1058 			break;
1059 		} else {
1060 			sc->sc_mode.level = mode.level;
1061 		}
1062 
1063 		/* store polling rate */
1064 		sc->sc_pollrate = mode.rate;
1065 
1066 		if (sc->sc_mode.level == 0) {
1067 			if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
1068 				sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
1069 			else
1070 				sc->sc_hw.buttons = sc->sc_buttons;
1071 			sc->sc_mode.protocol = MOUSE_PROTO_MSC;
1072 			sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
1073 			sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
1074 			sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
1075 		} else if (sc->sc_mode.level == 1) {
1076 			if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
1077 				sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
1078 			else
1079 				sc->sc_hw.buttons = sc->sc_buttons;
1080 			sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
1081 			sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
1082 			sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
1083 			sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
1084 		}
1085 		ums_reset_buf(sc);
1086 		break;
1087 
1088 	case MOUSE_GETLEVEL:
1089 		*(int *)addr = sc->sc_mode.level;
1090 		break;
1091 
1092 	case MOUSE_SETLEVEL:
1093 		if (*(int *)addr < 0 || *(int *)addr > 1) {
1094 			error = EINVAL;
1095 			break;
1096 		}
1097 		sc->sc_mode.level = *(int *)addr;
1098 
1099 		if (sc->sc_mode.level == 0) {
1100 			if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
1101 				sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
1102 			else
1103 				sc->sc_hw.buttons = sc->sc_buttons;
1104 			sc->sc_mode.protocol = MOUSE_PROTO_MSC;
1105 			sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
1106 			sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
1107 			sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
1108 		} else if (sc->sc_mode.level == 1) {
1109 			if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
1110 				sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
1111 			else
1112 				sc->sc_hw.buttons = sc->sc_buttons;
1113 			sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
1114 			sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
1115 			sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
1116 			sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
1117 		}
1118 		ums_reset_buf(sc);
1119 		break;
1120 
1121 	case MOUSE_GETSTATUS:{
1122 			mousestatus_t *status = (mousestatus_t *)addr;
1123 
1124 			*status = sc->sc_status;
1125 			sc->sc_status.obutton = sc->sc_status.button;
1126 			sc->sc_status.button = 0;
1127 			sc->sc_status.dx = 0;
1128 			sc->sc_status.dy = 0;
1129 			sc->sc_status.dz = 0;
1130 			/* sc->sc_status.dt = 0; */
1131 
1132 			if (status->dx || status->dy || status->dz /* || status->dt */ ) {
1133 				status->flags |= MOUSE_POSCHANGED;
1134 			}
1135 			if (status->button != status->obutton) {
1136 				status->flags |= MOUSE_BUTTONSCHANGED;
1137 			}
1138 			break;
1139 		}
1140 	default:
1141 		error = ENOTTY;
1142 		break;
1143 	}
1144 
1145 	mtx_unlock(&sc->sc_mtx);
1146 	return (error);
1147 }
1148 
1149 static int
1150 ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS)
1151 {
1152 	struct ums_softc *sc = arg1;
1153 	struct ums_info *info;
1154 	struct sbuf *sb;
1155 	int i, j, err, had_output;
1156 
1157 	sb = sbuf_new_auto();
1158 	for (i = 0, had_output = 0; i < UMS_INFO_MAX; i++) {
1159 		info = &sc->sc_info[i];
1160 
1161 		/* Don't emit empty info */
1162 		if ((info->sc_flags &
1163 		    (UMS_FLAG_X_AXIS | UMS_FLAG_Y_AXIS | UMS_FLAG_Z_AXIS |
1164 		     UMS_FLAG_T_AXIS | UMS_FLAG_W_AXIS)) == 0 &&
1165 		    info->sc_buttons == 0)
1166 			continue;
1167 
1168 		if (had_output)
1169 			sbuf_printf(sb, "\n");
1170 		had_output = 1;
1171 		sbuf_printf(sb, "i%d:", i + 1);
1172 		if (info->sc_flags & UMS_FLAG_X_AXIS)
1173 			sbuf_printf(sb, " X:r%d, p%d, s%d;",
1174 			    (int)info->sc_iid_x,
1175 			    (int)info->sc_loc_x.pos,
1176 			    (int)info->sc_loc_x.size);
1177 		if (info->sc_flags & UMS_FLAG_Y_AXIS)
1178 			sbuf_printf(sb, " Y:r%d, p%d, s%d;",
1179 			    (int)info->sc_iid_y,
1180 			    (int)info->sc_loc_y.pos,
1181 			    (int)info->sc_loc_y.size);
1182 		if (info->sc_flags & UMS_FLAG_Z_AXIS)
1183 			sbuf_printf(sb, " Z:r%d, p%d, s%d;",
1184 			    (int)info->sc_iid_z,
1185 			    (int)info->sc_loc_z.pos,
1186 			    (int)info->sc_loc_z.size);
1187 		if (info->sc_flags & UMS_FLAG_T_AXIS)
1188 			sbuf_printf(sb, " T:r%d, p%d, s%d;",
1189 			    (int)info->sc_iid_t,
1190 			    (int)info->sc_loc_t.pos,
1191 			    (int)info->sc_loc_t.size);
1192 		if (info->sc_flags & UMS_FLAG_W_AXIS)
1193 			sbuf_printf(sb, " W:r%d, p%d, s%d;",
1194 			    (int)info->sc_iid_w,
1195 			    (int)info->sc_loc_w.pos,
1196 			    (int)info->sc_loc_w.size);
1197 
1198 		for (j = 0; j < info->sc_buttons; j++) {
1199 			sbuf_printf(sb, " B%d:r%d, p%d, s%d;", j + 1,
1200 			    (int)info->sc_iid_btn[j],
1201 			    (int)info->sc_loc_btn[j].pos,
1202 			    (int)info->sc_loc_btn[j].size);
1203 		}
1204 	}
1205 	sbuf_finish(sb);
1206 	err = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
1207 	sbuf_delete(sb);
1208 
1209 	return (err);
1210 }
1211 
1212 static device_method_t ums_methods[] = {
1213 	DEVMETHOD(device_probe, ums_probe),
1214 	DEVMETHOD(device_attach, ums_attach),
1215 	DEVMETHOD(device_detach, ums_detach),
1216 
1217 	DEVMETHOD_END
1218 };
1219 
1220 static driver_t ums_driver = {
1221 	.name = "ums",
1222 	.methods = ums_methods,
1223 	.size = sizeof(struct ums_softc),
1224 };
1225 
1226 DRIVER_MODULE(ums, uhub, ums_driver, NULL, NULL);
1227 MODULE_DEPEND(ums, usb, 1, 1, 1);
1228 MODULE_DEPEND(ums, hid, 1, 1, 1);
1229 #ifdef EVDEV_SUPPORT
1230 MODULE_DEPEND(ums, evdev, 1, 1, 1);
1231 #endif
1232 MODULE_VERSION(ums, 1);
1233 USB_PNP_HOST_INFO(ums_devs);
1234