xref: /freebsd/sys/dev/usb/input/ums.c (revision 44d4804d1945435745518cd09eb8ae6ab22ecef4)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
35 
36 /*
37  * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
38  */
39 
40 #include "opt_evdev.h"
41 
42 #include <sys/stdint.h>
43 #include <sys/stddef.h>
44 #include <sys/param.h>
45 #include <sys/queue.h>
46 #include <sys/types.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/bus.h>
50 #include <sys/module.h>
51 #include <sys/lock.h>
52 #include <sys/mutex.h>
53 #include <sys/condvar.h>
54 #include <sys/sysctl.h>
55 #include <sys/sx.h>
56 #include <sys/unistd.h>
57 #include <sys/callout.h>
58 #include <sys/malloc.h>
59 #include <sys/priv.h>
60 #include <sys/conf.h>
61 #include <sys/fcntl.h>
62 #include <sys/sbuf.h>
63 
64 #include <dev/hid/hid.h>
65 
66 #include <dev/usb/usb.h>
67 #include <dev/usb/usbdi.h>
68 #include <dev/usb/usbdi_util.h>
69 #include <dev/usb/usbhid.h>
70 #include "usbdevs.h"
71 
72 #define	USB_DEBUG_VAR ums_debug
73 #include <dev/usb/usb_debug.h>
74 
75 #include <dev/usb/quirk/usb_quirk.h>
76 
77 #ifdef EVDEV_SUPPORT
78 #include <dev/evdev/input.h>
79 #include <dev/evdev/evdev.h>
80 #endif
81 
82 #include <sys/ioccom.h>
83 #include <sys/filio.h>
84 #include <sys/mouse.h>
85 
86 #ifdef USB_DEBUG
87 static int ums_debug = 0;
88 
89 static SYSCTL_NODE(_hw_usb, OID_AUTO, ums, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
90     "USB ums");
91 SYSCTL_INT(_hw_usb_ums, OID_AUTO, debug, CTLFLAG_RWTUN,
92     &ums_debug, 0, "Debug level");
93 #endif
94 
95 #define	MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
96 #define	MOUSE_FLAGS (HIO_RELATIVE)
97 
98 #define	UMS_BUF_SIZE      8		/* bytes */
99 #define	UMS_IFQ_MAXLEN   50		/* units */
100 #define	UMS_BUTTON_MAX   31		/* exclusive, must be less than 32 */
101 #define	UMS_BUT(i) ((i) < 3 ? (((i) + 2) % 3) : (i))
102 #define	UMS_INFO_MAX	  2		/* maximum number of HID sets */
103 
104 enum {
105 	UMS_INTR_DT,
106 	UMS_N_TRANSFER,
107 };
108 
109 struct ums_info {
110 	struct hid_location sc_loc_w;
111 	struct hid_location sc_loc_x;
112 	struct hid_location sc_loc_y;
113 	struct hid_location sc_loc_z;
114 	struct hid_location sc_loc_t;
115 	struct hid_location sc_loc_btn[UMS_BUTTON_MAX];
116 
117 	uint32_t sc_flags;
118 #define	UMS_FLAG_X_AXIS     0x0001
119 #define	UMS_FLAG_Y_AXIS     0x0002
120 #define	UMS_FLAG_Z_AXIS     0x0004
121 #define	UMS_FLAG_T_AXIS     0x0008
122 #define	UMS_FLAG_SBU        0x0010	/* spurious button up events */
123 #define	UMS_FLAG_REVZ	    0x0020	/* Z-axis is reversed */
124 #define	UMS_FLAG_W_AXIS     0x0040
125 
126 	uint8_t	sc_iid_w;
127 	uint8_t	sc_iid_x;
128 	uint8_t	sc_iid_y;
129 	uint8_t	sc_iid_z;
130 	uint8_t	sc_iid_t;
131 	uint8_t	sc_iid_btn[UMS_BUTTON_MAX];
132 	uint8_t	sc_buttons;
133 };
134 
135 struct ums_softc {
136 	struct usb_fifo_sc sc_fifo;
137 	struct mtx sc_mtx;
138 	struct usb_callout sc_callout;
139 	struct ums_info sc_info[UMS_INFO_MAX];
140 
141 	mousehw_t sc_hw;
142 	mousemode_t sc_mode;
143 	mousestatus_t sc_status;
144 
145 	struct usb_xfer *sc_xfer[UMS_N_TRANSFER];
146 
147 	int sc_pollrate;
148 	int sc_fflags;
149 #ifdef EVDEV_SUPPORT
150 	int sc_evflags;
151 #define	UMS_EVDEV_OPENED	1
152 #endif
153 
154 	uint8_t	sc_buttons;
155 	uint8_t	sc_iid;
156 	uint8_t	sc_temp[64];
157 
158 #ifdef EVDEV_SUPPORT
159 	struct evdev_dev *sc_evdev;
160 #endif
161 };
162 
163 static void ums_put_queue_timeout(void *__sc);
164 
165 static usb_callback_t ums_intr_callback;
166 
167 static device_probe_t ums_probe;
168 static device_attach_t ums_attach;
169 static device_detach_t ums_detach;
170 
171 static usb_fifo_cmd_t ums_fifo_start_read;
172 static usb_fifo_cmd_t ums_fifo_stop_read;
173 static usb_fifo_open_t ums_fifo_open;
174 static usb_fifo_close_t ums_fifo_close;
175 static usb_fifo_ioctl_t ums_fifo_ioctl;
176 
177 #ifdef EVDEV_SUPPORT
178 static evdev_open_t ums_ev_open;
179 static evdev_close_t ums_ev_close;
180 static void ums_evdev_push(struct ums_softc *, int32_t, int32_t,
181     int32_t, int32_t, int32_t);
182 #endif
183 
184 static void	ums_start_rx(struct ums_softc *);
185 static void	ums_stop_rx(struct ums_softc *);
186 static void	ums_put_queue(struct ums_softc *, int32_t, int32_t,
187 		    int32_t, int32_t, int32_t);
188 static int	ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS);
189 
190 static struct usb_fifo_methods ums_fifo_methods = {
191 	.f_open = &ums_fifo_open,
192 	.f_close = &ums_fifo_close,
193 	.f_ioctl = &ums_fifo_ioctl,
194 	.f_start_read = &ums_fifo_start_read,
195 	.f_stop_read = &ums_fifo_stop_read,
196 	.basename[0] = "ums",
197 };
198 
199 #ifdef EVDEV_SUPPORT
200 static const struct evdev_methods ums_evdev_methods = {
201 	.ev_open = &ums_ev_open,
202 	.ev_close = &ums_ev_close,
203 };
204 #endif
205 
206 static void
207 ums_put_queue_timeout(void *__sc)
208 {
209 	struct ums_softc *sc = __sc;
210 
211 	mtx_assert(&sc->sc_mtx, MA_OWNED);
212 
213 	ums_put_queue(sc, 0, 0, 0, 0, 0);
214 #ifdef EVDEV_SUPPORT
215 	ums_evdev_push(sc, 0, 0, 0, 0, 0);
216 #endif
217 }
218 
219 static void
220 ums_intr_callback(struct usb_xfer *xfer, usb_error_t error)
221 {
222 	struct ums_softc *sc = usbd_xfer_softc(xfer);
223 	struct ums_info *info = &sc->sc_info[0];
224 	struct usb_page_cache *pc;
225 	uint8_t *buf = sc->sc_temp;
226 	int32_t buttons = 0;
227 	int32_t buttons_found = 0;
228 #ifdef EVDEV_SUPPORT
229 	int32_t buttons_reported = 0;
230 #endif
231 	int32_t dw = 0;
232 	int32_t dx = 0;
233 	int32_t dy = 0;
234 	int32_t dz = 0;
235 	int32_t dt = 0;
236 	uint8_t i;
237 	uint8_t id;
238 	int len;
239 
240 	usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
241 
242 	switch (USB_GET_STATE(xfer)) {
243 	case USB_ST_TRANSFERRED:
244 		DPRINTFN(6, "sc=%p actlen=%d\n", sc, len);
245 
246 		if (len > (int)sizeof(sc->sc_temp)) {
247 			DPRINTFN(6, "truncating large packet to %zu bytes\n",
248 			    sizeof(sc->sc_temp));
249 			len = sizeof(sc->sc_temp);
250 		}
251 		if (len == 0)
252 			goto tr_setup;
253 
254 		pc = usbd_xfer_get_frame(xfer, 0);
255 		usbd_copy_out(pc, 0, buf, len);
256 
257 		DPRINTFN(6, "data = %02x %02x %02x %02x "
258 		    "%02x %02x %02x %02x\n",
259 		    (len > 0) ? buf[0] : 0, (len > 1) ? buf[1] : 0,
260 		    (len > 2) ? buf[2] : 0, (len > 3) ? buf[3] : 0,
261 		    (len > 4) ? buf[4] : 0, (len > 5) ? buf[5] : 0,
262 		    (len > 6) ? buf[6] : 0, (len > 7) ? buf[7] : 0);
263 
264 		if (sc->sc_iid) {
265 			id = *buf;
266 
267 			len--;
268 			buf++;
269 
270 		} else {
271 			id = 0;
272 			if (sc->sc_info[0].sc_flags & UMS_FLAG_SBU) {
273 				if ((*buf == 0x14) || (*buf == 0x15)) {
274 					goto tr_setup;
275 				}
276 			}
277 		}
278 
279 	repeat:
280 		if ((info->sc_flags & UMS_FLAG_W_AXIS) &&
281 		    (id == info->sc_iid_w))
282 			dw += hid_get_data(buf, len, &info->sc_loc_w);
283 
284 		if ((info->sc_flags & UMS_FLAG_X_AXIS) &&
285 		    (id == info->sc_iid_x))
286 			dx += hid_get_data(buf, len, &info->sc_loc_x);
287 
288 		if ((info->sc_flags & UMS_FLAG_Y_AXIS) &&
289 		    (id == info->sc_iid_y))
290 			dy -= hid_get_data(buf, len, &info->sc_loc_y);
291 
292 		if ((info->sc_flags & UMS_FLAG_Z_AXIS) &&
293 		    (id == info->sc_iid_z)) {
294 			int32_t temp;
295 			temp = hid_get_data(buf, len, &info->sc_loc_z);
296 			if (info->sc_flags & UMS_FLAG_REVZ)
297 				temp = -temp;
298 			dz -= temp;
299 		}
300 
301 		if ((info->sc_flags & UMS_FLAG_T_AXIS) &&
302 		    (id == info->sc_iid_t)) {
303 			dt += hid_get_data(buf, len, &info->sc_loc_t);
304 			/* T-axis is translated into button presses */
305 			buttons_found |= (1UL << 5) | (1UL << 6);
306 		}
307 
308 		for (i = 0; i < info->sc_buttons; i++) {
309 			uint32_t mask;
310 			mask = 1UL << UMS_BUT(i);
311 			/* check for correct button ID */
312 			if (id != info->sc_iid_btn[i])
313 				continue;
314 			/* check for button pressed */
315 			if (hid_get_data(buf, len, &info->sc_loc_btn[i]))
316 				buttons |= mask;
317 			/* register button mask */
318 			buttons_found |= mask;
319 		}
320 
321 		if (++info != &sc->sc_info[UMS_INFO_MAX])
322 			goto repeat;
323 
324 #ifdef EVDEV_SUPPORT
325 		buttons_reported = buttons;
326 #endif
327 		/* keep old button value(s) for non-detected buttons */
328 		buttons |= sc->sc_status.button & ~buttons_found;
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 
542 	for (j = 0; (i < UMS_BUTTON_MAX) && (j < 2); i++, j++) {
543 		if (!hid_locate(buf, len, HID_USAGE2(HUP_MICROSOFT, (j + 1)),
544 		    hid_input, index, &info->sc_loc_btn[i], NULL,
545 		    &info->sc_iid_btn[i])) {
546 			break;
547 		}
548 	}
549 
550 	info->sc_buttons = i;
551 
552 	if (i > sc->sc_buttons)
553 		sc->sc_buttons = i;
554 
555 	if (info->sc_flags == 0)
556 		return;
557 
558 	/* announce information about the mouse */
559 	device_printf(dev, "%d buttons and [%s%s%s%s%s] coordinates ID=%u\n",
560 	    (info->sc_buttons),
561 	    (info->sc_flags & UMS_FLAG_X_AXIS) ? "X" : "",
562 	    (info->sc_flags & UMS_FLAG_Y_AXIS) ? "Y" : "",
563 	    (info->sc_flags & UMS_FLAG_Z_AXIS) ? "Z" : "",
564 	    (info->sc_flags & UMS_FLAG_T_AXIS) ? "T" : "",
565 	    (info->sc_flags & UMS_FLAG_W_AXIS) ? "W" : "",
566 	    info->sc_iid_x);
567 }
568 
569 static int
570 ums_attach(device_t dev)
571 {
572 	struct usb_attach_arg *uaa = device_get_ivars(dev);
573 	struct ums_softc *sc = device_get_softc(dev);
574 	struct ums_info *info;
575 	void *d_ptr = NULL;
576 	int isize;
577 	int err;
578 	uint16_t d_len;
579 	uint8_t i;
580 #ifdef USB_DEBUG
581 	uint8_t j;
582 #endif
583 
584 	DPRINTFN(11, "sc=%p\n", sc);
585 
586 	device_set_usb_desc(dev);
587 
588 	mtx_init(&sc->sc_mtx, "ums lock", NULL, MTX_DEF | MTX_RECURSE);
589 
590 	usb_callout_init_mtx(&sc->sc_callout, &sc->sc_mtx, 0);
591 
592 	/*
593          * Force the report (non-boot) protocol.
594          *
595          * Mice without boot protocol support may choose not to implement
596          * Set_Protocol at all; Ignore any error.
597          */
598 	err = usbd_req_set_protocol(uaa->device, NULL,
599 	    uaa->info.bIfaceIndex, 1);
600 
601 	err = usbd_transfer_setup(uaa->device,
602 	    &uaa->info.bIfaceIndex, sc->sc_xfer, ums_config,
603 	    UMS_N_TRANSFER, sc, &sc->sc_mtx);
604 
605 	if (err) {
606 		DPRINTF("error=%s\n", usbd_errstr(err));
607 		goto detach;
608 	}
609 
610 	/* Get HID descriptor */
611 	err = usbd_req_get_hid_desc(uaa->device, NULL, &d_ptr,
612 	    &d_len, M_TEMP, uaa->info.bIfaceIndex);
613 
614 	if (err) {
615 		device_printf(dev, "error reading report description\n");
616 		goto detach;
617 	}
618 
619 	isize = hid_report_size_max(d_ptr, d_len, hid_input, &sc->sc_iid);
620 
621 	/*
622 	 * The Microsoft Wireless Notebook Optical Mouse seems to be in worse
623 	 * shape than the Wireless Intellimouse 2.0, as its X, Y, wheel, and
624 	 * all of its other button positions are all off. It also reports that
625 	 * it has two additional buttons and a tilt wheel.
626 	 */
627 	if (usb_test_quirk(uaa, UQ_MS_BAD_CLASS)) {
628 		sc->sc_iid = 0;
629 
630 		info = &sc->sc_info[0];
631 		info->sc_flags = (UMS_FLAG_X_AXIS |
632 		    UMS_FLAG_Y_AXIS |
633 		    UMS_FLAG_Z_AXIS |
634 		    UMS_FLAG_SBU);
635 		info->sc_buttons = 3;
636 		isize = 5;
637 		/* 1st byte of descriptor report contains garbage */
638 		info->sc_loc_x.pos = 16;
639 		info->sc_loc_x.size = 8;
640 		info->sc_loc_y.pos = 24;
641 		info->sc_loc_y.size = 8;
642 		info->sc_loc_z.pos = 32;
643 		info->sc_loc_z.size = 8;
644 		info->sc_loc_btn[0].pos = 8;
645 		info->sc_loc_btn[0].size = 1;
646 		info->sc_loc_btn[1].pos = 9;
647 		info->sc_loc_btn[1].size = 1;
648 		info->sc_loc_btn[2].pos = 10;
649 		info->sc_loc_btn[2].size = 1;
650 
651 		/* Announce device */
652 		device_printf(dev, "3 buttons and [XYZ] "
653 		    "coordinates ID=0\n");
654 
655 	} else {
656 		/* Search the HID descriptor and announce device */
657 		for (i = 0; i < UMS_INFO_MAX; i++) {
658 			ums_hid_parse(sc, dev, d_ptr, d_len, i);
659 		}
660 	}
661 
662 	if (usb_test_quirk(uaa, UQ_MS_REVZ)) {
663 		info = &sc->sc_info[0];
664 		/* Some wheels need the Z axis reversed. */
665 		info->sc_flags |= UMS_FLAG_REVZ;
666 	}
667 	if (isize > (int)usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT])) {
668 		DPRINTF("WARNING: report size, %d bytes, is larger "
669 		    "than interrupt size, %d bytes!\n", isize,
670 		    usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT]));
671 	}
672 	free(d_ptr, M_TEMP);
673 	d_ptr = NULL;
674 
675 #ifdef USB_DEBUG
676 	for (j = 0; j < UMS_INFO_MAX; j++) {
677 		info = &sc->sc_info[j];
678 
679 		DPRINTF("sc=%p, index=%d\n", sc, j);
680 		DPRINTF("X\t%d/%d id=%d\n", info->sc_loc_x.pos,
681 		    info->sc_loc_x.size, info->sc_iid_x);
682 		DPRINTF("Y\t%d/%d id=%d\n", info->sc_loc_y.pos,
683 		    info->sc_loc_y.size, info->sc_iid_y);
684 		DPRINTF("Z\t%d/%d id=%d\n", info->sc_loc_z.pos,
685 		    info->sc_loc_z.size, info->sc_iid_z);
686 		DPRINTF("T\t%d/%d id=%d\n", info->sc_loc_t.pos,
687 		    info->sc_loc_t.size, info->sc_iid_t);
688 		DPRINTF("W\t%d/%d id=%d\n", info->sc_loc_w.pos,
689 		    info->sc_loc_w.size, info->sc_iid_w);
690 
691 		for (i = 0; i < info->sc_buttons; i++) {
692 			DPRINTF("B%d\t%d/%d id=%d\n",
693 			    i + 1, info->sc_loc_btn[i].pos,
694 			    info->sc_loc_btn[i].size, info->sc_iid_btn[i]);
695 		}
696 	}
697 	DPRINTF("size=%d, id=%d\n", isize, sc->sc_iid);
698 #endif
699 
700 	err = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
701 	    &ums_fifo_methods, &sc->sc_fifo,
702 	    device_get_unit(dev), -1, uaa->info.bIfaceIndex,
703   	    UID_ROOT, GID_OPERATOR, 0644);
704 	if (err)
705 		goto detach;
706 
707 #ifdef EVDEV_SUPPORT
708 	sc->sc_evdev = evdev_alloc();
709 	evdev_set_name(sc->sc_evdev, device_get_desc(dev));
710 	evdev_set_phys(sc->sc_evdev, device_get_nameunit(dev));
711 	evdev_set_id(sc->sc_evdev, BUS_USB, uaa->info.idVendor,
712 	    uaa->info.idProduct, 0);
713 	evdev_set_serial(sc->sc_evdev, usb_get_serial(uaa->device));
714 	evdev_set_methods(sc->sc_evdev, sc, &ums_evdev_methods);
715 	evdev_support_prop(sc->sc_evdev, INPUT_PROP_POINTER);
716 	evdev_support_event(sc->sc_evdev, EV_SYN);
717 	evdev_support_event(sc->sc_evdev, EV_REL);
718 	evdev_support_event(sc->sc_evdev, EV_KEY);
719 
720 	info = &sc->sc_info[0];
721 
722 	if (info->sc_flags & UMS_FLAG_X_AXIS)
723 		evdev_support_rel(sc->sc_evdev, REL_X);
724 
725 	if (info->sc_flags & UMS_FLAG_Y_AXIS)
726 		evdev_support_rel(sc->sc_evdev, REL_Y);
727 
728 	if (info->sc_flags & UMS_FLAG_Z_AXIS)
729 		evdev_support_rel(sc->sc_evdev, REL_WHEEL);
730 
731 	if (info->sc_flags & UMS_FLAG_T_AXIS)
732 		evdev_support_rel(sc->sc_evdev, REL_HWHEEL);
733 
734 	for (i = 0; i < info->sc_buttons; i++)
735 		evdev_support_key(sc->sc_evdev, BTN_MOUSE + i);
736 
737 	err = evdev_register_mtx(sc->sc_evdev, &sc->sc_mtx);
738 	if (err)
739 		goto detach;
740 #endif
741 
742 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
743 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
744 	    OID_AUTO, "parseinfo",
745 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
746 	    sc, 0, ums_sysctl_handler_parseinfo, "",
747 	    "Dump of parsed HID report descriptor");
748 
749 	return (0);
750 
751 detach:
752 	if (d_ptr) {
753 		free(d_ptr, M_TEMP);
754 	}
755 	ums_detach(dev);
756 	return (ENOMEM);
757 }
758 
759 static int
760 ums_detach(device_t self)
761 {
762 	struct ums_softc *sc = device_get_softc(self);
763 
764 	DPRINTF("sc=%p\n", sc);
765 
766 	usb_fifo_detach(&sc->sc_fifo);
767 
768 #ifdef EVDEV_SUPPORT
769 	evdev_free(sc->sc_evdev);
770 #endif
771 
772 	usbd_transfer_unsetup(sc->sc_xfer, UMS_N_TRANSFER);
773 
774 	usb_callout_drain(&sc->sc_callout);
775 
776 	mtx_destroy(&sc->sc_mtx);
777 
778 	return (0);
779 }
780 
781 static void
782 ums_reset(struct ums_softc *sc)
783 {
784 
785 	/* reset all USB mouse parameters */
786 
787 	if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
788 		sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
789 	else
790 		sc->sc_hw.buttons = sc->sc_buttons;
791 
792 	sc->sc_hw.iftype = MOUSE_IF_USB;
793 	sc->sc_hw.type = MOUSE_MOUSE;
794 	sc->sc_hw.model = MOUSE_MODEL_GENERIC;
795 	sc->sc_hw.hwid = 0;
796 
797 	sc->sc_mode.protocol = MOUSE_PROTO_MSC;
798 	sc->sc_mode.rate = -1;
799 	sc->sc_mode.resolution = MOUSE_RES_UNKNOWN;
800 	sc->sc_mode.accelfactor = 0;
801 	sc->sc_mode.level = 0;
802 	sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
803 	sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
804 	sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
805 
806 	/* reset status */
807 
808 	sc->sc_status.flags = 0;
809 	sc->sc_status.button = 0;
810 	sc->sc_status.obutton = 0;
811 	sc->sc_status.dx = 0;
812 	sc->sc_status.dy = 0;
813 	sc->sc_status.dz = 0;
814 	/* sc->sc_status.dt = 0; */
815 }
816 
817 static void
818 ums_start_rx(struct ums_softc *sc)
819 {
820 	int rate;
821 
822 	/* Check if we should override the default polling interval */
823 	rate = sc->sc_pollrate;
824 	/* Range check rate */
825 	if (rate > 1000)
826 		rate = 1000;
827 	/* Check for set rate */
828 	if ((rate > 0) && (sc->sc_xfer[UMS_INTR_DT] != NULL)) {
829 		DPRINTF("Setting pollrate = %d\n", rate);
830 		/* Stop current transfer, if any */
831 		usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
832 		/* Set new interval */
833 		usbd_xfer_set_interval(sc->sc_xfer[UMS_INTR_DT], 1000 / rate);
834 		/* Only set pollrate once */
835 		sc->sc_pollrate = 0;
836 	}
837 
838 	usbd_transfer_start(sc->sc_xfer[UMS_INTR_DT]);
839 }
840 
841 static void
842 ums_stop_rx(struct ums_softc *sc)
843 {
844 	usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
845 	usb_callout_stop(&sc->sc_callout);
846 }
847 
848 static void
849 ums_fifo_start_read(struct usb_fifo *fifo)
850 {
851 	struct ums_softc *sc = usb_fifo_softc(fifo);
852 
853 	ums_start_rx(sc);
854 }
855 
856 static void
857 ums_fifo_stop_read(struct usb_fifo *fifo)
858 {
859 	struct ums_softc *sc = usb_fifo_softc(fifo);
860 
861 #ifdef EVDEV_SUPPORT
862 	if ((sc->sc_evflags & UMS_EVDEV_OPENED) == 0)
863 #endif
864 		ums_stop_rx(sc);
865 }
866 
867 #if ((MOUSE_SYS_PACKETSIZE != 8) || \
868      (MOUSE_MSC_PACKETSIZE != 5))
869 #error "Software assumptions are not met. Please update code."
870 #endif
871 
872 static void
873 ums_put_queue(struct ums_softc *sc, int32_t dx, int32_t dy,
874     int32_t dz, int32_t dt, int32_t buttons)
875 {
876 	uint8_t buf[8];
877 
878 	if (1) {
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)
946 {
947 	struct ums_softc *sc = evdev_get_softc(evdev);
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 	}
956 	ums_start_rx(sc);
957 
958 	return (0);
959 }
960 
961 static int
962 ums_ev_close(struct evdev_dev *evdev)
963 {
964 	struct ums_softc *sc = evdev_get_softc(evdev);
965 
966 	mtx_assert(&sc->sc_mtx, MA_OWNED);
967 
968 	sc->sc_evflags &= ~UMS_EVDEV_OPENED;
969 
970 	if (sc->sc_fflags == 0)
971 		ums_stop_rx(sc);
972 
973 	return (0);
974 }
975 #endif
976 
977 static int
978 ums_fifo_open(struct usb_fifo *fifo, int fflags)
979 {
980 	struct ums_softc *sc = usb_fifo_softc(fifo);
981 
982 	DPRINTFN(2, "\n");
983 
984 	/* check for duplicate open, should not happen */
985 	if (sc->sc_fflags & fflags)
986 		return (EBUSY);
987 
988 	/* check for first open */
989 #ifdef EVDEV_SUPPORT
990 	if (sc->sc_fflags == 0 && (sc->sc_evflags & UMS_EVDEV_OPENED) == 0)
991 		ums_reset(sc);
992 #else
993 	if (sc->sc_fflags == 0)
994 		ums_reset(sc);
995 #endif
996 
997 	if (fflags & FREAD) {
998 		/* allocate RX buffer */
999 		if (usb_fifo_alloc_buffer(fifo,
1000 		    UMS_BUF_SIZE, UMS_IFQ_MAXLEN)) {
1001 			return (ENOMEM);
1002 		}
1003 	}
1004 
1005 	sc->sc_fflags |= fflags & (FREAD | FWRITE);
1006 	return (0);
1007 }
1008 
1009 static void
1010 ums_fifo_close(struct usb_fifo *fifo, int fflags)
1011 {
1012 	struct ums_softc *sc = usb_fifo_softc(fifo);
1013 
1014 	DPRINTFN(2, "\n");
1015 
1016 	if (fflags & FREAD)
1017 		usb_fifo_free_buffer(fifo);
1018 
1019 	sc->sc_fflags &= ~(fflags & (FREAD | FWRITE));
1020 }
1021 
1022 static int
1023 ums_fifo_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1024 {
1025 	struct ums_softc *sc = usb_fifo_softc(fifo);
1026 	mousemode_t mode;
1027 	int error = 0;
1028 
1029 	DPRINTFN(2, "\n");
1030 
1031 	mtx_lock(&sc->sc_mtx);
1032 
1033 	switch (cmd) {
1034 	case MOUSE_GETHWINFO:
1035 		*(mousehw_t *)addr = sc->sc_hw;
1036 		break;
1037 
1038 	case MOUSE_GETMODE:
1039 		*(mousemode_t *)addr = sc->sc_mode;
1040 		break;
1041 
1042 	case MOUSE_SETMODE:
1043 		mode = *(mousemode_t *)addr;
1044 
1045 		if (mode.level == -1) {
1046 			/* don't change the current setting */
1047 		} else if ((mode.level < 0) || (mode.level > 1)) {
1048 			error = EINVAL;
1049 			break;
1050 		} else {
1051 			sc->sc_mode.level = mode.level;
1052 		}
1053 
1054 		/* store polling rate */
1055 		sc->sc_pollrate = mode.rate;
1056 
1057 		if (sc->sc_mode.level == 0) {
1058 			if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
1059 				sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
1060 			else
1061 				sc->sc_hw.buttons = sc->sc_buttons;
1062 			sc->sc_mode.protocol = MOUSE_PROTO_MSC;
1063 			sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
1064 			sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
1065 			sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
1066 		} else if (sc->sc_mode.level == 1) {
1067 			if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
1068 				sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
1069 			else
1070 				sc->sc_hw.buttons = sc->sc_buttons;
1071 			sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
1072 			sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
1073 			sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
1074 			sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
1075 		}
1076 		ums_reset_buf(sc);
1077 		break;
1078 
1079 	case MOUSE_GETLEVEL:
1080 		*(int *)addr = sc->sc_mode.level;
1081 		break;
1082 
1083 	case MOUSE_SETLEVEL:
1084 		if (*(int *)addr < 0 || *(int *)addr > 1) {
1085 			error = EINVAL;
1086 			break;
1087 		}
1088 		sc->sc_mode.level = *(int *)addr;
1089 
1090 		if (sc->sc_mode.level == 0) {
1091 			if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
1092 				sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
1093 			else
1094 				sc->sc_hw.buttons = sc->sc_buttons;
1095 			sc->sc_mode.protocol = MOUSE_PROTO_MSC;
1096 			sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
1097 			sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
1098 			sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
1099 		} else if (sc->sc_mode.level == 1) {
1100 			if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
1101 				sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
1102 			else
1103 				sc->sc_hw.buttons = sc->sc_buttons;
1104 			sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
1105 			sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
1106 			sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
1107 			sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
1108 		}
1109 		ums_reset_buf(sc);
1110 		break;
1111 
1112 	case MOUSE_GETSTATUS:{
1113 			mousestatus_t *status = (mousestatus_t *)addr;
1114 
1115 			*status = sc->sc_status;
1116 			sc->sc_status.obutton = sc->sc_status.button;
1117 			sc->sc_status.button = 0;
1118 			sc->sc_status.dx = 0;
1119 			sc->sc_status.dy = 0;
1120 			sc->sc_status.dz = 0;
1121 			/* sc->sc_status.dt = 0; */
1122 
1123 			if (status->dx || status->dy || status->dz /* || status->dt */ ) {
1124 				status->flags |= MOUSE_POSCHANGED;
1125 			}
1126 			if (status->button != status->obutton) {
1127 				status->flags |= MOUSE_BUTTONSCHANGED;
1128 			}
1129 			break;
1130 		}
1131 	default:
1132 		error = ENOTTY;
1133 		break;
1134 	}
1135 
1136 	mtx_unlock(&sc->sc_mtx);
1137 	return (error);
1138 }
1139 
1140 static int
1141 ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS)
1142 {
1143 	struct ums_softc *sc = arg1;
1144 	struct ums_info *info;
1145 	struct sbuf *sb;
1146 	int i, j, err, had_output;
1147 
1148 	sb = sbuf_new_auto();
1149 	for (i = 0, had_output = 0; i < UMS_INFO_MAX; i++) {
1150 		info = &sc->sc_info[i];
1151 
1152 		/* Don't emit empty info */
1153 		if ((info->sc_flags &
1154 		    (UMS_FLAG_X_AXIS | UMS_FLAG_Y_AXIS | UMS_FLAG_Z_AXIS |
1155 		     UMS_FLAG_T_AXIS | UMS_FLAG_W_AXIS)) == 0 &&
1156 		    info->sc_buttons == 0)
1157 			continue;
1158 
1159 		if (had_output)
1160 			sbuf_printf(sb, "\n");
1161 		had_output = 1;
1162 		sbuf_printf(sb, "i%d:", i + 1);
1163 		if (info->sc_flags & UMS_FLAG_X_AXIS)
1164 			sbuf_printf(sb, " X:r%d, p%d, s%d;",
1165 			    (int)info->sc_iid_x,
1166 			    (int)info->sc_loc_x.pos,
1167 			    (int)info->sc_loc_x.size);
1168 		if (info->sc_flags & UMS_FLAG_Y_AXIS)
1169 			sbuf_printf(sb, " Y:r%d, p%d, s%d;",
1170 			    (int)info->sc_iid_y,
1171 			    (int)info->sc_loc_y.pos,
1172 			    (int)info->sc_loc_y.size);
1173 		if (info->sc_flags & UMS_FLAG_Z_AXIS)
1174 			sbuf_printf(sb, " Z:r%d, p%d, s%d;",
1175 			    (int)info->sc_iid_z,
1176 			    (int)info->sc_loc_z.pos,
1177 			    (int)info->sc_loc_z.size);
1178 		if (info->sc_flags & UMS_FLAG_T_AXIS)
1179 			sbuf_printf(sb, " T:r%d, p%d, s%d;",
1180 			    (int)info->sc_iid_t,
1181 			    (int)info->sc_loc_t.pos,
1182 			    (int)info->sc_loc_t.size);
1183 		if (info->sc_flags & UMS_FLAG_W_AXIS)
1184 			sbuf_printf(sb, " W:r%d, p%d, s%d;",
1185 			    (int)info->sc_iid_w,
1186 			    (int)info->sc_loc_w.pos,
1187 			    (int)info->sc_loc_w.size);
1188 
1189 		for (j = 0; j < info->sc_buttons; j++) {
1190 			sbuf_printf(sb, " B%d:r%d, p%d, s%d;", j + 1,
1191 			    (int)info->sc_iid_btn[j],
1192 			    (int)info->sc_loc_btn[j].pos,
1193 			    (int)info->sc_loc_btn[j].size);
1194 		}
1195 	}
1196 	sbuf_finish(sb);
1197 	err = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
1198 	sbuf_delete(sb);
1199 
1200 	return (err);
1201 }
1202 
1203 static devclass_t ums_devclass;
1204 
1205 static device_method_t ums_methods[] = {
1206 	DEVMETHOD(device_probe, ums_probe),
1207 	DEVMETHOD(device_attach, ums_attach),
1208 	DEVMETHOD(device_detach, ums_detach),
1209 
1210 	DEVMETHOD_END
1211 };
1212 
1213 static driver_t ums_driver = {
1214 	.name = "ums",
1215 	.methods = ums_methods,
1216 	.size = sizeof(struct ums_softc),
1217 };
1218 
1219 DRIVER_MODULE(ums, uhub, ums_driver, ums_devclass, NULL, 0);
1220 MODULE_DEPEND(ums, usb, 1, 1, 1);
1221 MODULE_DEPEND(ums, hid, 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