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 1024
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) {
302 bSize = hid_get_byte(s, 1);
305 bSize = hid_get_byte(s, 1);
306 bSize |= hid_get_byte(s, 1) << 8;
307 bTag = hid_get_byte(s, 1);
308 bType = 0xff; /* XXX what should it be */
312 bType = (bSize >> 2) & 3;
323 dval = (int8_t)hid_get_byte(s, 1);
327 dval = hid_get_byte(s, 1);
328 dval |= hid_get_byte(s, 1) << 8;
329 dval = (int16_t)dval;
333 dval = hid_get_byte(s, 1);
334 dval |= hid_get_byte(s, 1) << 8;
335 dval |= hid_get_byte(s, 1) << 16;
336 dval |= hid_get_byte(s, 1) << 24;
340 dval = hid_get_byte(s, bSize);
341 DPRINTFN(0, "bad length %u (data=0x%02x)\n",
353 c->loc.count = s->loc_count;
354 c->loc.size = s->loc_size;
356 if (c->flags & HIO_VARIABLE) {
357 /* range check usage count */
358 if (c->loc.count > MAXLOCCNT) {
359 DPRINTFN(0, "Number of "
360 "items(%u) truncated to %u\n",
361 (unsigned)(c->loc.count),
363 s->ncount = MAXLOCCNT;
365 s->ncount = c->loc.count;
368 * The "top" loop will return
378 c->kind = hid_output;
380 case 10: /* Collection */
381 c->kind = hid_collection;
382 c->collection = dval;
384 c->usage = s->usage_last;
387 case 11: /* Feature */
388 c->kind = hid_feature;
390 case 12: /* End collection */
391 c->kind = hid_endcollection;
392 if (c->collevel == 0) {
393 DPRINTFN(0, "invalid end collection\n");
400 DPRINTFN(0, "Main bTag=%d\n", bTag);
407 c->_usage_page = dval << 16;
410 c->logical_minimum = dval;
413 c->logical_maximum = dval;
416 c->physical_minimum = dval;
419 c->physical_maximum = dval;
422 c->unit_exponent = dval;
428 /* mask because value is unsigned */
429 s->loc_size = dval & mask;
432 hid_switch_rid(s, c, dval & mask);
435 /* mask because value is unsigned */
436 s->loc_count = dval & mask;
440 if (s->pushlevel < MAXPUSH) {
441 s->cur[s->pushlevel] = *c;
442 /* store size and count */
443 c->loc.size = s->loc_size;
444 c->loc.count = s->loc_count;
445 /* update current item pointer */
446 c = &s->cur[s->pushlevel];
448 DPRINTFN(0, "Cannot push "
449 "item @ %d\n", s->pushlevel);
454 if (s->pushlevel < MAXPUSH) {
455 /* preserve position */
457 c = &s->cur[s->pushlevel];
458 /* restore size and count */
459 s->loc_size = c->loc.size;
460 s->loc_count = c->loc.count;
461 /* set default item location */
466 DPRINTFN(0, "Cannot pop "
467 "item @ %d\n", s->pushlevel);
471 DPRINTFN(0, "Global bTag=%d\n", bTag);
479 dval = (dval & mask) | c->_usage_page;
481 /* set last usage, in case of a collection */
482 s->usage_last = dval;
484 if (s->nusage < MAXUSAGE) {
485 s->usages_min[s->nusage] = dval;
486 s->usages_max[s->nusage] = dval;
489 DPRINTFN(0, "max usage reached\n");
492 /* clear any pending usage sets */
499 dval = (dval & mask) | c->_usage_page;
500 c->usage_minimum = dval;
507 dval = (dval & mask) | c->_usage_page;
508 c->usage_maximum = dval;
515 if ((s->nusage < MAXUSAGE) &&
516 (c->usage_minimum <= c->usage_maximum)) {
517 /* add usage range */
518 s->usages_min[s->nusage] =
520 s->usages_max[s->nusage] =
524 DPRINTFN(0, "Usage set dropped\n");
529 c->designator_index = dval;
532 c->designator_minimum = dval;
535 c->designator_maximum = dval;
538 c->string_index = dval;
541 c->string_minimum = dval;
544 c->string_maximum = dval;
547 c->set_delimiter = dval;
550 DPRINTFN(0, "Local bTag=%d\n", bTag);
555 DPRINTFN(0, "default bType=%d\n", bType);
562 /*------------------------------------------------------------------------*
564 *------------------------------------------------------------------------*/
566 hid_report_size(const void *buf, usb_size_t len, enum hid_kind k, uint8_t *id)
579 for (d = hid_start_parse(buf, len, 1 << k); hid_get_item(d, &h);) {
581 /* check for ID-byte presence */
582 if ((h.report_ID != 0) && !any_id) {
587 /* compute minimum */
588 if (lpos > h.loc.pos)
590 /* compute end position */
591 temp = h.loc.pos + (h.loc.size * h.loc.count);
592 /* compute maximum */
599 /* safety check - can happen in case of currupt descriptors */
605 /* check for ID byte */
611 /* return length in bytes rounded up */
612 return ((temp + 7) / 8);
615 /*------------------------------------------------------------------------*
617 *------------------------------------------------------------------------*/
619 hid_locate(const void *desc, usb_size_t size, int32_t u, enum hid_kind k,
620 uint8_t index, struct hid_location *loc, uint32_t *flags, uint8_t *id)
625 for (d = hid_start_parse(desc, size, 1 << k); hid_get_item(d, &h);) {
626 if (h.kind == k && !(h.flags & HIO_CONST) && h.usage == u) {
649 /*------------------------------------------------------------------------*
651 *------------------------------------------------------------------------*/
653 hid_get_data_sub(const uint8_t *buf, usb_size_t len, struct hid_location *loc,
656 uint32_t hpos = loc->pos;
657 uint32_t hsize = loc->size;
662 DPRINTFN(11, "hid_get_data: loc %d/%d\n", hpos, hsize);
664 /* Range check and limit */
670 /* Get data in a safe way */
678 data |= buf[rpos] << (8 * n);
681 /* Correctly shift down data */
682 data = (data >> (hpos % 8));
685 /* Mask and sign extend in one */
687 data = (int32_t)((int32_t)data << n) >> n;
689 data = (uint32_t)((uint32_t)data << n) >> n;
691 DPRINTFN(11, "hid_get_data: loc %d/%d = %lu\n",
692 loc->pos, loc->size, (long)data);
697 hid_get_data(const uint8_t *buf, usb_size_t len, struct hid_location *loc)
699 return (hid_get_data_sub(buf, len, loc, 1));
703 hid_get_data_unsigned(const uint8_t *buf, usb_size_t len, struct hid_location *loc)
705 return (hid_get_data_sub(buf, len, loc, 0));
708 /*------------------------------------------------------------------------*
710 *------------------------------------------------------------------------*/
712 hid_put_data_unsigned(uint8_t *buf, usb_size_t len,
713 struct hid_location *loc, unsigned int value)
715 uint32_t hpos = loc->pos;
716 uint32_t hsize = loc->size;
722 DPRINTFN(11, "hid_put_data: loc %d/%d = %u\n", hpos, hsize, value);
724 /* Range check and limit */
730 /* Put data in a safe way */
733 data = ((uint64_t)value) << (hpos % 8);
734 mask = ((1ULL << hsize) - 1ULL) << (hpos % 8);
739 buf[rpos] &= ~(mask >> (8 * n));
740 buf[rpos] |= (data >> (8 * n));
745 /*------------------------------------------------------------------------*
747 *------------------------------------------------------------------------*/
749 hid_is_collection(const void *desc, usb_size_t size, int32_t usage)
755 hd = hid_start_parse(desc, size, hid_input);
759 while ((err = hid_get_item(hd, &hi))) {
760 if (hi.kind == hid_collection &&
768 /*------------------------------------------------------------------------*
769 * hid_get_descriptor_from_usb
771 * This function will search for a HID descriptor between two USB
772 * interface descriptors.
775 * NULL: No more HID descriptors.
776 * Else: Pointer to HID descriptor.
777 *------------------------------------------------------------------------*/
778 struct usb_hid_descriptor *
779 hid_get_descriptor_from_usb(struct usb_config_descriptor *cd,
780 struct usb_interface_descriptor *id)
782 struct usb_descriptor *desc = (void *)id;
787 while ((desc = usb_desc_foreach(cd, desc))) {
788 if ((desc->bDescriptorType == UDESC_HID) &&
789 (desc->bLength >= USB_HID_DESCRIPTOR_SIZE(0))) {
792 if (desc->bDescriptorType == UDESC_INTERFACE) {
799 /*------------------------------------------------------------------------*
800 * usbd_req_get_hid_desc
802 * This function will read out an USB report descriptor from the USB
807 * Else: Success. The pointer should eventually be passed to free().
808 *------------------------------------------------------------------------*/
810 usbd_req_get_hid_desc(struct usb_device *udev, struct mtx *mtx,
811 void **descp, uint16_t *sizep,
812 struct malloc_type *mem, uint8_t iface_index)
814 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
815 struct usb_hid_descriptor *hid;
818 if ((iface == NULL) || (iface->idesc == NULL)) {
819 return (USB_ERR_INVAL);
821 hid = hid_get_descriptor_from_usb
822 (usbd_get_config_descriptor(udev), iface->idesc);
825 return (USB_ERR_IOERROR);
827 *sizep = UGETW(hid->descrs[0].wDescriptorLength);
829 return (USB_ERR_IOERROR);
834 *descp = malloc(*sizep, mem, M_ZERO | M_WAITOK);
839 if (*descp == NULL) {
840 return (USB_ERR_NOMEM);
842 err = usbd_req_get_report_descriptor
843 (udev, mtx, *descp, *sizep, iface_index);
850 return (USB_ERR_NORMAL_COMPLETION);
853 /*------------------------------------------------------------------------*
854 * calculate HID item resolution. unit/mm for distances, unit/rad for angles
855 *------------------------------------------------------------------------*/
857 hid_item_resolution(struct hid_item *hi)
860 * hid unit scaling table according to HID Usage Table Review
861 * Request 39 Tbl 17 http://www.usb.org/developers/hidpage/HUTRR39b.pdf
863 static const int64_t scale[0x10][2] = {
867 [0x03] = { 1, 1000 },
868 [0x04] = { 1, 10000 },
869 [0x05] = { 1, 100000 },
870 [0x06] = { 1, 1000000 },
871 [0x07] = { 1, 10000000 },
872 [0x08] = { 100000000, 1 },
873 [0x09] = { 10000000, 1 },
874 [0x0A] = { 1000000, 1 },
875 [0x0B] = { 100000, 1 },
876 [0x0C] = { 10000, 1 },
877 [0x0D] = { 1000, 1 },
881 int64_t logical_size;
882 int64_t physical_size;
908 if ((hi->logical_maximum <= hi->logical_minimum) ||
909 (hi->physical_maximum <= hi->physical_minimum) ||
910 (hi->unit_exponent < 0) || (hi->unit_exponent >= nitems(scale)))
913 logical_size = (int64_t)hi->logical_maximum -
914 (int64_t)hi->logical_minimum;
915 physical_size = (int64_t)hi->physical_maximum -
916 (int64_t)hi->physical_minimum;
917 /* Round to ceiling */
918 resolution = logical_size * multiplier * scale[hi->unit_exponent][0] /
919 (physical_size * divisor * scale[hi->unit_exponent][1]);
921 if (resolution > INT32_MAX)
927 /*------------------------------------------------------------------------*
930 * This function will decide if a USB descriptor belongs to a USB mouse.
933 * Zero: Not a USB mouse.
934 * Else: Is a USB mouse.
935 *------------------------------------------------------------------------*/
937 hid_is_mouse(const void *d_ptr, uint16_t d_len)
944 hd = hid_start_parse(d_ptr, d_len, 1 << hid_input);
951 while (hid_get_item(hd, &hi)) {
956 else if (hi.collection == 1 &&
958 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE))
961 case hid_endcollection:
969 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X) &&
970 (hi.flags & (HIO_CONST|HIO_RELATIVE)) == HIO_RELATIVE)
973 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y) &&
974 (hi.flags & (HIO_CONST|HIO_RELATIVE)) == HIO_RELATIVE)
985 /*------------------------------------------------------------------------*
988 * This function will decide if a USB descriptor belongs to a USB keyboard.
991 * Zero: Not a USB keyboard.
992 * Else: Is a USB keyboard.
993 *------------------------------------------------------------------------*/
995 hid_is_keyboard(const void *d_ptr, uint16_t d_len)
997 if (hid_is_collection(d_ptr, d_len,
998 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD)))