HIDClassHost.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2012.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2012 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 disclaim 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. #include "../../Core/USBMode.h"
  28. #if defined(USB_CAN_BE_HOST)
  29. #define __INCLUDE_FROM_HID_DRIVER
  30. #define __INCLUDE_FROM_HID_HOST_C
  31. #include "HIDClassHost.h"
  32. uint8_t HID_Host_ConfigurePipes(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo,
  33. uint16_t ConfigDescriptorSize,
  34. void* ConfigDescriptorData)
  35. {
  36. USB_Descriptor_Endpoint_t* DataINEndpoint = NULL;
  37. USB_Descriptor_Endpoint_t* DataOUTEndpoint = NULL;
  38. USB_Descriptor_Interface_t* HIDInterface = NULL;
  39. USB_HID_Descriptor_HID_t* HIDDescriptor = NULL;
  40. memset(&HIDInterfaceInfo->State, 0x00, sizeof(HIDInterfaceInfo->State));
  41. if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)
  42. return HID_ENUMERROR_InvalidConfigDescriptor;
  43. while (!(DataINEndpoint) || !(DataOUTEndpoint))
  44. {
  45. if (!(HIDInterface) ||
  46. USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
  47. DCOMP_HID_Host_NextHIDInterfaceEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
  48. {
  49. if (DataINEndpoint || DataOUTEndpoint)
  50. break;
  51. do
  52. {
  53. if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
  54. DCOMP_HID_Host_NextHIDInterface) != DESCRIPTOR_SEARCH_COMP_Found)
  55. {
  56. return HID_ENUMERROR_NoCompatibleInterfaceFound;
  57. }
  58. HIDInterface = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Interface_t);
  59. } while (HIDInterfaceInfo->Config.HIDInterfaceProtocol &&
  60. (HIDInterface->Protocol != HIDInterfaceInfo->Config.HIDInterfaceProtocol));
  61. if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
  62. DCOMP_HID_Host_NextHIDDescriptor) != DESCRIPTOR_SEARCH_COMP_Found)
  63. {
  64. return HID_ENUMERROR_NoCompatibleInterfaceFound;
  65. }
  66. HIDDescriptor = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_HID_Descriptor_HID_t);
  67. DataINEndpoint = NULL;
  68. DataOUTEndpoint = NULL;
  69. continue;
  70. }
  71. USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Endpoint_t);
  72. if ((EndpointData->EndpointAddress & ENDPOINT_DIR_MASK) == ENDPOINT_DIR_IN)
  73. DataINEndpoint = EndpointData;
  74. else
  75. DataOUTEndpoint = EndpointData;
  76. }
  77. HIDInterfaceInfo->Config.DataINPipe.Size = le16_to_cpu(DataINEndpoint->EndpointSize);
  78. HIDInterfaceInfo->Config.DataINPipe.EndpointAddress = DataINEndpoint->EndpointAddress;
  79. HIDInterfaceInfo->Config.DataINPipe.Type = EP_TYPE_INTERRUPT;
  80. HIDInterfaceInfo->Config.DataOUTPipe.Size = le16_to_cpu(DataOUTEndpoint->EndpointSize);
  81. HIDInterfaceInfo->Config.DataOUTPipe.EndpointAddress = DataOUTEndpoint->EndpointAddress;
  82. HIDInterfaceInfo->Config.DataOUTPipe.Type = EP_TYPE_INTERRUPT;
  83. if (!(Pipe_ConfigurePipeTable(&HIDInterfaceInfo->Config.DataINPipe, 1)))
  84. return false;
  85. if (!(Pipe_ConfigurePipeTable(&HIDInterfaceInfo->Config.DataOUTPipe, 1)))
  86. return false;
  87. HIDInterfaceInfo->State.InterfaceNumber = HIDInterface->InterfaceNumber;
  88. HIDInterfaceInfo->State.HIDReportSize = LE16_TO_CPU(HIDDescriptor->HIDReportLength);
  89. HIDInterfaceInfo->State.SupportsBootProtocol = (HIDInterface->SubClass != HID_CSCP_NonBootProtocol);
  90. HIDInterfaceInfo->State.LargestReportSize = 8;
  91. HIDInterfaceInfo->State.IsActive = true;
  92. return HID_ENUMERROR_NoError;
  93. }
  94. static uint8_t DCOMP_HID_Host_NextHIDInterface(void* const CurrentDescriptor)
  95. {
  96. USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
  97. if (Header->Type == DTYPE_Interface)
  98. {
  99. USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
  100. if (Interface->Class == HID_CSCP_HIDClass)
  101. return DESCRIPTOR_SEARCH_Found;
  102. }
  103. return DESCRIPTOR_SEARCH_NotFound;
  104. }
  105. static uint8_t DCOMP_HID_Host_NextHIDDescriptor(void* const CurrentDescriptor)
  106. {
  107. USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
  108. if (Header->Type == HID_DTYPE_HID)
  109. return DESCRIPTOR_SEARCH_Found;
  110. else if (Header->Type == DTYPE_Interface)
  111. return DESCRIPTOR_SEARCH_Fail;
  112. else
  113. return DESCRIPTOR_SEARCH_NotFound;
  114. }
  115. static uint8_t DCOMP_HID_Host_NextHIDInterfaceEndpoint(void* const CurrentDescriptor)
  116. {
  117. USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
  118. if (Header->Type == DTYPE_Endpoint)
  119. {
  120. USB_Descriptor_Endpoint_t* Endpoint = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Endpoint_t);
  121. if (!(Pipe_IsEndpointBound(Endpoint->EndpointAddress)))
  122. return DESCRIPTOR_SEARCH_Found;
  123. }
  124. else if (Header->Type == DTYPE_Interface)
  125. {
  126. return DESCRIPTOR_SEARCH_Fail;
  127. }
  128. return DESCRIPTOR_SEARCH_NotFound;
  129. }
  130. #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
  131. uint8_t HID_Host_ReceiveReportByID(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo,
  132. const uint8_t ReportID,
  133. void* Buffer)
  134. {
  135. USB_ControlRequest = (USB_Request_Header_t)
  136. {
  137. .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
  138. .bRequest = HID_REQ_SetReport,
  139. .wValue = ((HID_REPORT_ITEM_In + 1) << 8) | ReportID,
  140. .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
  141. .wLength = USB_GetHIDReportSize(HIDInterfaceInfo->Config.HIDParserData, ReportID, HID_REPORT_ITEM_In),
  142. };
  143. Pipe_SelectPipe(PIPE_CONTROLPIPE);
  144. return USB_Host_SendControlRequest(Buffer);
  145. }
  146. #endif
  147. uint8_t HID_Host_ReceiveReport(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo,
  148. void* Buffer)
  149. {
  150. if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.IsActive))
  151. return PIPE_READYWAIT_DeviceDisconnected;
  152. uint8_t ErrorCode;
  153. Pipe_SelectPipe(HIDInterfaceInfo->Config.DataINPipe.Address);
  154. Pipe_Unfreeze();
  155. uint16_t ReportSize;
  156. uint8_t* BufferPos = Buffer;
  157. #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
  158. if (!(HIDInterfaceInfo->State.UsingBootProtocol))
  159. {
  160. uint8_t ReportID = 0;
  161. if (HIDInterfaceInfo->Config.HIDParserData->UsingReportIDs)
  162. {
  163. ReportID = Pipe_Read_8();
  164. *(BufferPos++) = ReportID;
  165. }
  166. ReportSize = USB_GetHIDReportSize(HIDInterfaceInfo->Config.HIDParserData, ReportID, HID_REPORT_ITEM_In);
  167. }
  168. else
  169. #endif
  170. {
  171. ReportSize = Pipe_BytesInPipe();
  172. }
  173. if ((ErrorCode = Pipe_Read_Stream_LE(BufferPos, ReportSize, NULL)) != PIPE_RWSTREAM_NoError)
  174. return ErrorCode;
  175. Pipe_ClearIN();
  176. Pipe_Freeze();
  177. return PIPE_RWSTREAM_NoError;
  178. }
  179. uint8_t HID_Host_SendReportByID(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo,
  180. #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
  181. const uint8_t ReportID,
  182. #endif
  183. const uint8_t ReportType,
  184. void* Buffer,
  185. const uint16_t ReportSize)
  186. {
  187. #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
  188. if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.IsActive))
  189. return false;
  190. if (HIDInterfaceInfo->State.DeviceUsesOUTPipe && (ReportType == HID_REPORT_ITEM_Out))
  191. {
  192. uint8_t ErrorCode;
  193. Pipe_SelectPipe(HIDInterfaceInfo->Config.DataOUTPipe.Address);
  194. Pipe_Unfreeze();
  195. if (ReportID)
  196. Pipe_Write_Stream_LE(&ReportID, sizeof(ReportID), NULL);
  197. if ((ErrorCode = Pipe_Write_Stream_LE(Buffer, ReportSize, NULL)) != PIPE_RWSTREAM_NoError)
  198. return ErrorCode;
  199. Pipe_ClearOUT();
  200. Pipe_Freeze();
  201. return PIPE_RWSTREAM_NoError;
  202. }
  203. else
  204. #endif
  205. {
  206. USB_ControlRequest = (USB_Request_Header_t)
  207. {
  208. .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
  209. .bRequest = HID_REQ_SetReport,
  210. #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
  211. .wValue = ((ReportType + 1) << 8) | ReportID,
  212. #else
  213. .wValue = ((ReportType + 1) << 8),
  214. #endif
  215. .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
  216. .wLength = ReportSize,
  217. };
  218. Pipe_SelectPipe(PIPE_CONTROLPIPE);
  219. return USB_Host_SendControlRequest(Buffer);
  220. }
  221. }
  222. bool HID_Host_IsReportReceived(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo)
  223. {
  224. if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.IsActive))
  225. return false;
  226. bool ReportReceived;
  227. Pipe_SelectPipe(HIDInterfaceInfo->Config.DataINPipe.Address);
  228. Pipe_Unfreeze();
  229. ReportReceived = Pipe_IsINReceived();
  230. Pipe_Freeze();
  231. return ReportReceived;
  232. }
  233. uint8_t HID_Host_SetBootProtocol(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo)
  234. {
  235. uint8_t ErrorCode;
  236. if (!(HIDInterfaceInfo->State.SupportsBootProtocol))
  237. return HID_ERROR_LOGICAL;
  238. USB_ControlRequest = (USB_Request_Header_t)
  239. {
  240. .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
  241. .bRequest = HID_REQ_SetProtocol,
  242. .wValue = 0,
  243. .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
  244. .wLength = 0,
  245. };
  246. Pipe_SelectPipe(PIPE_CONTROLPIPE);
  247. if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
  248. return ErrorCode;
  249. HIDInterfaceInfo->State.LargestReportSize = 8;
  250. HIDInterfaceInfo->State.UsingBootProtocol = true;
  251. return HOST_SENDCONTROL_Successful;
  252. }
  253. uint8_t HID_Host_SetIdlePeriod(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo,
  254. const uint16_t MS)
  255. {
  256. USB_ControlRequest = (USB_Request_Header_t)
  257. {
  258. .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
  259. .bRequest = HID_REQ_SetIdle,
  260. .wValue = ((MS << 6) & 0xFF00),
  261. .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
  262. .wLength = 0,
  263. };
  264. Pipe_SelectPipe(PIPE_CONTROLPIPE);
  265. return USB_Host_SendControlRequest(NULL);
  266. }
  267. #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
  268. uint8_t HID_Host_SetReportProtocol(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo)
  269. {
  270. uint8_t ErrorCode;
  271. uint8_t HIDReportData[HIDInterfaceInfo->State.HIDReportSize];
  272. USB_ControlRequest = (USB_Request_Header_t)
  273. {
  274. .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_INTERFACE),
  275. .bRequest = REQ_GetDescriptor,
  276. .wValue = (HID_DTYPE_Report << 8),
  277. .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
  278. .wLength = HIDInterfaceInfo->State.HIDReportSize,
  279. };
  280. Pipe_SelectPipe(PIPE_CONTROLPIPE);
  281. if ((ErrorCode = USB_Host_SendControlRequest(HIDReportData)) != HOST_SENDCONTROL_Successful)
  282. return ErrorCode;
  283. if (HIDInterfaceInfo->State.UsingBootProtocol)
  284. {
  285. USB_ControlRequest = (USB_Request_Header_t)
  286. {
  287. .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
  288. .bRequest = HID_REQ_SetProtocol,
  289. .wValue = 1,
  290. .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
  291. .wLength = 0,
  292. };
  293. if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
  294. return ErrorCode;
  295. HIDInterfaceInfo->State.UsingBootProtocol = false;
  296. }
  297. if (HIDInterfaceInfo->Config.HIDParserData == NULL)
  298. return HID_ERROR_LOGICAL;
  299. if ((ErrorCode = USB_ProcessHIDReport(HIDReportData, HIDInterfaceInfo->State.HIDReportSize,
  300. HIDInterfaceInfo->Config.HIDParserData)) != HID_PARSE_Successful)
  301. {
  302. return HID_ERROR_LOGICAL | ErrorCode;
  303. }
  304. uint16_t LargestReportSizeBits = HIDInterfaceInfo->Config.HIDParserData->LargestReportSizeBits;
  305. HIDInterfaceInfo->State.LargestReportSize = (LargestReportSizeBits >> 3) + ((LargestReportSizeBits & 0x07) != 0);
  306. return 0;
  307. }
  308. #endif
  309. #endif