2 /* $NetBSD: hid.c,v 1.17 2001/11/13 06:24:53 lukem Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
6 * Copyright (c) 1998 The NetBSD Foundation, Inc.
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Lennart Augustsson (lennart@augustsson.net) at
11 * Carlstedt Research & Technology.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
35 #ifdef USB_GLOBAL_INCLUDE_FILE
36 #include USB_GLOBAL_INCLUDE_FILE
38 #include <sys/stdint.h>
39 #include <sys/stddef.h>
40 #include <sys/param.h>
41 #include <sys/queue.h>
42 #include <sys/types.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/module.h>
48 #include <sys/mutex.h>
49 #include <sys/condvar.h>
50 #include <sys/sysctl.h>
52 #include <sys/unistd.h>
53 #include <sys/callout.h>
54 #include <sys/malloc.h>
57 #include <dev/usb/usb.h>
58 #include <dev/usb/usbdi.h>
59 #include <dev/usb/usbdi_util.h>
60 #include <dev/usb/usbhid.h>
62 #define USB_DEBUG_VAR usb_debug
64 #include <dev/usb/usb_core.h>
65 #include <dev/usb/usb_debug.h>
66 #include <dev/usb/usb_process.h>
67 #include <dev/usb/usb_device.h>
68 #include <dev/usb/usb_request.h>
69 #endif /* USB_GLOBAL_INCLUDE_FILE */
71 static void hid_clear_local(struct hid_item *);
72 static uint8_t hid_get_byte(struct hid_data *s, const uint16_t wSize);
77 #define MAXLOCCNT 2048
88 struct hid_item cur[MAXPUSH];
89 struct hid_pos_data last_pos[MAXID];
90 int32_t usages_min[MAXUSAGE];
91 int32_t usages_max[MAXUSAGE];
92 int32_t usage_last; /* last seen usage */
93 uint32_t loc_size; /* last seen size */
94 uint32_t loc_count; /* last seen count */
95 uint32_t ncount; /* end usage item count */
96 uint32_t icount; /* current usage item count */
97 uint8_t kindset; /* we have 5 kinds so 8 bits are enough */
98 uint8_t pushlevel; /* current pushlevel */
99 uint8_t nusage; /* end "usages_min/max" index */
100 uint8_t iusage; /* current "usages_min/max" index */
101 uint8_t ousage; /* current "usages_min/max" offset */
102 uint8_t susage; /* usage set flags */
105 /*------------------------------------------------------------------------*
107 *------------------------------------------------------------------------*/
109 hid_clear_local(struct hid_item *c)
115 c->usage_minimum = 0;
116 c->usage_maximum = 0;
117 c->designator_index = 0;
118 c->designator_minimum = 0;
119 c->designator_maximum = 0;
121 c->string_minimum = 0;
122 c->string_maximum = 0;
123 c->set_delimiter = 0;
127 hid_switch_rid(struct hid_data *s, struct hid_item *c, int32_t next_rID)
131 /* check for same report ID - optimise */
133 if (c->report_ID == next_rID)
136 /* save current position for current rID */
138 if (c->report_ID == 0) {
141 for (i = 1; i != MAXID; i++) {
142 if (s->last_pos[i].rid == c->report_ID)
144 if (s->last_pos[i].rid == 0)
149 s->last_pos[i].rid = c->report_ID;
150 s->last_pos[i].pos = c->loc.pos;
153 /* store next report ID */
155 c->report_ID = next_rID;
157 /* lookup last position for next rID */
162 for (i = 1; i != MAXID; i++) {
163 if (s->last_pos[i].rid == next_rID)
165 if (s->last_pos[i].rid == 0)
170 s->last_pos[i].rid = next_rID;
171 c->loc.pos = s->last_pos[i].pos;
173 DPRINTF("Out of RID entries, position is set to zero!\n");
178 /*------------------------------------------------------------------------*
180 *------------------------------------------------------------------------*/
182 hid_start_parse(const void *d, usb_size_t len, int kindset)
186 if ((kindset-1) & kindset) {
187 DPRINTFN(0, "Only one bit can be "
188 "set in the kindset\n");
192 s = malloc(sizeof *s, M_TEMP, M_WAITOK | M_ZERO);
194 s->end = ((const uint8_t *)d) + len;
195 s->kindset = kindset;
199 /*------------------------------------------------------------------------*
201 *------------------------------------------------------------------------*/
203 hid_end_parse(struct hid_data *s)
211 /*------------------------------------------------------------------------*
212 * get byte from HID descriptor
213 *------------------------------------------------------------------------*/
215 hid_get_byte(struct hid_data *s, const uint16_t wSize)
222 /* check if end is reached */
226 /* read out a byte */
229 /* check if data pointer can be advanced by "wSize" bytes */
230 if ((s->end - ptr) < wSize)
241 /*------------------------------------------------------------------------*
243 *------------------------------------------------------------------------*/
245 hid_get_item(struct hid_data *s, struct hid_item *h)
248 unsigned int bTag, bType, bSize;
256 c = &s->cur[s->pushlevel];
259 /* check if there is an array of items */
260 if (s->icount < s->ncount) {
261 /* get current usage */
262 if (s->iusage < s->nusage) {
263 dval = s->usages_min[s->iusage] + s->ousage;
265 s->usage_last = dval;
266 if (dval == s->usages_max[s->iusage]) {
273 DPRINTFN(1, "Using last usage\n");
274 dval = s->usage_last;
278 * Only copy HID item, increment position and return
279 * if correct kindset!
281 if (s->kindset & (1 << c->kind)) {
283 DPRINTFN(1, "%u,%u,%u\n", h->loc.pos,
284 h->loc.size, h->loc.count);
285 c->loc.pos += c->loc.size * c->loc.count;
290 /* reset state variables */
300 while (s->p != s->end) {
301 bSize = hid_get_byte(s, 1);
304 bSize = hid_get_byte(s, 1);
305 bSize |= hid_get_byte(s, 1) << 8;
306 bTag = hid_get_byte(s, 1);
307 bType = 0xff; /* XXX what should it be */
311 bType = (bSize >> 2) & 3;
322 dval = (int8_t)hid_get_byte(s, 1);
326 dval = hid_get_byte(s, 1);
327 dval |= hid_get_byte(s, 1) << 8;
328 dval = (int16_t)dval;
332 dval = hid_get_byte(s, 1);
333 dval |= hid_get_byte(s, 1) << 8;
334 dval |= hid_get_byte(s, 1) << 16;
335 dval |= hid_get_byte(s, 1) << 24;
339 dval = hid_get_byte(s, bSize);
340 DPRINTFN(0, "bad length %u (data=0x%02x)\n",
352 c->loc.count = s->loc_count;
353 c->loc.size = s->loc_size;
355 if (c->flags & HIO_VARIABLE) {
356 /* range check usage count */
357 if (c->loc.count > MAXLOCCNT) {
358 DPRINTFN(0, "Number of "
359 "items(%u) truncated to %u\n",
360 (unsigned)(c->loc.count),
362 s->ncount = MAXLOCCNT;
364 s->ncount = c->loc.count;
367 * The "top" loop will return
377 c->kind = hid_output;
379 case 10: /* Collection */
380 c->kind = hid_collection;
381 c->collection = dval;
383 c->usage = s->usage_last;
386 case 11: /* Feature */
387 c->kind = hid_feature;
389 case 12: /* End collection */
390 c->kind = hid_endcollection;
391 if (c->collevel == 0) {
392 DPRINTFN(0, "invalid end collection\n");
399 DPRINTFN(0, "Main bTag=%d\n", bTag);
406 c->_usage_page = dval << 16;
409 c->logical_minimum = dval;
412 c->logical_maximum = dval;
415 c->physical_minimum = dval;
418 c->physical_maximum = dval;
421 c->unit_exponent = dval;
427 /* mask because value is unsigned */
428 s->loc_size = dval & mask;
431 hid_switch_rid(s, c, dval & mask);
434 /* mask because value is unsigned */
435 s->loc_count = dval & mask;
438 /* stop parsing, if invalid push level */
439 if ((s->pushlevel + 1) >= MAXPUSH) {
440 DPRINTFN(0, "Cannot push item @ %d\n", s->pushlevel);
444 s->cur[s->pushlevel] = *c;
445 /* store size and count */
446 c->loc.size = s->loc_size;
447 c->loc.count = s->loc_count;
448 /* update current item pointer */
449 c = &s->cur[s->pushlevel];
452 /* stop parsing, if invalid push level */
453 if (s->pushlevel == 0) {
454 DPRINTFN(0, "Cannot pop item @ 0\n");
458 /* preserve position */
460 c = &s->cur[s->pushlevel];
461 /* restore size and count */
462 s->loc_size = c->loc.size;
463 s->loc_count = c->loc.count;
464 /* set default item location */
470 DPRINTFN(0, "Global bTag=%d\n", bTag);
478 dval = (dval & mask) | c->_usage_page;
480 /* set last usage, in case of a collection */
481 s->usage_last = dval;
483 if (s->nusage < MAXUSAGE) {
484 s->usages_min[s->nusage] = dval;
485 s->usages_max[s->nusage] = dval;
488 DPRINTFN(0, "max usage reached\n");
491 /* clear any pending usage sets */
498 dval = (dval & mask) | c->_usage_page;
499 c->usage_minimum = dval;
506 dval = (dval & mask) | c->_usage_page;
507 c->usage_maximum = dval;
514 if ((s->nusage < MAXUSAGE) &&
515 (c->usage_minimum <= c->usage_maximum)) {
516 /* add usage range */
517 s->usages_min[s->nusage] =
519 s->usages_max[s->nusage] =
523 DPRINTFN(0, "Usage set dropped\n");
528 c->designator_index = dval;
531 c->designator_minimum = dval;
534 c->designator_maximum = dval;
537 c->string_index = dval;
540 c->string_minimum = dval;
543 c->string_maximum = dval;
546 c->set_delimiter = dval;
549 DPRINTFN(0, "Local bTag=%d\n", bTag);
554 DPRINTFN(0, "default bType=%d\n", bType);
561 /*------------------------------------------------------------------------*
563 *------------------------------------------------------------------------*/
565 hid_report_size(const void *buf, usb_size_t len, enum hid_kind k, uint8_t *id)
578 for (d = hid_start_parse(buf, len, 1 << k); hid_get_item(d, &h);) {
580 /* check for ID-byte presence */
581 if ((h.report_ID != 0) && !any_id) {
586 /* compute minimum */
587 if (lpos > h.loc.pos)
589 /* compute end position */
590 temp = h.loc.pos + (h.loc.size * h.loc.count);
591 /* compute maximum */
598 /* safety check - can happen in case of currupt descriptors */
604 /* check for ID byte */
610 /* return length in bytes rounded up */
611 return ((temp + 7) / 8);
614 /*------------------------------------------------------------------------*
616 *------------------------------------------------------------------------*/
618 hid_locate(const void *desc, usb_size_t size, int32_t u, enum hid_kind k,
619 uint8_t index, struct hid_location *loc, uint32_t *flags, uint8_t *id)
624 for (d = hid_start_parse(desc, size, 1 << k); hid_get_item(d, &h);) {
625 if (h.kind == k && !(h.flags & HIO_CONST) && h.usage == u) {
648 /*------------------------------------------------------------------------*
650 *------------------------------------------------------------------------*/
652 hid_get_data_sub(const uint8_t *buf, usb_size_t len, struct hid_location *loc,
655 uint32_t hpos = loc->pos;
656 uint32_t hsize = loc->size;
661 DPRINTFN(11, "hid_get_data: loc %d/%d\n", hpos, hsize);
663 /* Range check and limit */
669 /* Get data in a safe way */
677 data |= buf[rpos] << (8 * n);
680 /* Correctly shift down data */
681 data = (data >> (hpos % 8));
684 /* Mask and sign extend in one */
686 data = (int32_t)((int32_t)data << n) >> n;
688 data = (uint32_t)((uint32_t)data << n) >> n;
690 DPRINTFN(11, "hid_get_data: loc %d/%d = %lu\n",
691 loc->pos, loc->size, (long)data);
696 hid_get_data(const uint8_t *buf, usb_size_t len, struct hid_location *loc)
698 return (hid_get_data_sub(buf, len, loc, 1));
702 hid_get_data_unsigned(const uint8_t *buf, usb_size_t len, struct hid_location *loc)
704 return (hid_get_data_sub(buf, len, loc, 0));
707 /*------------------------------------------------------------------------*
709 *------------------------------------------------------------------------*/
711 hid_put_data_unsigned(uint8_t *buf, usb_size_t len,
712 struct hid_location *loc, unsigned int value)
714 uint32_t hpos = loc->pos;
715 uint32_t hsize = loc->size;
721 DPRINTFN(11, "hid_put_data: loc %d/%d = %u\n", hpos, hsize, value);
723 /* Range check and limit */
729 /* Put data in a safe way */
732 data = ((uint64_t)value) << (hpos % 8);
733 mask = ((1ULL << hsize) - 1ULL) << (hpos % 8);
738 buf[rpos] &= ~(mask >> (8 * n));
739 buf[rpos] |= (data >> (8 * n));
744 /*------------------------------------------------------------------------*
746 *------------------------------------------------------------------------*/
748 hid_is_collection(const void *desc, usb_size_t size, int32_t usage)
754 hd = hid_start_parse(desc, size, hid_input);
758 while ((err = hid_get_item(hd, &hi))) {
759 if (hi.kind == hid_collection &&
767 /*------------------------------------------------------------------------*
768 * hid_get_descriptor_from_usb
770 * This function will search for a HID descriptor between two USB
771 * interface descriptors.
774 * NULL: No more HID descriptors.
775 * Else: Pointer to HID descriptor.
776 *------------------------------------------------------------------------*/
777 struct usb_hid_descriptor *
778 hid_get_descriptor_from_usb(struct usb_config_descriptor *cd,
779 struct usb_interface_descriptor *id)
781 struct usb_descriptor *desc = (void *)id;
786 while ((desc = usb_desc_foreach(cd, desc))) {
787 if ((desc->bDescriptorType == UDESC_HID) &&
788 (desc->bLength >= USB_HID_DESCRIPTOR_SIZE(0))) {
791 if (desc->bDescriptorType == UDESC_INTERFACE) {
798 /*------------------------------------------------------------------------*
799 * usbd_req_get_hid_desc
801 * This function will read out an USB report descriptor from the USB
806 * Else: Success. The pointer should eventually be passed to free().
807 *------------------------------------------------------------------------*/
809 usbd_req_get_hid_desc(struct usb_device *udev, struct mtx *mtx,
810 void **descp, uint16_t *sizep,
811 struct malloc_type *mem, uint8_t iface_index)
813 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
814 struct usb_hid_descriptor *hid;
817 if ((iface == NULL) || (iface->idesc == NULL)) {
818 return (USB_ERR_INVAL);
820 hid = hid_get_descriptor_from_usb
821 (usbd_get_config_descriptor(udev), iface->idesc);
824 return (USB_ERR_IOERROR);
826 *sizep = UGETW(hid->descrs[0].wDescriptorLength);
828 return (USB_ERR_IOERROR);
833 *descp = malloc(*sizep, mem, M_ZERO | M_WAITOK);
838 if (*descp == NULL) {
839 return (USB_ERR_NOMEM);
841 err = usbd_req_get_report_descriptor
842 (udev, mtx, *descp, *sizep, iface_index);
849 return (USB_ERR_NORMAL_COMPLETION);
852 /*------------------------------------------------------------------------*
853 * calculate HID item resolution. unit/mm for distances, unit/rad for angles
854 *------------------------------------------------------------------------*/
856 hid_item_resolution(struct hid_item *hi)
859 * hid unit scaling table according to HID Usage Table Review
860 * Request 39 Tbl 17 http://www.usb.org/developers/hidpage/HUTRR39b.pdf
862 static const int64_t scale[0x10][2] = {
866 [0x03] = { 1, 1000 },
867 [0x04] = { 1, 10000 },
868 [0x05] = { 1, 100000 },
869 [0x06] = { 1, 1000000 },
870 [0x07] = { 1, 10000000 },
871 [0x08] = { 100000000, 1 },
872 [0x09] = { 10000000, 1 },
873 [0x0A] = { 1000000, 1 },
874 [0x0B] = { 100000, 1 },
875 [0x0C] = { 10000, 1 },
876 [0x0D] = { 1000, 1 },
880 int64_t logical_size;
881 int64_t physical_size;
907 if ((hi->logical_maximum <= hi->logical_minimum) ||
908 (hi->physical_maximum <= hi->physical_minimum) ||
909 (hi->unit_exponent < 0) || (hi->unit_exponent >= nitems(scale)))
912 logical_size = (int64_t)hi->logical_maximum -
913 (int64_t)hi->logical_minimum;
914 physical_size = (int64_t)hi->physical_maximum -
915 (int64_t)hi->physical_minimum;
916 /* Round to ceiling */
917 resolution = logical_size * multiplier * scale[hi->unit_exponent][0] /
918 (physical_size * divisor * scale[hi->unit_exponent][1]);
920 if (resolution > INT32_MAX)
926 /*------------------------------------------------------------------------*
929 * This function will decide if a USB descriptor belongs to a USB mouse.
932 * Zero: Not a USB mouse.
933 * Else: Is a USB mouse.
934 *------------------------------------------------------------------------*/
936 hid_is_mouse(const void *d_ptr, uint16_t d_len)
943 hd = hid_start_parse(d_ptr, d_len, 1 << hid_input);
950 while (hid_get_item(hd, &hi)) {
955 else if (hi.collection == 1 &&
957 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE))
960 case hid_endcollection:
968 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X) &&
969 (hi.flags & (HIO_CONST|HIO_RELATIVE)) == HIO_RELATIVE)
972 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y) &&
973 (hi.flags & (HIO_CONST|HIO_RELATIVE)) == HIO_RELATIVE)
984 /*------------------------------------------------------------------------*
987 * This function will decide if a USB descriptor belongs to a USB keyboard.
990 * Zero: Not a USB keyboard.
991 * Else: Is a USB keyboard.
992 *------------------------------------------------------------------------*/
994 hid_is_keyboard(const void *d_ptr, uint16_t d_len)
996 if (hid_is_collection(d_ptr, d_len,
997 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD)))