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