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