102ac6454SAndrew Thompson /* $NetBSD: uhid.c,v 1.46 2001/11/13 06:24:55 lukem Exp $ */ 202ac6454SAndrew Thompson 302ac6454SAndrew Thompson /* Also already merged from NetBSD: 402ac6454SAndrew Thompson * $NetBSD: uhid.c,v 1.54 2002/09/23 05:51:21 simonb Exp $ 502ac6454SAndrew Thompson */ 602ac6454SAndrew Thompson 702ac6454SAndrew Thompson #include <sys/cdefs.h> 802ac6454SAndrew Thompson __FBSDID("$FreeBSD$"); 902ac6454SAndrew Thompson 1002ac6454SAndrew Thompson /*- 1102ac6454SAndrew Thompson * Copyright (c) 1998 The NetBSD Foundation, Inc. 1202ac6454SAndrew Thompson * All rights reserved. 1302ac6454SAndrew Thompson * 1402ac6454SAndrew Thompson * This code is derived from software contributed to The NetBSD Foundation 1502ac6454SAndrew Thompson * by Lennart Augustsson (lennart@augustsson.net) at 1602ac6454SAndrew Thompson * Carlstedt Research & Technology. 1702ac6454SAndrew Thompson * 1802ac6454SAndrew Thompson * Redistribution and use in source and binary forms, with or without 1902ac6454SAndrew Thompson * modification, are permitted provided that the following conditions 2002ac6454SAndrew Thompson * are met: 2102ac6454SAndrew Thompson * 1. Redistributions of source code must retain the above copyright 2202ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer. 2302ac6454SAndrew Thompson * 2. Redistributions in binary form must reproduce the above copyright 2402ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer in the 2502ac6454SAndrew Thompson * documentation and/or other materials provided with the distribution. 2602ac6454SAndrew Thompson * 3. All advertising materials mentioning features or use of this software 2702ac6454SAndrew Thompson * must display the following acknowledgement: 2802ac6454SAndrew Thompson * This product includes software developed by the NetBSD 2902ac6454SAndrew Thompson * Foundation, Inc. and its contributors. 3002ac6454SAndrew Thompson * 4. Neither the name of The NetBSD Foundation nor the names of its 3102ac6454SAndrew Thompson * contributors may be used to endorse or promote products derived 3202ac6454SAndrew Thompson * from this software without specific prior written permission. 3302ac6454SAndrew Thompson * 3402ac6454SAndrew Thompson * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 3502ac6454SAndrew Thompson * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 3602ac6454SAndrew Thompson * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 3702ac6454SAndrew Thompson * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 3802ac6454SAndrew Thompson * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 3902ac6454SAndrew Thompson * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 4002ac6454SAndrew Thompson * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 4102ac6454SAndrew Thompson * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 4202ac6454SAndrew Thompson * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 4302ac6454SAndrew Thompson * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 4402ac6454SAndrew Thompson * POSSIBILITY OF SUCH DAMAGE. 4502ac6454SAndrew Thompson */ 4602ac6454SAndrew Thompson 4702ac6454SAndrew Thompson /* 4802ac6454SAndrew Thompson * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf 4902ac6454SAndrew Thompson */ 5002ac6454SAndrew Thompson 51ed6d949aSAndrew Thompson #include <sys/stdint.h> 52ed6d949aSAndrew Thompson #include <sys/stddef.h> 53ed6d949aSAndrew Thompson #include <sys/param.h> 54ed6d949aSAndrew Thompson #include <sys/queue.h> 55ed6d949aSAndrew Thompson #include <sys/types.h> 56ed6d949aSAndrew Thompson #include <sys/systm.h> 57ed6d949aSAndrew Thompson #include <sys/kernel.h> 58ed6d949aSAndrew Thompson #include <sys/bus.h> 59ed6d949aSAndrew Thompson #include <sys/linker_set.h> 60ed6d949aSAndrew Thompson #include <sys/module.h> 61ed6d949aSAndrew Thompson #include <sys/lock.h> 62ed6d949aSAndrew Thompson #include <sys/mutex.h> 63ed6d949aSAndrew Thompson #include <sys/condvar.h> 64ed6d949aSAndrew Thompson #include <sys/sysctl.h> 65ed6d949aSAndrew Thompson #include <sys/sx.h> 66ed6d949aSAndrew Thompson #include <sys/unistd.h> 67ed6d949aSAndrew Thompson #include <sys/callout.h> 68ed6d949aSAndrew Thompson #include <sys/malloc.h> 69ed6d949aSAndrew Thompson #include <sys/priv.h> 70ed6d949aSAndrew Thompson #include <sys/conf.h> 71ed6d949aSAndrew Thompson #include <sys/fcntl.h> 72ed6d949aSAndrew Thompson 7302ac6454SAndrew Thompson #include "usbdevs.h" 7402ac6454SAndrew Thompson #include <dev/usb/usb.h> 75ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h> 76ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h> 7702ac6454SAndrew Thompson #include <dev/usb/usbhid.h> 7802ac6454SAndrew Thompson #include <dev/usb/usb_ioctl.h> 7902ac6454SAndrew Thompson 8002ac6454SAndrew Thompson #define USB_DEBUG_VAR uhid_debug 8102ac6454SAndrew Thompson #include <dev/usb/usb_debug.h> 8202ac6454SAndrew Thompson 8302ac6454SAndrew Thompson #include <dev/usb/input/usb_rdesc.h> 8402ac6454SAndrew Thompson #include <dev/usb/quirk/usb_quirk.h> 8502ac6454SAndrew Thompson 8602ac6454SAndrew Thompson #if USB_DEBUG 8702ac6454SAndrew Thompson static int uhid_debug = 0; 8802ac6454SAndrew Thompson 899360ae40SAndrew Thompson SYSCTL_NODE(_hw_usb, OID_AUTO, uhid, CTLFLAG_RW, 0, "USB uhid"); 909360ae40SAndrew Thompson SYSCTL_INT(_hw_usb_uhid, OID_AUTO, debug, CTLFLAG_RW, 9102ac6454SAndrew Thompson &uhid_debug, 0, "Debug level"); 9202ac6454SAndrew Thompson #endif 9302ac6454SAndrew Thompson 9402ac6454SAndrew Thompson #define UHID_BSIZE 1024 /* bytes, buffer size */ 9502ac6454SAndrew Thompson #define UHID_FRAME_NUM 50 /* bytes, frame number */ 9602ac6454SAndrew Thompson 9702ac6454SAndrew Thompson enum { 9802ac6454SAndrew Thompson UHID_INTR_DT_RD, 9902ac6454SAndrew Thompson UHID_CTRL_DT_WR, 10002ac6454SAndrew Thompson UHID_CTRL_DT_RD, 10102ac6454SAndrew Thompson UHID_N_TRANSFER, 10202ac6454SAndrew Thompson }; 10302ac6454SAndrew Thompson 10402ac6454SAndrew Thompson struct uhid_softc { 105760bc48eSAndrew Thompson struct usb_fifo_sc sc_fifo; 10602ac6454SAndrew Thompson struct mtx sc_mtx; 10702ac6454SAndrew Thompson 108760bc48eSAndrew Thompson struct usb_xfer *sc_xfer[UHID_N_TRANSFER]; 109760bc48eSAndrew Thompson struct usb_device *sc_udev; 11002ac6454SAndrew Thompson void *sc_repdesc_ptr; 11102ac6454SAndrew Thompson 11202ac6454SAndrew Thompson uint32_t sc_isize; 11302ac6454SAndrew Thompson uint32_t sc_osize; 11402ac6454SAndrew Thompson uint32_t sc_fsize; 11502ac6454SAndrew Thompson 11602ac6454SAndrew Thompson uint16_t sc_repdesc_size; 11702ac6454SAndrew Thompson 11802ac6454SAndrew Thompson uint8_t sc_iface_no; 11902ac6454SAndrew Thompson uint8_t sc_iface_index; 12002ac6454SAndrew Thompson uint8_t sc_iid; 12102ac6454SAndrew Thompson uint8_t sc_oid; 12202ac6454SAndrew Thompson uint8_t sc_fid; 12302ac6454SAndrew Thompson uint8_t sc_flags; 12402ac6454SAndrew Thompson #define UHID_FLAG_IMMED 0x01 /* set if read should be immediate */ 12502ac6454SAndrew Thompson #define UHID_FLAG_STATIC_DESC 0x04 /* set if report descriptors are 12602ac6454SAndrew Thompson * static */ 12702ac6454SAndrew Thompson }; 12802ac6454SAndrew Thompson 12902ac6454SAndrew Thompson static const uint8_t uhid_xb360gp_report_descr[] = {UHID_XB360GP_REPORT_DESCR()}; 13002ac6454SAndrew Thompson static const uint8_t uhid_graphire_report_descr[] = {UHID_GRAPHIRE_REPORT_DESCR()}; 13102ac6454SAndrew Thompson static const uint8_t uhid_graphire3_4x5_report_descr[] = {UHID_GRAPHIRE3_4X5_REPORT_DESCR()}; 13202ac6454SAndrew Thompson 13302ac6454SAndrew Thompson /* prototypes */ 13402ac6454SAndrew Thompson 13502ac6454SAndrew Thompson static device_probe_t uhid_probe; 13602ac6454SAndrew Thompson static device_attach_t uhid_attach; 13702ac6454SAndrew Thompson static device_detach_t uhid_detach; 13802ac6454SAndrew Thompson 139e0a69b51SAndrew Thompson static usb_callback_t uhid_intr_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; 15002ac6454SAndrew Thompson 151760bc48eSAndrew Thompson static struct usb_fifo_methods uhid_fifo_methods = { 15202ac6454SAndrew Thompson .f_open = &uhid_open, 15302ac6454SAndrew Thompson .f_close = &uhid_close, 15402ac6454SAndrew Thompson .f_ioctl = &uhid_ioctl, 15502ac6454SAndrew Thompson .f_start_read = &uhid_start_read, 15602ac6454SAndrew Thompson .f_stop_read = &uhid_stop_read, 15702ac6454SAndrew Thompson .f_start_write = &uhid_start_write, 15802ac6454SAndrew Thompson .f_stop_write = &uhid_stop_write, 15902ac6454SAndrew Thompson .basename[0] = "uhid", 16002ac6454SAndrew Thompson }; 16102ac6454SAndrew Thompson 16202ac6454SAndrew Thompson static void 163ed6d949aSAndrew Thompson uhid_intr_callback(struct usb_xfer *xfer, usb_error_t error) 16402ac6454SAndrew Thompson { 165ed6d949aSAndrew Thompson struct uhid_softc *sc = usbd_xfer_softc(xfer); 166ed6d949aSAndrew Thompson struct usb_page_cache *pc; 167ed6d949aSAndrew Thompson int actlen; 168ed6d949aSAndrew Thompson 169ed6d949aSAndrew Thompson usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 17002ac6454SAndrew Thompson 17102ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) { 17202ac6454SAndrew Thompson case USB_ST_TRANSFERRED: 17302ac6454SAndrew Thompson DPRINTF("transferred!\n"); 17402ac6454SAndrew Thompson 175ed6d949aSAndrew Thompson pc = usbd_xfer_get_frame(xfer, 0); 1760ca80d71SAndrew Thompson 1770ca80d71SAndrew Thompson /* 1780ca80d71SAndrew Thompson * If the ID byte is non zero we allow descriptors 1790ca80d71SAndrew Thompson * having multiple sizes: 1800ca80d71SAndrew Thompson */ 1810ca80d71SAndrew Thompson if ((actlen >= sc->sc_isize) || 1820ca80d71SAndrew Thompson ((actlen > 0) && (sc->sc_iid != 0))) { 1830ca80d71SAndrew Thompson /* limit report length to the maximum */ 1840ca80d71SAndrew Thompson if (actlen > sc->sc_isize) 1850ca80d71SAndrew Thompson actlen = sc->sc_isize; 186ed6d949aSAndrew Thompson usb_fifo_put_data(sc->sc_fifo.fp[USB_FIFO_RX], pc, 1870ca80d71SAndrew Thompson 0, actlen, 1); 18802ac6454SAndrew Thompson } else { 18902ac6454SAndrew Thompson /* ignore it */ 1900ca80d71SAndrew Thompson DPRINTF("ignored transfer, %d bytes\n", actlen); 19102ac6454SAndrew Thompson } 19202ac6454SAndrew Thompson 19302ac6454SAndrew Thompson case USB_ST_SETUP: 19402ac6454SAndrew Thompson re_submit: 195a593f6b8SAndrew Thompson if (usb_fifo_put_bytes_max( 19602ac6454SAndrew Thompson sc->sc_fifo.fp[USB_FIFO_RX]) != 0) { 197ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, sc->sc_isize); 198a593f6b8SAndrew Thompson usbd_transfer_submit(xfer); 19902ac6454SAndrew Thompson } 20002ac6454SAndrew Thompson return; 20102ac6454SAndrew Thompson 20202ac6454SAndrew Thompson default: /* Error */ 203ed6d949aSAndrew Thompson if (error != USB_ERR_CANCELLED) { 20402ac6454SAndrew Thompson /* try to clear stall first */ 205ed6d949aSAndrew Thompson usbd_xfer_set_stall(xfer); 20602ac6454SAndrew Thompson goto re_submit; 20702ac6454SAndrew Thompson } 20802ac6454SAndrew Thompson return; 20902ac6454SAndrew Thompson } 21002ac6454SAndrew Thompson } 21102ac6454SAndrew Thompson 21202ac6454SAndrew Thompson static void 213760bc48eSAndrew Thompson uhid_fill_set_report(struct usb_device_request *req, uint8_t iface_no, 21402ac6454SAndrew Thompson uint8_t type, uint8_t id, uint16_t size) 21502ac6454SAndrew Thompson { 21602ac6454SAndrew Thompson req->bmRequestType = UT_WRITE_CLASS_INTERFACE; 21702ac6454SAndrew Thompson req->bRequest = UR_SET_REPORT; 21802ac6454SAndrew Thompson USETW2(req->wValue, type, id); 21902ac6454SAndrew Thompson req->wIndex[0] = iface_no; 22002ac6454SAndrew Thompson req->wIndex[1] = 0; 22102ac6454SAndrew Thompson USETW(req->wLength, size); 22202ac6454SAndrew Thompson } 22302ac6454SAndrew Thompson 22402ac6454SAndrew Thompson static void 225760bc48eSAndrew Thompson uhid_fill_get_report(struct usb_device_request *req, uint8_t iface_no, 22602ac6454SAndrew Thompson uint8_t type, uint8_t id, uint16_t size) 22702ac6454SAndrew Thompson { 22802ac6454SAndrew Thompson req->bmRequestType = UT_READ_CLASS_INTERFACE; 22902ac6454SAndrew Thompson req->bRequest = UR_GET_REPORT; 23002ac6454SAndrew Thompson USETW2(req->wValue, type, id); 23102ac6454SAndrew Thompson req->wIndex[0] = iface_no; 23202ac6454SAndrew Thompson req->wIndex[1] = 0; 23302ac6454SAndrew Thompson USETW(req->wLength, size); 23402ac6454SAndrew Thompson } 23502ac6454SAndrew Thompson 23602ac6454SAndrew Thompson static void 237ed6d949aSAndrew Thompson uhid_write_callback(struct usb_xfer *xfer, usb_error_t error) 23802ac6454SAndrew Thompson { 239ed6d949aSAndrew Thompson struct uhid_softc *sc = usbd_xfer_softc(xfer); 240760bc48eSAndrew Thompson struct usb_device_request req; 241ed6d949aSAndrew Thompson struct usb_page_cache *pc; 24202ac6454SAndrew Thompson uint32_t size = sc->sc_osize; 24302ac6454SAndrew Thompson uint32_t actlen; 24402ac6454SAndrew Thompson uint8_t id; 24502ac6454SAndrew Thompson 24602ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) { 24702ac6454SAndrew Thompson case USB_ST_TRANSFERRED: 24802ac6454SAndrew Thompson case USB_ST_SETUP: 24902ac6454SAndrew Thompson /* try to extract the ID byte */ 25002ac6454SAndrew Thompson if (sc->sc_oid) { 251ed6d949aSAndrew Thompson pc = usbd_xfer_get_frame(xfer, 0); 252ed6d949aSAndrew Thompson if (usb_fifo_get_data(sc->sc_fifo.fp[USB_FIFO_TX], pc, 25302ac6454SAndrew Thompson 0, 1, &actlen, 0)) { 25402ac6454SAndrew Thompson if (actlen != 1) { 25502ac6454SAndrew Thompson goto tr_error; 25602ac6454SAndrew Thompson } 257ed6d949aSAndrew Thompson usbd_copy_out(pc, 0, &id, 1); 25802ac6454SAndrew Thompson 25902ac6454SAndrew Thompson } else { 26002ac6454SAndrew Thompson return; 26102ac6454SAndrew Thompson } 26202ac6454SAndrew Thompson if (size) { 26302ac6454SAndrew Thompson size--; 26402ac6454SAndrew Thompson } 26502ac6454SAndrew Thompson } else { 26602ac6454SAndrew Thompson id = 0; 26702ac6454SAndrew Thompson } 26802ac6454SAndrew Thompson 269ed6d949aSAndrew Thompson pc = usbd_xfer_get_frame(xfer, 1); 270ed6d949aSAndrew Thompson if (usb_fifo_get_data(sc->sc_fifo.fp[USB_FIFO_TX], pc, 27102ac6454SAndrew Thompson 0, UHID_BSIZE, &actlen, 1)) { 27202ac6454SAndrew Thompson if (actlen != size) { 27302ac6454SAndrew Thompson goto tr_error; 27402ac6454SAndrew Thompson } 27502ac6454SAndrew Thompson uhid_fill_set_report 27602ac6454SAndrew Thompson (&req, sc->sc_iface_no, 27702ac6454SAndrew Thompson UHID_OUTPUT_REPORT, id, size); 27802ac6454SAndrew Thompson 279ed6d949aSAndrew Thompson pc = usbd_xfer_get_frame(xfer, 0); 280ed6d949aSAndrew Thompson usbd_copy_in(pc, 0, &req, sizeof(req)); 28102ac6454SAndrew Thompson 282ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, sizeof(req)); 283ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 1, size); 284ed6d949aSAndrew Thompson usbd_xfer_set_frames(xfer, size ? 2 : 1); 285a593f6b8SAndrew Thompson usbd_transfer_submit(xfer); 28602ac6454SAndrew Thompson } 28702ac6454SAndrew Thompson return; 28802ac6454SAndrew Thompson 28902ac6454SAndrew Thompson default: 29002ac6454SAndrew Thompson tr_error: 29102ac6454SAndrew Thompson /* bomb out */ 292a593f6b8SAndrew Thompson usb_fifo_get_data_error(sc->sc_fifo.fp[USB_FIFO_TX]); 29302ac6454SAndrew Thompson return; 29402ac6454SAndrew Thompson } 29502ac6454SAndrew Thompson } 29602ac6454SAndrew Thompson 29702ac6454SAndrew Thompson static void 298ed6d949aSAndrew Thompson uhid_read_callback(struct usb_xfer *xfer, usb_error_t error) 29902ac6454SAndrew Thompson { 300ed6d949aSAndrew Thompson struct uhid_softc *sc = usbd_xfer_softc(xfer); 301760bc48eSAndrew Thompson struct usb_device_request req; 302ed6d949aSAndrew Thompson struct usb_page_cache *pc; 303ed6d949aSAndrew Thompson 304ed6d949aSAndrew Thompson pc = usbd_xfer_get_frame(xfer, 0); 30502ac6454SAndrew Thompson 30602ac6454SAndrew Thompson switch (USB_GET_STATE(xfer)) { 30702ac6454SAndrew Thompson case USB_ST_TRANSFERRED: 308ed6d949aSAndrew Thompson usb_fifo_put_data(sc->sc_fifo.fp[USB_FIFO_RX], pc, sizeof(req), 309ed6d949aSAndrew Thompson sc->sc_isize, 1); 31002ac6454SAndrew Thompson return; 31102ac6454SAndrew Thompson 31202ac6454SAndrew Thompson case USB_ST_SETUP: 31302ac6454SAndrew Thompson 314a593f6b8SAndrew Thompson if (usb_fifo_put_bytes_max(sc->sc_fifo.fp[USB_FIFO_RX]) > 0) { 31502ac6454SAndrew Thompson 31602ac6454SAndrew Thompson uhid_fill_get_report 31702ac6454SAndrew Thompson (&req, sc->sc_iface_no, UHID_INPUT_REPORT, 31802ac6454SAndrew Thompson sc->sc_iid, sc->sc_isize); 31902ac6454SAndrew Thompson 320ed6d949aSAndrew Thompson usbd_copy_in(pc, 0, &req, sizeof(req)); 32102ac6454SAndrew Thompson 322ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 0, sizeof(req)); 323ed6d949aSAndrew Thompson usbd_xfer_set_frame_len(xfer, 1, sc->sc_isize); 324ed6d949aSAndrew Thompson usbd_xfer_set_frames(xfer, sc->sc_isize ? 2 : 1); 325a593f6b8SAndrew Thompson usbd_transfer_submit(xfer); 32602ac6454SAndrew Thompson } 32702ac6454SAndrew Thompson return; 32802ac6454SAndrew Thompson 32902ac6454SAndrew Thompson default: /* Error */ 33002ac6454SAndrew Thompson /* bomb out */ 331a593f6b8SAndrew Thompson usb_fifo_put_data_error(sc->sc_fifo.fp[USB_FIFO_RX]); 33202ac6454SAndrew Thompson return; 33302ac6454SAndrew Thompson } 33402ac6454SAndrew Thompson } 33502ac6454SAndrew Thompson 336760bc48eSAndrew Thompson static const struct usb_config uhid_config[UHID_N_TRANSFER] = { 33702ac6454SAndrew Thompson 33802ac6454SAndrew Thompson [UHID_INTR_DT_RD] = { 33902ac6454SAndrew Thompson .type = UE_INTERRUPT, 34002ac6454SAndrew Thompson .endpoint = UE_ADDR_ANY, 34102ac6454SAndrew Thompson .direction = UE_DIR_IN, 3424eae601eSAndrew Thompson .flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 3434eae601eSAndrew Thompson .bufsize = UHID_BSIZE, 3444eae601eSAndrew Thompson .callback = &uhid_intr_callback, 34502ac6454SAndrew Thompson }, 34602ac6454SAndrew Thompson 34702ac6454SAndrew Thompson [UHID_CTRL_DT_WR] = { 34802ac6454SAndrew Thompson .type = UE_CONTROL, 34902ac6454SAndrew Thompson .endpoint = 0x00, /* Control pipe */ 35002ac6454SAndrew Thompson .direction = UE_DIR_ANY, 351760bc48eSAndrew Thompson .bufsize = sizeof(struct usb_device_request) + UHID_BSIZE, 3524eae601eSAndrew Thompson .callback = &uhid_write_callback, 3534eae601eSAndrew Thompson .timeout = 1000, /* 1 second */ 35402ac6454SAndrew Thompson }, 35502ac6454SAndrew Thompson 35602ac6454SAndrew Thompson [UHID_CTRL_DT_RD] = { 35702ac6454SAndrew Thompson .type = UE_CONTROL, 35802ac6454SAndrew Thompson .endpoint = 0x00, /* Control pipe */ 35902ac6454SAndrew Thompson .direction = UE_DIR_ANY, 360760bc48eSAndrew Thompson .bufsize = sizeof(struct usb_device_request) + UHID_BSIZE, 3614eae601eSAndrew Thompson .callback = &uhid_read_callback, 3624eae601eSAndrew Thompson .timeout = 1000, /* 1 second */ 36302ac6454SAndrew Thompson }, 36402ac6454SAndrew Thompson }; 36502ac6454SAndrew Thompson 36602ac6454SAndrew Thompson static void 367760bc48eSAndrew Thompson uhid_start_read(struct usb_fifo *fifo) 36802ac6454SAndrew Thompson { 369ed6d949aSAndrew Thompson struct uhid_softc *sc = usb_fifo_softc(fifo); 37002ac6454SAndrew Thompson 37102ac6454SAndrew Thompson if (sc->sc_flags & UHID_FLAG_IMMED) { 372a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[UHID_CTRL_DT_RD]); 37302ac6454SAndrew Thompson } else { 374a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[UHID_INTR_DT_RD]); 37502ac6454SAndrew Thompson } 37602ac6454SAndrew Thompson } 37702ac6454SAndrew Thompson 37802ac6454SAndrew Thompson static void 379760bc48eSAndrew Thompson uhid_stop_read(struct usb_fifo *fifo) 38002ac6454SAndrew Thompson { 381ed6d949aSAndrew Thompson struct uhid_softc *sc = usb_fifo_softc(fifo); 38202ac6454SAndrew Thompson 383a593f6b8SAndrew Thompson usbd_transfer_stop(sc->sc_xfer[UHID_CTRL_DT_RD]); 384a593f6b8SAndrew Thompson usbd_transfer_stop(sc->sc_xfer[UHID_INTR_DT_RD]); 38502ac6454SAndrew Thompson } 38602ac6454SAndrew Thompson 38702ac6454SAndrew Thompson static void 388760bc48eSAndrew Thompson uhid_start_write(struct usb_fifo *fifo) 38902ac6454SAndrew Thompson { 390ed6d949aSAndrew Thompson struct uhid_softc *sc = usb_fifo_softc(fifo); 39102ac6454SAndrew Thompson 392a593f6b8SAndrew Thompson usbd_transfer_start(sc->sc_xfer[UHID_CTRL_DT_WR]); 39302ac6454SAndrew Thompson } 39402ac6454SAndrew Thompson 39502ac6454SAndrew Thompson static void 396760bc48eSAndrew Thompson uhid_stop_write(struct usb_fifo *fifo) 39702ac6454SAndrew Thompson { 398ed6d949aSAndrew Thompson struct uhid_softc *sc = usb_fifo_softc(fifo); 39902ac6454SAndrew Thompson 400a593f6b8SAndrew Thompson usbd_transfer_stop(sc->sc_xfer[UHID_CTRL_DT_WR]); 40102ac6454SAndrew Thompson } 40202ac6454SAndrew Thompson 40302ac6454SAndrew Thompson static int 40402ac6454SAndrew Thompson uhid_get_report(struct uhid_softc *sc, uint8_t type, 40502ac6454SAndrew Thompson uint8_t id, void *kern_data, void *user_data, 40602ac6454SAndrew Thompson uint16_t len) 40702ac6454SAndrew Thompson { 40802ac6454SAndrew Thompson int err; 40902ac6454SAndrew Thompson uint8_t free_data = 0; 41002ac6454SAndrew Thompson 41102ac6454SAndrew Thompson if (kern_data == NULL) { 41202ac6454SAndrew Thompson kern_data = malloc(len, M_USBDEV, M_WAITOK); 41302ac6454SAndrew Thompson if (kern_data == NULL) { 41402ac6454SAndrew Thompson err = ENOMEM; 41502ac6454SAndrew Thompson goto done; 41602ac6454SAndrew Thompson } 41702ac6454SAndrew Thompson free_data = 1; 41802ac6454SAndrew Thompson } 419a593f6b8SAndrew Thompson err = usbd_req_get_report(sc->sc_udev, NULL, kern_data, 42002ac6454SAndrew Thompson len, sc->sc_iface_index, type, id); 42102ac6454SAndrew Thompson if (err) { 42202ac6454SAndrew Thompson err = ENXIO; 42302ac6454SAndrew Thompson goto done; 42402ac6454SAndrew Thompson } 42502ac6454SAndrew Thompson if (user_data) { 42602ac6454SAndrew Thompson /* dummy buffer */ 42702ac6454SAndrew Thompson err = copyout(kern_data, user_data, len); 42802ac6454SAndrew Thompson if (err) { 42902ac6454SAndrew Thompson goto done; 43002ac6454SAndrew Thompson } 43102ac6454SAndrew Thompson } 43202ac6454SAndrew Thompson done: 43302ac6454SAndrew Thompson if (free_data) { 43402ac6454SAndrew Thompson free(kern_data, M_USBDEV); 43502ac6454SAndrew Thompson } 43602ac6454SAndrew Thompson return (err); 43702ac6454SAndrew Thompson } 43802ac6454SAndrew Thompson 43902ac6454SAndrew Thompson static int 44002ac6454SAndrew Thompson uhid_set_report(struct uhid_softc *sc, uint8_t type, 44102ac6454SAndrew Thompson uint8_t id, void *kern_data, void *user_data, 44202ac6454SAndrew Thompson uint16_t len) 44302ac6454SAndrew Thompson { 44402ac6454SAndrew Thompson int err; 44502ac6454SAndrew Thompson uint8_t free_data = 0; 44602ac6454SAndrew Thompson 44702ac6454SAndrew Thompson if (kern_data == NULL) { 44802ac6454SAndrew Thompson kern_data = malloc(len, M_USBDEV, M_WAITOK); 44902ac6454SAndrew Thompson if (kern_data == NULL) { 45002ac6454SAndrew Thompson err = ENOMEM; 45102ac6454SAndrew Thompson goto done; 45202ac6454SAndrew Thompson } 45302ac6454SAndrew Thompson free_data = 1; 45402ac6454SAndrew Thompson err = copyin(user_data, kern_data, len); 45502ac6454SAndrew Thompson if (err) { 45602ac6454SAndrew Thompson goto done; 45702ac6454SAndrew Thompson } 45802ac6454SAndrew Thompson } 459a593f6b8SAndrew Thompson err = usbd_req_set_report(sc->sc_udev, NULL, kern_data, 46002ac6454SAndrew Thompson len, sc->sc_iface_index, type, id); 46102ac6454SAndrew Thompson if (err) { 46202ac6454SAndrew Thompson err = ENXIO; 46302ac6454SAndrew Thompson goto done; 46402ac6454SAndrew Thompson } 46502ac6454SAndrew Thompson done: 46602ac6454SAndrew Thompson if (free_data) { 46702ac6454SAndrew Thompson free(kern_data, M_USBDEV); 46802ac6454SAndrew Thompson } 46902ac6454SAndrew Thompson return (err); 47002ac6454SAndrew Thompson } 47102ac6454SAndrew Thompson 47202ac6454SAndrew Thompson static int 473760bc48eSAndrew Thompson uhid_open(struct usb_fifo *fifo, int fflags) 47402ac6454SAndrew Thompson { 475ed6d949aSAndrew Thompson struct uhid_softc *sc = usb_fifo_softc(fifo); 47602ac6454SAndrew Thompson 47702ac6454SAndrew Thompson /* 47802ac6454SAndrew Thompson * The buffers are one byte larger than maximum so that one 47902ac6454SAndrew Thompson * can detect too large read/writes and short transfers: 48002ac6454SAndrew Thompson */ 48102ac6454SAndrew Thompson if (fflags & FREAD) { 48202ac6454SAndrew Thompson /* reset flags */ 48302ac6454SAndrew Thompson sc->sc_flags &= ~UHID_FLAG_IMMED; 48402ac6454SAndrew Thompson 485a593f6b8SAndrew Thompson if (usb_fifo_alloc_buffer(fifo, 48602ac6454SAndrew Thompson sc->sc_isize + 1, UHID_FRAME_NUM)) { 48702ac6454SAndrew Thompson return (ENOMEM); 48802ac6454SAndrew Thompson } 48902ac6454SAndrew Thompson } 49002ac6454SAndrew Thompson if (fflags & FWRITE) { 491a593f6b8SAndrew Thompson if (usb_fifo_alloc_buffer(fifo, 49202ac6454SAndrew Thompson sc->sc_osize + 1, UHID_FRAME_NUM)) { 49302ac6454SAndrew Thompson return (ENOMEM); 49402ac6454SAndrew Thompson } 49502ac6454SAndrew Thompson } 49602ac6454SAndrew Thompson return (0); 49702ac6454SAndrew Thompson } 49802ac6454SAndrew Thompson 49902ac6454SAndrew Thompson static void 500760bc48eSAndrew Thompson uhid_close(struct usb_fifo *fifo, int fflags) 50102ac6454SAndrew Thompson { 50202ac6454SAndrew Thompson if (fflags & (FREAD | FWRITE)) { 503a593f6b8SAndrew Thompson usb_fifo_free_buffer(fifo); 50402ac6454SAndrew Thompson } 50502ac6454SAndrew Thompson } 50602ac6454SAndrew Thompson 50702ac6454SAndrew Thompson static int 508760bc48eSAndrew Thompson uhid_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr, 509ee3e3ff5SAndrew Thompson int fflags) 51002ac6454SAndrew Thompson { 511ed6d949aSAndrew Thompson struct uhid_softc *sc = usb_fifo_softc(fifo); 512760bc48eSAndrew Thompson struct usb_gen_descriptor *ugd; 51302ac6454SAndrew Thompson uint32_t size; 51402ac6454SAndrew Thompson int error = 0; 51502ac6454SAndrew Thompson uint8_t id; 51602ac6454SAndrew Thompson 51702ac6454SAndrew Thompson switch (cmd) { 51802ac6454SAndrew Thompson case USB_GET_REPORT_DESC: 51902ac6454SAndrew Thompson ugd = addr; 52002ac6454SAndrew Thompson if (sc->sc_repdesc_size > ugd->ugd_maxlen) { 52102ac6454SAndrew Thompson size = ugd->ugd_maxlen; 52202ac6454SAndrew Thompson } else { 52302ac6454SAndrew Thompson size = sc->sc_repdesc_size; 52402ac6454SAndrew Thompson } 52502ac6454SAndrew Thompson ugd->ugd_actlen = size; 52602ac6454SAndrew Thompson if (ugd->ugd_data == NULL) 52702ac6454SAndrew Thompson break; /* descriptor length only */ 52802ac6454SAndrew Thompson error = copyout(sc->sc_repdesc_ptr, ugd->ugd_data, size); 52902ac6454SAndrew Thompson break; 53002ac6454SAndrew Thompson 53102ac6454SAndrew Thompson case USB_SET_IMMED: 53202ac6454SAndrew Thompson if (!(fflags & FREAD)) { 53302ac6454SAndrew Thompson error = EPERM; 53402ac6454SAndrew Thompson break; 53502ac6454SAndrew Thompson } 53602ac6454SAndrew Thompson if (*(int *)addr) { 53702ac6454SAndrew Thompson 53802ac6454SAndrew Thompson /* do a test read */ 53902ac6454SAndrew Thompson 54002ac6454SAndrew Thompson error = uhid_get_report(sc, UHID_INPUT_REPORT, 54102ac6454SAndrew Thompson sc->sc_iid, NULL, NULL, sc->sc_isize); 54202ac6454SAndrew Thompson if (error) { 54302ac6454SAndrew Thompson break; 54402ac6454SAndrew Thompson } 54502ac6454SAndrew Thompson mtx_lock(&sc->sc_mtx); 54602ac6454SAndrew Thompson sc->sc_flags |= UHID_FLAG_IMMED; 54702ac6454SAndrew Thompson mtx_unlock(&sc->sc_mtx); 54802ac6454SAndrew Thompson } else { 54902ac6454SAndrew Thompson mtx_lock(&sc->sc_mtx); 55002ac6454SAndrew Thompson sc->sc_flags &= ~UHID_FLAG_IMMED; 55102ac6454SAndrew Thompson mtx_unlock(&sc->sc_mtx); 55202ac6454SAndrew Thompson } 55302ac6454SAndrew Thompson break; 55402ac6454SAndrew Thompson 55502ac6454SAndrew Thompson case USB_GET_REPORT: 55602ac6454SAndrew Thompson if (!(fflags & FREAD)) { 55702ac6454SAndrew Thompson error = EPERM; 55802ac6454SAndrew Thompson break; 55902ac6454SAndrew Thompson } 56002ac6454SAndrew Thompson ugd = addr; 56102ac6454SAndrew Thompson switch (ugd->ugd_report_type) { 56202ac6454SAndrew Thompson case UHID_INPUT_REPORT: 56302ac6454SAndrew Thompson size = sc->sc_isize; 56402ac6454SAndrew Thompson id = sc->sc_iid; 56502ac6454SAndrew Thompson break; 56602ac6454SAndrew Thompson case UHID_OUTPUT_REPORT: 56702ac6454SAndrew Thompson size = sc->sc_osize; 56802ac6454SAndrew Thompson id = sc->sc_oid; 56902ac6454SAndrew Thompson break; 57002ac6454SAndrew Thompson case UHID_FEATURE_REPORT: 57102ac6454SAndrew Thompson size = sc->sc_fsize; 57202ac6454SAndrew Thompson id = sc->sc_fid; 57302ac6454SAndrew Thompson break; 57402ac6454SAndrew Thompson default: 57502ac6454SAndrew Thompson return (EINVAL); 57602ac6454SAndrew Thompson } 57702ac6454SAndrew Thompson error = uhid_get_report(sc, ugd->ugd_report_type, id, 57802ac6454SAndrew Thompson NULL, ugd->ugd_data, size); 57902ac6454SAndrew Thompson break; 58002ac6454SAndrew Thompson 58102ac6454SAndrew Thompson case USB_SET_REPORT: 58202ac6454SAndrew Thompson if (!(fflags & FWRITE)) { 58302ac6454SAndrew Thompson error = EPERM; 58402ac6454SAndrew Thompson break; 58502ac6454SAndrew Thompson } 58602ac6454SAndrew Thompson ugd = addr; 58702ac6454SAndrew Thompson switch (ugd->ugd_report_type) { 58802ac6454SAndrew Thompson case UHID_INPUT_REPORT: 58902ac6454SAndrew Thompson size = sc->sc_isize; 59002ac6454SAndrew Thompson id = sc->sc_iid; 59102ac6454SAndrew Thompson break; 59202ac6454SAndrew Thompson case UHID_OUTPUT_REPORT: 59302ac6454SAndrew Thompson size = sc->sc_osize; 59402ac6454SAndrew Thompson id = sc->sc_oid; 59502ac6454SAndrew Thompson break; 59602ac6454SAndrew Thompson case UHID_FEATURE_REPORT: 59702ac6454SAndrew Thompson size = sc->sc_fsize; 59802ac6454SAndrew Thompson id = sc->sc_fid; 59902ac6454SAndrew Thompson break; 60002ac6454SAndrew Thompson default: 60102ac6454SAndrew Thompson return (EINVAL); 60202ac6454SAndrew Thompson } 60302ac6454SAndrew Thompson error = uhid_set_report(sc, ugd->ugd_report_type, id, 60402ac6454SAndrew Thompson NULL, ugd->ugd_data, size); 60502ac6454SAndrew Thompson break; 60602ac6454SAndrew Thompson 60702ac6454SAndrew Thompson case USB_GET_REPORT_ID: 60802ac6454SAndrew Thompson *(int *)addr = 0; /* XXX: we only support reportid 0? */ 60902ac6454SAndrew Thompson break; 61002ac6454SAndrew Thompson 61102ac6454SAndrew Thompson default: 61202ac6454SAndrew Thompson error = EINVAL; 61302ac6454SAndrew Thompson break; 61402ac6454SAndrew Thompson } 61502ac6454SAndrew Thompson return (error); 61602ac6454SAndrew Thompson } 61702ac6454SAndrew Thompson 61802ac6454SAndrew Thompson static int 61902ac6454SAndrew Thompson uhid_probe(device_t dev) 62002ac6454SAndrew Thompson { 621760bc48eSAndrew Thompson struct usb_attach_arg *uaa = device_get_ivars(dev); 62202ac6454SAndrew Thompson 62302ac6454SAndrew Thompson DPRINTFN(11, "\n"); 62402ac6454SAndrew Thompson 625f29a0724SAndrew Thompson if (uaa->usb_mode != USB_MODE_HOST) { 62602ac6454SAndrew Thompson return (ENXIO); 62702ac6454SAndrew Thompson } 62802ac6454SAndrew Thompson if (uaa->use_generic == 0) { 62902ac6454SAndrew Thompson /* give Mouse and Keyboard drivers a try first */ 63002ac6454SAndrew Thompson return (ENXIO); 63102ac6454SAndrew Thompson } 63202ac6454SAndrew Thompson if (uaa->info.bInterfaceClass != UICLASS_HID) { 63302ac6454SAndrew Thompson 63402ac6454SAndrew Thompson /* the Xbox 360 gamepad doesn't use the HID class */ 63502ac6454SAndrew Thompson 63602ac6454SAndrew Thompson if ((uaa->info.bInterfaceClass != UICLASS_VENDOR) || 63702ac6454SAndrew Thompson (uaa->info.bInterfaceSubClass != UISUBCLASS_XBOX360_CONTROLLER) || 63802ac6454SAndrew Thompson (uaa->info.bInterfaceProtocol != UIPROTO_XBOX360_GAMEPAD)) { 63902ac6454SAndrew Thompson return (ENXIO); 64002ac6454SAndrew Thompson } 64102ac6454SAndrew Thompson } 642a593f6b8SAndrew Thompson if (usb_test_quirk(uaa, UQ_HID_IGNORE)) { 64302ac6454SAndrew Thompson return (ENXIO); 64402ac6454SAndrew Thompson } 645b201cde6SNathan Whitehorn return (BUS_PROBE_GENERIC); 64602ac6454SAndrew Thompson } 64702ac6454SAndrew Thompson 64802ac6454SAndrew Thompson static int 64902ac6454SAndrew Thompson uhid_attach(device_t dev) 65002ac6454SAndrew Thompson { 651760bc48eSAndrew Thompson struct usb_attach_arg *uaa = device_get_ivars(dev); 65202ac6454SAndrew Thompson struct uhid_softc *sc = device_get_softc(dev); 65302ac6454SAndrew Thompson int unit = device_get_unit(dev); 65402ac6454SAndrew Thompson int error = 0; 65502ac6454SAndrew Thompson 65602ac6454SAndrew Thompson DPRINTFN(10, "sc=%p\n", sc); 65702ac6454SAndrew Thompson 658a593f6b8SAndrew Thompson device_set_usb_desc(dev); 65902ac6454SAndrew Thompson 66002ac6454SAndrew Thompson mtx_init(&sc->sc_mtx, "uhid lock", NULL, MTX_DEF | MTX_RECURSE); 66102ac6454SAndrew Thompson 66202ac6454SAndrew Thompson sc->sc_udev = uaa->device; 66302ac6454SAndrew Thompson 66402ac6454SAndrew Thompson sc->sc_iface_no = uaa->info.bIfaceNum; 66502ac6454SAndrew Thompson sc->sc_iface_index = uaa->info.bIfaceIndex; 66602ac6454SAndrew Thompson 667a593f6b8SAndrew Thompson error = usbd_transfer_setup(uaa->device, 66802ac6454SAndrew Thompson &uaa->info.bIfaceIndex, sc->sc_xfer, uhid_config, 66902ac6454SAndrew Thompson UHID_N_TRANSFER, sc, &sc->sc_mtx); 67002ac6454SAndrew Thompson 67102ac6454SAndrew Thompson if (error) { 672a593f6b8SAndrew Thompson DPRINTF("error=%s\n", usbd_errstr(error)); 67302ac6454SAndrew Thompson goto detach; 67402ac6454SAndrew Thompson } 67502ac6454SAndrew Thompson if (uaa->info.idVendor == USB_VENDOR_WACOM) { 67602ac6454SAndrew Thompson 67702ac6454SAndrew Thompson /* the report descriptor for the Wacom Graphire is broken */ 67802ac6454SAndrew Thompson 67902ac6454SAndrew Thompson if (uaa->info.idProduct == USB_PRODUCT_WACOM_GRAPHIRE) { 68002ac6454SAndrew Thompson 68102ac6454SAndrew Thompson sc->sc_repdesc_size = sizeof(uhid_graphire_report_descr); 682ed6d949aSAndrew Thompson sc->sc_repdesc_ptr = &uhid_graphire_report_descr; 68302ac6454SAndrew Thompson sc->sc_flags |= UHID_FLAG_STATIC_DESC; 68402ac6454SAndrew Thompson 68502ac6454SAndrew Thompson } else if (uaa->info.idProduct == USB_PRODUCT_WACOM_GRAPHIRE3_4X5) { 68602ac6454SAndrew Thompson 68702ac6454SAndrew Thompson static uint8_t reportbuf[] = {2, 2, 2}; 68802ac6454SAndrew Thompson 68902ac6454SAndrew Thompson /* 69002ac6454SAndrew Thompson * The Graphire3 needs 0x0202 to be written to 69102ac6454SAndrew Thompson * feature report ID 2 before it'll start 69202ac6454SAndrew Thompson * returning digitizer data. 69302ac6454SAndrew Thompson */ 694a593f6b8SAndrew Thompson error = usbd_req_set_report(uaa->device, NULL, 695296ade60SAndrew Thompson reportbuf, sizeof(reportbuf), 69602ac6454SAndrew Thompson uaa->info.bIfaceIndex, UHID_FEATURE_REPORT, 2); 69702ac6454SAndrew Thompson 69802ac6454SAndrew Thompson if (error) { 69902ac6454SAndrew Thompson DPRINTF("set report failed, error=%s (ignored)\n", 700a593f6b8SAndrew Thompson usbd_errstr(error)); 70102ac6454SAndrew Thompson } 70202ac6454SAndrew Thompson sc->sc_repdesc_size = sizeof(uhid_graphire3_4x5_report_descr); 703ed6d949aSAndrew Thompson sc->sc_repdesc_ptr = &uhid_graphire3_4x5_report_descr; 70402ac6454SAndrew Thompson sc->sc_flags |= UHID_FLAG_STATIC_DESC; 70502ac6454SAndrew Thompson } 70602ac6454SAndrew Thompson } else if ((uaa->info.bInterfaceClass == UICLASS_VENDOR) && 70702ac6454SAndrew Thompson (uaa->info.bInterfaceSubClass == UISUBCLASS_XBOX360_CONTROLLER) && 70802ac6454SAndrew Thompson (uaa->info.bInterfaceProtocol == UIPROTO_XBOX360_GAMEPAD)) { 70902ac6454SAndrew Thompson 71002ac6454SAndrew Thompson /* the Xbox 360 gamepad has no report descriptor */ 71102ac6454SAndrew Thompson sc->sc_repdesc_size = sizeof(uhid_xb360gp_report_descr); 712ed6d949aSAndrew Thompson sc->sc_repdesc_ptr = &uhid_xb360gp_report_descr; 71302ac6454SAndrew Thompson sc->sc_flags |= UHID_FLAG_STATIC_DESC; 71402ac6454SAndrew Thompson } 71502ac6454SAndrew Thompson if (sc->sc_repdesc_ptr == NULL) { 71602ac6454SAndrew Thompson 717a593f6b8SAndrew Thompson error = usbd_req_get_hid_desc(uaa->device, NULL, 718296ade60SAndrew Thompson &sc->sc_repdesc_ptr, &sc->sc_repdesc_size, 719296ade60SAndrew Thompson M_USBDEV, uaa->info.bIfaceIndex); 72002ac6454SAndrew Thompson 72102ac6454SAndrew Thompson if (error) { 72202ac6454SAndrew Thompson device_printf(dev, "no report descriptor\n"); 72302ac6454SAndrew Thompson goto detach; 72402ac6454SAndrew Thompson } 72502ac6454SAndrew Thompson } 726a593f6b8SAndrew Thompson error = usbd_req_set_idle(uaa->device, NULL, 72702ac6454SAndrew Thompson uaa->info.bIfaceIndex, 0, 0); 72802ac6454SAndrew Thompson 72902ac6454SAndrew Thompson if (error) { 73002ac6454SAndrew Thompson DPRINTF("set idle failed, error=%s (ignored)\n", 731a593f6b8SAndrew Thompson usbd_errstr(error)); 73202ac6454SAndrew Thompson } 73302ac6454SAndrew Thompson sc->sc_isize = hid_report_size 73402ac6454SAndrew Thompson (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_input, &sc->sc_iid); 73502ac6454SAndrew Thompson 73602ac6454SAndrew Thompson sc->sc_osize = hid_report_size 73702ac6454SAndrew Thompson (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_output, &sc->sc_oid); 73802ac6454SAndrew Thompson 73902ac6454SAndrew Thompson sc->sc_fsize = hid_report_size 74002ac6454SAndrew Thompson (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_feature, &sc->sc_fid); 74102ac6454SAndrew Thompson 74202ac6454SAndrew Thompson if (sc->sc_isize > UHID_BSIZE) { 74302ac6454SAndrew Thompson DPRINTF("input size is too large, " 74402ac6454SAndrew Thompson "%d bytes (truncating)\n", 74502ac6454SAndrew Thompson sc->sc_isize); 74602ac6454SAndrew Thompson sc->sc_isize = UHID_BSIZE; 74702ac6454SAndrew Thompson } 74802ac6454SAndrew Thompson if (sc->sc_osize > UHID_BSIZE) { 74902ac6454SAndrew Thompson DPRINTF("output size is too large, " 75002ac6454SAndrew Thompson "%d bytes (truncating)\n", 75102ac6454SAndrew Thompson sc->sc_osize); 75202ac6454SAndrew Thompson sc->sc_osize = UHID_BSIZE; 75302ac6454SAndrew Thompson } 75402ac6454SAndrew Thompson if (sc->sc_fsize > UHID_BSIZE) { 75502ac6454SAndrew Thompson DPRINTF("feature size is too large, " 75602ac6454SAndrew Thompson "%d bytes (truncating)\n", 75702ac6454SAndrew Thompson sc->sc_fsize); 75802ac6454SAndrew Thompson sc->sc_fsize = UHID_BSIZE; 75902ac6454SAndrew Thompson } 76002ac6454SAndrew Thompson 761a593f6b8SAndrew Thompson error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx, 76202ac6454SAndrew Thompson &uhid_fifo_methods, &sc->sc_fifo, 763ee3e3ff5SAndrew Thompson unit, 0 - 1, uaa->info.bIfaceIndex, 764ee3e3ff5SAndrew Thompson UID_ROOT, GID_OPERATOR, 0644); 76502ac6454SAndrew Thompson if (error) { 76602ac6454SAndrew Thompson goto detach; 76702ac6454SAndrew Thompson } 76802ac6454SAndrew Thompson return (0); /* success */ 76902ac6454SAndrew Thompson 77002ac6454SAndrew Thompson detach: 77102ac6454SAndrew Thompson uhid_detach(dev); 77202ac6454SAndrew Thompson return (ENOMEM); 77302ac6454SAndrew Thompson } 77402ac6454SAndrew Thompson 77502ac6454SAndrew Thompson static int 77602ac6454SAndrew Thompson uhid_detach(device_t dev) 77702ac6454SAndrew Thompson { 77802ac6454SAndrew Thompson struct uhid_softc *sc = device_get_softc(dev); 77902ac6454SAndrew Thompson 780a593f6b8SAndrew Thompson usb_fifo_detach(&sc->sc_fifo); 78102ac6454SAndrew Thompson 782a593f6b8SAndrew Thompson usbd_transfer_unsetup(sc->sc_xfer, UHID_N_TRANSFER); 78302ac6454SAndrew Thompson 78402ac6454SAndrew Thompson if (sc->sc_repdesc_ptr) { 78502ac6454SAndrew Thompson if (!(sc->sc_flags & UHID_FLAG_STATIC_DESC)) { 78602ac6454SAndrew Thompson free(sc->sc_repdesc_ptr, M_USBDEV); 78702ac6454SAndrew Thompson } 78802ac6454SAndrew Thompson } 78902ac6454SAndrew Thompson mtx_destroy(&sc->sc_mtx); 79002ac6454SAndrew Thompson 79102ac6454SAndrew Thompson return (0); 79202ac6454SAndrew Thompson } 79302ac6454SAndrew Thompson 79402ac6454SAndrew Thompson static devclass_t uhid_devclass; 79502ac6454SAndrew Thompson 79602ac6454SAndrew Thompson static device_method_t uhid_methods[] = { 79702ac6454SAndrew Thompson DEVMETHOD(device_probe, uhid_probe), 79802ac6454SAndrew Thompson DEVMETHOD(device_attach, uhid_attach), 79902ac6454SAndrew Thompson DEVMETHOD(device_detach, uhid_detach), 80002ac6454SAndrew Thompson {0, 0} 80102ac6454SAndrew Thompson }; 80202ac6454SAndrew Thompson 80302ac6454SAndrew Thompson static driver_t uhid_driver = { 80402ac6454SAndrew Thompson .name = "uhid", 80502ac6454SAndrew Thompson .methods = uhid_methods, 80602ac6454SAndrew Thompson .size = sizeof(struct uhid_softc), 80702ac6454SAndrew Thompson }; 80802ac6454SAndrew Thompson 8099aef556dSAndrew Thompson DRIVER_MODULE(uhid, uhub, uhid_driver, uhid_devclass, NULL, 0); 81002ac6454SAndrew Thompson MODULE_DEPEND(uhid, usb, 1, 1, 1); 811