Commit of new class abstraction APIs for all device demos other than the MIDI demo...
[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->ReportBufferSize];
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_RegisterStartOfFrame(USB_ClassInfo_HID_t* HIDInterfaceInfo)
151 {
152 if (HIDInterfaceInfo->IdleMSRemaining)
153 HIDInterfaceInfo->IdleMSRemaining--;
154 }
155
156 void USB_HID_USBTask(USB_ClassInfo_HID_t* HIDInterfaceInfo)
157 {
158 if (!(USB_IsConnected))
159 return;
160
161 Endpoint_SelectEndpoint(HIDInterfaceInfo->ReportINEndpointNumber);
162
163 if (Endpoint_IsReadWriteAllowed() &&
164 !(HIDInterfaceInfo->IdleCount && HIDInterfaceInfo->IdleMSRemaining))
165 {
166 if (HIDInterfaceInfo->IdleCount && !(HIDInterfaceInfo->IdleMSRemaining))
167 HIDInterfaceInfo->IdleMSRemaining = HIDInterfaceInfo->IdleCount;
168
169 uint8_t ReportINData[HIDInterfaceInfo->ReportBufferSize];
170 uint16_t ReportINSize;
171
172 memset(ReportINData, 0, sizeof(ReportINData));
173
174 ReportINSize = CALLBACK_USB_HID_CreateNextHIDReport(HIDInterfaceInfo, ReportINData);
175
176 if (ReportINSize)
177 {
178 Endpoint_Write_Stream_LE(ReportINData, ReportINSize
179 #if !defined(NO_STREAM_CALLBACKS)
180 , NO_STREAM_CALLBACK
181 #endif
182 );
183 }
184
185 Endpoint_ClearIN();
186 }
187
188 if (HIDInterfaceInfo->ReportOUTEndpointNumber)
189 {
190 Endpoint_SelectEndpoint(HIDInterfaceInfo->ReportOUTEndpointNumber);
191
192 if (Endpoint_IsOUTReceived())
193 {
194 uint16_t ReportOUTSize = Endpoint_BytesInEndpoint();
195 uint8_t ReportOUTData[ReportOUTSize];
196
197 if (ReportOUTSize)
198 {
199 Endpoint_Read_Stream_LE(ReportOUTData, ReportOUTSize
200 #if !defined(NO_STREAM_CALLBACKS)
201 , NO_STREAM_CALLBACK
202 #endif
203 );
204 }
205
206 CALLBACK_USB_HID_ProcessReceivedHIDReport(HIDInterfaceInfo, ReportOUTData, ReportOUTSize);
207
208 Endpoint_ClearOUT();
209 }
210 }
211 }