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