1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2019-2020 Vladimir Kondratyev <wulf@FreeBSD.org>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 #include <sys/param.h>
30 #include <sys/bus.h>
31 #include <sys/ck.h>
32 #include <sys/epoch.h>
33 #include <sys/kdb.h>
34 #include <sys/kernel.h>
35 #include <sys/libkern.h>
36 #include <sys/lock.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/mutex.h>
40 #include <sys/proc.h>
41 #include <sys/systm.h>
42 #include <sys/sx.h>
43
44 #define HID_DEBUG_VAR hid_debug
45 #include <dev/hid/hid.h>
46 #include <dev/hid/hidbus.h>
47 #include <dev/hid/hidquirk.h>
48
49 #include "hid_if.h"
50
51 #define INPUT_EPOCH global_epoch_preempt
52 #define HID_RSIZE_MAX 1024
53
54 static hid_intr_t hidbus_intr;
55
56 static device_probe_t hidbus_probe;
57 static device_attach_t hidbus_attach;
58 static device_detach_t hidbus_detach;
59
60 struct hidbus_ivars {
61 int32_t usage;
62 uint8_t index;
63 uint32_t flags;
64 uintptr_t driver_info; /* for internal use */
65 struct mtx *mtx; /* child intr mtx */
66 hid_intr_t *intr_handler; /* executed under mtx*/
67 void *intr_ctx;
68 unsigned int refcnt; /* protected by mtx */
69 struct epoch_context epoch_ctx;
70 CK_STAILQ_ENTRY(hidbus_ivars) link;
71 };
72
73 struct hidbus_softc {
74 device_t dev;
75 struct sx sx;
76 struct mtx mtx;
77
78 bool nowrite;
79
80 struct hid_rdesc_info rdesc;
81 bool overloaded;
82 int nest; /* Child attach nesting lvl */
83 int nauto; /* Number of autochildren */
84
85 CK_STAILQ_HEAD(, hidbus_ivars) tlcs;
86 };
87
88 static int
hidbus_fill_rdesc_info(struct hid_rdesc_info * hri,const void * data,hid_size_t len)89 hidbus_fill_rdesc_info(struct hid_rdesc_info *hri, const void *data,
90 hid_size_t len)
91 {
92 int error = 0;
93
94 hri->data = __DECONST(void *, data);
95 hri->len = len;
96
97 /*
98 * If report descriptor is not available yet, set maximal
99 * report sizes high enough to allow hidraw to work.
100 */
101 hri->isize = len == 0 ? HID_RSIZE_MAX :
102 hid_report_size_max(data, len, hid_input, &hri->iid);
103 hri->osize = len == 0 ? HID_RSIZE_MAX :
104 hid_report_size_max(data, len, hid_output, &hri->oid);
105 hri->fsize = len == 0 ? HID_RSIZE_MAX :
106 hid_report_size_max(data, len, hid_feature, &hri->fid);
107
108 if (hri->isize > HID_RSIZE_MAX) {
109 DPRINTF("input size is too large, %u bytes (truncating)\n",
110 hri->isize);
111 hri->isize = HID_RSIZE_MAX;
112 error = EOVERFLOW;
113 }
114 if (hri->osize > HID_RSIZE_MAX) {
115 DPRINTF("output size is too large, %u bytes (truncating)\n",
116 hri->osize);
117 hri->osize = HID_RSIZE_MAX;
118 error = EOVERFLOW;
119 }
120 if (hri->fsize > HID_RSIZE_MAX) {
121 DPRINTF("feature size is too large, %u bytes (truncating)\n",
122 hri->fsize);
123 hri->fsize = HID_RSIZE_MAX;
124 error = EOVERFLOW;
125 }
126
127 return (error);
128 }
129
130 int
hidbus_locate(const void * desc,hid_size_t size,int32_t u,enum hid_kind k,uint8_t tlc_index,uint8_t index,struct hid_location * loc,uint32_t * flags,uint8_t * id,struct hid_absinfo * ai)131 hidbus_locate(const void *desc, hid_size_t size, int32_t u, enum hid_kind k,
132 uint8_t tlc_index, uint8_t index, struct hid_location *loc,
133 uint32_t *flags, uint8_t *id, struct hid_absinfo *ai)
134 {
135 struct hid_data *d;
136 struct hid_item h;
137 int i;
138
139 d = hid_start_parse(desc, size, 1 << k);
140 HIDBUS_FOREACH_ITEM(d, &h, tlc_index) {
141 for (i = 0; i < h.nusages; i++) {
142 if (h.kind == k && h.usages[i] == u) {
143 if (index--)
144 break;
145 if (loc != NULL)
146 *loc = h.loc;
147 if (flags != NULL)
148 *flags = h.flags;
149 if (id != NULL)
150 *id = h.report_ID;
151 if (ai != NULL && (h.flags&HIO_RELATIVE) == 0)
152 *ai = (struct hid_absinfo) {
153 .max = h.logical_maximum,
154 .min = h.logical_minimum,
155 .res = hid_item_resolution(&h),
156 };
157 hid_end_parse(d);
158 return (1);
159 }
160 }
161 }
162 if (loc != NULL)
163 loc->size = 0;
164 if (flags != NULL)
165 *flags = 0;
166 if (id != NULL)
167 *id = 0;
168 hid_end_parse(d);
169 return (0);
170 }
171
172 bool
hidbus_is_collection(const void * desc,hid_size_t size,int32_t usage,uint8_t tlc_index)173 hidbus_is_collection(const void *desc, hid_size_t size, int32_t usage,
174 uint8_t tlc_index)
175 {
176 struct hid_data *d;
177 struct hid_item h;
178 bool ret = false;
179
180 d = hid_start_parse(desc, size, 0);
181 HIDBUS_FOREACH_ITEM(d, &h, tlc_index) {
182 if (h.kind == hid_collection && h.usage == usage) {
183 ret = true;
184 break;
185 }
186 }
187 hid_end_parse(d);
188 return (ret);
189 }
190
191 static device_t
hidbus_add_child(device_t dev,u_int order,const char * name,int unit)192 hidbus_add_child(device_t dev, u_int order, const char *name, int unit)
193 {
194 struct hidbus_softc *sc = device_get_softc(dev);
195 struct hidbus_ivars *tlc;
196 device_t child;
197
198 child = device_add_child_ordered(dev, order, name, unit);
199 if (child == NULL)
200 return (child);
201
202 tlc = malloc(sizeof(struct hidbus_ivars), M_DEVBUF, M_WAITOK | M_ZERO);
203 tlc->mtx = &sc->mtx;
204 device_set_ivars(child, tlc);
205 sx_xlock(&sc->sx);
206 CK_STAILQ_INSERT_TAIL(&sc->tlcs, tlc, link);
207 sx_unlock(&sc->sx);
208
209 return (child);
210 }
211
212 static int
hidbus_enumerate_children(device_t dev,const void * data,hid_size_t len)213 hidbus_enumerate_children(device_t dev, const void* data, hid_size_t len)
214 {
215 struct hidbus_softc *sc = device_get_softc(dev);
216 struct hid_data *hd;
217 struct hid_item hi;
218 device_t child;
219 uint8_t index = 0;
220
221 if (data == NULL || len == 0)
222 return (ENXIO);
223
224 /* Add a child for each top level collection */
225 hd = hid_start_parse(data, len, 1 << hid_input);
226 while (hid_get_item(hd, &hi)) {
227 if (hi.kind != hid_collection || hi.collevel != 1)
228 continue;
229 child = BUS_ADD_CHILD(dev, 0, NULL, -1);
230 if (child == NULL) {
231 device_printf(dev, "Could not add HID device\n");
232 continue;
233 }
234 hidbus_set_index(child, index);
235 hidbus_set_usage(child, hi.usage);
236 hidbus_set_flags(child, HIDBUS_FLAG_AUTOCHILD);
237 index++;
238 DPRINTF("Add child TLC: 0x%04x:0x%04x\n",
239 HID_GET_USAGE_PAGE(hi.usage), HID_GET_USAGE(hi.usage));
240 }
241 hid_end_parse(hd);
242
243 if (index == 0)
244 return (ENXIO);
245
246 sc->nauto = index;
247
248 return (0);
249 }
250
251 static int
hidbus_attach_children(device_t dev)252 hidbus_attach_children(device_t dev)
253 {
254 struct hidbus_softc *sc = device_get_softc(dev);
255 int error;
256
257 HID_INTR_SETUP(device_get_parent(dev), hidbus_intr, sc, &sc->rdesc);
258
259 error = hidbus_enumerate_children(dev, sc->rdesc.data, sc->rdesc.len);
260 if (error != 0)
261 DPRINTF("failed to enumerate children: error %d\n", error);
262
263 /*
264 * hidbus_attach_children() can recurse through device_identify->
265 * hid_set_report_descr() call sequence. Do not perform children
266 * attach twice in that case.
267 */
268 sc->nest++;
269 bus_generic_probe(dev);
270 sc->nest--;
271 if (sc->nest != 0)
272 return (0);
273
274 if (hid_is_keyboard(sc->rdesc.data, sc->rdesc.len) != 0)
275 error = bus_generic_attach(dev);
276 else
277 error = bus_delayed_attach_children(dev);
278 if (error != 0)
279 device_printf(dev, "failed to attach child: error %d\n", error);
280
281 return (error);
282 }
283
284 static int
hidbus_detach_children(device_t dev)285 hidbus_detach_children(device_t dev)
286 {
287 device_t *children, bus;
288 bool is_bus;
289 int i, error;
290
291 error = 0;
292
293 is_bus = device_get_devclass(dev) == hidbus_devclass;
294 bus = is_bus ? dev : device_get_parent(dev);
295
296 KASSERT(device_get_devclass(bus) == hidbus_devclass,
297 ("Device is not hidbus or it's child"));
298
299 if (is_bus) {
300 /* If hidbus is passed, delete all children. */
301 bus_generic_detach(bus);
302 device_delete_children(bus);
303 } else {
304 /*
305 * If hidbus child is passed, delete all hidbus children
306 * except caller. Deleting the caller may result in deadlock.
307 */
308 error = device_get_children(bus, &children, &i);
309 if (error != 0)
310 return (error);
311 while (i-- > 0) {
312 if (children[i] == dev)
313 continue;
314 DPRINTF("Delete child. index=%d (%s)\n",
315 hidbus_get_index(children[i]),
316 device_get_nameunit(children[i]));
317 error = device_delete_child(bus, children[i]);
318 if (error) {
319 DPRINTF("Failed deleting %s\n",
320 device_get_nameunit(children[i]));
321 break;
322 }
323 }
324 free(children, M_TEMP);
325 }
326
327 HID_INTR_UNSETUP(device_get_parent(bus));
328
329 return (error);
330 }
331
332 static int
hidbus_probe(device_t dev)333 hidbus_probe(device_t dev)
334 {
335
336 device_set_desc(dev, "HID bus");
337
338 /* Allow other subclasses to override this driver. */
339 return (BUS_PROBE_GENERIC);
340 }
341
342 static int
hidbus_attach(device_t dev)343 hidbus_attach(device_t dev)
344 {
345 struct hidbus_softc *sc = device_get_softc(dev);
346 struct hid_device_info *devinfo = device_get_ivars(dev);
347 void *d_ptr = NULL;
348 hid_size_t d_len;
349 int error;
350
351 sc->dev = dev;
352 CK_STAILQ_INIT(&sc->tlcs);
353 mtx_init(&sc->mtx, "hidbus ivar lock", NULL, MTX_DEF);
354 sx_init(&sc->sx, "hidbus ivar list lock");
355
356 /*
357 * Ignore error. It is possible for non-HID device e.g. XBox360 gamepad
358 * to emulate HID through overloading of report descriptor.
359 */
360 d_len = devinfo->rdescsize;
361 if (d_len != 0) {
362 d_ptr = malloc(d_len, M_DEVBUF, M_ZERO | M_WAITOK);
363 error = hid_get_rdesc(dev, d_ptr, d_len);
364 if (error != 0) {
365 free(d_ptr, M_DEVBUF);
366 d_len = 0;
367 d_ptr = NULL;
368 }
369 }
370
371 hidbus_fill_rdesc_info(&sc->rdesc, d_ptr, d_len);
372
373 sc->nowrite = hid_test_quirk(devinfo, HQ_NOWRITE);
374
375 error = hidbus_attach_children(dev);
376 if (error != 0) {
377 hidbus_detach(dev);
378 return (ENXIO);
379 }
380
381 return (0);
382 }
383
384 static int
hidbus_detach(device_t dev)385 hidbus_detach(device_t dev)
386 {
387 struct hidbus_softc *sc = device_get_softc(dev);
388
389 hidbus_detach_children(dev);
390 sx_destroy(&sc->sx);
391 mtx_destroy(&sc->mtx);
392 free(sc->rdesc.data, M_DEVBUF);
393
394 return (0);
395 }
396
397 static void
hidbus_child_detached(device_t bus,device_t child)398 hidbus_child_detached(device_t bus, device_t child)
399 {
400 struct hidbus_softc *sc = device_get_softc(bus);
401 struct hidbus_ivars *tlc = device_get_ivars(child);
402
403 KASSERT(tlc->refcnt == 0, ("Child device is running"));
404 tlc->mtx = &sc->mtx;
405 tlc->intr_handler = NULL;
406 tlc->flags &= ~HIDBUS_FLAG_CAN_POLL;
407 }
408
409 /*
410 * Epoch callback indicating tlc is safe to destroy
411 */
412 static void
hidbus_ivar_dtor(epoch_context_t ctx)413 hidbus_ivar_dtor(epoch_context_t ctx)
414 {
415 struct hidbus_ivars *tlc;
416
417 tlc = __containerof(ctx, struct hidbus_ivars, epoch_ctx);
418 free(tlc, M_DEVBUF);
419 }
420
421 static void
hidbus_child_deleted(device_t bus,device_t child)422 hidbus_child_deleted(device_t bus, device_t child)
423 {
424 struct hidbus_softc *sc = device_get_softc(bus);
425 struct hidbus_ivars *tlc = device_get_ivars(child);
426
427 sx_xlock(&sc->sx);
428 KASSERT(tlc->refcnt == 0, ("Child device is running"));
429 CK_STAILQ_REMOVE(&sc->tlcs, tlc, hidbus_ivars, link);
430 sx_unlock(&sc->sx);
431 epoch_call(INPUT_EPOCH, hidbus_ivar_dtor, &tlc->epoch_ctx);
432 }
433
434 static int
hidbus_read_ivar(device_t bus,device_t child,int which,uintptr_t * result)435 hidbus_read_ivar(device_t bus, device_t child, int which, uintptr_t *result)
436 {
437 struct hidbus_softc *sc = device_get_softc(bus);
438 struct hidbus_ivars *tlc = device_get_ivars(child);
439
440 switch (which) {
441 case HIDBUS_IVAR_INDEX:
442 *result = tlc->index;
443 break;
444 case HIDBUS_IVAR_USAGE:
445 *result = tlc->usage;
446 break;
447 case HIDBUS_IVAR_FLAGS:
448 *result = tlc->flags;
449 break;
450 case HIDBUS_IVAR_DRIVER_INFO:
451 *result = tlc->driver_info;
452 break;
453 case HIDBUS_IVAR_LOCK:
454 *result = (uintptr_t)(tlc->mtx == &sc->mtx ? NULL : tlc->mtx);
455 break;
456 default:
457 return (EINVAL);
458 }
459 return (0);
460 }
461
462 static int
hidbus_write_ivar(device_t bus,device_t child,int which,uintptr_t value)463 hidbus_write_ivar(device_t bus, device_t child, int which, uintptr_t value)
464 {
465 struct hidbus_softc *sc = device_get_softc(bus);
466 struct hidbus_ivars *tlc = device_get_ivars(child);
467
468 switch (which) {
469 case HIDBUS_IVAR_INDEX:
470 tlc->index = value;
471 break;
472 case HIDBUS_IVAR_USAGE:
473 tlc->usage = value;
474 break;
475 case HIDBUS_IVAR_FLAGS:
476 tlc->flags = value;
477 if ((value & HIDBUS_FLAG_CAN_POLL) != 0)
478 HID_INTR_SETUP(
479 device_get_parent(bus), NULL, NULL, NULL);
480 break;
481 case HIDBUS_IVAR_DRIVER_INFO:
482 tlc->driver_info = value;
483 break;
484 case HIDBUS_IVAR_LOCK:
485 tlc->mtx = (struct mtx *)value == NULL ?
486 &sc->mtx : (struct mtx *)value;
487 break;
488 default:
489 return (EINVAL);
490 }
491 return (0);
492 }
493
494 /* Location hint for devctl(8) */
495 static int
hidbus_child_location_str(device_t bus,device_t child,char * buf,size_t buflen)496 hidbus_child_location_str(device_t bus, device_t child, char *buf,
497 size_t buflen)
498 {
499 struct hidbus_ivars *tlc = device_get_ivars(child);
500
501 snprintf(buf, buflen, "index=%hhu", tlc->index);
502 return (0);
503 }
504
505 /* PnP information for devctl(8) */
506 static int
hidbus_child_pnpinfo_str(device_t bus,device_t child,char * buf,size_t buflen)507 hidbus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
508 size_t buflen)
509 {
510 struct hidbus_ivars *tlc = device_get_ivars(child);
511 struct hid_device_info *devinfo = device_get_ivars(bus);
512
513 snprintf(buf, buflen, "page=0x%04x usage=0x%04x bus=0x%02hx "
514 "vendor=0x%04hx product=0x%04hx version=0x%04hx%s%s",
515 HID_GET_USAGE_PAGE(tlc->usage), HID_GET_USAGE(tlc->usage),
516 devinfo->idBus, devinfo->idVendor, devinfo->idProduct,
517 devinfo->idVersion, devinfo->idPnP[0] == '\0' ? "" : " _HID=",
518 devinfo->idPnP[0] == '\0' ? "" : devinfo->idPnP);
519 return (0);
520 }
521
522 void
hidbus_set_desc(device_t child,const char * suffix)523 hidbus_set_desc(device_t child, const char *suffix)
524 {
525 device_t bus = device_get_parent(child);
526 struct hidbus_softc *sc = device_get_softc(bus);
527 struct hid_device_info *devinfo = device_get_ivars(bus);
528 struct hidbus_ivars *tlc = device_get_ivars(child);
529 char buf[80];
530
531 /* Do not add NULL suffix or if device name already contains it. */
532 if (suffix != NULL && strcasestr(devinfo->name, suffix) == NULL &&
533 (sc->nauto > 1 || (tlc->flags & HIDBUS_FLAG_AUTOCHILD) == 0)) {
534 snprintf(buf, sizeof(buf), "%s %s", devinfo->name, suffix);
535 device_set_desc_copy(child, buf);
536 } else
537 device_set_desc(child, devinfo->name);
538 }
539
540 device_t
hidbus_find_child(device_t bus,int32_t usage)541 hidbus_find_child(device_t bus, int32_t usage)
542 {
543 device_t *children, child;
544 int ccount, i;
545
546 GIANT_REQUIRED;
547
548 /* Get a list of all hidbus children */
549 if (device_get_children(bus, &children, &ccount) != 0)
550 return (NULL);
551
552 /* Scan through to find required TLC */
553 for (i = 0, child = NULL; i < ccount; i++) {
554 if (hidbus_get_usage(children[i]) == usage) {
555 child = children[i];
556 break;
557 }
558 }
559 free(children, M_TEMP);
560
561 return (child);
562 }
563
564 void
hidbus_intr(void * context,void * buf,hid_size_t len)565 hidbus_intr(void *context, void *buf, hid_size_t len)
566 {
567 struct hidbus_softc *sc = context;
568 struct hidbus_ivars *tlc;
569 struct epoch_tracker et;
570
571 /*
572 * Broadcast input report to all subscribers.
573 * TODO: Add check for input report ID.
574 *
575 * Relock mutex on every TLC item as we can't hold any locks over whole
576 * TLC list here due to LOR with open()/close() handlers.
577 */
578 if (!HID_IN_POLLING_MODE())
579 epoch_enter_preempt(INPUT_EPOCH, &et);
580 CK_STAILQ_FOREACH(tlc, &sc->tlcs, link) {
581 if (tlc->refcnt == 0 || tlc->intr_handler == NULL)
582 continue;
583 if (HID_IN_POLLING_MODE()) {
584 if ((tlc->flags & HIDBUS_FLAG_CAN_POLL) != 0)
585 tlc->intr_handler(tlc->intr_ctx, buf, len);
586 } else {
587 mtx_lock(tlc->mtx);
588 tlc->intr_handler(tlc->intr_ctx, buf, len);
589 mtx_unlock(tlc->mtx);
590 }
591 }
592 if (!HID_IN_POLLING_MODE())
593 epoch_exit_preempt(INPUT_EPOCH, &et);
594 }
595
596 void
hidbus_set_intr(device_t child,hid_intr_t * handler,void * context)597 hidbus_set_intr(device_t child, hid_intr_t *handler, void *context)
598 {
599 struct hidbus_ivars *tlc = device_get_ivars(child);
600
601 tlc->intr_handler = handler;
602 tlc->intr_ctx = context;
603 }
604
605 int
hidbus_intr_start(device_t child)606 hidbus_intr_start(device_t child)
607 {
608 device_t bus = device_get_parent(child);
609 struct hidbus_softc *sc = device_get_softc(bus);
610 struct hidbus_ivars *ivar = device_get_ivars(child);
611 struct hidbus_ivars *tlc;
612 bool refcnted = false;
613 int error;
614
615 if (sx_xlock_sig(&sc->sx) != 0)
616 return (EINTR);
617 CK_STAILQ_FOREACH(tlc, &sc->tlcs, link) {
618 refcnted |= (tlc->refcnt != 0);
619 if (tlc == ivar) {
620 mtx_lock(tlc->mtx);
621 ++tlc->refcnt;
622 mtx_unlock(tlc->mtx);
623 }
624 }
625 error = refcnted ? 0 : HID_INTR_START(device_get_parent(bus));
626 sx_unlock(&sc->sx);
627
628 return (error);
629 }
630
631 int
hidbus_intr_stop(device_t child)632 hidbus_intr_stop(device_t child)
633 {
634 device_t bus = device_get_parent(child);
635 struct hidbus_softc *sc = device_get_softc(bus);
636 struct hidbus_ivars *ivar = device_get_ivars(child);
637 struct hidbus_ivars *tlc;
638 bool refcnted = false;
639 int error;
640
641 if (sx_xlock_sig(&sc->sx) != 0)
642 return (EINTR);
643 CK_STAILQ_FOREACH(tlc, &sc->tlcs, link) {
644 if (tlc == ivar) {
645 mtx_lock(tlc->mtx);
646 MPASS(tlc->refcnt != 0);
647 --tlc->refcnt;
648 mtx_unlock(tlc->mtx);
649 }
650 refcnted |= (tlc->refcnt != 0);
651 }
652 error = refcnted ? 0 : HID_INTR_STOP(device_get_parent(bus));
653 sx_unlock(&sc->sx);
654
655 return (error);
656 }
657
658 void
hidbus_intr_poll(device_t child)659 hidbus_intr_poll(device_t child)
660 {
661 device_t bus = device_get_parent(child);
662
663 HID_INTR_POLL(device_get_parent(bus));
664 }
665
666 struct hid_rdesc_info *
hidbus_get_rdesc_info(device_t child)667 hidbus_get_rdesc_info(device_t child)
668 {
669 device_t bus = device_get_parent(child);
670 struct hidbus_softc *sc = device_get_softc(bus);
671
672 return (&sc->rdesc);
673 }
674
675 /*
676 * HID interface.
677 *
678 * Hidbus as well as any hidbus child can be passed as first arg.
679 */
680
681 /* Read cached report descriptor */
682 int
hid_get_report_descr(device_t dev,void ** data,hid_size_t * len)683 hid_get_report_descr(device_t dev, void **data, hid_size_t *len)
684 {
685 device_t bus;
686 struct hidbus_softc *sc;
687
688 bus = device_get_devclass(dev) == hidbus_devclass ?
689 dev : device_get_parent(dev);
690 sc = device_get_softc(bus);
691
692 /*
693 * Do not send request to a transport backend.
694 * Use cached report descriptor instead of it.
695 */
696 if (sc->rdesc.data == NULL || sc->rdesc.len == 0)
697 return (ENXIO);
698
699 if (data != NULL)
700 *data = sc->rdesc.data;
701 if (len != NULL)
702 *len = sc->rdesc.len;
703
704 return (0);
705 }
706
707 /*
708 * Replace cached report descriptor with top level driver provided one.
709 *
710 * It deletes all hidbus children except caller and enumerates them again after
711 * new descriptor has been registered. Currently it can not be called from
712 * autoenumerated (by report's TLC) child device context as it results in child
713 * duplication. To overcome this limitation hid_set_report_descr() should be
714 * called from device_identify driver's handler with hidbus itself passed as
715 * 'device_t dev' parameter.
716 */
717 int
hid_set_report_descr(device_t dev,const void * data,hid_size_t len)718 hid_set_report_descr(device_t dev, const void *data, hid_size_t len)
719 {
720 struct hid_rdesc_info rdesc;
721 device_t bus;
722 struct hidbus_softc *sc;
723 bool is_bus;
724 int error;
725
726 GIANT_REQUIRED;
727
728 is_bus = device_get_devclass(dev) == hidbus_devclass;
729 bus = is_bus ? dev : device_get_parent(dev);
730 sc = device_get_softc(bus);
731
732 /*
733 * Do not overload already overloaded report descriptor in
734 * device_identify handler. It causes infinite recursion loop.
735 */
736 if (is_bus && sc->overloaded)
737 return(0);
738
739 DPRINTFN(5, "len=%d\n", len);
740 DPRINTFN(5, "data = %*D\n", len, data, " ");
741
742 error = hidbus_fill_rdesc_info(&rdesc, data, len);
743 if (error != 0)
744 return (error);
745
746 error = hidbus_detach_children(dev);
747 if (error != 0)
748 return(error);
749
750 /* Make private copy to handle a case of dynamicaly allocated data. */
751 rdesc.data = malloc(len, M_DEVBUF, M_ZERO | M_WAITOK);
752 bcopy(data, rdesc.data, len);
753 sc->overloaded = true;
754 free(sc->rdesc.data, M_DEVBUF);
755 bcopy(&rdesc, &sc->rdesc, sizeof(struct hid_rdesc_info));
756
757 error = hidbus_attach_children(bus);
758
759 return (error);
760 }
761
762 static int
hidbus_write(device_t dev,const void * data,hid_size_t len)763 hidbus_write(device_t dev, const void *data, hid_size_t len)
764 {
765 struct hidbus_softc *sc;
766 uint8_t id;
767
768 sc = device_get_softc(dev);
769 /*
770 * Output interrupt endpoint is often optional. If HID device
771 * does not provide it, send reports via control pipe.
772 */
773 if (sc->nowrite) {
774 /* try to extract the ID byte */
775 id = (sc->rdesc.oid & (len > 0)) ? *(const uint8_t*)data : 0;
776 return (hid_set_report(dev, data, len, HID_OUTPUT_REPORT, id));
777 }
778
779 return (hid_write(dev, data, len));
780 }
781
782 /*------------------------------------------------------------------------*
783 * hidbus_lookup_id
784 *
785 * This functions takes an array of "struct hid_device_id" and tries
786 * to match the entries with the information in "struct hid_device_info".
787 *
788 * Return values:
789 * NULL: No match found.
790 * Else: Pointer to matching entry.
791 *------------------------------------------------------------------------*/
792 const struct hid_device_id *
hidbus_lookup_id(device_t dev,const struct hid_device_id * id,int nitems_id)793 hidbus_lookup_id(device_t dev, const struct hid_device_id *id, int nitems_id)
794 {
795 const struct hid_device_id *id_end;
796 const struct hid_device_info *info;
797 int32_t usage;
798 bool is_child;
799
800 if (id == NULL) {
801 goto done;
802 }
803
804 id_end = id + nitems_id;
805 info = hid_get_device_info(dev);
806 is_child = device_get_devclass(dev) != hidbus_devclass;
807 if (is_child)
808 usage = hidbus_get_usage(dev);
809
810 /*
811 * Keep on matching array entries until we find a match or
812 * until we reach the end of the matching array:
813 */
814 for (; id != id_end; id++) {
815
816 if (is_child && (id->match_flag_page) &&
817 (id->page != HID_GET_USAGE_PAGE(usage))) {
818 continue;
819 }
820 if (is_child && (id->match_flag_usage) &&
821 (id->usage != HID_GET_USAGE(usage))) {
822 continue;
823 }
824 if ((id->match_flag_bus) &&
825 (id->idBus != info->idBus)) {
826 continue;
827 }
828 if ((id->match_flag_vendor) &&
829 (id->idVendor != info->idVendor)) {
830 continue;
831 }
832 if ((id->match_flag_product) &&
833 (id->idProduct != info->idProduct)) {
834 continue;
835 }
836 if ((id->match_flag_ver_lo) &&
837 (id->idVersion_lo > info->idVersion)) {
838 continue;
839 }
840 if ((id->match_flag_ver_hi) &&
841 (id->idVersion_hi < info->idVersion)) {
842 continue;
843 }
844 if (id->match_flag_pnp &&
845 strncmp(id->idPnP, info->idPnP, HID_PNP_ID_SIZE) != 0) {
846 continue;
847 }
848 /* We found a match! */
849 return (id);
850 }
851
852 done:
853 return (NULL);
854 }
855
856 /*------------------------------------------------------------------------*
857 * hidbus_lookup_driver_info - factored out code
858 *
859 * Return values:
860 * 0: Success
861 * Else: Failure
862 *------------------------------------------------------------------------*/
863 int
hidbus_lookup_driver_info(device_t child,const struct hid_device_id * id,int nitems_id)864 hidbus_lookup_driver_info(device_t child, const struct hid_device_id *id,
865 int nitems_id)
866 {
867
868 id = hidbus_lookup_id(child, id, nitems_id);
869 if (id) {
870 /* copy driver info */
871 hidbus_set_driver_info(child, id->driver_info);
872 return (0);
873 }
874 return (ENXIO);
875 }
876
877 const struct hid_device_info *
hid_get_device_info(device_t dev)878 hid_get_device_info(device_t dev)
879 {
880 device_t bus;
881
882 bus = device_get_devclass(dev) == hidbus_devclass ?
883 dev : device_get_parent(dev);
884
885 return (device_get_ivars(bus));
886 }
887
888 static device_method_t hidbus_methods[] = {
889 /* device interface */
890 DEVMETHOD(device_probe, hidbus_probe),
891 DEVMETHOD(device_attach, hidbus_attach),
892 DEVMETHOD(device_detach, hidbus_detach),
893 DEVMETHOD(device_suspend, bus_generic_suspend),
894 DEVMETHOD(device_resume, bus_generic_resume),
895
896 /* bus interface */
897 DEVMETHOD(bus_add_child, hidbus_add_child),
898 DEVMETHOD(bus_child_detached, hidbus_child_detached),
899 DEVMETHOD(bus_child_deleted, hidbus_child_deleted),
900 DEVMETHOD(bus_read_ivar, hidbus_read_ivar),
901 DEVMETHOD(bus_write_ivar, hidbus_write_ivar),
902 DEVMETHOD(bus_child_pnpinfo_str,hidbus_child_pnpinfo_str),
903 DEVMETHOD(bus_child_location_str,hidbus_child_location_str),
904
905 /* hid interface */
906 DEVMETHOD(hid_get_rdesc, hid_get_rdesc),
907 DEVMETHOD(hid_read, hid_read),
908 DEVMETHOD(hid_write, hidbus_write),
909 DEVMETHOD(hid_get_report, hid_get_report),
910 DEVMETHOD(hid_set_report, hid_set_report),
911 DEVMETHOD(hid_set_idle, hid_set_idle),
912 DEVMETHOD(hid_set_protocol, hid_set_protocol),
913 DEVMETHOD(hid_ioctl, hid_ioctl),
914
915 DEVMETHOD_END
916 };
917
918 devclass_t hidbus_devclass;
919 driver_t hidbus_driver = {
920 "hidbus",
921 hidbus_methods,
922 sizeof(struct hidbus_softc),
923 };
924
925 MODULE_DEPEND(hidbus, hid, 1, 1, 1);
926 MODULE_VERSION(hidbus, 1);
927 DRIVER_MODULE(hidbus, hvhid, hidbus_driver, hidbus_devclass, 0, 0);
928 DRIVER_MODULE(hidbus, iichid, hidbus_driver, hidbus_devclass, 0, 0);
929 DRIVER_MODULE(hidbus, usbhid, hidbus_driver, hidbus_devclass, 0, 0);
930