xref: /dragonfly/sys/bus/u4b/input/ums.c (revision cfd1aba3)
1 /*-
2  * Copyright (c) 1998 The NetBSD Foundation, Inc.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to The NetBSD Foundation
6  * by Lennart Augustsson (lennart@augustsson.net) at
7  * Carlstedt Research & Technology.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /*
32  * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
33  */
34 
35 #include <sys/stdint.h>
36 #include <sys/param.h>
37 #include <sys/queue.h>
38 #include <sys/types.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/bus.h>
42 #include <sys/module.h>
43 #include <sys/lock.h>
44 #include <sys/condvar.h>
45 #include <sys/sysctl.h>
46 #include <sys/unistd.h>
47 #include <sys/callout.h>
48 #include <sys/malloc.h>
49 #include <sys/priv.h>
50 #include <sys/conf.h>
51 #include <sys/fcntl.h>
52 #include <sys/sbuf.h>
53 
54 #include <bus/u4b/usb.h>
55 #include <bus/u4b/usbdi.h>
56 #include <bus/u4b/usbdi_util.h>
57 #include <bus/u4b/usbhid.h>
58 #include "usbdevs.h"
59 
60 #define	USB_DEBUG_VAR ums_debug
61 #include <bus/u4b/usb_debug.h>
62 
63 #include <bus/u4b/quirk/usb_quirk.h>
64 
65 #include <sys/ioccom.h>
66 #include <sys/filio.h>
67 #include <sys/tty.h>
68 #include <sys/mouse.h>
69 
70 #ifdef USB_DEBUG
71 static int ums_debug = 0;
72 
73 static SYSCTL_NODE(_hw_usb, OID_AUTO, ums, CTLFLAG_RW, 0, "USB ums");
74 SYSCTL_INT(_hw_usb_ums, OID_AUTO, debug, CTLFLAG_RW,
75     &ums_debug, 0, "Debug level");
76 #endif
77 
78 #define	MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
79 #define	MOUSE_FLAGS (HIO_RELATIVE)
80 
81 #define	UMS_BUF_SIZE      8		/* bytes */
82 #define	UMS_IFQ_MAXLEN   50		/* units */
83 #define	UMS_BUTTON_MAX   31		/* exclusive, must be less than 32 */
84 #define	UMS_BUT(i) ((i) < 3 ? (((i) + 2) % 3) : (i))
85 #define	UMS_INFO_MAX	  2		/* maximum number of HID sets */
86 
87 enum {
88 	UMS_INTR_DT,
89 	UMS_N_TRANSFER,
90 };
91 
92 struct ums_info {
93 	struct hid_location sc_loc_w;
94 	struct hid_location sc_loc_x;
95 	struct hid_location sc_loc_y;
96 	struct hid_location sc_loc_z;
97 	struct hid_location sc_loc_t;
98 	struct hid_location sc_loc_btn[UMS_BUTTON_MAX];
99 
100 	uint32_t sc_flags;
101 #define	UMS_FLAG_X_AXIS     0x0001
102 #define	UMS_FLAG_Y_AXIS     0x0002
103 #define	UMS_FLAG_Z_AXIS     0x0004
104 #define	UMS_FLAG_T_AXIS     0x0008
105 #define	UMS_FLAG_SBU        0x0010	/* spurious button up events */
106 #define	UMS_FLAG_REVZ	    0x0020	/* Z-axis is reversed */
107 #define	UMS_FLAG_W_AXIS     0x0040
108 
109 	uint8_t	sc_iid_w;
110 	uint8_t	sc_iid_x;
111 	uint8_t	sc_iid_y;
112 	uint8_t	sc_iid_z;
113 	uint8_t	sc_iid_t;
114 	uint8_t	sc_iid_btn[UMS_BUTTON_MAX];
115 	uint8_t	sc_buttons;
116 };
117 
118 struct ums_softc {
119 	struct usb_fifo_sc sc_fifo;
120 	struct lock sc_lock;
121 	struct usb_callout sc_callout;
122 	struct ums_info sc_info[UMS_INFO_MAX];
123 
124 	mousehw_t sc_hw;
125 	mousemode_t sc_mode;
126 	mousestatus_t sc_status;
127 
128 	struct usb_xfer *sc_xfer[UMS_N_TRANSFER];
129 
130 	int sc_pollrate;
131 	int sc_fflags;
132 	int sc_read_running;
133 
134 	uint8_t	sc_buttons;
135 	uint8_t	sc_iid;
136 	uint8_t	sc_temp[64];
137 };
138 
139 static void ums_put_queue_timeout(void *__sc);
140 
141 static usb_callback_t ums_intr_callback;
142 
143 static device_probe_t ums_probe;
144 static device_attach_t ums_attach;
145 static device_detach_t ums_detach;
146 
147 static usb_fifo_cmd_t ums_start_read;
148 static usb_fifo_cmd_t ums_stop_read;
149 static usb_fifo_open_t ums_open;
150 static usb_fifo_close_t ums_close;
151 static usb_fifo_ioctl_t ums_ioctl;
152 
153 static void	ums_put_queue(struct ums_softc *, int32_t, int32_t,
154 		    int32_t, int32_t, int32_t);
155 #if 0 /* XXX */
156 static int	ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS);
157 #endif
158 
159 static struct usb_fifo_methods ums_fifo_methods = {
160 	.f_open = &ums_open,
161 	.f_close = &ums_close,
162 	.f_ioctl = &ums_ioctl,
163 	.f_start_read = &ums_start_read,
164 	.f_stop_read = &ums_stop_read,
165 	.basename[0] = "ums",
166 };
167 
168 static void
169 ums_put_queue_timeout(void *__sc)
170 {
171 	struct ums_softc *sc = __sc;
172 
173 	KKASSERT(lockowned(&sc->sc_lock));
174 
175 	ums_put_queue(sc, 0, 0, 0, 0, 0);
176 }
177 
178 static void
179 ums_intr_callback(struct usb_xfer *xfer, usb_error_t error)
180 {
181 	struct ums_softc *sc = usbd_xfer_softc(xfer);
182 	struct ums_info *info = &sc->sc_info[0];
183 	struct usb_page_cache *pc;
184 	uint8_t *buf = sc->sc_temp;
185 	int32_t buttons = 0;
186 	int32_t buttons_found = 0;
187 	int32_t dw = 0;
188 	int32_t dx = 0;
189 	int32_t dy = 0;
190 	int32_t dz = 0;
191 	int32_t dt = 0;
192 	uint8_t i;
193 	uint8_t id;
194 	int len;
195 
196 	usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
197 
198 	switch (USB_GET_STATE(xfer)) {
199 	case USB_ST_TRANSFERRED:
200 		DPRINTFN(6, "sc=%p actlen=%d\n", sc, len);
201 
202 		if (len > (int)sizeof(sc->sc_temp)) {
203 			DPRINTFN(6, "truncating large packet to %zu bytes\n",
204 			    sizeof(sc->sc_temp));
205 			len = sizeof(sc->sc_temp);
206 		}
207 		if (len == 0)
208 			goto tr_setup;
209 
210 		pc = usbd_xfer_get_frame(xfer, 0);
211 		usbd_copy_out(pc, 0, buf, len);
212 
213 		DPRINTFN(6, "data = %02x %02x %02x %02x "
214 		    "%02x %02x %02x %02x\n",
215 		    (len > 0) ? buf[0] : 0, (len > 1) ? buf[1] : 0,
216 		    (len > 2) ? buf[2] : 0, (len > 3) ? buf[3] : 0,
217 		    (len > 4) ? buf[4] : 0, (len > 5) ? buf[5] : 0,
218 		    (len > 6) ? buf[6] : 0, (len > 7) ? buf[7] : 0);
219 
220 		if (sc->sc_iid) {
221 			id = *buf;
222 
223 			len--;
224 			buf++;
225 
226 		} else {
227 			id = 0;
228 			if (sc->sc_info[0].sc_flags & UMS_FLAG_SBU) {
229 				if ((*buf == 0x14) || (*buf == 0x15)) {
230 					goto tr_setup;
231 				}
232 			}
233 		}
234 
235 	repeat:
236 		if ((info->sc_flags & UMS_FLAG_W_AXIS) &&
237 		    (id == info->sc_iid_w))
238 			dw += hid_get_data(buf, len, &info->sc_loc_w);
239 
240 		if ((info->sc_flags & UMS_FLAG_X_AXIS) &&
241 		    (id == info->sc_iid_x))
242 			dx += hid_get_data(buf, len, &info->sc_loc_x);
243 
244 		if ((info->sc_flags & UMS_FLAG_Y_AXIS) &&
245 		    (id == info->sc_iid_y))
246 			dy = -hid_get_data(buf, len, &info->sc_loc_y);
247 
248 		if ((info->sc_flags & UMS_FLAG_Z_AXIS) &&
249 		    (id == info->sc_iid_z)) {
250 			int32_t temp;
251 			temp = hid_get_data(buf, len, &info->sc_loc_z);
252 			if (info->sc_flags & UMS_FLAG_REVZ)
253 				temp = -temp;
254 			dz -= temp;
255 		}
256 
257 		if ((info->sc_flags & UMS_FLAG_T_AXIS) &&
258 		    (id == info->sc_iid_t))
259 			dt -= hid_get_data(buf, len, &info->sc_loc_t);
260 
261 		for (i = 0; i < info->sc_buttons; i++) {
262 			uint32_t mask;
263 			mask = 1UL << UMS_BUT(i);
264 			/* check for correct button ID */
265 			if (id != info->sc_iid_btn[i])
266 				continue;
267 			/* check for button pressed */
268 			if (hid_get_data(buf, len, &info->sc_loc_btn[i]))
269 				buttons |= mask;
270 			/* register button mask */
271 			buttons_found |= mask;
272 		}
273 
274 		if (++info != &sc->sc_info[UMS_INFO_MAX])
275 			goto repeat;
276 
277 		/* keep old button value(s) for non-detected buttons */
278 		buttons |= sc->sc_status.button & ~buttons_found;
279 
280 		if (dx || dy || dz || dt || dw ||
281 		    (buttons != sc->sc_status.button)) {
282 			DPRINTFN(6, "x:%d y:%d z:%d t:%d w:%d buttons:0x%08x\n",
283 			    dx, dy, dz, dt, dw, buttons);
284 
285 			/* translate T-axis into button presses until further */
286 			if (dt > 0)
287 				buttons |= 1UL << 3;
288 			else if (dt < 0)
289 				buttons |= 1UL << 4;
290 
291 			sc->sc_status.button = buttons;
292 			sc->sc_status.dx += dx;
293 			sc->sc_status.dy += dy;
294 			sc->sc_status.dz += dz;
295 			/*
296 			 * sc->sc_status.dt += dt;
297 			 * no way to export this yet
298 			 */
299 
300 			/*
301 		         * The Qtronix keyboard has a built in PS/2
302 		         * port for a mouse.  The firmware once in a
303 		         * while posts a spurious button up
304 		         * event. This event we ignore by doing a
305 		         * timeout for 50 msecs.  If we receive
306 		         * dx=dy=dz=buttons=0 before we add the event
307 		         * to the queue.  In any other case we delete
308 		         * the timeout event.
309 		         */
310 			if ((sc->sc_info[0].sc_flags & UMS_FLAG_SBU) &&
311 			    (dx == 0) && (dy == 0) && (dz == 0) && (dt == 0) &&
312 			    (dw == 0) && (buttons == 0)) {
313 
314 				usb_callout_reset(&sc->sc_callout, hz / 20,
315 				    &ums_put_queue_timeout, sc);
316 			} else {
317 
318 				usb_callout_stop(&sc->sc_callout);
319 
320 				ums_put_queue(sc, dx, dy, dz, dt, buttons);
321 			}
322 		}
323 	case USB_ST_SETUP:
324 tr_setup:
325 		/* check if we can put more data into the FIFO */
326 		if (usb_fifo_put_bytes_max(
327 		    sc->sc_fifo.fp[USB_FIFO_RX]) != 0) {
328 			usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
329 			usbd_transfer_submit(xfer);
330 		}
331 		break;
332 
333 	default:			/* Error */
334 		if (error != USB_ERR_CANCELLED) {
335 			/* try clear stall first */
336 			usbd_xfer_set_stall(xfer);
337 			goto tr_setup;
338 		}
339 		break;
340 	}
341 }
342 
343 static const struct usb_config ums_config[UMS_N_TRANSFER] = {
344 
345 	[UMS_INTR_DT] = {
346 		.type = UE_INTERRUPT,
347 		.endpoint = UE_ADDR_ANY,
348 		.direction = UE_DIR_IN,
349 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
350 		.bufsize = 0,	/* use wMaxPacketSize */
351 		.callback = &ums_intr_callback,
352 	},
353 };
354 
355 /* A match on these entries will load ums */
356 static const STRUCT_USB_HOST_ID __used ums_devs[] = {
357 	{USB_IFACE_CLASS(UICLASS_HID),
358 	 USB_IFACE_SUBCLASS(UISUBCLASS_BOOT),
359 	 USB_IFACE_PROTOCOL(UIPROTO_MOUSE),},
360 };
361 
362 static int
363 ums_probe(device_t dev)
364 {
365 	struct usb_attach_arg *uaa = device_get_ivars(dev);
366 	void *d_ptr;
367 	int error;
368 	uint16_t d_len;
369 
370 	DPRINTFN(11, "\n");
371 
372 	if (uaa->usb_mode != USB_MODE_HOST)
373 		return (ENXIO);
374 
375 	if (uaa->info.bInterfaceClass != UICLASS_HID)
376 		return (ENXIO);
377 
378 	if (usb_test_quirk(uaa, UQ_UMS_IGNORE))
379 		return (ENXIO);
380 
381 	if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
382 	    (uaa->info.bInterfaceProtocol == UIPROTO_MOUSE))
383 		return (BUS_PROBE_DEFAULT);
384 
385 	error = usbd_req_get_hid_desc(uaa->device, NULL,
386 	    &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
387 
388 	if (error)
389 		return (ENXIO);
390 
391 	if (hid_is_mouse(d_ptr, d_len))
392 		error = BUS_PROBE_DEFAULT;
393 	else
394 		error = ENXIO;
395 
396 	kfree(d_ptr, M_TEMP);
397 	return (error);
398 }
399 
400 static void
401 ums_hid_parse(struct ums_softc *sc, device_t dev, const uint8_t *buf,
402     uint16_t len, uint8_t index)
403 {
404 	struct ums_info *info = &sc->sc_info[index];
405 	uint32_t flags;
406 	uint8_t i;
407 	uint8_t j;
408 
409 	if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
410 	    hid_input, index, &info->sc_loc_x, &flags, &info->sc_iid_x)) {
411 
412 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
413 			info->sc_flags |= UMS_FLAG_X_AXIS;
414 		}
415 	}
416 	if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
417 	    hid_input, index, &info->sc_loc_y, &flags, &info->sc_iid_y)) {
418 
419 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
420 			info->sc_flags |= UMS_FLAG_Y_AXIS;
421 		}
422 	}
423 	/* Try the wheel first as the Z activator since it's tradition. */
424 	if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
425 	    HUG_WHEEL), hid_input, index, &info->sc_loc_z, &flags,
426 	    &info->sc_iid_z) ||
427 	    hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
428 	    HUG_TWHEEL), hid_input, index, &info->sc_loc_z, &flags,
429 	    &info->sc_iid_z)) {
430 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
431 			info->sc_flags |= UMS_FLAG_Z_AXIS;
432 		}
433 		/*
434 		 * We might have both a wheel and Z direction, if so put
435 		 * put the Z on the W coordinate.
436 		 */
437 		if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
438 		    HUG_Z), hid_input, index, &info->sc_loc_w, &flags,
439 		    &info->sc_iid_w)) {
440 
441 			if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
442 				info->sc_flags |= UMS_FLAG_W_AXIS;
443 			}
444 		}
445 	} else if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
446 	    HUG_Z), hid_input, index, &info->sc_loc_z, &flags,
447 	    &info->sc_iid_z)) {
448 
449 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
450 			info->sc_flags |= UMS_FLAG_Z_AXIS;
451 		}
452 	}
453 	/*
454 	 * The Microsoft Wireless Intellimouse 2.0 reports it's wheel
455 	 * using 0x0048, which is HUG_TWHEEL, and seems to expect you
456 	 * to know that the byte after the wheel is the tilt axis.
457 	 * There are no other HID axis descriptors other than X,Y and
458 	 * TWHEEL
459 	 */
460 	if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
461 	    HUG_TWHEEL), hid_input, index, &info->sc_loc_t,
462 	    &flags, &info->sc_iid_t)) {
463 
464 		info->sc_loc_t.pos += 8;
465 
466 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
467 			info->sc_flags |= UMS_FLAG_T_AXIS;
468 		}
469 	} else if (hid_locate(buf, len, HID_USAGE2(HUP_CONSUMER,
470 		HUC_AC_PAN), hid_input, index, &info->sc_loc_t,
471 		&flags, &info->sc_iid_t)) {
472 
473 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS)
474 			info->sc_flags |= UMS_FLAG_T_AXIS;
475 	}
476 	/* figure out the number of buttons */
477 
478 	for (i = 0; i < UMS_BUTTON_MAX; i++) {
479 		if (!hid_locate(buf, len, HID_USAGE2(HUP_BUTTON, (i + 1)),
480 		    hid_input, index, &info->sc_loc_btn[i], NULL,
481 		    &info->sc_iid_btn[i])) {
482 			break;
483 		}
484 	}
485 
486 	/* detect other buttons */
487 
488 	for (j = 0; (i < UMS_BUTTON_MAX) && (j < 2); i++, j++) {
489 		if (!hid_locate(buf, len, HID_USAGE2(HUP_MICROSOFT, (j + 1)),
490 		    hid_input, index, &info->sc_loc_btn[i], NULL,
491 		    &info->sc_iid_btn[i])) {
492 			break;
493 		}
494 	}
495 
496 	info->sc_buttons = i;
497 
498 	if (i > sc->sc_buttons)
499 		sc->sc_buttons = i;
500 
501 	if (info->sc_flags == 0)
502 		return;
503 
504 	/* announce information about the mouse */
505 	device_printf(dev, "%d buttons and [%s%s%s%s%s] coordinates ID=%u\n",
506 	    (info->sc_buttons),
507 	    (info->sc_flags & UMS_FLAG_X_AXIS) ? "X" : "",
508 	    (info->sc_flags & UMS_FLAG_Y_AXIS) ? "Y" : "",
509 	    (info->sc_flags & UMS_FLAG_Z_AXIS) ? "Z" : "",
510 	    (info->sc_flags & UMS_FLAG_T_AXIS) ? "T" : "",
511 	    (info->sc_flags & UMS_FLAG_W_AXIS) ? "W" : "",
512 	    info->sc_iid_x);
513 }
514 
515 static int
516 ums_attach(device_t dev)
517 {
518 	struct usb_attach_arg *uaa = device_get_ivars(dev);
519 	struct ums_softc *sc = device_get_softc(dev);
520 	struct ums_info *info;
521 	void *d_ptr = NULL;
522 	int isize;
523 	int err;
524 	uint16_t d_len;
525 	uint8_t i;
526 #ifdef USB_DEBUG
527 	uint8_t j;
528 #endif
529 
530 	DPRINTFN(11, "sc=%p\n", sc);
531 
532 	device_set_usb_desc(dev);
533 
534 	lockinit(&sc->sc_lock, "ums lock", 0, LK_CANRECURSE);
535 
536 	usb_callout_init_mtx(&sc->sc_callout, &sc->sc_lock, 0);
537 
538 	/*
539          * Force the report (non-boot) protocol.
540          *
541          * Mice without boot protocol support may choose not to implement
542          * Set_Protocol at all; Ignore any error.
543          */
544 	err = usbd_req_set_protocol(uaa->device, NULL,
545 	    uaa->info.bIfaceIndex, 1);
546 
547 	err = usbd_transfer_setup(uaa->device,
548 	    &uaa->info.bIfaceIndex, sc->sc_xfer, ums_config,
549 	    UMS_N_TRANSFER, sc, &sc->sc_lock);
550 
551 	if (err) {
552 		DPRINTF("error=%s\n", usbd_errstr(err));
553 		goto detach;
554 	}
555 
556 	/* Get HID descriptor */
557 	err = usbd_req_get_hid_desc(uaa->device, NULL, &d_ptr,
558 	    &d_len, M_TEMP, uaa->info.bIfaceIndex);
559 
560 	if (err) {
561 		device_printf(dev, "error reading report description\n");
562 		goto detach;
563 	}
564 
565 	isize = hid_report_size(d_ptr, d_len, hid_input, &sc->sc_iid);
566 
567 	/*
568 	 * The Microsoft Wireless Notebook Optical Mouse seems to be in worse
569 	 * shape than the Wireless Intellimouse 2.0, as its X, Y, wheel, and
570 	 * all of its other button positions are all off. It also reports that
571 	 * it has two addional buttons and a tilt wheel.
572 	 */
573 	if (usb_test_quirk(uaa, UQ_MS_BAD_CLASS)) {
574 
575 		sc->sc_iid = 0;
576 
577 		info = &sc->sc_info[0];
578 		info->sc_flags = (UMS_FLAG_X_AXIS |
579 		    UMS_FLAG_Y_AXIS |
580 		    UMS_FLAG_Z_AXIS |
581 		    UMS_FLAG_SBU);
582 		info->sc_buttons = 3;
583 		isize = 5;
584 		/* 1st byte of descriptor report contains garbage */
585 		info->sc_loc_x.pos = 16;
586 		info->sc_loc_x.size = 8;
587 		info->sc_loc_y.pos = 24;
588 		info->sc_loc_y.size = 8;
589 		info->sc_loc_z.pos = 32;
590 		info->sc_loc_z.size = 8;
591 		info->sc_loc_btn[0].pos = 8;
592 		info->sc_loc_btn[0].size = 1;
593 		info->sc_loc_btn[1].pos = 9;
594 		info->sc_loc_btn[1].size = 1;
595 		info->sc_loc_btn[2].pos = 10;
596 		info->sc_loc_btn[2].size = 1;
597 
598 		/* Announce device */
599 		device_printf(dev, "3 buttons and [XYZ] "
600 		    "coordinates ID=0\n");
601 
602 	} else {
603 		/* Search the HID descriptor and announce device */
604 		for (i = 0; i < UMS_INFO_MAX; i++) {
605 			ums_hid_parse(sc, dev, d_ptr, d_len, i);
606 		}
607 	}
608 
609 	if (usb_test_quirk(uaa, UQ_MS_REVZ)) {
610 		info = &sc->sc_info[0];
611 		/* Some wheels need the Z axis reversed. */
612 		info->sc_flags |= UMS_FLAG_REVZ;
613 	}
614 	if (isize > (int)usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT])) {
615 		DPRINTF("WARNING: report size, %d bytes, is larger "
616 		    "than interrupt size, %d bytes!\n", isize,
617 		    usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT]));
618 	}
619 	kfree(d_ptr, M_TEMP);
620 	d_ptr = NULL;
621 
622 #ifdef USB_DEBUG
623 	for (j = 0; j < UMS_INFO_MAX; j++) {
624 		info = &sc->sc_info[j];
625 
626 		DPRINTF("sc=%p, index=%d\n", sc, j);
627 		DPRINTF("X\t%d/%d id=%d\n", info->sc_loc_x.pos,
628 		    info->sc_loc_x.size, info->sc_iid_x);
629 		DPRINTF("Y\t%d/%d id=%d\n", info->sc_loc_y.pos,
630 		    info->sc_loc_y.size, info->sc_iid_y);
631 		DPRINTF("Z\t%d/%d id=%d\n", info->sc_loc_z.pos,
632 		    info->sc_loc_z.size, info->sc_iid_z);
633 		DPRINTF("T\t%d/%d id=%d\n", info->sc_loc_t.pos,
634 		    info->sc_loc_t.size, info->sc_iid_t);
635 		DPRINTF("W\t%d/%d id=%d\n", info->sc_loc_w.pos,
636 		    info->sc_loc_w.size, info->sc_iid_w);
637 
638 		for (i = 0; i < info->sc_buttons; i++) {
639 			DPRINTF("B%d\t%d/%d id=%d\n",
640 			    i + 1, info->sc_loc_btn[i].pos,
641 			    info->sc_loc_btn[i].size, info->sc_iid_btn[i]);
642 		}
643 	}
644 	DPRINTF("size=%d, id=%d\n", isize, sc->sc_iid);
645 #endif
646 
647 	err = usb_fifo_attach(uaa->device, sc, &sc->sc_lock,
648 	    &ums_fifo_methods, &sc->sc_fifo,
649 	    device_get_unit(dev), -1, uaa->info.bIfaceIndex,
650   	    UID_ROOT, GID_OPERATOR, 0644);
651 	if (err)
652 		goto detach;
653 #if 0
654 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
655 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
656 	    OID_AUTO, "parseinfo", CTLTYPE_STRING|CTLFLAG_RD,
657 	    sc, 0, ums_sysctl_handler_parseinfo,
658 	    "", "Dump of parsed HID report descriptor");
659 #endif
660 
661 	return (0);
662 
663 detach:
664 	if (d_ptr) {
665 		kfree(d_ptr, M_TEMP);
666 	}
667 	ums_detach(dev);
668 	return (ENOMEM);
669 }
670 
671 static int
672 ums_detach(device_t self)
673 {
674 	struct ums_softc *sc = device_get_softc(self);
675 
676 	DPRINTF("sc=%p\n", sc);
677 
678 	usb_fifo_detach(&sc->sc_fifo);
679 
680 	usbd_transfer_unsetup(sc->sc_xfer, UMS_N_TRANSFER);
681 
682 	usb_callout_drain(&sc->sc_callout);
683 
684 	lockuninit(&sc->sc_lock);
685 
686 	return (0);
687 }
688 
689 static void
690 ums_start_read(struct usb_fifo *fifo)
691 {
692 	struct ums_softc *sc = usb_fifo_softc(fifo);
693 	int rate;
694 
695 	/* Check if we should override the default polling interval */
696 	rate = sc->sc_pollrate;
697 	/* Range check rate */
698 	if (rate > 1000)
699 		rate = 1000;
700 	/* Check for set rate */
701 	if ((rate > 0) && (sc->sc_xfer[UMS_INTR_DT] != NULL)) {
702 		DPRINTF("Setting pollrate = %d\n", rate);
703 		/* Stop current transfer, if any */
704 		if (sc->sc_read_running) {
705 			usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
706 			sc->sc_read_running = 0;
707 		}
708 		/* Set new interval */
709 		usbd_xfer_set_interval(sc->sc_xfer[UMS_INTR_DT], 1000 / rate);
710 		/* Only set pollrate once */
711 		sc->sc_pollrate = 0;
712 	}
713 	if (sc->sc_read_running == 0) {
714 		sc->sc_read_running = 1;
715 		usbd_transfer_start(sc->sc_xfer[UMS_INTR_DT]);
716 	}
717 }
718 
719 static void
720 ums_stop_read(struct usb_fifo *fifo)
721 {
722 	struct ums_softc *sc = usb_fifo_softc(fifo);
723 
724 	if (sc->sc_read_running) {
725 		usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
726 		sc->sc_read_running = 0;
727 		usb_callout_stop(&sc->sc_callout);
728 	}
729 }
730 
731 
732 #if ((MOUSE_SYS_PACKETSIZE != 8) || \
733      (MOUSE_MSC_PACKETSIZE != 5))
734 #error "Software assumptions are not met. Please update code."
735 #endif
736 
737 static void
738 ums_put_queue(struct ums_softc *sc, int32_t dx, int32_t dy,
739     int32_t dz, int32_t dt, int32_t buttons)
740 {
741 	uint8_t buf[8];
742 
743 	if (1) {
744 
745 		if (dx > 254)
746 			dx = 254;
747 		if (dx < -256)
748 			dx = -256;
749 		if (dy > 254)
750 			dy = 254;
751 		if (dy < -256)
752 			dy = -256;
753 		if (dz > 126)
754 			dz = 126;
755 		if (dz < -128)
756 			dz = -128;
757 		if (dt > 126)
758 			dt = 126;
759 		if (dt < -128)
760 			dt = -128;
761 
762 		buf[0] = sc->sc_mode.syncmask[1];
763 		buf[0] |= (~buttons) & MOUSE_MSC_BUTTONS;
764 		buf[1] = dx >> 1;
765 		buf[2] = dy >> 1;
766 		buf[3] = dx - (dx >> 1);
767 		buf[4] = dy - (dy >> 1);
768 
769 		if (sc->sc_mode.level == 1) {
770 			buf[5] = dz >> 1;
771 			buf[6] = dz - (dz >> 1);
772 			buf[7] = (((~buttons) >> 3) & MOUSE_SYS_EXTBUTTONS);
773 		}
774 		usb_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf,
775 		    sc->sc_mode.packetsize, 1);
776 
777 	} else {
778 		DPRINTF("Buffer full, discarded packet\n");
779 	}
780 }
781 
782 static void
783 ums_reset_buf(struct ums_softc *sc)
784 {
785 	/* reset read queue, must be called locked */
786 	usb_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]);
787 }
788 
789 static int
790 ums_open(struct usb_fifo *fifo, int fflags)
791 {
792 	struct ums_softc *sc = usb_fifo_softc(fifo);
793 
794 	DPRINTFN(2, "\n");
795 
796 	/* check for duplicate open, should not happen */
797 	if (sc->sc_fflags & fflags)
798 		return (EBUSY);
799 
800 	/* check for first open */
801 	if (sc->sc_fflags == 0) {
802 
803 		/* reset all USB mouse parameters */
804 
805 		if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
806 			sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
807 		else
808 			sc->sc_hw.buttons = sc->sc_buttons;
809 
810 		sc->sc_hw.iftype = MOUSE_IF_USB;
811 		sc->sc_hw.type = MOUSE_MOUSE;
812 		sc->sc_hw.model = MOUSE_MODEL_GENERIC;
813 		sc->sc_hw.hwid = 0;
814 
815 		sc->sc_mode.protocol = MOUSE_PROTO_MSC;
816 		sc->sc_mode.rate = -1;
817 		sc->sc_mode.resolution = MOUSE_RES_UNKNOWN;
818 		sc->sc_mode.accelfactor = 0;
819 		sc->sc_mode.level = 0;
820 		sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
821 		sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
822 		sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
823 
824 		/* reset status */
825 
826 		sc->sc_status.flags = 0;
827 		sc->sc_status.button = 0;
828 		sc->sc_status.obutton = 0;
829 		sc->sc_status.dx = 0;
830 		sc->sc_status.dy = 0;
831 		sc->sc_status.dz = 0;
832 		/* sc->sc_status.dt = 0; */
833 	}
834 
835 	if (fflags & FREAD) {
836 		/* allocate RX buffer */
837 		if (usb_fifo_alloc_buffer(fifo,
838 		    UMS_BUF_SIZE, UMS_IFQ_MAXLEN)) {
839 			return (ENOMEM);
840 		}
841 	}
842 
843 	sc->sc_fflags |= fflags & (FREAD | FWRITE);
844 	return (0);
845 }
846 
847 static void
848 ums_close(struct usb_fifo *fifo, int fflags)
849 {
850 	struct ums_softc *sc = usb_fifo_softc(fifo);
851 
852 	DPRINTFN(2, "\n");
853 
854 	if (fflags & FREAD)
855 		usb_fifo_free_buffer(fifo);
856 
857 	sc->sc_fflags &= ~(fflags & (FREAD | FWRITE));
858 }
859 
860 static int
861 ums_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
862 {
863 	struct ums_softc *sc = usb_fifo_softc(fifo);
864 	mousemode_t mode;
865 	int error = 0;
866 
867 	DPRINTFN(2, "\n");
868 
869 	lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
870 
871 	switch (cmd) {
872 	case MOUSE_GETHWINFO:
873 		*(mousehw_t *)addr = sc->sc_hw;
874 		break;
875 
876 	case MOUSE_GETMODE:
877 		*(mousemode_t *)addr = sc->sc_mode;
878 		break;
879 
880 	case MOUSE_SETMODE:
881 		mode = *(mousemode_t *)addr;
882 
883 		if (mode.level == -1) {
884 			/* don't change the current setting */
885 		} else if ((mode.level < 0) || (mode.level > 1)) {
886 			error = EINVAL;
887 			break;
888 		} else {
889 			sc->sc_mode.level = mode.level;
890 		}
891 
892 		/* store polling rate */
893 		sc->sc_pollrate = mode.rate;
894 
895 		if (sc->sc_mode.level == 0) {
896 			if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
897 				sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
898 			else
899 				sc->sc_hw.buttons = sc->sc_buttons;
900 			sc->sc_mode.protocol = MOUSE_PROTO_MSC;
901 			sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
902 			sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
903 			sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
904 		} else if (sc->sc_mode.level == 1) {
905 			if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
906 				sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
907 			else
908 				sc->sc_hw.buttons = sc->sc_buttons;
909 			sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
910 			sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
911 			sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
912 			sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
913 		}
914 		ums_reset_buf(sc);
915 		break;
916 
917 	case MOUSE_GETLEVEL:
918 		*(int *)addr = sc->sc_mode.level;
919 		break;
920 
921 	case MOUSE_SETLEVEL:
922 		if (*(int *)addr < 0 || *(int *)addr > 1) {
923 			error = EINVAL;
924 			break;
925 		}
926 		sc->sc_mode.level = *(int *)addr;
927 
928 		if (sc->sc_mode.level == 0) {
929 			if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
930 				sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
931 			else
932 				sc->sc_hw.buttons = sc->sc_buttons;
933 			sc->sc_mode.protocol = MOUSE_PROTO_MSC;
934 			sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
935 			sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
936 			sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
937 		} else if (sc->sc_mode.level == 1) {
938 			if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
939 				sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
940 			else
941 				sc->sc_hw.buttons = sc->sc_buttons;
942 			sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
943 			sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
944 			sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
945 			sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
946 		}
947 		ums_reset_buf(sc);
948 		break;
949 
950 	case MOUSE_GETSTATUS:{
951 			mousestatus_t *status = (mousestatus_t *)addr;
952 
953 			*status = sc->sc_status;
954 			sc->sc_status.obutton = sc->sc_status.button;
955 			sc->sc_status.button = 0;
956 			sc->sc_status.dx = 0;
957 			sc->sc_status.dy = 0;
958 			sc->sc_status.dz = 0;
959 			/* sc->sc_status.dt = 0; */
960 
961 			if (status->dx || status->dy || status->dz /* || status->dt */ ) {
962 				status->flags |= MOUSE_POSCHANGED;
963 			}
964 			if (status->button != status->obutton) {
965 				status->flags |= MOUSE_BUTTONSCHANGED;
966 			}
967 			break;
968 		}
969 	default:
970 		error = ENOTTY;
971 		break;
972 	}
973 
974 	lockmgr(&sc->sc_lock, LK_RELEASE);
975 	return (error);
976 }
977 
978 #if 0 /* XXX */
979 static int
980 ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS)
981 {
982 	struct ums_softc *sc = arg1;
983 	struct ums_info *info;
984 	struct sbuf *sb;
985 	int i, j, err, had_output;
986 
987 	sb = sbuf_new_auto();
988 	for (i = 0, had_output = 0; i < UMS_INFO_MAX; i++) {
989 		info = &sc->sc_info[i];
990 
991 		/* Don't emit empty info */
992 		if ((info->sc_flags &
993 		    (UMS_FLAG_X_AXIS | UMS_FLAG_Y_AXIS | UMS_FLAG_Z_AXIS |
994 		     UMS_FLAG_T_AXIS | UMS_FLAG_W_AXIS)) == 0 &&
995 		    info->sc_buttons == 0)
996 			continue;
997 
998 		if (had_output)
999 			sbuf_printf(sb, "\n");
1000 		had_output = 1;
1001 		sbuf_printf(sb, "i%d:", i + 1);
1002 		if (info->sc_flags & UMS_FLAG_X_AXIS)
1003 			sbuf_printf(sb, " X:r%d, p%d, s%d;",
1004 			    (int)info->sc_iid_x,
1005 			    (int)info->sc_loc_x.pos,
1006 			    (int)info->sc_loc_x.size);
1007 		if (info->sc_flags & UMS_FLAG_Y_AXIS)
1008 			sbuf_printf(sb, " Y:r%d, p%d, s%d;",
1009 			    (int)info->sc_iid_y,
1010 			    (int)info->sc_loc_y.pos,
1011 			    (int)info->sc_loc_y.size);
1012 		if (info->sc_flags & UMS_FLAG_Z_AXIS)
1013 			sbuf_printf(sb, " Z:r%d, p%d, s%d;",
1014 			    (int)info->sc_iid_z,
1015 			    (int)info->sc_loc_z.pos,
1016 			    (int)info->sc_loc_z.size);
1017 		if (info->sc_flags & UMS_FLAG_T_AXIS)
1018 			sbuf_printf(sb, " T:r%d, p%d, s%d;",
1019 			    (int)info->sc_iid_t,
1020 			    (int)info->sc_loc_t.pos,
1021 			    (int)info->sc_loc_t.size);
1022 		if (info->sc_flags & UMS_FLAG_W_AXIS)
1023 			sbuf_printf(sb, " W:r%d, p%d, s%d;",
1024 			    (int)info->sc_iid_w,
1025 			    (int)info->sc_loc_w.pos,
1026 			    (int)info->sc_loc_w.size);
1027 
1028 		for (j = 0; j < info->sc_buttons; j++) {
1029 			sbuf_printf(sb, " B%d:r%d, p%d, s%d;", j + 1,
1030 			    (int)info->sc_iid_btn[j],
1031 			    (int)info->sc_loc_btn[j].pos,
1032 			    (int)info->sc_loc_btn[j].size);
1033 		}
1034 	}
1035 	sbuf_finish(sb);
1036 	err = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
1037 	sbuf_delete(sb);
1038 
1039 	return (err);
1040 }
1041 #endif
1042 
1043 static devclass_t ums_devclass;
1044 
1045 static device_method_t ums_methods[] = {
1046 	DEVMETHOD(device_probe, ums_probe),
1047 	DEVMETHOD(device_attach, ums_attach),
1048 	DEVMETHOD(device_detach, ums_detach),
1049 	DEVMETHOD_END
1050 };
1051 
1052 static driver_t ums_driver = {
1053 	.name = "ums",
1054 	.methods = ums_methods,
1055 	.size = sizeof(struct ums_softc),
1056 };
1057 
1058 DRIVER_MODULE(ums, uhub, ums_driver, ums_devclass, NULL, NULL);
1059 MODULE_DEPEND(ums, usb, 1, 1, 1);
1060 MODULE_VERSION(ums, 1);
1061