1 /* $NetBSD: hid.c,v 1.17 2001/11/13 06:24:53 lukem Exp $ */
7 * Copyright (c) 1998 The NetBSD Foundation, Inc.
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by Lennart Augustsson (lennart@augustsson.net) at
12 * Carlstedt Research & Technology.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the NetBSD
25 * Foundation, Inc. and its contributors.
26 * 4. Neither the name of The NetBSD Foundation nor the names of its
27 * contributors may be used to endorse or promote products derived
28 * from this software without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
31 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
32 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
34 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
43 #include <sys/stdint.h>
44 #include <sys/stddef.h>
45 #include <sys/param.h>
46 #include <sys/queue.h>
47 #include <sys/types.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
51 #include <sys/linker_set.h>
52 #include <sys/module.h>
54 #include <sys/mutex.h>
55 #include <sys/condvar.h>
56 #include <sys/sysctl.h>
58 #include <sys/unistd.h>
59 #include <sys/callout.h>
60 #include <sys/malloc.h>
63 #include <dev/usb/usb.h>
64 #include <dev/usb/usbdi.h>
65 #include <dev/usb/usbdi_util.h>
66 #include <dev/usb/usbhid.h>
68 #define USB_DEBUG_VAR usb_debug
70 #include <dev/usb/usb_core.h>
71 #include <dev/usb/usb_debug.h>
72 #include <dev/usb/usb_process.h>
73 #include <dev/usb/usb_device.h>
74 #include <dev/usb/usb_request.h>
76 static void hid_clear_local(struct hid_item *);
77 static uint8_t hid_get_byte(struct hid_data *s, const uint16_t wSize);
92 struct hid_item cur[MAXPUSH];
93 struct hid_pos_data last_pos[MAXID];
94 int32_t usages_min[MAXUSAGE];
95 int32_t usages_max[MAXUSAGE];
96 int32_t usage_last; /* last seen usage */
97 uint32_t loc_size; /* last seen size */
98 uint32_t loc_count; /* last seen count */
99 uint8_t kindset; /* we have 5 kinds so 8 bits are enough */
100 uint8_t pushlevel; /* current pushlevel */
101 uint8_t ncount; /* end usage item count */
102 uint8_t icount; /* current usage item count */
103 uint8_t nusage; /* end "usages_min/max" index */
104 uint8_t iusage; /* current "usages_min/max" index */
105 uint8_t ousage; /* current "usages_min/max" offset */
106 uint8_t susage; /* usage set flags */
109 /*------------------------------------------------------------------------*
111 *------------------------------------------------------------------------*/
113 hid_clear_local(struct hid_item *c)
119 c->usage_minimum = 0;
120 c->usage_maximum = 0;
121 c->designator_index = 0;
122 c->designator_minimum = 0;
123 c->designator_maximum = 0;
125 c->string_minimum = 0;
126 c->string_maximum = 0;
127 c->set_delimiter = 0;
131 hid_switch_rid(struct hid_data *s, struct hid_item *c, int32_t next_rID)
135 /* check for same report ID - optimise */
137 if (c->report_ID == next_rID)
140 /* save current position for current rID */
142 if (c->report_ID == 0) {
145 for (i = 1; i != MAXID; i++) {
146 if (s->last_pos[i].rid == c->report_ID)
148 if (s->last_pos[i].rid == 0)
153 s->last_pos[i].rid = c->report_ID;
154 s->last_pos[i].pos = c->loc.pos;
157 /* store next report ID */
159 c->report_ID = next_rID;
161 /* lookup last position for next rID */
166 for (i = 1; i != MAXID; i++) {
167 if (s->last_pos[i].rid == next_rID)
169 if (s->last_pos[i].rid == 0)
174 s->last_pos[i].rid = next_rID;
175 c->loc.pos = s->last_pos[i].pos;
177 DPRINTF("Out of RID entries, position is set to zero!\n");
182 /*------------------------------------------------------------------------*
184 *------------------------------------------------------------------------*/
186 hid_start_parse(const void *d, usb_size_t len, int kindset)
190 if ((kindset-1) & kindset) {
191 DPRINTFN(0, "Only one bit can be "
192 "set in the kindset\n");
196 s = malloc(sizeof *s, M_TEMP, M_WAITOK | M_ZERO);
198 s->end = ((const uint8_t *)d) + len;
199 s->kindset = kindset;
203 /*------------------------------------------------------------------------*
205 *------------------------------------------------------------------------*/
207 hid_end_parse(struct hid_data *s)
215 /*------------------------------------------------------------------------*
216 * get byte from HID descriptor
217 *------------------------------------------------------------------------*/
219 hid_get_byte(struct hid_data *s, const uint16_t wSize)
226 /* check if end is reached */
230 /* read out a byte */
233 /* check if data pointer can be advanced by "wSize" bytes */
234 if ((s->end - ptr) < wSize)
245 /*------------------------------------------------------------------------*
247 *------------------------------------------------------------------------*/
249 hid_get_item(struct hid_data *s, struct hid_item *h)
252 unsigned int bTag, bType, bSize;
260 c = &s->cur[s->pushlevel];
263 /* check if there is an array of items */
264 if (s->icount < s->ncount) {
265 /* get current usage */
266 if (s->iusage < s->nusage) {
267 dval = s->usages_min[s->iusage] + s->ousage;
269 s->usage_last = dval;
270 if (dval == s->usages_max[s->iusage]) {
277 DPRINTFN(1, "Using last usage\n");
278 dval = s->usage_last;
282 * Only copy HID item, increment position and return
283 * if correct kindset!
285 if (s->kindset & (1 << c->kind)) {
287 DPRINTFN(1, "%u,%u,%u\n", h->loc.pos,
288 h->loc.size, h->loc.count);
289 c->loc.pos += c->loc.size * c->loc.count;
294 /* reset state variables */
304 while (s->p != s->end) {
306 bSize = hid_get_byte(s, 1);
309 bSize = hid_get_byte(s, 1);
310 bSize |= hid_get_byte(s, 1) << 8;
311 bTag = hid_get_byte(s, 1);
312 bType = 0xff; /* XXX what should it be */
316 bType = (bSize >> 2) & 3;
327 dval = (int8_t)hid_get_byte(s, 1);
331 dval = hid_get_byte(s, 1);
332 dval |= hid_get_byte(s, 1) << 8;
333 dval = (int16_t)dval;
337 dval = hid_get_byte(s, 1);
338 dval |= hid_get_byte(s, 1) << 8;
339 dval |= hid_get_byte(s, 1) << 16;
340 dval |= hid_get_byte(s, 1) << 24;
344 dval = hid_get_byte(s, bSize);
345 DPRINTFN(0, "bad length %u (data=0x%02x)\n",
357 c->loc.count = s->loc_count;
358 c->loc.size = s->loc_size;
360 if (c->flags & HIO_VARIABLE) {
361 /* range check usage count */
362 if (c->loc.count > 255) {
363 DPRINTFN(0, "Number of "
364 "items truncated to 255\n");
367 s->ncount = c->loc.count;
370 * The "top" loop will return
380 c->kind = hid_output;
383 case 10: /* Collection */
384 c->kind = hid_collection;
385 c->collection = dval;
387 c->usage = s->usage_last;
390 case 11: /* Feature */
391 c->kind = hid_feature;
394 case 12: /* End collection */
395 c->kind = hid_endcollection;
396 if (c->collevel == 0) {
397 DPRINTFN(0, "invalid end collection\n");
404 DPRINTFN(0, "Main bTag=%d\n", bTag);
411 c->_usage_page = dval << 16;
414 c->logical_minimum = dval;
417 c->logical_maximum = dval;
420 c->physical_minimum = dval;
423 c->physical_maximum = dval;
426 c->unit_exponent = dval;
432 /* mask because value is unsigned */
433 s->loc_size = dval & mask;
436 hid_switch_rid(s, c, dval);
439 /* mask because value is unsigned */
440 s->loc_count = dval & mask;
444 if (s->pushlevel < MAXPUSH) {
445 s->cur[s->pushlevel] = *c;
446 /* store size and count */
447 c->loc.size = s->loc_size;
448 c->loc.count = s->loc_count;
449 /* update current item pointer */
450 c = &s->cur[s->pushlevel];
452 DPRINTFN(0, "Cannot push "
453 "item @ %d\n", s->pushlevel);
458 if (s->pushlevel < MAXPUSH) {
459 /* preserve position */
461 c = &s->cur[s->pushlevel];
462 /* restore size and count */
463 s->loc_size = c->loc.size;
464 s->loc_count = c->loc.count;
465 /* set default item location */
470 DPRINTFN(0, "Cannot pop "
471 "item @ %d\n", s->pushlevel);
475 DPRINTFN(0, "Global bTag=%d\n", bTag);
483 dval = (dval & mask) | c->_usage_page;
485 /* set last usage, in case of a collection */
486 s->usage_last = dval;
488 if (s->nusage < MAXUSAGE) {
489 s->usages_min[s->nusage] = dval;
490 s->usages_max[s->nusage] = dval;
493 DPRINTFN(0, "max usage reached\n");
496 /* clear any pending usage sets */
503 dval = (dval & mask) | c->_usage_page;
504 c->usage_minimum = dval;
511 dval = (dval & mask) | c->_usage_page;
512 c->usage_maximum = dval;
519 if ((s->nusage < MAXUSAGE) &&
520 (c->usage_minimum <= c->usage_maximum)) {
521 /* add usage range */
522 s->usages_min[s->nusage] =
524 s->usages_max[s->nusage] =
528 DPRINTFN(0, "Usage set dropped\n");
533 c->designator_index = dval;
536 c->designator_minimum = dval;
539 c->designator_maximum = dval;
542 c->string_index = dval;
545 c->string_minimum = dval;
548 c->string_maximum = dval;
551 c->set_delimiter = dval;
554 DPRINTFN(0, "Local bTag=%d\n", bTag);
559 DPRINTFN(0, "default bType=%d\n", bType);
566 /*------------------------------------------------------------------------*
568 *------------------------------------------------------------------------*/
570 hid_report_size(const void *buf, usb_size_t len, enum hid_kind k, uint8_t *id)
583 for (d = hid_start_parse(buf, len, 1 << k); hid_get_item(d, &h);) {
585 /* check for ID-byte presense */
586 if ((h.report_ID != 0) && !any_id) {
591 /* compute minimum */
592 if (lpos > h.loc.pos)
594 /* compute end position */
595 temp = h.loc.pos + (h.loc.size * h.loc.count);
596 /* compute maximum */
603 /* safety check - can happen in case of currupt descriptors */
609 /* check for ID byte */
615 /* return length in bytes rounded up */
616 return ((temp + 7) / 8);
619 /*------------------------------------------------------------------------*
621 *------------------------------------------------------------------------*/
623 hid_locate(const void *desc, usb_size_t size, uint32_t u, enum hid_kind k,
624 uint8_t index, struct hid_location *loc, uint32_t *flags, uint8_t *id)
629 for (d = hid_start_parse(desc, size, 1 << k); hid_get_item(d, &h);) {
630 if (h.kind == k && !(h.flags & HIO_CONST) && h.usage == u) {
653 /*------------------------------------------------------------------------*
655 *------------------------------------------------------------------------*/
657 hid_get_data_sub(const uint8_t *buf, usb_size_t len, struct hid_location *loc,
660 uint32_t hpos = loc->pos;
661 uint32_t hsize = loc->size;
666 DPRINTFN(11, "hid_get_data: loc %d/%d\n", hpos, hsize);
668 /* Range check and limit */
674 /* Get data in a safe way */
682 data |= buf[rpos] << (8 * n);
685 /* Correctly shift down data */
686 data = (data >> (hpos % 8));
689 /* Mask and sign extend in one */
691 data = (int32_t)((int32_t)data << n) >> n;
693 data = (uint32_t)((uint32_t)data << n) >> n;
695 DPRINTFN(11, "hid_get_data: loc %d/%d = %lu\n",
696 loc->pos, loc->size, (long)data);
701 hid_get_data(const uint8_t *buf, usb_size_t len, struct hid_location *loc)
703 return (hid_get_data_sub(buf, len, loc, 1));
707 hid_get_data_unsigned(const uint8_t *buf, usb_size_t len, struct hid_location *loc)
709 return (hid_get_data_sub(buf, len, loc, 0));
712 /*------------------------------------------------------------------------*
714 *------------------------------------------------------------------------*/
716 hid_is_collection(const void *desc, usb_size_t size, uint32_t usage)
722 hd = hid_start_parse(desc, size, hid_input);
726 while ((err = hid_get_item(hd, &hi))) {
727 if (hi.kind == hid_collection &&
735 /*------------------------------------------------------------------------*
736 * hid_get_descriptor_from_usb
738 * This function will search for a HID descriptor between two USB
739 * interface descriptors.
742 * NULL: No more HID descriptors.
743 * Else: Pointer to HID descriptor.
744 *------------------------------------------------------------------------*/
745 struct usb_hid_descriptor *
746 hid_get_descriptor_from_usb(struct usb_config_descriptor *cd,
747 struct usb_interface_descriptor *id)
749 struct usb_descriptor *desc = (void *)id;
754 while ((desc = usb_desc_foreach(cd, desc))) {
755 if ((desc->bDescriptorType == UDESC_HID) &&
756 (desc->bLength >= USB_HID_DESCRIPTOR_SIZE(0))) {
759 if (desc->bDescriptorType == UDESC_INTERFACE) {
766 /*------------------------------------------------------------------------*
767 * usbd_req_get_hid_desc
769 * This function will read out an USB report descriptor from the USB
774 * Else: Success. The pointer should eventually be passed to free().
775 *------------------------------------------------------------------------*/
777 usbd_req_get_hid_desc(struct usb_device *udev, struct mtx *mtx,
778 void **descp, uint16_t *sizep,
779 struct malloc_type *mem, uint8_t iface_index)
781 struct usb_interface *iface = usbd_get_iface(udev, iface_index);
782 struct usb_hid_descriptor *hid;
785 if ((iface == NULL) || (iface->idesc == NULL)) {
786 return (USB_ERR_INVAL);
788 hid = hid_get_descriptor_from_usb
789 (usbd_get_config_descriptor(udev), iface->idesc);
792 return (USB_ERR_IOERROR);
794 *sizep = UGETW(hid->descrs[0].wDescriptorLength);
796 return (USB_ERR_IOERROR);
801 *descp = malloc(*sizep, mem, M_ZERO | M_WAITOK);
806 if (*descp == NULL) {
807 return (USB_ERR_NOMEM);
809 err = usbd_req_get_report_descriptor
810 (udev, mtx, *descp, *sizep, iface_index);
817 return (USB_ERR_NORMAL_COMPLETION);