]> git.donarmstrong.com Git - qmk_firmware.git/blob - protocol/lufa/LUFA-120730/LUFA/Drivers/USB/Class/Device/HIDClassDevice.c
Squashed 'tmk_core/' content from commit 05caacc
[qmk_firmware.git] / protocol / lufa / LUFA-120730 / LUFA / Drivers / USB / Class / Device / HIDClassDevice.c
1 /*\r
2              LUFA Library\r
3      Copyright (C) Dean Camera, 2012.\r
4 \r
5   dean [at] fourwalledcubicle [dot] com\r
6            www.lufa-lib.org\r
7 */\r
8 \r
9 /*\r
10   Copyright 2012  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
11 \r
12   Permission to use, copy, modify, distribute, and sell this\r
13   software and its documentation for any purpose is hereby granted\r
14   without fee, provided that the above copyright notice appear in\r
15   all copies and that both that the copyright notice and this\r
16   permission notice and warranty disclaimer appear in supporting\r
17   documentation, and that the name of the author not be used in\r
18   advertising or publicity pertaining to distribution of the\r
19   software without specific, written prior permission.\r
20 \r
21   The author disclaim all warranties with regard to this\r
22   software, including all implied warranties of merchantability\r
23   and fitness.  In no event shall the author be liable for any\r
24   special, indirect or consequential damages or any damages\r
25   whatsoever resulting from loss of use, data or profits, whether\r
26   in an action of contract, negligence or other tortious action,\r
27   arising out of or in connection with the use or performance of\r
28   this software.\r
29 */\r
30 \r
31 #define  __INCLUDE_FROM_USB_DRIVER\r
32 #include "../../Core/USBMode.h"\r
33 \r
34 #if defined(USB_CAN_BE_DEVICE)\r
35 \r
36 #define  __INCLUDE_FROM_HID_DRIVER\r
37 #define  __INCLUDE_FROM_HID_DEVICE_C\r
38 #include "HIDClassDevice.h"\r
39 \r
40 void HID_Device_ProcessControlRequest(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo)\r
41 {\r
42         if (!(Endpoint_IsSETUPReceived()))\r
43           return;\r
44 \r
45         if (USB_ControlRequest.wIndex != HIDInterfaceInfo->Config.InterfaceNumber)\r
46           return;\r
47 \r
48         switch (USB_ControlRequest.bRequest)\r
49         {\r
50                 case HID_REQ_GetReport:\r
51                         if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))\r
52                         {\r
53                                 uint16_t ReportSize = 0;\r
54                                 uint8_t  ReportID   = (USB_ControlRequest.wValue & 0xFF);\r
55                                 uint8_t  ReportType = (USB_ControlRequest.wValue >> 8) - 1;\r
56                                 uint8_t  ReportData[HIDInterfaceInfo->Config.PrevReportINBufferSize];\r
57 \r
58                                 memset(ReportData, 0, sizeof(ReportData));\r
59 \r
60                                 CALLBACK_HID_Device_CreateHIDReport(HIDInterfaceInfo, &ReportID, ReportType, ReportData, &ReportSize);\r
61 \r
62                                 if (HIDInterfaceInfo->Config.PrevReportINBuffer != NULL)\r
63                                 {\r
64                                         memcpy(HIDInterfaceInfo->Config.PrevReportINBuffer, ReportData,\r
65                                                HIDInterfaceInfo->Config.PrevReportINBufferSize);\r
66                                 }\r
67 \r
68                                 Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP);\r
69 \r
70                                 Endpoint_ClearSETUP();\r
71                                 Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);\r
72                                 Endpoint_ClearOUT();\r
73                         }\r
74 \r
75                         break;\r
76                 case HID_REQ_SetReport:\r
77                         if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))\r
78                         {\r
79                                 uint16_t ReportSize = USB_ControlRequest.wLength;\r
80                                 uint8_t  ReportID   = (USB_ControlRequest.wValue & 0xFF);\r
81                                 uint8_t  ReportType = (USB_ControlRequest.wValue >> 8) - 1;\r
82                                 uint8_t  ReportData[ReportSize];\r
83 \r
84                                 Endpoint_ClearSETUP();\r
85                                 Endpoint_Read_Control_Stream_LE(ReportData, ReportSize);\r
86                                 Endpoint_ClearIN();\r
87 \r
88                                 CALLBACK_HID_Device_ProcessHIDReport(HIDInterfaceInfo, ReportID, ReportType,\r
89                                                                      &ReportData[ReportID ? 1 : 0], ReportSize - (ReportID ? 1 : 0));\r
90                         }\r
91 \r
92                         break;\r
93                 case HID_REQ_GetProtocol:\r
94                         if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))\r
95                         {\r
96                                 Endpoint_ClearSETUP();\r
97                                 while (!(Endpoint_IsINReady()));\r
98                                 Endpoint_Write_8(HIDInterfaceInfo->State.UsingReportProtocol);\r
99                                 Endpoint_ClearIN();\r
100                                 Endpoint_ClearStatusStage();\r
101                         }\r
102 \r
103                         break;\r
104                 case HID_REQ_SetProtocol:\r
105                         if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))\r
106                         {\r
107                                 Endpoint_ClearSETUP();\r
108                                 Endpoint_ClearStatusStage();\r
109 \r
110                                 HIDInterfaceInfo->State.UsingReportProtocol = ((USB_ControlRequest.wValue & 0xFF) != 0x00);\r
111                         }\r
112 \r
113                         break;\r
114                 case HID_REQ_SetIdle:\r
115                         if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))\r
116                         {\r
117                                 Endpoint_ClearSETUP();\r
118                                 Endpoint_ClearStatusStage();\r
119 \r
120                                 HIDInterfaceInfo->State.IdleCount = ((USB_ControlRequest.wValue & 0xFF00) >> 6);\r
121                         }\r
122 \r
123                         break;\r
124                 case HID_REQ_GetIdle:\r
125                         if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))\r
126                         {\r
127                                 Endpoint_ClearSETUP();\r
128                                 while (!(Endpoint_IsINReady()));\r
129                                 Endpoint_Write_8(HIDInterfaceInfo->State.IdleCount >> 2);\r
130                                 Endpoint_ClearIN();\r
131                                 Endpoint_ClearStatusStage();\r
132                         }\r
133 \r
134                         break;\r
135         }\r
136 }\r
137 \r
138 bool HID_Device_ConfigureEndpoints(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo)\r
139 {\r
140         memset(&HIDInterfaceInfo->State, 0x00, sizeof(HIDInterfaceInfo->State));\r
141         HIDInterfaceInfo->State.UsingReportProtocol = true;\r
142         HIDInterfaceInfo->State.IdleCount           = 500;\r
143 \r
144         HIDInterfaceInfo->Config.ReportINEndpoint.Type = EP_TYPE_INTERRUPT;\r
145 \r
146         if (!(Endpoint_ConfigureEndpointTable(&HIDInterfaceInfo->Config.ReportINEndpoint, 1)))\r
147           return false; \r
148         \r
149         return true;\r
150 }\r
151 \r
152 void HID_Device_USBTask(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo)\r
153 {\r
154         if (USB_DeviceState != DEVICE_STATE_Configured)\r
155           return;\r
156 \r
157         if (HIDInterfaceInfo->State.PrevFrameNum == USB_Device_GetFrameNumber())\r
158           return;\r
159           \r
160         Endpoint_SelectEndpoint(HIDInterfaceInfo->Config.ReportINEndpoint.Address);\r
161 \r
162         if (Endpoint_IsReadWriteAllowed())\r
163         {\r
164                 uint8_t  ReportINData[HIDInterfaceInfo->Config.PrevReportINBufferSize];\r
165                 uint8_t  ReportID     = 0;\r
166                 uint16_t ReportINSize = 0;\r
167 \r
168                 memset(ReportINData, 0, sizeof(ReportINData));\r
169 \r
170                 bool ForceSend         = CALLBACK_HID_Device_CreateHIDReport(HIDInterfaceInfo, &ReportID, HID_REPORT_ITEM_In,\r
171                                                                              ReportINData, &ReportINSize);\r
172                 bool StatesChanged     = false;\r
173                 bool IdlePeriodElapsed = (HIDInterfaceInfo->State.IdleCount && !(HIDInterfaceInfo->State.IdleMSRemaining));\r
174 \r
175                 if (HIDInterfaceInfo->Config.PrevReportINBuffer != NULL)\r
176                 {\r
177                         StatesChanged = (memcmp(ReportINData, HIDInterfaceInfo->Config.PrevReportINBuffer, ReportINSize) != 0);\r
178                         memcpy(HIDInterfaceInfo->Config.PrevReportINBuffer, ReportINData, HIDInterfaceInfo->Config.PrevReportINBufferSize);\r
179                 }\r
180 \r
181                 if (ReportINSize && (ForceSend || StatesChanged || IdlePeriodElapsed))\r
182                 {\r
183                         HIDInterfaceInfo->State.IdleMSRemaining = HIDInterfaceInfo->State.IdleCount;\r
184 \r
185                         Endpoint_SelectEndpoint(HIDInterfaceInfo->Config.ReportINEndpoint.Address);\r
186 \r
187                         if (ReportID)\r
188                           Endpoint_Write_8(ReportID);\r
189 \r
190                         Endpoint_Write_Stream_LE(ReportINData, ReportINSize, NULL);\r
191 \r
192                         Endpoint_ClearIN();\r
193                 }\r
194                 \r
195                 HIDInterfaceInfo->State.PrevFrameNum = USB_Device_GetFrameNumber();\r
196         }\r
197 }\r
198 \r
199 #endif\r
200 \r