xref: /freebsd/sys/dev/usb/input/uhid.c (revision 6580f5c38dd5b01aeeaed16b370f1a12423437f0)
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 			error = copyin(ugd->ugd_data, &id, 1);
638 		if (error == 0)
639 			error = uhid_get_report(sc, ugd->ugd_report_type, id,
640 			    NULL, ugd->ugd_data, imin(ugd->ugd_maxlen, size));
641 		break;
642 
643 	case USB_SET_REPORT:
644 		if (!(fflags & FWRITE)) {
645 			error = EPERM;
646 			break;
647 		}
648 		switch (ugd->ugd_report_type) {
649 		case UHID_INPUT_REPORT:
650 			size = sc->sc_isize;
651 			id = sc->sc_iid;
652 			break;
653 		case UHID_OUTPUT_REPORT:
654 			size = sc->sc_osize;
655 			id = sc->sc_oid;
656 			break;
657 		case UHID_FEATURE_REPORT:
658 			size = sc->sc_fsize;
659 			id = sc->sc_fid;
660 			break;
661 		default:
662 			return (EINVAL);
663 		}
664 		if (id != 0)
665 			error = copyin(ugd->ugd_data, &id, 1);
666 		if (error == 0)
667 			error = uhid_set_report(sc, ugd->ugd_report_type, id,
668 			    NULL, ugd->ugd_data, imin(ugd->ugd_maxlen, size));
669 		break;
670 
671 	case USB_GET_REPORT_ID:
672 		*(int *)addr = 0;	/* XXX: we only support reportid 0? */
673 		break;
674 
675 	default:
676 		error = ENOIOCTL;
677 		break;
678 	}
679 #ifdef COMPAT_FREEBSD32
680 	if (ugd32 != NULL)
681 		update_usb_gen_descriptor32(ugd32, ugd);
682 #endif
683 	return (error);
684 }
685 
686 static int
687 uhid_ioctl_post(struct usb_fifo *fifo, u_long cmd, void *addr,
688     int fflags)
689 {
690 	int error;
691 
692 	switch (cmd) {
693 	case USB_GET_DEVICEINFO:
694 		error = ugen_fill_deviceinfo(fifo, addr);
695 		break;
696 
697 	default:
698 		error = EINVAL;
699 		break;
700 	}
701 	return (error);
702 }
703 
704 static const STRUCT_USB_HOST_ID uhid_devs[] = {
705 	/* generic HID class */
706 	{USB_IFACE_CLASS(UICLASS_HID),},
707 	/* the Xbox 360 gamepad doesn't use the HID class */
708 	{USB_IFACE_CLASS(UICLASS_VENDOR),
709 	 USB_IFACE_SUBCLASS(UISUBCLASS_XBOX360_CONTROLLER),
710 	 USB_IFACE_PROTOCOL(UIPROTO_XBOX360_GAMEPAD),},
711 };
712 
713 static int
714 uhid_probe(device_t dev)
715 {
716 	struct usb_attach_arg *uaa = device_get_ivars(dev);
717 	int error;
718 	void *buf;
719 	uint16_t len;
720 
721 	DPRINTFN(11, "\n");
722 
723 	if (uaa->usb_mode != USB_MODE_HOST)
724 		return (ENXIO);
725 
726 	error = usbd_lookup_id_by_uaa(uhid_devs, sizeof(uhid_devs), uaa);
727 	if (error)
728 		return (error);
729 
730 	if (usb_test_quirk(uaa, UQ_HID_IGNORE))
731 		return (ENXIO);
732 
733 	/*
734 	 * Don't attach to mouse and keyboard devices, hence then no
735 	 * "nomatch" event is generated and then ums and ukbd won't
736 	 * attach properly when loaded.
737 	 */
738 	if ((uaa->info.bInterfaceClass == UICLASS_HID) &&
739 	    (uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
740 	    (((uaa->info.bInterfaceProtocol == UIPROTO_BOOT_KEYBOARD) &&
741 	      !usb_test_quirk(uaa, UQ_KBD_IGNORE)) ||
742 	     ((uaa->info.bInterfaceProtocol == UIPROTO_MOUSE) &&
743 	      !usb_test_quirk(uaa, UQ_UMS_IGNORE))))
744 		return (ENXIO);
745 
746 	/* Check for mandatory multitouch usages to give wmt(4) a chance */
747 	if (!usb_test_quirk(uaa, UQ_WMT_IGNORE)) {
748 		error = usbd_req_get_hid_desc(uaa->device, NULL,
749 		    &buf, &len, M_USBDEV, uaa->info.bIfaceIndex);
750 		/* Let HID decscriptor-less devices to be handled at attach */
751 		if (!error) {
752 			if (hid_locate(buf, len,
753 			    HID_USAGE2(HUP_DIGITIZERS, HUD_CONTACT_MAX),
754 			    hid_feature, 0, NULL, NULL, NULL) &&
755 			    hid_locate(buf, len,
756 			    HID_USAGE2(HUP_DIGITIZERS, HUD_CONTACTID),
757 			    hid_input, 0, NULL, NULL, NULL)) {
758 				free(buf, M_USBDEV);
759 				return (ENXIO);
760 			}
761 			free(buf, M_USBDEV);
762 		}
763 	}
764 
765 	return (BUS_PROBE_GENERIC);
766 }
767 
768 static int
769 uhid_attach(device_t dev)
770 {
771 	struct usb_attach_arg *uaa = device_get_ivars(dev);
772 	struct uhid_softc *sc = device_get_softc(dev);
773 	int unit = device_get_unit(dev);
774 	int error = 0;
775 
776 	DPRINTFN(10, "sc=%p\n", sc);
777 
778 	device_set_usb_desc(dev);
779 
780 	mtx_init(&sc->sc_mtx, "uhid lock", NULL, MTX_DEF | MTX_RECURSE);
781 
782 	sc->sc_udev = uaa->device;
783 
784 	sc->sc_iface_no = uaa->info.bIfaceNum;
785 	sc->sc_iface_index = uaa->info.bIfaceIndex;
786 
787 	error = usbd_transfer_setup(uaa->device,
788 	    &uaa->info.bIfaceIndex, sc->sc_xfer, uhid_config,
789 	    UHID_N_TRANSFER, sc, &sc->sc_mtx);
790 
791 	if (error) {
792 		DPRINTF("error=%s\n", usbd_errstr(error));
793 		goto detach;
794 	}
795 	if (uaa->info.idVendor == USB_VENDOR_WACOM) {
796 		/* the report descriptor for the Wacom Graphire is broken */
797 
798 		if (uaa->info.idProduct == USB_PRODUCT_WACOM_GRAPHIRE) {
799 			sc->sc_repdesc_size = sizeof(uhid_graphire_report_descr);
800 			sc->sc_repdesc_ptr = __DECONST(void *, &uhid_graphire_report_descr);
801 			sc->sc_flags |= UHID_FLAG_STATIC_DESC;
802 
803 		} else if (uaa->info.idProduct == USB_PRODUCT_WACOM_GRAPHIRE3_4X5) {
804 			static uint8_t reportbuf[] = {2, 2, 2};
805 
806 			/*
807 			 * The Graphire3 needs 0x0202 to be written to
808 			 * feature report ID 2 before it'll start
809 			 * returning digitizer data.
810 			 */
811 			error = usbd_req_set_report(uaa->device, NULL,
812 			    reportbuf, sizeof(reportbuf),
813 			    uaa->info.bIfaceIndex, UHID_FEATURE_REPORT, 2);
814 
815 			if (error) {
816 				DPRINTF("set report failed, error=%s (ignored)\n",
817 				    usbd_errstr(error));
818 			}
819 			sc->sc_repdesc_size = sizeof(uhid_graphire3_4x5_report_descr);
820 			sc->sc_repdesc_ptr = __DECONST(void *, &uhid_graphire3_4x5_report_descr);
821 			sc->sc_flags |= UHID_FLAG_STATIC_DESC;
822 		}
823 	} else if ((uaa->info.bInterfaceClass == UICLASS_VENDOR) &&
824 	    (uaa->info.bInterfaceSubClass == UISUBCLASS_XBOX360_CONTROLLER) &&
825 	    (uaa->info.bInterfaceProtocol == UIPROTO_XBOX360_GAMEPAD)) {
826 		static const uint8_t reportbuf[3] = {1, 3, 0};
827 		/*
828 		 * Turn off the four LEDs on the gamepad which
829 		 * are blinking by default:
830 		 */
831 		error = usbd_req_set_report(uaa->device, NULL,
832 		    __DECONST(void *, reportbuf), sizeof(reportbuf),
833 		    uaa->info.bIfaceIndex, UHID_OUTPUT_REPORT, 0);
834 		if (error) {
835 			DPRINTF("set output report failed, error=%s (ignored)\n",
836 			    usbd_errstr(error));
837 		}
838 		/* the Xbox 360 gamepad has no report descriptor */
839 		sc->sc_repdesc_size = sizeof(uhid_xb360gp_report_descr);
840 		sc->sc_repdesc_ptr = __DECONST(void *, &uhid_xb360gp_report_descr);
841 		sc->sc_flags |= UHID_FLAG_STATIC_DESC;
842 	}
843 	if (sc->sc_repdesc_ptr == NULL) {
844 		error = usbd_req_get_hid_desc(uaa->device, NULL,
845 		    &sc->sc_repdesc_ptr, &sc->sc_repdesc_size,
846 		    M_USBDEV, uaa->info.bIfaceIndex);
847 
848 		if (error) {
849 			device_printf(dev, "no report descriptor\n");
850 			goto detach;
851 		}
852 	}
853 	error = usbd_req_set_idle(uaa->device, NULL,
854 	    uaa->info.bIfaceIndex, 0, 0);
855 
856 	if (error) {
857 		DPRINTF("set idle failed, error=%s (ignored)\n",
858 		    usbd_errstr(error));
859 	}
860 	sc->sc_isize = hid_report_size_max
861 	    (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_input, &sc->sc_iid);
862 
863 	sc->sc_osize = hid_report_size_max
864 	    (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_output, &sc->sc_oid);
865 
866 	sc->sc_fsize = hid_report_size_max
867 	    (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_feature, &sc->sc_fid);
868 
869 	if (sc->sc_isize > UHID_BSIZE) {
870 		DPRINTF("input size is too large, "
871 		    "%d bytes (truncating)\n",
872 		    sc->sc_isize);
873 		sc->sc_isize = UHID_BSIZE;
874 	}
875 	if (sc->sc_osize > UHID_BSIZE) {
876 		DPRINTF("output size is too large, "
877 		    "%d bytes (truncating)\n",
878 		    sc->sc_osize);
879 		sc->sc_osize = UHID_BSIZE;
880 	}
881 	if (sc->sc_fsize > UHID_BSIZE) {
882 		DPRINTF("feature size is too large, "
883 		    "%d bytes (truncating)\n",
884 		    sc->sc_fsize);
885 		sc->sc_fsize = UHID_BSIZE;
886 	}
887 
888 	error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
889 	    &uhid_fifo_methods, &sc->sc_fifo,
890 	    unit, -1, uaa->info.bIfaceIndex,
891 	    UID_ROOT, GID_OPERATOR, 0644);
892 	if (error) {
893 		goto detach;
894 	}
895 	return (0);			/* success */
896 
897 detach:
898 	uhid_detach(dev);
899 	return (ENOMEM);
900 }
901 
902 static int
903 uhid_detach(device_t dev)
904 {
905 	struct uhid_softc *sc = device_get_softc(dev);
906 
907 	usb_fifo_detach(&sc->sc_fifo);
908 
909 	usbd_transfer_unsetup(sc->sc_xfer, UHID_N_TRANSFER);
910 
911 	if (sc->sc_repdesc_ptr) {
912 		if (!(sc->sc_flags & UHID_FLAG_STATIC_DESC)) {
913 			free(sc->sc_repdesc_ptr, M_USBDEV);
914 		}
915 	}
916 	mtx_destroy(&sc->sc_mtx);
917 
918 	return (0);
919 }
920 
921 static device_method_t uhid_methods[] = {
922 	DEVMETHOD(device_probe, uhid_probe),
923 	DEVMETHOD(device_attach, uhid_attach),
924 	DEVMETHOD(device_detach, uhid_detach),
925 
926 	DEVMETHOD_END
927 };
928 
929 static driver_t uhid_driver = {
930 #ifdef HIDRAW_MAKE_UHID_ALIAS
931 	.name = "hidraw",
932 #else
933 	.name = "uhid",
934 #endif
935 	.methods = uhid_methods,
936 	.size = sizeof(struct uhid_softc),
937 };
938 
939 DRIVER_MODULE(uhid, uhub, uhid_driver, NULL, NULL);
940 MODULE_DEPEND(uhid, usb, 1, 1, 1);
941 MODULE_DEPEND(uhid, hid, 1, 1, 1);
942 MODULE_VERSION(uhid, 1);
943 USB_PNP_HOST_INFO(uhid_devs);
944