2 * Copyright (c) 1999 Lennart Augustsson <augustss@netbsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
27 #ifdef HAVE_XORG_CONFIG_H
28 #include <xorg-config.h>
36 #include <dev/usb/usb.h>
37 #include <dev/usb/usbhid.h>
48 unsigned int usages
[MAXUSAGE
];
56 static int min(int x
, int y
) { return x
< y
? x
: y
; }
59 hid_clear_local(hid_item_t
*c
)
62 _DIAGASSERT(c
!= NULL
);
67 c
->designator_index
= 0;
68 c
->designator_minimum
= 0;
69 c
->designator_maximum
= 0;
71 c
->string_minimum
= 0;
72 c
->string_maximum
= 0;
77 hid_start_parse(report_desc_t d
, int kindset
)
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
;
92 hid_end_parse(hid_data_t s
)
95 _DIAGASSERT(s
!= NULL
);
98 hid_item_t
*hi
= s
->cur
.next
->next
;
106 hid_get_item(hid_data_t s
, hid_item_t
*h
)
109 unsigned int bTag
= 0, bType
= 0, bSize
;
116 _DIAGASSERT(s
!= NULL
);
117 _DIAGASSERT(h
!= NULL
);
123 if (s
->multi
< s
->multimax
) {
124 c
->usage
= s
->usages
[min(s
->multi
, s
->nusage
-1)];
127 c
->pos
+= 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;
189 if (!(s
->kindset
& (1 << hid_input
)))
194 if (c
->flags
& HIO_VARIABLE
) {
195 s
->multimax
= c
->report_count
;
199 for (i
= c
->usage_minimum
;
200 i
<= c
->usage_maximum
;
202 s
->usages
[s
->nusage
] = i
;
203 if (s
->nusage
< MAXUSAGE
-1)
211 c
->usage
= c
->usage_minimum
;
214 c
->pos
+= c
->report_size
* c
->report_count
;
220 if (!(s
->kindset
& (1 << hid_output
)))
222 c
->kind
= hid_output
;
225 case 10: /* Collection */
226 c
->kind
= hid_collection
;
227 c
->collection
= dval
;
233 case 11: /* Feature */
234 if (!(s
->kindset
& (1 << hid_feature
)))
236 c
->kind
= hid_feature
;
239 case 12: /* End collection */
240 c
->kind
= hid_endcollection
;
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
, enum hid_kind k
, int *idp
)
361 _DIAGASSERT(r
!= NULL
);
362 /* idp may be NULL */
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) {
377 return ((size
+ 7) / 8);
381 hid_locate(desc
, u
, k
, h
)
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
) {