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