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