1 /* $NetBSD: parse.c,v 1.11 2000/09/24 02:19:54 augustss Exp $ */
4 * Copyright (c) 1999, 2001 Lennart Augustsson <augustss@netbsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
37 #include <dev/usb/usb.h>
38 #include <dev/usb/usbhid.h>
56 struct hid_item cur[MAXPUSH];
57 struct hid_pos_data last_pos[MAXID];
58 int32_t usages_min[MAXUSAGE];
59 int32_t usages_max[MAXUSAGE];
60 int32_t usage_last; /* last seen usage */
61 uint32_t loc_size; /* last seen size */
62 uint32_t loc_count; /* last seen count */
63 uint8_t kindset; /* we have 5 kinds so 8 bits are enough */
64 uint8_t pushlevel; /* current pushlevel */
65 uint8_t ncount; /* end usage item count */
66 uint8_t icount; /* current usage item count */
67 uint8_t nusage; /* end "usages_min/max" index */
68 uint8_t iusage; /* current "usages_min/max" index */
69 uint8_t ousage; /* current "usages_min/max" offset */
70 uint8_t susage; /* usage set flags */
73 /*------------------------------------------------------------------------*
75 *------------------------------------------------------------------------*/
77 hid_clear_local(hid_item_t *c)
83 c->designator_index = 0;
84 c->designator_minimum = 0;
85 c->designator_maximum = 0;
87 c->string_minimum = 0;
88 c->string_maximum = 0;
93 hid_switch_rid(struct hid_data *s, struct hid_item *c, int32_t next_rID)
97 /* check for same report ID - optimise */
99 if (c->report_ID == next_rID)
102 /* save current position for current rID */
104 if (c->report_ID == 0) {
107 for (i = 1; i != MAXID; i++) {
108 if (s->last_pos[i].rid == c->report_ID)
110 if (s->last_pos[i].rid == 0)
115 s->last_pos[i].rid = c->report_ID;
116 s->last_pos[i].pos = c->pos;
119 /* store next report ID */
121 c->report_ID = next_rID;
123 /* lookup last position for next rID */
128 for (i = 1; i != MAXID; i++) {
129 if (s->last_pos[i].rid == next_rID)
131 if (s->last_pos[i].rid == 0)
136 s->last_pos[i].rid = next_rID;
137 c->pos = s->last_pos[i].pos;
139 c->pos = 0; /* Out of RID entries. */
142 /*------------------------------------------------------------------------*
144 *------------------------------------------------------------------------*/
146 hid_start_parse(report_desc_t d, int kindset, int id __unused)
150 s = malloc(sizeof *s);
151 memset(s, 0, sizeof *s);
152 s->start = s->p = d->data;
153 s->end = d->data + d->size;
154 s->kindset = kindset;
158 /*------------------------------------------------------------------------*
160 *------------------------------------------------------------------------*/
162 hid_end_parse(hid_data_t s)
171 /*------------------------------------------------------------------------*
172 * get byte from HID descriptor
173 *------------------------------------------------------------------------*/
175 hid_get_byte(struct hid_data *s, const uint16_t wSize)
182 /* check if end is reached */
186 /* read out a byte */
189 /* check if data pointer can be advanced by "wSize" bytes */
190 if ((s->end - ptr) < wSize)
201 /*------------------------------------------------------------------------*
203 *------------------------------------------------------------------------*/
205 hid_get_item(hid_data_t s, hid_item_t *h)
208 unsigned int bTag, bType, bSize;
216 c = &s->cur[s->pushlevel];
219 /* check if there is an array of items */
220 if (s->icount < s->ncount) {
221 /* get current usage */
222 if (s->iusage < s->nusage) {
223 dval = s->usages_min[s->iusage] + s->ousage;
225 s->usage_last = dval;
226 if (dval == s->usages_max[s->iusage]) {
233 /* Using last usage */
234 dval = s->usage_last;
238 * Only copy HID item, increment position and return
239 * if correct kindset!
241 if (s->kindset & (1 << c->kind)) {
243 c->pos += c->report_size * c->report_count;
248 /* reset state variables */
258 while (s->p != s->end) {
260 bSize = hid_get_byte(s, 1);
263 bSize = hid_get_byte(s, 1);
264 bSize |= hid_get_byte(s, 1) << 8;
265 bTag = hid_get_byte(s, 1);
266 bType = 0xff; /* XXX what should it be */
270 bType = (bSize >> 2) & 3;
282 dval = (int8_t)hid_get_byte(s, 1);
286 dval = hid_get_byte(s, 1);
287 dval |= hid_get_byte(s, 1) << 8;
288 dval = (int16_t)dval;
292 dval = hid_get_byte(s, 1);
293 dval |= hid_get_byte(s, 1) << 8;
294 dval |= hid_get_byte(s, 1) << 16;
295 dval |= hid_get_byte(s, 1) << 24;
299 dval = hid_get_byte(s, bSize);
310 c->report_count = s->loc_count;
311 c->report_size = s->loc_size;
313 if (c->flags & HIO_VARIABLE) {
314 /* range check usage count */
315 if (c->report_count > 255) {
318 s->ncount = c->report_count;
321 * The "top" loop will return
331 c->kind = hid_output;
334 case 10: /* Collection */
335 c->kind = hid_collection;
336 c->collection = dval;
338 c->usage = s->usage_last;
341 case 11: /* Feature */
342 c->kind = hid_feature;
345 case 12: /* End collection */
346 c->kind = hid_endcollection;
347 if (c->collevel == 0) {
348 /* Invalid end collection. */
362 c->_usage_page = dval << 16;
365 c->logical_minimum = dval;
368 c->logical_maximum = dval;
371 c->physical_minimum = dval;
374 c->physical_maximum = dval;
377 c->unit_exponent = dval;
383 /* mask because value is unsigned */
384 s->loc_size = dval & mask;
387 hid_switch_rid(s, c, dval);
390 /* mask because value is unsigned */
391 s->loc_count = dval & mask;
395 if (s->pushlevel < MAXPUSH) {
396 s->cur[s->pushlevel] = *c;
397 /* store size and count */
398 c->report_size = s->loc_size;
399 c->report_count = s->loc_count;
400 /* update current item pointer */
401 c = &s->cur[s->pushlevel];
406 if (s->pushlevel < MAXPUSH) {
407 /* preserve position */
409 c = &s->cur[s->pushlevel];
410 /* restore size and count */
411 s->loc_size = c->report_size;
412 s->loc_count = c->report_count;
413 /* set default item location */
427 dval = (dval & mask) | c->_usage_page;
429 /* set last usage, in case of a collection */
430 s->usage_last = dval;
432 if (s->nusage < MAXUSAGE) {
433 s->usages_min[s->nusage] = dval;
434 s->usages_max[s->nusage] = dval;
439 /* clear any pending usage sets */
446 dval = (dval & mask) | c->_usage_page;
447 c->usage_minimum = dval;
454 dval = (dval & mask) | c->_usage_page;
455 c->usage_maximum = dval;
462 if ((s->nusage < MAXUSAGE) &&
463 (c->usage_minimum <= c->usage_maximum)) {
464 /* add usage range */
465 s->usages_min[s->nusage] =
467 s->usages_max[s->nusage] =
476 c->designator_index = dval;
479 c->designator_minimum = dval;
482 c->designator_maximum = dval;
485 c->string_index = dval;
488 c->string_minimum = dval;
491 c->string_maximum = dval;
494 c->set_delimiter = dval;
508 hid_report_size(report_desc_t r, enum hid_kind k, int id)
519 memset(&h, 0, sizeof h);
520 for (d = hid_start_parse(r, 1 << k, id); hid_get_item(d, &h); ) {
521 if (h.report_ID == id && h.kind == k) {
522 /* compute minimum */
525 /* compute end position */
526 temp = h.pos + (h.report_size * h.report_count);
527 /* compute maximum */
534 /* safety check - can happen in case of currupt descriptors */
543 /* return length in bytes rounded up */
544 return ((temp + 7) / 8);
548 hid_locate(report_desc_t desc, unsigned int u, enum hid_kind k,
549 hid_item_t *h, int id)
553 for (d = hid_start_parse(desc, 1 << k, id); hid_get_item(d, h); ) {
554 if (h->kind == k && !(h->flags & HIO_CONST) && h->usage == u) {