xref: /openbsd-src/sys/dev/usb/uhidev.c (revision 4e1ee0786f11cc571bd0be17d38e46f635c719fc)
1 /*	$OpenBSD: uhidev.c,v 1.95 2021/09/12 06:58:08 anton Exp $	*/
2 /*	$NetBSD: uhidev.c,v 1.14 2003/03/11 16:44:00 augustss Exp $	*/
3 
4 /*
5  * Copyright (c) 2001 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Lennart Augustsson (lennart@augustsson.net) at
10  * Carlstedt Research & Technology.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * HID spec: https://www.usb.org/sites/default/files/hid1_11.pdf
36  */
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/signalvar.h>
43 #include <sys/device.h>
44 #include <sys/ioctl.h>
45 #include <sys/conf.h>
46 
47 #include <machine/bus.h>
48 
49 #include <dev/usb/usb.h>
50 #include <dev/usb/usbhid.h>
51 
52 #include <dev/usb/usbdevs.h>
53 #include <dev/usb/usbdi.h>
54 #include <dev/usb/usbdi_util.h>
55 #include <dev/usb/usbdivar.h>
56 #include <dev/usb/usb_mem.h>
57 #include <dev/usb/usb_quirks.h>
58 
59 #include <dev/usb/uhidev.h>
60 
61 #ifndef SMALL_KERNEL
62 /* Replacement report descriptors for devices shipped with broken ones */
63 #include <dev/usb/uhid_rdesc.h>
64 int uhidev_use_rdesc(struct uhidev_softc *, usb_interface_descriptor_t *,
65 		int, int, void **, int *);
66 #define UISUBCLASS_XBOX360_CONTROLLER 0x5d
67 #define UIPROTO_XBOX360_GAMEPAD 0x01
68 #endif /* !SMALL_KERNEL */
69 
70 #define DEVNAME(sc)		((sc)->sc_dev.dv_xname)
71 
72 #ifdef UHIDEV_DEBUG
73 #define DPRINTF(x)	do { if (uhidevdebug) printf x; } while (0)
74 #define DPRINTFN(n,x)	do { if (uhidevdebug>(n)) printf x; } while (0)
75 int	uhidevdebug = 0;
76 #else
77 #define DPRINTF(x)
78 #define DPRINTFN(n,x)
79 #endif
80 
81 struct uhidev_async_info {
82 	void (*callback)(void *priv, int id, void *data, int len);
83 	void *priv;
84 	void *data;
85 	int id;
86 };
87 
88 void uhidev_intr(struct usbd_xfer *, void *, usbd_status);
89 
90 int uhidev_maxrepid(void *buf, int len);
91 int uhidevprint(void *aux, const char *pnp);
92 int uhidevsubmatch(struct device *parent, void *cf, void *aux);
93 
94 int uhidev_match(struct device *, void *, void *);
95 void uhidev_attach(struct device *, struct device *, void *);
96 int uhidev_detach(struct device *, int);
97 int uhidev_activate(struct device *, int);
98 
99 void uhidev_get_report_async_cb(struct usbd_xfer *, void *, usbd_status);
100 void uhidev_set_report_async_cb(struct usbd_xfer *, void *, usbd_status);
101 
102 struct cfdriver uhidev_cd = {
103 	NULL, "uhidev", DV_DULL
104 };
105 
106 const struct cfattach uhidev_ca = {
107 	sizeof(struct uhidev_softc), uhidev_match, uhidev_attach,
108 	uhidev_detach, uhidev_activate,
109 };
110 
111 int
112 uhidev_match(struct device *parent, void *match, void *aux)
113 {
114 	struct usb_attach_arg *uaa = aux;
115 	usb_interface_descriptor_t *id;
116 
117 	if (uaa->iface == NULL)
118 		return (UMATCH_NONE);
119 	id = usbd_get_interface_descriptor(uaa->iface);
120 	if (id == NULL)
121 		return (UMATCH_NONE);
122 #ifndef SMALL_KERNEL
123 	if (id->bInterfaceClass == UICLASS_VENDOR &&
124 	    id->bInterfaceSubClass == UISUBCLASS_XBOX360_CONTROLLER &&
125 	    id->bInterfaceProtocol == UIPROTO_XBOX360_GAMEPAD)
126 		return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
127 #endif /* !SMALL_KERNEL */
128 	if (id->bInterfaceClass != UICLASS_HID)
129 		return (UMATCH_NONE);
130 	if (usbd_get_quirks(uaa->device)->uq_flags & UQ_BAD_HID)
131 		return (UMATCH_NONE);
132 
133 	return (UMATCH_IFACECLASS_GENERIC);
134 }
135 
136 void
137 uhidev_attach(struct device *parent, struct device *self, void *aux)
138 {
139 	struct uhidev_softc *sc = (struct uhidev_softc *)self;
140 	struct usb_attach_arg *uaa = aux;
141 	usb_interface_descriptor_t *id;
142 	usb_endpoint_descriptor_t *ed;
143 	struct uhidev_attach_arg uha;
144 	int size, nrepid, repid, repsz;
145 	int i;
146 	void *desc = NULL;
147 	struct device *dev;
148 
149 	sc->sc_udev = uaa->device;
150 	sc->sc_iface = uaa->iface;
151 	sc->sc_ifaceno = uaa->ifaceno;
152 	id = usbd_get_interface_descriptor(sc->sc_iface);
153 
154 	sc->sc_iep_addr = sc->sc_oep_addr = -1;
155 	for (i = 0; i < id->bNumEndpoints; i++) {
156 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
157 		if (ed == NULL) {
158 			printf("%s: could not read endpoint descriptor\n",
159 			    DEVNAME(sc));
160 			return;
161 		}
162 
163 		DPRINTFN(10,("uhidev_attach: bLength=%d bDescriptorType=%d "
164 		    "bEndpointAddress=%d-%s bmAttributes=%d wMaxPacketSize=%d"
165 		    " bInterval=%d\n",
166 		    ed->bLength, ed->bDescriptorType,
167 		    ed->bEndpointAddress & UE_ADDR,
168 		    UE_GET_DIR(ed->bEndpointAddress)==UE_DIR_IN? "in" : "out",
169 		    UE_GET_XFERTYPE(ed->bmAttributes),
170 		    UGETW(ed->wMaxPacketSize), ed->bInterval));
171 
172 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
173 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
174 			sc->sc_iep_addr = ed->bEndpointAddress;
175 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
176 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
177 			sc->sc_oep_addr = ed->bEndpointAddress;
178 		} else {
179 			printf("%s: unexpected endpoint\n", DEVNAME(sc));
180 			return;
181 		}
182 	}
183 
184 	/*
185 	 * Check that we found an input interrupt endpoint.
186 	 * The output interrupt endpoint is optional
187 	 */
188 	if (sc->sc_iep_addr == -1) {
189 		printf("%s: no input interrupt endpoint\n", DEVNAME(sc));
190 		return;
191 	}
192 
193 #ifndef SMALL_KERNEL
194 	if (uhidev_use_rdesc(sc, id, uaa->vendor, uaa->product, &desc, &size))
195 		return;
196 #endif /* !SMALL_KERNEL */
197 
198 	if (desc == NULL) {
199 		struct usb_hid_descriptor *hid;
200 
201 		hid = usbd_get_hid_descriptor(sc->sc_udev, id);
202 		if (hid == NULL) {
203 			printf("%s: no HID descriptor\n", DEVNAME(sc));
204 			return;
205 		}
206 		size = UGETW(hid->descrs[0].wDescriptorLength);
207 		desc = malloc(size, M_USBDEV, M_NOWAIT);
208 		if (desc == NULL) {
209 			printf("%s: no memory\n", DEVNAME(sc));
210 			return;
211 		}
212 		if (usbd_get_report_descriptor(sc->sc_udev, sc->sc_ifaceno,
213 		    desc, size)) {
214 			printf("%s: no report descriptor\n", DEVNAME(sc));
215 			free(desc, M_USBDEV, size);
216 			return;
217 		}
218 	}
219 
220 	sc->sc_repdesc = desc;
221 	sc->sc_repdesc_size = size;
222 
223 	nrepid = uhidev_maxrepid(desc, size);
224 	if (nrepid < 0)
225 		return;
226 	printf("%s: iclass %d/%d", DEVNAME(sc), id->bInterfaceClass,
227 	    id->bInterfaceSubClass);
228 	if (nrepid > 0)
229 		printf(", %d report id%s", nrepid, nrepid > 1 ? "s" : "");
230 	printf("\n");
231 	nrepid++;
232 	sc->sc_subdevs = mallocarray(nrepid, sizeof(struct uhidev *),
233 	    M_USBDEV, M_NOWAIT | M_ZERO);
234 	if (sc->sc_subdevs == NULL) {
235 		printf("%s: no memory\n", DEVNAME(sc));
236 		return;
237 	}
238 	sc->sc_nrepid = nrepid;
239 	sc->sc_isize = 0;
240 
241 	for (repid = 0; repid < nrepid; repid++) {
242 		repsz = hid_report_size(desc, size, hid_input, repid);
243 		DPRINTF(("uhidev_match: repid=%d, repsz=%d\n", repid, repsz));
244 		if (repsz > sc->sc_isize)
245 			sc->sc_isize = repsz;
246 	}
247 	sc->sc_isize += (nrepid != 1);	/* one byte for the report ID */
248 	DPRINTF(("uhidev_attach: isize=%d\n", sc->sc_isize));
249 
250 	uha.uaa = uaa;
251 	uha.parent = sc;
252 	uha.reportid = UHIDEV_CLAIM_MULTIPLE_REPORTID;
253 	uha.nreports = nrepid;
254 	uha.claimed = malloc(nrepid, M_TEMP, M_WAITOK|M_ZERO);
255 
256 	/* Look for a driver claiming multiple report IDs first. */
257 	dev = config_found_sm(self, &uha, NULL, uhidevsubmatch);
258 	if (dev != NULL) {
259 		for (repid = 0; repid < nrepid; repid++) {
260 			/*
261 			 * Could already be assigned by uhidev_set_report_dev().
262 			 */
263 			if (sc->sc_subdevs[repid] != NULL)
264 				continue;
265 
266 			if (uha.claimed[repid])
267 				sc->sc_subdevs[repid] = (struct uhidev *)dev;
268 		}
269 	}
270 
271 	free(uha.claimed, M_TEMP, nrepid);
272 	uha.claimed = NULL;
273 
274 	for (repid = 0; repid < nrepid; repid++) {
275 		DPRINTF(("%s: try repid=%d\n", __func__, repid));
276 		if (hid_report_size(desc, size, hid_input, repid) == 0 &&
277 		    hid_report_size(desc, size, hid_output, repid) == 0 &&
278 		    hid_report_size(desc, size, hid_feature, repid) == 0)
279 			continue;
280 
281 		/* Could already be assigned by uhidev_set_report_dev(). */
282 		if (sc->sc_subdevs[repid] != NULL)
283 			continue;
284 
285 		uha.reportid = repid;
286 		dev = config_found_sm(self, &uha, uhidevprint, uhidevsubmatch);
287 		sc->sc_subdevs[repid] = (struct uhidev *)dev;
288 	}
289 }
290 
291 #ifndef SMALL_KERNEL
292 int
293 uhidev_use_rdesc(struct uhidev_softc *sc, usb_interface_descriptor_t *id,
294 		int vendor, int product, void **descp, int *sizep)
295 {
296 	static uByte reportbuf[] = {2, 2};
297 	const void *descptr = NULL;
298 	void *desc;
299 	int size;
300 
301 	if (vendor == USB_VENDOR_WACOM) {
302 		/* The report descriptor for the Wacom Graphire is broken. */
303 		switch (product) {
304 		case USB_PRODUCT_WACOM_GRAPHIRE:
305 			size = sizeof(uhid_graphire_report_descr);
306 			descptr = uhid_graphire_report_descr;
307 			break;
308 		case USB_PRODUCT_WACOM_GRAPHIRE3_4X5:
309 		case USB_PRODUCT_WACOM_GRAPHIRE4_4X5:
310 			uhidev_set_report(sc, UHID_FEATURE_REPORT,
311 			    2, &reportbuf, sizeof(reportbuf));
312 			size = sizeof(uhid_graphire3_4x5_report_descr);
313 			descptr = uhid_graphire3_4x5_report_descr;
314 			break;
315 		default:
316 			break;
317 		}
318 	} else if ((id->bInterfaceClass == UICLASS_VENDOR &&
319 		   id->bInterfaceSubClass == UISUBCLASS_XBOX360_CONTROLLER &&
320 		   id->bInterfaceProtocol == UIPROTO_XBOX360_GAMEPAD)) {
321 		/* The Xbox 360 gamepad has no report descriptor. */
322 		size = sizeof(uhid_xb360gp_report_descr);
323 		descptr = uhid_xb360gp_report_descr;
324 	}
325 
326 	if (descptr) {
327 		desc = malloc(size, M_USBDEV, M_NOWAIT);
328 		if (desc == NULL)
329 			return (ENOMEM);
330 
331 		memcpy(desc, descptr, size);
332 
333 		*descp = desc;
334 		*sizep = size;
335 	}
336 
337 	return (0);
338 }
339 #endif /* !SMALL_KERNEL */
340 
341 int
342 uhidev_maxrepid(void *buf, int len)
343 {
344 	struct hid_data *d;
345 	struct hid_item h;
346 	int maxid;
347 
348 	maxid = -1;
349 	h.report_ID = 0;
350 	for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); )
351 		if (h.report_ID > maxid)
352 			maxid = h.report_ID;
353 	hid_end_parse(d);
354 	return (maxid);
355 }
356 
357 int
358 uhidevprint(void *aux, const char *pnp)
359 {
360 	struct uhidev_attach_arg *uha = aux;
361 
362 	if (pnp)
363 		printf("uhid at %s", pnp);
364 	if (uha->reportid != 0 && uha->reportid != UHIDEV_CLAIM_MULTIPLE_REPORTID)
365 		printf(" reportid %d", uha->reportid);
366 	return (UNCONF);
367 }
368 
369 int uhidevsubmatch(struct device *parent, void *match, void *aux)
370 {
371 	struct uhidev_attach_arg *uha = aux;
372         struct cfdata *cf = match;
373 
374 	if (cf->uhidevcf_reportid != UHIDEV_UNK_REPORTID &&
375 	    cf->uhidevcf_reportid != uha->reportid)
376 		return (0);
377 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
378 }
379 
380 int
381 uhidev_activate(struct device *self, int act)
382 {
383 	struct uhidev_softc *sc = (struct uhidev_softc *)self;
384 	int i, j, already, rv = 0, r;
385 
386 	switch (act) {
387 	case DVACT_DEACTIVATE:
388 		for (i = 0; i < sc->sc_nrepid; i++) {
389 			if (sc->sc_subdevs[i] == NULL)
390 				continue;
391 
392 			/*
393 			 * Only notify devices attached to multiple report ids
394 			 * once.
395 			 */
396 			for (already = 0, j = 0; j < i; j++) {
397 				if (sc->sc_subdevs[i] == sc->sc_subdevs[j]) {
398 					already = 1;
399 					break;
400 				}
401 			}
402 
403 			if (!already) {
404 				r = config_deactivate(
405 				    &sc->sc_subdevs[i]->sc_dev);
406 				if (r && r != EOPNOTSUPP)
407 					rv = r;
408 			}
409 		}
410 		usbd_deactivate(sc->sc_udev);
411 		break;
412 	}
413 	return (rv);
414 }
415 
416 int
417 uhidev_detach(struct device *self, int flags)
418 {
419 	struct uhidev_softc *sc = (struct uhidev_softc *)self;
420 	int i, j, rv = 0;
421 
422 	DPRINTF(("uhidev_detach: sc=%p flags=%d\n", sc, flags));
423 
424 	if (sc->sc_opipe != NULL) {
425 		usbd_close_pipe(sc->sc_opipe);
426 		sc->sc_opipe = NULL;
427 	}
428 
429 	if (sc->sc_ipipe != NULL) {
430 		usbd_close_pipe(sc->sc_ipipe);
431 		sc->sc_ipipe = NULL;
432 	}
433 
434 	if (sc->sc_repdesc != NULL)
435 		free(sc->sc_repdesc, M_USBDEV, sc->sc_repdesc_size);
436 
437 	for (i = 0; i < sc->sc_nrepid; i++) {
438 		if (sc->sc_subdevs[i] == NULL)
439 			continue;
440 
441 		rv |= config_detach(&sc->sc_subdevs[i]->sc_dev, flags);
442 
443 		/*
444 		 * Nullify without detaching any other instances of this device
445 		 * found on other report ids.
446 		 */
447 		for (j = i + 1; j < sc->sc_nrepid; j++) {
448 			if (sc->sc_subdevs[i] == sc->sc_subdevs[j])
449 				sc->sc_subdevs[j] = NULL;
450 		}
451 
452 		sc->sc_subdevs[i] = NULL;
453 	}
454 
455 	return (rv);
456 }
457 
458 void
459 uhidev_intr(struct usbd_xfer *xfer, void *addr, usbd_status status)
460 {
461 	struct uhidev_softc *sc = addr;
462 	struct uhidev *scd;
463 	u_char *p;
464 	u_int rep;
465 	u_int32_t cc;
466 
467 	if (usbd_is_dying(sc->sc_udev))
468 		return;
469 
470 	usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
471 
472 #ifdef UHIDEV_DEBUG
473 	if (uhidevdebug > 5) {
474 		u_int32_t i;
475 
476 		DPRINTF(("uhidev_intr: status=%d cc=%d\n", status, cc));
477 		DPRINTF(("uhidev_intr: data ="));
478 		for (i = 0; i < cc; i++)
479 			DPRINTF((" %02x", sc->sc_ibuf[i]));
480 		DPRINTF(("\n"));
481 	}
482 #endif
483 
484 	if (status == USBD_CANCELLED || status == USBD_IOERROR)
485 		return;
486 
487 	if (status != USBD_NORMAL_COMPLETION) {
488 		DPRINTF(("%s: interrupt status=%d\n", DEVNAME(sc), status));
489 		usbd_clear_endpoint_stall_async(sc->sc_ipipe);
490 		return;
491 	}
492 
493 	p = sc->sc_ibuf;
494 	if (sc->sc_nrepid != 1)
495 		rep = *p++, cc--;
496 	else
497 		rep = 0;
498 	if (rep >= sc->sc_nrepid) {
499 		printf("uhidev_intr: bad repid %d\n", rep);
500 		return;
501 	}
502 	scd = sc->sc_subdevs[rep];
503 	DPRINTFN(5,("uhidev_intr: rep=%d, scd=%p state=0x%x\n",
504 		    rep, scd, scd ? scd->sc_state : 0));
505 	if (scd == NULL || !(scd->sc_state & UHIDEV_OPEN))
506 		return;
507 
508 	scd->sc_intr(scd, p, cc);
509 }
510 
511 void
512 uhidev_get_report_desc(struct uhidev_softc *sc, void **desc, int *size)
513 {
514 	*desc = sc->sc_repdesc;
515 	*size = sc->sc_repdesc_size;
516 }
517 
518 int
519 uhidev_open(struct uhidev *scd)
520 {
521 	struct uhidev_softc *sc = scd->sc_parent;
522 	usbd_status err;
523 	int error;
524 
525 	DPRINTF(("uhidev_open: open pipe, state=%d refcnt=%d\n",
526 		 scd->sc_state, sc->sc_refcnt));
527 
528 	if (scd->sc_state & UHIDEV_OPEN)
529 		return (EBUSY);
530 	scd->sc_state |= UHIDEV_OPEN;
531 	if (sc->sc_refcnt++)
532 		return (0);
533 
534 	if (sc->sc_isize == 0)
535 		return (0);
536 
537 	sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
538 
539 	/* Set up input interrupt pipe. */
540 	DPRINTF(("uhidev_open: isize=%d, ep=0x%02x\n", sc->sc_isize,
541 	    sc->sc_iep_addr));
542 
543 	err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_iep_addr,
544 		  USBD_SHORT_XFER_OK, &sc->sc_ipipe, sc, sc->sc_ibuf,
545 		  sc->sc_isize, uhidev_intr, USBD_DEFAULT_INTERVAL);
546 	if (err != USBD_NORMAL_COMPLETION) {
547 		DPRINTF(("uhidopen: usbd_open_pipe_intr failed, "
548 		    "error=%d\n", err));
549 		error = EIO;
550 		goto out1;
551 	}
552 
553 	DPRINTF(("uhidev_open: sc->sc_ipipe=%p\n", sc->sc_ipipe));
554 
555 	sc->sc_ixfer = usbd_alloc_xfer(sc->sc_udev);
556 	if (sc->sc_ixfer == NULL) {
557 		DPRINTF(("uhidev_open: couldn't allocate an xfer\n"));
558 		error = ENOMEM;
559 		goto out1; // xxxx
560 	}
561 
562 	/*
563 	 * Set up output interrupt pipe if an output interrupt endpoint
564 	 * exists.
565 	 */
566 	if (sc->sc_oep_addr != -1) {
567 		DPRINTF(("uhidev_open: oep=0x%02x\n", sc->sc_oep_addr));
568 
569 		err = usbd_open_pipe(sc->sc_iface, sc->sc_oep_addr,
570 		    0, &sc->sc_opipe);
571 		if (err != USBD_NORMAL_COMPLETION) {
572 			DPRINTF(("uhidev_open: usbd_open_pipe failed, "
573 			    "error=%d\n", err));
574 			error = EIO;
575 			goto out2;
576 		}
577 
578 		DPRINTF(("uhidev_open: sc->sc_opipe=%p\n", sc->sc_opipe));
579 
580 		sc->sc_oxfer = usbd_alloc_xfer(sc->sc_udev);
581 		if (sc->sc_oxfer == NULL) {
582 			DPRINTF(("uhidev_open: couldn't allocate an xfer\n"));
583 			error = ENOMEM;
584 			goto out3;
585 		}
586 
587 		sc->sc_owxfer = usbd_alloc_xfer(sc->sc_udev);
588 		if (sc->sc_owxfer == NULL) {
589 			DPRINTF(("uhidev_open: couldn't allocate owxfer\n"));
590 			error = ENOMEM;
591 			goto out3;
592 		}
593 	}
594 
595 	return (0);
596 
597 out3:
598 	/* Abort output pipe */
599 	usbd_close_pipe(sc->sc_opipe);
600 out2:
601 	/* Abort input pipe */
602 	usbd_close_pipe(sc->sc_ipipe);
603 out1:
604 	DPRINTF(("uhidev_open: failed in someway"));
605 	free(sc->sc_ibuf, M_USBDEV, sc->sc_isize);
606 	scd->sc_state &= ~UHIDEV_OPEN;
607 	sc->sc_refcnt = 0;
608 	sc->sc_ipipe = NULL;
609 	sc->sc_opipe = NULL;
610 	if (sc->sc_oxfer != NULL) {
611 		usbd_free_xfer(sc->sc_oxfer);
612 		sc->sc_oxfer = NULL;
613 	}
614 	if (sc->sc_owxfer != NULL) {
615 		usbd_free_xfer(sc->sc_owxfer);
616 		sc->sc_owxfer = NULL;
617 	}
618 	if (sc->sc_ixfer != NULL) {
619 		usbd_free_xfer(sc->sc_ixfer);
620 		sc->sc_ixfer = NULL;
621 	}
622 	return (error);
623 }
624 
625 void
626 uhidev_close(struct uhidev *scd)
627 {
628 	struct uhidev_softc *sc = scd->sc_parent;
629 
630 	if (!(scd->sc_state & UHIDEV_OPEN))
631 		return;
632 	scd->sc_state &= ~UHIDEV_OPEN;
633 	if (--sc->sc_refcnt)
634 		return;
635 	DPRINTF(("uhidev_close: close pipe\n"));
636 
637 	/* Disable interrupts. */
638 	if (sc->sc_opipe != NULL) {
639 		usbd_close_pipe(sc->sc_opipe);
640 		sc->sc_opipe = NULL;
641 	}
642 
643 	if (sc->sc_ipipe != NULL) {
644 		usbd_close_pipe(sc->sc_ipipe);
645 		sc->sc_ipipe = NULL;
646 	}
647 
648 	if (sc->sc_oxfer != NULL) {
649 		usbd_free_xfer(sc->sc_oxfer);
650 		sc->sc_oxfer = NULL;
651 	}
652 
653 	if (sc->sc_owxfer != NULL) {
654 		usbd_free_xfer(sc->sc_owxfer);
655 		sc->sc_owxfer = NULL;
656 	}
657 
658 	if (sc->sc_ixfer != NULL) {
659 		usbd_free_xfer(sc->sc_ixfer);
660 		sc->sc_ixfer = NULL;
661 	}
662 
663 	if (sc->sc_ibuf != NULL) {
664 		free(sc->sc_ibuf, M_USBDEV, sc->sc_isize);
665 		sc->sc_ibuf = NULL;
666 	}
667 }
668 
669 int
670 uhidev_report_type_conv(int hid_type_id)
671 {
672 	switch (hid_type_id) {
673 	case hid_input:
674 		return UHID_INPUT_REPORT;
675 	case hid_output:
676 		return UHID_OUTPUT_REPORT;
677 	case hid_feature:
678 		return UHID_FEATURE_REPORT;
679 	default:
680 		return -1;
681 	}
682 }
683 
684 int
685 uhidev_set_report(struct uhidev_softc *sc, int type, int id, void *data,
686     int len)
687 {
688 	usb_device_request_t req;
689 	char *buf = data;
690 	int actlen = len;
691 
692 	/* Prepend the reportID. */
693 	if (id > 0) {
694 		len++;
695 		buf = malloc(len, M_TEMP, M_WAITOK);
696 		buf[0] = id;
697 		memcpy(buf + 1, data, len - 1);
698 	}
699 
700 	if (sc->sc_opipe != NULL) {
701 		usbd_setup_xfer(sc->sc_owxfer, sc->sc_opipe, 0, buf, len,
702 		    USBD_SYNCHRONOUS | USBD_CATCH, 0, NULL);
703 		if (usbd_transfer(sc->sc_owxfer)) {
704 			usbd_clear_endpoint_stall(sc->sc_opipe);
705 			actlen = -1;
706 		}
707 	} else {
708 		req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
709 		req.bRequest = UR_SET_REPORT;
710 		USETW2(req.wValue, type, id);
711 		USETW(req.wIndex, sc->sc_ifaceno);
712 		USETW(req.wLength, len);
713 
714 		if (usbd_do_request(sc->sc_udev, &req, buf))
715 			actlen = -1;
716 	}
717 
718 	if (id > 0)
719 		free(buf, M_TEMP, len);
720 
721 	return (actlen);
722 }
723 
724 void
725 uhidev_set_report_async_cb(struct usbd_xfer *xfer, void *priv, usbd_status err)
726 {
727 	struct uhidev_softc *sc = priv;
728 
729 	if (err == USBD_STALLED)
730 		usbd_clear_endpoint_stall_async(sc->sc_opipe);
731 	usbd_free_xfer(xfer);
732 }
733 
734 int
735 uhidev_set_report_async(struct uhidev_softc *sc, int type, int id, void *data,
736     int len)
737 {
738 	struct usbd_xfer *xfer;
739 	usb_device_request_t req;
740 	int actlen = len;
741 	char *buf;
742 
743 	xfer = usbd_alloc_xfer(sc->sc_udev);
744 	if (xfer == NULL)
745 		return (-1);
746 
747 	if (id > 0)
748 		len++;
749 
750 	buf = usbd_alloc_buffer(xfer, len);
751 	if (buf == NULL) {
752 		usbd_free_xfer(xfer);
753 		return (-1);
754 	}
755 
756 	/* Prepend the reportID. */
757 	if (id > 0) {
758 		buf[0] = id;
759 		memcpy(buf + 1, data, len - 1);
760 	} else {
761 		memcpy(buf, data, len);
762 	}
763 
764 	if (sc->sc_opipe != NULL) {
765 		usbd_setup_xfer(xfer, sc->sc_opipe, sc, buf, len,
766 		    USBD_NO_COPY, USBD_DEFAULT_TIMEOUT,
767 		    uhidev_set_report_async_cb);
768 		if (usbd_transfer(xfer)) {
769 			usbd_clear_endpoint_stall_async(sc->sc_opipe);
770 			actlen = -1;
771 		}
772 	} else {
773 		req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
774 		req.bRequest = UR_SET_REPORT;
775 		USETW2(req.wValue, type, id);
776 		USETW(req.wIndex, sc->sc_ifaceno);
777 		USETW(req.wLength, len);
778 		if (usbd_request_async(xfer, &req, NULL, NULL))
779 			actlen = -1;
780 	}
781 
782 	return (actlen);
783 }
784 
785 int
786 uhidev_get_report(struct uhidev_softc *sc, int type, int id, void *data,
787     int len)
788 {
789 	usb_device_request_t req;
790 	char *buf = data;
791 	usbd_status err;
792 	int actlen;
793 
794 	if (id > 0) {
795 		len++;
796 		buf = malloc(len, M_TEMP, M_WAITOK|M_ZERO);
797 	}
798 
799 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
800 	req.bRequest = UR_GET_REPORT;
801 	USETW2(req.wValue, type, id);
802 	USETW(req.wIndex, sc->sc_ifaceno);
803 	USETW(req.wLength, len);
804 
805 	err = usbd_do_request_flags(sc->sc_udev, &req, buf, 0, &actlen,
806 	    USBD_DEFAULT_TIMEOUT);
807 	if (err != USBD_NORMAL_COMPLETION && err != USBD_SHORT_XFER)
808 		actlen = -1;
809 
810 	/* Skip the reportID. */
811 	if (id > 0) {
812 		memcpy(data, buf + 1, len - 1);
813 		free(buf, M_TEMP, len);
814 	}
815 
816 	return (actlen);
817 }
818 
819 void
820 uhidev_get_report_async_cb(struct usbd_xfer *xfer, void *priv, usbd_status err)
821 {
822 	struct uhidev_async_info *info = priv;
823 	char *buf;
824 	int len = -1;
825 
826 	if (!usbd_is_dying(xfer->pipe->device)) {
827 		if (err == USBD_NORMAL_COMPLETION || err == USBD_SHORT_XFER) {
828 			len = xfer->actlen;
829 			buf = KERNADDR(&xfer->dmabuf, 0);
830 			if (info->id > 0) {
831 				len--;
832 				memcpy(info->data, buf + 1, len);
833 			} else {
834 				memcpy(info->data, buf, len);
835 			}
836 		}
837 		info->callback(info->priv, info->id, info->data, len);
838 	}
839 	free(info, M_TEMP, sizeof(*info));
840 	usbd_free_xfer(xfer);
841 }
842 
843 int
844 uhidev_get_report_async(struct uhidev_softc *sc, int type, int id, void *data,
845     int len, void *priv, void (*callback)(void *, int, void *, int))
846 {
847 	struct usbd_xfer *xfer;
848 	usb_device_request_t req;
849 	struct uhidev_async_info *info;
850 	int actlen = len;
851 	char *buf;
852 
853 	xfer = usbd_alloc_xfer(sc->sc_udev);
854 	if (xfer == NULL)
855 		return (-1);
856 
857 	if (id > 0)
858 		len++;
859 
860 	buf = usbd_alloc_buffer(xfer, len);
861 	if (buf == NULL) {
862 		usbd_free_xfer(xfer);
863 		return (-1);
864 	}
865 
866 	info = malloc(sizeof(*info), M_TEMP, M_NOWAIT);
867 	if (info == NULL) {
868 		usbd_free_xfer(xfer);
869 		return (-1);
870 	}
871 
872 	info->callback = callback;
873 	info->priv = priv;
874 	info->data = data;
875 	info->id = id;
876 
877 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
878 	req.bRequest = UR_GET_REPORT;
879 	USETW2(req.wValue, type, id);
880 	USETW(req.wIndex, sc->sc_ifaceno);
881 	USETW(req.wLength, len);
882 
883 	if (usbd_request_async(xfer, &req, info, uhidev_get_report_async_cb)) {
884 		free(info, M_TEMP, sizeof(*info));
885 		actlen = -1;
886 	}
887 
888 	return (actlen);
889 }
890 
891 usbd_status
892 uhidev_write(struct uhidev_softc *sc, void *data, int len)
893 {
894 	usbd_status error;
895 
896 	DPRINTF(("uhidev_write: data=%p, len=%d\n", data, len));
897 
898 	if (sc->sc_opipe == NULL)
899 		return USBD_INVAL;
900 
901 #ifdef UHIDEV_DEBUG
902 	if (uhidevdebug > 50) {
903 
904 		u_int32_t i;
905 		u_int8_t *d = data;
906 
907 		DPRINTF(("uhidev_write: data ="));
908 		for (i = 0; i < len; i++)
909 			DPRINTF((" %02x", d[i]));
910 		DPRINTF(("\n"));
911 	}
912 #endif
913 	usbd_setup_xfer(sc->sc_owxfer, sc->sc_opipe, 0, data, len,
914 	    USBD_SYNCHRONOUS | USBD_CATCH, 0, NULL);
915 	error = usbd_transfer(sc->sc_owxfer);
916 	if (error)
917 		usbd_clear_endpoint_stall(sc->sc_opipe);
918 
919 	return (error);
920 }
921 
922 int
923 uhidev_ioctl(struct uhidev *sc, u_long cmd, caddr_t addr, int flag,
924     struct proc *p)
925 {
926 	struct usb_ctl_report_desc *rd;
927 	struct usb_ctl_report *re;
928 	int size;
929 	void *desc;
930 
931 	switch (cmd) {
932 	case USB_GET_REPORT_DESC:
933 		uhidev_get_report_desc(sc->sc_parent, &desc, &size);
934 		rd = (struct usb_ctl_report_desc *)addr;
935 		size = min(size, sizeof rd->ucrd_data);
936 		rd->ucrd_size = size;
937 		memcpy(rd->ucrd_data, desc, size);
938 		break;
939 	case USB_GET_REPORT:
940 		re = (struct usb_ctl_report *)addr;
941 		switch (re->ucr_report) {
942 		case UHID_INPUT_REPORT:
943 			size = sc->sc_isize;
944 			break;
945 		case UHID_OUTPUT_REPORT:
946 			size = sc->sc_osize;
947 			break;
948 		case UHID_FEATURE_REPORT:
949 			size = sc->sc_fsize;
950 			break;
951 		default:
952 			return EINVAL;
953 		}
954 		if (uhidev_get_report(sc->sc_parent, re->ucr_report,
955 		    sc->sc_report_id, re->ucr_data, size) != size)
956 			return EIO;
957 		break;
958 	case USB_SET_REPORT:
959 		re = (struct usb_ctl_report *)addr;
960 		switch (re->ucr_report) {
961 		case UHID_INPUT_REPORT:
962 			size = sc->sc_isize;
963 			break;
964 		case UHID_OUTPUT_REPORT:
965 			size = sc->sc_osize;
966 			break;
967 		case UHID_FEATURE_REPORT:
968 			size = sc->sc_fsize;
969 			break;
970 		default:
971 			return EINVAL;
972 		}
973 		if (uhidev_set_report(sc->sc_parent, re->ucr_report,
974 		    sc->sc_report_id, re->ucr_data, size) != size)
975 			return EIO;
976 		break;
977 	case USB_GET_REPORT_ID:
978 		*(int *)addr = sc->sc_report_id;
979 		break;
980 	default:
981 		return -1;
982 	}
983 	return 0;
984 }
985 
986 int
987 uhidev_set_report_dev(struct uhidev_softc *sc, struct uhidev *dev, int repid)
988 {
989 	if ((dev->sc_state & UHIDEV_OPEN) == 0)
990 		return ENODEV;
991 	if (repid >= sc->sc_nrepid)
992 		return EINVAL;
993 
994 	sc->sc_subdevs[repid] = dev;
995 	return 0;
996 }
997