xref: /freebsd/sys/dev/usb/input/ums.c (revision e17f5b1d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Lennart Augustsson (lennart@augustsson.net) at
9  * Carlstedt Research & Technology.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 /*
37  * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
38  */
39 
40 #include "opt_evdev.h"
41 
42 #include <sys/stdint.h>
43 #include <sys/stddef.h>
44 #include <sys/param.h>
45 #include <sys/queue.h>
46 #include <sys/types.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/bus.h>
50 #include <sys/module.h>
51 #include <sys/lock.h>
52 #include <sys/mutex.h>
53 #include <sys/condvar.h>
54 #include <sys/sysctl.h>
55 #include <sys/sx.h>
56 #include <sys/unistd.h>
57 #include <sys/callout.h>
58 #include <sys/malloc.h>
59 #include <sys/priv.h>
60 #include <sys/conf.h>
61 #include <sys/fcntl.h>
62 #include <sys/sbuf.h>
63 
64 #include <dev/usb/usb.h>
65 #include <dev/usb/usbdi.h>
66 #include <dev/usb/usbdi_util.h>
67 #include <dev/usb/usbhid.h>
68 #include "usbdevs.h"
69 
70 #define	USB_DEBUG_VAR ums_debug
71 #include <dev/usb/usb_debug.h>
72 
73 #include <dev/usb/quirk/usb_quirk.h>
74 
75 #ifdef EVDEV_SUPPORT
76 #include <dev/evdev/input.h>
77 #include <dev/evdev/evdev.h>
78 #endif
79 
80 #include <sys/ioccom.h>
81 #include <sys/filio.h>
82 #include <sys/mouse.h>
83 
84 #ifdef USB_DEBUG
85 static int ums_debug = 0;
86 
87 static SYSCTL_NODE(_hw_usb, OID_AUTO, ums, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
88     "USB ums");
89 SYSCTL_INT(_hw_usb_ums, OID_AUTO, debug, CTLFLAG_RWTUN,
90     &ums_debug, 0, "Debug level");
91 #endif
92 
93 #define	MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
94 #define	MOUSE_FLAGS (HIO_RELATIVE)
95 
96 #define	UMS_BUF_SIZE      8		/* bytes */
97 #define	UMS_IFQ_MAXLEN   50		/* units */
98 #define	UMS_BUTTON_MAX   31		/* exclusive, must be less than 32 */
99 #define	UMS_BUT(i) ((i) < 3 ? (((i) + 2) % 3) : (i))
100 #define	UMS_INFO_MAX	  2		/* maximum number of HID sets */
101 
102 enum {
103 	UMS_INTR_DT,
104 	UMS_N_TRANSFER,
105 };
106 
107 struct ums_info {
108 	struct hid_location sc_loc_w;
109 	struct hid_location sc_loc_x;
110 	struct hid_location sc_loc_y;
111 	struct hid_location sc_loc_z;
112 	struct hid_location sc_loc_t;
113 	struct hid_location sc_loc_btn[UMS_BUTTON_MAX];
114 
115 	uint32_t sc_flags;
116 #define	UMS_FLAG_X_AXIS     0x0001
117 #define	UMS_FLAG_Y_AXIS     0x0002
118 #define	UMS_FLAG_Z_AXIS     0x0004
119 #define	UMS_FLAG_T_AXIS     0x0008
120 #define	UMS_FLAG_SBU        0x0010	/* spurious button up events */
121 #define	UMS_FLAG_REVZ	    0x0020	/* Z-axis is reversed */
122 #define	UMS_FLAG_W_AXIS     0x0040
123 
124 	uint8_t	sc_iid_w;
125 	uint8_t	sc_iid_x;
126 	uint8_t	sc_iid_y;
127 	uint8_t	sc_iid_z;
128 	uint8_t	sc_iid_t;
129 	uint8_t	sc_iid_btn[UMS_BUTTON_MAX];
130 	uint8_t	sc_buttons;
131 };
132 
133 struct ums_softc {
134 	struct usb_fifo_sc sc_fifo;
135 	struct mtx sc_mtx;
136 	struct usb_callout sc_callout;
137 	struct ums_info sc_info[UMS_INFO_MAX];
138 
139 	mousehw_t sc_hw;
140 	mousemode_t sc_mode;
141 	mousestatus_t sc_status;
142 
143 	struct usb_xfer *sc_xfer[UMS_N_TRANSFER];
144 
145 	int sc_pollrate;
146 	int sc_fflags;
147 #ifdef EVDEV_SUPPORT
148 	int sc_evflags;
149 #define	UMS_EVDEV_OPENED	1
150 #endif
151 
152 	uint8_t	sc_buttons;
153 	uint8_t	sc_iid;
154 	uint8_t	sc_temp[64];
155 
156 #ifdef EVDEV_SUPPORT
157 	struct evdev_dev *sc_evdev;
158 #endif
159 };
160 
161 static void ums_put_queue_timeout(void *__sc);
162 
163 static usb_callback_t ums_intr_callback;
164 
165 static device_probe_t ums_probe;
166 static device_attach_t ums_attach;
167 static device_detach_t ums_detach;
168 
169 static usb_fifo_cmd_t ums_fifo_start_read;
170 static usb_fifo_cmd_t ums_fifo_stop_read;
171 static usb_fifo_open_t ums_fifo_open;
172 static usb_fifo_close_t ums_fifo_close;
173 static usb_fifo_ioctl_t ums_fifo_ioctl;
174 
175 #ifdef EVDEV_SUPPORT
176 static evdev_open_t ums_ev_open;
177 static evdev_close_t ums_ev_close;
178 static void ums_evdev_push(struct ums_softc *, int32_t, int32_t,
179     int32_t, int32_t, int32_t);
180 #endif
181 
182 static void	ums_start_rx(struct ums_softc *);
183 static void	ums_stop_rx(struct ums_softc *);
184 static void	ums_put_queue(struct ums_softc *, int32_t, int32_t,
185 		    int32_t, int32_t, int32_t);
186 static int	ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS);
187 
188 static struct usb_fifo_methods ums_fifo_methods = {
189 	.f_open = &ums_fifo_open,
190 	.f_close = &ums_fifo_close,
191 	.f_ioctl = &ums_fifo_ioctl,
192 	.f_start_read = &ums_fifo_start_read,
193 	.f_stop_read = &ums_fifo_stop_read,
194 	.basename[0] = "ums",
195 };
196 
197 #ifdef EVDEV_SUPPORT
198 static const struct evdev_methods ums_evdev_methods = {
199 	.ev_open = &ums_ev_open,
200 	.ev_close = &ums_ev_close,
201 };
202 #endif
203 
204 static void
205 ums_put_queue_timeout(void *__sc)
206 {
207 	struct ums_softc *sc = __sc;
208 
209 	mtx_assert(&sc->sc_mtx, MA_OWNED);
210 
211 	ums_put_queue(sc, 0, 0, 0, 0, 0);
212 #ifdef EVDEV_SUPPORT
213 	ums_evdev_push(sc, 0, 0, 0, 0, 0);
214 #endif
215 }
216 
217 static void
218 ums_intr_callback(struct usb_xfer *xfer, usb_error_t error)
219 {
220 	struct ums_softc *sc = usbd_xfer_softc(xfer);
221 	struct ums_info *info = &sc->sc_info[0];
222 	struct usb_page_cache *pc;
223 	uint8_t *buf = sc->sc_temp;
224 	int32_t buttons = 0;
225 	int32_t buttons_found = 0;
226 #ifdef EVDEV_SUPPORT
227 	int32_t buttons_reported = 0;
228 #endif
229 	int32_t dw = 0;
230 	int32_t dx = 0;
231 	int32_t dy = 0;
232 	int32_t dz = 0;
233 	int32_t dt = 0;
234 	uint8_t i;
235 	uint8_t id;
236 	int len;
237 
238 	usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
239 
240 	switch (USB_GET_STATE(xfer)) {
241 	case USB_ST_TRANSFERRED:
242 		DPRINTFN(6, "sc=%p actlen=%d\n", sc, len);
243 
244 		if (len > (int)sizeof(sc->sc_temp)) {
245 			DPRINTFN(6, "truncating large packet to %zu bytes\n",
246 			    sizeof(sc->sc_temp));
247 			len = sizeof(sc->sc_temp);
248 		}
249 		if (len == 0)
250 			goto tr_setup;
251 
252 		pc = usbd_xfer_get_frame(xfer, 0);
253 		usbd_copy_out(pc, 0, buf, len);
254 
255 		DPRINTFN(6, "data = %02x %02x %02x %02x "
256 		    "%02x %02x %02x %02x\n",
257 		    (len > 0) ? buf[0] : 0, (len > 1) ? buf[1] : 0,
258 		    (len > 2) ? buf[2] : 0, (len > 3) ? buf[3] : 0,
259 		    (len > 4) ? buf[4] : 0, (len > 5) ? buf[5] : 0,
260 		    (len > 6) ? buf[6] : 0, (len > 7) ? buf[7] : 0);
261 
262 		if (sc->sc_iid) {
263 			id = *buf;
264 
265 			len--;
266 			buf++;
267 
268 		} else {
269 			id = 0;
270 			if (sc->sc_info[0].sc_flags & UMS_FLAG_SBU) {
271 				if ((*buf == 0x14) || (*buf == 0x15)) {
272 					goto tr_setup;
273 				}
274 			}
275 		}
276 
277 	repeat:
278 		if ((info->sc_flags & UMS_FLAG_W_AXIS) &&
279 		    (id == info->sc_iid_w))
280 			dw += hid_get_data(buf, len, &info->sc_loc_w);
281 
282 		if ((info->sc_flags & UMS_FLAG_X_AXIS) &&
283 		    (id == info->sc_iid_x))
284 			dx += hid_get_data(buf, len, &info->sc_loc_x);
285 
286 		if ((info->sc_flags & UMS_FLAG_Y_AXIS) &&
287 		    (id == info->sc_iid_y))
288 			dy -= hid_get_data(buf, len, &info->sc_loc_y);
289 
290 		if ((info->sc_flags & UMS_FLAG_Z_AXIS) &&
291 		    (id == info->sc_iid_z)) {
292 			int32_t temp;
293 			temp = hid_get_data(buf, len, &info->sc_loc_z);
294 			if (info->sc_flags & UMS_FLAG_REVZ)
295 				temp = -temp;
296 			dz -= temp;
297 		}
298 
299 		if ((info->sc_flags & UMS_FLAG_T_AXIS) &&
300 		    (id == info->sc_iid_t)) {
301 			dt += hid_get_data(buf, len, &info->sc_loc_t);
302 			/* T-axis is translated into button presses */
303 			buttons_found |= (1UL << 5) | (1UL << 6);
304 		}
305 
306 		for (i = 0; i < info->sc_buttons; i++) {
307 			uint32_t mask;
308 			mask = 1UL << UMS_BUT(i);
309 			/* check for correct button ID */
310 			if (id != info->sc_iid_btn[i])
311 				continue;
312 			/* check for button pressed */
313 			if (hid_get_data(buf, len, &info->sc_loc_btn[i]))
314 				buttons |= mask;
315 			/* register button mask */
316 			buttons_found |= mask;
317 		}
318 
319 		if (++info != &sc->sc_info[UMS_INFO_MAX])
320 			goto repeat;
321 
322 #ifdef EVDEV_SUPPORT
323 		buttons_reported = buttons;
324 #endif
325 		/* keep old button value(s) for non-detected buttons */
326 		buttons |= sc->sc_status.button & ~buttons_found;
327 
328 		if (dx || dy || dz || dt || dw ||
329 		    (buttons != sc->sc_status.button)) {
330 
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",
755 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
756 	    sc, 0, ums_sysctl_handler_parseinfo, "",
757 	    "Dump of parsed HID report descriptor");
758 
759 	return (0);
760 
761 detach:
762 	if (d_ptr) {
763 		free(d_ptr, M_TEMP);
764 	}
765 	ums_detach(dev);
766 	return (ENOMEM);
767 }
768 
769 static int
770 ums_detach(device_t self)
771 {
772 	struct ums_softc *sc = device_get_softc(self);
773 
774 	DPRINTF("sc=%p\n", sc);
775 
776 	usb_fifo_detach(&sc->sc_fifo);
777 
778 #ifdef EVDEV_SUPPORT
779 	evdev_free(sc->sc_evdev);
780 #endif
781 
782 	usbd_transfer_unsetup(sc->sc_xfer, UMS_N_TRANSFER);
783 
784 	usb_callout_drain(&sc->sc_callout);
785 
786 	mtx_destroy(&sc->sc_mtx);
787 
788 	return (0);
789 }
790 
791 static void
792 ums_reset(struct ums_softc *sc)
793 {
794 
795 	/* reset all USB mouse parameters */
796 
797 	if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
798 		sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
799 	else
800 		sc->sc_hw.buttons = sc->sc_buttons;
801 
802 	sc->sc_hw.iftype = MOUSE_IF_USB;
803 	sc->sc_hw.type = MOUSE_MOUSE;
804 	sc->sc_hw.model = MOUSE_MODEL_GENERIC;
805 	sc->sc_hw.hwid = 0;
806 
807 	sc->sc_mode.protocol = MOUSE_PROTO_MSC;
808 	sc->sc_mode.rate = -1;
809 	sc->sc_mode.resolution = MOUSE_RES_UNKNOWN;
810 	sc->sc_mode.accelfactor = 0;
811 	sc->sc_mode.level = 0;
812 	sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
813 	sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
814 	sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
815 
816 	/* reset status */
817 
818 	sc->sc_status.flags = 0;
819 	sc->sc_status.button = 0;
820 	sc->sc_status.obutton = 0;
821 	sc->sc_status.dx = 0;
822 	sc->sc_status.dy = 0;
823 	sc->sc_status.dz = 0;
824 	/* sc->sc_status.dt = 0; */
825 }
826 
827 static void
828 ums_start_rx(struct ums_softc *sc)
829 {
830 	int rate;
831 
832 	/* Check if we should override the default polling interval */
833 	rate = sc->sc_pollrate;
834 	/* Range check rate */
835 	if (rate > 1000)
836 		rate = 1000;
837 	/* Check for set rate */
838 	if ((rate > 0) && (sc->sc_xfer[UMS_INTR_DT] != NULL)) {
839 		DPRINTF("Setting pollrate = %d\n", rate);
840 		/* Stop current transfer, if any */
841 		usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
842 		/* Set new interval */
843 		usbd_xfer_set_interval(sc->sc_xfer[UMS_INTR_DT], 1000 / rate);
844 		/* Only set pollrate once */
845 		sc->sc_pollrate = 0;
846 	}
847 
848 	usbd_transfer_start(sc->sc_xfer[UMS_INTR_DT]);
849 }
850 
851 static void
852 ums_stop_rx(struct ums_softc *sc)
853 {
854 	usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
855 	usb_callout_stop(&sc->sc_callout);
856 }
857 
858 static void
859 ums_fifo_start_read(struct usb_fifo *fifo)
860 {
861 	struct ums_softc *sc = usb_fifo_softc(fifo);
862 
863 	ums_start_rx(sc);
864 }
865 
866 static void
867 ums_fifo_stop_read(struct usb_fifo *fifo)
868 {
869 	struct ums_softc *sc = usb_fifo_softc(fifo);
870 
871 	ums_stop_rx(sc);
872 }
873 
874 
875 #if ((MOUSE_SYS_PACKETSIZE != 8) || \
876      (MOUSE_MSC_PACKETSIZE != 5))
877 #error "Software assumptions are not met. Please update code."
878 #endif
879 
880 static void
881 ums_put_queue(struct ums_softc *sc, int32_t dx, int32_t dy,
882     int32_t dz, int32_t dt, int32_t buttons)
883 {
884 	uint8_t buf[8];
885 
886 	if (1) {
887 
888 		if (dx > 254)
889 			dx = 254;
890 		if (dx < -256)
891 			dx = -256;
892 		if (dy > 254)
893 			dy = 254;
894 		if (dy < -256)
895 			dy = -256;
896 		if (dz > 126)
897 			dz = 126;
898 		if (dz < -128)
899 			dz = -128;
900 		if (dt > 126)
901 			dt = 126;
902 		if (dt < -128)
903 			dt = -128;
904 
905 		buf[0] = sc->sc_mode.syncmask[1];
906 		buf[0] |= (~buttons) & MOUSE_MSC_BUTTONS;
907 		buf[1] = dx >> 1;
908 		buf[2] = dy >> 1;
909 		buf[3] = dx - (dx >> 1);
910 		buf[4] = dy - (dy >> 1);
911 
912 		if (sc->sc_mode.level == 1) {
913 			buf[5] = dz >> 1;
914 			buf[6] = dz - (dz >> 1);
915 			buf[7] = (((~buttons) >> 3) & MOUSE_SYS_EXTBUTTONS);
916 		}
917 		usb_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf,
918 		    sc->sc_mode.packetsize, 1);
919 	} else {
920 		DPRINTF("Buffer full, discarded packet\n");
921 	}
922 }
923 
924 #ifdef EVDEV_SUPPORT
925 static void
926 ums_evdev_push(struct ums_softc *sc, int32_t dx, int32_t dy,
927     int32_t dz, int32_t dt, int32_t buttons)
928 {
929 	if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) {
930 		/* Push evdev event */
931 		evdev_push_rel(sc->sc_evdev, REL_X, dx);
932 		evdev_push_rel(sc->sc_evdev, REL_Y, -dy);
933 		evdev_push_rel(sc->sc_evdev, REL_WHEEL, -dz);
934 		evdev_push_rel(sc->sc_evdev, REL_HWHEEL, dt);
935 		evdev_push_mouse_btn(sc->sc_evdev,
936 		    (buttons & ~MOUSE_STDBUTTONS) |
937 		    (buttons & (1 << 2) ? MOUSE_BUTTON1DOWN : 0) |
938 		    (buttons & (1 << 1) ? MOUSE_BUTTON2DOWN : 0) |
939 		    (buttons & (1 << 0) ? MOUSE_BUTTON3DOWN : 0));
940 		evdev_sync(sc->sc_evdev);
941 	}
942 }
943 #endif
944 
945 static void
946 ums_reset_buf(struct ums_softc *sc)
947 {
948 	/* reset read queue, must be called locked */
949 	usb_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]);
950 }
951 
952 #ifdef EVDEV_SUPPORT
953 static int
954 ums_ev_open(struct evdev_dev *evdev)
955 {
956 	struct ums_softc *sc = evdev_get_softc(evdev);
957 
958 	mtx_assert(&sc->sc_mtx, MA_OWNED);
959 
960 	sc->sc_evflags = UMS_EVDEV_OPENED;
961 
962 	if (sc->sc_fflags == 0) {
963 		ums_reset(sc);
964 		ums_start_rx(sc);
965 	}
966 
967 	return (0);
968 }
969 
970 static int
971 ums_ev_close(struct evdev_dev *evdev)
972 {
973 	struct ums_softc *sc = evdev_get_softc(evdev);
974 
975 	mtx_assert(&sc->sc_mtx, MA_OWNED);
976 
977 	sc->sc_evflags = 0;
978 
979 	if (sc->sc_fflags == 0)
980 		ums_stop_rx(sc);
981 
982 	return (0);
983 }
984 #endif
985 
986 static int
987 ums_fifo_open(struct usb_fifo *fifo, int fflags)
988 {
989 	struct ums_softc *sc = usb_fifo_softc(fifo);
990 
991 	DPRINTFN(2, "\n");
992 
993 	/* check for duplicate open, should not happen */
994 	if (sc->sc_fflags & fflags)
995 		return (EBUSY);
996 
997 	/* check for first open */
998 #ifdef EVDEV_SUPPORT
999 	if (sc->sc_fflags == 0 && sc->sc_evflags == 0)
1000 		ums_reset(sc);
1001 #else
1002 	if (sc->sc_fflags == 0)
1003 		ums_reset(sc);
1004 #endif
1005 
1006 	if (fflags & FREAD) {
1007 		/* allocate RX buffer */
1008 		if (usb_fifo_alloc_buffer(fifo,
1009 		    UMS_BUF_SIZE, UMS_IFQ_MAXLEN)) {
1010 			return (ENOMEM);
1011 		}
1012 	}
1013 
1014 	sc->sc_fflags |= fflags & (FREAD | FWRITE);
1015 	return (0);
1016 }
1017 
1018 static void
1019 ums_fifo_close(struct usb_fifo *fifo, int fflags)
1020 {
1021 	struct ums_softc *sc = usb_fifo_softc(fifo);
1022 
1023 	DPRINTFN(2, "\n");
1024 
1025 	if (fflags & FREAD)
1026 		usb_fifo_free_buffer(fifo);
1027 
1028 	sc->sc_fflags &= ~(fflags & (FREAD | FWRITE));
1029 }
1030 
1031 static int
1032 ums_fifo_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1033 {
1034 	struct ums_softc *sc = usb_fifo_softc(fifo);
1035 	mousemode_t mode;
1036 	int error = 0;
1037 
1038 	DPRINTFN(2, "\n");
1039 
1040 	mtx_lock(&sc->sc_mtx);
1041 
1042 	switch (cmd) {
1043 	case MOUSE_GETHWINFO:
1044 		*(mousehw_t *)addr = sc->sc_hw;
1045 		break;
1046 
1047 	case MOUSE_GETMODE:
1048 		*(mousemode_t *)addr = sc->sc_mode;
1049 		break;
1050 
1051 	case MOUSE_SETMODE:
1052 		mode = *(mousemode_t *)addr;
1053 
1054 		if (mode.level == -1) {
1055 			/* don't change the current setting */
1056 		} else if ((mode.level < 0) || (mode.level > 1)) {
1057 			error = EINVAL;
1058 			break;
1059 		} else {
1060 			sc->sc_mode.level = mode.level;
1061 		}
1062 
1063 		/* store polling rate */
1064 		sc->sc_pollrate = mode.rate;
1065 
1066 		if (sc->sc_mode.level == 0) {
1067 			if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
1068 				sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
1069 			else
1070 				sc->sc_hw.buttons = sc->sc_buttons;
1071 			sc->sc_mode.protocol = MOUSE_PROTO_MSC;
1072 			sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
1073 			sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
1074 			sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
1075 		} else if (sc->sc_mode.level == 1) {
1076 			if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
1077 				sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
1078 			else
1079 				sc->sc_hw.buttons = sc->sc_buttons;
1080 			sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
1081 			sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
1082 			sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
1083 			sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
1084 		}
1085 		ums_reset_buf(sc);
1086 		break;
1087 
1088 	case MOUSE_GETLEVEL:
1089 		*(int *)addr = sc->sc_mode.level;
1090 		break;
1091 
1092 	case MOUSE_SETLEVEL:
1093 		if (*(int *)addr < 0 || *(int *)addr > 1) {
1094 			error = EINVAL;
1095 			break;
1096 		}
1097 		sc->sc_mode.level = *(int *)addr;
1098 
1099 		if (sc->sc_mode.level == 0) {
1100 			if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
1101 				sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
1102 			else
1103 				sc->sc_hw.buttons = sc->sc_buttons;
1104 			sc->sc_mode.protocol = MOUSE_PROTO_MSC;
1105 			sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
1106 			sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
1107 			sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
1108 		} else if (sc->sc_mode.level == 1) {
1109 			if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
1110 				sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
1111 			else
1112 				sc->sc_hw.buttons = sc->sc_buttons;
1113 			sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
1114 			sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
1115 			sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
1116 			sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
1117 		}
1118 		ums_reset_buf(sc);
1119 		break;
1120 
1121 	case MOUSE_GETSTATUS:{
1122 			mousestatus_t *status = (mousestatus_t *)addr;
1123 
1124 			*status = sc->sc_status;
1125 			sc->sc_status.obutton = sc->sc_status.button;
1126 			sc->sc_status.button = 0;
1127 			sc->sc_status.dx = 0;
1128 			sc->sc_status.dy = 0;
1129 			sc->sc_status.dz = 0;
1130 			/* sc->sc_status.dt = 0; */
1131 
1132 			if (status->dx || status->dy || status->dz /* || status->dt */ ) {
1133 				status->flags |= MOUSE_POSCHANGED;
1134 			}
1135 			if (status->button != status->obutton) {
1136 				status->flags |= MOUSE_BUTTONSCHANGED;
1137 			}
1138 			break;
1139 		}
1140 	default:
1141 		error = ENOTTY;
1142 		break;
1143 	}
1144 
1145 	mtx_unlock(&sc->sc_mtx);
1146 	return (error);
1147 }
1148 
1149 static int
1150 ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS)
1151 {
1152 	struct ums_softc *sc = arg1;
1153 	struct ums_info *info;
1154 	struct sbuf *sb;
1155 	int i, j, err, had_output;
1156 
1157 	sb = sbuf_new_auto();
1158 	for (i = 0, had_output = 0; i < UMS_INFO_MAX; i++) {
1159 		info = &sc->sc_info[i];
1160 
1161 		/* Don't emit empty info */
1162 		if ((info->sc_flags &
1163 		    (UMS_FLAG_X_AXIS | UMS_FLAG_Y_AXIS | UMS_FLAG_Z_AXIS |
1164 		     UMS_FLAG_T_AXIS | UMS_FLAG_W_AXIS)) == 0 &&
1165 		    info->sc_buttons == 0)
1166 			continue;
1167 
1168 		if (had_output)
1169 			sbuf_printf(sb, "\n");
1170 		had_output = 1;
1171 		sbuf_printf(sb, "i%d:", i + 1);
1172 		if (info->sc_flags & UMS_FLAG_X_AXIS)
1173 			sbuf_printf(sb, " X:r%d, p%d, s%d;",
1174 			    (int)info->sc_iid_x,
1175 			    (int)info->sc_loc_x.pos,
1176 			    (int)info->sc_loc_x.size);
1177 		if (info->sc_flags & UMS_FLAG_Y_AXIS)
1178 			sbuf_printf(sb, " Y:r%d, p%d, s%d;",
1179 			    (int)info->sc_iid_y,
1180 			    (int)info->sc_loc_y.pos,
1181 			    (int)info->sc_loc_y.size);
1182 		if (info->sc_flags & UMS_FLAG_Z_AXIS)
1183 			sbuf_printf(sb, " Z:r%d, p%d, s%d;",
1184 			    (int)info->sc_iid_z,
1185 			    (int)info->sc_loc_z.pos,
1186 			    (int)info->sc_loc_z.size);
1187 		if (info->sc_flags & UMS_FLAG_T_AXIS)
1188 			sbuf_printf(sb, " T:r%d, p%d, s%d;",
1189 			    (int)info->sc_iid_t,
1190 			    (int)info->sc_loc_t.pos,
1191 			    (int)info->sc_loc_t.size);
1192 		if (info->sc_flags & UMS_FLAG_W_AXIS)
1193 			sbuf_printf(sb, " W:r%d, p%d, s%d;",
1194 			    (int)info->sc_iid_w,
1195 			    (int)info->sc_loc_w.pos,
1196 			    (int)info->sc_loc_w.size);
1197 
1198 		for (j = 0; j < info->sc_buttons; j++) {
1199 			sbuf_printf(sb, " B%d:r%d, p%d, s%d;", j + 1,
1200 			    (int)info->sc_iid_btn[j],
1201 			    (int)info->sc_loc_btn[j].pos,
1202 			    (int)info->sc_loc_btn[j].size);
1203 		}
1204 	}
1205 	sbuf_finish(sb);
1206 	err = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
1207 	sbuf_delete(sb);
1208 
1209 	return (err);
1210 }
1211 
1212 static devclass_t ums_devclass;
1213 
1214 static device_method_t ums_methods[] = {
1215 	DEVMETHOD(device_probe, ums_probe),
1216 	DEVMETHOD(device_attach, ums_attach),
1217 	DEVMETHOD(device_detach, ums_detach),
1218 
1219 	DEVMETHOD_END
1220 };
1221 
1222 static driver_t ums_driver = {
1223 	.name = "ums",
1224 	.methods = ums_methods,
1225 	.size = sizeof(struct ums_softc),
1226 };
1227 
1228 DRIVER_MODULE(ums, uhub, ums_driver, ums_devclass, NULL, 0);
1229 MODULE_DEPEND(ums, usb, 1, 1, 1);
1230 #ifdef EVDEV_SUPPORT
1231 MODULE_DEPEND(ums, evdev, 1, 1, 1);
1232 #endif
1233 MODULE_VERSION(ums, 1);
1234 USB_PNP_HOST_INFO(ums_devs);
1235