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