4c13436e073b72f4acf92b32b60be8a3a86c7afa
[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_ProcessControlPacket(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo)
37 {
38 if (!(Endpoint_IsSETUPReceived()))
39 return;
40
41 if ((USB_ControlRequest.wIndex != HIDInterfaceInfo->Config.InterfaceNumber) &&
42 (USB_ControlRequest.bRequest != REQ_SetIdle))
43 {
44 return;
45 }
46
47 switch (USB_ControlRequest.bRequest)
48 {
49 case REQ_GetReport:
50 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
51 {
52 Endpoint_ClearSETUP();
53
54 uint8_t ReportINData[HID_MAX_REPORT_SIZE];
55 uint16_t ReportINSize;
56 uint8_t ReportID = (USB_ControlRequest.wValue & 0xFF);
57
58 memset(ReportINData, 0, sizeof(ReportINData));
59
60 ReportINSize = CALLBACK_HID_Device_CreateHIDReport(HIDInterfaceInfo, &ReportID, ReportINData);
61
62 Endpoint_Write_Control_Stream_LE(ReportINData, ReportINSize);
63 Endpoint_ClearOUT();
64 }
65
66 break;
67 case REQ_SetReport:
68 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
69 {
70 Endpoint_ClearSETUP();
71
72 uint16_t ReportOUTSize = USB_ControlRequest.wLength;
73 uint8_t ReportOUTData[ReportOUTSize];
74 uint8_t ReportID = (USB_ControlRequest.wValue & 0xFF);
75
76 Endpoint_Read_Control_Stream_LE(ReportOUTData, ReportOUTSize);
77 Endpoint_ClearIN();
78
79 CALLBACK_HID_Device_ProcessHIDReport(HIDInterfaceInfo, ReportID, ReportOUTData, ReportOUTSize);
80 }
81
82 break;
83 case REQ_GetProtocol:
84 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
85 {
86 Endpoint_ClearSETUP();
87
88 Endpoint_Write_Byte(HIDInterfaceInfo->State.UsingReportProtocol);
89 Endpoint_ClearIN();
90
91 Endpoint_ClearStatusStage();
92 }
93
94 break;
95 case REQ_SetProtocol:
96 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
97 {
98 Endpoint_ClearSETUP();
99
100 HIDInterfaceInfo->State.UsingReportProtocol = (USB_ControlRequest.wValue != 0x0000);
101
102 Endpoint_ClearStatusStage();
103 }
104
105 break;
106 case REQ_SetIdle:
107 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
108 {
109 if ((USB_ControlRequest.wIndex == HIDInterfaceInfo->Config.InterfaceNumber) ||
110 (USB_ControlRequest.wValue & 0xFF) == 0)
111 {
112 Endpoint_ClearSETUP();
113
114 HIDInterfaceInfo->State.IdleCount = ((USB_ControlRequest.wValue & 0xFF00) >> 6);
115
116 Endpoint_ClearStatusStage();
117 }
118 }
119
120 break;
121 case REQ_GetIdle:
122 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
123 {
124 Endpoint_ClearSETUP();
125
126 Endpoint_Write_Byte(HIDInterfaceInfo->State.IdleCount >> 2);
127 Endpoint_ClearIN();
128
129 Endpoint_ClearStatusStage();
130 }
131
132 break;
133 }
134 }
135
136 bool HID_Device_ConfigureEndpoints(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo)
137 {
138 memset(&HIDInterfaceInfo->State, 0x00, sizeof(HIDInterfaceInfo->State));
139 HIDInterfaceInfo->State.IdleCount = 500;
140 HIDInterfaceInfo->State.UsingReportProtocol = true;
141
142 if (!(Endpoint_ConfigureEndpoint(HIDInterfaceInfo->Config.ReportINEndpointNumber, EP_TYPE_INTERRUPT,
143 ENDPOINT_DIR_IN, HIDInterfaceInfo->Config.ReportINEndpointSize, ENDPOINT_BANK_SINGLE)))
144 {
145 return false;
146 }
147
148 return true;
149 }
150
151 void HID_Device_USBTask(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo)
152 {
153 static uint8_t PreviousReportINData[HID_MAX_REPORT_SIZE];
154
155 if (USB_DeviceState != DEVICE_STATE_Configured)
156 return;
157
158 Endpoint_SelectEndpoint(HIDInterfaceInfo->Config.ReportINEndpointNumber);
159
160 if (Endpoint_IsReadWriteAllowed())
161 {
162 uint8_t ReportINData[HID_MAX_REPORT_SIZE];
163 uint8_t ReportID = 0;
164 uint16_t ReportINSize;
165
166 memset(ReportINData, 0, sizeof(ReportINData));
167
168 ReportINSize = CALLBACK_HID_Device_CreateHIDReport(HIDInterfaceInfo, &ReportID, ReportINData);
169
170 bool StatesChanged = (memcmp(ReportINData, PreviousReportINData, ReportINSize) != 0);
171 bool IdlePeriodElapsed = (HIDInterfaceInfo->State.IdleCount && !(HIDInterfaceInfo->State.IdleMSRemaining));
172
173 memcpy(PreviousReportINData, ReportINData, ReportINSize);
174
175 if (ReportINSize && (StatesChanged || IdlePeriodElapsed))
176 {
177 HIDInterfaceInfo->State.IdleMSRemaining = HIDInterfaceInfo->State.IdleCount;
178
179 if (ReportID)
180 Endpoint_Write_Stream_LE(&ReportID, sizeof(ReportID), NO_STREAM_CALLBACK);
181
182 Endpoint_Write_Stream_LE(ReportINData, ReportINSize, NO_STREAM_CALLBACK);
183 }
184
185 Endpoint_ClearIN();
186 }
187 }
188
189 #endif