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