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