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