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