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 /*-
8 * SPDX-License-Identifier: BSD-2-Clause
9 *
10 * Copyright (c) 1998 The NetBSD Foundation, Inc.
11 * All rights reserved.
12 *
13 * This code is derived from software contributed to The NetBSD Foundation
14 * by Lennart Augustsson (lennart@augustsson.net) at
15 * Carlstedt Research & Technology.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * HID spec: http://www.usb.org/developers/devclass_docs/HID1_11.pdf
41 */
42
43 #include <sys/stdint.h>
44 #include <sys/stddef.h>
45 #include <sys/param.h>
46 #include <sys/queue.h>
47 #include <sys/types.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/bus.h>
51 #include <sys/module.h>
52 #include <sys/lock.h>
53 #include <sys/mutex.h>
54 #include <sys/condvar.h>
55 #include <sys/sysctl.h>
56 #include <sys/sx.h>
57 #include <sys/unistd.h>
58 #include <sys/callout.h>
59 #include <sys/malloc.h>
60 #include <sys/priv.h>
61 #include <sys/conf.h>
62 #include <sys/fcntl.h>
63
64 #include <dev/hid/hid.h>
65
66 #include "usbdevs.h"
67 #include <dev/usb/usb.h>
68 #include <dev/usb/usbdi.h>
69 #include <dev/usb/usbdi_util.h>
70 #include <dev/usb/usbhid.h>
71 #include <dev/usb/usb_ioctl.h>
72 #include <dev/usb/usb_generic.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 static usb_fifo_ioctl_t uhid_ioctl_post;
148
149 static struct usb_fifo_methods uhid_fifo_methods = {
150 .f_open = &uhid_open,
151 .f_close = &uhid_close,
152 .f_ioctl = &uhid_ioctl,
153 .f_ioctl_post = &uhid_ioctl_post,
154 .f_start_read = &uhid_start_read,
155 .f_stop_read = &uhid_stop_read,
156 .f_start_write = &uhid_start_write,
157 .f_stop_write = &uhid_stop_write,
158 .basename[0] = "uhid",
159 };
160
161 static void
uhid_intr_write_callback(struct usb_xfer * xfer,usb_error_t error)162 uhid_intr_write_callback(struct usb_xfer *xfer, usb_error_t error)
163 {
164 struct uhid_softc *sc = usbd_xfer_softc(xfer);
165 struct usb_page_cache *pc;
166 int actlen;
167
168 switch (USB_GET_STATE(xfer)) {
169 case USB_ST_TRANSFERRED:
170 case USB_ST_SETUP:
171 tr_setup:
172 pc = usbd_xfer_get_frame(xfer, 0);
173 if (usb_fifo_get_data(sc->sc_fifo.fp[USB_FIFO_TX], pc,
174 0, usbd_xfer_max_len(xfer), &actlen, 0)) {
175 usbd_xfer_set_frame_len(xfer, 0, actlen);
176 usbd_transfer_submit(xfer);
177 }
178 return;
179
180 default: /* Error */
181 if (error != USB_ERR_CANCELLED) {
182 /* try to clear stall first */
183 usbd_xfer_set_stall(xfer);
184 goto tr_setup;
185 }
186 return;
187 }
188 }
189
190 static void
uhid_intr_read_callback(struct usb_xfer * xfer,usb_error_t error)191 uhid_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
192 {
193 struct uhid_softc *sc = usbd_xfer_softc(xfer);
194 struct usb_page_cache *pc;
195 int actlen;
196
197 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
198
199 switch (USB_GET_STATE(xfer)) {
200 case USB_ST_TRANSFERRED:
201 DPRINTF("transferred!\n");
202
203 pc = usbd_xfer_get_frame(xfer, 0);
204
205 /*
206 * If the ID byte is non zero we allow descriptors
207 * having multiple sizes:
208 */
209 if ((actlen >= (int)sc->sc_isize) ||
210 ((actlen > 0) && (sc->sc_iid != 0))) {
211 /* limit report length to the maximum */
212 if (actlen > (int)sc->sc_isize)
213 actlen = sc->sc_isize;
214 usb_fifo_put_data(sc->sc_fifo.fp[USB_FIFO_RX], pc,
215 0, actlen, 1);
216
217 /*
218 * Do not do read-ahead, because this may lead
219 * to data loss!
220 */
221 return;
222 } else {
223 /* ignore it */
224 DPRINTF("ignored transfer, %d bytes\n", actlen);
225 }
226
227 case USB_ST_SETUP:
228 re_submit:
229 if (usb_fifo_put_bytes_max(
230 sc->sc_fifo.fp[USB_FIFO_RX]) != 0) {
231 usbd_xfer_set_frame_len(xfer, 0, sc->sc_isize);
232 usbd_transfer_submit(xfer);
233 }
234 return;
235
236 default: /* Error */
237 if (error != USB_ERR_CANCELLED) {
238 /* try to clear stall first */
239 usbd_xfer_set_stall(xfer);
240 goto re_submit;
241 }
242 return;
243 }
244 }
245
246 static void
uhid_fill_set_report(struct usb_device_request * req,uint8_t iface_no,uint8_t type,uint8_t id,uint16_t size)247 uhid_fill_set_report(struct usb_device_request *req, uint8_t iface_no,
248 uint8_t type, uint8_t id, uint16_t size)
249 {
250 req->bmRequestType = UT_WRITE_CLASS_INTERFACE;
251 req->bRequest = UR_SET_REPORT;
252 USETW2(req->wValue, type, id);
253 req->wIndex[0] = iface_no;
254 req->wIndex[1] = 0;
255 USETW(req->wLength, size);
256 }
257
258 static void
uhid_fill_get_report(struct usb_device_request * req,uint8_t iface_no,uint8_t type,uint8_t id,uint16_t size)259 uhid_fill_get_report(struct usb_device_request *req, uint8_t iface_no,
260 uint8_t type, uint8_t id, uint16_t size)
261 {
262 req->bmRequestType = UT_READ_CLASS_INTERFACE;
263 req->bRequest = UR_GET_REPORT;
264 USETW2(req->wValue, type, id);
265 req->wIndex[0] = iface_no;
266 req->wIndex[1] = 0;
267 USETW(req->wLength, size);
268 }
269
270 static void
uhid_write_callback(struct usb_xfer * xfer,usb_error_t error)271 uhid_write_callback(struct usb_xfer *xfer, usb_error_t error)
272 {
273 struct uhid_softc *sc = usbd_xfer_softc(xfer);
274 struct usb_device_request req;
275 struct usb_page_cache *pc;
276 uint32_t size = sc->sc_osize;
277 uint32_t actlen;
278 uint8_t id;
279
280 switch (USB_GET_STATE(xfer)) {
281 case USB_ST_TRANSFERRED:
282 case USB_ST_SETUP:
283 /* try to extract the ID byte */
284 if (sc->sc_oid) {
285 pc = usbd_xfer_get_frame(xfer, 0);
286 if (usb_fifo_get_data(sc->sc_fifo.fp[USB_FIFO_TX], pc,
287 0, 1, &actlen, 0)) {
288 if (actlen != 1) {
289 goto tr_error;
290 }
291 usbd_copy_out(pc, 0, &id, 1);
292
293 } else {
294 return;
295 }
296 if (size) {
297 size--;
298 }
299 } else {
300 id = 0;
301 }
302
303 pc = usbd_xfer_get_frame(xfer, 1);
304 if (usb_fifo_get_data(sc->sc_fifo.fp[USB_FIFO_TX], pc,
305 0, UHID_BSIZE, &actlen, 1)) {
306 if (actlen != size) {
307 goto tr_error;
308 }
309 uhid_fill_set_report
310 (&req, sc->sc_iface_no,
311 UHID_OUTPUT_REPORT, id, size);
312
313 pc = usbd_xfer_get_frame(xfer, 0);
314 usbd_copy_in(pc, 0, &req, sizeof(req));
315
316 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
317 usbd_xfer_set_frame_len(xfer, 1, size);
318 usbd_xfer_set_frames(xfer, size ? 2 : 1);
319 usbd_transfer_submit(xfer);
320 }
321 return;
322
323 default:
324 tr_error:
325 /* bomb out */
326 usb_fifo_get_data_error(sc->sc_fifo.fp[USB_FIFO_TX]);
327 return;
328 }
329 }
330
331 static void
uhid_read_callback(struct usb_xfer * xfer,usb_error_t error)332 uhid_read_callback(struct usb_xfer *xfer, usb_error_t error)
333 {
334 struct uhid_softc *sc = usbd_xfer_softc(xfer);
335 struct usb_device_request req;
336 struct usb_page_cache *pc;
337
338 pc = usbd_xfer_get_frame(xfer, 0);
339
340 switch (USB_GET_STATE(xfer)) {
341 case USB_ST_TRANSFERRED:
342 usb_fifo_put_data(sc->sc_fifo.fp[USB_FIFO_RX], pc, sizeof(req),
343 sc->sc_isize, 1);
344 return;
345
346 case USB_ST_SETUP:
347
348 if (usb_fifo_put_bytes_max(sc->sc_fifo.fp[USB_FIFO_RX]) > 0) {
349 uhid_fill_get_report
350 (&req, sc->sc_iface_no, UHID_INPUT_REPORT,
351 sc->sc_iid, sc->sc_isize);
352
353 usbd_copy_in(pc, 0, &req, sizeof(req));
354
355 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
356 usbd_xfer_set_frame_len(xfer, 1, sc->sc_isize);
357 usbd_xfer_set_frames(xfer, sc->sc_isize ? 2 : 1);
358 usbd_transfer_submit(xfer);
359 }
360 return;
361
362 default: /* Error */
363 /* bomb out */
364 usb_fifo_put_data_error(sc->sc_fifo.fp[USB_FIFO_RX]);
365 return;
366 }
367 }
368
369 static const struct usb_config uhid_config[UHID_N_TRANSFER] = {
370 [UHID_INTR_DT_WR] = {
371 .type = UE_INTERRUPT,
372 .endpoint = UE_ADDR_ANY,
373 .direction = UE_DIR_OUT,
374 .flags = {.pipe_bof = 1,.no_pipe_ok = 1, },
375 .bufsize = UHID_BSIZE,
376 .callback = &uhid_intr_write_callback,
377 },
378
379 [UHID_INTR_DT_RD] = {
380 .type = UE_INTERRUPT,
381 .endpoint = UE_ADDR_ANY,
382 .direction = UE_DIR_IN,
383 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
384 .bufsize = UHID_BSIZE,
385 .callback = &uhid_intr_read_callback,
386 },
387
388 [UHID_CTRL_DT_WR] = {
389 .type = UE_CONTROL,
390 .endpoint = 0x00, /* Control pipe */
391 .direction = UE_DIR_ANY,
392 .bufsize = sizeof(struct usb_device_request) + UHID_BSIZE,
393 .callback = &uhid_write_callback,
394 .timeout = 1000, /* 1 second */
395 },
396
397 [UHID_CTRL_DT_RD] = {
398 .type = UE_CONTROL,
399 .endpoint = 0x00, /* Control pipe */
400 .direction = UE_DIR_ANY,
401 .bufsize = sizeof(struct usb_device_request) + UHID_BSIZE,
402 .callback = &uhid_read_callback,
403 .timeout = 1000, /* 1 second */
404 },
405 };
406
407 static void
uhid_start_read(struct usb_fifo * fifo)408 uhid_start_read(struct usb_fifo *fifo)
409 {
410 struct uhid_softc *sc = usb_fifo_softc(fifo);
411
412 if (sc->sc_flags & UHID_FLAG_IMMED) {
413 usbd_transfer_start(sc->sc_xfer[UHID_CTRL_DT_RD]);
414 } else {
415 usbd_transfer_start(sc->sc_xfer[UHID_INTR_DT_RD]);
416 }
417 }
418
419 static void
uhid_stop_read(struct usb_fifo * fifo)420 uhid_stop_read(struct usb_fifo *fifo)
421 {
422 struct uhid_softc *sc = usb_fifo_softc(fifo);
423
424 usbd_transfer_stop(sc->sc_xfer[UHID_CTRL_DT_RD]);
425 usbd_transfer_stop(sc->sc_xfer[UHID_INTR_DT_RD]);
426 }
427
428 static void
uhid_start_write(struct usb_fifo * fifo)429 uhid_start_write(struct usb_fifo *fifo)
430 {
431 struct uhid_softc *sc = usb_fifo_softc(fifo);
432
433 if ((sc->sc_flags & UHID_FLAG_IMMED) ||
434 sc->sc_xfer[UHID_INTR_DT_WR] == NULL) {
435 usbd_transfer_start(sc->sc_xfer[UHID_CTRL_DT_WR]);
436 } else {
437 usbd_transfer_start(sc->sc_xfer[UHID_INTR_DT_WR]);
438 }
439 }
440
441 static void
uhid_stop_write(struct usb_fifo * fifo)442 uhid_stop_write(struct usb_fifo *fifo)
443 {
444 struct uhid_softc *sc = usb_fifo_softc(fifo);
445
446 usbd_transfer_stop(sc->sc_xfer[UHID_CTRL_DT_WR]);
447 usbd_transfer_stop(sc->sc_xfer[UHID_INTR_DT_WR]);
448 }
449
450 static int
uhid_get_report(struct uhid_softc * sc,uint8_t type,uint8_t id,void * kern_data,void * user_data,uint16_t len)451 uhid_get_report(struct uhid_softc *sc, uint8_t type,
452 uint8_t id, void *kern_data, void *user_data,
453 uint16_t len)
454 {
455 int err;
456 uint8_t free_data = 0;
457
458 if (kern_data == NULL) {
459 kern_data = malloc(len, M_USBDEV, M_WAITOK);
460 free_data = 1;
461 }
462 err = usbd_req_get_report(sc->sc_udev, NULL, kern_data,
463 len, sc->sc_iface_index, type, id);
464 if (err) {
465 err = ENXIO;
466 goto done;
467 }
468 if (user_data) {
469 /* dummy buffer */
470 err = copyout(kern_data, user_data, len);
471 if (err) {
472 goto done;
473 }
474 }
475 done:
476 if (free_data) {
477 free(kern_data, M_USBDEV);
478 }
479 return (err);
480 }
481
482 static int
uhid_set_report(struct uhid_softc * sc,uint8_t type,uint8_t id,void * kern_data,void * user_data,uint16_t len)483 uhid_set_report(struct uhid_softc *sc, uint8_t type,
484 uint8_t id, void *kern_data, void *user_data,
485 uint16_t len)
486 {
487 int err;
488 uint8_t free_data = 0;
489
490 if (kern_data == NULL) {
491 kern_data = malloc(len, M_USBDEV, M_WAITOK);
492 free_data = 1;
493 err = copyin(user_data, kern_data, len);
494 if (err) {
495 goto done;
496 }
497 }
498 err = usbd_req_set_report(sc->sc_udev, NULL, kern_data,
499 len, sc->sc_iface_index, type, id);
500 if (err) {
501 err = ENXIO;
502 goto done;
503 }
504 done:
505 if (free_data) {
506 free(kern_data, M_USBDEV);
507 }
508 return (err);
509 }
510
511 static int
uhid_open(struct usb_fifo * fifo,int fflags)512 uhid_open(struct usb_fifo *fifo, int fflags)
513 {
514 struct uhid_softc *sc = usb_fifo_softc(fifo);
515
516 /*
517 * The buffers are one byte larger than maximum so that one
518 * can detect too large read/writes and short transfers:
519 */
520 if (fflags & FREAD) {
521 /* reset flags */
522 mtx_lock(&sc->sc_mtx);
523 sc->sc_flags &= ~UHID_FLAG_IMMED;
524 mtx_unlock(&sc->sc_mtx);
525
526 if (usb_fifo_alloc_buffer(fifo,
527 sc->sc_isize + 1, UHID_FRAME_NUM)) {
528 return (ENOMEM);
529 }
530 }
531 if (fflags & FWRITE) {
532 if (usb_fifo_alloc_buffer(fifo,
533 sc->sc_osize + 1, UHID_FRAME_NUM)) {
534 return (ENOMEM);
535 }
536 }
537 return (0);
538 }
539
540 static void
uhid_close(struct usb_fifo * fifo,int fflags)541 uhid_close(struct usb_fifo *fifo, int fflags)
542 {
543 if (fflags & (FREAD | FWRITE)) {
544 usb_fifo_free_buffer(fifo);
545 }
546 }
547
548 static int
uhid_ioctl(struct usb_fifo * fifo,u_long cmd,void * addr,int fflags)549 uhid_ioctl(struct usb_fifo *fifo, u_long cmd, void *addr,
550 int fflags)
551 {
552 struct uhid_softc *sc = usb_fifo_softc(fifo);
553 struct usb_gen_descriptor *ugd;
554 #ifdef COMPAT_FREEBSD32
555 struct usb_gen_descriptor local_ugd;
556 struct usb_gen_descriptor32 *ugd32 = NULL;
557 #endif
558 uint32_t size;
559 int error = 0;
560 uint8_t id;
561
562 ugd = addr;
563 #ifdef COMPAT_FREEBSD32
564 switch (cmd) {
565 case USB_GET_REPORT_DESC32:
566 case USB_GET_REPORT32:
567 case USB_SET_REPORT32:
568 ugd32 = addr;
569 ugd = &local_ugd;
570 usb_gen_descriptor_from32(ugd, ugd32);
571 cmd = _IOC_NEWTYPE(cmd, struct usb_gen_descriptor);
572 break;
573 }
574 #endif
575
576 switch (cmd) {
577 case USB_GET_REPORT_DESC:
578 if (sc->sc_repdesc_size > ugd->ugd_maxlen) {
579 size = ugd->ugd_maxlen;
580 } else {
581 size = sc->sc_repdesc_size;
582 }
583 ugd->ugd_actlen = size;
584 if (ugd->ugd_data == NULL)
585 break; /* descriptor length only */
586 error = copyout(sc->sc_repdesc_ptr, ugd->ugd_data, size);
587 break;
588
589 case USB_SET_IMMED:
590 if (!(fflags & FREAD)) {
591 error = EPERM;
592 break;
593 }
594 if (*(int *)addr) {
595 /* do a test read */
596
597 error = uhid_get_report(sc, UHID_INPUT_REPORT,
598 sc->sc_iid, NULL, NULL, sc->sc_isize);
599 if (error) {
600 break;
601 }
602 mtx_lock(&sc->sc_mtx);
603 sc->sc_flags |= UHID_FLAG_IMMED;
604 mtx_unlock(&sc->sc_mtx);
605 } else {
606 mtx_lock(&sc->sc_mtx);
607 sc->sc_flags &= ~UHID_FLAG_IMMED;
608 mtx_unlock(&sc->sc_mtx);
609 }
610 break;
611
612 case USB_GET_REPORT:
613 if (!(fflags & FREAD)) {
614 error = EPERM;
615 break;
616 }
617 switch (ugd->ugd_report_type) {
618 case UHID_INPUT_REPORT:
619 size = sc->sc_isize;
620 id = sc->sc_iid;
621 break;
622 case UHID_OUTPUT_REPORT:
623 size = sc->sc_osize;
624 id = sc->sc_oid;
625 break;
626 case UHID_FEATURE_REPORT:
627 size = sc->sc_fsize;
628 id = sc->sc_fid;
629 break;
630 default:
631 return (EINVAL);
632 }
633 size = imin(ugd->ugd_maxlen, size);
634 if (id != 0)
635 error = copyin(ugd->ugd_data, &id, 1);
636 if (error == 0)
637 error = uhid_get_report(sc, ugd->ugd_report_type, id,
638 NULL, ugd->ugd_data, size);
639 ugd->ugd_actlen = size;
640 break;
641
642 case USB_SET_REPORT:
643 if (!(fflags & FWRITE)) {
644 error = EPERM;
645 break;
646 }
647 switch (ugd->ugd_report_type) {
648 case UHID_INPUT_REPORT:
649 size = sc->sc_isize;
650 id = sc->sc_iid;
651 break;
652 case UHID_OUTPUT_REPORT:
653 size = sc->sc_osize;
654 id = sc->sc_oid;
655 break;
656 case UHID_FEATURE_REPORT:
657 size = sc->sc_fsize;
658 id = sc->sc_fid;
659 break;
660 default:
661 return (EINVAL);
662 }
663 if (id != 0)
664 error = copyin(ugd->ugd_data, &id, 1);
665 if (error == 0)
666 error = uhid_set_report(sc, ugd->ugd_report_type, id,
667 NULL, ugd->ugd_data, imin(ugd->ugd_maxlen, size));
668 break;
669
670 case USB_GET_REPORT_ID:
671 *(int *)addr = 0; /* XXX: we only support reportid 0? */
672 break;
673
674 default:
675 error = ENOIOCTL;
676 break;
677 }
678 #ifdef COMPAT_FREEBSD32
679 if (ugd32 != NULL)
680 update_usb_gen_descriptor32(ugd32, ugd);
681 #endif
682 return (error);
683 }
684
685 static int
uhid_ioctl_post(struct usb_fifo * fifo,u_long cmd,void * addr,int fflags)686 uhid_ioctl_post(struct usb_fifo *fifo, u_long cmd, void *addr,
687 int fflags)
688 {
689 int error;
690
691 switch (cmd) {
692 case USB_GET_DEVICEINFO:
693 error = ugen_fill_deviceinfo(fifo, addr);
694 break;
695
696 default:
697 error = EINVAL;
698 break;
699 }
700 return (error);
701 }
702
703 static const STRUCT_USB_HOST_ID uhid_devs[] = {
704 /* generic HID class */
705 {USB_IFACE_CLASS(UICLASS_HID),},
706 /* the Xbox 360 gamepad doesn't use the HID class */
707 {USB_IFACE_CLASS(UICLASS_VENDOR),
708 USB_IFACE_SUBCLASS(UISUBCLASS_XBOX360_CONTROLLER),
709 USB_IFACE_PROTOCOL(UIPROTO_XBOX360_GAMEPAD),},
710 };
711
712 static int
uhid_probe(device_t dev)713 uhid_probe(device_t dev)
714 {
715 struct usb_attach_arg *uaa = device_get_ivars(dev);
716 int error;
717 void *buf;
718 uint16_t len;
719
720 DPRINTFN(11, "\n");
721
722 if (uaa->usb_mode != USB_MODE_HOST)
723 return (ENXIO);
724
725 error = usbd_lookup_id_by_uaa(uhid_devs, sizeof(uhid_devs), uaa);
726 if (error)
727 return (error);
728
729 if (usb_test_quirk(uaa, UQ_HID_IGNORE))
730 return (ENXIO);
731
732 /*
733 * Don't attach to mouse and keyboard devices, hence then no
734 * "nomatch" event is generated and then ums and ukbd won't
735 * attach properly when loaded.
736 */
737 if ((uaa->info.bInterfaceClass == UICLASS_HID) &&
738 (uaa->info.bInterfaceSubClass == UISUBCLASS_BOOT) &&
739 (((uaa->info.bInterfaceProtocol == UIPROTO_BOOT_KEYBOARD) &&
740 !usb_test_quirk(uaa, UQ_KBD_IGNORE)) ||
741 ((uaa->info.bInterfaceProtocol == UIPROTO_MOUSE) &&
742 !usb_test_quirk(uaa, UQ_UMS_IGNORE))))
743 return (ENXIO);
744
745 /* Check for mandatory multitouch usages to give wmt(4) a chance */
746 if (!usb_test_quirk(uaa, UQ_WMT_IGNORE)) {
747 error = usbd_req_get_hid_desc(uaa->device, NULL,
748 &buf, &len, M_USBDEV, uaa->info.bIfaceIndex);
749 /* Let HID decscriptor-less devices to be handled at attach */
750 if (!error) {
751 if (hid_locate(buf, len,
752 HID_USAGE2(HUP_DIGITIZERS, HUD_CONTACT_MAX),
753 hid_feature, 0, NULL, NULL, NULL) &&
754 hid_locate(buf, len,
755 HID_USAGE2(HUP_DIGITIZERS, HUD_CONTACTID),
756 hid_input, 0, NULL, NULL, NULL)) {
757 free(buf, M_USBDEV);
758 return (ENXIO);
759 }
760 free(buf, M_USBDEV);
761 }
762 }
763
764 return (BUS_PROBE_GENERIC);
765 }
766
767 static int
uhid_attach(device_t dev)768 uhid_attach(device_t dev)
769 {
770 struct usb_attach_arg *uaa = device_get_ivars(dev);
771 struct uhid_softc *sc = device_get_softc(dev);
772 int unit = device_get_unit(dev);
773 int error = 0;
774
775 DPRINTFN(10, "sc=%p\n", sc);
776
777 device_set_usb_desc(dev);
778
779 mtx_init(&sc->sc_mtx, "uhid lock", NULL, MTX_DEF | MTX_RECURSE);
780
781 sc->sc_udev = uaa->device;
782
783 sc->sc_iface_no = uaa->info.bIfaceNum;
784 sc->sc_iface_index = uaa->info.bIfaceIndex;
785
786 error = usbd_transfer_setup(uaa->device,
787 &uaa->info.bIfaceIndex, sc->sc_xfer, uhid_config,
788 UHID_N_TRANSFER, sc, &sc->sc_mtx);
789
790 if (error) {
791 DPRINTF("error=%s\n", usbd_errstr(error));
792 goto detach;
793 }
794 if (uaa->info.idVendor == USB_VENDOR_WACOM) {
795 /* the report descriptor for the Wacom Graphire is broken */
796
797 if (uaa->info.idProduct == USB_PRODUCT_WACOM_GRAPHIRE) {
798 sc->sc_repdesc_size = sizeof(uhid_graphire_report_descr);
799 sc->sc_repdesc_ptr = __DECONST(void *, &uhid_graphire_report_descr);
800 sc->sc_flags |= UHID_FLAG_STATIC_DESC;
801
802 } else if (uaa->info.idProduct == USB_PRODUCT_WACOM_GRAPHIRE3_4X5) {
803 static uint8_t reportbuf[] = {2, 2, 2};
804
805 /*
806 * The Graphire3 needs 0x0202 to be written to
807 * feature report ID 2 before it'll start
808 * returning digitizer data.
809 */
810 error = usbd_req_set_report(uaa->device, NULL,
811 reportbuf, sizeof(reportbuf),
812 uaa->info.bIfaceIndex, UHID_FEATURE_REPORT, 2);
813
814 if (error) {
815 DPRINTF("set report failed, error=%s (ignored)\n",
816 usbd_errstr(error));
817 }
818 sc->sc_repdesc_size = sizeof(uhid_graphire3_4x5_report_descr);
819 sc->sc_repdesc_ptr = __DECONST(void *, &uhid_graphire3_4x5_report_descr);
820 sc->sc_flags |= UHID_FLAG_STATIC_DESC;
821 }
822 } else if ((uaa->info.bInterfaceClass == UICLASS_VENDOR) &&
823 (uaa->info.bInterfaceSubClass == UISUBCLASS_XBOX360_CONTROLLER) &&
824 (uaa->info.bInterfaceProtocol == UIPROTO_XBOX360_GAMEPAD)) {
825 static const uint8_t reportbuf[3] = {1, 3, 0};
826 /*
827 * Turn off the four LEDs on the gamepad which
828 * are blinking by default:
829 */
830 error = usbd_req_set_report(uaa->device, NULL,
831 __DECONST(void *, reportbuf), sizeof(reportbuf),
832 uaa->info.bIfaceIndex, UHID_OUTPUT_REPORT, 0);
833 if (error) {
834 DPRINTF("set output report failed, error=%s (ignored)\n",
835 usbd_errstr(error));
836 }
837 /* the Xbox 360 gamepad has no report descriptor */
838 sc->sc_repdesc_size = sizeof(uhid_xb360gp_report_descr);
839 sc->sc_repdesc_ptr = __DECONST(void *, &uhid_xb360gp_report_descr);
840 sc->sc_flags |= UHID_FLAG_STATIC_DESC;
841 }
842 if (sc->sc_repdesc_ptr == NULL) {
843 error = usbd_req_get_hid_desc(uaa->device, NULL,
844 &sc->sc_repdesc_ptr, &sc->sc_repdesc_size,
845 M_USBDEV, uaa->info.bIfaceIndex);
846
847 if (error) {
848 device_printf(dev, "no report descriptor\n");
849 goto detach;
850 }
851 }
852 error = usbd_req_set_idle(uaa->device, NULL,
853 uaa->info.bIfaceIndex, 0, 0);
854
855 if (error) {
856 DPRINTF("set idle failed, error=%s (ignored)\n",
857 usbd_errstr(error));
858 }
859 sc->sc_isize = hid_report_size_max
860 (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_input, &sc->sc_iid);
861
862 sc->sc_osize = hid_report_size_max
863 (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_output, &sc->sc_oid);
864
865 sc->sc_fsize = hid_report_size_max
866 (sc->sc_repdesc_ptr, sc->sc_repdesc_size, hid_feature, &sc->sc_fid);
867
868 if (sc->sc_isize > UHID_BSIZE) {
869 DPRINTF("input size is too large, "
870 "%d bytes (truncating)\n",
871 sc->sc_isize);
872 sc->sc_isize = UHID_BSIZE;
873 }
874 if (sc->sc_osize > UHID_BSIZE) {
875 DPRINTF("output size is too large, "
876 "%d bytes (truncating)\n",
877 sc->sc_osize);
878 sc->sc_osize = UHID_BSIZE;
879 }
880 if (sc->sc_fsize > UHID_BSIZE) {
881 DPRINTF("feature size is too large, "
882 "%d bytes (truncating)\n",
883 sc->sc_fsize);
884 sc->sc_fsize = UHID_BSIZE;
885 }
886
887 error = usb_fifo_attach(uaa->device, sc, &sc->sc_mtx,
888 &uhid_fifo_methods, &sc->sc_fifo,
889 unit, -1, uaa->info.bIfaceIndex,
890 UID_ROOT, GID_OPERATOR, 0644);
891 if (error) {
892 goto detach;
893 }
894 return (0); /* success */
895
896 detach:
897 uhid_detach(dev);
898 return (ENOMEM);
899 }
900
901 static int
uhid_detach(device_t dev)902 uhid_detach(device_t dev)
903 {
904 struct uhid_softc *sc = device_get_softc(dev);
905
906 usb_fifo_detach(&sc->sc_fifo);
907
908 usbd_transfer_unsetup(sc->sc_xfer, UHID_N_TRANSFER);
909
910 if (sc->sc_repdesc_ptr) {
911 if (!(sc->sc_flags & UHID_FLAG_STATIC_DESC)) {
912 free(sc->sc_repdesc_ptr, M_USBDEV);
913 }
914 }
915 mtx_destroy(&sc->sc_mtx);
916
917 return (0);
918 }
919
920 static device_method_t uhid_methods[] = {
921 DEVMETHOD(device_probe, uhid_probe),
922 DEVMETHOD(device_attach, uhid_attach),
923 DEVMETHOD(device_detach, uhid_detach),
924
925 DEVMETHOD_END
926 };
927
928 static driver_t uhid_driver = {
929 .name = "uhid",
930 .methods = uhid_methods,
931 .size = sizeof(struct uhid_softc),
932 };
933
934 DRIVER_MODULE(uhid, uhub, uhid_driver, NULL, NULL);
935 MODULE_DEPEND(uhid, usb, 1, 1, 1);
936 MODULE_DEPEND(uhid, hid, 1, 1, 1);
937 MODULE_VERSION(uhid, 1);
938 USB_PNP_HOST_INFO(uhid_devs);
939