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