Added flag to the HID report parser to indicate if a device has multiple reports.
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Host / HIDParser.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 #include "../../HighLevel/USBMode.h"
32 #if defined(USB_CAN_BE_HOST)
33
34 #include "HIDParser.h"
35
36 uint8_t USB_ProcessHIDReport(const uint8_t* ReportData, uint16_t ReportSize, HID_ReportInfo_t* const ParserData)
37 {
38 HID_StateTable_t StateTable[HID_STATETABLE_STACK_DEPTH];
39 HID_StateTable_t* CurrStateTable = &StateTable[0];
40 uint16_t UsageStack[HID_USAGE_STACK_DEPTH];
41 uint8_t UsageStackSize = 0;
42 uint16_t BitOffsetIn = 0;
43 uint16_t BitOffsetOut = 0;
44 #if defined(HID_ENABLE_FEATURE_PROCESSING)
45 uint16_t BitOffsetFeature = 0;
46 #endif
47 HID_CollectionPath_t* CurrCollectionPath = NULL;
48
49 memset(ParserData, 0x00, sizeof(HID_ReportInfo_t));
50 memset(StateTable, 0x00, sizeof(StateTable));
51
52 while (ReportSize)
53 {
54 uint8_t HIDReportItem = *(ReportData++);
55 uint32_t ReportItemData = 0;
56
57 ReportSize--;
58
59 switch (HIDReportItem & DATA_SIZE_MASK)
60 {
61 case DATA_SIZE_4:
62 ReportItemData = *((uint32_t*)ReportData);
63 ReportSize -= 4;
64 ReportData += 4;
65 break;
66 case DATA_SIZE_2:
67 ReportItemData = *((uint16_t*)ReportData);
68 ReportSize -= 2;
69 ReportData += 2;
70 break;
71 case DATA_SIZE_1:
72 ReportItemData = *((uint8_t*)ReportData);
73 ReportSize -= 1;
74 ReportData += 1;
75 break;
76 }
77
78 switch (HIDReportItem & (TYPE_MASK | TAG_MASK))
79 {
80 case (TYPE_GLOBAL | TAG_GLOBAL_PUSH):
81 if (CurrStateTable == &StateTable[HID_STATETABLE_STACK_DEPTH - 1])
82 return HID_PARSE_HIDStackOverflow;
83
84 memcpy((CurrStateTable + 1),
85 CurrStateTable,
86 sizeof(HID_ReportItem_t));
87
88 CurrStateTable++;
89 break;
90 case (TYPE_GLOBAL | TAG_GLOBAL_POP):
91 if (CurrStateTable == &StateTable[0])
92 return HID_PARSE_HIDStackUnderflow;
93
94 CurrStateTable--;
95 break;
96 case (TYPE_GLOBAL | TAG_GLOBAL_USAGEPAGE):
97 CurrStateTable->Attributes.Usage.Page = ReportItemData;
98 break;
99 case (TYPE_GLOBAL | TAG_GLOBAL_LOGICALMIN):
100 CurrStateTable->Attributes.Logical.Minimum = ReportItemData;
101 break;
102 case (TYPE_GLOBAL | TAG_GLOBAL_LOGICALMAX):
103 CurrStateTable->Attributes.Logical.Maximum = ReportItemData;
104 break;
105 case (TYPE_GLOBAL | TAG_GLOBAL_PHYSMIN):
106 CurrStateTable->Attributes.Physical.Minimum = ReportItemData;
107 break;
108 case (TYPE_GLOBAL | TAG_GLOBAL_PHYSMAX):
109 CurrStateTable->Attributes.Physical.Maximum = ReportItemData;
110 break;
111 case (TYPE_GLOBAL | TAG_GLOBAL_UNITEXP):
112 CurrStateTable->Attributes.Unit.Exponent = ReportItemData;
113 break;
114 case (TYPE_GLOBAL | TAG_GLOBAL_UNIT):
115 CurrStateTable->Attributes.Unit.Type = ReportItemData;
116 break;
117 case (TYPE_GLOBAL | TAG_GLOBAL_REPORTSIZE):
118 CurrStateTable->Attributes.BitSize = ReportItemData;
119 break;
120 case (TYPE_GLOBAL | TAG_GLOBAL_REPORTCOUNT):
121 CurrStateTable->ReportCount = ReportItemData;
122 break;
123 case (TYPE_GLOBAL | TAG_GLOBAL_REPORTID):
124 CurrStateTable->ReportID = ReportItemData;
125 ParserData->UsingMultipleReports = true;
126 BitOffsetIn = 0;
127 BitOffsetOut = 0;
128
129 #if defined(HID_ENABLE_FEATURE_PROCESSING)
130 BitOffsetFeature = 0;
131 #endif
132 break;
133 case (TYPE_LOCAL | TAG_LOCAL_USAGE):
134 if (UsageStackSize == HID_USAGE_STACK_DEPTH)
135 return HID_PARSE_UsageStackOverflow;
136
137 UsageStack[UsageStackSize++] = ReportItemData;
138 break;
139 case (TYPE_LOCAL | TAG_LOCAL_USAGEMIN):
140 CurrStateTable->Attributes.Usage.MinMax.Minimum = ReportItemData;
141 break;
142 case (TYPE_LOCAL | TAG_LOCAL_USAGEMAX):
143 CurrStateTable->Attributes.Usage.MinMax.Maximum = ReportItemData;
144 break;
145 case (TYPE_MAIN | TAG_MAIN_COLLECTION):
146 if (CurrCollectionPath == NULL)
147 {
148 CurrCollectionPath = &ParserData->CollectionPaths[0];
149 }
150 else
151 {
152 HID_CollectionPath_t* ParentCollectionPath = CurrCollectionPath;
153
154 CurrCollectionPath = &ParserData->CollectionPaths[1];
155
156 while (CurrCollectionPath->Parent != NULL);
157 {
158 if (CurrCollectionPath == &ParserData->CollectionPaths[HID_MAX_COLLECTIONS - 1])
159 return HID_PARSE_InsufficientCollectionPaths;
160
161 CurrCollectionPath++;
162 }
163
164 CurrCollectionPath->Parent = ParentCollectionPath;
165 }
166
167 CurrCollectionPath->Type = ReportItemData;
168 CurrCollectionPath->Usage.Page = CurrStateTable->Attributes.Usage.Page;
169
170 if (UsageStackSize)
171 {
172 CurrCollectionPath->Usage.Usage = UsageStack[0];
173
174 for (uint8_t i = 0; i < UsageStackSize; i++)
175 UsageStack[i] = UsageStack[i + 1];
176
177 UsageStackSize--;
178 }
179 else
180 {
181 CurrCollectionPath->Usage.Usage = 0;
182 }
183
184 break;
185 case (TYPE_MAIN | TAG_MAIN_ENDCOLLECTION):
186 if (CurrCollectionPath == NULL)
187 return HID_PARSE_UnexpectedEndCollection;
188
189 CurrCollectionPath = CurrCollectionPath->Parent;
190
191 break;
192 case (TYPE_MAIN | TAG_MAIN_INPUT):
193 case (TYPE_MAIN | TAG_MAIN_OUTPUT):
194 #if defined(HID_ENABLE_FEATURE_PROCESSING)
195 case (TYPE_MAIN | TAG_MAIN_FEATURE):
196 #endif
197 for (uint8_t ReportItemNum = 0; ReportItemNum < CurrStateTable->ReportCount; ReportItemNum++)
198 {
199 HID_ReportItem_t* CurrReportItem = &ParserData->ReportItems[ParserData->TotalReportItems];
200
201 if (ParserData->TotalReportItems == HID_MAX_REPORTITEMS)
202 return HID_PARSE_InsufficientReportItems;
203
204 memcpy(&CurrReportItem->Attributes,
205 &CurrStateTable->Attributes,
206 sizeof(HID_ReportItem_Attributes_t));
207
208 CurrReportItem->ItemFlags = ReportItemData;
209 CurrReportItem->CollectionPath = CurrCollectionPath;
210 CurrReportItem->ReportID = CurrStateTable->ReportID;
211
212 if (UsageStackSize)
213 {
214 CurrReportItem->Attributes.Usage.Usage = UsageStack[0];
215
216 for (uint8_t i = 0; i < UsageStackSize; i++)
217 UsageStack[i] = UsageStack[i + 1];
218
219 UsageStackSize--;
220 }
221 else
222 {
223 CurrReportItem->Attributes.Usage.Usage = 0;
224 }
225
226 switch (HIDReportItem & TAG_MASK)
227 {
228 case TAG_MAIN_INPUT:
229 CurrReportItem->ItemType = REPORT_ITEM_TYPE_In;
230 CurrReportItem->BitOffset = BitOffsetIn;
231
232 BitOffsetIn += CurrStateTable->Attributes.BitSize;
233
234 break;
235 case TAG_MAIN_OUTPUT:
236 CurrReportItem->ItemType = REPORT_ITEM_TYPE_Out;
237 CurrReportItem->BitOffset = BitOffsetOut;
238
239 BitOffsetOut += CurrStateTable->Attributes.BitSize;
240
241 break;
242 #if defined(HID_ENABLE_FEATURE_PROCESSING)
243 case TAG_MAIN_FEATURE:
244 CurrReportItem->ItemType = REPORT_ITEM_TYPE_Feature;
245 CurrReportItem->BitOffset = BitOffsetFeature;
246
247 BitOffsetFeature += CurrStateTable->Attributes.BitSize;
248
249 break;
250 #endif
251 }
252
253 #if defined(HID_INCLUDE_CONSTANT_DATA_ITEMS)
254 ParserData->TotalReportItems++;
255 #else
256 if (!(ReportItemData & IOF_CONSTANT))
257 ParserData->TotalReportItems++;
258 #endif
259 }
260
261 UsageStackSize = 0;
262
263 break;
264 }
265
266 if ((HIDReportItem & TYPE_MASK) == TYPE_MAIN)
267 {
268 CurrStateTable->Attributes.Usage.MinMax.Minimum = 0;
269 CurrStateTable->Attributes.Usage.MinMax.Maximum = 0;
270 UsageStackSize = 0;
271 }
272 }
273
274 return HID_PARSE_Successful;
275 }
276
277 bool USB_GetHIDReportItemInfo(const uint8_t* ReportData, HID_ReportItem_t* const ReportItem)
278 {
279 uint16_t DataBitsRem = ReportItem->Attributes.BitSize;
280 uint16_t CurrentBit = ReportItem->BitOffset;
281 uint32_t BitMask = (1 << 0);
282
283 ReportItem->Value = 0;
284
285 if (ReportItem->ReportID)
286 {
287 if (ReportItem->ReportID != ReportData[0])
288 return false;
289
290 ReportData++;
291 }
292
293 while (DataBitsRem--)
294 {
295 if (ReportData[CurrentBit / 8] & (1 << (CurrentBit % 8)))
296 ReportItem->Value |= BitMask;
297
298 CurrentBit++;
299 BitMask <<= 1;
300 }
301
302 return true;
303 }
304
305 void USB_SetHIDReportItemInfo(uint8_t* ReportData, const HID_ReportItem_t* ReportItem)
306 {
307 uint16_t DataBitsRem = ReportItem->Attributes.BitSize;
308 uint16_t CurrentBit = ReportItem->BitOffset;
309 uint32_t BitMask = (1 << 0);
310
311 if (ReportItem->ReportID)
312 {
313 ReportData[0] = ReportItem->ReportID;
314 ReportData++;
315 }
316
317 while (DataBitsRem--)
318 {
319 if (ReportItem->Value & (1 << (CurrentBit % 8)))
320 ReportData[CurrentBit / 8] |= BitMask;
321
322 CurrentBit++;
323 BitMask <<= 1;
324 }
325 }
326
327 #endif