First import
[xorg_rtime.git] / xorg-server-1.4 / hw / xfree86 / os-support / bsd / libusb / parse.c
blob0a846e42bc6f6323980ac79d2c891b26f789a3c9
1 /*
2 * Copyright (c) 1999 Lennart Augustsson <augustss@netbsd.org>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
27 #ifdef HAVE_XORG_CONFIG_H
28 #include <xorg-config.h>
29 #endif
31 #include <assert.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <sys/time.h>
36 #include <dev/usb/usb.h>
37 #include <dev/usb/usbhid.h>
39 #include "usb.h"
40 #include "usbvar.h"
42 #define MAXUSAGE 100
43 struct hid_data {
44 u_char *start;
45 u_char *end;
46 u_char *p;
47 hid_item_t cur;
48 unsigned int usages[MAXUSAGE];
49 int nusage;
50 int minset;
51 int multi;
52 int multimax;
53 int kindset;
56 static int min(int x, int y) { return x < y ? x : y; }
58 static void
59 hid_clear_local(hid_item_t *c)
62 _DIAGASSERT(c != NULL);
64 c->usage = 0;
65 c->usage_minimum = 0;
66 c->usage_maximum = 0;
67 c->designator_index = 0;
68 c->designator_minimum = 0;
69 c->designator_maximum = 0;
70 c->string_index = 0;
71 c->string_minimum = 0;
72 c->string_maximum = 0;
73 c->set_delimiter = 0;
76 hid_data_t
77 hid_start_parse(report_desc_t d, int kindset)
79 struct hid_data *s;
81 _DIAGASSERT(d != NULL);
83 s = malloc(sizeof *s);
84 memset(s, 0, sizeof *s);
85 s->start = s->p = d->data;
86 s->end = d->data + d->size;
87 s->kindset = kindset;
88 return (s);
91 void
92 hid_end_parse(hid_data_t s)
95 _DIAGASSERT(s != NULL);
97 while (s->cur.next) {
98 hid_item_t *hi = s->cur.next->next;
99 free(s->cur.next);
100 s->cur.next = hi;
102 free(s);
106 hid_get_item(hid_data_t s, hid_item_t *h)
108 hid_item_t *c;
109 unsigned int bTag = 0, bType = 0, bSize;
110 unsigned char *data;
111 int dval;
112 unsigned char *p;
113 hid_item_t *hi;
114 int i;
116 _DIAGASSERT(s != NULL);
117 _DIAGASSERT(h != NULL);
119 c = &s->cur;
121 top:
122 if (s->multimax) {
123 if (s->multi < s->multimax) {
124 c->usage = s->usages[min(s->multi, s->nusage-1)];
125 s->multi++;
126 *h = *c;
127 c->pos += c->report_size;
128 h->next = 0;
129 return (1);
130 } else {
131 c->report_count = s->multimax;
132 s->multimax = 0;
133 s->nusage = 0;
134 hid_clear_local(c);
137 for (;;) {
138 p = s->p;
139 if (p >= s->end)
140 return (0);
142 bSize = *p++;
143 if (bSize == 0xfe) {
144 /* long item */
145 bSize = *p++;
146 bSize |= *p++ << 8;
147 bTag = *p++;
148 data = p;
149 p += bSize;
150 } else {
151 /* short item */
152 bTag = bSize >> 4;
153 bType = (bSize >> 2) & 3;
154 bSize &= 3;
155 if (bSize == 3) bSize = 4;
156 data = p;
157 p += bSize;
159 s->p = p;
161 * The spec is unclear if the data is signed or unsigned.
163 switch(bSize) {
164 case 0:
165 dval = 0;
166 break;
167 case 1:
168 dval = (int8_t)*data++;
169 break;
170 case 2:
171 dval = *data++;
172 dval |= *data++ << 8;
173 dval = (int16_t)dval;
174 break;
175 case 4:
176 dval = *data++;
177 dval |= *data++ << 8;
178 dval |= *data++ << 16;
179 dval |= *data++ << 24;
180 break;
181 default:
182 return (-1);
185 switch (bType) {
186 case 0: /* Main */
187 switch (bTag) {
188 case 8: /* Input */
189 if (!(s->kindset & (1 << hid_input)))
190 continue;
191 c->kind = hid_input;
192 c->flags = dval;
193 ret:
194 if (c->flags & HIO_VARIABLE) {
195 s->multimax = c->report_count;
196 s->multi = 0;
197 c->report_count = 1;
198 if (s->minset) {
199 for (i = c->usage_minimum;
200 i <= c->usage_maximum;
201 i++) {
202 s->usages[s->nusage] = i;
203 if (s->nusage < MAXUSAGE-1)
204 s->nusage++;
206 s->minset = 0;
208 goto top;
209 } else {
210 if (s->minset)
211 c->usage = c->usage_minimum;
212 *h = *c;
213 h->next = 0;
214 c->pos += c->report_size * c->report_count;
215 hid_clear_local(c);
216 s->minset = 0;
217 return (1);
219 case 9: /* Output */
220 if (!(s->kindset & (1 << hid_output)))
221 continue;
222 c->kind = hid_output;
223 c->flags = dval;
224 goto ret;
225 case 10: /* Collection */
226 c->kind = hid_collection;
227 c->collection = dval;
228 c->collevel++;
229 *h = *c;
230 hid_clear_local(c);
231 s->nusage = 0;
232 return (1);
233 case 11: /* Feature */
234 if (!(s->kindset & (1 << hid_feature)))
235 continue;
236 c->kind = hid_feature;
237 c->flags = dval;
238 goto ret;
239 case 12: /* End collection */
240 c->kind = hid_endcollection;
241 c->collevel--;
242 *h = *c;
243 hid_clear_local(c);
244 s->nusage = 0;
245 return (1);
246 default:
247 return (-2);
250 case 1: /* Global */
251 switch (bTag) {
252 case 0:
253 c->_usage_page = dval << 16;
254 break;
255 case 1:
256 c->logical_minimum = dval;
257 break;
258 case 2:
259 c->logical_maximum = dval;
260 break;
261 case 3:
262 c->physical_maximum = dval;
263 break;
264 case 4:
265 c->physical_maximum = dval;
266 break;
267 case 5:
268 c->unit_exponent = dval;
269 break;
270 case 6:
271 c->unit = dval;
272 break;
273 case 7:
274 c->report_size = dval;
275 break;
276 case 8:
277 c->report_ID = dval;
278 break;
279 case 9:
280 c->report_count = dval;
281 break;
282 case 10: /* Push */
283 hi = malloc(sizeof *hi);
284 *hi = s->cur;
285 c->next = hi;
286 break;
287 case 11: /* Pop */
288 hi = c->next;
289 s->cur = *hi;
290 free(hi);
291 break;
292 default:
293 return (-3);
295 break;
296 case 2: /* Local */
297 switch (bTag) {
298 case 0:
299 if (bSize == 1)
300 dval = c->_usage_page | (dval&0xff);
301 else if (bSize == 2)
302 dval = c->_usage_page | (dval&0xffff);
303 c->usage = dval;
304 if (s->nusage < MAXUSAGE)
305 s->usages[s->nusage++] = dval;
306 /* else XXX */
307 break;
308 case 1:
309 s->minset = 1;
310 if (bSize == 1)
311 dval = c->_usage_page | (dval&0xff);
312 else if (bSize == 2)
313 dval = c->_usage_page | (dval&0xffff);
314 c->usage_minimum = dval;
315 break;
316 case 2:
317 if (bSize == 1)
318 dval = c->_usage_page | (dval&0xff);
319 else if (bSize == 2)
320 dval = c->_usage_page | (dval&0xffff);
321 c->usage_maximum = dval;
322 break;
323 case 3:
324 c->designator_index = dval;
325 break;
326 case 4:
327 c->designator_minimum = dval;
328 break;
329 case 5:
330 c->designator_maximum = dval;
331 break;
332 case 7:
333 c->string_index = dval;
334 break;
335 case 8:
336 c->string_minimum = dval;
337 break;
338 case 9:
339 c->string_maximum = dval;
340 break;
341 case 10:
342 c->set_delimiter = dval;
343 break;
344 default:
345 return (-4);
347 break;
348 default:
349 return (-5);
355 hid_report_size(report_desc_t r, enum hid_kind k, int *idp)
357 struct hid_data *d;
358 hid_item_t h;
359 int size, id;
361 _DIAGASSERT(r != NULL);
362 /* idp may be NULL */
364 id = 0;
365 if (idp)
366 *idp = 0;
367 memset(&h, 0, sizeof h);
368 for (d = hid_start_parse(r, 1<<k); hid_get_item(d, &h); ) {
369 if (h.report_ID != 0) {
370 if (idp)
371 *idp = h.report_ID;
372 id = 8;
375 hid_end_parse(d);
376 size = h.pos + id;
377 return ((size + 7) / 8);
381 hid_locate(desc, u, k, h)
382 report_desc_t desc;
383 unsigned int u;
384 enum hid_kind k;
385 hid_item_t *h;
387 hid_data_t d;
389 _DIAGASSERT(desc != NULL);
390 _DIAGASSERT(h != NULL);
392 for (d = hid_start_parse(desc, 1<<k); hid_get_item(d, h); ) {
393 if (h->kind == k && !(h->flags & HIO_CONST) && h->usage == u) {
394 hid_end_parse(d);
395 return (1);
398 hid_end_parse(d);
399 h->report_size = 0;
400 return (0);