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