Move out many of the common class driver constants into grouped enums, to make them...
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Host / 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_HOST)
34
35 #define __INCLUDE_FROM_HID_CLASS_HOST_C
36 #define __INCLUDE_FROM_HID_DRIVER
37 #include "HID.h"
38
39 uint8_t HID_Host_ConfigurePipes(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo,
40 uint16_t ConfigDescriptorSize,
41 void* ConfigDescriptorData)
42 {
43 uint8_t FoundEndpoints = 0;
44
45 memset(&HIDInterfaceInfo->State, 0x00, sizeof(HIDInterfaceInfo->State));
46
47 if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)
48 return HID_ENUMERROR_InvalidConfigDescriptor;
49
50 USB_Descriptor_Interface_t* CurrentHIDInterface;
51
52 do
53 {
54 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
55 DCOMP_HID_Host_NextHIDInterface) != DESCRIPTOR_SEARCH_COMP_Found)
56 {
57 return HID_ENUMERROR_NoHIDInterfaceFound;
58 }
59
60 CurrentHIDInterface = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Interface_t);
61 } while (HIDInterfaceInfo->Config.HIDInterfaceProtocol &&
62 (CurrentHIDInterface->Protocol != HIDInterfaceInfo->Config.HIDInterfaceProtocol));
63
64 HIDInterfaceInfo->State.InterfaceNumber = CurrentHIDInterface->InterfaceNumber;
65 HIDInterfaceInfo->State.SupportsBootProtocol = (CurrentHIDInterface->SubClass != HID_BOOTP_NonBootProtocol);
66
67 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData, DCOMP_HID_NextHID) != DESCRIPTOR_SEARCH_COMP_Found)
68 return HID_ENUMERROR_NoHIDDescriptorFound;
69
70 HIDInterfaceInfo->State.HIDReportSize = DESCRIPTOR_CAST(ConfigDescriptorData, USB_HID_Descriptor_HID_t).HIDReportLength;
71
72 while (FoundEndpoints != (HID_FOUND_DATAPIPE_IN | HID_FOUND_DATAPIPE_OUT))
73 {
74 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
75 DCOMP_HID_Host_NextHIDInterfaceEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
76 {
77 if (FoundEndpoints & HID_FOUND_DATAPIPE_IN)
78 break;
79
80 return HID_ENUMERROR_EndpointsNotFound;
81 }
82
83 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Endpoint_t);
84
85 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
86 {
87 Pipe_ConfigurePipe(HIDInterfaceInfo->Config.DataINPipeNumber, EP_TYPE_INTERRUPT, PIPE_TOKEN_IN,
88 EndpointData->EndpointAddress, EndpointData->EndpointSize,
89 HIDInterfaceInfo->Config.DataINPipeDoubleBank ? PIPE_BANK_DOUBLE : PIPE_BANK_SINGLE);
90 HIDInterfaceInfo->State.DataINPipeSize = EndpointData->EndpointSize;
91
92 FoundEndpoints |= HID_FOUND_DATAPIPE_IN;
93 }
94 else
95 {
96 Pipe_ConfigurePipe(HIDInterfaceInfo->Config.DataOUTPipeNumber, EP_TYPE_INTERRUPT, PIPE_TOKEN_OUT,
97 EndpointData->EndpointAddress, EndpointData->EndpointSize,
98 HIDInterfaceInfo->Config.DataOUTPipeDoubleBank ? PIPE_BANK_DOUBLE : PIPE_BANK_SINGLE);
99 HIDInterfaceInfo->State.DataOUTPipeSize = EndpointData->EndpointSize;
100
101 HIDInterfaceInfo->State.DeviceUsesOUTPipe = true;
102
103 FoundEndpoints |= HID_FOUND_DATAPIPE_OUT;
104 }
105 }
106
107 HIDInterfaceInfo->State.LargestReportSize = 8;
108 HIDInterfaceInfo->State.IsActive = true;
109 return HID_ENUMERROR_NoError;
110 }
111
112 static uint8_t DCOMP_HID_Host_NextHIDInterface(void* const CurrentDescriptor)
113 {
114 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
115 {
116 USB_Descriptor_Interface_t* CurrentInterface = DESCRIPTOR_PCAST(CurrentDescriptor,
117 USB_Descriptor_Interface_t);
118
119 if (CurrentInterface->Class == HID_INTERFACE_CLASS)
120 return DESCRIPTOR_SEARCH_Found;
121 }
122
123 return DESCRIPTOR_SEARCH_NotFound;
124 }
125
126 static uint8_t DCOMP_HID_NextHID(void* const CurrentDescriptor)
127 {
128 if (DESCRIPTOR_TYPE(CurrentDescriptor) == HID_DTYPE_HID)
129 return DESCRIPTOR_SEARCH_Found;
130 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
131 return DESCRIPTOR_SEARCH_Fail;
132 else
133 return DESCRIPTOR_SEARCH_NotFound;
134 }
135
136 static uint8_t DCOMP_HID_Host_NextHIDInterfaceEndpoint(void* const CurrentDescriptor)
137 {
138 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
139 {
140 USB_Descriptor_Endpoint_t* CurrentEndpoint = DESCRIPTOR_PCAST(CurrentDescriptor,
141 USB_Descriptor_Endpoint_t);
142
143 if (!(Pipe_IsEndpointBound(CurrentEndpoint->EndpointAddress)))
144 return DESCRIPTOR_SEARCH_Found;
145 }
146 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
147 {
148 return DESCRIPTOR_SEARCH_Fail;
149 }
150
151 return DESCRIPTOR_SEARCH_NotFound;
152 }
153
154 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
155 uint8_t HID_Host_ReceiveReportByID(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo,
156 const uint8_t ReportID,
157 void* Buffer)
158 {
159 USB_ControlRequest = (USB_Request_Header_t)
160 {
161 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
162 .bRequest = HID_REQ_SetReport,
163 .wValue = ((HID_REPORT_ITEM_In + 1) << 8) | ReportID,
164 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
165 .wLength = USB_GetHIDReportSize(HIDInterfaceInfo->Config.HIDParserData, ReportID, HID_REPORT_ITEM_In),
166 };
167
168 Pipe_SelectPipe(PIPE_CONTROLPIPE);
169
170 return USB_Host_SendControlRequest(Buffer);
171 }
172 #endif
173
174 uint8_t HID_Host_ReceiveReport(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo,
175 void* Buffer)
176 {
177 if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.IsActive))
178 return PIPE_READYWAIT_DeviceDisconnected;
179
180 uint8_t ErrorCode;
181
182 Pipe_SelectPipe(HIDInterfaceInfo->Config.DataINPipeNumber);
183 Pipe_Unfreeze();
184
185 uint16_t ReportSize;
186 uint8_t* BufferPos = Buffer;
187
188 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
189 if (!(HIDInterfaceInfo->State.UsingBootProtocol))
190 {
191 uint8_t ReportID = 0;
192
193 if (HIDInterfaceInfo->Config.HIDParserData->UsingReportIDs)
194 {
195 ReportID = Pipe_Read_Byte();
196 *(BufferPos++) = ReportID;
197 }
198
199 ReportSize = USB_GetHIDReportSize(HIDInterfaceInfo->Config.HIDParserData, ReportID, HID_REPORT_ITEM_In);
200 }
201 else
202 #endif
203 {
204 ReportSize = Pipe_BytesInPipe();
205 }
206
207 if ((ErrorCode = Pipe_Read_Stream_LE(BufferPos, ReportSize, NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
208 return ErrorCode;
209
210 Pipe_ClearIN();
211 Pipe_Freeze();
212
213 return PIPE_RWSTREAM_NoError;
214 }
215
216 uint8_t HID_Host_SendReportByID(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo,
217 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
218 const uint8_t ReportID,
219 #endif
220 const uint8_t ReportType,
221 void* Buffer,
222 const uint16_t ReportSize)
223 {
224 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
225 if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.IsActive))
226 return false;
227
228 if (HIDInterfaceInfo->State.DeviceUsesOUTPipe && (ReportType == HID_REPORT_ITEM_Out))
229 {
230 uint8_t ErrorCode;
231
232 Pipe_SelectPipe(HIDInterfaceInfo->Config.DataOUTPipeNumber);
233 Pipe_Unfreeze();
234
235 if (ReportID)
236 Pipe_Write_Stream_LE(&ReportID, sizeof(ReportID), NO_STREAM_CALLBACK);
237
238 if ((ErrorCode = Pipe_Write_Stream_LE(Buffer, ReportSize, NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
239 return ErrorCode;
240
241 Pipe_ClearOUT();
242 Pipe_Freeze();
243
244 return PIPE_RWSTREAM_NoError;
245 }
246 else
247 #endif
248 {
249 USB_ControlRequest = (USB_Request_Header_t)
250 {
251 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
252 .bRequest = HID_REQ_SetReport,
253 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
254 .wValue = ((ReportType + 1) << 8) | ReportID,
255 #else
256 .wValue = ((ReportType + 1) << 8),
257 #endif
258 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
259 .wLength = ReportSize,
260 };
261
262 Pipe_SelectPipe(PIPE_CONTROLPIPE);
263
264 return USB_Host_SendControlRequest(Buffer);
265 }
266 }
267
268 bool HID_Host_IsReportReceived(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo)
269 {
270 if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.IsActive))
271 return false;
272
273 bool ReportReceived;
274
275 Pipe_SelectPipe(HIDInterfaceInfo->Config.DataINPipeNumber);
276 Pipe_Unfreeze();
277
278 ReportReceived = Pipe_IsINReceived();
279
280 Pipe_Freeze();
281
282 return ReportReceived;
283 }
284
285 uint8_t HID_Host_SetBootProtocol(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo)
286 {
287 uint8_t ErrorCode;
288
289 USB_ControlRequest = (USB_Request_Header_t)
290 {
291 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
292 .bRequest = HID_REQ_SetProtocol,
293 .wValue = 0,
294 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
295 .wLength = 0,
296 };
297
298 Pipe_SelectPipe(PIPE_CONTROLPIPE);
299
300 if (!(HIDInterfaceInfo->State.SupportsBootProtocol))
301 return HID_ERROR_LOGICAL;
302
303 if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
304 return ErrorCode;
305
306 HIDInterfaceInfo->State.LargestReportSize = 8;
307 HIDInterfaceInfo->State.UsingBootProtocol = true;
308
309 return HOST_SENDCONTROL_Successful;
310 }
311
312 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
313 uint8_t HID_Host_SetReportProtocol(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo)
314 {
315 uint8_t ErrorCode;
316
317 uint8_t HIDReportData[HIDInterfaceInfo->State.HIDReportSize];
318
319 USB_ControlRequest = (USB_Request_Header_t)
320 {
321 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_INTERFACE),
322 .bRequest = REQ_GetDescriptor,
323 .wValue = (HID_DTYPE_Report << 8),
324 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
325 .wLength = HIDInterfaceInfo->State.HIDReportSize,
326 };
327
328 Pipe_SelectPipe(PIPE_CONTROLPIPE);
329
330 if ((ErrorCode = USB_Host_SendControlRequest(HIDReportData)) != HOST_SENDCONTROL_Successful)
331 return ErrorCode;
332
333 if (HIDInterfaceInfo->State.UsingBootProtocol)
334 {
335 USB_ControlRequest = (USB_Request_Header_t)
336 {
337 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
338 .bRequest = HID_REQ_SetProtocol,
339 .wValue = 1,
340 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
341 .wLength = 0,
342 };
343
344 if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
345 return ErrorCode;
346
347 HIDInterfaceInfo->State.UsingBootProtocol = false;
348 }
349
350 if (HIDInterfaceInfo->Config.HIDParserData == NULL)
351 return HID_ERROR_LOGICAL;
352
353 if ((ErrorCode = USB_ProcessHIDReport(HIDReportData, HIDInterfaceInfo->State.HIDReportSize,
354 HIDInterfaceInfo->Config.HIDParserData)) != HID_PARSE_Successful)
355 {
356 return HID_ERROR_LOGICAL | ErrorCode;
357 }
358
359 uint8_t LargestReportSizeBits = HIDInterfaceInfo->Config.HIDParserData->LargestReportSizeBits;
360 HIDInterfaceInfo->State.LargestReportSize = (LargestReportSizeBits >> 3) + ((LargestReportSizeBits & 0x07) != 0);
361
362 return 0;
363 }
364 #endif
365
366 #endif