Home | History | Annotate | Line # | Download | only in usb
uhidev.c revision 1.61.2.2.4.1
      1  1.61.2.2.4.1     skrll /*	$NetBSD: uhidev.c,v 1.61.2.2.4.1 2016/09/06 20:33:09 skrll Exp $	*/
      2           1.1  augustss 
      3           1.1  augustss /*
      4          1.56       mrg  * Copyright (c) 2001, 2012 The NetBSD Foundation, Inc.
      5           1.1  augustss  * All rights reserved.
      6           1.1  augustss  *
      7           1.1  augustss  * This code is derived from software contributed to The NetBSD Foundation
      8           1.1  augustss  * by Lennart Augustsson (lennart (at) augustsson.net) at
      9          1.56       mrg  * Carlstedt Research & Technology and Matthew R. Green (mrg (at) eterna.com.au).
     10           1.1  augustss  *
     11           1.1  augustss  * Redistribution and use in source and binary forms, with or without
     12           1.1  augustss  * modification, are permitted provided that the following conditions
     13           1.1  augustss  * are met:
     14           1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     15           1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     16           1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     17           1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     18           1.1  augustss  *    documentation and/or other materials provided with the distribution.
     19           1.1  augustss  *
     20           1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21           1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22           1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23           1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24           1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25           1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26           1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27           1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28           1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29           1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30           1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     31           1.1  augustss  */
     32           1.1  augustss 
     33           1.1  augustss /*
     34          1.14  augustss  * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
     35           1.1  augustss  */
     36          1.15     lukem 
     37          1.15     lukem #include <sys/cdefs.h>
     38  1.61.2.2.4.1     skrll __KERNEL_RCSID(0, "$NetBSD: uhidev.c,v 1.61.2.2.4.1 2016/09/06 20:33:09 skrll Exp $");
     39  1.61.2.2.4.1     skrll 
     40  1.61.2.2.4.1     skrll #ifdef _KERNEL_OPT
     41  1.61.2.2.4.1     skrll #include "opt_usb.h"
     42  1.61.2.2.4.1     skrll #endif
     43           1.1  augustss 
     44           1.1  augustss #include <sys/param.h>
     45           1.1  augustss #include <sys/systm.h>
     46           1.1  augustss #include <sys/kernel.h>
     47  1.61.2.2.4.1     skrll #include <sys/kmem.h>
     48           1.1  augustss #include <sys/signalvar.h>
     49           1.1  augustss #include <sys/device.h>
     50           1.1  augustss #include <sys/ioctl.h>
     51           1.1  augustss #include <sys/conf.h>
     52           1.1  augustss 
     53           1.1  augustss #include <dev/usb/usb.h>
     54           1.1  augustss #include <dev/usb/usbhid.h>
     55           1.1  augustss 
     56           1.1  augustss #include <dev/usb/usbdevs.h>
     57           1.1  augustss #include <dev/usb/usbdi.h>
     58           1.1  augustss #include <dev/usb/usbdi_util.h>
     59           1.1  augustss #include <dev/usb/hid.h>
     60           1.1  augustss #include <dev/usb/usb_quirks.h>
     61           1.1  augustss 
     62           1.1  augustss #include <dev/usb/uhidev.h>
     63           1.1  augustss 
     64           1.1  augustss /* Report descriptor for broken Wacom Graphire */
     65           1.1  augustss #include <dev/usb/ugraphire_rdesc.h>
     66          1.51  jmcneill /* Report descriptor for game controllers in "XInput" mode */
     67          1.51  jmcneill #include <dev/usb/xinput_rdesc.h>
     68      1.61.2.1       snj /* Report descriptor for Xbox One controllers */
     69      1.61.2.1       snj #include <dev/usb/x1input_rdesc.h>
     70           1.1  augustss 
     71          1.22  drochner #include "locators.h"
     72          1.22  drochner 
     73           1.1  augustss #ifdef UHIDEV_DEBUG
     74          1.48    dyoung #define DPRINTF(x)	if (uhidevdebug) printf x
     75          1.48    dyoung #define DPRINTFN(n,x)	if (uhidevdebug>(n)) printf x
     76           1.1  augustss int	uhidevdebug = 0;
     77           1.1  augustss #else
     78           1.1  augustss #define DPRINTF(x)
     79           1.1  augustss #define DPRINTFN(n,x)
     80           1.1  augustss #endif
     81           1.1  augustss 
     82  1.61.2.2.4.1     skrll Static void uhidev_intr(struct usbd_xfer *, void *, usbd_status);
     83           1.1  augustss 
     84          1.29  drochner Static int uhidev_maxrepid(void *, int);
     85          1.29  drochner Static int uhidevprint(void *, const char *);
     86           1.1  augustss 
     87          1.41      cube int uhidev_match(device_t, cfdata_t, void *);
     88          1.39    dyoung void uhidev_attach(device_t, device_t, void *);
     89          1.39    dyoung void uhidev_childdet(device_t, device_t);
     90          1.39    dyoung int uhidev_detach(device_t, int);
     91          1.39    dyoung int uhidev_activate(device_t, enum devact);
     92          1.39    dyoung extern struct cfdriver uhidev_cd;
     93          1.41      cube CFATTACH_DECL2_NEW(uhidev, sizeof(struct uhidev_softc), uhidev_match,
     94          1.39    dyoung     uhidev_attach, uhidev_detach, uhidev_activate, NULL, uhidev_childdet);
     95           1.1  augustss 
     96          1.58     skrll int
     97          1.48    dyoung uhidev_match(device_t parent, cfdata_t match, void *aux)
     98           1.1  augustss {
     99  1.61.2.2.4.1     skrll 	struct usbif_attach_arg *uiaa = aux;
    100           1.6  augustss 
    101          1.51  jmcneill 	/* Game controllers in "XInput" mode */
    102  1.61.2.2.4.1     skrll 	if (USBIF_IS_XINPUT(uiaa))
    103          1.51  jmcneill 		return UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO;
    104      1.61.2.1       snj 	/* Xbox One controllers */
    105  1.61.2.2.4.1     skrll  	if (USBIF_IS_X1INPUT(uiaa) && uiaa->uiaa_ifaceno == 0)
    106      1.61.2.1       snj 		return UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO;
    107      1.61.2.1       snj 
    108  1.61.2.2.4.1     skrll 	if (uiaa->uiaa_class != UICLASS_HID)
    109  1.61.2.2.4.1     skrll 		return UMATCH_NONE;
    110  1.61.2.2.4.1     skrll 	if (usbd_get_quirks(uiaa->uiaa_device)->uq_flags & UQ_HID_IGNORE)
    111  1.61.2.2.4.1     skrll 		return UMATCH_NONE;
    112  1.61.2.2.4.1     skrll 	return UMATCH_IFACECLASS_GENERIC;
    113           1.1  augustss }
    114           1.1  augustss 
    115          1.58     skrll void
    116          1.48    dyoung uhidev_attach(device_t parent, device_t self, void *aux)
    117           1.1  augustss {
    118          1.48    dyoung 	struct uhidev_softc *sc = device_private(self);
    119  1.61.2.2.4.1     skrll 	struct usbif_attach_arg *uiaa = aux;
    120  1.61.2.2.4.1     skrll 	struct usbd_interface *iface = uiaa->uiaa_iface;
    121           1.1  augustss 	usb_interface_descriptor_t *id;
    122           1.1  augustss 	usb_endpoint_descriptor_t *ed;
    123           1.1  augustss 	struct uhidev_attach_arg uha;
    124          1.42  drochner 	device_t dev;
    125          1.42  drochner 	struct uhidev *csc;
    126          1.44     rafal 	int maxinpktsize, size, nrepid, repid, repsz;
    127          1.32  christos 	int *repsizes;
    128          1.25     skrll 	int i;
    129          1.19  jdolecek 	void *desc;
    130          1.20  augustss 	const void *descptr;
    131           1.1  augustss 	usbd_status err;
    132          1.26  augustss 	char *devinfop;
    133          1.28  drochner 	int locs[UHIDBUSCF_NLOCS];
    134           1.6  augustss 
    135          1.41      cube 	sc->sc_dev = self;
    136  1.61.2.2.4.1     skrll 	sc->sc_udev = uiaa->uiaa_device;
    137           1.1  augustss 	sc->sc_iface = iface;
    138          1.43    plunky 
    139          1.43    plunky 	aprint_naive("\n");
    140          1.43    plunky 	aprint_normal("\n");
    141          1.43    plunky 
    142  1.61.2.2.4.1     skrll 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
    143          1.56       mrg 
    144           1.1  augustss 	id = usbd_get_interface_descriptor(iface);
    145          1.26  augustss 
    146  1.61.2.2.4.1     skrll 	devinfop = usbd_devinfo_alloc(uiaa->uiaa_device, 0);
    147          1.41      cube 	aprint_normal_dev(self, "%s, iclass %d/%d\n",
    148          1.26  augustss 	       devinfop, id->bInterfaceClass, id->bInterfaceSubClass);
    149          1.26  augustss 	usbd_devinfo_free(devinfop);
    150           1.1  augustss 
    151          1.38  jmcneill 	if (!pmf_device_register(self, NULL, NULL))
    152          1.38  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    153          1.38  jmcneill 
    154           1.1  augustss 	(void)usbd_set_idle(iface, 0, 0);
    155           1.1  augustss #if 0
    156           1.1  augustss 
    157  1.61.2.2.4.1     skrll 	if ((usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_NO_SET_PROTO) == 0 &&
    158           1.1  augustss 	    id->bInterfaceSubClass != UISUBCLASS_BOOT)
    159           1.1  augustss 		(void)usbd_set_protocol(iface, 1);
    160           1.1  augustss #endif
    161           1.1  augustss 
    162          1.44     rafal 	maxinpktsize = 0;
    163          1.25     skrll 	sc->sc_iep_addr = sc->sc_oep_addr = -1;
    164          1.25     skrll 	for (i = 0; i < id->bNumEndpoints; i++) {
    165          1.25     skrll 		ed = usbd_interface2endpoint_descriptor(iface, i);
    166          1.25     skrll 		if (ed == NULL) {
    167          1.41      cube 			aprint_error_dev(self,
    168          1.41      cube 			    "could not read endpoint descriptor\n");
    169          1.25     skrll 			sc->sc_dying = 1;
    170          1.48    dyoung 			return;
    171          1.25     skrll 		}
    172          1.25     skrll 
    173          1.25     skrll 		DPRINTFN(10,("uhidev_attach: bLength=%d bDescriptorType=%d "
    174          1.25     skrll 		    "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
    175          1.25     skrll 		    " bInterval=%d\n",
    176          1.25     skrll 		    ed->bLength, ed->bDescriptorType,
    177          1.25     skrll 		    ed->bEndpointAddress & UE_ADDR,
    178          1.25     skrll 		    UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out",
    179          1.25     skrll 		    ed->bmAttributes & UE_XFERTYPE,
    180          1.25     skrll 		    UGETW(ed->wMaxPacketSize), ed->bInterval));
    181          1.25     skrll 
    182          1.25     skrll 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    183          1.25     skrll 		    (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
    184          1.44     rafal 			maxinpktsize = UGETW(ed->wMaxPacketSize);
    185          1.25     skrll 			sc->sc_iep_addr = ed->bEndpointAddress;
    186          1.25     skrll 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    187          1.25     skrll 		    (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
    188          1.25     skrll 			sc->sc_oep_addr = ed->bEndpointAddress;
    189          1.25     skrll 		} else {
    190          1.41      cube 			aprint_verbose_dev(self, "endpoint %d: ignored\n", i);
    191          1.25     skrll 		}
    192           1.1  augustss 	}
    193           1.1  augustss 
    194          1.25     skrll 	/*
    195          1.25     skrll 	 * Check that we found an input interrupt endpoint. The output interrupt
    196          1.25     skrll 	 * endpoint is optional
    197          1.25     skrll 	 */
    198          1.25     skrll 	if (sc->sc_iep_addr == -1) {
    199          1.41      cube 		aprint_error_dev(self, "no input interrupt endpoint\n");
    200           1.1  augustss 		sc->sc_dying = 1;
    201          1.48    dyoung 		return;
    202           1.1  augustss 	}
    203           1.1  augustss 
    204           1.1  augustss 	/* XXX need to extend this */
    205          1.20  augustss 	descptr = NULL;
    206  1.61.2.2.4.1     skrll 	if (uiaa->uiaa_vendor == USB_VENDOR_WACOM) {
    207          1.20  augustss 		static uByte reportbuf[] = {2, 2, 2};
    208          1.17  augustss 
    209           1.1  augustss 		/* The report descriptor for the Wacom Graphire is broken. */
    210  1.61.2.2.4.1     skrll 		switch (uiaa->uiaa_product) {
    211          1.17  augustss 		case USB_PRODUCT_WACOM_GRAPHIRE:
    212          1.49   tsutsui 		case USB_PRODUCT_WACOM_GRAPHIRE2:
    213          1.33      ghen 		case USB_PRODUCT_WACOM_GRAPHIRE3_4X5:
    214          1.33      ghen 		case USB_PRODUCT_WACOM_GRAPHIRE3_6X8:
    215          1.33      ghen 		case USB_PRODUCT_WACOM_GRAPHIRE4_4X5: /* The 6x8 too? */
    216          1.17  augustss 			/*
    217          1.17  augustss 			 * The Graphire3 needs 0x0202 to be written to
    218          1.17  augustss 			 * feature report ID 2 before it'll start
    219          1.17  augustss 			 * returning digitizer data.
    220          1.17  augustss 			 */
    221  1.61.2.2.4.1     skrll 			usbd_set_report(uiaa->uiaa_iface, UHID_FEATURE_REPORT, 2,
    222  1.61.2.2.4.1     skrll 			    &reportbuf, sizeof(reportbuf));
    223          1.17  augustss 
    224  1.61.2.2.4.1     skrll 			size = sizeof(uhid_graphire3_4x5_report_descr);
    225          1.17  augustss 			descptr = uhid_graphire3_4x5_report_descr;
    226          1.17  augustss 			break;
    227          1.20  augustss 		default:
    228          1.20  augustss 			/* Keep descriptor */
    229          1.20  augustss 			break;
    230          1.17  augustss 		}
    231          1.17  augustss 	}
    232  1.61.2.2.4.1     skrll 	if (USBIF_IS_XINPUT(uiaa)) {
    233  1.61.2.2.4.1     skrll 		size = sizeof(uhid_xinput_report_descr);
    234          1.51  jmcneill 		descptr = uhid_xinput_report_descr;
    235          1.51  jmcneill 	}
    236  1.61.2.2.4.1     skrll 	if (USBIF_IS_X1INPUT(uiaa)) {
    237      1.61.2.1       snj 		sc->sc_flags |= UHIDEV_F_XB1;
    238  1.61.2.2.4.1     skrll 		size = sizeof(uhid_x1input_report_descr);
    239      1.61.2.1       snj 		descptr = uhid_x1input_report_descr;
    240      1.61.2.1       snj 	}
    241          1.17  augustss 
    242          1.17  augustss 	if (descptr) {
    243  1.61.2.2.4.1     skrll 		desc = kmem_alloc(size, KM_SLEEP);
    244           1.1  augustss 		if (desc == NULL)
    245           1.1  augustss 			err = USBD_NOMEM;
    246           1.1  augustss 		else {
    247           1.1  augustss 			err = USBD_NORMAL_COMPLETION;
    248          1.17  augustss 			memcpy(desc, descptr, size);
    249           1.1  augustss 		}
    250           1.1  augustss 	} else {
    251           1.1  augustss 		desc = NULL;
    252  1.61.2.2.4.1     skrll 		err = usbd_read_report_desc(uiaa->uiaa_iface, &desc, &size);
    253           1.1  augustss 	}
    254           1.1  augustss 	if (err) {
    255          1.41      cube 		aprint_error_dev(self, "no report descriptor\n");
    256           1.1  augustss 		sc->sc_dying = 1;
    257          1.48    dyoung 		return;
    258           1.1  augustss 	}
    259           1.6  augustss 
    260  1.61.2.2.4.1     skrll 	if (uiaa->uiaa_vendor == USB_VENDOR_HOSIDEN &&
    261  1.61.2.2.4.1     skrll 	    uiaa->uiaa_product == USB_PRODUCT_HOSIDEN_PPP) {
    262          1.27  augustss 		static uByte reportbuf[] = { 1 };
    263          1.27  augustss 		/*
    264          1.58     skrll 		 *  This device was sold by Konami with its ParaParaParadise
    265          1.27  augustss 		 *  game for PlayStation2.  It needs to be "turned on"
    266          1.27  augustss 		 *  before it will send any reports.
    267          1.27  augustss 		 */
    268          1.27  augustss 
    269  1.61.2.2.4.1     skrll 		usbd_set_report(uiaa->uiaa_iface, UHID_FEATURE_REPORT, 0,
    270  1.61.2.2.4.1     skrll 		    &reportbuf, sizeof(reportbuf));
    271          1.27  augustss 	}
    272          1.27  augustss 
    273  1.61.2.2.4.1     skrll 	if (uiaa->uiaa_vendor == USB_VENDOR_LOGITECH &&
    274  1.61.2.2.4.1     skrll 	    uiaa->uiaa_product == USB_PRODUCT_LOGITECH_CBT44 && size == 0xb1) {
    275          1.47  jakllsch 		uint8_t *data = desc;
    276          1.47  jakllsch 		/*
    277          1.47  jakllsch 		 * This device has a odd USAGE_MINIMUM value that would
    278          1.47  jakllsch 		 * cause the multimedia keys to have their usage number
    279          1.47  jakllsch 		 * shifted up one usage.  Adjust so the usages are sane.
    280          1.47  jakllsch 		 */
    281          1.47  jakllsch 
    282          1.47  jakllsch 		if (data[0x56] == 0x19 && data[0x57] == 0x01 &&
    283          1.47  jakllsch 		    data[0x58] == 0x2a && data[0x59] == 0x8c)
    284          1.47  jakllsch 			data[0x57] = 0x00;
    285          1.47  jakllsch 	}
    286          1.47  jakllsch 
    287          1.52   aymeric 	/*
    288          1.52   aymeric 	 * Enable the Six Axis and DualShock 3 controllers.
    289          1.52   aymeric 	 * See http://ps3.jim.sh/sixaxis/usb/
    290          1.52   aymeric 	 */
    291  1.61.2.2.4.1     skrll 	if (uiaa->uiaa_vendor == USB_VENDOR_SONY &&
    292  1.61.2.2.4.1     skrll 	    uiaa->uiaa_product == USB_PRODUCT_SONY_PS3CONTROLLER) {
    293          1.52   aymeric 		usb_device_request_t req;
    294          1.52   aymeric 		char data[17];
    295          1.52   aymeric 		int actlen;
    296          1.52   aymeric 
    297          1.52   aymeric 		req.bmRequestType = UT_READ_CLASS_INTERFACE;
    298          1.52   aymeric 		req.bRequest = 1;
    299          1.52   aymeric 		USETW(req.wValue, 0x3f2);
    300          1.52   aymeric 		USETW(req.wIndex, 0);
    301  1.61.2.2.4.1     skrll 		USETW(req.wLength, sizeof(data));
    302          1.52   aymeric 
    303          1.52   aymeric 		usbd_do_request_flags(sc->sc_udev, &req, data,
    304          1.52   aymeric 			USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
    305          1.52   aymeric 	}
    306          1.52   aymeric 
    307           1.1  augustss 	sc->sc_repdesc = desc;
    308           1.1  augustss 	sc->sc_repdesc_size = size;
    309           1.1  augustss 
    310  1.61.2.2.4.1     skrll 	uha.uiaa = uiaa;
    311           1.1  augustss 	nrepid = uhidev_maxrepid(desc, size);
    312           1.1  augustss 	if (nrepid < 0)
    313          1.48    dyoung 		return;
    314           1.1  augustss 	if (nrepid > 0)
    315          1.41      cube 		aprint_normal_dev(self, "%d report ids\n", nrepid);
    316           1.1  augustss 	nrepid++;
    317  1.61.2.2.4.1     skrll 	repsizes = kmem_alloc(nrepid * sizeof(*repsizes), KM_SLEEP);
    318          1.32  christos 	if (repsizes == NULL)
    319          1.32  christos 		goto nomem;
    320  1.61.2.2.4.1     skrll 	sc->sc_subdevs = kmem_zalloc(nrepid * sizeof(device_t),
    321  1.61.2.2.4.1     skrll 	    KM_SLEEP);
    322           1.1  augustss 	if (sc->sc_subdevs == NULL) {
    323  1.61.2.2.4.1     skrll 		kmem_free(repsizes, nrepid * sizeof(*repsizes));
    324          1.32  christos nomem:
    325          1.41      cube 		aprint_error_dev(self, "no memory\n");
    326          1.48    dyoung 		return;
    327           1.1  augustss 	}
    328          1.44     rafal 
    329          1.44     rafal 	/* Just request max packet size for the interrupt pipe */
    330          1.44     rafal 	sc->sc_isize = maxinpktsize;
    331           1.1  augustss 	sc->sc_nrepid = nrepid;
    332           1.1  augustss 
    333  1.61.2.2.4.1     skrll 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
    334           1.1  augustss 
    335           1.1  augustss 	for (repid = 0; repid < nrepid; repid++) {
    336           1.1  augustss 		repsz = hid_report_size(desc, size, hid_input, repid);
    337           1.1  augustss 		DPRINTF(("uhidev_match: repid=%d, repsz=%d\n", repid, repsz));
    338           1.2  augustss 		repsizes[repid] = repsz;
    339           1.1  augustss 	}
    340          1.44     rafal 
    341           1.1  augustss 	DPRINTF(("uhidev_attach: isize=%d\n", sc->sc_isize));
    342           1.1  augustss 
    343           1.1  augustss 	uha.parent = sc;
    344           1.1  augustss 	for (repid = 0; repid < nrepid; repid++) {
    345           1.1  augustss 		DPRINTF(("uhidev_match: try repid=%d\n", repid));
    346           1.1  augustss 		if (hid_report_size(desc, size, hid_input, repid) == 0 &&
    347           1.1  augustss 		    hid_report_size(desc, size, hid_output, repid) == 0 &&
    348           1.1  augustss 		    hid_report_size(desc, size, hid_feature, repid) == 0) {
    349           1.5  augustss 			;	/* already NULL in sc->sc_subdevs[repid] */
    350           1.1  augustss 		} else {
    351           1.1  augustss 			uha.reportid = repid;
    352          1.28  drochner 			locs[UHIDBUSCF_REPORTID] = repid;
    353          1.22  drochner 
    354          1.42  drochner 			dev = config_found_sm_loc(self,
    355          1.28  drochner 				"uhidbus", locs, &uha,
    356          1.42  drochner 				uhidevprint, config_stdsubmatch);
    357           1.1  augustss 			sc->sc_subdevs[repid] = dev;
    358           1.4  augustss 			if (dev != NULL) {
    359          1.42  drochner 				csc = device_private(dev);
    360          1.42  drochner 				csc->sc_in_rep_size = repsizes[repid];
    361           1.1  augustss #ifdef DIAGNOSTIC
    362           1.1  augustss 				DPRINTF(("uhidev_match: repid=%d dev=%p\n",
    363           1.1  augustss 					 repid, dev));
    364          1.42  drochner 				if (csc->sc_intr == NULL) {
    365  1.61.2.2.4.1     skrll 					kmem_free(repsizes,
    366  1.61.2.2.4.1     skrll 					    nrepid * sizeof(*repsizes));
    367          1.41      cube 					aprint_error_dev(self,
    368          1.41      cube 					    "sc_intr == NULL\n");
    369          1.48    dyoung 					return;
    370           1.1  augustss 				}
    371           1.4  augustss #endif
    372          1.42  drochner 				rnd_attach_source(&csc->rnd_source,
    373          1.48    dyoung 						  device_xname(dev),
    374          1.61       tls 						  RND_TYPE_TTY,
    375          1.61       tls 						  RND_FLAG_DEFAULT);
    376           1.1  augustss 			}
    377           1.1  augustss 		}
    378           1.1  augustss 	}
    379  1.61.2.2.4.1     skrll 	kmem_free(repsizes, nrepid * sizeof(*repsizes));
    380           1.1  augustss 
    381          1.48    dyoung 	return;
    382           1.1  augustss }
    383           1.1  augustss 
    384           1.1  augustss int
    385           1.1  augustss uhidev_maxrepid(void *buf, int len)
    386           1.1  augustss {
    387           1.1  augustss 	struct hid_data *d;
    388           1.1  augustss 	struct hid_item h;
    389           1.1  augustss 	int maxid;
    390           1.1  augustss 
    391           1.1  augustss 	maxid = -1;
    392           1.1  augustss 	h.report_ID = 0;
    393           1.1  augustss 	for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); )
    394           1.1  augustss 		if (h.report_ID > maxid)
    395           1.1  augustss 			maxid = h.report_ID;
    396           1.1  augustss 	hid_end_parse(d);
    397  1.61.2.2.4.1     skrll 	return maxid;
    398           1.1  augustss }
    399           1.1  augustss 
    400           1.1  augustss int
    401           1.1  augustss uhidevprint(void *aux, const char *pnp)
    402           1.1  augustss {
    403           1.1  augustss 	struct uhidev_attach_arg *uha = aux;
    404           1.1  augustss 
    405           1.1  augustss 	if (pnp)
    406          1.12   thorpej 		aprint_normal("uhid at %s", pnp);
    407           1.1  augustss 	if (uha->reportid != 0)
    408          1.12   thorpej 		aprint_normal(" reportid %d", uha->reportid);
    409  1.61.2.2.4.1     skrll 	return UNCONF;
    410           1.1  augustss }
    411           1.1  augustss 
    412           1.1  augustss int
    413          1.39    dyoung uhidev_activate(device_t self, enum devact act)
    414           1.1  augustss {
    415          1.39    dyoung 	struct uhidev_softc *sc = device_private(self);
    416           1.1  augustss 
    417           1.1  augustss 	switch (act) {
    418           1.1  augustss 	case DVACT_DEACTIVATE:
    419           1.1  augustss 		sc->sc_dying = 1;
    420          1.45    dyoung 		return 0;
    421          1.16  christos 	default:
    422          1.45    dyoung 		return EOPNOTSUPP;
    423           1.1  augustss 	}
    424           1.1  augustss }
    425           1.1  augustss 
    426          1.39    dyoung void
    427          1.39    dyoung uhidev_childdet(device_t self, device_t child)
    428          1.39    dyoung {
    429          1.39    dyoung 	int i;
    430          1.39    dyoung 	struct uhidev_softc *sc = device_private(self);
    431          1.39    dyoung 
    432          1.39    dyoung 	for (i = 0; i < sc->sc_nrepid; i++) {
    433          1.42  drochner 		if (sc->sc_subdevs[i] == child)
    434          1.39    dyoung 			break;
    435          1.39    dyoung 	}
    436          1.39    dyoung 	KASSERT(i < sc->sc_nrepid);
    437          1.39    dyoung 	sc->sc_subdevs[i] = NULL;
    438          1.39    dyoung }
    439          1.39    dyoung 
    440          1.58     skrll int
    441          1.48    dyoung uhidev_detach(device_t self, int flags)
    442           1.1  augustss {
    443          1.48    dyoung 	struct uhidev_softc *sc = device_private(self);
    444           1.1  augustss 	int i, rv;
    445          1.42  drochner 	struct uhidev *csc;
    446           1.1  augustss 
    447           1.1  augustss 	DPRINTF(("uhidev_detach: sc=%p flags=%d\n", sc, flags));
    448           1.1  augustss 
    449           1.1  augustss 	sc->sc_dying = 1;
    450          1.25     skrll 	if (sc->sc_ipipe != NULL)
    451          1.25     skrll 		usbd_abort_pipe(sc->sc_ipipe);
    452           1.1  augustss 
    453           1.1  augustss 	if (sc->sc_repdesc != NULL)
    454  1.61.2.2.4.1     skrll 		kmem_free(sc->sc_repdesc, sc->sc_repdesc_size);
    455           1.1  augustss 
    456           1.1  augustss 	rv = 0;
    457           1.1  augustss 	for (i = 0; i < sc->sc_nrepid; i++) {
    458           1.1  augustss 		if (sc->sc_subdevs[i] != NULL) {
    459          1.42  drochner 			csc = device_private(sc->sc_subdevs[i]);
    460          1.42  drochner 			rnd_detach_source(&csc->rnd_source);
    461          1.42  drochner 			rv |= config_detach(sc->sc_subdevs[i], flags);
    462           1.1  augustss 		}
    463           1.1  augustss 	}
    464           1.1  augustss 
    465  1.61.2.2.4.1     skrll 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
    466           1.1  augustss 
    467          1.38  jmcneill 	pmf_device_deregister(self);
    468          1.56       mrg 	mutex_destroy(&sc->sc_lock);
    469          1.38  jmcneill 
    470  1.61.2.2.4.1     skrll 	return rv;
    471           1.1  augustss }
    472           1.1  augustss 
    473           1.1  augustss void
    474  1.61.2.2.4.1     skrll uhidev_intr(struct usbd_xfer *xfer, void *addr, usbd_status status)
    475           1.1  augustss {
    476           1.1  augustss 	struct uhidev_softc *sc = addr;
    477          1.42  drochner 	device_t cdev;
    478           1.1  augustss 	struct uhidev *scd;
    479           1.1  augustss 	u_char *p;
    480           1.1  augustss 	u_int rep;
    481  1.61.2.2.4.1     skrll 	uint32_t cc;
    482           1.1  augustss 
    483           1.1  augustss 	usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
    484           1.1  augustss 
    485           1.1  augustss #ifdef UHIDEV_DEBUG
    486           1.1  augustss 	if (uhidevdebug > 5) {
    487  1.61.2.2.4.1     skrll 		uint32_t i;
    488           1.6  augustss 
    489           1.1  augustss 		DPRINTF(("uhidev_intr: status=%d cc=%d\n", status, cc));
    490           1.1  augustss 		DPRINTF(("uhidev_intr: data ="));
    491           1.1  augustss 		for (i = 0; i < cc; i++)
    492           1.1  augustss 			DPRINTF((" %02x", sc->sc_ibuf[i]));
    493           1.1  augustss 		DPRINTF(("\n"));
    494           1.1  augustss 	}
    495           1.1  augustss #endif
    496           1.1  augustss 
    497           1.1  augustss 	if (status == USBD_CANCELLED)
    498           1.1  augustss 		return;
    499           1.1  augustss 
    500           1.1  augustss 	if (status != USBD_NORMAL_COMPLETION) {
    501          1.48    dyoung 		DPRINTF(("%s: interrupt status=%d\n", device_xname(sc->sc_dev),
    502           1.3  augustss 			 status));
    503          1.25     skrll 		usbd_clear_endpoint_stall_async(sc->sc_ipipe);
    504           1.1  augustss 		return;
    505           1.1  augustss 	}
    506           1.1  augustss 
    507           1.1  augustss 	p = sc->sc_ibuf;
    508           1.1  augustss 	if (sc->sc_nrepid != 1)
    509           1.1  augustss 		rep = *p++, cc--;
    510           1.1  augustss 	else
    511           1.1  augustss 		rep = 0;
    512           1.1  augustss 	if (rep >= sc->sc_nrepid) {
    513           1.1  augustss 		printf("uhidev_intr: bad repid %d\n", rep);
    514           1.1  augustss 		return;
    515           1.1  augustss 	}
    516          1.42  drochner 	cdev = sc->sc_subdevs[rep];
    517          1.42  drochner 	if (!cdev)
    518          1.42  drochner 		return;
    519          1.42  drochner 	scd = device_private(cdev);
    520           1.1  augustss 	DPRINTFN(5,("uhidev_intr: rep=%d, scd=%p state=0x%x\n",
    521           1.1  augustss 		    rep, scd, scd ? scd->sc_state : 0));
    522          1.42  drochner 	if (!(scd->sc_state & UHIDEV_OPEN))
    523           1.1  augustss 		return;
    524          1.46  jakllsch #ifdef UHIDEV_DEBUG
    525          1.46  jakllsch 	if (scd->sc_in_rep_size != cc) {
    526          1.46  jakllsch 		DPRINTF(("%s: expected %d bytes, got %d\n",
    527          1.48    dyoung 		       device_xname(sc->sc_dev), scd->sc_in_rep_size, cc));
    528          1.46  jakllsch 	}
    529          1.46  jakllsch #endif
    530          1.46  jakllsch 	if (cc == 0) {
    531          1.46  jakllsch 		DPRINTF(("%s: 0-length input ignored\n",
    532          1.48    dyoung 			device_xname(sc->sc_dev)));
    533          1.23  augustss 		return;
    534          1.23  augustss 	}
    535          1.10      fair 	rnd_add_uint32(&scd->rnd_source, (uintptr_t)(sc->sc_ibuf));
    536           1.1  augustss 	scd->sc_intr(scd, p, cc);
    537           1.1  augustss }
    538           1.1  augustss 
    539           1.1  augustss void
    540           1.1  augustss uhidev_get_report_desc(struct uhidev_softc *sc, void **desc, int *size)
    541           1.1  augustss {
    542           1.1  augustss 	*desc = sc->sc_repdesc;
    543           1.1  augustss 	*size = sc->sc_repdesc_size;
    544           1.1  augustss }
    545           1.1  augustss 
    546           1.1  augustss int
    547           1.1  augustss uhidev_open(struct uhidev *scd)
    548           1.1  augustss {
    549           1.1  augustss 	struct uhidev_softc *sc = scd->sc_parent;
    550           1.1  augustss 	usbd_status err;
    551          1.25     skrll 	int error;
    552           1.1  augustss 
    553          1.56       mrg 	DPRINTF(("uhidev_open: open pipe, state=%d\n", scd->sc_state));
    554           1.1  augustss 
    555          1.56       mrg 	mutex_enter(&sc->sc_lock);
    556          1.56       mrg 	if (scd->sc_state & UHIDEV_OPEN) {
    557          1.56       mrg 		mutex_exit(&sc->sc_lock);
    558  1.61.2.2.4.1     skrll 		return EBUSY;
    559          1.56       mrg 	}
    560           1.1  augustss 	scd->sc_state |= UHIDEV_OPEN;
    561          1.60     skrll 	if (sc->sc_refcnt++) {
    562          1.60     skrll 		mutex_exit(&sc->sc_lock);
    563  1.61.2.2.4.1     skrll 		return 0;
    564          1.60     skrll 	}
    565          1.56       mrg 	mutex_exit(&sc->sc_lock);
    566           1.1  augustss 
    567           1.1  augustss 	if (sc->sc_isize == 0)
    568  1.61.2.2.4.1     skrll 		return 0;
    569           1.1  augustss 
    570  1.61.2.2.4.1     skrll 	sc->sc_ibuf = kmem_alloc(sc->sc_isize, KM_SLEEP);
    571           1.1  augustss 
    572          1.25     skrll 	/* Set up input interrupt pipe. */
    573           1.1  augustss 	DPRINTF(("uhidev_open: isize=%d, ep=0x%02x\n", sc->sc_isize,
    574          1.25     skrll 		 sc->sc_iep_addr));
    575          1.58     skrll 
    576          1.25     skrll 	err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_iep_addr,
    577          1.25     skrll 		  USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_ibuf,
    578           1.1  augustss 		  sc->sc_isize, uhidev_intr, USBD_DEFAULT_INTERVAL);
    579          1.25     skrll 	if (err != USBD_NORMAL_COMPLETION) {
    580           1.1  augustss 		DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
    581          1.25     skrll 		    "error=%d\n", err));
    582          1.25     skrll 		error = EIO;
    583          1.25     skrll 		goto out1;
    584          1.25     skrll 	}
    585          1.25     skrll 
    586          1.25     skrll 	/*
    587          1.25     skrll 	 * Set up output interrupt pipe if an output interrupt endpoint
    588          1.25     skrll 	 * exists.
    589          1.25     skrll 	 */
    590          1.25     skrll 	if (sc->sc_oep_addr != -1) {
    591          1.25     skrll 		DPRINTF(("uhidev_open: oep=0x%02x\n", sc->sc_oep_addr));
    592          1.25     skrll 
    593          1.25     skrll 		err = usbd_open_pipe(sc->sc_iface, sc->sc_oep_addr,
    594          1.25     skrll 		    0, &sc->sc_opipe);
    595          1.25     skrll 
    596          1.25     skrll 		if (err != USBD_NORMAL_COMPLETION) {
    597          1.25     skrll 			DPRINTF(("uhidev_open: usbd_open_pipe failed, "
    598          1.25     skrll 			    "error=%d\n", err));
    599          1.25     skrll 			error = EIO;
    600          1.25     skrll 			goto out2;
    601          1.25     skrll 		}
    602          1.25     skrll 		DPRINTF(("uhidev_open: sc->sc_opipe=%p\n", sc->sc_opipe));
    603          1.25     skrll 
    604  1.61.2.2.4.1     skrll 		error = usbd_create_xfer(sc->sc_opipe, UHIDEV_OSIZE, 0, 0,
    605  1.61.2.2.4.1     skrll 		    &sc->sc_oxfer);
    606  1.61.2.2.4.1     skrll 		if (error) {
    607          1.25     skrll 			DPRINTF(("uhidev_open: couldn't allocate an xfer\n"));
    608          1.25     skrll 			goto out3;
    609          1.25     skrll 		}
    610      1.61.2.1       snj 
    611      1.61.2.1       snj 		if (sc->sc_flags & UHIDEV_F_XB1) {
    612      1.61.2.1       snj 			uint8_t init_data[] = { 0x05, 0x20 };
    613      1.61.2.1       snj 			int init_data_len = sizeof(init_data);
    614      1.61.2.1       snj 			err = usbd_intr_transfer(sc->sc_oxfer, sc->sc_opipe, 0,
    615  1.61.2.2.4.1     skrll 			    USBD_NO_TIMEOUT, init_data, &init_data_len);
    616      1.61.2.1       snj 			if (err != USBD_NORMAL_COMPLETION) {
    617      1.61.2.1       snj 				DPRINTF(("uhidev_open: xb1 init failed, "
    618      1.61.2.1       snj 				    "error=%d\n", err));
    619      1.61.2.1       snj 				error = EIO;
    620      1.61.2.1       snj 				goto out4;
    621      1.61.2.1       snj 			}
    622      1.61.2.1       snj 		}
    623           1.1  augustss 	}
    624          1.58     skrll 
    625  1.61.2.2.4.1     skrll 	return 0;
    626      1.61.2.1       snj out4:
    627      1.61.2.1       snj 	/* Free output xfer */
    628      1.61.2.1       snj 	if (sc->sc_oxfer != NULL)
    629  1.61.2.2.4.1     skrll 		usbd_destroy_xfer(sc->sc_oxfer);
    630          1.25     skrll out3:
    631          1.25     skrll 	/* Abort output pipe */
    632          1.25     skrll 	usbd_close_pipe(sc->sc_opipe);
    633          1.25     skrll out2:
    634          1.25     skrll 	/* Abort input pipe */
    635          1.25     skrll 	usbd_close_pipe(sc->sc_ipipe);
    636          1.25     skrll out1:
    637          1.25     skrll 	DPRINTF(("uhidev_open: failed in someway"));
    638  1.61.2.2.4.1     skrll 	kmem_free(sc->sc_ibuf, sc->sc_isize);
    639          1.56       mrg 	mutex_enter(&sc->sc_lock);
    640          1.25     skrll 	scd->sc_state &= ~UHIDEV_OPEN;
    641          1.60     skrll 	sc->sc_refcnt = 0;
    642          1.59  christos 	sc->sc_ibuf = NULL;
    643          1.25     skrll 	sc->sc_ipipe = NULL;
    644          1.25     skrll 	sc->sc_opipe = NULL;
    645          1.25     skrll 	sc->sc_oxfer = NULL;
    646          1.56       mrg 	mutex_exit(&sc->sc_lock);
    647          1.25     skrll 	return error;
    648           1.1  augustss }
    649           1.1  augustss 
    650           1.1  augustss void
    651      1.61.2.2       snj uhidev_stop(struct uhidev *scd)
    652      1.61.2.2       snj {
    653      1.61.2.2       snj 	struct uhidev_softc *sc = scd->sc_parent;
    654      1.61.2.2       snj 
    655      1.61.2.2       snj 	/* Disable interrupts. */
    656      1.61.2.2       snj 	if (sc->sc_opipe != NULL) {
    657      1.61.2.2       snj 		usbd_abort_pipe(sc->sc_opipe);
    658      1.61.2.2       snj 		usbd_close_pipe(sc->sc_opipe);
    659      1.61.2.2       snj 		sc->sc_opipe = NULL;
    660      1.61.2.2       snj 	}
    661      1.61.2.2       snj 
    662      1.61.2.2       snj 	if (sc->sc_ipipe != NULL) {
    663      1.61.2.2       snj 		usbd_abort_pipe(sc->sc_ipipe);
    664      1.61.2.2       snj 		usbd_close_pipe(sc->sc_ipipe);
    665      1.61.2.2       snj 		sc->sc_ipipe = NULL;
    666      1.61.2.2       snj 	}
    667      1.61.2.2       snj 
    668      1.61.2.2       snj 	if (sc->sc_ibuf != NULL) {
    669  1.61.2.2.4.1     skrll 		kmem_free(sc->sc_ibuf, sc->sc_isize);
    670      1.61.2.2       snj 		sc->sc_ibuf = NULL;
    671      1.61.2.2       snj 	}
    672      1.61.2.2       snj }
    673      1.61.2.2       snj 
    674      1.61.2.2       snj void
    675           1.1  augustss uhidev_close(struct uhidev *scd)
    676           1.1  augustss {
    677           1.1  augustss 	struct uhidev_softc *sc = scd->sc_parent;
    678           1.1  augustss 
    679          1.56       mrg 	mutex_enter(&sc->sc_lock);
    680          1.56       mrg 	if (!(scd->sc_state & UHIDEV_OPEN)) {
    681          1.56       mrg 		mutex_exit(&sc->sc_lock);
    682           1.1  augustss 		return;
    683          1.56       mrg 	}
    684           1.1  augustss 	scd->sc_state &= ~UHIDEV_OPEN;
    685          1.60     skrll 	if (--sc->sc_refcnt) {
    686          1.60     skrll 		mutex_exit(&sc->sc_lock);
    687          1.60     skrll 		return;
    688          1.60     skrll 	}
    689          1.56       mrg 	mutex_exit(&sc->sc_lock);
    690          1.56       mrg 
    691           1.1  augustss 	DPRINTF(("uhidev_close: close pipe\n"));
    692           1.1  augustss 
    693          1.59  christos 	if (sc->sc_oxfer != NULL) {
    694  1.61.2.2.4.1     skrll 		usbd_destroy_xfer(sc->sc_oxfer);
    695          1.59  christos 		sc->sc_oxfer = NULL;
    696          1.59  christos 	}
    697          1.58     skrll 
    698  1.61.2.2.4.1     skrll 
    699      1.61.2.2       snj 	/* Possibly redundant, but properly handled */
    700      1.61.2.2       snj 	uhidev_stop(scd);
    701           1.1  augustss }
    702           1.1  augustss 
    703           1.1  augustss usbd_status
    704           1.1  augustss uhidev_set_report(struct uhidev *scd, int type, void *data, int len)
    705           1.1  augustss {
    706          1.13   dsainty 	char *buf;
    707          1.13   dsainty 	usbd_status retstat;
    708          1.13   dsainty 
    709          1.13   dsainty 	if (scd->sc_report_id == 0)
    710          1.13   dsainty 		return usbd_set_report(scd->sc_parent->sc_iface, type,
    711          1.13   dsainty 				       scd->sc_report_id, data, len);
    712          1.13   dsainty 
    713  1.61.2.2.4.1     skrll 	buf = kmem_alloc(len + 1, KM_SLEEP);
    714          1.13   dsainty 	buf[0] = scd->sc_report_id;
    715          1.13   dsainty 	memcpy(buf+1, data, len);
    716          1.13   dsainty 
    717          1.13   dsainty 	retstat = usbd_set_report(scd->sc_parent->sc_iface, type,
    718          1.18   dsainty 				  scd->sc_report_id, buf, len + 1);
    719          1.13   dsainty 
    720  1.61.2.2.4.1     skrll 	kmem_free(buf, len + 1);
    721           1.1  augustss 
    722          1.13   dsainty 	return retstat;
    723           1.1  augustss }
    724           1.1  augustss 
    725           1.1  augustss usbd_status
    726           1.1  augustss uhidev_get_report(struct uhidev *scd, int type, void *data, int len)
    727           1.1  augustss {
    728           1.6  augustss 	return usbd_get_report(scd->sc_parent->sc_iface, type,
    729           1.1  augustss 			       scd->sc_report_id, data, len);
    730           1.1  augustss }
    731          1.25     skrll 
    732          1.25     skrll usbd_status
    733          1.25     skrll uhidev_write(struct uhidev_softc *sc, void *data, int len)
    734          1.25     skrll {
    735          1.25     skrll 
    736          1.25     skrll 	DPRINTF(("uhidev_write: data=%p, len=%d\n", data, len));
    737          1.25     skrll 
    738          1.25     skrll 	if (sc->sc_opipe == NULL)
    739          1.25     skrll 		return USBD_INVAL;
    740          1.25     skrll 
    741          1.25     skrll #ifdef UHIDEV_DEBUG
    742          1.25     skrll 	if (uhidevdebug > 50) {
    743          1.25     skrll 
    744  1.61.2.2.4.1     skrll 		uint32_t i;
    745  1.61.2.2.4.1     skrll 		uint8_t *d = data;
    746          1.25     skrll 
    747          1.25     skrll 		DPRINTF(("uhidev_write: data ="));
    748          1.25     skrll 		for (i = 0; i < len; i++)
    749          1.25     skrll 			DPRINTF((" %02x", d[i]));
    750          1.25     skrll 		DPRINTF(("\n"));
    751          1.25     skrll 	}
    752          1.25     skrll #endif
    753  1.61.2.2.4.1     skrll 	return usbd_intr_transfer(sc->sc_oxfer, sc->sc_opipe, 0, USBD_NO_TIMEOUT,
    754  1.61.2.2.4.1     skrll 	    data, &len);
    755          1.25     skrll }
    756