xref: /dragonfly/sys/bus/u4b/input/ums.c (revision e98bdfd3)
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 
283 			DPRINTFN(6, "x:%d y:%d z:%d t:%d w:%d buttons:0x%08x\n",
284 			    dx, dy, dz, dt, dw, buttons);
285 
286 			/* translate T-axis into button presses until further */
287 			if (dt > 0)
288 				buttons |= 1UL << 3;
289 			else if (dt < 0)
290 				buttons |= 1UL << 4;
291 
292 			sc->sc_status.button = buttons;
293 			sc->sc_status.dx += dx;
294 			sc->sc_status.dy += dy;
295 			sc->sc_status.dz += dz;
296 			/*
297 			 * sc->sc_status.dt += dt;
298 			 * no way to export this yet
299 			 */
300 
301 			/*
302 		         * The Qtronix keyboard has a built in PS/2
303 		         * port for a mouse.  The firmware once in a
304 		         * while posts a spurious button up
305 		         * event. This event we ignore by doing a
306 		         * timeout for 50 msecs.  If we receive
307 		         * dx=dy=dz=buttons=0 before we add the event
308 		         * to the queue.  In any other case we delete
309 		         * the timeout event.
310 		         */
311 			if ((sc->sc_info[0].sc_flags & UMS_FLAG_SBU) &&
312 			    (dx == 0) && (dy == 0) && (dz == 0) && (dt == 0) &&
313 			    (dw == 0) && (buttons == 0)) {
314 
315 				usb_callout_reset(&sc->sc_callout, hz / 20,
316 				    &ums_put_queue_timeout, sc);
317 			} else {
318 
319 				usb_callout_stop(&sc->sc_callout);
320 
321 				ums_put_queue(sc, dx, dy, dz, dt, buttons);
322 			}
323 		}
324 	case USB_ST_SETUP:
325 tr_setup:
326 		/* check if we can put more data into the FIFO */
327 		if (usb_fifo_put_bytes_max(
328 		    sc->sc_fifo.fp[USB_FIFO_RX]) != 0) {
329 			usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
330 			usbd_transfer_submit(xfer);
331 		} else {
332 			/*
333 			 * When the FIFO becomes full we stop cycling the
334 			 * poll and must clear sc_read_running.  The next
335 			 * userland read/select/kqueue request will call
336 			 * f_start_read to get it going again.
337 			 */
338 			sc->sc_read_running = 0;
339 		}
340 		break;
341 
342 	default:			/* Error */
343 		if (error != USB_ERR_CANCELLED) {
344 			/* try clear stall first */
345 			usbd_xfer_set_stall(xfer);
346 			goto tr_setup;
347 		}
348 		break;
349 	}
350 }
351 
352 static const struct usb_config ums_config[UMS_N_TRANSFER] = {
353 
354 	[UMS_INTR_DT] = {
355 		.type = UE_INTERRUPT,
356 		.endpoint = UE_ADDR_ANY,
357 		.direction = UE_DIR_IN,
358 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
359 		.bufsize = 0,	/* use wMaxPacketSize */
360 		.callback = &ums_intr_callback,
361 	},
362 };
363 
364 /* A match on these entries will load ums */
365 static const STRUCT_USB_HOST_ID __used ums_devs[] = {
366 	{USB_IFACE_CLASS(UICLASS_HID),
367 	 USB_IFACE_SUBCLASS(UISUBCLASS_BOOT),
368 	 USB_IFACE_PROTOCOL(UIPROTO_MOUSE),},
369 };
370 
371 static int
372 ums_probe(device_t dev)
373 {
374 	struct usb_attach_arg *uaa = device_get_ivars(dev);
375 	void *d_ptr;
376 	int error;
377 	uint16_t d_len;
378 
379 	DPRINTFN(11, "\n");
380 
381 	if (uaa->usb_mode != USB_MODE_HOST)
382 		return (ENXIO);
383 
384 	if (uaa->info.bInterfaceClass != UICLASS_HID)
385 		return (ENXIO);
386 
387 	if (usb_test_quirk(uaa, UQ_UMS_IGNORE))
388 		return (ENXIO);
389 
390 	if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
391 	    (uaa->info.bInterfaceProtocol == UIPROTO_MOUSE))
392 		return (BUS_PROBE_DEFAULT);
393 
394 	error = usbd_req_get_hid_desc(uaa->device, NULL,
395 	    &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
396 
397 	if (error)
398 		return (ENXIO);
399 
400 	if (hid_is_mouse(d_ptr, d_len))
401 		error = BUS_PROBE_DEFAULT;
402 	else
403 		error = ENXIO;
404 
405 	kfree(d_ptr, M_TEMP);
406 	return (error);
407 }
408 
409 static void
410 ums_hid_parse(struct ums_softc *sc, device_t dev, const uint8_t *buf,
411     uint16_t len, uint8_t index)
412 {
413 	struct ums_info *info = &sc->sc_info[index];
414 	uint32_t flags;
415 	uint8_t i;
416 	uint8_t j;
417 
418 	if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
419 	    hid_input, index, &info->sc_loc_x, &flags, &info->sc_iid_x)) {
420 
421 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
422 			info->sc_flags |= UMS_FLAG_X_AXIS;
423 		}
424 	}
425 	if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
426 	    hid_input, index, &info->sc_loc_y, &flags, &info->sc_iid_y)) {
427 
428 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
429 			info->sc_flags |= UMS_FLAG_Y_AXIS;
430 		}
431 	}
432 	/* Try the wheel first as the Z activator since it's tradition. */
433 	if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
434 	    HUG_WHEEL), hid_input, index, &info->sc_loc_z, &flags,
435 	    &info->sc_iid_z) ||
436 	    hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
437 	    HUG_TWHEEL), hid_input, index, &info->sc_loc_z, &flags,
438 	    &info->sc_iid_z)) {
439 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
440 			info->sc_flags |= UMS_FLAG_Z_AXIS;
441 		}
442 		/*
443 		 * We might have both a wheel and Z direction, if so put
444 		 * put the Z on the W coordinate.
445 		 */
446 		if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
447 		    HUG_Z), hid_input, index, &info->sc_loc_w, &flags,
448 		    &info->sc_iid_w)) {
449 
450 			if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
451 				info->sc_flags |= UMS_FLAG_W_AXIS;
452 			}
453 		}
454 	} else if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
455 	    HUG_Z), hid_input, index, &info->sc_loc_z, &flags,
456 	    &info->sc_iid_z)) {
457 
458 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
459 			info->sc_flags |= UMS_FLAG_Z_AXIS;
460 		}
461 	}
462 	/*
463 	 * The Microsoft Wireless Intellimouse 2.0 reports it's wheel
464 	 * using 0x0048, which is HUG_TWHEEL, and seems to expect you
465 	 * to know that the byte after the wheel is the tilt axis.
466 	 * There are no other HID axis descriptors other than X,Y and
467 	 * TWHEEL
468 	 */
469 	if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
470 	    HUG_TWHEEL), hid_input, index, &info->sc_loc_t,
471 	    &flags, &info->sc_iid_t)) {
472 
473 		info->sc_loc_t.pos += 8;
474 
475 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
476 			info->sc_flags |= UMS_FLAG_T_AXIS;
477 		}
478 	} else if (hid_locate(buf, len, HID_USAGE2(HUP_CONSUMER,
479 		HUC_AC_PAN), hid_input, index, &info->sc_loc_t,
480 		&flags, &info->sc_iid_t)) {
481 
482 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS)
483 			info->sc_flags |= UMS_FLAG_T_AXIS;
484 	}
485 	/* figure out the number of buttons */
486 
487 	for (i = 0; i < UMS_BUTTON_MAX; i++) {
488 		if (!hid_locate(buf, len, HID_USAGE2(HUP_BUTTON, (i + 1)),
489 		    hid_input, index, &info->sc_loc_btn[i], NULL,
490 		    &info->sc_iid_btn[i])) {
491 			break;
492 		}
493 	}
494 
495 	/* detect other buttons */
496 
497 	for (j = 0; (i < UMS_BUTTON_MAX) && (j < 2); i++, j++) {
498 		if (!hid_locate(buf, len, HID_USAGE2(HUP_MICROSOFT, (j + 1)),
499 		    hid_input, index, &info->sc_loc_btn[i], NULL,
500 		    &info->sc_iid_btn[i])) {
501 			break;
502 		}
503 	}
504 
505 	info->sc_buttons = i;
506 
507 	if (i > sc->sc_buttons)
508 		sc->sc_buttons = i;
509 
510 	if (info->sc_flags == 0)
511 		return;
512 
513 	/* announce information about the mouse */
514 	device_printf(dev, "%d buttons and [%s%s%s%s%s] coordinates ID=%u\n",
515 	    (info->sc_buttons),
516 	    (info->sc_flags & UMS_FLAG_X_AXIS) ? "X" : "",
517 	    (info->sc_flags & UMS_FLAG_Y_AXIS) ? "Y" : "",
518 	    (info->sc_flags & UMS_FLAG_Z_AXIS) ? "Z" : "",
519 	    (info->sc_flags & UMS_FLAG_T_AXIS) ? "T" : "",
520 	    (info->sc_flags & UMS_FLAG_W_AXIS) ? "W" : "",
521 	    info->sc_iid_x);
522 }
523 
524 static int
525 ums_attach(device_t dev)
526 {
527 	struct usb_attach_arg *uaa = device_get_ivars(dev);
528 	struct ums_softc *sc = device_get_softc(dev);
529 	struct ums_info *info;
530 	void *d_ptr = NULL;
531 	int isize;
532 	int err;
533 	uint16_t d_len;
534 	uint8_t i;
535 #ifdef USB_DEBUG
536 	uint8_t j;
537 #endif
538 
539 	DPRINTFN(11, "sc=%p\n", sc);
540 
541 	device_set_usb_desc(dev);
542 
543 	lockinit(&sc->sc_lock, "ums lock", 0, LK_CANRECURSE);
544 
545 	usb_callout_init_mtx(&sc->sc_callout, &sc->sc_lock, 0);
546 
547 	/*
548          * Force the report (non-boot) protocol.
549          *
550          * Mice without boot protocol support may choose not to implement
551          * Set_Protocol at all; Ignore any error.
552          */
553 	err = usbd_req_set_protocol(uaa->device, NULL,
554 	    uaa->info.bIfaceIndex, 1);
555 
556 	err = usbd_transfer_setup(uaa->device,
557 	    &uaa->info.bIfaceIndex, sc->sc_xfer, ums_config,
558 	    UMS_N_TRANSFER, sc, &sc->sc_lock);
559 
560 	if (err) {
561 		DPRINTF("error=%s\n", usbd_errstr(err));
562 		goto detach;
563 	}
564 
565 	/* Get HID descriptor */
566 	err = usbd_req_get_hid_desc(uaa->device, NULL, &d_ptr,
567 	    &d_len, M_TEMP, uaa->info.bIfaceIndex);
568 
569 	if (err) {
570 		device_printf(dev, "error reading report description\n");
571 		goto detach;
572 	}
573 
574 	isize = hid_report_size(d_ptr, d_len, hid_input, &sc->sc_iid);
575 
576 	/*
577 	 * The Microsoft Wireless Notebook Optical Mouse seems to be in worse
578 	 * shape than the Wireless Intellimouse 2.0, as its X, Y, wheel, and
579 	 * all of its other button positions are all off. It also reports that
580 	 * it has two addional buttons and a tilt wheel.
581 	 */
582 	if (usb_test_quirk(uaa, UQ_MS_BAD_CLASS)) {
583 
584 		sc->sc_iid = 0;
585 
586 		info = &sc->sc_info[0];
587 		info->sc_flags = (UMS_FLAG_X_AXIS |
588 		    UMS_FLAG_Y_AXIS |
589 		    UMS_FLAG_Z_AXIS |
590 		    UMS_FLAG_SBU);
591 		info->sc_buttons = 3;
592 		isize = 5;
593 		/* 1st byte of descriptor report contains garbage */
594 		info->sc_loc_x.pos = 16;
595 		info->sc_loc_x.size = 8;
596 		info->sc_loc_y.pos = 24;
597 		info->sc_loc_y.size = 8;
598 		info->sc_loc_z.pos = 32;
599 		info->sc_loc_z.size = 8;
600 		info->sc_loc_btn[0].pos = 8;
601 		info->sc_loc_btn[0].size = 1;
602 		info->sc_loc_btn[1].pos = 9;
603 		info->sc_loc_btn[1].size = 1;
604 		info->sc_loc_btn[2].pos = 10;
605 		info->sc_loc_btn[2].size = 1;
606 
607 		/* Announce device */
608 		device_printf(dev, "3 buttons and [XYZ] "
609 		    "coordinates ID=0\n");
610 
611 	} else {
612 		/* Search the HID descriptor and announce device */
613 		for (i = 0; i < UMS_INFO_MAX; i++) {
614 			ums_hid_parse(sc, dev, d_ptr, d_len, i);
615 		}
616 	}
617 
618 	if (usb_test_quirk(uaa, UQ_MS_REVZ)) {
619 		info = &sc->sc_info[0];
620 		/* Some wheels need the Z axis reversed. */
621 		info->sc_flags |= UMS_FLAG_REVZ;
622 	}
623 	if (isize > (int)usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT])) {
624 		DPRINTF("WARNING: report size, %d bytes, is larger "
625 		    "than interrupt size, %d bytes!\n", isize,
626 		    usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT]));
627 	}
628 	kfree(d_ptr, M_TEMP);
629 	d_ptr = NULL;
630 
631 #ifdef USB_DEBUG
632 	for (j = 0; j < UMS_INFO_MAX; j++) {
633 		info = &sc->sc_info[j];
634 
635 		DPRINTF("sc=%p, index=%d\n", sc, j);
636 		DPRINTF("X\t%d/%d id=%d\n", info->sc_loc_x.pos,
637 		    info->sc_loc_x.size, info->sc_iid_x);
638 		DPRINTF("Y\t%d/%d id=%d\n", info->sc_loc_y.pos,
639 		    info->sc_loc_y.size, info->sc_iid_y);
640 		DPRINTF("Z\t%d/%d id=%d\n", info->sc_loc_z.pos,
641 		    info->sc_loc_z.size, info->sc_iid_z);
642 		DPRINTF("T\t%d/%d id=%d\n", info->sc_loc_t.pos,
643 		    info->sc_loc_t.size, info->sc_iid_t);
644 		DPRINTF("W\t%d/%d id=%d\n", info->sc_loc_w.pos,
645 		    info->sc_loc_w.size, info->sc_iid_w);
646 
647 		for (i = 0; i < info->sc_buttons; i++) {
648 			DPRINTF("B%d\t%d/%d id=%d\n",
649 			    i + 1, info->sc_loc_btn[i].pos,
650 			    info->sc_loc_btn[i].size, info->sc_iid_btn[i]);
651 		}
652 	}
653 	DPRINTF("size=%d, id=%d\n", isize, sc->sc_iid);
654 #endif
655 
656 	err = usb_fifo_attach(uaa->device, sc, &sc->sc_lock,
657 	    &ums_fifo_methods, &sc->sc_fifo,
658 	    device_get_unit(dev), -1, uaa->info.bIfaceIndex,
659   	    UID_ROOT, GID_OPERATOR, 0644);
660 	if (err)
661 		goto detach;
662 #if 0
663 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
664 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
665 	    OID_AUTO, "parseinfo", CTLTYPE_STRING|CTLFLAG_RD,
666 	    sc, 0, ums_sysctl_handler_parseinfo,
667 	    "", "Dump of parsed HID report descriptor");
668 #endif
669 
670 	return (0);
671 
672 detach:
673 	if (d_ptr) {
674 		kfree(d_ptr, M_TEMP);
675 	}
676 	ums_detach(dev);
677 	return (ENOMEM);
678 }
679 
680 static int
681 ums_detach(device_t self)
682 {
683 	struct ums_softc *sc = device_get_softc(self);
684 
685 	DPRINTF("sc=%p\n", sc);
686 
687 	usb_fifo_detach(&sc->sc_fifo);
688 
689 	usbd_transfer_unsetup(sc->sc_xfer, UMS_N_TRANSFER);
690 
691 	usb_callout_drain(&sc->sc_callout);
692 
693 	lockuninit(&sc->sc_lock);
694 
695 	return (0);
696 }
697 
698 static void
699 ums_start_read(struct usb_fifo *fifo)
700 {
701 	struct ums_softc *sc = usb_fifo_softc(fifo);
702 	int rate;
703 
704 	/* Check if we should override the default polling interval */
705 	rate = sc->sc_pollrate;
706 	/* Range check rate */
707 	if (rate > 1000)
708 		rate = 1000;
709 	/* Check for set rate */
710 	if ((rate > 0) && (sc->sc_xfer[UMS_INTR_DT] != NULL)) {
711 		DPRINTF("Setting pollrate = %d\n", rate);
712 		/* Stop current transfer, if any */
713 		if (sc->sc_read_running) {
714 			usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
715 			sc->sc_read_running = 0;
716 		}
717 		/* Set new interval */
718 		usbd_xfer_set_interval(sc->sc_xfer[UMS_INTR_DT], 1000 / rate);
719 		/* Only set pollrate once */
720 		sc->sc_pollrate = 0;
721 	}
722 	if (sc->sc_read_running == 0) {
723 		sc->sc_read_running = 1;
724 		usbd_transfer_start(sc->sc_xfer[UMS_INTR_DT]);
725 	}
726 }
727 
728 static void
729 ums_stop_read(struct usb_fifo *fifo)
730 {
731 	struct ums_softc *sc = usb_fifo_softc(fifo);
732 
733 	if (sc->sc_read_running) {
734 		usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
735 		sc->sc_read_running = 0;
736 		usb_callout_stop(&sc->sc_callout);
737 	}
738 }
739 
740 
741 #if ((MOUSE_SYS_PACKETSIZE != 8) || \
742      (MOUSE_MSC_PACKETSIZE != 5))
743 #error "Software assumptions are not met. Please update code."
744 #endif
745 
746 static void
747 ums_put_queue(struct ums_softc *sc, int32_t dx, int32_t dy,
748     int32_t dz, int32_t dt, int32_t buttons)
749 {
750 	uint8_t buf[8];
751 
752 	if (1) {
753 
754 		if (dx > 254)
755 			dx = 254;
756 		if (dx < -256)
757 			dx = -256;
758 		if (dy > 254)
759 			dy = 254;
760 		if (dy < -256)
761 			dy = -256;
762 		if (dz > 126)
763 			dz = 126;
764 		if (dz < -128)
765 			dz = -128;
766 		if (dt > 126)
767 			dt = 126;
768 		if (dt < -128)
769 			dt = -128;
770 
771 		buf[0] = sc->sc_mode.syncmask[1];
772 		buf[0] |= (~buttons) & MOUSE_MSC_BUTTONS;
773 		buf[1] = dx >> 1;
774 		buf[2] = dy >> 1;
775 		buf[3] = dx - (dx >> 1);
776 		buf[4] = dy - (dy >> 1);
777 
778 		if (sc->sc_mode.level == 1) {
779 			buf[5] = dz >> 1;
780 			buf[6] = dz - (dz >> 1);
781 			buf[7] = (((~buttons) >> 3) & MOUSE_SYS_EXTBUTTONS);
782 		}
783 		usb_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf,
784 		    sc->sc_mode.packetsize, 1);
785 
786 	} else {
787 		DPRINTF("Buffer full, discarded packet\n");
788 	}
789 }
790 
791 static void
792 ums_reset_buf(struct ums_softc *sc)
793 {
794 	/* reset read queue, must be called locked */
795 	usb_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]);
796 }
797 
798 static int
799 ums_open(struct usb_fifo *fifo, int fflags)
800 {
801 	struct ums_softc *sc = usb_fifo_softc(fifo);
802 
803 	DPRINTFN(2, "\n");
804 
805 	/* check for duplicate open, should not happen */
806 	if (sc->sc_fflags & fflags)
807 		return (EBUSY);
808 
809 	/* check for first open */
810 	if (sc->sc_fflags == 0) {
811 
812 		/* reset all USB mouse parameters */
813 
814 		if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
815 			sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
816 		else
817 			sc->sc_hw.buttons = sc->sc_buttons;
818 
819 		sc->sc_hw.iftype = MOUSE_IF_USB;
820 		sc->sc_hw.type = MOUSE_MOUSE;
821 		sc->sc_hw.model = MOUSE_MODEL_GENERIC;
822 		sc->sc_hw.hwid = 0;
823 
824 		sc->sc_mode.protocol = MOUSE_PROTO_MSC;
825 		sc->sc_mode.rate = -1;
826 		sc->sc_mode.resolution = MOUSE_RES_UNKNOWN;
827 		sc->sc_mode.accelfactor = 0;
828 		sc->sc_mode.level = 0;
829 		sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
830 		sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
831 		sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
832 
833 		/* reset status */
834 
835 		sc->sc_status.flags = 0;
836 		sc->sc_status.button = 0;
837 		sc->sc_status.obutton = 0;
838 		sc->sc_status.dx = 0;
839 		sc->sc_status.dy = 0;
840 		sc->sc_status.dz = 0;
841 		/* sc->sc_status.dt = 0; */
842 	}
843 
844 	if (fflags & FREAD) {
845 		/* allocate RX buffer */
846 		if (usb_fifo_alloc_buffer(fifo,
847 		    UMS_BUF_SIZE, UMS_IFQ_MAXLEN)) {
848 			return (ENOMEM);
849 		}
850 	}
851 
852 	sc->sc_fflags |= fflags & (FREAD | FWRITE);
853 	return (0);
854 }
855 
856 static void
857 ums_close(struct usb_fifo *fifo, int fflags)
858 {
859 	struct ums_softc *sc = usb_fifo_softc(fifo);
860 
861 	DPRINTFN(2, "\n");
862 
863 	if (fflags & FREAD)
864 		usb_fifo_free_buffer(fifo);
865 
866 	sc->sc_fflags &= ~(fflags & (FREAD | FWRITE));
867 }
868 
869 static int
870 ums_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
871 {
872 	struct ums_softc *sc = usb_fifo_softc(fifo);
873 	mousemode_t mode;
874 	int error = 0;
875 
876 	DPRINTFN(2, "\n");
877 
878 	lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
879 
880 	switch (cmd) {
881 	case MOUSE_GETHWINFO:
882 		*(mousehw_t *)addr = sc->sc_hw;
883 		break;
884 
885 	case MOUSE_GETMODE:
886 		*(mousemode_t *)addr = sc->sc_mode;
887 		break;
888 
889 	case MOUSE_SETMODE:
890 		mode = *(mousemode_t *)addr;
891 
892 		if (mode.level == -1) {
893 			/* don't change the current setting */
894 		} else if ((mode.level < 0) || (mode.level > 1)) {
895 			error = EINVAL;
896 			break;
897 		} else {
898 			sc->sc_mode.level = mode.level;
899 		}
900 
901 		/* store polling rate */
902 		sc->sc_pollrate = mode.rate;
903 
904 		if (sc->sc_mode.level == 0) {
905 			if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
906 				sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
907 			else
908 				sc->sc_hw.buttons = sc->sc_buttons;
909 			sc->sc_mode.protocol = MOUSE_PROTO_MSC;
910 			sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
911 			sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
912 			sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
913 		} else if (sc->sc_mode.level == 1) {
914 			if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
915 				sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
916 			else
917 				sc->sc_hw.buttons = sc->sc_buttons;
918 			sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
919 			sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
920 			sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
921 			sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
922 		}
923 		ums_reset_buf(sc);
924 		break;
925 
926 	case MOUSE_GETLEVEL:
927 		*(int *)addr = sc->sc_mode.level;
928 		break;
929 
930 	case MOUSE_SETLEVEL:
931 		if (*(int *)addr < 0 || *(int *)addr > 1) {
932 			error = EINVAL;
933 			break;
934 		}
935 		sc->sc_mode.level = *(int *)addr;
936 
937 		if (sc->sc_mode.level == 0) {
938 			if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
939 				sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
940 			else
941 				sc->sc_hw.buttons = sc->sc_buttons;
942 			sc->sc_mode.protocol = MOUSE_PROTO_MSC;
943 			sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
944 			sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
945 			sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
946 		} else if (sc->sc_mode.level == 1) {
947 			if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
948 				sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
949 			else
950 				sc->sc_hw.buttons = sc->sc_buttons;
951 			sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
952 			sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
953 			sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
954 			sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
955 		}
956 		ums_reset_buf(sc);
957 		break;
958 
959 	case MOUSE_GETSTATUS:{
960 			mousestatus_t *status = (mousestatus_t *)addr;
961 
962 			*status = sc->sc_status;
963 			sc->sc_status.obutton = sc->sc_status.button;
964 			sc->sc_status.button = 0;
965 			sc->sc_status.dx = 0;
966 			sc->sc_status.dy = 0;
967 			sc->sc_status.dz = 0;
968 			/* sc->sc_status.dt = 0; */
969 
970 			if (status->dx || status->dy || status->dz /* || status->dt */ ) {
971 				status->flags |= MOUSE_POSCHANGED;
972 			}
973 			if (status->button != status->obutton) {
974 				status->flags |= MOUSE_BUTTONSCHANGED;
975 			}
976 			break;
977 		}
978 	default:
979 		error = ENOTTY;
980 		break;
981 	}
982 
983 	lockmgr(&sc->sc_lock, LK_RELEASE);
984 	return (error);
985 }
986 
987 #if 0 /* XXX */
988 static int
989 ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS)
990 {
991 	struct ums_softc *sc = arg1;
992 	struct ums_info *info;
993 	struct sbuf *sb;
994 	int i, j, err, had_output;
995 
996 	sb = sbuf_new_auto();
997 	for (i = 0, had_output = 0; i < UMS_INFO_MAX; i++) {
998 		info = &sc->sc_info[i];
999 
1000 		/* Don't emit empty info */
1001 		if ((info->sc_flags &
1002 		    (UMS_FLAG_X_AXIS | UMS_FLAG_Y_AXIS | UMS_FLAG_Z_AXIS |
1003 		     UMS_FLAG_T_AXIS | UMS_FLAG_W_AXIS)) == 0 &&
1004 		    info->sc_buttons == 0)
1005 			continue;
1006 
1007 		if (had_output)
1008 			sbuf_printf(sb, "\n");
1009 		had_output = 1;
1010 		sbuf_printf(sb, "i%d:", i + 1);
1011 		if (info->sc_flags & UMS_FLAG_X_AXIS)
1012 			sbuf_printf(sb, " X:r%d, p%d, s%d;",
1013 			    (int)info->sc_iid_x,
1014 			    (int)info->sc_loc_x.pos,
1015 			    (int)info->sc_loc_x.size);
1016 		if (info->sc_flags & UMS_FLAG_Y_AXIS)
1017 			sbuf_printf(sb, " Y:r%d, p%d, s%d;",
1018 			    (int)info->sc_iid_y,
1019 			    (int)info->sc_loc_y.pos,
1020 			    (int)info->sc_loc_y.size);
1021 		if (info->sc_flags & UMS_FLAG_Z_AXIS)
1022 			sbuf_printf(sb, " Z:r%d, p%d, s%d;",
1023 			    (int)info->sc_iid_z,
1024 			    (int)info->sc_loc_z.pos,
1025 			    (int)info->sc_loc_z.size);
1026 		if (info->sc_flags & UMS_FLAG_T_AXIS)
1027 			sbuf_printf(sb, " T:r%d, p%d, s%d;",
1028 			    (int)info->sc_iid_t,
1029 			    (int)info->sc_loc_t.pos,
1030 			    (int)info->sc_loc_t.size);
1031 		if (info->sc_flags & UMS_FLAG_W_AXIS)
1032 			sbuf_printf(sb, " W:r%d, p%d, s%d;",
1033 			    (int)info->sc_iid_w,
1034 			    (int)info->sc_loc_w.pos,
1035 			    (int)info->sc_loc_w.size);
1036 
1037 		for (j = 0; j < info->sc_buttons; j++) {
1038 			sbuf_printf(sb, " B%d:r%d, p%d, s%d;", j + 1,
1039 			    (int)info->sc_iid_btn[j],
1040 			    (int)info->sc_loc_btn[j].pos,
1041 			    (int)info->sc_loc_btn[j].size);
1042 		}
1043 	}
1044 	sbuf_finish(sb);
1045 	err = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
1046 	sbuf_delete(sb);
1047 
1048 	return (err);
1049 }
1050 #endif
1051 
1052 static devclass_t ums_devclass;
1053 
1054 static device_method_t ums_methods[] = {
1055 	DEVMETHOD(device_probe, ums_probe),
1056 	DEVMETHOD(device_attach, ums_attach),
1057 	DEVMETHOD(device_detach, ums_detach),
1058 	DEVMETHOD_END
1059 };
1060 
1061 static driver_t ums_driver = {
1062 	.name = "ums",
1063 	.methods = ums_methods,
1064 	.size = sizeof(struct ums_softc),
1065 };
1066 
1067 DRIVER_MODULE(ums, uhub, ums_driver, ums_devclass, NULL, NULL);
1068 MODULE_DEPEND(ums, usb, 1, 1, 1);
1069 MODULE_VERSION(ums, 1);
1070