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 /*-
9b61a5730SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
10718cf2ccSPedro F. Giffuni *
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
449be6b22dSVladimir Kondratyev #include "opt_hid.h"
459be6b22dSVladimir Kondratyev
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
67eead9017SVladimir Kondratyev #include <dev/hid/hid.h>
68eead9017SVladimir Kondratyev
6902ac6454SAndrew Thompson #include "usbdevs.h"
7002ac6454SAndrew Thompson #include <dev/usb/usb.h>
71ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
72ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h>
7302ac6454SAndrew Thompson #include <dev/usb/usbhid.h>
7402ac6454SAndrew Thompson #include <dev/usb/usb_ioctl.h>
75c77bfaa7SHans Petter Selasky #include <dev/usb/usb_generic.h>
7602ac6454SAndrew Thompson
7702ac6454SAndrew Thompson #define USB_DEBUG_VAR uhid_debug
7802ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
7902ac6454SAndrew Thompson
8002ac6454SAndrew Thompson #include <dev/usb/input/usb_rdesc.h>
8102ac6454SAndrew Thompson #include <dev/usb/quirk/usb_quirk.h>
8202ac6454SAndrew Thompson
83b850ecc1SAndrew Thompson #ifdef USB_DEBUG
8402ac6454SAndrew Thompson static int uhid_debug = 0;
8502ac6454SAndrew Thompson
86f8d2b1f3SPawel Biernacki static SYSCTL_NODE(_hw_usb, OID_AUTO, uhid, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
87f8d2b1f3SPawel Biernacki "USB uhid");
88ece4b0bdSHans Petter Selasky SYSCTL_INT(_hw_usb_uhid, OID_AUTO, debug, CTLFLAG_RWTUN,
8902ac6454SAndrew Thompson &uhid_debug, 0, "Debug level");
9002ac6454SAndrew Thompson #endif
9102ac6454SAndrew Thompson
9202ac6454SAndrew Thompson #define UHID_BSIZE 1024 /* bytes, buffer size */
9302ac6454SAndrew Thompson #define UHID_FRAME_NUM 50 /* bytes, frame number */
9402ac6454SAndrew Thompson
9502ac6454SAndrew Thompson enum {
963a38def3SAlexander Motin UHID_INTR_DT_WR,
9702ac6454SAndrew Thompson UHID_INTR_DT_RD,
9802ac6454SAndrew Thompson UHID_CTRL_DT_WR,
9902ac6454SAndrew Thompson UHID_CTRL_DT_RD,
10002ac6454SAndrew Thompson UHID_N_TRANSFER,
10102ac6454SAndrew Thompson };
10202ac6454SAndrew Thompson
10302ac6454SAndrew Thompson struct uhid_softc {
104760bc48eSAndrew Thompson struct usb_fifo_sc sc_fifo;
10502ac6454SAndrew Thompson struct mtx sc_mtx;
10602ac6454SAndrew Thompson
107760bc48eSAndrew Thompson struct usb_xfer *sc_xfer[UHID_N_TRANSFER];
108760bc48eSAndrew Thompson struct usb_device *sc_udev;
10902ac6454SAndrew Thompson void *sc_repdesc_ptr;
11002ac6454SAndrew Thompson
11102ac6454SAndrew Thompson uint32_t sc_isize;
11202ac6454SAndrew Thompson uint32_t sc_osize;
11302ac6454SAndrew Thompson uint32_t sc_fsize;
11402ac6454SAndrew Thompson
11502ac6454SAndrew Thompson uint16_t sc_repdesc_size;
11602ac6454SAndrew Thompson
11702ac6454SAndrew Thompson uint8_t sc_iface_no;
11802ac6454SAndrew Thompson uint8_t sc_iface_index;
11902ac6454SAndrew Thompson uint8_t sc_iid;
12002ac6454SAndrew Thompson uint8_t sc_oid;
12102ac6454SAndrew Thompson uint8_t sc_fid;
12202ac6454SAndrew Thompson uint8_t sc_flags;
12302ac6454SAndrew Thompson #define UHID_FLAG_IMMED 0x01 /* set if read should be immediate */
12402ac6454SAndrew Thompson #define UHID_FLAG_STATIC_DESC 0x04 /* set if report descriptors are
12502ac6454SAndrew Thompson * static */
12602ac6454SAndrew Thompson };
12702ac6454SAndrew Thompson
12802ac6454SAndrew Thompson static const uint8_t uhid_xb360gp_report_descr[] = {UHID_XB360GP_REPORT_DESCR()};
12902ac6454SAndrew Thompson static const uint8_t uhid_graphire_report_descr[] = {UHID_GRAPHIRE_REPORT_DESCR()};
13002ac6454SAndrew Thompson static const uint8_t uhid_graphire3_4x5_report_descr[] = {UHID_GRAPHIRE3_4X5_REPORT_DESCR()};
13102ac6454SAndrew Thompson
13202ac6454SAndrew Thompson /* prototypes */
13302ac6454SAndrew Thompson
13402ac6454SAndrew Thompson static device_probe_t uhid_probe;
13502ac6454SAndrew Thompson static device_attach_t uhid_attach;
13602ac6454SAndrew Thompson static device_detach_t uhid_detach;
13702ac6454SAndrew Thompson
1383a38def3SAlexander Motin static usb_callback_t uhid_intr_write_callback;
1393a38def3SAlexander Motin static usb_callback_t uhid_intr_read_callback;
140e0a69b51SAndrew Thompson static usb_callback_t uhid_write_callback;
141e0a69b51SAndrew Thompson static usb_callback_t uhid_read_callback;
14202ac6454SAndrew Thompson
143e0a69b51SAndrew Thompson static usb_fifo_cmd_t uhid_start_read;
144e0a69b51SAndrew Thompson static usb_fifo_cmd_t uhid_stop_read;
145e0a69b51SAndrew Thompson static usb_fifo_cmd_t uhid_start_write;
146e0a69b51SAndrew Thompson static usb_fifo_cmd_t uhid_stop_write;
147e0a69b51SAndrew Thompson static usb_fifo_open_t uhid_open;
148e0a69b51SAndrew Thompson static usb_fifo_close_t uhid_close;
149e0a69b51SAndrew Thompson static usb_fifo_ioctl_t uhid_ioctl;
150c77bfaa7SHans Petter Selasky static usb_fifo_ioctl_t uhid_ioctl_post;
15102ac6454SAndrew Thompson
152760bc48eSAndrew Thompson static struct usb_fifo_methods uhid_fifo_methods = {
15302ac6454SAndrew Thompson .f_open = &uhid_open,
15402ac6454SAndrew Thompson .f_close = &uhid_close,
15502ac6454SAndrew Thompson .f_ioctl = &uhid_ioctl,
156c77bfaa7SHans Petter Selasky .f_ioctl_post = &uhid_ioctl_post,
15702ac6454SAndrew Thompson .f_start_read = &uhid_start_read,
15802ac6454SAndrew Thompson .f_stop_read = &uhid_stop_read,
15902ac6454SAndrew Thompson .f_start_write = &uhid_start_write,
16002ac6454SAndrew Thompson .f_stop_write = &uhid_stop_write,
16102ac6454SAndrew Thompson .basename[0] = "uhid",
16202ac6454SAndrew Thompson };
16302ac6454SAndrew Thompson
16402ac6454SAndrew Thompson static void
uhid_intr_write_callback(struct usb_xfer * xfer,usb_error_t error)1653a38def3SAlexander Motin uhid_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
1663a38def3SAlexander Motin {
1673a38def3SAlexander Motin struct uhid_softc *sc = usbd_xfer_softc(xfer);
1683a38def3SAlexander Motin struct usb_page_cache *pc;
1693a38def3SAlexander Motin int actlen;
1703a38def3SAlexander Motin
1713a38def3SAlexander Motin switch (USB_GET_STATE(xfer)) {
1723a38def3SAlexander Motin case USB_ST_TRANSFERRED:
1733a38def3SAlexander Motin case USB_ST_SETUP:
1743a38def3SAlexander Motin tr_setup:
1753a38def3SAlexander Motin pc = usbd_xfer_get_frame(xfer, 0);
1763a38def3SAlexander Motin if (usb_fifo_get_data(sc->sc_fifo.fp[USB_FIFO_TX], pc,
1773a38def3SAlexander Motin 0, usbd_xfer_max_len(xfer), &actlen, 0)) {
1783a38def3SAlexander Motin usbd_xfer_set_frame_len(xfer, 0, actlen);
1793a38def3SAlexander Motin usbd_transfer_submit(xfer);
1803a38def3SAlexander Motin }
1813a38def3SAlexander Motin return;
1823a38def3SAlexander Motin
1833a38def3SAlexander Motin default: /* Error */
1843a38def3SAlexander Motin if (error != USB_ERR_CANCELLED) {
1853a38def3SAlexander Motin /* try to clear stall first */
1863a38def3SAlexander Motin usbd_xfer_set_stall(xfer);
1873a38def3SAlexander Motin goto tr_setup;
1883a38def3SAlexander Motin }
1893a38def3SAlexander Motin return;
1903a38def3SAlexander Motin }
1913a38def3SAlexander Motin }
1923a38def3SAlexander Motin
1933a38def3SAlexander Motin static void
uhid_intr_read_callback(struct usb_xfer * xfer,usb_error_t error)1943a38def3SAlexander Motin uhid_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
19502ac6454SAndrew Thompson {
196ed6d949aSAndrew Thompson struct uhid_softc *sc = usbd_xfer_softc(xfer);
197ed6d949aSAndrew Thompson struct usb_page_cache *pc;
198ed6d949aSAndrew Thompson int actlen;
199ed6d949aSAndrew Thompson
200ed6d949aSAndrew Thompson usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
20102ac6454SAndrew Thompson
20202ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) {
20302ac6454SAndrew Thompson case USB_ST_TRANSFERRED:
20402ac6454SAndrew Thompson DPRINTF("transferred!\n");
20502ac6454SAndrew Thompson
206ed6d949aSAndrew Thompson pc = usbd_xfer_get_frame(xfer, 0);
2070ca80d71SAndrew Thompson
2080ca80d71SAndrew Thompson /*
2090ca80d71SAndrew Thompson * If the ID byte is non zero we allow descriptors
2100ca80d71SAndrew Thompson * having multiple sizes:
2110ca80d71SAndrew Thompson */
2126d917491SHans Petter Selasky if ((actlen >= (int)sc->sc_isize) ||
2130ca80d71SAndrew Thompson ((actlen > 0) && (sc->sc_iid != 0))) {
2140ca80d71SAndrew Thompson /* limit report length to the maximum */
2156d917491SHans Petter Selasky if (actlen > (int)sc->sc_isize)
2160ca80d71SAndrew Thompson actlen = sc->sc_isize;
217ed6d949aSAndrew Thompson usb_fifo_put_data(sc->sc_fifo.fp[USB_FIFO_RX], pc,
2180ca80d71SAndrew Thompson 0, actlen, 1);
219b6f61525SHans Petter Selasky
220b6f61525SHans Petter Selasky /*
221b6f61525SHans Petter Selasky * Do not do read-ahead, because this may lead
222b6f61525SHans Petter Selasky * to data loss!
223b6f61525SHans Petter Selasky */
224b6f61525SHans Petter Selasky return;
22502ac6454SAndrew Thompson } else {
22602ac6454SAndrew Thompson /* ignore it */
2270ca80d71SAndrew Thompson DPRINTF("ignored transfer, %d bytes\n", actlen);
22802ac6454SAndrew Thompson }
22902ac6454SAndrew Thompson
23002ac6454SAndrew Thompson case USB_ST_SETUP:
23102ac6454SAndrew Thompson re_submit:
232a593f6b8SAndrew Thompson if (usb_fifo_put_bytes_max(
23302ac6454SAndrew Thompson sc->sc_fifo.fp[USB_FIFO_RX]) != 0) {
234ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, sc->sc_isize);
235a593f6b8SAndrew Thompson usbd_transfer_submit(xfer);
23602ac6454SAndrew Thompson }
23702ac6454SAndrew Thompson return;
23802ac6454SAndrew Thompson
23902ac6454SAndrew Thompson default: /* Error */
240ed6d949aSAndrew Thompson if (error != USB_ERR_CANCELLED) {
24102ac6454SAndrew Thompson /* try to clear stall first */
242ed6d949aSAndrew Thompson usbd_xfer_set_stall(xfer);
24302ac6454SAndrew Thompson goto re_submit;
24402ac6454SAndrew Thompson }
24502ac6454SAndrew Thompson return;
24602ac6454SAndrew Thompson }
24702ac6454SAndrew Thompson }
24802ac6454SAndrew Thompson
24902ac6454SAndrew Thompson static void
uhid_fill_set_report(struct usb_device_request * req,uint8_t iface_no,uint8_t type,uint8_t id,uint16_t size)250760bc48eSAndrew Thompson uhid_fill_set_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_WRITE_CLASS_INTERFACE;
25402ac6454SAndrew Thompson req->bRequest = UR_SET_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
uhid_fill_get_report(struct usb_device_request * req,uint8_t iface_no,uint8_t type,uint8_t id,uint16_t size)262760bc48eSAndrew Thompson uhid_fill_get_report(struct usb_device_request *req, uint8_t iface_no,
26302ac6454SAndrew Thompson uint8_t type, uint8_t id, uint16_t size)
26402ac6454SAndrew Thompson {
26502ac6454SAndrew Thompson req->bmRequestType = UT_READ_CLASS_INTERFACE;
26602ac6454SAndrew Thompson req->bRequest = UR_GET_REPORT;
26702ac6454SAndrew Thompson USETW2(req->wValue, type, id);
26802ac6454SAndrew Thompson req->wIndex[0] = iface_no;
26902ac6454SAndrew Thompson req->wIndex[1] = 0;
27002ac6454SAndrew Thompson USETW(req->wLength, size);
27102ac6454SAndrew Thompson }
27202ac6454SAndrew Thompson
27302ac6454SAndrew Thompson static void
uhid_write_callback(struct usb_xfer * xfer,usb_error_t error)274ed6d949aSAndrew Thompson uhid_write_callback(struct usb_xfer *xfer, usb_error_t error)
27502ac6454SAndrew Thompson {
276ed6d949aSAndrew Thompson struct uhid_softc *sc = usbd_xfer_softc(xfer);
277760bc48eSAndrew Thompson struct usb_device_request req;
278ed6d949aSAndrew Thompson struct usb_page_cache *pc;
27902ac6454SAndrew Thompson uint32_t size = sc->sc_osize;
28002ac6454SAndrew Thompson uint32_t actlen;
28102ac6454SAndrew Thompson uint8_t id;
28202ac6454SAndrew Thompson
28302ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) {
28402ac6454SAndrew Thompson case USB_ST_TRANSFERRED:
28502ac6454SAndrew Thompson case USB_ST_SETUP:
28602ac6454SAndrew Thompson /* try to extract the ID byte */
28702ac6454SAndrew Thompson if (sc->sc_oid) {
288ed6d949aSAndrew Thompson pc = usbd_xfer_get_frame(xfer, 0);
289ed6d949aSAndrew Thompson if (usb_fifo_get_data(sc->sc_fifo.fp[USB_FIFO_TX], pc,
29002ac6454SAndrew Thompson 0, 1, &actlen, 0)) {
29102ac6454SAndrew Thompson if (actlen != 1) {
29202ac6454SAndrew Thompson goto tr_error;
29302ac6454SAndrew Thompson }
294ed6d949aSAndrew Thompson usbd_copy_out(pc, 0, &id, 1);
29502ac6454SAndrew Thompson
29602ac6454SAndrew Thompson } else {
29702ac6454SAndrew Thompson return;
29802ac6454SAndrew Thompson }
29902ac6454SAndrew Thompson if (size) {
30002ac6454SAndrew Thompson size--;
30102ac6454SAndrew Thompson }
30202ac6454SAndrew Thompson } else {
30302ac6454SAndrew Thompson id = 0;
30402ac6454SAndrew Thompson }
30502ac6454SAndrew Thompson
306ed6d949aSAndrew Thompson pc = usbd_xfer_get_frame(xfer, 1);
307ed6d949aSAndrew Thompson if (usb_fifo_get_data(sc->sc_fifo.fp[USB_FIFO_TX], pc,
30802ac6454SAndrew Thompson 0, UHID_BSIZE, &actlen, 1)) {
30902ac6454SAndrew Thompson if (actlen != size) {
31002ac6454SAndrew Thompson goto tr_error;
31102ac6454SAndrew Thompson }
31202ac6454SAndrew Thompson uhid_fill_set_report
31302ac6454SAndrew Thompson (&req, sc->sc_iface_no,
31402ac6454SAndrew Thompson UHID_OUTPUT_REPORT, id, size);
31502ac6454SAndrew Thompson
316ed6d949aSAndrew Thompson pc = usbd_xfer_get_frame(xfer, 0);
317ed6d949aSAndrew Thompson usbd_copy_in(pc, 0, &req, sizeof(req));
31802ac6454SAndrew Thompson
319ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
320ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 1, size);
321ed6d949aSAndrew Thompson usbd_xfer_set_frames(xfer, size ? 2 : 1);
322a593f6b8SAndrew Thompson usbd_transfer_submit(xfer);
32302ac6454SAndrew Thompson }
32402ac6454SAndrew Thompson return;
32502ac6454SAndrew Thompson
32602ac6454SAndrew Thompson default:
32702ac6454SAndrew Thompson tr_error:
32802ac6454SAndrew Thompson /* bomb out */
329a593f6b8SAndrew Thompson usb_fifo_get_data_error(sc->sc_fifo.fp[USB_FIFO_TX]);
33002ac6454SAndrew Thompson return;
33102ac6454SAndrew Thompson }
33202ac6454SAndrew Thompson }
33302ac6454SAndrew Thompson
33402ac6454SAndrew Thompson static void
uhid_read_callback(struct usb_xfer * xfer,usb_error_t error)335ed6d949aSAndrew Thompson uhid_read_callback(struct usb_xfer *xfer, usb_error_t error)
33602ac6454SAndrew Thompson {
337ed6d949aSAndrew Thompson struct uhid_softc *sc = usbd_xfer_softc(xfer);
338760bc48eSAndrew Thompson struct usb_device_request req;
339ed6d949aSAndrew Thompson struct usb_page_cache *pc;
340ed6d949aSAndrew Thompson
341ed6d949aSAndrew Thompson pc = usbd_xfer_get_frame(xfer, 0);
34202ac6454SAndrew Thompson
34302ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) {
34402ac6454SAndrew Thompson case USB_ST_TRANSFERRED:
345ed6d949aSAndrew Thompson usb_fifo_put_data(sc->sc_fifo.fp[USB_FIFO_RX], pc, sizeof(req),
346ed6d949aSAndrew Thompson sc->sc_isize, 1);
34702ac6454SAndrew Thompson return;
34802ac6454SAndrew Thompson
34902ac6454SAndrew Thompson case USB_ST_SETUP:
35002ac6454SAndrew Thompson
351a593f6b8SAndrew Thompson if (usb_fifo_put_bytes_max(sc->sc_fifo.fp[USB_FIFO_RX]) > 0) {
35202ac6454SAndrew Thompson uhid_fill_get_report
35302ac6454SAndrew Thompson (&req, sc->sc_iface_no, UHID_INPUT_REPORT,
35402ac6454SAndrew Thompson sc->sc_iid, sc->sc_isize);
35502ac6454SAndrew Thompson
356ed6d949aSAndrew Thompson usbd_copy_in(pc, 0, &req, sizeof(req));
35702ac6454SAndrew Thompson
358ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
359ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 1, sc->sc_isize);
360ed6d949aSAndrew Thompson usbd_xfer_set_frames(xfer, sc->sc_isize ? 2 : 1);
361a593f6b8SAndrew Thompson usbd_transfer_submit(xfer);
36202ac6454SAndrew Thompson }
36302ac6454SAndrew Thompson return;
36402ac6454SAndrew Thompson
36502ac6454SAndrew Thompson default: /* Error */
36602ac6454SAndrew Thompson /* bomb out */
367a593f6b8SAndrew Thompson usb_fifo_put_data_error(sc->sc_fifo.fp[USB_FIFO_RX]);
36802ac6454SAndrew Thompson return;
36902ac6454SAndrew Thompson }
37002ac6454SAndrew Thompson }
37102ac6454SAndrew Thompson
372760bc48eSAndrew Thompson static const struct usb_config uhid_config[UHID_N_TRANSFER] = {
3733a38def3SAlexander Motin [UHID_INTR_DT_WR] = {
3743a38def3SAlexander Motin .type = UE_INTERRUPT,
3753a38def3SAlexander Motin .endpoint = UE_ADDR_ANY,
3763a38def3SAlexander Motin .direction = UE_DIR_OUT,
3773dfe1a8eSAlexander Motin .flags = {.pipe_bof = 1,.no_pipe_ok = 1, },
3783a38def3SAlexander Motin .bufsize = UHID_BSIZE,
3793a38def3SAlexander Motin .callback = &uhid_intr_write_callback,
3803a38def3SAlexander Motin },
3813a38def3SAlexander Motin
38202ac6454SAndrew Thompson [UHID_INTR_DT_RD] = {
38302ac6454SAndrew Thompson .type = UE_INTERRUPT,
38402ac6454SAndrew Thompson .endpoint = UE_ADDR_ANY,
38502ac6454SAndrew Thompson .direction = UE_DIR_IN,
3864eae601eSAndrew Thompson .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
3874eae601eSAndrew Thompson .bufsize = UHID_BSIZE,
3883a38def3SAlexander Motin .callback = &uhid_intr_read_callback,
38902ac6454SAndrew Thompson },
39002ac6454SAndrew Thompson
39102ac6454SAndrew Thompson [UHID_CTRL_DT_WR] = {
39202ac6454SAndrew Thompson .type = UE_CONTROL,
39302ac6454SAndrew Thompson .endpoint = 0x00, /* Control pipe */
39402ac6454SAndrew Thompson .direction = UE_DIR_ANY,
395760bc48eSAndrew Thompson .bufsize = sizeof(struct usb_device_request) + UHID_BSIZE,
3964eae601eSAndrew Thompson .callback = &uhid_write_callback,
3974eae601eSAndrew Thompson .timeout = 1000, /* 1 second */
39802ac6454SAndrew Thompson },
39902ac6454SAndrew Thompson
40002ac6454SAndrew Thompson [UHID_CTRL_DT_RD] = {
40102ac6454SAndrew Thompson .type = UE_CONTROL,
40202ac6454SAndrew Thompson .endpoint = 0x00, /* Control pipe */
40302ac6454SAndrew Thompson .direction = UE_DIR_ANY,
404760bc48eSAndrew Thompson .bufsize = sizeof(struct usb_device_request) + UHID_BSIZE,
4054eae601eSAndrew Thompson .callback = &uhid_read_callback,
4064eae601eSAndrew Thompson .timeout = 1000, /* 1 second */
40702ac6454SAndrew Thompson },
40802ac6454SAndrew Thompson };
40902ac6454SAndrew Thompson
41002ac6454SAndrew Thompson static void
uhid_start_read(struct usb_fifo * fifo)411760bc48eSAndrew Thompson uhid_start_read(struct usb_fifo *fifo)
41202ac6454SAndrew Thompson {
413ed6d949aSAndrew Thompson struct uhid_softc *sc = usb_fifo_softc(fifo);
41402ac6454SAndrew Thompson
41502ac6454SAndrew Thompson if (sc->sc_flags & UHID_FLAG_IMMED) {
416a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[UHID_CTRL_DT_RD]);
41702ac6454SAndrew Thompson } else {
418a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[UHID_INTR_DT_RD]);
41902ac6454SAndrew Thompson }
42002ac6454SAndrew Thompson }
42102ac6454SAndrew Thompson
42202ac6454SAndrew Thompson static void
uhid_stop_read(struct usb_fifo * fifo)423760bc48eSAndrew Thompson uhid_stop_read(struct usb_fifo *fifo)
42402ac6454SAndrew Thompson {
425ed6d949aSAndrew Thompson struct uhid_softc *sc = usb_fifo_softc(fifo);
42602ac6454SAndrew Thompson
427a593f6b8SAndrew Thompson usbd_transfer_stop(sc->sc_xfer[UHID_CTRL_DT_RD]);
428a593f6b8SAndrew Thompson usbd_transfer_stop(sc->sc_xfer[UHID_INTR_DT_RD]);
42902ac6454SAndrew Thompson }
43002ac6454SAndrew Thompson
43102ac6454SAndrew Thompson static void
uhid_start_write(struct usb_fifo * fifo)432760bc48eSAndrew Thompson uhid_start_write(struct usb_fifo *fifo)
43302ac6454SAndrew Thompson {
434ed6d949aSAndrew Thompson struct uhid_softc *sc = usb_fifo_softc(fifo);
43502ac6454SAndrew Thompson
4363dfe1a8eSAlexander Motin if ((sc->sc_flags & UHID_FLAG_IMMED) ||
4373dfe1a8eSAlexander Motin sc->sc_xfer[UHID_INTR_DT_WR] == NULL) {
438a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[UHID_CTRL_DT_WR]);
4393a38def3SAlexander Motin } else {
4403a38def3SAlexander Motin usbd_transfer_start(sc->sc_xfer[UHID_INTR_DT_WR]);
4413a38def3SAlexander Motin }
44202ac6454SAndrew Thompson }
44302ac6454SAndrew Thompson
44402ac6454SAndrew Thompson static void
uhid_stop_write(struct usb_fifo * fifo)445760bc48eSAndrew Thompson uhid_stop_write(struct usb_fifo *fifo)
44602ac6454SAndrew Thompson {
447ed6d949aSAndrew Thompson struct uhid_softc *sc = usb_fifo_softc(fifo);
44802ac6454SAndrew Thompson
449a593f6b8SAndrew Thompson usbd_transfer_stop(sc->sc_xfer[UHID_CTRL_DT_WR]);
4503a38def3SAlexander Motin usbd_transfer_stop(sc->sc_xfer[UHID_INTR_DT_WR]);
45102ac6454SAndrew Thompson }
45202ac6454SAndrew Thompson
45302ac6454SAndrew Thompson static int
uhid_get_report(struct uhid_softc * sc,uint8_t type,uint8_t id,void * kern_data,void * user_data,uint16_t len)45402ac6454SAndrew Thompson uhid_get_report(struct uhid_softc *sc, uint8_t type,
45502ac6454SAndrew Thompson uint8_t id, void *kern_data, void *user_data,
45602ac6454SAndrew Thompson uint16_t len)
45702ac6454SAndrew Thompson {
45802ac6454SAndrew Thompson int err;
45902ac6454SAndrew Thompson uint8_t free_data = 0;
46002ac6454SAndrew Thompson
46102ac6454SAndrew Thompson if (kern_data == NULL) {
46202ac6454SAndrew Thompson kern_data = malloc(len, M_USBDEV, M_WAITOK);
46302ac6454SAndrew Thompson free_data = 1;
46402ac6454SAndrew Thompson }
465a593f6b8SAndrew Thompson err = usbd_req_get_report(sc->sc_udev, NULL, kern_data,
46602ac6454SAndrew Thompson len, sc->sc_iface_index, type, id);
46702ac6454SAndrew Thompson if (err) {
46802ac6454SAndrew Thompson err = ENXIO;
46902ac6454SAndrew Thompson goto done;
47002ac6454SAndrew Thompson }
47102ac6454SAndrew Thompson if (user_data) {
47202ac6454SAndrew Thompson /* dummy buffer */
47302ac6454SAndrew Thompson err = copyout(kern_data, user_data, len);
47402ac6454SAndrew Thompson if (err) {
47502ac6454SAndrew Thompson goto done;
47602ac6454SAndrew Thompson }
47702ac6454SAndrew Thompson }
47802ac6454SAndrew Thompson done:
47902ac6454SAndrew Thompson if (free_data) {
48002ac6454SAndrew Thompson free(kern_data, M_USBDEV);
48102ac6454SAndrew Thompson }
48202ac6454SAndrew Thompson return (err);
48302ac6454SAndrew Thompson }
48402ac6454SAndrew Thompson
48502ac6454SAndrew Thompson static int
uhid_set_report(struct uhid_softc * sc,uint8_t type,uint8_t id,void * kern_data,void * user_data,uint16_t len)48602ac6454SAndrew Thompson uhid_set_report(struct uhid_softc *sc, uint8_t type,
48702ac6454SAndrew Thompson uint8_t id, void *kern_data, void *user_data,
48802ac6454SAndrew Thompson uint16_t len)
48902ac6454SAndrew Thompson {
49002ac6454SAndrew Thompson int err;
49102ac6454SAndrew Thompson uint8_t free_data = 0;
49202ac6454SAndrew Thompson
49302ac6454SAndrew Thompson if (kern_data == NULL) {
49402ac6454SAndrew Thompson kern_data = malloc(len, M_USBDEV, M_WAITOK);
49502ac6454SAndrew Thompson free_data = 1;
49602ac6454SAndrew Thompson err = copyin(user_data, kern_data, len);
49702ac6454SAndrew Thompson if (err) {
49802ac6454SAndrew Thompson goto done;
49902ac6454SAndrew Thompson }
50002ac6454SAndrew Thompson }
501a593f6b8SAndrew Thompson err = usbd_req_set_report(sc->sc_udev, NULL, kern_data,
50202ac6454SAndrew Thompson len, sc->sc_iface_index, type, id);
50302ac6454SAndrew Thompson if (err) {
50402ac6454SAndrew Thompson err = ENXIO;
50502ac6454SAndrew Thompson goto done;
50602ac6454SAndrew Thompson }
50702ac6454SAndrew Thompson done:
50802ac6454SAndrew Thompson if (free_data) {
50902ac6454SAndrew Thompson free(kern_data, M_USBDEV);
51002ac6454SAndrew Thompson }
51102ac6454SAndrew Thompson return (err);
51202ac6454SAndrew Thompson }
51302ac6454SAndrew Thompson
51402ac6454SAndrew Thompson static int
uhid_open(struct usb_fifo * fifo,int fflags)515760bc48eSAndrew Thompson uhid_open(struct usb_fifo *fifo, int fflags)
51602ac6454SAndrew Thompson {
517ed6d949aSAndrew Thompson struct uhid_softc *sc = usb_fifo_softc(fifo);
51802ac6454SAndrew Thompson
51902ac6454SAndrew Thompson /*
52002ac6454SAndrew Thompson * The buffers are one byte larger than maximum so that one
52102ac6454SAndrew Thompson * can detect too large read/writes and short transfers:
52202ac6454SAndrew Thompson */
52302ac6454SAndrew Thompson if (fflags & FREAD) {
52402ac6454SAndrew Thompson /* reset flags */
525996f8471SHans Petter Selasky mtx_lock(&sc->sc_mtx);
52602ac6454SAndrew Thompson sc->sc_flags &= ~UHID_FLAG_IMMED;
527996f8471SHans Petter Selasky mtx_unlock(&sc->sc_mtx);
52802ac6454SAndrew Thompson
529a593f6b8SAndrew Thompson if (usb_fifo_alloc_buffer(fifo,
53002ac6454SAndrew Thompson sc->sc_isize + 1, UHID_FRAME_NUM)) {
53102ac6454SAndrew Thompson return (ENOMEM);
53202ac6454SAndrew Thompson }
53302ac6454SAndrew Thompson }
53402ac6454SAndrew Thompson if (fflags & FWRITE) {
535a593f6b8SAndrew Thompson if (usb_fifo_alloc_buffer(fifo,
53602ac6454SAndrew Thompson sc->sc_osize + 1, UHID_FRAME_NUM)) {
53702ac6454SAndrew Thompson return (ENOMEM);
53802ac6454SAndrew Thompson }
53902ac6454SAndrew Thompson }
54002ac6454SAndrew Thompson return (0);
54102ac6454SAndrew Thompson }
54202ac6454SAndrew Thompson
54302ac6454SAndrew Thompson static void
uhid_close(struct usb_fifo * fifo,int fflags)544760bc48eSAndrew Thompson uhid_close(struct usb_fifo *fifo, int fflags)
54502ac6454SAndrew Thompson {
54602ac6454SAndrew Thompson if (fflags & (FREAD | FWRITE)) {
547a593f6b8SAndrew Thompson usb_fifo_free_buffer(fifo);
54802ac6454SAndrew Thompson }
54902ac6454SAndrew Thompson }
55002ac6454SAndrew Thompson
55102ac6454SAndrew Thompson static int
uhid_ioctl(struct usb_fifo * fifo,u_long cmd,void * addr,int fflags)552760bc48eSAndrew Thompson uhid_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr,
553ee3e3ff5SAndrew Thompson int fflags)
55402ac6454SAndrew Thompson {
555ed6d949aSAndrew Thompson struct uhid_softc *sc = usb_fifo_softc(fifo);
556760bc48eSAndrew Thompson struct usb_gen_descriptor *ugd;
55745b48cbcSBrooks Davis #ifdef COMPAT_FREEBSD32
55845b48cbcSBrooks Davis struct usb_gen_descriptor local_ugd;
55945b48cbcSBrooks Davis struct usb_gen_descriptor32 *ugd32 = NULL;
56045b48cbcSBrooks Davis #endif
56102ac6454SAndrew Thompson uint32_t size;
56202ac6454SAndrew Thompson int error = 0;
56302ac6454SAndrew Thompson uint8_t id;
56402ac6454SAndrew Thompson
56545b48cbcSBrooks Davis ugd = addr;
56645b48cbcSBrooks Davis #ifdef COMPAT_FREEBSD32
56745b48cbcSBrooks Davis switch (cmd) {
56845b48cbcSBrooks Davis case USB_GET_REPORT_DESC32:
56945b48cbcSBrooks Davis case USB_GET_REPORT32:
57045b48cbcSBrooks Davis case USB_SET_REPORT32:
57145b48cbcSBrooks Davis ugd32 = addr;
57245b48cbcSBrooks Davis ugd = &local_ugd;
57345b48cbcSBrooks Davis usb_gen_descriptor_from32(ugd, ugd32);
57445b48cbcSBrooks Davis cmd = _IOC_NEWTYPE(cmd, struct usb_gen_descriptor);
57545b48cbcSBrooks Davis break;
57645b48cbcSBrooks Davis }
57745b48cbcSBrooks Davis #endif
57845b48cbcSBrooks Davis
57902ac6454SAndrew Thompson switch (cmd) {
58002ac6454SAndrew Thompson case USB_GET_REPORT_DESC:
58102ac6454SAndrew Thompson if (sc->sc_repdesc_size > ugd->ugd_maxlen) {
58202ac6454SAndrew Thompson size = ugd->ugd_maxlen;
58302ac6454SAndrew Thompson } else {
58402ac6454SAndrew Thompson size = sc->sc_repdesc_size;
58502ac6454SAndrew Thompson }
58602ac6454SAndrew Thompson ugd->ugd_actlen = size;
58702ac6454SAndrew Thompson if (ugd->ugd_data == NULL)
58802ac6454SAndrew Thompson break; /* descriptor length only */
58902ac6454SAndrew Thompson error = copyout(sc->sc_repdesc_ptr, ugd->ugd_data, size);
59002ac6454SAndrew Thompson break;
59102ac6454SAndrew Thompson
59202ac6454SAndrew Thompson case USB_SET_IMMED:
59302ac6454SAndrew Thompson if (!(fflags & FREAD)) {
59402ac6454SAndrew Thompson error = EPERM;
59502ac6454SAndrew Thompson break;
59602ac6454SAndrew Thompson }
59702ac6454SAndrew Thompson if (*(int *)addr) {
59802ac6454SAndrew Thompson /* do a test read */
59902ac6454SAndrew Thompson
60002ac6454SAndrew Thompson error = uhid_get_report(sc, UHID_INPUT_REPORT,
60102ac6454SAndrew Thompson sc->sc_iid, NULL, NULL, sc->sc_isize);
60202ac6454SAndrew Thompson if (error) {
60302ac6454SAndrew Thompson break;
60402ac6454SAndrew Thompson }
60502ac6454SAndrew Thompson mtx_lock(&sc->sc_mtx);
60602ac6454SAndrew Thompson sc->sc_flags |= UHID_FLAG_IMMED;
60702ac6454SAndrew Thompson mtx_unlock(&sc->sc_mtx);
60802ac6454SAndrew Thompson } else {
60902ac6454SAndrew Thompson mtx_lock(&sc->sc_mtx);
61002ac6454SAndrew Thompson sc->sc_flags &= ~UHID_FLAG_IMMED;
61102ac6454SAndrew Thompson mtx_unlock(&sc->sc_mtx);
61202ac6454SAndrew Thompson }
61302ac6454SAndrew Thompson break;
61402ac6454SAndrew Thompson
61502ac6454SAndrew Thompson case USB_GET_REPORT:
61602ac6454SAndrew Thompson if (!(fflags & FREAD)) {
61702ac6454SAndrew Thompson error = EPERM;
61802ac6454SAndrew Thompson break;
61902ac6454SAndrew Thompson }
62002ac6454SAndrew Thompson switch (ugd->ugd_report_type) {
62102ac6454SAndrew Thompson case UHID_INPUT_REPORT:
62202ac6454SAndrew Thompson size = sc->sc_isize;
62302ac6454SAndrew Thompson id = sc->sc_iid;
62402ac6454SAndrew Thompson break;
62502ac6454SAndrew Thompson case UHID_OUTPUT_REPORT:
62602ac6454SAndrew Thompson size = sc->sc_osize;
62702ac6454SAndrew Thompson id = sc->sc_oid;
62802ac6454SAndrew Thompson break;
62902ac6454SAndrew Thompson case UHID_FEATURE_REPORT:
63002ac6454SAndrew Thompson size = sc->sc_fsize;
63102ac6454SAndrew Thompson id = sc->sc_fid;
63202ac6454SAndrew Thompson break;
63302ac6454SAndrew Thompson default:
63402ac6454SAndrew Thompson return (EINVAL);
63502ac6454SAndrew Thompson }
636*0b5d86b3SMatthew Nygard Dodd size = imin(ugd->ugd_maxlen, size);
6377778ab7eSAlexander Motin if (id != 0)
638b2caed2fSMark Johnston error = copyin(ugd->ugd_data, &id, 1);
639b2caed2fSMark Johnston if (error == 0)
64002ac6454SAndrew Thompson error = uhid_get_report(sc, ugd->ugd_report_type, id,
641*0b5d86b3SMatthew Nygard Dodd NULL, ugd->ugd_data, size);
642*0b5d86b3SMatthew Nygard Dodd ugd->ugd_actlen = size;
64302ac6454SAndrew Thompson break;
64402ac6454SAndrew Thompson
64502ac6454SAndrew Thompson case USB_SET_REPORT:
64602ac6454SAndrew Thompson if (!(fflags & FWRITE)) {
64702ac6454SAndrew Thompson error = EPERM;
64802ac6454SAndrew Thompson break;
64902ac6454SAndrew Thompson }
65002ac6454SAndrew Thompson switch (ugd->ugd_report_type) {
65102ac6454SAndrew Thompson case UHID_INPUT_REPORT:
65202ac6454SAndrew Thompson size = sc->sc_isize;
65302ac6454SAndrew Thompson id = sc->sc_iid;
65402ac6454SAndrew Thompson break;
65502ac6454SAndrew Thompson case UHID_OUTPUT_REPORT:
65602ac6454SAndrew Thompson size = sc->sc_osize;
65702ac6454SAndrew Thompson id = sc->sc_oid;
65802ac6454SAndrew Thompson break;
65902ac6454SAndrew Thompson case UHID_FEATURE_REPORT:
66002ac6454SAndrew Thompson size = sc->sc_fsize;
66102ac6454SAndrew Thompson id = sc->sc_fid;
66202ac6454SAndrew Thompson break;
66302ac6454SAndrew Thompson default:
66402ac6454SAndrew Thompson return (EINVAL);
66502ac6454SAndrew Thompson }
6667778ab7eSAlexander Motin if (id != 0)
667b2caed2fSMark Johnston error = copyin(ugd->ugd_data, &id, 1);
668b2caed2fSMark Johnston if (error == 0)
66902ac6454SAndrew Thompson error = uhid_set_report(sc, ugd->ugd_report_type, id,
6707778ab7eSAlexander Motin NULL, ugd->ugd_data, imin(ugd->ugd_maxlen, size));
67102ac6454SAndrew Thompson break;
67202ac6454SAndrew Thompson
67302ac6454SAndrew Thompson case USB_GET_REPORT_ID:
67402ac6454SAndrew Thompson *(int *)addr = 0; /* XXX: we only support reportid 0? */
67502ac6454SAndrew Thompson break;
67602ac6454SAndrew Thompson
67702ac6454SAndrew Thompson default:
678c77bfaa7SHans Petter Selasky error = ENOIOCTL;
679c77bfaa7SHans Petter Selasky break;
680c77bfaa7SHans Petter Selasky }
68145b48cbcSBrooks Davis #ifdef COMPAT_FREEBSD32
68245b48cbcSBrooks Davis if (ugd32 != NULL)
68345b48cbcSBrooks Davis update_usb_gen_descriptor32(ugd32, ugd);
68445b48cbcSBrooks Davis #endif
685c77bfaa7SHans Petter Selasky return (error);
686c77bfaa7SHans Petter Selasky }
687c77bfaa7SHans Petter Selasky
688c77bfaa7SHans Petter Selasky static int
uhid_ioctl_post(struct usb_fifo * fifo,u_long cmd,void * addr,int fflags)689c77bfaa7SHans Petter Selasky uhid_ioctl_post(struct usb_fifo *fifo, u_long cmd, void *addr,
690c77bfaa7SHans Petter Selasky int fflags)
691c77bfaa7SHans Petter Selasky {
692c77bfaa7SHans Petter Selasky int error;
693c77bfaa7SHans Petter Selasky
694c77bfaa7SHans Petter Selasky switch (cmd) {
695c77bfaa7SHans Petter Selasky case USB_GET_DEVICEINFO:
696c77bfaa7SHans Petter Selasky error = ugen_fill_deviceinfo(fifo, addr);
697c77bfaa7SHans Petter Selasky break;
698c77bfaa7SHans Petter Selasky
699c77bfaa7SHans Petter Selasky default:
70002ac6454SAndrew Thompson error = EINVAL;
70102ac6454SAndrew Thompson break;
70202ac6454SAndrew Thompson }
70302ac6454SAndrew Thompson return (error);
70402ac6454SAndrew Thompson }
70502ac6454SAndrew Thompson
706f1a16106SHans Petter Selasky static const STRUCT_USB_HOST_ID uhid_devs[] = {
707f1a16106SHans Petter Selasky /* generic HID class */
708f1a16106SHans Petter Selasky {USB_IFACE_CLASS(UICLASS_HID),},
709f1a16106SHans Petter Selasky /* the Xbox 360 gamepad doesn't use the HID class */
710f1a16106SHans Petter Selasky {USB_IFACE_CLASS(UICLASS_VENDOR),
711f1a16106SHans Petter Selasky USB_IFACE_SUBCLASS(UISUBCLASS_XBOX360_CONTROLLER),
712f1a16106SHans Petter Selasky USB_IFACE_PROTOCOL(UIPROTO_XBOX360_GAMEPAD),},
713f1a16106SHans Petter Selasky };
714f1a16106SHans Petter Selasky
71502ac6454SAndrew Thompson static int
uhid_probe(device_t dev)71602ac6454SAndrew Thompson uhid_probe(device_t dev)
71702ac6454SAndrew Thompson {
718760bc48eSAndrew Thompson struct usb_attach_arg *uaa = device_get_ivars(dev);
719f1a16106SHans Petter Selasky int error;
720236e308aSVladimir Kondratyev void *buf;
721236e308aSVladimir Kondratyev uint16_t len;
72202ac6454SAndrew Thompson
72302ac6454SAndrew Thompson DPRINTFN(11, "\n");
72402ac6454SAndrew Thompson
725f1a16106SHans Petter Selasky if (uaa->usb_mode != USB_MODE_HOST)
72602ac6454SAndrew Thompson return (ENXIO);
72702ac6454SAndrew Thompson
728f1a16106SHans Petter Selasky error = usbd_lookup_id_by_uaa(uhid_devs, sizeof(uhid_devs), uaa);
729f1a16106SHans Petter Selasky if (error)
730f1a16106SHans Petter Selasky return (error);
73102ac6454SAndrew Thompson
732f1a16106SHans Petter Selasky if (usb_test_quirk(uaa, UQ_HID_IGNORE))
73302ac6454SAndrew Thompson return (ENXIO);
734f1a16106SHans Petter Selasky
73534e5f64bSHans Petter Selasky /*
73634e5f64bSHans Petter Selasky * Don't attach to mouse and keyboard devices, hence then no
73734e5f64bSHans Petter Selasky * "nomatch" event is generated and then ums and ukbd won't
73834e5f64bSHans Petter Selasky * attach properly when loaded.
73934e5f64bSHans Petter Selasky */
74034e5f64bSHans Petter Selasky if ((uaa->info.bInterfaceClass == UICLASS_HID) &&
74134e5f64bSHans Petter Selasky (uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
74268ea9fd7SEitan Adler (((uaa->info.bInterfaceProtocol == UIPROTO_BOOT_KEYBOARD) &&
74368ea9fd7SEitan Adler !usb_test_quirk(uaa, UQ_KBD_IGNORE)) ||
74468ea9fd7SEitan Adler ((uaa->info.bInterfaceProtocol == UIPROTO_MOUSE) &&
74568ea9fd7SEitan Adler !usb_test_quirk(uaa, UQ_UMS_IGNORE))))
74634e5f64bSHans Petter Selasky return (ENXIO);
74734e5f64bSHans Petter Selasky
748236e308aSVladimir Kondratyev /* Check for mandatory multitouch usages to give wmt(4) a chance */
749236e308aSVladimir Kondratyev if (!usb_test_quirk(uaa, UQ_WMT_IGNORE)) {
750236e308aSVladimir Kondratyev error = usbd_req_get_hid_desc(uaa->device, NULL,
751236e308aSVladimir Kondratyev &buf, &len, M_USBDEV, uaa->info.bIfaceIndex);
752236e308aSVladimir Kondratyev /* Let HID decscriptor-less devices to be handled at attach */
753236e308aSVladimir Kondratyev if (!error) {
754236e308aSVladimir Kondratyev if (hid_locate(buf, len,
755236e308aSVladimir Kondratyev HID_USAGE2(HUP_DIGITIZERS, HUD_CONTACT_MAX),
756236e308aSVladimir Kondratyev hid_feature, 0, NULL, NULL, NULL) &&
757236e308aSVladimir Kondratyev hid_locate(buf, len,
758236e308aSVladimir Kondratyev HID_USAGE2(HUP_DIGITIZERS, HUD_CONTACTID),
759236e308aSVladimir Kondratyev hid_input, 0, NULL, NULL, NULL)) {
760236e308aSVladimir Kondratyev free(buf, M_USBDEV);
761236e308aSVladimir Kondratyev return (ENXIO);
762236e308aSVladimir Kondratyev }
763236e308aSVladimir Kondratyev free(buf, M_USBDEV);
764236e308aSVladimir Kondratyev }
765236e308aSVladimir Kondratyev }
766236e308aSVladimir Kondratyev
767b201cde6SNathan Whitehorn return (BUS_PROBE_GENERIC);
76802ac6454SAndrew Thompson }
76902ac6454SAndrew Thompson
77002ac6454SAndrew Thompson static int
uhid_attach(device_t dev)77102ac6454SAndrew Thompson uhid_attach(device_t dev)
77202ac6454SAndrew Thompson {
773760bc48eSAndrew Thompson struct usb_attach_arg *uaa = device_get_ivars(dev);
77402ac6454SAndrew Thompson struct uhid_softc *sc = device_get_softc(dev);
77502ac6454SAndrew Thompson int unit = device_get_unit(dev);
77602ac6454SAndrew Thompson int error = 0;
77702ac6454SAndrew Thompson
77802ac6454SAndrew Thompson DPRINTFN(10, "sc=%p\n", sc);
77902ac6454SAndrew Thompson
780a593f6b8SAndrew Thompson device_set_usb_desc(dev);
78102ac6454SAndrew Thompson
78202ac6454SAndrew Thompson mtx_init(&sc->sc_mtx, "uhid lock", NULL, MTX_DEF | MTX_RECURSE);
78302ac6454SAndrew Thompson
78402ac6454SAndrew Thompson sc->sc_udev = uaa->device;
78502ac6454SAndrew Thompson
78602ac6454SAndrew Thompson sc->sc_iface_no = uaa->info.bIfaceNum;
78702ac6454SAndrew Thompson sc->sc_iface_index = uaa->info.bIfaceIndex;
78802ac6454SAndrew Thompson
789a593f6b8SAndrew Thompson error = usbd_transfer_setup(uaa->device,
79002ac6454SAndrew Thompson &uaa->info.bIfaceIndex, sc->sc_xfer, uhid_config,
79102ac6454SAndrew Thompson UHID_N_TRANSFER, sc, &sc->sc_mtx);
79202ac6454SAndrew Thompson
79302ac6454SAndrew Thompson if (error) {
794a593f6b8SAndrew Thompson DPRINTF("error=%s\n", usbd_errstr(error));
79502ac6454SAndrew Thompson goto detach;
79602ac6454SAndrew Thompson }
79702ac6454SAndrew Thompson if (uaa->info.idVendor == USB_VENDOR_WACOM) {
79802ac6454SAndrew Thompson /* the report descriptor for the Wacom Graphire is broken */
79902ac6454SAndrew Thompson
80002ac6454SAndrew Thompson if (uaa->info.idProduct == USB_PRODUCT_WACOM_GRAPHIRE) {
80102ac6454SAndrew Thompson sc->sc_repdesc_size = sizeof(uhid_graphire_report_descr);
802620d4f3cSDimitry Andric sc->sc_repdesc_ptr = __DECONST(void *, &uhid_graphire_report_descr);
80302ac6454SAndrew Thompson sc->sc_flags |= UHID_FLAG_STATIC_DESC;
80402ac6454SAndrew Thompson
80502ac6454SAndrew Thompson } else if (uaa->info.idProduct == USB_PRODUCT_WACOM_GRAPHIRE3_4X5) {
80602ac6454SAndrew Thompson static uint8_t reportbuf[] = {2, 2, 2};
80702ac6454SAndrew Thompson
80802ac6454SAndrew Thompson /*
80902ac6454SAndrew Thompson * The Graphire3 needs 0x0202 to be written to
81002ac6454SAndrew Thompson * feature report ID 2 before it'll start
81102ac6454SAndrew Thompson * returning digitizer data.
81202ac6454SAndrew Thompson */
813a593f6b8SAndrew Thompson error = usbd_req_set_report(uaa->device, NULL,
814296ade60SAndrew Thompson reportbuf, sizeof(reportbuf),
81502ac6454SAndrew Thompson uaa->info.bIfaceIndex, UHID_FEATURE_REPORT, 2);
81602ac6454SAndrew Thompson
81702ac6454SAndrew Thompson if (error) {
81802ac6454SAndrew Thompson DPRINTF("set report failed, error=%s (ignored)\n",
819a593f6b8SAndrew Thompson usbd_errstr(error));
82002ac6454SAndrew Thompson }
82102ac6454SAndrew Thompson sc->sc_repdesc_size = sizeof(uhid_graphire3_4x5_report_descr);
822620d4f3cSDimitry Andric sc->sc_repdesc_ptr = __DECONST(void *, &uhid_graphire3_4x5_report_descr);
82302ac6454SAndrew Thompson sc->sc_flags |= UHID_FLAG_STATIC_DESC;
82402ac6454SAndrew Thompson }
82502ac6454SAndrew Thompson } else if ((uaa->info.bInterfaceClass == UICLASS_VENDOR) &&
82602ac6454SAndrew Thompson (uaa->info.bInterfaceSubClass == UISUBCLASS_XBOX360_CONTROLLER) &&
82702ac6454SAndrew Thompson (uaa->info.bInterfaceProtocol == UIPROTO_XBOX360_GAMEPAD)) {
828817a8cacSHans Petter Selasky static const uint8_t reportbuf[3] = {1, 3, 0};
829817a8cacSHans Petter Selasky /*
830817a8cacSHans Petter Selasky * Turn off the four LEDs on the gamepad which
831817a8cacSHans Petter Selasky * are blinking by default:
832817a8cacSHans Petter Selasky */
833817a8cacSHans Petter Selasky error = usbd_req_set_report(uaa->device, NULL,
834817a8cacSHans Petter Selasky __DECONST(void *, reportbuf), sizeof(reportbuf),
835817a8cacSHans Petter Selasky uaa->info.bIfaceIndex, UHID_OUTPUT_REPORT, 0);
836817a8cacSHans Petter Selasky if (error) {
837817a8cacSHans Petter Selasky DPRINTF("set output report failed, error=%s (ignored)\n",
838817a8cacSHans Petter Selasky usbd_errstr(error));
839817a8cacSHans Petter Selasky }
84002ac6454SAndrew Thompson /* the Xbox 360 gamepad has no report descriptor */
84102ac6454SAndrew Thompson sc->sc_repdesc_size = sizeof(uhid_xb360gp_report_descr);
842620d4f3cSDimitry Andric sc->sc_repdesc_ptr = __DECONST(void *, &uhid_xb360gp_report_descr);
84302ac6454SAndrew Thompson sc->sc_flags |= UHID_FLAG_STATIC_DESC;
84402ac6454SAndrew Thompson }
84502ac6454SAndrew Thompson if (sc->sc_repdesc_ptr == NULL) {
846a593f6b8SAndrew Thompson error = usbd_req_get_hid_desc(uaa->device, NULL,
847296ade60SAndrew Thompson &sc->sc_repdesc_ptr, &sc->sc_repdesc_size,
848296ade60SAndrew Thompson M_USBDEV, uaa->info.bIfaceIndex);
84902ac6454SAndrew Thompson
85002ac6454SAndrew Thompson if (error) {
85102ac6454SAndrew Thompson device_printf(dev, "no report descriptor\n");
85202ac6454SAndrew Thompson goto detach;
85302ac6454SAndrew Thompson }
85402ac6454SAndrew Thompson }
855a593f6b8SAndrew Thompson error = usbd_req_set_idle(uaa->device, NULL,
85602ac6454SAndrew Thompson uaa->info.bIfaceIndex, 0, 0);
85702ac6454SAndrew Thompson
85802ac6454SAndrew Thompson if (error) {
85902ac6454SAndrew Thompson DPRINTF("set idle failed, error=%s (ignored)\n",
860a593f6b8SAndrew Thompson usbd_errstr(error));
86102ac6454SAndrew Thompson }
862eead9017SVladimir Kondratyev sc->sc_isize = hid_report_size_max
86302ac6454SAndrew Thompson (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_input, &sc->sc_iid);
86402ac6454SAndrew Thompson
865eead9017SVladimir Kondratyev sc->sc_osize = hid_report_size_max
86602ac6454SAndrew Thompson (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_output, &sc->sc_oid);
86702ac6454SAndrew Thompson
868eead9017SVladimir Kondratyev sc->sc_fsize = hid_report_size_max
86902ac6454SAndrew Thompson (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_feature, &sc->sc_fid);
87002ac6454SAndrew Thompson
87102ac6454SAndrew Thompson if (sc->sc_isize > UHID_BSIZE) {
87202ac6454SAndrew Thompson DPRINTF("input size is too large, "
87302ac6454SAndrew Thompson "%d bytes (truncating)\n",
87402ac6454SAndrew Thompson sc->sc_isize);
87502ac6454SAndrew Thompson sc->sc_isize = UHID_BSIZE;
87602ac6454SAndrew Thompson }
87702ac6454SAndrew Thompson if (sc->sc_osize > UHID_BSIZE) {
87802ac6454SAndrew Thompson DPRINTF("output size is too large, "
87902ac6454SAndrew Thompson "%d bytes (truncating)\n",
88002ac6454SAndrew Thompson sc->sc_osize);
88102ac6454SAndrew Thompson sc->sc_osize = UHID_BSIZE;
88202ac6454SAndrew Thompson }
88302ac6454SAndrew Thompson if (sc->sc_fsize > UHID_BSIZE) {
88402ac6454SAndrew Thompson DPRINTF("feature size is too large, "
88502ac6454SAndrew Thompson "%d bytes (truncating)\n",
88602ac6454SAndrew Thompson sc->sc_fsize);
88702ac6454SAndrew Thompson sc->sc_fsize = UHID_BSIZE;
88802ac6454SAndrew Thompson }
88902ac6454SAndrew Thompson
890a593f6b8SAndrew Thompson error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
89102ac6454SAndrew Thompson &uhid_fifo_methods, &sc->sc_fifo,
8926d917491SHans Petter Selasky unit, -1, uaa->info.bIfaceIndex,
893ee3e3ff5SAndrew Thompson UID_ROOT, GID_OPERATOR, 0644);
89402ac6454SAndrew Thompson if (error) {
89502ac6454SAndrew Thompson goto detach;
89602ac6454SAndrew Thompson }
89702ac6454SAndrew Thompson return (0); /* success */
89802ac6454SAndrew Thompson
89902ac6454SAndrew Thompson detach:
90002ac6454SAndrew Thompson uhid_detach(dev);
90102ac6454SAndrew Thompson return (ENOMEM);
90202ac6454SAndrew Thompson }
90302ac6454SAndrew Thompson
90402ac6454SAndrew Thompson static int
uhid_detach(device_t dev)90502ac6454SAndrew Thompson uhid_detach(device_t dev)
90602ac6454SAndrew Thompson {
90702ac6454SAndrew Thompson struct uhid_softc *sc = device_get_softc(dev);
90802ac6454SAndrew Thompson
909a593f6b8SAndrew Thompson usb_fifo_detach(&sc->sc_fifo);
91002ac6454SAndrew Thompson
911a593f6b8SAndrew Thompson usbd_transfer_unsetup(sc->sc_xfer, UHID_N_TRANSFER);
91202ac6454SAndrew Thompson
91302ac6454SAndrew Thompson if (sc->sc_repdesc_ptr) {
91402ac6454SAndrew Thompson if (!(sc->sc_flags & UHID_FLAG_STATIC_DESC)) {
91502ac6454SAndrew Thompson free(sc->sc_repdesc_ptr, M_USBDEV);
91602ac6454SAndrew Thompson }
91702ac6454SAndrew Thompson }
91802ac6454SAndrew Thompson mtx_destroy(&sc->sc_mtx);
91902ac6454SAndrew Thompson
92002ac6454SAndrew Thompson return (0);
92102ac6454SAndrew Thompson }
92202ac6454SAndrew Thompson
92302ac6454SAndrew Thompson static device_method_t uhid_methods[] = {
92402ac6454SAndrew Thompson DEVMETHOD(device_probe, uhid_probe),
92502ac6454SAndrew Thompson DEVMETHOD(device_attach, uhid_attach),
92602ac6454SAndrew Thompson DEVMETHOD(device_detach, uhid_detach),
92761bfd867SSofian Brabez
92861bfd867SSofian Brabez DEVMETHOD_END
92902ac6454SAndrew Thompson };
93002ac6454SAndrew Thompson
93102ac6454SAndrew Thompson static driver_t uhid_driver = {
9329be6b22dSVladimir Kondratyev #ifdef HIDRAW_MAKE_UHID_ALIAS
9339be6b22dSVladimir Kondratyev .name = "hidraw",
9349be6b22dSVladimir Kondratyev #else
93502ac6454SAndrew Thompson .name = "uhid",
9369be6b22dSVladimir Kondratyev #endif
93702ac6454SAndrew Thompson .methods = uhid_methods,
93802ac6454SAndrew Thompson .size = sizeof(struct uhid_softc),
93902ac6454SAndrew Thompson };
94002ac6454SAndrew Thompson
941a4777bb4SJohn Baldwin DRIVER_MODULE(uhid, uhub, uhid_driver, NULL, NULL);
94202ac6454SAndrew Thompson MODULE_DEPEND(uhid, usb, 1, 1, 1);
94367de2db2SVladimir Kondratyev MODULE_DEPEND(uhid, hid, 1, 1, 1);
944910cb8feSAndrew Thompson MODULE_VERSION(uhid, 1);
945f809f280SWarner Losh USB_PNP_HOST_INFO(uhid_devs);
946