Home | History | Annotate | Line # | Download | only in usb
uhidev.c revision 1.79.2.2
      1  1.79.2.2   thorpej /*	$NetBSD: uhidev.c,v 1.79.2.2 2021/03/22 16:23:46 thorpej 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.79.2.2   thorpej __KERNEL_RCSID(0, "$NetBSD: uhidev.c,v 1.79.2.2 2021/03/22 16:23:46 thorpej Exp $");
     39      1.66  jakllsch 
     40      1.66  jakllsch #ifdef _KERNEL_OPT
     41      1.66  jakllsch #include "opt_usb.h"
     42      1.66  jakllsch #endif
     43       1.1  augustss 
     44       1.1  augustss #include <sys/param.h>
     45      1.79  riastrad #include <sys/types.h>
     46      1.79  riastrad 
     47      1.79  riastrad #include <sys/conf.h>
     48      1.79  riastrad #include <sys/device.h>
     49      1.79  riastrad #include <sys/ioctl.h>
     50       1.1  augustss #include <sys/kernel.h>
     51      1.65     skrll #include <sys/kmem.h>
     52      1.79  riastrad #include <sys/lwp.h>
     53      1.79  riastrad #include <sys/rndsource.h>
     54       1.1  augustss #include <sys/signalvar.h>
     55      1.79  riastrad #include <sys/systm.h>
     56       1.1  augustss 
     57       1.1  augustss #include <dev/usb/usb.h>
     58       1.1  augustss #include <dev/usb/usbhid.h>
     59       1.1  augustss 
     60       1.1  augustss #include <dev/usb/usbdevs.h>
     61       1.1  augustss #include <dev/usb/usbdi.h>
     62       1.1  augustss #include <dev/usb/usbdi_util.h>
     63       1.1  augustss #include <dev/usb/usb_quirks.h>
     64       1.1  augustss 
     65       1.1  augustss #include <dev/usb/uhidev.h>
     66      1.73    bouyer #include <dev/hid/hid.h>
     67       1.1  augustss 
     68       1.1  augustss /* Report descriptor for broken Wacom Graphire */
     69       1.1  augustss #include <dev/usb/ugraphire_rdesc.h>
     70      1.51  jmcneill /* Report descriptor for game controllers in "XInput" mode */
     71      1.51  jmcneill #include <dev/usb/xinput_rdesc.h>
     72      1.62  jmcneill /* Report descriptor for Xbox One controllers */
     73      1.62  jmcneill #include <dev/usb/x1input_rdesc.h>
     74       1.1  augustss 
     75      1.22  drochner #include "locators.h"
     76      1.22  drochner 
     77       1.1  augustss #ifdef UHIDEV_DEBUG
     78      1.48    dyoung #define DPRINTF(x)	if (uhidevdebug) printf x
     79      1.48    dyoung #define DPRINTFN(n,x)	if (uhidevdebug>(n)) printf x
     80       1.1  augustss int	uhidevdebug = 0;
     81       1.1  augustss #else
     82       1.1  augustss #define DPRINTF(x)
     83       1.1  augustss #define DPRINTFN(n,x)
     84       1.1  augustss #endif
     85       1.1  augustss 
     86      1.65     skrll Static void uhidev_intr(struct usbd_xfer *, void *, usbd_status);
     87       1.1  augustss 
     88      1.29  drochner Static int uhidev_maxrepid(void *, int);
     89      1.29  drochner Static int uhidevprint(void *, const char *);
     90       1.1  augustss 
     91      1.76      maxv static int uhidev_match(device_t, cfdata_t, void *);
     92      1.76      maxv static void uhidev_attach(device_t, device_t, void *);
     93      1.76      maxv static void uhidev_childdet(device_t, device_t);
     94      1.76      maxv static int uhidev_detach(device_t, int);
     95      1.76      maxv static int uhidev_activate(device_t, enum devact);
     96      1.75       mrg 
     97      1.41      cube CFATTACH_DECL2_NEW(uhidev, sizeof(struct uhidev_softc), uhidev_match,
     98      1.39    dyoung     uhidev_attach, uhidev_detach, uhidev_activate, NULL, uhidev_childdet);
     99       1.1  augustss 
    100      1.76      maxv static int
    101      1.48    dyoung uhidev_match(device_t parent, cfdata_t match, void *aux)
    102       1.1  augustss {
    103      1.65     skrll 	struct usbif_attach_arg *uiaa = aux;
    104       1.6  augustss 
    105      1.51  jmcneill 	/* Game controllers in "XInput" mode */
    106      1.65     skrll 	if (USBIF_IS_XINPUT(uiaa))
    107      1.51  jmcneill 		return UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO;
    108      1.62  jmcneill 	/* Xbox One controllers */
    109      1.69     skrll 	if (USBIF_IS_X1INPUT(uiaa) && uiaa->uiaa_ifaceno == 0)
    110      1.62  jmcneill 		return UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO;
    111      1.62  jmcneill 
    112      1.65     skrll 	if (uiaa->uiaa_class != UICLASS_HID)
    113      1.65     skrll 		return UMATCH_NONE;
    114      1.65     skrll 	if (usbd_get_quirks(uiaa->uiaa_device)->uq_flags & UQ_HID_IGNORE)
    115      1.65     skrll 		return UMATCH_NONE;
    116      1.65     skrll 	return UMATCH_IFACECLASS_GENERIC;
    117       1.1  augustss }
    118       1.1  augustss 
    119      1.76      maxv static void
    120      1.48    dyoung uhidev_attach(device_t parent, device_t self, void *aux)
    121       1.1  augustss {
    122      1.48    dyoung 	struct uhidev_softc *sc = device_private(self);
    123      1.65     skrll 	struct usbif_attach_arg *uiaa = aux;
    124      1.65     skrll 	struct usbd_interface *iface = uiaa->uiaa_iface;
    125       1.1  augustss 	usb_interface_descriptor_t *id;
    126       1.1  augustss 	usb_endpoint_descriptor_t *ed;
    127       1.1  augustss 	struct uhidev_attach_arg uha;
    128      1.42  drochner 	device_t dev;
    129      1.42  drochner 	struct uhidev *csc;
    130      1.44     rafal 	int maxinpktsize, size, nrepid, repid, repsz;
    131      1.32  christos 	int *repsizes;
    132      1.25     skrll 	int i;
    133      1.19  jdolecek 	void *desc;
    134      1.20  augustss 	const void *descptr;
    135       1.1  augustss 	usbd_status err;
    136      1.26  augustss 	char *devinfop;
    137      1.28  drochner 	int locs[UHIDBUSCF_NLOCS];
    138       1.6  augustss 
    139      1.41      cube 	sc->sc_dev = self;
    140      1.65     skrll 	sc->sc_udev = uiaa->uiaa_device;
    141       1.1  augustss 	sc->sc_iface = iface;
    142      1.43    plunky 
    143      1.43    plunky 	aprint_naive("\n");
    144      1.43    plunky 	aprint_normal("\n");
    145      1.43    plunky 
    146      1.65     skrll 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
    147      1.79  riastrad 	cv_init(&sc->sc_cv, "uhidev");
    148      1.79  riastrad 	sc->sc_writelock = NULL;
    149      1.79  riastrad 	sc->sc_configlock = NULL;
    150      1.56       mrg 
    151       1.1  augustss 	id = usbd_get_interface_descriptor(iface);
    152      1.26  augustss 
    153      1.65     skrll 	devinfop = usbd_devinfo_alloc(uiaa->uiaa_device, 0);
    154      1.41      cube 	aprint_normal_dev(self, "%s, iclass %d/%d\n",
    155      1.26  augustss 	       devinfop, id->bInterfaceClass, id->bInterfaceSubClass);
    156      1.26  augustss 	usbd_devinfo_free(devinfop);
    157       1.1  augustss 
    158      1.38  jmcneill 	if (!pmf_device_register(self, NULL, NULL))
    159      1.38  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    160      1.38  jmcneill 
    161      1.72     ryoon 	if (uiaa->uiaa_vendor == USB_VENDOR_WACOM) {
    162      1.72     ryoon 		if (uiaa->uiaa_product == USB_PRODUCT_WACOM_XD0912U) {
    163      1.72     ryoon 		/*
    164      1.72     ryoon 		 * Wacom Intuos2 (XD-0912-U) requires longer idle time to
    165      1.72     ryoon 		 * initialize the device with 0x0202.
    166      1.72     ryoon 		 */
    167      1.72     ryoon 			DELAY(500000);
    168      1.72     ryoon 		}
    169      1.72     ryoon 	}
    170       1.1  augustss 	(void)usbd_set_idle(iface, 0, 0);
    171       1.1  augustss 
    172      1.71  jakllsch 	if ((usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_NO_SET_PROTO) == 0)
    173       1.1  augustss 		(void)usbd_set_protocol(iface, 1);
    174       1.1  augustss 
    175      1.44     rafal 	maxinpktsize = 0;
    176      1.25     skrll 	sc->sc_iep_addr = sc->sc_oep_addr = -1;
    177      1.25     skrll 	for (i = 0; i < id->bNumEndpoints; i++) {
    178      1.25     skrll 		ed = usbd_interface2endpoint_descriptor(iface, i);
    179      1.25     skrll 		if (ed == NULL) {
    180      1.41      cube 			aprint_error_dev(self,
    181      1.41      cube 			    "could not read endpoint descriptor\n");
    182      1.25     skrll 			sc->sc_dying = 1;
    183      1.48    dyoung 			return;
    184      1.25     skrll 		}
    185      1.25     skrll 
    186      1.25     skrll 		DPRINTFN(10,("uhidev_attach: bLength=%d bDescriptorType=%d "
    187      1.25     skrll 		    "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
    188      1.25     skrll 		    " bInterval=%d\n",
    189      1.25     skrll 		    ed->bLength, ed->bDescriptorType,
    190      1.25     skrll 		    ed->bEndpointAddress & UE_ADDR,
    191      1.25     skrll 		    UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out",
    192      1.25     skrll 		    ed->bmAttributes & UE_XFERTYPE,
    193      1.25     skrll 		    UGETW(ed->wMaxPacketSize), ed->bInterval));
    194      1.25     skrll 
    195      1.25     skrll 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    196      1.25     skrll 		    (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
    197      1.44     rafal 			maxinpktsize = UGETW(ed->wMaxPacketSize);
    198      1.25     skrll 			sc->sc_iep_addr = ed->bEndpointAddress;
    199      1.25     skrll 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    200      1.25     skrll 		    (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
    201      1.25     skrll 			sc->sc_oep_addr = ed->bEndpointAddress;
    202      1.25     skrll 		} else {
    203      1.41      cube 			aprint_verbose_dev(self, "endpoint %d: ignored\n", i);
    204      1.25     skrll 		}
    205       1.1  augustss 	}
    206       1.1  augustss 
    207      1.25     skrll 	/*
    208      1.25     skrll 	 * Check that we found an input interrupt endpoint. The output interrupt
    209      1.25     skrll 	 * endpoint is optional
    210      1.25     skrll 	 */
    211      1.25     skrll 	if (sc->sc_iep_addr == -1) {
    212      1.41      cube 		aprint_error_dev(self, "no input interrupt endpoint\n");
    213       1.1  augustss 		sc->sc_dying = 1;
    214      1.48    dyoung 		return;
    215       1.1  augustss 	}
    216       1.1  augustss 
    217       1.1  augustss 	/* XXX need to extend this */
    218      1.20  augustss 	descptr = NULL;
    219      1.65     skrll 	if (uiaa->uiaa_vendor == USB_VENDOR_WACOM) {
    220      1.72     ryoon 		static uByte reportbuf[3];
    221      1.17  augustss 
    222       1.1  augustss 		/* The report descriptor for the Wacom Graphire is broken. */
    223      1.65     skrll 		switch (uiaa->uiaa_product) {
    224      1.33      ghen 		case USB_PRODUCT_WACOM_GRAPHIRE3_4X5:
    225      1.33      ghen 		case USB_PRODUCT_WACOM_GRAPHIRE3_6X8:
    226      1.33      ghen 		case USB_PRODUCT_WACOM_GRAPHIRE4_4X5: /* The 6x8 too? */
    227      1.17  augustss 			/*
    228      1.17  augustss 			 * The Graphire3 needs 0x0202 to be written to
    229      1.17  augustss 			 * feature report ID 2 before it'll start
    230      1.17  augustss 			 * returning digitizer data.
    231      1.17  augustss 			 */
    232      1.72     ryoon 			reportbuf[0] = 0x02;
    233      1.72     ryoon 			reportbuf[1] = 0x02;
    234      1.65     skrll 			usbd_set_report(uiaa->uiaa_iface, UHID_FEATURE_REPORT, 2,
    235      1.72     ryoon 			    &reportbuf, 2);
    236      1.17  augustss 
    237      1.65     skrll 			size = sizeof(uhid_graphire3_4x5_report_descr);
    238      1.17  augustss 			descptr = uhid_graphire3_4x5_report_descr;
    239      1.17  augustss 			break;
    240      1.72     ryoon 		case USB_PRODUCT_WACOM_GRAPHIRE:
    241      1.72     ryoon 		case USB_PRODUCT_WACOM_GRAPHIRE2:
    242      1.72     ryoon 		case USB_PRODUCT_WACOM_XD0912U:
    243      1.72     ryoon 		case USB_PRODUCT_WACOM_CTH690K0:
    244      1.72     ryoon 			reportbuf[0] = 0x02;
    245      1.72     ryoon 			reportbuf[1] = 0x02;
    246      1.72     ryoon 			usbd_set_report(uiaa->uiaa_iface, UHID_FEATURE_REPORT, 2,
    247      1.72     ryoon 			    &reportbuf, 2);
    248      1.72     ryoon 			break;
    249      1.20  augustss 		default:
    250      1.20  augustss 			/* Keep descriptor */
    251      1.20  augustss 			break;
    252      1.17  augustss 		}
    253      1.17  augustss 	}
    254      1.65     skrll 	if (USBIF_IS_XINPUT(uiaa)) {
    255      1.65     skrll 		size = sizeof(uhid_xinput_report_descr);
    256      1.51  jmcneill 		descptr = uhid_xinput_report_descr;
    257      1.51  jmcneill 	}
    258      1.65     skrll 	if (USBIF_IS_X1INPUT(uiaa)) {
    259      1.62  jmcneill 		sc->sc_flags |= UHIDEV_F_XB1;
    260      1.65     skrll 		size = sizeof(uhid_x1input_report_descr);
    261      1.62  jmcneill 		descptr = uhid_x1input_report_descr;
    262      1.62  jmcneill 	}
    263      1.17  augustss 
    264      1.17  augustss 	if (descptr) {
    265      1.65     skrll 		desc = kmem_alloc(size, KM_SLEEP);
    266      1.70       chs 		err = USBD_NORMAL_COMPLETION;
    267      1.70       chs 		memcpy(desc, descptr, size);
    268       1.1  augustss 	} else {
    269       1.1  augustss 		desc = NULL;
    270      1.65     skrll 		err = usbd_read_report_desc(uiaa->uiaa_iface, &desc, &size);
    271       1.1  augustss 	}
    272       1.1  augustss 	if (err) {
    273      1.41      cube 		aprint_error_dev(self, "no report descriptor\n");
    274       1.1  augustss 		sc->sc_dying = 1;
    275      1.48    dyoung 		return;
    276       1.1  augustss 	}
    277       1.6  augustss 
    278      1.65     skrll 	if (uiaa->uiaa_vendor == USB_VENDOR_HOSIDEN &&
    279      1.65     skrll 	    uiaa->uiaa_product == USB_PRODUCT_HOSIDEN_PPP) {
    280      1.27  augustss 		static uByte reportbuf[] = { 1 };
    281      1.27  augustss 		/*
    282      1.58     skrll 		 *  This device was sold by Konami with its ParaParaParadise
    283      1.27  augustss 		 *  game for PlayStation2.  It needs to be "turned on"
    284      1.27  augustss 		 *  before it will send any reports.
    285      1.27  augustss 		 */
    286      1.27  augustss 
    287      1.65     skrll 		usbd_set_report(uiaa->uiaa_iface, UHID_FEATURE_REPORT, 0,
    288      1.65     skrll 		    &reportbuf, sizeof(reportbuf));
    289      1.27  augustss 	}
    290      1.27  augustss 
    291      1.65     skrll 	if (uiaa->uiaa_vendor == USB_VENDOR_LOGITECH &&
    292      1.65     skrll 	    uiaa->uiaa_product == USB_PRODUCT_LOGITECH_CBT44 && size == 0xb1) {
    293      1.47  jakllsch 		uint8_t *data = desc;
    294      1.47  jakllsch 		/*
    295      1.47  jakllsch 		 * This device has a odd USAGE_MINIMUM value that would
    296      1.47  jakllsch 		 * cause the multimedia keys to have their usage number
    297      1.47  jakllsch 		 * shifted up one usage.  Adjust so the usages are sane.
    298      1.47  jakllsch 		 */
    299      1.47  jakllsch 
    300      1.47  jakllsch 		if (data[0x56] == 0x19 && data[0x57] == 0x01 &&
    301      1.47  jakllsch 		    data[0x58] == 0x2a && data[0x59] == 0x8c)
    302      1.47  jakllsch 			data[0x57] = 0x00;
    303      1.47  jakllsch 	}
    304      1.47  jakllsch 
    305      1.52   aymeric 	/*
    306      1.52   aymeric 	 * Enable the Six Axis and DualShock 3 controllers.
    307      1.52   aymeric 	 * See http://ps3.jim.sh/sixaxis/usb/
    308      1.52   aymeric 	 */
    309      1.65     skrll 	if (uiaa->uiaa_vendor == USB_VENDOR_SONY &&
    310      1.65     skrll 	    uiaa->uiaa_product == USB_PRODUCT_SONY_PS3CONTROLLER) {
    311      1.52   aymeric 		usb_device_request_t req;
    312      1.52   aymeric 		char data[17];
    313      1.52   aymeric 		int actlen;
    314      1.52   aymeric 
    315      1.52   aymeric 		req.bmRequestType = UT_READ_CLASS_INTERFACE;
    316      1.52   aymeric 		req.bRequest = 1;
    317      1.52   aymeric 		USETW(req.wValue, 0x3f2);
    318      1.52   aymeric 		USETW(req.wIndex, 0);
    319      1.65     skrll 		USETW(req.wLength, sizeof(data));
    320      1.52   aymeric 
    321      1.52   aymeric 		usbd_do_request_flags(sc->sc_udev, &req, data,
    322      1.52   aymeric 			USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
    323      1.52   aymeric 	}
    324      1.52   aymeric 
    325       1.1  augustss 	sc->sc_repdesc = desc;
    326       1.1  augustss 	sc->sc_repdesc_size = size;
    327       1.1  augustss 
    328      1.65     skrll 	uha.uiaa = uiaa;
    329       1.1  augustss 	nrepid = uhidev_maxrepid(desc, size);
    330       1.1  augustss 	if (nrepid < 0)
    331      1.48    dyoung 		return;
    332       1.1  augustss 	if (nrepid > 0)
    333      1.41      cube 		aprint_normal_dev(self, "%d report ids\n", nrepid);
    334       1.1  augustss 	nrepid++;
    335      1.65     skrll 	repsizes = kmem_alloc(nrepid * sizeof(*repsizes), KM_SLEEP);
    336      1.79  riastrad 	sc->sc_subdevs = kmem_zalloc(nrepid * sizeof(sc->sc_subdevs[0]),
    337      1.65     skrll 	    KM_SLEEP);
    338      1.44     rafal 
    339      1.44     rafal 	/* Just request max packet size for the interrupt pipe */
    340      1.44     rafal 	sc->sc_isize = maxinpktsize;
    341       1.1  augustss 	sc->sc_nrepid = nrepid;
    342       1.1  augustss 
    343      1.68   msaitoh 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
    344       1.1  augustss 
    345       1.1  augustss 	for (repid = 0; repid < nrepid; repid++) {
    346       1.1  augustss 		repsz = hid_report_size(desc, size, hid_input, repid);
    347       1.1  augustss 		DPRINTF(("uhidev_match: repid=%d, repsz=%d\n", repid, repsz));
    348       1.2  augustss 		repsizes[repid] = repsz;
    349       1.1  augustss 	}
    350      1.44     rafal 
    351       1.1  augustss 	DPRINTF(("uhidev_attach: isize=%d\n", sc->sc_isize));
    352       1.1  augustss 
    353       1.1  augustss 	uha.parent = sc;
    354       1.1  augustss 	for (repid = 0; repid < nrepid; repid++) {
    355       1.1  augustss 		DPRINTF(("uhidev_match: try repid=%d\n", repid));
    356       1.1  augustss 		if (hid_report_size(desc, size, hid_input, repid) == 0 &&
    357       1.1  augustss 		    hid_report_size(desc, size, hid_output, repid) == 0 &&
    358       1.1  augustss 		    hid_report_size(desc, size, hid_feature, repid) == 0) {
    359       1.5  augustss 			;	/* already NULL in sc->sc_subdevs[repid] */
    360       1.1  augustss 		} else {
    361       1.1  augustss 			uha.reportid = repid;
    362      1.28  drochner 			locs[UHIDBUSCF_REPORTID] = repid;
    363      1.22  drochner 
    364  1.79.2.1   thorpej 			dev = config_found(self, &uha, uhidevprint,
    365  1.79.2.1   thorpej 					   CFARG_SUBMATCH, config_stdsubmatch,
    366  1.79.2.1   thorpej 					   CFARG_LOCATORS, locs,
    367  1.79.2.1   thorpej 					   CFARG_EOL);
    368       1.1  augustss 			sc->sc_subdevs[repid] = dev;
    369       1.4  augustss 			if (dev != NULL) {
    370      1.42  drochner 				csc = device_private(dev);
    371      1.42  drochner 				csc->sc_in_rep_size = repsizes[repid];
    372       1.1  augustss #ifdef DIAGNOSTIC
    373       1.1  augustss 				DPRINTF(("uhidev_match: repid=%d dev=%p\n",
    374       1.1  augustss 					 repid, dev));
    375      1.42  drochner 				if (csc->sc_intr == NULL) {
    376      1.65     skrll 					kmem_free(repsizes,
    377      1.65     skrll 					    nrepid * sizeof(*repsizes));
    378      1.41      cube 					aprint_error_dev(self,
    379      1.41      cube 					    "sc_intr == NULL\n");
    380      1.48    dyoung 					return;
    381       1.1  augustss 				}
    382       1.4  augustss #endif
    383      1.42  drochner 				rnd_attach_source(&csc->rnd_source,
    384      1.48    dyoung 						  device_xname(dev),
    385      1.61       tls 						  RND_TYPE_TTY,
    386      1.61       tls 						  RND_FLAG_DEFAULT);
    387       1.1  augustss 			}
    388       1.1  augustss 		}
    389       1.1  augustss 	}
    390      1.65     skrll 	kmem_free(repsizes, nrepid * sizeof(*repsizes));
    391       1.1  augustss 
    392      1.48    dyoung 	return;
    393       1.1  augustss }
    394       1.1  augustss 
    395       1.1  augustss int
    396       1.1  augustss uhidev_maxrepid(void *buf, int len)
    397       1.1  augustss {
    398       1.1  augustss 	struct hid_data *d;
    399       1.1  augustss 	struct hid_item h;
    400       1.1  augustss 	int maxid;
    401       1.1  augustss 
    402       1.1  augustss 	maxid = -1;
    403       1.1  augustss 	h.report_ID = 0;
    404       1.1  augustss 	for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); )
    405      1.74  jakllsch 		if ((int)h.report_ID > maxid)
    406       1.1  augustss 			maxid = h.report_ID;
    407       1.1  augustss 	hid_end_parse(d);
    408      1.65     skrll 	return maxid;
    409       1.1  augustss }
    410       1.1  augustss 
    411       1.1  augustss int
    412       1.1  augustss uhidevprint(void *aux, const char *pnp)
    413       1.1  augustss {
    414       1.1  augustss 	struct uhidev_attach_arg *uha = aux;
    415       1.1  augustss 
    416       1.1  augustss 	if (pnp)
    417      1.12   thorpej 		aprint_normal("uhid at %s", pnp);
    418       1.1  augustss 	if (uha->reportid != 0)
    419      1.12   thorpej 		aprint_normal(" reportid %d", uha->reportid);
    420      1.65     skrll 	return UNCONF;
    421       1.1  augustss }
    422       1.1  augustss 
    423      1.76      maxv static int
    424      1.39    dyoung uhidev_activate(device_t self, enum devact act)
    425       1.1  augustss {
    426      1.39    dyoung 	struct uhidev_softc *sc = device_private(self);
    427       1.1  augustss 
    428       1.1  augustss 	switch (act) {
    429       1.1  augustss 	case DVACT_DEACTIVATE:
    430       1.1  augustss 		sc->sc_dying = 1;
    431      1.45    dyoung 		return 0;
    432      1.16  christos 	default:
    433      1.45    dyoung 		return EOPNOTSUPP;
    434       1.1  augustss 	}
    435       1.1  augustss }
    436       1.1  augustss 
    437      1.76      maxv static void
    438      1.39    dyoung uhidev_childdet(device_t self, device_t child)
    439      1.39    dyoung {
    440      1.39    dyoung 	int i;
    441      1.39    dyoung 	struct uhidev_softc *sc = device_private(self);
    442      1.39    dyoung 
    443      1.39    dyoung 	for (i = 0; i < sc->sc_nrepid; i++) {
    444      1.42  drochner 		if (sc->sc_subdevs[i] == child)
    445      1.39    dyoung 			break;
    446      1.39    dyoung 	}
    447      1.39    dyoung 	KASSERT(i < sc->sc_nrepid);
    448      1.39    dyoung 	sc->sc_subdevs[i] = NULL;
    449      1.39    dyoung }
    450      1.39    dyoung 
    451      1.76      maxv static int
    452      1.48    dyoung uhidev_detach(device_t self, int flags)
    453       1.1  augustss {
    454      1.48    dyoung 	struct uhidev_softc *sc = device_private(self);
    455       1.1  augustss 	int i, rv;
    456      1.42  drochner 	struct uhidev *csc;
    457       1.1  augustss 
    458       1.1  augustss 	DPRINTF(("uhidev_detach: sc=%p flags=%d\n", sc, flags));
    459       1.1  augustss 
    460      1.79  riastrad 	/* Notify that we are going away.  */
    461      1.79  riastrad 	mutex_enter(&sc->sc_lock);
    462       1.1  augustss 	sc->sc_dying = 1;
    463      1.79  riastrad 	cv_broadcast(&sc->sc_cv);
    464      1.79  riastrad 	mutex_exit(&sc->sc_lock);
    465       1.1  augustss 
    466      1.79  riastrad 	/*
    467      1.79  riastrad 	 * Try to detach all our children.  If anything fails, bail.
    468      1.79  riastrad 	 * Failure can happen if this is from drvctl -d; of course, if
    469      1.79  riastrad 	 * this is a USB device being yanked, flags will have
    470      1.79  riastrad 	 * DETACH_FORCE and the children will not have the option of
    471      1.79  riastrad 	 * refusing detachment.
    472      1.79  riastrad 	 */
    473       1.1  augustss 	for (i = 0; i < sc->sc_nrepid; i++) {
    474      1.79  riastrad 		if (sc->sc_subdevs[i] == NULL)
    475      1.79  riastrad 			continue;
    476      1.79  riastrad 		/*
    477      1.79  riastrad 		 * XXX rnd_detach_source should go in uhidev_childdet,
    478      1.79  riastrad 		 * but the struct krndsource lives in the child's
    479      1.79  riastrad 		 * softc, which is gone by the time of childdet.  The
    480      1.79  riastrad 		 * parent uhidev_softc should be changed to allocate
    481      1.79  riastrad 		 * the struct krndsource, not the child.
    482      1.79  riastrad 		 */
    483      1.79  riastrad 		csc = device_private(sc->sc_subdevs[i]);
    484      1.79  riastrad 		rnd_detach_source(&csc->rnd_source);
    485      1.79  riastrad 		rv = config_detach(sc->sc_subdevs[i], flags);
    486      1.79  riastrad 		if (rv) {
    487      1.79  riastrad 			rnd_attach_source(&csc->rnd_source,
    488      1.79  riastrad 			    device_xname(sc->sc_dev),
    489      1.79  riastrad 			    RND_TYPE_TTY, RND_FLAG_DEFAULT);
    490      1.79  riastrad 			mutex_enter(&sc->sc_lock);
    491      1.79  riastrad 			sc->sc_dying = 0;
    492      1.79  riastrad 			mutex_exit(&sc->sc_lock);
    493      1.79  riastrad 			return rv;
    494       1.1  augustss 		}
    495       1.1  augustss 	}
    496       1.1  augustss 
    497      1.79  riastrad 	KASSERTMSG(sc->sc_refcnt == 0,
    498      1.79  riastrad 	    "%s: %d refs remain", device_xname(sc->sc_dev), sc->sc_refcnt);
    499      1.79  riastrad 	KASSERT(sc->sc_opipe == NULL);
    500      1.79  riastrad 	KASSERT(sc->sc_ipipe == NULL);
    501      1.79  riastrad 	KASSERT(sc->sc_ibuf == NULL);
    502      1.79  riastrad 
    503      1.79  riastrad 	if (sc->sc_repdesc != NULL) {
    504      1.79  riastrad 		kmem_free(sc->sc_repdesc, sc->sc_repdesc_size);
    505      1.79  riastrad 		sc->sc_repdesc = NULL;
    506      1.79  riastrad 	}
    507      1.79  riastrad 	if (sc->sc_subdevs != NULL) {
    508      1.79  riastrad 		int nrepid = sc->sc_nrepid;
    509      1.79  riastrad 		kmem_free(sc->sc_subdevs, nrepid * sizeof(sc->sc_subdevs[0]));
    510      1.79  riastrad 		sc->sc_subdevs = NULL;
    511      1.79  riastrad 	}
    512      1.79  riastrad 
    513      1.68   msaitoh 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
    514       1.1  augustss 
    515      1.38  jmcneill 	pmf_device_deregister(self);
    516      1.79  riastrad 	KASSERT(sc->sc_configlock == NULL);
    517      1.79  riastrad 	KASSERT(sc->sc_writelock == NULL);
    518      1.79  riastrad 	cv_destroy(&sc->sc_cv);
    519      1.56       mrg 	mutex_destroy(&sc->sc_lock);
    520      1.38  jmcneill 
    521      1.65     skrll 	return rv;
    522       1.1  augustss }
    523       1.1  augustss 
    524       1.1  augustss void
    525      1.65     skrll uhidev_intr(struct usbd_xfer *xfer, void *addr, usbd_status status)
    526       1.1  augustss {
    527       1.1  augustss 	struct uhidev_softc *sc = addr;
    528      1.42  drochner 	device_t cdev;
    529       1.1  augustss 	struct uhidev *scd;
    530       1.1  augustss 	u_char *p;
    531       1.1  augustss 	u_int rep;
    532      1.65     skrll 	uint32_t cc;
    533       1.1  augustss 
    534       1.1  augustss 	usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
    535       1.1  augustss 
    536       1.1  augustss #ifdef UHIDEV_DEBUG
    537       1.1  augustss 	if (uhidevdebug > 5) {
    538      1.65     skrll 		uint32_t i;
    539       1.6  augustss 
    540       1.1  augustss 		DPRINTF(("uhidev_intr: status=%d cc=%d\n", status, cc));
    541       1.1  augustss 		DPRINTF(("uhidev_intr: data ="));
    542       1.1  augustss 		for (i = 0; i < cc; i++)
    543       1.1  augustss 			DPRINTF((" %02x", sc->sc_ibuf[i]));
    544       1.1  augustss 		DPRINTF(("\n"));
    545       1.1  augustss 	}
    546       1.1  augustss #endif
    547       1.1  augustss 
    548       1.1  augustss 	if (status == USBD_CANCELLED)
    549       1.1  augustss 		return;
    550       1.1  augustss 
    551       1.1  augustss 	if (status != USBD_NORMAL_COMPLETION) {
    552      1.48    dyoung 		DPRINTF(("%s: interrupt status=%d\n", device_xname(sc->sc_dev),
    553       1.3  augustss 			 status));
    554      1.25     skrll 		usbd_clear_endpoint_stall_async(sc->sc_ipipe);
    555       1.1  augustss 		return;
    556       1.1  augustss 	}
    557       1.1  augustss 
    558       1.1  augustss 	p = sc->sc_ibuf;
    559       1.1  augustss 	if (sc->sc_nrepid != 1)
    560       1.1  augustss 		rep = *p++, cc--;
    561       1.1  augustss 	else
    562       1.1  augustss 		rep = 0;
    563       1.1  augustss 	if (rep >= sc->sc_nrepid) {
    564       1.1  augustss 		printf("uhidev_intr: bad repid %d\n", rep);
    565       1.1  augustss 		return;
    566       1.1  augustss 	}
    567      1.42  drochner 	cdev = sc->sc_subdevs[rep];
    568      1.42  drochner 	if (!cdev)
    569      1.42  drochner 		return;
    570      1.42  drochner 	scd = device_private(cdev);
    571      1.77  christos 	DPRINTFN(5,("uhidev_intr: rep=%d, scd=%p state=%#x\n",
    572       1.1  augustss 		    rep, scd, scd ? scd->sc_state : 0));
    573      1.42  drochner 	if (!(scd->sc_state & UHIDEV_OPEN))
    574       1.1  augustss 		return;
    575      1.46  jakllsch #ifdef UHIDEV_DEBUG
    576      1.46  jakllsch 	if (scd->sc_in_rep_size != cc) {
    577      1.46  jakllsch 		DPRINTF(("%s: expected %d bytes, got %d\n",
    578      1.48    dyoung 		       device_xname(sc->sc_dev), scd->sc_in_rep_size, cc));
    579      1.46  jakllsch 	}
    580      1.46  jakllsch #endif
    581      1.46  jakllsch 	if (cc == 0) {
    582      1.46  jakllsch 		DPRINTF(("%s: 0-length input ignored\n",
    583      1.48    dyoung 			device_xname(sc->sc_dev)));
    584      1.23  augustss 		return;
    585      1.23  augustss 	}
    586      1.10      fair 	rnd_add_uint32(&scd->rnd_source, (uintptr_t)(sc->sc_ibuf));
    587       1.1  augustss 	scd->sc_intr(scd, p, cc);
    588       1.1  augustss }
    589       1.1  augustss 
    590       1.1  augustss void
    591       1.1  augustss uhidev_get_report_desc(struct uhidev_softc *sc, void **desc, int *size)
    592       1.1  augustss {
    593       1.1  augustss 	*desc = sc->sc_repdesc;
    594       1.1  augustss 	*size = sc->sc_repdesc_size;
    595       1.1  augustss }
    596       1.1  augustss 
    597      1.79  riastrad static int
    598      1.79  riastrad uhidev_config_enter(struct uhidev_softc *sc)
    599      1.79  riastrad {
    600      1.79  riastrad 	int error;
    601      1.79  riastrad 
    602      1.79  riastrad 	KASSERT(mutex_owned(&sc->sc_lock));
    603      1.79  riastrad 
    604      1.79  riastrad 	for (;;) {
    605      1.79  riastrad 		if (sc->sc_dying)
    606      1.79  riastrad 			return ENXIO;
    607      1.79  riastrad 		if (sc->sc_configlock == NULL)
    608      1.79  riastrad 			break;
    609      1.79  riastrad 		error = cv_wait_sig(&sc->sc_cv, &sc->sc_lock);
    610      1.79  riastrad 		if (error)
    611      1.79  riastrad 			return error;
    612      1.79  riastrad 	}
    613      1.79  riastrad 
    614      1.79  riastrad 	sc->sc_configlock = curlwp;
    615      1.79  riastrad 	return 0;
    616      1.79  riastrad }
    617      1.79  riastrad 
    618      1.79  riastrad static void
    619      1.79  riastrad uhidev_config_enter_nointr(struct uhidev_softc *sc)
    620      1.79  riastrad {
    621      1.79  riastrad 
    622      1.79  riastrad 	KASSERT(mutex_owned(&sc->sc_lock));
    623      1.79  riastrad 
    624      1.79  riastrad 	while (sc->sc_configlock)
    625      1.79  riastrad 		cv_wait(&sc->sc_cv, &sc->sc_lock);
    626      1.79  riastrad 	sc->sc_configlock = curlwp;
    627      1.79  riastrad }
    628      1.79  riastrad 
    629      1.79  riastrad static void
    630      1.79  riastrad uhidev_config_exit(struct uhidev_softc *sc)
    631      1.79  riastrad {
    632      1.79  riastrad 
    633      1.79  riastrad 	KASSERT(mutex_owned(&sc->sc_lock));
    634      1.79  riastrad 	KASSERTMSG(sc->sc_configlock == curlwp, "%s: migrated from %p to %p",
    635      1.79  riastrad 	    device_xname(sc->sc_dev), curlwp, sc->sc_configlock);
    636      1.79  riastrad 
    637      1.79  riastrad 	sc->sc_configlock = NULL;
    638      1.79  riastrad 	cv_broadcast(&sc->sc_cv);
    639      1.79  riastrad }
    640      1.79  riastrad 
    641      1.79  riastrad /*
    642      1.79  riastrad  * uhidev_open_pipes(sc)
    643      1.79  riastrad  *
    644      1.79  riastrad  *	Ensure the pipes of the softc are open.  Caller must hold
    645      1.79  riastrad  *	sc_lock, which may be released and reacquired.
    646      1.79  riastrad  */
    647      1.79  riastrad static int
    648      1.79  riastrad uhidev_open_pipes(struct uhidev_softc *sc)
    649       1.1  augustss {
    650       1.1  augustss 	usbd_status err;
    651      1.25     skrll 	int error;
    652       1.1  augustss 
    653      1.79  riastrad 	KASSERT(mutex_owned(&sc->sc_lock));
    654      1.79  riastrad 
    655      1.79  riastrad 	/* If the device is dying, refuse.  */
    656      1.79  riastrad 	if (sc->sc_dying)
    657      1.79  riastrad 		return ENXIO;
    658       1.1  augustss 
    659      1.79  riastrad 	/*
    660      1.79  riastrad 	 * If the pipes are already open, just increment the reference
    661      1.79  riastrad 	 * count, or fail if it would overflow.
    662      1.79  riastrad 	 */
    663      1.79  riastrad 	if (sc->sc_refcnt) {
    664      1.79  riastrad 		if (sc->sc_refcnt == INT_MAX)
    665      1.79  riastrad 			return EBUSY;
    666      1.79  riastrad 		sc->sc_refcnt++;
    667      1.65     skrll 		return 0;
    668      1.60     skrll 	}
    669       1.1  augustss 
    670      1.79  riastrad 	/*
    671      1.79  riastrad 	 * If there's no input data to prepare, don't bother with the
    672      1.79  riastrad 	 * pipes.  We assume any device that does output also does
    673      1.79  riastrad 	 * input; if you have a device where this is wrong, then
    674      1.79  riastrad 	 * uhidev_write will fail gracefully (it checks sc->sc_opipe),
    675      1.79  riastrad 	 * and you can use that device to test the changes needed to
    676      1.79  riastrad 	 * open the output pipe here.
    677      1.79  riastrad 	 */
    678       1.1  augustss 	if (sc->sc_isize == 0)
    679      1.65     skrll 		return 0;
    680       1.1  augustss 
    681      1.79  riastrad 	/*
    682      1.79  riastrad 	 * Lock the configuration and release sc_lock we may sleep to
    683      1.79  riastrad 	 * allocate.  If someone else got in first, we're done;
    684      1.79  riastrad 	 * otherwise open the pipes.
    685      1.79  riastrad 	 */
    686      1.79  riastrad 	error = uhidev_config_enter(sc);
    687      1.79  riastrad 	if (error)
    688      1.79  riastrad 		goto out;
    689      1.79  riastrad 	if (sc->sc_refcnt) {
    690      1.79  riastrad 		if (sc->sc_refcnt == INT_MAX) {
    691      1.79  riastrad 			error = EBUSY;
    692      1.79  riastrad 		} else {
    693      1.79  riastrad 			sc->sc_refcnt++;
    694      1.79  riastrad 			error = 0;
    695      1.79  riastrad 		}
    696      1.79  riastrad 		goto out0;
    697      1.79  riastrad 	}
    698      1.79  riastrad 	mutex_exit(&sc->sc_lock);
    699      1.79  riastrad 
    700      1.79  riastrad 	/* Allocate an input buffer.  */
    701      1.65     skrll 	sc->sc_ibuf = kmem_alloc(sc->sc_isize, KM_SLEEP);
    702       1.1  augustss 
    703      1.25     skrll 	/* Set up input interrupt pipe. */
    704      1.79  riastrad 	DPRINTF(("%s: isize=%d, ep=0x%02x\n", __func__, sc->sc_isize,
    705      1.25     skrll 		 sc->sc_iep_addr));
    706      1.58     skrll 
    707      1.25     skrll 	err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_iep_addr,
    708      1.25     skrll 		  USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_ibuf,
    709       1.1  augustss 		  sc->sc_isize, uhidev_intr, USBD_DEFAULT_INTERVAL);
    710      1.25     skrll 	if (err != USBD_NORMAL_COMPLETION) {
    711       1.1  augustss 		DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
    712      1.25     skrll 		    "error=%d\n", err));
    713      1.25     skrll 		error = EIO;
    714      1.25     skrll 		goto out1;
    715      1.25     skrll 	}
    716      1.25     skrll 
    717      1.25     skrll 	/*
    718      1.25     skrll 	 * Set up output interrupt pipe if an output interrupt endpoint
    719      1.25     skrll 	 * exists.
    720      1.25     skrll 	 */
    721      1.25     skrll 	if (sc->sc_oep_addr != -1) {
    722      1.78  christos 		DPRINTF(("uhidev_open: oep=0x%02x\n", sc->sc_oep_addr));
    723      1.25     skrll 
    724      1.25     skrll 		err = usbd_open_pipe(sc->sc_iface, sc->sc_oep_addr,
    725      1.25     skrll 		    0, &sc->sc_opipe);
    726      1.25     skrll 
    727      1.25     skrll 		if (err != USBD_NORMAL_COMPLETION) {
    728      1.25     skrll 			DPRINTF(("uhidev_open: usbd_open_pipe failed, "
    729      1.25     skrll 			    "error=%d\n", err));
    730      1.25     skrll 			error = EIO;
    731      1.25     skrll 			goto out2;
    732      1.25     skrll 		}
    733      1.25     skrll 		DPRINTF(("uhidev_open: sc->sc_opipe=%p\n", sc->sc_opipe));
    734      1.25     skrll 
    735      1.65     skrll 		error = usbd_create_xfer(sc->sc_opipe, UHIDEV_OSIZE, 0, 0,
    736      1.65     skrll 		    &sc->sc_oxfer);
    737      1.65     skrll 		if (error) {
    738      1.25     skrll 			DPRINTF(("uhidev_open: couldn't allocate an xfer\n"));
    739      1.25     skrll 			goto out3;
    740      1.25     skrll 		}
    741      1.62  jmcneill 
    742      1.62  jmcneill 		if (sc->sc_flags & UHIDEV_F_XB1) {
    743      1.62  jmcneill 			uint8_t init_data[] = { 0x05, 0x20 };
    744      1.62  jmcneill 			int init_data_len = sizeof(init_data);
    745      1.62  jmcneill 			err = usbd_intr_transfer(sc->sc_oxfer, sc->sc_opipe, 0,
    746      1.65     skrll 			    USBD_NO_TIMEOUT, init_data, &init_data_len);
    747      1.62  jmcneill 			if (err != USBD_NORMAL_COMPLETION) {
    748      1.62  jmcneill 				DPRINTF(("uhidev_open: xb1 init failed, "
    749      1.62  jmcneill 				    "error=%d\n", err));
    750      1.62  jmcneill 				error = EIO;
    751      1.62  jmcneill 				goto out4;
    752      1.62  jmcneill 			}
    753      1.62  jmcneill 		}
    754       1.1  augustss 	}
    755      1.58     skrll 
    756      1.79  riastrad 	/* Success!  */
    757      1.79  riastrad 	mutex_enter(&sc->sc_lock);
    758      1.79  riastrad 	KASSERTMSG(sc->sc_refcnt == 0, "%d refs spuriously acquired",
    759      1.79  riastrad 	    sc->sc_refcnt);
    760      1.79  riastrad 	sc->sc_refcnt++;
    761      1.79  riastrad 	goto out0;
    762      1.79  riastrad 
    763      1.79  riastrad out4:	if (sc->sc_oxfer) {
    764      1.79  riastrad 		usbd_abort_pipe(sc->sc_opipe);
    765      1.65     skrll 		usbd_destroy_xfer(sc->sc_oxfer);
    766      1.79  riastrad 		sc->sc_oxfer = NULL;
    767      1.79  riastrad 	}
    768      1.79  riastrad out3:	if (sc->sc_opipe) {
    769      1.79  riastrad 		usbd_close_pipe(sc->sc_opipe);
    770      1.79  riastrad 		sc->sc_opipe = NULL;
    771      1.79  riastrad 	}
    772      1.79  riastrad out2:	if (sc->sc_ipipe) {
    773      1.79  riastrad 		usbd_abort_pipe(sc->sc_ipipe);
    774      1.79  riastrad 		usbd_close_pipe(sc->sc_ipipe);
    775      1.79  riastrad 		sc->sc_ipipe = NULL;
    776      1.79  riastrad 	}
    777      1.79  riastrad out1:	kmem_free(sc->sc_ibuf, sc->sc_isize);
    778      1.79  riastrad 	sc->sc_ibuf = NULL;
    779      1.56       mrg 	mutex_enter(&sc->sc_lock);
    780      1.79  riastrad out0:	KASSERT(mutex_owned(&sc->sc_lock));
    781      1.79  riastrad 	uhidev_config_exit(sc);
    782      1.79  riastrad out:	KASSERT(mutex_owned(&sc->sc_lock));
    783      1.25     skrll 	return error;
    784       1.1  augustss }
    785       1.1  augustss 
    786      1.79  riastrad static void
    787      1.79  riastrad uhidev_close_pipes(struct uhidev_softc *sc)
    788      1.63       mrg {
    789      1.63       mrg 
    790      1.79  riastrad 	KASSERT(mutex_owned(&sc->sc_lock));
    791      1.79  riastrad 	KASSERTMSG(sc->sc_refcnt > 0, "%s: refcnt fouled: %d",
    792      1.79  riastrad 	    device_xname(sc->sc_dev), sc->sc_refcnt);
    793      1.79  riastrad 
    794      1.79  riastrad 	/* If this isn't the last reference, just decrement.  */
    795      1.79  riastrad 	if (sc->sc_refcnt > 1) {
    796      1.79  riastrad 		sc->sc_refcnt--;
    797      1.79  riastrad 		return;
    798      1.79  riastrad 	}
    799      1.79  riastrad 
    800      1.79  riastrad 	/*
    801      1.79  riastrad 	 * Lock the configuration and release sc_lock so we may sleep
    802      1.79  riastrad 	 * to free memory.  We're not waiting for anyone to allocate or
    803      1.79  riastrad 	 * free anything.
    804      1.79  riastrad 	 */
    805      1.79  riastrad 	uhidev_config_enter_nointr(sc);
    806      1.79  riastrad 
    807      1.79  riastrad 	/*
    808      1.79  riastrad 	 * If someone else acquired a reference while we were waiting
    809      1.79  riastrad 	 * for the config lock, nothing more for us to do.
    810      1.79  riastrad 	 */
    811      1.79  riastrad 	if (sc->sc_refcnt > 1) {
    812      1.79  riastrad 		sc->sc_refcnt--;
    813      1.79  riastrad 		uhidev_config_exit(sc);
    814      1.79  riastrad 		return;
    815      1.79  riastrad 	}
    816      1.79  riastrad 
    817      1.79  riastrad 	/*
    818      1.79  riastrad 	 * We're the last reference and committed to closing the pipes.
    819      1.79  riastrad 	 * Decrement the reference count before we release the lock --
    820      1.79  riastrad 	 * access to the pipes is allowed as long as the reference
    821      1.79  riastrad 	 * count is positive, so this forces all new opens to wait
    822      1.79  riastrad 	 * until the config lock is released.
    823      1.79  riastrad 	 */
    824      1.79  riastrad 	KASSERTMSG(sc->sc_refcnt == 1, "%s: refcnt fouled: %d",
    825      1.79  riastrad 	    device_xname(sc->sc_dev), sc->sc_refcnt);
    826      1.79  riastrad 	sc->sc_refcnt--;
    827      1.79  riastrad 	mutex_exit(&sc->sc_lock);
    828      1.79  riastrad 
    829      1.79  riastrad 	if (sc->sc_oxfer) {
    830      1.63       mrg 		usbd_abort_pipe(sc->sc_opipe);
    831      1.79  riastrad 		usbd_destroy_xfer(sc->sc_oxfer);
    832      1.79  riastrad 		sc->sc_oxfer = NULL;
    833      1.79  riastrad 	}
    834      1.79  riastrad 	if (sc->sc_opipe) {
    835      1.63       mrg 		usbd_close_pipe(sc->sc_opipe);
    836      1.63       mrg 		sc->sc_opipe = NULL;
    837      1.63       mrg 	}
    838      1.79  riastrad 	if (sc->sc_ipipe) {
    839      1.63       mrg 		usbd_abort_pipe(sc->sc_ipipe);
    840      1.63       mrg 		usbd_close_pipe(sc->sc_ipipe);
    841      1.63       mrg 		sc->sc_ipipe = NULL;
    842      1.63       mrg 	}
    843      1.79  riastrad 	kmem_free(sc->sc_ibuf, sc->sc_isize);
    844      1.79  riastrad 	sc->sc_ibuf = NULL;
    845      1.79  riastrad 
    846      1.79  riastrad 	mutex_enter(&sc->sc_lock);
    847      1.79  riastrad 	uhidev_config_exit(sc);
    848      1.79  riastrad 	KASSERTMSG(sc->sc_refcnt == 0, "%s: refcnt fouled: %d",
    849      1.79  riastrad 	    device_xname(sc->sc_dev), sc->sc_refcnt);
    850      1.79  riastrad }
    851      1.79  riastrad 
    852      1.79  riastrad int
    853      1.79  riastrad uhidev_open(struct uhidev *scd)
    854      1.79  riastrad {
    855      1.79  riastrad 	struct uhidev_softc *sc = scd->sc_parent;
    856      1.79  riastrad 	int error;
    857      1.79  riastrad 
    858      1.79  riastrad 	mutex_enter(&sc->sc_lock);
    859      1.79  riastrad 
    860      1.79  riastrad 	DPRINTF(("uhidev_open(%s, report %d = %s): state=%x refcnt=%d\n",
    861      1.79  riastrad 		device_xname(sc->sc_dev),
    862      1.79  riastrad 		scd->sc_report_id,
    863      1.79  riastrad 		device_xname(scd->sc_dev),
    864      1.79  riastrad 		scd->sc_state,
    865      1.79  riastrad 		sc->sc_refcnt));
    866      1.63       mrg 
    867      1.79  riastrad 	/* Mark the report id open.  This is an exclusive lock.  */
    868      1.79  riastrad 	if (scd->sc_state & UHIDEV_OPEN) {
    869      1.79  riastrad 		error = EBUSY;
    870      1.79  riastrad 		goto fail0;
    871      1.63       mrg 	}
    872      1.79  riastrad 	scd->sc_state |= UHIDEV_OPEN;
    873      1.79  riastrad 
    874      1.79  riastrad 	/* Open the pipes which are shared by all report ids.  */
    875      1.79  riastrad 	error = uhidev_open_pipes(sc);
    876      1.79  riastrad 	if (error)
    877      1.79  riastrad 		goto fail1;
    878      1.79  riastrad 
    879      1.79  riastrad 	mutex_exit(&sc->sc_lock);
    880      1.79  riastrad 
    881      1.79  riastrad 	/* Success!  */
    882      1.79  riastrad 	return 0;
    883      1.79  riastrad 
    884      1.79  riastrad fail2: __unused
    885      1.79  riastrad 	uhidev_close_pipes(sc);
    886      1.79  riastrad fail1:	KASSERTMSG(scd->sc_state & UHIDEV_OPEN,
    887      1.79  riastrad 	    "%s: report id %d: closed while opening",
    888      1.79  riastrad 	    device_xname(sc->sc_dev), scd->sc_report_id);
    889      1.79  riastrad 	scd->sc_state &= ~UHIDEV_OPEN;
    890      1.79  riastrad fail0:	mutex_exit(&sc->sc_lock);
    891      1.79  riastrad 	return error;
    892      1.63       mrg }
    893      1.63       mrg 
    894      1.63       mrg void
    895      1.79  riastrad uhidev_stop(struct uhidev *scd)
    896       1.1  augustss {
    897       1.1  augustss 	struct uhidev_softc *sc = scd->sc_parent;
    898      1.79  riastrad 	bool abort = false;
    899       1.1  augustss 
    900      1.56       mrg 	mutex_enter(&sc->sc_lock);
    901      1.79  riastrad 	if (sc->sc_writereportid == scd->sc_report_id)
    902      1.79  riastrad 		abort = true;
    903      1.56       mrg 	mutex_exit(&sc->sc_lock);
    904      1.56       mrg 
    905      1.79  riastrad 	if (abort && sc->sc_opipe)
    906      1.79  riastrad 		usbd_abort_pipe(sc->sc_opipe);
    907      1.79  riastrad }
    908      1.79  riastrad 
    909      1.79  riastrad void
    910      1.79  riastrad uhidev_close(struct uhidev *scd)
    911      1.79  riastrad {
    912      1.79  riastrad 	struct uhidev_softc *sc = scd->sc_parent;
    913       1.1  augustss 
    914      1.79  riastrad 	mutex_enter(&sc->sc_lock);
    915      1.58     skrll 
    916      1.79  riastrad 	DPRINTF(("uhidev_close(%s, report %d = %s): state=%x refcnt=%d\n",
    917      1.79  riastrad 		device_xname(sc->sc_dev),
    918      1.79  riastrad 		scd->sc_report_id,
    919      1.79  riastrad 		device_xname(scd->sc_dev),
    920      1.79  riastrad 		scd->sc_state,
    921      1.79  riastrad 		sc->sc_refcnt));
    922      1.79  riastrad 
    923      1.79  riastrad 	KASSERTMSG(scd->sc_state & UHIDEV_OPEN,
    924      1.79  riastrad 	    "%s: report id %d: unpaired close",
    925      1.79  riastrad 	    device_xname(sc->sc_dev), scd->sc_report_id);
    926      1.79  riastrad 	uhidev_close_pipes(sc);
    927      1.79  riastrad 	KASSERTMSG(scd->sc_state & UHIDEV_OPEN,
    928      1.79  riastrad 	    "%s: report id %d: closed while closing",
    929      1.79  riastrad 	    device_xname(sc->sc_dev), scd->sc_report_id);
    930      1.79  riastrad 	scd->sc_state &= ~UHIDEV_OPEN;
    931      1.65     skrll 
    932      1.79  riastrad 	mutex_exit(&sc->sc_lock);
    933       1.1  augustss }
    934       1.1  augustss 
    935       1.1  augustss usbd_status
    936       1.1  augustss uhidev_set_report(struct uhidev *scd, int type, void *data, int len)
    937       1.1  augustss {
    938      1.13   dsainty 	char *buf;
    939      1.13   dsainty 	usbd_status retstat;
    940      1.13   dsainty 
    941      1.13   dsainty 	if (scd->sc_report_id == 0)
    942      1.13   dsainty 		return usbd_set_report(scd->sc_parent->sc_iface, type,
    943      1.13   dsainty 				       scd->sc_report_id, data, len);
    944      1.13   dsainty 
    945      1.65     skrll 	buf = kmem_alloc(len + 1, KM_SLEEP);
    946      1.13   dsainty 	buf[0] = scd->sc_report_id;
    947      1.13   dsainty 	memcpy(buf+1, data, len);
    948      1.13   dsainty 
    949      1.13   dsainty 	retstat = usbd_set_report(scd->sc_parent->sc_iface, type,
    950      1.18   dsainty 				  scd->sc_report_id, buf, len + 1);
    951      1.13   dsainty 
    952      1.65     skrll 	kmem_free(buf, len + 1);
    953       1.1  augustss 
    954      1.13   dsainty 	return retstat;
    955       1.1  augustss }
    956       1.1  augustss 
    957       1.1  augustss usbd_status
    958       1.1  augustss uhidev_get_report(struct uhidev *scd, int type, void *data, int len)
    959       1.1  augustss {
    960       1.6  augustss 	return usbd_get_report(scd->sc_parent->sc_iface, type,
    961       1.1  augustss 			       scd->sc_report_id, data, len);
    962       1.1  augustss }
    963      1.25     skrll 
    964      1.25     skrll usbd_status
    965      1.25     skrll uhidev_write(struct uhidev_softc *sc, void *data, int len)
    966      1.25     skrll {
    967      1.79  riastrad 	usbd_status err;
    968      1.25     skrll 
    969      1.25     skrll 	DPRINTF(("uhidev_write: data=%p, len=%d\n", data, len));
    970      1.25     skrll 
    971      1.25     skrll 	if (sc->sc_opipe == NULL)
    972      1.25     skrll 		return USBD_INVAL;
    973      1.25     skrll 
    974      1.79  riastrad 	mutex_enter(&sc->sc_lock);
    975      1.79  riastrad 	KASSERT(sc->sc_refcnt);
    976      1.79  riastrad 	for (;;) {
    977      1.79  riastrad 		if (sc->sc_dying) {
    978      1.79  riastrad 			err = USBD_IOERROR;
    979      1.79  riastrad 			goto out;
    980      1.79  riastrad 		}
    981      1.79  riastrad 		if (sc->sc_writelock == NULL)
    982      1.79  riastrad 			break;
    983      1.79  riastrad 		if (cv_wait_sig(&sc->sc_cv, &sc->sc_lock)) {
    984      1.79  riastrad 			err = USBD_INTERRUPTED;
    985      1.79  riastrad 			goto out;
    986      1.79  riastrad 		}
    987      1.79  riastrad 	}
    988      1.79  riastrad 	sc->sc_writelock = curlwp;
    989      1.79  riastrad 	mutex_exit(&sc->sc_lock);
    990      1.79  riastrad 
    991      1.25     skrll #ifdef UHIDEV_DEBUG
    992      1.25     skrll 	if (uhidevdebug > 50) {
    993      1.25     skrll 
    994      1.65     skrll 		uint32_t i;
    995      1.65     skrll 		uint8_t *d = data;
    996      1.25     skrll 
    997      1.25     skrll 		DPRINTF(("uhidev_write: data ="));
    998      1.25     skrll 		for (i = 0; i < len; i++)
    999      1.25     skrll 			DPRINTF((" %02x", d[i]));
   1000      1.25     skrll 		DPRINTF(("\n"));
   1001      1.25     skrll 	}
   1002      1.25     skrll #endif
   1003      1.79  riastrad 	err = usbd_intr_transfer(sc->sc_oxfer, sc->sc_opipe, 0,
   1004      1.79  riastrad 	    USBD_NO_TIMEOUT, data, &len);
   1005      1.79  riastrad 
   1006      1.79  riastrad 	mutex_enter(&sc->sc_lock);
   1007      1.79  riastrad 	KASSERT(sc->sc_refcnt);
   1008      1.79  riastrad 	KASSERTMSG(sc->sc_writelock == curlwp, "%s: migrated from %p to %p",
   1009      1.79  riastrad 	    device_xname(sc->sc_dev), curlwp, sc->sc_writelock);
   1010      1.79  riastrad 	sc->sc_writelock = NULL;
   1011      1.79  riastrad 	cv_broadcast(&sc->sc_cv);
   1012      1.79  riastrad out:	mutex_exit(&sc->sc_lock);
   1013      1.79  riastrad 	return err;
   1014      1.25     skrll }
   1015