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