Fixed PrinterHost demo returning invalid Device ID data when the attached device...
[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 HID_CollectionPath_t* CurrCollectionPath = NULL;
41 HID_ReportSizeInfo_t* CurrReportIDInfo = &ParserData->ReportIDSizes[0];
42 uint16_t UsageStack[HID_USAGE_STACK_DEPTH];
43 uint8_t UsageStackSize = 0;
44
45 ParserData->TotalReportItems = 0;
46 ParserData->TotalDeviceReports = 1;
47 ParserData->UsingReportIDs = false;
48
49 for (uint8_t CurrCollection = 0; CurrCollection < HID_MAX_COLLECTIONS; CurrCollection++)
50 ParserData->CollectionPaths[CurrCollection].Parent = NULL;
51
52 memset(CurrStateTable, 0x00, sizeof(HID_StateTable_t));
53 memset(CurrReportIDInfo, 0x00, sizeof(HID_ReportSizeInfo_t));
54
55 while (ReportSize)
56 {
57 uint8_t HIDReportItem = *ReportData;
58 uint32_t ReportItemData = 0;
59
60 ReportData++;
61 ReportSize--;
62
63 switch (HIDReportItem & DATA_SIZE_MASK)
64 {
65 case DATA_SIZE_4:
66 ReportItemData = *((uint32_t*)ReportData);
67 ReportSize -= 4;
68 ReportData += 4;
69 break;
70 case DATA_SIZE_2:
71 ReportItemData = *((uint16_t*)ReportData);
72 ReportSize -= 2;
73 ReportData += 2;
74 break;
75 case DATA_SIZE_1:
76 ReportItemData = *((uint8_t*)ReportData);
77 ReportSize -= 1;
78 ReportData += 1;
79 break;
80 }
81
82 switch (HIDReportItem & (TYPE_MASK | TAG_MASK))
83 {
84 case (TYPE_GLOBAL | TAG_GLOBAL_PUSH):
85 if (CurrStateTable == &StateTable[HID_STATETABLE_STACK_DEPTH - 1])
86 return HID_PARSE_HIDStackOverflow;
87
88 memcpy((CurrStateTable + 1),
89 CurrStateTable,
90 sizeof(HID_ReportItem_t));
91
92 CurrStateTable++;
93 break;
94 case (TYPE_GLOBAL | TAG_GLOBAL_POP):
95 if (CurrStateTable == &StateTable[0])
96 return HID_PARSE_HIDStackUnderflow;
97
98 CurrStateTable--;
99 break;
100 case (TYPE_GLOBAL | TAG_GLOBAL_USAGEPAGE):
101 CurrStateTable->Attributes.Usage.Page = ReportItemData;
102 break;
103 case (TYPE_GLOBAL | TAG_GLOBAL_LOGICALMIN):
104 CurrStateTable->Attributes.Logical.Minimum = ReportItemData;
105 break;
106 case (TYPE_GLOBAL | TAG_GLOBAL_LOGICALMAX):
107 CurrStateTable->Attributes.Logical.Maximum = ReportItemData;
108 break;
109 case (TYPE_GLOBAL | TAG_GLOBAL_PHYSMIN):
110 CurrStateTable->Attributes.Physical.Minimum = ReportItemData;
111 break;
112 case (TYPE_GLOBAL | TAG_GLOBAL_PHYSMAX):
113 CurrStateTable->Attributes.Physical.Maximum = ReportItemData;
114 break;
115 case (TYPE_GLOBAL | TAG_GLOBAL_UNITEXP):
116 CurrStateTable->Attributes.Unit.Exponent = ReportItemData;
117 break;
118 case (TYPE_GLOBAL | TAG_GLOBAL_UNIT):
119 CurrStateTable->Attributes.Unit.Type = ReportItemData;
120 break;
121 case (TYPE_GLOBAL | TAG_GLOBAL_REPORTSIZE):
122 CurrStateTable->Attributes.BitSize = ReportItemData;
123 break;
124 case (TYPE_GLOBAL | TAG_GLOBAL_REPORTCOUNT):
125 CurrStateTable->ReportCount = ReportItemData;
126 break;
127 case (TYPE_GLOBAL | TAG_GLOBAL_REPORTID):
128 CurrStateTable->ReportID = ReportItemData;
129
130 if (ParserData->UsingReportIDs)
131 {
132 CurrReportIDInfo = NULL;
133
134 for (uint8_t i = 0; i < ParserData->TotalDeviceReports; i++)
135 {
136 if (ParserData->ReportIDSizes[i].ReportID == CurrStateTable->ReportID)
137 {
138 CurrReportIDInfo = &ParserData->ReportIDSizes[i];
139 break;
140 }
141 }
142
143 if (CurrReportIDInfo == NULL)
144 {
145 if (ParserData->TotalDeviceReports++ > HID_MAX_REPORT_IDS)
146 return HID_PARSE_InsufficientReportIDItems;
147
148 CurrReportIDInfo = &ParserData->ReportIDSizes[ParserData->TotalDeviceReports - 1];
149 memset(CurrReportIDInfo, 0x00, sizeof(HID_ReportSizeInfo_t));
150 }
151 }
152
153 ParserData->UsingReportIDs = true;
154
155 CurrReportIDInfo->ReportID = CurrStateTable->ReportID;
156 break;
157 case (TYPE_LOCAL | TAG_LOCAL_USAGE):
158 if (UsageStackSize == HID_USAGE_STACK_DEPTH)
159 return HID_PARSE_UsageStackOverflow;
160
161 UsageStack[UsageStackSize++] = ReportItemData;
162 break;
163 case (TYPE_LOCAL | TAG_LOCAL_USAGEMIN):
164 CurrStateTable->Attributes.Usage.MinMax.Minimum = ReportItemData;
165 break;
166 case (TYPE_LOCAL | TAG_LOCAL_USAGEMAX):
167 CurrStateTable->Attributes.Usage.MinMax.Maximum = ReportItemData;
168 break;
169 case (TYPE_MAIN | TAG_MAIN_COLLECTION):
170 if (CurrCollectionPath == NULL)
171 {
172 CurrCollectionPath = &ParserData->CollectionPaths[0];
173 }
174 else
175 {
176 HID_CollectionPath_t* ParentCollectionPath = CurrCollectionPath;
177
178 CurrCollectionPath = &ParserData->CollectionPaths[1];
179
180 while (CurrCollectionPath->Parent != NULL);
181 {
182 if (CurrCollectionPath == &ParserData->CollectionPaths[HID_MAX_COLLECTIONS - 1])
183 return HID_PARSE_InsufficientCollectionPaths;
184
185 CurrCollectionPath++;
186 }
187
188 CurrCollectionPath->Parent = ParentCollectionPath;
189 }
190
191 CurrCollectionPath->Type = ReportItemData;
192 CurrCollectionPath->Usage.Page = CurrStateTable->Attributes.Usage.Page;
193
194 if (UsageStackSize)
195 {
196 CurrCollectionPath->Usage.Usage = UsageStack[0];
197
198 for (uint8_t i = 0; i < UsageStackSize; i++)
199 UsageStack[i] = UsageStack[i + 1];
200
201 UsageStackSize--;
202 }
203 else
204 {
205 CurrCollectionPath->Usage.Usage = 0;
206 }
207
208 break;
209 case (TYPE_MAIN | TAG_MAIN_ENDCOLLECTION):
210 if (CurrCollectionPath == NULL)
211 return HID_PARSE_UnexpectedEndCollection;
212
213 CurrCollectionPath = CurrCollectionPath->Parent;
214
215 break;
216 case (TYPE_MAIN | TAG_MAIN_INPUT):
217 case (TYPE_MAIN | TAG_MAIN_OUTPUT):
218 case (TYPE_MAIN | TAG_MAIN_FEATURE):
219 for (uint8_t ReportItemNum = 0; ReportItemNum < CurrStateTable->ReportCount; ReportItemNum++)
220 {
221 HID_ReportItem_t NewReportItem;
222
223 memcpy(&NewReportItem.Attributes,
224 &CurrStateTable->Attributes,
225 sizeof(HID_ReportItem_Attributes_t));
226
227 NewReportItem.ItemFlags = ReportItemData;
228 NewReportItem.CollectionPath = CurrCollectionPath;
229 NewReportItem.ReportID = CurrStateTable->ReportID;
230
231 if (UsageStackSize)
232 {
233 NewReportItem.Attributes.Usage.Usage = UsageStack[0];
234
235 for (uint8_t i = 0; i < UsageStackSize; i++)
236 UsageStack[i] = UsageStack[i + 1];
237
238 UsageStackSize--;
239 }
240 else
241 {
242 NewReportItem.Attributes.Usage.Usage = 0;
243 }
244
245 uint8_t ReportSizeIndex = 0;
246
247 switch (HIDReportItem & TAG_MASK)
248 {
249 case TAG_MAIN_INPUT:
250 NewReportItem.ItemType = REPORT_ITEM_TYPE_In;
251 NewReportItem.BitOffset = CurrReportIDInfo->ReportSizeBits[REPORT_ITEM_TYPE_In];
252
253 ReportSizeIndex = REPORT_ITEM_TYPE_In;
254 break;
255 case TAG_MAIN_OUTPUT:
256 NewReportItem.ItemType = REPORT_ITEM_TYPE_Out;
257 NewReportItem.BitOffset = CurrReportIDInfo->ReportSizeBits[REPORT_ITEM_TYPE_Out];
258
259 ReportSizeIndex = REPORT_ITEM_TYPE_Out;
260 break;
261 case TAG_MAIN_FEATURE:
262 NewReportItem.ItemType = REPORT_ITEM_TYPE_Feature;
263 NewReportItem.BitOffset = CurrReportIDInfo->ReportSizeBits[REPORT_ITEM_TYPE_Feature];
264
265 ReportSizeIndex = REPORT_ITEM_TYPE_Feature;
266 break;
267 }
268
269 CurrReportIDInfo->ReportSizeBits[ReportSizeIndex] += CurrStateTable->Attributes.BitSize;
270
271 if (ParserData->LargestReportSizeBits < CurrReportIDInfo->ReportSizeBits[ReportSizeIndex])
272 ParserData->LargestReportSizeBits = CurrReportIDInfo->ReportSizeBits[ReportSizeIndex];
273
274 if (!(ReportItemData & IOF_CONSTANT) && CALLBACK_HIDParser_FilterHIDReportItem(&CurrStateTable->Attributes))
275 {
276 if (ParserData->TotalReportItems == HID_MAX_REPORTITEMS)
277 return HID_PARSE_InsufficientReportItems;
278
279 memcpy(&ParserData->ReportItems[ParserData->TotalReportItems],
280 &NewReportItem, sizeof(HID_ReportItem_t));
281
282 ParserData->TotalReportItems++;
283 }
284 }
285
286 UsageStackSize = 0;
287
288 break;
289 }
290
291 if ((HIDReportItem & TYPE_MASK) == TYPE_MAIN)
292 {
293 CurrStateTable->Attributes.Usage.MinMax.Minimum = 0;
294 CurrStateTable->Attributes.Usage.MinMax.Maximum = 0;
295 UsageStackSize = 0;
296 }
297 }
298
299 if (!(ParserData->TotalReportItems))
300 return HID_PARSE_NoUnfilteredReportItems;
301
302 return HID_PARSE_Successful;
303 }
304
305 bool USB_GetHIDReportItemInfo(const uint8_t* ReportData, HID_ReportItem_t* const ReportItem)
306 {
307 uint16_t DataBitsRem = ReportItem->Attributes.BitSize;
308 uint16_t CurrentBit = ReportItem->BitOffset;
309 uint32_t BitMask = (1 << 0);
310
311 ReportItem->Value = 0;
312
313 if (ReportItem->ReportID)
314 {
315 if (ReportItem->ReportID != ReportData[0])
316 return false;
317
318 ReportData++;
319 }
320
321 while (DataBitsRem--)
322 {
323 if (ReportData[CurrentBit / 8] & (1 << (CurrentBit % 8)))
324 ReportItem->Value |= BitMask;
325
326 CurrentBit++;
327 BitMask <<= 1;
328 }
329
330 return true;
331 }
332
333 void USB_SetHIDReportItemInfo(uint8_t* ReportData, const HID_ReportItem_t* ReportItem)
334 {
335 uint16_t DataBitsRem = ReportItem->Attributes.BitSize;
336 uint16_t CurrentBit = ReportItem->BitOffset;
337 uint32_t BitMask = (1 << 0);
338
339 if (ReportItem->ReportID)
340 {
341 ReportData[0] = ReportItem->ReportID;
342 ReportData++;
343 }
344
345 while (DataBitsRem--)
346 {
347 if (ReportItem->Value & (1 << (CurrentBit % 8)))
348 ReportData[CurrentBit / 8] |= BitMask;
349
350 CurrentBit++;
351 BitMask <<= 1;
352 }
353 }
354
355 uint16_t USB_GetHIDReportSize(HID_ReportInfo_t* const ParserData, const uint8_t ReportID, const uint8_t ReportType)
356 {
357 for (uint8_t i = 0; i < HID_MAX_REPORT_IDS; i++)
358 {
359 if (ParserData->ReportIDSizes[i].ReportID == ReportID)
360 return ParserData->ReportIDSizes[i].ReportSizeBits[ReportType];
361 }
362
363 return 0;
364 }
365
366 #endif