xref: /freebsd/sys/dev/usb/input/uhid.c (revision b3aaa0cc21c63d388230c7ef2a80abd631ff20d5)
1 /*	$NetBSD: uhid.c,v 1.46 2001/11/13 06:24:55 lukem Exp $	*/
2 
3 /* Also already merged from NetBSD:
4  *	$NetBSD: uhid.c,v 1.54 2002/09/23 05:51:21 simonb Exp $
5  */
6 
7 #include <sys/cdefs.h>
8 __FBSDID("$FreeBSD$");
9 
10 /*-
11  * Copyright (c) 1998 The NetBSD Foundation, Inc.
12  * All rights reserved.
13  *
14  * This code is derived from software contributed to The NetBSD Foundation
15  * by Lennart Augustsson (lennart@augustsson.net) at
16  * Carlstedt Research & Technology.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  * 1. Redistributions of source code must retain the above copyright
22  *    notice, this list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must reproduce the above copyright
24  *    notice, this list of conditions and the following disclaimer in the
25  *    documentation and/or other materials provided with the distribution.
26  * 3. All advertising materials mentioning features or use of this software
27  *    must display the following acknowledgement:
28  *        This product includes software developed by the NetBSD
29  *        Foundation, Inc. and its contributors.
30  * 4. Neither the name of The NetBSD Foundation nor the names of its
31  *    contributors may be used to endorse or promote products derived
32  *    from this software without specific prior written permission.
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
35  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
36  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
37  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
38  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
39  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
40  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
42  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
43  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44  * POSSIBILITY OF SUCH DAMAGE.
45  */
46 
47 /*
48  * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
49  */
50 
51 #include "usbdevs.h"
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usb_mfunc.h>
54 #include <dev/usb/usb_error.h>
55 #include <dev/usb/usbhid.h>
56 #include <dev/usb/usb_ioctl.h>
57 
58 #define	USB_DEBUG_VAR uhid_debug
59 
60 #include <dev/usb/usb_core.h>
61 #include <dev/usb/usb_util.h>
62 #include <dev/usb/usb_debug.h>
63 #include <dev/usb/usb_busdma.h>
64 #include <dev/usb/usb_process.h>
65 #include <dev/usb/usb_transfer.h>
66 #include <dev/usb/usb_request.h>
67 #include <dev/usb/usb_dynamic.h>
68 #include <dev/usb/usb_mbuf.h>
69 #include <dev/usb/usb_dev.h>
70 #include <dev/usb/usb_hid.h>
71 
72 #include <dev/usb/input/usb_rdesc.h>
73 
74 #include <dev/usb/quirk/usb_quirk.h>
75 
76 #if USB_DEBUG
77 static int uhid_debug = 0;
78 
79 SYSCTL_NODE(_hw_usb2, OID_AUTO, uhid, CTLFLAG_RW, 0, "USB uhid");
80 SYSCTL_INT(_hw_usb2_uhid, OID_AUTO, debug, CTLFLAG_RW,
81     &uhid_debug, 0, "Debug level");
82 #endif
83 
84 #define	UHID_BSIZE	1024		/* bytes, buffer size */
85 #define	UHID_FRAME_NUM 	  50		/* bytes, frame number */
86 
87 enum {
88 	UHID_INTR_DT_RD,
89 	UHID_CTRL_DT_WR,
90 	UHID_CTRL_DT_RD,
91 	UHID_N_TRANSFER,
92 };
93 
94 struct uhid_softc {
95 	struct usb2_fifo_sc sc_fifo;
96 	struct mtx sc_mtx;
97 
98 	struct usb2_xfer *sc_xfer[UHID_N_TRANSFER];
99 	struct usb2_device *sc_udev;
100 	void   *sc_repdesc_ptr;
101 
102 	uint32_t sc_isize;
103 	uint32_t sc_osize;
104 	uint32_t sc_fsize;
105 
106 	uint16_t sc_repdesc_size;
107 
108 	uint8_t	sc_iface_no;
109 	uint8_t	sc_iface_index;
110 	uint8_t	sc_iid;
111 	uint8_t	sc_oid;
112 	uint8_t	sc_fid;
113 	uint8_t	sc_flags;
114 #define	UHID_FLAG_IMMED        0x01	/* set if read should be immediate */
115 #define	UHID_FLAG_STATIC_DESC  0x04	/* set if report descriptors are
116 					 * static */
117 };
118 
119 static const uint8_t uhid_xb360gp_report_descr[] = {UHID_XB360GP_REPORT_DESCR()};
120 static const uint8_t uhid_graphire_report_descr[] = {UHID_GRAPHIRE_REPORT_DESCR()};
121 static const uint8_t uhid_graphire3_4x5_report_descr[] = {UHID_GRAPHIRE3_4X5_REPORT_DESCR()};
122 
123 /* prototypes */
124 
125 static device_probe_t uhid_probe;
126 static device_attach_t uhid_attach;
127 static device_detach_t uhid_detach;
128 
129 static usb2_callback_t uhid_intr_callback;
130 static usb2_callback_t uhid_write_callback;
131 static usb2_callback_t uhid_read_callback;
132 
133 static usb2_fifo_cmd_t uhid_start_read;
134 static usb2_fifo_cmd_t uhid_stop_read;
135 static usb2_fifo_cmd_t uhid_start_write;
136 static usb2_fifo_cmd_t uhid_stop_write;
137 static usb2_fifo_open_t uhid_open;
138 static usb2_fifo_close_t uhid_close;
139 static usb2_fifo_ioctl_t uhid_ioctl;
140 
141 static struct usb2_fifo_methods uhid_fifo_methods = {
142 	.f_open = &uhid_open,
143 	.f_close = &uhid_close,
144 	.f_ioctl = &uhid_ioctl,
145 	.f_start_read = &uhid_start_read,
146 	.f_stop_read = &uhid_stop_read,
147 	.f_start_write = &uhid_start_write,
148 	.f_stop_write = &uhid_stop_write,
149 	.basename[0] = "uhid",
150 };
151 
152 static void
153 uhid_intr_callback(struct usb2_xfer *xfer)
154 {
155 	struct uhid_softc *sc = xfer->priv_sc;
156 
157 	switch (USB_GET_STATE(xfer)) {
158 	case USB_ST_TRANSFERRED:
159 		DPRINTF("transferred!\n");
160 
161 		if (xfer->actlen >= sc->sc_isize) {
162 			usb2_fifo_put_data(
163 			    sc->sc_fifo.fp[USB_FIFO_RX],
164 			    xfer->frbuffers,
165 			    0, sc->sc_isize, 1);
166 		} else {
167 			/* ignore it */
168 			DPRINTF("ignored short transfer, "
169 			    "%d bytes\n", xfer->actlen);
170 		}
171 
172 	case USB_ST_SETUP:
173 re_submit:
174 		if (usb2_fifo_put_bytes_max(
175 		    sc->sc_fifo.fp[USB_FIFO_RX]) != 0) {
176 			xfer->frlengths[0] = sc->sc_isize;
177 			usb2_start_hardware(xfer);
178 		}
179 		return;
180 
181 	default:			/* Error */
182 		if (xfer->error != USB_ERR_CANCELLED) {
183 			/* try to clear stall first */
184 			xfer->flags.stall_pipe = 1;
185 			goto re_submit;
186 		}
187 		return;
188 	}
189 }
190 
191 static void
192 uhid_fill_set_report(struct usb2_device_request *req, uint8_t iface_no,
193     uint8_t type, uint8_t id, uint16_t size)
194 {
195 	req->bmRequestType = UT_WRITE_CLASS_INTERFACE;
196 	req->bRequest = UR_SET_REPORT;
197 	USETW2(req->wValue, type, id);
198 	req->wIndex[0] = iface_no;
199 	req->wIndex[1] = 0;
200 	USETW(req->wLength, size);
201 }
202 
203 static void
204 uhid_fill_get_report(struct usb2_device_request *req, uint8_t iface_no,
205     uint8_t type, uint8_t id, uint16_t size)
206 {
207 	req->bmRequestType = UT_READ_CLASS_INTERFACE;
208 	req->bRequest = UR_GET_REPORT;
209 	USETW2(req->wValue, type, id);
210 	req->wIndex[0] = iface_no;
211 	req->wIndex[1] = 0;
212 	USETW(req->wLength, size);
213 }
214 
215 static void
216 uhid_write_callback(struct usb2_xfer *xfer)
217 {
218 	struct uhid_softc *sc = xfer->priv_sc;
219 	struct usb2_device_request req;
220 	uint32_t size = sc->sc_osize;
221 	uint32_t actlen;
222 	uint8_t id;
223 
224 	switch (USB_GET_STATE(xfer)) {
225 	case USB_ST_TRANSFERRED:
226 	case USB_ST_SETUP:
227 		/* try to extract the ID byte */
228 		if (sc->sc_oid) {
229 
230 			if (usb2_fifo_get_data(
231 			    sc->sc_fifo.fp[USB_FIFO_TX],
232 			    xfer->frbuffers,
233 			    0, 1, &actlen, 0)) {
234 				if (actlen != 1) {
235 					goto tr_error;
236 				}
237 				usb2_copy_out(xfer->frbuffers, 0, &id, 1);
238 
239 			} else {
240 				return;
241 			}
242 			if (size) {
243 				size--;
244 			}
245 		} else {
246 			id = 0;
247 		}
248 
249 		if (usb2_fifo_get_data(
250 		    sc->sc_fifo.fp[USB_FIFO_TX],
251 		    xfer->frbuffers + 1,
252 		    0, UHID_BSIZE, &actlen, 1)) {
253 			if (actlen != size) {
254 				goto tr_error;
255 			}
256 			uhid_fill_set_report
257 			    (&req, sc->sc_iface_no,
258 			    UHID_OUTPUT_REPORT, id, size);
259 
260 			usb2_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
261 
262 			xfer->frlengths[0] = sizeof(req);
263 			xfer->frlengths[1] = size;
264 			xfer->nframes = xfer->frlengths[1] ? 2 : 1;
265 			usb2_start_hardware(xfer);
266 		}
267 		return;
268 
269 	default:
270 tr_error:
271 		/* bomb out */
272 		usb2_fifo_get_data_error(sc->sc_fifo.fp[USB_FIFO_TX]);
273 		return;
274 	}
275 }
276 
277 static void
278 uhid_read_callback(struct usb2_xfer *xfer)
279 {
280 	struct uhid_softc *sc = xfer->priv_sc;
281 	struct usb2_device_request req;
282 
283 	switch (USB_GET_STATE(xfer)) {
284 	case USB_ST_TRANSFERRED:
285 		usb2_fifo_put_data(sc->sc_fifo.fp[USB_FIFO_RX], xfer->frbuffers,
286 		    sizeof(req), sc->sc_isize, 1);
287 		return;
288 
289 	case USB_ST_SETUP:
290 
291 		if (usb2_fifo_put_bytes_max(sc->sc_fifo.fp[USB_FIFO_RX]) > 0) {
292 
293 			uhid_fill_get_report
294 			    (&req, sc->sc_iface_no, UHID_INPUT_REPORT,
295 			    sc->sc_iid, sc->sc_isize);
296 
297 			usb2_copy_in(xfer->frbuffers, 0, &req, sizeof(req));
298 
299 			xfer->frlengths[0] = sizeof(req);
300 			xfer->frlengths[1] = sc->sc_isize;
301 			xfer->nframes = xfer->frlengths[1] ? 2 : 1;
302 			usb2_start_hardware(xfer);
303 		}
304 		return;
305 
306 	default:			/* Error */
307 		/* bomb out */
308 		usb2_fifo_put_data_error(sc->sc_fifo.fp[USB_FIFO_RX]);
309 		return;
310 	}
311 }
312 
313 static const struct usb2_config uhid_config[UHID_N_TRANSFER] = {
314 
315 	[UHID_INTR_DT_RD] = {
316 		.type = UE_INTERRUPT,
317 		.endpoint = UE_ADDR_ANY,
318 		.direction = UE_DIR_IN,
319 		.mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
320 		.mh.bufsize = UHID_BSIZE,
321 		.mh.callback = &uhid_intr_callback,
322 	},
323 
324 	[UHID_CTRL_DT_WR] = {
325 		.type = UE_CONTROL,
326 		.endpoint = 0x00,	/* Control pipe */
327 		.direction = UE_DIR_ANY,
328 		.mh.bufsize = sizeof(struct usb2_device_request) + UHID_BSIZE,
329 		.mh.callback = &uhid_write_callback,
330 		.mh.timeout = 1000,	/* 1 second */
331 	},
332 
333 	[UHID_CTRL_DT_RD] = {
334 		.type = UE_CONTROL,
335 		.endpoint = 0x00,	/* Control pipe */
336 		.direction = UE_DIR_ANY,
337 		.mh.bufsize = sizeof(struct usb2_device_request) + UHID_BSIZE,
338 		.mh.callback = &uhid_read_callback,
339 		.mh.timeout = 1000,	/* 1 second */
340 	},
341 };
342 
343 static void
344 uhid_start_read(struct usb2_fifo *fifo)
345 {
346 	struct uhid_softc *sc = fifo->priv_sc0;
347 
348 	if (sc->sc_flags & UHID_FLAG_IMMED) {
349 		usb2_transfer_start(sc->sc_xfer[UHID_CTRL_DT_RD]);
350 	} else {
351 		usb2_transfer_start(sc->sc_xfer[UHID_INTR_DT_RD]);
352 	}
353 }
354 
355 static void
356 uhid_stop_read(struct usb2_fifo *fifo)
357 {
358 	struct uhid_softc *sc = fifo->priv_sc0;
359 
360 	usb2_transfer_stop(sc->sc_xfer[UHID_CTRL_DT_RD]);
361 	usb2_transfer_stop(sc->sc_xfer[UHID_INTR_DT_RD]);
362 }
363 
364 static void
365 uhid_start_write(struct usb2_fifo *fifo)
366 {
367 	struct uhid_softc *sc = fifo->priv_sc0;
368 
369 	usb2_transfer_start(sc->sc_xfer[UHID_CTRL_DT_WR]);
370 }
371 
372 static void
373 uhid_stop_write(struct usb2_fifo *fifo)
374 {
375 	struct uhid_softc *sc = fifo->priv_sc0;
376 
377 	usb2_transfer_stop(sc->sc_xfer[UHID_CTRL_DT_WR]);
378 }
379 
380 static int
381 uhid_get_report(struct uhid_softc *sc, uint8_t type,
382     uint8_t id, void *kern_data, void *user_data,
383     uint16_t len)
384 {
385 	int err;
386 	uint8_t free_data = 0;
387 
388 	if (kern_data == NULL) {
389 		kern_data = malloc(len, M_USBDEV, M_WAITOK);
390 		if (kern_data == NULL) {
391 			err = ENOMEM;
392 			goto done;
393 		}
394 		free_data = 1;
395 	}
396 	err = usb2_req_get_report(sc->sc_udev, NULL, kern_data,
397 	    len, sc->sc_iface_index, type, id);
398 	if (err) {
399 		err = ENXIO;
400 		goto done;
401 	}
402 	if (user_data) {
403 		/* dummy buffer */
404 		err = copyout(kern_data, user_data, len);
405 		if (err) {
406 			goto done;
407 		}
408 	}
409 done:
410 	if (free_data) {
411 		free(kern_data, M_USBDEV);
412 	}
413 	return (err);
414 }
415 
416 static int
417 uhid_set_report(struct uhid_softc *sc, uint8_t type,
418     uint8_t id, void *kern_data, void *user_data,
419     uint16_t len)
420 {
421 	int err;
422 	uint8_t free_data = 0;
423 
424 	if (kern_data == NULL) {
425 		kern_data = malloc(len, M_USBDEV, M_WAITOK);
426 		if (kern_data == NULL) {
427 			err = ENOMEM;
428 			goto done;
429 		}
430 		free_data = 1;
431 		err = copyin(user_data, kern_data, len);
432 		if (err) {
433 			goto done;
434 		}
435 	}
436 	err = usb2_req_set_report(sc->sc_udev, NULL, kern_data,
437 	    len, sc->sc_iface_index, type, id);
438 	if (err) {
439 		err = ENXIO;
440 		goto done;
441 	}
442 done:
443 	if (free_data) {
444 		free(kern_data, M_USBDEV);
445 	}
446 	return (err);
447 }
448 
449 static int
450 uhid_open(struct usb2_fifo *fifo, int fflags, struct thread *td)
451 {
452 	struct uhid_softc *sc = fifo->priv_sc0;
453 
454 	/*
455 	 * The buffers are one byte larger than maximum so that one
456 	 * can detect too large read/writes and short transfers:
457 	 */
458 	if (fflags & FREAD) {
459 		/* reset flags */
460 		sc->sc_flags &= ~UHID_FLAG_IMMED;
461 
462 		if (usb2_fifo_alloc_buffer(fifo,
463 		    sc->sc_isize + 1, UHID_FRAME_NUM)) {
464 			return (ENOMEM);
465 		}
466 	}
467 	if (fflags & FWRITE) {
468 		if (usb2_fifo_alloc_buffer(fifo,
469 		    sc->sc_osize + 1, UHID_FRAME_NUM)) {
470 			return (ENOMEM);
471 		}
472 	}
473 	return (0);
474 }
475 
476 static void
477 uhid_close(struct usb2_fifo *fifo, int fflags, struct thread *td)
478 {
479 	if (fflags & (FREAD | FWRITE)) {
480 		usb2_fifo_free_buffer(fifo);
481 	}
482 }
483 
484 static int
485 uhid_ioctl(struct usb2_fifo *fifo, u_long cmd, void *addr,
486     int fflags, struct thread *td)
487 {
488 	struct uhid_softc *sc = fifo->priv_sc0;
489 	struct usb2_gen_descriptor *ugd;
490 	uint32_t size;
491 	int error = 0;
492 	uint8_t id;
493 
494 	switch (cmd) {
495 	case USB_GET_REPORT_DESC:
496 		ugd = addr;
497 		if (sc->sc_repdesc_size > ugd->ugd_maxlen) {
498 			size = ugd->ugd_maxlen;
499 		} else {
500 			size = sc->sc_repdesc_size;
501 		}
502 		ugd->ugd_actlen = size;
503 		if (ugd->ugd_data == NULL)
504 			break;		/* descriptor length only */
505 		error = copyout(sc->sc_repdesc_ptr, ugd->ugd_data, size);
506 		break;
507 
508 	case USB_SET_IMMED:
509 		if (!(fflags & FREAD)) {
510 			error = EPERM;
511 			break;
512 		}
513 		if (*(int *)addr) {
514 
515 			/* do a test read */
516 
517 			error = uhid_get_report(sc, UHID_INPUT_REPORT,
518 			    sc->sc_iid, NULL, NULL, sc->sc_isize);
519 			if (error) {
520 				break;
521 			}
522 			mtx_lock(&sc->sc_mtx);
523 			sc->sc_flags |= UHID_FLAG_IMMED;
524 			mtx_unlock(&sc->sc_mtx);
525 		} else {
526 			mtx_lock(&sc->sc_mtx);
527 			sc->sc_flags &= ~UHID_FLAG_IMMED;
528 			mtx_unlock(&sc->sc_mtx);
529 		}
530 		break;
531 
532 	case USB_GET_REPORT:
533 		if (!(fflags & FREAD)) {
534 			error = EPERM;
535 			break;
536 		}
537 		ugd = addr;
538 		switch (ugd->ugd_report_type) {
539 		case UHID_INPUT_REPORT:
540 			size = sc->sc_isize;
541 			id = sc->sc_iid;
542 			break;
543 		case UHID_OUTPUT_REPORT:
544 			size = sc->sc_osize;
545 			id = sc->sc_oid;
546 			break;
547 		case UHID_FEATURE_REPORT:
548 			size = sc->sc_fsize;
549 			id = sc->sc_fid;
550 			break;
551 		default:
552 			return (EINVAL);
553 		}
554 		error = uhid_get_report(sc, ugd->ugd_report_type, id,
555 		    NULL, ugd->ugd_data, size);
556 		break;
557 
558 	case USB_SET_REPORT:
559 		if (!(fflags & FWRITE)) {
560 			error = EPERM;
561 			break;
562 		}
563 		ugd = addr;
564 		switch (ugd->ugd_report_type) {
565 		case UHID_INPUT_REPORT:
566 			size = sc->sc_isize;
567 			id = sc->sc_iid;
568 			break;
569 		case UHID_OUTPUT_REPORT:
570 			size = sc->sc_osize;
571 			id = sc->sc_oid;
572 			break;
573 		case UHID_FEATURE_REPORT:
574 			size = sc->sc_fsize;
575 			id = sc->sc_fid;
576 			break;
577 		default:
578 			return (EINVAL);
579 		}
580 		error = uhid_set_report(sc, ugd->ugd_report_type, id,
581 		    NULL, ugd->ugd_data, size);
582 		break;
583 
584 	case USB_GET_REPORT_ID:
585 		*(int *)addr = 0;	/* XXX: we only support reportid 0? */
586 		break;
587 
588 	default:
589 		error = EINVAL;
590 		break;
591 	}
592 	return (error);
593 }
594 
595 static int
596 uhid_probe(device_t dev)
597 {
598 	struct usb2_attach_arg *uaa = device_get_ivars(dev);
599 
600 	DPRINTFN(11, "\n");
601 
602 	if (uaa->usb2_mode != USB_MODE_HOST) {
603 		return (ENXIO);
604 	}
605 	if (uaa->use_generic == 0) {
606 		/* give Mouse and Keyboard drivers a try first */
607 		return (ENXIO);
608 	}
609 	if (uaa->info.bInterfaceClass != UICLASS_HID) {
610 
611 		/* the Xbox 360 gamepad doesn't use the HID class */
612 
613 		if ((uaa->info.bInterfaceClass != UICLASS_VENDOR) ||
614 		    (uaa->info.bInterfaceSubClass != UISUBCLASS_XBOX360_CONTROLLER) ||
615 		    (uaa->info.bInterfaceProtocol != UIPROTO_XBOX360_GAMEPAD)) {
616 			return (ENXIO);
617 		}
618 	}
619 	if (usb2_test_quirk(uaa, UQ_HID_IGNORE)) {
620 		return (ENXIO);
621 	}
622 	return (0);
623 }
624 
625 static int
626 uhid_attach(device_t dev)
627 {
628 	struct usb2_attach_arg *uaa = device_get_ivars(dev);
629 	struct uhid_softc *sc = device_get_softc(dev);
630 	int unit = device_get_unit(dev);
631 	int error = 0;
632 
633 	DPRINTFN(10, "sc=%p\n", sc);
634 
635 	device_set_usb2_desc(dev);
636 
637 	mtx_init(&sc->sc_mtx, "uhid lock", NULL, MTX_DEF | MTX_RECURSE);
638 
639 	sc->sc_udev = uaa->device;
640 
641 	sc->sc_iface_no = uaa->info.bIfaceNum;
642 	sc->sc_iface_index = uaa->info.bIfaceIndex;
643 
644 	error = usb2_transfer_setup(uaa->device,
645 	    &uaa->info.bIfaceIndex, sc->sc_xfer, uhid_config,
646 	    UHID_N_TRANSFER, sc, &sc->sc_mtx);
647 
648 	if (error) {
649 		DPRINTF("error=%s\n", usb2_errstr(error));
650 		goto detach;
651 	}
652 	if (uaa->info.idVendor == USB_VENDOR_WACOM) {
653 
654 		/* the report descriptor for the Wacom Graphire is broken */
655 
656 		if (uaa->info.idProduct == USB_PRODUCT_WACOM_GRAPHIRE) {
657 
658 			sc->sc_repdesc_size = sizeof(uhid_graphire_report_descr);
659 			sc->sc_repdesc_ptr = USB_ADD_BYTES(uhid_graphire_report_descr, 0);
660 			sc->sc_flags |= UHID_FLAG_STATIC_DESC;
661 
662 		} else if (uaa->info.idProduct == USB_PRODUCT_WACOM_GRAPHIRE3_4X5) {
663 
664 			static uint8_t reportbuf[] = {2, 2, 2};
665 
666 			/*
667 			 * The Graphire3 needs 0x0202 to be written to
668 			 * feature report ID 2 before it'll start
669 			 * returning digitizer data.
670 			 */
671 			error = usb2_req_set_report
672 			    (uaa->device, &Giant, reportbuf, sizeof(reportbuf),
673 			    uaa->info.bIfaceIndex, UHID_FEATURE_REPORT, 2);
674 
675 			if (error) {
676 				DPRINTF("set report failed, error=%s (ignored)\n",
677 				    usb2_errstr(error));
678 			}
679 			sc->sc_repdesc_size = sizeof(uhid_graphire3_4x5_report_descr);
680 			sc->sc_repdesc_ptr = USB_ADD_BYTES(uhid_graphire3_4x5_report_descr, 0);
681 			sc->sc_flags |= UHID_FLAG_STATIC_DESC;
682 		}
683 	} else if ((uaa->info.bInterfaceClass == UICLASS_VENDOR) &&
684 		    (uaa->info.bInterfaceSubClass == UISUBCLASS_XBOX360_CONTROLLER) &&
685 	    (uaa->info.bInterfaceProtocol == UIPROTO_XBOX360_GAMEPAD)) {
686 
687 		/* the Xbox 360 gamepad has no report descriptor */
688 		sc->sc_repdesc_size = sizeof(uhid_xb360gp_report_descr);
689 		sc->sc_repdesc_ptr = USB_ADD_BYTES(uhid_xb360gp_report_descr, 0);
690 		sc->sc_flags |= UHID_FLAG_STATIC_DESC;
691 	}
692 	if (sc->sc_repdesc_ptr == NULL) {
693 
694 		error = usb2_req_get_hid_desc
695 		    (uaa->device, &Giant, &sc->sc_repdesc_ptr,
696 		    &sc->sc_repdesc_size, M_USBDEV, uaa->info.bIfaceIndex);
697 
698 		if (error) {
699 			device_printf(dev, "no report descriptor\n");
700 			goto detach;
701 		}
702 	}
703 	error = usb2_req_set_idle(uaa->device, &Giant,
704 	    uaa->info.bIfaceIndex, 0, 0);
705 
706 	if (error) {
707 		DPRINTF("set idle failed, error=%s (ignored)\n",
708 		    usb2_errstr(error));
709 	}
710 	sc->sc_isize = hid_report_size
711 	    (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_input, &sc->sc_iid);
712 
713 	sc->sc_osize = hid_report_size
714 	    (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_output, &sc->sc_oid);
715 
716 	sc->sc_fsize = hid_report_size
717 	    (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_feature, &sc->sc_fid);
718 
719 	if (sc->sc_isize > UHID_BSIZE) {
720 		DPRINTF("input size is too large, "
721 		    "%d bytes (truncating)\n",
722 		    sc->sc_isize);
723 		sc->sc_isize = UHID_BSIZE;
724 	}
725 	if (sc->sc_osize > UHID_BSIZE) {
726 		DPRINTF("output size is too large, "
727 		    "%d bytes (truncating)\n",
728 		    sc->sc_osize);
729 		sc->sc_osize = UHID_BSIZE;
730 	}
731 	if (sc->sc_fsize > UHID_BSIZE) {
732 		DPRINTF("feature size is too large, "
733 		    "%d bytes (truncating)\n",
734 		    sc->sc_fsize);
735 		sc->sc_fsize = UHID_BSIZE;
736 	}
737 	/* set interface permissions */
738 	usb2_set_iface_perm(uaa->device, uaa->info.bIfaceIndex,
739 	    UID_ROOT, GID_OPERATOR, 0644);
740 
741 	error = usb2_fifo_attach(uaa->device, sc, &sc->sc_mtx,
742 	    &uhid_fifo_methods, &sc->sc_fifo,
743 	    unit, 0 - 1, uaa->info.bIfaceIndex);
744 	if (error) {
745 		goto detach;
746 	}
747 	return (0);			/* success */
748 
749 detach:
750 	uhid_detach(dev);
751 	return (ENOMEM);
752 }
753 
754 static int
755 uhid_detach(device_t dev)
756 {
757 	struct uhid_softc *sc = device_get_softc(dev);
758 
759 	usb2_fifo_detach(&sc->sc_fifo);
760 
761 	usb2_transfer_unsetup(sc->sc_xfer, UHID_N_TRANSFER);
762 
763 	if (sc->sc_repdesc_ptr) {
764 		if (!(sc->sc_flags & UHID_FLAG_STATIC_DESC)) {
765 			free(sc->sc_repdesc_ptr, M_USBDEV);
766 		}
767 	}
768 	mtx_destroy(&sc->sc_mtx);
769 
770 	return (0);
771 }
772 
773 static devclass_t uhid_devclass;
774 
775 static device_method_t uhid_methods[] = {
776 	DEVMETHOD(device_probe, uhid_probe),
777 	DEVMETHOD(device_attach, uhid_attach),
778 	DEVMETHOD(device_detach, uhid_detach),
779 	{0, 0}
780 };
781 
782 static driver_t uhid_driver = {
783 	.name = "uhid",
784 	.methods = uhid_methods,
785 	.size = sizeof(struct uhid_softc),
786 };
787 
788 DRIVER_MODULE(uhid, ushub, uhid_driver, uhid_devclass, NULL, 0);
789 MODULE_DEPEND(uhid, usb, 1, 1, 1);
790