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