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