1/*
2 * Copyright 2009-2011, Michael Lotz, mmlr@mlotz.ch.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef HID_DATA_TYPES_H
6#define HID_DATA_TYPES_H
7
8#include <lendian_bitfield.h>
9
10#define ITEM_TYPE_MAIN						0x0
11#define ITEM_TYPE_GLOBAL					0x1
12#define ITEM_TYPE_LOCAL						0x2
13#define ITEM_TYPE_LONG						0x3
14
15#define ITEM_TAG_MAIN_INPUT					0x8
16#define ITEM_TAG_MAIN_OUTPUT				0x9
17#define ITEM_TAG_MAIN_FEATURE				0xb
18#define ITEM_TAG_MAIN_COLLECTION			0xa
19#define ITEM_TAG_MAIN_END_COLLECTION		0xc
20
21#define ITEM_TAG_GLOBAL_USAGE_PAGE			0x0
22#define ITEM_TAG_GLOBAL_LOGICAL_MINIMUM		0x1
23#define ITEM_TAG_GLOBAL_LOGICAL_MAXIMUM		0x2
24#define ITEM_TAG_GLOBAL_PHYSICAL_MINIMUM	0x3
25#define ITEM_TAG_GLOBAL_PHYSICAL_MAXIMUM	0x4
26#define ITEM_TAG_GLOBAL_UNIT_EXPONENT		0x5
27#define ITEM_TAG_GLOBAL_UNIT				0x6
28#define ITEM_TAG_GLOBAL_REPORT_SIZE			0x7
29#define ITEM_TAG_GLOBAL_REPORT_ID			0x8
30#define ITEM_TAG_GLOBAL_REPORT_COUNT		0x9
31#define ITEM_TAG_GLOBAL_PUSH				0xa
32#define ITEM_TAG_GLOBAL_POP					0xb
33
34#define ITEM_TAG_LOCAL_USAGE				0x0
35#define ITEM_TAG_LOCAL_USAGE_MINIMUM		0x1
36#define ITEM_TAG_LOCAL_USAGE_MAXIMUM		0x2
37#define ITEM_TAG_LOCAL_DESIGNATOR_INDEX		0x3
38#define ITEM_TAG_LOCAL_DESIGNATOR_MINIMUM	0x4
39#define ITEM_TAG_LOCAL_DESIGNATOR_MAXIMUM	0x5
40#define ITEM_TAG_LOCAL_STRING_INDEX			0x7
41#define ITEM_TAG_LOCAL_STRING_MINIMUM		0x8
42#define ITEM_TAG_LOCAL_STRING_MAXIMUM		0x9
43#define ITEM_TAG_LOCAL_DELIMITER			0xa
44
45#define ITEM_TAG_LONG						0xf
46
47#define COLLECTION_PHYSICAL					0x00
48#define COLLECTION_APPLICATION				0x01
49#define COLLECTION_LOGICAL					0x02
50#define COLLECTION_REPORT					0x03
51#define COLLECTION_NAMED_ARRAY				0x04
52#define COLLECTION_USAGE_SWITCH				0x05
53#define COLLECTION_USAGE_MODIFIER			0x06
54#define COLLECTION_ALL						0xff
55
56#define UNIT_SYSTEM							0x0
57#define UNIT_LENGTH							0x1
58#define UNIT_MASS							0x2
59#define UNIT_TIME							0x3
60#define UNIT_TEMPERATURE					0x4
61#define UNIT_CURRENT						0x5
62#define UNIT_LUMINOUS_INTENSITY				0x6
63
64#define USAGE_PAGE_SHIFT					16
65#define USAGE_PAGE_MASK						0xffff
66#define USAGE_ID_SHIFT						0
67#define USAGE_ID_MASK						0xffff
68
69
70typedef struct item_prefix {
71	LBITFIELD8_3(
72		size	: 2,
73		type	: 2,
74		tag		: 4
75	);
76} _PACKED item_prefix;
77
78
79typedef struct short_item {
80	item_prefix	prefix;
81
82	union {
83		uint8	as_uint8[4];
84		int8	as_int8[4];
85		uint16	as_uint16[2];
86		int16	as_int16[2];
87		uint32	as_uint32;
88		int32	as_int32;
89	} data;
90} _PACKED short_item;
91
92
93typedef struct long_item {
94	item_prefix	prefix;
95	uint8		data_size;
96	uint8		long_item_tag;
97	uint8		data[0];
98} _PACKED long_item;
99
100
101typedef struct main_item_data {
102	LBITFIELD9(
103		data_constant	: 1,
104		array_variable	: 1,
105		relative		: 1,
106		wrap			: 1,
107		non_linear		: 1,
108		no_preferred	: 1,
109		null_state		: 1,
110		is_volatile		: 1,
111		bits_bytes		: 1
112	);
113
114	//uint8			reserved[2];
115} _PACKED main_item_data;
116
117
118typedef union main_item_data_converter {
119	main_item_data	main_data;
120	uint16			flat_data;
121} main_item_data_converter;
122
123
124typedef struct usage_value {
125	union {
126		struct {
127			uint16	usage_id;
128			uint16	usage_page;
129		} _PACKED s;
130		uint32		extended;
131	} u;
132
133	bool			is_extended;
134
135					usage_value()
136					{
137						u.extended = 0;
138						is_extended = false;
139					}
140} usage_value;
141
142
143typedef struct global_item_state {
144	uint16			usage_page;
145	uint32			logical_minimum;
146	uint32			logical_maximum;
147	uint32			physical_minimum;
148	uint32			physical_maximum;
149	uint8			unit_exponent;
150	uint8			unit;
151	uint32			report_size;
152	uint32			report_count;
153	uint8			report_id;
154	global_item_state *link;
155} global_item_state;
156
157
158typedef struct local_item_state {
159	usage_value *	usage_stack;
160	uint32			usage_stack_used;
161	usage_value		usage_minimum;
162	usage_value		usage_maximum;
163	bool			usage_minimum_set;
164	bool			usage_maximum_set;
165	uint32			designator_index;
166	bool			designator_index_set;
167	uint32			designator_minimum;
168	uint32			designator_maximum;
169	uint8			string_index;
170	bool			string_index_set;
171	uint8			string_minimum;
172	uint8			string_maximum;
173} local_item_state;
174
175#endif // HID_DATA_TYPES_H
176