xref: /dragonfly/sys/bus/u4b/input/ums.c (revision a668c4cd)
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 
133 	uint8_t	sc_buttons;
134 	uint8_t	sc_iid;
135 	uint8_t	sc_temp[64];
136 };
137 
138 static void ums_put_queue_timeout(void *__sc);
139 
140 static usb_callback_t ums_intr_callback;
141 
142 static device_probe_t ums_probe;
143 static device_attach_t ums_attach;
144 static device_detach_t ums_detach;
145 
146 static usb_fifo_cmd_t ums_start_read;
147 static usb_fifo_cmd_t ums_stop_read;
148 static usb_fifo_open_t ums_open;
149 static usb_fifo_close_t ums_close;
150 static usb_fifo_ioctl_t ums_ioctl;
151 
152 static void	ums_put_queue(struct ums_softc *, int32_t, int32_t,
153 		    int32_t, int32_t, int32_t);
154 #if 0 /* XXX */
155 static int	ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS);
156 #endif
157 
158 static struct usb_fifo_methods ums_fifo_methods = {
159 	.f_open = &ums_open,
160 	.f_close = &ums_close,
161 	.f_ioctl = &ums_ioctl,
162 	.f_start_read = &ums_start_read,
163 	.f_stop_read = &ums_stop_read,
164 	.basename[0] = "ums",
165 };
166 
167 static void
168 ums_put_queue_timeout(void *__sc)
169 {
170 	struct ums_softc *sc = __sc;
171 
172 	KKASSERT(lockowned(&sc->sc_lock));
173 
174 	ums_put_queue(sc, 0, 0, 0, 0, 0);
175 }
176 
177 static void
178 ums_intr_callback(struct usb_xfer *xfer, usb_error_t error)
179 {
180 	struct ums_softc *sc = usbd_xfer_softc(xfer);
181 	struct ums_info *info = &sc->sc_info[0];
182 	struct usb_page_cache *pc;
183 	uint8_t *buf = sc->sc_temp;
184 	int32_t buttons = 0;
185 	int32_t buttons_found = 0;
186 	int32_t dw = 0;
187 	int32_t dx = 0;
188 	int32_t dy = 0;
189 	int32_t dz = 0;
190 	int32_t dt = 0;
191 	uint8_t i;
192 	uint8_t id;
193 	int len;
194 
195 	usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
196 
197 	switch (USB_GET_STATE(xfer)) {
198 	case USB_ST_TRANSFERRED:
199 		DPRINTFN(6, "sc=%p actlen=%d\n", sc, len);
200 
201 		if (len > (int)sizeof(sc->sc_temp)) {
202 			DPRINTFN(6, "truncating large packet to %zu bytes\n",
203 			    sizeof(sc->sc_temp));
204 			len = sizeof(sc->sc_temp);
205 		}
206 		if (len == 0)
207 			goto tr_setup;
208 
209 		pc = usbd_xfer_get_frame(xfer, 0);
210 		usbd_copy_out(pc, 0, buf, len);
211 
212 		DPRINTFN(6, "data = %02x %02x %02x %02x "
213 		    "%02x %02x %02x %02x\n",
214 		    (len > 0) ? buf[0] : 0, (len > 1) ? buf[1] : 0,
215 		    (len > 2) ? buf[2] : 0, (len > 3) ? buf[3] : 0,
216 		    (len > 4) ? buf[4] : 0, (len > 5) ? buf[5] : 0,
217 		    (len > 6) ? buf[6] : 0, (len > 7) ? buf[7] : 0);
218 
219 		if (sc->sc_iid) {
220 			id = *buf;
221 
222 			len--;
223 			buf++;
224 
225 		} else {
226 			id = 0;
227 			if (sc->sc_info[0].sc_flags & UMS_FLAG_SBU) {
228 				if ((*buf == 0x14) || (*buf == 0x15)) {
229 					goto tr_setup;
230 				}
231 			}
232 		}
233 
234 	repeat:
235 		if ((info->sc_flags & UMS_FLAG_W_AXIS) &&
236 		    (id == info->sc_iid_w))
237 			dw += hid_get_data(buf, len, &info->sc_loc_w);
238 
239 		if ((info->sc_flags & UMS_FLAG_X_AXIS) &&
240 		    (id == info->sc_iid_x))
241 			dx += hid_get_data(buf, len, &info->sc_loc_x);
242 
243 		if ((info->sc_flags & UMS_FLAG_Y_AXIS) &&
244 		    (id == info->sc_iid_y))
245 			dy = -hid_get_data(buf, len, &info->sc_loc_y);
246 
247 		if ((info->sc_flags & UMS_FLAG_Z_AXIS) &&
248 		    (id == info->sc_iid_z)) {
249 			int32_t temp;
250 			temp = hid_get_data(buf, len, &info->sc_loc_z);
251 			if (info->sc_flags & UMS_FLAG_REVZ)
252 				temp = -temp;
253 			dz -= temp;
254 		}
255 
256 		if ((info->sc_flags & UMS_FLAG_T_AXIS) &&
257 		    (id == info->sc_iid_t))
258 			dt -= hid_get_data(buf, len, &info->sc_loc_t);
259 
260 		for (i = 0; i < info->sc_buttons; i++) {
261 			uint32_t mask;
262 			mask = 1UL << UMS_BUT(i);
263 			/* check for correct button ID */
264 			if (id != info->sc_iid_btn[i])
265 				continue;
266 			/* check for button pressed */
267 			if (hid_get_data(buf, len, &info->sc_loc_btn[i]))
268 				buttons |= mask;
269 			/* register button mask */
270 			buttons_found |= mask;
271 		}
272 
273 		if (++info != &sc->sc_info[UMS_INFO_MAX])
274 			goto repeat;
275 
276 		/* keep old button value(s) for non-detected buttons */
277 		buttons |= sc->sc_status.button & ~buttons_found;
278 
279 		if (dx || dy || dz || dt || dw ||
280 		    (buttons != sc->sc_status.button)) {
281 
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 		usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
705 		/* Set new interval */
706 		usbd_xfer_set_interval(sc->sc_xfer[UMS_INTR_DT], 1000 / rate);
707 		/* Only set pollrate once */
708 		sc->sc_pollrate = 0;
709 	}
710 
711 	usbd_transfer_start(sc->sc_xfer[UMS_INTR_DT]);
712 }
713 
714 static void
715 ums_stop_read(struct usb_fifo *fifo)
716 {
717 	struct ums_softc *sc = usb_fifo_softc(fifo);
718 
719 	usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
720 	usb_callout_stop(&sc->sc_callout);
721 }
722 
723 
724 #if ((MOUSE_SYS_PACKETSIZE != 8) || \
725      (MOUSE_MSC_PACKETSIZE != 5))
726 #error "Software assumptions are not met. Please update code."
727 #endif
728 
729 static void
730 ums_put_queue(struct ums_softc *sc, int32_t dx, int32_t dy,
731     int32_t dz, int32_t dt, int32_t buttons)
732 {
733 	uint8_t buf[8];
734 
735 	if (1) {
736 
737 		if (dx > 254)
738 			dx = 254;
739 		if (dx < -256)
740 			dx = -256;
741 		if (dy > 254)
742 			dy = 254;
743 		if (dy < -256)
744 			dy = -256;
745 		if (dz > 126)
746 			dz = 126;
747 		if (dz < -128)
748 			dz = -128;
749 		if (dt > 126)
750 			dt = 126;
751 		if (dt < -128)
752 			dt = -128;
753 
754 		buf[0] = sc->sc_mode.syncmask[1];
755 		buf[0] |= (~buttons) & MOUSE_MSC_BUTTONS;
756 		buf[1] = dx >> 1;
757 		buf[2] = dy >> 1;
758 		buf[3] = dx - (dx >> 1);
759 		buf[4] = dy - (dy >> 1);
760 
761 		if (sc->sc_mode.level == 1) {
762 			buf[5] = dz >> 1;
763 			buf[6] = dz - (dz >> 1);
764 			buf[7] = (((~buttons) >> 3) & MOUSE_SYS_EXTBUTTONS);
765 		}
766 		usb_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf,
767 		    sc->sc_mode.packetsize, 1);
768 
769 	} else {
770 		DPRINTF("Buffer full, discarded packet\n");
771 	}
772 }
773 
774 static void
775 ums_reset_buf(struct ums_softc *sc)
776 {
777 	/* reset read queue, must be called locked */
778 	usb_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]);
779 }
780 
781 static int
782 ums_open(struct usb_fifo *fifo, int fflags)
783 {
784 	struct ums_softc *sc = usb_fifo_softc(fifo);
785 
786 	DPRINTFN(2, "\n");
787 
788 	/* check for duplicate open, should not happen */
789 	if (sc->sc_fflags & fflags)
790 		return (EBUSY);
791 
792 	/* check for first open */
793 	if (sc->sc_fflags == 0) {
794 
795 		/* reset all USB mouse parameters */
796 
797 		if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
798 			sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
799 		else
800 			sc->sc_hw.buttons = sc->sc_buttons;
801 
802 		sc->sc_hw.iftype = MOUSE_IF_USB;
803 		sc->sc_hw.type = MOUSE_MOUSE;
804 		sc->sc_hw.model = MOUSE_MODEL_GENERIC;
805 		sc->sc_hw.hwid = 0;
806 
807 		sc->sc_mode.protocol = MOUSE_PROTO_MSC;
808 		sc->sc_mode.rate = -1;
809 		sc->sc_mode.resolution = MOUSE_RES_UNKNOWN;
810 		sc->sc_mode.accelfactor = 0;
811 		sc->sc_mode.level = 0;
812 		sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
813 		sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
814 		sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
815 
816 		/* reset status */
817 
818 		sc->sc_status.flags = 0;
819 		sc->sc_status.button = 0;
820 		sc->sc_status.obutton = 0;
821 		sc->sc_status.dx = 0;
822 		sc->sc_status.dy = 0;
823 		sc->sc_status.dz = 0;
824 		/* sc->sc_status.dt = 0; */
825 	}
826 
827 	if (fflags & FREAD) {
828 		/* allocate RX buffer */
829 		if (usb_fifo_alloc_buffer(fifo,
830 		    UMS_BUF_SIZE, UMS_IFQ_MAXLEN)) {
831 			return (ENOMEM);
832 		}
833 	}
834 
835 	sc->sc_fflags |= fflags & (FREAD | FWRITE);
836 	return (0);
837 }
838 
839 static void
840 ums_close(struct usb_fifo *fifo, int fflags)
841 {
842 	struct ums_softc *sc = usb_fifo_softc(fifo);
843 
844 	DPRINTFN(2, "\n");
845 
846 	if (fflags & FREAD)
847 		usb_fifo_free_buffer(fifo);
848 
849 	sc->sc_fflags &= ~(fflags & (FREAD | FWRITE));
850 }
851 
852 static int
853 ums_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
854 {
855 	struct ums_softc *sc = usb_fifo_softc(fifo);
856 	mousemode_t mode;
857 	int error = 0;
858 
859 	DPRINTFN(2, "\n");
860 
861 	lockmgr(&sc->sc_lock, LK_EXCLUSIVE);
862 
863 	switch (cmd) {
864 	case MOUSE_GETHWINFO:
865 		*(mousehw_t *)addr = sc->sc_hw;
866 		break;
867 
868 	case MOUSE_GETMODE:
869 		*(mousemode_t *)addr = sc->sc_mode;
870 		break;
871 
872 	case MOUSE_SETMODE:
873 		mode = *(mousemode_t *)addr;
874 
875 		if (mode.level == -1) {
876 			/* don't change the current setting */
877 		} else if ((mode.level < 0) || (mode.level > 1)) {
878 			error = EINVAL;
879 			break;
880 		} else {
881 			sc->sc_mode.level = mode.level;
882 		}
883 
884 		/* store polling rate */
885 		sc->sc_pollrate = mode.rate;
886 
887 		if (sc->sc_mode.level == 0) {
888 			if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
889 				sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
890 			else
891 				sc->sc_hw.buttons = sc->sc_buttons;
892 			sc->sc_mode.protocol = MOUSE_PROTO_MSC;
893 			sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
894 			sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
895 			sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
896 		} else if (sc->sc_mode.level == 1) {
897 			if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
898 				sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
899 			else
900 				sc->sc_hw.buttons = sc->sc_buttons;
901 			sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
902 			sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
903 			sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
904 			sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
905 		}
906 		ums_reset_buf(sc);
907 		break;
908 
909 	case MOUSE_GETLEVEL:
910 		*(int *)addr = sc->sc_mode.level;
911 		break;
912 
913 	case MOUSE_SETLEVEL:
914 		if (*(int *)addr < 0 || *(int *)addr > 1) {
915 			error = EINVAL;
916 			break;
917 		}
918 		sc->sc_mode.level = *(int *)addr;
919 
920 		if (sc->sc_mode.level == 0) {
921 			if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
922 				sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
923 			else
924 				sc->sc_hw.buttons = sc->sc_buttons;
925 			sc->sc_mode.protocol = MOUSE_PROTO_MSC;
926 			sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
927 			sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
928 			sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
929 		} else if (sc->sc_mode.level == 1) {
930 			if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
931 				sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
932 			else
933 				sc->sc_hw.buttons = sc->sc_buttons;
934 			sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
935 			sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
936 			sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
937 			sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
938 		}
939 		ums_reset_buf(sc);
940 		break;
941 
942 	case MOUSE_GETSTATUS:{
943 			mousestatus_t *status = (mousestatus_t *)addr;
944 
945 			*status = sc->sc_status;
946 			sc->sc_status.obutton = sc->sc_status.button;
947 			sc->sc_status.button = 0;
948 			sc->sc_status.dx = 0;
949 			sc->sc_status.dy = 0;
950 			sc->sc_status.dz = 0;
951 			/* sc->sc_status.dt = 0; */
952 
953 			if (status->dx || status->dy || status->dz /* || status->dt */ ) {
954 				status->flags |= MOUSE_POSCHANGED;
955 			}
956 			if (status->button != status->obutton) {
957 				status->flags |= MOUSE_BUTTONSCHANGED;
958 			}
959 			break;
960 		}
961 	default:
962 		error = ENOTTY;
963 		break;
964 	}
965 
966 	lockmgr(&sc->sc_lock, LK_RELEASE);
967 	return (error);
968 }
969 
970 #if 0 /* XXX */
971 static int
972 ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS)
973 {
974 	struct ums_softc *sc = arg1;
975 	struct ums_info *info;
976 	struct sbuf *sb;
977 	int i, j, err, had_output;
978 
979 	sb = sbuf_new_auto();
980 	for (i = 0, had_output = 0; i < UMS_INFO_MAX; i++) {
981 		info = &sc->sc_info[i];
982 
983 		/* Don't emit empty info */
984 		if ((info->sc_flags &
985 		    (UMS_FLAG_X_AXIS | UMS_FLAG_Y_AXIS | UMS_FLAG_Z_AXIS |
986 		     UMS_FLAG_T_AXIS | UMS_FLAG_W_AXIS)) == 0 &&
987 		    info->sc_buttons == 0)
988 			continue;
989 
990 		if (had_output)
991 			sbuf_printf(sb, "\n");
992 		had_output = 1;
993 		sbuf_printf(sb, "i%d:", i + 1);
994 		if (info->sc_flags & UMS_FLAG_X_AXIS)
995 			sbuf_printf(sb, " X:r%d, p%d, s%d;",
996 			    (int)info->sc_iid_x,
997 			    (int)info->sc_loc_x.pos,
998 			    (int)info->sc_loc_x.size);
999 		if (info->sc_flags & UMS_FLAG_Y_AXIS)
1000 			sbuf_printf(sb, " Y:r%d, p%d, s%d;",
1001 			    (int)info->sc_iid_y,
1002 			    (int)info->sc_loc_y.pos,
1003 			    (int)info->sc_loc_y.size);
1004 		if (info->sc_flags & UMS_FLAG_Z_AXIS)
1005 			sbuf_printf(sb, " Z:r%d, p%d, s%d;",
1006 			    (int)info->sc_iid_z,
1007 			    (int)info->sc_loc_z.pos,
1008 			    (int)info->sc_loc_z.size);
1009 		if (info->sc_flags & UMS_FLAG_T_AXIS)
1010 			sbuf_printf(sb, " T:r%d, p%d, s%d;",
1011 			    (int)info->sc_iid_t,
1012 			    (int)info->sc_loc_t.pos,
1013 			    (int)info->sc_loc_t.size);
1014 		if (info->sc_flags & UMS_FLAG_W_AXIS)
1015 			sbuf_printf(sb, " W:r%d, p%d, s%d;",
1016 			    (int)info->sc_iid_w,
1017 			    (int)info->sc_loc_w.pos,
1018 			    (int)info->sc_loc_w.size);
1019 
1020 		for (j = 0; j < info->sc_buttons; j++) {
1021 			sbuf_printf(sb, " B%d:r%d, p%d, s%d;", j + 1,
1022 			    (int)info->sc_iid_btn[j],
1023 			    (int)info->sc_loc_btn[j].pos,
1024 			    (int)info->sc_loc_btn[j].size);
1025 		}
1026 	}
1027 	sbuf_finish(sb);
1028 	err = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
1029 	sbuf_delete(sb);
1030 
1031 	return (err);
1032 }
1033 #endif
1034 
1035 static devclass_t ums_devclass;
1036 
1037 static device_method_t ums_methods[] = {
1038 	DEVMETHOD(device_probe, ums_probe),
1039 	DEVMETHOD(device_attach, ums_attach),
1040 	DEVMETHOD(device_detach, ums_detach),
1041 	DEVMETHOD_END
1042 };
1043 
1044 static driver_t ums_driver = {
1045 	.name = "ums",
1046 	.methods = ums_methods,
1047 	.size = sizeof(struct ums_softc),
1048 };
1049 
1050 DRIVER_MODULE(ums, uhub, ums_driver, ums_devclass, NULL, NULL);
1051 MODULE_DEPEND(ums, usb, 1, 1, 1);
1052 MODULE_VERSION(ums, 1);
1053