xref: /freebsd/sys/dev/usb/input/uhid.c (revision 910cb8feb94f25e1ad5446f771445c2a3f97a845)
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 /*-
1102ac6454SAndrew Thompson  * Copyright (c) 1998 The NetBSD Foundation, Inc.
1202ac6454SAndrew Thompson  * All rights reserved.
1302ac6454SAndrew Thompson  *
1402ac6454SAndrew Thompson  * This code is derived from software contributed to The NetBSD Foundation
1502ac6454SAndrew Thompson  * by Lennart Augustsson (lennart@augustsson.net) at
1602ac6454SAndrew Thompson  * Carlstedt Research & Technology.
1702ac6454SAndrew Thompson  *
1802ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
1902ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
2002ac6454SAndrew Thompson  * are met:
2102ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
2202ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
2302ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
2402ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
2502ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
2602ac6454SAndrew Thompson  *
2702ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2802ac6454SAndrew Thompson  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2902ac6454SAndrew Thompson  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
3002ac6454SAndrew Thompson  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
3102ac6454SAndrew Thompson  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3202ac6454SAndrew Thompson  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3302ac6454SAndrew Thompson  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3402ac6454SAndrew Thompson  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3502ac6454SAndrew Thompson  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3602ac6454SAndrew Thompson  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3702ac6454SAndrew Thompson  * POSSIBILITY OF SUCH DAMAGE.
3802ac6454SAndrew Thompson  */
3902ac6454SAndrew Thompson 
4002ac6454SAndrew Thompson /*
4102ac6454SAndrew Thompson  * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
4202ac6454SAndrew Thompson  */
4302ac6454SAndrew Thompson 
44ed6d949aSAndrew Thompson #include <sys/stdint.h>
45ed6d949aSAndrew Thompson #include <sys/stddef.h>
46ed6d949aSAndrew Thompson #include <sys/param.h>
47ed6d949aSAndrew Thompson #include <sys/queue.h>
48ed6d949aSAndrew Thompson #include <sys/types.h>
49ed6d949aSAndrew Thompson #include <sys/systm.h>
50ed6d949aSAndrew Thompson #include <sys/kernel.h>
51ed6d949aSAndrew Thompson #include <sys/bus.h>
52ed6d949aSAndrew Thompson #include <sys/linker_set.h>
53ed6d949aSAndrew Thompson #include <sys/module.h>
54ed6d949aSAndrew Thompson #include <sys/lock.h>
55ed6d949aSAndrew Thompson #include <sys/mutex.h>
56ed6d949aSAndrew Thompson #include <sys/condvar.h>
57ed6d949aSAndrew Thompson #include <sys/sysctl.h>
58ed6d949aSAndrew Thompson #include <sys/sx.h>
59ed6d949aSAndrew Thompson #include <sys/unistd.h>
60ed6d949aSAndrew Thompson #include <sys/callout.h>
61ed6d949aSAndrew Thompson #include <sys/malloc.h>
62ed6d949aSAndrew Thompson #include <sys/priv.h>
63ed6d949aSAndrew Thompson #include <sys/conf.h>
64ed6d949aSAndrew Thompson #include <sys/fcntl.h>
65ed6d949aSAndrew Thompson 
6602ac6454SAndrew Thompson #include "usbdevs.h"
6702ac6454SAndrew Thompson #include <dev/usb/usb.h>
68ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
69ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h>
7002ac6454SAndrew Thompson #include <dev/usb/usbhid.h>
7102ac6454SAndrew Thompson #include <dev/usb/usb_ioctl.h>
7202ac6454SAndrew Thompson 
7302ac6454SAndrew Thompson #define	USB_DEBUG_VAR uhid_debug
7402ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
7502ac6454SAndrew Thompson 
7602ac6454SAndrew Thompson #include <dev/usb/input/usb_rdesc.h>
7702ac6454SAndrew Thompson #include <dev/usb/quirk/usb_quirk.h>
7802ac6454SAndrew Thompson 
79b850ecc1SAndrew Thompson #ifdef USB_DEBUG
8002ac6454SAndrew Thompson static int uhid_debug = 0;
8102ac6454SAndrew Thompson 
829360ae40SAndrew Thompson SYSCTL_NODE(_hw_usb, OID_AUTO, uhid, CTLFLAG_RW, 0, "USB uhid");
839360ae40SAndrew Thompson SYSCTL_INT(_hw_usb_uhid, OID_AUTO, debug, CTLFLAG_RW,
8402ac6454SAndrew Thompson     &uhid_debug, 0, "Debug level");
8502ac6454SAndrew Thompson #endif
8602ac6454SAndrew Thompson 
8702ac6454SAndrew Thompson #define	UHID_BSIZE	1024		/* bytes, buffer size */
8802ac6454SAndrew Thompson #define	UHID_FRAME_NUM 	  50		/* bytes, frame number */
8902ac6454SAndrew Thompson 
9002ac6454SAndrew Thompson enum {
9102ac6454SAndrew Thompson 	UHID_INTR_DT_RD,
9202ac6454SAndrew Thompson 	UHID_CTRL_DT_WR,
9302ac6454SAndrew Thompson 	UHID_CTRL_DT_RD,
9402ac6454SAndrew Thompson 	UHID_N_TRANSFER,
9502ac6454SAndrew Thompson };
9602ac6454SAndrew Thompson 
9702ac6454SAndrew Thompson struct uhid_softc {
98760bc48eSAndrew Thompson 	struct usb_fifo_sc sc_fifo;
9902ac6454SAndrew Thompson 	struct mtx sc_mtx;
10002ac6454SAndrew Thompson 
101760bc48eSAndrew Thompson 	struct usb_xfer *sc_xfer[UHID_N_TRANSFER];
102760bc48eSAndrew Thompson 	struct usb_device *sc_udev;
10302ac6454SAndrew Thompson 	void   *sc_repdesc_ptr;
10402ac6454SAndrew Thompson 
10502ac6454SAndrew Thompson 	uint32_t sc_isize;
10602ac6454SAndrew Thompson 	uint32_t sc_osize;
10702ac6454SAndrew Thompson 	uint32_t sc_fsize;
10802ac6454SAndrew Thompson 
10902ac6454SAndrew Thompson 	uint16_t sc_repdesc_size;
11002ac6454SAndrew Thompson 
11102ac6454SAndrew Thompson 	uint8_t	sc_iface_no;
11202ac6454SAndrew Thompson 	uint8_t	sc_iface_index;
11302ac6454SAndrew Thompson 	uint8_t	sc_iid;
11402ac6454SAndrew Thompson 	uint8_t	sc_oid;
11502ac6454SAndrew Thompson 	uint8_t	sc_fid;
11602ac6454SAndrew Thompson 	uint8_t	sc_flags;
11702ac6454SAndrew Thompson #define	UHID_FLAG_IMMED        0x01	/* set if read should be immediate */
11802ac6454SAndrew Thompson #define	UHID_FLAG_STATIC_DESC  0x04	/* set if report descriptors are
11902ac6454SAndrew Thompson 					 * static */
12002ac6454SAndrew Thompson };
12102ac6454SAndrew Thompson 
12202ac6454SAndrew Thompson static const uint8_t uhid_xb360gp_report_descr[] = {UHID_XB360GP_REPORT_DESCR()};
12302ac6454SAndrew Thompson static const uint8_t uhid_graphire_report_descr[] = {UHID_GRAPHIRE_REPORT_DESCR()};
12402ac6454SAndrew Thompson static const uint8_t uhid_graphire3_4x5_report_descr[] = {UHID_GRAPHIRE3_4X5_REPORT_DESCR()};
12502ac6454SAndrew Thompson 
12602ac6454SAndrew Thompson /* prototypes */
12702ac6454SAndrew Thompson 
12802ac6454SAndrew Thompson static device_probe_t uhid_probe;
12902ac6454SAndrew Thompson static device_attach_t uhid_attach;
13002ac6454SAndrew Thompson static device_detach_t uhid_detach;
13102ac6454SAndrew Thompson 
132e0a69b51SAndrew Thompson static usb_callback_t uhid_intr_callback;
133e0a69b51SAndrew Thompson static usb_callback_t uhid_write_callback;
134e0a69b51SAndrew Thompson static usb_callback_t uhid_read_callback;
13502ac6454SAndrew Thompson 
136e0a69b51SAndrew Thompson static usb_fifo_cmd_t uhid_start_read;
137e0a69b51SAndrew Thompson static usb_fifo_cmd_t uhid_stop_read;
138e0a69b51SAndrew Thompson static usb_fifo_cmd_t uhid_start_write;
139e0a69b51SAndrew Thompson static usb_fifo_cmd_t uhid_stop_write;
140e0a69b51SAndrew Thompson static usb_fifo_open_t uhid_open;
141e0a69b51SAndrew Thompson static usb_fifo_close_t uhid_close;
142e0a69b51SAndrew Thompson static usb_fifo_ioctl_t uhid_ioctl;
14302ac6454SAndrew Thompson 
144760bc48eSAndrew Thompson static struct usb_fifo_methods uhid_fifo_methods = {
14502ac6454SAndrew Thompson 	.f_open = &uhid_open,
14602ac6454SAndrew Thompson 	.f_close = &uhid_close,
14702ac6454SAndrew Thompson 	.f_ioctl = &uhid_ioctl,
14802ac6454SAndrew Thompson 	.f_start_read = &uhid_start_read,
14902ac6454SAndrew Thompson 	.f_stop_read = &uhid_stop_read,
15002ac6454SAndrew Thompson 	.f_start_write = &uhid_start_write,
15102ac6454SAndrew Thompson 	.f_stop_write = &uhid_stop_write,
15202ac6454SAndrew Thompson 	.basename[0] = "uhid",
15302ac6454SAndrew Thompson };
15402ac6454SAndrew Thompson 
15502ac6454SAndrew Thompson static void
156ed6d949aSAndrew Thompson uhid_intr_callback(struct usb_xfer *xfer, usb_error_t error)
15702ac6454SAndrew Thompson {
158ed6d949aSAndrew Thompson 	struct uhid_softc *sc = usbd_xfer_softc(xfer);
159ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
160ed6d949aSAndrew Thompson 	int actlen;
161ed6d949aSAndrew Thompson 
162ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
16302ac6454SAndrew Thompson 
16402ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
16502ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
16602ac6454SAndrew Thompson 		DPRINTF("transferred!\n");
16702ac6454SAndrew Thompson 
168ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
1690ca80d71SAndrew Thompson 
1700ca80d71SAndrew Thompson 		/*
1710ca80d71SAndrew Thompson 		 * If the ID byte is non zero we allow descriptors
1720ca80d71SAndrew Thompson 		 * having multiple sizes:
1730ca80d71SAndrew Thompson 		 */
1740ca80d71SAndrew Thompson 		if ((actlen >= sc->sc_isize) ||
1750ca80d71SAndrew Thompson 		    ((actlen > 0) && (sc->sc_iid != 0))) {
1760ca80d71SAndrew Thompson 			/* limit report length to the maximum */
1770ca80d71SAndrew Thompson 			if (actlen > sc->sc_isize)
1780ca80d71SAndrew Thompson 				actlen = sc->sc_isize;
179ed6d949aSAndrew Thompson 			usb_fifo_put_data(sc->sc_fifo.fp[USB_FIFO_RX], pc,
1800ca80d71SAndrew Thompson 			    0, actlen, 1);
18102ac6454SAndrew Thompson 		} else {
18202ac6454SAndrew Thompson 			/* ignore it */
1830ca80d71SAndrew Thompson 			DPRINTF("ignored transfer, %d bytes\n", actlen);
18402ac6454SAndrew Thompson 		}
18502ac6454SAndrew Thompson 
18602ac6454SAndrew Thompson 	case USB_ST_SETUP:
18702ac6454SAndrew Thompson re_submit:
188a593f6b8SAndrew Thompson 		if (usb_fifo_put_bytes_max(
18902ac6454SAndrew Thompson 		    sc->sc_fifo.fp[USB_FIFO_RX]) != 0) {
190ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, sc->sc_isize);
191a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
19202ac6454SAndrew Thompson 		}
19302ac6454SAndrew Thompson 		return;
19402ac6454SAndrew Thompson 
19502ac6454SAndrew Thompson 	default:			/* Error */
196ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
19702ac6454SAndrew Thompson 			/* try to clear stall first */
198ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
19902ac6454SAndrew Thompson 			goto re_submit;
20002ac6454SAndrew Thompson 		}
20102ac6454SAndrew Thompson 		return;
20202ac6454SAndrew Thompson 	}
20302ac6454SAndrew Thompson }
20402ac6454SAndrew Thompson 
20502ac6454SAndrew Thompson static void
206760bc48eSAndrew Thompson uhid_fill_set_report(struct usb_device_request *req, uint8_t iface_no,
20702ac6454SAndrew Thompson     uint8_t type, uint8_t id, uint16_t size)
20802ac6454SAndrew Thompson {
20902ac6454SAndrew Thompson 	req->bmRequestType = UT_WRITE_CLASS_INTERFACE;
21002ac6454SAndrew Thompson 	req->bRequest = UR_SET_REPORT;
21102ac6454SAndrew Thompson 	USETW2(req->wValue, type, id);
21202ac6454SAndrew Thompson 	req->wIndex[0] = iface_no;
21302ac6454SAndrew Thompson 	req->wIndex[1] = 0;
21402ac6454SAndrew Thompson 	USETW(req->wLength, size);
21502ac6454SAndrew Thompson }
21602ac6454SAndrew Thompson 
21702ac6454SAndrew Thompson static void
218760bc48eSAndrew Thompson uhid_fill_get_report(struct usb_device_request *req, uint8_t iface_no,
21902ac6454SAndrew Thompson     uint8_t type, uint8_t id, uint16_t size)
22002ac6454SAndrew Thompson {
22102ac6454SAndrew Thompson 	req->bmRequestType = UT_READ_CLASS_INTERFACE;
22202ac6454SAndrew Thompson 	req->bRequest = UR_GET_REPORT;
22302ac6454SAndrew Thompson 	USETW2(req->wValue, type, id);
22402ac6454SAndrew Thompson 	req->wIndex[0] = iface_no;
22502ac6454SAndrew Thompson 	req->wIndex[1] = 0;
22602ac6454SAndrew Thompson 	USETW(req->wLength, size);
22702ac6454SAndrew Thompson }
22802ac6454SAndrew Thompson 
22902ac6454SAndrew Thompson static void
230ed6d949aSAndrew Thompson uhid_write_callback(struct usb_xfer *xfer, usb_error_t error)
23102ac6454SAndrew Thompson {
232ed6d949aSAndrew Thompson 	struct uhid_softc *sc = usbd_xfer_softc(xfer);
233760bc48eSAndrew Thompson 	struct usb_device_request req;
234ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
23502ac6454SAndrew Thompson 	uint32_t size = sc->sc_osize;
23602ac6454SAndrew Thompson 	uint32_t actlen;
23702ac6454SAndrew Thompson 	uint8_t id;
23802ac6454SAndrew Thompson 
23902ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
24002ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
24102ac6454SAndrew Thompson 	case USB_ST_SETUP:
24202ac6454SAndrew Thompson 		/* try to extract the ID byte */
24302ac6454SAndrew Thompson 		if (sc->sc_oid) {
244ed6d949aSAndrew Thompson 			pc = usbd_xfer_get_frame(xfer, 0);
245ed6d949aSAndrew Thompson 			if (usb_fifo_get_data(sc->sc_fifo.fp[USB_FIFO_TX], pc,
24602ac6454SAndrew Thompson 			    0, 1, &actlen, 0)) {
24702ac6454SAndrew Thompson 				if (actlen != 1) {
24802ac6454SAndrew Thompson 					goto tr_error;
24902ac6454SAndrew Thompson 				}
250ed6d949aSAndrew Thompson 				usbd_copy_out(pc, 0, &id, 1);
25102ac6454SAndrew Thompson 
25202ac6454SAndrew Thompson 			} else {
25302ac6454SAndrew Thompson 				return;
25402ac6454SAndrew Thompson 			}
25502ac6454SAndrew Thompson 			if (size) {
25602ac6454SAndrew Thompson 				size--;
25702ac6454SAndrew Thompson 			}
25802ac6454SAndrew Thompson 		} else {
25902ac6454SAndrew Thompson 			id = 0;
26002ac6454SAndrew Thompson 		}
26102ac6454SAndrew Thompson 
262ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 1);
263ed6d949aSAndrew Thompson 		if (usb_fifo_get_data(sc->sc_fifo.fp[USB_FIFO_TX], pc,
26402ac6454SAndrew Thompson 		    0, UHID_BSIZE, &actlen, 1)) {
26502ac6454SAndrew Thompson 			if (actlen != size) {
26602ac6454SAndrew Thompson 				goto tr_error;
26702ac6454SAndrew Thompson 			}
26802ac6454SAndrew Thompson 			uhid_fill_set_report
26902ac6454SAndrew Thompson 			    (&req, sc->sc_iface_no,
27002ac6454SAndrew Thompson 			    UHID_OUTPUT_REPORT, id, size);
27102ac6454SAndrew Thompson 
272ed6d949aSAndrew Thompson 			pc = usbd_xfer_get_frame(xfer, 0);
273ed6d949aSAndrew Thompson 			usbd_copy_in(pc, 0, &req, sizeof(req));
27402ac6454SAndrew Thompson 
275ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
276ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 1, size);
277ed6d949aSAndrew Thompson 			usbd_xfer_set_frames(xfer, size ? 2 : 1);
278a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
27902ac6454SAndrew Thompson 		}
28002ac6454SAndrew Thompson 		return;
28102ac6454SAndrew Thompson 
28202ac6454SAndrew Thompson 	default:
28302ac6454SAndrew Thompson tr_error:
28402ac6454SAndrew Thompson 		/* bomb out */
285a593f6b8SAndrew Thompson 		usb_fifo_get_data_error(sc->sc_fifo.fp[USB_FIFO_TX]);
28602ac6454SAndrew Thompson 		return;
28702ac6454SAndrew Thompson 	}
28802ac6454SAndrew Thompson }
28902ac6454SAndrew Thompson 
29002ac6454SAndrew Thompson static void
291ed6d949aSAndrew Thompson uhid_read_callback(struct usb_xfer *xfer, usb_error_t error)
29202ac6454SAndrew Thompson {
293ed6d949aSAndrew Thompson 	struct uhid_softc *sc = usbd_xfer_softc(xfer);
294760bc48eSAndrew Thompson 	struct usb_device_request req;
295ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
296ed6d949aSAndrew Thompson 
297ed6d949aSAndrew Thompson 	pc = usbd_xfer_get_frame(xfer, 0);
29802ac6454SAndrew Thompson 
29902ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
30002ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
301ed6d949aSAndrew Thompson 		usb_fifo_put_data(sc->sc_fifo.fp[USB_FIFO_RX], pc, sizeof(req),
302ed6d949aSAndrew Thompson 		    sc->sc_isize, 1);
30302ac6454SAndrew Thompson 		return;
30402ac6454SAndrew Thompson 
30502ac6454SAndrew Thompson 	case USB_ST_SETUP:
30602ac6454SAndrew Thompson 
307a593f6b8SAndrew Thompson 		if (usb_fifo_put_bytes_max(sc->sc_fifo.fp[USB_FIFO_RX]) > 0) {
30802ac6454SAndrew Thompson 
30902ac6454SAndrew Thompson 			uhid_fill_get_report
31002ac6454SAndrew Thompson 			    (&req, sc->sc_iface_no, UHID_INPUT_REPORT,
31102ac6454SAndrew Thompson 			    sc->sc_iid, sc->sc_isize);
31202ac6454SAndrew Thompson 
313ed6d949aSAndrew Thompson 			usbd_copy_in(pc, 0, &req, sizeof(req));
31402ac6454SAndrew Thompson 
315ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
316ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 1, sc->sc_isize);
317ed6d949aSAndrew Thompson 			usbd_xfer_set_frames(xfer, sc->sc_isize ? 2 : 1);
318a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
31902ac6454SAndrew Thompson 		}
32002ac6454SAndrew Thompson 		return;
32102ac6454SAndrew Thompson 
32202ac6454SAndrew Thompson 	default:			/* Error */
32302ac6454SAndrew Thompson 		/* bomb out */
324a593f6b8SAndrew Thompson 		usb_fifo_put_data_error(sc->sc_fifo.fp[USB_FIFO_RX]);
32502ac6454SAndrew Thompson 		return;
32602ac6454SAndrew Thompson 	}
32702ac6454SAndrew Thompson }
32802ac6454SAndrew Thompson 
329760bc48eSAndrew Thompson static const struct usb_config uhid_config[UHID_N_TRANSFER] = {
33002ac6454SAndrew Thompson 
33102ac6454SAndrew Thompson 	[UHID_INTR_DT_RD] = {
33202ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
33302ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
33402ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
3354eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
3364eae601eSAndrew Thompson 		.bufsize = UHID_BSIZE,
3374eae601eSAndrew Thompson 		.callback = &uhid_intr_callback,
33802ac6454SAndrew Thompson 	},
33902ac6454SAndrew Thompson 
34002ac6454SAndrew Thompson 	[UHID_CTRL_DT_WR] = {
34102ac6454SAndrew Thompson 		.type = UE_CONTROL,
34202ac6454SAndrew Thompson 		.endpoint = 0x00,	/* Control pipe */
34302ac6454SAndrew Thompson 		.direction = UE_DIR_ANY,
344760bc48eSAndrew Thompson 		.bufsize = sizeof(struct usb_device_request) + UHID_BSIZE,
3454eae601eSAndrew Thompson 		.callback = &uhid_write_callback,
3464eae601eSAndrew Thompson 		.timeout = 1000,	/* 1 second */
34702ac6454SAndrew Thompson 	},
34802ac6454SAndrew Thompson 
34902ac6454SAndrew Thompson 	[UHID_CTRL_DT_RD] = {
35002ac6454SAndrew Thompson 		.type = UE_CONTROL,
35102ac6454SAndrew Thompson 		.endpoint = 0x00,	/* Control pipe */
35202ac6454SAndrew Thompson 		.direction = UE_DIR_ANY,
353760bc48eSAndrew Thompson 		.bufsize = sizeof(struct usb_device_request) + UHID_BSIZE,
3544eae601eSAndrew Thompson 		.callback = &uhid_read_callback,
3554eae601eSAndrew Thompson 		.timeout = 1000,	/* 1 second */
35602ac6454SAndrew Thompson 	},
35702ac6454SAndrew Thompson };
35802ac6454SAndrew Thompson 
35902ac6454SAndrew Thompson static void
360760bc48eSAndrew Thompson uhid_start_read(struct usb_fifo *fifo)
36102ac6454SAndrew Thompson {
362ed6d949aSAndrew Thompson 	struct uhid_softc *sc = usb_fifo_softc(fifo);
36302ac6454SAndrew Thompson 
36402ac6454SAndrew Thompson 	if (sc->sc_flags & UHID_FLAG_IMMED) {
365a593f6b8SAndrew Thompson 		usbd_transfer_start(sc->sc_xfer[UHID_CTRL_DT_RD]);
36602ac6454SAndrew Thompson 	} else {
367a593f6b8SAndrew Thompson 		usbd_transfer_start(sc->sc_xfer[UHID_INTR_DT_RD]);
36802ac6454SAndrew Thompson 	}
36902ac6454SAndrew Thompson }
37002ac6454SAndrew Thompson 
37102ac6454SAndrew Thompson static void
372760bc48eSAndrew Thompson uhid_stop_read(struct usb_fifo *fifo)
37302ac6454SAndrew Thompson {
374ed6d949aSAndrew Thompson 	struct uhid_softc *sc = usb_fifo_softc(fifo);
37502ac6454SAndrew Thompson 
376a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UHID_CTRL_DT_RD]);
377a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UHID_INTR_DT_RD]);
37802ac6454SAndrew Thompson }
37902ac6454SAndrew Thompson 
38002ac6454SAndrew Thompson static void
381760bc48eSAndrew Thompson uhid_start_write(struct usb_fifo *fifo)
38202ac6454SAndrew Thompson {
383ed6d949aSAndrew Thompson 	struct uhid_softc *sc = usb_fifo_softc(fifo);
38402ac6454SAndrew Thompson 
385a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UHID_CTRL_DT_WR]);
38602ac6454SAndrew Thompson }
38702ac6454SAndrew Thompson 
38802ac6454SAndrew Thompson static void
389760bc48eSAndrew Thompson uhid_stop_write(struct usb_fifo *fifo)
39002ac6454SAndrew Thompson {
391ed6d949aSAndrew Thompson 	struct uhid_softc *sc = usb_fifo_softc(fifo);
39202ac6454SAndrew Thompson 
393a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UHID_CTRL_DT_WR]);
39402ac6454SAndrew Thompson }
39502ac6454SAndrew Thompson 
39602ac6454SAndrew Thompson static int
39702ac6454SAndrew Thompson uhid_get_report(struct uhid_softc *sc, uint8_t type,
39802ac6454SAndrew Thompson     uint8_t id, void *kern_data, void *user_data,
39902ac6454SAndrew Thompson     uint16_t len)
40002ac6454SAndrew Thompson {
40102ac6454SAndrew Thompson 	int err;
40202ac6454SAndrew Thompson 	uint8_t free_data = 0;
40302ac6454SAndrew Thompson 
40402ac6454SAndrew Thompson 	if (kern_data == NULL) {
40502ac6454SAndrew Thompson 		kern_data = malloc(len, M_USBDEV, M_WAITOK);
40602ac6454SAndrew Thompson 		if (kern_data == NULL) {
40702ac6454SAndrew Thompson 			err = ENOMEM;
40802ac6454SAndrew Thompson 			goto done;
40902ac6454SAndrew Thompson 		}
41002ac6454SAndrew Thompson 		free_data = 1;
41102ac6454SAndrew Thompson 	}
412a593f6b8SAndrew Thompson 	err = usbd_req_get_report(sc->sc_udev, NULL, kern_data,
41302ac6454SAndrew Thompson 	    len, sc->sc_iface_index, type, id);
41402ac6454SAndrew Thompson 	if (err) {
41502ac6454SAndrew Thompson 		err = ENXIO;
41602ac6454SAndrew Thompson 		goto done;
41702ac6454SAndrew Thompson 	}
41802ac6454SAndrew Thompson 	if (user_data) {
41902ac6454SAndrew Thompson 		/* dummy buffer */
42002ac6454SAndrew Thompson 		err = copyout(kern_data, user_data, len);
42102ac6454SAndrew Thompson 		if (err) {
42202ac6454SAndrew Thompson 			goto done;
42302ac6454SAndrew Thompson 		}
42402ac6454SAndrew Thompson 	}
42502ac6454SAndrew Thompson done:
42602ac6454SAndrew Thompson 	if (free_data) {
42702ac6454SAndrew Thompson 		free(kern_data, M_USBDEV);
42802ac6454SAndrew Thompson 	}
42902ac6454SAndrew Thompson 	return (err);
43002ac6454SAndrew Thompson }
43102ac6454SAndrew Thompson 
43202ac6454SAndrew Thompson static int
43302ac6454SAndrew Thompson uhid_set_report(struct uhid_softc *sc, uint8_t type,
43402ac6454SAndrew Thompson     uint8_t id, void *kern_data, void *user_data,
43502ac6454SAndrew Thompson     uint16_t len)
43602ac6454SAndrew Thompson {
43702ac6454SAndrew Thompson 	int err;
43802ac6454SAndrew Thompson 	uint8_t free_data = 0;
43902ac6454SAndrew Thompson 
44002ac6454SAndrew Thompson 	if (kern_data == NULL) {
44102ac6454SAndrew Thompson 		kern_data = malloc(len, M_USBDEV, M_WAITOK);
44202ac6454SAndrew Thompson 		if (kern_data == NULL) {
44302ac6454SAndrew Thompson 			err = ENOMEM;
44402ac6454SAndrew Thompson 			goto done;
44502ac6454SAndrew Thompson 		}
44602ac6454SAndrew Thompson 		free_data = 1;
44702ac6454SAndrew Thompson 		err = copyin(user_data, kern_data, len);
44802ac6454SAndrew Thompson 		if (err) {
44902ac6454SAndrew Thompson 			goto done;
45002ac6454SAndrew Thompson 		}
45102ac6454SAndrew Thompson 	}
452a593f6b8SAndrew Thompson 	err = usbd_req_set_report(sc->sc_udev, NULL, kern_data,
45302ac6454SAndrew Thompson 	    len, sc->sc_iface_index, type, id);
45402ac6454SAndrew Thompson 	if (err) {
45502ac6454SAndrew Thompson 		err = ENXIO;
45602ac6454SAndrew Thompson 		goto done;
45702ac6454SAndrew Thompson 	}
45802ac6454SAndrew Thompson done:
45902ac6454SAndrew Thompson 	if (free_data) {
46002ac6454SAndrew Thompson 		free(kern_data, M_USBDEV);
46102ac6454SAndrew Thompson 	}
46202ac6454SAndrew Thompson 	return (err);
46302ac6454SAndrew Thompson }
46402ac6454SAndrew Thompson 
46502ac6454SAndrew Thompson static int
466760bc48eSAndrew Thompson uhid_open(struct usb_fifo *fifo, int fflags)
46702ac6454SAndrew Thompson {
468ed6d949aSAndrew Thompson 	struct uhid_softc *sc = usb_fifo_softc(fifo);
46902ac6454SAndrew Thompson 
47002ac6454SAndrew Thompson 	/*
47102ac6454SAndrew Thompson 	 * The buffers are one byte larger than maximum so that one
47202ac6454SAndrew Thompson 	 * can detect too large read/writes and short transfers:
47302ac6454SAndrew Thompson 	 */
47402ac6454SAndrew Thompson 	if (fflags & FREAD) {
47502ac6454SAndrew Thompson 		/* reset flags */
47602ac6454SAndrew Thompson 		sc->sc_flags &= ~UHID_FLAG_IMMED;
47702ac6454SAndrew Thompson 
478a593f6b8SAndrew Thompson 		if (usb_fifo_alloc_buffer(fifo,
47902ac6454SAndrew Thompson 		    sc->sc_isize + 1, UHID_FRAME_NUM)) {
48002ac6454SAndrew Thompson 			return (ENOMEM);
48102ac6454SAndrew Thompson 		}
48202ac6454SAndrew Thompson 	}
48302ac6454SAndrew Thompson 	if (fflags & FWRITE) {
484a593f6b8SAndrew Thompson 		if (usb_fifo_alloc_buffer(fifo,
48502ac6454SAndrew Thompson 		    sc->sc_osize + 1, UHID_FRAME_NUM)) {
48602ac6454SAndrew Thompson 			return (ENOMEM);
48702ac6454SAndrew Thompson 		}
48802ac6454SAndrew Thompson 	}
48902ac6454SAndrew Thompson 	return (0);
49002ac6454SAndrew Thompson }
49102ac6454SAndrew Thompson 
49202ac6454SAndrew Thompson static void
493760bc48eSAndrew Thompson uhid_close(struct usb_fifo *fifo, int fflags)
49402ac6454SAndrew Thompson {
49502ac6454SAndrew Thompson 	if (fflags & (FREAD | FWRITE)) {
496a593f6b8SAndrew Thompson 		usb_fifo_free_buffer(fifo);
49702ac6454SAndrew Thompson 	}
49802ac6454SAndrew Thompson }
49902ac6454SAndrew Thompson 
50002ac6454SAndrew Thompson static int
501760bc48eSAndrew Thompson uhid_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr,
502ee3e3ff5SAndrew Thompson     int fflags)
50302ac6454SAndrew Thompson {
504ed6d949aSAndrew Thompson 	struct uhid_softc *sc = usb_fifo_softc(fifo);
505760bc48eSAndrew Thompson 	struct usb_gen_descriptor *ugd;
50602ac6454SAndrew Thompson 	uint32_t size;
50702ac6454SAndrew Thompson 	int error = 0;
50802ac6454SAndrew Thompson 	uint8_t id;
50902ac6454SAndrew Thompson 
51002ac6454SAndrew Thompson 	switch (cmd) {
51102ac6454SAndrew Thompson 	case USB_GET_REPORT_DESC:
51202ac6454SAndrew Thompson 		ugd = addr;
51302ac6454SAndrew Thompson 		if (sc->sc_repdesc_size > ugd->ugd_maxlen) {
51402ac6454SAndrew Thompson 			size = ugd->ugd_maxlen;
51502ac6454SAndrew Thompson 		} else {
51602ac6454SAndrew Thompson 			size = sc->sc_repdesc_size;
51702ac6454SAndrew Thompson 		}
51802ac6454SAndrew Thompson 		ugd->ugd_actlen = size;
51902ac6454SAndrew Thompson 		if (ugd->ugd_data == NULL)
52002ac6454SAndrew Thompson 			break;		/* descriptor length only */
52102ac6454SAndrew Thompson 		error = copyout(sc->sc_repdesc_ptr, ugd->ugd_data, size);
52202ac6454SAndrew Thompson 		break;
52302ac6454SAndrew Thompson 
52402ac6454SAndrew Thompson 	case USB_SET_IMMED:
52502ac6454SAndrew Thompson 		if (!(fflags & FREAD)) {
52602ac6454SAndrew Thompson 			error = EPERM;
52702ac6454SAndrew Thompson 			break;
52802ac6454SAndrew Thompson 		}
52902ac6454SAndrew Thompson 		if (*(int *)addr) {
53002ac6454SAndrew Thompson 
53102ac6454SAndrew Thompson 			/* do a test read */
53202ac6454SAndrew Thompson 
53302ac6454SAndrew Thompson 			error = uhid_get_report(sc, UHID_INPUT_REPORT,
53402ac6454SAndrew Thompson 			    sc->sc_iid, NULL, NULL, sc->sc_isize);
53502ac6454SAndrew Thompson 			if (error) {
53602ac6454SAndrew Thompson 				break;
53702ac6454SAndrew Thompson 			}
53802ac6454SAndrew Thompson 			mtx_lock(&sc->sc_mtx);
53902ac6454SAndrew Thompson 			sc->sc_flags |= UHID_FLAG_IMMED;
54002ac6454SAndrew Thompson 			mtx_unlock(&sc->sc_mtx);
54102ac6454SAndrew Thompson 		} else {
54202ac6454SAndrew Thompson 			mtx_lock(&sc->sc_mtx);
54302ac6454SAndrew Thompson 			sc->sc_flags &= ~UHID_FLAG_IMMED;
54402ac6454SAndrew Thompson 			mtx_unlock(&sc->sc_mtx);
54502ac6454SAndrew Thompson 		}
54602ac6454SAndrew Thompson 		break;
54702ac6454SAndrew Thompson 
54802ac6454SAndrew Thompson 	case USB_GET_REPORT:
54902ac6454SAndrew Thompson 		if (!(fflags & FREAD)) {
55002ac6454SAndrew Thompson 			error = EPERM;
55102ac6454SAndrew Thompson 			break;
55202ac6454SAndrew Thompson 		}
55302ac6454SAndrew Thompson 		ugd = addr;
55402ac6454SAndrew Thompson 		switch (ugd->ugd_report_type) {
55502ac6454SAndrew Thompson 		case UHID_INPUT_REPORT:
55602ac6454SAndrew Thompson 			size = sc->sc_isize;
55702ac6454SAndrew Thompson 			id = sc->sc_iid;
55802ac6454SAndrew Thompson 			break;
55902ac6454SAndrew Thompson 		case UHID_OUTPUT_REPORT:
56002ac6454SAndrew Thompson 			size = sc->sc_osize;
56102ac6454SAndrew Thompson 			id = sc->sc_oid;
56202ac6454SAndrew Thompson 			break;
56302ac6454SAndrew Thompson 		case UHID_FEATURE_REPORT:
56402ac6454SAndrew Thompson 			size = sc->sc_fsize;
56502ac6454SAndrew Thompson 			id = sc->sc_fid;
56602ac6454SAndrew Thompson 			break;
56702ac6454SAndrew Thompson 		default:
56802ac6454SAndrew Thompson 			return (EINVAL);
56902ac6454SAndrew Thompson 		}
57002ac6454SAndrew Thompson 		error = uhid_get_report(sc, ugd->ugd_report_type, id,
57102ac6454SAndrew Thompson 		    NULL, ugd->ugd_data, size);
57202ac6454SAndrew Thompson 		break;
57302ac6454SAndrew Thompson 
57402ac6454SAndrew Thompson 	case USB_SET_REPORT:
57502ac6454SAndrew Thompson 		if (!(fflags & FWRITE)) {
57602ac6454SAndrew Thompson 			error = EPERM;
57702ac6454SAndrew Thompson 			break;
57802ac6454SAndrew Thompson 		}
57902ac6454SAndrew Thompson 		ugd = addr;
58002ac6454SAndrew Thompson 		switch (ugd->ugd_report_type) {
58102ac6454SAndrew Thompson 		case UHID_INPUT_REPORT:
58202ac6454SAndrew Thompson 			size = sc->sc_isize;
58302ac6454SAndrew Thompson 			id = sc->sc_iid;
58402ac6454SAndrew Thompson 			break;
58502ac6454SAndrew Thompson 		case UHID_OUTPUT_REPORT:
58602ac6454SAndrew Thompson 			size = sc->sc_osize;
58702ac6454SAndrew Thompson 			id = sc->sc_oid;
58802ac6454SAndrew Thompson 			break;
58902ac6454SAndrew Thompson 		case UHID_FEATURE_REPORT:
59002ac6454SAndrew Thompson 			size = sc->sc_fsize;
59102ac6454SAndrew Thompson 			id = sc->sc_fid;
59202ac6454SAndrew Thompson 			break;
59302ac6454SAndrew Thompson 		default:
59402ac6454SAndrew Thompson 			return (EINVAL);
59502ac6454SAndrew Thompson 		}
59602ac6454SAndrew Thompson 		error = uhid_set_report(sc, ugd->ugd_report_type, id,
59702ac6454SAndrew Thompson 		    NULL, ugd->ugd_data, size);
59802ac6454SAndrew Thompson 		break;
59902ac6454SAndrew Thompson 
60002ac6454SAndrew Thompson 	case USB_GET_REPORT_ID:
60102ac6454SAndrew Thompson 		*(int *)addr = 0;	/* XXX: we only support reportid 0? */
60202ac6454SAndrew Thompson 		break;
60302ac6454SAndrew Thompson 
60402ac6454SAndrew Thompson 	default:
60502ac6454SAndrew Thompson 		error = EINVAL;
60602ac6454SAndrew Thompson 		break;
60702ac6454SAndrew Thompson 	}
60802ac6454SAndrew Thompson 	return (error);
60902ac6454SAndrew Thompson }
61002ac6454SAndrew Thompson 
61102ac6454SAndrew Thompson static int
61202ac6454SAndrew Thompson uhid_probe(device_t dev)
61302ac6454SAndrew Thompson {
614760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
61502ac6454SAndrew Thompson 
61602ac6454SAndrew Thompson 	DPRINTFN(11, "\n");
61702ac6454SAndrew Thompson 
618f29a0724SAndrew Thompson 	if (uaa->usb_mode != USB_MODE_HOST) {
61902ac6454SAndrew Thompson 		return (ENXIO);
62002ac6454SAndrew Thompson 	}
62102ac6454SAndrew Thompson 	if (uaa->use_generic == 0) {
62202ac6454SAndrew Thompson 		/* give Mouse and Keyboard drivers a try first */
62302ac6454SAndrew Thompson 		return (ENXIO);
62402ac6454SAndrew Thompson 	}
62502ac6454SAndrew Thompson 	if (uaa->info.bInterfaceClass != UICLASS_HID) {
62602ac6454SAndrew Thompson 
62702ac6454SAndrew Thompson 		/* the Xbox 360 gamepad doesn't use the HID class */
62802ac6454SAndrew Thompson 
62902ac6454SAndrew Thompson 		if ((uaa->info.bInterfaceClass != UICLASS_VENDOR) ||
63002ac6454SAndrew Thompson 		    (uaa->info.bInterfaceSubClass != UISUBCLASS_XBOX360_CONTROLLER) ||
63102ac6454SAndrew Thompson 		    (uaa->info.bInterfaceProtocol != UIPROTO_XBOX360_GAMEPAD)) {
63202ac6454SAndrew Thompson 			return (ENXIO);
63302ac6454SAndrew Thompson 		}
63402ac6454SAndrew Thompson 	}
635a593f6b8SAndrew Thompson 	if (usb_test_quirk(uaa, UQ_HID_IGNORE)) {
63602ac6454SAndrew Thompson 		return (ENXIO);
63702ac6454SAndrew Thompson 	}
638b201cde6SNathan Whitehorn 	return (BUS_PROBE_GENERIC);
63902ac6454SAndrew Thompson }
64002ac6454SAndrew Thompson 
64102ac6454SAndrew Thompson static int
64202ac6454SAndrew Thompson uhid_attach(device_t dev)
64302ac6454SAndrew Thompson {
644760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
64502ac6454SAndrew Thompson 	struct uhid_softc *sc = device_get_softc(dev);
64602ac6454SAndrew Thompson 	int unit = device_get_unit(dev);
64702ac6454SAndrew Thompson 	int error = 0;
64802ac6454SAndrew Thompson 
64902ac6454SAndrew Thompson 	DPRINTFN(10, "sc=%p\n", sc);
65002ac6454SAndrew Thompson 
651a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
65202ac6454SAndrew Thompson 
65302ac6454SAndrew Thompson 	mtx_init(&sc->sc_mtx, "uhid lock", NULL, MTX_DEF | MTX_RECURSE);
65402ac6454SAndrew Thompson 
65502ac6454SAndrew Thompson 	sc->sc_udev = uaa->device;
65602ac6454SAndrew Thompson 
65702ac6454SAndrew Thompson 	sc->sc_iface_no = uaa->info.bIfaceNum;
65802ac6454SAndrew Thompson 	sc->sc_iface_index = uaa->info.bIfaceIndex;
65902ac6454SAndrew Thompson 
660a593f6b8SAndrew Thompson 	error = usbd_transfer_setup(uaa->device,
66102ac6454SAndrew Thompson 	    &uaa->info.bIfaceIndex, sc->sc_xfer, uhid_config,
66202ac6454SAndrew Thompson 	    UHID_N_TRANSFER, sc, &sc->sc_mtx);
66302ac6454SAndrew Thompson 
66402ac6454SAndrew Thompson 	if (error) {
665a593f6b8SAndrew Thompson 		DPRINTF("error=%s\n", usbd_errstr(error));
66602ac6454SAndrew Thompson 		goto detach;
66702ac6454SAndrew Thompson 	}
66802ac6454SAndrew Thompson 	if (uaa->info.idVendor == USB_VENDOR_WACOM) {
66902ac6454SAndrew Thompson 
67002ac6454SAndrew Thompson 		/* the report descriptor for the Wacom Graphire is broken */
67102ac6454SAndrew Thompson 
67202ac6454SAndrew Thompson 		if (uaa->info.idProduct == USB_PRODUCT_WACOM_GRAPHIRE) {
67302ac6454SAndrew Thompson 
67402ac6454SAndrew Thompson 			sc->sc_repdesc_size = sizeof(uhid_graphire_report_descr);
675ed6d949aSAndrew Thompson 			sc->sc_repdesc_ptr = &uhid_graphire_report_descr;
67602ac6454SAndrew Thompson 			sc->sc_flags |= UHID_FLAG_STATIC_DESC;
67702ac6454SAndrew Thompson 
67802ac6454SAndrew Thompson 		} else if (uaa->info.idProduct == USB_PRODUCT_WACOM_GRAPHIRE3_4X5) {
67902ac6454SAndrew Thompson 
68002ac6454SAndrew Thompson 			static uint8_t reportbuf[] = {2, 2, 2};
68102ac6454SAndrew Thompson 
68202ac6454SAndrew Thompson 			/*
68302ac6454SAndrew Thompson 			 * The Graphire3 needs 0x0202 to be written to
68402ac6454SAndrew Thompson 			 * feature report ID 2 before it'll start
68502ac6454SAndrew Thompson 			 * returning digitizer data.
68602ac6454SAndrew Thompson 			 */
687a593f6b8SAndrew Thompson 			error = usbd_req_set_report(uaa->device, NULL,
688296ade60SAndrew Thompson 			    reportbuf, sizeof(reportbuf),
68902ac6454SAndrew Thompson 			    uaa->info.bIfaceIndex, UHID_FEATURE_REPORT, 2);
69002ac6454SAndrew Thompson 
69102ac6454SAndrew Thompson 			if (error) {
69202ac6454SAndrew Thompson 				DPRINTF("set report failed, error=%s (ignored)\n",
693a593f6b8SAndrew Thompson 				    usbd_errstr(error));
69402ac6454SAndrew Thompson 			}
69502ac6454SAndrew Thompson 			sc->sc_repdesc_size = sizeof(uhid_graphire3_4x5_report_descr);
696ed6d949aSAndrew Thompson 			sc->sc_repdesc_ptr = &uhid_graphire3_4x5_report_descr;
69702ac6454SAndrew Thompson 			sc->sc_flags |= UHID_FLAG_STATIC_DESC;
69802ac6454SAndrew Thompson 		}
69902ac6454SAndrew Thompson 	} else if ((uaa->info.bInterfaceClass == UICLASS_VENDOR) &&
70002ac6454SAndrew Thompson 		    (uaa->info.bInterfaceSubClass == UISUBCLASS_XBOX360_CONTROLLER) &&
70102ac6454SAndrew Thompson 	    (uaa->info.bInterfaceProtocol == UIPROTO_XBOX360_GAMEPAD)) {
70202ac6454SAndrew Thompson 
70302ac6454SAndrew Thompson 		/* the Xbox 360 gamepad has no report descriptor */
70402ac6454SAndrew Thompson 		sc->sc_repdesc_size = sizeof(uhid_xb360gp_report_descr);
705ed6d949aSAndrew Thompson 		sc->sc_repdesc_ptr = &uhid_xb360gp_report_descr;
70602ac6454SAndrew Thompson 		sc->sc_flags |= UHID_FLAG_STATIC_DESC;
70702ac6454SAndrew Thompson 	}
70802ac6454SAndrew Thompson 	if (sc->sc_repdesc_ptr == NULL) {
70902ac6454SAndrew Thompson 
710a593f6b8SAndrew Thompson 		error = usbd_req_get_hid_desc(uaa->device, NULL,
711296ade60SAndrew Thompson 		    &sc->sc_repdesc_ptr, &sc->sc_repdesc_size,
712296ade60SAndrew Thompson 		    M_USBDEV, uaa->info.bIfaceIndex);
71302ac6454SAndrew Thompson 
71402ac6454SAndrew Thompson 		if (error) {
71502ac6454SAndrew Thompson 			device_printf(dev, "no report descriptor\n");
71602ac6454SAndrew Thompson 			goto detach;
71702ac6454SAndrew Thompson 		}
71802ac6454SAndrew Thompson 	}
719a593f6b8SAndrew Thompson 	error = usbd_req_set_idle(uaa->device, NULL,
72002ac6454SAndrew Thompson 	    uaa->info.bIfaceIndex, 0, 0);
72102ac6454SAndrew Thompson 
72202ac6454SAndrew Thompson 	if (error) {
72302ac6454SAndrew Thompson 		DPRINTF("set idle failed, error=%s (ignored)\n",
724a593f6b8SAndrew Thompson 		    usbd_errstr(error));
72502ac6454SAndrew Thompson 	}
72602ac6454SAndrew Thompson 	sc->sc_isize = hid_report_size
72702ac6454SAndrew Thompson 	    (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_input, &sc->sc_iid);
72802ac6454SAndrew Thompson 
72902ac6454SAndrew Thompson 	sc->sc_osize = hid_report_size
73002ac6454SAndrew Thompson 	    (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_output, &sc->sc_oid);
73102ac6454SAndrew Thompson 
73202ac6454SAndrew Thompson 	sc->sc_fsize = hid_report_size
73302ac6454SAndrew Thompson 	    (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_feature, &sc->sc_fid);
73402ac6454SAndrew Thompson 
73502ac6454SAndrew Thompson 	if (sc->sc_isize > UHID_BSIZE) {
73602ac6454SAndrew Thompson 		DPRINTF("input size is too large, "
73702ac6454SAndrew Thompson 		    "%d bytes (truncating)\n",
73802ac6454SAndrew Thompson 		    sc->sc_isize);
73902ac6454SAndrew Thompson 		sc->sc_isize = UHID_BSIZE;
74002ac6454SAndrew Thompson 	}
74102ac6454SAndrew Thompson 	if (sc->sc_osize > UHID_BSIZE) {
74202ac6454SAndrew Thompson 		DPRINTF("output size is too large, "
74302ac6454SAndrew Thompson 		    "%d bytes (truncating)\n",
74402ac6454SAndrew Thompson 		    sc->sc_osize);
74502ac6454SAndrew Thompson 		sc->sc_osize = UHID_BSIZE;
74602ac6454SAndrew Thompson 	}
74702ac6454SAndrew Thompson 	if (sc->sc_fsize > UHID_BSIZE) {
74802ac6454SAndrew Thompson 		DPRINTF("feature size is too large, "
74902ac6454SAndrew Thompson 		    "%d bytes (truncating)\n",
75002ac6454SAndrew Thompson 		    sc->sc_fsize);
75102ac6454SAndrew Thompson 		sc->sc_fsize = UHID_BSIZE;
75202ac6454SAndrew Thompson 	}
75302ac6454SAndrew Thompson 
754a593f6b8SAndrew Thompson 	error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
75502ac6454SAndrew Thompson 	    &uhid_fifo_methods, &sc->sc_fifo,
756ee3e3ff5SAndrew Thompson 	    unit, 0 - 1, uaa->info.bIfaceIndex,
757ee3e3ff5SAndrew Thompson 	    UID_ROOT, GID_OPERATOR, 0644);
75802ac6454SAndrew Thompson 	if (error) {
75902ac6454SAndrew Thompson 		goto detach;
76002ac6454SAndrew Thompson 	}
76102ac6454SAndrew Thompson 	return (0);			/* success */
76202ac6454SAndrew Thompson 
76302ac6454SAndrew Thompson detach:
76402ac6454SAndrew Thompson 	uhid_detach(dev);
76502ac6454SAndrew Thompson 	return (ENOMEM);
76602ac6454SAndrew Thompson }
76702ac6454SAndrew Thompson 
76802ac6454SAndrew Thompson static int
76902ac6454SAndrew Thompson uhid_detach(device_t dev)
77002ac6454SAndrew Thompson {
77102ac6454SAndrew Thompson 	struct uhid_softc *sc = device_get_softc(dev);
77202ac6454SAndrew Thompson 
773a593f6b8SAndrew Thompson 	usb_fifo_detach(&sc->sc_fifo);
77402ac6454SAndrew Thompson 
775a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, UHID_N_TRANSFER);
77602ac6454SAndrew Thompson 
77702ac6454SAndrew Thompson 	if (sc->sc_repdesc_ptr) {
77802ac6454SAndrew Thompson 		if (!(sc->sc_flags & UHID_FLAG_STATIC_DESC)) {
77902ac6454SAndrew Thompson 			free(sc->sc_repdesc_ptr, M_USBDEV);
78002ac6454SAndrew Thompson 		}
78102ac6454SAndrew Thompson 	}
78202ac6454SAndrew Thompson 	mtx_destroy(&sc->sc_mtx);
78302ac6454SAndrew Thompson 
78402ac6454SAndrew Thompson 	return (0);
78502ac6454SAndrew Thompson }
78602ac6454SAndrew Thompson 
78702ac6454SAndrew Thompson static devclass_t uhid_devclass;
78802ac6454SAndrew Thompson 
78902ac6454SAndrew Thompson static device_method_t uhid_methods[] = {
79002ac6454SAndrew Thompson 	DEVMETHOD(device_probe, uhid_probe),
79102ac6454SAndrew Thompson 	DEVMETHOD(device_attach, uhid_attach),
79202ac6454SAndrew Thompson 	DEVMETHOD(device_detach, uhid_detach),
79302ac6454SAndrew Thompson 	{0, 0}
79402ac6454SAndrew Thompson };
79502ac6454SAndrew Thompson 
79602ac6454SAndrew Thompson static driver_t uhid_driver = {
79702ac6454SAndrew Thompson 	.name = "uhid",
79802ac6454SAndrew Thompson 	.methods = uhid_methods,
79902ac6454SAndrew Thompson 	.size = sizeof(struct uhid_softc),
80002ac6454SAndrew Thompson };
80102ac6454SAndrew Thompson 
8029aef556dSAndrew Thompson DRIVER_MODULE(uhid, uhub, uhid_driver, uhid_devclass, NULL, 0);
80302ac6454SAndrew Thompson MODULE_DEPEND(uhid, usb, 1, 1, 1);
804*910cb8feSAndrew Thompson MODULE_VERSION(uhid, 1);
805