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
34 #include <bus/u4b/usb.h>
35 #include <bus/u4b/usbhid.h>
47 uint32_t pos[ITEMTYPES];
54 struct hid_item cur[MAXPUSH];
55 struct hid_pos_data last_pos[MAXID];
56 uint32_t pos[ITEMTYPES];
57 int32_t usages_min[MAXUSAGE];
58 int32_t usages_max[MAXUSAGE];
59 int32_t usage_last; /* last seen usage */
60 uint32_t loc_size; /* last seen size */
61 uint32_t loc_count; /* last seen count */
62 uint8_t kindset; /* we have 5 kinds so 8 bits are enough */
63 uint8_t pushlevel; /* current pushlevel */
64 uint8_t ncount; /* end usage item count */
65 uint8_t icount; /* current usage item count */
66 uint8_t nusage; /* end "usages_min/max" index */
67 uint8_t iusage; /* current "usages_min/max" index */
68 uint8_t ousage; /* current "usages_min/max" offset */
69 uint8_t susage; /* usage set flags */
72 /*------------------------------------------------------------------------*
74 *------------------------------------------------------------------------*/
76 hid_clear_local(hid_item_t *c)
82 c->designator_index = 0;
83 c->designator_minimum = 0;
84 c->designator_maximum = 0;
86 c->string_minimum = 0;
87 c->string_maximum = 0;
92 hid_switch_rid(struct hid_data *s, struct hid_item *c, int32_t next_rID)
96 /* check for same report ID - optimise */
98 if (c->report_ID == next_rID)
101 /* save current position for current rID */
103 if (c->report_ID == 0) {
106 for (i = 1; i != MAXID; i++) {
107 if (s->last_pos[i].rid == c->report_ID)
109 if (s->last_pos[i].rid == 0)
114 s->last_pos[i].rid = c->report_ID;
115 for (j = 0; j < ITEMTYPES; j++)
116 s->last_pos[i].pos[j] = s->pos[j];
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 for (j = 0; j < ITEMTYPES; j++)
138 s->pos[j] = s->last_pos[i].pos[j];
140 for (j = 0; j < ITEMTYPES; j++)
141 s->pos[j] = 0; /* Out of RID entries. */
145 /*------------------------------------------------------------------------*
147 *------------------------------------------------------------------------*/
149 hid_start_parse(report_desc_t d, int kindset, int id __unused)
153 s = malloc(sizeof *s);
154 memset(s, 0, sizeof *s);
155 s->start = s->p = d->data;
156 s->end = d->data + d->size;
157 s->kindset = kindset;
161 /*------------------------------------------------------------------------*
163 *------------------------------------------------------------------------*/
165 hid_end_parse(hid_data_t s)
174 /*------------------------------------------------------------------------*
175 * get byte from HID descriptor
176 *------------------------------------------------------------------------*/
178 hid_get_byte(struct hid_data *s, const uint16_t wSize)
185 /* check if end is reached */
189 /* read out a byte */
192 /* check if data pointer can be advanced by "wSize" bytes */
193 if ((s->end - ptr) < wSize)
204 /*------------------------------------------------------------------------*
206 *------------------------------------------------------------------------*/
208 hid_get_item(hid_data_t s, hid_item_t *h)
211 unsigned int bTag, bType, bSize;
218 c = &s->cur[s->pushlevel];
221 /* check if there is an array of items */
222 if (s->icount < s->ncount) {
223 /* get current usage */
224 if (s->iusage < s->nusage) {
225 dval = s->usages_min[s->iusage] + s->ousage;
227 s->usage_last = dval;
228 if (dval == s->usages_max[s->iusage]) {
235 /* Using last usage */
236 dval = s->usage_last;
240 * Only copy HID item, increment position and return
241 * if correct kindset!
243 if (s->kindset & (1 << c->kind)) {
245 h->pos = s->pos[c->kind];
246 s->pos[c->kind] += c->report_size * c->report_count;
251 /* reset state variables */
261 while (s->p != s->end) {
263 bSize = hid_get_byte(s, 1);
266 bSize = hid_get_byte(s, 1);
267 bSize |= hid_get_byte(s, 1) << 8;
268 bTag = hid_get_byte(s, 1);
269 bType = 0xff; /* XXX what should it be */
273 bType = (bSize >> 2) & 3;
285 dval = (int8_t)hid_get_byte(s, 1);
289 dval = hid_get_byte(s, 1);
290 dval |= hid_get_byte(s, 1) << 8;
291 dval = (int16_t)dval;
295 dval = hid_get_byte(s, 1);
296 dval |= hid_get_byte(s, 1) << 8;
297 dval |= hid_get_byte(s, 1) << 16;
298 dval |= hid_get_byte(s, 1) << 24;
302 dval = hid_get_byte(s, bSize);
313 c->report_count = s->loc_count;
314 c->report_size = s->loc_size;
316 if (c->flags & HIO_VARIABLE) {
317 /* range check usage count */
318 if (c->report_count > 255) {
321 s->ncount = c->report_count;
324 * The "top" loop will return
328 c->usage_minimum = 0;
329 c->usage_maximum = 0;
336 c->kind = hid_output;
339 case 10: /* Collection */
340 c->kind = hid_collection;
341 c->collection = dval;
343 c->usage = s->usage_last;
346 case 11: /* Feature */
347 c->kind = hid_feature;
350 case 12: /* End collection */
351 c->kind = hid_endcollection;
352 if (c->collevel == 0) {
353 /* Invalid end collection. */
367 c->_usage_page = dval << 16;
370 c->logical_minimum = dval;
373 c->logical_maximum = dval;
376 c->physical_minimum = dval;
379 c->physical_maximum = dval;
382 c->unit_exponent = dval;
388 /* mask because value is unsigned */
389 s->loc_size = dval & mask;
392 hid_switch_rid(s, c, dval);
395 /* mask because value is unsigned */
396 s->loc_count = dval & mask;
400 if (s->pushlevel < MAXPUSH) {
401 s->cur[s->pushlevel] = *c;
402 /* store size and count */
403 c->report_size = s->loc_size;
404 c->report_count = s->loc_count;
405 /* update current item pointer */
406 c = &s->cur[s->pushlevel];
411 if (s->pushlevel < MAXPUSH) {
412 c = &s->cur[s->pushlevel];
413 /* restore size and count */
414 s->loc_size = c->report_size;
415 s->loc_count = c->report_count;
428 dval = (dval & mask) | c->_usage_page;
430 /* set last usage, in case of a collection */
431 s->usage_last = dval;
433 if (s->nusage < MAXUSAGE) {
434 s->usages_min[s->nusage] = dval;
435 s->usages_max[s->nusage] = dval;
440 /* clear any pending usage sets */
447 dval = (dval & mask) | c->_usage_page;
448 c->usage_minimum = dval;
455 dval = (dval & mask) | c->_usage_page;
456 c->usage_maximum = dval;
463 if ((s->nusage < MAXUSAGE) &&
464 (c->usage_minimum <= c->usage_maximum)) {
465 /* add usage range */
466 s->usages_min[s->nusage] =
468 s->usages_max[s->nusage] =
477 c->designator_index = dval;
480 c->designator_minimum = dval;
483 c->designator_maximum = dval;
486 c->string_index = dval;
489 c->string_minimum = dval;
492 c->string_maximum = dval;
495 c->set_delimiter = dval;
509 hid_report_size(report_desc_t r, enum hid_kind k, int id)
521 memset(&h, 0, sizeof h);
522 for (d = hid_start_parse(r, 1 << k, id); hid_get_item(d, &h); ) {
523 if ((h.report_ID == id || id < 0) && h.kind == k) {
524 /* compute minimum */
527 /* compute end position */
528 temp = h.pos + (h.report_size * h.report_count);
529 /* compute maximum */
532 if (h.report_ID != 0)
538 /* safety check - can happen in case of currupt descriptors */
544 /* return length in bytes rounded up */
545 return ((temp + 7) / 8 + report_id);
549 hid_locate(report_desc_t desc, unsigned int u, enum hid_kind k,
550 hid_item_t *h, int id)
554 for (d = hid_start_parse(desc, 1 << k, id); hid_get_item(d, h); ) {
555 if (h->kind == k && !(h->flags & HIO_CONST) && h->usage == u) {