Make HID device class driver ignore the previous HID report comparison buffer when...
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Device / HID.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 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 disclaim 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 #include "../../HighLevel/USBMode.h"
32 #if defined(USB_CAN_BE_DEVICE)
33
34 #include "HID.h"
35
36 void HID_Device_ProcessControlRequest(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo)
37 {
38 if (!(Endpoint_IsSETUPReceived()))
39 return;
40
41 if (USB_ControlRequest.wIndex != HIDInterfaceInfo->Config.InterfaceNumber)
42 return;
43
44 switch (USB_ControlRequest.bRequest)
45 {
46 case REQ_GetReport:
47 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
48 {
49 Endpoint_ClearSETUP();
50
51 uint16_t ReportINSize = 0;
52 uint8_t ReportID = (USB_ControlRequest.wValue & 0xFF);
53
54 memset(HIDInterfaceInfo->Config.PrevReportINBuffer, 0, HIDInterfaceInfo->Config.PrevReportINBufferSize);
55
56 CALLBACK_HID_Device_CreateHIDReport(HIDInterfaceInfo, &ReportID,
57 HIDInterfaceInfo->Config.PrevReportINBuffer, &ReportINSize);
58
59 Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP);
60 Endpoint_Write_Control_Stream_LE(HIDInterfaceInfo->Config.PrevReportINBuffer, ReportINSize);
61 Endpoint_ClearOUT();
62 }
63
64 break;
65 case REQ_SetReport:
66 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
67 {
68 Endpoint_ClearSETUP();
69
70 uint16_t ReportOUTSize = USB_ControlRequest.wLength;
71 uint8_t ReportOUTData[ReportOUTSize];
72 uint8_t ReportID = (USB_ControlRequest.wValue & 0xFF);
73
74 Endpoint_Read_Control_Stream_LE(ReportOUTData, ReportOUTSize);
75 Endpoint_ClearIN();
76
77 CALLBACK_HID_Device_ProcessHIDReport(HIDInterfaceInfo, ReportID, ReportOUTData, ReportOUTSize);
78 }
79
80 break;
81 case REQ_GetProtocol:
82 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
83 {
84 Endpoint_ClearSETUP();
85
86 Endpoint_Write_Byte(HIDInterfaceInfo->State.UsingReportProtocol);
87 Endpoint_ClearIN();
88
89 Endpoint_ClearStatusStage();
90 }
91
92 break;
93 case REQ_SetProtocol:
94 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
95 {
96 Endpoint_ClearSETUP();
97
98 HIDInterfaceInfo->State.UsingReportProtocol = (USB_ControlRequest.wValue != 0x0000);
99
100 Endpoint_ClearStatusStage();
101 }
102
103 break;
104 case REQ_SetIdle:
105 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
106 {
107 Endpoint_ClearSETUP();
108
109 HIDInterfaceInfo->State.IdleCount = ((USB_ControlRequest.wValue & 0xFF00) >> 6);
110
111 Endpoint_ClearStatusStage();
112 }
113
114 break;
115 case REQ_GetIdle:
116 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
117 {
118 Endpoint_ClearSETUP();
119
120 Endpoint_Write_Byte(HIDInterfaceInfo->State.IdleCount >> 2);
121 Endpoint_ClearIN();
122
123 Endpoint_ClearStatusStage();
124 }
125
126 break;
127 }
128 }
129
130 bool HID_Device_ConfigureEndpoints(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo)
131 {
132 memset(&HIDInterfaceInfo->State, 0x00, sizeof(HIDInterfaceInfo->State));
133 HIDInterfaceInfo->State.UsingReportProtocol = true;
134 HIDInterfaceInfo->State.IdleCount = 500;
135
136 if (!(Endpoint_ConfigureEndpoint(HIDInterfaceInfo->Config.ReportINEndpointNumber, EP_TYPE_INTERRUPT,
137 ENDPOINT_DIR_IN, HIDInterfaceInfo->Config.ReportINEndpointSize, ENDPOINT_BANK_SINGLE)))
138 {
139 return false;
140 }
141
142 return true;
143 }
144
145 void HID_Device_USBTask(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo)
146 {
147 if (USB_DeviceState != DEVICE_STATE_Configured)
148 return;
149
150 Endpoint_SelectEndpoint(HIDInterfaceInfo->Config.ReportINEndpointNumber);
151
152 if (Endpoint_IsReadWriteAllowed())
153 {
154 uint8_t ReportINData[HIDInterfaceInfo->Config.PrevReportINBufferSize];
155 uint8_t ReportID = 0;
156 uint16_t ReportINSize = 0;
157
158 memset(ReportINData, 0, sizeof(ReportINData));
159
160 bool ForceSend = CALLBACK_HID_Device_CreateHIDReport(HIDInterfaceInfo, &ReportID, ReportINData, &ReportINSize);
161 bool StatesChanged = false;
162 bool IdlePeriodElapsed = (HIDInterfaceInfo->State.IdleCount && !(HIDInterfaceInfo->State.IdleMSRemaining));
163
164 if (HIDInterfaceInfo->Config.PrevReportINBuffer != NULL)
165 {
166 StatesChanged = (memcmp(ReportINData, HIDInterfaceInfo->Config.PrevReportINBuffer, ReportINSize) != 0);
167 memcpy(HIDInterfaceInfo->Config.PrevReportINBuffer, ReportINData, ReportINSize);
168 }
169
170 if (ReportINSize && (ForceSend || StatesChanged || IdlePeriodElapsed))
171 {
172 HIDInterfaceInfo->State.IdleMSRemaining = HIDInterfaceInfo->State.IdleCount;
173
174 Endpoint_SelectEndpoint(HIDInterfaceInfo->Config.ReportINEndpointNumber);
175
176 if (ReportID)
177 Endpoint_Write_Byte(ReportID);
178
179 Endpoint_Write_Stream_LE(ReportINData, ReportINSize, NO_STREAM_CALLBACK);
180
181 Endpoint_ClearIN();
182 }
183 }
184 }
185
186 void HID_Device_MillisecondElapsed(USB_ClassInfo_HID_Device_t* HIDInterfaceInfo)
187 {
188 if (HIDInterfaceInfo->State.IdleMSRemaining)
189 HIDInterfaceInfo->State.IdleMSRemaining--;
190 }
191
192 #endif