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