xref: /freebsd/sys/dev/usb/input/ums.c (revision 02ac6454880b59bbc5f3f74dffaffa90b30adc8b)
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 
4502ac6454SAndrew Thompson #include "usbdevs.h"
4602ac6454SAndrew Thompson #include <dev/usb/usb.h>
4702ac6454SAndrew Thompson #include <dev/usb/usb_mfunc.h>
4802ac6454SAndrew Thompson #include <dev/usb/usb_error.h>
4902ac6454SAndrew Thompson #include <dev/usb/usbhid.h>
5002ac6454SAndrew Thompson 
5102ac6454SAndrew Thompson #define	USB_DEBUG_VAR ums_debug
5202ac6454SAndrew Thompson 
5302ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
5402ac6454SAndrew Thompson #include <dev/usb/usb_util.h>
5502ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
5602ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
5702ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
5802ac6454SAndrew Thompson #include <dev/usb/usb_transfer.h>
5902ac6454SAndrew Thompson #include <dev/usb/usb_request.h>
6002ac6454SAndrew Thompson #include <dev/usb/usb_dynamic.h>
6102ac6454SAndrew Thompson #include <dev/usb/usb_mbuf.h>
6202ac6454SAndrew Thompson #include <dev/usb/usb_dev.h>
6302ac6454SAndrew Thompson #include <dev/usb/usb_hid.h>
6402ac6454SAndrew Thompson 
6502ac6454SAndrew Thompson #include <dev/usb/quirk/usb_quirk.h>
6602ac6454SAndrew Thompson 
6702ac6454SAndrew Thompson #include <sys/ioccom.h>
6802ac6454SAndrew Thompson #include <sys/filio.h>
6902ac6454SAndrew Thompson #include <sys/tty.h>
7002ac6454SAndrew Thompson #include <sys/mouse.h>
7102ac6454SAndrew Thompson 
7202ac6454SAndrew Thompson #if USB_DEBUG
7302ac6454SAndrew Thompson static int ums_debug = 0;
7402ac6454SAndrew Thompson 
7502ac6454SAndrew Thompson SYSCTL_NODE(_hw_usb2, OID_AUTO, ums, CTLFLAG_RW, 0, "USB ums");
7602ac6454SAndrew Thompson SYSCTL_INT(_hw_usb2_ums, OID_AUTO, debug, CTLFLAG_RW,
7702ac6454SAndrew Thompson     &ums_debug, 0, "Debug level");
7802ac6454SAndrew Thompson #endif
7902ac6454SAndrew Thompson 
8002ac6454SAndrew Thompson #define	MOUSE_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
8102ac6454SAndrew Thompson #define	MOUSE_FLAGS (HIO_RELATIVE)
8202ac6454SAndrew Thompson 
8302ac6454SAndrew Thompson #define	UMS_BUF_SIZE      8		/* bytes */
8402ac6454SAndrew Thompson #define	UMS_IFQ_MAXLEN   50		/* units */
8502ac6454SAndrew Thompson #define	UMS_BUTTON_MAX   31		/* exclusive, must be less than 32 */
8602ac6454SAndrew Thompson #define	UMS_BUT(i) ((i) < 3 ? (((i) + 2) % 3) : (i))
8702ac6454SAndrew Thompson 
8802ac6454SAndrew Thompson enum {
8902ac6454SAndrew Thompson 	UMS_INTR_DT,
9002ac6454SAndrew Thompson 	UMS_INTR_CS,
9102ac6454SAndrew Thompson 	UMS_N_TRANSFER = 2,
9202ac6454SAndrew Thompson };
9302ac6454SAndrew Thompson 
9402ac6454SAndrew Thompson struct ums_softc {
9502ac6454SAndrew Thompson 	struct usb2_fifo_sc sc_fifo;
9602ac6454SAndrew Thompson 	struct mtx sc_mtx;
9702ac6454SAndrew Thompson 	struct usb2_callout sc_callout;
9802ac6454SAndrew Thompson 	struct hid_location sc_loc_w;
9902ac6454SAndrew Thompson 	struct hid_location sc_loc_x;
10002ac6454SAndrew Thompson 	struct hid_location sc_loc_y;
10102ac6454SAndrew Thompson 	struct hid_location sc_loc_z;
10202ac6454SAndrew Thompson 	struct hid_location sc_loc_t;
10302ac6454SAndrew Thompson 	struct hid_location sc_loc_btn[UMS_BUTTON_MAX];
10402ac6454SAndrew Thompson 	mousehw_t sc_hw;
10502ac6454SAndrew Thompson 	mousemode_t sc_mode;
10602ac6454SAndrew Thompson 	mousestatus_t sc_status;
10702ac6454SAndrew Thompson 
10802ac6454SAndrew Thompson 	struct usb2_xfer *sc_xfer[UMS_N_TRANSFER];
10902ac6454SAndrew Thompson 
11002ac6454SAndrew Thompson 	uint32_t sc_flags;
11102ac6454SAndrew Thompson #define	UMS_FLAG_X_AXIS     0x0001
11202ac6454SAndrew Thompson #define	UMS_FLAG_Y_AXIS     0x0002
11302ac6454SAndrew Thompson #define	UMS_FLAG_Z_AXIS     0x0004
11402ac6454SAndrew Thompson #define	UMS_FLAG_T_AXIS     0x0008
11502ac6454SAndrew Thompson #define	UMS_FLAG_SBU        0x0010	/* spurious button up events */
11602ac6454SAndrew Thompson #define	UMS_FLAG_INTR_STALL 0x0020	/* set if transfer error */
11702ac6454SAndrew Thompson #define	UMS_FLAG_REVZ	    0x0040	/* Z-axis is reversed */
11802ac6454SAndrew Thompson #define	UMS_FLAG_W_AXIS     0x0080
11902ac6454SAndrew Thompson 
12002ac6454SAndrew Thompson 	uint8_t	sc_buttons;
12102ac6454SAndrew Thompson 	uint8_t	sc_iid;
12202ac6454SAndrew Thompson 	uint8_t	sc_temp[64];
12302ac6454SAndrew Thompson };
12402ac6454SAndrew Thompson 
12502ac6454SAndrew Thompson static void ums_put_queue_timeout(void *__sc);
12602ac6454SAndrew Thompson 
12702ac6454SAndrew Thompson static usb2_callback_t ums_clear_stall_callback;
12802ac6454SAndrew Thompson static usb2_callback_t ums_intr_callback;
12902ac6454SAndrew Thompson 
13002ac6454SAndrew Thompson static device_probe_t ums_probe;
13102ac6454SAndrew Thompson static device_attach_t ums_attach;
13202ac6454SAndrew Thompson static device_detach_t ums_detach;
13302ac6454SAndrew Thompson 
13402ac6454SAndrew Thompson static usb2_fifo_cmd_t ums_start_read;
13502ac6454SAndrew Thompson static usb2_fifo_cmd_t ums_stop_read;
13602ac6454SAndrew Thompson static usb2_fifo_open_t ums_open;
13702ac6454SAndrew Thompson static usb2_fifo_close_t ums_close;
13802ac6454SAndrew Thompson static usb2_fifo_ioctl_t ums_ioctl;
13902ac6454SAndrew Thompson 
14002ac6454SAndrew 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);
14102ac6454SAndrew Thompson 
14202ac6454SAndrew Thompson static struct usb2_fifo_methods ums_fifo_methods = {
14302ac6454SAndrew Thompson 	.f_open = &ums_open,
14402ac6454SAndrew Thompson 	.f_close = &ums_close,
14502ac6454SAndrew Thompson 	.f_ioctl = &ums_ioctl,
14602ac6454SAndrew Thompson 	.f_start_read = &ums_start_read,
14702ac6454SAndrew Thompson 	.f_stop_read = &ums_stop_read,
14802ac6454SAndrew Thompson 	.basename[0] = "ums",
14902ac6454SAndrew Thompson };
15002ac6454SAndrew Thompson 
15102ac6454SAndrew Thompson static void
15202ac6454SAndrew Thompson ums_put_queue_timeout(void *__sc)
15302ac6454SAndrew Thompson {
15402ac6454SAndrew Thompson 	struct ums_softc *sc = __sc;
15502ac6454SAndrew Thompson 
15602ac6454SAndrew Thompson 	mtx_assert(&sc->sc_mtx, MA_OWNED);
15702ac6454SAndrew Thompson 
15802ac6454SAndrew Thompson 	ums_put_queue(sc, 0, 0, 0, 0, 0);
15902ac6454SAndrew Thompson }
16002ac6454SAndrew Thompson 
16102ac6454SAndrew Thompson static void
16202ac6454SAndrew Thompson ums_clear_stall_callback(struct usb2_xfer *xfer)
16302ac6454SAndrew Thompson {
16402ac6454SAndrew Thompson 	struct ums_softc *sc = xfer->priv_sc;
16502ac6454SAndrew Thompson 	struct usb2_xfer *xfer_other = sc->sc_xfer[UMS_INTR_DT];
16602ac6454SAndrew Thompson 
16702ac6454SAndrew Thompson 	if (usb2_clear_stall_callback(xfer, xfer_other)) {
16802ac6454SAndrew Thompson 		DPRINTF("stall cleared\n");
16902ac6454SAndrew Thompson 		sc->sc_flags &= ~UMS_FLAG_INTR_STALL;
17002ac6454SAndrew Thompson 		usb2_transfer_start(xfer_other);
17102ac6454SAndrew Thompson 	}
17202ac6454SAndrew Thompson }
17302ac6454SAndrew Thompson 
17402ac6454SAndrew Thompson static void
17502ac6454SAndrew Thompson ums_intr_callback(struct usb2_xfer *xfer)
17602ac6454SAndrew Thompson {
17702ac6454SAndrew Thompson 	struct ums_softc *sc = xfer->priv_sc;
17802ac6454SAndrew Thompson 	uint8_t *buf = sc->sc_temp;
17902ac6454SAndrew Thompson 	uint16_t len = xfer->actlen;
18002ac6454SAndrew Thompson 	int32_t buttons = 0;
18102ac6454SAndrew Thompson 	int32_t dw;
18202ac6454SAndrew Thompson 	int32_t dx;
18302ac6454SAndrew Thompson 	int32_t dy;
18402ac6454SAndrew Thompson 	int32_t dz;
18502ac6454SAndrew Thompson 	int32_t dt;
18602ac6454SAndrew Thompson 	uint8_t i;
18702ac6454SAndrew Thompson 
18802ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
18902ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
19002ac6454SAndrew Thompson 		DPRINTFN(6, "sc=%p actlen=%d\n", sc, len);
19102ac6454SAndrew Thompson 
19202ac6454SAndrew Thompson 		if (len > sizeof(sc->sc_temp)) {
19302ac6454SAndrew Thompson 			DPRINTFN(6, "truncating large packet to %zu bytes\n",
19402ac6454SAndrew Thompson 			    sizeof(sc->sc_temp));
19502ac6454SAndrew Thompson 			len = sizeof(sc->sc_temp);
19602ac6454SAndrew Thompson 		}
19702ac6454SAndrew Thompson 		if (len == 0) {
19802ac6454SAndrew Thompson 			goto tr_setup;
19902ac6454SAndrew Thompson 		}
20002ac6454SAndrew Thompson 		usb2_copy_out(xfer->frbuffers, 0, buf, len);
20102ac6454SAndrew Thompson 
20202ac6454SAndrew Thompson 		DPRINTFN(6, "data = %02x %02x %02x %02x "
20302ac6454SAndrew Thompson 		    "%02x %02x %02x %02x\n",
20402ac6454SAndrew Thompson 		    (len > 0) ? buf[0] : 0, (len > 1) ? buf[1] : 0,
20502ac6454SAndrew Thompson 		    (len > 2) ? buf[2] : 0, (len > 3) ? buf[3] : 0,
20602ac6454SAndrew Thompson 		    (len > 4) ? buf[4] : 0, (len > 5) ? buf[5] : 0,
20702ac6454SAndrew Thompson 		    (len > 6) ? buf[6] : 0, (len > 7) ? buf[7] : 0);
20802ac6454SAndrew Thompson 
20902ac6454SAndrew Thompson 		/*
21002ac6454SAndrew Thompson 		 * The M$ Wireless Intellimouse 2.0 sends 1 extra leading byte
21102ac6454SAndrew Thompson 		 * of data compared to most USB mice. This byte frequently
21202ac6454SAndrew Thompson 		 * switches from 0x01 (usual state) to 0x02. I assume it is to
21302ac6454SAndrew Thompson 		 * allow extra, non-standard, reporting (say battery-life).
21402ac6454SAndrew Thompson 		 *
21502ac6454SAndrew Thompson 		 * However at the same time it generates a left-click message
21602ac6454SAndrew Thompson 		 * on the button byte which causes spurious left-click's where
21702ac6454SAndrew Thompson 		 * there shouldn't be.  This should sort that.  Currently it's
21802ac6454SAndrew Thompson 		 * the only user of UMS_FLAG_T_AXIS so use it as an
21902ac6454SAndrew Thompson 		 * identifier.
22002ac6454SAndrew Thompson 		 *
22102ac6454SAndrew Thompson 		 *
22202ac6454SAndrew Thompson 		 * UPDATE: This problem affects the M$ Wireless Notebook Optical Mouse,
22302ac6454SAndrew Thompson 		 * too. However, the leading byte for this mouse is normally 0x11,
22402ac6454SAndrew Thompson 		 * and the phantom mouse click occurs when its 0x14.
22502ac6454SAndrew Thompson 		 *
22602ac6454SAndrew Thompson 		 * We probably should switch to some more official quirk.
22702ac6454SAndrew Thompson 		 */
22802ac6454SAndrew Thompson 		if (sc->sc_iid) {
22902ac6454SAndrew Thompson 			if (sc->sc_flags & UMS_FLAG_T_AXIS) {
23002ac6454SAndrew Thompson 				if (*buf == 0x02) {
23102ac6454SAndrew Thompson 					goto tr_setup;
23202ac6454SAndrew Thompson 				}
23302ac6454SAndrew Thompson 			} else {
23402ac6454SAndrew Thompson 				if (*buf != sc->sc_iid) {
23502ac6454SAndrew Thompson 					goto tr_setup;
23602ac6454SAndrew Thompson 				}
23702ac6454SAndrew Thompson 			}
23802ac6454SAndrew Thompson 
23902ac6454SAndrew Thompson 			len--;
24002ac6454SAndrew Thompson 			buf++;
24102ac6454SAndrew Thompson 
24202ac6454SAndrew Thompson 		} else {
24302ac6454SAndrew Thompson 			if (sc->sc_flags & UMS_FLAG_SBU) {
24402ac6454SAndrew Thompson 				if ((*buf == 0x14) || (*buf == 0x15)) {
24502ac6454SAndrew Thompson 					goto tr_setup;
24602ac6454SAndrew Thompson 				}
24702ac6454SAndrew Thompson 			}
24802ac6454SAndrew Thompson 		}
24902ac6454SAndrew Thompson 
25002ac6454SAndrew Thompson 		dw = (sc->sc_flags & UMS_FLAG_W_AXIS) ?
25102ac6454SAndrew Thompson 		    hid_get_data(buf, len, &sc->sc_loc_w) : 0;
25202ac6454SAndrew Thompson 
25302ac6454SAndrew Thompson 		dx = (sc->sc_flags & UMS_FLAG_X_AXIS) ?
25402ac6454SAndrew Thompson 		    hid_get_data(buf, len, &sc->sc_loc_x) : 0;
25502ac6454SAndrew Thompson 
25602ac6454SAndrew Thompson 		dy = (sc->sc_flags & UMS_FLAG_Y_AXIS) ?
25702ac6454SAndrew Thompson 		    -hid_get_data(buf, len, &sc->sc_loc_y) : 0;
25802ac6454SAndrew Thompson 
25902ac6454SAndrew Thompson 		dz = (sc->sc_flags & UMS_FLAG_Z_AXIS) ?
26002ac6454SAndrew Thompson 		    -hid_get_data(buf, len, &sc->sc_loc_z) : 0;
26102ac6454SAndrew Thompson 
26202ac6454SAndrew Thompson 		if (sc->sc_flags & UMS_FLAG_REVZ) {
26302ac6454SAndrew Thompson 			dz = -dz;
26402ac6454SAndrew Thompson 		}
26502ac6454SAndrew Thompson 		dt = (sc->sc_flags & UMS_FLAG_T_AXIS) ?
26602ac6454SAndrew Thompson 		    -hid_get_data(buf, len, &sc->sc_loc_t): 0;
26702ac6454SAndrew Thompson 
26802ac6454SAndrew Thompson 		for (i = 0; i < sc->sc_buttons; i++) {
26902ac6454SAndrew Thompson 			if (hid_get_data(buf, len, &sc->sc_loc_btn[i])) {
27002ac6454SAndrew Thompson 				buttons |= (1 << UMS_BUT(i));
27102ac6454SAndrew Thompson 			}
27202ac6454SAndrew Thompson 		}
27302ac6454SAndrew Thompson 
27402ac6454SAndrew Thompson 		if (dx || dy || dz || dt || dw ||
27502ac6454SAndrew Thompson 		    (buttons != sc->sc_status.button)) {
27602ac6454SAndrew Thompson 
27702ac6454SAndrew Thompson 			DPRINTFN(6, "x:%d y:%d z:%d t:%d w:%d buttons:0x%08x\n",
27802ac6454SAndrew Thompson 			    dx, dy, dz, dt, dw, buttons);
27902ac6454SAndrew Thompson 
28002ac6454SAndrew Thompson 			sc->sc_status.button = buttons;
28102ac6454SAndrew Thompson 			sc->sc_status.dx += dx;
28202ac6454SAndrew Thompson 			sc->sc_status.dy += dy;
28302ac6454SAndrew Thompson 			sc->sc_status.dz += dz;
28402ac6454SAndrew Thompson 			/*
28502ac6454SAndrew Thompson 			 * sc->sc_status.dt += dt;
28602ac6454SAndrew Thompson 			 * no way to export this yet
28702ac6454SAndrew Thompson 			 */
28802ac6454SAndrew Thompson 
28902ac6454SAndrew Thompson 			/*
29002ac6454SAndrew Thompson 		         * The Qtronix keyboard has a built in PS/2 port for a mouse.
29102ac6454SAndrew Thompson 		         * The firmware once in a while posts a spurious button up
29202ac6454SAndrew Thompson 		         * event. This event we ignore by doing a timeout for 50 msecs.
29302ac6454SAndrew Thompson 		         * If we receive dx=dy=dz=buttons=0 before we add the event to
29402ac6454SAndrew Thompson 		         * the queue.
29502ac6454SAndrew Thompson 		         * In any other case we delete the timeout event.
29602ac6454SAndrew Thompson 		         */
29702ac6454SAndrew Thompson 			if ((sc->sc_flags & UMS_FLAG_SBU) &&
29802ac6454SAndrew Thompson 			    (dx == 0) && (dy == 0) && (dz == 0) && (dt == 0) &&
29902ac6454SAndrew Thompson 			    (dw == 0) && (buttons == 0)) {
30002ac6454SAndrew Thompson 
30102ac6454SAndrew Thompson 				usb2_callout_reset(&sc->sc_callout, hz / 20,
30202ac6454SAndrew Thompson 				    &ums_put_queue_timeout, sc);
30302ac6454SAndrew Thompson 			} else {
30402ac6454SAndrew Thompson 
30502ac6454SAndrew Thompson 				usb2_callout_stop(&sc->sc_callout);
30602ac6454SAndrew Thompson 
30702ac6454SAndrew Thompson 				ums_put_queue(sc, dx, dy, dz, dt, buttons);
30802ac6454SAndrew Thompson 			}
30902ac6454SAndrew Thompson 		}
31002ac6454SAndrew Thompson 	case USB_ST_SETUP:
31102ac6454SAndrew Thompson tr_setup:
31202ac6454SAndrew Thompson 		if (sc->sc_flags & UMS_FLAG_INTR_STALL) {
31302ac6454SAndrew Thompson 			usb2_transfer_start(sc->sc_xfer[UMS_INTR_CS]);
31402ac6454SAndrew Thompson 		} else {
31502ac6454SAndrew Thompson 			/* check if we can put more data into the FIFO */
31602ac6454SAndrew Thompson 			if (usb2_fifo_put_bytes_max(
31702ac6454SAndrew Thompson 			    sc->sc_fifo.fp[USB_FIFO_RX]) != 0) {
31802ac6454SAndrew Thompson 				xfer->frlengths[0] = xfer->max_data_length;
31902ac6454SAndrew Thompson 				usb2_start_hardware(xfer);
32002ac6454SAndrew Thompson 			}
32102ac6454SAndrew Thompson 		}
32202ac6454SAndrew Thompson 		return;
32302ac6454SAndrew Thompson 
32402ac6454SAndrew Thompson 	default:			/* Error */
32502ac6454SAndrew Thompson 		if (xfer->error != USB_ERR_CANCELLED) {
32602ac6454SAndrew Thompson 			/* start clear stall */
32702ac6454SAndrew Thompson 			sc->sc_flags |= UMS_FLAG_INTR_STALL;
32802ac6454SAndrew Thompson 			usb2_transfer_start(sc->sc_xfer[UMS_INTR_CS]);
32902ac6454SAndrew Thompson 		}
33002ac6454SAndrew Thompson 		return;
33102ac6454SAndrew Thompson 	}
33202ac6454SAndrew Thompson }
33302ac6454SAndrew Thompson 
33402ac6454SAndrew Thompson static const struct usb2_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,
34002ac6454SAndrew Thompson 		.mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
34102ac6454SAndrew Thompson 		.mh.bufsize = 0,	/* use wMaxPacketSize */
34202ac6454SAndrew Thompson 		.mh.callback = &ums_intr_callback,
34302ac6454SAndrew Thompson 	},
34402ac6454SAndrew Thompson 
34502ac6454SAndrew Thompson 	[UMS_INTR_CS] = {
34602ac6454SAndrew Thompson 		.type = UE_CONTROL,
34702ac6454SAndrew Thompson 		.endpoint = 0x00,	/* Control pipe */
34802ac6454SAndrew Thompson 		.direction = UE_DIR_ANY,
34902ac6454SAndrew Thompson 		.mh.bufsize = sizeof(struct usb2_device_request),
35002ac6454SAndrew Thompson 		.mh.callback = &ums_clear_stall_callback,
35102ac6454SAndrew Thompson 		.mh.timeout = 1000,	/* 1 second */
35202ac6454SAndrew Thompson 		.mh.interval = 50,	/* 50ms */
35302ac6454SAndrew Thompson 	},
35402ac6454SAndrew Thompson };
35502ac6454SAndrew Thompson 
35602ac6454SAndrew Thompson static int
35702ac6454SAndrew Thompson ums_probe(device_t dev)
35802ac6454SAndrew Thompson {
35902ac6454SAndrew Thompson 	struct usb2_attach_arg *uaa = device_get_ivars(dev);
36002ac6454SAndrew Thompson 	struct usb2_interface_descriptor *id;
36102ac6454SAndrew Thompson 	void *d_ptr;
36202ac6454SAndrew Thompson 	int32_t error = 0;
36302ac6454SAndrew Thompson 	uint16_t d_len;
36402ac6454SAndrew Thompson 
36502ac6454SAndrew Thompson 	DPRINTFN(11, "\n");
36602ac6454SAndrew Thompson 
36702ac6454SAndrew Thompson 	if (uaa->usb2_mode != USB_MODE_HOST) {
36802ac6454SAndrew Thompson 		return (ENXIO);
36902ac6454SAndrew Thompson 	}
37002ac6454SAndrew Thompson 	if (uaa->iface == NULL) {
37102ac6454SAndrew Thompson 		return (ENXIO);
37202ac6454SAndrew Thompson 	}
37302ac6454SAndrew Thompson 	id = usb2_get_interface_descriptor(uaa->iface);
37402ac6454SAndrew Thompson 
37502ac6454SAndrew Thompson 	if ((id == NULL) ||
37602ac6454SAndrew Thompson 	    (id->bInterfaceClass != UICLASS_HID)) {
37702ac6454SAndrew Thompson 		return (ENXIO);
37802ac6454SAndrew Thompson 	}
37902ac6454SAndrew Thompson 	error = usb2_req_get_hid_desc
38002ac6454SAndrew Thompson 	    (uaa->device, &Giant,
38102ac6454SAndrew Thompson 	    &d_ptr, &d_len, M_TEMP, uaa->info.bIfaceIndex);
38202ac6454SAndrew Thompson 
38302ac6454SAndrew Thompson 	if (error) {
38402ac6454SAndrew Thompson 		return (ENXIO);
38502ac6454SAndrew Thompson 	}
38602ac6454SAndrew Thompson 	if (hid_is_collection(d_ptr, d_len,
38702ac6454SAndrew Thompson 	    HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE))) {
38802ac6454SAndrew Thompson 		error = 0;
38902ac6454SAndrew Thompson 	} else if ((id->bInterfaceSubClass == UISUBCLASS_BOOT) &&
39002ac6454SAndrew Thompson 	    (id->bInterfaceProtocol == UIPROTO_MOUSE)) {
39102ac6454SAndrew Thompson 		error = 0;
39202ac6454SAndrew Thompson 	} else {
39302ac6454SAndrew Thompson 		error = ENXIO;
39402ac6454SAndrew Thompson 	}
39502ac6454SAndrew Thompson 
39602ac6454SAndrew Thompson 	free(d_ptr, M_TEMP);
39702ac6454SAndrew Thompson 	return (error);
39802ac6454SAndrew Thompson }
39902ac6454SAndrew Thompson 
40002ac6454SAndrew Thompson static int
40102ac6454SAndrew Thompson ums_attach(device_t dev)
40202ac6454SAndrew Thompson {
40302ac6454SAndrew Thompson 	struct usb2_attach_arg *uaa = device_get_ivars(dev);
40402ac6454SAndrew Thompson 	struct ums_softc *sc = device_get_softc(dev);
40502ac6454SAndrew Thompson 	void *d_ptr = NULL;
40602ac6454SAndrew Thompson 	int unit = device_get_unit(dev);
40702ac6454SAndrew Thompson 	int32_t isize;
40802ac6454SAndrew Thompson 	uint32_t flags;
40902ac6454SAndrew Thompson 	int32_t err;
41002ac6454SAndrew Thompson 	uint16_t d_len;
41102ac6454SAndrew Thompson 	uint8_t i;
41202ac6454SAndrew Thompson 
41302ac6454SAndrew Thompson 	DPRINTFN(11, "sc=%p\n", sc);
41402ac6454SAndrew Thompson 
41502ac6454SAndrew Thompson 	device_set_usb2_desc(dev);
41602ac6454SAndrew Thompson 
41702ac6454SAndrew Thompson 	mtx_init(&sc->sc_mtx, "ums lock", NULL, MTX_DEF | MTX_RECURSE);
41802ac6454SAndrew Thompson 
41902ac6454SAndrew Thompson 	usb2_callout_init_mtx(&sc->sc_callout, &sc->sc_mtx, 0);
42002ac6454SAndrew Thompson 
42102ac6454SAndrew Thompson 	/*
42202ac6454SAndrew Thompson          * Force the report (non-boot) protocol.
42302ac6454SAndrew Thompson          *
42402ac6454SAndrew Thompson          * Mice without boot protocol support may choose not to implement
42502ac6454SAndrew Thompson          * Set_Protocol at all; Ignore any error.
42602ac6454SAndrew Thompson          */
42702ac6454SAndrew Thompson 	err = usb2_req_set_protocol(uaa->device, NULL, uaa->info.bIfaceIndex, 1);
42802ac6454SAndrew Thompson 
42902ac6454SAndrew Thompson 	err = usb2_transfer_setup(uaa->device,
43002ac6454SAndrew Thompson 	    &uaa->info.bIfaceIndex, sc->sc_xfer, ums_config,
43102ac6454SAndrew Thompson 	    UMS_N_TRANSFER, sc, &sc->sc_mtx);
43202ac6454SAndrew Thompson 
43302ac6454SAndrew Thompson 	if (err) {
43402ac6454SAndrew Thompson 		DPRINTF("error=%s\n", usb2_errstr(err));
43502ac6454SAndrew Thompson 		goto detach;
43602ac6454SAndrew Thompson 	}
43702ac6454SAndrew Thompson 	err = usb2_req_get_hid_desc
43802ac6454SAndrew Thompson 	    (uaa->device, &Giant, &d_ptr,
43902ac6454SAndrew Thompson 	    &d_len, M_TEMP, uaa->info.bIfaceIndex);
44002ac6454SAndrew Thompson 
44102ac6454SAndrew Thompson 	if (err) {
44202ac6454SAndrew Thompson 		device_printf(dev, "error reading report description\n");
44302ac6454SAndrew Thompson 		goto detach;
44402ac6454SAndrew Thompson 	}
44502ac6454SAndrew Thompson 	if (hid_locate(d_ptr, d_len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
44602ac6454SAndrew Thompson 	    hid_input, &sc->sc_loc_x, &flags)) {
44702ac6454SAndrew Thompson 
44802ac6454SAndrew Thompson 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
44902ac6454SAndrew Thompson 			sc->sc_flags |= UMS_FLAG_X_AXIS;
45002ac6454SAndrew Thompson 		}
45102ac6454SAndrew Thompson 	}
45202ac6454SAndrew Thompson 	if (hid_locate(d_ptr, d_len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
45302ac6454SAndrew Thompson 	    hid_input, &sc->sc_loc_y, &flags)) {
45402ac6454SAndrew Thompson 
45502ac6454SAndrew Thompson 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
45602ac6454SAndrew Thompson 			sc->sc_flags |= UMS_FLAG_Y_AXIS;
45702ac6454SAndrew Thompson 		}
45802ac6454SAndrew Thompson 	}
45902ac6454SAndrew Thompson 	/* Try the wheel first as the Z activator since it's tradition. */
46002ac6454SAndrew Thompson 	if (hid_locate(d_ptr, d_len, HID_USAGE2(HUP_GENERIC_DESKTOP,
46102ac6454SAndrew Thompson 	    HUG_WHEEL), hid_input, &sc->sc_loc_z, &flags) ||
46202ac6454SAndrew Thompson 	    hid_locate(d_ptr, d_len, HID_USAGE2(HUP_GENERIC_DESKTOP,
46302ac6454SAndrew Thompson 	    HUG_TWHEEL), hid_input, &sc->sc_loc_z, &flags)) {
46402ac6454SAndrew Thompson 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
46502ac6454SAndrew Thompson 			sc->sc_flags |= UMS_FLAG_Z_AXIS;
46602ac6454SAndrew Thompson 		}
46702ac6454SAndrew Thompson 		/*
46802ac6454SAndrew Thompson 		 * We might have both a wheel and Z direction, if so put
46902ac6454SAndrew Thompson 		 * put the Z on the W coordinate.
47002ac6454SAndrew Thompson 		 */
47102ac6454SAndrew Thompson 		if (hid_locate(d_ptr, d_len, HID_USAGE2(HUP_GENERIC_DESKTOP,
47202ac6454SAndrew Thompson 		    HUG_Z), hid_input, &sc->sc_loc_w, &flags)) {
47302ac6454SAndrew Thompson 
47402ac6454SAndrew Thompson 			if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
47502ac6454SAndrew Thompson 				sc->sc_flags |= UMS_FLAG_W_AXIS;
47602ac6454SAndrew Thompson 			}
47702ac6454SAndrew Thompson 		}
47802ac6454SAndrew Thompson 	} else if (hid_locate(d_ptr, d_len, HID_USAGE2(HUP_GENERIC_DESKTOP,
47902ac6454SAndrew Thompson 	    HUG_Z), hid_input, &sc->sc_loc_z, &flags)) {
48002ac6454SAndrew Thompson 
48102ac6454SAndrew Thompson 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
48202ac6454SAndrew Thompson 			sc->sc_flags |= UMS_FLAG_Z_AXIS;
48302ac6454SAndrew Thompson 		}
48402ac6454SAndrew Thompson 	}
48502ac6454SAndrew Thompson 	/*
48602ac6454SAndrew Thompson 	 * The Microsoft Wireless Intellimouse 2.0 reports it's wheel
48702ac6454SAndrew Thompson 	 * using 0x0048, which is HUG_TWHEEL, and seems to expect you
48802ac6454SAndrew Thompson 	 * to know that the byte after the wheel is the tilt axis.
48902ac6454SAndrew Thompson 	 * There are no other HID axis descriptors other than X,Y and
49002ac6454SAndrew Thompson 	 * TWHEEL
49102ac6454SAndrew Thompson 	 */
49202ac6454SAndrew Thompson 	if (hid_locate(d_ptr, d_len, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_TWHEEL),
49302ac6454SAndrew Thompson 	    hid_input, &sc->sc_loc_t, &flags)) {
49402ac6454SAndrew Thompson 
49502ac6454SAndrew Thompson 		sc->sc_loc_t.pos += 8;
49602ac6454SAndrew Thompson 
49702ac6454SAndrew Thompson 		if ((flags & MOUSE_FLAGS_MASK) == MOUSE_FLAGS) {
49802ac6454SAndrew Thompson 			sc->sc_flags |= UMS_FLAG_T_AXIS;
49902ac6454SAndrew Thompson 		}
50002ac6454SAndrew Thompson 	}
50102ac6454SAndrew Thompson 	/* figure out the number of buttons */
50202ac6454SAndrew Thompson 
50302ac6454SAndrew Thompson 	for (i = 0; i < UMS_BUTTON_MAX; i++) {
50402ac6454SAndrew Thompson 		if (!hid_locate(d_ptr, d_len, HID_USAGE2(HUP_BUTTON, (i + 1)),
50502ac6454SAndrew Thompson 		    hid_input, &sc->sc_loc_btn[i], NULL)) {
50602ac6454SAndrew Thompson 			break;
50702ac6454SAndrew Thompson 		}
50802ac6454SAndrew Thompson 	}
50902ac6454SAndrew Thompson 
51002ac6454SAndrew Thompson 	sc->sc_buttons = i;
51102ac6454SAndrew Thompson 
51202ac6454SAndrew Thompson 	isize = hid_report_size(d_ptr, d_len, hid_input, &sc->sc_iid);
51302ac6454SAndrew Thompson 
51402ac6454SAndrew Thompson 	/*
51502ac6454SAndrew Thompson 	 * The Microsoft Wireless Notebook Optical Mouse seems to be in worse
51602ac6454SAndrew Thompson 	 * shape than the Wireless Intellimouse 2.0, as its X, Y, wheel, and
51702ac6454SAndrew Thompson 	 * all of its other button positions are all off. It also reports that
51802ac6454SAndrew Thompson 	 * it has two addional buttons and a tilt wheel.
51902ac6454SAndrew Thompson 	 */
52002ac6454SAndrew Thompson 	if (usb2_test_quirk(uaa, UQ_MS_BAD_CLASS)) {
52102ac6454SAndrew Thompson 		sc->sc_flags = (UMS_FLAG_X_AXIS |
52202ac6454SAndrew Thompson 		    UMS_FLAG_Y_AXIS |
52302ac6454SAndrew Thompson 		    UMS_FLAG_Z_AXIS |
52402ac6454SAndrew Thompson 		    UMS_FLAG_SBU);
52502ac6454SAndrew Thompson 		sc->sc_buttons = 3;
52602ac6454SAndrew Thompson 		isize = 5;
52702ac6454SAndrew Thompson 		sc->sc_iid = 0;
52802ac6454SAndrew Thompson 		/* 1st byte of descriptor report contains garbage */
52902ac6454SAndrew Thompson 		sc->sc_loc_x.pos = 16;
53002ac6454SAndrew Thompson 		sc->sc_loc_y.pos = 24;
53102ac6454SAndrew Thompson 		sc->sc_loc_z.pos = 32;
53202ac6454SAndrew Thompson 		sc->sc_loc_btn[0].pos = 8;
53302ac6454SAndrew Thompson 		sc->sc_loc_btn[1].pos = 9;
53402ac6454SAndrew Thompson 		sc->sc_loc_btn[2].pos = 10;
53502ac6454SAndrew Thompson 	}
53602ac6454SAndrew Thompson 	/*
53702ac6454SAndrew Thompson 	 * The Microsoft Wireless Notebook Optical Mouse 3000 Model 1049 has
53802ac6454SAndrew Thompson 	 * five Report IDs: 19 23 24 17 18 (in the order they appear in report
53902ac6454SAndrew Thompson 	 * descriptor), it seems that report id 17 contains the necessary
54002ac6454SAndrew Thompson 	 * mouse information(3-buttons,X,Y,wheel) so we specify it manually.
54102ac6454SAndrew Thompson 	 */
54202ac6454SAndrew Thompson 	if ((uaa->info.idVendor == USB_VENDOR_MICROSOFT) &&
54302ac6454SAndrew Thompson 	    (uaa->info.idProduct == USB_PRODUCT_MICROSOFT_WLNOTEBOOK3)) {
54402ac6454SAndrew Thompson 		sc->sc_flags = (UMS_FLAG_X_AXIS |
54502ac6454SAndrew Thompson 		    UMS_FLAG_Y_AXIS |
54602ac6454SAndrew Thompson 		    UMS_FLAG_Z_AXIS);
54702ac6454SAndrew Thompson 		sc->sc_buttons = 3;
54802ac6454SAndrew Thompson 		isize = 5;
54902ac6454SAndrew Thompson 		sc->sc_iid = 17;
55002ac6454SAndrew Thompson 		sc->sc_loc_x.pos = 8;
55102ac6454SAndrew Thompson 		sc->sc_loc_y.pos = 16;
55202ac6454SAndrew Thompson 		sc->sc_loc_z.pos = 24;
55302ac6454SAndrew Thompson 		sc->sc_loc_btn[0].pos = 0;
55402ac6454SAndrew Thompson 		sc->sc_loc_btn[1].pos = 1;
55502ac6454SAndrew Thompson 		sc->sc_loc_btn[2].pos = 2;
55602ac6454SAndrew Thompson 	}
55702ac6454SAndrew Thompson 	if (usb2_test_quirk(uaa, UQ_MS_REVZ)) {
55802ac6454SAndrew Thompson 		/* Some wheels need the Z axis reversed. */
55902ac6454SAndrew Thompson 		sc->sc_flags |= UMS_FLAG_REVZ;
56002ac6454SAndrew Thompson 	}
56102ac6454SAndrew Thompson 	if (isize > sc->sc_xfer[UMS_INTR_DT]->max_frame_size) {
56202ac6454SAndrew Thompson 		DPRINTF("WARNING: report size, %d bytes, is larger "
56302ac6454SAndrew Thompson 		    "than interrupt size, %d bytes!\n",
56402ac6454SAndrew Thompson 		    isize, sc->sc_xfer[UMS_INTR_DT]->max_frame_size);
56502ac6454SAndrew Thompson 	}
56602ac6454SAndrew Thompson 	/* announce information about the mouse */
56702ac6454SAndrew Thompson 
56802ac6454SAndrew Thompson 	device_printf(dev, "%d buttons and [%s%s%s%s%s] coordinates\n",
56902ac6454SAndrew Thompson 	    (sc->sc_buttons),
57002ac6454SAndrew Thompson 	    (sc->sc_flags & UMS_FLAG_X_AXIS) ? "X" : "",
57102ac6454SAndrew Thompson 	    (sc->sc_flags & UMS_FLAG_Y_AXIS) ? "Y" : "",
57202ac6454SAndrew Thompson 	    (sc->sc_flags & UMS_FLAG_Z_AXIS) ? "Z" : "",
57302ac6454SAndrew Thompson 	    (sc->sc_flags & UMS_FLAG_T_AXIS) ? "T" : "",
57402ac6454SAndrew Thompson 	    (sc->sc_flags & UMS_FLAG_W_AXIS) ? "W" : "");
57502ac6454SAndrew Thompson 
57602ac6454SAndrew Thompson 	free(d_ptr, M_TEMP);
57702ac6454SAndrew Thompson 	d_ptr = NULL;
57802ac6454SAndrew Thompson 
57902ac6454SAndrew Thompson #if USB_DEBUG
58002ac6454SAndrew Thompson 	DPRINTF("sc=%p\n", sc);
58102ac6454SAndrew Thompson 	DPRINTF("X\t%d/%d\n", sc->sc_loc_x.pos, sc->sc_loc_x.size);
58202ac6454SAndrew Thompson 	DPRINTF("Y\t%d/%d\n", sc->sc_loc_y.pos, sc->sc_loc_y.size);
58302ac6454SAndrew Thompson 	DPRINTF("Z\t%d/%d\n", sc->sc_loc_z.pos, sc->sc_loc_z.size);
58402ac6454SAndrew Thompson 	DPRINTF("T\t%d/%d\n", sc->sc_loc_t.pos, sc->sc_loc_t.size);
58502ac6454SAndrew Thompson 	DPRINTF("W\t%d/%d\n", sc->sc_loc_w.pos, sc->sc_loc_w.size);
58602ac6454SAndrew Thompson 
58702ac6454SAndrew Thompson 	for (i = 0; i < sc->sc_buttons; i++) {
58802ac6454SAndrew Thompson 		DPRINTF("B%d\t%d/%d\n",
58902ac6454SAndrew Thompson 		    i + 1, sc->sc_loc_btn[i].pos, sc->sc_loc_btn[i].size);
59002ac6454SAndrew Thompson 	}
59102ac6454SAndrew Thompson 	DPRINTF("size=%d, id=%d\n", isize, sc->sc_iid);
59202ac6454SAndrew Thompson #endif
59302ac6454SAndrew Thompson 
59402ac6454SAndrew Thompson 	if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
59502ac6454SAndrew Thompson 		sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
59602ac6454SAndrew Thompson 	else
59702ac6454SAndrew Thompson 		sc->sc_hw.buttons = sc->sc_buttons;
59802ac6454SAndrew Thompson 
59902ac6454SAndrew Thompson 	sc->sc_hw.iftype = MOUSE_IF_USB;
60002ac6454SAndrew Thompson 	sc->sc_hw.type = MOUSE_MOUSE;
60102ac6454SAndrew Thompson 	sc->sc_hw.model = MOUSE_MODEL_GENERIC;
60202ac6454SAndrew Thompson 	sc->sc_hw.hwid = 0;
60302ac6454SAndrew Thompson 
60402ac6454SAndrew Thompson 	sc->sc_mode.protocol = MOUSE_PROTO_MSC;
60502ac6454SAndrew Thompson 	sc->sc_mode.rate = -1;
60602ac6454SAndrew Thompson 	sc->sc_mode.resolution = MOUSE_RES_UNKNOWN;
60702ac6454SAndrew Thompson 	sc->sc_mode.accelfactor = 0;
60802ac6454SAndrew Thompson 	sc->sc_mode.level = 0;
60902ac6454SAndrew Thompson 	sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
61002ac6454SAndrew Thompson 	sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
61102ac6454SAndrew Thompson 	sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
61202ac6454SAndrew Thompson 
61302ac6454SAndrew Thompson 	sc->sc_status.flags = 0;
61402ac6454SAndrew Thompson 	sc->sc_status.button = 0;
61502ac6454SAndrew Thompson 	sc->sc_status.obutton = 0;
61602ac6454SAndrew Thompson 	sc->sc_status.dx = 0;
61702ac6454SAndrew Thompson 	sc->sc_status.dy = 0;
61802ac6454SAndrew Thompson 	sc->sc_status.dz = 0;
61902ac6454SAndrew Thompson 
62002ac6454SAndrew Thompson 	/* set interface permissions */
62102ac6454SAndrew Thompson 	usb2_set_iface_perm(uaa->device, uaa->info.bIfaceIndex,
62202ac6454SAndrew Thompson 	    UID_ROOT, GID_OPERATOR, 0644);
62302ac6454SAndrew Thompson 
62402ac6454SAndrew Thompson 	err = usb2_fifo_attach(uaa->device, sc, &sc->sc_mtx,
62502ac6454SAndrew Thompson 	    &ums_fifo_methods, &sc->sc_fifo,
62602ac6454SAndrew Thompson 	    unit, 0 - 1, uaa->info.bIfaceIndex);
62702ac6454SAndrew Thompson 	if (err) {
62802ac6454SAndrew Thompson 		goto detach;
62902ac6454SAndrew Thompson 	}
63002ac6454SAndrew Thompson 	return (0);
63102ac6454SAndrew Thompson 
63202ac6454SAndrew Thompson detach:
63302ac6454SAndrew Thompson 	if (d_ptr) {
63402ac6454SAndrew Thompson 		free(d_ptr, M_TEMP);
63502ac6454SAndrew Thompson 	}
63602ac6454SAndrew Thompson 	ums_detach(dev);
63702ac6454SAndrew Thompson 	return (ENOMEM);
63802ac6454SAndrew Thompson }
63902ac6454SAndrew Thompson 
64002ac6454SAndrew Thompson static int
64102ac6454SAndrew Thompson ums_detach(device_t self)
64202ac6454SAndrew Thompson {
64302ac6454SAndrew Thompson 	struct ums_softc *sc = device_get_softc(self);
64402ac6454SAndrew Thompson 
64502ac6454SAndrew Thompson 	DPRINTF("sc=%p\n", sc);
64602ac6454SAndrew Thompson 
64702ac6454SAndrew Thompson 	usb2_fifo_detach(&sc->sc_fifo);
64802ac6454SAndrew Thompson 
64902ac6454SAndrew Thompson 	usb2_transfer_unsetup(sc->sc_xfer, UMS_N_TRANSFER);
65002ac6454SAndrew Thompson 
65102ac6454SAndrew Thompson 	usb2_callout_drain(&sc->sc_callout);
65202ac6454SAndrew Thompson 
65302ac6454SAndrew Thompson 	mtx_destroy(&sc->sc_mtx);
65402ac6454SAndrew Thompson 
65502ac6454SAndrew Thompson 	return (0);
65602ac6454SAndrew Thompson }
65702ac6454SAndrew Thompson 
65802ac6454SAndrew Thompson static void
65902ac6454SAndrew Thompson ums_start_read(struct usb2_fifo *fifo)
66002ac6454SAndrew Thompson {
66102ac6454SAndrew Thompson 	struct ums_softc *sc = fifo->priv_sc0;
66202ac6454SAndrew Thompson 
66302ac6454SAndrew Thompson 	usb2_transfer_start(sc->sc_xfer[UMS_INTR_DT]);
66402ac6454SAndrew Thompson }
66502ac6454SAndrew Thompson 
66602ac6454SAndrew Thompson static void
66702ac6454SAndrew Thompson ums_stop_read(struct usb2_fifo *fifo)
66802ac6454SAndrew Thompson {
66902ac6454SAndrew Thompson 	struct ums_softc *sc = fifo->priv_sc0;
67002ac6454SAndrew Thompson 
67102ac6454SAndrew Thompson 	usb2_transfer_stop(sc->sc_xfer[UMS_INTR_CS]);
67202ac6454SAndrew Thompson 	usb2_transfer_stop(sc->sc_xfer[UMS_INTR_DT]);
67302ac6454SAndrew Thompson 	usb2_callout_stop(&sc->sc_callout);
67402ac6454SAndrew Thompson }
67502ac6454SAndrew Thompson 
67602ac6454SAndrew Thompson 
67702ac6454SAndrew Thompson #if ((MOUSE_SYS_PACKETSIZE != 8) || \
67802ac6454SAndrew Thompson      (MOUSE_MSC_PACKETSIZE != 5))
67902ac6454SAndrew Thompson #error "Software assumptions are not met. Please update code."
68002ac6454SAndrew Thompson #endif
68102ac6454SAndrew Thompson 
68202ac6454SAndrew Thompson static void
68302ac6454SAndrew Thompson ums_put_queue(struct ums_softc *sc, int32_t dx, int32_t dy,
68402ac6454SAndrew Thompson     int32_t dz, int32_t dt, int32_t buttons)
68502ac6454SAndrew Thompson {
68602ac6454SAndrew Thompson 	uint8_t buf[8];
68702ac6454SAndrew Thompson 
68802ac6454SAndrew Thompson 	if (1) {
68902ac6454SAndrew Thompson 
69002ac6454SAndrew Thompson 		if (dx > 254)
69102ac6454SAndrew Thompson 			dx = 254;
69202ac6454SAndrew Thompson 		if (dx < -256)
69302ac6454SAndrew Thompson 			dx = -256;
69402ac6454SAndrew Thompson 		if (dy > 254)
69502ac6454SAndrew Thompson 			dy = 254;
69602ac6454SAndrew Thompson 		if (dy < -256)
69702ac6454SAndrew Thompson 			dy = -256;
69802ac6454SAndrew Thompson 		if (dz > 126)
69902ac6454SAndrew Thompson 			dz = 126;
70002ac6454SAndrew Thompson 		if (dz < -128)
70102ac6454SAndrew Thompson 			dz = -128;
70202ac6454SAndrew Thompson 		if (dt > 126)
70302ac6454SAndrew Thompson 			dt = 126;
70402ac6454SAndrew Thompson 		if (dt < -128)
70502ac6454SAndrew Thompson 			dt = -128;
70602ac6454SAndrew Thompson 
70702ac6454SAndrew Thompson 		buf[0] = sc->sc_mode.syncmask[1];
70802ac6454SAndrew Thompson 		buf[0] |= (~buttons) & MOUSE_MSC_BUTTONS;
70902ac6454SAndrew Thompson 		buf[1] = dx >> 1;
71002ac6454SAndrew Thompson 		buf[2] = dy >> 1;
71102ac6454SAndrew Thompson 		buf[3] = dx - (dx >> 1);
71202ac6454SAndrew Thompson 		buf[4] = dy - (dy >> 1);
71302ac6454SAndrew Thompson 
71402ac6454SAndrew Thompson 		if (sc->sc_mode.level == 1) {
71502ac6454SAndrew Thompson 			buf[5] = dz >> 1;
71602ac6454SAndrew Thompson 			buf[6] = dz - (dz >> 1);
71702ac6454SAndrew Thompson 			buf[7] = (((~buttons) >> 3) & MOUSE_SYS_EXTBUTTONS);
71802ac6454SAndrew Thompson 		}
71902ac6454SAndrew Thompson 		usb2_fifo_put_data_linear(sc->sc_fifo.fp[USB_FIFO_RX], buf,
72002ac6454SAndrew Thompson 		    sc->sc_mode.packetsize, 1);
72102ac6454SAndrew Thompson 
72202ac6454SAndrew Thompson 	} else {
72302ac6454SAndrew Thompson 		DPRINTF("Buffer full, discarded packet\n");
72402ac6454SAndrew Thompson 	}
72502ac6454SAndrew Thompson }
72602ac6454SAndrew Thompson 
72702ac6454SAndrew Thompson static void
72802ac6454SAndrew Thompson ums_reset_buf(struct ums_softc *sc)
72902ac6454SAndrew Thompson {
73002ac6454SAndrew Thompson 	/* reset read queue */
73102ac6454SAndrew Thompson 	usb2_fifo_reset(sc->sc_fifo.fp[USB_FIFO_RX]);
73202ac6454SAndrew Thompson }
73302ac6454SAndrew Thompson 
73402ac6454SAndrew Thompson static int
73502ac6454SAndrew Thompson ums_open(struct usb2_fifo *fifo, int fflags, struct thread *td)
73602ac6454SAndrew Thompson {
73702ac6454SAndrew Thompson 	struct ums_softc *sc = fifo->priv_sc0;
73802ac6454SAndrew Thompson 
73902ac6454SAndrew Thompson 	DPRINTFN(2, "\n");
74002ac6454SAndrew Thompson 
74102ac6454SAndrew Thompson 	if (fflags & FREAD) {
74202ac6454SAndrew Thompson 
74302ac6454SAndrew Thompson 		/* reset status */
74402ac6454SAndrew Thompson 
74502ac6454SAndrew Thompson 		sc->sc_status.flags = 0;
74602ac6454SAndrew Thompson 		sc->sc_status.button = 0;
74702ac6454SAndrew Thompson 		sc->sc_status.obutton = 0;
74802ac6454SAndrew Thompson 		sc->sc_status.dx = 0;
74902ac6454SAndrew Thompson 		sc->sc_status.dy = 0;
75002ac6454SAndrew Thompson 		sc->sc_status.dz = 0;
75102ac6454SAndrew Thompson 		/* sc->sc_status.dt = 0; */
75202ac6454SAndrew Thompson 
75302ac6454SAndrew Thompson 		if (usb2_fifo_alloc_buffer(fifo,
75402ac6454SAndrew Thompson 		    UMS_BUF_SIZE, UMS_IFQ_MAXLEN)) {
75502ac6454SAndrew Thompson 			return (ENOMEM);
75602ac6454SAndrew Thompson 		}
75702ac6454SAndrew Thompson 	}
75802ac6454SAndrew Thompson 	return (0);
75902ac6454SAndrew Thompson }
76002ac6454SAndrew Thompson 
76102ac6454SAndrew Thompson static void
76202ac6454SAndrew Thompson ums_close(struct usb2_fifo *fifo, int fflags, struct thread *td)
76302ac6454SAndrew Thompson {
76402ac6454SAndrew Thompson 	if (fflags & FREAD) {
76502ac6454SAndrew Thompson 		usb2_fifo_free_buffer(fifo);
76602ac6454SAndrew Thompson 	}
76702ac6454SAndrew Thompson }
76802ac6454SAndrew Thompson 
76902ac6454SAndrew Thompson static int
77002ac6454SAndrew Thompson ums_ioctl(struct usb2_fifo *fifo, u_long cmd, void *addr,
77102ac6454SAndrew Thompson     int fflags, struct thread *td)
77202ac6454SAndrew Thompson {
77302ac6454SAndrew Thompson 	struct ums_softc *sc = fifo->priv_sc0;
77402ac6454SAndrew Thompson 	mousemode_t mode;
77502ac6454SAndrew Thompson 	int error = 0;
77602ac6454SAndrew Thompson 
77702ac6454SAndrew Thompson 	DPRINTFN(2, "\n");
77802ac6454SAndrew Thompson 
77902ac6454SAndrew Thompson 	mtx_lock(&sc->sc_mtx);
78002ac6454SAndrew Thompson 
78102ac6454SAndrew Thompson 	switch (cmd) {
78202ac6454SAndrew Thompson 	case MOUSE_GETHWINFO:
78302ac6454SAndrew Thompson 		*(mousehw_t *)addr = sc->sc_hw;
78402ac6454SAndrew Thompson 		break;
78502ac6454SAndrew Thompson 
78602ac6454SAndrew Thompson 	case MOUSE_GETMODE:
78702ac6454SAndrew Thompson 		*(mousemode_t *)addr = sc->sc_mode;
78802ac6454SAndrew Thompson 		break;
78902ac6454SAndrew Thompson 
79002ac6454SAndrew Thompson 	case MOUSE_SETMODE:
79102ac6454SAndrew Thompson 		mode = *(mousemode_t *)addr;
79202ac6454SAndrew Thompson 
79302ac6454SAndrew Thompson 		if (mode.level == -1) {
79402ac6454SAndrew Thompson 			/* don't change the current setting */
79502ac6454SAndrew Thompson 		} else if ((mode.level < 0) || (mode.level > 1)) {
79602ac6454SAndrew Thompson 			error = EINVAL;
79702ac6454SAndrew Thompson 			goto done;
79802ac6454SAndrew Thompson 		} else {
79902ac6454SAndrew Thompson 			sc->sc_mode.level = mode.level;
80002ac6454SAndrew Thompson 		}
80102ac6454SAndrew Thompson 
80202ac6454SAndrew Thompson 		if (sc->sc_mode.level == 0) {
80302ac6454SAndrew Thompson 			if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
80402ac6454SAndrew Thompson 				sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
80502ac6454SAndrew Thompson 			else
80602ac6454SAndrew Thompson 				sc->sc_hw.buttons = sc->sc_buttons;
80702ac6454SAndrew Thompson 			sc->sc_mode.protocol = MOUSE_PROTO_MSC;
80802ac6454SAndrew Thompson 			sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
80902ac6454SAndrew Thompson 			sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
81002ac6454SAndrew Thompson 			sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
81102ac6454SAndrew Thompson 		} else if (sc->sc_mode.level == 1) {
81202ac6454SAndrew Thompson 			if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
81302ac6454SAndrew Thompson 				sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
81402ac6454SAndrew Thompson 			else
81502ac6454SAndrew Thompson 				sc->sc_hw.buttons = sc->sc_buttons;
81602ac6454SAndrew Thompson 			sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
81702ac6454SAndrew Thompson 			sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
81802ac6454SAndrew Thompson 			sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
81902ac6454SAndrew Thompson 			sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
82002ac6454SAndrew Thompson 		}
82102ac6454SAndrew Thompson 		ums_reset_buf(sc);
82202ac6454SAndrew Thompson 		break;
82302ac6454SAndrew Thompson 
82402ac6454SAndrew Thompson 	case MOUSE_GETLEVEL:
82502ac6454SAndrew Thompson 		*(int *)addr = sc->sc_mode.level;
82602ac6454SAndrew Thompson 		break;
82702ac6454SAndrew Thompson 
82802ac6454SAndrew Thompson 	case MOUSE_SETLEVEL:
82902ac6454SAndrew Thompson 		if (*(int *)addr < 0 || *(int *)addr > 1) {
83002ac6454SAndrew Thompson 			error = EINVAL;
83102ac6454SAndrew Thompson 			goto done;
83202ac6454SAndrew Thompson 		}
83302ac6454SAndrew Thompson 		sc->sc_mode.level = *(int *)addr;
83402ac6454SAndrew Thompson 
83502ac6454SAndrew Thompson 		if (sc->sc_mode.level == 0) {
83602ac6454SAndrew Thompson 			if (sc->sc_buttons > MOUSE_MSC_MAXBUTTON)
83702ac6454SAndrew Thompson 				sc->sc_hw.buttons = MOUSE_MSC_MAXBUTTON;
83802ac6454SAndrew Thompson 			else
83902ac6454SAndrew Thompson 				sc->sc_hw.buttons = sc->sc_buttons;
84002ac6454SAndrew Thompson 			sc->sc_mode.protocol = MOUSE_PROTO_MSC;
84102ac6454SAndrew Thompson 			sc->sc_mode.packetsize = MOUSE_MSC_PACKETSIZE;
84202ac6454SAndrew Thompson 			sc->sc_mode.syncmask[0] = MOUSE_MSC_SYNCMASK;
84302ac6454SAndrew Thompson 			sc->sc_mode.syncmask[1] = MOUSE_MSC_SYNC;
84402ac6454SAndrew Thompson 		} else if (sc->sc_mode.level == 1) {
84502ac6454SAndrew Thompson 			if (sc->sc_buttons > MOUSE_SYS_MAXBUTTON)
84602ac6454SAndrew Thompson 				sc->sc_hw.buttons = MOUSE_SYS_MAXBUTTON;
84702ac6454SAndrew Thompson 			else
84802ac6454SAndrew Thompson 				sc->sc_hw.buttons = sc->sc_buttons;
84902ac6454SAndrew Thompson 			sc->sc_mode.protocol = MOUSE_PROTO_SYSMOUSE;
85002ac6454SAndrew Thompson 			sc->sc_mode.packetsize = MOUSE_SYS_PACKETSIZE;
85102ac6454SAndrew Thompson 			sc->sc_mode.syncmask[0] = MOUSE_SYS_SYNCMASK;
85202ac6454SAndrew Thompson 			sc->sc_mode.syncmask[1] = MOUSE_SYS_SYNC;
85302ac6454SAndrew Thompson 		}
85402ac6454SAndrew Thompson 		ums_reset_buf(sc);
85502ac6454SAndrew Thompson 		break;
85602ac6454SAndrew Thompson 
85702ac6454SAndrew Thompson 	case MOUSE_GETSTATUS:{
85802ac6454SAndrew Thompson 			mousestatus_t *status = (mousestatus_t *)addr;
85902ac6454SAndrew Thompson 
86002ac6454SAndrew Thompson 			*status = sc->sc_status;
86102ac6454SAndrew Thompson 			sc->sc_status.obutton = sc->sc_status.button;
86202ac6454SAndrew Thompson 			sc->sc_status.button = 0;
86302ac6454SAndrew Thompson 			sc->sc_status.dx = 0;
86402ac6454SAndrew Thompson 			sc->sc_status.dy = 0;
86502ac6454SAndrew Thompson 			sc->sc_status.dz = 0;
86602ac6454SAndrew Thompson 			/* sc->sc_status.dt = 0; */
86702ac6454SAndrew Thompson 
86802ac6454SAndrew Thompson 			if (status->dx || status->dy || status->dz /* || status->dt */ ) {
86902ac6454SAndrew Thompson 				status->flags |= MOUSE_POSCHANGED;
87002ac6454SAndrew Thompson 			}
87102ac6454SAndrew Thompson 			if (status->button != status->obutton) {
87202ac6454SAndrew Thompson 				status->flags |= MOUSE_BUTTONSCHANGED;
87302ac6454SAndrew Thompson 			}
87402ac6454SAndrew Thompson 			break;
87502ac6454SAndrew Thompson 		}
87602ac6454SAndrew Thompson 	default:
87702ac6454SAndrew Thompson 		error = ENOTTY;
87802ac6454SAndrew Thompson 	}
87902ac6454SAndrew Thompson 
88002ac6454SAndrew Thompson done:
88102ac6454SAndrew Thompson 	mtx_unlock(&sc->sc_mtx);
88202ac6454SAndrew Thompson 	return (error);
88302ac6454SAndrew Thompson }
88402ac6454SAndrew Thompson 
88502ac6454SAndrew Thompson static devclass_t ums_devclass;
88602ac6454SAndrew Thompson 
88702ac6454SAndrew Thompson static device_method_t ums_methods[] = {
88802ac6454SAndrew Thompson 	DEVMETHOD(device_probe, ums_probe),
88902ac6454SAndrew Thompson 	DEVMETHOD(device_attach, ums_attach),
89002ac6454SAndrew Thompson 	DEVMETHOD(device_detach, ums_detach),
89102ac6454SAndrew Thompson 	{0, 0}
89202ac6454SAndrew Thompson };
89302ac6454SAndrew Thompson 
89402ac6454SAndrew Thompson static driver_t ums_driver = {
89502ac6454SAndrew Thompson 	.name = "ums",
89602ac6454SAndrew Thompson 	.methods = ums_methods,
89702ac6454SAndrew Thompson 	.size = sizeof(struct ums_softc),
89802ac6454SAndrew Thompson };
89902ac6454SAndrew Thompson 
90002ac6454SAndrew Thompson DRIVER_MODULE(ums, ushub, ums_driver, ums_devclass, NULL, 0);
90102ac6454SAndrew Thompson MODULE_DEPEND(ums, usb, 1, 1, 1);
902