xref: /freebsd/sys/dev/usb/input/ums.c (revision ed6d949afdbe3f25e6ef640881d8a0a72d7b7aa8)
102ac6454SAndrew Thompson /*-
202ac6454SAndrew Thompson  * Copyright (c) 1998 The NetBSD Foundation, Inc.
302ac6454SAndrew Thompson  * All rights reserved.
402ac6454SAndrew Thompson  *
502ac6454SAndrew Thompson  * This code is derived from software contributed to The NetBSD Foundation
602ac6454SAndrew Thompson  * by Lennart Augustsson (lennart@augustsson.net) at
702ac6454SAndrew Thompson  * Carlstedt Research & Technology.
802ac6454SAndrew Thompson  *
902ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
1002ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
1102ac6454SAndrew Thompson  * are met:
1202ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1302ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1402ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1502ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1602ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1702ac6454SAndrew Thompson  * 3. All advertising materials mentioning features or use of this software
1802ac6454SAndrew Thompson  *    must display the following acknowledgement:
1902ac6454SAndrew Thompson  *        This product includes software developed by the NetBSD
2002ac6454SAndrew Thompson  *        Foundation, Inc. and its contributors.
2102ac6454SAndrew Thompson  * 4. Neither the name of The NetBSD Foundation nor the names of its
2202ac6454SAndrew Thompson  *    contributors may be used to endorse or promote products derived
2302ac6454SAndrew Thompson  *    from this software without specific prior written permission.
2402ac6454SAndrew Thompson  *
2502ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2602ac6454SAndrew Thompson  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2702ac6454SAndrew Thompson  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2802ac6454SAndrew Thompson  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2902ac6454SAndrew Thompson  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3002ac6454SAndrew Thompson  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3102ac6454SAndrew Thompson  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3202ac6454SAndrew Thompson  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3302ac6454SAndrew Thompson  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3402ac6454SAndrew Thompson  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3502ac6454SAndrew Thompson  * POSSIBILITY OF SUCH DAMAGE.
3602ac6454SAndrew Thompson  */
3702ac6454SAndrew Thompson 
3802ac6454SAndrew Thompson #include <sys/cdefs.h>
3902ac6454SAndrew Thompson __FBSDID("$FreeBSD$");
4002ac6454SAndrew Thompson 
4102ac6454SAndrew Thompson /*
4202ac6454SAndrew Thompson  * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
4302ac6454SAndrew Thompson  */
4402ac6454SAndrew Thompson 
45ed6d949aSAndrew Thompson #include <sys/stdint.h>
46ed6d949aSAndrew Thompson #include <sys/stddef.h>
47ed6d949aSAndrew Thompson #include <sys/param.h>
48ed6d949aSAndrew Thompson #include <sys/queue.h>
49ed6d949aSAndrew Thompson #include <sys/types.h>
50ed6d949aSAndrew Thompson #include <sys/systm.h>
51ed6d949aSAndrew Thompson #include <sys/kernel.h>
52ed6d949aSAndrew Thompson #include <sys/bus.h>
53ed6d949aSAndrew Thompson #include <sys/linker_set.h>
54ed6d949aSAndrew Thompson #include <sys/module.h>
55ed6d949aSAndrew Thompson #include <sys/lock.h>
56ed6d949aSAndrew Thompson #include <sys/mutex.h>
57ed6d949aSAndrew Thompson #include <sys/condvar.h>
58ed6d949aSAndrew Thompson #include <sys/sysctl.h>
59ed6d949aSAndrew Thompson #include <sys/sx.h>
60ed6d949aSAndrew Thompson #include <sys/unistd.h>
61ed6d949aSAndrew Thompson #include <sys/callout.h>
62ed6d949aSAndrew Thompson #include <sys/malloc.h>
63ed6d949aSAndrew Thompson #include <sys/priv.h>
64ed6d949aSAndrew Thompson #include <sys/conf.h>
65ed6d949aSAndrew Thompson #include <sys/fcntl.h>
66ed6d949aSAndrew Thompson 
6702ac6454SAndrew Thompson #include <dev/usb/usb.h>
68ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
69ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h>
7002ac6454SAndrew Thompson #include <dev/usb/usbhid.h>
71ed6d949aSAndrew Thompson #include "usbdevs.h"
7202ac6454SAndrew Thompson 
7302ac6454SAndrew Thompson #define	USB_DEBUG_VAR ums_debug
7402ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
7502ac6454SAndrew Thompson 
7602ac6454SAndrew Thompson #include <dev/usb/quirk/usb_quirk.h>
7702ac6454SAndrew Thompson 
7802ac6454SAndrew Thompson #include <sys/ioccom.h>
7902ac6454SAndrew Thompson #include <sys/filio.h>
8002ac6454SAndrew Thompson #include <sys/tty.h>
8102ac6454SAndrew Thompson #include <sys/mouse.h>
8202ac6454SAndrew Thompson 
8302ac6454SAndrew Thompson #if USB_DEBUG
8402ac6454SAndrew Thompson static int ums_debug = 0;
8502ac6454SAndrew Thompson 
869360ae40SAndrew Thompson SYSCTL_NODE(_hw_usb, OID_AUTO, ums, CTLFLAG_RW, 0, "USB ums");
879360ae40SAndrew Thompson SYSCTL_INT(_hw_usb_ums, OID_AUTO, debug, CTLFLAG_RW,
8802ac6454SAndrew Thompson     &ums_debug, 0, "Debug level");
8902ac6454SAndrew Thompson #endif
9002ac6454SAndrew Thompson 
9102ac6454SAndrew Thompson #define	MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
9202ac6454SAndrew Thompson #define	MOUSE_FLAGS (HIO_RELATIVE)
9302ac6454SAndrew Thompson 
9402ac6454SAndrew Thompson #define	UMS_BUF_SIZE      8		/* bytes */
9502ac6454SAndrew Thompson #define	UMS_IFQ_MAXLEN   50		/* units */
9602ac6454SAndrew Thompson #define	UMS_BUTTON_MAX   31		/* exclusive, must be less than 32 */
9702ac6454SAndrew Thompson #define	UMS_BUT(i) ((i) < 3 ? (((i) + 2) % 3) : (i))
98de967835SAndrew Thompson #define	UMS_INFO_MAX	  2		/* maximum number of HID sets */
9902ac6454SAndrew Thompson 
10002ac6454SAndrew Thompson enum {
10102ac6454SAndrew Thompson 	UMS_INTR_DT,
1023e5e312aSAndrew Thompson 	UMS_N_TRANSFER,
10302ac6454SAndrew Thompson };
10402ac6454SAndrew Thompson 
105de967835SAndrew Thompson struct ums_info {
10602ac6454SAndrew Thompson 	struct hid_location sc_loc_w;
10702ac6454SAndrew Thompson 	struct hid_location sc_loc_x;
10802ac6454SAndrew Thompson 	struct hid_location sc_loc_y;
10902ac6454SAndrew Thompson 	struct hid_location sc_loc_z;
11002ac6454SAndrew Thompson 	struct hid_location sc_loc_t;
11102ac6454SAndrew Thompson 	struct hid_location sc_loc_btn[UMS_BUTTON_MAX];
11202ac6454SAndrew Thompson 
11302ac6454SAndrew Thompson 	uint32_t sc_flags;
11402ac6454SAndrew Thompson #define	UMS_FLAG_X_AXIS     0x0001
11502ac6454SAndrew Thompson #define	UMS_FLAG_Y_AXIS     0x0002
11602ac6454SAndrew Thompson #define	UMS_FLAG_Z_AXIS     0x0004
11702ac6454SAndrew Thompson #define	UMS_FLAG_T_AXIS     0x0008
11802ac6454SAndrew Thompson #define	UMS_FLAG_SBU        0x0010	/* spurious button up events */
1193e5e312aSAndrew Thompson #define	UMS_FLAG_REVZ	    0x0020	/* Z-axis is reversed */
1203e5e312aSAndrew Thompson #define	UMS_FLAG_W_AXIS     0x0040
12102ac6454SAndrew Thompson 
122bf87f556SAndrew Thompson 	uint8_t	sc_iid_w;
123bf87f556SAndrew Thompson 	uint8_t	sc_iid_x;
124bf87f556SAndrew Thompson 	uint8_t	sc_iid_y;
125bf87f556SAndrew Thompson 	uint8_t	sc_iid_z;
126bf87f556SAndrew Thompson 	uint8_t	sc_iid_t;
127bf87f556SAndrew Thompson 	uint8_t	sc_iid_btn[UMS_BUTTON_MAX];
128de967835SAndrew Thompson 	uint8_t	sc_buttons;
129de967835SAndrew Thompson };
130de967835SAndrew Thompson 
131de967835SAndrew Thompson struct ums_softc {
132760bc48eSAndrew Thompson 	struct usb_fifo_sc sc_fifo;
133de967835SAndrew Thompson 	struct mtx sc_mtx;
134760bc48eSAndrew Thompson 	struct usb_callout sc_callout;
135de967835SAndrew Thompson 	struct ums_info sc_info[UMS_INFO_MAX];
136de967835SAndrew Thompson 
137de967835SAndrew Thompson 	mousehw_t sc_hw;
138de967835SAndrew Thompson 	mousemode_t sc_mode;
139de967835SAndrew Thompson 	mousestatus_t sc_status;
140de967835SAndrew Thompson 
141760bc48eSAndrew Thompson 	struct usb_xfer *sc_xfer[UMS_N_TRANSFER];
142de967835SAndrew Thompson 
143de967835SAndrew Thompson 	uint8_t	sc_buttons;
144de967835SAndrew Thompson 	uint8_t	sc_iid;
14502ac6454SAndrew Thompson 	uint8_t	sc_temp[64];
14602ac6454SAndrew Thompson };
14702ac6454SAndrew Thompson 
14802ac6454SAndrew Thompson static void ums_put_queue_timeout(void *__sc);
14902ac6454SAndrew Thompson 
150e0a69b51SAndrew Thompson static usb_callback_t ums_intr_callback;
15102ac6454SAndrew Thompson 
15202ac6454SAndrew Thompson static device_probe_t ums_probe;
15302ac6454SAndrew Thompson static device_attach_t ums_attach;
15402ac6454SAndrew Thompson static device_detach_t ums_detach;
15502ac6454SAndrew Thompson 
156e0a69b51SAndrew Thompson static usb_fifo_cmd_t ums_start_read;
157e0a69b51SAndrew Thompson static usb_fifo_cmd_t ums_stop_read;
158e0a69b51SAndrew Thompson static usb_fifo_open_t ums_open;
159e0a69b51SAndrew Thompson static usb_fifo_close_t ums_close;
160e0a69b51SAndrew Thompson static usb_fifo_ioctl_t ums_ioctl;
16102ac6454SAndrew Thompson 
16202ac6454SAndrew Thompson static void ums_put_queue(struct ums_softc *sc, int32_t dx, int32_t dy, int32_t dz, int32_t dt, int32_t buttons);
16302ac6454SAndrew Thompson 
164760bc48eSAndrew Thompson static struct usb_fifo_methods ums_fifo_methods = {
16502ac6454SAndrew Thompson 	.f_open = &ums_open,
16602ac6454SAndrew Thompson 	.f_close = &ums_close,
16702ac6454SAndrew Thompson 	.f_ioctl = &ums_ioctl,
16802ac6454SAndrew Thompson 	.f_start_read = &ums_start_read,
16902ac6454SAndrew Thompson 	.f_stop_read = &ums_stop_read,
17002ac6454SAndrew Thompson 	.basename[0] = "ums",
17102ac6454SAndrew Thompson };
17202ac6454SAndrew Thompson 
17302ac6454SAndrew Thompson static void
17402ac6454SAndrew Thompson ums_put_queue_timeout(void *__sc)
17502ac6454SAndrew Thompson {
17602ac6454SAndrew Thompson 	struct ums_softc *sc = __sc;
17702ac6454SAndrew Thompson 
17802ac6454SAndrew Thompson 	mtx_assert(&sc->sc_mtx, MA_OWNED);
17902ac6454SAndrew Thompson 
18002ac6454SAndrew Thompson 	ums_put_queue(sc, 0, 0, 0, 0, 0);
18102ac6454SAndrew Thompson }
18202ac6454SAndrew Thompson 
18302ac6454SAndrew Thompson static void
184ed6d949aSAndrew Thompson ums_intr_callback(struct usb_xfer *xfer, usb_error_t error)
18502ac6454SAndrew Thompson {
186ed6d949aSAndrew Thompson 	struct ums_softc *sc = usbd_xfer_softc(xfer);
187de967835SAndrew Thompson 	struct ums_info *info = &sc->sc_info[0];
188ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
18902ac6454SAndrew Thompson 	uint8_t *buf = sc->sc_temp;
19002ac6454SAndrew Thompson 	int32_t buttons = 0;
191de967835SAndrew Thompson 	int32_t dw = 0;
192de967835SAndrew Thompson 	int32_t dx = 0;
193de967835SAndrew Thompson 	int32_t dy = 0;
194de967835SAndrew Thompson 	int32_t dz = 0;
195de967835SAndrew Thompson 	int32_t dt = 0;
19602ac6454SAndrew Thompson 	uint8_t i;
197bf87f556SAndrew Thompson 	uint8_t id;
198ed6d949aSAndrew Thompson 	int len;
199ed6d949aSAndrew Thompson 
200ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
20102ac6454SAndrew Thompson 
20202ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
20302ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
20402ac6454SAndrew Thompson 		DPRINTFN(6, "sc=%p actlen=%d\n", sc, len);
20502ac6454SAndrew Thompson 
20602ac6454SAndrew Thompson 		if (len > sizeof(sc->sc_temp)) {
20702ac6454SAndrew Thompson 			DPRINTFN(6, "truncating large packet to %zu bytes\n",
20802ac6454SAndrew Thompson 			    sizeof(sc->sc_temp));
20902ac6454SAndrew Thompson 			len = sizeof(sc->sc_temp);
21002ac6454SAndrew Thompson 		}
2113e5e312aSAndrew Thompson 		if (len == 0)
21202ac6454SAndrew Thompson 			goto tr_setup;
2133e5e312aSAndrew Thompson 
214ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
215ed6d949aSAndrew Thompson 		usbd_copy_out(pc, 0, buf, len);
21602ac6454SAndrew Thompson 
21702ac6454SAndrew Thompson 		DPRINTFN(6, "data = %02x %02x %02x %02x "
21802ac6454SAndrew Thompson 		    "%02x %02x %02x %02x\n",
21902ac6454SAndrew Thompson 		    (len > 0) ? buf[0] : 0, (len > 1) ? buf[1] : 0,
22002ac6454SAndrew Thompson 		    (len > 2) ? buf[2] : 0, (len > 3) ? buf[3] : 0,
22102ac6454SAndrew Thompson 		    (len > 4) ? buf[4] : 0, (len > 5) ? buf[5] : 0,
22202ac6454SAndrew Thompson 		    (len > 6) ? buf[6] : 0, (len > 7) ? buf[7] : 0);
22302ac6454SAndrew Thompson 
22402ac6454SAndrew Thompson 		if (sc->sc_iid) {
225bf87f556SAndrew Thompson 			id = *buf;
22602ac6454SAndrew Thompson 
22702ac6454SAndrew Thompson 			len--;
22802ac6454SAndrew Thompson 			buf++;
22902ac6454SAndrew Thompson 
23002ac6454SAndrew Thompson 		} else {
231bf87f556SAndrew Thompson 			id = 0;
232de967835SAndrew Thompson 			if (sc->sc_info[0].sc_flags & UMS_FLAG_SBU) {
23302ac6454SAndrew Thompson 				if ((*buf == 0x14) || (*buf == 0x15)) {
23402ac6454SAndrew Thompson 					goto tr_setup;
23502ac6454SAndrew Thompson 				}
23602ac6454SAndrew Thompson 			}
23702ac6454SAndrew Thompson 		}
23802ac6454SAndrew Thompson 
239de967835SAndrew Thompson 	repeat:
240de967835SAndrew Thompson 		if ((info->sc_flags & UMS_FLAG_W_AXIS) &&
241de967835SAndrew Thompson 		    (id == info->sc_iid_w))
242de967835SAndrew Thompson 			dw += hid_get_data(buf, len, &info->sc_loc_w);
24302ac6454SAndrew Thompson 
244de967835SAndrew Thompson 		if ((info->sc_flags & UMS_FLAG_X_AXIS) &&
245de967835SAndrew Thompson 		    (id == info->sc_iid_x))
246de967835SAndrew Thompson 			dx += hid_get_data(buf, len, &info->sc_loc_x);
24702ac6454SAndrew Thompson 
248de967835SAndrew Thompson 		if ((info->sc_flags & UMS_FLAG_Y_AXIS) &&
249de967835SAndrew Thompson 		    (id == info->sc_iid_y))
250de967835SAndrew Thompson 			dy = -hid_get_data(buf, len, &info->sc_loc_y);
25102ac6454SAndrew Thompson 
252de967835SAndrew Thompson 		if ((info->sc_flags & UMS_FLAG_Z_AXIS) &&
253de967835SAndrew Thompson 		    (id == info->sc_iid_z)) {
254de967835SAndrew Thompson 			int32_t temp;
255de967835SAndrew Thompson 			temp = hid_get_data(buf, len, &info->sc_loc_z);
256de967835SAndrew Thompson 			if (info->sc_flags & UMS_FLAG_REVZ)
257de967835SAndrew Thompson 				temp = -temp;
258de967835SAndrew Thompson 			dz -= temp;
259de967835SAndrew Thompson 		}
26002ac6454SAndrew Thompson 
261de967835SAndrew Thompson 		if ((info->sc_flags & UMS_FLAG_T_AXIS) &&
262de967835SAndrew Thompson 		    (id == info->sc_iid_t))
263de967835SAndrew Thompson 			dt -= hid_get_data(buf, len, &info->sc_loc_t);
264bf87f556SAndrew Thompson 
265de967835SAndrew Thompson 		for (i = 0; i < info->sc_buttons; i++) {
266de967835SAndrew Thompson 			if (id != info->sc_iid_btn[i])
267bf87f556SAndrew Thompson 				continue;
268de967835SAndrew Thompson 			if (hid_get_data(buf, len, &info->sc_loc_btn[i])) {
26902ac6454SAndrew Thompson 				buttons |= (1 << UMS_BUT(i));
27002ac6454SAndrew Thompson 			}
27102ac6454SAndrew Thompson 		}
27202ac6454SAndrew Thompson 
273de967835SAndrew Thompson 		if (++info != &sc->sc_info[UMS_INFO_MAX])
274de967835SAndrew Thompson 			goto repeat;
275de967835SAndrew Thompson 
27602ac6454SAndrew Thompson 		if (dx || dy || dz || dt || dw ||
27702ac6454SAndrew Thompson 		    (buttons != sc->sc_status.button)) {
27802ac6454SAndrew Thompson 
27902ac6454SAndrew Thompson 			DPRINTFN(6, "x:%d y:%d z:%d t:%d w:%d buttons:0x%08x\n",
28002ac6454SAndrew Thompson 			    dx, dy, dz, dt, dw, buttons);
28102ac6454SAndrew Thompson 
28202ac6454SAndrew Thompson 			sc->sc_status.button = buttons;
28302ac6454SAndrew Thompson 			sc->sc_status.dx += dx;
28402ac6454SAndrew Thompson 			sc->sc_status.dy += dy;
28502ac6454SAndrew Thompson 			sc->sc_status.dz += dz;
28602ac6454SAndrew Thompson 			/*
28702ac6454SAndrew Thompson 			 * sc->sc_status.dt += dt;
28802ac6454SAndrew Thompson 			 * no way to export this yet
28902ac6454SAndrew Thompson 			 */
29002ac6454SAndrew Thompson 
29102ac6454SAndrew Thompson 			/*
2923e5e312aSAndrew Thompson 		         * The Qtronix keyboard has a built in PS/2
2933e5e312aSAndrew Thompson 		         * port for a mouse.  The firmware once in a
2943e5e312aSAndrew Thompson 		         * while posts a spurious button up
2953e5e312aSAndrew Thompson 		         * event. This event we ignore by doing a
2963e5e312aSAndrew Thompson 		         * timeout for 50 msecs.  If we receive
2973e5e312aSAndrew Thompson 		         * dx=dy=dz=buttons=0 before we add the event
2983e5e312aSAndrew Thompson 		         * to the queue.  In any other case we delete
2993e5e312aSAndrew Thompson 		         * the timeout event.
30002ac6454SAndrew Thompson 		         */
301de967835SAndrew Thompson 			if ((sc->sc_info[0].sc_flags & UMS_FLAG_SBU) &&
30202ac6454SAndrew Thompson 			    (dx == 0) && (dy == 0) && (dz == 0) && (dt == 0) &&
30302ac6454SAndrew Thompson 			    (dw == 0) && (buttons == 0)) {
30402ac6454SAndrew Thompson 
305a593f6b8SAndrew Thompson 				usb_callout_reset(&sc->sc_callout, hz / 20,
30602ac6454SAndrew Thompson 				    &ums_put_queue_timeout, sc);
30702ac6454SAndrew Thompson 			} else {
30802ac6454SAndrew Thompson 
309a593f6b8SAndrew Thompson 				usb_callout_stop(&sc->sc_callout);
31002ac6454SAndrew Thompson 
31102ac6454SAndrew Thompson 				ums_put_queue(sc, dx, dy, dz, dt, buttons);
31202ac6454SAndrew Thompson 			}
31302ac6454SAndrew Thompson 		}
31402ac6454SAndrew Thompson 	case USB_ST_SETUP:
31502ac6454SAndrew Thompson tr_setup:
31602ac6454SAndrew Thompson 		/* check if we can put more data into the FIFO */
317a593f6b8SAndrew Thompson 		if (usb_fifo_put_bytes_max(
31802ac6454SAndrew Thompson 		    sc->sc_fifo.fp[USB_FIFO_RX]) != 0) {
319ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
320a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
32102ac6454SAndrew Thompson 		}
3223e5e312aSAndrew Thompson 		break;
32302ac6454SAndrew Thompson 
32402ac6454SAndrew Thompson 	default:			/* Error */
325ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
3263e5e312aSAndrew Thompson 			/* try clear stall first */
327ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
3283e5e312aSAndrew Thompson 			goto tr_setup;
32902ac6454SAndrew Thompson 		}
3303e5e312aSAndrew Thompson 		break;
33102ac6454SAndrew Thompson 	}
33202ac6454SAndrew Thompson }
33302ac6454SAndrew Thompson 
334760bc48eSAndrew Thompson static const struct usb_config ums_config[UMS_N_TRANSFER] = {
33502ac6454SAndrew Thompson 
33602ac6454SAndrew Thompson 	[UMS_INTR_DT] = {
33702ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
33802ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
33902ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
3404eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
3414eae601eSAndrew Thompson 		.bufsize = 0,	/* use wMaxPacketSize */
3424eae601eSAndrew Thompson 		.callback = &ums_intr_callback,
34302ac6454SAndrew Thompson 	},
34402ac6454SAndrew Thompson };
34502ac6454SAndrew Thompson 
34602ac6454SAndrew Thompson static int
34702ac6454SAndrew Thompson ums_probe(device_t dev)
34802ac6454SAndrew Thompson {
349760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
35002ac6454SAndrew Thompson 	void *d_ptr;
3513e5e312aSAndrew Thompson 	int error;
35202ac6454SAndrew Thompson 	uint16_t d_len;
35302ac6454SAndrew Thompson 
35402ac6454SAndrew Thompson 	DPRINTFN(11, "\n");
35502ac6454SAndrew Thompson 
356f29a0724SAndrew Thompson 	if (uaa->usb_mode != USB_MODE_HOST)
35702ac6454SAndrew Thompson 		return (ENXIO);
3583e5e312aSAndrew Thompson 
359fd4f10ceSAndrew Thompson 	if (uaa->info.bInterfaceClass != UICLASS_HID)
36002ac6454SAndrew Thompson 		return (ENXIO);
3613e5e312aSAndrew Thompson 
362fd4f10ceSAndrew Thompson 	if ((uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
363fd4f10ceSAndrew Thompson 	    (uaa->info.bInterfaceProtocol == UIPROTO_MOUSE))
364fd4f10ceSAndrew Thompson 		return (0);
365fd4f10ceSAndrew Thompson 
366a593f6b8SAndrew Thompson 	error = usbd_req_get_hid_desc(uaa->device, NULL,
36702ac6454SAndrew Thompson 	    &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
36802ac6454SAndrew Thompson 
3693e5e312aSAndrew Thompson 	if (error)
37002ac6454SAndrew Thompson 		return (ENXIO);
3713e5e312aSAndrew Thompson 
37202ac6454SAndrew Thompson 	if (hid_is_collection(d_ptr, d_len,
3733e5e312aSAndrew Thompson 	    HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE)))
37402ac6454SAndrew Thompson 		error = 0;
3753e5e312aSAndrew Thompson 	else
37602ac6454SAndrew Thompson 		error = ENXIO;
37702ac6454SAndrew Thompson 
37802ac6454SAndrew Thompson 	free(d_ptr, M_TEMP);
37902ac6454SAndrew Thompson 	return (error);
38002ac6454SAndrew Thompson }
38102ac6454SAndrew Thompson 
382de967835SAndrew Thompson static void
383de967835SAndrew Thompson ums_hid_parse(struct ums_softc *sc, device_t dev, const uint8_t *buf,
384de967835SAndrew Thompson     uint16_t len, uint8_t index)
385de967835SAndrew Thompson {
386de967835SAndrew Thompson 	struct ums_info *info = &sc->sc_info[index];
387de967835SAndrew Thompson 	uint32_t flags;
388de967835SAndrew Thompson 	uint8_t i;
389de967835SAndrew Thompson 
390de967835SAndrew Thompson 	if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
391de967835SAndrew Thompson 	    hid_input, index, &info->sc_loc_x, &flags, &info->sc_iid_x)) {
392de967835SAndrew Thompson 
393de967835SAndrew Thompson 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
394de967835SAndrew Thompson 			info->sc_flags |= UMS_FLAG_X_AXIS;
395de967835SAndrew Thompson 		}
396de967835SAndrew Thompson 	}
397de967835SAndrew Thompson 	if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
398de967835SAndrew Thompson 	    hid_input, index, &info->sc_loc_y, &flags, &info->sc_iid_y)) {
399de967835SAndrew Thompson 
400de967835SAndrew Thompson 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
401de967835SAndrew Thompson 			info->sc_flags |= UMS_FLAG_Y_AXIS;
402de967835SAndrew Thompson 		}
403de967835SAndrew Thompson 	}
404de967835SAndrew Thompson 	/* Try the wheel first as the Z activator since it's tradition. */
405de967835SAndrew Thompson 	if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
406de967835SAndrew Thompson 	    HUG_WHEEL), hid_input, index, &info->sc_loc_z, &flags,
407de967835SAndrew Thompson 	    &info->sc_iid_z) ||
408de967835SAndrew Thompson 	    hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
409de967835SAndrew Thompson 	    HUG_TWHEEL), hid_input, index, &info->sc_loc_z, &flags,
410de967835SAndrew Thompson 	    &info->sc_iid_z)) {
411de967835SAndrew Thompson 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
412de967835SAndrew Thompson 			info->sc_flags |= UMS_FLAG_Z_AXIS;
413de967835SAndrew Thompson 		}
414de967835SAndrew Thompson 		/*
415de967835SAndrew Thompson 		 * We might have both a wheel and Z direction, if so put
416de967835SAndrew Thompson 		 * put the Z on the W coordinate.
417de967835SAndrew Thompson 		 */
418de967835SAndrew Thompson 		if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
419de967835SAndrew Thompson 		    HUG_Z), hid_input, index, &info->sc_loc_w, &flags,
420de967835SAndrew Thompson 		    &info->sc_iid_w)) {
421de967835SAndrew Thompson 
422de967835SAndrew Thompson 			if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
423de967835SAndrew Thompson 				info->sc_flags |= UMS_FLAG_W_AXIS;
424de967835SAndrew Thompson 			}
425de967835SAndrew Thompson 		}
426de967835SAndrew Thompson 	} else if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
427de967835SAndrew Thompson 	    HUG_Z), hid_input, index, &info->sc_loc_z, &flags,
428de967835SAndrew Thompson 	    &info->sc_iid_z)) {
429de967835SAndrew Thompson 
430de967835SAndrew Thompson 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
431de967835SAndrew Thompson 			info->sc_flags |= UMS_FLAG_Z_AXIS;
432de967835SAndrew Thompson 		}
433de967835SAndrew Thompson 	}
434de967835SAndrew Thompson 	/*
435de967835SAndrew Thompson 	 * The Microsoft Wireless Intellimouse 2.0 reports it's wheel
436de967835SAndrew Thompson 	 * using 0x0048, which is HUG_TWHEEL, and seems to expect you
437de967835SAndrew Thompson 	 * to know that the byte after the wheel is the tilt axis.
438de967835SAndrew Thompson 	 * There are no other HID axis descriptors other than X,Y and
439de967835SAndrew Thompson 	 * TWHEEL
440de967835SAndrew Thompson 	 */
441de967835SAndrew Thompson 	if (hid_locate(buf, len, HID_USAGE2(HUP_GENERIC_DESKTOP,
442de967835SAndrew Thompson 	    HUG_TWHEEL), hid_input, index, &info->sc_loc_t,
443de967835SAndrew Thompson 	    &flags, &info->sc_iid_t)) {
444de967835SAndrew Thompson 
445de967835SAndrew Thompson 		info->sc_loc_t.pos += 8;
446de967835SAndrew Thompson 
447de967835SAndrew Thompson 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
448de967835SAndrew Thompson 			info->sc_flags |= UMS_FLAG_T_AXIS;
449de967835SAndrew Thompson 		}
450de967835SAndrew Thompson 	}
451de967835SAndrew Thompson 	/* figure out the number of buttons */
452de967835SAndrew Thompson 
453de967835SAndrew Thompson 	for (i = 0; i < UMS_BUTTON_MAX; i++) {
454de967835SAndrew Thompson 		if (!hid_locate(buf, len, HID_USAGE2(HUP_BUTTON, (i + 1)),
455de967835SAndrew Thompson 		    hid_input, index, &info->sc_loc_btn[i], NULL,
456de967835SAndrew Thompson 		    &info->sc_iid_btn[i])) {
457de967835SAndrew Thompson 			break;
458de967835SAndrew Thompson 		}
459de967835SAndrew Thompson 	}
460de967835SAndrew Thompson 	info->sc_buttons = i;
461de967835SAndrew Thompson 
462de967835SAndrew Thompson 	if (i > sc->sc_buttons)
463de967835SAndrew Thompson 		sc->sc_buttons = i;
464de967835SAndrew Thompson 
465de967835SAndrew Thompson 	if (info->sc_flags == 0)
466de967835SAndrew Thompson 		return;
467de967835SAndrew Thompson 
468de967835SAndrew Thompson 	/* announce information about the mouse */
469de967835SAndrew Thompson 	device_printf(dev, "%d buttons and [%s%s%s%s%s] coordinates ID=%u\n",
470de967835SAndrew Thompson 	    (info->sc_buttons),
471de967835SAndrew Thompson 	    (info->sc_flags & UMS_FLAG_X_AXIS) ? "X" : "",
472de967835SAndrew Thompson 	    (info->sc_flags & UMS_FLAG_Y_AXIS) ? "Y" : "",
473de967835SAndrew Thompson 	    (info->sc_flags & UMS_FLAG_Z_AXIS) ? "Z" : "",
474de967835SAndrew Thompson 	    (info->sc_flags & UMS_FLAG_T_AXIS) ? "T" : "",
475de967835SAndrew Thompson 	    (info->sc_flags & UMS_FLAG_W_AXIS) ? "W" : "",
476de967835SAndrew Thompson 	    info->sc_iid_x);
477de967835SAndrew Thompson }
478de967835SAndrew Thompson 
47902ac6454SAndrew Thompson static int
48002ac6454SAndrew Thompson ums_attach(device_t dev)
48102ac6454SAndrew Thompson {
482760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
48302ac6454SAndrew Thompson 	struct ums_softc *sc = device_get_softc(dev);
484de967835SAndrew Thompson 	struct ums_info *info;
48502ac6454SAndrew Thompson 	void *d_ptr = NULL;
4863e5e312aSAndrew Thompson 	int isize;
4873e5e312aSAndrew Thompson 	int err;
48802ac6454SAndrew Thompson 	uint16_t d_len;
48902ac6454SAndrew Thompson 	uint8_t i;
490de967835SAndrew Thompson 	uint8_t j;
49102ac6454SAndrew Thompson 
49202ac6454SAndrew Thompson 	DPRINTFN(11, "sc=%p\n", sc);
49302ac6454SAndrew Thompson 
494a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
49502ac6454SAndrew Thompson 
49602ac6454SAndrew Thompson 	mtx_init(&sc->sc_mtx, "ums lock", NULL, MTX_DEF | MTX_RECURSE);
49702ac6454SAndrew Thompson 
498a593f6b8SAndrew Thompson 	usb_callout_init_mtx(&sc->sc_callout, &sc->sc_mtx, 0);
49902ac6454SAndrew Thompson 
50002ac6454SAndrew Thompson 	/*
50102ac6454SAndrew Thompson          * Force the report (non-boot) protocol.
50202ac6454SAndrew Thompson          *
50302ac6454SAndrew Thompson          * Mice without boot protocol support may choose not to implement
50402ac6454SAndrew Thompson          * Set_Protocol at all; Ignore any error.
50502ac6454SAndrew Thompson          */
506a593f6b8SAndrew Thompson 	err = usbd_req_set_protocol(uaa->device, NULL,
507de967835SAndrew Thompson 	    uaa->info.bIfaceIndex, 1);
50802ac6454SAndrew Thompson 
509a593f6b8SAndrew Thompson 	err = usbd_transfer_setup(uaa->device,
51002ac6454SAndrew Thompson 	    &uaa->info.bIfaceIndex, sc->sc_xfer, ums_config,
51102ac6454SAndrew Thompson 	    UMS_N_TRANSFER, sc, &sc->sc_mtx);
51202ac6454SAndrew Thompson 
51302ac6454SAndrew Thompson 	if (err) {
514a593f6b8SAndrew Thompson 		DPRINTF("error=%s\n", usbd_errstr(err));
51502ac6454SAndrew Thompson 		goto detach;
51602ac6454SAndrew Thompson 	}
517a593f6b8SAndrew Thompson 	err = usbd_req_get_hid_desc(uaa->device, NULL, &d_ptr,
51802ac6454SAndrew Thompson 	    &d_len, M_TEMP, uaa->info.bIfaceIndex);
51902ac6454SAndrew Thompson 
52002ac6454SAndrew Thompson 	if (err) {
52102ac6454SAndrew Thompson 		device_printf(dev, "error reading report description\n");
52202ac6454SAndrew Thompson 		goto detach;
52302ac6454SAndrew Thompson 	}
52402ac6454SAndrew Thompson 
525bf87f556SAndrew Thompson 	isize = hid_report_size(d_ptr, d_len, hid_input, &sc->sc_iid);
52602ac6454SAndrew Thompson 
52702ac6454SAndrew Thompson 	/*
52802ac6454SAndrew Thompson 	 * The Microsoft Wireless Notebook Optical Mouse seems to be in worse
52902ac6454SAndrew Thompson 	 * shape than the Wireless Intellimouse 2.0, as its X, Y, wheel, and
53002ac6454SAndrew Thompson 	 * all of its other button positions are all off. It also reports that
53102ac6454SAndrew Thompson 	 * it has two addional buttons and a tilt wheel.
53202ac6454SAndrew Thompson 	 */
533a593f6b8SAndrew Thompson 	if (usb_test_quirk(uaa, UQ_MS_BAD_CLASS)) {
534de967835SAndrew Thompson 		info = &sc->sc_info[0];
535de967835SAndrew Thompson 		info->sc_flags = (UMS_FLAG_X_AXIS |
53602ac6454SAndrew Thompson 		    UMS_FLAG_Y_AXIS |
53702ac6454SAndrew Thompson 		    UMS_FLAG_Z_AXIS |
53802ac6454SAndrew Thompson 		    UMS_FLAG_SBU);
539de967835SAndrew Thompson 		info->sc_buttons = 3;
54002ac6454SAndrew Thompson 		isize = 5;
54102ac6454SAndrew Thompson 		/* 1st byte of descriptor report contains garbage */
542de967835SAndrew Thompson 		info->sc_loc_x.pos = 16;
543de967835SAndrew Thompson 		info->sc_loc_y.pos = 24;
544de967835SAndrew Thompson 		info->sc_loc_z.pos = 32;
545de967835SAndrew Thompson 		info->sc_loc_btn[0].pos = 8;
546de967835SAndrew Thompson 		info->sc_loc_btn[1].pos = 9;
547de967835SAndrew Thompson 		info->sc_loc_btn[2].pos = 10;
5483e5e312aSAndrew Thompson 
549de967835SAndrew Thompson 		/* Announce device */
550de967835SAndrew Thompson 		device_printf(dev, "3 buttons and [XYZ] "
551de967835SAndrew Thompson 		    "coordinates ID=0\n");
552de967835SAndrew Thompson 
553de967835SAndrew Thompson 	} else {
554de967835SAndrew Thompson 		/* Search the HID descriptor and announce device */
555de967835SAndrew Thompson 		for (i = 0; i < UMS_INFO_MAX; i++) {
556de967835SAndrew Thompson 			ums_hid_parse(sc, dev, d_ptr, d_len, i);
557de967835SAndrew Thompson 		}
55802ac6454SAndrew Thompson 	}
5593e5e312aSAndrew Thompson 
560a593f6b8SAndrew Thompson 	if (usb_test_quirk(uaa, UQ_MS_REVZ)) {
561de967835SAndrew Thompson 		info = &sc->sc_info[0];
56202ac6454SAndrew Thompson 		/* Some wheels need the Z axis reversed. */
563de967835SAndrew Thompson 		info->sc_flags |= UMS_FLAG_REVZ;
56402ac6454SAndrew Thompson 	}
565ed6d949aSAndrew Thompson 	if (isize > usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT])) {
56602ac6454SAndrew Thompson 		DPRINTF("WARNING: report size, %d bytes, is larger "
567ed6d949aSAndrew Thompson 		    "than interrupt size, %d bytes!\n", isize,
568ed6d949aSAndrew Thompson 		    usbd_xfer_max_framelen(sc->sc_xfer[UMS_INTR_DT]));
56902ac6454SAndrew Thompson 	}
57002ac6454SAndrew Thompson 	free(d_ptr, M_TEMP);
57102ac6454SAndrew Thompson 	d_ptr = NULL;
57202ac6454SAndrew Thompson 
57302ac6454SAndrew Thompson #if USB_DEBUG
574de967835SAndrew Thompson 	for (j = 0; j < UMS_INFO_MAX; j++) {
575de967835SAndrew Thompson 		info = &sc->sc_info[j];
57602ac6454SAndrew Thompson 
577de967835SAndrew Thompson 		DPRINTF("sc=%p, index=%d\n", sc, j);
578de967835SAndrew Thompson 		DPRINTF("X\t%d/%d id=%d\n", info->sc_loc_x.pos,
579de967835SAndrew Thompson 		    info->sc_loc_x.size, info->sc_iid_x);
580de967835SAndrew Thompson 		DPRINTF("Y\t%d/%d id=%d\n", info->sc_loc_y.pos,
581de967835SAndrew Thompson 		    info->sc_loc_y.size, info->sc_iid_y);
582de967835SAndrew Thompson 		DPRINTF("Z\t%d/%d id=%d\n", info->sc_loc_z.pos,
583de967835SAndrew Thompson 		    info->sc_loc_z.size, info->sc_iid_z);
584de967835SAndrew Thompson 		DPRINTF("T\t%d/%d id=%d\n", info->sc_loc_t.pos,
585de967835SAndrew Thompson 		    info->sc_loc_t.size, info->sc_iid_t);
586de967835SAndrew Thompson 		DPRINTF("W\t%d/%d id=%d\n", info->sc_loc_w.pos,
587de967835SAndrew Thompson 		    info->sc_loc_w.size, info->sc_iid_w);
588de967835SAndrew Thompson 
589de967835SAndrew Thompson 		for (i = 0; i < info->sc_buttons; i++) {
590bf87f556SAndrew Thompson 			DPRINTF("B%d\t%d/%d id=%d\n",
591de967835SAndrew Thompson 			    i + 1, info->sc_loc_btn[i].pos,
592de967835SAndrew Thompson 			    info->sc_loc_btn[i].size, info->sc_iid_btn[i]);
593de967835SAndrew Thompson 		}
59402ac6454SAndrew Thompson 	}
59502ac6454SAndrew Thompson 	DPRINTF("size=%d, id=%d\n", isize, sc->sc_iid);
59602ac6454SAndrew Thompson #endif
59702ac6454SAndrew Thompson 
59802ac6454SAndrew Thompson 	if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
59902ac6454SAndrew Thompson 		sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
60002ac6454SAndrew Thompson 	else
60102ac6454SAndrew Thompson 		sc->sc_hw.buttons = sc->sc_buttons;
60202ac6454SAndrew Thompson 
60302ac6454SAndrew Thompson 	sc->sc_hw.iftype = MOUSE_IF_USB;
60402ac6454SAndrew Thompson 	sc->sc_hw.type = MOUSE_MOUSE;
60502ac6454SAndrew Thompson 	sc->sc_hw.model = MOUSE_MODEL_GENERIC;
60602ac6454SAndrew Thompson 	sc->sc_hw.hwid = 0;
60702ac6454SAndrew Thompson 
60802ac6454SAndrew Thompson 	sc->sc_mode.protocol = MOUSE_PROTO_MSC;
60902ac6454SAndrew Thompson 	sc->sc_mode.rate = -1;
61002ac6454SAndrew Thompson 	sc->sc_mode.resolution = MOUSE_RES_UNKNOWN;
61102ac6454SAndrew Thompson 	sc->sc_mode.accelfactor = 0;
61202ac6454SAndrew Thompson 	sc->sc_mode.level = 0;
61302ac6454SAndrew Thompson 	sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
61402ac6454SAndrew Thompson 	sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
61502ac6454SAndrew Thompson 	sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
61602ac6454SAndrew Thompson 
617a593f6b8SAndrew Thompson 	err = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
61802ac6454SAndrew Thompson 	    &ums_fifo_methods, &sc->sc_fifo,
619de967835SAndrew Thompson 	    device_get_unit(dev), 0 - 1, uaa->info.bIfaceIndex,
620ee3e3ff5SAndrew Thompson   	    UID_ROOT, GID_OPERATOR, 0644);
62102ac6454SAndrew Thompson 	if (err) {
62202ac6454SAndrew Thompson 		goto detach;
62302ac6454SAndrew Thompson 	}
62402ac6454SAndrew Thompson 	return (0);
62502ac6454SAndrew Thompson 
62602ac6454SAndrew Thompson detach:
62702ac6454SAndrew Thompson 	if (d_ptr) {
62802ac6454SAndrew Thompson 		free(d_ptr, M_TEMP);
62902ac6454SAndrew Thompson 	}
63002ac6454SAndrew Thompson 	ums_detach(dev);
63102ac6454SAndrew Thompson 	return (ENOMEM);
63202ac6454SAndrew Thompson }
63302ac6454SAndrew Thompson 
63402ac6454SAndrew Thompson static int
63502ac6454SAndrew Thompson ums_detach(device_t self)
63602ac6454SAndrew Thompson {
63702ac6454SAndrew Thompson 	struct ums_softc *sc = device_get_softc(self);
63802ac6454SAndrew Thompson 
63902ac6454SAndrew Thompson 	DPRINTF("sc=%p\n", sc);
64002ac6454SAndrew Thompson 
641a593f6b8SAndrew Thompson 	usb_fifo_detach(&sc->sc_fifo);
64202ac6454SAndrew Thompson 
643a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, UMS_N_TRANSFER);
64402ac6454SAndrew Thompson 
645a593f6b8SAndrew Thompson 	usb_callout_drain(&sc->sc_callout);
64602ac6454SAndrew Thompson 
64702ac6454SAndrew Thompson 	mtx_destroy(&sc->sc_mtx);
64802ac6454SAndrew Thompson 
64902ac6454SAndrew Thompson 	return (0);
65002ac6454SAndrew Thompson }
65102ac6454SAndrew Thompson 
65202ac6454SAndrew Thompson static void
653760bc48eSAndrew Thompson ums_start_read(struct usb_fifo *fifo)
65402ac6454SAndrew Thompson {
655ed6d949aSAndrew Thompson 	struct ums_softc *sc = usb_fifo_softc(fifo);
65602ac6454SAndrew Thompson 
657a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UMS_INTR_DT]);
65802ac6454SAndrew Thompson }
65902ac6454SAndrew Thompson 
66002ac6454SAndrew Thompson static void
661760bc48eSAndrew Thompson ums_stop_read(struct usb_fifo *fifo)
66202ac6454SAndrew Thompson {
663ed6d949aSAndrew Thompson 	struct ums_softc *sc = usb_fifo_softc(fifo);
66402ac6454SAndrew Thompson 
665a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
666a593f6b8SAndrew Thompson 	usb_callout_stop(&sc->sc_callout);
66702ac6454SAndrew Thompson }
66802ac6454SAndrew Thompson 
66902ac6454SAndrew Thompson 
67002ac6454SAndrew Thompson #if ((MOUSE_SYS_PACKETSIZE != 8) || \
67102ac6454SAndrew Thompson      (MOUSE_MSC_PACKETSIZE != 5))
67202ac6454SAndrew Thompson #error "Software assumptions are not met. Please update code."
67302ac6454SAndrew Thompson #endif
67402ac6454SAndrew Thompson 
67502ac6454SAndrew Thompson static void
67602ac6454SAndrew Thompson ums_put_queue(struct ums_softc *sc, int32_t dx, int32_t dy,
67702ac6454SAndrew Thompson     int32_t dz, int32_t dt, int32_t buttons)
67802ac6454SAndrew Thompson {
67902ac6454SAndrew Thompson 	uint8_t buf[8];
68002ac6454SAndrew Thompson 
68102ac6454SAndrew Thompson 	if (1) {
68202ac6454SAndrew Thompson 
68302ac6454SAndrew Thompson 		if (dx > 254)
68402ac6454SAndrew Thompson 			dx = 254;
68502ac6454SAndrew Thompson 		if (dx < -256)
68602ac6454SAndrew Thompson 			dx = -256;
68702ac6454SAndrew Thompson 		if (dy > 254)
68802ac6454SAndrew Thompson 			dy = 254;
68902ac6454SAndrew Thompson 		if (dy < -256)
69002ac6454SAndrew Thompson 			dy = -256;
69102ac6454SAndrew Thompson 		if (dz > 126)
69202ac6454SAndrew Thompson 			dz = 126;
69302ac6454SAndrew Thompson 		if (dz < -128)
69402ac6454SAndrew Thompson 			dz = -128;
69502ac6454SAndrew Thompson 		if (dt > 126)
69602ac6454SAndrew Thompson 			dt = 126;
69702ac6454SAndrew Thompson 		if (dt < -128)
69802ac6454SAndrew Thompson 			dt = -128;
69902ac6454SAndrew Thompson 
70002ac6454SAndrew Thompson 		buf[0] = sc->sc_mode.syncmask[1];
70102ac6454SAndrew Thompson 		buf[0] |= (~buttons) & MOUSE_MSC_BUTTONS;
70202ac6454SAndrew Thompson 		buf[1] = dx >> 1;
70302ac6454SAndrew Thompson 		buf[2] = dy >> 1;
70402ac6454SAndrew Thompson 		buf[3] = dx - (dx >> 1);
70502ac6454SAndrew Thompson 		buf[4] = dy - (dy >> 1);
70602ac6454SAndrew Thompson 
70702ac6454SAndrew Thompson 		if (sc->sc_mode.level == 1) {
70802ac6454SAndrew Thompson 			buf[5] = dz >> 1;
70902ac6454SAndrew Thompson 			buf[6] = dz - (dz >> 1);
71002ac6454SAndrew Thompson 			buf[7] = (((~buttons) >> 3) & MOUSE_SYS_EXTBUTTONS);
71102ac6454SAndrew Thompson 		}
712a593f6b8SAndrew Thompson 		usb_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf,
71302ac6454SAndrew Thompson 		    sc->sc_mode.packetsize, 1);
71402ac6454SAndrew Thompson 
71502ac6454SAndrew Thompson 	} else {
71602ac6454SAndrew Thompson 		DPRINTF("Buffer full, discarded packet\n");
71702ac6454SAndrew Thompson 	}
71802ac6454SAndrew Thompson }
71902ac6454SAndrew Thompson 
72002ac6454SAndrew Thompson static void
72102ac6454SAndrew Thompson ums_reset_buf(struct ums_softc *sc)
72202ac6454SAndrew Thompson {
72302ac6454SAndrew Thompson 	/* reset read queue */
724a593f6b8SAndrew Thompson 	usb_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]);
72502ac6454SAndrew Thompson }
72602ac6454SAndrew Thompson 
72702ac6454SAndrew Thompson static int
728760bc48eSAndrew Thompson ums_open(struct usb_fifo *fifo, int fflags)
72902ac6454SAndrew Thompson {
730ed6d949aSAndrew Thompson 	struct ums_softc *sc = usb_fifo_softc(fifo);
73102ac6454SAndrew Thompson 
73202ac6454SAndrew Thompson 	DPRINTFN(2, "\n");
73302ac6454SAndrew Thompson 
73402ac6454SAndrew Thompson 	if (fflags & FREAD) {
73502ac6454SAndrew Thompson 
73602ac6454SAndrew Thompson 		/* reset status */
73702ac6454SAndrew Thompson 
73802ac6454SAndrew Thompson 		sc->sc_status.flags = 0;
73902ac6454SAndrew Thompson 		sc->sc_status.button = 0;
74002ac6454SAndrew Thompson 		sc->sc_status.obutton = 0;
74102ac6454SAndrew Thompson 		sc->sc_status.dx = 0;
74202ac6454SAndrew Thompson 		sc->sc_status.dy = 0;
74302ac6454SAndrew Thompson 		sc->sc_status.dz = 0;
74402ac6454SAndrew Thompson 		/* sc->sc_status.dt = 0; */
74502ac6454SAndrew Thompson 
746a593f6b8SAndrew Thompson 		if (usb_fifo_alloc_buffer(fifo,
74702ac6454SAndrew Thompson 		    UMS_BUF_SIZE, UMS_IFQ_MAXLEN)) {
74802ac6454SAndrew Thompson 			return (ENOMEM);
74902ac6454SAndrew Thompson 		}
75002ac6454SAndrew Thompson 	}
75102ac6454SAndrew Thompson 	return (0);
75202ac6454SAndrew Thompson }
75302ac6454SAndrew Thompson 
75402ac6454SAndrew Thompson static void
755760bc48eSAndrew Thompson ums_close(struct usb_fifo *fifo, int fflags)
75602ac6454SAndrew Thompson {
75702ac6454SAndrew Thompson 	if (fflags & FREAD) {
758a593f6b8SAndrew Thompson 		usb_fifo_free_buffer(fifo);
75902ac6454SAndrew Thompson 	}
76002ac6454SAndrew Thompson }
76102ac6454SAndrew Thompson 
76202ac6454SAndrew Thompson static int
763760bc48eSAndrew Thompson ums_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, int fflags)
76402ac6454SAndrew Thompson {
765ed6d949aSAndrew Thompson 	struct ums_softc *sc = usb_fifo_softc(fifo);
76602ac6454SAndrew Thompson 	mousemode_t mode;
76702ac6454SAndrew Thompson 	int error = 0;
76802ac6454SAndrew Thompson 
76902ac6454SAndrew Thompson 	DPRINTFN(2, "\n");
77002ac6454SAndrew Thompson 
77102ac6454SAndrew Thompson 	mtx_lock(&sc->sc_mtx);
77202ac6454SAndrew Thompson 
77302ac6454SAndrew Thompson 	switch (cmd) {
77402ac6454SAndrew Thompson 	case MOUSE_GETHWINFO:
77502ac6454SAndrew Thompson 		*(mousehw_t *)addr = sc->sc_hw;
77602ac6454SAndrew Thompson 		break;
77702ac6454SAndrew Thompson 
77802ac6454SAndrew Thompson 	case MOUSE_GETMODE:
77902ac6454SAndrew Thompson 		*(mousemode_t *)addr = sc->sc_mode;
78002ac6454SAndrew Thompson 		break;
78102ac6454SAndrew Thompson 
78202ac6454SAndrew Thompson 	case MOUSE_SETMODE:
78302ac6454SAndrew Thompson 		mode = *(mousemode_t *)addr;
78402ac6454SAndrew Thompson 
78502ac6454SAndrew Thompson 		if (mode.level == -1) {
78602ac6454SAndrew Thompson 			/* don't change the current setting */
78702ac6454SAndrew Thompson 		} else if ((mode.level < 0) || (mode.level > 1)) {
78802ac6454SAndrew Thompson 			error = EINVAL;
78902ac6454SAndrew Thompson 			goto done;
79002ac6454SAndrew Thompson 		} else {
79102ac6454SAndrew Thompson 			sc->sc_mode.level = mode.level;
79202ac6454SAndrew Thompson 		}
79302ac6454SAndrew Thompson 
79402ac6454SAndrew Thompson 		if (sc->sc_mode.level == 0) {
79502ac6454SAndrew Thompson 			if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
79602ac6454SAndrew Thompson 				sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
79702ac6454SAndrew Thompson 			else
79802ac6454SAndrew Thompson 				sc->sc_hw.buttons = sc->sc_buttons;
79902ac6454SAndrew Thompson 			sc->sc_mode.protocol = MOUSE_PROTO_MSC;
80002ac6454SAndrew Thompson 			sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
80102ac6454SAndrew Thompson 			sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
80202ac6454SAndrew Thompson 			sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
80302ac6454SAndrew Thompson 		} else if (sc->sc_mode.level == 1) {
80402ac6454SAndrew Thompson 			if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
80502ac6454SAndrew Thompson 				sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
80602ac6454SAndrew Thompson 			else
80702ac6454SAndrew Thompson 				sc->sc_hw.buttons = sc->sc_buttons;
80802ac6454SAndrew Thompson 			sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
80902ac6454SAndrew Thompson 			sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
81002ac6454SAndrew Thompson 			sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
81102ac6454SAndrew Thompson 			sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
81202ac6454SAndrew Thompson 		}
81302ac6454SAndrew Thompson 		ums_reset_buf(sc);
81402ac6454SAndrew Thompson 		break;
81502ac6454SAndrew Thompson 
81602ac6454SAndrew Thompson 	case MOUSE_GETLEVEL:
81702ac6454SAndrew Thompson 		*(int *)addr = sc->sc_mode.level;
81802ac6454SAndrew Thompson 		break;
81902ac6454SAndrew Thompson 
82002ac6454SAndrew Thompson 	case MOUSE_SETLEVEL:
82102ac6454SAndrew Thompson 		if (*(int *)addr < 0 || *(int *)addr > 1) {
82202ac6454SAndrew Thompson 			error = EINVAL;
82302ac6454SAndrew Thompson 			goto done;
82402ac6454SAndrew Thompson 		}
82502ac6454SAndrew Thompson 		sc->sc_mode.level = *(int *)addr;
82602ac6454SAndrew Thompson 
82702ac6454SAndrew Thompson 		if (sc->sc_mode.level == 0) {
82802ac6454SAndrew Thompson 			if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
82902ac6454SAndrew Thompson 				sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
83002ac6454SAndrew Thompson 			else
83102ac6454SAndrew Thompson 				sc->sc_hw.buttons = sc->sc_buttons;
83202ac6454SAndrew Thompson 			sc->sc_mode.protocol = MOUSE_PROTO_MSC;
83302ac6454SAndrew Thompson 			sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
83402ac6454SAndrew Thompson 			sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
83502ac6454SAndrew Thompson 			sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
83602ac6454SAndrew Thompson 		} else if (sc->sc_mode.level == 1) {
83702ac6454SAndrew Thompson 			if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
83802ac6454SAndrew Thompson 				sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
83902ac6454SAndrew Thompson 			else
84002ac6454SAndrew Thompson 				sc->sc_hw.buttons = sc->sc_buttons;
84102ac6454SAndrew Thompson 			sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
84202ac6454SAndrew Thompson 			sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
84302ac6454SAndrew Thompson 			sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
84402ac6454SAndrew Thompson 			sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
84502ac6454SAndrew Thompson 		}
84602ac6454SAndrew Thompson 		ums_reset_buf(sc);
84702ac6454SAndrew Thompson 		break;
84802ac6454SAndrew Thompson 
84902ac6454SAndrew Thompson 	case MOUSE_GETSTATUS:{
85002ac6454SAndrew Thompson 			mousestatus_t *status = (mousestatus_t *)addr;
85102ac6454SAndrew Thompson 
85202ac6454SAndrew Thompson 			*status = sc->sc_status;
85302ac6454SAndrew Thompson 			sc->sc_status.obutton = sc->sc_status.button;
85402ac6454SAndrew Thompson 			sc->sc_status.button = 0;
85502ac6454SAndrew Thompson 			sc->sc_status.dx = 0;
85602ac6454SAndrew Thompson 			sc->sc_status.dy = 0;
85702ac6454SAndrew Thompson 			sc->sc_status.dz = 0;
85802ac6454SAndrew Thompson 			/* sc->sc_status.dt = 0; */
85902ac6454SAndrew Thompson 
86002ac6454SAndrew Thompson 			if (status->dx || status->dy || status->dz /* || status->dt */ ) {
86102ac6454SAndrew Thompson 				status->flags |= MOUSE_POSCHANGED;
86202ac6454SAndrew Thompson 			}
86302ac6454SAndrew Thompson 			if (status->button != status->obutton) {
86402ac6454SAndrew Thompson 				status->flags |= MOUSE_BUTTONSCHANGED;
86502ac6454SAndrew Thompson 			}
86602ac6454SAndrew Thompson 			break;
86702ac6454SAndrew Thompson 		}
86802ac6454SAndrew Thompson 	default:
86902ac6454SAndrew Thompson 		error = ENOTTY;
87002ac6454SAndrew Thompson 	}
87102ac6454SAndrew Thompson 
87202ac6454SAndrew Thompson done:
87302ac6454SAndrew Thompson 	mtx_unlock(&sc->sc_mtx);
87402ac6454SAndrew Thompson 	return (error);
87502ac6454SAndrew Thompson }
87602ac6454SAndrew Thompson 
87702ac6454SAndrew Thompson static devclass_t ums_devclass;
87802ac6454SAndrew Thompson 
87902ac6454SAndrew Thompson static device_method_t ums_methods[] = {
88002ac6454SAndrew Thompson 	DEVMETHOD(device_probe, ums_probe),
88102ac6454SAndrew Thompson 	DEVMETHOD(device_attach, ums_attach),
88202ac6454SAndrew Thompson 	DEVMETHOD(device_detach, ums_detach),
88302ac6454SAndrew Thompson 	{0, 0}
88402ac6454SAndrew Thompson };
88502ac6454SAndrew Thompson 
88602ac6454SAndrew Thompson static driver_t ums_driver = {
88702ac6454SAndrew Thompson 	.name = "ums",
88802ac6454SAndrew Thompson 	.methods = ums_methods,
88902ac6454SAndrew Thompson 	.size = sizeof(struct ums_softc),
89002ac6454SAndrew Thompson };
89102ac6454SAndrew Thompson 
8929aef556dSAndrew Thompson DRIVER_MODULE(ums, uhub, ums_driver, ums_devclass, NULL, 0);
89302ac6454SAndrew Thompson MODULE_DEPEND(ums, usb, 1, 1, 1);
894