1 /* $NetBSD: parse.c,v 1.11 2000/09/24 02:19:54 augustss Exp $ */
4 * Copyright (c) 1999 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>
49 unsigned int usages[MAXUSAGE];
56 /* Absolute data position (bits) for input/output/feature.
57 Assumes that hid_input, hid_output and hid_feature have
59 unsigned int kindpos[3];
62 static int min(int x, int y) { return x < y ? x : y; }
65 hid_clear_local(hid_item_t *c)
70 c->designator_index = 0;
71 c->designator_minimum = 0;
72 c->designator_maximum = 0;
74 c->string_minimum = 0;
75 c->string_maximum = 0;
80 hid_start_parse(report_desc_t d, int kindset)
84 s = malloc(sizeof *s);
85 memset(s, 0, sizeof *s);
86 s->start = s->p = d->data;
87 s->end = d->data + d->size;
93 hid_end_parse(hid_data_t s)
96 hid_item_t *hi = s->cur.next->next;
104 hid_get_item(hid_data_t s, hid_item_t *h)
107 unsigned int bTag = 0, bType = 0, bSize;
119 if (s->multi < s->multimax) {
120 c->usage = s->usages[min(s->multi, s->nusage-1)];
124 /* 'multimax' is only non-zero if the current
125 item kind is input/output/feature */
126 h->pos = s->kindpos[c->kind];
127 s->kindpos[c->kind] += c->report_size;
131 c->report_count = s->multimax;
153 bType = (bSize >> 2) & 3;
155 if (bSize == 3) bSize = 4;
161 * The spec is unclear if the data is signed or unsigned.
168 dval = (int8_t)*data++;
172 dval |= *data++ << 8;
173 dval = (int16_t)dval;
177 dval |= *data++ << 8;
178 dval |= *data++ << 16;
179 dval |= *data++ << 24;
191 if (!(s->kindset & (1 << retkind))) {
192 /* Drop the items of this kind */
198 if (c->flags & HIO_VARIABLE) {
199 s->multimax = c->report_count;
203 for (i = c->usage_minimum;
204 i <= c->usage_maximum;
206 s->usages[s->nusage] = i;
207 if (s->nusage < MAXUSAGE-1)
215 c->usage = c->usage_minimum;
218 h->pos = s->kindpos[c->kind];
219 s->kindpos[c->kind] += c->report_size * c->report_count;
225 retkind = hid_output;
227 case 10: /* Collection */
228 c->kind = hid_collection;
229 c->collection = dval;
233 c->report_ID = NO_REPORT_ID;
236 case 11: /* Feature */
237 retkind = hid_feature;
239 case 12: /* End collection */
240 c->kind = hid_endcollection;
243 /*hid_clear_local(c);*/
253 c->_usage_page = dval << 16;
256 c->logical_minimum = dval;
259 c->logical_maximum = dval;
262 c->physical_maximum = dval;
265 c->physical_maximum = dval;
268 c->unit_exponent = dval;
274 c->report_size = dval;
280 c->report_count = dval;
283 hi = malloc(sizeof *hi);
300 dval = c->_usage_page | (dval&0xff);
302 dval = c->_usage_page | (dval&0xffff);
304 if (s->nusage < MAXUSAGE)
305 s->usages[s->nusage++] = dval;
311 dval = c->_usage_page | (dval&0xff);
313 dval = c->_usage_page | (dval&0xffff);
314 c->usage_minimum = dval;
318 dval = c->_usage_page | (dval&0xff);
320 dval = c->_usage_page | (dval&0xffff);
321 c->usage_maximum = dval;
324 c->designator_index = dval;
327 c->designator_minimum = dval;
330 c->designator_maximum = dval;
333 c->string_index = dval;
336 c->string_minimum = dval;
339 c->string_maximum = dval;
342 c->set_delimiter = dval;
355 hid_report_size(report_desc_t r, unsigned int id, enum hid_kind k)
359 unsigned int size = 0;
361 memset(&h, 0, sizeof h);
362 d = hid_start_parse(r, 1<<k);
363 while (hid_get_item(d, &h)) {
364 if (h.report_ID == id && h.kind == k) {
365 unsigned int newsize = h.pos + h.report_size;
372 if (id != NO_REPORT_ID)
373 size += 8; /* add 8 bits for the report ID */
375 return ((size + 7) / 8); /* return size in bytes */
379 hid_locate(report_desc_t desc, unsigned int u, enum hid_kind k, hid_item_t *h)
383 for (d = hid_start_parse(desc, 1<<k); hid_get_item(d, h); ) {
384 if (h->kind == k && !(h->flags & HIO_CONST) && h->usage == u) {