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
36 #include "usbcompat.h"
44 unsigned int usages
[MAXUSAGE
];
54 * The start of collection item has no report ID set, so save
55 * it until we know the ID.
58 unsigned char hassavedcoll
;
60 * Absolute data position (bits) for input/output/feature.
61 * Assumes that hid_input, hid_output and hid_feature have
64 unsigned int kindpos
[3];
67 static int min(int x
, int y
) { return x
< y
? x
: y
; }
69 static int hid_get_item_raw(hid_data_t s
, hid_item_t
*h
);
72 hid_clear_local(hid_item_t
*c
)
77 c
->designator_index
= 0;
78 c
->designator_minimum
= 0;
79 c
->designator_maximum
= 0;
81 c
->string_minimum
= 0;
82 c
->string_maximum
= 0;
87 hid_start_parse(report_desc_t d
, int kindset
, int id
)
91 s
= (struct hid_data
*)malloc(sizeof *s
);
92 memset(s
, 0, sizeof *s
);
93 s
->start
= s
->p
= d
->data
;
94 s
->end
= d
->data
+ d
->size
;
102 hid_end_parse(hid_data_t s
)
104 while (s
->cur
.next
) {
105 hid_item_t
*hi
= s
->cur
.next
->next
;
113 hid_get_item(hid_data_t s
, hid_item_t
*h
)
118 r
= hid_get_item_raw(s
, h
);
121 if (h
->report_ID
== s
->reportid
|| s
->reportid
== -1)
127 #define REPORT_SAVED_COLL \
129 if (s->hassavedcoll) { \
131 h->report_ID = c->report_ID; \
132 s->hassavedcoll = 0; \
135 } while(/*LINTED*/ 0)
138 hid_get_item_raw(hid_data_t s
, hid_item_t
*h
)
141 unsigned int bTag
= 0, bType
= 0, bSize
;
155 if (c
->logical_minimum
>= c
->logical_maximum
) {
156 if (s
->logminsize
== 1)
157 c
->logical_minimum
=(signed char)c
->logical_minimum
;
158 else if (s
->logminsize
== 2)
159 c
->logical_minimum
=(signed short)c
->logical_minimum
;
161 if (s
->multi
< s
->multimax
) {
162 c
->usage
= s
->usages
[min(s
->multi
, s
->nusage
-1)];
166 * 'multimax' is only non-zero if the current
167 * item kind is input/output/feature
169 h
->pos
= s
->kindpos
[c
->kind
];
170 s
->kindpos
[c
->kind
] += c
->report_size
;
174 c
->report_count
= s
->multimax
;
196 bType
= (bSize
>> 2) & 3;
198 if (bSize
== 3) bSize
= 4;
204 * The spec is unclear if the data is signed or unsigned.
215 dval
|= *data
++ << 8;
219 dval
|= *data
++ << 8;
220 dval
|= *data
++ << 16;
221 dval
|= *data
++ << 24;
233 if (!(s
->kindset
& (1 << retkind
))) {
234 /* Drop the items of this kind */
240 if (c
->flags
& HIO_VARIABLE
) {
241 s
->multimax
= c
->report_count
;
245 for (i
= c
->usage_minimum
;
246 i
<= c
->usage_maximum
;
248 s
->usages
[s
->nusage
] = i
;
249 if (s
->nusage
< MAXUSAGE
-1)
252 c
->usage_minimum
= 0;
253 c
->usage_maximum
= 0;
259 c
->usage
= c
->usage_minimum
;
262 h
->pos
= s
->kindpos
[c
->kind
];
263 s
->kindpos
[c
->kind
] +=
264 c
->report_size
* c
->report_count
;
270 retkind
= hid_output
;
272 case 10: /* Collection */
273 c
->kind
= hid_collection
;
274 c
->collection
= dval
;
278 /*c->report_ID = NO_REPORT_ID;*/
280 if (s
->hassavedcoll
) {
282 h
->report_ID
= nc
.report_ID
;
290 case 11: /* Feature */
291 retkind
= hid_feature
;
293 case 12: /* End collection */
295 c
->kind
= hid_endcollection
;
298 /*hid_clear_local(c);*/
309 c
->_usage_page
= dval
<< 16;
312 c
->logical_minimum
= dval
;
313 s
->logminsize
= bSize
;
316 c
->logical_maximum
= dval
;
319 c
->physical_minimum
= dval
;
322 c
->physical_maximum
= dval
;
325 c
->unit_exponent
= dval
;
331 c
->report_size
= dval
;
335 s
->kindpos
[hid_input
] =
336 s
->kindpos
[hid_output
] =
337 s
->kindpos
[hid_feature
] = 0;
340 c
->report_count
= dval
;
343 hi
= (hid_item_t
*)malloc(sizeof *hi
);
359 c
->usage
= c
->_usage_page
| dval
;
360 if (s
->nusage
< MAXUSAGE
)
361 s
->usages
[s
->nusage
++] = c
->usage
;
366 c
->usage_minimum
= c
->_usage_page
| dval
;
369 c
->usage_maximum
= c
->_usage_page
| dval
;
372 c
->designator_index
= dval
;
375 c
->designator_minimum
= dval
;
378 c
->designator_maximum
= dval
;
381 c
->string_index
= dval
;
384 c
->string_minimum
= dval
;
387 c
->string_maximum
= dval
;
390 c
->set_delimiter
= dval
;
403 hid_report_size(report_desc_t r
, enum hid_kind k
, int id
)
409 memset(&h
, 0, sizeof h
);
411 for (d
= hid_start_parse(r
, 1<<k
, id
); hid_get_item(d
, &h
); ) {
412 if (h
.report_ID
== id
&& h
.kind
== k
) {
413 size
= d
->kindpos
[k
];
417 return ((size
+ 7) / 8);
421 hid_locate(report_desc_t desc
, unsigned int u
, enum hid_kind k
,
422 hid_item_t
*h
, int id
)
426 for (d
= hid_start_parse(desc
, 1<<k
, id
); hid_get_item(d
, h
); ) {
427 if (h
->kind
== k
&& !(h
->flags
& HIO_CONST
) && h
->usage
== u
) {