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