[USB]
[reactos.git] / reactos / 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 ULONG
261 HidParser_UsesReportId(
262 IN PVOID CollectionContext,
263 IN UCHAR ReportType);
264
265 HIDPARSER_STATUS
266 HidParser_GetCollectionUsagePage(
267 IN PVOID CollectionContext,
268 OUT PUSHORT Usage,
269 OUT PUSHORT UsagePage);
270
271 ULONG
272 HidParser_GetReportLength(
273 IN PVOID CollectionContext,
274 IN UCHAR ReportType);
275
276 UCHAR
277 HidParser_IsReportIDUsed(
278 IN PHID_PARSER Parser);
279
280 ULONG
281 HidParser_GetReportItemCountFromReportType(
282 IN PVOID CollectionContext,
283 IN UCHAR ReportType);
284
285 ULONG
286 HidParser_GetReportItemTypeCountFromReportType(
287 IN PVOID CollectionContext,
288 IN UCHAR ReportType,
289 IN ULONG bData);
290
291 ULONG
292 HidParser_GetMaxUsageListLengthWithReportAndPage(
293 IN PVOID CollectionContext,
294 IN UCHAR ReportType,
295 IN USAGE UsagePage OPTIONAL);
296
297 HIDPARSER_STATUS
298 HidParser_GetSpecificValueCapsWithReport(
299 IN PHID_PARSER Parser,
300 IN PVOID CollectionContext,
301 IN UCHAR ReportType,
302 IN USHORT UsagePage,
303 IN USHORT Usage,
304 OUT PHIDP_VALUE_CAPS ValueCaps,
305 IN OUT PULONG ValueCapsLength);
306
307
308 HIDPARSER_STATUS
309 HidParser_GetUsagesWithReport(
310 IN PHID_PARSER Parser,
311 IN PVOID CollectionContext,
312 IN UCHAR ReportType,
313 IN USAGE UsagePage,
314 OUT USAGE *UsageList,
315 IN OUT PULONG UsageLength,
316 IN PCHAR ReportDescriptor,
317 IN ULONG ReportDescriptorLength);
318
319 HIDPARSER_STATUS
320 HidParser_GetScaledUsageValueWithReport(
321 IN PHID_PARSER Parser,
322 IN PVOID CollectionContext,
323 IN UCHAR ReportType,
324 IN USAGE UsagePage,
325 IN USAGE Usage,
326 OUT PLONG UsageValue,
327 IN PCHAR ReportDescriptor,
328 IN ULONG ReportDescriptorLength);
329
330 /* parser.c */
331
332 HIDPARSER_STATUS
333 HidParser_BuildContext(
334 IN PHID_PARSER Parser,
335 IN PVOID ParserContext,
336 IN ULONG CollectionIndex,
337 IN ULONG ContextSize,
338 OUT PVOID *CollectionContext);
339
340 ULONG
341 HidParser_CalculateContextSize(
342 IN PHID_COLLECTION Collection);
343
344 HIDPARSER_STATUS
345 HidParser_ParseReportDescriptor(
346 PHID_PARSER Parser,
347 PUCHAR Report,
348 ULONG ReportSize,
349 OUT PVOID *ParserContext);
350
351 ULONG
352 HidParser_NumberOfTopCollections(
353 IN PVOID ParserContext);
354
355 ULONG
356 HidParser_GetContextSize(
357 IN PHID_PARSER Parser,
358 IN PVOID ParserContext,
359 IN ULONG CollectionNumber);
360
361
362 /* context.c */
363
364 PHID_COLLECTION
365 HidParser_GetCollectionFromContext(
366 IN PVOID Context);
367
368 ULONG
369 HidParser_GetTotalCollectionCount(
370 IN PVOID CollectionContext);
371
372 HIDPARSER_STATUS
373 HidParser_BuildCollectionContext(
374 IN PHID_PARSER Parser,
375 IN PHID_COLLECTION RootCollection,
376 IN PVOID Context,
377 IN ULONG ContextSize);
378
379 PHID_REPORT
380 HidParser_GetReportInCollection(
381 IN PVOID Context,
382 IN UCHAR ReportType);