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