2 /* $NetBSD: hid.c,v 1.17 2001/11/13 06:24:53 lukem Exp $ */
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart@augustsson.net) at
9 * Carlstedt Research & Technology.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #ifdef USB_GLOBAL_INCLUDE_FILE
34 #include USB_GLOBAL_INCLUDE_FILE
36 #include <sys/stdint.h>
37 #include <sys/stddef.h>
38 #include <sys/param.h>
39 #include <sys/queue.h>
40 #include <sys/types.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
44 #include <sys/module.h>
46 #include <sys/mutex.h>
47 #include <sys/condvar.h>
48 #include <sys/sysctl.h>
50 #include <sys/unistd.h>
51 #include <sys/callout.h>
52 #include <sys/malloc.h>
55 #include <dev/usb/usb.h>
56 #include <dev/usb/usbdi.h>
57 #include <dev/usb/usbdi_util.h>
58 #include <dev/usb/usbhid.h>
60 #define USB_DEBUG_VAR usb_debug
62 #include <dev/usb/usb_core.h>
63 #include <dev/usb/usb_debug.h>
64 #include <dev/usb/usb_process.h>
65 #include <dev/usb/usb_device.h>
66 #include <dev/usb/usb_request.h>
67 #endif /* USB_GLOBAL_INCLUDE_FILE */
69 static void hid_clear_local(struct hid_item *);
70 static uint8_t hid_get_byte(struct hid_data *s, const uint16_t wSize);
85 struct hid_item cur[MAXPUSH];
86 struct hid_pos_data last_pos[MAXID];
87 int32_t usages_min[MAXUSAGE];
88 int32_t usages_max[MAXUSAGE];
89 int32_t usage_last; /* last seen usage */
90 uint32_t loc_size; /* last seen size */
91 uint32_t loc_count; /* last seen count */
92 uint8_t kindset; /* we have 5 kinds so 8 bits are enough */
93 uint8_t pushlevel; /* current pushlevel */
94 uint8_t ncount; /* end usage item count */
95 uint8_t icount; /* current usage item count */
96 uint8_t nusage; /* end "usages_min/max" index */
97 uint8_t iusage; /* current "usages_min/max" index */
98 uint8_t ousage; /* current "usages_min/max" offset */
99 uint8_t susage; /* usage set flags */
102 /*------------------------------------------------------------------------*
104 *------------------------------------------------------------------------*/
106 hid_clear_local(struct hid_item *c)
112 c->usage_minimum = 0;
113 c->usage_maximum = 0;
114 c->designator_index = 0;
115 c->designator_minimum = 0;
116 c->designator_maximum = 0;
118 c->string_minimum = 0;
119 c->string_maximum = 0;
120 c->set_delimiter = 0;
124 hid_switch_rid(struct hid_data *s, struct hid_item *c, int32_t next_rID)
128 /* check for same report ID - optimise */
130 if (c->report_ID == next_rID)
133 /* save current position for current rID */
135 if (c->report_ID == 0) {
138 for (i = 1; i != MAXID; i++) {
139 if (s->last_pos[i].rid == c->report_ID)
141 if (s->last_pos[i].rid == 0)
146 s->last_pos[i].rid = c->report_ID;
147 s->last_pos[i].pos = c->loc.pos;
150 /* store next report ID */
152 c->report_ID = next_rID;
154 /* lookup last position for next rID */
159 for (i = 1; i != MAXID; i++) {
160 if (s->last_pos[i].rid == next_rID)
162 if (s->last_pos[i].rid == 0)
167 s->last_pos[i].rid = next_rID;
168 c->loc.pos = s->last_pos[i].pos;
170 DPRINTF("Out of RID entries, position is set to zero!\n");
175 /*------------------------------------------------------------------------*
177 *------------------------------------------------------------------------*/
179 hid_start_parse(const void *d, usb_size_t len, int kindset)
183 if ((kindset-1) & kindset) {
184 DPRINTFN(0, "Only one bit can be "
185 "set in the kindset\n");
189 s = malloc(sizeof *s, M_TEMP, M_WAITOK | M_ZERO);
191 s->end = ((const uint8_t *)d) + len;
192 s->kindset = kindset;
196 /*------------------------------------------------------------------------*
198 *------------------------------------------------------------------------*/
200 hid_end_parse(struct hid_data *s)
208 /*------------------------------------------------------------------------*
209 * get byte from HID descriptor
210 *------------------------------------------------------------------------*/
212 hid_get_byte(struct hid_data *s, const uint16_t wSize)
219 /* check if end is reached */
223 /* read out a byte */
226 /* check if data pointer can be advanced by "wSize" bytes */
227 if ((s->end - ptr) < wSize)
238 /*------------------------------------------------------------------------*
240 *------------------------------------------------------------------------*/
242 hid_get_item(struct hid_data *s, struct hid_item *h)
245 unsigned int bTag, bType, bSize;
253 c = &s->cur[s->pushlevel];
256 /* check if there is an array of items */
257 if (s->icount < s->ncount) {
258 /* get current usage */
259 if (s->iusage < s->nusage) {
260 dval = s->usages_min[s->iusage] + s->ousage;
262 s->usage_last = dval;
263 if (dval == s->usages_max[s->iusage]) {
270 DPRINTFN(1, "Using last usage\n");
271 dval = s->usage_last;
275 * Only copy HID item, increment position and return
276 * if correct kindset!
278 if (s->kindset & (1 << c->kind)) {
280 DPRINTFN(1, "%u,%u,%u\n", h->loc.pos,
281 h->loc.size, h->loc.count);
282 c->loc.pos += c->loc.size * c->loc.count;
287 /* reset state variables */
297 while (s->p != s->end) {
299 bSize = hid_get_byte(s, 1);
302 bSize = hid_get_byte(s, 1);
303 bSize |= hid_get_byte(s, 1) << 8;
304 bTag = hid_get_byte(s, 1);
305 bType = 0xff; /* XXX what should it be */
309 bType = (bSize >> 2) & 3;
320 dval = (int8_t)hid_get_byte(s, 1);
324 dval = hid_get_byte(s, 1);
325 dval |= hid_get_byte(s, 1) << 8;
326 dval = (int16_t)dval;
330 dval = hid_get_byte(s, 1);
331 dval |= hid_get_byte(s, 1) << 8;
332 dval |= hid_get_byte(s, 1) << 16;
333 dval |= hid_get_byte(s, 1) << 24;
337 dval = hid_get_byte(s, bSize);
338 DPRINTFN(0, "bad length %u (data=0x%02x)\n",
350 c->loc.count = s->loc_count;
351 c->loc.size = s->loc_size;
353 if (c->flags & HIO_VARIABLE) {
354 /* range check usage count */
355 if (c->loc.count > 255) {
356 DPRINTFN(0, "Number of "
357 "items(%u) truncated to 255\n",
358 (unsigned)(c->loc.count));
361 s->ncount = c->loc.count;
364 * The "top" loop will return
374 c->kind = hid_output;
377 case 10: /* Collection */
378 c->kind = hid_collection;
379 c->collection = dval;
381 c->usage = s->usage_last;
384 case 11: /* Feature */
385 c->kind = hid_feature;
388 case 12: /* End collection */
389 c->kind = hid_endcollection;
390 if (c->collevel == 0) {
391 DPRINTFN(0, "invalid end collection\n");
398 DPRINTFN(0, "Main bTag=%d\n", bTag);
405 c->_usage_page = dval << 16;
408 c->logical_minimum = dval;
411 c->logical_maximum = dval;
414 c->physical_minimum = dval;
417 c->physical_maximum = dval;
420 c->unit_exponent = dval;
426 /* mask because value is unsigned */
427 s->loc_size = dval & mask;
430 hid_switch_rid(s, c, dval & mask);
433 /* mask because value is unsigned */
434 s->loc_count = dval & mask;
438 if (s->pushlevel < MAXPUSH) {
439 s->cur[s->pushlevel] = *c;
440 /* store size and count */
441 c->loc.size = s->loc_size;
442 c->loc.count = s->loc_count;
443 /* update current item pointer */
444 c = &s->cur[s->pushlevel];
446 DPRINTFN(0, "Cannot push "
447 "item @ %d\n", s->pushlevel);
452 if (s->pushlevel < MAXPUSH) {
453 /* preserve position */
455 c = &s->cur[s->pushlevel];
456 /* restore size and count */
457 s->loc_size = c->loc.size;
458 s->loc_count = c->loc.count;
459 /* set default item location */
464 DPRINTFN(0, "Cannot pop "
465 "item @ %d\n", s->pushlevel);
469 DPRINTFN(0, "Global bTag=%d\n", bTag);
477 dval = (dval & mask) | c->_usage_page;
479 /* set last usage, in case of a collection */
480 s->usage_last = dval;
482 if (s->nusage < MAXUSAGE) {
483 s->usages_min[s->nusage] = dval;
484 s->usages_max[s->nusage] = dval;
487 DPRINTFN(0, "max usage reached\n");
490 /* clear any pending usage sets */
497 dval = (dval & mask) | c->_usage_page;
498 c->usage_minimum = dval;
505 dval = (dval & mask) | c->_usage_page;
506 c->usage_maximum = dval;
513 if ((s->nusage < MAXUSAGE) &&
514 (c->usage_minimum <= c->usage_maximum)) {
515 /* add usage range */
516 s->usages_min[s->nusage] =
518 s->usages_max[s->nusage] =
522 DPRINTFN(0, "Usage set dropped\n");
527 c->designator_index = dval;
530 c->designator_minimum = dval;
533 c->designator_maximum = dval;
536 c->string_index = dval;
539 c->string_minimum = dval;
542 c->string_maximum = dval;
545 c->set_delimiter = dval;
548 DPRINTFN(0, "Local bTag=%d\n", bTag);
553 DPRINTFN(0, "default bType=%d\n", bType);
560 /*------------------------------------------------------------------------*
562 *------------------------------------------------------------------------*/
564 hid_report_size(const void *buf, usb_size_t len, enum hid_kind k, uint8_t *id)
577 for (d = hid_start_parse(buf, len, 1 << k); hid_get_item(d, &h);) {
579 /* check for ID-byte presence */
580 if ((h.report_ID != 0) && !any_id) {
585 /* compute minimum */
586 if (lpos > h.loc.pos)
588 /* compute end position */
589 temp = h.loc.pos + (h.loc.size * h.loc.count);
590 /* compute maximum */
597 /* safety check - can happen in case of currupt descriptors */
603 /* check for ID byte */
609 /* return length in bytes rounded up */
610 return ((temp + 7) / 8);
613 /*------------------------------------------------------------------------*
615 *------------------------------------------------------------------------*/
617 hid_locate(const void *desc, usb_size_t size, int32_t u, enum hid_kind k,
618 uint8_t index, struct hid_location *loc, uint32_t *flags, uint8_t *id)
623 for (d = hid_start_parse(desc, size, 1 << k); hid_get_item(d, &h);) {
624 if (h.kind == k && !(h.flags & HIO_CONST) && h.usage == u) {
647 /*------------------------------------------------------------------------*
649 *------------------------------------------------------------------------*/
651 hid_get_data_sub(const uint8_t *buf, usb_size_t len, struct hid_location *loc,
654 uint32_t hpos = loc->pos;
655 uint32_t hsize = loc->size;
660 DPRINTFN(11, "hid_get_data: loc %d/%d\n", hpos, hsize);
662 /* Range check and limit */
668 /* Get data in a safe way */
676 data |= buf[rpos] << (8 * n);
679 /* Correctly shift down data */
680 data = (data >> (hpos % 8));
683 /* Mask and sign extend in one */
685 data = (int32_t)((int32_t)data << n) >> n;
687 data = (uint32_t)((uint32_t)data << n) >> n;
689 DPRINTFN(11, "hid_get_data: loc %d/%d = %lu\n",
690 loc->pos, loc->size, (long)data);
695 hid_get_data(const uint8_t *buf, usb_size_t len, struct hid_location *loc)
697 return (hid_get_data_sub(buf, len, loc, 1));
701 hid_get_data_unsigned(const uint8_t *buf, usb_size_t len, struct hid_location *loc)
703 return (hid_get_data_sub(buf, len, loc, 0));
706 /*------------------------------------------------------------------------*
708 *------------------------------------------------------------------------*/
710 hid_put_data_unsigned(uint8_t *buf, usb_size_t len,
711 struct hid_location *loc, unsigned int value)
713 uint32_t hpos = loc->pos;
714 uint32_t hsize = loc->size;
720 DPRINTFN(11, "hid_put_data: loc %d/%d = %u\n", hpos, hsize, value);
722 /* Range check and limit */
728 /* Put data in a safe way */
731 data = ((uint64_t)value) << (hpos % 8);
732 mask = ((1ULL << hsize) - 1ULL) << (hpos % 8);
737 buf[rpos] &= ~(mask >> (8 * n));
738 buf[rpos] |= (data >> (8 * n));
743 /*------------------------------------------------------------------------*
745 *------------------------------------------------------------------------*/
747 hid_is_collection(const void *desc, usb_size_t size, int32_t usage)
753 hd = hid_start_parse(desc, size, hid_input);
757 while ((err = hid_get_item(hd, &hi))) {
758 if (hi.kind == hid_collection &&
766 /*------------------------------------------------------------------------*
767 * hid_get_descriptor_from_usb
769 * This function will search for a HID descriptor between two USB
770 * interface descriptors.
773 * NULL: No more HID descriptors.
774 * Else: Pointer to HID descriptor.
775 *------------------------------------------------------------------------*/
776 struct usb_hid_descriptor *
777 hid_get_descriptor_from_usb(struct usb_config_descriptor *cd,
778 struct usb_interface_descriptor *id)
780 struct usb_descriptor *desc = (void *)id;
785 while ((desc = usb_desc_foreach(cd, desc))) {
786 if ((desc->bDescriptorType == UDESC_HID) &&
787 (desc->bLength >= USB_HID_DESCRIPTOR_SIZE(0))) {
790 if (desc->bDescriptorType == UDESC_INTERFACE) {
797 /*------------------------------------------------------------------------*
798 * usbd_req_get_hid_desc
800 * This function will read out an USB report descriptor from the USB
805 * Else: Success. The pointer should eventually be passed to free().
806 *------------------------------------------------------------------------*/
808 usbd_req_get_hid_desc(struct usb_device *udev, struct mtx *mtx,
809 void **descp, uint16_t *sizep,
810 struct malloc_type *mem, uint8_t iface_index)
812 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
813 struct usb_hid_descriptor *hid;
816 if ((iface == NULL) || (iface->idesc == NULL)) {
817 return (USB_ERR_INVAL);
819 hid = hid_get_descriptor_from_usb
820 (usbd_get_config_descriptor(udev), iface->idesc);
823 return (USB_ERR_IOERROR);
825 *sizep = UGETW(hid->descrs[0].wDescriptorLength);
827 return (USB_ERR_IOERROR);
832 *descp = malloc(*sizep, mem, M_ZERO | M_WAITOK);
837 if (*descp == NULL) {
838 return (USB_ERR_NOMEM);
840 err = usbd_req_get_report_descriptor
841 (udev, mtx, *descp, *sizep, iface_index);
848 return (USB_ERR_NORMAL_COMPLETION);
851 /*------------------------------------------------------------------------*
854 * This function will decide if a USB descriptor belongs to a USB mouse.
857 * Zero: Not a USB mouse.
858 * Else: Is a USB mouse.
859 *------------------------------------------------------------------------*/
861 hid_is_mouse(const void *d_ptr, uint16_t d_len)
868 hd = hid_start_parse(d_ptr, d_len, 1 << hid_input);
875 while (hid_get_item(hd, &hi)) {
880 else if (hi.collection == 1 &&
882 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_MOUSE))
885 case hid_endcollection:
893 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X) &&
894 (hi.flags & (HIO_CONST|HIO_RELATIVE)) == HIO_RELATIVE)
897 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y) &&
898 (hi.flags & (HIO_CONST|HIO_RELATIVE)) == HIO_RELATIVE)
909 /*------------------------------------------------------------------------*
912 * This function will decide if a USB descriptor belongs to a USB keyboard.
915 * Zero: Not a USB keyboard.
916 * Else: Is a USB keyboard.
917 *------------------------------------------------------------------------*/
919 hid_is_keyboard(const void *d_ptr, uint16_t d_len)
921 if (hid_is_collection(d_ptr, d_len,
922 HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_KEYBOARD)))