Home | History | Annotate | Line # | Download | only in usb
uhidev.c revision 1.27.2.5
      1  1.27.2.5      yamt /*	$NetBSD: uhidev.c,v 1.27.2.5 2007/12/07 17:31:36 yamt Exp $	*/
      2       1.1  augustss 
      3       1.1  augustss /*
      4       1.1  augustss  * Copyright (c) 2001 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.1  augustss  * Carlstedt Research & Technology.
     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  * 3. All advertising materials mentioning features or use of this software
     20       1.1  augustss  *    must display the following acknowledgement:
     21       1.1  augustss  *        This product includes software developed by the NetBSD
     22       1.1  augustss  *        Foundation, Inc. and its contributors.
     23       1.1  augustss  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24       1.1  augustss  *    contributors may be used to endorse or promote products derived
     25       1.1  augustss  *    from this software without specific prior written permission.
     26       1.1  augustss  *
     27       1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28       1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29       1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30       1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31       1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32       1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33       1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34       1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35       1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36       1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37       1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     38       1.1  augustss  */
     39       1.1  augustss 
     40       1.1  augustss /*
     41      1.14  augustss  * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
     42       1.1  augustss  */
     43      1.15     lukem 
     44      1.15     lukem #include <sys/cdefs.h>
     45  1.27.2.5      yamt __KERNEL_RCSID(0, "$NetBSD: uhidev.c,v 1.27.2.5 2007/12/07 17:31:36 yamt Exp $");
     46       1.1  augustss 
     47       1.1  augustss #include <sys/param.h>
     48       1.1  augustss #include <sys/systm.h>
     49       1.1  augustss #include <sys/kernel.h>
     50       1.1  augustss #include <sys/malloc.h>
     51       1.1  augustss #include <sys/signalvar.h>
     52       1.1  augustss #include <sys/device.h>
     53       1.1  augustss #include <sys/ioctl.h>
     54       1.1  augustss #include <sys/conf.h>
     55       1.1  augustss 
     56       1.1  augustss #include <dev/usb/usb.h>
     57       1.1  augustss #include <dev/usb/usbhid.h>
     58       1.1  augustss 
     59       1.1  augustss #include <dev/usb/usbdevs.h>
     60       1.1  augustss #include <dev/usb/usbdi.h>
     61       1.1  augustss #include <dev/usb/usbdi_util.h>
     62       1.1  augustss #include <dev/usb/hid.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.1  augustss 
     67       1.1  augustss /* Report descriptor for broken Wacom Graphire */
     68       1.1  augustss #include <dev/usb/ugraphire_rdesc.h>
     69       1.1  augustss 
     70      1.22  drochner #include "locators.h"
     71      1.22  drochner 
     72       1.1  augustss #ifdef UHIDEV_DEBUG
     73       1.1  augustss #define DPRINTF(x)	if (uhidevdebug) logprintf x
     74       1.1  augustss #define DPRINTFN(n,x)	if (uhidevdebug>(n)) logprintf x
     75       1.1  augustss int	uhidevdebug = 0;
     76       1.1  augustss #else
     77       1.1  augustss #define DPRINTF(x)
     78       1.1  augustss #define DPRINTFN(n,x)
     79       1.1  augustss #endif
     80       1.1  augustss 
     81       1.1  augustss Static void uhidev_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
     82       1.1  augustss 
     83  1.27.2.1      yamt Static int uhidev_maxrepid(void *, int);
     84  1.27.2.1      yamt Static int uhidevprint(void *, const char *);
     85       1.1  augustss 
     86       1.1  augustss USB_DECLARE_DRIVER(uhidev);
     87       1.1  augustss 
     88       1.1  augustss USB_MATCH(uhidev)
     89       1.1  augustss {
     90  1.27.2.3      yamt 	USB_IFMATCH_START(uhidev, uaa);
     91       1.6  augustss 
     92  1.27.2.3      yamt 	if (uaa->class != UICLASS_HID)
     93       1.1  augustss 		return (UMATCH_NONE);
     94  1.27.2.4      yamt 	if (usbd_get_quirks(uaa->device)->uq_flags & UQ_HID_IGNORE)
     95  1.27.2.4      yamt 		return (UMATCH_NONE);
     96       1.1  augustss 	return (UMATCH_IFACECLASS_GENERIC);
     97       1.1  augustss }
     98       1.1  augustss 
     99       1.1  augustss USB_ATTACH(uhidev)
    100       1.1  augustss {
    101  1.27.2.3      yamt 	USB_IFATTACH_START(uhidev, sc, uaa);
    102       1.1  augustss 	usbd_interface_handle iface = uaa->iface;
    103       1.1  augustss 	usb_interface_descriptor_t *id;
    104       1.1  augustss 	usb_endpoint_descriptor_t *ed;
    105       1.1  augustss 	struct uhidev_attach_arg uha;
    106       1.1  augustss 	struct uhidev *dev;
    107       1.1  augustss 	int size, nrepid, repid, repsz;
    108  1.27.2.1      yamt 	int *repsizes;
    109      1.25     skrll 	int i;
    110      1.19  jdolecek 	void *desc;
    111      1.20  augustss 	const void *descptr;
    112       1.1  augustss 	usbd_status err;
    113      1.26  augustss 	char *devinfop;
    114  1.27.2.1      yamt 	int locs[UHIDBUSCF_NLOCS];
    115       1.6  augustss 
    116       1.1  augustss 	sc->sc_udev = uaa->device;
    117       1.1  augustss 	sc->sc_iface = iface;
    118       1.1  augustss 	id = usbd_get_interface_descriptor(iface);
    119      1.26  augustss 
    120      1.26  augustss 	devinfop = usbd_devinfo_alloc(uaa->device, 0);
    121       1.1  augustss 	USB_ATTACH_SETUP;
    122  1.27.2.5      yamt 	aprint_normal("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
    123      1.26  augustss 	       devinfop, id->bInterfaceClass, id->bInterfaceSubClass);
    124      1.26  augustss 	usbd_devinfo_free(devinfop);
    125       1.1  augustss 
    126       1.1  augustss 	(void)usbd_set_idle(iface, 0, 0);
    127       1.1  augustss #if 0
    128       1.1  augustss 
    129       1.1  augustss 	qflags = usbd_get_quirks(sc->sc_udev)->uq_flags;
    130       1.1  augustss 	if ((qflags & UQ_NO_SET_PROTO) == 0 &&
    131       1.1  augustss 	    id->bInterfaceSubClass != UISUBCLASS_BOOT)
    132       1.1  augustss 		(void)usbd_set_protocol(iface, 1);
    133       1.1  augustss #endif
    134       1.1  augustss 
    135      1.25     skrll 	sc->sc_iep_addr = sc->sc_oep_addr = -1;
    136      1.25     skrll 	for (i = 0; i < id->bNumEndpoints; i++) {
    137      1.25     skrll 		ed = usbd_interface2endpoint_descriptor(iface, i);
    138      1.25     skrll 		if (ed == NULL) {
    139  1.27.2.5      yamt 			aprint_error("%s: could not read endpoint descriptor\n",
    140      1.25     skrll 			    USBDEVNAME(sc->sc_dev));
    141      1.25     skrll 			sc->sc_dying = 1;
    142      1.25     skrll 			USB_ATTACH_ERROR_RETURN;
    143      1.25     skrll 		}
    144      1.25     skrll 
    145      1.25     skrll 		DPRINTFN(10,("uhidev_attach: bLength=%d bDescriptorType=%d "
    146      1.25     skrll 		    "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
    147      1.25     skrll 		    " bInterval=%d\n",
    148      1.25     skrll 		    ed->bLength, ed->bDescriptorType,
    149      1.25     skrll 		    ed->bEndpointAddress & UE_ADDR,
    150      1.25     skrll 		    UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out",
    151      1.25     skrll 		    ed->bmAttributes & UE_XFERTYPE,
    152      1.25     skrll 		    UGETW(ed->wMaxPacketSize), ed->bInterval));
    153      1.25     skrll 
    154      1.25     skrll 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    155      1.25     skrll 		    (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
    156      1.25     skrll 			sc->sc_iep_addr = ed->bEndpointAddress;
    157      1.25     skrll 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    158      1.25     skrll 		    (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
    159      1.25     skrll 			sc->sc_oep_addr = ed->bEndpointAddress;
    160      1.25     skrll 		} else {
    161  1.27.2.5      yamt 			aprint_verbose("%s: endpoint %d: ignored\n",
    162  1.27.2.5      yamt 			    USBDEVNAME(sc->sc_dev), i);
    163      1.25     skrll 		}
    164       1.1  augustss 	}
    165       1.1  augustss 
    166      1.25     skrll 	/*
    167      1.25     skrll 	 * Check that we found an input interrupt endpoint. The output interrupt
    168      1.25     skrll 	 * endpoint is optional
    169      1.25     skrll 	 */
    170      1.25     skrll 	if (sc->sc_iep_addr == -1) {
    171  1.27.2.5      yamt 		aprint_error("%s: no input interrupt endpoint\n",
    172  1.27.2.5      yamt 		    USBDEVNAME(sc->sc_dev));
    173       1.1  augustss 		sc->sc_dying = 1;
    174       1.1  augustss 		USB_ATTACH_ERROR_RETURN;
    175       1.1  augustss 	}
    176       1.1  augustss 
    177       1.1  augustss 	/* XXX need to extend this */
    178      1.20  augustss 	descptr = NULL;
    179      1.20  augustss 	if (uaa->vendor == USB_VENDOR_WACOM) {
    180      1.20  augustss 		static uByte reportbuf[] = {2, 2, 2};
    181      1.17  augustss 
    182       1.1  augustss 		/* The report descriptor for the Wacom Graphire is broken. */
    183      1.17  augustss 		switch (uaa->product) {
    184      1.17  augustss 		case USB_PRODUCT_WACOM_GRAPHIRE:
    185      1.17  augustss 			size = sizeof uhid_graphire_report_descr;
    186      1.17  augustss 			descptr = uhid_graphire_report_descr;
    187      1.17  augustss 			break;
    188      1.17  augustss 
    189  1.27.2.2      yamt 		case USB_PRODUCT_WACOM_GRAPHIRE3_4X5:
    190  1.27.2.2      yamt 		case USB_PRODUCT_WACOM_GRAPHIRE3_6X8:
    191  1.27.2.2      yamt 		case USB_PRODUCT_WACOM_GRAPHIRE4_4X5: /* The 6x8 too? */
    192      1.17  augustss 			/*
    193      1.17  augustss 			 * The Graphire3 needs 0x0202 to be written to
    194      1.17  augustss 			 * feature report ID 2 before it'll start
    195      1.17  augustss 			 * returning digitizer data.
    196      1.17  augustss 			 */
    197      1.17  augustss 			usbd_set_report(uaa->iface, UHID_FEATURE_REPORT, 2,
    198      1.20  augustss 			    &reportbuf, sizeof reportbuf);
    199      1.17  augustss 
    200      1.17  augustss 			size = sizeof uhid_graphire3_4x5_report_descr;
    201      1.17  augustss 			descptr = uhid_graphire3_4x5_report_descr;
    202      1.17  augustss 			break;
    203      1.20  augustss 		default:
    204      1.20  augustss 			/* Keep descriptor */
    205      1.20  augustss 			break;
    206      1.17  augustss 		}
    207      1.17  augustss 	}
    208      1.17  augustss 
    209      1.17  augustss 	if (descptr) {
    210       1.1  augustss 		desc = malloc(size, M_USBDEV, M_NOWAIT);
    211       1.1  augustss 		if (desc == NULL)
    212       1.1  augustss 			err = USBD_NOMEM;
    213       1.1  augustss 		else {
    214       1.1  augustss 			err = USBD_NORMAL_COMPLETION;
    215      1.17  augustss 			memcpy(desc, descptr, size);
    216       1.1  augustss 		}
    217       1.1  augustss 	} else {
    218       1.1  augustss 		desc = NULL;
    219      1.17  augustss 		err = usbd_read_report_desc(uaa->iface, &desc, &size,
    220      1.17  augustss 		    M_USBDEV);
    221       1.1  augustss 	}
    222       1.1  augustss 	if (err) {
    223  1.27.2.5      yamt 		aprint_error("%s: no report descriptor\n",
    224  1.27.2.5      yamt 		    USBDEVNAME(sc->sc_dev));
    225       1.1  augustss 		sc->sc_dying = 1;
    226       1.1  augustss 		USB_ATTACH_ERROR_RETURN;
    227       1.1  augustss 	}
    228       1.6  augustss 
    229      1.27  augustss 	if (uaa->vendor == USB_VENDOR_HOSIDEN &&
    230      1.27  augustss 	    uaa->product == USB_PRODUCT_HOSIDEN_PPP) {
    231      1.27  augustss 		static uByte reportbuf[] = { 1 };
    232      1.27  augustss 		/*
    233      1.27  augustss 		 *  This device was sold by Konami with its ParaParaParadise
    234      1.27  augustss 		 *  game for PlayStation2.  It needs to be "turned on"
    235      1.27  augustss 		 *  before it will send any reports.
    236      1.27  augustss 		 */
    237      1.27  augustss 
    238      1.27  augustss 		usbd_set_report(uaa->iface, UHID_FEATURE_REPORT, 0,
    239      1.27  augustss 		    &reportbuf, sizeof reportbuf);
    240      1.27  augustss 	}
    241      1.27  augustss 
    242       1.1  augustss 	sc->sc_repdesc = desc;
    243       1.1  augustss 	sc->sc_repdesc_size = size;
    244       1.1  augustss 
    245       1.1  augustss 	uha.uaa = uaa;
    246       1.1  augustss 	nrepid = uhidev_maxrepid(desc, size);
    247       1.1  augustss 	if (nrepid < 0)
    248       1.1  augustss 		USB_ATTACH_SUCCESS_RETURN;
    249       1.1  augustss 	if (nrepid > 0)
    250  1.27.2.5      yamt 		aprint_normal("%s: %d report ids\n",
    251  1.27.2.5      yamt 		    USBDEVNAME(sc->sc_dev), nrepid);
    252       1.1  augustss 	nrepid++;
    253  1.27.2.1      yamt 	repsizes = malloc(nrepid * sizeof(*repsizes), M_TEMP, M_NOWAIT);
    254  1.27.2.1      yamt 	if (repsizes == NULL)
    255  1.27.2.1      yamt 		goto nomem;
    256       1.1  augustss 	sc->sc_subdevs = malloc(nrepid * sizeof(device_ptr_t),
    257       1.5  augustss 				M_USBDEV, M_NOWAIT | M_ZERO);
    258       1.1  augustss 	if (sc->sc_subdevs == NULL) {
    259  1.27.2.1      yamt 		free(repsizes, M_TEMP);
    260  1.27.2.1      yamt nomem:
    261  1.27.2.5      yamt 		aprint_error("%s: no memory\n", USBDEVNAME(sc->sc_dev));
    262       1.1  augustss 		USB_ATTACH_ERROR_RETURN;
    263       1.1  augustss 	}
    264       1.1  augustss 	sc->sc_nrepid = nrepid;
    265       1.1  augustss 	sc->sc_isize = 0;
    266       1.1  augustss 
    267       1.1  augustss 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    268       1.1  augustss 			   USBDEV(sc->sc_dev));
    269       1.1  augustss 
    270       1.1  augustss 	for (repid = 0; repid < nrepid; repid++) {
    271       1.1  augustss 		repsz = hid_report_size(desc, size, hid_input, repid);
    272       1.1  augustss 		DPRINTF(("uhidev_match: repid=%d, repsz=%d\n", repid, repsz));
    273       1.2  augustss 		repsizes[repid] = repsz;
    274       1.1  augustss 		if (repsz > 0) {
    275       1.1  augustss 			if (repsz > sc->sc_isize)
    276       1.1  augustss 				sc->sc_isize = repsz;
    277       1.1  augustss 		}
    278       1.1  augustss 	}
    279       1.1  augustss 	sc->sc_isize += nrepid != 1;	/* space for report ID */
    280       1.1  augustss 	DPRINTF(("uhidev_attach: isize=%d\n", sc->sc_isize));
    281       1.1  augustss 
    282       1.1  augustss 	uha.parent = sc;
    283       1.1  augustss 	for (repid = 0; repid < nrepid; repid++) {
    284       1.1  augustss 		DPRINTF(("uhidev_match: try repid=%d\n", repid));
    285       1.1  augustss 		if (hid_report_size(desc, size, hid_input, repid) == 0 &&
    286       1.1  augustss 		    hid_report_size(desc, size, hid_output, repid) == 0 &&
    287       1.1  augustss 		    hid_report_size(desc, size, hid_feature, repid) == 0) {
    288       1.5  augustss 			;	/* already NULL in sc->sc_subdevs[repid] */
    289       1.1  augustss 		} else {
    290       1.1  augustss 			uha.reportid = repid;
    291  1.27.2.1      yamt 			locs[UHIDBUSCF_REPORTID] = repid;
    292      1.22  drochner 
    293      1.22  drochner 			dev = (struct uhidev *)config_found_sm_loc(self,
    294  1.27.2.1      yamt 				"uhidbus", locs, &uha,
    295  1.27.2.3      yamt 				uhidevprint, config_stdsubmatch);
    296       1.1  augustss 			sc->sc_subdevs[repid] = dev;
    297       1.4  augustss 			if (dev != NULL) {
    298       1.4  augustss 				dev->sc_in_rep_size = repsizes[repid];
    299       1.1  augustss #ifdef DIAGNOSTIC
    300       1.1  augustss 				DPRINTF(("uhidev_match: repid=%d dev=%p\n",
    301       1.1  augustss 					 repid, dev));
    302       1.1  augustss 				if (dev->sc_intr == NULL) {
    303  1.27.2.1      yamt 					free(repsizes, M_TEMP);
    304       1.1  augustss 					printf("%s: sc_intr == NULL\n",
    305       1.1  augustss 					       USBDEVNAME(sc->sc_dev));
    306       1.1  augustss 					USB_ATTACH_ERROR_RETURN;
    307       1.1  augustss 				}
    308       1.4  augustss #endif
    309       1.9       dan #if NRND > 0
    310      1.24     perry 				rnd_attach_source(&dev->rnd_source,
    311      1.24     perry 						  USBDEVNAME(dev->sc_dev),
    312       1.9       dan 						  RND_TYPE_TTY, 0);
    313       1.9       dan #endif
    314       1.1  augustss 			}
    315       1.1  augustss 		}
    316       1.1  augustss 	}
    317  1.27.2.1      yamt 	free(repsizes, M_TEMP);
    318       1.1  augustss 
    319       1.1  augustss 	USB_ATTACH_SUCCESS_RETURN;
    320       1.1  augustss }
    321       1.1  augustss 
    322       1.1  augustss int
    323       1.1  augustss uhidev_maxrepid(void *buf, int len)
    324       1.1  augustss {
    325       1.1  augustss 	struct hid_data *d;
    326       1.1  augustss 	struct hid_item h;
    327       1.1  augustss 	int maxid;
    328       1.1  augustss 
    329       1.1  augustss 	maxid = -1;
    330       1.1  augustss 	h.report_ID = 0;
    331       1.1  augustss 	for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); )
    332       1.1  augustss 		if (h.report_ID > maxid)
    333       1.1  augustss 			maxid = h.report_ID;
    334       1.1  augustss 	hid_end_parse(d);
    335       1.1  augustss 	return (maxid);
    336       1.1  augustss }
    337       1.1  augustss 
    338       1.1  augustss int
    339       1.1  augustss uhidevprint(void *aux, const char *pnp)
    340       1.1  augustss {
    341       1.1  augustss 	struct uhidev_attach_arg *uha = aux;
    342       1.1  augustss 
    343       1.1  augustss 	if (pnp)
    344      1.12   thorpej 		aprint_normal("uhid at %s", pnp);
    345       1.1  augustss 	if (uha->reportid != 0)
    346      1.12   thorpej 		aprint_normal(" reportid %d", uha->reportid);
    347       1.1  augustss 	return (UNCONF);
    348       1.1  augustss }
    349       1.1  augustss 
    350       1.1  augustss int
    351       1.1  augustss uhidev_activate(device_ptr_t self, enum devact act)
    352       1.1  augustss {
    353       1.1  augustss 	struct uhidev_softc *sc = (struct uhidev_softc *)self;
    354       1.1  augustss 	int i, rv;
    355       1.1  augustss 
    356       1.1  augustss 	switch (act) {
    357       1.1  augustss 	case DVACT_ACTIVATE:
    358       1.1  augustss 		return (EOPNOTSUPP);
    359       1.1  augustss 
    360       1.1  augustss 	case DVACT_DEACTIVATE:
    361       1.1  augustss 		rv = 0;
    362       1.1  augustss 		for (i = 0; i < sc->sc_nrepid; i++)
    363       1.1  augustss 			if (sc->sc_subdevs[i] != NULL)
    364       1.1  augustss 				rv |= config_deactivate(
    365       1.1  augustss 					&sc->sc_subdevs[i]->sc_dev);
    366       1.1  augustss 		sc->sc_dying = 1;
    367      1.16  christos 		break;
    368      1.16  christos 	default:
    369      1.16  christos 		rv = 0;
    370       1.1  augustss 		break;
    371       1.1  augustss 	}
    372       1.1  augustss 	return (rv);
    373       1.1  augustss }
    374       1.1  augustss 
    375       1.1  augustss USB_DETACH(uhidev)
    376       1.1  augustss {
    377       1.1  augustss 	USB_DETACH_START(uhidev, sc);
    378       1.1  augustss 	int i, rv;
    379       1.1  augustss 
    380       1.1  augustss 	DPRINTF(("uhidev_detach: sc=%p flags=%d\n", sc, flags));
    381       1.1  augustss 
    382       1.1  augustss 	sc->sc_dying = 1;
    383      1.25     skrll 	if (sc->sc_ipipe != NULL)
    384      1.25     skrll 		usbd_abort_pipe(sc->sc_ipipe);
    385       1.1  augustss 
    386       1.1  augustss 	if (sc->sc_repdesc != NULL)
    387       1.1  augustss 		free(sc->sc_repdesc, M_USBDEV);
    388       1.1  augustss 
    389       1.1  augustss 	rv = 0;
    390       1.1  augustss 	for (i = 0; i < sc->sc_nrepid; i++) {
    391       1.1  augustss 		if (sc->sc_subdevs[i] != NULL) {
    392       1.9       dan #if NRND > 0
    393       1.9       dan 			rnd_detach_source(&sc->sc_subdevs[i]->rnd_source);
    394       1.9       dan #endif
    395       1.1  augustss 			rv |= config_detach(&sc->sc_subdevs[i]->sc_dev, flags);
    396       1.1  augustss 			sc->sc_subdevs[i] = NULL;
    397       1.1  augustss 		}
    398       1.1  augustss 	}
    399       1.1  augustss 
    400       1.1  augustss 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    401       1.1  augustss 			   USBDEV(sc->sc_dev));
    402       1.1  augustss 
    403       1.1  augustss 	return (rv);
    404       1.1  augustss }
    405       1.1  augustss 
    406       1.1  augustss void
    407       1.1  augustss uhidev_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
    408       1.1  augustss {
    409       1.1  augustss 	struct uhidev_softc *sc = addr;
    410       1.1  augustss 	struct uhidev *scd;
    411       1.1  augustss 	u_char *p;
    412       1.1  augustss 	u_int rep;
    413       1.1  augustss 	u_int32_t cc;
    414       1.1  augustss 
    415       1.1  augustss 	usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
    416       1.1  augustss 
    417       1.1  augustss #ifdef UHIDEV_DEBUG
    418       1.1  augustss 	if (uhidevdebug > 5) {
    419       1.1  augustss 		u_int32_t i;
    420       1.6  augustss 
    421       1.1  augustss 		DPRINTF(("uhidev_intr: status=%d cc=%d\n", status, cc));
    422       1.1  augustss 		DPRINTF(("uhidev_intr: data ="));
    423       1.1  augustss 		for (i = 0; i < cc; i++)
    424       1.1  augustss 			DPRINTF((" %02x", sc->sc_ibuf[i]));
    425       1.1  augustss 		DPRINTF(("\n"));
    426       1.1  augustss 	}
    427       1.1  augustss #endif
    428       1.1  augustss 
    429       1.1  augustss 	if (status == USBD_CANCELLED)
    430       1.1  augustss 		return;
    431       1.1  augustss 
    432       1.1  augustss 	if (status != USBD_NORMAL_COMPLETION) {
    433       1.3  augustss 		DPRINTF(("%s: interrupt status=%d\n", USBDEVNAME(sc->sc_dev),
    434       1.3  augustss 			 status));
    435      1.25     skrll 		usbd_clear_endpoint_stall_async(sc->sc_ipipe);
    436       1.1  augustss 		return;
    437       1.1  augustss 	}
    438       1.1  augustss 
    439       1.1  augustss 	p = sc->sc_ibuf;
    440       1.1  augustss 	if (sc->sc_nrepid != 1)
    441       1.1  augustss 		rep = *p++, cc--;
    442       1.1  augustss 	else
    443       1.1  augustss 		rep = 0;
    444       1.1  augustss 	if (rep >= sc->sc_nrepid) {
    445       1.1  augustss 		printf("uhidev_intr: bad repid %d\n", rep);
    446       1.1  augustss 		return;
    447       1.1  augustss 	}
    448       1.1  augustss 	scd = sc->sc_subdevs[rep];
    449       1.1  augustss 	DPRINTFN(5,("uhidev_intr: rep=%d, scd=%p state=0x%x\n",
    450       1.1  augustss 		    rep, scd, scd ? scd->sc_state : 0));
    451       1.1  augustss 	if (scd == NULL || !(scd->sc_state & UHIDEV_OPEN))
    452       1.1  augustss 		return;
    453      1.23  augustss 	if (scd->sc_in_rep_size != cc) {
    454      1.23  augustss 		printf("%s: bad input length %d != %d\n",
    455      1.23  augustss 		       USBDEVNAME(sc->sc_dev), scd->sc_in_rep_size, cc);
    456      1.23  augustss 		return;
    457      1.23  augustss 	}
    458       1.9       dan #if NRND > 0
    459      1.10      fair 	rnd_add_uint32(&scd->rnd_source, (uintptr_t)(sc->sc_ibuf));
    460       1.2  augustss #endif
    461       1.1  augustss 	scd->sc_intr(scd, p, cc);
    462       1.1  augustss }
    463       1.1  augustss 
    464       1.1  augustss void
    465       1.1  augustss uhidev_get_report_desc(struct uhidev_softc *sc, void **desc, int *size)
    466       1.1  augustss {
    467       1.1  augustss 	*desc = sc->sc_repdesc;
    468       1.1  augustss 	*size = sc->sc_repdesc_size;
    469       1.1  augustss }
    470       1.1  augustss 
    471       1.1  augustss int
    472       1.1  augustss uhidev_open(struct uhidev *scd)
    473       1.1  augustss {
    474       1.1  augustss 	struct uhidev_softc *sc = scd->sc_parent;
    475       1.1  augustss 	usbd_status err;
    476      1.25     skrll 	int error;
    477       1.1  augustss 
    478       1.1  augustss 	DPRINTF(("uhidev_open: open pipe, state=%d refcnt=%d\n",
    479       1.1  augustss 		 scd->sc_state, sc->sc_refcnt));
    480       1.1  augustss 
    481       1.1  augustss 	if (scd->sc_state & UHIDEV_OPEN)
    482       1.1  augustss 		return (EBUSY);
    483       1.1  augustss 	scd->sc_state |= UHIDEV_OPEN;
    484       1.1  augustss 	if (sc->sc_refcnt++)
    485       1.1  augustss 		return (0);
    486       1.1  augustss 
    487       1.1  augustss 	if (sc->sc_isize == 0)
    488       1.1  augustss 		return (0);
    489       1.1  augustss 
    490       1.1  augustss 	sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
    491       1.1  augustss 
    492      1.25     skrll 	/* Set up input interrupt pipe. */
    493       1.1  augustss 	DPRINTF(("uhidev_open: isize=%d, ep=0x%02x\n", sc->sc_isize,
    494      1.25     skrll 		 sc->sc_iep_addr));
    495      1.25     skrll 
    496      1.25     skrll 	err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_iep_addr,
    497      1.25     skrll 		  USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_ibuf,
    498       1.1  augustss 		  sc->sc_isize, uhidev_intr, USBD_DEFAULT_INTERVAL);
    499      1.25     skrll 	if (err != USBD_NORMAL_COMPLETION) {
    500       1.1  augustss 		DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
    501      1.25     skrll 		    "error=%d\n", err));
    502      1.25     skrll 		error = EIO;
    503      1.25     skrll 		goto out1;
    504      1.25     skrll 	}
    505      1.25     skrll 
    506      1.25     skrll 	/*
    507      1.25     skrll 	 * Set up output interrupt pipe if an output interrupt endpoint
    508      1.25     skrll 	 * exists.
    509      1.25     skrll 	 */
    510      1.25     skrll 	if (sc->sc_oep_addr != -1) {
    511      1.25     skrll 		DPRINTF(("uhidev_open: oep=0x%02x\n", sc->sc_oep_addr));
    512      1.25     skrll 
    513      1.25     skrll 		err = usbd_open_pipe(sc->sc_iface, sc->sc_oep_addr,
    514      1.25     skrll 		    0, &sc->sc_opipe);
    515      1.25     skrll 
    516      1.25     skrll 		if (err != USBD_NORMAL_COMPLETION) {
    517      1.25     skrll 			DPRINTF(("uhidev_open: usbd_open_pipe failed, "
    518      1.25     skrll 			    "error=%d\n", err));
    519      1.25     skrll 			error = EIO;
    520      1.25     skrll 			goto out2;
    521      1.25     skrll 		}
    522      1.25     skrll 		DPRINTF(("uhidev_open: sc->sc_opipe=%p\n", sc->sc_opipe));
    523      1.25     skrll 
    524      1.25     skrll 		sc->sc_oxfer = usbd_alloc_xfer(sc->sc_udev);
    525      1.25     skrll 		if (sc->sc_oxfer == NULL) {
    526      1.25     skrll 			DPRINTF(("uhidev_open: couldn't allocate an xfer\n"));
    527      1.25     skrll 			error = ENOMEM;
    528      1.25     skrll 			goto out3;
    529      1.25     skrll 		}
    530       1.1  augustss 	}
    531      1.25     skrll 
    532       1.1  augustss 	return (0);
    533      1.25     skrll out3:
    534      1.25     skrll 	/* Abort output pipe */
    535      1.25     skrll 	usbd_close_pipe(sc->sc_opipe);
    536      1.25     skrll out2:
    537      1.25     skrll 	/* Abort input pipe */
    538      1.25     skrll 	usbd_close_pipe(sc->sc_ipipe);
    539      1.25     skrll out1:
    540      1.25     skrll 	DPRINTF(("uhidev_open: failed in someway"));
    541      1.25     skrll 	free(sc->sc_ibuf, M_USBDEV);
    542      1.25     skrll 	scd->sc_state &= ~UHIDEV_OPEN;
    543      1.25     skrll 	sc->sc_refcnt = 0;
    544      1.25     skrll 	sc->sc_ipipe = NULL;
    545      1.25     skrll 	sc->sc_opipe = NULL;
    546      1.25     skrll 	sc->sc_oxfer = NULL;
    547      1.25     skrll 	return error;
    548       1.1  augustss }
    549       1.1  augustss 
    550       1.1  augustss void
    551       1.1  augustss uhidev_close(struct uhidev *scd)
    552       1.1  augustss {
    553       1.1  augustss 	struct uhidev_softc *sc = scd->sc_parent;
    554       1.1  augustss 
    555       1.1  augustss 	if (!(scd->sc_state & UHIDEV_OPEN))
    556       1.1  augustss 		return;
    557       1.1  augustss 	scd->sc_state &= ~UHIDEV_OPEN;
    558       1.1  augustss 	if (--sc->sc_refcnt)
    559       1.1  augustss 		return;
    560       1.1  augustss 	DPRINTF(("uhidev_close: close pipe\n"));
    561       1.1  augustss 
    562      1.25     skrll 	if (sc->sc_oxfer != NULL)
    563      1.25     skrll 		usbd_free_xfer(sc->sc_oxfer);
    564      1.25     skrll 
    565       1.1  augustss 	/* Disable interrupts. */
    566      1.25     skrll 	if (sc->sc_opipe != NULL) {
    567      1.25     skrll 		usbd_abort_pipe(sc->sc_opipe);
    568      1.25     skrll 		usbd_close_pipe(sc->sc_opipe);
    569      1.25     skrll 		sc->sc_opipe = NULL;
    570      1.25     skrll 	}
    571      1.25     skrll 
    572      1.25     skrll 	if (sc->sc_ipipe != NULL) {
    573      1.25     skrll 		usbd_abort_pipe(sc->sc_ipipe);
    574      1.25     skrll 		usbd_close_pipe(sc->sc_ipipe);
    575      1.25     skrll 		sc->sc_ipipe = NULL;
    576       1.1  augustss 	}
    577       1.1  augustss 
    578       1.1  augustss 	if (sc->sc_ibuf != NULL) {
    579       1.1  augustss 		free(sc->sc_ibuf, M_USBDEV);
    580       1.1  augustss 		sc->sc_ibuf = NULL;
    581       1.1  augustss 	}
    582       1.1  augustss }
    583       1.1  augustss 
    584       1.1  augustss usbd_status
    585       1.1  augustss uhidev_set_report(struct uhidev *scd, int type, void *data, int len)
    586       1.1  augustss {
    587      1.13   dsainty 	char *buf;
    588      1.13   dsainty 	usbd_status retstat;
    589      1.13   dsainty 
    590      1.13   dsainty 	if (scd->sc_report_id == 0)
    591      1.13   dsainty 		return usbd_set_report(scd->sc_parent->sc_iface, type,
    592      1.13   dsainty 				       scd->sc_report_id, data, len);
    593      1.13   dsainty 
    594      1.13   dsainty 	buf = malloc(len + 1, M_TEMP, M_WAITOK);
    595      1.13   dsainty 	buf[0] = scd->sc_report_id;
    596      1.13   dsainty 	memcpy(buf+1, data, len);
    597      1.13   dsainty 
    598      1.13   dsainty 	retstat = usbd_set_report(scd->sc_parent->sc_iface, type,
    599      1.18   dsainty 				  scd->sc_report_id, buf, len + 1);
    600      1.13   dsainty 
    601      1.13   dsainty 	free(buf, M_TEMP);
    602       1.1  augustss 
    603      1.13   dsainty 	return retstat;
    604       1.1  augustss }
    605       1.1  augustss 
    606       1.1  augustss void
    607       1.1  augustss uhidev_set_report_async(struct uhidev *scd, int type, void *data, int len)
    608       1.1  augustss {
    609       1.1  augustss 	/* XXX */
    610       1.1  augustss 	char buf[100];
    611       1.1  augustss 	if (scd->sc_report_id) {
    612       1.1  augustss 		buf[0] = scd->sc_report_id;
    613       1.1  augustss 		memcpy(buf+1, data, len);
    614       1.1  augustss 		len++;
    615       1.1  augustss 		data = buf;
    616       1.1  augustss 	}
    617       1.1  augustss 
    618       1.6  augustss 	usbd_set_report_async(scd->sc_parent->sc_iface, type,
    619       1.1  augustss 			      scd->sc_report_id, data, len);
    620       1.1  augustss }
    621       1.1  augustss 
    622       1.1  augustss usbd_status
    623       1.1  augustss uhidev_get_report(struct uhidev *scd, int type, void *data, int len)
    624       1.1  augustss {
    625       1.6  augustss 	return usbd_get_report(scd->sc_parent->sc_iface, type,
    626       1.1  augustss 			       scd->sc_report_id, data, len);
    627       1.1  augustss }
    628      1.25     skrll 
    629      1.25     skrll usbd_status
    630      1.25     skrll uhidev_write(struct uhidev_softc *sc, void *data, int len)
    631      1.25     skrll {
    632      1.25     skrll 
    633      1.25     skrll 	DPRINTF(("uhidev_write: data=%p, len=%d\n", data, len));
    634      1.25     skrll 
    635      1.25     skrll 	if (sc->sc_opipe == NULL)
    636      1.25     skrll 		return USBD_INVAL;
    637      1.25     skrll 
    638      1.25     skrll #ifdef UHIDEV_DEBUG
    639      1.25     skrll 	if (uhidevdebug > 50) {
    640      1.25     skrll 
    641      1.25     skrll 		u_int32_t i;
    642      1.25     skrll 		u_int8_t *d = data;
    643      1.25     skrll 
    644      1.25     skrll 		DPRINTF(("uhidev_write: data ="));
    645      1.25     skrll 		for (i = 0; i < len; i++)
    646      1.25     skrll 			DPRINTF((" %02x", d[i]));
    647      1.25     skrll 		DPRINTF(("\n"));
    648      1.25     skrll 	}
    649      1.25     skrll #endif
    650      1.25     skrll 	return usbd_intr_transfer(sc->sc_oxfer, sc->sc_opipe, 0,
    651      1.25     skrll 	    USBD_NO_TIMEOUT, data, &len, "uhidevwi");
    652      1.25     skrll }
    653