xref: /freebsd/sys/dev/usb/input/uhid.c (revision 718cf2ccb9956613756ab15d7a0e28f2c8e91cab)
102ac6454SAndrew Thompson /*	$NetBSD: uhid.c,v 1.46 2001/11/13 06:24:55 lukem Exp $	*/
202ac6454SAndrew Thompson 
302ac6454SAndrew Thompson /* Also already merged from NetBSD:
402ac6454SAndrew Thompson  *	$NetBSD: uhid.c,v 1.54 2002/09/23 05:51:21 simonb Exp $
502ac6454SAndrew Thompson  */
602ac6454SAndrew Thompson 
702ac6454SAndrew Thompson #include <sys/cdefs.h>
802ac6454SAndrew Thompson __FBSDID("$FreeBSD$");
902ac6454SAndrew Thompson 
1002ac6454SAndrew Thompson /*-
11*718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
12*718cf2ccSPedro F. Giffuni  *
1302ac6454SAndrew Thompson  * Copyright (c) 1998 The NetBSD Foundation, Inc.
1402ac6454SAndrew Thompson  * All rights reserved.
1502ac6454SAndrew Thompson  *
1602ac6454SAndrew Thompson  * This code is derived from software contributed to The NetBSD Foundation
1702ac6454SAndrew Thompson  * by Lennart Augustsson (lennart@augustsson.net) at
1802ac6454SAndrew Thompson  * Carlstedt Research & Technology.
1902ac6454SAndrew Thompson  *
2002ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
2102ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
2202ac6454SAndrew Thompson  * are met:
2302ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
2402ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
2502ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
2602ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
2702ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
2802ac6454SAndrew Thompson  *
2902ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
3002ac6454SAndrew Thompson  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
3102ac6454SAndrew Thompson  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
3202ac6454SAndrew Thompson  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
3302ac6454SAndrew Thompson  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3402ac6454SAndrew Thompson  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3502ac6454SAndrew Thompson  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3602ac6454SAndrew Thompson  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3702ac6454SAndrew Thompson  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3802ac6454SAndrew Thompson  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3902ac6454SAndrew Thompson  * POSSIBILITY OF SUCH DAMAGE.
4002ac6454SAndrew Thompson  */
4102ac6454SAndrew Thompson 
4202ac6454SAndrew Thompson /*
4302ac6454SAndrew Thompson  * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
4402ac6454SAndrew Thompson  */
4502ac6454SAndrew Thompson 
46ed6d949aSAndrew Thompson #include <sys/stdint.h>
47ed6d949aSAndrew Thompson #include <sys/stddef.h>
48ed6d949aSAndrew Thompson #include <sys/param.h>
49ed6d949aSAndrew Thompson #include <sys/queue.h>
50ed6d949aSAndrew Thompson #include <sys/types.h>
51ed6d949aSAndrew Thompson #include <sys/systm.h>
52ed6d949aSAndrew Thompson #include <sys/kernel.h>
53ed6d949aSAndrew Thompson #include <sys/bus.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 "usbdevs.h"
6802ac6454SAndrew Thompson #include <dev/usb/usb.h>
69ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
70ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h>
7102ac6454SAndrew Thompson #include <dev/usb/usbhid.h>
7202ac6454SAndrew Thompson #include <dev/usb/usb_ioctl.h>
7302ac6454SAndrew Thompson 
7402ac6454SAndrew Thompson #define	USB_DEBUG_VAR uhid_debug
7502ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
7602ac6454SAndrew Thompson 
7702ac6454SAndrew Thompson #include <dev/usb/input/usb_rdesc.h>
7802ac6454SAndrew Thompson #include <dev/usb/quirk/usb_quirk.h>
7902ac6454SAndrew Thompson 
80b850ecc1SAndrew Thompson #ifdef USB_DEBUG
8102ac6454SAndrew Thompson static int uhid_debug = 0;
8202ac6454SAndrew Thompson 
836472ac3dSEd Schouten static SYSCTL_NODE(_hw_usb, OID_AUTO, uhid, CTLFLAG_RW, 0, "USB uhid");
84ece4b0bdSHans Petter Selasky SYSCTL_INT(_hw_usb_uhid, OID_AUTO, debug, CTLFLAG_RWTUN,
8502ac6454SAndrew Thompson     &uhid_debug, 0, "Debug level");
8602ac6454SAndrew Thompson #endif
8702ac6454SAndrew Thompson 
8802ac6454SAndrew Thompson #define	UHID_BSIZE	1024		/* bytes, buffer size */
8902ac6454SAndrew Thompson #define	UHID_FRAME_NUM 	  50		/* bytes, frame number */
9002ac6454SAndrew Thompson 
9102ac6454SAndrew Thompson enum {
923a38def3SAlexander Motin 	UHID_INTR_DT_WR,
9302ac6454SAndrew Thompson 	UHID_INTR_DT_RD,
9402ac6454SAndrew Thompson 	UHID_CTRL_DT_WR,
9502ac6454SAndrew Thompson 	UHID_CTRL_DT_RD,
9602ac6454SAndrew Thompson 	UHID_N_TRANSFER,
9702ac6454SAndrew Thompson };
9802ac6454SAndrew Thompson 
9902ac6454SAndrew Thompson struct uhid_softc {
100760bc48eSAndrew Thompson 	struct usb_fifo_sc sc_fifo;
10102ac6454SAndrew Thompson 	struct mtx sc_mtx;
10202ac6454SAndrew Thompson 
103760bc48eSAndrew Thompson 	struct usb_xfer *sc_xfer[UHID_N_TRANSFER];
104760bc48eSAndrew Thompson 	struct usb_device *sc_udev;
10502ac6454SAndrew Thompson 	void   *sc_repdesc_ptr;
10602ac6454SAndrew Thompson 
10702ac6454SAndrew Thompson 	uint32_t sc_isize;
10802ac6454SAndrew Thompson 	uint32_t sc_osize;
10902ac6454SAndrew Thompson 	uint32_t sc_fsize;
11002ac6454SAndrew Thompson 
11102ac6454SAndrew Thompson 	uint16_t sc_repdesc_size;
11202ac6454SAndrew Thompson 
11302ac6454SAndrew Thompson 	uint8_t	sc_iface_no;
11402ac6454SAndrew Thompson 	uint8_t	sc_iface_index;
11502ac6454SAndrew Thompson 	uint8_t	sc_iid;
11602ac6454SAndrew Thompson 	uint8_t	sc_oid;
11702ac6454SAndrew Thompson 	uint8_t	sc_fid;
11802ac6454SAndrew Thompson 	uint8_t	sc_flags;
11902ac6454SAndrew Thompson #define	UHID_FLAG_IMMED        0x01	/* set if read should be immediate */
12002ac6454SAndrew Thompson #define	UHID_FLAG_STATIC_DESC  0x04	/* set if report descriptors are
12102ac6454SAndrew Thompson 					 * static */
12202ac6454SAndrew Thompson };
12302ac6454SAndrew Thompson 
12402ac6454SAndrew Thompson static const uint8_t uhid_xb360gp_report_descr[] = {UHID_XB360GP_REPORT_DESCR()};
12502ac6454SAndrew Thompson static const uint8_t uhid_graphire_report_descr[] = {UHID_GRAPHIRE_REPORT_DESCR()};
12602ac6454SAndrew Thompson static const uint8_t uhid_graphire3_4x5_report_descr[] = {UHID_GRAPHIRE3_4X5_REPORT_DESCR()};
12702ac6454SAndrew Thompson 
12802ac6454SAndrew Thompson /* prototypes */
12902ac6454SAndrew Thompson 
13002ac6454SAndrew Thompson static device_probe_t uhid_probe;
13102ac6454SAndrew Thompson static device_attach_t uhid_attach;
13202ac6454SAndrew Thompson static device_detach_t uhid_detach;
13302ac6454SAndrew Thompson 
1343a38def3SAlexander Motin static usb_callback_t uhid_intr_write_callback;
1353a38def3SAlexander Motin static usb_callback_t uhid_intr_read_callback;
136e0a69b51SAndrew Thompson static usb_callback_t uhid_write_callback;
137e0a69b51SAndrew Thompson static usb_callback_t uhid_read_callback;
13802ac6454SAndrew Thompson 
139e0a69b51SAndrew Thompson static usb_fifo_cmd_t uhid_start_read;
140e0a69b51SAndrew Thompson static usb_fifo_cmd_t uhid_stop_read;
141e0a69b51SAndrew Thompson static usb_fifo_cmd_t uhid_start_write;
142e0a69b51SAndrew Thompson static usb_fifo_cmd_t uhid_stop_write;
143e0a69b51SAndrew Thompson static usb_fifo_open_t uhid_open;
144e0a69b51SAndrew Thompson static usb_fifo_close_t uhid_close;
145e0a69b51SAndrew Thompson static usb_fifo_ioctl_t uhid_ioctl;
14602ac6454SAndrew Thompson 
147760bc48eSAndrew Thompson static struct usb_fifo_methods uhid_fifo_methods = {
14802ac6454SAndrew Thompson 	.f_open = &uhid_open,
14902ac6454SAndrew Thompson 	.f_close = &uhid_close,
15002ac6454SAndrew Thompson 	.f_ioctl = &uhid_ioctl,
15102ac6454SAndrew Thompson 	.f_start_read = &uhid_start_read,
15202ac6454SAndrew Thompson 	.f_stop_read = &uhid_stop_read,
15302ac6454SAndrew Thompson 	.f_start_write = &uhid_start_write,
15402ac6454SAndrew Thompson 	.f_stop_write = &uhid_stop_write,
15502ac6454SAndrew Thompson 	.basename[0] = "uhid",
15602ac6454SAndrew Thompson };
15702ac6454SAndrew Thompson 
15802ac6454SAndrew Thompson static void
1593a38def3SAlexander Motin uhid_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
1603a38def3SAlexander Motin {
1613a38def3SAlexander Motin 	struct uhid_softc *sc = usbd_xfer_softc(xfer);
1623a38def3SAlexander Motin 	struct usb_page_cache *pc;
1633a38def3SAlexander Motin 	int actlen;
1643a38def3SAlexander Motin 
1653a38def3SAlexander Motin 	switch (USB_GET_STATE(xfer)) {
1663a38def3SAlexander Motin 	case USB_ST_TRANSFERRED:
1673a38def3SAlexander Motin 	case USB_ST_SETUP:
1683a38def3SAlexander Motin tr_setup:
1693a38def3SAlexander Motin 		pc = usbd_xfer_get_frame(xfer, 0);
1703a38def3SAlexander Motin 		if (usb_fifo_get_data(sc->sc_fifo.fp[USB_FIFO_TX], pc,
1713a38def3SAlexander Motin 		    0, usbd_xfer_max_len(xfer), &actlen, 0)) {
1723a38def3SAlexander Motin 			usbd_xfer_set_frame_len(xfer, 0, actlen);
1733a38def3SAlexander Motin 			usbd_transfer_submit(xfer);
1743a38def3SAlexander Motin 		}
1753a38def3SAlexander Motin 		return;
1763a38def3SAlexander Motin 
1773a38def3SAlexander Motin 	default:			/* Error */
1783a38def3SAlexander Motin 		if (error != USB_ERR_CANCELLED) {
1793a38def3SAlexander Motin 			/* try to clear stall first */
1803a38def3SAlexander Motin 			usbd_xfer_set_stall(xfer);
1813a38def3SAlexander Motin 			goto tr_setup;
1823a38def3SAlexander Motin 		}
1833a38def3SAlexander Motin 		return;
1843a38def3SAlexander Motin 	}
1853a38def3SAlexander Motin }
1863a38def3SAlexander Motin 
1873a38def3SAlexander Motin static void
1883a38def3SAlexander Motin uhid_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
18902ac6454SAndrew Thompson {
190ed6d949aSAndrew Thompson 	struct uhid_softc *sc = usbd_xfer_softc(xfer);
191ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
192ed6d949aSAndrew Thompson 	int actlen;
193ed6d949aSAndrew Thompson 
194ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
19502ac6454SAndrew Thompson 
19602ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
19702ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
19802ac6454SAndrew Thompson 		DPRINTF("transferred!\n");
19902ac6454SAndrew Thompson 
200ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
2010ca80d71SAndrew Thompson 
2020ca80d71SAndrew Thompson 		/*
2030ca80d71SAndrew Thompson 		 * If the ID byte is non zero we allow descriptors
2040ca80d71SAndrew Thompson 		 * having multiple sizes:
2050ca80d71SAndrew Thompson 		 */
2066d917491SHans Petter Selasky 		if ((actlen >= (int)sc->sc_isize) ||
2070ca80d71SAndrew Thompson 		    ((actlen > 0) && (sc->sc_iid != 0))) {
2080ca80d71SAndrew Thompson 			/* limit report length to the maximum */
2096d917491SHans Petter Selasky 			if (actlen > (int)sc->sc_isize)
2100ca80d71SAndrew Thompson 				actlen = sc->sc_isize;
211ed6d949aSAndrew Thompson 			usb_fifo_put_data(sc->sc_fifo.fp[USB_FIFO_RX], pc,
2120ca80d71SAndrew Thompson 			    0, actlen, 1);
21302ac6454SAndrew Thompson 		} else {
21402ac6454SAndrew Thompson 			/* ignore it */
2150ca80d71SAndrew Thompson 			DPRINTF("ignored transfer, %d bytes\n", actlen);
21602ac6454SAndrew Thompson 		}
21702ac6454SAndrew Thompson 
21802ac6454SAndrew Thompson 	case USB_ST_SETUP:
21902ac6454SAndrew Thompson re_submit:
220a593f6b8SAndrew Thompson 		if (usb_fifo_put_bytes_max(
22102ac6454SAndrew Thompson 		    sc->sc_fifo.fp[USB_FIFO_RX]) != 0) {
222ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, sc->sc_isize);
223a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
22402ac6454SAndrew Thompson 		}
22502ac6454SAndrew Thompson 		return;
22602ac6454SAndrew Thompson 
22702ac6454SAndrew Thompson 	default:			/* Error */
228ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
22902ac6454SAndrew Thompson 			/* try to clear stall first */
230ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
23102ac6454SAndrew Thompson 			goto re_submit;
23202ac6454SAndrew Thompson 		}
23302ac6454SAndrew Thompson 		return;
23402ac6454SAndrew Thompson 	}
23502ac6454SAndrew Thompson }
23602ac6454SAndrew Thompson 
23702ac6454SAndrew Thompson static void
238760bc48eSAndrew Thompson uhid_fill_set_report(struct usb_device_request *req, uint8_t iface_no,
23902ac6454SAndrew Thompson     uint8_t type, uint8_t id, uint16_t size)
24002ac6454SAndrew Thompson {
24102ac6454SAndrew Thompson 	req->bmRequestType = UT_WRITE_CLASS_INTERFACE;
24202ac6454SAndrew Thompson 	req->bRequest = UR_SET_REPORT;
24302ac6454SAndrew Thompson 	USETW2(req->wValue, type, id);
24402ac6454SAndrew Thompson 	req->wIndex[0] = iface_no;
24502ac6454SAndrew Thompson 	req->wIndex[1] = 0;
24602ac6454SAndrew Thompson 	USETW(req->wLength, size);
24702ac6454SAndrew Thompson }
24802ac6454SAndrew Thompson 
24902ac6454SAndrew Thompson static void
250760bc48eSAndrew Thompson uhid_fill_get_report(struct usb_device_request *req, uint8_t iface_no,
25102ac6454SAndrew Thompson     uint8_t type, uint8_t id, uint16_t size)
25202ac6454SAndrew Thompson {
25302ac6454SAndrew Thompson 	req->bmRequestType = UT_READ_CLASS_INTERFACE;
25402ac6454SAndrew Thompson 	req->bRequest = UR_GET_REPORT;
25502ac6454SAndrew Thompson 	USETW2(req->wValue, type, id);
25602ac6454SAndrew Thompson 	req->wIndex[0] = iface_no;
25702ac6454SAndrew Thompson 	req->wIndex[1] = 0;
25802ac6454SAndrew Thompson 	USETW(req->wLength, size);
25902ac6454SAndrew Thompson }
26002ac6454SAndrew Thompson 
26102ac6454SAndrew Thompson static void
262ed6d949aSAndrew Thompson uhid_write_callback(struct usb_xfer *xfer, usb_error_t error)
26302ac6454SAndrew Thompson {
264ed6d949aSAndrew Thompson 	struct uhid_softc *sc = usbd_xfer_softc(xfer);
265760bc48eSAndrew Thompson 	struct usb_device_request req;
266ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
26702ac6454SAndrew Thompson 	uint32_t size = sc->sc_osize;
26802ac6454SAndrew Thompson 	uint32_t actlen;
26902ac6454SAndrew Thompson 	uint8_t id;
27002ac6454SAndrew Thompson 
27102ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
27202ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
27302ac6454SAndrew Thompson 	case USB_ST_SETUP:
27402ac6454SAndrew Thompson 		/* try to extract the ID byte */
27502ac6454SAndrew Thompson 		if (sc->sc_oid) {
276ed6d949aSAndrew Thompson 			pc = usbd_xfer_get_frame(xfer, 0);
277ed6d949aSAndrew Thompson 			if (usb_fifo_get_data(sc->sc_fifo.fp[USB_FIFO_TX], pc,
27802ac6454SAndrew Thompson 			    0, 1, &actlen, 0)) {
27902ac6454SAndrew Thompson 				if (actlen != 1) {
28002ac6454SAndrew Thompson 					goto tr_error;
28102ac6454SAndrew Thompson 				}
282ed6d949aSAndrew Thompson 				usbd_copy_out(pc, 0, &id, 1);
28302ac6454SAndrew Thompson 
28402ac6454SAndrew Thompson 			} else {
28502ac6454SAndrew Thompson 				return;
28602ac6454SAndrew Thompson 			}
28702ac6454SAndrew Thompson 			if (size) {
28802ac6454SAndrew Thompson 				size--;
28902ac6454SAndrew Thompson 			}
29002ac6454SAndrew Thompson 		} else {
29102ac6454SAndrew Thompson 			id = 0;
29202ac6454SAndrew Thompson 		}
29302ac6454SAndrew Thompson 
294ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 1);
295ed6d949aSAndrew Thompson 		if (usb_fifo_get_data(sc->sc_fifo.fp[USB_FIFO_TX], pc,
29602ac6454SAndrew Thompson 		    0, UHID_BSIZE, &actlen, 1)) {
29702ac6454SAndrew Thompson 			if (actlen != size) {
29802ac6454SAndrew Thompson 				goto tr_error;
29902ac6454SAndrew Thompson 			}
30002ac6454SAndrew Thompson 			uhid_fill_set_report
30102ac6454SAndrew Thompson 			    (&req, sc->sc_iface_no,
30202ac6454SAndrew Thompson 			    UHID_OUTPUT_REPORT, id, size);
30302ac6454SAndrew Thompson 
304ed6d949aSAndrew Thompson 			pc = usbd_xfer_get_frame(xfer, 0);
305ed6d949aSAndrew Thompson 			usbd_copy_in(pc, 0, &req, sizeof(req));
30602ac6454SAndrew Thompson 
307ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
308ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 1, size);
309ed6d949aSAndrew Thompson 			usbd_xfer_set_frames(xfer, size ? 2 : 1);
310a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
31102ac6454SAndrew Thompson 		}
31202ac6454SAndrew Thompson 		return;
31302ac6454SAndrew Thompson 
31402ac6454SAndrew Thompson 	default:
31502ac6454SAndrew Thompson tr_error:
31602ac6454SAndrew Thompson 		/* bomb out */
317a593f6b8SAndrew Thompson 		usb_fifo_get_data_error(sc->sc_fifo.fp[USB_FIFO_TX]);
31802ac6454SAndrew Thompson 		return;
31902ac6454SAndrew Thompson 	}
32002ac6454SAndrew Thompson }
32102ac6454SAndrew Thompson 
32202ac6454SAndrew Thompson static void
323ed6d949aSAndrew Thompson uhid_read_callback(struct usb_xfer *xfer, usb_error_t error)
32402ac6454SAndrew Thompson {
325ed6d949aSAndrew Thompson 	struct uhid_softc *sc = usbd_xfer_softc(xfer);
326760bc48eSAndrew Thompson 	struct usb_device_request req;
327ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
328ed6d949aSAndrew Thompson 
329ed6d949aSAndrew Thompson 	pc = usbd_xfer_get_frame(xfer, 0);
33002ac6454SAndrew Thompson 
33102ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
33202ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
333ed6d949aSAndrew Thompson 		usb_fifo_put_data(sc->sc_fifo.fp[USB_FIFO_RX], pc, sizeof(req),
334ed6d949aSAndrew Thompson 		    sc->sc_isize, 1);
33502ac6454SAndrew Thompson 		return;
33602ac6454SAndrew Thompson 
33702ac6454SAndrew Thompson 	case USB_ST_SETUP:
33802ac6454SAndrew Thompson 
339a593f6b8SAndrew Thompson 		if (usb_fifo_put_bytes_max(sc->sc_fifo.fp[USB_FIFO_RX]) > 0) {
34002ac6454SAndrew Thompson 
34102ac6454SAndrew Thompson 			uhid_fill_get_report
34202ac6454SAndrew Thompson 			    (&req, sc->sc_iface_no, UHID_INPUT_REPORT,
34302ac6454SAndrew Thompson 			    sc->sc_iid, sc->sc_isize);
34402ac6454SAndrew Thompson 
345ed6d949aSAndrew Thompson 			usbd_copy_in(pc, 0, &req, sizeof(req));
34602ac6454SAndrew Thompson 
347ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
348ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 1, sc->sc_isize);
349ed6d949aSAndrew Thompson 			usbd_xfer_set_frames(xfer, sc->sc_isize ? 2 : 1);
350a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
35102ac6454SAndrew Thompson 		}
35202ac6454SAndrew Thompson 		return;
35302ac6454SAndrew Thompson 
35402ac6454SAndrew Thompson 	default:			/* Error */
35502ac6454SAndrew Thompson 		/* bomb out */
356a593f6b8SAndrew Thompson 		usb_fifo_put_data_error(sc->sc_fifo.fp[USB_FIFO_RX]);
35702ac6454SAndrew Thompson 		return;
35802ac6454SAndrew Thompson 	}
35902ac6454SAndrew Thompson }
36002ac6454SAndrew Thompson 
361760bc48eSAndrew Thompson static const struct usb_config uhid_config[UHID_N_TRANSFER] = {
36202ac6454SAndrew Thompson 
3633a38def3SAlexander Motin 	[UHID_INTR_DT_WR] = {
3643a38def3SAlexander Motin 		.type = UE_INTERRUPT,
3653a38def3SAlexander Motin 		.endpoint = UE_ADDR_ANY,
3663a38def3SAlexander Motin 		.direction = UE_DIR_OUT,
3673dfe1a8eSAlexander Motin 		.flags = {.pipe_bof = 1,.no_pipe_ok = 1, },
3683a38def3SAlexander Motin 		.bufsize = UHID_BSIZE,
3693a38def3SAlexander Motin 		.callback = &uhid_intr_write_callback,
3703a38def3SAlexander Motin 	},
3713a38def3SAlexander Motin 
37202ac6454SAndrew Thompson 	[UHID_INTR_DT_RD] = {
37302ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
37402ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
37502ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
3764eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
3774eae601eSAndrew Thompson 		.bufsize = UHID_BSIZE,
3783a38def3SAlexander Motin 		.callback = &uhid_intr_read_callback,
37902ac6454SAndrew Thompson 	},
38002ac6454SAndrew Thompson 
38102ac6454SAndrew Thompson 	[UHID_CTRL_DT_WR] = {
38202ac6454SAndrew Thompson 		.type = UE_CONTROL,
38302ac6454SAndrew Thompson 		.endpoint = 0x00,	/* Control pipe */
38402ac6454SAndrew Thompson 		.direction = UE_DIR_ANY,
385760bc48eSAndrew Thompson 		.bufsize = sizeof(struct usb_device_request) + UHID_BSIZE,
3864eae601eSAndrew Thompson 		.callback = &uhid_write_callback,
3874eae601eSAndrew Thompson 		.timeout = 1000,	/* 1 second */
38802ac6454SAndrew Thompson 	},
38902ac6454SAndrew Thompson 
39002ac6454SAndrew Thompson 	[UHID_CTRL_DT_RD] = {
39102ac6454SAndrew Thompson 		.type = UE_CONTROL,
39202ac6454SAndrew Thompson 		.endpoint = 0x00,	/* Control pipe */
39302ac6454SAndrew Thompson 		.direction = UE_DIR_ANY,
394760bc48eSAndrew Thompson 		.bufsize = sizeof(struct usb_device_request) + UHID_BSIZE,
3954eae601eSAndrew Thompson 		.callback = &uhid_read_callback,
3964eae601eSAndrew Thompson 		.timeout = 1000,	/* 1 second */
39702ac6454SAndrew Thompson 	},
39802ac6454SAndrew Thompson };
39902ac6454SAndrew Thompson 
40002ac6454SAndrew Thompson static void
401760bc48eSAndrew Thompson uhid_start_read(struct usb_fifo *fifo)
40202ac6454SAndrew Thompson {
403ed6d949aSAndrew Thompson 	struct uhid_softc *sc = usb_fifo_softc(fifo);
40402ac6454SAndrew Thompson 
40502ac6454SAndrew Thompson 	if (sc->sc_flags & UHID_FLAG_IMMED) {
406a593f6b8SAndrew Thompson 		usbd_transfer_start(sc->sc_xfer[UHID_CTRL_DT_RD]);
40702ac6454SAndrew Thompson 	} else {
408a593f6b8SAndrew Thompson 		usbd_transfer_start(sc->sc_xfer[UHID_INTR_DT_RD]);
40902ac6454SAndrew Thompson 	}
41002ac6454SAndrew Thompson }
41102ac6454SAndrew Thompson 
41202ac6454SAndrew Thompson static void
413760bc48eSAndrew Thompson uhid_stop_read(struct usb_fifo *fifo)
41402ac6454SAndrew Thompson {
415ed6d949aSAndrew Thompson 	struct uhid_softc *sc = usb_fifo_softc(fifo);
41602ac6454SAndrew Thompson 
417a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UHID_CTRL_DT_RD]);
418a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UHID_INTR_DT_RD]);
41902ac6454SAndrew Thompson }
42002ac6454SAndrew Thompson 
42102ac6454SAndrew Thompson static void
422760bc48eSAndrew Thompson uhid_start_write(struct usb_fifo *fifo)
42302ac6454SAndrew Thompson {
424ed6d949aSAndrew Thompson 	struct uhid_softc *sc = usb_fifo_softc(fifo);
42502ac6454SAndrew Thompson 
4263dfe1a8eSAlexander Motin 	if ((sc->sc_flags & UHID_FLAG_IMMED) ||
4273dfe1a8eSAlexander Motin 	    sc->sc_xfer[UHID_INTR_DT_WR] == NULL) {
428a593f6b8SAndrew Thompson 		usbd_transfer_start(sc->sc_xfer[UHID_CTRL_DT_WR]);
4293a38def3SAlexander Motin 	} else {
4303a38def3SAlexander Motin 		usbd_transfer_start(sc->sc_xfer[UHID_INTR_DT_WR]);
4313a38def3SAlexander Motin 	}
43202ac6454SAndrew Thompson }
43302ac6454SAndrew Thompson 
43402ac6454SAndrew Thompson static void
435760bc48eSAndrew Thompson uhid_stop_write(struct usb_fifo *fifo)
43602ac6454SAndrew Thompson {
437ed6d949aSAndrew Thompson 	struct uhid_softc *sc = usb_fifo_softc(fifo);
43802ac6454SAndrew Thompson 
439a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UHID_CTRL_DT_WR]);
4403a38def3SAlexander Motin 	usbd_transfer_stop(sc->sc_xfer[UHID_INTR_DT_WR]);
44102ac6454SAndrew Thompson }
44202ac6454SAndrew Thompson 
44302ac6454SAndrew Thompson static int
44402ac6454SAndrew Thompson uhid_get_report(struct uhid_softc *sc, uint8_t type,
44502ac6454SAndrew Thompson     uint8_t id, void *kern_data, void *user_data,
44602ac6454SAndrew Thompson     uint16_t len)
44702ac6454SAndrew Thompson {
44802ac6454SAndrew Thompson 	int err;
44902ac6454SAndrew Thompson 	uint8_t free_data = 0;
45002ac6454SAndrew Thompson 
45102ac6454SAndrew Thompson 	if (kern_data == NULL) {
45202ac6454SAndrew Thompson 		kern_data = malloc(len, M_USBDEV, M_WAITOK);
45302ac6454SAndrew Thompson 		if (kern_data == NULL) {
45402ac6454SAndrew Thompson 			err = ENOMEM;
45502ac6454SAndrew Thompson 			goto done;
45602ac6454SAndrew Thompson 		}
45702ac6454SAndrew Thompson 		free_data = 1;
45802ac6454SAndrew Thompson 	}
459a593f6b8SAndrew Thompson 	err = usbd_req_get_report(sc->sc_udev, NULL, kern_data,
46002ac6454SAndrew Thompson 	    len, sc->sc_iface_index, type, id);
46102ac6454SAndrew Thompson 	if (err) {
46202ac6454SAndrew Thompson 		err = ENXIO;
46302ac6454SAndrew Thompson 		goto done;
46402ac6454SAndrew Thompson 	}
46502ac6454SAndrew Thompson 	if (user_data) {
46602ac6454SAndrew Thompson 		/* dummy buffer */
46702ac6454SAndrew Thompson 		err = copyout(kern_data, user_data, len);
46802ac6454SAndrew Thompson 		if (err) {
46902ac6454SAndrew Thompson 			goto done;
47002ac6454SAndrew Thompson 		}
47102ac6454SAndrew Thompson 	}
47202ac6454SAndrew Thompson done:
47302ac6454SAndrew Thompson 	if (free_data) {
47402ac6454SAndrew Thompson 		free(kern_data, M_USBDEV);
47502ac6454SAndrew Thompson 	}
47602ac6454SAndrew Thompson 	return (err);
47702ac6454SAndrew Thompson }
47802ac6454SAndrew Thompson 
47902ac6454SAndrew Thompson static int
48002ac6454SAndrew Thompson uhid_set_report(struct uhid_softc *sc, uint8_t type,
48102ac6454SAndrew Thompson     uint8_t id, void *kern_data, void *user_data,
48202ac6454SAndrew Thompson     uint16_t len)
48302ac6454SAndrew Thompson {
48402ac6454SAndrew Thompson 	int err;
48502ac6454SAndrew Thompson 	uint8_t free_data = 0;
48602ac6454SAndrew Thompson 
48702ac6454SAndrew Thompson 	if (kern_data == NULL) {
48802ac6454SAndrew Thompson 		kern_data = malloc(len, M_USBDEV, M_WAITOK);
48902ac6454SAndrew Thompson 		if (kern_data == NULL) {
49002ac6454SAndrew Thompson 			err = ENOMEM;
49102ac6454SAndrew Thompson 			goto done;
49202ac6454SAndrew Thompson 		}
49302ac6454SAndrew Thompson 		free_data = 1;
49402ac6454SAndrew Thompson 		err = copyin(user_data, kern_data, len);
49502ac6454SAndrew Thompson 		if (err) {
49602ac6454SAndrew Thompson 			goto done;
49702ac6454SAndrew Thompson 		}
49802ac6454SAndrew Thompson 	}
499a593f6b8SAndrew Thompson 	err = usbd_req_set_report(sc->sc_udev, NULL, kern_data,
50002ac6454SAndrew Thompson 	    len, sc->sc_iface_index, type, id);
50102ac6454SAndrew Thompson 	if (err) {
50202ac6454SAndrew Thompson 		err = ENXIO;
50302ac6454SAndrew Thompson 		goto done;
50402ac6454SAndrew Thompson 	}
50502ac6454SAndrew Thompson done:
50602ac6454SAndrew Thompson 	if (free_data) {
50702ac6454SAndrew Thompson 		free(kern_data, M_USBDEV);
50802ac6454SAndrew Thompson 	}
50902ac6454SAndrew Thompson 	return (err);
51002ac6454SAndrew Thompson }
51102ac6454SAndrew Thompson 
51202ac6454SAndrew Thompson static int
513760bc48eSAndrew Thompson uhid_open(struct usb_fifo *fifo, int fflags)
51402ac6454SAndrew Thompson {
515ed6d949aSAndrew Thompson 	struct uhid_softc *sc = usb_fifo_softc(fifo);
51602ac6454SAndrew Thompson 
51702ac6454SAndrew Thompson 	/*
51802ac6454SAndrew Thompson 	 * The buffers are one byte larger than maximum so that one
51902ac6454SAndrew Thompson 	 * can detect too large read/writes and short transfers:
52002ac6454SAndrew Thompson 	 */
52102ac6454SAndrew Thompson 	if (fflags & FREAD) {
52202ac6454SAndrew Thompson 		/* reset flags */
523996f8471SHans Petter Selasky 		mtx_lock(&sc->sc_mtx);
52402ac6454SAndrew Thompson 		sc->sc_flags &= ~UHID_FLAG_IMMED;
525996f8471SHans Petter Selasky 		mtx_unlock(&sc->sc_mtx);
52602ac6454SAndrew Thompson 
527a593f6b8SAndrew Thompson 		if (usb_fifo_alloc_buffer(fifo,
52802ac6454SAndrew Thompson 		    sc->sc_isize + 1, UHID_FRAME_NUM)) {
52902ac6454SAndrew Thompson 			return (ENOMEM);
53002ac6454SAndrew Thompson 		}
53102ac6454SAndrew Thompson 	}
53202ac6454SAndrew Thompson 	if (fflags & FWRITE) {
533a593f6b8SAndrew Thompson 		if (usb_fifo_alloc_buffer(fifo,
53402ac6454SAndrew Thompson 		    sc->sc_osize + 1, UHID_FRAME_NUM)) {
53502ac6454SAndrew Thompson 			return (ENOMEM);
53602ac6454SAndrew Thompson 		}
53702ac6454SAndrew Thompson 	}
53802ac6454SAndrew Thompson 	return (0);
53902ac6454SAndrew Thompson }
54002ac6454SAndrew Thompson 
54102ac6454SAndrew Thompson static void
542760bc48eSAndrew Thompson uhid_close(struct usb_fifo *fifo, int fflags)
54302ac6454SAndrew Thompson {
54402ac6454SAndrew Thompson 	if (fflags & (FREAD | FWRITE)) {
545a593f6b8SAndrew Thompson 		usb_fifo_free_buffer(fifo);
54602ac6454SAndrew Thompson 	}
54702ac6454SAndrew Thompson }
54802ac6454SAndrew Thompson 
54902ac6454SAndrew Thompson static int
550760bc48eSAndrew Thompson uhid_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr,
551ee3e3ff5SAndrew Thompson     int fflags)
55202ac6454SAndrew Thompson {
553ed6d949aSAndrew Thompson 	struct uhid_softc *sc = usb_fifo_softc(fifo);
554760bc48eSAndrew Thompson 	struct usb_gen_descriptor *ugd;
55502ac6454SAndrew Thompson 	uint32_t size;
55602ac6454SAndrew Thompson 	int error = 0;
55702ac6454SAndrew Thompson 	uint8_t id;
55802ac6454SAndrew Thompson 
55902ac6454SAndrew Thompson 	switch (cmd) {
56002ac6454SAndrew Thompson 	case USB_GET_REPORT_DESC:
56102ac6454SAndrew Thompson 		ugd = addr;
56202ac6454SAndrew Thompson 		if (sc->sc_repdesc_size > ugd->ugd_maxlen) {
56302ac6454SAndrew Thompson 			size = ugd->ugd_maxlen;
56402ac6454SAndrew Thompson 		} else {
56502ac6454SAndrew Thompson 			size = sc->sc_repdesc_size;
56602ac6454SAndrew Thompson 		}
56702ac6454SAndrew Thompson 		ugd->ugd_actlen = size;
56802ac6454SAndrew Thompson 		if (ugd->ugd_data == NULL)
56902ac6454SAndrew Thompson 			break;		/* descriptor length only */
57002ac6454SAndrew Thompson 		error = copyout(sc->sc_repdesc_ptr, ugd->ugd_data, size);
57102ac6454SAndrew Thompson 		break;
57202ac6454SAndrew Thompson 
57302ac6454SAndrew Thompson 	case USB_SET_IMMED:
57402ac6454SAndrew Thompson 		if (!(fflags & FREAD)) {
57502ac6454SAndrew Thompson 			error = EPERM;
57602ac6454SAndrew Thompson 			break;
57702ac6454SAndrew Thompson 		}
57802ac6454SAndrew Thompson 		if (*(int *)addr) {
57902ac6454SAndrew Thompson 
58002ac6454SAndrew Thompson 			/* do a test read */
58102ac6454SAndrew Thompson 
58202ac6454SAndrew Thompson 			error = uhid_get_report(sc, UHID_INPUT_REPORT,
58302ac6454SAndrew Thompson 			    sc->sc_iid, NULL, NULL, sc->sc_isize);
58402ac6454SAndrew Thompson 			if (error) {
58502ac6454SAndrew Thompson 				break;
58602ac6454SAndrew Thompson 			}
58702ac6454SAndrew Thompson 			mtx_lock(&sc->sc_mtx);
58802ac6454SAndrew Thompson 			sc->sc_flags |= UHID_FLAG_IMMED;
58902ac6454SAndrew Thompson 			mtx_unlock(&sc->sc_mtx);
59002ac6454SAndrew Thompson 		} else {
59102ac6454SAndrew Thompson 			mtx_lock(&sc->sc_mtx);
59202ac6454SAndrew Thompson 			sc->sc_flags &= ~UHID_FLAG_IMMED;
59302ac6454SAndrew Thompson 			mtx_unlock(&sc->sc_mtx);
59402ac6454SAndrew Thompson 		}
59502ac6454SAndrew Thompson 		break;
59602ac6454SAndrew Thompson 
59702ac6454SAndrew Thompson 	case USB_GET_REPORT:
59802ac6454SAndrew Thompson 		if (!(fflags & FREAD)) {
59902ac6454SAndrew Thompson 			error = EPERM;
60002ac6454SAndrew Thompson 			break;
60102ac6454SAndrew Thompson 		}
60202ac6454SAndrew Thompson 		ugd = addr;
60302ac6454SAndrew Thompson 		switch (ugd->ugd_report_type) {
60402ac6454SAndrew Thompson 		case UHID_INPUT_REPORT:
60502ac6454SAndrew Thompson 			size = sc->sc_isize;
60602ac6454SAndrew Thompson 			id = sc->sc_iid;
60702ac6454SAndrew Thompson 			break;
60802ac6454SAndrew Thompson 		case UHID_OUTPUT_REPORT:
60902ac6454SAndrew Thompson 			size = sc->sc_osize;
61002ac6454SAndrew Thompson 			id = sc->sc_oid;
61102ac6454SAndrew Thompson 			break;
61202ac6454SAndrew Thompson 		case UHID_FEATURE_REPORT:
61302ac6454SAndrew Thompson 			size = sc->sc_fsize;
61402ac6454SAndrew Thompson 			id = sc->sc_fid;
61502ac6454SAndrew Thompson 			break;
61602ac6454SAndrew Thompson 		default:
61702ac6454SAndrew Thompson 			return (EINVAL);
61802ac6454SAndrew Thompson 		}
6197778ab7eSAlexander Motin 		if (id != 0)
6207778ab7eSAlexander Motin 			copyin(ugd->ugd_data, &id, 1);
62102ac6454SAndrew Thompson 		error = uhid_get_report(sc, ugd->ugd_report_type, id,
6227778ab7eSAlexander Motin 		    NULL, ugd->ugd_data, imin(ugd->ugd_maxlen, size));
62302ac6454SAndrew Thompson 		break;
62402ac6454SAndrew Thompson 
62502ac6454SAndrew Thompson 	case USB_SET_REPORT:
62602ac6454SAndrew Thompson 		if (!(fflags & FWRITE)) {
62702ac6454SAndrew Thompson 			error = EPERM;
62802ac6454SAndrew Thompson 			break;
62902ac6454SAndrew Thompson 		}
63002ac6454SAndrew Thompson 		ugd = addr;
63102ac6454SAndrew Thompson 		switch (ugd->ugd_report_type) {
63202ac6454SAndrew Thompson 		case UHID_INPUT_REPORT:
63302ac6454SAndrew Thompson 			size = sc->sc_isize;
63402ac6454SAndrew Thompson 			id = sc->sc_iid;
63502ac6454SAndrew Thompson 			break;
63602ac6454SAndrew Thompson 		case UHID_OUTPUT_REPORT:
63702ac6454SAndrew Thompson 			size = sc->sc_osize;
63802ac6454SAndrew Thompson 			id = sc->sc_oid;
63902ac6454SAndrew Thompson 			break;
64002ac6454SAndrew Thompson 		case UHID_FEATURE_REPORT:
64102ac6454SAndrew Thompson 			size = sc->sc_fsize;
64202ac6454SAndrew Thompson 			id = sc->sc_fid;
64302ac6454SAndrew Thompson 			break;
64402ac6454SAndrew Thompson 		default:
64502ac6454SAndrew Thompson 			return (EINVAL);
64602ac6454SAndrew Thompson 		}
6477778ab7eSAlexander Motin 		if (id != 0)
6487778ab7eSAlexander Motin 			copyin(ugd->ugd_data, &id, 1);
64902ac6454SAndrew Thompson 		error = uhid_set_report(sc, ugd->ugd_report_type, id,
6507778ab7eSAlexander Motin 		    NULL, ugd->ugd_data, imin(ugd->ugd_maxlen, size));
65102ac6454SAndrew Thompson 		break;
65202ac6454SAndrew Thompson 
65302ac6454SAndrew Thompson 	case USB_GET_REPORT_ID:
65402ac6454SAndrew Thompson 		*(int *)addr = 0;	/* XXX: we only support reportid 0? */
65502ac6454SAndrew Thompson 		break;
65602ac6454SAndrew Thompson 
65702ac6454SAndrew Thompson 	default:
65802ac6454SAndrew Thompson 		error = EINVAL;
65902ac6454SAndrew Thompson 		break;
66002ac6454SAndrew Thompson 	}
66102ac6454SAndrew Thompson 	return (error);
66202ac6454SAndrew Thompson }
66302ac6454SAndrew Thompson 
664f1a16106SHans Petter Selasky static const STRUCT_USB_HOST_ID uhid_devs[] = {
665f1a16106SHans Petter Selasky 	/* generic HID class */
666f1a16106SHans Petter Selasky 	{USB_IFACE_CLASS(UICLASS_HID),},
667f1a16106SHans Petter Selasky 	/* the Xbox 360 gamepad doesn't use the HID class */
668f1a16106SHans Petter Selasky 	{USB_IFACE_CLASS(UICLASS_VENDOR),
669f1a16106SHans Petter Selasky 	 USB_IFACE_SUBCLASS(UISUBCLASS_XBOX360_CONTROLLER),
670f1a16106SHans Petter Selasky 	 USB_IFACE_PROTOCOL(UIPROTO_XBOX360_GAMEPAD),},
671f1a16106SHans Petter Selasky };
672f1a16106SHans Petter Selasky 
67302ac6454SAndrew Thompson static int
67402ac6454SAndrew Thompson uhid_probe(device_t dev)
67502ac6454SAndrew Thompson {
676760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
677f1a16106SHans Petter Selasky 	int error;
67802ac6454SAndrew Thompson 
67902ac6454SAndrew Thompson 	DPRINTFN(11, "\n");
68002ac6454SAndrew Thompson 
681f1a16106SHans Petter Selasky 	if (uaa->usb_mode != USB_MODE_HOST)
68202ac6454SAndrew Thompson 		return (ENXIO);
68302ac6454SAndrew Thompson 
684f1a16106SHans Petter Selasky 	error = usbd_lookup_id_by_uaa(uhid_devs, sizeof(uhid_devs), uaa);
685f1a16106SHans Petter Selasky 	if (error)
686f1a16106SHans Petter Selasky 		return (error);
68702ac6454SAndrew Thompson 
688f1a16106SHans Petter Selasky 	if (usb_test_quirk(uaa, UQ_HID_IGNORE))
68902ac6454SAndrew Thompson 		return (ENXIO);
690f1a16106SHans Petter Selasky 
69134e5f64bSHans Petter Selasky 	/*
69234e5f64bSHans Petter Selasky 	 * Don't attach to mouse and keyboard devices, hence then no
69334e5f64bSHans Petter Selasky 	 * "nomatch" event is generated and then ums and ukbd won't
69434e5f64bSHans Petter Selasky 	 * attach properly when loaded.
69534e5f64bSHans Petter Selasky 	 */
69634e5f64bSHans Petter Selasky 	if ((uaa->info.bInterfaceClass == UICLASS_HID) &&
69734e5f64bSHans Petter Selasky 	    (uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
69868ea9fd7SEitan Adler 	    (((uaa->info.bInterfaceProtocol == UIPROTO_BOOT_KEYBOARD) &&
69968ea9fd7SEitan Adler 	      !usb_test_quirk(uaa, UQ_KBD_IGNORE)) ||
70068ea9fd7SEitan Adler 	     ((uaa->info.bInterfaceProtocol == UIPROTO_MOUSE) &&
70168ea9fd7SEitan Adler 	      !usb_test_quirk(uaa, UQ_UMS_IGNORE))))
70234e5f64bSHans Petter Selasky 		return (ENXIO);
70334e5f64bSHans Petter Selasky 
704b201cde6SNathan Whitehorn 	return (BUS_PROBE_GENERIC);
70502ac6454SAndrew Thompson }
70602ac6454SAndrew Thompson 
70702ac6454SAndrew Thompson static int
70802ac6454SAndrew Thompson uhid_attach(device_t dev)
70902ac6454SAndrew Thompson {
710760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
71102ac6454SAndrew Thompson 	struct uhid_softc *sc = device_get_softc(dev);
71202ac6454SAndrew Thompson 	int unit = device_get_unit(dev);
71302ac6454SAndrew Thompson 	int error = 0;
71402ac6454SAndrew Thompson 
71502ac6454SAndrew Thompson 	DPRINTFN(10, "sc=%p\n", sc);
71602ac6454SAndrew Thompson 
717a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
71802ac6454SAndrew Thompson 
71902ac6454SAndrew Thompson 	mtx_init(&sc->sc_mtx, "uhid lock", NULL, MTX_DEF | MTX_RECURSE);
72002ac6454SAndrew Thompson 
72102ac6454SAndrew Thompson 	sc->sc_udev = uaa->device;
72202ac6454SAndrew Thompson 
72302ac6454SAndrew Thompson 	sc->sc_iface_no = uaa->info.bIfaceNum;
72402ac6454SAndrew Thompson 	sc->sc_iface_index = uaa->info.bIfaceIndex;
72502ac6454SAndrew Thompson 
726a593f6b8SAndrew Thompson 	error = usbd_transfer_setup(uaa->device,
72702ac6454SAndrew Thompson 	    &uaa->info.bIfaceIndex, sc->sc_xfer, uhid_config,
72802ac6454SAndrew Thompson 	    UHID_N_TRANSFER, sc, &sc->sc_mtx);
72902ac6454SAndrew Thompson 
73002ac6454SAndrew Thompson 	if (error) {
731a593f6b8SAndrew Thompson 		DPRINTF("error=%s\n", usbd_errstr(error));
73202ac6454SAndrew Thompson 		goto detach;
73302ac6454SAndrew Thompson 	}
73402ac6454SAndrew Thompson 	if (uaa->info.idVendor == USB_VENDOR_WACOM) {
73502ac6454SAndrew Thompson 
73602ac6454SAndrew Thompson 		/* the report descriptor for the Wacom Graphire is broken */
73702ac6454SAndrew Thompson 
73802ac6454SAndrew Thompson 		if (uaa->info.idProduct == USB_PRODUCT_WACOM_GRAPHIRE) {
73902ac6454SAndrew Thompson 
74002ac6454SAndrew Thompson 			sc->sc_repdesc_size = sizeof(uhid_graphire_report_descr);
741620d4f3cSDimitry Andric 			sc->sc_repdesc_ptr = __DECONST(void *, &uhid_graphire_report_descr);
74202ac6454SAndrew Thompson 			sc->sc_flags |= UHID_FLAG_STATIC_DESC;
74302ac6454SAndrew Thompson 
74402ac6454SAndrew Thompson 		} else if (uaa->info.idProduct == USB_PRODUCT_WACOM_GRAPHIRE3_4X5) {
74502ac6454SAndrew Thompson 
74602ac6454SAndrew Thompson 			static uint8_t reportbuf[] = {2, 2, 2};
74702ac6454SAndrew Thompson 
74802ac6454SAndrew Thompson 			/*
74902ac6454SAndrew Thompson 			 * The Graphire3 needs 0x0202 to be written to
75002ac6454SAndrew Thompson 			 * feature report ID 2 before it'll start
75102ac6454SAndrew Thompson 			 * returning digitizer data.
75202ac6454SAndrew Thompson 			 */
753a593f6b8SAndrew Thompson 			error = usbd_req_set_report(uaa->device, NULL,
754296ade60SAndrew Thompson 			    reportbuf, sizeof(reportbuf),
75502ac6454SAndrew Thompson 			    uaa->info.bIfaceIndex, UHID_FEATURE_REPORT, 2);
75602ac6454SAndrew Thompson 
75702ac6454SAndrew Thompson 			if (error) {
75802ac6454SAndrew Thompson 				DPRINTF("set report failed, error=%s (ignored)\n",
759a593f6b8SAndrew Thompson 				    usbd_errstr(error));
76002ac6454SAndrew Thompson 			}
76102ac6454SAndrew Thompson 			sc->sc_repdesc_size = sizeof(uhid_graphire3_4x5_report_descr);
762620d4f3cSDimitry Andric 			sc->sc_repdesc_ptr = __DECONST(void *, &uhid_graphire3_4x5_report_descr);
76302ac6454SAndrew Thompson 			sc->sc_flags |= UHID_FLAG_STATIC_DESC;
76402ac6454SAndrew Thompson 		}
76502ac6454SAndrew Thompson 	} else if ((uaa->info.bInterfaceClass == UICLASS_VENDOR) &&
76602ac6454SAndrew Thompson 	    (uaa->info.bInterfaceSubClass == UISUBCLASS_XBOX360_CONTROLLER) &&
76702ac6454SAndrew Thompson 	    (uaa->info.bInterfaceProtocol == UIPROTO_XBOX360_GAMEPAD)) {
768817a8cacSHans Petter Selasky 		static const uint8_t reportbuf[3] = {1, 3, 0};
769817a8cacSHans Petter Selasky 		/*
770817a8cacSHans Petter Selasky 		 * Turn off the four LEDs on the gamepad which
771817a8cacSHans Petter Selasky 		 * are blinking by default:
772817a8cacSHans Petter Selasky 		 */
773817a8cacSHans Petter Selasky 		error = usbd_req_set_report(uaa->device, NULL,
774817a8cacSHans Petter Selasky 		    __DECONST(void *, reportbuf), sizeof(reportbuf),
775817a8cacSHans Petter Selasky 		    uaa->info.bIfaceIndex, UHID_OUTPUT_REPORT, 0);
776817a8cacSHans Petter Selasky 		if (error) {
777817a8cacSHans Petter Selasky 			DPRINTF("set output report failed, error=%s (ignored)\n",
778817a8cacSHans Petter Selasky 			    usbd_errstr(error));
779817a8cacSHans Petter Selasky 		}
78002ac6454SAndrew Thompson 		/* the Xbox 360 gamepad has no report descriptor */
78102ac6454SAndrew Thompson 		sc->sc_repdesc_size = sizeof(uhid_xb360gp_report_descr);
782620d4f3cSDimitry Andric 		sc->sc_repdesc_ptr = __DECONST(void *, &uhid_xb360gp_report_descr);
78302ac6454SAndrew Thompson 		sc->sc_flags |= UHID_FLAG_STATIC_DESC;
78402ac6454SAndrew Thompson 	}
78502ac6454SAndrew Thompson 	if (sc->sc_repdesc_ptr == NULL) {
78602ac6454SAndrew Thompson 
787a593f6b8SAndrew Thompson 		error = usbd_req_get_hid_desc(uaa->device, NULL,
788296ade60SAndrew Thompson 		    &sc->sc_repdesc_ptr, &sc->sc_repdesc_size,
789296ade60SAndrew Thompson 		    M_USBDEV, uaa->info.bIfaceIndex);
79002ac6454SAndrew Thompson 
79102ac6454SAndrew Thompson 		if (error) {
79202ac6454SAndrew Thompson 			device_printf(dev, "no report descriptor\n");
79302ac6454SAndrew Thompson 			goto detach;
79402ac6454SAndrew Thompson 		}
79502ac6454SAndrew Thompson 	}
796a593f6b8SAndrew Thompson 	error = usbd_req_set_idle(uaa->device, NULL,
79702ac6454SAndrew Thompson 	    uaa->info.bIfaceIndex, 0, 0);
79802ac6454SAndrew Thompson 
79902ac6454SAndrew Thompson 	if (error) {
80002ac6454SAndrew Thompson 		DPRINTF("set idle failed, error=%s (ignored)\n",
801a593f6b8SAndrew Thompson 		    usbd_errstr(error));
80202ac6454SAndrew Thompson 	}
80302ac6454SAndrew Thompson 	sc->sc_isize = hid_report_size
80402ac6454SAndrew Thompson 	    (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_input, &sc->sc_iid);
80502ac6454SAndrew Thompson 
80602ac6454SAndrew Thompson 	sc->sc_osize = hid_report_size
80702ac6454SAndrew Thompson 	    (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_output, &sc->sc_oid);
80802ac6454SAndrew Thompson 
80902ac6454SAndrew Thompson 	sc->sc_fsize = hid_report_size
81002ac6454SAndrew Thompson 	    (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_feature, &sc->sc_fid);
81102ac6454SAndrew Thompson 
81202ac6454SAndrew Thompson 	if (sc->sc_isize > UHID_BSIZE) {
81302ac6454SAndrew Thompson 		DPRINTF("input size is too large, "
81402ac6454SAndrew Thompson 		    "%d bytes (truncating)\n",
81502ac6454SAndrew Thompson 		    sc->sc_isize);
81602ac6454SAndrew Thompson 		sc->sc_isize = UHID_BSIZE;
81702ac6454SAndrew Thompson 	}
81802ac6454SAndrew Thompson 	if (sc->sc_osize > UHID_BSIZE) {
81902ac6454SAndrew Thompson 		DPRINTF("output size is too large, "
82002ac6454SAndrew Thompson 		    "%d bytes (truncating)\n",
82102ac6454SAndrew Thompson 		    sc->sc_osize);
82202ac6454SAndrew Thompson 		sc->sc_osize = UHID_BSIZE;
82302ac6454SAndrew Thompson 	}
82402ac6454SAndrew Thompson 	if (sc->sc_fsize > UHID_BSIZE) {
82502ac6454SAndrew Thompson 		DPRINTF("feature size is too large, "
82602ac6454SAndrew Thompson 		    "%d bytes (truncating)\n",
82702ac6454SAndrew Thompson 		    sc->sc_fsize);
82802ac6454SAndrew Thompson 		sc->sc_fsize = UHID_BSIZE;
82902ac6454SAndrew Thompson 	}
83002ac6454SAndrew Thompson 
831a593f6b8SAndrew Thompson 	error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
83202ac6454SAndrew Thompson 	    &uhid_fifo_methods, &sc->sc_fifo,
8336d917491SHans Petter Selasky 	    unit, -1, uaa->info.bIfaceIndex,
834ee3e3ff5SAndrew Thompson 	    UID_ROOT, GID_OPERATOR, 0644);
83502ac6454SAndrew Thompson 	if (error) {
83602ac6454SAndrew Thompson 		goto detach;
83702ac6454SAndrew Thompson 	}
83802ac6454SAndrew Thompson 	return (0);			/* success */
83902ac6454SAndrew Thompson 
84002ac6454SAndrew Thompson detach:
84102ac6454SAndrew Thompson 	uhid_detach(dev);
84202ac6454SAndrew Thompson 	return (ENOMEM);
84302ac6454SAndrew Thompson }
84402ac6454SAndrew Thompson 
84502ac6454SAndrew Thompson static int
84602ac6454SAndrew Thompson uhid_detach(device_t dev)
84702ac6454SAndrew Thompson {
84802ac6454SAndrew Thompson 	struct uhid_softc *sc = device_get_softc(dev);
84902ac6454SAndrew Thompson 
850a593f6b8SAndrew Thompson 	usb_fifo_detach(&sc->sc_fifo);
85102ac6454SAndrew Thompson 
852a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, UHID_N_TRANSFER);
85302ac6454SAndrew Thompson 
85402ac6454SAndrew Thompson 	if (sc->sc_repdesc_ptr) {
85502ac6454SAndrew Thompson 		if (!(sc->sc_flags & UHID_FLAG_STATIC_DESC)) {
85602ac6454SAndrew Thompson 			free(sc->sc_repdesc_ptr, M_USBDEV);
85702ac6454SAndrew Thompson 		}
85802ac6454SAndrew Thompson 	}
85902ac6454SAndrew Thompson 	mtx_destroy(&sc->sc_mtx);
86002ac6454SAndrew Thompson 
86102ac6454SAndrew Thompson 	return (0);
86202ac6454SAndrew Thompson }
86302ac6454SAndrew Thompson 
86402ac6454SAndrew Thompson static devclass_t uhid_devclass;
86502ac6454SAndrew Thompson 
86602ac6454SAndrew Thompson static device_method_t uhid_methods[] = {
86702ac6454SAndrew Thompson 	DEVMETHOD(device_probe, uhid_probe),
86802ac6454SAndrew Thompson 	DEVMETHOD(device_attach, uhid_attach),
86902ac6454SAndrew Thompson 	DEVMETHOD(device_detach, uhid_detach),
87061bfd867SSofian Brabez 
87161bfd867SSofian Brabez 	DEVMETHOD_END
87202ac6454SAndrew Thompson };
87302ac6454SAndrew Thompson 
87402ac6454SAndrew Thompson static driver_t uhid_driver = {
87502ac6454SAndrew Thompson 	.name = "uhid",
87602ac6454SAndrew Thompson 	.methods = uhid_methods,
87702ac6454SAndrew Thompson 	.size = sizeof(struct uhid_softc),
87802ac6454SAndrew Thompson };
87902ac6454SAndrew Thompson 
8809aef556dSAndrew Thompson DRIVER_MODULE(uhid, uhub, uhid_driver, uhid_devclass, NULL, 0);
88102ac6454SAndrew Thompson MODULE_DEPEND(uhid, usb, 1, 1, 1);
882910cb8feSAndrew Thompson MODULE_VERSION(uhid, 1);
883f809f280SWarner Losh USB_PNP_HOST_INFO(uhid_devs);
884