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