Home | History | Annotate | Line # | Download | only in usb
uhidev.c revision 1.79.2.1
      1  1.79.2.1   thorpej /*	$NetBSD: uhidev.c,v 1.79.2.1 2021/03/22 02:01:02 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.1   thorpej __KERNEL_RCSID(0, "$NetBSD: uhidev.c,v 1.79.2.1 2021/03/22 02:01:02 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_IATTR, "uhidbus",
    367  1.79.2.1   thorpej 					   CFARG_LOCATORS, locs,
    368  1.79.2.1   thorpej 					   CFARG_EOL);
    369       1.1  augustss 			sc->sc_subdevs[repid] = dev;
    370       1.4  augustss 			if (dev != NULL) {
    371      1.42  drochner 				csc = device_private(dev);
    372      1.42  drochner 				csc->sc_in_rep_size = repsizes[repid];
    373       1.1  augustss #ifdef DIAGNOSTIC
    374       1.1  augustss 				DPRINTF(("uhidev_match: repid=%d dev=%p\n",
    375       1.1  augustss 					 repid, dev));
    376      1.42  drochner 				if (csc->sc_intr == NULL) {
    377      1.65     skrll 					kmem_free(repsizes,
    378      1.65     skrll 					    nrepid * sizeof(*repsizes));
    379      1.41      cube 					aprint_error_dev(self,
    380      1.41      cube 					    "sc_intr == NULL\n");
    381      1.48    dyoung 					return;
    382       1.1  augustss 				}
    383       1.4  augustss #endif
    384      1.42  drochner 				rnd_attach_source(&csc->rnd_source,
    385      1.48    dyoung 						  device_xname(dev),
    386      1.61       tls 						  RND_TYPE_TTY,
    387      1.61       tls 						  RND_FLAG_DEFAULT);
    388       1.1  augustss 			}
    389       1.1  augustss 		}
    390       1.1  augustss 	}
    391      1.65     skrll 	kmem_free(repsizes, nrepid * sizeof(*repsizes));
    392       1.1  augustss 
    393      1.48    dyoung 	return;
    394       1.1  augustss }
    395       1.1  augustss 
    396       1.1  augustss int
    397       1.1  augustss uhidev_maxrepid(void *buf, int len)
    398       1.1  augustss {
    399       1.1  augustss 	struct hid_data *d;
    400       1.1  augustss 	struct hid_item h;
    401       1.1  augustss 	int maxid;
    402       1.1  augustss 
    403       1.1  augustss 	maxid = -1;
    404       1.1  augustss 	h.report_ID = 0;
    405       1.1  augustss 	for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); )
    406      1.74  jakllsch 		if ((int)h.report_ID > maxid)
    407       1.1  augustss 			maxid = h.report_ID;
    408       1.1  augustss 	hid_end_parse(d);
    409      1.65     skrll 	return maxid;
    410       1.1  augustss }
    411       1.1  augustss 
    412       1.1  augustss int
    413       1.1  augustss uhidevprint(void *aux, const char *pnp)
    414       1.1  augustss {
    415       1.1  augustss 	struct uhidev_attach_arg *uha = aux;
    416       1.1  augustss 
    417       1.1  augustss 	if (pnp)
    418      1.12   thorpej 		aprint_normal("uhid at %s", pnp);
    419       1.1  augustss 	if (uha->reportid != 0)
    420      1.12   thorpej 		aprint_normal(" reportid %d", uha->reportid);
    421      1.65     skrll 	return UNCONF;
    422       1.1  augustss }
    423       1.1  augustss 
    424      1.76      maxv static int
    425      1.39    dyoung uhidev_activate(device_t self, enum devact act)
    426       1.1  augustss {
    427      1.39    dyoung 	struct uhidev_softc *sc = device_private(self);
    428       1.1  augustss 
    429       1.1  augustss 	switch (act) {
    430       1.1  augustss 	case DVACT_DEACTIVATE:
    431       1.1  augustss 		sc->sc_dying = 1;
    432      1.45    dyoung 		return 0;
    433      1.16  christos 	default:
    434      1.45    dyoung 		return EOPNOTSUPP;
    435       1.1  augustss 	}
    436       1.1  augustss }
    437       1.1  augustss 
    438      1.76      maxv static void
    439      1.39    dyoung uhidev_childdet(device_t self, device_t child)
    440      1.39    dyoung {
    441      1.39    dyoung 	int i;
    442      1.39    dyoung 	struct uhidev_softc *sc = device_private(self);
    443      1.39    dyoung 
    444      1.39    dyoung 	for (i = 0; i < sc->sc_nrepid; i++) {
    445      1.42  drochner 		if (sc->sc_subdevs[i] == child)
    446      1.39    dyoung 			break;
    447      1.39    dyoung 	}
    448      1.39    dyoung 	KASSERT(i < sc->sc_nrepid);
    449      1.39    dyoung 	sc->sc_subdevs[i] = NULL;
    450      1.39    dyoung }
    451      1.39    dyoung 
    452      1.76      maxv static int
    453      1.48    dyoung uhidev_detach(device_t self, int flags)
    454       1.1  augustss {
    455      1.48    dyoung 	struct uhidev_softc *sc = device_private(self);
    456       1.1  augustss 	int i, rv;
    457      1.42  drochner 	struct uhidev *csc;
    458       1.1  augustss 
    459       1.1  augustss 	DPRINTF(("uhidev_detach: sc=%p flags=%d\n", sc, flags));
    460       1.1  augustss 
    461      1.79  riastrad 	/* Notify that we are going away.  */
    462      1.79  riastrad 	mutex_enter(&sc->sc_lock);
    463       1.1  augustss 	sc->sc_dying = 1;
    464      1.79  riastrad 	cv_broadcast(&sc->sc_cv);
    465      1.79  riastrad 	mutex_exit(&sc->sc_lock);
    466       1.1  augustss 
    467      1.79  riastrad 	/*
    468      1.79  riastrad 	 * Try to detach all our children.  If anything fails, bail.
    469      1.79  riastrad 	 * Failure can happen if this is from drvctl -d; of course, if
    470      1.79  riastrad 	 * this is a USB device being yanked, flags will have
    471      1.79  riastrad 	 * DETACH_FORCE and the children will not have the option of
    472      1.79  riastrad 	 * refusing detachment.
    473      1.79  riastrad 	 */
    474       1.1  augustss 	for (i = 0; i < sc->sc_nrepid; i++) {
    475      1.79  riastrad 		if (sc->sc_subdevs[i] == NULL)
    476      1.79  riastrad 			continue;
    477      1.79  riastrad 		/*
    478      1.79  riastrad 		 * XXX rnd_detach_source should go in uhidev_childdet,
    479      1.79  riastrad 		 * but the struct krndsource lives in the child's
    480      1.79  riastrad 		 * softc, which is gone by the time of childdet.  The
    481      1.79  riastrad 		 * parent uhidev_softc should be changed to allocate
    482      1.79  riastrad 		 * the struct krndsource, not the child.
    483      1.79  riastrad 		 */
    484      1.79  riastrad 		csc = device_private(sc->sc_subdevs[i]);
    485      1.79  riastrad 		rnd_detach_source(&csc->rnd_source);
    486      1.79  riastrad 		rv = config_detach(sc->sc_subdevs[i], flags);
    487      1.79  riastrad 		if (rv) {
    488      1.79  riastrad 			rnd_attach_source(&csc->rnd_source,
    489      1.79  riastrad 			    device_xname(sc->sc_dev),
    490      1.79  riastrad 			    RND_TYPE_TTY, RND_FLAG_DEFAULT);
    491      1.79  riastrad 			mutex_enter(&sc->sc_lock);
    492      1.79  riastrad 			sc->sc_dying = 0;
    493      1.79  riastrad 			mutex_exit(&sc->sc_lock);
    494      1.79  riastrad 			return rv;
    495       1.1  augustss 		}
    496       1.1  augustss 	}
    497       1.1  augustss 
    498      1.79  riastrad 	KASSERTMSG(sc->sc_refcnt == 0,
    499      1.79  riastrad 	    "%s: %d refs remain", device_xname(sc->sc_dev), sc->sc_refcnt);
    500      1.79  riastrad 	KASSERT(sc->sc_opipe == NULL);
    501      1.79  riastrad 	KASSERT(sc->sc_ipipe == NULL);
    502      1.79  riastrad 	KASSERT(sc->sc_ibuf == NULL);
    503      1.79  riastrad 
    504      1.79  riastrad 	if (sc->sc_repdesc != NULL) {
    505      1.79  riastrad 		kmem_free(sc->sc_repdesc, sc->sc_repdesc_size);
    506      1.79  riastrad 		sc->sc_repdesc = NULL;
    507      1.79  riastrad 	}
    508      1.79  riastrad 	if (sc->sc_subdevs != NULL) {
    509      1.79  riastrad 		int nrepid = sc->sc_nrepid;
    510      1.79  riastrad 		kmem_free(sc->sc_subdevs, nrepid * sizeof(sc->sc_subdevs[0]));
    511      1.79  riastrad 		sc->sc_subdevs = NULL;
    512      1.79  riastrad 	}
    513      1.79  riastrad 
    514      1.68   msaitoh 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
    515       1.1  augustss 
    516      1.38  jmcneill 	pmf_device_deregister(self);
    517      1.79  riastrad 	KASSERT(sc->sc_configlock == NULL);
    518      1.79  riastrad 	KASSERT(sc->sc_writelock == NULL);
    519      1.79  riastrad 	cv_destroy(&sc->sc_cv);
    520      1.56       mrg 	mutex_destroy(&sc->sc_lock);
    521      1.38  jmcneill 
    522      1.65     skrll 	return rv;
    523       1.1  augustss }
    524       1.1  augustss 
    525       1.1  augustss void
    526      1.65     skrll uhidev_intr(struct usbd_xfer *xfer, void *addr, usbd_status status)
    527       1.1  augustss {
    528       1.1  augustss 	struct uhidev_softc *sc = addr;
    529      1.42  drochner 	device_t cdev;
    530       1.1  augustss 	struct uhidev *scd;
    531       1.1  augustss 	u_char *p;
    532       1.1  augustss 	u_int rep;
    533      1.65     skrll 	uint32_t cc;
    534       1.1  augustss 
    535       1.1  augustss 	usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
    536       1.1  augustss 
    537       1.1  augustss #ifdef UHIDEV_DEBUG
    538       1.1  augustss 	if (uhidevdebug > 5) {
    539      1.65     skrll 		uint32_t i;
    540       1.6  augustss 
    541       1.1  augustss 		DPRINTF(("uhidev_intr: status=%d cc=%d\n", status, cc));
    542       1.1  augustss 		DPRINTF(("uhidev_intr: data ="));
    543       1.1  augustss 		for (i = 0; i < cc; i++)
    544       1.1  augustss 			DPRINTF((" %02x", sc->sc_ibuf[i]));
    545       1.1  augustss 		DPRINTF(("\n"));
    546       1.1  augustss 	}
    547       1.1  augustss #endif
    548       1.1  augustss 
    549       1.1  augustss 	if (status == USBD_CANCELLED)
    550       1.1  augustss 		return;
    551       1.1  augustss 
    552       1.1  augustss 	if (status != USBD_NORMAL_COMPLETION) {
    553      1.48    dyoung 		DPRINTF(("%s: interrupt status=%d\n", device_xname(sc->sc_dev),
    554       1.3  augustss 			 status));
    555      1.25     skrll 		usbd_clear_endpoint_stall_async(sc->sc_ipipe);
    556       1.1  augustss 		return;
    557       1.1  augustss 	}
    558       1.1  augustss 
    559       1.1  augustss 	p = sc->sc_ibuf;
    560       1.1  augustss 	if (sc->sc_nrepid != 1)
    561       1.1  augustss 		rep = *p++, cc--;
    562       1.1  augustss 	else
    563       1.1  augustss 		rep = 0;
    564       1.1  augustss 	if (rep >= sc->sc_nrepid) {
    565       1.1  augustss 		printf("uhidev_intr: bad repid %d\n", rep);
    566       1.1  augustss 		return;
    567       1.1  augustss 	}
    568      1.42  drochner 	cdev = sc->sc_subdevs[rep];
    569      1.42  drochner 	if (!cdev)
    570      1.42  drochner 		return;
    571      1.42  drochner 	scd = device_private(cdev);
    572      1.77  christos 	DPRINTFN(5,("uhidev_intr: rep=%d, scd=%p state=%#x\n",
    573       1.1  augustss 		    rep, scd, scd ? scd->sc_state : 0));
    574      1.42  drochner 	if (!(scd->sc_state & UHIDEV_OPEN))
    575       1.1  augustss 		return;
    576      1.46  jakllsch #ifdef UHIDEV_DEBUG
    577      1.46  jakllsch 	if (scd->sc_in_rep_size != cc) {
    578      1.46  jakllsch 		DPRINTF(("%s: expected %d bytes, got %d\n",
    579      1.48    dyoung 		       device_xname(sc->sc_dev), scd->sc_in_rep_size, cc));
    580      1.46  jakllsch 	}
    581      1.46  jakllsch #endif
    582      1.46  jakllsch 	if (cc == 0) {
    583      1.46  jakllsch 		DPRINTF(("%s: 0-length input ignored\n",
    584      1.48    dyoung 			device_xname(sc->sc_dev)));
    585      1.23  augustss 		return;
    586      1.23  augustss 	}
    587      1.10      fair 	rnd_add_uint32(&scd->rnd_source, (uintptr_t)(sc->sc_ibuf));
    588       1.1  augustss 	scd->sc_intr(scd, p, cc);
    589       1.1  augustss }
    590       1.1  augustss 
    591       1.1  augustss void
    592       1.1  augustss uhidev_get_report_desc(struct uhidev_softc *sc, void **desc, int *size)
    593       1.1  augustss {
    594       1.1  augustss 	*desc = sc->sc_repdesc;
    595       1.1  augustss 	*size = sc->sc_repdesc_size;
    596       1.1  augustss }
    597       1.1  augustss 
    598      1.79  riastrad static int
    599      1.79  riastrad uhidev_config_enter(struct uhidev_softc *sc)
    600      1.79  riastrad {
    601      1.79  riastrad 	int error;
    602      1.79  riastrad 
    603      1.79  riastrad 	KASSERT(mutex_owned(&sc->sc_lock));
    604      1.79  riastrad 
    605      1.79  riastrad 	for (;;) {
    606      1.79  riastrad 		if (sc->sc_dying)
    607      1.79  riastrad 			return ENXIO;
    608      1.79  riastrad 		if (sc->sc_configlock == NULL)
    609      1.79  riastrad 			break;
    610      1.79  riastrad 		error = cv_wait_sig(&sc->sc_cv, &sc->sc_lock);
    611      1.79  riastrad 		if (error)
    612      1.79  riastrad 			return error;
    613      1.79  riastrad 	}
    614      1.79  riastrad 
    615      1.79  riastrad 	sc->sc_configlock = curlwp;
    616      1.79  riastrad 	return 0;
    617      1.79  riastrad }
    618      1.79  riastrad 
    619      1.79  riastrad static void
    620      1.79  riastrad uhidev_config_enter_nointr(struct uhidev_softc *sc)
    621      1.79  riastrad {
    622      1.79  riastrad 
    623      1.79  riastrad 	KASSERT(mutex_owned(&sc->sc_lock));
    624      1.79  riastrad 
    625      1.79  riastrad 	while (sc->sc_configlock)
    626      1.79  riastrad 		cv_wait(&sc->sc_cv, &sc->sc_lock);
    627      1.79  riastrad 	sc->sc_configlock = curlwp;
    628      1.79  riastrad }
    629      1.79  riastrad 
    630      1.79  riastrad static void
    631      1.79  riastrad uhidev_config_exit(struct uhidev_softc *sc)
    632      1.79  riastrad {
    633      1.79  riastrad 
    634      1.79  riastrad 	KASSERT(mutex_owned(&sc->sc_lock));
    635      1.79  riastrad 	KASSERTMSG(sc->sc_configlock == curlwp, "%s: migrated from %p to %p",
    636      1.79  riastrad 	    device_xname(sc->sc_dev), curlwp, sc->sc_configlock);
    637      1.79  riastrad 
    638      1.79  riastrad 	sc->sc_configlock = NULL;
    639      1.79  riastrad 	cv_broadcast(&sc->sc_cv);
    640      1.79  riastrad }
    641      1.79  riastrad 
    642      1.79  riastrad /*
    643      1.79  riastrad  * uhidev_open_pipes(sc)
    644      1.79  riastrad  *
    645      1.79  riastrad  *	Ensure the pipes of the softc are open.  Caller must hold
    646      1.79  riastrad  *	sc_lock, which may be released and reacquired.
    647      1.79  riastrad  */
    648      1.79  riastrad static int
    649      1.79  riastrad uhidev_open_pipes(struct uhidev_softc *sc)
    650       1.1  augustss {
    651       1.1  augustss 	usbd_status err;
    652      1.25     skrll 	int error;
    653       1.1  augustss 
    654      1.79  riastrad 	KASSERT(mutex_owned(&sc->sc_lock));
    655      1.79  riastrad 
    656      1.79  riastrad 	/* If the device is dying, refuse.  */
    657      1.79  riastrad 	if (sc->sc_dying)
    658      1.79  riastrad 		return ENXIO;
    659       1.1  augustss 
    660      1.79  riastrad 	/*
    661      1.79  riastrad 	 * If the pipes are already open, just increment the reference
    662      1.79  riastrad 	 * count, or fail if it would overflow.
    663      1.79  riastrad 	 */
    664      1.79  riastrad 	if (sc->sc_refcnt) {
    665      1.79  riastrad 		if (sc->sc_refcnt == INT_MAX)
    666      1.79  riastrad 			return EBUSY;
    667      1.79  riastrad 		sc->sc_refcnt++;
    668      1.65     skrll 		return 0;
    669      1.60     skrll 	}
    670       1.1  augustss 
    671      1.79  riastrad 	/*
    672      1.79  riastrad 	 * If there's no input data to prepare, don't bother with the
    673      1.79  riastrad 	 * pipes.  We assume any device that does output also does
    674      1.79  riastrad 	 * input; if you have a device where this is wrong, then
    675      1.79  riastrad 	 * uhidev_write will fail gracefully (it checks sc->sc_opipe),
    676      1.79  riastrad 	 * and you can use that device to test the changes needed to
    677      1.79  riastrad 	 * open the output pipe here.
    678      1.79  riastrad 	 */
    679       1.1  augustss 	if (sc->sc_isize == 0)
    680      1.65     skrll 		return 0;
    681       1.1  augustss 
    682      1.79  riastrad 	/*
    683      1.79  riastrad 	 * Lock the configuration and release sc_lock we may sleep to
    684      1.79  riastrad 	 * allocate.  If someone else got in first, we're done;
    685      1.79  riastrad 	 * otherwise open the pipes.
    686      1.79  riastrad 	 */
    687      1.79  riastrad 	error = uhidev_config_enter(sc);
    688      1.79  riastrad 	if (error)
    689      1.79  riastrad 		goto out;
    690      1.79  riastrad 	if (sc->sc_refcnt) {
    691      1.79  riastrad 		if (sc->sc_refcnt == INT_MAX) {
    692      1.79  riastrad 			error = EBUSY;
    693      1.79  riastrad 		} else {
    694      1.79  riastrad 			sc->sc_refcnt++;
    695      1.79  riastrad 			error = 0;
    696      1.79  riastrad 		}
    697      1.79  riastrad 		goto out0;
    698      1.79  riastrad 	}
    699      1.79  riastrad 	mutex_exit(&sc->sc_lock);
    700      1.79  riastrad 
    701      1.79  riastrad 	/* Allocate an input buffer.  */
    702      1.65     skrll 	sc->sc_ibuf = kmem_alloc(sc->sc_isize, KM_SLEEP);
    703       1.1  augustss 
    704      1.25     skrll 	/* Set up input interrupt pipe. */
    705      1.79  riastrad 	DPRINTF(("%s: isize=%d, ep=0x%02x\n", __func__, sc->sc_isize,
    706      1.25     skrll 		 sc->sc_iep_addr));
    707      1.58     skrll 
    708      1.25     skrll 	err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_iep_addr,
    709      1.25     skrll 		  USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_ibuf,
    710       1.1  augustss 		  sc->sc_isize, uhidev_intr, USBD_DEFAULT_INTERVAL);
    711      1.25     skrll 	if (err != USBD_NORMAL_COMPLETION) {
    712       1.1  augustss 		DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
    713      1.25     skrll 		    "error=%d\n", err));
    714      1.25     skrll 		error = EIO;
    715      1.25     skrll 		goto out1;
    716      1.25     skrll 	}
    717      1.25     skrll 
    718      1.25     skrll 	/*
    719      1.25     skrll 	 * Set up output interrupt pipe if an output interrupt endpoint
    720      1.25     skrll 	 * exists.
    721      1.25     skrll 	 */
    722      1.25     skrll 	if (sc->sc_oep_addr != -1) {
    723      1.78  christos 		DPRINTF(("uhidev_open: oep=0x%02x\n", sc->sc_oep_addr));
    724      1.25     skrll 
    725      1.25     skrll 		err = usbd_open_pipe(sc->sc_iface, sc->sc_oep_addr,
    726      1.25     skrll 		    0, &sc->sc_opipe);
    727      1.25     skrll 
    728      1.25     skrll 		if (err != USBD_NORMAL_COMPLETION) {
    729      1.25     skrll 			DPRINTF(("uhidev_open: usbd_open_pipe failed, "
    730      1.25     skrll 			    "error=%d\n", err));
    731      1.25     skrll 			error = EIO;
    732      1.25     skrll 			goto out2;
    733      1.25     skrll 		}
    734      1.25     skrll 		DPRINTF(("uhidev_open: sc->sc_opipe=%p\n", sc->sc_opipe));
    735      1.25     skrll 
    736      1.65     skrll 		error = usbd_create_xfer(sc->sc_opipe, UHIDEV_OSIZE, 0, 0,
    737      1.65     skrll 		    &sc->sc_oxfer);
    738      1.65     skrll 		if (error) {
    739      1.25     skrll 			DPRINTF(("uhidev_open: couldn't allocate an xfer\n"));
    740      1.25     skrll 			goto out3;
    741      1.25     skrll 		}
    742      1.62  jmcneill 
    743      1.62  jmcneill 		if (sc->sc_flags & UHIDEV_F_XB1) {
    744      1.62  jmcneill 			uint8_t init_data[] = { 0x05, 0x20 };
    745      1.62  jmcneill 			int init_data_len = sizeof(init_data);
    746      1.62  jmcneill 			err = usbd_intr_transfer(sc->sc_oxfer, sc->sc_opipe, 0,
    747      1.65     skrll 			    USBD_NO_TIMEOUT, init_data, &init_data_len);
    748      1.62  jmcneill 			if (err != USBD_NORMAL_COMPLETION) {
    749      1.62  jmcneill 				DPRINTF(("uhidev_open: xb1 init failed, "
    750      1.62  jmcneill 				    "error=%d\n", err));
    751      1.62  jmcneill 				error = EIO;
    752      1.62  jmcneill 				goto out4;
    753      1.62  jmcneill 			}
    754      1.62  jmcneill 		}
    755       1.1  augustss 	}
    756      1.58     skrll 
    757      1.79  riastrad 	/* Success!  */
    758      1.79  riastrad 	mutex_enter(&sc->sc_lock);
    759      1.79  riastrad 	KASSERTMSG(sc->sc_refcnt == 0, "%d refs spuriously acquired",
    760      1.79  riastrad 	    sc->sc_refcnt);
    761      1.79  riastrad 	sc->sc_refcnt++;
    762      1.79  riastrad 	goto out0;
    763      1.79  riastrad 
    764      1.79  riastrad out4:	if (sc->sc_oxfer) {
    765      1.79  riastrad 		usbd_abort_pipe(sc->sc_opipe);
    766      1.65     skrll 		usbd_destroy_xfer(sc->sc_oxfer);
    767      1.79  riastrad 		sc->sc_oxfer = NULL;
    768      1.79  riastrad 	}
    769      1.79  riastrad out3:	if (sc->sc_opipe) {
    770      1.79  riastrad 		usbd_close_pipe(sc->sc_opipe);
    771      1.79  riastrad 		sc->sc_opipe = NULL;
    772      1.79  riastrad 	}
    773      1.79  riastrad out2:	if (sc->sc_ipipe) {
    774      1.79  riastrad 		usbd_abort_pipe(sc->sc_ipipe);
    775      1.79  riastrad 		usbd_close_pipe(sc->sc_ipipe);
    776      1.79  riastrad 		sc->sc_ipipe = NULL;
    777      1.79  riastrad 	}
    778      1.79  riastrad out1:	kmem_free(sc->sc_ibuf, sc->sc_isize);
    779      1.79  riastrad 	sc->sc_ibuf = NULL;
    780      1.56       mrg 	mutex_enter(&sc->sc_lock);
    781      1.79  riastrad out0:	KASSERT(mutex_owned(&sc->sc_lock));
    782      1.79  riastrad 	uhidev_config_exit(sc);
    783      1.79  riastrad out:	KASSERT(mutex_owned(&sc->sc_lock));
    784      1.25     skrll 	return error;
    785       1.1  augustss }
    786       1.1  augustss 
    787      1.79  riastrad static void
    788      1.79  riastrad uhidev_close_pipes(struct uhidev_softc *sc)
    789      1.63       mrg {
    790      1.63       mrg 
    791      1.79  riastrad 	KASSERT(mutex_owned(&sc->sc_lock));
    792      1.79  riastrad 	KASSERTMSG(sc->sc_refcnt > 0, "%s: refcnt fouled: %d",
    793      1.79  riastrad 	    device_xname(sc->sc_dev), sc->sc_refcnt);
    794      1.79  riastrad 
    795      1.79  riastrad 	/* If this isn't the last reference, just decrement.  */
    796      1.79  riastrad 	if (sc->sc_refcnt > 1) {
    797      1.79  riastrad 		sc->sc_refcnt--;
    798      1.79  riastrad 		return;
    799      1.79  riastrad 	}
    800      1.79  riastrad 
    801      1.79  riastrad 	/*
    802      1.79  riastrad 	 * Lock the configuration and release sc_lock so we may sleep
    803      1.79  riastrad 	 * to free memory.  We're not waiting for anyone to allocate or
    804      1.79  riastrad 	 * free anything.
    805      1.79  riastrad 	 */
    806      1.79  riastrad 	uhidev_config_enter_nointr(sc);
    807      1.79  riastrad 
    808      1.79  riastrad 	/*
    809      1.79  riastrad 	 * If someone else acquired a reference while we were waiting
    810      1.79  riastrad 	 * for the config lock, nothing more for us to do.
    811      1.79  riastrad 	 */
    812      1.79  riastrad 	if (sc->sc_refcnt > 1) {
    813      1.79  riastrad 		sc->sc_refcnt--;
    814      1.79  riastrad 		uhidev_config_exit(sc);
    815      1.79  riastrad 		return;
    816      1.79  riastrad 	}
    817      1.79  riastrad 
    818      1.79  riastrad 	/*
    819      1.79  riastrad 	 * We're the last reference and committed to closing the pipes.
    820      1.79  riastrad 	 * Decrement the reference count before we release the lock --
    821      1.79  riastrad 	 * access to the pipes is allowed as long as the reference
    822      1.79  riastrad 	 * count is positive, so this forces all new opens to wait
    823      1.79  riastrad 	 * until the config lock is released.
    824      1.79  riastrad 	 */
    825      1.79  riastrad 	KASSERTMSG(sc->sc_refcnt == 1, "%s: refcnt fouled: %d",
    826      1.79  riastrad 	    device_xname(sc->sc_dev), sc->sc_refcnt);
    827      1.79  riastrad 	sc->sc_refcnt--;
    828      1.79  riastrad 	mutex_exit(&sc->sc_lock);
    829      1.79  riastrad 
    830      1.79  riastrad 	if (sc->sc_oxfer) {
    831      1.63       mrg 		usbd_abort_pipe(sc->sc_opipe);
    832      1.79  riastrad 		usbd_destroy_xfer(sc->sc_oxfer);
    833      1.79  riastrad 		sc->sc_oxfer = NULL;
    834      1.79  riastrad 	}
    835      1.79  riastrad 	if (sc->sc_opipe) {
    836      1.63       mrg 		usbd_close_pipe(sc->sc_opipe);
    837      1.63       mrg 		sc->sc_opipe = NULL;
    838      1.63       mrg 	}
    839      1.79  riastrad 	if (sc->sc_ipipe) {
    840      1.63       mrg 		usbd_abort_pipe(sc->sc_ipipe);
    841      1.63       mrg 		usbd_close_pipe(sc->sc_ipipe);
    842      1.63       mrg 		sc->sc_ipipe = NULL;
    843      1.63       mrg 	}
    844      1.79  riastrad 	kmem_free(sc->sc_ibuf, sc->sc_isize);
    845      1.79  riastrad 	sc->sc_ibuf = NULL;
    846      1.79  riastrad 
    847      1.79  riastrad 	mutex_enter(&sc->sc_lock);
    848      1.79  riastrad 	uhidev_config_exit(sc);
    849      1.79  riastrad 	KASSERTMSG(sc->sc_refcnt == 0, "%s: refcnt fouled: %d",
    850      1.79  riastrad 	    device_xname(sc->sc_dev), sc->sc_refcnt);
    851      1.79  riastrad }
    852      1.79  riastrad 
    853      1.79  riastrad int
    854      1.79  riastrad uhidev_open(struct uhidev *scd)
    855      1.79  riastrad {
    856      1.79  riastrad 	struct uhidev_softc *sc = scd->sc_parent;
    857      1.79  riastrad 	int error;
    858      1.79  riastrad 
    859      1.79  riastrad 	mutex_enter(&sc->sc_lock);
    860      1.79  riastrad 
    861      1.79  riastrad 	DPRINTF(("uhidev_open(%s, report %d = %s): state=%x refcnt=%d\n",
    862      1.79  riastrad 		device_xname(sc->sc_dev),
    863      1.79  riastrad 		scd->sc_report_id,
    864      1.79  riastrad 		device_xname(scd->sc_dev),
    865      1.79  riastrad 		scd->sc_state,
    866      1.79  riastrad 		sc->sc_refcnt));
    867      1.63       mrg 
    868      1.79  riastrad 	/* Mark the report id open.  This is an exclusive lock.  */
    869      1.79  riastrad 	if (scd->sc_state & UHIDEV_OPEN) {
    870      1.79  riastrad 		error = EBUSY;
    871      1.79  riastrad 		goto fail0;
    872      1.63       mrg 	}
    873      1.79  riastrad 	scd->sc_state |= UHIDEV_OPEN;
    874      1.79  riastrad 
    875      1.79  riastrad 	/* Open the pipes which are shared by all report ids.  */
    876      1.79  riastrad 	error = uhidev_open_pipes(sc);
    877      1.79  riastrad 	if (error)
    878      1.79  riastrad 		goto fail1;
    879      1.79  riastrad 
    880      1.79  riastrad 	mutex_exit(&sc->sc_lock);
    881      1.79  riastrad 
    882      1.79  riastrad 	/* Success!  */
    883      1.79  riastrad 	return 0;
    884      1.79  riastrad 
    885      1.79  riastrad fail2: __unused
    886      1.79  riastrad 	uhidev_close_pipes(sc);
    887      1.79  riastrad fail1:	KASSERTMSG(scd->sc_state & UHIDEV_OPEN,
    888      1.79  riastrad 	    "%s: report id %d: closed while opening",
    889      1.79  riastrad 	    device_xname(sc->sc_dev), scd->sc_report_id);
    890      1.79  riastrad 	scd->sc_state &= ~UHIDEV_OPEN;
    891      1.79  riastrad fail0:	mutex_exit(&sc->sc_lock);
    892      1.79  riastrad 	return error;
    893      1.63       mrg }
    894      1.63       mrg 
    895      1.63       mrg void
    896      1.79  riastrad uhidev_stop(struct uhidev *scd)
    897       1.1  augustss {
    898       1.1  augustss 	struct uhidev_softc *sc = scd->sc_parent;
    899      1.79  riastrad 	bool abort = false;
    900       1.1  augustss 
    901      1.56       mrg 	mutex_enter(&sc->sc_lock);
    902      1.79  riastrad 	if (sc->sc_writereportid == scd->sc_report_id)
    903      1.79  riastrad 		abort = true;
    904      1.56       mrg 	mutex_exit(&sc->sc_lock);
    905      1.56       mrg 
    906      1.79  riastrad 	if (abort && sc->sc_opipe)
    907      1.79  riastrad 		usbd_abort_pipe(sc->sc_opipe);
    908      1.79  riastrad }
    909      1.79  riastrad 
    910      1.79  riastrad void
    911      1.79  riastrad uhidev_close(struct uhidev *scd)
    912      1.79  riastrad {
    913      1.79  riastrad 	struct uhidev_softc *sc = scd->sc_parent;
    914       1.1  augustss 
    915      1.79  riastrad 	mutex_enter(&sc->sc_lock);
    916      1.58     skrll 
    917      1.79  riastrad 	DPRINTF(("uhidev_close(%s, report %d = %s): state=%x refcnt=%d\n",
    918      1.79  riastrad 		device_xname(sc->sc_dev),
    919      1.79  riastrad 		scd->sc_report_id,
    920      1.79  riastrad 		device_xname(scd->sc_dev),
    921      1.79  riastrad 		scd->sc_state,
    922      1.79  riastrad 		sc->sc_refcnt));
    923      1.79  riastrad 
    924      1.79  riastrad 	KASSERTMSG(scd->sc_state & UHIDEV_OPEN,
    925      1.79  riastrad 	    "%s: report id %d: unpaired close",
    926      1.79  riastrad 	    device_xname(sc->sc_dev), scd->sc_report_id);
    927      1.79  riastrad 	uhidev_close_pipes(sc);
    928      1.79  riastrad 	KASSERTMSG(scd->sc_state & UHIDEV_OPEN,
    929      1.79  riastrad 	    "%s: report id %d: closed while closing",
    930      1.79  riastrad 	    device_xname(sc->sc_dev), scd->sc_report_id);
    931      1.79  riastrad 	scd->sc_state &= ~UHIDEV_OPEN;
    932      1.65     skrll 
    933      1.79  riastrad 	mutex_exit(&sc->sc_lock);
    934       1.1  augustss }
    935       1.1  augustss 
    936       1.1  augustss usbd_status
    937       1.1  augustss uhidev_set_report(struct uhidev *scd, int type, void *data, int len)
    938       1.1  augustss {
    939      1.13   dsainty 	char *buf;
    940      1.13   dsainty 	usbd_status retstat;
    941      1.13   dsainty 
    942      1.13   dsainty 	if (scd->sc_report_id == 0)
    943      1.13   dsainty 		return usbd_set_report(scd->sc_parent->sc_iface, type,
    944      1.13   dsainty 				       scd->sc_report_id, data, len);
    945      1.13   dsainty 
    946      1.65     skrll 	buf = kmem_alloc(len + 1, KM_SLEEP);
    947      1.13   dsainty 	buf[0] = scd->sc_report_id;
    948      1.13   dsainty 	memcpy(buf+1, data, len);
    949      1.13   dsainty 
    950      1.13   dsainty 	retstat = usbd_set_report(scd->sc_parent->sc_iface, type,
    951      1.18   dsainty 				  scd->sc_report_id, buf, len + 1);
    952      1.13   dsainty 
    953      1.65     skrll 	kmem_free(buf, len + 1);
    954       1.1  augustss 
    955      1.13   dsainty 	return retstat;
    956       1.1  augustss }
    957       1.1  augustss 
    958       1.1  augustss usbd_status
    959       1.1  augustss uhidev_get_report(struct uhidev *scd, int type, void *data, int len)
    960       1.1  augustss {
    961       1.6  augustss 	return usbd_get_report(scd->sc_parent->sc_iface, type,
    962       1.1  augustss 			       scd->sc_report_id, data, len);
    963       1.1  augustss }
    964      1.25     skrll 
    965      1.25     skrll usbd_status
    966      1.25     skrll uhidev_write(struct uhidev_softc *sc, void *data, int len)
    967      1.25     skrll {
    968      1.79  riastrad 	usbd_status err;
    969      1.25     skrll 
    970      1.25     skrll 	DPRINTF(("uhidev_write: data=%p, len=%d\n", data, len));
    971      1.25     skrll 
    972      1.25     skrll 	if (sc->sc_opipe == NULL)
    973      1.25     skrll 		return USBD_INVAL;
    974      1.25     skrll 
    975      1.79  riastrad 	mutex_enter(&sc->sc_lock);
    976      1.79  riastrad 	KASSERT(sc->sc_refcnt);
    977      1.79  riastrad 	for (;;) {
    978      1.79  riastrad 		if (sc->sc_dying) {
    979      1.79  riastrad 			err = USBD_IOERROR;
    980      1.79  riastrad 			goto out;
    981      1.79  riastrad 		}
    982      1.79  riastrad 		if (sc->sc_writelock == NULL)
    983      1.79  riastrad 			break;
    984      1.79  riastrad 		if (cv_wait_sig(&sc->sc_cv, &sc->sc_lock)) {
    985      1.79  riastrad 			err = USBD_INTERRUPTED;
    986      1.79  riastrad 			goto out;
    987      1.79  riastrad 		}
    988      1.79  riastrad 	}
    989      1.79  riastrad 	sc->sc_writelock = curlwp;
    990      1.79  riastrad 	mutex_exit(&sc->sc_lock);
    991      1.79  riastrad 
    992      1.25     skrll #ifdef UHIDEV_DEBUG
    993      1.25     skrll 	if (uhidevdebug > 50) {
    994      1.25     skrll 
    995      1.65     skrll 		uint32_t i;
    996      1.65     skrll 		uint8_t *d = data;
    997      1.25     skrll 
    998      1.25     skrll 		DPRINTF(("uhidev_write: data ="));
    999      1.25     skrll 		for (i = 0; i < len; i++)
   1000      1.25     skrll 			DPRINTF((" %02x", d[i]));
   1001      1.25     skrll 		DPRINTF(("\n"));
   1002      1.25     skrll 	}
   1003      1.25     skrll #endif
   1004      1.79  riastrad 	err = usbd_intr_transfer(sc->sc_oxfer, sc->sc_opipe, 0,
   1005      1.79  riastrad 	    USBD_NO_TIMEOUT, data, &len);
   1006      1.79  riastrad 
   1007      1.79  riastrad 	mutex_enter(&sc->sc_lock);
   1008      1.79  riastrad 	KASSERT(sc->sc_refcnt);
   1009      1.79  riastrad 	KASSERTMSG(sc->sc_writelock == curlwp, "%s: migrated from %p to %p",
   1010      1.79  riastrad 	    device_xname(sc->sc_dev), curlwp, sc->sc_writelock);
   1011      1.79  riastrad 	sc->sc_writelock = NULL;
   1012      1.79  riastrad 	cv_broadcast(&sc->sc_cv);
   1013      1.79  riastrad out:	mutex_exit(&sc->sc_lock);
   1014      1.79  riastrad 	return err;
   1015      1.25     skrll }
   1016