xref: /freebsd/sys/dev/usb/input/ums.c (revision f8d2b1f3f72086bd169f1a0890b6faba8434a01a)
102ac6454SAndrew Thompson /*-
2718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3718cf2ccSPedro F. Giffuni  *
402ac6454SAndrew Thompson  * Copyright (c) 1998 The NetBSD Foundation, Inc.
502ac6454SAndrew Thompson  * All rights reserved.
602ac6454SAndrew Thompson  *
702ac6454SAndrew Thompson  * This code is derived from software contributed to The NetBSD Foundation
802ac6454SAndrew Thompson  * by Lennart Augustsson (lennart@augustsson.net) at
902ac6454SAndrew Thompson  * Carlstedt Research & Technology.
1002ac6454SAndrew Thompson  *
1102ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
1202ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
1302ac6454SAndrew Thompson  * are met:
1402ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1502ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1602ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1702ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1802ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1902ac6454SAndrew Thompson  *
2002ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2102ac6454SAndrew Thompson  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2202ac6454SAndrew Thompson  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2302ac6454SAndrew Thompson  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2402ac6454SAndrew Thompson  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2502ac6454SAndrew Thompson  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2602ac6454SAndrew Thompson  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2702ac6454SAndrew Thompson  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2802ac6454SAndrew Thompson  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2902ac6454SAndrew Thompson  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3002ac6454SAndrew Thompson  * POSSIBILITY OF SUCH DAMAGE.
3102ac6454SAndrew Thompson  */
3202ac6454SAndrew Thompson 
3302ac6454SAndrew Thompson #include <sys/cdefs.h>
3402ac6454SAndrew Thompson __FBSDID("$FreeBSD$");
3502ac6454SAndrew Thompson 
3602ac6454SAndrew Thompson /*
3702ac6454SAndrew Thompson  * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
3802ac6454SAndrew Thompson  */
3902ac6454SAndrew Thompson 
40c3af259aSOleksandr Tymoshenko #include "opt_evdev.h"
41c3af259aSOleksandr Tymoshenko 
42ed6d949aSAndrew Thompson #include <sys/stdint.h>
43ed6d949aSAndrew Thompson #include <sys/stddef.h>
44ed6d949aSAndrew Thompson #include <sys/param.h>
45ed6d949aSAndrew Thompson #include <sys/queue.h>
46ed6d949aSAndrew Thompson #include <sys/types.h>
47ed6d949aSAndrew Thompson #include <sys/systm.h>
48ed6d949aSAndrew Thompson #include <sys/kernel.h>
49ed6d949aSAndrew Thompson #include <sys/bus.h>
50ed6d949aSAndrew Thompson #include <sys/module.h>
51ed6d949aSAndrew Thompson #include <sys/lock.h>
52ed6d949aSAndrew Thompson #include <sys/mutex.h>
53ed6d949aSAndrew Thompson #include <sys/condvar.h>
54ed6d949aSAndrew Thompson #include <sys/sysctl.h>
55ed6d949aSAndrew Thompson #include <sys/sx.h>
56ed6d949aSAndrew Thompson #include <sys/unistd.h>
57ed6d949aSAndrew Thompson #include <sys/callout.h>
58ed6d949aSAndrew Thompson #include <sys/malloc.h>
59ed6d949aSAndrew Thompson #include <sys/priv.h>
60ed6d949aSAndrew Thompson #include <sys/conf.h>
61ed6d949aSAndrew Thompson #include <sys/fcntl.h>
624ed0304cSAndrew Thompson #include <sys/sbuf.h>
63ed6d949aSAndrew Thompson 
6402ac6454SAndrew Thompson #include <dev/usb/usb.h>
65ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
66ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h>
6702ac6454SAndrew Thompson #include <dev/usb/usbhid.h>
68ed6d949aSAndrew Thompson #include "usbdevs.h"
6902ac6454SAndrew Thompson 
7002ac6454SAndrew Thompson #define	USB_DEBUG_VAR ums_debug
7102ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
7202ac6454SAndrew Thompson 
7302ac6454SAndrew Thompson #include <dev/usb/quirk/usb_quirk.h>
7402ac6454SAndrew Thompson 
75a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
76c3af259aSOleksandr Tymoshenko #include <dev/evdev/input.h>
77c3af259aSOleksandr Tymoshenko #include <dev/evdev/evdev.h>
78c3af259aSOleksandr Tymoshenko #endif
79c3af259aSOleksandr Tymoshenko 
8002ac6454SAndrew Thompson #include <sys/ioccom.h>
8102ac6454SAndrew Thompson #include <sys/filio.h>
8202ac6454SAndrew Thompson #include <sys/mouse.h>
8302ac6454SAndrew Thompson 
84b850ecc1SAndrew Thompson #ifdef USB_DEBUG
8502ac6454SAndrew Thompson static int ums_debug = 0;
8602ac6454SAndrew Thompson 
87*f8d2b1f3SPawel Biernacki static SYSCTL_NODE(_hw_usb, OID_AUTO, ums, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
88*f8d2b1f3SPawel Biernacki     "USB ums");
89ece4b0bdSHans Petter Selasky SYSCTL_INT(_hw_usb_ums, OID_AUTO, debug, CTLFLAG_RWTUN,
9002ac6454SAndrew Thompson     &ums_debug, 0, "Debug level");
9102ac6454SAndrew Thompson #endif
9202ac6454SAndrew Thompson 
9302ac6454SAndrew Thompson #define	MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
9402ac6454SAndrew Thompson #define	MOUSE_FLAGS (HIO_RELATIVE)
9502ac6454SAndrew Thompson 
9602ac6454SAndrew Thompson #define	UMS_BUF_SIZE      8		/* bytes */
9702ac6454SAndrew Thompson #define	UMS_IFQ_MAXLEN   50		/* units */
9802ac6454SAndrew Thompson #define	UMS_BUTTON_MAX   31		/* exclusive, must be less than 32 */
9902ac6454SAndrew Thompson #define	UMS_BUT(i) ((i) < 3 ? (((i) + 2) % 3) : (i))
100de967835SAndrew Thompson #define	UMS_INFO_MAX	  2		/* maximum number of HID sets */
10102ac6454SAndrew Thompson 
10202ac6454SAndrew Thompson enum {
10302ac6454SAndrew Thompson 	UMS_INTR_DT,
1043e5e312aSAndrew Thompson 	UMS_N_TRANSFER,
10502ac6454SAndrew Thompson };
10602ac6454SAndrew Thompson 
107de967835SAndrew Thompson struct ums_info {
10802ac6454SAndrew Thompson 	struct hid_location sc_loc_w;
10902ac6454SAndrew Thompson 	struct hid_location sc_loc_x;
11002ac6454SAndrew Thompson 	struct hid_location sc_loc_y;
11102ac6454SAndrew Thompson 	struct hid_location sc_loc_z;
11202ac6454SAndrew Thompson 	struct hid_location sc_loc_t;
11302ac6454SAndrew Thompson 	struct hid_location sc_loc_btn[UMS_BUTTON_MAX];
11402ac6454SAndrew Thompson 
11502ac6454SAndrew Thompson 	uint32_t sc_flags;
11602ac6454SAndrew Thompson #define	UMS_FLAG_X_AXIS     0x0001
11702ac6454SAndrew Thompson #define	UMS_FLAG_Y_AXIS     0x0002
11802ac6454SAndrew Thompson #define	UMS_FLAG_Z_AXIS     0x0004
11902ac6454SAndrew Thompson #define	UMS_FLAG_T_AXIS     0x0008
12002ac6454SAndrew Thompson #define	UMS_FLAG_SBU        0x0010	/* spurious button up events */
1213e5e312aSAndrew Thompson #define	UMS_FLAG_REVZ	    0x0020	/* Z-axis is reversed */
1223e5e312aSAndrew Thompson #define	UMS_FLAG_W_AXIS     0x0040
12302ac6454SAndrew Thompson 
124bf87f556SAndrew Thompson 	uint8_t	sc_iid_w;
125bf87f556SAndrew Thompson 	uint8_t	sc_iid_x;
126bf87f556SAndrew Thompson 	uint8_t	sc_iid_y;
127bf87f556SAndrew Thompson 	uint8_t	sc_iid_z;
128bf87f556SAndrew Thompson 	uint8_t	sc_iid_t;
129bf87f556SAndrew Thompson 	uint8_t	sc_iid_btn[UMS_BUTTON_MAX];
130de967835SAndrew Thompson 	uint8_t	sc_buttons;
131de967835SAndrew Thompson };
132de967835SAndrew Thompson 
133de967835SAndrew Thompson struct ums_softc {
134760bc48eSAndrew Thompson 	struct usb_fifo_sc sc_fifo;
135de967835SAndrew Thompson 	struct mtx sc_mtx;
136760bc48eSAndrew Thompson 	struct usb_callout sc_callout;
137de967835SAndrew Thompson 	struct ums_info sc_info[UMS_INFO_MAX];
138de967835SAndrew Thompson 
139de967835SAndrew Thompson 	mousehw_t sc_hw;
140de967835SAndrew Thompson 	mousemode_t sc_mode;
141de967835SAndrew Thompson 	mousestatus_t sc_status;
142de967835SAndrew Thompson 
143760bc48eSAndrew Thompson 	struct usb_xfer *sc_xfer[UMS_N_TRANSFER];
144de967835SAndrew Thompson 
14581f82884SAlfred Perlstein 	int sc_pollrate;
14632e79f55SHans Petter Selasky 	int sc_fflags;
147a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
148c3af259aSOleksandr Tymoshenko 	int sc_evflags;
149c3af259aSOleksandr Tymoshenko #define	UMS_EVDEV_OPENED	1
150c3af259aSOleksandr Tymoshenko #endif
15181f82884SAlfred Perlstein 
152de967835SAndrew Thompson 	uint8_t	sc_buttons;
153de967835SAndrew Thompson 	uint8_t	sc_iid;
15402ac6454SAndrew Thompson 	uint8_t	sc_temp[64];
155c3af259aSOleksandr Tymoshenko 
156a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
157c3af259aSOleksandr Tymoshenko 	struct evdev_dev *sc_evdev;
158c3af259aSOleksandr Tymoshenko #endif
15902ac6454SAndrew Thompson };
16002ac6454SAndrew Thompson 
16102ac6454SAndrew Thompson static void ums_put_queue_timeout(void *__sc);
16202ac6454SAndrew Thompson 
163e0a69b51SAndrew Thompson static usb_callback_t ums_intr_callback;
16402ac6454SAndrew Thompson 
16502ac6454SAndrew Thompson static device_probe_t ums_probe;
16602ac6454SAndrew Thompson static device_attach_t ums_attach;
16702ac6454SAndrew Thompson static device_detach_t ums_detach;
16802ac6454SAndrew Thompson 
169c3af259aSOleksandr Tymoshenko static usb_fifo_cmd_t ums_fifo_start_read;
170c3af259aSOleksandr Tymoshenko static usb_fifo_cmd_t ums_fifo_stop_read;
171c3af259aSOleksandr Tymoshenko static usb_fifo_open_t ums_fifo_open;
172c3af259aSOleksandr Tymoshenko static usb_fifo_close_t ums_fifo_close;
173c3af259aSOleksandr Tymoshenko static usb_fifo_ioctl_t ums_fifo_ioctl;
17402ac6454SAndrew Thompson 
175a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
176c3af259aSOleksandr Tymoshenko static evdev_open_t ums_ev_open;
177c3af259aSOleksandr Tymoshenko static evdev_close_t ums_ev_close;
178e012a280SOleksandr Tymoshenko static void ums_evdev_push(struct ums_softc *, int32_t, int32_t,
179e012a280SOleksandr Tymoshenko     int32_t, int32_t, int32_t);
180c3af259aSOleksandr Tymoshenko #endif
181c3af259aSOleksandr Tymoshenko 
182c3af259aSOleksandr Tymoshenko static void	ums_start_rx(struct ums_softc *);
183c3af259aSOleksandr Tymoshenko static void	ums_stop_rx(struct ums_softc *);
1844ed0304cSAndrew Thompson static void	ums_put_queue(struct ums_softc *, int32_t, int32_t,
1854ed0304cSAndrew Thompson 		    int32_t, int32_t, int32_t);
1864ed0304cSAndrew Thompson static int	ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS);
18702ac6454SAndrew Thompson 
188760bc48eSAndrew Thompson static struct usb_fifo_methods ums_fifo_methods = {
189c3af259aSOleksandr Tymoshenko 	.f_open = &ums_fifo_open,
190c3af259aSOleksandr Tymoshenko 	.f_close = &ums_fifo_close,
191c3af259aSOleksandr Tymoshenko 	.f_ioctl = &ums_fifo_ioctl,
192c3af259aSOleksandr Tymoshenko 	.f_start_read = &ums_fifo_start_read,
193c3af259aSOleksandr Tymoshenko 	.f_stop_read = &ums_fifo_stop_read,
19402ac6454SAndrew Thompson 	.basename[0] = "ums",
19502ac6454SAndrew Thompson };
19602ac6454SAndrew Thompson 
197a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
1985163e77fSOleksandr Tymoshenko static const struct evdev_methods ums_evdev_methods = {
199c3af259aSOleksandr Tymoshenko 	.ev_open = &ums_ev_open,
200c3af259aSOleksandr Tymoshenko 	.ev_close = &ums_ev_close,
201c3af259aSOleksandr Tymoshenko };
202c3af259aSOleksandr Tymoshenko #endif
203c3af259aSOleksandr Tymoshenko 
20402ac6454SAndrew Thompson static void
20502ac6454SAndrew Thompson ums_put_queue_timeout(void *__sc)
20602ac6454SAndrew Thompson {
20702ac6454SAndrew Thompson 	struct ums_softc *sc = __sc;
20802ac6454SAndrew Thompson 
20902ac6454SAndrew Thompson 	mtx_assert(&sc->sc_mtx, MA_OWNED);
21002ac6454SAndrew Thompson 
21102ac6454SAndrew Thompson 	ums_put_queue(sc, 0, 0, 0, 0, 0);
212e012a280SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
213e012a280SOleksandr Tymoshenko 	ums_evdev_push(sc, 0, 0, 0, 0, 0);
214e012a280SOleksandr Tymoshenko #endif
21502ac6454SAndrew Thompson }
21602ac6454SAndrew Thompson 
21702ac6454SAndrew Thompson static void
218ed6d949aSAndrew Thompson ums_intr_callback(struct usb_xfer *xfer, usb_error_t error)
21902ac6454SAndrew Thompson {
220ed6d949aSAndrew Thompson 	struct ums_softc *sc = usbd_xfer_softc(xfer);
221de967835SAndrew Thompson 	struct ums_info *info = &sc->sc_info[0];
222ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
22302ac6454SAndrew Thompson 	uint8_t *buf = sc->sc_temp;
22402ac6454SAndrew Thompson 	int32_t buttons = 0;
22581f82884SAlfred Perlstein 	int32_t buttons_found = 0;
226e012a280SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
227e012a280SOleksandr Tymoshenko 	int32_t buttons_reported = 0;
228e012a280SOleksandr Tymoshenko #endif
229de967835SAndrew Thompson 	int32_t dw = 0;
230de967835SAndrew Thompson 	int32_t dx = 0;
231de967835SAndrew Thompson 	int32_t dy = 0;
232de967835SAndrew Thompson 	int32_t dz = 0;
233de967835SAndrew Thompson 	int32_t dt = 0;
23402ac6454SAndrew Thompson 	uint8_t i;
235bf87f556SAndrew Thompson 	uint8_t id;
236ed6d949aSAndrew Thompson 	int len;
237ed6d949aSAndrew Thompson 
238ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
23902ac6454SAndrew Thompson 
24002ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
24102ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
24202ac6454SAndrew Thompson 		DPRINTFN(6, "sc=%p actlen=%d\n", sc, len);
24302ac6454SAndrew Thompson 
2446d917491SHans Petter Selasky 		if (len > (int)sizeof(sc->sc_temp)) {
24502ac6454SAndrew Thompson 			DPRINTFN(6, "truncating large packet to %zu bytes\n",
24602ac6454SAndrew Thompson 			    sizeof(sc->sc_temp));
24702ac6454SAndrew Thompson 			len = sizeof(sc->sc_temp);
24802ac6454SAndrew Thompson 		}
2493e5e312aSAndrew Thompson 		if (len == 0)
25002ac6454SAndrew Thompson 			goto tr_setup;
2513e5e312aSAndrew Thompson 
252ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
253ed6d949aSAndrew Thompson 		usbd_copy_out(pc, 0, buf, len);
25402ac6454SAndrew Thompson 
25502ac6454SAndrew Thompson 		DPRINTFN(6, "data = %02x %02x %02x %02x "
25602ac6454SAndrew Thompson 		    "%02x %02x %02x %02x\n",
25702ac6454SAndrew Thompson 		    (len > 0) ? buf[0] : 0, (len > 1) ? buf[1] : 0,
25802ac6454SAndrew Thompson 		    (len > 2) ? buf[2] : 0, (len > 3) ? buf[3] : 0,
25902ac6454SAndrew Thompson 		    (len > 4) ? buf[4] : 0, (len > 5) ? buf[5] : 0,
26002ac6454SAndrew Thompson 		    (len > 6) ? buf[6] : 0, (len > 7) ? buf[7] : 0);
26102ac6454SAndrew Thompson 
26202ac6454SAndrew Thompson 		if (sc->sc_iid) {
263bf87f556SAndrew Thompson 			id = *buf;
26402ac6454SAndrew Thompson 
26502ac6454SAndrew Thompson 			len--;
26602ac6454SAndrew Thompson 			buf++;
26702ac6454SAndrew Thompson 
26802ac6454SAndrew Thompson 		} else {
269bf87f556SAndrew Thompson 			id = 0;
270de967835SAndrew Thompson 			if (sc->sc_info[0].sc_flags & UMS_FLAG_SBU) {
27102ac6454SAndrew Thompson 				if ((*buf == 0x14) || (*buf == 0x15)) {
27202ac6454SAndrew Thompson 					goto tr_setup;
27302ac6454SAndrew Thompson 				}
27402ac6454SAndrew Thompson 			}
27502ac6454SAndrew Thompson 		}
27602ac6454SAndrew Thompson 
277de967835SAndrew Thompson 	repeat:
278de967835SAndrew Thompson 		if ((info->sc_flags & UMS_FLAG_W_AXIS) &&
279de967835SAndrew Thompson 		    (id == info->sc_iid_w))
280de967835SAndrew Thompson 			dw += hid_get_data(buf, len, &info->sc_loc_w);
28102ac6454SAndrew Thompson 
282de967835SAndrew Thompson 		if ((info->sc_flags & UMS_FLAG_X_AXIS) &&
283de967835SAndrew Thompson 		    (id == info->sc_iid_x))
284de967835SAndrew Thompson 			dx += hid_get_data(buf, len, &info->sc_loc_x);
28502ac6454SAndrew Thompson 
286de967835SAndrew Thompson 		if ((info->sc_flags & UMS_FLAG_Y_AXIS) &&
287de967835SAndrew Thompson 		    (id == info->sc_iid_y))
288e9517d24SPeter Grehan 			dy -= hid_get_data(buf, len, &info->sc_loc_y);
28902ac6454SAndrew Thompson 
290de967835SAndrew Thompson 		if ((info->sc_flags & UMS_FLAG_Z_AXIS) &&
291de967835SAndrew Thompson 		    (id == info->sc_iid_z)) {
292de967835SAndrew Thompson 			int32_t temp;
293de967835SAndrew Thompson 			temp = hid_get_data(buf, len, &info->sc_loc_z);
294de967835SAndrew Thompson 			if (info->sc_flags & UMS_FLAG_REVZ)
295de967835SAndrew Thompson 				temp = -temp;
296de967835SAndrew Thompson 			dz -= temp;
297de967835SAndrew Thompson 		}
29802ac6454SAndrew Thompson 
299de967835SAndrew Thompson 		if ((info->sc_flags & UMS_FLAG_T_AXIS) &&
300e2efc9beSHans Petter Selasky 		    (id == info->sc_iid_t)) {
3017707483aSVladimir Kondratyev 			dt += hid_get_data(buf, len, &info->sc_loc_t);
302e2efc9beSHans Petter Selasky 			/* T-axis is translated into button presses */
303e2efc9beSHans Petter Selasky 			buttons_found |= (1UL << 5) | (1UL << 6);
304e2efc9beSHans Petter Selasky 		}
305bf87f556SAndrew Thompson 
306de967835SAndrew Thompson 		for (i = 0; i < info->sc_buttons; i++) {
30781f82884SAlfred Perlstein 			uint32_t mask;
30881f82884SAlfred Perlstein 			mask = 1UL << UMS_BUT(i);
30981f82884SAlfred Perlstein 			/* check for correct button ID */
310de967835SAndrew Thompson 			if (id != info->sc_iid_btn[i])
311bf87f556SAndrew Thompson 				continue;
31281f82884SAlfred Perlstein 			/* check for button pressed */
31381f82884SAlfred Perlstein 			if (hid_get_data(buf, len, &info->sc_loc_btn[i]))
31481f82884SAlfred Perlstein 				buttons |= mask;
31581f82884SAlfred Perlstein 			/* register button mask */
31681f82884SAlfred Perlstein 			buttons_found |= mask;
31702ac6454SAndrew Thompson 		}
31802ac6454SAndrew Thompson 
319de967835SAndrew Thompson 		if (++info != &sc->sc_info[UMS_INFO_MAX])
320de967835SAndrew Thompson 			goto repeat;
321de967835SAndrew Thompson 
322e012a280SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
323e012a280SOleksandr Tymoshenko 		buttons_reported = buttons;
324e012a280SOleksandr Tymoshenko #endif
32581f82884SAlfred Perlstein 		/* keep old button value(s) for non-detected buttons */
32681f82884SAlfred Perlstein 		buttons |= sc->sc_status.button & ~buttons_found;
32781f82884SAlfred Perlstein 
32802ac6454SAndrew Thompson 		if (dx || dy || dz || dt || dw ||
32902ac6454SAndrew Thompson 		    (buttons != sc->sc_status.button)) {
33002ac6454SAndrew Thompson 
33102ac6454SAndrew Thompson 			DPRINTFN(6, "x:%d y:%d z:%d t:%d w:%d buttons:0x%08x\n",
33202ac6454SAndrew Thompson 			    dx, dy, dz, dt, dw, buttons);
33302ac6454SAndrew Thompson 
334300f96f1SAndrew Thompson 			/* translate T-axis into button presses until further */
335d03403c4SHans Petter Selasky 			if (dt > 0) {
336d03403c4SHans Petter Selasky 				ums_put_queue(sc, 0, 0, 0, 0, buttons);
3377707483aSVladimir Kondratyev 				buttons |= 1UL << 6;
338d03403c4SHans Petter Selasky 			} else if (dt < 0) {
339d03403c4SHans Petter Selasky 				ums_put_queue(sc, 0, 0, 0, 0, buttons);
3407707483aSVladimir Kondratyev 				buttons |= 1UL << 5;
341d03403c4SHans Petter Selasky 			}
342300f96f1SAndrew Thompson 
34302ac6454SAndrew Thompson 			sc->sc_status.button = buttons;
34402ac6454SAndrew Thompson 			sc->sc_status.dx += dx;
34502ac6454SAndrew Thompson 			sc->sc_status.dy += dy;
34602ac6454SAndrew Thompson 			sc->sc_status.dz += dz;
34702ac6454SAndrew Thompson 			/*
34802ac6454SAndrew Thompson 			 * sc->sc_status.dt += dt;
34902ac6454SAndrew Thompson 			 * no way to export this yet
35002ac6454SAndrew Thompson 			 */
35102ac6454SAndrew Thompson 
35202ac6454SAndrew Thompson 			/*
3533e5e312aSAndrew Thompson 		         * The Qtronix keyboard has a built in PS/2
3543e5e312aSAndrew Thompson 		         * port for a mouse.  The firmware once in a
3553e5e312aSAndrew Thompson 		         * while posts a spurious button up
3563e5e312aSAndrew Thompson 		         * event. This event we ignore by doing a
3573e5e312aSAndrew Thompson 		         * timeout for 50 msecs.  If we receive
3583e5e312aSAndrew Thompson 		         * dx=dy=dz=buttons=0 before we add the event
3593e5e312aSAndrew Thompson 		         * to the queue.  In any other case we delete
3603e5e312aSAndrew Thompson 		         * the timeout event.
36102ac6454SAndrew Thompson 		         */
362de967835SAndrew Thompson 			if ((sc->sc_info[0].sc_flags & UMS_FLAG_SBU) &&
36302ac6454SAndrew Thompson 			    (dx == 0) && (dy == 0) && (dz == 0) && (dt == 0) &&
36402ac6454SAndrew Thompson 			    (dw == 0) && (buttons == 0)) {
36502ac6454SAndrew Thompson 
366a593f6b8SAndrew Thompson 				usb_callout_reset(&sc->sc_callout, hz / 20,
36702ac6454SAndrew Thompson 				    &ums_put_queue_timeout, sc);
36802ac6454SAndrew Thompson 			} else {
36902ac6454SAndrew Thompson 
370a593f6b8SAndrew Thompson 				usb_callout_stop(&sc->sc_callout);
37102ac6454SAndrew Thompson 
37202ac6454SAndrew Thompson 				ums_put_queue(sc, dx, dy, dz, dt, buttons);
373e012a280SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
374e012a280SOleksandr Tymoshenko 				ums_evdev_push(sc, dx, dy, dz, dt,
375e012a280SOleksandr Tymoshenko 				    buttons_reported);
376e012a280SOleksandr Tymoshenko #endif
377e012a280SOleksandr Tymoshenko 
37802ac6454SAndrew Thompson 			}
37902ac6454SAndrew Thompson 		}
38002ac6454SAndrew Thompson 	case USB_ST_SETUP:
38102ac6454SAndrew Thompson tr_setup:
38202ac6454SAndrew Thompson 		/* check if we can put more data into the FIFO */
383c3af259aSOleksandr Tymoshenko 		if (usb_fifo_put_bytes_max(sc->sc_fifo.fp[USB_FIFO_RX]) == 0) {
384a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
385c3af259aSOleksandr Tymoshenko 			if (sc->sc_evflags == 0)
386c3af259aSOleksandr Tymoshenko 				break;
387c3af259aSOleksandr Tymoshenko #else
388c3af259aSOleksandr Tymoshenko 			break;
389c3af259aSOleksandr Tymoshenko #endif
390c3af259aSOleksandr Tymoshenko 		}
391c3af259aSOleksandr Tymoshenko 
392ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
393a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
3943e5e312aSAndrew Thompson 		break;
39502ac6454SAndrew Thompson 
39602ac6454SAndrew Thompson 	default:			/* Error */
397ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
3983e5e312aSAndrew Thompson 			/* try clear stall first */
399ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
4003e5e312aSAndrew Thompson 			goto tr_setup;
40102ac6454SAndrew Thompson 		}
4023e5e312aSAndrew Thompson 		break;
40302ac6454SAndrew Thompson 	}
40402ac6454SAndrew Thompson }
40502ac6454SAndrew Thompson 
406760bc48eSAndrew Thompson static const struct usb_config ums_config[UMS_N_TRANSFER] = {
40702ac6454SAndrew Thompson 
40802ac6454SAndrew Thompson 	[UMS_INTR_DT] = {
40902ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
41002ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
41102ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
4124eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
4134eae601eSAndrew Thompson 		.bufsize = 0,	/* use wMaxPacketSize */
4144eae601eSAndrew Thompson 		.callback = &ums_intr_callback,
41502ac6454SAndrew Thompson 	},
41602ac6454SAndrew Thompson };
41702ac6454SAndrew Thompson 
4189c916c62SHans Petter Selasky /* A match on these entries will load ums */
4199c916c62SHans Petter Selasky static const STRUCT_USB_HOST_ID __used ums_devs[] = {
4209c916c62SHans Petter Selasky 	{USB_IFACE_CLASS(UICLASS_HID),
4219c916c62SHans Petter Selasky 	 USB_IFACE_SUBCLASS(UISUBCLASS_BOOT),
4229c916c62SHans Petter Selasky 	 USB_IFACE_PROTOCOL(UIPROTO_MOUSE),},
4239c916c62SHans Petter Selasky };
4249c916c62SHans Petter Selasky 
42502ac6454SAndrew Thompson static int
42602ac6454SAndrew Thompson ums_probe(device_t dev)
42702ac6454SAndrew Thompson {
428760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
42902ac6454SAndrew Thompson 	void *d_ptr;
43060867f57SHans Petter Selasky 	int error;
43102ac6454SAndrew Thompson 	uint16_t d_len;
43202ac6454SAndrew Thompson 
43302ac6454SAndrew Thompson 	DPRINTFN(11, "\n");
43402ac6454SAndrew Thompson 
435f29a0724SAndrew Thompson 	if (uaa->usb_mode != USB_MODE_HOST)
43602ac6454SAndrew Thompson 		return (ENXIO);
4373e5e312aSAndrew Thompson 
438fd4f10ceSAndrew Thompson 	if (uaa->info.bInterfaceClass != UICLASS_HID)
43902ac6454SAndrew Thompson 		return (ENXIO);
4403e5e312aSAndrew Thompson 
441d7dd1341SHans Petter Selasky 	if (usb_test_quirk(uaa, UQ_UMS_IGNORE))
442d7dd1341SHans Petter Selasky 		return (ENXIO);
443d7dd1341SHans Petter Selasky 
444fd4f10ceSAndrew Thompson 	if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
445fd4f10ceSAndrew Thompson 	    (uaa->info.bInterfaceProtocol == UIPROTO_MOUSE))
446cd10bffaSAndriy Gapon 		return (BUS_PROBE_DEFAULT);
447fd4f10ceSAndrew Thompson 
448a593f6b8SAndrew Thompson 	error = usbd_req_get_hid_desc(uaa->device, NULL,
44902ac6454SAndrew Thompson 	    &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
45002ac6454SAndrew Thompson 
4513e5e312aSAndrew Thompson 	if (error)
45202ac6454SAndrew Thompson 		return (ENXIO);
4533e5e312aSAndrew Thompson 
45460867f57SHans Petter Selasky 	if (hid_is_mouse(d_ptr, d_len))
45560867f57SHans Petter Selasky 		error = BUS_PROBE_DEFAULT;
45660867f57SHans Petter Selasky 	else
45760867f57SHans Petter Selasky 		error = ENXIO;
45860867f57SHans Petter Selasky 
45902ac6454SAndrew Thompson 	free(d_ptr, M_TEMP);
46060867f57SHans Petter Selasky 	return (error);
46102ac6454SAndrew Thompson }
46202ac6454SAndrew Thompson 
463de967835SAndrew Thompson static void
464de967835SAndrew Thompson ums_hid_parse(struct ums_softc *sc, device_t dev, const uint8_t *buf,
465de967835SAndrew Thompson     uint16_t len, uint8_t index)
466de967835SAndrew Thompson {
467de967835SAndrew Thompson 	struct ums_info *info = &sc->sc_info[index];
468de967835SAndrew Thompson 	uint32_t flags;
469de967835SAndrew Thompson 	uint8_t i;
470ee6e1a6fSAndrew Thompson 	uint8_t j;
471de967835SAndrew Thompson 
472de967835SAndrew Thompson 	if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
473de967835SAndrew Thompson 	    hid_input, index, &info->sc_loc_x, &flags, &info->sc_iid_x)) {
474de967835SAndrew Thompson 
475de967835SAndrew Thompson 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
476de967835SAndrew Thompson 			info->sc_flags |= UMS_FLAG_X_AXIS;
477de967835SAndrew Thompson 		}
478de967835SAndrew Thompson 	}
479de967835SAndrew Thompson 	if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
480de967835SAndrew Thompson 	    hid_input, index, &info->sc_loc_y, &flags, &info->sc_iid_y)) {
481de967835SAndrew Thompson 
482de967835SAndrew Thompson 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
483de967835SAndrew Thompson 			info->sc_flags |= UMS_FLAG_Y_AXIS;
484de967835SAndrew Thompson 		}
485de967835SAndrew Thompson 	}
486de967835SAndrew Thompson 	/* Try the wheel first as the Z activator since it's tradition. */
487de967835SAndrew Thompson 	if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
488de967835SAndrew Thompson 	    HUG_WHEEL), hid_input, index, &info->sc_loc_z, &flags,
489de967835SAndrew Thompson 	    &info->sc_iid_z) ||
490de967835SAndrew Thompson 	    hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
491de967835SAndrew Thompson 	    HUG_TWHEEL), hid_input, index, &info->sc_loc_z, &flags,
492de967835SAndrew Thompson 	    &info->sc_iid_z)) {
493de967835SAndrew Thompson 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
494de967835SAndrew Thompson 			info->sc_flags |= UMS_FLAG_Z_AXIS;
495de967835SAndrew Thompson 		}
496de967835SAndrew Thompson 		/*
497de967835SAndrew Thompson 		 * We might have both a wheel and Z direction, if so put
498de967835SAndrew Thompson 		 * put the Z on the W coordinate.
499de967835SAndrew Thompson 		 */
500de967835SAndrew Thompson 		if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
501de967835SAndrew Thompson 		    HUG_Z), hid_input, index, &info->sc_loc_w, &flags,
502de967835SAndrew Thompson 		    &info->sc_iid_w)) {
503de967835SAndrew Thompson 
504de967835SAndrew Thompson 			if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
505de967835SAndrew Thompson 				info->sc_flags |= UMS_FLAG_W_AXIS;
506de967835SAndrew Thompson 			}
507de967835SAndrew Thompson 		}
508de967835SAndrew Thompson 	} else if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
509de967835SAndrew Thompson 	    HUG_Z), hid_input, index, &info->sc_loc_z, &flags,
510de967835SAndrew Thompson 	    &info->sc_iid_z)) {
511de967835SAndrew Thompson 
512de967835SAndrew Thompson 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
513de967835SAndrew Thompson 			info->sc_flags |= UMS_FLAG_Z_AXIS;
514de967835SAndrew Thompson 		}
515de967835SAndrew Thompson 	}
516de967835SAndrew Thompson 	/*
517de967835SAndrew Thompson 	 * The Microsoft Wireless Intellimouse 2.0 reports it's wheel
518de967835SAndrew Thompson 	 * using 0x0048, which is HUG_TWHEEL, and seems to expect you
519de967835SAndrew Thompson 	 * to know that the byte after the wheel is the tilt axis.
520de967835SAndrew Thompson 	 * There are no other HID axis descriptors other than X,Y and
521de967835SAndrew Thompson 	 * TWHEEL
522de967835SAndrew Thompson 	 */
523de967835SAndrew Thompson 	if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
524de967835SAndrew Thompson 	    HUG_TWHEEL), hid_input, index, &info->sc_loc_t,
525de967835SAndrew Thompson 	    &flags, &info->sc_iid_t)) {
526de967835SAndrew Thompson 
527de967835SAndrew Thompson 		info->sc_loc_t.pos += 8;
528de967835SAndrew Thompson 
529de967835SAndrew Thompson 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
530de967835SAndrew Thompson 			info->sc_flags |= UMS_FLAG_T_AXIS;
531de967835SAndrew Thompson 		}
532300f96f1SAndrew Thompson 	} else if (hid_locate(buf, len, HID_USAGE2(HUP_CONSUMER,
533300f96f1SAndrew Thompson 		HUC_AC_PAN), hid_input, index, &info->sc_loc_t,
534300f96f1SAndrew Thompson 		&flags, &info->sc_iid_t)) {
535300f96f1SAndrew Thompson 
536300f96f1SAndrew Thompson 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS)
537300f96f1SAndrew Thompson 			info->sc_flags |= UMS_FLAG_T_AXIS;
538de967835SAndrew Thompson 	}
539de967835SAndrew Thompson 	/* figure out the number of buttons */
540de967835SAndrew Thompson 
541de967835SAndrew Thompson 	for (i = 0; i < UMS_BUTTON_MAX; i++) {
542de967835SAndrew Thompson 		if (!hid_locate(buf, len, HID_USAGE2(HUP_BUTTON, (i + 1)),
543de967835SAndrew Thompson 		    hid_input, index, &info->sc_loc_btn[i], NULL,
544de967835SAndrew Thompson 		    &info->sc_iid_btn[i])) {
545de967835SAndrew Thompson 			break;
546de967835SAndrew Thompson 		}
547de967835SAndrew Thompson 	}
548ee6e1a6fSAndrew Thompson 
549ee6e1a6fSAndrew Thompson 	/* detect other buttons */
550ee6e1a6fSAndrew Thompson 
5511f27b685SAndrew Thompson 	for (j = 0; (i < UMS_BUTTON_MAX) && (j < 2); i++, j++) {
552ee6e1a6fSAndrew Thompson 		if (!hid_locate(buf, len, HID_USAGE2(HUP_MICROSOFT, (j + 1)),
553ee6e1a6fSAndrew Thompson 		    hid_input, index, &info->sc_loc_btn[i], NULL,
554ee6e1a6fSAndrew Thompson 		    &info->sc_iid_btn[i])) {
555ee6e1a6fSAndrew Thompson 			break;
556ee6e1a6fSAndrew Thompson 		}
557ee6e1a6fSAndrew Thompson 	}
558ee6e1a6fSAndrew Thompson 
559de967835SAndrew Thompson 	info->sc_buttons = i;
560de967835SAndrew Thompson 
561de967835SAndrew Thompson 	if (i > sc->sc_buttons)
562de967835SAndrew Thompson 		sc->sc_buttons = i;
563de967835SAndrew Thompson 
564de967835SAndrew Thompson 	if (info->sc_flags == 0)
565de967835SAndrew Thompson 		return;
566de967835SAndrew Thompson 
567de967835SAndrew Thompson 	/* announce information about the mouse */
568de967835SAndrew Thompson 	device_printf(dev, "%d buttons and [%s%s%s%s%s] coordinates ID=%u\n",
569de967835SAndrew Thompson 	    (info->sc_buttons),
570de967835SAndrew Thompson 	    (info->sc_flags & UMS_FLAG_X_AXIS) ? "X" : "",
571de967835SAndrew Thompson 	    (info->sc_flags & UMS_FLAG_Y_AXIS) ? "Y" : "",
572de967835SAndrew Thompson 	    (info->sc_flags & UMS_FLAG_Z_AXIS) ? "Z" : "",
573de967835SAndrew Thompson 	    (info->sc_flags & UMS_FLAG_T_AXIS) ? "T" : "",
574de967835SAndrew Thompson 	    (info->sc_flags & UMS_FLAG_W_AXIS) ? "W" : "",
575de967835SAndrew Thompson 	    info->sc_iid_x);
576de967835SAndrew Thompson }
577de967835SAndrew Thompson 
57802ac6454SAndrew Thompson static int
57902ac6454SAndrew Thompson ums_attach(device_t dev)
58002ac6454SAndrew Thompson {
581760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
58202ac6454SAndrew Thompson 	struct ums_softc *sc = device_get_softc(dev);
583de967835SAndrew Thompson 	struct ums_info *info;
58402ac6454SAndrew Thompson 	void *d_ptr = NULL;
5853e5e312aSAndrew Thompson 	int isize;
5863e5e312aSAndrew Thompson 	int err;
58702ac6454SAndrew Thompson 	uint16_t d_len;
58802ac6454SAndrew Thompson 	uint8_t i;
589b850ecc1SAndrew Thompson #ifdef USB_DEBUG
590de967835SAndrew Thompson 	uint8_t j;
591b850ecc1SAndrew Thompson #endif
59202ac6454SAndrew Thompson 
59302ac6454SAndrew Thompson 	DPRINTFN(11, "sc=%p\n", sc);
59402ac6454SAndrew Thompson 
595a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
59602ac6454SAndrew Thompson 
59702ac6454SAndrew Thompson 	mtx_init(&sc->sc_mtx, "ums lock", NULL, MTX_DEF | MTX_RECURSE);
59802ac6454SAndrew Thompson 
599a593f6b8SAndrew Thompson 	usb_callout_init_mtx(&sc->sc_callout, &sc->sc_mtx, 0);
60002ac6454SAndrew Thompson 
60102ac6454SAndrew Thompson 	/*
60202ac6454SAndrew Thompson          * Force the report (non-boot) protocol.
60302ac6454SAndrew Thompson          *
60402ac6454SAndrew Thompson          * Mice without boot protocol support may choose not to implement
60502ac6454SAndrew Thompson          * Set_Protocol at all; Ignore any error.
60602ac6454SAndrew Thompson          */
607a593f6b8SAndrew Thompson 	err = usbd_req_set_protocol(uaa->device, NULL,
608de967835SAndrew Thompson 	    uaa->info.bIfaceIndex, 1);
60902ac6454SAndrew Thompson 
610a593f6b8SAndrew Thompson 	err = usbd_transfer_setup(uaa->device,
61102ac6454SAndrew Thompson 	    &uaa->info.bIfaceIndex, sc->sc_xfer, ums_config,
61202ac6454SAndrew Thompson 	    UMS_N_TRANSFER, sc, &sc->sc_mtx);
61302ac6454SAndrew Thompson 
61402ac6454SAndrew Thompson 	if (err) {
615a593f6b8SAndrew Thompson 		DPRINTF("error=%s\n", usbd_errstr(err));
61602ac6454SAndrew Thompson 		goto detach;
61702ac6454SAndrew Thompson 	}
61881f82884SAlfred Perlstein 
61981f82884SAlfred Perlstein 	/* Get HID descriptor */
620a593f6b8SAndrew Thompson 	err = usbd_req_get_hid_desc(uaa->device, NULL, &d_ptr,
62102ac6454SAndrew Thompson 	    &d_len, M_TEMP, uaa->info.bIfaceIndex);
62202ac6454SAndrew Thompson 
62302ac6454SAndrew Thompson 	if (err) {
62402ac6454SAndrew Thompson 		device_printf(dev, "error reading report description\n");
62502ac6454SAndrew Thompson 		goto detach;
62602ac6454SAndrew Thompson 	}
62702ac6454SAndrew Thompson 
628bf87f556SAndrew Thompson 	isize = hid_report_size(d_ptr, d_len, hid_input, &sc->sc_iid);
62902ac6454SAndrew Thompson 
63002ac6454SAndrew Thompson 	/*
63102ac6454SAndrew Thompson 	 * The Microsoft Wireless Notebook Optical Mouse seems to be in worse
63202ac6454SAndrew Thompson 	 * shape than the Wireless Intellimouse 2.0, as its X, Y, wheel, and
63302ac6454SAndrew Thompson 	 * all of its other button positions are all off. It also reports that
63420733245SPedro F. Giffuni 	 * it has two additional buttons and a tilt wheel.
63502ac6454SAndrew Thompson 	 */
636a593f6b8SAndrew Thompson 	if (usb_test_quirk(uaa, UQ_MS_BAD_CLASS)) {
63781f82884SAlfred Perlstein 
63881f82884SAlfred Perlstein 		sc->sc_iid = 0;
63981f82884SAlfred Perlstein 
640de967835SAndrew Thompson 		info = &sc->sc_info[0];
641de967835SAndrew Thompson 		info->sc_flags = (UMS_FLAG_X_AXIS |
64202ac6454SAndrew Thompson 		    UMS_FLAG_Y_AXIS |
64302ac6454SAndrew Thompson 		    UMS_FLAG_Z_AXIS |
64402ac6454SAndrew Thompson 		    UMS_FLAG_SBU);
645de967835SAndrew Thompson 		info->sc_buttons = 3;
64602ac6454SAndrew Thompson 		isize = 5;
64702ac6454SAndrew Thompson 		/* 1st byte of descriptor report contains garbage */
648de967835SAndrew Thompson 		info->sc_loc_x.pos = 16;
64981f82884SAlfred Perlstein 		info->sc_loc_x.size = 8;
650de967835SAndrew Thompson 		info->sc_loc_y.pos = 24;
65181f82884SAlfred Perlstein 		info->sc_loc_y.size = 8;
652de967835SAndrew Thompson 		info->sc_loc_z.pos = 32;
65381f82884SAlfred Perlstein 		info->sc_loc_z.size = 8;
654de967835SAndrew Thompson 		info->sc_loc_btn[0].pos = 8;
65581f82884SAlfred Perlstein 		info->sc_loc_btn[0].size = 1;
656de967835SAndrew Thompson 		info->sc_loc_btn[1].pos = 9;
65781f82884SAlfred Perlstein 		info->sc_loc_btn[1].size = 1;
658de967835SAndrew Thompson 		info->sc_loc_btn[2].pos = 10;
65981f82884SAlfred Perlstein 		info->sc_loc_btn[2].size = 1;
6603e5e312aSAndrew Thompson 
661de967835SAndrew Thompson 		/* Announce device */
662de967835SAndrew Thompson 		device_printf(dev, "3 buttons and [XYZ] "
663de967835SAndrew Thompson 		    "coordinates ID=0\n");
664de967835SAndrew Thompson 
665de967835SAndrew Thompson 	} else {
666de967835SAndrew Thompson 		/* Search the HID descriptor and announce device */
667de967835SAndrew Thompson 		for (i = 0; i < UMS_INFO_MAX; i++) {
668de967835SAndrew Thompson 			ums_hid_parse(sc, dev, d_ptr, d_len, i);
669de967835SAndrew Thompson 		}
67002ac6454SAndrew Thompson 	}
6713e5e312aSAndrew Thompson 
672a593f6b8SAndrew Thompson 	if (usb_test_quirk(uaa, UQ_MS_REVZ)) {
673de967835SAndrew Thompson 		info = &sc->sc_info[0];
67402ac6454SAndrew Thompson 		/* Some wheels need the Z axis reversed. */
675de967835SAndrew Thompson 		info->sc_flags |= UMS_FLAG_REVZ;
67602ac6454SAndrew Thompson 	}
6776d917491SHans Petter Selasky 	if (isize > (int)usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT])) {
67802ac6454SAndrew Thompson 		DPRINTF("WARNING: report size, %d bytes, is larger "
679ed6d949aSAndrew Thompson 		    "than interrupt size, %d bytes!\n", isize,
680ed6d949aSAndrew Thompson 		    usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT]));
68102ac6454SAndrew Thompson 	}
68202ac6454SAndrew Thompson 	free(d_ptr, M_TEMP);
68302ac6454SAndrew Thompson 	d_ptr = NULL;
68402ac6454SAndrew Thompson 
685b850ecc1SAndrew Thompson #ifdef USB_DEBUG
686de967835SAndrew Thompson 	for (j = 0; j < UMS_INFO_MAX; j++) {
687de967835SAndrew Thompson 		info = &sc->sc_info[j];
68802ac6454SAndrew Thompson 
689de967835SAndrew Thompson 		DPRINTF("sc=%p, index=%d\n", sc, j);
690de967835SAndrew Thompson 		DPRINTF("X\t%d/%d id=%d\n", info->sc_loc_x.pos,
691de967835SAndrew Thompson 		    info->sc_loc_x.size, info->sc_iid_x);
692de967835SAndrew Thompson 		DPRINTF("Y\t%d/%d id=%d\n", info->sc_loc_y.pos,
693de967835SAndrew Thompson 		    info->sc_loc_y.size, info->sc_iid_y);
694de967835SAndrew Thompson 		DPRINTF("Z\t%d/%d id=%d\n", info->sc_loc_z.pos,
695de967835SAndrew Thompson 		    info->sc_loc_z.size, info->sc_iid_z);
696de967835SAndrew Thompson 		DPRINTF("T\t%d/%d id=%d\n", info->sc_loc_t.pos,
697de967835SAndrew Thompson 		    info->sc_loc_t.size, info->sc_iid_t);
698de967835SAndrew Thompson 		DPRINTF("W\t%d/%d id=%d\n", info->sc_loc_w.pos,
699de967835SAndrew Thompson 		    info->sc_loc_w.size, info->sc_iid_w);
700de967835SAndrew Thompson 
701de967835SAndrew Thompson 		for (i = 0; i < info->sc_buttons; i++) {
702bf87f556SAndrew Thompson 			DPRINTF("B%d\t%d/%d id=%d\n",
703de967835SAndrew Thompson 			    i + 1, info->sc_loc_btn[i].pos,
704de967835SAndrew Thompson 			    info->sc_loc_btn[i].size, info->sc_iid_btn[i]);
705de967835SAndrew Thompson 		}
70602ac6454SAndrew Thompson 	}
70702ac6454SAndrew Thompson 	DPRINTF("size=%d, id=%d\n", isize, sc->sc_iid);
70802ac6454SAndrew Thompson #endif
70902ac6454SAndrew Thompson 
710a593f6b8SAndrew Thompson 	err = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
71102ac6454SAndrew Thompson 	    &ums_fifo_methods, &sc->sc_fifo,
7126d917491SHans Petter Selasky 	    device_get_unit(dev), -1, uaa->info.bIfaceIndex,
713ee3e3ff5SAndrew Thompson   	    UID_ROOT, GID_OPERATOR, 0644);
71432e79f55SHans Petter Selasky 	if (err)
71502ac6454SAndrew Thompson 		goto detach;
71632e79f55SHans Petter Selasky 
717a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
718c3af259aSOleksandr Tymoshenko 	sc->sc_evdev = evdev_alloc();
719c3af259aSOleksandr Tymoshenko 	evdev_set_name(sc->sc_evdev, device_get_desc(dev));
720c3af259aSOleksandr Tymoshenko 	evdev_set_phys(sc->sc_evdev, device_get_nameunit(dev));
721c3af259aSOleksandr Tymoshenko 	evdev_set_id(sc->sc_evdev, BUS_USB, uaa->info.idVendor,
722c3af259aSOleksandr Tymoshenko 	    uaa->info.idProduct, 0);
723c3af259aSOleksandr Tymoshenko 	evdev_set_serial(sc->sc_evdev, usb_get_serial(uaa->device));
724c3af259aSOleksandr Tymoshenko 	evdev_set_methods(sc->sc_evdev, sc, &ums_evdev_methods);
725c3af259aSOleksandr Tymoshenko 	evdev_support_prop(sc->sc_evdev, INPUT_PROP_POINTER);
726c3af259aSOleksandr Tymoshenko 	evdev_support_event(sc->sc_evdev, EV_SYN);
727c3af259aSOleksandr Tymoshenko 	evdev_support_event(sc->sc_evdev, EV_REL);
728c3af259aSOleksandr Tymoshenko 	evdev_support_event(sc->sc_evdev, EV_KEY);
729c3af259aSOleksandr Tymoshenko 
730c3af259aSOleksandr Tymoshenko 	info = &sc->sc_info[0];
731c3af259aSOleksandr Tymoshenko 
732c3af259aSOleksandr Tymoshenko 	if (info->sc_flags & UMS_FLAG_X_AXIS)
733c3af259aSOleksandr Tymoshenko 		evdev_support_rel(sc->sc_evdev, REL_X);
734c3af259aSOleksandr Tymoshenko 
735c3af259aSOleksandr Tymoshenko 	if (info->sc_flags & UMS_FLAG_Y_AXIS)
736c3af259aSOleksandr Tymoshenko 		evdev_support_rel(sc->sc_evdev, REL_Y);
737c3af259aSOleksandr Tymoshenko 
738c3af259aSOleksandr Tymoshenko 	if (info->sc_flags & UMS_FLAG_Z_AXIS)
739c3af259aSOleksandr Tymoshenko 		evdev_support_rel(sc->sc_evdev, REL_WHEEL);
740c3af259aSOleksandr Tymoshenko 
741c3af259aSOleksandr Tymoshenko 	if (info->sc_flags & UMS_FLAG_T_AXIS)
742c3af259aSOleksandr Tymoshenko 		evdev_support_rel(sc->sc_evdev, REL_HWHEEL);
743c3af259aSOleksandr Tymoshenko 
744c3af259aSOleksandr Tymoshenko 	for (i = 0; i < info->sc_buttons; i++)
745c3af259aSOleksandr Tymoshenko 		evdev_support_key(sc->sc_evdev, BTN_MOUSE + i);
746c3af259aSOleksandr Tymoshenko 
747e012a280SOleksandr Tymoshenko 	err = evdev_register_mtx(sc->sc_evdev, &sc->sc_mtx);
748c3af259aSOleksandr Tymoshenko 	if (err)
749c3af259aSOleksandr Tymoshenko 		goto detach;
750c3af259aSOleksandr Tymoshenko #endif
751c3af259aSOleksandr Tymoshenko 
7524ed0304cSAndrew Thompson 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
7534ed0304cSAndrew Thompson 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
754*f8d2b1f3SPawel Biernacki 	    OID_AUTO, "parseinfo",
755*f8d2b1f3SPawel Biernacki 	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
756*f8d2b1f3SPawel Biernacki 	    sc, 0, ums_sysctl_handler_parseinfo, "",
757*f8d2b1f3SPawel Biernacki 	    "Dump of parsed HID report descriptor");
7584ed0304cSAndrew Thompson 
75902ac6454SAndrew Thompson 	return (0);
76002ac6454SAndrew Thompson 
76102ac6454SAndrew Thompson detach:
76202ac6454SAndrew Thompson 	if (d_ptr) {
76302ac6454SAndrew Thompson 		free(d_ptr, M_TEMP);
76402ac6454SAndrew Thompson 	}
76502ac6454SAndrew Thompson 	ums_detach(dev);
76602ac6454SAndrew Thompson 	return (ENOMEM);
76702ac6454SAndrew Thompson }
76802ac6454SAndrew Thompson 
76902ac6454SAndrew Thompson static int
77002ac6454SAndrew Thompson ums_detach(device_t self)
77102ac6454SAndrew Thompson {
77202ac6454SAndrew Thompson 	struct ums_softc *sc = device_get_softc(self);
77302ac6454SAndrew Thompson 
77402ac6454SAndrew Thompson 	DPRINTF("sc=%p\n", sc);
77502ac6454SAndrew Thompson 
776a593f6b8SAndrew Thompson 	usb_fifo_detach(&sc->sc_fifo);
77702ac6454SAndrew Thompson 
778a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
779c3af259aSOleksandr Tymoshenko 	evdev_free(sc->sc_evdev);
780c3af259aSOleksandr Tymoshenko #endif
781c3af259aSOleksandr Tymoshenko 
782a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, UMS_N_TRANSFER);
78302ac6454SAndrew Thompson 
784a593f6b8SAndrew Thompson 	usb_callout_drain(&sc->sc_callout);
78502ac6454SAndrew Thompson 
78602ac6454SAndrew Thompson 	mtx_destroy(&sc->sc_mtx);
78702ac6454SAndrew Thompson 
78802ac6454SAndrew Thompson 	return (0);
78902ac6454SAndrew Thompson }
79002ac6454SAndrew Thompson 
79102ac6454SAndrew Thompson static void
792c3af259aSOleksandr Tymoshenko ums_reset(struct ums_softc *sc)
79302ac6454SAndrew Thompson {
794c3af259aSOleksandr Tymoshenko 
795c3af259aSOleksandr Tymoshenko 	/* reset all USB mouse parameters */
796c3af259aSOleksandr Tymoshenko 
797c3af259aSOleksandr Tymoshenko 	if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
798c3af259aSOleksandr Tymoshenko 		sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
799c3af259aSOleksandr Tymoshenko 	else
800c3af259aSOleksandr Tymoshenko 		sc->sc_hw.buttons = sc->sc_buttons;
801c3af259aSOleksandr Tymoshenko 
802c3af259aSOleksandr Tymoshenko 	sc->sc_hw.iftype = MOUSE_IF_USB;
803c3af259aSOleksandr Tymoshenko 	sc->sc_hw.type = MOUSE_MOUSE;
804c3af259aSOleksandr Tymoshenko 	sc->sc_hw.model = MOUSE_MODEL_GENERIC;
805c3af259aSOleksandr Tymoshenko 	sc->sc_hw.hwid = 0;
806c3af259aSOleksandr Tymoshenko 
807c3af259aSOleksandr Tymoshenko 	sc->sc_mode.protocol = MOUSE_PROTO_MSC;
808c3af259aSOleksandr Tymoshenko 	sc->sc_mode.rate = -1;
809c3af259aSOleksandr Tymoshenko 	sc->sc_mode.resolution = MOUSE_RES_UNKNOWN;
810c3af259aSOleksandr Tymoshenko 	sc->sc_mode.accelfactor = 0;
811c3af259aSOleksandr Tymoshenko 	sc->sc_mode.level = 0;
812c3af259aSOleksandr Tymoshenko 	sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
813c3af259aSOleksandr Tymoshenko 	sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
814c3af259aSOleksandr Tymoshenko 	sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
815c3af259aSOleksandr Tymoshenko 
816c3af259aSOleksandr Tymoshenko 	/* reset status */
817c3af259aSOleksandr Tymoshenko 
818c3af259aSOleksandr Tymoshenko 	sc->sc_status.flags = 0;
819c3af259aSOleksandr Tymoshenko 	sc->sc_status.button = 0;
820c3af259aSOleksandr Tymoshenko 	sc->sc_status.obutton = 0;
821c3af259aSOleksandr Tymoshenko 	sc->sc_status.dx = 0;
822c3af259aSOleksandr Tymoshenko 	sc->sc_status.dy = 0;
823c3af259aSOleksandr Tymoshenko 	sc->sc_status.dz = 0;
824c3af259aSOleksandr Tymoshenko 	/* sc->sc_status.dt = 0; */
825c3af259aSOleksandr Tymoshenko }
826c3af259aSOleksandr Tymoshenko 
827c3af259aSOleksandr Tymoshenko static void
828c3af259aSOleksandr Tymoshenko ums_start_rx(struct ums_softc *sc)
829c3af259aSOleksandr Tymoshenko {
83081f82884SAlfred Perlstein 	int rate;
83181f82884SAlfred Perlstein 
83281f82884SAlfred Perlstein 	/* Check if we should override the default polling interval */
83381f82884SAlfred Perlstein 	rate = sc->sc_pollrate;
83481f82884SAlfred Perlstein 	/* Range check rate */
83581f82884SAlfred Perlstein 	if (rate > 1000)
83681f82884SAlfred Perlstein 		rate = 1000;
83781f82884SAlfred Perlstein 	/* Check for set rate */
83881f82884SAlfred Perlstein 	if ((rate > 0) && (sc->sc_xfer[UMS_INTR_DT] != NULL)) {
83981f82884SAlfred Perlstein 		DPRINTF("Setting pollrate = %d\n", rate);
84081f82884SAlfred Perlstein 		/* Stop current transfer, if any */
84181f82884SAlfred Perlstein 		usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
84281f82884SAlfred Perlstein 		/* Set new interval */
84381f82884SAlfred Perlstein 		usbd_xfer_set_interval(sc->sc_xfer[UMS_INTR_DT], 1000 / rate);
84481f82884SAlfred Perlstein 		/* Only set pollrate once */
84581f82884SAlfred Perlstein 		sc->sc_pollrate = 0;
84681f82884SAlfred Perlstein 	}
84702ac6454SAndrew Thompson 
848a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UMS_INTR_DT]);
84902ac6454SAndrew Thompson }
85002ac6454SAndrew Thompson 
85102ac6454SAndrew Thompson static void
852c3af259aSOleksandr Tymoshenko ums_stop_rx(struct ums_softc *sc)
853c3af259aSOleksandr Tymoshenko {
854c3af259aSOleksandr Tymoshenko 	usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
855c3af259aSOleksandr Tymoshenko 	usb_callout_stop(&sc->sc_callout);
856c3af259aSOleksandr Tymoshenko }
857c3af259aSOleksandr Tymoshenko 
858c3af259aSOleksandr Tymoshenko static void
859c3af259aSOleksandr Tymoshenko ums_fifo_start_read(struct usb_fifo *fifo)
86002ac6454SAndrew Thompson {
861ed6d949aSAndrew Thompson 	struct ums_softc *sc = usb_fifo_softc(fifo);
86202ac6454SAndrew Thompson 
863c3af259aSOleksandr Tymoshenko 	ums_start_rx(sc);
864c3af259aSOleksandr Tymoshenko }
865c3af259aSOleksandr Tymoshenko 
866c3af259aSOleksandr Tymoshenko static void
867c3af259aSOleksandr Tymoshenko ums_fifo_stop_read(struct usb_fifo *fifo)
868c3af259aSOleksandr Tymoshenko {
869c3af259aSOleksandr Tymoshenko 	struct ums_softc *sc = usb_fifo_softc(fifo);
870c3af259aSOleksandr Tymoshenko 
871c3af259aSOleksandr Tymoshenko 	ums_stop_rx(sc);
87202ac6454SAndrew Thompson }
87302ac6454SAndrew Thompson 
87402ac6454SAndrew Thompson 
87502ac6454SAndrew Thompson #if ((MOUSE_SYS_PACKETSIZE != 8) || \
87602ac6454SAndrew Thompson      (MOUSE_MSC_PACKETSIZE != 5))
87702ac6454SAndrew Thompson #error "Software assumptions are not met. Please update code."
87802ac6454SAndrew Thompson #endif
87902ac6454SAndrew Thompson 
88002ac6454SAndrew Thompson static void
88102ac6454SAndrew Thompson ums_put_queue(struct ums_softc *sc, int32_t dx, int32_t dy,
88202ac6454SAndrew Thompson     int32_t dz, int32_t dt, int32_t buttons)
88302ac6454SAndrew Thompson {
88402ac6454SAndrew Thompson 	uint8_t buf[8];
88502ac6454SAndrew Thompson 
88602ac6454SAndrew Thompson 	if (1) {
88702ac6454SAndrew Thompson 
88802ac6454SAndrew Thompson 		if (dx > 254)
88902ac6454SAndrew Thompson 			dx = 254;
89002ac6454SAndrew Thompson 		if (dx < -256)
89102ac6454SAndrew Thompson 			dx = -256;
89202ac6454SAndrew Thompson 		if (dy > 254)
89302ac6454SAndrew Thompson 			dy = 254;
89402ac6454SAndrew Thompson 		if (dy < -256)
89502ac6454SAndrew Thompson 			dy = -256;
89602ac6454SAndrew Thompson 		if (dz > 126)
89702ac6454SAndrew Thompson 			dz = 126;
89802ac6454SAndrew Thompson 		if (dz < -128)
89902ac6454SAndrew Thompson 			dz = -128;
90002ac6454SAndrew Thompson 		if (dt > 126)
90102ac6454SAndrew Thompson 			dt = 126;
90202ac6454SAndrew Thompson 		if (dt < -128)
90302ac6454SAndrew Thompson 			dt = -128;
90402ac6454SAndrew Thompson 
90502ac6454SAndrew Thompson 		buf[0] = sc->sc_mode.syncmask[1];
90602ac6454SAndrew Thompson 		buf[0] |= (~buttons) & MOUSE_MSC_BUTTONS;
90702ac6454SAndrew Thompson 		buf[1] = dx >> 1;
90802ac6454SAndrew Thompson 		buf[2] = dy >> 1;
90902ac6454SAndrew Thompson 		buf[3] = dx - (dx >> 1);
91002ac6454SAndrew Thompson 		buf[4] = dy - (dy >> 1);
91102ac6454SAndrew Thompson 
91202ac6454SAndrew Thompson 		if (sc->sc_mode.level == 1) {
91302ac6454SAndrew Thompson 			buf[5] = dz >> 1;
91402ac6454SAndrew Thompson 			buf[6] = dz - (dz >> 1);
91502ac6454SAndrew Thompson 			buf[7] = (((~buttons) >> 3) & MOUSE_SYS_EXTBUTTONS);
91602ac6454SAndrew Thompson 		}
917a593f6b8SAndrew Thompson 		usb_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf,
91802ac6454SAndrew Thompson 		    sc->sc_mode.packetsize, 1);
919e012a280SOleksandr Tymoshenko 	} else {
920e012a280SOleksandr Tymoshenko 		DPRINTF("Buffer full, discarded packet\n");
921e012a280SOleksandr Tymoshenko 	}
922e012a280SOleksandr Tymoshenko }
92302ac6454SAndrew Thompson 
924a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
925e012a280SOleksandr Tymoshenko static void
926e012a280SOleksandr Tymoshenko ums_evdev_push(struct ums_softc *sc, int32_t dx, int32_t dy,
927e012a280SOleksandr Tymoshenko     int32_t dz, int32_t dt, int32_t buttons)
928e012a280SOleksandr Tymoshenko {
929c3af259aSOleksandr Tymoshenko 	if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) {
930c3af259aSOleksandr Tymoshenko 		/* Push evdev event */
931e012a280SOleksandr Tymoshenko 		evdev_push_rel(sc->sc_evdev, REL_X, dx);
932e012a280SOleksandr Tymoshenko 		evdev_push_rel(sc->sc_evdev, REL_Y, -dy);
933e012a280SOleksandr Tymoshenko 		evdev_push_rel(sc->sc_evdev, REL_WHEEL, -dz);
934e012a280SOleksandr Tymoshenko 		evdev_push_rel(sc->sc_evdev, REL_HWHEEL, dt);
935c3af259aSOleksandr Tymoshenko 		evdev_push_mouse_btn(sc->sc_evdev,
936c3af259aSOleksandr Tymoshenko 		    (buttons & ~MOUSE_STDBUTTONS) |
937c3af259aSOleksandr Tymoshenko 		    (buttons & (1 << 2) ? MOUSE_BUTTON1DOWN : 0) |
938c3af259aSOleksandr Tymoshenko 		    (buttons & (1 << 1) ? MOUSE_BUTTON2DOWN : 0) |
939c3af259aSOleksandr Tymoshenko 		    (buttons & (1 << 0) ? MOUSE_BUTTON3DOWN : 0));
940c3af259aSOleksandr Tymoshenko 		evdev_sync(sc->sc_evdev);
941c3af259aSOleksandr Tymoshenko 	}
942e012a280SOleksandr Tymoshenko }
943c3af259aSOleksandr Tymoshenko #endif
94402ac6454SAndrew Thompson 
94502ac6454SAndrew Thompson static void
94602ac6454SAndrew Thompson ums_reset_buf(struct ums_softc *sc)
94702ac6454SAndrew Thompson {
94832e79f55SHans Petter Selasky 	/* reset read queue, must be called locked */
949a593f6b8SAndrew Thompson 	usb_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]);
95002ac6454SAndrew Thompson }
95102ac6454SAndrew Thompson 
952a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
95302ac6454SAndrew Thompson static int
954911aed94SVladimir Kondratyev ums_ev_open(struct evdev_dev *evdev)
955c3af259aSOleksandr Tymoshenko {
956911aed94SVladimir Kondratyev 	struct ums_softc *sc = evdev_get_softc(evdev);
957c3af259aSOleksandr Tymoshenko 
958e012a280SOleksandr Tymoshenko 	mtx_assert(&sc->sc_mtx, MA_OWNED);
959c3af259aSOleksandr Tymoshenko 
960c3af259aSOleksandr Tymoshenko 	sc->sc_evflags = UMS_EVDEV_OPENED;
961c3af259aSOleksandr Tymoshenko 
962c3af259aSOleksandr Tymoshenko 	if (sc->sc_fflags == 0) {
963c3af259aSOleksandr Tymoshenko 		ums_reset(sc);
964c3af259aSOleksandr Tymoshenko 		ums_start_rx(sc);
965c3af259aSOleksandr Tymoshenko 	}
966c3af259aSOleksandr Tymoshenko 
967c3af259aSOleksandr Tymoshenko 	return (0);
968c3af259aSOleksandr Tymoshenko }
969c3af259aSOleksandr Tymoshenko 
970911aed94SVladimir Kondratyev static int
971911aed94SVladimir Kondratyev ums_ev_close(struct evdev_dev *evdev)
972c3af259aSOleksandr Tymoshenko {
973911aed94SVladimir Kondratyev 	struct ums_softc *sc = evdev_get_softc(evdev);
974c3af259aSOleksandr Tymoshenko 
975e012a280SOleksandr Tymoshenko 	mtx_assert(&sc->sc_mtx, MA_OWNED);
976c3af259aSOleksandr Tymoshenko 
977c3af259aSOleksandr Tymoshenko 	sc->sc_evflags = 0;
978c3af259aSOleksandr Tymoshenko 
979c3af259aSOleksandr Tymoshenko 	if (sc->sc_fflags == 0)
980c3af259aSOleksandr Tymoshenko 		ums_stop_rx(sc);
981911aed94SVladimir Kondratyev 
982911aed94SVladimir Kondratyev 	return (0);
983c3af259aSOleksandr Tymoshenko }
984c3af259aSOleksandr Tymoshenko #endif
985c3af259aSOleksandr Tymoshenko 
986c3af259aSOleksandr Tymoshenko static int
987c3af259aSOleksandr Tymoshenko ums_fifo_open(struct usb_fifo *fifo, int fflags)
98802ac6454SAndrew Thompson {
989ed6d949aSAndrew Thompson 	struct ums_softc *sc = usb_fifo_softc(fifo);
99002ac6454SAndrew Thompson 
99102ac6454SAndrew Thompson 	DPRINTFN(2, "\n");
99202ac6454SAndrew Thompson 
99332e79f55SHans Petter Selasky 	/* check for duplicate open, should not happen */
99432e79f55SHans Petter Selasky 	if (sc->sc_fflags & fflags)
99532e79f55SHans Petter Selasky 		return (EBUSY);
99632e79f55SHans Petter Selasky 
99732e79f55SHans Petter Selasky 	/* check for first open */
998a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
999c3af259aSOleksandr Tymoshenko 	if (sc->sc_fflags == 0 && sc->sc_evflags == 0)
1000c3af259aSOleksandr Tymoshenko 		ums_reset(sc);
1001c3af259aSOleksandr Tymoshenko #else
1002c3af259aSOleksandr Tymoshenko 	if (sc->sc_fflags == 0)
1003c3af259aSOleksandr Tymoshenko 		ums_reset(sc);
1004c3af259aSOleksandr Tymoshenko #endif
100502ac6454SAndrew Thompson 
100632e79f55SHans Petter Selasky 	if (fflags & FREAD) {
100732e79f55SHans Petter Selasky 		/* allocate RX buffer */
1008a593f6b8SAndrew Thompson 		if (usb_fifo_alloc_buffer(fifo,
100902ac6454SAndrew Thompson 		    UMS_BUF_SIZE, UMS_IFQ_MAXLEN)) {
101002ac6454SAndrew Thompson 			return (ENOMEM);
101102ac6454SAndrew Thompson 		}
101202ac6454SAndrew Thompson 	}
101332e79f55SHans Petter Selasky 
101432e79f55SHans Petter Selasky 	sc->sc_fflags |= fflags & (FREAD | FWRITE);
101502ac6454SAndrew Thompson 	return (0);
101602ac6454SAndrew Thompson }
101702ac6454SAndrew Thompson 
101802ac6454SAndrew Thompson static void
1019c3af259aSOleksandr Tymoshenko ums_fifo_close(struct usb_fifo *fifo, int fflags)
102002ac6454SAndrew Thompson {
102132e79f55SHans Petter Selasky 	struct ums_softc *sc = usb_fifo_softc(fifo);
102232e79f55SHans Petter Selasky 
102332e79f55SHans Petter Selasky 	DPRINTFN(2, "\n");
102432e79f55SHans Petter Selasky 
102532e79f55SHans Petter Selasky 	if (fflags & FREAD)
1026a593f6b8SAndrew Thompson 		usb_fifo_free_buffer(fifo);
102732e79f55SHans Petter Selasky 
102832e79f55SHans Petter Selasky 	sc->sc_fflags &= ~(fflags & (FREAD | FWRITE));
102902ac6454SAndrew Thompson }
103002ac6454SAndrew Thompson 
103102ac6454SAndrew Thompson static int
1032c3af259aSOleksandr Tymoshenko ums_fifo_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
103302ac6454SAndrew Thompson {
1034ed6d949aSAndrew Thompson 	struct ums_softc *sc = usb_fifo_softc(fifo);
103502ac6454SAndrew Thompson 	mousemode_t mode;
103602ac6454SAndrew Thompson 	int error = 0;
103702ac6454SAndrew Thompson 
103802ac6454SAndrew Thompson 	DPRINTFN(2, "\n");
103902ac6454SAndrew Thompson 
104002ac6454SAndrew Thompson 	mtx_lock(&sc->sc_mtx);
104102ac6454SAndrew Thompson 
104202ac6454SAndrew Thompson 	switch (cmd) {
104302ac6454SAndrew Thompson 	case MOUSE_GETHWINFO:
104402ac6454SAndrew Thompson 		*(mousehw_t *)addr = sc->sc_hw;
104502ac6454SAndrew Thompson 		break;
104602ac6454SAndrew Thompson 
104702ac6454SAndrew Thompson 	case MOUSE_GETMODE:
104802ac6454SAndrew Thompson 		*(mousemode_t *)addr = sc->sc_mode;
104902ac6454SAndrew Thompson 		break;
105002ac6454SAndrew Thompson 
105102ac6454SAndrew Thompson 	case MOUSE_SETMODE:
105202ac6454SAndrew Thompson 		mode = *(mousemode_t *)addr;
105302ac6454SAndrew Thompson 
105402ac6454SAndrew Thompson 		if (mode.level == -1) {
105502ac6454SAndrew Thompson 			/* don't change the current setting */
105602ac6454SAndrew Thompson 		} else if ((mode.level < 0) || (mode.level > 1)) {
105702ac6454SAndrew Thompson 			error = EINVAL;
105832e79f55SHans Petter Selasky 			break;
105902ac6454SAndrew Thompson 		} else {
106002ac6454SAndrew Thompson 			sc->sc_mode.level = mode.level;
106102ac6454SAndrew Thompson 		}
106202ac6454SAndrew Thompson 
106381f82884SAlfred Perlstein 		/* store polling rate */
106481f82884SAlfred Perlstein 		sc->sc_pollrate = mode.rate;
106581f82884SAlfred Perlstein 
106602ac6454SAndrew Thompson 		if (sc->sc_mode.level == 0) {
106702ac6454SAndrew Thompson 			if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
106802ac6454SAndrew Thompson 				sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
106902ac6454SAndrew Thompson 			else
107002ac6454SAndrew Thompson 				sc->sc_hw.buttons = sc->sc_buttons;
107102ac6454SAndrew Thompson 			sc->sc_mode.protocol = MOUSE_PROTO_MSC;
107202ac6454SAndrew Thompson 			sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
107302ac6454SAndrew Thompson 			sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
107402ac6454SAndrew Thompson 			sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
107502ac6454SAndrew Thompson 		} else if (sc->sc_mode.level == 1) {
107602ac6454SAndrew Thompson 			if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
107702ac6454SAndrew Thompson 				sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
107802ac6454SAndrew Thompson 			else
107902ac6454SAndrew Thompson 				sc->sc_hw.buttons = sc->sc_buttons;
108002ac6454SAndrew Thompson 			sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
108102ac6454SAndrew Thompson 			sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
108202ac6454SAndrew Thompson 			sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
108302ac6454SAndrew Thompson 			sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
108402ac6454SAndrew Thompson 		}
108502ac6454SAndrew Thompson 		ums_reset_buf(sc);
108602ac6454SAndrew Thompson 		break;
108702ac6454SAndrew Thompson 
108802ac6454SAndrew Thompson 	case MOUSE_GETLEVEL:
108902ac6454SAndrew Thompson 		*(int *)addr = sc->sc_mode.level;
109002ac6454SAndrew Thompson 		break;
109102ac6454SAndrew Thompson 
109202ac6454SAndrew Thompson 	case MOUSE_SETLEVEL:
109302ac6454SAndrew Thompson 		if (*(int *)addr < 0 || *(int *)addr > 1) {
109402ac6454SAndrew Thompson 			error = EINVAL;
109532e79f55SHans Petter Selasky 			break;
109602ac6454SAndrew Thompson 		}
109702ac6454SAndrew Thompson 		sc->sc_mode.level = *(int *)addr;
109802ac6454SAndrew Thompson 
109902ac6454SAndrew Thompson 		if (sc->sc_mode.level == 0) {
110002ac6454SAndrew Thompson 			if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
110102ac6454SAndrew Thompson 				sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
110202ac6454SAndrew Thompson 			else
110302ac6454SAndrew Thompson 				sc->sc_hw.buttons = sc->sc_buttons;
110402ac6454SAndrew Thompson 			sc->sc_mode.protocol = MOUSE_PROTO_MSC;
110502ac6454SAndrew Thompson 			sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
110602ac6454SAndrew Thompson 			sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
110702ac6454SAndrew Thompson 			sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
110802ac6454SAndrew Thompson 		} else if (sc->sc_mode.level == 1) {
110902ac6454SAndrew Thompson 			if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
111002ac6454SAndrew Thompson 				sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
111102ac6454SAndrew Thompson 			else
111202ac6454SAndrew Thompson 				sc->sc_hw.buttons = sc->sc_buttons;
111302ac6454SAndrew Thompson 			sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
111402ac6454SAndrew Thompson 			sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
111502ac6454SAndrew Thompson 			sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
111602ac6454SAndrew Thompson 			sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
111702ac6454SAndrew Thompson 		}
111802ac6454SAndrew Thompson 		ums_reset_buf(sc);
111902ac6454SAndrew Thompson 		break;
112002ac6454SAndrew Thompson 
112102ac6454SAndrew Thompson 	case MOUSE_GETSTATUS:{
112202ac6454SAndrew Thompson 			mousestatus_t *status = (mousestatus_t *)addr;
112302ac6454SAndrew Thompson 
112402ac6454SAndrew Thompson 			*status = sc->sc_status;
112502ac6454SAndrew Thompson 			sc->sc_status.obutton = sc->sc_status.button;
112602ac6454SAndrew Thompson 			sc->sc_status.button = 0;
112702ac6454SAndrew Thompson 			sc->sc_status.dx = 0;
112802ac6454SAndrew Thompson 			sc->sc_status.dy = 0;
112902ac6454SAndrew Thompson 			sc->sc_status.dz = 0;
113002ac6454SAndrew Thompson 			/* sc->sc_status.dt = 0; */
113102ac6454SAndrew Thompson 
113202ac6454SAndrew Thompson 			if (status->dx || status->dy || status->dz /* || status->dt */ ) {
113302ac6454SAndrew Thompson 				status->flags |= MOUSE_POSCHANGED;
113402ac6454SAndrew Thompson 			}
113502ac6454SAndrew Thompson 			if (status->button != status->obutton) {
113602ac6454SAndrew Thompson 				status->flags |= MOUSE_BUTTONSCHANGED;
113702ac6454SAndrew Thompson 			}
113802ac6454SAndrew Thompson 			break;
113902ac6454SAndrew Thompson 		}
114002ac6454SAndrew Thompson 	default:
114102ac6454SAndrew Thompson 		error = ENOTTY;
114232e79f55SHans Petter Selasky 		break;
114302ac6454SAndrew Thompson 	}
114402ac6454SAndrew Thompson 
114502ac6454SAndrew Thompson 	mtx_unlock(&sc->sc_mtx);
114602ac6454SAndrew Thompson 	return (error);
114702ac6454SAndrew Thompson }
114802ac6454SAndrew Thompson 
11494ed0304cSAndrew Thompson static int
11504ed0304cSAndrew Thompson ums_sysctl_handler_parseinfo(SYSCTL_HANDLER_ARGS)
11514ed0304cSAndrew Thompson {
11524ed0304cSAndrew Thompson 	struct ums_softc *sc = arg1;
11534ed0304cSAndrew Thompson 	struct ums_info *info;
11544ed0304cSAndrew Thompson 	struct sbuf *sb;
1155b635069cSHans Petter Selasky 	int i, j, err, had_output;
11564ed0304cSAndrew Thompson 
11574ed0304cSAndrew Thompson 	sb = sbuf_new_auto();
1158b635069cSHans Petter Selasky 	for (i = 0, had_output = 0; i < UMS_INFO_MAX; i++) {
11594ed0304cSAndrew Thompson 		info = &sc->sc_info[i];
11604ed0304cSAndrew Thompson 
11614ed0304cSAndrew Thompson 		/* Don't emit empty info */
11624ed0304cSAndrew Thompson 		if ((info->sc_flags &
11634ed0304cSAndrew Thompson 		    (UMS_FLAG_X_AXIS | UMS_FLAG_Y_AXIS | UMS_FLAG_Z_AXIS |
11644ed0304cSAndrew Thompson 		     UMS_FLAG_T_AXIS | UMS_FLAG_W_AXIS)) == 0 &&
11654ed0304cSAndrew Thompson 		    info->sc_buttons == 0)
11664ed0304cSAndrew Thompson 			continue;
11674ed0304cSAndrew Thompson 
1168b635069cSHans Petter Selasky 		if (had_output)
1169b635069cSHans Petter Selasky 			sbuf_printf(sb, "\n");
1170b635069cSHans Petter Selasky 		had_output = 1;
11714ed0304cSAndrew Thompson 		sbuf_printf(sb, "i%d:", i + 1);
11724ed0304cSAndrew Thompson 		if (info->sc_flags & UMS_FLAG_X_AXIS)
11734ed0304cSAndrew Thompson 			sbuf_printf(sb, " X:r%d, p%d, s%d;",
11744ed0304cSAndrew Thompson 			    (int)info->sc_iid_x,
11754ed0304cSAndrew Thompson 			    (int)info->sc_loc_x.pos,
11764ed0304cSAndrew Thompson 			    (int)info->sc_loc_x.size);
11774ed0304cSAndrew Thompson 		if (info->sc_flags & UMS_FLAG_Y_AXIS)
11784ed0304cSAndrew Thompson 			sbuf_printf(sb, " Y:r%d, p%d, s%d;",
11794ed0304cSAndrew Thompson 			    (int)info->sc_iid_y,
11804ed0304cSAndrew Thompson 			    (int)info->sc_loc_y.pos,
11814ed0304cSAndrew Thompson 			    (int)info->sc_loc_y.size);
11824ed0304cSAndrew Thompson 		if (info->sc_flags & UMS_FLAG_Z_AXIS)
11834ed0304cSAndrew Thompson 			sbuf_printf(sb, " Z:r%d, p%d, s%d;",
11844ed0304cSAndrew Thompson 			    (int)info->sc_iid_z,
11854ed0304cSAndrew Thompson 			    (int)info->sc_loc_z.pos,
11864ed0304cSAndrew Thompson 			    (int)info->sc_loc_z.size);
11874ed0304cSAndrew Thompson 		if (info->sc_flags & UMS_FLAG_T_AXIS)
11884ed0304cSAndrew Thompson 			sbuf_printf(sb, " T:r%d, p%d, s%d;",
11894ed0304cSAndrew Thompson 			    (int)info->sc_iid_t,
11904ed0304cSAndrew Thompson 			    (int)info->sc_loc_t.pos,
11914ed0304cSAndrew Thompson 			    (int)info->sc_loc_t.size);
11924ed0304cSAndrew Thompson 		if (info->sc_flags & UMS_FLAG_W_AXIS)
11934ed0304cSAndrew Thompson 			sbuf_printf(sb, " W:r%d, p%d, s%d;",
11944ed0304cSAndrew Thompson 			    (int)info->sc_iid_w,
11954ed0304cSAndrew Thompson 			    (int)info->sc_loc_w.pos,
11964ed0304cSAndrew Thompson 			    (int)info->sc_loc_w.size);
11974ed0304cSAndrew Thompson 
11984ed0304cSAndrew Thompson 		for (j = 0; j < info->sc_buttons; j++) {
11994ed0304cSAndrew Thompson 			sbuf_printf(sb, " B%d:r%d, p%d, s%d;", j + 1,
12004ed0304cSAndrew Thompson 			    (int)info->sc_iid_btn[j],
12014ed0304cSAndrew Thompson 			    (int)info->sc_loc_btn[j].pos,
12024ed0304cSAndrew Thompson 			    (int)info->sc_loc_btn[j].size);
12034ed0304cSAndrew Thompson 		}
12044ed0304cSAndrew Thompson 	}
12054ed0304cSAndrew Thompson 	sbuf_finish(sb);
12064ed0304cSAndrew Thompson 	err = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
12074ed0304cSAndrew Thompson 	sbuf_delete(sb);
12084ed0304cSAndrew Thompson 
12094ed0304cSAndrew Thompson 	return (err);
12104ed0304cSAndrew Thompson }
12114ed0304cSAndrew Thompson 
121202ac6454SAndrew Thompson static devclass_t ums_devclass;
121302ac6454SAndrew Thompson 
121402ac6454SAndrew Thompson static device_method_t ums_methods[] = {
121502ac6454SAndrew Thompson 	DEVMETHOD(device_probe, ums_probe),
121602ac6454SAndrew Thompson 	DEVMETHOD(device_attach, ums_attach),
121702ac6454SAndrew Thompson 	DEVMETHOD(device_detach, ums_detach),
121861bfd867SSofian Brabez 
121961bfd867SSofian Brabez 	DEVMETHOD_END
122002ac6454SAndrew Thompson };
122102ac6454SAndrew Thompson 
122202ac6454SAndrew Thompson static driver_t ums_driver = {
122302ac6454SAndrew Thompson 	.name = "ums",
122402ac6454SAndrew Thompson 	.methods = ums_methods,
122502ac6454SAndrew Thompson 	.size = sizeof(struct ums_softc),
122602ac6454SAndrew Thompson };
122702ac6454SAndrew Thompson 
12289aef556dSAndrew Thompson DRIVER_MODULE(ums, uhub, ums_driver, ums_devclass, NULL, 0);
122902ac6454SAndrew Thompson MODULE_DEPEND(ums, usb, 1, 1, 1);
1230a6b15a34SOleksandr Tymoshenko #ifdef EVDEV_SUPPORT
1231fa26e8edSOleksandr Tymoshenko MODULE_DEPEND(ums, evdev, 1, 1, 1);
1232fa26e8edSOleksandr Tymoshenko #endif
1233910cb8feSAndrew Thompson MODULE_VERSION(ums, 1);
1234f809f280SWarner Losh USB_PNP_HOST_INFO(ums_devs);
1235