[NTOSKRNL_VISTA]
[reactos.git] / reactos / sdk / lib / drivers / hidparser / parser.h
1 #ifndef _HIDPARSER_H_
2 #define _HIDPARSER_H_
3
4 #include <wdm.h>
5 #include <pshpack1.h>
6 #define _HIDPI_
7 #define _HIDPI_NO_FUNCTION_MACROS_
8 #include <hidpddi.h>
9
10 #include "hidparser.h"
11
12 /*
13 * Copyright 2007, Haiku, Inc. All Rights Reserved.
14 * Distributed under the terms of the MIT License.
15 */
16
17 #define HID_REPORT_TYPE_ANY 0x07
18
19
20 #define ITEM_TYPE_MAIN 0x0
21 #define ITEM_TYPE_GLOBAL 0x1
22 #define ITEM_TYPE_LOCAL 0x2
23 #define ITEM_TYPE_LONG 0x3
24
25 #define ITEM_TAG_MAIN_INPUT 0x8
26 #define ITEM_TAG_MAIN_OUTPUT 0x9
27 #define ITEM_TAG_MAIN_FEATURE 0xb
28 #define ITEM_TAG_MAIN_COLLECTION 0xa
29 #define ITEM_TAG_MAIN_END_COLLECTION 0xc
30
31 #define ITEM_TAG_GLOBAL_USAGE_PAGE 0x0
32 #define ITEM_TAG_GLOBAL_LOGICAL_MINIMUM 0x1
33 #define ITEM_TAG_GLOBAL_LOGICAL_MAXIMUM 0x2
34 #define ITEM_TAG_GLOBAL_PHYSICAL_MINIMUM 0x3
35 #define ITEM_TAG_GLOBAL_PHYSICAL_MAXIMUM 0x4
36 #define ITEM_TAG_GLOBAL_UNIT_EXPONENT 0x5
37 #define ITEM_TAG_GLOBAL_UNIT 0x6
38 #define ITEM_TAG_GLOBAL_REPORT_SIZE 0x7
39 #define ITEM_TAG_GLOBAL_REPORT_ID 0x8
40 #define ITEM_TAG_GLOBAL_REPORT_COUNT 0x9
41 #define ITEM_TAG_GLOBAL_PUSH 0xa
42 #define ITEM_TAG_GLOBAL_POP 0xb
43
44 #define ITEM_TAG_LOCAL_USAGE 0x0
45 #define ITEM_TAG_LOCAL_USAGE_MINIMUM 0x1
46 #define ITEM_TAG_LOCAL_USAGE_MAXIMUM 0x2
47 #define ITEM_TAG_LOCAL_DESIGNATOR_INDEX 0x3
48 #define ITEM_TAG_LOCAL_DESIGNATOR_MINIMUM 0x4
49 #define ITEM_TAG_LOCAL_DESIGNATOR_MAXIMUM 0x5
50 #define ITEM_TAG_LOCAL_STRING_INDEX 0x7
51 #define ITEM_TAG_LOCAL_STRING_MINIMUM 0x8
52 #define ITEM_TAG_LOCAL_STRING_MAXIMUM 0x9
53 #define ITEM_TAG_LOCAL_DELIMITER 0xa
54
55 #define ITEM_TAG_LONG 0xf
56
57 #define COLLECTION_PHYSICAL 0x00
58 #define COLLECTION_APPLICATION 0x01
59 #define COLLECTION_LOGICAL 0x02
60 #define COLLECTION_REPORT 0x03
61 #define COLLECTION_NAMED_ARRAY 0x04
62 #define COLLECTION_USAGE_SWITCH 0x05
63 #define COLLECTION_USAGE_MODIFIER 0x06
64 #define COLLECTION_ALL 0xff
65
66 #define UNIT_SYSTEM 0x0
67 #define UNIT_LENGTH 0x1
68 #define UNIT_MASS 0x2
69 #define UNIT_TIME 0x3
70 #define UNIT_TEMPERATURE 0x4
71 #define UNIT_CURRENT 0x5
72 #define UNIT_LUMINOUS_INTENSITY 0x6
73
74 #define USAGE_PAGE_SHIFT 16
75 #define USAGE_PAGE_MASK 0xffff
76 #define USAGE_ID_SHIFT 0
77 #define USAGE_ID_MASK 0xffff
78
79 typedef struct
80 {
81 UCHAR Size:2;
82 UCHAR Type:2;
83 UCHAR Tag:4;
84 }ITEM_PREFIX, *PITEM_PREFIX;
85
86 typedef struct
87 {
88 ITEM_PREFIX Prefix;
89
90 union
91 {
92 UCHAR UData8[4];
93 CHAR SData8[4];
94 USHORT UData16[2];
95 SHORT SData16[2];
96 ULONG UData32;
97 LONG SData32;
98 }Data;
99
100 }SHORT_ITEM, *PSHORT_ITEM;
101
102 typedef struct
103 {
104 ITEM_PREFIX Prefix;
105 UCHAR DataSize;
106 UCHAR LongItemTag;
107 UCHAR Data[0];
108
109 }LONG_ITEM,*PLONG_ITEM;
110
111
112 #define LBITFIELD9(b1,b2,b3,b4,b5,b6,b7,b8,b9) USHORT b9,b8,b7,b6,b5,b4,b3,b2,b1
113 typedef struct
114 {
115 USHORT DataConstant:1;
116 USHORT ArrayVariable:1;
117 USHORT Relative:1;
118 USHORT Wrap:1;
119 USHORT NonLinear:1;
120 USHORT NoPreferred:1;
121 USHORT NullState:1;
122 USHORT IsVolatile:1;
123 USHORT BitsBytes:1;
124 UCHAR reserved[2];
125
126 }MAIN_ITEM_DATA, *PMAIN_ITEM_DATA;
127
128 typedef struct __GLOBAL_ITEM_STATE_
129 {
130 USHORT UsagePage;
131 ULONG LogicalMinimum;
132 ULONG LogicialMaximum;
133 ULONG PhysicalMinimum;
134 ULONG PhysicalMaximum;
135 UCHAR UnitExponent;
136 UCHAR Unit;
137 ULONG ReportSize;
138 ULONG ReportCount;
139 UCHAR ReportId;
140 struct __GLOBAL_ITEM_STATE__ * Next;
141 }GLOBAL_ITEM_STATE, *PGLOBAL_ITEM_STATE;
142
143
144 typedef struct usage_value
145 {
146 union
147 {
148 struct {
149 USHORT UsageId;
150 USHORT UsagePage;
151 }s;
152 ULONG Extended;
153 }u;
154
155 UCHAR IsExtended;
156 }USAGE_VALUE, *PUSAGE_VALUE;
157
158
159 typedef struct
160 {
161 PUSAGE_VALUE UsageStack;
162 ULONG UsageStackUsed;
163 ULONG UsageStackAllocated;
164
165 USAGE_VALUE UsageMinimum;
166 USAGE_VALUE UsageMaximum;
167
168 UCHAR UsageMinimumSet;
169 UCHAR UsageMaximumSet;
170
171 ULONG DesignatorIndex;
172 UCHAR DesignatorIndexSet;
173
174 ULONG DesignatorMinimum;
175 ULONG DesignatorMaximum;
176
177 UCHAR StringIndex;
178 UCHAR StringIndexSet;
179 UCHAR StringMinimum;
180 UCHAR StringMaximum;
181
182 }LOCAL_ITEM_STATE, *PLOCAL_ITEM_STATE;
183
184 typedef struct
185 {
186 ULONG ByteOffset;
187 UCHAR Shift;
188 ULONG Mask;
189 UCHAR BitCount;
190 UCHAR HasData;
191 UCHAR Array;
192 UCHAR Relative;
193 ULONG Minimum;
194 ULONG Maximum;
195 ULONG UsageMinimum;
196 ULONG UsageMaximum;
197 ULONG Data;
198 UCHAR Valid;
199 }HID_REPORT_ITEM, *PHID_REPORT_ITEM;
200
201 struct _HID_REPORT;
202
203 typedef struct __HID_COLLECTION__
204 {
205 UCHAR Type;
206 ULONG Usage;
207 UCHAR StringID;
208 UCHAR PhysicalID;
209 ULONG ReportCount;
210 ULONG NodeCount;
211
212 struct __HID_COLLECTION__ ** Nodes;
213 struct __HID_COLLECTION__ * Root;
214 struct _HID_REPORT ** Reports;
215
216 ULONG Offsets[1];
217
218 }HID_COLLECTION, *PHID_COLLECTION;
219
220 typedef struct _HID_REPORT
221 {
222 UCHAR Type;
223 UCHAR ReportID;
224 ULONG ReportSize;
225 ULONG ItemCount;
226 ULONG ItemAllocated;
227 HID_REPORT_ITEM Items[1];
228 }HID_REPORT, *PHID_REPORT;
229
230 typedef struct
231 {
232 //
233 // global item state
234 //
235 GLOBAL_ITEM_STATE GlobalItemState;
236
237 //
238 // local item state
239 //
240 LOCAL_ITEM_STATE LocalItemState;
241
242 //
243 // root collection
244 //
245 PHID_COLLECTION RootCollection;
246
247 //
248 // uses report ids
249 //
250 UCHAR UseReportIDs;
251
252 //
253 // collection index
254 //
255 ULONG CollectionIndex;
256
257 }HID_PARSER_CONTEXT, *PHID_PARSER_CONTEXT;
258
259 #define HID_REPORT_TYPE_INPUT 0x01
260 #define HID_REPORT_TYPE_OUTPUT 0x02
261 #define HID_REPORT_TYPE_FEATURE 0x04
262
263 ULONG
264 HidParser_UsesReportId(
265 IN PVOID CollectionContext,
266 IN UCHAR ReportType);
267
268 HIDPARSER_STATUS
269 HidParser_GetCollectionUsagePage(
270 IN PVOID CollectionContext,
271 OUT PUSHORT Usage,
272 OUT PUSHORT UsagePage);
273
274 ULONG
275 HidParser_GetReportLength(
276 IN PVOID CollectionContext,
277 IN UCHAR ReportType);
278
279 UCHAR
280 HidParser_IsReportIDUsed(
281 IN PHID_PARSER Parser);
282
283 ULONG
284 HidParser_GetReportItemCountFromReportType(
285 IN PVOID CollectionContext,
286 IN UCHAR ReportType);
287
288 ULONG
289 HidParser_GetReportItemTypeCountFromReportType(
290 IN PVOID CollectionContext,
291 IN UCHAR ReportType,
292 IN ULONG bData);
293
294 ULONG
295 HidParser_GetMaxUsageListLengthWithReportAndPage(
296 IN PVOID CollectionContext,
297 IN UCHAR ReportType,
298 IN USAGE UsagePage OPTIONAL);
299
300 HIDPARSER_STATUS
301 HidParser_GetSpecificValueCapsWithReport(
302 IN PHID_PARSER Parser,
303 IN PVOID CollectionContext,
304 IN UCHAR ReportType,
305 IN USHORT UsagePage,
306 IN USHORT Usage,
307 OUT PHIDP_VALUE_CAPS ValueCaps,
308 IN OUT PUSHORT ValueCapsLength);
309
310
311 HIDPARSER_STATUS
312 HidParser_GetUsagesWithReport(
313 IN PHID_PARSER Parser,
314 IN PVOID CollectionContext,
315 IN UCHAR ReportType,
316 IN USAGE UsagePage,
317 OUT USAGE *UsageList,
318 IN OUT PULONG UsageLength,
319 IN PCHAR ReportDescriptor,
320 IN ULONG ReportDescriptorLength);
321
322 HIDPARSER_STATUS
323 HidParser_GetScaledUsageValueWithReport(
324 IN PHID_PARSER Parser,
325 IN PVOID CollectionContext,
326 IN UCHAR ReportType,
327 IN USAGE UsagePage,
328 IN USAGE Usage,
329 OUT PLONG UsageValue,
330 IN PCHAR ReportDescriptor,
331 IN ULONG ReportDescriptorLength);
332
333 HIDPARSER_STATUS
334 HidParser_GetUsageValueWithReport(
335 IN PHID_PARSER Parser,
336 IN PVOID CollectionContext,
337 IN UCHAR ReportType,
338 IN USAGE UsagePage,
339 IN USAGE Usage,
340 OUT PULONG UsageValue,
341 IN PCHAR ReportDescriptor,
342 IN ULONG ReportDescriptorLength);
343
344 /* parser.c */
345
346 HIDPARSER_STATUS
347 HidParser_BuildContext(
348 IN PHID_PARSER Parser,
349 IN PVOID ParserContext,
350 IN ULONG CollectionIndex,
351 IN ULONG ContextSize,
352 OUT PVOID *CollectionContext);
353
354 ULONG
355 HidParser_CalculateContextSize(
356 IN PHID_COLLECTION Collection);
357
358 HIDPARSER_STATUS
359 HidParser_ParseReportDescriptor(
360 PHID_PARSER Parser,
361 PUCHAR Report,
362 ULONG ReportSize,
363 OUT PVOID *ParserContext);
364
365 ULONG
366 HidParser_NumberOfTopCollections(
367 IN PVOID ParserContext);
368
369 ULONG
370 HidParser_GetContextSize(
371 IN PHID_PARSER Parser,
372 IN PVOID ParserContext,
373 IN ULONG CollectionNumber);
374
375
376 /* context.c */
377
378 PHID_COLLECTION
379 HidParser_GetCollectionFromContext(
380 IN PVOID Context);
381
382 ULONG
383 HidParser_GetTotalCollectionCount(
384 IN PVOID CollectionContext);
385
386 HIDPARSER_STATUS
387 HidParser_BuildCollectionContext(
388 IN PHID_PARSER Parser,
389 IN PHID_COLLECTION RootCollection,
390 IN PVOID Context,
391 IN ULONG ContextSize);
392
393 PHID_REPORT
394 HidParser_GetReportInCollection(
395 IN PVOID Context,
396 IN UCHAR ReportType);
397
398 #endif /* _HIDPARSER_H_ */