Fixed EVENT_USB_CDC_ControLineStateChanged() event not taking the CDC interface struc...
[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 "HID.h"
32
33 void USB_HID_ProcessControlPacket(USB_ClassInfo_HID_t* HIDInterfaceInfo)
34 {
35 if (!(Endpoint_IsSETUPReceived()))
36 return;
37
38 if (USB_ControlRequest.wIndex != HIDInterfaceInfo->InterfaceNumber)
39 return;
40
41 switch (USB_ControlRequest.bRequest)
42 {
43 case REQ_GetReport:
44 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
45 {
46 Endpoint_ClearSETUP();
47
48 uint8_t ReportINData[HIDInterfaceInfo->ReportINBufferSize];
49 uint16_t ReportINSize;
50
51 memset(ReportINData, 0, sizeof(ReportINData));
52
53 ReportINSize = CALLBACK_USB_HID_CreateNextHIDReport(HIDInterfaceInfo, ReportINData);
54
55 Endpoint_Write_Control_Stream_LE(ReportINData, ReportINSize);
56 Endpoint_ClearOUT();
57 }
58
59 break;
60 case REQ_SetReport:
61 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
62 {
63 Endpoint_ClearSETUP();
64
65 uint16_t ReportOUTSize = USB_ControlRequest.wLength;
66 uint8_t ReportOUTData[ReportOUTSize];
67
68 Endpoint_Read_Control_Stream_LE(ReportOUTData, ReportOUTSize);
69 Endpoint_ClearIN();
70
71 CALLBACK_USB_HID_ProcessReceivedHIDReport(HIDInterfaceInfo, ReportOUTData, ReportOUTSize);
72 }
73
74 break;
75 case REQ_GetProtocol:
76 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
77 {
78 Endpoint_ClearSETUP();
79
80 Endpoint_Write_Byte(HIDInterfaceInfo->UsingReportProtocol);
81 Endpoint_ClearIN();
82
83 while (!(Endpoint_IsOUTReceived()));
84 Endpoint_ClearOUT();
85 }
86
87 break;
88 case REQ_SetProtocol:
89 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
90 {
91 Endpoint_ClearSETUP();
92
93 HIDInterfaceInfo->UsingReportProtocol = (USB_ControlRequest.wValue != 0x0000);
94
95 while (!(Endpoint_IsINReady()));
96 Endpoint_ClearIN();
97 }
98
99 break;
100 case REQ_SetIdle:
101 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
102 {
103 Endpoint_ClearSETUP();
104
105 HIDInterfaceInfo->IdleCount = ((USB_ControlRequest.wValue >> 8) << 2);
106
107 while (!(Endpoint_IsINReady()));
108 Endpoint_ClearIN();
109 }
110
111 break;
112 case REQ_GetIdle:
113 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
114 {
115 Endpoint_ClearSETUP();
116
117 Endpoint_Write_Byte(HIDInterfaceInfo->IdleCount >> 2);
118 Endpoint_ClearIN();
119
120 while (!(Endpoint_IsOUTReceived()));
121 Endpoint_ClearOUT();
122 }
123
124 break;
125 }
126 }
127
128 bool USB_HID_ConfigureEndpoints(USB_ClassInfo_HID_t* HIDInterfaceInfo)
129 {
130 HIDInterfaceInfo->UsingReportProtocol = true;
131
132 if (!(Endpoint_ConfigureEndpoint(HIDInterfaceInfo->ReportINEndpointNumber, EP_TYPE_INTERRUPT,
133 ENDPOINT_DIR_IN, HIDInterfaceInfo->ReportINEndpointSize, ENDPOINT_BANK_SINGLE)))
134 {
135 return false;
136 }
137
138 if (HIDInterfaceInfo->ReportOUTEndpointNumber)
139 {
140 if (!(Endpoint_ConfigureEndpoint(HIDInterfaceInfo->ReportOUTEndpointNumber, EP_TYPE_INTERRUPT,
141 ENDPOINT_DIR_OUT, HIDInterfaceInfo->ReportOUTEndpointSize, ENDPOINT_BANK_SINGLE)))
142 {
143 return false;
144 }
145 }
146
147 return true;
148 }
149
150 void USB_HID_USBTask(USB_ClassInfo_HID_t* HIDInterfaceInfo)
151 {
152 if (!(USB_IsConnected))
153 return;
154
155 Endpoint_SelectEndpoint(HIDInterfaceInfo->ReportINEndpointNumber);
156
157 if (Endpoint_IsReadWriteAllowed() &&
158 !(HIDInterfaceInfo->IdleCount && HIDInterfaceInfo->IdleMSRemaining))
159 {
160 if (HIDInterfaceInfo->IdleCount && !(HIDInterfaceInfo->IdleMSRemaining))
161 HIDInterfaceInfo->IdleMSRemaining = HIDInterfaceInfo->IdleCount;
162
163 uint8_t ReportINData[HIDInterfaceInfo->ReportINBufferSize];
164 uint16_t ReportINSize;
165
166 memset(ReportINData, 0, sizeof(ReportINData));
167
168 ReportINSize = CALLBACK_USB_HID_CreateNextHIDReport(HIDInterfaceInfo, ReportINData);
169
170 if (ReportINSize)
171 Endpoint_Write_Stream_LE(ReportINData, ReportINSize, NO_STREAM_CALLBACK);
172
173 Endpoint_ClearIN();
174 }
175
176 if (HIDInterfaceInfo->ReportOUTEndpointNumber)
177 {
178 Endpoint_SelectEndpoint(HIDInterfaceInfo->ReportOUTEndpointNumber);
179
180 if (Endpoint_IsOUTReceived())
181 {
182 uint16_t ReportOUTSize = Endpoint_BytesInEndpoint();
183 uint8_t ReportOUTData[ReportOUTSize];
184
185 if (ReportOUTSize)
186 Endpoint_Read_Stream_LE(ReportOUTData, ReportOUTSize, NO_STREAM_CALLBACK);
187
188 CALLBACK_USB_HID_ProcessReceivedHIDReport(HIDInterfaceInfo, ReportOUTData, ReportOUTSize);
189
190 Endpoint_ClearOUT();
191 }
192 }
193 }