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/module.h> 53ed6d949aSAndrew Thompson #include <sys/lock.h> 54ed6d949aSAndrew Thompson #include <sys/mutex.h> 55ed6d949aSAndrew Thompson #include <sys/condvar.h> 56ed6d949aSAndrew Thompson #include <sys/sysctl.h> 57ed6d949aSAndrew Thompson #include <sys/sx.h> 58ed6d949aSAndrew Thompson #include <sys/unistd.h> 59ed6d949aSAndrew Thompson #include <sys/callout.h> 60ed6d949aSAndrew Thompson #include <sys/malloc.h> 61ed6d949aSAndrew Thompson #include <sys/priv.h> 62ed6d949aSAndrew Thompson #include <sys/conf.h> 63ed6d949aSAndrew Thompson #include <sys/fcntl.h> 64ed6d949aSAndrew Thompson 6502ac6454SAndrew Thompson #include "usbdevs.h" 6602ac6454SAndrew Thompson #include <dev/usb/usb.h> 67ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h> 68ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h> 6902ac6454SAndrew Thompson #include <dev/usb/usbhid.h> 7002ac6454SAndrew Thompson #include <dev/usb/usb_ioctl.h> 7102ac6454SAndrew Thompson 7202ac6454SAndrew Thompson #define USB_DEBUG_VAR uhid_debug 7302ac6454SAndrew Thompson #include <dev/usb/usb_debug.h> 7402ac6454SAndrew Thompson 7502ac6454SAndrew Thompson #include <dev/usb/input/usb_rdesc.h> 7602ac6454SAndrew Thompson #include <dev/usb/quirk/usb_quirk.h> 7702ac6454SAndrew Thompson 78b850ecc1SAndrew Thompson #ifdef USB_DEBUG 7902ac6454SAndrew Thompson static int uhid_debug = 0; 8002ac6454SAndrew Thompson 816472ac3dSEd Schouten static SYSCTL_NODE(_hw_usb, OID_AUTO, uhid, CTLFLAG_RW, 0, "USB uhid"); 82ece4b0bdSHans Petter Selasky SYSCTL_INT(_hw_usb_uhid, OID_AUTO, debug, CTLFLAG_RWTUN, 8302ac6454SAndrew Thompson &uhid_debug, 0, "Debug level"); 8402ac6454SAndrew Thompson #endif 8502ac6454SAndrew Thompson 8602ac6454SAndrew Thompson #define UHID_BSIZE 1024 /* bytes, buffer size */ 8702ac6454SAndrew Thompson #define UHID_FRAME_NUM 50 /* bytes, frame number */ 8802ac6454SAndrew Thompson 8902ac6454SAndrew Thompson enum { 903a38def3SAlexander Motin UHID_INTR_DT_WR, 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 1323a38def3SAlexander Motin static usb_callback_t uhid_intr_write_callback; 1333a38def3SAlexander Motin static usb_callback_t uhid_intr_read_callback; 134e0a69b51SAndrew Thompson static usb_callback_t uhid_write_callback; 135e0a69b51SAndrew Thompson static usb_callback_t uhid_read_callback; 13602ac6454SAndrew Thompson 137e0a69b51SAndrew Thompson static usb_fifo_cmd_t uhid_start_read; 138e0a69b51SAndrew Thompson static usb_fifo_cmd_t uhid_stop_read; 139e0a69b51SAndrew Thompson static usb_fifo_cmd_t uhid_start_write; 140e0a69b51SAndrew Thompson static usb_fifo_cmd_t uhid_stop_write; 141e0a69b51SAndrew Thompson static usb_fifo_open_t uhid_open; 142e0a69b51SAndrew Thompson static usb_fifo_close_t uhid_close; 143e0a69b51SAndrew Thompson static usb_fifo_ioctl_t uhid_ioctl; 14402ac6454SAndrew Thompson 145760bc48eSAndrew Thompson static struct usb_fifo_methods uhid_fifo_methods = { 14602ac6454SAndrew Thompson .f_open = &uhid_open, 14702ac6454SAndrew Thompson .f_close = &uhid_close, 14802ac6454SAndrew Thompson .f_ioctl = &uhid_ioctl, 14902ac6454SAndrew Thompson .f_start_read = &uhid_start_read, 15002ac6454SAndrew Thompson .f_stop_read = &uhid_stop_read, 15102ac6454SAndrew Thompson .f_start_write = &uhid_start_write, 15202ac6454SAndrew Thompson .f_stop_write = &uhid_stop_write, 15302ac6454SAndrew Thompson .basename[0] = "uhid", 15402ac6454SAndrew Thompson }; 15502ac6454SAndrew Thompson 15602ac6454SAndrew Thompson static void 1573a38def3SAlexander Motin uhid_intr_write_callback(struct usb_xfer *xfer, usb_error_t error) 1583a38def3SAlexander Motin { 1593a38def3SAlexander Motin struct uhid_softc *sc = usbd_xfer_softc(xfer); 1603a38def3SAlexander Motin struct usb_page_cache *pc; 1613a38def3SAlexander Motin int actlen; 1623a38def3SAlexander Motin 1633a38def3SAlexander Motin switch (USB_GET_STATE(xfer)) { 1643a38def3SAlexander Motin case USB_ST_TRANSFERRED: 1653a38def3SAlexander Motin case USB_ST_SETUP: 1663a38def3SAlexander Motin tr_setup: 1673a38def3SAlexander Motin pc = usbd_xfer_get_frame(xfer, 0); 1683a38def3SAlexander Motin if (usb_fifo_get_data(sc->sc_fifo.fp[USB_FIFO_TX], pc, 1693a38def3SAlexander Motin 0, usbd_xfer_max_len(xfer), &actlen, 0)) { 1703a38def3SAlexander Motin usbd_xfer_set_frame_len(xfer, 0, actlen); 1713a38def3SAlexander Motin usbd_transfer_submit(xfer); 1723a38def3SAlexander Motin } 1733a38def3SAlexander Motin return; 1743a38def3SAlexander Motin 1753a38def3SAlexander Motin default: /* Error */ 1763a38def3SAlexander Motin if (error != USB_ERR_CANCELLED) { 1773a38def3SAlexander Motin /* try to clear stall first */ 1783a38def3SAlexander Motin usbd_xfer_set_stall(xfer); 1793a38def3SAlexander Motin goto tr_setup; 1803a38def3SAlexander Motin } 1813a38def3SAlexander Motin return; 1823a38def3SAlexander Motin } 1833a38def3SAlexander Motin } 1843a38def3SAlexander Motin 1853a38def3SAlexander Motin static void 1863a38def3SAlexander Motin uhid_intr_read_callback(struct usb_xfer *xfer, usb_error_t error) 18702ac6454SAndrew Thompson { 188ed6d949aSAndrew Thompson struct uhid_softc *sc = usbd_xfer_softc(xfer); 189ed6d949aSAndrew Thompson struct usb_page_cache *pc; 190ed6d949aSAndrew Thompson int actlen; 191ed6d949aSAndrew Thompson 192ed6d949aSAndrew Thompson usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 19302ac6454SAndrew Thompson 19402ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) { 19502ac6454SAndrew Thompson case USB_ST_TRANSFERRED: 19602ac6454SAndrew Thompson DPRINTF("transferred!\n"); 19702ac6454SAndrew Thompson 198ed6d949aSAndrew Thompson pc = usbd_xfer_get_frame(xfer, 0); 1990ca80d71SAndrew Thompson 2000ca80d71SAndrew Thompson /* 2010ca80d71SAndrew Thompson * If the ID byte is non zero we allow descriptors 2020ca80d71SAndrew Thompson * having multiple sizes: 2030ca80d71SAndrew Thompson */ 2046d917491SHans Petter Selasky if ((actlen >= (int)sc->sc_isize) || 2050ca80d71SAndrew Thompson ((actlen > 0) && (sc->sc_iid != 0))) { 2060ca80d71SAndrew Thompson /* limit report length to the maximum */ 2076d917491SHans Petter Selasky if (actlen > (int)sc->sc_isize) 2080ca80d71SAndrew Thompson actlen = sc->sc_isize; 209ed6d949aSAndrew Thompson usb_fifo_put_data(sc->sc_fifo.fp[USB_FIFO_RX], pc, 2100ca80d71SAndrew Thompson 0, actlen, 1); 21102ac6454SAndrew Thompson } else { 21202ac6454SAndrew Thompson /* ignore it */ 2130ca80d71SAndrew Thompson DPRINTF("ignored transfer, %d bytes\n", actlen); 21402ac6454SAndrew Thompson } 21502ac6454SAndrew Thompson 21602ac6454SAndrew Thompson case USB_ST_SETUP: 21702ac6454SAndrew Thompson re_submit: 218a593f6b8SAndrew Thompson if (usb_fifo_put_bytes_max( 21902ac6454SAndrew Thompson sc->sc_fifo.fp[USB_FIFO_RX]) != 0) { 220ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, sc->sc_isize); 221a593f6b8SAndrew Thompson usbd_transfer_submit(xfer); 22202ac6454SAndrew Thompson } 22302ac6454SAndrew Thompson return; 22402ac6454SAndrew Thompson 22502ac6454SAndrew Thompson default: /* Error */ 226ed6d949aSAndrew Thompson if (error != USB_ERR_CANCELLED) { 22702ac6454SAndrew Thompson /* try to clear stall first */ 228ed6d949aSAndrew Thompson usbd_xfer_set_stall(xfer); 22902ac6454SAndrew Thompson goto re_submit; 23002ac6454SAndrew Thompson } 23102ac6454SAndrew Thompson return; 23202ac6454SAndrew Thompson } 23302ac6454SAndrew Thompson } 23402ac6454SAndrew Thompson 23502ac6454SAndrew Thompson static void 236760bc48eSAndrew Thompson uhid_fill_set_report(struct usb_device_request *req, uint8_t iface_no, 23702ac6454SAndrew Thompson uint8_t type, uint8_t id, uint16_t size) 23802ac6454SAndrew Thompson { 23902ac6454SAndrew Thompson req->bmRequestType = UT_WRITE_CLASS_INTERFACE; 24002ac6454SAndrew Thompson req->bRequest = UR_SET_REPORT; 24102ac6454SAndrew Thompson USETW2(req->wValue, type, id); 24202ac6454SAndrew Thompson req->wIndex[0] = iface_no; 24302ac6454SAndrew Thompson req->wIndex[1] = 0; 24402ac6454SAndrew Thompson USETW(req->wLength, size); 24502ac6454SAndrew Thompson } 24602ac6454SAndrew Thompson 24702ac6454SAndrew Thompson static void 248760bc48eSAndrew Thompson uhid_fill_get_report(struct usb_device_request *req, uint8_t iface_no, 24902ac6454SAndrew Thompson uint8_t type, uint8_t id, uint16_t size) 25002ac6454SAndrew Thompson { 25102ac6454SAndrew Thompson req->bmRequestType = UT_READ_CLASS_INTERFACE; 25202ac6454SAndrew Thompson req->bRequest = UR_GET_REPORT; 25302ac6454SAndrew Thompson USETW2(req->wValue, type, id); 25402ac6454SAndrew Thompson req->wIndex[0] = iface_no; 25502ac6454SAndrew Thompson req->wIndex[1] = 0; 25602ac6454SAndrew Thompson USETW(req->wLength, size); 25702ac6454SAndrew Thompson } 25802ac6454SAndrew Thompson 25902ac6454SAndrew Thompson static void 260ed6d949aSAndrew Thompson uhid_write_callback(struct usb_xfer *xfer, usb_error_t error) 26102ac6454SAndrew Thompson { 262ed6d949aSAndrew Thompson struct uhid_softc *sc = usbd_xfer_softc(xfer); 263760bc48eSAndrew Thompson struct usb_device_request req; 264ed6d949aSAndrew Thompson struct usb_page_cache *pc; 26502ac6454SAndrew Thompson uint32_t size = sc->sc_osize; 26602ac6454SAndrew Thompson uint32_t actlen; 26702ac6454SAndrew Thompson uint8_t id; 26802ac6454SAndrew Thompson 26902ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) { 27002ac6454SAndrew Thompson case USB_ST_TRANSFERRED: 27102ac6454SAndrew Thompson case USB_ST_SETUP: 27202ac6454SAndrew Thompson /* try to extract the ID byte */ 27302ac6454SAndrew Thompson if (sc->sc_oid) { 274ed6d949aSAndrew Thompson pc = usbd_xfer_get_frame(xfer, 0); 275ed6d949aSAndrew Thompson if (usb_fifo_get_data(sc->sc_fifo.fp[USB_FIFO_TX], pc, 27602ac6454SAndrew Thompson 0, 1, &actlen, 0)) { 27702ac6454SAndrew Thompson if (actlen != 1) { 27802ac6454SAndrew Thompson goto tr_error; 27902ac6454SAndrew Thompson } 280ed6d949aSAndrew Thompson usbd_copy_out(pc, 0, &id, 1); 28102ac6454SAndrew Thompson 28202ac6454SAndrew Thompson } else { 28302ac6454SAndrew Thompson return; 28402ac6454SAndrew Thompson } 28502ac6454SAndrew Thompson if (size) { 28602ac6454SAndrew Thompson size--; 28702ac6454SAndrew Thompson } 28802ac6454SAndrew Thompson } else { 28902ac6454SAndrew Thompson id = 0; 29002ac6454SAndrew Thompson } 29102ac6454SAndrew Thompson 292ed6d949aSAndrew Thompson pc = usbd_xfer_get_frame(xfer, 1); 293ed6d949aSAndrew Thompson if (usb_fifo_get_data(sc->sc_fifo.fp[USB_FIFO_TX], pc, 29402ac6454SAndrew Thompson 0, UHID_BSIZE, &actlen, 1)) { 29502ac6454SAndrew Thompson if (actlen != size) { 29602ac6454SAndrew Thompson goto tr_error; 29702ac6454SAndrew Thompson } 29802ac6454SAndrew Thompson uhid_fill_set_report 29902ac6454SAndrew Thompson (&req, sc->sc_iface_no, 30002ac6454SAndrew Thompson UHID_OUTPUT_REPORT, id, size); 30102ac6454SAndrew Thompson 302ed6d949aSAndrew Thompson pc = usbd_xfer_get_frame(xfer, 0); 303ed6d949aSAndrew Thompson usbd_copy_in(pc, 0, &req, sizeof(req)); 30402ac6454SAndrew Thompson 305ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, sizeof(req)); 306ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 1, size); 307ed6d949aSAndrew Thompson usbd_xfer_set_frames(xfer, size ? 2 : 1); 308a593f6b8SAndrew Thompson usbd_transfer_submit(xfer); 30902ac6454SAndrew Thompson } 31002ac6454SAndrew Thompson return; 31102ac6454SAndrew Thompson 31202ac6454SAndrew Thompson default: 31302ac6454SAndrew Thompson tr_error: 31402ac6454SAndrew Thompson /* bomb out */ 315a593f6b8SAndrew Thompson usb_fifo_get_data_error(sc->sc_fifo.fp[USB_FIFO_TX]); 31602ac6454SAndrew Thompson return; 31702ac6454SAndrew Thompson } 31802ac6454SAndrew Thompson } 31902ac6454SAndrew Thompson 32002ac6454SAndrew Thompson static void 321ed6d949aSAndrew Thompson uhid_read_callback(struct usb_xfer *xfer, usb_error_t error) 32202ac6454SAndrew Thompson { 323ed6d949aSAndrew Thompson struct uhid_softc *sc = usbd_xfer_softc(xfer); 324760bc48eSAndrew Thompson struct usb_device_request req; 325ed6d949aSAndrew Thompson struct usb_page_cache *pc; 326ed6d949aSAndrew Thompson 327ed6d949aSAndrew Thompson pc = usbd_xfer_get_frame(xfer, 0); 32802ac6454SAndrew Thompson 32902ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) { 33002ac6454SAndrew Thompson case USB_ST_TRANSFERRED: 331ed6d949aSAndrew Thompson usb_fifo_put_data(sc->sc_fifo.fp[USB_FIFO_RX], pc, sizeof(req), 332ed6d949aSAndrew Thompson sc->sc_isize, 1); 33302ac6454SAndrew Thompson return; 33402ac6454SAndrew Thompson 33502ac6454SAndrew Thompson case USB_ST_SETUP: 33602ac6454SAndrew Thompson 337a593f6b8SAndrew Thompson if (usb_fifo_put_bytes_max(sc->sc_fifo.fp[USB_FIFO_RX]) > 0) { 33802ac6454SAndrew Thompson 33902ac6454SAndrew Thompson uhid_fill_get_report 34002ac6454SAndrew Thompson (&req, sc->sc_iface_no, UHID_INPUT_REPORT, 34102ac6454SAndrew Thompson sc->sc_iid, sc->sc_isize); 34202ac6454SAndrew Thompson 343ed6d949aSAndrew Thompson usbd_copy_in(pc, 0, &req, sizeof(req)); 34402ac6454SAndrew Thompson 345ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, sizeof(req)); 346ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 1, sc->sc_isize); 347ed6d949aSAndrew Thompson usbd_xfer_set_frames(xfer, sc->sc_isize ? 2 : 1); 348a593f6b8SAndrew Thompson usbd_transfer_submit(xfer); 34902ac6454SAndrew Thompson } 35002ac6454SAndrew Thompson return; 35102ac6454SAndrew Thompson 35202ac6454SAndrew Thompson default: /* Error */ 35302ac6454SAndrew Thompson /* bomb out */ 354a593f6b8SAndrew Thompson usb_fifo_put_data_error(sc->sc_fifo.fp[USB_FIFO_RX]); 35502ac6454SAndrew Thompson return; 35602ac6454SAndrew Thompson } 35702ac6454SAndrew Thompson } 35802ac6454SAndrew Thompson 359760bc48eSAndrew Thompson static const struct usb_config uhid_config[UHID_N_TRANSFER] = { 36002ac6454SAndrew Thompson 3613a38def3SAlexander Motin [UHID_INTR_DT_WR] = { 3623a38def3SAlexander Motin .type = UE_INTERRUPT, 3633a38def3SAlexander Motin .endpoint = UE_ADDR_ANY, 3643a38def3SAlexander Motin .direction = UE_DIR_OUT, 3653dfe1a8eSAlexander Motin .flags = {.pipe_bof = 1,.no_pipe_ok = 1, }, 3663a38def3SAlexander Motin .bufsize = UHID_BSIZE, 3673a38def3SAlexander Motin .callback = &uhid_intr_write_callback, 3683a38def3SAlexander Motin }, 3693a38def3SAlexander Motin 37002ac6454SAndrew Thompson [UHID_INTR_DT_RD] = { 37102ac6454SAndrew Thompson .type = UE_INTERRUPT, 37202ac6454SAndrew Thompson .endpoint = UE_ADDR_ANY, 37302ac6454SAndrew Thompson .direction = UE_DIR_IN, 3744eae601eSAndrew Thompson .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 3754eae601eSAndrew Thompson .bufsize = UHID_BSIZE, 3763a38def3SAlexander Motin .callback = &uhid_intr_read_callback, 37702ac6454SAndrew Thompson }, 37802ac6454SAndrew Thompson 37902ac6454SAndrew Thompson [UHID_CTRL_DT_WR] = { 38002ac6454SAndrew Thompson .type = UE_CONTROL, 38102ac6454SAndrew Thompson .endpoint = 0x00, /* Control pipe */ 38202ac6454SAndrew Thompson .direction = UE_DIR_ANY, 383760bc48eSAndrew Thompson .bufsize = sizeof(struct usb_device_request) + UHID_BSIZE, 3844eae601eSAndrew Thompson .callback = &uhid_write_callback, 3854eae601eSAndrew Thompson .timeout = 1000, /* 1 second */ 38602ac6454SAndrew Thompson }, 38702ac6454SAndrew Thompson 38802ac6454SAndrew Thompson [UHID_CTRL_DT_RD] = { 38902ac6454SAndrew Thompson .type = UE_CONTROL, 39002ac6454SAndrew Thompson .endpoint = 0x00, /* Control pipe */ 39102ac6454SAndrew Thompson .direction = UE_DIR_ANY, 392760bc48eSAndrew Thompson .bufsize = sizeof(struct usb_device_request) + UHID_BSIZE, 3934eae601eSAndrew Thompson .callback = &uhid_read_callback, 3944eae601eSAndrew Thompson .timeout = 1000, /* 1 second */ 39502ac6454SAndrew Thompson }, 39602ac6454SAndrew Thompson }; 39702ac6454SAndrew Thompson 39802ac6454SAndrew Thompson static void 399760bc48eSAndrew Thompson uhid_start_read(struct usb_fifo *fifo) 40002ac6454SAndrew Thompson { 401ed6d949aSAndrew Thompson struct uhid_softc *sc = usb_fifo_softc(fifo); 40202ac6454SAndrew Thompson 40302ac6454SAndrew Thompson if (sc->sc_flags & UHID_FLAG_IMMED) { 404a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[UHID_CTRL_DT_RD]); 40502ac6454SAndrew Thompson } else { 406a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[UHID_INTR_DT_RD]); 40702ac6454SAndrew Thompson } 40802ac6454SAndrew Thompson } 40902ac6454SAndrew Thompson 41002ac6454SAndrew Thompson static void 411760bc48eSAndrew Thompson uhid_stop_read(struct usb_fifo *fifo) 41202ac6454SAndrew Thompson { 413ed6d949aSAndrew Thompson struct uhid_softc *sc = usb_fifo_softc(fifo); 41402ac6454SAndrew Thompson 415a593f6b8SAndrew Thompson usbd_transfer_stop(sc->sc_xfer[UHID_CTRL_DT_RD]); 416a593f6b8SAndrew Thompson usbd_transfer_stop(sc->sc_xfer[UHID_INTR_DT_RD]); 41702ac6454SAndrew Thompson } 41802ac6454SAndrew Thompson 41902ac6454SAndrew Thompson static void 420760bc48eSAndrew Thompson uhid_start_write(struct usb_fifo *fifo) 42102ac6454SAndrew Thompson { 422ed6d949aSAndrew Thompson struct uhid_softc *sc = usb_fifo_softc(fifo); 42302ac6454SAndrew Thompson 4243dfe1a8eSAlexander Motin if ((sc->sc_flags & UHID_FLAG_IMMED) || 4253dfe1a8eSAlexander Motin sc->sc_xfer[UHID_INTR_DT_WR] == NULL) { 426a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[UHID_CTRL_DT_WR]); 4273a38def3SAlexander Motin } else { 4283a38def3SAlexander Motin usbd_transfer_start(sc->sc_xfer[UHID_INTR_DT_WR]); 4293a38def3SAlexander Motin } 43002ac6454SAndrew Thompson } 43102ac6454SAndrew Thompson 43202ac6454SAndrew Thompson static void 433760bc48eSAndrew Thompson uhid_stop_write(struct usb_fifo *fifo) 43402ac6454SAndrew Thompson { 435ed6d949aSAndrew Thompson struct uhid_softc *sc = usb_fifo_softc(fifo); 43602ac6454SAndrew Thompson 437a593f6b8SAndrew Thompson usbd_transfer_stop(sc->sc_xfer[UHID_CTRL_DT_WR]); 4383a38def3SAlexander Motin usbd_transfer_stop(sc->sc_xfer[UHID_INTR_DT_WR]); 43902ac6454SAndrew Thompson } 44002ac6454SAndrew Thompson 44102ac6454SAndrew Thompson static int 44202ac6454SAndrew Thompson uhid_get_report(struct uhid_softc *sc, uint8_t type, 44302ac6454SAndrew Thompson uint8_t id, void *kern_data, void *user_data, 44402ac6454SAndrew Thompson uint16_t len) 44502ac6454SAndrew Thompson { 44602ac6454SAndrew Thompson int err; 44702ac6454SAndrew Thompson uint8_t free_data = 0; 44802ac6454SAndrew Thompson 44902ac6454SAndrew Thompson if (kern_data == NULL) { 45002ac6454SAndrew Thompson kern_data = malloc(len, M_USBDEV, M_WAITOK); 45102ac6454SAndrew Thompson if (kern_data == NULL) { 45202ac6454SAndrew Thompson err = ENOMEM; 45302ac6454SAndrew Thompson goto done; 45402ac6454SAndrew Thompson } 45502ac6454SAndrew Thompson free_data = 1; 45602ac6454SAndrew Thompson } 457a593f6b8SAndrew Thompson err = usbd_req_get_report(sc->sc_udev, NULL, kern_data, 45802ac6454SAndrew Thompson len, sc->sc_iface_index, type, id); 45902ac6454SAndrew Thompson if (err) { 46002ac6454SAndrew Thompson err = ENXIO; 46102ac6454SAndrew Thompson goto done; 46202ac6454SAndrew Thompson } 46302ac6454SAndrew Thompson if (user_data) { 46402ac6454SAndrew Thompson /* dummy buffer */ 46502ac6454SAndrew Thompson err = copyout(kern_data, user_data, len); 46602ac6454SAndrew Thompson if (err) { 46702ac6454SAndrew Thompson goto done; 46802ac6454SAndrew Thompson } 46902ac6454SAndrew Thompson } 47002ac6454SAndrew Thompson done: 47102ac6454SAndrew Thompson if (free_data) { 47202ac6454SAndrew Thompson free(kern_data, M_USBDEV); 47302ac6454SAndrew Thompson } 47402ac6454SAndrew Thompson return (err); 47502ac6454SAndrew Thompson } 47602ac6454SAndrew Thompson 47702ac6454SAndrew Thompson static int 47802ac6454SAndrew Thompson uhid_set_report(struct uhid_softc *sc, uint8_t type, 47902ac6454SAndrew Thompson uint8_t id, void *kern_data, void *user_data, 48002ac6454SAndrew Thompson uint16_t len) 48102ac6454SAndrew Thompson { 48202ac6454SAndrew Thompson int err; 48302ac6454SAndrew Thompson uint8_t free_data = 0; 48402ac6454SAndrew Thompson 48502ac6454SAndrew Thompson if (kern_data == NULL) { 48602ac6454SAndrew Thompson kern_data = malloc(len, M_USBDEV, M_WAITOK); 48702ac6454SAndrew Thompson if (kern_data == NULL) { 48802ac6454SAndrew Thompson err = ENOMEM; 48902ac6454SAndrew Thompson goto done; 49002ac6454SAndrew Thompson } 49102ac6454SAndrew Thompson free_data = 1; 49202ac6454SAndrew Thompson err = copyin(user_data, kern_data, len); 49302ac6454SAndrew Thompson if (err) { 49402ac6454SAndrew Thompson goto done; 49502ac6454SAndrew Thompson } 49602ac6454SAndrew Thompson } 497a593f6b8SAndrew Thompson err = usbd_req_set_report(sc->sc_udev, NULL, kern_data, 49802ac6454SAndrew Thompson len, sc->sc_iface_index, type, id); 49902ac6454SAndrew Thompson if (err) { 50002ac6454SAndrew Thompson err = ENXIO; 50102ac6454SAndrew Thompson goto done; 50202ac6454SAndrew Thompson } 50302ac6454SAndrew Thompson done: 50402ac6454SAndrew Thompson if (free_data) { 50502ac6454SAndrew Thompson free(kern_data, M_USBDEV); 50602ac6454SAndrew Thompson } 50702ac6454SAndrew Thompson return (err); 50802ac6454SAndrew Thompson } 50902ac6454SAndrew Thompson 51002ac6454SAndrew Thompson static int 511760bc48eSAndrew Thompson uhid_open(struct usb_fifo *fifo, int fflags) 51202ac6454SAndrew Thompson { 513ed6d949aSAndrew Thompson struct uhid_softc *sc = usb_fifo_softc(fifo); 51402ac6454SAndrew Thompson 51502ac6454SAndrew Thompson /* 51602ac6454SAndrew Thompson * The buffers are one byte larger than maximum so that one 51702ac6454SAndrew Thompson * can detect too large read/writes and short transfers: 51802ac6454SAndrew Thompson */ 51902ac6454SAndrew Thompson if (fflags & FREAD) { 52002ac6454SAndrew Thompson /* reset flags */ 52102ac6454SAndrew Thompson sc->sc_flags &= ~UHID_FLAG_IMMED; 52202ac6454SAndrew Thompson 523a593f6b8SAndrew Thompson if (usb_fifo_alloc_buffer(fifo, 52402ac6454SAndrew Thompson sc->sc_isize + 1, UHID_FRAME_NUM)) { 52502ac6454SAndrew Thompson return (ENOMEM); 52602ac6454SAndrew Thompson } 52702ac6454SAndrew Thompson } 52802ac6454SAndrew Thompson if (fflags & FWRITE) { 529a593f6b8SAndrew Thompson if (usb_fifo_alloc_buffer(fifo, 53002ac6454SAndrew Thompson sc->sc_osize + 1, UHID_FRAME_NUM)) { 53102ac6454SAndrew Thompson return (ENOMEM); 53202ac6454SAndrew Thompson } 53302ac6454SAndrew Thompson } 53402ac6454SAndrew Thompson return (0); 53502ac6454SAndrew Thompson } 53602ac6454SAndrew Thompson 53702ac6454SAndrew Thompson static void 538760bc48eSAndrew Thompson uhid_close(struct usb_fifo *fifo, int fflags) 53902ac6454SAndrew Thompson { 54002ac6454SAndrew Thompson if (fflags & (FREAD | FWRITE)) { 541a593f6b8SAndrew Thompson usb_fifo_free_buffer(fifo); 54202ac6454SAndrew Thompson } 54302ac6454SAndrew Thompson } 54402ac6454SAndrew Thompson 54502ac6454SAndrew Thompson static int 546760bc48eSAndrew Thompson uhid_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, 547ee3e3ff5SAndrew Thompson int fflags) 54802ac6454SAndrew Thompson { 549ed6d949aSAndrew Thompson struct uhid_softc *sc = usb_fifo_softc(fifo); 550760bc48eSAndrew Thompson struct usb_gen_descriptor *ugd; 55102ac6454SAndrew Thompson uint32_t size; 55202ac6454SAndrew Thompson int error = 0; 55302ac6454SAndrew Thompson uint8_t id; 55402ac6454SAndrew Thompson 55502ac6454SAndrew Thompson switch (cmd) { 55602ac6454SAndrew Thompson case USB_GET_REPORT_DESC: 55702ac6454SAndrew Thompson ugd = addr; 55802ac6454SAndrew Thompson if (sc->sc_repdesc_size > ugd->ugd_maxlen) { 55902ac6454SAndrew Thompson size = ugd->ugd_maxlen; 56002ac6454SAndrew Thompson } else { 56102ac6454SAndrew Thompson size = sc->sc_repdesc_size; 56202ac6454SAndrew Thompson } 56302ac6454SAndrew Thompson ugd->ugd_actlen = size; 56402ac6454SAndrew Thompson if (ugd->ugd_data == NULL) 56502ac6454SAndrew Thompson break; /* descriptor length only */ 56602ac6454SAndrew Thompson error = copyout(sc->sc_repdesc_ptr, ugd->ugd_data, size); 56702ac6454SAndrew Thompson break; 56802ac6454SAndrew Thompson 56902ac6454SAndrew Thompson case USB_SET_IMMED: 57002ac6454SAndrew Thompson if (!(fflags & FREAD)) { 57102ac6454SAndrew Thompson error = EPERM; 57202ac6454SAndrew Thompson break; 57302ac6454SAndrew Thompson } 57402ac6454SAndrew Thompson if (*(int *)addr) { 57502ac6454SAndrew Thompson 57602ac6454SAndrew Thompson /* do a test read */ 57702ac6454SAndrew Thompson 57802ac6454SAndrew Thompson error = uhid_get_report(sc, UHID_INPUT_REPORT, 57902ac6454SAndrew Thompson sc->sc_iid, NULL, NULL, sc->sc_isize); 58002ac6454SAndrew Thompson if (error) { 58102ac6454SAndrew Thompson break; 58202ac6454SAndrew Thompson } 58302ac6454SAndrew Thompson mtx_lock(&sc->sc_mtx); 58402ac6454SAndrew Thompson sc->sc_flags |= UHID_FLAG_IMMED; 58502ac6454SAndrew Thompson mtx_unlock(&sc->sc_mtx); 58602ac6454SAndrew Thompson } else { 58702ac6454SAndrew Thompson mtx_lock(&sc->sc_mtx); 58802ac6454SAndrew Thompson sc->sc_flags &= ~UHID_FLAG_IMMED; 58902ac6454SAndrew Thompson mtx_unlock(&sc->sc_mtx); 59002ac6454SAndrew Thompson } 59102ac6454SAndrew Thompson break; 59202ac6454SAndrew Thompson 59302ac6454SAndrew Thompson case USB_GET_REPORT: 59402ac6454SAndrew Thompson if (!(fflags & FREAD)) { 59502ac6454SAndrew Thompson error = EPERM; 59602ac6454SAndrew Thompson break; 59702ac6454SAndrew Thompson } 59802ac6454SAndrew Thompson ugd = addr; 59902ac6454SAndrew Thompson switch (ugd->ugd_report_type) { 60002ac6454SAndrew Thompson case UHID_INPUT_REPORT: 60102ac6454SAndrew Thompson size = sc->sc_isize; 60202ac6454SAndrew Thompson id = sc->sc_iid; 60302ac6454SAndrew Thompson break; 60402ac6454SAndrew Thompson case UHID_OUTPUT_REPORT: 60502ac6454SAndrew Thompson size = sc->sc_osize; 60602ac6454SAndrew Thompson id = sc->sc_oid; 60702ac6454SAndrew Thompson break; 60802ac6454SAndrew Thompson case UHID_FEATURE_REPORT: 60902ac6454SAndrew Thompson size = sc->sc_fsize; 61002ac6454SAndrew Thompson id = sc->sc_fid; 61102ac6454SAndrew Thompson break; 61202ac6454SAndrew Thompson default: 61302ac6454SAndrew Thompson return (EINVAL); 61402ac6454SAndrew Thompson } 6157778ab7eSAlexander Motin if (id != 0) 6167778ab7eSAlexander Motin copyin(ugd->ugd_data, &id, 1); 61702ac6454SAndrew Thompson error = uhid_get_report(sc, ugd->ugd_report_type, id, 6187778ab7eSAlexander Motin NULL, ugd->ugd_data, imin(ugd->ugd_maxlen, size)); 61902ac6454SAndrew Thompson break; 62002ac6454SAndrew Thompson 62102ac6454SAndrew Thompson case USB_SET_REPORT: 62202ac6454SAndrew Thompson if (!(fflags & FWRITE)) { 62302ac6454SAndrew Thompson error = EPERM; 62402ac6454SAndrew Thompson break; 62502ac6454SAndrew Thompson } 62602ac6454SAndrew Thompson ugd = addr; 62702ac6454SAndrew Thompson switch (ugd->ugd_report_type) { 62802ac6454SAndrew Thompson case UHID_INPUT_REPORT: 62902ac6454SAndrew Thompson size = sc->sc_isize; 63002ac6454SAndrew Thompson id = sc->sc_iid; 63102ac6454SAndrew Thompson break; 63202ac6454SAndrew Thompson case UHID_OUTPUT_REPORT: 63302ac6454SAndrew Thompson size = sc->sc_osize; 63402ac6454SAndrew Thompson id = sc->sc_oid; 63502ac6454SAndrew Thompson break; 63602ac6454SAndrew Thompson case UHID_FEATURE_REPORT: 63702ac6454SAndrew Thompson size = sc->sc_fsize; 63802ac6454SAndrew Thompson id = sc->sc_fid; 63902ac6454SAndrew Thompson break; 64002ac6454SAndrew Thompson default: 64102ac6454SAndrew Thompson return (EINVAL); 64202ac6454SAndrew Thompson } 6437778ab7eSAlexander Motin if (id != 0) 6447778ab7eSAlexander Motin copyin(ugd->ugd_data, &id, 1); 64502ac6454SAndrew Thompson error = uhid_set_report(sc, ugd->ugd_report_type, id, 6467778ab7eSAlexander Motin NULL, ugd->ugd_data, imin(ugd->ugd_maxlen, size)); 64702ac6454SAndrew Thompson break; 64802ac6454SAndrew Thompson 64902ac6454SAndrew Thompson case USB_GET_REPORT_ID: 65002ac6454SAndrew Thompson *(int *)addr = 0; /* XXX: we only support reportid 0? */ 65102ac6454SAndrew Thompson break; 65202ac6454SAndrew Thompson 65302ac6454SAndrew Thompson default: 65402ac6454SAndrew Thompson error = EINVAL; 65502ac6454SAndrew Thompson break; 65602ac6454SAndrew Thompson } 65702ac6454SAndrew Thompson return (error); 65802ac6454SAndrew Thompson } 65902ac6454SAndrew Thompson 660f1a16106SHans Petter Selasky static const STRUCT_USB_HOST_ID uhid_devs[] = { 661f1a16106SHans Petter Selasky /* generic HID class */ 662f1a16106SHans Petter Selasky {USB_IFACE_CLASS(UICLASS_HID),}, 663f1a16106SHans Petter Selasky /* the Xbox 360 gamepad doesn't use the HID class */ 664f1a16106SHans Petter Selasky {USB_IFACE_CLASS(UICLASS_VENDOR), 665f1a16106SHans Petter Selasky USB_IFACE_SUBCLASS(UISUBCLASS_XBOX360_CONTROLLER), 666f1a16106SHans Petter Selasky USB_IFACE_PROTOCOL(UIPROTO_XBOX360_GAMEPAD),}, 667f1a16106SHans Petter Selasky }; 668f1a16106SHans Petter Selasky 66902ac6454SAndrew Thompson static int 67002ac6454SAndrew Thompson uhid_probe(device_t dev) 67102ac6454SAndrew Thompson { 672760bc48eSAndrew Thompson struct usb_attach_arg *uaa = device_get_ivars(dev); 673f1a16106SHans Petter Selasky int error; 67402ac6454SAndrew Thompson 67502ac6454SAndrew Thompson DPRINTFN(11, "\n"); 67602ac6454SAndrew Thompson 677f1a16106SHans Petter Selasky if (uaa->usb_mode != USB_MODE_HOST) 67802ac6454SAndrew Thompson return (ENXIO); 67902ac6454SAndrew Thompson 680f1a16106SHans Petter Selasky error = usbd_lookup_id_by_uaa(uhid_devs, sizeof(uhid_devs), uaa); 681f1a16106SHans Petter Selasky if (error) 682f1a16106SHans Petter Selasky return (error); 68302ac6454SAndrew Thompson 684f1a16106SHans Petter Selasky if (usb_test_quirk(uaa, UQ_HID_IGNORE)) 68502ac6454SAndrew Thompson return (ENXIO); 686f1a16106SHans Petter Selasky 68734e5f64bSHans Petter Selasky /* 68834e5f64bSHans Petter Selasky * Don't attach to mouse and keyboard devices, hence then no 68934e5f64bSHans Petter Selasky * "nomatch" event is generated and then ums and ukbd won't 69034e5f64bSHans Petter Selasky * attach properly when loaded. 69134e5f64bSHans Petter Selasky */ 69234e5f64bSHans Petter Selasky if ((uaa->info.bInterfaceClass == UICLASS_HID) && 69334e5f64bSHans Petter Selasky (uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) && 69468ea9fd7SEitan Adler (((uaa->info.bInterfaceProtocol == UIPROTO_BOOT_KEYBOARD) && 69568ea9fd7SEitan Adler !usb_test_quirk(uaa, UQ_KBD_IGNORE)) || 69668ea9fd7SEitan Adler ((uaa->info.bInterfaceProtocol == UIPROTO_MOUSE) && 69768ea9fd7SEitan Adler !usb_test_quirk(uaa, UQ_UMS_IGNORE)))) 69834e5f64bSHans Petter Selasky return (ENXIO); 69934e5f64bSHans Petter Selasky 700b201cde6SNathan Whitehorn return (BUS_PROBE_GENERIC); 70102ac6454SAndrew Thompson } 70202ac6454SAndrew Thompson 70302ac6454SAndrew Thompson static int 70402ac6454SAndrew Thompson uhid_attach(device_t dev) 70502ac6454SAndrew Thompson { 706760bc48eSAndrew Thompson struct usb_attach_arg *uaa = device_get_ivars(dev); 70702ac6454SAndrew Thompson struct uhid_softc *sc = device_get_softc(dev); 70802ac6454SAndrew Thompson int unit = device_get_unit(dev); 70902ac6454SAndrew Thompson int error = 0; 71002ac6454SAndrew Thompson 71102ac6454SAndrew Thompson DPRINTFN(10, "sc=%p\n", sc); 71202ac6454SAndrew Thompson 713a593f6b8SAndrew Thompson device_set_usb_desc(dev); 71402ac6454SAndrew Thompson 71502ac6454SAndrew Thompson mtx_init(&sc->sc_mtx, "uhid lock", NULL, MTX_DEF | MTX_RECURSE); 71602ac6454SAndrew Thompson 71702ac6454SAndrew Thompson sc->sc_udev = uaa->device; 71802ac6454SAndrew Thompson 71902ac6454SAndrew Thompson sc->sc_iface_no = uaa->info.bIfaceNum; 72002ac6454SAndrew Thompson sc->sc_iface_index = uaa->info.bIfaceIndex; 72102ac6454SAndrew Thompson 722a593f6b8SAndrew Thompson error = usbd_transfer_setup(uaa->device, 72302ac6454SAndrew Thompson &uaa->info.bIfaceIndex, sc->sc_xfer, uhid_config, 72402ac6454SAndrew Thompson UHID_N_TRANSFER, sc, &sc->sc_mtx); 72502ac6454SAndrew Thompson 72602ac6454SAndrew Thompson if (error) { 727a593f6b8SAndrew Thompson DPRINTF("error=%s\n", usbd_errstr(error)); 72802ac6454SAndrew Thompson goto detach; 72902ac6454SAndrew Thompson } 73002ac6454SAndrew Thompson if (uaa->info.idVendor == USB_VENDOR_WACOM) { 73102ac6454SAndrew Thompson 73202ac6454SAndrew Thompson /* the report descriptor for the Wacom Graphire is broken */ 73302ac6454SAndrew Thompson 73402ac6454SAndrew Thompson if (uaa->info.idProduct == USB_PRODUCT_WACOM_GRAPHIRE) { 73502ac6454SAndrew Thompson 73602ac6454SAndrew Thompson sc->sc_repdesc_size = sizeof(uhid_graphire_report_descr); 737*620d4f3cSDimitry Andric sc->sc_repdesc_ptr = __DECONST(void *, &uhid_graphire_report_descr); 73802ac6454SAndrew Thompson sc->sc_flags |= UHID_FLAG_STATIC_DESC; 73902ac6454SAndrew Thompson 74002ac6454SAndrew Thompson } else if (uaa->info.idProduct == USB_PRODUCT_WACOM_GRAPHIRE3_4X5) { 74102ac6454SAndrew Thompson 74202ac6454SAndrew Thompson static uint8_t reportbuf[] = {2, 2, 2}; 74302ac6454SAndrew Thompson 74402ac6454SAndrew Thompson /* 74502ac6454SAndrew Thompson * The Graphire3 needs 0x0202 to be written to 74602ac6454SAndrew Thompson * feature report ID 2 before it'll start 74702ac6454SAndrew Thompson * returning digitizer data. 74802ac6454SAndrew Thompson */ 749a593f6b8SAndrew Thompson error = usbd_req_set_report(uaa->device, NULL, 750296ade60SAndrew Thompson reportbuf, sizeof(reportbuf), 75102ac6454SAndrew Thompson uaa->info.bIfaceIndex, UHID_FEATURE_REPORT, 2); 75202ac6454SAndrew Thompson 75302ac6454SAndrew Thompson if (error) { 75402ac6454SAndrew Thompson DPRINTF("set report failed, error=%s (ignored)\n", 755a593f6b8SAndrew Thompson usbd_errstr(error)); 75602ac6454SAndrew Thompson } 75702ac6454SAndrew Thompson sc->sc_repdesc_size = sizeof(uhid_graphire3_4x5_report_descr); 758*620d4f3cSDimitry Andric sc->sc_repdesc_ptr = __DECONST(void *, &uhid_graphire3_4x5_report_descr); 75902ac6454SAndrew Thompson sc->sc_flags |= UHID_FLAG_STATIC_DESC; 76002ac6454SAndrew Thompson } 76102ac6454SAndrew Thompson } else if ((uaa->info.bInterfaceClass == UICLASS_VENDOR) && 76202ac6454SAndrew Thompson (uaa->info.bInterfaceSubClass == UISUBCLASS_XBOX360_CONTROLLER) && 76302ac6454SAndrew Thompson (uaa->info.bInterfaceProtocol == UIPROTO_XBOX360_GAMEPAD)) { 764817a8cacSHans Petter Selasky static const uint8_t reportbuf[3] = {1, 3, 0}; 765817a8cacSHans Petter Selasky /* 766817a8cacSHans Petter Selasky * Turn off the four LEDs on the gamepad which 767817a8cacSHans Petter Selasky * are blinking by default: 768817a8cacSHans Petter Selasky */ 769817a8cacSHans Petter Selasky error = usbd_req_set_report(uaa->device, NULL, 770817a8cacSHans Petter Selasky __DECONST(void *, reportbuf), sizeof(reportbuf), 771817a8cacSHans Petter Selasky uaa->info.bIfaceIndex, UHID_OUTPUT_REPORT, 0); 772817a8cacSHans Petter Selasky if (error) { 773817a8cacSHans Petter Selasky DPRINTF("set output report failed, error=%s (ignored)\n", 774817a8cacSHans Petter Selasky usbd_errstr(error)); 775817a8cacSHans Petter Selasky } 77602ac6454SAndrew Thompson /* the Xbox 360 gamepad has no report descriptor */ 77702ac6454SAndrew Thompson sc->sc_repdesc_size = sizeof(uhid_xb360gp_report_descr); 778*620d4f3cSDimitry Andric sc->sc_repdesc_ptr = __DECONST(void *, &uhid_xb360gp_report_descr); 77902ac6454SAndrew Thompson sc->sc_flags |= UHID_FLAG_STATIC_DESC; 78002ac6454SAndrew Thompson } 78102ac6454SAndrew Thompson if (sc->sc_repdesc_ptr == NULL) { 78202ac6454SAndrew Thompson 783a593f6b8SAndrew Thompson error = usbd_req_get_hid_desc(uaa->device, NULL, 784296ade60SAndrew Thompson &sc->sc_repdesc_ptr, &sc->sc_repdesc_size, 785296ade60SAndrew Thompson M_USBDEV, uaa->info.bIfaceIndex); 78602ac6454SAndrew Thompson 78702ac6454SAndrew Thompson if (error) { 78802ac6454SAndrew Thompson device_printf(dev, "no report descriptor\n"); 78902ac6454SAndrew Thompson goto detach; 79002ac6454SAndrew Thompson } 79102ac6454SAndrew Thompson } 792a593f6b8SAndrew Thompson error = usbd_req_set_idle(uaa->device, NULL, 79302ac6454SAndrew Thompson uaa->info.bIfaceIndex, 0, 0); 79402ac6454SAndrew Thompson 79502ac6454SAndrew Thompson if (error) { 79602ac6454SAndrew Thompson DPRINTF("set idle failed, error=%s (ignored)\n", 797a593f6b8SAndrew Thompson usbd_errstr(error)); 79802ac6454SAndrew Thompson } 79902ac6454SAndrew Thompson sc->sc_isize = hid_report_size 80002ac6454SAndrew Thompson (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_input, &sc->sc_iid); 80102ac6454SAndrew Thompson 80202ac6454SAndrew Thompson sc->sc_osize = hid_report_size 80302ac6454SAndrew Thompson (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_output, &sc->sc_oid); 80402ac6454SAndrew Thompson 80502ac6454SAndrew Thompson sc->sc_fsize = hid_report_size 80602ac6454SAndrew Thompson (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_feature, &sc->sc_fid); 80702ac6454SAndrew Thompson 80802ac6454SAndrew Thompson if (sc->sc_isize > UHID_BSIZE) { 80902ac6454SAndrew Thompson DPRINTF("input size is too large, " 81002ac6454SAndrew Thompson "%d bytes (truncating)\n", 81102ac6454SAndrew Thompson sc->sc_isize); 81202ac6454SAndrew Thompson sc->sc_isize = UHID_BSIZE; 81302ac6454SAndrew Thompson } 81402ac6454SAndrew Thompson if (sc->sc_osize > UHID_BSIZE) { 81502ac6454SAndrew Thompson DPRINTF("output size is too large, " 81602ac6454SAndrew Thompson "%d bytes (truncating)\n", 81702ac6454SAndrew Thompson sc->sc_osize); 81802ac6454SAndrew Thompson sc->sc_osize = UHID_BSIZE; 81902ac6454SAndrew Thompson } 82002ac6454SAndrew Thompson if (sc->sc_fsize > UHID_BSIZE) { 82102ac6454SAndrew Thompson DPRINTF("feature size is too large, " 82202ac6454SAndrew Thompson "%d bytes (truncating)\n", 82302ac6454SAndrew Thompson sc->sc_fsize); 82402ac6454SAndrew Thompson sc->sc_fsize = UHID_BSIZE; 82502ac6454SAndrew Thompson } 82602ac6454SAndrew Thompson 827a593f6b8SAndrew Thompson error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx, 82802ac6454SAndrew Thompson &uhid_fifo_methods, &sc->sc_fifo, 8296d917491SHans Petter Selasky unit, -1, uaa->info.bIfaceIndex, 830ee3e3ff5SAndrew Thompson UID_ROOT, GID_OPERATOR, 0644); 83102ac6454SAndrew Thompson if (error) { 83202ac6454SAndrew Thompson goto detach; 83302ac6454SAndrew Thompson } 83402ac6454SAndrew Thompson return (0); /* success */ 83502ac6454SAndrew Thompson 83602ac6454SAndrew Thompson detach: 83702ac6454SAndrew Thompson uhid_detach(dev); 83802ac6454SAndrew Thompson return (ENOMEM); 83902ac6454SAndrew Thompson } 84002ac6454SAndrew Thompson 84102ac6454SAndrew Thompson static int 84202ac6454SAndrew Thompson uhid_detach(device_t dev) 84302ac6454SAndrew Thompson { 84402ac6454SAndrew Thompson struct uhid_softc *sc = device_get_softc(dev); 84502ac6454SAndrew Thompson 846a593f6b8SAndrew Thompson usb_fifo_detach(&sc->sc_fifo); 84702ac6454SAndrew Thompson 848a593f6b8SAndrew Thompson usbd_transfer_unsetup(sc->sc_xfer, UHID_N_TRANSFER); 84902ac6454SAndrew Thompson 85002ac6454SAndrew Thompson if (sc->sc_repdesc_ptr) { 85102ac6454SAndrew Thompson if (!(sc->sc_flags & UHID_FLAG_STATIC_DESC)) { 85202ac6454SAndrew Thompson free(sc->sc_repdesc_ptr, M_USBDEV); 85302ac6454SAndrew Thompson } 85402ac6454SAndrew Thompson } 85502ac6454SAndrew Thompson mtx_destroy(&sc->sc_mtx); 85602ac6454SAndrew Thompson 85702ac6454SAndrew Thompson return (0); 85802ac6454SAndrew Thompson } 85902ac6454SAndrew Thompson 86002ac6454SAndrew Thompson static devclass_t uhid_devclass; 86102ac6454SAndrew Thompson 86202ac6454SAndrew Thompson static device_method_t uhid_methods[] = { 86302ac6454SAndrew Thompson DEVMETHOD(device_probe, uhid_probe), 86402ac6454SAndrew Thompson DEVMETHOD(device_attach, uhid_attach), 86502ac6454SAndrew Thompson DEVMETHOD(device_detach, uhid_detach), 86661bfd867SSofian Brabez 86761bfd867SSofian Brabez DEVMETHOD_END 86802ac6454SAndrew Thompson }; 86902ac6454SAndrew Thompson 87002ac6454SAndrew Thompson static driver_t uhid_driver = { 87102ac6454SAndrew Thompson .name = "uhid", 87202ac6454SAndrew Thompson .methods = uhid_methods, 87302ac6454SAndrew Thompson .size = sizeof(struct uhid_softc), 87402ac6454SAndrew Thompson }; 87502ac6454SAndrew Thompson 8769aef556dSAndrew Thompson DRIVER_MODULE(uhid, uhub, uhid_driver, uhid_devclass, NULL, 0); 87702ac6454SAndrew Thompson MODULE_DEPEND(uhid, usb, 1, 1, 1); 878910cb8feSAndrew Thompson MODULE_VERSION(uhid, 1); 879