1 /* $NetBSD: parse.c,v 1.5 2004/01/05 17:55:48 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 __RCSID("$NetBSD: parse.c,v 1.5 2004/01/05 17:55:48 augustss Exp $");
37 #include <dev/usb/usb.h>
38 #include <dev/usb/usbhid.h>
49 unsigned int usages
[MAXUSAGE
];
59 * The start of collection item has no report ID set, so save
60 * it until we know the ID.
65 * Absolute data position (bits) for input/output/feature.
66 * Assumes that hid_input, hid_output and hid_feature have
69 unsigned int kindpos
[3];
72 static int min(int x
, int y
) { return x
< y
? x
: y
; }
74 static int hid_get_item_raw(hid_data_t s
, hid_item_t
*h
);
77 hid_clear_local(hid_item_t
*c
)
80 _DIAGASSERT(c
!= NULL
);
85 c
->designator_index
= 0;
86 c
->designator_minimum
= 0;
87 c
->designator_maximum
= 0;
89 c
->string_minimum
= 0;
90 c
->string_maximum
= 0;
95 hid_start_parse(report_desc_t d
, int kindset
, int id
)
99 _DIAGASSERT(d
!= NULL
);
101 s
= malloc(sizeof *s
);
102 memset(s
, 0, sizeof *s
);
103 s
->start
= s
->p
= d
->data
;
104 s
->end
= d
->data
+ d
->size
;
105 s
->kindset
= kindset
;
112 hid_end_parse(hid_data_t s
)
115 _DIAGASSERT(s
!= NULL
);
117 while (s
->cur
.next
) {
118 hid_item_t
*hi
= s
->cur
.next
->next
;
126 hid_get_item(hid_data_t s
, hid_item_t
*h
)
131 r
= hid_get_item_raw(s
, h
);
134 if (h
->report_ID
== s
->reportid
|| s
->reportid
== -1)
140 #define REPORT_SAVED_COLL \
142 if (s->hassavedcoll) { \
144 h->report_ID = c->report_ID; \
145 s->hassavedcoll = 0; \
148 } while(/*LINTED*/ 0)
151 hid_get_item_raw(hid_data_t s
, hid_item_t
*h
)
154 unsigned int bTag
= 0, bType
= 0, bSize
;
163 _DIAGASSERT(s
!= NULL
);
164 _DIAGASSERT(h
!= NULL
);
171 if (c
->logical_minimum
>= c
->logical_maximum
) {
172 if (s
->logminsize
== 1)
173 c
->logical_minimum
=(int8_t)c
->logical_minimum
;
174 else if (s
->logminsize
== 2)
175 c
->logical_minimum
=(int16_t)c
->logical_minimum
;
177 if (s
->multi
< s
->multimax
) {
178 c
->usage
= s
->usages
[min(s
->multi
, s
->nusage
-1)];
182 * 'multimax' is only non-zero if the current
183 * item kind is input/output/feature
185 h
->pos
= s
->kindpos
[c
->kind
];
186 s
->kindpos
[c
->kind
] += c
->report_size
;
190 c
->report_count
= s
->multimax
;
212 bType
= (bSize
>> 2) & 3;
214 if (bSize
== 3) bSize
= 4;
220 * The spec is unclear if the data is signed or unsigned.
227 dval
= /*(int8_t)*/*data
++;
231 dval
|= *data
++ << 8;
232 dval
= /*(int16_t)*/dval
;
236 dval
|= *data
++ << 8;
237 dval
|= *data
++ << 16;
238 dval
|= *data
++ << 24;
250 if (!(s
->kindset
& (1 << retkind
))) {
251 /* Drop the items of this kind */
257 if (c
->flags
& HIO_VARIABLE
) {
258 s
->multimax
= c
->report_count
;
262 for (i
= c
->usage_minimum
;
263 i
<= c
->usage_maximum
;
265 s
->usages
[s
->nusage
] = i
;
266 if (s
->nusage
< MAXUSAGE
-1)
269 c
->usage_minimum
= 0;
270 c
->usage_maximum
= 0;
276 c
->usage
= c
->usage_minimum
;
279 h
->pos
= s
->kindpos
[c
->kind
];
280 s
->kindpos
[c
->kind
] +=
281 c
->report_size
* c
->report_count
;
287 retkind
= hid_output
;
289 case 10: /* Collection */
290 c
->kind
= hid_collection
;
291 c
->collection
= dval
;
295 /*c->report_ID = NO_REPORT_ID;*/
297 if (s
->hassavedcoll
) {
299 h
->report_ID
= nc
.report_ID
;
307 case 11: /* Feature */
308 retkind
= hid_feature
;
310 case 12: /* End collection */
312 c
->kind
= hid_endcollection
;
315 /*hid_clear_local(c);*/
326 c
->_usage_page
= dval
<< 16;
329 c
->logical_minimum
= dval
;
330 s
->logminsize
= bSize
;
333 c
->logical_maximum
= dval
;
336 c
->physical_minimum
= dval
;
339 c
->physical_maximum
= dval
;
342 c
->unit_exponent
= dval
;
348 c
->report_size
= dval
;
352 s
->kindpos
[hid_input
] =
353 s
->kindpos
[hid_output
] =
354 s
->kindpos
[hid_feature
] = 0;
357 c
->report_count
= dval
;
360 hi
= malloc(sizeof *hi
);
376 c
->usage
= c
->_usage_page
| dval
;
377 if (s
->nusage
< MAXUSAGE
)
378 s
->usages
[s
->nusage
++] = c
->usage
;
383 c
->usage_minimum
= c
->_usage_page
| dval
;
386 c
->usage_maximum
= c
->_usage_page
| dval
;
389 c
->designator_index
= dval
;
392 c
->designator_minimum
= dval
;
395 c
->designator_maximum
= dval
;
398 c
->string_index
= dval
;
401 c
->string_minimum
= dval
;
404 c
->string_maximum
= dval
;
407 c
->set_delimiter
= dval
;
420 hid_report_size(report_desc_t r
, enum hid_kind k
, int id
)
426 _DIAGASSERT(r
!= NULL
);
428 memset(&h
, 0, sizeof h
);
430 for (d
= hid_start_parse(r
, 1<<k
, id
); hid_get_item(d
, &h
); ) {
431 if (h
.report_ID
== id
&& h
.kind
== k
) {
432 size
= d
->kindpos
[k
];
436 return ((size
+ 7) / 8);
440 hid_locate(report_desc_t desc
, unsigned int u
, enum hid_kind k
,
441 hid_item_t
*h
, int id
)
445 _DIAGASSERT(desc
!= NULL
);
446 _DIAGASSERT(h
!= NULL
);
448 for (d
= hid_start_parse(desc
, 1<<k
, id
); hid_get_item(d
, h
); ) {
449 if (h
->kind
== k
&& !(h
->flags
& HIO_CONST
) && h
->usage
== u
) {