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