xref: /freebsd/sys/dev/usb/input/ums.c (revision ca48e43ba9ee73a07cdbad8365117793b01273bb)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 /*
34  * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
35  */
36 
37 #include "opt_evdev.h"
38 
39 #include <sys/stdint.h>
40 #include <sys/stddef.h>
41 #include <sys/param.h>
42 #include <sys/queue.h>
43 #include <sys/types.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/bus.h>
47 #include <sys/module.h>
48 #include <sys/lock.h>
49 #include <sys/mutex.h>
50 #include <sys/condvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/sx.h>
53 #include <sys/unistd.h>
54 #include <sys/callout.h>
55 #include <sys/malloc.h>
56 #include <sys/priv.h>
57 #include <sys/conf.h>
58 #include <sys/fcntl.h>
59 #include <sys/sbuf.h>
60 
61 #include <dev/hid/hid.h>
62 
63 #include <dev/usb/usb.h>
64 #include <dev/usb/usbdi.h>
65 #include <dev/usb/usbdi_util.h>
66 #include <dev/usb/usbhid.h>
67 #include "usbdevs.h"
68 
69 #define	USB_DEBUG_VAR ums_debug
70 #include <dev/usb/usb_debug.h>
71 
72 #include <dev/usb/quirk/usb_quirk.h>
73 
74 #ifdef EVDEV_SUPPORT
75 #include <dev/evdev/input.h>
76 #include <dev/evdev/evdev.h>
77 #endif
78 
79 #include <sys/ioccom.h>
80 #include <sys/filio.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 | CTLFLAG_MPSAFE, 0,
87     "USB ums");
88 SYSCTL_INT(_hw_usb_ums, OID_AUTO, debug, CTLFLAG_RWTUN,
89     &ums_debug, 0, "Debug level");
90 #endif
91 
92 #define	MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
93 #define	MOUSE_FLAGS (HIO_RELATIVE)
94 
95 #define	UMS_BUF_SIZE      8		/* bytes */
96 #define	UMS_IFQ_MAXLEN   50		/* units */
97 #define	UMS_BUTTON_MAX   31		/* exclusive, must be less than 32 */
98 #define	UMS_BUT(i) ((i) < 3 ? (((i) + 2) % 3) : (i))
99 #define	UMS_INFO_MAX	  2		/* maximum number of HID sets */
100 
101 enum {
102 	UMS_INTR_DT,
103 	UMS_N_TRANSFER,
104 };
105 
106 struct ums_info {
107 	struct hid_location sc_loc_w;
108 	struct hid_location sc_loc_x;
109 	struct hid_location sc_loc_y;
110 	struct hid_location sc_loc_z;
111 	struct hid_location sc_loc_t;
112 	struct hid_location sc_loc_btn[UMS_BUTTON_MAX];
113 
114 	uint32_t sc_flags;
115 #define	UMS_FLAG_X_AXIS     0x0001
116 #define	UMS_FLAG_Y_AXIS     0x0002
117 #define	UMS_FLAG_Z_AXIS     0x0004
118 #define	UMS_FLAG_T_AXIS     0x0008
119 #define	UMS_FLAG_SBU        0x0010	/* spurious button up events */
120 #define	UMS_FLAG_REVZ	    0x0020	/* Z-axis is reversed */
121 #define	UMS_FLAG_W_AXIS     0x0040
122 #define	UMS_FLAG_VBTN	    0x0080	/* Buttons in vendor usage page */
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
ums_put_queue_timeout(void * __sc)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
ums_intr_callback(struct usb_xfer * xfer,usb_error_t error)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 		/* keep old button value(s) for non-detected buttons */
323 		buttons |= sc->sc_status.button & ~buttons_found;
324 
325 #ifdef EVDEV_SUPPORT
326 		buttons_reported = buttons;
327 #endif
328 
329 		if (dx || dy || dz || dt || dw ||
330 		    (buttons != sc->sc_status.button)) {
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 				usb_callout_reset(&sc->sc_callout, hz / 20,
366 				    &ums_put_queue_timeout, sc);
367 			} else {
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 	case USB_ST_SETUP:
378 tr_setup:
379 		/* check if we can put more data into the FIFO */
380 		if (usb_fifo_put_bytes_max(sc->sc_fifo.fp[USB_FIFO_RX]) == 0) {
381 #ifdef EVDEV_SUPPORT
382 			if ((sc->sc_evflags & UMS_EVDEV_OPENED) == 0)
383 				break;
384 #else
385 			break;
386 #endif
387 		}
388 
389 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
390 		usbd_transfer_submit(xfer);
391 		break;
392 
393 	default:			/* Error */
394 		if (error != USB_ERR_CANCELLED) {
395 			/* try clear stall first */
396 			usbd_xfer_set_stall(xfer);
397 			goto tr_setup;
398 		}
399 		break;
400 	}
401 }
402 
403 static const struct usb_config ums_config[UMS_N_TRANSFER] = {
404 	[UMS_INTR_DT] = {
405 		.type = UE_INTERRUPT,
406 		.endpoint = UE_ADDR_ANY,
407 		.direction = UE_DIR_IN,
408 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
409 		.bufsize = 0,	/* use wMaxPacketSize */
410 		.callback = &ums_intr_callback,
411 	},
412 };
413 
414 /* A match on these entries will load ums */
415 static const STRUCT_USB_HOST_ID __used ums_devs[] = {
416 	{USB_IFACE_CLASS(UICLASS_HID),
417 	 USB_IFACE_SUBCLASS(UISUBCLASS_BOOT),
418 	 USB_IFACE_PROTOCOL(UIPROTO_MOUSE),},
419 };
420 
421 static int
ums_probe(device_t dev)422 ums_probe(device_t dev)
423 {
424 	struct usb_attach_arg *uaa = device_get_ivars(dev);
425 	void *d_ptr;
426 	int error;
427 	uint16_t d_len;
428 
429 	DPRINTFN(11, "\n");
430 
431 	if (uaa->usb_mode != USB_MODE_HOST)
432 		return (ENXIO);
433 
434 	if (uaa->info.bInterfaceClass != UICLASS_HID)
435 		return (ENXIO);
436 
437 	if (usb_test_quirk(uaa, UQ_UMS_IGNORE))
438 		return (ENXIO);
439 
440 	if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
441 	    (uaa->info.bInterfaceProtocol == UIPROTO_MOUSE))
442 		return (BUS_PROBE_DEFAULT);
443 
444 	error = usbd_req_get_hid_desc(uaa->device, NULL,
445 	    &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
446 
447 	if (error)
448 		return (ENXIO);
449 
450 	if (hid_is_mouse(d_ptr, d_len))
451 		error = BUS_PROBE_DEFAULT;
452 	else
453 		error = ENXIO;
454 
455 	free(d_ptr, M_TEMP);
456 	return (error);
457 }
458 
459 static void
ums_hid_parse(struct ums_softc * sc,device_t dev,const uint8_t * buf,uint16_t len,uint8_t index)460 ums_hid_parse(struct ums_softc *sc, device_t dev, const uint8_t *buf,
461     uint16_t len, uint8_t index)
462 {
463 	struct ums_info *info = &sc->sc_info[index];
464 	uint32_t flags;
465 	uint8_t i;
466 	uint8_t j;
467 
468 	if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
469 	    hid_input, index, &info->sc_loc_x, &flags, &info->sc_iid_x)) {
470 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
471 			info->sc_flags |= UMS_FLAG_X_AXIS;
472 		}
473 	}
474 	if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
475 	    hid_input, index, &info->sc_loc_y, &flags, &info->sc_iid_y)) {
476 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
477 			info->sc_flags |= UMS_FLAG_Y_AXIS;
478 		}
479 	}
480 	/* Try the wheel first as the Z activator since it's tradition. */
481 	if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
482 	    HUG_WHEEL), hid_input, index, &info->sc_loc_z, &flags,
483 	    &info->sc_iid_z) ||
484 	    hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
485 	    HUG_TWHEEL), hid_input, index, &info->sc_loc_z, &flags,
486 	    &info->sc_iid_z)) {
487 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
488 			info->sc_flags |= UMS_FLAG_Z_AXIS;
489 		}
490 		/*
491 		 * We might have both a wheel and Z direction, if so put
492 		 * put the Z on the W coordinate.
493 		 */
494 		if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
495 		    HUG_Z), hid_input, index, &info->sc_loc_w, &flags,
496 		    &info->sc_iid_w)) {
497 			if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
498 				info->sc_flags |= UMS_FLAG_W_AXIS;
499 			}
500 		}
501 	} else if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
502 	    HUG_Z), hid_input, index, &info->sc_loc_z, &flags,
503 	    &info->sc_iid_z)) {
504 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
505 			info->sc_flags |= UMS_FLAG_Z_AXIS;
506 		}
507 	}
508 	/*
509 	 * The Microsoft Wireless Intellimouse 2.0 reports it's wheel
510 	 * using 0x0048, which is HUG_TWHEEL, and seems to expect you
511 	 * to know that the byte after the wheel is the tilt axis.
512 	 * There are no other HID axis descriptors other than X,Y and
513 	 * TWHEEL
514 	 */
515 	if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
516 	    HUG_TWHEEL), hid_input, index, &info->sc_loc_t,
517 	    &flags, &info->sc_iid_t)) {
518 		info->sc_loc_t.pos += 8;
519 
520 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
521 			info->sc_flags |= UMS_FLAG_T_AXIS;
522 		}
523 	} else if (hid_locate(buf, len, HID_USAGE2(HUP_CONSUMER,
524 		HUC_AC_PAN), hid_input, index, &info->sc_loc_t,
525 		&flags, &info->sc_iid_t)) {
526 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS)
527 			info->sc_flags |= UMS_FLAG_T_AXIS;
528 	}
529 	/* figure out the number of buttons */
530 
531 	for (i = 0; i < UMS_BUTTON_MAX; i++) {
532 		if (!hid_locate(buf, len, HID_USAGE2(HUP_BUTTON, (i + 1)),
533 		    hid_input, index, &info->sc_loc_btn[i], NULL,
534 		    &info->sc_iid_btn[i])) {
535 			break;
536 		}
537 	}
538 
539 	/* detect other buttons */
540 	if (info->sc_flags & UMS_FLAG_VBTN) {
541 		for (j = 0; (i < UMS_BUTTON_MAX) && (j < 2); i++, j++) {
542 			if (!hid_locate(buf, len, HID_USAGE2(HUP_MICROSOFT,
543 			    (j + 1)), hid_input, index, &info->sc_loc_btn[i],
544 			    NULL, &info->sc_iid_btn[i])) {
545 				break;
546 			}
547 		}
548 	}
549 
550 	info->sc_buttons = i;
551 
552 	if (i > sc->sc_buttons)
553 		sc->sc_buttons = i;
554 
555 	if (info->sc_flags == 0)
556 		return;
557 
558 	/* announce information about the mouse */
559 	device_printf(dev, "%d buttons and [%s%s%s%s%s] coordinates ID=%u\n",
560 	    (info->sc_buttons),
561 	    (info->sc_flags & UMS_FLAG_X_AXIS) ? "X" : "",
562 	    (info->sc_flags & UMS_FLAG_Y_AXIS) ? "Y" : "",
563 	    (info->sc_flags & UMS_FLAG_Z_AXIS) ? "Z" : "",
564 	    (info->sc_flags & UMS_FLAG_T_AXIS) ? "T" : "",
565 	    (info->sc_flags & UMS_FLAG_W_AXIS) ? "W" : "",
566 	    info->sc_iid_x);
567 }
568 
569 static int
ums_attach(device_t dev)570 ums_attach(device_t dev)
571 {
572 	struct usb_attach_arg *uaa = device_get_ivars(dev);
573 	struct ums_softc *sc = device_get_softc(dev);
574 	struct ums_info *info;
575 	void *d_ptr = NULL;
576 	int isize;
577 	int err;
578 	uint16_t d_len;
579 	uint8_t i;
580 #ifdef USB_DEBUG
581 	uint8_t j;
582 #endif
583 
584 	DPRINTFN(11, "sc=%p\n", sc);
585 
586 	device_set_usb_desc(dev);
587 
588 	mtx_init(&sc->sc_mtx, "ums lock", NULL, MTX_DEF | MTX_RECURSE);
589 
590 	usb_callout_init_mtx(&sc->sc_callout, &sc->sc_mtx, 0);
591 
592 	/*
593          * Force the report (non-boot) protocol.
594          *
595          * Mice without boot protocol support may choose not to implement
596          * Set_Protocol at all; Ignore any error.
597          */
598 	err = usbd_req_set_protocol(uaa->device, NULL,
599 	    uaa->info.bIfaceIndex, 1);
600 
601 	err = usbd_transfer_setup(uaa->device,
602 	    &uaa->info.bIfaceIndex, sc->sc_xfer, ums_config,
603 	    UMS_N_TRANSFER, sc, &sc->sc_mtx);
604 
605 	if (err) {
606 		DPRINTF("error=%s\n", usbd_errstr(err));
607 		goto detach;
608 	}
609 
610 	/* Get HID descriptor */
611 	err = usbd_req_get_hid_desc(uaa->device, NULL, &d_ptr,
612 	    &d_len, M_TEMP, uaa->info.bIfaceIndex);
613 
614 	if (err) {
615 		device_printf(dev, "error reading report description\n");
616 		goto detach;
617 	}
618 
619 	isize = hid_report_size_max(d_ptr, d_len, hid_input, &sc->sc_iid);
620 
621 	if (usb_test_quirk(uaa, UQ_MS_VENDOR_BTN))
622 		for (i = 0; i < UMS_INFO_MAX; i++)
623 			sc->sc_info[i].sc_flags |= UMS_FLAG_VBTN;
624 
625 	/*
626 	 * The Microsoft Wireless Notebook Optical Mouse seems to be in worse
627 	 * shape than the Wireless Intellimouse 2.0, as its X, Y, wheel, and
628 	 * all of its other button positions are all off. It also reports that
629 	 * it has two additional buttons and a tilt wheel.
630 	 */
631 	if (usb_test_quirk(uaa, UQ_MS_BAD_CLASS)) {
632 		sc->sc_iid = 0;
633 
634 		info = &sc->sc_info[0];
635 		info->sc_flags = (UMS_FLAG_X_AXIS |
636 		    UMS_FLAG_Y_AXIS |
637 		    UMS_FLAG_Z_AXIS |
638 		    UMS_FLAG_SBU);
639 		info->sc_buttons = 3;
640 		isize = 5;
641 		/* 1st byte of descriptor report contains garbage */
642 		info->sc_loc_x.pos = 16;
643 		info->sc_loc_x.size = 8;
644 		info->sc_loc_y.pos = 24;
645 		info->sc_loc_y.size = 8;
646 		info->sc_loc_z.pos = 32;
647 		info->sc_loc_z.size = 8;
648 		info->sc_loc_btn[0].pos = 8;
649 		info->sc_loc_btn[0].size = 1;
650 		info->sc_loc_btn[1].pos = 9;
651 		info->sc_loc_btn[1].size = 1;
652 		info->sc_loc_btn[2].pos = 10;
653 		info->sc_loc_btn[2].size = 1;
654 
655 		/* Announce device */
656 		device_printf(dev, "3 buttons and [XYZ] "
657 		    "coordinates ID=0\n");
658 
659 	} else {
660 		/* Search the HID descriptor and announce device */
661 		for (i = 0; i < UMS_INFO_MAX; i++) {
662 			ums_hid_parse(sc, dev, d_ptr, d_len, i);
663 		}
664 	}
665 
666 	if (usb_test_quirk(uaa, UQ_MS_REVZ)) {
667 		info = &sc->sc_info[0];
668 		/* Some wheels need the Z axis reversed. */
669 		info->sc_flags |= UMS_FLAG_REVZ;
670 	}
671 	if (isize > (int)usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT])) {
672 		DPRINTF("WARNING: report size, %d bytes, is larger "
673 		    "than interrupt size, %d bytes!\n", isize,
674 		    usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT]));
675 	}
676 	free(d_ptr, M_TEMP);
677 	d_ptr = NULL;
678 
679 #ifdef USB_DEBUG
680 	for (j = 0; j < UMS_INFO_MAX; j++) {
681 		info = &sc->sc_info[j];
682 
683 		DPRINTF("sc=%p, index=%d\n", sc, j);
684 		DPRINTF("X\t%d/%d id=%d\n", info->sc_loc_x.pos,
685 		    info->sc_loc_x.size, info->sc_iid_x);
686 		DPRINTF("Y\t%d/%d id=%d\n", info->sc_loc_y.pos,
687 		    info->sc_loc_y.size, info->sc_iid_y);
688 		DPRINTF("Z\t%d/%d id=%d\n", info->sc_loc_z.pos,
689 		    info->sc_loc_z.size, info->sc_iid_z);
690 		DPRINTF("T\t%d/%d id=%d\n", info->sc_loc_t.pos,
691 		    info->sc_loc_t.size, info->sc_iid_t);
692 		DPRINTF("W\t%d/%d id=%d\n", info->sc_loc_w.pos,
693 		    info->sc_loc_w.size, info->sc_iid_w);
694 
695 		for (i = 0; i < info->sc_buttons; i++) {
696 			DPRINTF("B%d\t%d/%d id=%d\n",
697 			    i + 1, info->sc_loc_btn[i].pos,
698 			    info->sc_loc_btn[i].size, info->sc_iid_btn[i]);
699 		}
700 	}
701 	DPRINTF("size=%d, id=%d\n", isize, sc->sc_iid);
702 #endif
703 
704 	err = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
705 	    &ums_fifo_methods, &sc->sc_fifo,
706 	    device_get_unit(dev), -1, uaa->info.bIfaceIndex,
707   	    UID_ROOT, GID_OPERATOR, 0644);
708 	if (err)
709 		goto detach;
710 
711 #ifdef EVDEV_SUPPORT
712 	sc->sc_evdev = evdev_alloc();
713 	evdev_set_name(sc->sc_evdev, device_get_desc(dev));
714 	evdev_set_phys(sc->sc_evdev, device_get_nameunit(dev));
715 	evdev_set_id(sc->sc_evdev, BUS_USB, uaa->info.idVendor,
716 	    uaa->info.idProduct, 0);
717 	evdev_set_serial(sc->sc_evdev, usb_get_serial(uaa->device));
718 	evdev_set_methods(sc->sc_evdev, sc, &ums_evdev_methods);
719 	evdev_support_prop(sc->sc_evdev, INPUT_PROP_POINTER);
720 	evdev_support_event(sc->sc_evdev, EV_SYN);
721 	evdev_support_event(sc->sc_evdev, EV_REL);
722 	evdev_support_event(sc->sc_evdev, EV_KEY);
723 
724 	info = &sc->sc_info[0];
725 
726 	if (info->sc_flags & UMS_FLAG_X_AXIS)
727 		evdev_support_rel(sc->sc_evdev, REL_X);
728 
729 	if (info->sc_flags & UMS_FLAG_Y_AXIS)
730 		evdev_support_rel(sc->sc_evdev, REL_Y);
731 
732 	if (info->sc_flags & UMS_FLAG_Z_AXIS)
733 		evdev_support_rel(sc->sc_evdev, REL_WHEEL);
734 
735 	if (info->sc_flags & UMS_FLAG_T_AXIS)
736 		evdev_support_rel(sc->sc_evdev, REL_HWHEEL);
737 
738 	for (i = 0; i < info->sc_buttons; i++)
739 		evdev_support_key(sc->sc_evdev, BTN_MOUSE + i);
740 
741 	err = evdev_register_mtx(sc->sc_evdev, &sc->sc_mtx);
742 	if (err)
743 		goto detach;
744 #endif
745 
746 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
747 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
748 	    OID_AUTO, "parseinfo",
749 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
750 	    sc, 0, ums_sysctl_handler_parseinfo, "",
751 	    "Dump of parsed HID report descriptor");
752 
753 	return (0);
754 
755 detach:
756 	if (d_ptr) {
757 		free(d_ptr, M_TEMP);
758 	}
759 	ums_detach(dev);
760 	return (ENOMEM);
761 }
762 
763 static int
ums_detach(device_t self)764 ums_detach(device_t self)
765 {
766 	struct ums_softc *sc = device_get_softc(self);
767 
768 	DPRINTF("sc=%p\n", sc);
769 
770 	usb_fifo_detach(&sc->sc_fifo);
771 
772 #ifdef EVDEV_SUPPORT
773 	evdev_free(sc->sc_evdev);
774 #endif
775 
776 	usbd_transfer_unsetup(sc->sc_xfer, UMS_N_TRANSFER);
777 
778 	usb_callout_drain(&sc->sc_callout);
779 
780 	mtx_destroy(&sc->sc_mtx);
781 
782 	return (0);
783 }
784 
785 static void
ums_reset(struct ums_softc * sc)786 ums_reset(struct ums_softc *sc)
787 {
788 
789 	/* reset all USB mouse parameters */
790 
791 	if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
792 		sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
793 	else
794 		sc->sc_hw.buttons = sc->sc_buttons;
795 
796 	sc->sc_hw.iftype = MOUSE_IF_USB;
797 	sc->sc_hw.type = MOUSE_MOUSE;
798 	sc->sc_hw.model = MOUSE_MODEL_GENERIC;
799 	sc->sc_hw.hwid = 0;
800 
801 	sc->sc_mode.protocol = MOUSE_PROTO_MSC;
802 	sc->sc_mode.rate = -1;
803 	sc->sc_mode.resolution = MOUSE_RES_UNKNOWN;
804 	sc->sc_mode.accelfactor = 0;
805 	sc->sc_mode.level = 0;
806 	sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
807 	sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
808 	sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
809 
810 	/* reset status */
811 
812 	sc->sc_status.flags = 0;
813 	sc->sc_status.button = 0;
814 	sc->sc_status.obutton = 0;
815 	sc->sc_status.dx = 0;
816 	sc->sc_status.dy = 0;
817 	sc->sc_status.dz = 0;
818 	/* sc->sc_status.dt = 0; */
819 }
820 
821 static void
ums_start_rx(struct ums_softc * sc)822 ums_start_rx(struct ums_softc *sc)
823 {
824 	int rate;
825 
826 	/* Check if we should override the default polling interval */
827 	rate = sc->sc_pollrate;
828 	/* Range check rate */
829 	if (rate > 1000)
830 		rate = 1000;
831 	/* Check for set rate */
832 	if ((rate > 0) && (sc->sc_xfer[UMS_INTR_DT] != NULL)) {
833 		DPRINTF("Setting pollrate = %d\n", rate);
834 		/* Stop current transfer, if any */
835 		usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
836 		/* Set new interval */
837 		usbd_xfer_set_interval(sc->sc_xfer[UMS_INTR_DT], 1000 / rate);
838 		/* Only set pollrate once */
839 		sc->sc_pollrate = 0;
840 	}
841 
842 	usbd_transfer_start(sc->sc_xfer[UMS_INTR_DT]);
843 }
844 
845 static void
ums_stop_rx(struct ums_softc * sc)846 ums_stop_rx(struct ums_softc *sc)
847 {
848 	usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
849 	usb_callout_stop(&sc->sc_callout);
850 }
851 
852 static void
ums_fifo_start_read(struct usb_fifo * fifo)853 ums_fifo_start_read(struct usb_fifo *fifo)
854 {
855 	struct ums_softc *sc = usb_fifo_softc(fifo);
856 
857 	ums_start_rx(sc);
858 }
859 
860 static void
ums_fifo_stop_read(struct usb_fifo * fifo)861 ums_fifo_stop_read(struct usb_fifo *fifo)
862 {
863 	struct ums_softc *sc = usb_fifo_softc(fifo);
864 
865 #ifdef EVDEV_SUPPORT
866 	if ((sc->sc_evflags & UMS_EVDEV_OPENED) == 0)
867 #endif
868 		ums_stop_rx(sc);
869 }
870 
871 #if ((MOUSE_SYS_PACKETSIZE != 8) || \
872      (MOUSE_MSC_PACKETSIZE != 5))
873 #error "Software assumptions are not met. Please update code."
874 #endif
875 
876 static void
ums_put_queue(struct ums_softc * sc,int32_t dx,int32_t dy,int32_t dz,int32_t dt,int32_t buttons)877 ums_put_queue(struct ums_softc *sc, int32_t dx, int32_t dy,
878     int32_t dz, int32_t dt, int32_t buttons)
879 {
880 	uint8_t buf[8];
881 
882 #ifdef EVDEV_SUPPORT
883 	if (evdev_is_grabbed(sc->sc_evdev))
884 		return;
885 #endif
886 	if (1) {
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
ums_evdev_push(struct ums_softc * sc,int32_t dx,int32_t dy,int32_t dz,int32_t dt,int32_t buttons)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
ums_reset_buf(struct ums_softc * sc)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
ums_ev_open(struct evdev_dev * evdev)953 ums_ev_open(struct evdev_dev *evdev)
954 {
955 	struct ums_softc *sc = evdev_get_softc(evdev);
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 	}
964 	ums_start_rx(sc);
965 
966 	return (0);
967 }
968 
969 static int
ums_ev_close(struct evdev_dev * evdev)970 ums_ev_close(struct evdev_dev *evdev)
971 {
972 	struct ums_softc *sc = evdev_get_softc(evdev);
973 
974 	mtx_assert(&sc->sc_mtx, MA_OWNED);
975 
976 	sc->sc_evflags &= ~UMS_EVDEV_OPENED;
977 
978 	if (sc->sc_fflags == 0)
979 		ums_stop_rx(sc);
980 
981 	return (0);
982 }
983 #endif
984 
985 static int
ums_fifo_open(struct usb_fifo * fifo,int fflags)986 ums_fifo_open(struct usb_fifo *fifo, int fflags)
987 {
988 	struct ums_softc *sc = usb_fifo_softc(fifo);
989 
990 	DPRINTFN(2, "\n");
991 
992 	/* check for duplicate open, should not happen */
993 	if (sc->sc_fflags & fflags)
994 		return (EBUSY);
995 
996 	/* check for first open */
997 #ifdef EVDEV_SUPPORT
998 	if (sc->sc_fflags == 0 && (sc->sc_evflags & UMS_EVDEV_OPENED) == 0)
999 		ums_reset(sc);
1000 #else
1001 	if (sc->sc_fflags == 0)
1002 		ums_reset(sc);
1003 #endif
1004 
1005 	if (fflags & FREAD) {
1006 		/* allocate RX buffer */
1007 		if (usb_fifo_alloc_buffer(fifo,
1008 		    UMS_BUF_SIZE, UMS_IFQ_MAXLEN)) {
1009 			return (ENOMEM);
1010 		}
1011 	}
1012 
1013 	sc->sc_fflags |= fflags & (FREAD | FWRITE);
1014 	return (0);
1015 }
1016 
1017 static void
ums_fifo_close(struct usb_fifo * fifo,int fflags)1018 ums_fifo_close(struct usb_fifo *fifo, int fflags)
1019 {
1020 	struct ums_softc *sc = usb_fifo_softc(fifo);
1021 
1022 	DPRINTFN(2, "\n");
1023 
1024 	if (fflags & FREAD)
1025 		usb_fifo_free_buffer(fifo);
1026 
1027 	sc->sc_fflags &= ~(fflags & (FREAD | FWRITE));
1028 }
1029 
1030 static int
ums_fifo_ioctl(struct usb_fifo * fifo,u_long cmd,void * addr,int fflags)1031 ums_fifo_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
1032 {
1033 	struct ums_softc *sc = usb_fifo_softc(fifo);
1034 	mousemode_t mode;
1035 	int error = 0;
1036 
1037 	DPRINTFN(2, "\n");
1038 
1039 	mtx_lock(&sc->sc_mtx);
1040 
1041 	switch (cmd) {
1042 	case MOUSE_GETHWINFO:
1043 		*(mousehw_t *)addr = sc->sc_hw;
1044 		break;
1045 
1046 	case MOUSE_GETMODE:
1047 		*(mousemode_t *)addr = sc->sc_mode;
1048 		break;
1049 
1050 	case MOUSE_SETMODE:
1051 		mode = *(mousemode_t *)addr;
1052 
1053 		if (mode.level == -1) {
1054 			/* don't change the current setting */
1055 		} else if ((mode.level < 0) || (mode.level > 1)) {
1056 			error = EINVAL;
1057 			break;
1058 		} else {
1059 			sc->sc_mode.level = mode.level;
1060 		}
1061 
1062 		/* store polling rate */
1063 		sc->sc_pollrate = mode.rate;
1064 
1065 		if (sc->sc_mode.level == 0) {
1066 			if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
1067 				sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
1068 			else
1069 				sc->sc_hw.buttons = sc->sc_buttons;
1070 			sc->sc_mode.protocol = MOUSE_PROTO_MSC;
1071 			sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
1072 			sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
1073 			sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
1074 		} else if (sc->sc_mode.level == 1) {
1075 			if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
1076 				sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
1077 			else
1078 				sc->sc_hw.buttons = sc->sc_buttons;
1079 			sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
1080 			sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
1081 			sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
1082 			sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
1083 		}
1084 		ums_reset_buf(sc);
1085 		break;
1086 
1087 	case MOUSE_GETLEVEL:
1088 		*(int *)addr = sc->sc_mode.level;
1089 		break;
1090 
1091 	case MOUSE_SETLEVEL:
1092 		if (*(int *)addr < 0 || *(int *)addr > 1) {
1093 			error = EINVAL;
1094 			break;
1095 		}
1096 		sc->sc_mode.level = *(int *)addr;
1097 
1098 		if (sc->sc_mode.level == 0) {
1099 			if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
1100 				sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
1101 			else
1102 				sc->sc_hw.buttons = sc->sc_buttons;
1103 			sc->sc_mode.protocol = MOUSE_PROTO_MSC;
1104 			sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
1105 			sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
1106 			sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
1107 		} else if (sc->sc_mode.level == 1) {
1108 			if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
1109 				sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
1110 			else
1111 				sc->sc_hw.buttons = sc->sc_buttons;
1112 			sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
1113 			sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
1114 			sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
1115 			sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
1116 		}
1117 		ums_reset_buf(sc);
1118 		break;
1119 
1120 	case MOUSE_GETSTATUS:{
1121 			mousestatus_t *status = (mousestatus_t *)addr;
1122 
1123 			*status = sc->sc_status;
1124 			sc->sc_status.obutton = sc->sc_status.button;
1125 			sc->sc_status.button = 0;
1126 			sc->sc_status.dx = 0;
1127 			sc->sc_status.dy = 0;
1128 			sc->sc_status.dz = 0;
1129 			/* sc->sc_status.dt = 0; */
1130 
1131 			if (status->dx || status->dy || status->dz /* || status->dt */ ) {
1132 				status->flags |= MOUSE_POSCHANGED;
1133 			}
1134 			if (status->button != status->obutton) {
1135 				status->flags |= MOUSE_BUTTONSCHANGED;
1136 			}
1137 			break;
1138 		}
1139 	default:
1140 		error = ENOTTY;
1141 		break;
1142 	}
1143 
1144 	mtx_unlock(&sc->sc_mtx);
1145 	return (error);
1146 }
1147 
1148 static int
ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS)1149 ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS)
1150 {
1151 	struct ums_softc *sc = arg1;
1152 	struct ums_info *info;
1153 	struct sbuf *sb;
1154 	int i, j, err, had_output;
1155 
1156 	sb = sbuf_new_auto();
1157 	for (i = 0, had_output = 0; i < UMS_INFO_MAX; i++) {
1158 		info = &sc->sc_info[i];
1159 
1160 		/* Don't emit empty info */
1161 		if ((info->sc_flags &
1162 		    (UMS_FLAG_X_AXIS | UMS_FLAG_Y_AXIS | UMS_FLAG_Z_AXIS |
1163 		     UMS_FLAG_T_AXIS | UMS_FLAG_W_AXIS)) == 0 &&
1164 		    info->sc_buttons == 0)
1165 			continue;
1166 
1167 		if (had_output)
1168 			sbuf_printf(sb, "\n");
1169 		had_output = 1;
1170 		sbuf_printf(sb, "i%d:", i + 1);
1171 		if (info->sc_flags & UMS_FLAG_X_AXIS)
1172 			sbuf_printf(sb, " X:r%d, p%d, s%d;",
1173 			    (int)info->sc_iid_x,
1174 			    (int)info->sc_loc_x.pos,
1175 			    (int)info->sc_loc_x.size);
1176 		if (info->sc_flags & UMS_FLAG_Y_AXIS)
1177 			sbuf_printf(sb, " Y:r%d, p%d, s%d;",
1178 			    (int)info->sc_iid_y,
1179 			    (int)info->sc_loc_y.pos,
1180 			    (int)info->sc_loc_y.size);
1181 		if (info->sc_flags & UMS_FLAG_Z_AXIS)
1182 			sbuf_printf(sb, " Z:r%d, p%d, s%d;",
1183 			    (int)info->sc_iid_z,
1184 			    (int)info->sc_loc_z.pos,
1185 			    (int)info->sc_loc_z.size);
1186 		if (info->sc_flags & UMS_FLAG_T_AXIS)
1187 			sbuf_printf(sb, " T:r%d, p%d, s%d;",
1188 			    (int)info->sc_iid_t,
1189 			    (int)info->sc_loc_t.pos,
1190 			    (int)info->sc_loc_t.size);
1191 		if (info->sc_flags & UMS_FLAG_W_AXIS)
1192 			sbuf_printf(sb, " W:r%d, p%d, s%d;",
1193 			    (int)info->sc_iid_w,
1194 			    (int)info->sc_loc_w.pos,
1195 			    (int)info->sc_loc_w.size);
1196 
1197 		for (j = 0; j < info->sc_buttons; j++) {
1198 			sbuf_printf(sb, " B%d:r%d, p%d, s%d;", j + 1,
1199 			    (int)info->sc_iid_btn[j],
1200 			    (int)info->sc_loc_btn[j].pos,
1201 			    (int)info->sc_loc_btn[j].size);
1202 		}
1203 	}
1204 	sbuf_finish(sb);
1205 	err = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
1206 	sbuf_delete(sb);
1207 
1208 	return (err);
1209 }
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, NULL, NULL);
1226 MODULE_DEPEND(ums, usb, 1, 1, 1);
1227 MODULE_DEPEND(ums, hid, 1, 1, 1);
1228 #ifdef EVDEV_SUPPORT
1229 MODULE_DEPEND(ums, evdev, 1, 1, 1);
1230 #endif
1231 MODULE_VERSION(ums, 1);
1232 USB_PNP_HOST_INFO(ums_devs);
1233