Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HIDParser.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2014.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. #define __INCLUDE_FROM_USB_DRIVER
  27. #define __INCLUDE_FROM_HID_DRIVER
  28. #include "HIDParser.h"
  29. uint8_t USB_ProcessHIDReport(const uint8_t* ReportData,
  30. uint16_t ReportSize,
  31. HID_ReportInfo_t* const ParserData)
  32. {
  33. HID_StateTable_t StateTable[HID_STATETABLE_STACK_DEPTH];
  34. HID_StateTable_t* CurrStateTable = &StateTable[0];
  35. HID_CollectionPath_t* CurrCollectionPath = NULL;
  36. HID_ReportSizeInfo_t* CurrReportIDInfo = &ParserData->ReportIDSizes[0];
  37. uint16_t UsageList[HID_USAGE_STACK_DEPTH];
  38. uint8_t UsageListSize = 0;
  39. HID_MinMax_t UsageMinMax = {0, 0};
  40. memset(ParserData, 0x00, sizeof(HID_ReportInfo_t));
  41. memset(CurrStateTable, 0x00, sizeof(HID_StateTable_t));
  42. memset(CurrReportIDInfo, 0x00, sizeof(HID_ReportSizeInfo_t));
  43. ParserData->TotalDeviceReports = 1;
  44. while (ReportSize)
  45. {
  46. uint8_t HIDReportItem = *ReportData;
  47. uint32_t ReportItemData;
  48. ReportData++;
  49. ReportSize--;
  50. switch (HIDReportItem & HID_RI_DATA_SIZE_MASK)
  51. {
  52. case HID_RI_DATA_BITS_32:
  53. ReportItemData = (((uint32_t)ReportData[3] << 24) | ((uint32_t)ReportData[2] << 16) |
  54. ((uint16_t)ReportData[1] << 8) | ReportData[0]);
  55. ReportSize -= 4;
  56. ReportData += 4;
  57. break;
  58. case HID_RI_DATA_BITS_16:
  59. ReportItemData = (((uint16_t)ReportData[1] << 8) | (ReportData[0]));
  60. ReportSize -= 2;
  61. ReportData += 2;
  62. break;
  63. case HID_RI_DATA_BITS_8:
  64. ReportItemData = ReportData[0];
  65. ReportSize -= 1;
  66. ReportData += 1;
  67. break;
  68. default:
  69. ReportItemData = 0;
  70. break;
  71. }
  72. switch (HIDReportItem & (HID_RI_TYPE_MASK | HID_RI_TAG_MASK))
  73. {
  74. case HID_RI_PUSH(0):
  75. if (CurrStateTable == &StateTable[HID_STATETABLE_STACK_DEPTH - 1])
  76. return HID_PARSE_HIDStackOverflow;
  77. memcpy((CurrStateTable + 1),
  78. CurrStateTable,
  79. sizeof(HID_ReportItem_t));
  80. CurrStateTable++;
  81. break;
  82. case HID_RI_POP(0):
  83. if (CurrStateTable == &StateTable[0])
  84. return HID_PARSE_HIDStackUnderflow;
  85. CurrStateTable--;
  86. break;
  87. case HID_RI_USAGE_PAGE(0):
  88. if ((HIDReportItem & HID_RI_DATA_SIZE_MASK) == HID_RI_DATA_BITS_32)
  89. CurrStateTable->Attributes.Usage.Page = (ReportItemData >> 16);
  90. CurrStateTable->Attributes.Usage.Page = ReportItemData;
  91. break;
  92. case HID_RI_LOGICAL_MINIMUM(0):
  93. CurrStateTable->Attributes.Logical.Minimum = ReportItemData;
  94. break;
  95. case HID_RI_LOGICAL_MAXIMUM(0):
  96. CurrStateTable->Attributes.Logical.Maximum = ReportItemData;
  97. break;
  98. case HID_RI_PHYSICAL_MINIMUM(0):
  99. CurrStateTable->Attributes.Physical.Minimum = ReportItemData;
  100. break;
  101. case HID_RI_PHYSICAL_MAXIMUM(0):
  102. CurrStateTable->Attributes.Physical.Maximum = ReportItemData;
  103. break;
  104. case HID_RI_UNIT_EXPONENT(0):
  105. CurrStateTable->Attributes.Unit.Exponent = ReportItemData;
  106. break;
  107. case HID_RI_UNIT(0):
  108. CurrStateTable->Attributes.Unit.Type = ReportItemData;
  109. break;
  110. case HID_RI_REPORT_SIZE(0):
  111. CurrStateTable->Attributes.BitSize = ReportItemData;
  112. break;
  113. case HID_RI_REPORT_COUNT(0):
  114. CurrStateTable->ReportCount = ReportItemData;
  115. break;
  116. case HID_RI_REPORT_ID(0):
  117. CurrStateTable->ReportID = ReportItemData;
  118. if (ParserData->UsingReportIDs)
  119. {
  120. CurrReportIDInfo = NULL;
  121. for (uint8_t i = 0; i < ParserData->TotalDeviceReports; i++)
  122. {
  123. if (ParserData->ReportIDSizes[i].ReportID == CurrStateTable->ReportID)
  124. {
  125. CurrReportIDInfo = &ParserData->ReportIDSizes[i];
  126. break;
  127. }
  128. }
  129. if (CurrReportIDInfo == NULL)
  130. {
  131. if (ParserData->TotalDeviceReports == HID_MAX_REPORT_IDS)
  132. return HID_PARSE_InsufficientReportIDItems;
  133. CurrReportIDInfo = &ParserData->ReportIDSizes[ParserData->TotalDeviceReports++];
  134. memset(CurrReportIDInfo, 0x00, sizeof(HID_ReportSizeInfo_t));
  135. }
  136. }
  137. ParserData->UsingReportIDs = true;
  138. CurrReportIDInfo->ReportID = CurrStateTable->ReportID;
  139. break;
  140. case HID_RI_USAGE(0):
  141. if (UsageListSize == HID_USAGE_STACK_DEPTH)
  142. return HID_PARSE_UsageListOverflow;
  143. UsageList[UsageListSize++] = ReportItemData;
  144. break;
  145. case HID_RI_USAGE_MINIMUM(0):
  146. UsageMinMax.Minimum = ReportItemData;
  147. break;
  148. case HID_RI_USAGE_MAXIMUM(0):
  149. UsageMinMax.Maximum = ReportItemData;
  150. break;
  151. case HID_RI_COLLECTION(0):
  152. if (CurrCollectionPath == NULL)
  153. {
  154. CurrCollectionPath = &ParserData->CollectionPaths[0];
  155. }
  156. else
  157. {
  158. HID_CollectionPath_t* ParentCollectionPath = CurrCollectionPath;
  159. CurrCollectionPath = &ParserData->CollectionPaths[1];
  160. while (CurrCollectionPath->Parent != NULL)
  161. {
  162. if (CurrCollectionPath == &ParserData->CollectionPaths[HID_MAX_COLLECTIONS - 1])
  163. return HID_PARSE_InsufficientCollectionPaths;
  164. CurrCollectionPath++;
  165. }
  166. CurrCollectionPath->Parent = ParentCollectionPath;
  167. }
  168. CurrCollectionPath->Type = ReportItemData;
  169. CurrCollectionPath->Usage.Page = CurrStateTable->Attributes.Usage.Page;
  170. if (UsageListSize)
  171. {
  172. CurrCollectionPath->Usage.Usage = UsageList[0];
  173. for (uint8_t i = 1; i < UsageListSize; i++)
  174. UsageList[i - 1] = UsageList[i];
  175. UsageListSize--;
  176. }
  177. else if (UsageMinMax.Minimum <= UsageMinMax.Maximum)
  178. {
  179. CurrCollectionPath->Usage.Usage = UsageMinMax.Minimum++;
  180. }
  181. break;
  182. case HID_RI_END_COLLECTION(0):
  183. if (CurrCollectionPath == NULL)
  184. return HID_PARSE_UnexpectedEndCollection;
  185. CurrCollectionPath = CurrCollectionPath->Parent;
  186. break;
  187. case HID_RI_INPUT(0):
  188. case HID_RI_OUTPUT(0):
  189. case HID_RI_FEATURE(0):
  190. for (uint8_t ReportItemNum = 0; ReportItemNum < CurrStateTable->ReportCount; ReportItemNum++)
  191. {
  192. HID_ReportItem_t NewReportItem;
  193. memcpy(&NewReportItem.Attributes,
  194. &CurrStateTable->Attributes,
  195. sizeof(HID_ReportItem_Attributes_t));
  196. NewReportItem.ItemFlags = ReportItemData;
  197. NewReportItem.CollectionPath = CurrCollectionPath;
  198. NewReportItem.ReportID = CurrStateTable->ReportID;
  199. if (UsageListSize)
  200. {
  201. NewReportItem.Attributes.Usage.Usage = UsageList[0];
  202. for (uint8_t i = 1; i < UsageListSize; i++)
  203. UsageList[i - 1] = UsageList[i];
  204. UsageListSize--;
  205. }
  206. else if (UsageMinMax.Minimum <= UsageMinMax.Maximum)
  207. {
  208. NewReportItem.Attributes.Usage.Usage = UsageMinMax.Minimum++;
  209. }
  210. uint8_t ItemTypeTag = (HIDReportItem & (HID_RI_TYPE_MASK | HID_RI_TAG_MASK));
  211. if (ItemTypeTag == HID_RI_INPUT(0))
  212. NewReportItem.ItemType = HID_REPORT_ITEM_In;
  213. else if (ItemTypeTag == HID_RI_OUTPUT(0))
  214. NewReportItem.ItemType = HID_REPORT_ITEM_Out;
  215. else
  216. NewReportItem.ItemType = HID_REPORT_ITEM_Feature;
  217. NewReportItem.BitOffset = CurrReportIDInfo->ReportSizeBits[NewReportItem.ItemType];
  218. CurrReportIDInfo->ReportSizeBits[NewReportItem.ItemType] += CurrStateTable->Attributes.BitSize;
  219. ParserData->LargestReportSizeBits = MAX(ParserData->LargestReportSizeBits, CurrReportIDInfo->ReportSizeBits[NewReportItem.ItemType]);
  220. if (ParserData->TotalReportItems == HID_MAX_REPORTITEMS)
  221. return HID_PARSE_InsufficientReportItems;
  222. memcpy(&ParserData->ReportItems[ParserData->TotalReportItems],
  223. &NewReportItem, sizeof(HID_ReportItem_t));
  224. if (!(ReportItemData & HID_IOF_CONSTANT) && CALLBACK_HIDParser_FilterHIDReportItem(&NewReportItem))
  225. ParserData->TotalReportItems++;
  226. }
  227. break;
  228. default:
  229. break;
  230. }
  231. if ((HIDReportItem & HID_RI_TYPE_MASK) == HID_RI_TYPE_MAIN)
  232. {
  233. UsageMinMax.Minimum = 0;
  234. UsageMinMax.Maximum = 0;
  235. UsageListSize = 0;
  236. }
  237. }
  238. if (!(ParserData->TotalReportItems))
  239. return HID_PARSE_NoUnfilteredReportItems;
  240. return HID_PARSE_Successful;
  241. }
  242. bool USB_GetHIDReportItemInfo(const uint8_t* ReportData,
  243. HID_ReportItem_t* const ReportItem)
  244. {
  245. if (ReportItem == NULL)
  246. return false;
  247. uint16_t DataBitsRem = ReportItem->Attributes.BitSize;
  248. uint16_t CurrentBit = ReportItem->BitOffset;
  249. uint32_t BitMask = (1 << 0);
  250. if (ReportItem->ReportID)
  251. {
  252. if (ReportItem->ReportID != ReportData[0])
  253. return false;
  254. ReportData++;
  255. }
  256. ReportItem->PreviousValue = ReportItem->Value;
  257. ReportItem->Value = 0;
  258. while (DataBitsRem--)
  259. {
  260. if (ReportData[CurrentBit / 8] & (1 << (CurrentBit % 8)))
  261. ReportItem->Value |= BitMask;
  262. CurrentBit++;
  263. BitMask <<= 1;
  264. }
  265. return true;
  266. }
  267. void USB_SetHIDReportItemInfo(uint8_t* ReportData,
  268. HID_ReportItem_t* const ReportItem)
  269. {
  270. if (ReportItem == NULL)
  271. return;
  272. uint16_t DataBitsRem = ReportItem->Attributes.BitSize;
  273. uint16_t CurrentBit = ReportItem->BitOffset;
  274. uint32_t BitMask = (1 << 0);
  275. if (ReportItem->ReportID)
  276. {
  277. ReportData[0] = ReportItem->ReportID;
  278. ReportData++;
  279. }
  280. ReportItem->PreviousValue = ReportItem->Value;
  281. while (DataBitsRem--)
  282. {
  283. if (ReportItem->Value & (1 << (CurrentBit % 8)))
  284. ReportData[CurrentBit / 8] |= BitMask;
  285. CurrentBit++;
  286. BitMask <<= 1;
  287. }
  288. }
  289. uint16_t USB_GetHIDReportSize(HID_ReportInfo_t* const ParserData,
  290. const uint8_t ReportID,
  291. const uint8_t ReportType)
  292. {
  293. for (uint8_t i = 0; i < HID_MAX_REPORT_IDS; i++)
  294. {
  295. uint16_t ReportSizeBits = ParserData->ReportIDSizes[i].ReportSizeBits[ReportType];
  296. if (ParserData->ReportIDSizes[i].ReportID == ReportID)
  297. return (ReportSizeBits / 8) + ((ReportSizeBits % 8) ? 1 : 0);
  298. }
  299. return 0;
  300. }