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