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