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