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