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.

HIDClassDevice.c 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. #include "../../Core/USBMode.h"
  28. #if defined(USB_CAN_BE_DEVICE)
  29. #define __INCLUDE_FROM_HID_DRIVER
  30. #define __INCLUDE_FROM_HID_DEVICE_C
  31. #include "HIDClassDevice.h"
  32. void HID_Device_ProcessControlRequest(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo)
  33. {
  34. if (!(Endpoint_IsSETUPReceived()))
  35. return;
  36. if (USB_ControlRequest.wIndex != HIDInterfaceInfo->Config.InterfaceNumber)
  37. return;
  38. switch (USB_ControlRequest.bRequest)
  39. {
  40. case HID_REQ_GetReport:
  41. if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
  42. {
  43. uint16_t ReportSize = 0;
  44. uint8_t ReportID = (USB_ControlRequest.wValue & 0xFF);
  45. uint8_t ReportType = (USB_ControlRequest.wValue >> 8) - 1;
  46. uint8_t ReportData[HIDInterfaceInfo->Config.PrevReportINBufferSize];
  47. memset(ReportData, 0, sizeof(ReportData));
  48. CALLBACK_HID_Device_CreateHIDReport(HIDInterfaceInfo, &ReportID, ReportType, ReportData, &ReportSize);
  49. if (HIDInterfaceInfo->Config.PrevReportINBuffer != NULL)
  50. {
  51. memcpy(HIDInterfaceInfo->Config.PrevReportINBuffer, ReportData,
  52. HIDInterfaceInfo->Config.PrevReportINBufferSize);
  53. }
  54. Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP);
  55. Endpoint_ClearSETUP();
  56. if (ReportID)
  57. Endpoint_Write_8(ReportID);
  58. Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
  59. Endpoint_ClearOUT();
  60. }
  61. break;
  62. case HID_REQ_SetReport:
  63. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
  64. {
  65. uint16_t ReportSize = USB_ControlRequest.wLength;
  66. uint8_t ReportID = (USB_ControlRequest.wValue & 0xFF);
  67. uint8_t ReportType = (USB_ControlRequest.wValue >> 8) - 1;
  68. uint8_t ReportData[ReportSize];
  69. Endpoint_ClearSETUP();
  70. Endpoint_Read_Control_Stream_LE(ReportData, ReportSize);
  71. Endpoint_ClearIN();
  72. CALLBACK_HID_Device_ProcessHIDReport(HIDInterfaceInfo, ReportID, ReportType,
  73. &ReportData[ReportID ? 1 : 0], ReportSize - (ReportID ? 1 : 0));
  74. }
  75. break;
  76. case HID_REQ_GetProtocol:
  77. if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
  78. {
  79. Endpoint_ClearSETUP();
  80. while (!(Endpoint_IsINReady()));
  81. Endpoint_Write_8(HIDInterfaceInfo->State.UsingReportProtocol);
  82. Endpoint_ClearIN();
  83. Endpoint_ClearStatusStage();
  84. }
  85. break;
  86. case HID_REQ_SetProtocol:
  87. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
  88. {
  89. Endpoint_ClearSETUP();
  90. Endpoint_ClearStatusStage();
  91. HIDInterfaceInfo->State.UsingReportProtocol = ((USB_ControlRequest.wValue & 0xFF) != 0x00);
  92. }
  93. break;
  94. case HID_REQ_SetIdle:
  95. if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
  96. {
  97. Endpoint_ClearSETUP();
  98. Endpoint_ClearStatusStage();
  99. HIDInterfaceInfo->State.IdleCount = ((USB_ControlRequest.wValue & 0xFF00) >> 6);
  100. }
  101. break;
  102. case HID_REQ_GetIdle:
  103. if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
  104. {
  105. Endpoint_ClearSETUP();
  106. while (!(Endpoint_IsINReady()));
  107. Endpoint_Write_8(HIDInterfaceInfo->State.IdleCount >> 2);
  108. Endpoint_ClearIN();
  109. Endpoint_ClearStatusStage();
  110. }
  111. break;
  112. }
  113. }
  114. bool HID_Device_ConfigureEndpoints(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo)
  115. {
  116. memset(&HIDInterfaceInfo->State, 0x00, sizeof(HIDInterfaceInfo->State));
  117. HIDInterfaceInfo->State.UsingReportProtocol = true;
  118. HIDInterfaceInfo->State.IdleCount = 500;
  119. HIDInterfaceInfo->Config.ReportINEndpoint.Type = EP_TYPE_INTERRUPT;
  120. if (!(Endpoint_ConfigureEndpointTable(&HIDInterfaceInfo->Config.ReportINEndpoint, 1)))
  121. return false;
  122. return true;
  123. }
  124. void HID_Device_USBTask(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo)
  125. {
  126. if (USB_DeviceState != DEVICE_STATE_Configured)
  127. return;
  128. if (HIDInterfaceInfo->State.PrevFrameNum == USB_Device_GetFrameNumber())
  129. {
  130. #if defined(USB_DEVICE_OPT_LOWSPEED)
  131. if (!(USB_Options & USB_DEVICE_OPT_LOWSPEED))
  132. return;
  133. #else
  134. return;
  135. #endif
  136. }
  137. Endpoint_SelectEndpoint(HIDInterfaceInfo->Config.ReportINEndpoint.Address);
  138. if (Endpoint_IsReadWriteAllowed())
  139. {
  140. uint8_t ReportINData[HIDInterfaceInfo->Config.PrevReportINBufferSize];
  141. uint8_t ReportID = 0;
  142. uint16_t ReportINSize = 0;
  143. memset(ReportINData, 0, sizeof(ReportINData));
  144. bool ForceSend = CALLBACK_HID_Device_CreateHIDReport(HIDInterfaceInfo, &ReportID, HID_REPORT_ITEM_In,
  145. ReportINData, &ReportINSize);
  146. bool StatesChanged = false;
  147. bool IdlePeriodElapsed = (HIDInterfaceInfo->State.IdleCount && !(HIDInterfaceInfo->State.IdleMSRemaining));
  148. if (HIDInterfaceInfo->Config.PrevReportINBuffer != NULL)
  149. {
  150. StatesChanged = (memcmp(ReportINData, HIDInterfaceInfo->Config.PrevReportINBuffer, ReportINSize) != 0);
  151. memcpy(HIDInterfaceInfo->Config.PrevReportINBuffer, ReportINData, HIDInterfaceInfo->Config.PrevReportINBufferSize);
  152. }
  153. if (ReportINSize && (ForceSend || StatesChanged || IdlePeriodElapsed))
  154. {
  155. HIDInterfaceInfo->State.IdleMSRemaining = HIDInterfaceInfo->State.IdleCount;
  156. Endpoint_SelectEndpoint(HIDInterfaceInfo->Config.ReportINEndpoint.Address);
  157. if (ReportID)
  158. Endpoint_Write_8(ReportID);
  159. Endpoint_Write_Stream_LE(ReportINData, ReportINSize, NULL);
  160. Endpoint_ClearIN();
  161. }
  162. HIDInterfaceInfo->State.PrevFrameNum = USB_Device_GetFrameNumber();
  163. }
  164. }
  165. #endif