xref: /freebsd/sys/dev/usb/input/uhid.c (revision a593f6b8de8b956bd0dd22f74805ede942e3d6a9)
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  * 3. All advertising materials mentioning features or use of this software
2702ac6454SAndrew Thompson  *    must display the following acknowledgement:
2802ac6454SAndrew Thompson  *        This product includes software developed by the NetBSD
2902ac6454SAndrew Thompson  *        Foundation, Inc. and its contributors.
3002ac6454SAndrew Thompson  * 4. Neither the name of The NetBSD Foundation nor the names of its
3102ac6454SAndrew Thompson  *    contributors may be used to endorse or promote products derived
3202ac6454SAndrew Thompson  *    from this software without specific prior written permission.
3302ac6454SAndrew Thompson  *
3402ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
3502ac6454SAndrew Thompson  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
3602ac6454SAndrew Thompson  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
3702ac6454SAndrew Thompson  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
3802ac6454SAndrew Thompson  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3902ac6454SAndrew Thompson  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
4002ac6454SAndrew Thompson  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
4102ac6454SAndrew Thompson  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
4202ac6454SAndrew Thompson  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
4302ac6454SAndrew Thompson  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
4402ac6454SAndrew Thompson  * POSSIBILITY OF SUCH DAMAGE.
4502ac6454SAndrew Thompson  */
4602ac6454SAndrew Thompson 
4702ac6454SAndrew Thompson /*
4802ac6454SAndrew Thompson  * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
4902ac6454SAndrew Thompson  */
5002ac6454SAndrew Thompson 
5102ac6454SAndrew Thompson #include "usbdevs.h"
5202ac6454SAndrew Thompson #include <dev/usb/usb.h>
5302ac6454SAndrew Thompson #include <dev/usb/usb_mfunc.h>
5402ac6454SAndrew Thompson #include <dev/usb/usb_error.h>
5502ac6454SAndrew Thompson #include <dev/usb/usbhid.h>
5602ac6454SAndrew Thompson #include <dev/usb/usb_ioctl.h>
5702ac6454SAndrew Thompson 
5802ac6454SAndrew Thompson #define	USB_DEBUG_VAR uhid_debug
5902ac6454SAndrew Thompson 
6002ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
6102ac6454SAndrew Thompson #include <dev/usb/usb_util.h>
6202ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
6302ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
6402ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
6502ac6454SAndrew Thompson #include <dev/usb/usb_transfer.h>
6602ac6454SAndrew Thompson #include <dev/usb/usb_request.h>
6702ac6454SAndrew Thompson #include <dev/usb/usb_dynamic.h>
6802ac6454SAndrew Thompson #include <dev/usb/usb_mbuf.h>
6902ac6454SAndrew Thompson #include <dev/usb/usb_dev.h>
7002ac6454SAndrew Thompson #include <dev/usb/usb_hid.h>
7102ac6454SAndrew Thompson 
7202ac6454SAndrew Thompson #include <dev/usb/input/usb_rdesc.h>
7302ac6454SAndrew Thompson 
7402ac6454SAndrew Thompson #include <dev/usb/quirk/usb_quirk.h>
7502ac6454SAndrew Thompson 
7602ac6454SAndrew Thompson #if USB_DEBUG
7702ac6454SAndrew Thompson static int uhid_debug = 0;
7802ac6454SAndrew Thompson 
799360ae40SAndrew Thompson SYSCTL_NODE(_hw_usb, OID_AUTO, uhid, CTLFLAG_RW, 0, "USB uhid");
809360ae40SAndrew Thompson SYSCTL_INT(_hw_usb_uhid, OID_AUTO, debug, CTLFLAG_RW,
8102ac6454SAndrew Thompson     &uhid_debug, 0, "Debug level");
8202ac6454SAndrew Thompson #endif
8302ac6454SAndrew Thompson 
8402ac6454SAndrew Thompson #define	UHID_BSIZE	1024		/* bytes, buffer size */
8502ac6454SAndrew Thompson #define	UHID_FRAME_NUM 	  50		/* bytes, frame number */
8602ac6454SAndrew Thompson 
8702ac6454SAndrew Thompson enum {
8802ac6454SAndrew Thompson 	UHID_INTR_DT_RD,
8902ac6454SAndrew Thompson 	UHID_CTRL_DT_WR,
9002ac6454SAndrew Thompson 	UHID_CTRL_DT_RD,
9102ac6454SAndrew Thompson 	UHID_N_TRANSFER,
9202ac6454SAndrew Thompson };
9302ac6454SAndrew Thompson 
9402ac6454SAndrew Thompson struct uhid_softc {
95760bc48eSAndrew Thompson 	struct usb_fifo_sc sc_fifo;
9602ac6454SAndrew Thompson 	struct mtx sc_mtx;
9702ac6454SAndrew Thompson 
98760bc48eSAndrew Thompson 	struct usb_xfer *sc_xfer[UHID_N_TRANSFER];
99760bc48eSAndrew Thompson 	struct usb_device *sc_udev;
10002ac6454SAndrew Thompson 	void   *sc_repdesc_ptr;
10102ac6454SAndrew Thompson 
10202ac6454SAndrew Thompson 	uint32_t sc_isize;
10302ac6454SAndrew Thompson 	uint32_t sc_osize;
10402ac6454SAndrew Thompson 	uint32_t sc_fsize;
10502ac6454SAndrew Thompson 
10602ac6454SAndrew Thompson 	uint16_t sc_repdesc_size;
10702ac6454SAndrew Thompson 
10802ac6454SAndrew Thompson 	uint8_t	sc_iface_no;
10902ac6454SAndrew Thompson 	uint8_t	sc_iface_index;
11002ac6454SAndrew Thompson 	uint8_t	sc_iid;
11102ac6454SAndrew Thompson 	uint8_t	sc_oid;
11202ac6454SAndrew Thompson 	uint8_t	sc_fid;
11302ac6454SAndrew Thompson 	uint8_t	sc_flags;
11402ac6454SAndrew Thompson #define	UHID_FLAG_IMMED        0x01	/* set if read should be immediate */
11502ac6454SAndrew Thompson #define	UHID_FLAG_STATIC_DESC  0x04	/* set if report descriptors are
11602ac6454SAndrew Thompson 					 * static */
11702ac6454SAndrew Thompson };
11802ac6454SAndrew Thompson 
11902ac6454SAndrew Thompson static const uint8_t uhid_xb360gp_report_descr[] = {UHID_XB360GP_REPORT_DESCR()};
12002ac6454SAndrew Thompson static const uint8_t uhid_graphire_report_descr[] = {UHID_GRAPHIRE_REPORT_DESCR()};
12102ac6454SAndrew Thompson static const uint8_t uhid_graphire3_4x5_report_descr[] = {UHID_GRAPHIRE3_4X5_REPORT_DESCR()};
12202ac6454SAndrew Thompson 
12302ac6454SAndrew Thompson /* prototypes */
12402ac6454SAndrew Thompson 
12502ac6454SAndrew Thompson static device_probe_t uhid_probe;
12602ac6454SAndrew Thompson static device_attach_t uhid_attach;
12702ac6454SAndrew Thompson static device_detach_t uhid_detach;
12802ac6454SAndrew Thompson 
129e0a69b51SAndrew Thompson static usb_callback_t uhid_intr_callback;
130e0a69b51SAndrew Thompson static usb_callback_t uhid_write_callback;
131e0a69b51SAndrew Thompson static usb_callback_t uhid_read_callback;
13202ac6454SAndrew Thompson 
133e0a69b51SAndrew Thompson static usb_fifo_cmd_t uhid_start_read;
134e0a69b51SAndrew Thompson static usb_fifo_cmd_t uhid_stop_read;
135e0a69b51SAndrew Thompson static usb_fifo_cmd_t uhid_start_write;
136e0a69b51SAndrew Thompson static usb_fifo_cmd_t uhid_stop_write;
137e0a69b51SAndrew Thompson static usb_fifo_open_t uhid_open;
138e0a69b51SAndrew Thompson static usb_fifo_close_t uhid_close;
139e0a69b51SAndrew Thompson static usb_fifo_ioctl_t uhid_ioctl;
14002ac6454SAndrew Thompson 
141760bc48eSAndrew Thompson static struct usb_fifo_methods uhid_fifo_methods = {
14202ac6454SAndrew Thompson 	.f_open = &uhid_open,
14302ac6454SAndrew Thompson 	.f_close = &uhid_close,
14402ac6454SAndrew Thompson 	.f_ioctl = &uhid_ioctl,
14502ac6454SAndrew Thompson 	.f_start_read = &uhid_start_read,
14602ac6454SAndrew Thompson 	.f_stop_read = &uhid_stop_read,
14702ac6454SAndrew Thompson 	.f_start_write = &uhid_start_write,
14802ac6454SAndrew Thompson 	.f_stop_write = &uhid_stop_write,
14902ac6454SAndrew Thompson 	.basename[0] = "uhid",
15002ac6454SAndrew Thompson };
15102ac6454SAndrew Thompson 
15202ac6454SAndrew Thompson static void
153760bc48eSAndrew Thompson uhid_intr_callback(struct usb_xfer *xfer)
15402ac6454SAndrew Thompson {
15502ac6454SAndrew Thompson 	struct uhid_softc *sc = xfer->priv_sc;
15602ac6454SAndrew Thompson 
15702ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
15802ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
15902ac6454SAndrew Thompson 		DPRINTF("transferred!\n");
16002ac6454SAndrew Thompson 
16102ac6454SAndrew Thompson 		if (xfer->actlen >= sc->sc_isize) {
162a593f6b8SAndrew Thompson 			usb_fifo_put_data(
16302ac6454SAndrew Thompson 			    sc->sc_fifo.fp[USB_FIFO_RX],
16402ac6454SAndrew Thompson 			    xfer->frbuffers,
16502ac6454SAndrew Thompson 			    0, sc->sc_isize, 1);
16602ac6454SAndrew Thompson 		} else {
16702ac6454SAndrew Thompson 			/* ignore it */
16802ac6454SAndrew Thompson 			DPRINTF("ignored short transfer, "
16902ac6454SAndrew Thompson 			    "%d bytes\n", xfer->actlen);
17002ac6454SAndrew Thompson 		}
17102ac6454SAndrew Thompson 
17202ac6454SAndrew Thompson 	case USB_ST_SETUP:
17302ac6454SAndrew Thompson re_submit:
174a593f6b8SAndrew Thompson 		if (usb_fifo_put_bytes_max(
17502ac6454SAndrew Thompson 		    sc->sc_fifo.fp[USB_FIFO_RX]) != 0) {
17602ac6454SAndrew Thompson 			xfer->frlengths[0] = sc->sc_isize;
177a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
17802ac6454SAndrew Thompson 		}
17902ac6454SAndrew Thompson 		return;
18002ac6454SAndrew Thompson 
18102ac6454SAndrew Thompson 	default:			/* Error */
18202ac6454SAndrew Thompson 		if (xfer->error != USB_ERR_CANCELLED) {
18302ac6454SAndrew Thompson 			/* try to clear stall first */
18402ac6454SAndrew Thompson 			xfer->flags.stall_pipe = 1;
18502ac6454SAndrew Thompson 			goto re_submit;
18602ac6454SAndrew Thompson 		}
18702ac6454SAndrew Thompson 		return;
18802ac6454SAndrew Thompson 	}
18902ac6454SAndrew Thompson }
19002ac6454SAndrew Thompson 
19102ac6454SAndrew Thompson static void
192760bc48eSAndrew Thompson uhid_fill_set_report(struct usb_device_request *req, uint8_t iface_no,
19302ac6454SAndrew Thompson     uint8_t type, uint8_t id, uint16_t size)
19402ac6454SAndrew Thompson {
19502ac6454SAndrew Thompson 	req->bmRequestType = UT_WRITE_CLASS_INTERFACE;
19602ac6454SAndrew Thompson 	req->bRequest = UR_SET_REPORT;
19702ac6454SAndrew Thompson 	USETW2(req->wValue, type, id);
19802ac6454SAndrew Thompson 	req->wIndex[0] = iface_no;
19902ac6454SAndrew Thompson 	req->wIndex[1] = 0;
20002ac6454SAndrew Thompson 	USETW(req->wLength, size);
20102ac6454SAndrew Thompson }
20202ac6454SAndrew Thompson 
20302ac6454SAndrew Thompson static void
204760bc48eSAndrew Thompson uhid_fill_get_report(struct usb_device_request *req, uint8_t iface_no,
20502ac6454SAndrew Thompson     uint8_t type, uint8_t id, uint16_t size)
20602ac6454SAndrew Thompson {
20702ac6454SAndrew Thompson 	req->bmRequestType = UT_READ_CLASS_INTERFACE;
20802ac6454SAndrew Thompson 	req->bRequest = UR_GET_REPORT;
20902ac6454SAndrew Thompson 	USETW2(req->wValue, type, id);
21002ac6454SAndrew Thompson 	req->wIndex[0] = iface_no;
21102ac6454SAndrew Thompson 	req->wIndex[1] = 0;
21202ac6454SAndrew Thompson 	USETW(req->wLength, size);
21302ac6454SAndrew Thompson }
21402ac6454SAndrew Thompson 
21502ac6454SAndrew Thompson static void
216760bc48eSAndrew Thompson uhid_write_callback(struct usb_xfer *xfer)
21702ac6454SAndrew Thompson {
21802ac6454SAndrew Thompson 	struct uhid_softc *sc = xfer->priv_sc;
219760bc48eSAndrew Thompson 	struct usb_device_request req;
22002ac6454SAndrew Thompson 	uint32_t size = sc->sc_osize;
22102ac6454SAndrew Thompson 	uint32_t actlen;
22202ac6454SAndrew Thompson 	uint8_t id;
22302ac6454SAndrew Thompson 
22402ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
22502ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
22602ac6454SAndrew Thompson 	case USB_ST_SETUP:
22702ac6454SAndrew Thompson 		/* try to extract the ID byte */
22802ac6454SAndrew Thompson 		if (sc->sc_oid) {
22902ac6454SAndrew Thompson 
230a593f6b8SAndrew Thompson 			if (usb_fifo_get_data(
23102ac6454SAndrew Thompson 			    sc->sc_fifo.fp[USB_FIFO_TX],
23202ac6454SAndrew Thompson 			    xfer->frbuffers,
23302ac6454SAndrew Thompson 			    0, 1, &actlen, 0)) {
23402ac6454SAndrew Thompson 				if (actlen != 1) {
23502ac6454SAndrew Thompson 					goto tr_error;
23602ac6454SAndrew Thompson 				}
237a593f6b8SAndrew Thompson 				usbd_copy_out(xfer->frbuffers, 0, &id, 1);
23802ac6454SAndrew Thompson 
23902ac6454SAndrew Thompson 			} else {
24002ac6454SAndrew Thompson 				return;
24102ac6454SAndrew Thompson 			}
24202ac6454SAndrew Thompson 			if (size) {
24302ac6454SAndrew Thompson 				size--;
24402ac6454SAndrew Thompson 			}
24502ac6454SAndrew Thompson 		} else {
24602ac6454SAndrew Thompson 			id = 0;
24702ac6454SAndrew Thompson 		}
24802ac6454SAndrew Thompson 
249a593f6b8SAndrew Thompson 		if (usb_fifo_get_data(
25002ac6454SAndrew Thompson 		    sc->sc_fifo.fp[USB_FIFO_TX],
25102ac6454SAndrew Thompson 		    xfer->frbuffers + 1,
25202ac6454SAndrew Thompson 		    0, UHID_BSIZE, &actlen, 1)) {
25302ac6454SAndrew Thompson 			if (actlen != size) {
25402ac6454SAndrew Thompson 				goto tr_error;
25502ac6454SAndrew Thompson 			}
25602ac6454SAndrew Thompson 			uhid_fill_set_report
25702ac6454SAndrew Thompson 			    (&req, sc->sc_iface_no,
25802ac6454SAndrew Thompson 			    UHID_OUTPUT_REPORT, id, size);
25902ac6454SAndrew Thompson 
260a593f6b8SAndrew Thompson 			usbd_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
26102ac6454SAndrew Thompson 
26202ac6454SAndrew Thompson 			xfer->frlengths[0] = sizeof(req);
26302ac6454SAndrew Thompson 			xfer->frlengths[1] = size;
26402ac6454SAndrew Thompson 			xfer->nframes = xfer->frlengths[1] ? 2 : 1;
265a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
26602ac6454SAndrew Thompson 		}
26702ac6454SAndrew Thompson 		return;
26802ac6454SAndrew Thompson 
26902ac6454SAndrew Thompson 	default:
27002ac6454SAndrew Thompson tr_error:
27102ac6454SAndrew Thompson 		/* bomb out */
272a593f6b8SAndrew Thompson 		usb_fifo_get_data_error(sc->sc_fifo.fp[USB_FIFO_TX]);
27302ac6454SAndrew Thompson 		return;
27402ac6454SAndrew Thompson 	}
27502ac6454SAndrew Thompson }
27602ac6454SAndrew Thompson 
27702ac6454SAndrew Thompson static void
278760bc48eSAndrew Thompson uhid_read_callback(struct usb_xfer *xfer)
27902ac6454SAndrew Thompson {
28002ac6454SAndrew Thompson 	struct uhid_softc *sc = xfer->priv_sc;
281760bc48eSAndrew Thompson 	struct usb_device_request req;
28202ac6454SAndrew Thompson 
28302ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
28402ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
285a593f6b8SAndrew Thompson 		usb_fifo_put_data(sc->sc_fifo.fp[USB_FIFO_RX], xfer->frbuffers,
28602ac6454SAndrew Thompson 		    sizeof(req), sc->sc_isize, 1);
28702ac6454SAndrew Thompson 		return;
28802ac6454SAndrew Thompson 
28902ac6454SAndrew Thompson 	case USB_ST_SETUP:
29002ac6454SAndrew Thompson 
291a593f6b8SAndrew Thompson 		if (usb_fifo_put_bytes_max(sc->sc_fifo.fp[USB_FIFO_RX]) > 0) {
29202ac6454SAndrew Thompson 
29302ac6454SAndrew Thompson 			uhid_fill_get_report
29402ac6454SAndrew Thompson 			    (&req, sc->sc_iface_no, UHID_INPUT_REPORT,
29502ac6454SAndrew Thompson 			    sc->sc_iid, sc->sc_isize);
29602ac6454SAndrew Thompson 
297a593f6b8SAndrew Thompson 			usbd_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
29802ac6454SAndrew Thompson 
29902ac6454SAndrew Thompson 			xfer->frlengths[0] = sizeof(req);
30002ac6454SAndrew Thompson 			xfer->frlengths[1] = sc->sc_isize;
30102ac6454SAndrew Thompson 			xfer->nframes = xfer->frlengths[1] ? 2 : 1;
302a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
30302ac6454SAndrew Thompson 		}
30402ac6454SAndrew Thompson 		return;
30502ac6454SAndrew Thompson 
30602ac6454SAndrew Thompson 	default:			/* Error */
30702ac6454SAndrew Thompson 		/* bomb out */
308a593f6b8SAndrew Thompson 		usb_fifo_put_data_error(sc->sc_fifo.fp[USB_FIFO_RX]);
30902ac6454SAndrew Thompson 		return;
31002ac6454SAndrew Thompson 	}
31102ac6454SAndrew Thompson }
31202ac6454SAndrew Thompson 
313760bc48eSAndrew Thompson static const struct usb_config uhid_config[UHID_N_TRANSFER] = {
31402ac6454SAndrew Thompson 
31502ac6454SAndrew Thompson 	[UHID_INTR_DT_RD] = {
31602ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
31702ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
31802ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
3194eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
3204eae601eSAndrew Thompson 		.bufsize = UHID_BSIZE,
3214eae601eSAndrew Thompson 		.callback = &uhid_intr_callback,
32202ac6454SAndrew Thompson 	},
32302ac6454SAndrew Thompson 
32402ac6454SAndrew Thompson 	[UHID_CTRL_DT_WR] = {
32502ac6454SAndrew Thompson 		.type = UE_CONTROL,
32602ac6454SAndrew Thompson 		.endpoint = 0x00,	/* Control pipe */
32702ac6454SAndrew Thompson 		.direction = UE_DIR_ANY,
328760bc48eSAndrew Thompson 		.bufsize = sizeof(struct usb_device_request) + UHID_BSIZE,
3294eae601eSAndrew Thompson 		.callback = &uhid_write_callback,
3304eae601eSAndrew Thompson 		.timeout = 1000,	/* 1 second */
33102ac6454SAndrew Thompson 	},
33202ac6454SAndrew Thompson 
33302ac6454SAndrew Thompson 	[UHID_CTRL_DT_RD] = {
33402ac6454SAndrew Thompson 		.type = UE_CONTROL,
33502ac6454SAndrew Thompson 		.endpoint = 0x00,	/* Control pipe */
33602ac6454SAndrew Thompson 		.direction = UE_DIR_ANY,
337760bc48eSAndrew Thompson 		.bufsize = sizeof(struct usb_device_request) + UHID_BSIZE,
3384eae601eSAndrew Thompson 		.callback = &uhid_read_callback,
3394eae601eSAndrew Thompson 		.timeout = 1000,	/* 1 second */
34002ac6454SAndrew Thompson 	},
34102ac6454SAndrew Thompson };
34202ac6454SAndrew Thompson 
34302ac6454SAndrew Thompson static void
344760bc48eSAndrew Thompson uhid_start_read(struct usb_fifo *fifo)
34502ac6454SAndrew Thompson {
34602ac6454SAndrew Thompson 	struct uhid_softc *sc = fifo->priv_sc0;
34702ac6454SAndrew Thompson 
34802ac6454SAndrew Thompson 	if (sc->sc_flags & UHID_FLAG_IMMED) {
349a593f6b8SAndrew Thompson 		usbd_transfer_start(sc->sc_xfer[UHID_CTRL_DT_RD]);
35002ac6454SAndrew Thompson 	} else {
351a593f6b8SAndrew Thompson 		usbd_transfer_start(sc->sc_xfer[UHID_INTR_DT_RD]);
35202ac6454SAndrew Thompson 	}
35302ac6454SAndrew Thompson }
35402ac6454SAndrew Thompson 
35502ac6454SAndrew Thompson static void
356760bc48eSAndrew Thompson uhid_stop_read(struct usb_fifo *fifo)
35702ac6454SAndrew Thompson {
35802ac6454SAndrew Thompson 	struct uhid_softc *sc = fifo->priv_sc0;
35902ac6454SAndrew Thompson 
360a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UHID_CTRL_DT_RD]);
361a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UHID_INTR_DT_RD]);
36202ac6454SAndrew Thompson }
36302ac6454SAndrew Thompson 
36402ac6454SAndrew Thompson static void
365760bc48eSAndrew Thompson uhid_start_write(struct usb_fifo *fifo)
36602ac6454SAndrew Thompson {
36702ac6454SAndrew Thompson 	struct uhid_softc *sc = fifo->priv_sc0;
36802ac6454SAndrew Thompson 
369a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UHID_CTRL_DT_WR]);
37002ac6454SAndrew Thompson }
37102ac6454SAndrew Thompson 
37202ac6454SAndrew Thompson static void
373760bc48eSAndrew Thompson uhid_stop_write(struct usb_fifo *fifo)
37402ac6454SAndrew Thompson {
37502ac6454SAndrew Thompson 	struct uhid_softc *sc = fifo->priv_sc0;
37602ac6454SAndrew Thompson 
377a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UHID_CTRL_DT_WR]);
37802ac6454SAndrew Thompson }
37902ac6454SAndrew Thompson 
38002ac6454SAndrew Thompson static int
38102ac6454SAndrew Thompson uhid_get_report(struct uhid_softc *sc, uint8_t type,
38202ac6454SAndrew Thompson     uint8_t id, void *kern_data, void *user_data,
38302ac6454SAndrew Thompson     uint16_t len)
38402ac6454SAndrew Thompson {
38502ac6454SAndrew Thompson 	int err;
38602ac6454SAndrew Thompson 	uint8_t free_data = 0;
38702ac6454SAndrew Thompson 
38802ac6454SAndrew Thompson 	if (kern_data == NULL) {
38902ac6454SAndrew Thompson 		kern_data = malloc(len, M_USBDEV, M_WAITOK);
39002ac6454SAndrew Thompson 		if (kern_data == NULL) {
39102ac6454SAndrew Thompson 			err = ENOMEM;
39202ac6454SAndrew Thompson 			goto done;
39302ac6454SAndrew Thompson 		}
39402ac6454SAndrew Thompson 		free_data = 1;
39502ac6454SAndrew Thompson 	}
396a593f6b8SAndrew Thompson 	err = usbd_req_get_report(sc->sc_udev, NULL, kern_data,
39702ac6454SAndrew Thompson 	    len, sc->sc_iface_index, type, id);
39802ac6454SAndrew Thompson 	if (err) {
39902ac6454SAndrew Thompson 		err = ENXIO;
40002ac6454SAndrew Thompson 		goto done;
40102ac6454SAndrew Thompson 	}
40202ac6454SAndrew Thompson 	if (user_data) {
40302ac6454SAndrew Thompson 		/* dummy buffer */
40402ac6454SAndrew Thompson 		err = copyout(kern_data, user_data, len);
40502ac6454SAndrew Thompson 		if (err) {
40602ac6454SAndrew Thompson 			goto done;
40702ac6454SAndrew Thompson 		}
40802ac6454SAndrew Thompson 	}
40902ac6454SAndrew Thompson done:
41002ac6454SAndrew Thompson 	if (free_data) {
41102ac6454SAndrew Thompson 		free(kern_data, M_USBDEV);
41202ac6454SAndrew Thompson 	}
41302ac6454SAndrew Thompson 	return (err);
41402ac6454SAndrew Thompson }
41502ac6454SAndrew Thompson 
41602ac6454SAndrew Thompson static int
41702ac6454SAndrew Thompson uhid_set_report(struct uhid_softc *sc, uint8_t type,
41802ac6454SAndrew Thompson     uint8_t id, void *kern_data, void *user_data,
41902ac6454SAndrew Thompson     uint16_t len)
42002ac6454SAndrew Thompson {
42102ac6454SAndrew Thompson 	int err;
42202ac6454SAndrew Thompson 	uint8_t free_data = 0;
42302ac6454SAndrew Thompson 
42402ac6454SAndrew Thompson 	if (kern_data == NULL) {
42502ac6454SAndrew Thompson 		kern_data = malloc(len, M_USBDEV, M_WAITOK);
42602ac6454SAndrew Thompson 		if (kern_data == NULL) {
42702ac6454SAndrew Thompson 			err = ENOMEM;
42802ac6454SAndrew Thompson 			goto done;
42902ac6454SAndrew Thompson 		}
43002ac6454SAndrew Thompson 		free_data = 1;
43102ac6454SAndrew Thompson 		err = copyin(user_data, kern_data, len);
43202ac6454SAndrew Thompson 		if (err) {
43302ac6454SAndrew Thompson 			goto done;
43402ac6454SAndrew Thompson 		}
43502ac6454SAndrew Thompson 	}
436a593f6b8SAndrew Thompson 	err = usbd_req_set_report(sc->sc_udev, NULL, kern_data,
43702ac6454SAndrew Thompson 	    len, sc->sc_iface_index, type, id);
43802ac6454SAndrew Thompson 	if (err) {
43902ac6454SAndrew Thompson 		err = ENXIO;
44002ac6454SAndrew Thompson 		goto done;
44102ac6454SAndrew Thompson 	}
44202ac6454SAndrew Thompson done:
44302ac6454SAndrew Thompson 	if (free_data) {
44402ac6454SAndrew Thompson 		free(kern_data, M_USBDEV);
44502ac6454SAndrew Thompson 	}
44602ac6454SAndrew Thompson 	return (err);
44702ac6454SAndrew Thompson }
44802ac6454SAndrew Thompson 
44902ac6454SAndrew Thompson static int
450760bc48eSAndrew Thompson uhid_open(struct usb_fifo *fifo, int fflags)
45102ac6454SAndrew Thompson {
45202ac6454SAndrew Thompson 	struct uhid_softc *sc = fifo->priv_sc0;
45302ac6454SAndrew Thompson 
45402ac6454SAndrew Thompson 	/*
45502ac6454SAndrew Thompson 	 * The buffers are one byte larger than maximum so that one
45602ac6454SAndrew Thompson 	 * can detect too large read/writes and short transfers:
45702ac6454SAndrew Thompson 	 */
45802ac6454SAndrew Thompson 	if (fflags & FREAD) {
45902ac6454SAndrew Thompson 		/* reset flags */
46002ac6454SAndrew Thompson 		sc->sc_flags &= ~UHID_FLAG_IMMED;
46102ac6454SAndrew Thompson 
462a593f6b8SAndrew Thompson 		if (usb_fifo_alloc_buffer(fifo,
46302ac6454SAndrew Thompson 		    sc->sc_isize + 1, UHID_FRAME_NUM)) {
46402ac6454SAndrew Thompson 			return (ENOMEM);
46502ac6454SAndrew Thompson 		}
46602ac6454SAndrew Thompson 	}
46702ac6454SAndrew Thompson 	if (fflags & FWRITE) {
468a593f6b8SAndrew Thompson 		if (usb_fifo_alloc_buffer(fifo,
46902ac6454SAndrew Thompson 		    sc->sc_osize + 1, UHID_FRAME_NUM)) {
47002ac6454SAndrew Thompson 			return (ENOMEM);
47102ac6454SAndrew Thompson 		}
47202ac6454SAndrew Thompson 	}
47302ac6454SAndrew Thompson 	return (0);
47402ac6454SAndrew Thompson }
47502ac6454SAndrew Thompson 
47602ac6454SAndrew Thompson static void
477760bc48eSAndrew Thompson uhid_close(struct usb_fifo *fifo, int fflags)
47802ac6454SAndrew Thompson {
47902ac6454SAndrew Thompson 	if (fflags & (FREAD | FWRITE)) {
480a593f6b8SAndrew Thompson 		usb_fifo_free_buffer(fifo);
48102ac6454SAndrew Thompson 	}
48202ac6454SAndrew Thompson }
48302ac6454SAndrew Thompson 
48402ac6454SAndrew Thompson static int
485760bc48eSAndrew Thompson uhid_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr,
486ee3e3ff5SAndrew Thompson     int fflags)
48702ac6454SAndrew Thompson {
48802ac6454SAndrew Thompson 	struct uhid_softc *sc = fifo->priv_sc0;
489760bc48eSAndrew Thompson 	struct usb_gen_descriptor *ugd;
49002ac6454SAndrew Thompson 	uint32_t size;
49102ac6454SAndrew Thompson 	int error = 0;
49202ac6454SAndrew Thompson 	uint8_t id;
49302ac6454SAndrew Thompson 
49402ac6454SAndrew Thompson 	switch (cmd) {
49502ac6454SAndrew Thompson 	case USB_GET_REPORT_DESC:
49602ac6454SAndrew Thompson 		ugd = addr;
49702ac6454SAndrew Thompson 		if (sc->sc_repdesc_size > ugd->ugd_maxlen) {
49802ac6454SAndrew Thompson 			size = ugd->ugd_maxlen;
49902ac6454SAndrew Thompson 		} else {
50002ac6454SAndrew Thompson 			size = sc->sc_repdesc_size;
50102ac6454SAndrew Thompson 		}
50202ac6454SAndrew Thompson 		ugd->ugd_actlen = size;
50302ac6454SAndrew Thompson 		if (ugd->ugd_data == NULL)
50402ac6454SAndrew Thompson 			break;		/* descriptor length only */
50502ac6454SAndrew Thompson 		error = copyout(sc->sc_repdesc_ptr, ugd->ugd_data, size);
50602ac6454SAndrew Thompson 		break;
50702ac6454SAndrew Thompson 
50802ac6454SAndrew Thompson 	case USB_SET_IMMED:
50902ac6454SAndrew Thompson 		if (!(fflags & FREAD)) {
51002ac6454SAndrew Thompson 			error = EPERM;
51102ac6454SAndrew Thompson 			break;
51202ac6454SAndrew Thompson 		}
51302ac6454SAndrew Thompson 		if (*(int *)addr) {
51402ac6454SAndrew Thompson 
51502ac6454SAndrew Thompson 			/* do a test read */
51602ac6454SAndrew Thompson 
51702ac6454SAndrew Thompson 			error = uhid_get_report(sc, UHID_INPUT_REPORT,
51802ac6454SAndrew Thompson 			    sc->sc_iid, NULL, NULL, sc->sc_isize);
51902ac6454SAndrew Thompson 			if (error) {
52002ac6454SAndrew Thompson 				break;
52102ac6454SAndrew Thompson 			}
52202ac6454SAndrew Thompson 			mtx_lock(&sc->sc_mtx);
52302ac6454SAndrew Thompson 			sc->sc_flags |= UHID_FLAG_IMMED;
52402ac6454SAndrew Thompson 			mtx_unlock(&sc->sc_mtx);
52502ac6454SAndrew Thompson 		} else {
52602ac6454SAndrew Thompson 			mtx_lock(&sc->sc_mtx);
52702ac6454SAndrew Thompson 			sc->sc_flags &= ~UHID_FLAG_IMMED;
52802ac6454SAndrew Thompson 			mtx_unlock(&sc->sc_mtx);
52902ac6454SAndrew Thompson 		}
53002ac6454SAndrew Thompson 		break;
53102ac6454SAndrew Thompson 
53202ac6454SAndrew Thompson 	case USB_GET_REPORT:
53302ac6454SAndrew Thompson 		if (!(fflags & FREAD)) {
53402ac6454SAndrew Thompson 			error = EPERM;
53502ac6454SAndrew Thompson 			break;
53602ac6454SAndrew Thompson 		}
53702ac6454SAndrew Thompson 		ugd = addr;
53802ac6454SAndrew Thompson 		switch (ugd->ugd_report_type) {
53902ac6454SAndrew Thompson 		case UHID_INPUT_REPORT:
54002ac6454SAndrew Thompson 			size = sc->sc_isize;
54102ac6454SAndrew Thompson 			id = sc->sc_iid;
54202ac6454SAndrew Thompson 			break;
54302ac6454SAndrew Thompson 		case UHID_OUTPUT_REPORT:
54402ac6454SAndrew Thompson 			size = sc->sc_osize;
54502ac6454SAndrew Thompson 			id = sc->sc_oid;
54602ac6454SAndrew Thompson 			break;
54702ac6454SAndrew Thompson 		case UHID_FEATURE_REPORT:
54802ac6454SAndrew Thompson 			size = sc->sc_fsize;
54902ac6454SAndrew Thompson 			id = sc->sc_fid;
55002ac6454SAndrew Thompson 			break;
55102ac6454SAndrew Thompson 		default:
55202ac6454SAndrew Thompson 			return (EINVAL);
55302ac6454SAndrew Thompson 		}
55402ac6454SAndrew Thompson 		error = uhid_get_report(sc, ugd->ugd_report_type, id,
55502ac6454SAndrew Thompson 		    NULL, ugd->ugd_data, size);
55602ac6454SAndrew Thompson 		break;
55702ac6454SAndrew Thompson 
55802ac6454SAndrew Thompson 	case USB_SET_REPORT:
55902ac6454SAndrew Thompson 		if (!(fflags & FWRITE)) {
56002ac6454SAndrew Thompson 			error = EPERM;
56102ac6454SAndrew Thompson 			break;
56202ac6454SAndrew Thompson 		}
56302ac6454SAndrew Thompson 		ugd = addr;
56402ac6454SAndrew Thompson 		switch (ugd->ugd_report_type) {
56502ac6454SAndrew Thompson 		case UHID_INPUT_REPORT:
56602ac6454SAndrew Thompson 			size = sc->sc_isize;
56702ac6454SAndrew Thompson 			id = sc->sc_iid;
56802ac6454SAndrew Thompson 			break;
56902ac6454SAndrew Thompson 		case UHID_OUTPUT_REPORT:
57002ac6454SAndrew Thompson 			size = sc->sc_osize;
57102ac6454SAndrew Thompson 			id = sc->sc_oid;
57202ac6454SAndrew Thompson 			break;
57302ac6454SAndrew Thompson 		case UHID_FEATURE_REPORT:
57402ac6454SAndrew Thompson 			size = sc->sc_fsize;
57502ac6454SAndrew Thompson 			id = sc->sc_fid;
57602ac6454SAndrew Thompson 			break;
57702ac6454SAndrew Thompson 		default:
57802ac6454SAndrew Thompson 			return (EINVAL);
57902ac6454SAndrew Thompson 		}
58002ac6454SAndrew Thompson 		error = uhid_set_report(sc, ugd->ugd_report_type, id,
58102ac6454SAndrew Thompson 		    NULL, ugd->ugd_data, size);
58202ac6454SAndrew Thompson 		break;
58302ac6454SAndrew Thompson 
58402ac6454SAndrew Thompson 	case USB_GET_REPORT_ID:
58502ac6454SAndrew Thompson 		*(int *)addr = 0;	/* XXX: we only support reportid 0? */
58602ac6454SAndrew Thompson 		break;
58702ac6454SAndrew Thompson 
58802ac6454SAndrew Thompson 	default:
58902ac6454SAndrew Thompson 		error = EINVAL;
59002ac6454SAndrew Thompson 		break;
59102ac6454SAndrew Thompson 	}
59202ac6454SAndrew Thompson 	return (error);
59302ac6454SAndrew Thompson }
59402ac6454SAndrew Thompson 
59502ac6454SAndrew Thompson static int
59602ac6454SAndrew Thompson uhid_probe(device_t dev)
59702ac6454SAndrew Thompson {
598760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
59902ac6454SAndrew Thompson 
60002ac6454SAndrew Thompson 	DPRINTFN(11, "\n");
60102ac6454SAndrew Thompson 
602f29a0724SAndrew Thompson 	if (uaa->usb_mode != USB_MODE_HOST) {
60302ac6454SAndrew Thompson 		return (ENXIO);
60402ac6454SAndrew Thompson 	}
60502ac6454SAndrew Thompson 	if (uaa->use_generic == 0) {
60602ac6454SAndrew Thompson 		/* give Mouse and Keyboard drivers a try first */
60702ac6454SAndrew Thompson 		return (ENXIO);
60802ac6454SAndrew Thompson 	}
60902ac6454SAndrew Thompson 	if (uaa->info.bInterfaceClass != UICLASS_HID) {
61002ac6454SAndrew Thompson 
61102ac6454SAndrew Thompson 		/* the Xbox 360 gamepad doesn't use the HID class */
61202ac6454SAndrew Thompson 
61302ac6454SAndrew Thompson 		if ((uaa->info.bInterfaceClass != UICLASS_VENDOR) ||
61402ac6454SAndrew Thompson 		    (uaa->info.bInterfaceSubClass != UISUBCLASS_XBOX360_CONTROLLER) ||
61502ac6454SAndrew Thompson 		    (uaa->info.bInterfaceProtocol != UIPROTO_XBOX360_GAMEPAD)) {
61602ac6454SAndrew Thompson 			return (ENXIO);
61702ac6454SAndrew Thompson 		}
61802ac6454SAndrew Thompson 	}
619a593f6b8SAndrew Thompson 	if (usb_test_quirk(uaa, UQ_HID_IGNORE)) {
62002ac6454SAndrew Thompson 		return (ENXIO);
62102ac6454SAndrew Thompson 	}
62202ac6454SAndrew Thompson 	return (0);
62302ac6454SAndrew Thompson }
62402ac6454SAndrew Thompson 
62502ac6454SAndrew Thompson static int
62602ac6454SAndrew Thompson uhid_attach(device_t dev)
62702ac6454SAndrew Thompson {
628760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
62902ac6454SAndrew Thompson 	struct uhid_softc *sc = device_get_softc(dev);
63002ac6454SAndrew Thompson 	int unit = device_get_unit(dev);
63102ac6454SAndrew Thompson 	int error = 0;
63202ac6454SAndrew Thompson 
63302ac6454SAndrew Thompson 	DPRINTFN(10, "sc=%p\n", sc);
63402ac6454SAndrew Thompson 
635a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
63602ac6454SAndrew Thompson 
63702ac6454SAndrew Thompson 	mtx_init(&sc->sc_mtx, "uhid lock", NULL, MTX_DEF | MTX_RECURSE);
63802ac6454SAndrew Thompson 
63902ac6454SAndrew Thompson 	sc->sc_udev = uaa->device;
64002ac6454SAndrew Thompson 
64102ac6454SAndrew Thompson 	sc->sc_iface_no = uaa->info.bIfaceNum;
64202ac6454SAndrew Thompson 	sc->sc_iface_index = uaa->info.bIfaceIndex;
64302ac6454SAndrew Thompson 
644a593f6b8SAndrew Thompson 	error = usbd_transfer_setup(uaa->device,
64502ac6454SAndrew Thompson 	    &uaa->info.bIfaceIndex, sc->sc_xfer, uhid_config,
64602ac6454SAndrew Thompson 	    UHID_N_TRANSFER, sc, &sc->sc_mtx);
64702ac6454SAndrew Thompson 
64802ac6454SAndrew Thompson 	if (error) {
649a593f6b8SAndrew Thompson 		DPRINTF("error=%s\n", usbd_errstr(error));
65002ac6454SAndrew Thompson 		goto detach;
65102ac6454SAndrew Thompson 	}
65202ac6454SAndrew Thompson 	if (uaa->info.idVendor == USB_VENDOR_WACOM) {
65302ac6454SAndrew Thompson 
65402ac6454SAndrew Thompson 		/* the report descriptor for the Wacom Graphire is broken */
65502ac6454SAndrew Thompson 
65602ac6454SAndrew Thompson 		if (uaa->info.idProduct == USB_PRODUCT_WACOM_GRAPHIRE) {
65702ac6454SAndrew Thompson 
65802ac6454SAndrew Thompson 			sc->sc_repdesc_size = sizeof(uhid_graphire_report_descr);
65902ac6454SAndrew Thompson 			sc->sc_repdesc_ptr = USB_ADD_BYTES(uhid_graphire_report_descr, 0);
66002ac6454SAndrew Thompson 			sc->sc_flags |= UHID_FLAG_STATIC_DESC;
66102ac6454SAndrew Thompson 
66202ac6454SAndrew Thompson 		} else if (uaa->info.idProduct == USB_PRODUCT_WACOM_GRAPHIRE3_4X5) {
66302ac6454SAndrew Thompson 
66402ac6454SAndrew Thompson 			static uint8_t reportbuf[] = {2, 2, 2};
66502ac6454SAndrew Thompson 
66602ac6454SAndrew Thompson 			/*
66702ac6454SAndrew Thompson 			 * The Graphire3 needs 0x0202 to be written to
66802ac6454SAndrew Thompson 			 * feature report ID 2 before it'll start
66902ac6454SAndrew Thompson 			 * returning digitizer data.
67002ac6454SAndrew Thompson 			 */
671a593f6b8SAndrew Thompson 			error = usbd_req_set_report(uaa->device, NULL,
672296ade60SAndrew Thompson 			    reportbuf, sizeof(reportbuf),
67302ac6454SAndrew Thompson 			    uaa->info.bIfaceIndex, UHID_FEATURE_REPORT, 2);
67402ac6454SAndrew Thompson 
67502ac6454SAndrew Thompson 			if (error) {
67602ac6454SAndrew Thompson 				DPRINTF("set report failed, error=%s (ignored)\n",
677a593f6b8SAndrew Thompson 				    usbd_errstr(error));
67802ac6454SAndrew Thompson 			}
67902ac6454SAndrew Thompson 			sc->sc_repdesc_size = sizeof(uhid_graphire3_4x5_report_descr);
68002ac6454SAndrew Thompson 			sc->sc_repdesc_ptr = USB_ADD_BYTES(uhid_graphire3_4x5_report_descr, 0);
68102ac6454SAndrew Thompson 			sc->sc_flags |= UHID_FLAG_STATIC_DESC;
68202ac6454SAndrew Thompson 		}
68302ac6454SAndrew Thompson 	} else if ((uaa->info.bInterfaceClass == UICLASS_VENDOR) &&
68402ac6454SAndrew Thompson 		    (uaa->info.bInterfaceSubClass == UISUBCLASS_XBOX360_CONTROLLER) &&
68502ac6454SAndrew Thompson 	    (uaa->info.bInterfaceProtocol == UIPROTO_XBOX360_GAMEPAD)) {
68602ac6454SAndrew Thompson 
68702ac6454SAndrew Thompson 		/* the Xbox 360 gamepad has no report descriptor */
68802ac6454SAndrew Thompson 		sc->sc_repdesc_size = sizeof(uhid_xb360gp_report_descr);
68902ac6454SAndrew Thompson 		sc->sc_repdesc_ptr = USB_ADD_BYTES(uhid_xb360gp_report_descr, 0);
69002ac6454SAndrew Thompson 		sc->sc_flags |= UHID_FLAG_STATIC_DESC;
69102ac6454SAndrew Thompson 	}
69202ac6454SAndrew Thompson 	if (sc->sc_repdesc_ptr == NULL) {
69302ac6454SAndrew Thompson 
694a593f6b8SAndrew Thompson 		error = usbd_req_get_hid_desc(uaa->device, NULL,
695296ade60SAndrew Thompson 		    &sc->sc_repdesc_ptr, &sc->sc_repdesc_size,
696296ade60SAndrew Thompson 		    M_USBDEV, uaa->info.bIfaceIndex);
69702ac6454SAndrew Thompson 
69802ac6454SAndrew Thompson 		if (error) {
69902ac6454SAndrew Thompson 			device_printf(dev, "no report descriptor\n");
70002ac6454SAndrew Thompson 			goto detach;
70102ac6454SAndrew Thompson 		}
70202ac6454SAndrew Thompson 	}
703a593f6b8SAndrew Thompson 	error = usbd_req_set_idle(uaa->device, NULL,
70402ac6454SAndrew Thompson 	    uaa->info.bIfaceIndex, 0, 0);
70502ac6454SAndrew Thompson 
70602ac6454SAndrew Thompson 	if (error) {
70702ac6454SAndrew Thompson 		DPRINTF("set idle failed, error=%s (ignored)\n",
708a593f6b8SAndrew Thompson 		    usbd_errstr(error));
70902ac6454SAndrew Thompson 	}
71002ac6454SAndrew Thompson 	sc->sc_isize = hid_report_size
71102ac6454SAndrew Thompson 	    (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_input, &sc->sc_iid);
71202ac6454SAndrew Thompson 
71302ac6454SAndrew Thompson 	sc->sc_osize = hid_report_size
71402ac6454SAndrew Thompson 	    (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_output, &sc->sc_oid);
71502ac6454SAndrew Thompson 
71602ac6454SAndrew Thompson 	sc->sc_fsize = hid_report_size
71702ac6454SAndrew Thompson 	    (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_feature, &sc->sc_fid);
71802ac6454SAndrew Thompson 
71902ac6454SAndrew Thompson 	if (sc->sc_isize > UHID_BSIZE) {
72002ac6454SAndrew Thompson 		DPRINTF("input size is too large, "
72102ac6454SAndrew Thompson 		    "%d bytes (truncating)\n",
72202ac6454SAndrew Thompson 		    sc->sc_isize);
72302ac6454SAndrew Thompson 		sc->sc_isize = UHID_BSIZE;
72402ac6454SAndrew Thompson 	}
72502ac6454SAndrew Thompson 	if (sc->sc_osize > UHID_BSIZE) {
72602ac6454SAndrew Thompson 		DPRINTF("output size is too large, "
72702ac6454SAndrew Thompson 		    "%d bytes (truncating)\n",
72802ac6454SAndrew Thompson 		    sc->sc_osize);
72902ac6454SAndrew Thompson 		sc->sc_osize = UHID_BSIZE;
73002ac6454SAndrew Thompson 	}
73102ac6454SAndrew Thompson 	if (sc->sc_fsize > UHID_BSIZE) {
73202ac6454SAndrew Thompson 		DPRINTF("feature size is too large, "
73302ac6454SAndrew Thompson 		    "%d bytes (truncating)\n",
73402ac6454SAndrew Thompson 		    sc->sc_fsize);
73502ac6454SAndrew Thompson 		sc->sc_fsize = UHID_BSIZE;
73602ac6454SAndrew Thompson 	}
73702ac6454SAndrew Thompson 
738a593f6b8SAndrew Thompson 	error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
73902ac6454SAndrew Thompson 	    &uhid_fifo_methods, &sc->sc_fifo,
740ee3e3ff5SAndrew Thompson 	    unit, 0 - 1, uaa->info.bIfaceIndex,
741ee3e3ff5SAndrew Thompson 	    UID_ROOT, GID_OPERATOR, 0644);
74202ac6454SAndrew Thompson 	if (error) {
74302ac6454SAndrew Thompson 		goto detach;
74402ac6454SAndrew Thompson 	}
74502ac6454SAndrew Thompson 	return (0);			/* success */
74602ac6454SAndrew Thompson 
74702ac6454SAndrew Thompson detach:
74802ac6454SAndrew Thompson 	uhid_detach(dev);
74902ac6454SAndrew Thompson 	return (ENOMEM);
75002ac6454SAndrew Thompson }
75102ac6454SAndrew Thompson 
75202ac6454SAndrew Thompson static int
75302ac6454SAndrew Thompson uhid_detach(device_t dev)
75402ac6454SAndrew Thompson {
75502ac6454SAndrew Thompson 	struct uhid_softc *sc = device_get_softc(dev);
75602ac6454SAndrew Thompson 
757a593f6b8SAndrew Thompson 	usb_fifo_detach(&sc->sc_fifo);
75802ac6454SAndrew Thompson 
759a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, UHID_N_TRANSFER);
76002ac6454SAndrew Thompson 
76102ac6454SAndrew Thompson 	if (sc->sc_repdesc_ptr) {
76202ac6454SAndrew Thompson 		if (!(sc->sc_flags & UHID_FLAG_STATIC_DESC)) {
76302ac6454SAndrew Thompson 			free(sc->sc_repdesc_ptr, M_USBDEV);
76402ac6454SAndrew Thompson 		}
76502ac6454SAndrew Thompson 	}
76602ac6454SAndrew Thompson 	mtx_destroy(&sc->sc_mtx);
76702ac6454SAndrew Thompson 
76802ac6454SAndrew Thompson 	return (0);
76902ac6454SAndrew Thompson }
77002ac6454SAndrew Thompson 
77102ac6454SAndrew Thompson static devclass_t uhid_devclass;
77202ac6454SAndrew Thompson 
77302ac6454SAndrew Thompson static device_method_t uhid_methods[] = {
77402ac6454SAndrew Thompson 	DEVMETHOD(device_probe, uhid_probe),
77502ac6454SAndrew Thompson 	DEVMETHOD(device_attach, uhid_attach),
77602ac6454SAndrew Thompson 	DEVMETHOD(device_detach, uhid_detach),
77702ac6454SAndrew Thompson 	{0, 0}
77802ac6454SAndrew Thompson };
77902ac6454SAndrew Thompson 
78002ac6454SAndrew Thompson static driver_t uhid_driver = {
78102ac6454SAndrew Thompson 	.name = "uhid",
78202ac6454SAndrew Thompson 	.methods = uhid_methods,
78302ac6454SAndrew Thompson 	.size = sizeof(struct uhid_softc),
78402ac6454SAndrew Thompson };
78502ac6454SAndrew Thompson 
7869aef556dSAndrew Thompson DRIVER_MODULE(uhid, uhub, uhid_driver, uhid_devclass, NULL, 0);
78702ac6454SAndrew Thompson MODULE_DEPEND(uhid, usb, 1, 1, 1);
788