xref: /freebsd/sys/dev/usb/input/ums.c (revision 3a3f90c6c3cc3124c6f4df8a11b57c8a36a2d13c)
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  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *        This product includes software developed by the NetBSD
20  *        Foundation, Inc. and its contributors.
21  * 4. Neither the name of The NetBSD Foundation nor the names of its
22  *    contributors may be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 /*
42  * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
43  */
44 
45 #include "usbdevs.h"
46 #include <dev/usb/usb.h>
47 #include <dev/usb/usb_mfunc.h>
48 #include <dev/usb/usb_error.h>
49 #include <dev/usb/usbhid.h>
50 
51 #define	USB_DEBUG_VAR ums_debug
52 
53 #include <dev/usb/usb_core.h>
54 #include <dev/usb/usb_util.h>
55 #include <dev/usb/usb_debug.h>
56 #include <dev/usb/usb_busdma.h>
57 #include <dev/usb/usb_process.h>
58 #include <dev/usb/usb_transfer.h>
59 #include <dev/usb/usb_request.h>
60 #include <dev/usb/usb_dynamic.h>
61 #include <dev/usb/usb_mbuf.h>
62 #include <dev/usb/usb_dev.h>
63 #include <dev/usb/usb_hid.h>
64 
65 #include <dev/usb/quirk/usb_quirk.h>
66 
67 #include <sys/ioccom.h>
68 #include <sys/filio.h>
69 #include <sys/tty.h>
70 #include <sys/mouse.h>
71 
72 #if USB_DEBUG
73 static int ums_debug = 0;
74 
75 SYSCTL_NODE(_hw_usb2, OID_AUTO, ums, CTLFLAG_RW, 0, "USB ums");
76 SYSCTL_INT(_hw_usb2_ums, OID_AUTO, debug, CTLFLAG_RW,
77     &ums_debug, 0, "Debug level");
78 #endif
79 
80 #define	MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
81 #define	MOUSE_FLAGS (HIO_RELATIVE)
82 
83 #define	UMS_BUF_SIZE      8		/* bytes */
84 #define	UMS_IFQ_MAXLEN   50		/* units */
85 #define	UMS_BUTTON_MAX   31		/* exclusive, must be less than 32 */
86 #define	UMS_BUT(i) ((i) < 3 ? (((i) + 2) % 3) : (i))
87 
88 enum {
89 	UMS_INTR_DT,
90 	UMS_INTR_CS,
91 	UMS_N_TRANSFER = 2,
92 };
93 
94 struct ums_softc {
95 	struct usb2_fifo_sc sc_fifo;
96 	struct mtx sc_mtx;
97 	struct usb2_callout sc_callout;
98 	struct hid_location sc_loc_w;
99 	struct hid_location sc_loc_x;
100 	struct hid_location sc_loc_y;
101 	struct hid_location sc_loc_z;
102 	struct hid_location sc_loc_t;
103 	struct hid_location sc_loc_btn[UMS_BUTTON_MAX];
104 	mousehw_t sc_hw;
105 	mousemode_t sc_mode;
106 	mousestatus_t sc_status;
107 
108 	struct usb2_xfer *sc_xfer[UMS_N_TRANSFER];
109 
110 	uint32_t sc_flags;
111 #define	UMS_FLAG_X_AXIS     0x0001
112 #define	UMS_FLAG_Y_AXIS     0x0002
113 #define	UMS_FLAG_Z_AXIS     0x0004
114 #define	UMS_FLAG_T_AXIS     0x0008
115 #define	UMS_FLAG_SBU        0x0010	/* spurious button up events */
116 #define	UMS_FLAG_INTR_STALL 0x0020	/* set if transfer error */
117 #define	UMS_FLAG_REVZ	    0x0040	/* Z-axis is reversed */
118 #define	UMS_FLAG_W_AXIS     0x0080
119 
120 	uint8_t	sc_buttons;
121 	uint8_t	sc_iid;
122 	uint8_t	sc_temp[64];
123 };
124 
125 static void ums_put_queue_timeout(void *__sc);
126 
127 static usb2_callback_t ums_clear_stall_callback;
128 static usb2_callback_t ums_intr_callback;
129 
130 static device_probe_t ums_probe;
131 static device_attach_t ums_attach;
132 static device_detach_t ums_detach;
133 
134 static usb2_fifo_cmd_t ums_start_read;
135 static usb2_fifo_cmd_t ums_stop_read;
136 static usb2_fifo_open_t ums_open;
137 static usb2_fifo_close_t ums_close;
138 static usb2_fifo_ioctl_t ums_ioctl;
139 
140 static void ums_put_queue(struct ums_softc *sc, int32_t dx, int32_t dy, int32_t dz, int32_t dt, int32_t buttons);
141 
142 static struct usb2_fifo_methods ums_fifo_methods = {
143 	.f_open = &ums_open,
144 	.f_close = &ums_close,
145 	.f_ioctl = &ums_ioctl,
146 	.f_start_read = &ums_start_read,
147 	.f_stop_read = &ums_stop_read,
148 	.basename[0] = "ums",
149 };
150 
151 static void
152 ums_put_queue_timeout(void *__sc)
153 {
154 	struct ums_softc *sc = __sc;
155 
156 	mtx_assert(&sc->sc_mtx, MA_OWNED);
157 
158 	ums_put_queue(sc, 0, 0, 0, 0, 0);
159 }
160 
161 static void
162 ums_clear_stall_callback(struct usb2_xfer *xfer)
163 {
164 	struct ums_softc *sc = xfer->priv_sc;
165 	struct usb2_xfer *xfer_other = sc->sc_xfer[UMS_INTR_DT];
166 
167 	if (usb2_clear_stall_callback(xfer, xfer_other)) {
168 		DPRINTF("stall cleared\n");
169 		sc->sc_flags &= ~UMS_FLAG_INTR_STALL;
170 		usb2_transfer_start(xfer_other);
171 	}
172 }
173 
174 static void
175 ums_intr_callback(struct usb2_xfer *xfer)
176 {
177 	struct ums_softc *sc = xfer->priv_sc;
178 	uint8_t *buf = sc->sc_temp;
179 	uint16_t len = xfer->actlen;
180 	int32_t buttons = 0;
181 	int32_t dw;
182 	int32_t dx;
183 	int32_t dy;
184 	int32_t dz;
185 	int32_t dt;
186 	uint8_t i;
187 
188 	switch (USB_GET_STATE(xfer)) {
189 	case USB_ST_TRANSFERRED:
190 		DPRINTFN(6, "sc=%p actlen=%d\n", sc, len);
191 
192 		if (len > sizeof(sc->sc_temp)) {
193 			DPRINTFN(6, "truncating large packet to %zu bytes\n",
194 			    sizeof(sc->sc_temp));
195 			len = sizeof(sc->sc_temp);
196 		}
197 		if (len == 0) {
198 			goto tr_setup;
199 		}
200 		usb2_copy_out(xfer->frbuffers, 0, buf, len);
201 
202 		DPRINTFN(6, "data = %02x %02x %02x %02x "
203 		    "%02x %02x %02x %02x\n",
204 		    (len > 0) ? buf[0] : 0, (len > 1) ? buf[1] : 0,
205 		    (len > 2) ? buf[2] : 0, (len > 3) ? buf[3] : 0,
206 		    (len > 4) ? buf[4] : 0, (len > 5) ? buf[5] : 0,
207 		    (len > 6) ? buf[6] : 0, (len > 7) ? buf[7] : 0);
208 
209 		/*
210 		 * The M$ Wireless Intellimouse 2.0 sends 1 extra leading byte
211 		 * of data compared to most USB mice. This byte frequently
212 		 * switches from 0x01 (usual state) to 0x02. I assume it is to
213 		 * allow extra, non-standard, reporting (say battery-life).
214 		 *
215 		 * However at the same time it generates a left-click message
216 		 * on the button byte which causes spurious left-click's where
217 		 * there shouldn't be.  This should sort that.  Currently it's
218 		 * the only user of UMS_FLAG_T_AXIS so use it as an
219 		 * identifier.
220 		 *
221 		 *
222 		 * UPDATE: This problem affects the M$ Wireless Notebook Optical Mouse,
223 		 * too. However, the leading byte for this mouse is normally 0x11,
224 		 * and the phantom mouse click occurs when its 0x14.
225 		 *
226 		 * We probably should switch to some more official quirk.
227 		 */
228 		if (sc->sc_iid) {
229 			if (sc->sc_flags & UMS_FLAG_T_AXIS) {
230 				if (*buf == 0x02) {
231 					goto tr_setup;
232 				}
233 			} else {
234 				if (*buf != sc->sc_iid) {
235 					goto tr_setup;
236 				}
237 			}
238 
239 			len--;
240 			buf++;
241 
242 		} else {
243 			if (sc->sc_flags & UMS_FLAG_SBU) {
244 				if ((*buf == 0x14) || (*buf == 0x15)) {
245 					goto tr_setup;
246 				}
247 			}
248 		}
249 
250 		dw = (sc->sc_flags & UMS_FLAG_W_AXIS) ?
251 		    hid_get_data(buf, len, &sc->sc_loc_w) : 0;
252 
253 		dx = (sc->sc_flags & UMS_FLAG_X_AXIS) ?
254 		    hid_get_data(buf, len, &sc->sc_loc_x) : 0;
255 
256 		dy = (sc->sc_flags & UMS_FLAG_Y_AXIS) ?
257 		    -hid_get_data(buf, len, &sc->sc_loc_y) : 0;
258 
259 		dz = (sc->sc_flags & UMS_FLAG_Z_AXIS) ?
260 		    -hid_get_data(buf, len, &sc->sc_loc_z) : 0;
261 
262 		if (sc->sc_flags & UMS_FLAG_REVZ) {
263 			dz = -dz;
264 		}
265 		dt = (sc->sc_flags & UMS_FLAG_T_AXIS) ?
266 		    -hid_get_data(buf, len, &sc->sc_loc_t): 0;
267 
268 		for (i = 0; i < sc->sc_buttons; i++) {
269 			if (hid_get_data(buf, len, &sc->sc_loc_btn[i])) {
270 				buttons |= (1 << UMS_BUT(i));
271 			}
272 		}
273 
274 		if (dx || dy || dz || dt || dw ||
275 		    (buttons != sc->sc_status.button)) {
276 
277 			DPRINTFN(6, "x:%d y:%d z:%d t:%d w:%d buttons:0x%08x\n",
278 			    dx, dy, dz, dt, dw, buttons);
279 
280 			sc->sc_status.button = buttons;
281 			sc->sc_status.dx += dx;
282 			sc->sc_status.dy += dy;
283 			sc->sc_status.dz += dz;
284 			/*
285 			 * sc->sc_status.dt += dt;
286 			 * no way to export this yet
287 			 */
288 
289 			/*
290 		         * The Qtronix keyboard has a built in PS/2 port for a mouse.
291 		         * The firmware once in a while posts a spurious button up
292 		         * event. This event we ignore by doing a timeout for 50 msecs.
293 		         * If we receive dx=dy=dz=buttons=0 before we add the event to
294 		         * the queue.
295 		         * In any other case we delete the timeout event.
296 		         */
297 			if ((sc->sc_flags & UMS_FLAG_SBU) &&
298 			    (dx == 0) && (dy == 0) && (dz == 0) && (dt == 0) &&
299 			    (dw == 0) && (buttons == 0)) {
300 
301 				usb2_callout_reset(&sc->sc_callout, hz / 20,
302 				    &ums_put_queue_timeout, sc);
303 			} else {
304 
305 				usb2_callout_stop(&sc->sc_callout);
306 
307 				ums_put_queue(sc, dx, dy, dz, dt, buttons);
308 			}
309 		}
310 	case USB_ST_SETUP:
311 tr_setup:
312 		if (sc->sc_flags & UMS_FLAG_INTR_STALL) {
313 			usb2_transfer_start(sc->sc_xfer[UMS_INTR_CS]);
314 		} else {
315 			/* check if we can put more data into the FIFO */
316 			if (usb2_fifo_put_bytes_max(
317 			    sc->sc_fifo.fp[USB_FIFO_RX]) != 0) {
318 				xfer->frlengths[0] = xfer->max_data_length;
319 				usb2_start_hardware(xfer);
320 			}
321 		}
322 		return;
323 
324 	default:			/* Error */
325 		if (xfer->error != USB_ERR_CANCELLED) {
326 			/* start clear stall */
327 			sc->sc_flags |= UMS_FLAG_INTR_STALL;
328 			usb2_transfer_start(sc->sc_xfer[UMS_INTR_CS]);
329 		}
330 		return;
331 	}
332 }
333 
334 static const struct usb2_config ums_config[UMS_N_TRANSFER] = {
335 
336 	[UMS_INTR_DT] = {
337 		.type = UE_INTERRUPT,
338 		.endpoint = UE_ADDR_ANY,
339 		.direction = UE_DIR_IN,
340 		.mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
341 		.mh.bufsize = 0,	/* use wMaxPacketSize */
342 		.mh.callback = &ums_intr_callback,
343 	},
344 
345 	[UMS_INTR_CS] = {
346 		.type = UE_CONTROL,
347 		.endpoint = 0x00,	/* Control pipe */
348 		.direction = UE_DIR_ANY,
349 		.mh.bufsize = sizeof(struct usb2_device_request),
350 		.mh.callback = &ums_clear_stall_callback,
351 		.mh.timeout = 1000,	/* 1 second */
352 		.mh.interval = 50,	/* 50ms */
353 	},
354 };
355 
356 static int
357 ums_probe(device_t dev)
358 {
359 	struct usb2_attach_arg *uaa = device_get_ivars(dev);
360 	struct usb2_interface_descriptor *id;
361 	void *d_ptr;
362 	int32_t error = 0;
363 	uint16_t d_len;
364 
365 	DPRINTFN(11, "\n");
366 
367 	if (uaa->usb2_mode != USB_MODE_HOST) {
368 		return (ENXIO);
369 	}
370 	if (uaa->iface == NULL) {
371 		return (ENXIO);
372 	}
373 	id = usb2_get_interface_descriptor(uaa->iface);
374 
375 	if ((id == NULL) ||
376 	    (id->bInterfaceClass != UICLASS_HID)) {
377 		return (ENXIO);
378 	}
379 	error = usb2_req_get_hid_desc
380 	    (uaa->device, &Giant,
381 	    &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
382 
383 	if (error) {
384 		return (ENXIO);
385 	}
386 	if (hid_is_collection(d_ptr, d_len,
387 	    HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE))) {
388 		error = 0;
389 	} else if ((id->bInterfaceSubClass == UISUBCLASS_BOOT) &&
390 	    (id->bInterfaceProtocol == UIPROTO_MOUSE)) {
391 		error = 0;
392 	} else {
393 		error = ENXIO;
394 	}
395 
396 	free(d_ptr, M_TEMP);
397 	return (error);
398 }
399 
400 static int
401 ums_attach(device_t dev)
402 {
403 	struct usb2_attach_arg *uaa = device_get_ivars(dev);
404 	struct ums_softc *sc = device_get_softc(dev);
405 	void *d_ptr = NULL;
406 	int unit = device_get_unit(dev);
407 	int32_t isize;
408 	uint32_t flags;
409 	int32_t err;
410 	uint16_t d_len;
411 	uint8_t i;
412 
413 	DPRINTFN(11, "sc=%p\n", sc);
414 
415 	device_set_usb2_desc(dev);
416 
417 	mtx_init(&sc->sc_mtx, "ums lock", NULL, MTX_DEF | MTX_RECURSE);
418 
419 	usb2_callout_init_mtx(&sc->sc_callout, &sc->sc_mtx, 0);
420 
421 	/*
422          * Force the report (non-boot) protocol.
423          *
424          * Mice without boot protocol support may choose not to implement
425          * Set_Protocol at all; Ignore any error.
426          */
427 	err = usb2_req_set_protocol(uaa->device, NULL, uaa->info.bIfaceIndex, 1);
428 
429 	err = usb2_transfer_setup(uaa->device,
430 	    &uaa->info.bIfaceIndex, sc->sc_xfer, ums_config,
431 	    UMS_N_TRANSFER, sc, &sc->sc_mtx);
432 
433 	if (err) {
434 		DPRINTF("error=%s\n", usb2_errstr(err));
435 		goto detach;
436 	}
437 	err = usb2_req_get_hid_desc
438 	    (uaa->device, &Giant, &d_ptr,
439 	    &d_len, M_TEMP, uaa->info.bIfaceIndex);
440 
441 	if (err) {
442 		device_printf(dev, "error reading report description\n");
443 		goto detach;
444 	}
445 	if (hid_locate(d_ptr, d_len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
446 	    hid_input, &sc->sc_loc_x, &flags)) {
447 
448 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
449 			sc->sc_flags |= UMS_FLAG_X_AXIS;
450 		}
451 	}
452 	if (hid_locate(d_ptr, d_len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
453 	    hid_input, &sc->sc_loc_y, &flags)) {
454 
455 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
456 			sc->sc_flags |= UMS_FLAG_Y_AXIS;
457 		}
458 	}
459 	/* Try the wheel first as the Z activator since it's tradition. */
460 	if (hid_locate(d_ptr, d_len, HID_USAGE2(HUP_GENERIC_DESKTOP,
461 	    HUG_WHEEL), hid_input, &sc->sc_loc_z, &flags) ||
462 	    hid_locate(d_ptr, d_len, HID_USAGE2(HUP_GENERIC_DESKTOP,
463 	    HUG_TWHEEL), hid_input, &sc->sc_loc_z, &flags)) {
464 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
465 			sc->sc_flags |= UMS_FLAG_Z_AXIS;
466 		}
467 		/*
468 		 * We might have both a wheel and Z direction, if so put
469 		 * put the Z on the W coordinate.
470 		 */
471 		if (hid_locate(d_ptr, d_len, HID_USAGE2(HUP_GENERIC_DESKTOP,
472 		    HUG_Z), hid_input, &sc->sc_loc_w, &flags)) {
473 
474 			if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
475 				sc->sc_flags |= UMS_FLAG_W_AXIS;
476 			}
477 		}
478 	} else if (hid_locate(d_ptr, d_len, HID_USAGE2(HUP_GENERIC_DESKTOP,
479 	    HUG_Z), hid_input, &sc->sc_loc_z, &flags)) {
480 
481 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
482 			sc->sc_flags |= UMS_FLAG_Z_AXIS;
483 		}
484 	}
485 	/*
486 	 * The Microsoft Wireless Intellimouse 2.0 reports it's wheel
487 	 * using 0x0048, which is HUG_TWHEEL, and seems to expect you
488 	 * to know that the byte after the wheel is the tilt axis.
489 	 * There are no other HID axis descriptors other than X,Y and
490 	 * TWHEEL
491 	 */
492 	if (hid_locate(d_ptr, d_len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_TWHEEL),
493 	    hid_input, &sc->sc_loc_t, &flags)) {
494 
495 		sc->sc_loc_t.pos += 8;
496 
497 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
498 			sc->sc_flags |= UMS_FLAG_T_AXIS;
499 		}
500 	}
501 	/* figure out the number of buttons */
502 
503 	for (i = 0; i < UMS_BUTTON_MAX; i++) {
504 		if (!hid_locate(d_ptr, d_len, HID_USAGE2(HUP_BUTTON, (i + 1)),
505 		    hid_input, &sc->sc_loc_btn[i], NULL)) {
506 			break;
507 		}
508 	}
509 
510 	sc->sc_buttons = i;
511 
512 	isize = hid_report_size(d_ptr, d_len, hid_input, &sc->sc_iid);
513 
514 	/*
515 	 * The Microsoft Wireless Notebook Optical Mouse seems to be in worse
516 	 * shape than the Wireless Intellimouse 2.0, as its X, Y, wheel, and
517 	 * all of its other button positions are all off. It also reports that
518 	 * it has two addional buttons and a tilt wheel.
519 	 */
520 	if (usb2_test_quirk(uaa, UQ_MS_BAD_CLASS)) {
521 		sc->sc_flags = (UMS_FLAG_X_AXIS |
522 		    UMS_FLAG_Y_AXIS |
523 		    UMS_FLAG_Z_AXIS |
524 		    UMS_FLAG_SBU);
525 		sc->sc_buttons = 3;
526 		isize = 5;
527 		sc->sc_iid = 0;
528 		/* 1st byte of descriptor report contains garbage */
529 		sc->sc_loc_x.pos = 16;
530 		sc->sc_loc_y.pos = 24;
531 		sc->sc_loc_z.pos = 32;
532 		sc->sc_loc_btn[0].pos = 8;
533 		sc->sc_loc_btn[1].pos = 9;
534 		sc->sc_loc_btn[2].pos = 10;
535 	}
536 	/*
537 	 * The Microsoft Wireless Notebook Optical Mouse 3000 Model 1049 has
538 	 * five Report IDs: 19 23 24 17 18 (in the order they appear in report
539 	 * descriptor), it seems that report id 17 contains the necessary
540 	 * mouse information(3-buttons,X,Y,wheel) so we specify it manually.
541 	 */
542 	if ((uaa->info.idVendor == USB_VENDOR_MICROSOFT) &&
543 	    (uaa->info.idProduct == USB_PRODUCT_MICROSOFT_WLNOTEBOOK3)) {
544 		sc->sc_flags = (UMS_FLAG_X_AXIS |
545 		    UMS_FLAG_Y_AXIS |
546 		    UMS_FLAG_Z_AXIS);
547 		sc->sc_buttons = 3;
548 		isize = 5;
549 		sc->sc_iid = 17;
550 		sc->sc_loc_x.pos = 8;
551 		sc->sc_loc_y.pos = 16;
552 		sc->sc_loc_z.pos = 24;
553 		sc->sc_loc_btn[0].pos = 0;
554 		sc->sc_loc_btn[1].pos = 1;
555 		sc->sc_loc_btn[2].pos = 2;
556 	}
557 	if (usb2_test_quirk(uaa, UQ_MS_REVZ)) {
558 		/* Some wheels need the Z axis reversed. */
559 		sc->sc_flags |= UMS_FLAG_REVZ;
560 	}
561 	if (isize > sc->sc_xfer[UMS_INTR_DT]->max_frame_size) {
562 		DPRINTF("WARNING: report size, %d bytes, is larger "
563 		    "than interrupt size, %d bytes!\n",
564 		    isize, sc->sc_xfer[UMS_INTR_DT]->max_frame_size);
565 	}
566 	/* announce information about the mouse */
567 
568 	device_printf(dev, "%d buttons and [%s%s%s%s%s] coordinates\n",
569 	    (sc->sc_buttons),
570 	    (sc->sc_flags & UMS_FLAG_X_AXIS) ? "X" : "",
571 	    (sc->sc_flags & UMS_FLAG_Y_AXIS) ? "Y" : "",
572 	    (sc->sc_flags & UMS_FLAG_Z_AXIS) ? "Z" : "",
573 	    (sc->sc_flags & UMS_FLAG_T_AXIS) ? "T" : "",
574 	    (sc->sc_flags & UMS_FLAG_W_AXIS) ? "W" : "");
575 
576 	free(d_ptr, M_TEMP);
577 	d_ptr = NULL;
578 
579 #if USB_DEBUG
580 	DPRINTF("sc=%p\n", sc);
581 	DPRINTF("X\t%d/%d\n", sc->sc_loc_x.pos, sc->sc_loc_x.size);
582 	DPRINTF("Y\t%d/%d\n", sc->sc_loc_y.pos, sc->sc_loc_y.size);
583 	DPRINTF("Z\t%d/%d\n", sc->sc_loc_z.pos, sc->sc_loc_z.size);
584 	DPRINTF("T\t%d/%d\n", sc->sc_loc_t.pos, sc->sc_loc_t.size);
585 	DPRINTF("W\t%d/%d\n", sc->sc_loc_w.pos, sc->sc_loc_w.size);
586 
587 	for (i = 0; i < sc->sc_buttons; i++) {
588 		DPRINTF("B%d\t%d/%d\n",
589 		    i + 1, sc->sc_loc_btn[i].pos, sc->sc_loc_btn[i].size);
590 	}
591 	DPRINTF("size=%d, id=%d\n", isize, sc->sc_iid);
592 #endif
593 
594 	if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
595 		sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
596 	else
597 		sc->sc_hw.buttons = sc->sc_buttons;
598 
599 	sc->sc_hw.iftype = MOUSE_IF_USB;
600 	sc->sc_hw.type = MOUSE_MOUSE;
601 	sc->sc_hw.model = MOUSE_MODEL_GENERIC;
602 	sc->sc_hw.hwid = 0;
603 
604 	sc->sc_mode.protocol = MOUSE_PROTO_MSC;
605 	sc->sc_mode.rate = -1;
606 	sc->sc_mode.resolution = MOUSE_RES_UNKNOWN;
607 	sc->sc_mode.accelfactor = 0;
608 	sc->sc_mode.level = 0;
609 	sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
610 	sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
611 	sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
612 
613 	sc->sc_status.flags = 0;
614 	sc->sc_status.button = 0;
615 	sc->sc_status.obutton = 0;
616 	sc->sc_status.dx = 0;
617 	sc->sc_status.dy = 0;
618 	sc->sc_status.dz = 0;
619 
620 	/* set interface permissions */
621 	usb2_set_iface_perm(uaa->device, uaa->info.bIfaceIndex,
622 	    UID_ROOT, GID_OPERATOR, 0644);
623 
624 	err = usb2_fifo_attach(uaa->device, sc, &sc->sc_mtx,
625 	    &ums_fifo_methods, &sc->sc_fifo,
626 	    unit, 0 - 1, uaa->info.bIfaceIndex);
627 	if (err) {
628 		goto detach;
629 	}
630 	return (0);
631 
632 detach:
633 	if (d_ptr) {
634 		free(d_ptr, M_TEMP);
635 	}
636 	ums_detach(dev);
637 	return (ENOMEM);
638 }
639 
640 static int
641 ums_detach(device_t self)
642 {
643 	struct ums_softc *sc = device_get_softc(self);
644 
645 	DPRINTF("sc=%p\n", sc);
646 
647 	usb2_fifo_detach(&sc->sc_fifo);
648 
649 	usb2_transfer_unsetup(sc->sc_xfer, UMS_N_TRANSFER);
650 
651 	usb2_callout_drain(&sc->sc_callout);
652 
653 	mtx_destroy(&sc->sc_mtx);
654 
655 	return (0);
656 }
657 
658 static void
659 ums_start_read(struct usb2_fifo *fifo)
660 {
661 	struct ums_softc *sc = fifo->priv_sc0;
662 
663 	usb2_transfer_start(sc->sc_xfer[UMS_INTR_DT]);
664 }
665 
666 static void
667 ums_stop_read(struct usb2_fifo *fifo)
668 {
669 	struct ums_softc *sc = fifo->priv_sc0;
670 
671 	usb2_transfer_stop(sc->sc_xfer[UMS_INTR_CS]);
672 	usb2_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
673 	usb2_callout_stop(&sc->sc_callout);
674 }
675 
676 
677 #if ((MOUSE_SYS_PACKETSIZE != 8) || \
678      (MOUSE_MSC_PACKETSIZE != 5))
679 #error "Software assumptions are not met. Please update code."
680 #endif
681 
682 static void
683 ums_put_queue(struct ums_softc *sc, int32_t dx, int32_t dy,
684     int32_t dz, int32_t dt, int32_t buttons)
685 {
686 	uint8_t buf[8];
687 
688 	if (1) {
689 
690 		if (dx > 254)
691 			dx = 254;
692 		if (dx < -256)
693 			dx = -256;
694 		if (dy > 254)
695 			dy = 254;
696 		if (dy < -256)
697 			dy = -256;
698 		if (dz > 126)
699 			dz = 126;
700 		if (dz < -128)
701 			dz = -128;
702 		if (dt > 126)
703 			dt = 126;
704 		if (dt < -128)
705 			dt = -128;
706 
707 		buf[0] = sc->sc_mode.syncmask[1];
708 		buf[0] |= (~buttons) & MOUSE_MSC_BUTTONS;
709 		buf[1] = dx >> 1;
710 		buf[2] = dy >> 1;
711 		buf[3] = dx - (dx >> 1);
712 		buf[4] = dy - (dy >> 1);
713 
714 		if (sc->sc_mode.level == 1) {
715 			buf[5] = dz >> 1;
716 			buf[6] = dz - (dz >> 1);
717 			buf[7] = (((~buttons) >> 3) & MOUSE_SYS_EXTBUTTONS);
718 		}
719 		usb2_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf,
720 		    sc->sc_mode.packetsize, 1);
721 
722 	} else {
723 		DPRINTF("Buffer full, discarded packet\n");
724 	}
725 }
726 
727 static void
728 ums_reset_buf(struct ums_softc *sc)
729 {
730 	/* reset read queue */
731 	usb2_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]);
732 }
733 
734 static int
735 ums_open(struct usb2_fifo *fifo, int fflags, struct thread *td)
736 {
737 	struct ums_softc *sc = fifo->priv_sc0;
738 
739 	DPRINTFN(2, "\n");
740 
741 	if (fflags & FREAD) {
742 
743 		/* reset status */
744 
745 		sc->sc_status.flags = 0;
746 		sc->sc_status.button = 0;
747 		sc->sc_status.obutton = 0;
748 		sc->sc_status.dx = 0;
749 		sc->sc_status.dy = 0;
750 		sc->sc_status.dz = 0;
751 		/* sc->sc_status.dt = 0; */
752 
753 		if (usb2_fifo_alloc_buffer(fifo,
754 		    UMS_BUF_SIZE, UMS_IFQ_MAXLEN)) {
755 			return (ENOMEM);
756 		}
757 	}
758 	return (0);
759 }
760 
761 static void
762 ums_close(struct usb2_fifo *fifo, int fflags, struct thread *td)
763 {
764 	if (fflags & FREAD) {
765 		usb2_fifo_free_buffer(fifo);
766 	}
767 }
768 
769 static int
770 ums_ioctl(struct usb2_fifo *fifo, u_long cmd, void *addr,
771     int fflags, struct thread *td)
772 {
773 	struct ums_softc *sc = fifo->priv_sc0;
774 	mousemode_t mode;
775 	int error = 0;
776 
777 	DPRINTFN(2, "\n");
778 
779 	mtx_lock(&sc->sc_mtx);
780 
781 	switch (cmd) {
782 	case MOUSE_GETHWINFO:
783 		*(mousehw_t *)addr = sc->sc_hw;
784 		break;
785 
786 	case MOUSE_GETMODE:
787 		*(mousemode_t *)addr = sc->sc_mode;
788 		break;
789 
790 	case MOUSE_SETMODE:
791 		mode = *(mousemode_t *)addr;
792 
793 		if (mode.level == -1) {
794 			/* don't change the current setting */
795 		} else if ((mode.level < 0) || (mode.level > 1)) {
796 			error = EINVAL;
797 			goto done;
798 		} else {
799 			sc->sc_mode.level = mode.level;
800 		}
801 
802 		if (sc->sc_mode.level == 0) {
803 			if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
804 				sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
805 			else
806 				sc->sc_hw.buttons = sc->sc_buttons;
807 			sc->sc_mode.protocol = MOUSE_PROTO_MSC;
808 			sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
809 			sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
810 			sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
811 		} else if (sc->sc_mode.level == 1) {
812 			if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
813 				sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
814 			else
815 				sc->sc_hw.buttons = sc->sc_buttons;
816 			sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
817 			sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
818 			sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
819 			sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
820 		}
821 		ums_reset_buf(sc);
822 		break;
823 
824 	case MOUSE_GETLEVEL:
825 		*(int *)addr = sc->sc_mode.level;
826 		break;
827 
828 	case MOUSE_SETLEVEL:
829 		if (*(int *)addr < 0 || *(int *)addr > 1) {
830 			error = EINVAL;
831 			goto done;
832 		}
833 		sc->sc_mode.level = *(int *)addr;
834 
835 		if (sc->sc_mode.level == 0) {
836 			if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
837 				sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
838 			else
839 				sc->sc_hw.buttons = sc->sc_buttons;
840 			sc->sc_mode.protocol = MOUSE_PROTO_MSC;
841 			sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
842 			sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
843 			sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
844 		} else if (sc->sc_mode.level == 1) {
845 			if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
846 				sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
847 			else
848 				sc->sc_hw.buttons = sc->sc_buttons;
849 			sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
850 			sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
851 			sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
852 			sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
853 		}
854 		ums_reset_buf(sc);
855 		break;
856 
857 	case MOUSE_GETSTATUS:{
858 			mousestatus_t *status = (mousestatus_t *)addr;
859 
860 			*status = sc->sc_status;
861 			sc->sc_status.obutton = sc->sc_status.button;
862 			sc->sc_status.button = 0;
863 			sc->sc_status.dx = 0;
864 			sc->sc_status.dy = 0;
865 			sc->sc_status.dz = 0;
866 			/* sc->sc_status.dt = 0; */
867 
868 			if (status->dx || status->dy || status->dz /* || status->dt */ ) {
869 				status->flags |= MOUSE_POSCHANGED;
870 			}
871 			if (status->button != status->obutton) {
872 				status->flags |= MOUSE_BUTTONSCHANGED;
873 			}
874 			break;
875 		}
876 	default:
877 		error = ENOTTY;
878 	}
879 
880 done:
881 	mtx_unlock(&sc->sc_mtx);
882 	return (error);
883 }
884 
885 static devclass_t ums_devclass;
886 
887 static device_method_t ums_methods[] = {
888 	DEVMETHOD(device_probe, ums_probe),
889 	DEVMETHOD(device_attach, ums_attach),
890 	DEVMETHOD(device_detach, ums_detach),
891 	{0, 0}
892 };
893 
894 static driver_t ums_driver = {
895 	.name = "ums",
896 	.methods = ums_methods,
897 	.size = sizeof(struct ums_softc),
898 };
899 
900 DRIVER_MODULE(ums, ushub, ums_driver, ums_devclass, NULL, 0);
901 MODULE_DEPEND(ums, usb, 1, 1, 1);
902