Added class specific descriptor type defines with standard USB-IF element naming.
[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 uint16_t ReportSize = 0;
52 uint8_t ReportID = (USB_ControlRequest.wValue & 0xFF);
53 uint8_t ReportType = (USB_ControlRequest.wValue >> 8) - 1;
54 uint8_t ReportData[HIDInterfaceInfo->Config.PrevReportINBufferSize];
55
56 memset(ReportData, 0, sizeof(ReportData));
57
58 CALLBACK_HID_Device_CreateHIDReport(HIDInterfaceInfo, &ReportID, ReportType, ReportData, &ReportSize);
59
60 if (HIDInterfaceInfo->Config.PrevReportINBuffer != NULL)
61 memcpy(HIDInterfaceInfo->Config.PrevReportINBuffer, ReportData, HIDInterfaceInfo->Config.PrevReportINBufferSize);
62
63 Endpoint_ClearSETUP();
64 Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP);
65 Endpoint_Write_Control_Stream_LE(ReportData, ReportSize);
66 Endpoint_ClearOUT();
67 }
68
69 break;
70 case REQ_SetReport:
71 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
72 {
73 uint16_t ReportSize = USB_ControlRequest.wLength;
74 uint8_t ReportID = (USB_ControlRequest.wValue & 0xFF);
75 uint8_t ReportType = (USB_ControlRequest.wValue >> 8) - 1;
76 uint8_t ReportData[ReportSize];
77
78 Endpoint_ClearSETUP();
79 Endpoint_Read_Control_Stream_LE(ReportData, ReportSize);
80 Endpoint_ClearIN();
81
82 CALLBACK_HID_Device_ProcessHIDReport(HIDInterfaceInfo, ReportID, ReportType, ReportData, ReportSize);
83 }
84
85 break;
86 case REQ_GetProtocol:
87 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
88 {
89 Endpoint_ClearSETUP();
90 Endpoint_Write_Byte(HIDInterfaceInfo->State.UsingReportProtocol);
91 Endpoint_ClearIN();
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 Endpoint_ClearStatusStage();
101
102 HIDInterfaceInfo->State.UsingReportProtocol = ((USB_ControlRequest.wValue & 0xFF) != 0x00);
103 }
104
105 break;
106 case REQ_SetIdle:
107 if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
108 {
109 Endpoint_ClearSETUP();
110 Endpoint_ClearStatusStage();
111
112 HIDInterfaceInfo->State.IdleCount = ((USB_ControlRequest.wValue & 0xFF00) >> 6);
113 }
114
115 break;
116 case REQ_GetIdle:
117 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
118 {
119 Endpoint_ClearSETUP();
120 Endpoint_Write_Byte(HIDInterfaceInfo->State.IdleCount >> 2);
121 Endpoint_ClearIN();
122 Endpoint_ClearStatusStage();
123 }
124
125 break;
126 }
127 }
128
129 bool HID_Device_ConfigureEndpoints(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo)
130 {
131 memset(&HIDInterfaceInfo->State, 0x00, sizeof(HIDInterfaceInfo->State));
132 HIDInterfaceInfo->State.UsingReportProtocol = true;
133 HIDInterfaceInfo->State.IdleCount = 500;
134
135 if (!(Endpoint_ConfigureEndpoint(HIDInterfaceInfo->Config.ReportINEndpointNumber, EP_TYPE_INTERRUPT,
136 ENDPOINT_DIR_IN, HIDInterfaceInfo->Config.ReportINEndpointSize,
137 HIDInterfaceInfo->Config.ReportINEndpointDoubleBank ? ENDPOINT_BANK_DOUBLE : 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, REPORT_ITEM_TYPE_In,
161 ReportINData, &ReportINSize);
162 bool StatesChanged = false;
163 bool IdlePeriodElapsed = (HIDInterfaceInfo->State.IdleCount && !(HIDInterfaceInfo->State.IdleMSRemaining));
164
165 if (HIDInterfaceInfo->Config.PrevReportINBuffer != NULL)
166 {
167 StatesChanged = (memcmp(ReportINData, HIDInterfaceInfo->Config.PrevReportINBuffer, ReportINSize) != 0);
168 memcpy(HIDInterfaceInfo->Config.PrevReportINBuffer, ReportINData, HIDInterfaceInfo->Config.PrevReportINBufferSize);
169 }
170
171 if (ReportINSize && (ForceSend || StatesChanged || IdlePeriodElapsed))
172 {
173 HIDInterfaceInfo->State.IdleMSRemaining = HIDInterfaceInfo->State.IdleCount;
174
175 Endpoint_SelectEndpoint(HIDInterfaceInfo->Config.ReportINEndpointNumber);
176
177 if (ReportID)
178 Endpoint_Write_Byte(ReportID);
179
180 Endpoint_Write_Stream_LE(ReportINData, ReportINSize, NO_STREAM_CALLBACK);
181
182 Endpoint_ClearIN();
183 }
184 }
185 }
186
187 #endif