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