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