]> git.donarmstrong.com Git - qmk_firmware.git/blob - lib/lufa/LUFA/Drivers/USB/Class/Device/HIDClassDevice.c
Merge commit '60b30c036397cb5627fa374bb930794b225daa29' as 'lib/lufa'
[qmk_firmware.git] / lib / lufa / LUFA / Drivers / USB / Class / Device / HIDClassDevice.c
1 /*
2              LUFA Library
3      Copyright (C) Dean Camera, 2017.
4
5   dean [at] fourwalledcubicle [dot] com
6            www.lufa-lib.org
7 */
8
9 /*
10   Copyright 2017  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 disclaims 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 "../../Core/USBMode.h"
33
34 #if defined(USB_CAN_BE_DEVICE)
35
36 #define  __INCLUDE_FROM_HID_DRIVER
37 #define  __INCLUDE_FROM_HID_DEVICE_C
38 #include "HIDClassDevice.h"
39
40 void HID_Device_ProcessControlRequest(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo)
41 {
42         if (!(Endpoint_IsSETUPReceived()))
43           return;
44
45         if (USB_ControlRequest.wIndex != HIDInterfaceInfo->Config.InterfaceNumber)
46           return;
47
48         switch (USB_ControlRequest.bRequest)
49         {
50                 case HID_REQ_GetReport:
51                         if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
52                         {
53                                 uint16_t ReportSize = 0;
54                                 uint8_t  ReportID   = (USB_ControlRequest.wValue & 0xFF);
55                                 uint8_t  ReportType = (USB_ControlRequest.wValue >> 8) - 1;
56                                 uint8_t  ReportData[HIDInterfaceInfo->Config.PrevReportINBufferSize];
57
58                                 memset(ReportData, 0, sizeof(ReportData));
59
60                                 CALLBACK_HID_Device_CreateHIDReport(HIDInterfaceInfo, &ReportID, ReportType, ReportData, &ReportSize);
61
62                                 if (HIDInterfaceInfo->Config.PrevReportINBuffer != NULL)
63                                 {
64                                         memcpy(HIDInterfaceInfo->Config.PrevReportINBuffer, ReportData,
65                                                HIDInterfaceInfo->Config.PrevReportINBufferSize);
66                                 }
67
68                                 Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP);
69
70                                 Endpoint_ClearSETUP();
71
72                                 if (ReportID)
73                                   Endpoint_Write_8(ReportID);
74
75                                 Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
76                                 Endpoint_ClearOUT();
77                         }
78
79                         break;
80                 case HID_REQ_SetReport:
81                         if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
82                         {
83                                 uint16_t ReportSize = USB_ControlRequest.wLength;
84                                 uint8_t  ReportID   = (USB_ControlRequest.wValue & 0xFF);
85                                 uint8_t  ReportType = (USB_ControlRequest.wValue >> 8) - 1;
86                                 uint8_t  ReportData[ReportSize];
87
88                                 Endpoint_ClearSETUP();
89                                 Endpoint_Read_Control_Stream_LE(ReportData, ReportSize);
90                                 Endpoint_ClearIN();
91
92                                 CALLBACK_HID_Device_ProcessHIDReport(HIDInterfaceInfo, ReportID, ReportType,
93                                                                      &ReportData[ReportID ? 1 : 0], ReportSize - (ReportID ? 1 : 0));
94                         }
95
96                         break;
97                 case HID_REQ_GetProtocol:
98                         if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
99                         {
100                                 Endpoint_ClearSETUP();
101                                 while (!(Endpoint_IsINReady()));
102                                 Endpoint_Write_8(HIDInterfaceInfo->State.UsingReportProtocol);
103                                 Endpoint_ClearIN();
104                                 Endpoint_ClearStatusStage();
105                         }
106
107                         break;
108                 case HID_REQ_SetProtocol:
109                         if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
110                         {
111                                 Endpoint_ClearSETUP();
112                                 Endpoint_ClearStatusStage();
113
114                                 HIDInterfaceInfo->State.UsingReportProtocol = ((USB_ControlRequest.wValue & 0xFF) != 0x00);
115                         }
116
117                         break;
118                 case HID_REQ_SetIdle:
119                         if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
120                         {
121                                 Endpoint_ClearSETUP();
122                                 Endpoint_ClearStatusStage();
123
124                                 HIDInterfaceInfo->State.IdleCount = ((USB_ControlRequest.wValue & 0xFF00) >> 6);
125                         }
126
127                         break;
128                 case HID_REQ_GetIdle:
129                         if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
130                         {
131                                 Endpoint_ClearSETUP();
132                                 while (!(Endpoint_IsINReady()));
133                                 Endpoint_Write_8(HIDInterfaceInfo->State.IdleCount >> 2);
134                                 Endpoint_ClearIN();
135                                 Endpoint_ClearStatusStage();
136                         }
137
138                         break;
139         }
140 }
141
142 bool HID_Device_ConfigureEndpoints(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo)
143 {
144         memset(&HIDInterfaceInfo->State, 0x00, sizeof(HIDInterfaceInfo->State));
145         HIDInterfaceInfo->State.UsingReportProtocol = true;
146         HIDInterfaceInfo->State.IdleCount           = 500;
147
148         HIDInterfaceInfo->Config.ReportINEndpoint.Type = EP_TYPE_INTERRUPT;
149
150         if (!(Endpoint_ConfigureEndpointTable(&HIDInterfaceInfo->Config.ReportINEndpoint, 1)))
151           return false;
152
153         return true;
154 }
155
156 void HID_Device_USBTask(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo)
157 {
158         if (USB_DeviceState != DEVICE_STATE_Configured)
159           return;
160
161         if (HIDInterfaceInfo->State.PrevFrameNum == USB_Device_GetFrameNumber())
162         {
163                 #if defined(USB_DEVICE_OPT_LOWSPEED)
164                 if (!(USB_Options & USB_DEVICE_OPT_LOWSPEED))
165                   return;
166                 #else
167                 return;
168                 #endif
169         }
170
171         Endpoint_SelectEndpoint(HIDInterfaceInfo->Config.ReportINEndpoint.Address);
172
173         if (Endpoint_IsReadWriteAllowed())
174         {
175                 uint8_t  ReportINData[HIDInterfaceInfo->Config.PrevReportINBufferSize];
176                 uint8_t  ReportID     = 0;
177                 uint16_t ReportINSize = 0;
178
179                 memset(ReportINData, 0, sizeof(ReportINData));
180
181                 bool ForceSend         = CALLBACK_HID_Device_CreateHIDReport(HIDInterfaceInfo, &ReportID, HID_REPORT_ITEM_In,
182                                                                              ReportINData, &ReportINSize);
183                 bool StatesChanged     = false;
184                 bool IdlePeriodElapsed = (HIDInterfaceInfo->State.IdleCount && !(HIDInterfaceInfo->State.IdleMSRemaining));
185
186                 if (HIDInterfaceInfo->Config.PrevReportINBuffer != NULL)
187                 {
188                         StatesChanged = (memcmp(ReportINData, HIDInterfaceInfo->Config.PrevReportINBuffer, ReportINSize) != 0);
189                         memcpy(HIDInterfaceInfo->Config.PrevReportINBuffer, ReportINData, HIDInterfaceInfo->Config.PrevReportINBufferSize);
190                 }
191
192                 if (ReportINSize && (ForceSend || StatesChanged || IdlePeriodElapsed))
193                 {
194                         HIDInterfaceInfo->State.IdleMSRemaining = HIDInterfaceInfo->State.IdleCount;
195
196                         Endpoint_SelectEndpoint(HIDInterfaceInfo->Config.ReportINEndpoint.Address);
197
198                         if (ReportID)
199                           Endpoint_Write_8(ReportID);
200
201                         Endpoint_Write_Stream_LE(ReportINData, ReportINSize, NULL);
202
203                         Endpoint_ClearIN();
204                 }
205
206                 HIDInterfaceInfo->State.PrevFrameNum = USB_Device_GetFrameNumber();
207         }
208 }
209
210 #endif
211