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 <dev/usb/usb.h>
44 #include <dev/usb/usb_mfunc.h>
45 #include <dev/usb/usb_error.h>
46 #include <dev/usb/usb_defs.h>
47 #include <dev/usb/usbhid.h>
49 #define USB_DEBUG_VAR usb2_debug
51 #include <dev/usb/usb_core.h>
52 #include <dev/usb/usb_debug.h>
53 #include <dev/usb/usb_parse.h>
54 #include <dev/usb/usb_process.h>
55 #include <dev/usb/usb_device.h>
56 #include <dev/usb/usb_request.h>
57 #include <dev/usb/usb_hid.h>
59 static void hid_clear_local(struct hid_item *);
60 static uint8_t hid_get_byte(struct hid_data *s, const uint16_t wSize);
68 struct hid_item cur[MAXPUSH];
69 int32_t usages_min[MAXUSAGE];
70 int32_t usages_max[MAXUSAGE];
71 int32_t usage_last; /* last seen usage */
72 uint32_t loc_size; /* last seen size */
73 uint32_t loc_count; /* last seen count */
74 uint8_t kindset; /* we have 5 kinds so 8 bits are enough */
75 uint8_t pushlevel; /* current pushlevel */
76 uint8_t ncount; /* end usage item count */
77 uint8_t icount; /* current usage item count */
78 uint8_t nusage; /* end "usages_min/max" index */
79 uint8_t iusage; /* current "usages_min/max" index */
80 uint8_t ousage; /* current "usages_min/max" offset */
81 uint8_t susage; /* usage set flags */
84 /*------------------------------------------------------------------------*
86 *------------------------------------------------------------------------*/
88 hid_clear_local(struct hid_item *c)
96 c->designator_index = 0;
97 c->designator_minimum = 0;
98 c->designator_maximum = 0;
100 c->string_minimum = 0;
101 c->string_maximum = 0;
102 c->set_delimiter = 0;
105 /*------------------------------------------------------------------------*
107 *------------------------------------------------------------------------*/
109 hid_start_parse(const void *d, int len, int kindset)
113 if ((kindset-1) & kindset) {
114 DPRINTFN(0, "Only one bit can be "
115 "set in the kindset\n");
119 s = malloc(sizeof *s, M_TEMP, M_WAITOK | M_ZERO);
121 s->end = ((const uint8_t *)d) + len;
122 s->kindset = kindset;
126 /*------------------------------------------------------------------------*
128 *------------------------------------------------------------------------*/
130 hid_end_parse(struct hid_data *s)
138 /*------------------------------------------------------------------------*
139 * get byte from HID descriptor
140 *------------------------------------------------------------------------*/
142 hid_get_byte(struct hid_data *s, const uint16_t wSize)
149 /* check if end is reached */
153 /* read out a byte */
156 /* check if data pointer can be advanced by "wSize" bytes */
157 if ((s->end - ptr) < wSize)
168 /*------------------------------------------------------------------------*
170 *------------------------------------------------------------------------*/
172 hid_get_item(struct hid_data *s, struct hid_item *h)
175 unsigned int bTag, bType, bSize;
183 c = &s->cur[s->pushlevel];
186 /* check if there is an array of items */
187 if (s->icount < s->ncount) {
188 /* get current usage */
189 if (s->iusage < s->nusage) {
190 dval = s->usages_min[s->iusage] + s->ousage;
192 s->usage_last = dval;
193 if (dval == s->usages_max[s->iusage]) {
200 DPRINTFN(1, "Using last usage\n");
201 dval = s->usage_last;
205 * Only copy HID item, increment position and return
206 * if correct kindset!
208 if (s->kindset & (1 << c->kind)) {
210 DPRINTFN(1, "%u,%u,%u\n", h->loc.pos,
211 h->loc.size, h->loc.count);
212 c->loc.pos += c->loc.size * c->loc.count;
217 /* reset state variables */
227 while (s->p != s->end) {
229 bSize = hid_get_byte(s, 1);
232 bSize = hid_get_byte(s, 1);
233 bSize |= hid_get_byte(s, 1) << 8;
234 bTag = hid_get_byte(s, 1);
235 bType = 0xff; /* XXX what should it be */
239 bType = (bSize >> 2) & 3;
250 dval = (int8_t)hid_get_byte(s, 1);
254 dval = hid_get_byte(s, 1);
255 dval |= hid_get_byte(s, 1) << 8;
256 dval = (int16_t)dval;
260 dval = hid_get_byte(s, 1);
261 dval |= hid_get_byte(s, 1) << 8;
262 dval |= hid_get_byte(s, 1) << 16;
263 dval |= hid_get_byte(s, 1) << 24;
267 dval = hid_get_byte(s, bSize);
268 DPRINTFN(0, "bad length %u (data=0x%02x)\n",
280 c->loc.count = s->loc_count;
281 c->loc.size = s->loc_size;
283 if (c->flags & HIO_VARIABLE) {
284 /* range check usage count */
285 if (c->loc.count > 255) {
286 DPRINTFN(0, "Number of "
287 "items truncated to 255\n");
290 s->ncount = c->loc.count;
293 * The "top" loop will return
300 /* set default usage */
301 /* use the undefined HID PAGE */
306 c->kind = hid_output;
309 case 10: /* Collection */
310 c->kind = hid_collection;
311 c->collection = dval;
315 case 11: /* Feature */
316 c->kind = hid_feature;
319 case 12: /* End collection */
320 c->kind = hid_endcollection;
321 if (c->collevel == 0) {
322 DPRINTFN(0, "invalid end collection\n");
329 DPRINTFN(0, "Main bTag=%d\n", bTag);
336 c->_usage_page = dval << 16;
339 c->logical_minimum = dval;
342 c->logical_maximum = dval;
345 c->physical_minimum = dval;
348 c->physical_maximum = dval;
351 c->unit_exponent = dval;
357 /* mask because value is unsigned */
358 s->loc_size = dval & mask;
362 /* new report - reset position */
366 /* mask because value is unsigned */
367 s->loc_count = dval & mask;
371 if (s->pushlevel < MAXPUSH) {
372 s->cur[s->pushlevel] = *c;
373 /* store size and count */
374 c->loc.size = s->loc_size;
375 c->loc.count = s->loc_count;
376 /* update current item pointer */
377 c = &s->cur[s->pushlevel];
379 DPRINTFN(0, "Cannot push "
380 "item @ %d!\n", s->pushlevel);
385 if (s->pushlevel < MAXPUSH) {
386 /* preserve position */
388 c = &s->cur[s->pushlevel];
389 /* restore size and count */
390 s->loc_size = c->loc.size;
391 s->loc_count = c->loc.count;
392 /* set default item location */
397 DPRINTFN(0, "Cannot pop "
398 "item @ %d!\n", s->pushlevel);
402 DPRINTFN(0, "Global bTag=%d\n", bTag);
410 dval = (dval & mask) | c->_usage_page;
412 if (s->nusage < MAXUSAGE) {
413 s->usages_min[s->nusage] = dval;
414 s->usages_max[s->nusage] = dval;
417 DPRINTFN(0, "max usage reached!\n");
420 /* clear any pending usage sets */
427 dval = (dval & mask) | c->_usage_page;
428 c->usage_minimum = dval;
435 dval = (dval & mask) | c->_usage_page;
436 c->usage_maximum = dval;
443 if ((s->nusage < MAXUSAGE) &&
444 (c->usage_minimum < c->usage_maximum)) {
445 /* add usage range */
446 s->usages_min[s->nusage] =
448 s->usages_max[s->nusage] =
452 DPRINTFN(0, "Usage set dropped!\n");
457 c->designator_index = dval;
460 c->designator_minimum = dval;
463 c->designator_maximum = dval;
466 c->string_index = dval;
469 c->string_minimum = dval;
472 c->string_maximum = dval;
475 c->set_delimiter = dval;
478 DPRINTFN(0, "Local bTag=%d\n", bTag);
483 DPRINTFN(0, "default bType=%d\n", bType);
490 /*------------------------------------------------------------------------*
492 *------------------------------------------------------------------------*/
494 hid_report_size(const void *buf, int len, enum hid_kind k, uint8_t *id)
507 for (d = hid_start_parse(buf, len, 1 << k); hid_get_item(d, &h);) {
509 /* check for ID-byte presense */
510 if ((h.report_ID != 0) && !any_id) {
515 /* compute minimum */
516 if (lpos > h.loc.pos)
518 /* compute end position */
519 temp = h.loc.pos + (h.loc.size * h.loc.count);
520 /* compute maximum */
527 /* safety check - can happen in case of currupt descriptors */
533 /* check for ID byte */
539 /* return length in bytes rounded up */
540 return ((temp + 7) / 8);
543 /*------------------------------------------------------------------------*
545 *------------------------------------------------------------------------*/
547 hid_locate(const void *desc, int size, uint32_t u, enum hid_kind k,
548 struct hid_location *loc, uint32_t *flags, uint8_t *id)
553 for (d = hid_start_parse(desc, size, 1 << k); hid_get_item(d, &h);) {
554 if (h.kind == k && !(h.flags & HIO_CONST) && h.usage == u) {
575 /*------------------------------------------------------------------------*
577 *------------------------------------------------------------------------*/
579 hid_get_data(const uint8_t *buf, uint32_t len, struct hid_location *loc)
581 uint32_t hpos = loc->pos;
582 uint32_t hsize = loc->size;
587 DPRINTFN(11, "hid_get_data: loc %d/%d\n", hpos, hsize);
589 /* Range check and limit */
595 /* Get data in a safe way */
603 data |= buf[rpos] << (8 * n);
606 /* Correctly shift down data */
607 data = (data >> (hpos % 8));
609 /* Mask and sign extend in one */
611 data = ((int32_t)data << n) >> n;
613 DPRINTFN(11, "hid_get_data: loc %d/%d = %lu\n",
614 loc->pos, loc->size, (long)data);
618 /*------------------------------------------------------------------------*
620 *------------------------------------------------------------------------*/
622 hid_is_collection(const void *desc, int size, uint32_t usage)
628 hd = hid_start_parse(desc, size, hid_input);
632 err = hid_get_item(hd, &hi) &&
633 hi.kind == hid_collection &&
639 /*------------------------------------------------------------------------*
640 * hid_get_descriptor_from_usb
642 * This function will search for a HID descriptor between two USB
643 * interface descriptors.
646 * NULL: No more HID descriptors.
647 * Else: Pointer to HID descriptor.
648 *------------------------------------------------------------------------*/
649 struct usb2_hid_descriptor *
650 hid_get_descriptor_from_usb(struct usb2_config_descriptor *cd,
651 struct usb2_interface_descriptor *id)
653 struct usb2_descriptor *desc = (void *)id;
658 while ((desc = usb2_desc_foreach(cd, desc))) {
659 if ((desc->bDescriptorType == UDESC_HID) &&
660 (desc->bLength >= USB_HID_DESCRIPTOR_SIZE(0))) {
663 if (desc->bDescriptorType == UDESC_INTERFACE) {
670 /*------------------------------------------------------------------------*
671 * usb2_req_get_hid_desc
673 * This function will read out an USB report descriptor from the USB
678 * Else: Success. The pointer should eventually be passed to free().
679 *------------------------------------------------------------------------*/
681 usb2_req_get_hid_desc(struct usb2_device *udev, struct mtx *mtx,
682 void **descp, uint16_t *sizep,
683 usb2_malloc_type mem, uint8_t iface_index)
685 struct usb2_interface *iface = usb2_get_iface(udev, iface_index);
686 struct usb2_hid_descriptor *hid;
689 if ((iface == NULL) || (iface->idesc == NULL)) {
690 return (USB_ERR_INVAL);
692 hid = hid_get_descriptor_from_usb
693 (usb2_get_config_descriptor(udev), iface->idesc);
696 return (USB_ERR_IOERROR);
698 *sizep = UGETW(hid->descrs[0].wDescriptorLength);
700 return (USB_ERR_IOERROR);
705 *descp = malloc(*sizep, mem, M_ZERO | M_WAITOK);
710 if (*descp == NULL) {
711 return (USB_ERR_NOMEM);
713 err = usb2_req_get_report_descriptor
714 (udev, mtx, *descp, *sizep, iface_index);
721 return (USB_ERR_NORMAL_COMPLETION);