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