Refactored Host mode Class Driver *_Host_ConfigurePipes() routines to be more space...
[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.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_HOST)
34
35 #define __INCLUDE_FROM_HID_DRIVER
36 #define __INCLUDE_FROM_HID_HOST_C
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 USB_Descriptor_Endpoint_t* DataINEndpoint = NULL;
44 USB_Descriptor_Endpoint_t* DataOUTEndpoint = NULL;
45 USB_Descriptor_Interface_t* HIDInterface = NULL;
46 USB_HID_Descriptor_HID_t* HIDDescriptor = NULL;
47
48 memset(&HIDInterfaceInfo->State, 0x00, sizeof(HIDInterfaceInfo->State));
49
50 if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)
51 return HID_ENUMERROR_InvalidConfigDescriptor;
52
53 while (!(DataINEndpoint) || !(DataOUTEndpoint))
54 {
55 if (!(HIDInterface) ||
56 USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
57 DCOMP_HID_Host_NextHIDInterfaceEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
58 {
59 if (DataINEndpoint || DataOUTEndpoint)
60 break;
61
62 do
63 {
64 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
65 DCOMP_HID_Host_NextHIDInterface) != DESCRIPTOR_SEARCH_COMP_Found)
66 {
67 return HID_ENUMERROR_NoCompatibleInterfaceFound;
68 }
69
70 HIDInterface = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Interface_t);
71 } while (HIDInterfaceInfo->Config.HIDInterfaceProtocol &&
72 (HIDInterface->Protocol != HIDInterfaceInfo->Config.HIDInterfaceProtocol));
73
74 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
75 DCOMP_HID_Host_NextHID) != DESCRIPTOR_SEARCH_COMP_Found)
76 {
77 return HID_ENUMERROR_NoCompatibleInterfaceFound;
78 }
79
80 HIDDescriptor = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_HID_Descriptor_HID_t);
81
82 DataINEndpoint = NULL;
83 DataOUTEndpoint = NULL;
84
85 continue;
86 }
87
88 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Endpoint_t);
89
90 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
91 DataINEndpoint = EndpointData;
92 else
93 DataOUTEndpoint = EndpointData;
94 }
95
96 for (uint8_t PipeNum = 1; PipeNum < PIPE_TOTAL_PIPES; PipeNum++)
97 {
98 uint16_t Size;
99 uint8_t Type;
100 uint8_t Token;
101 uint8_t EndpointAddress;
102 uint8_t InterruptPeriod;
103 bool DoubleBanked;
104
105 if (PipeNum == HIDInterfaceInfo->Config.DataINPipeNumber)
106 {
107 Size = DataINEndpoint->EndpointSize;
108 EndpointAddress = DataINEndpoint->EndpointAddress;
109 Token = PIPE_TOKEN_IN;
110 Type = EP_TYPE_INTERRUPT;
111 DoubleBanked = HIDInterfaceInfo->Config.DataINPipeDoubleBank;
112 InterruptPeriod = DataINEndpoint->PollingIntervalMS;
113
114 HIDInterfaceInfo->State.DataINPipeSize = DataINEndpoint->EndpointSize;
115 }
116 else if (PipeNum == HIDInterfaceInfo->Config.DataOUTPipeNumber)
117 {
118 Size = DataOUTEndpoint->EndpointSize;
119 EndpointAddress = DataOUTEndpoint->EndpointAddress;
120 Token = PIPE_TOKEN_OUT;
121 Type = EP_TYPE_INTERRUPT;
122 DoubleBanked = HIDInterfaceInfo->Config.DataOUTPipeDoubleBank;
123 InterruptPeriod = DataOUTEndpoint->PollingIntervalMS;
124
125 HIDInterfaceInfo->State.DataOUTPipeSize = DataOUTEndpoint->EndpointSize;
126 HIDInterfaceInfo->State.DeviceUsesOUTPipe = true;
127 }
128 else
129 {
130 continue;
131 }
132
133 if (!(Pipe_ConfigurePipe(PipeNum, Type, Token, EndpointAddress, Size,
134 DoubleBanked ? PIPE_BANK_DOUBLE : PIPE_BANK_SINGLE)))
135 {
136 return HID_ENUMERROR_PipeConfigurationFailed;
137 }
138
139 if (InterruptPeriod)
140 Pipe_SetInterruptPeriod(InterruptPeriod);
141 }
142
143 HIDInterfaceInfo->State.InterfaceNumber = HIDInterface->InterfaceNumber;
144 HIDInterfaceInfo->State.HIDReportSize = HIDDescriptor->HIDReportLength;
145 HIDInterfaceInfo->State.SupportsBootProtocol = (HIDInterface->SubClass != HID_CSCP_NonBootProtocol);
146 HIDInterfaceInfo->State.LargestReportSize = 8;
147 HIDInterfaceInfo->State.IsActive = true;
148
149 return HID_ENUMERROR_NoError;
150 }
151
152 static uint8_t DCOMP_HID_Host_NextHIDInterface(void* const CurrentDescriptor)
153 {
154 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
155
156 if (Header->Type == DTYPE_Interface)
157 {
158 USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
159
160 if (Interface->Class == HID_CSCP_HIDClass)
161 return DESCRIPTOR_SEARCH_Found;
162 }
163
164 return DESCRIPTOR_SEARCH_NotFound;
165 }
166
167 static uint8_t DCOMP_HID_Host_NextHID(void* const CurrentDescriptor)
168 {
169 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
170
171 if (Header->Type == HID_DTYPE_HID)
172 return DESCRIPTOR_SEARCH_Found;
173 else if (Header->Type == DTYPE_Interface)
174 return DESCRIPTOR_SEARCH_Fail;
175 else
176 return DESCRIPTOR_SEARCH_NotFound;
177 }
178
179 static uint8_t DCOMP_HID_Host_NextHIDInterfaceEndpoint(void* const CurrentDescriptor)
180 {
181 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
182
183 if (Header->Type == DTYPE_Endpoint)
184 {
185 USB_Descriptor_Endpoint_t* Endpoint = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Endpoint_t);
186
187 if (!(Pipe_IsEndpointBound(Endpoint->EndpointAddress)))
188 return DESCRIPTOR_SEARCH_Found;
189 }
190 else if (Header->Type == DTYPE_Interface)
191 {
192 return DESCRIPTOR_SEARCH_Fail;
193 }
194
195 return DESCRIPTOR_SEARCH_NotFound;
196 }
197
198 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
199 uint8_t HID_Host_ReceiveReportByID(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo,
200 const uint8_t ReportID,
201 void* Buffer)
202 {
203 USB_ControlRequest = (USB_Request_Header_t)
204 {
205 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
206 .bRequest = HID_REQ_SetReport,
207 .wValue = ((HID_REPORT_ITEM_In + 1) << 8) | ReportID,
208 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
209 .wLength = USB_GetHIDReportSize(HIDInterfaceInfo->Config.HIDParserData, ReportID, HID_REPORT_ITEM_In),
210 };
211
212 Pipe_SelectPipe(PIPE_CONTROLPIPE);
213
214 return USB_Host_SendControlRequest(Buffer);
215 }
216 #endif
217
218 uint8_t HID_Host_ReceiveReport(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo,
219 void* Buffer)
220 {
221 if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.IsActive))
222 return PIPE_READYWAIT_DeviceDisconnected;
223
224 uint8_t ErrorCode;
225
226 Pipe_SelectPipe(HIDInterfaceInfo->Config.DataINPipeNumber);
227 Pipe_Unfreeze();
228
229 uint16_t ReportSize;
230 uint8_t* BufferPos = Buffer;
231
232 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
233 if (!(HIDInterfaceInfo->State.UsingBootProtocol))
234 {
235 uint8_t ReportID = 0;
236
237 if (HIDInterfaceInfo->Config.HIDParserData->UsingReportIDs)
238 {
239 ReportID = Pipe_Read_Byte();
240 *(BufferPos++) = ReportID;
241 }
242
243 ReportSize = USB_GetHIDReportSize(HIDInterfaceInfo->Config.HIDParserData, ReportID, HID_REPORT_ITEM_In);
244 }
245 else
246 #endif
247 {
248 ReportSize = Pipe_BytesInPipe();
249 }
250
251 if ((ErrorCode = Pipe_Read_Stream_LE(BufferPos, ReportSize, NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
252 return ErrorCode;
253
254 Pipe_ClearIN();
255 Pipe_Freeze();
256
257 return PIPE_RWSTREAM_NoError;
258 }
259
260 uint8_t HID_Host_SendReportByID(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo,
261 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
262 const uint8_t ReportID,
263 #endif
264 const uint8_t ReportType,
265 void* Buffer,
266 const uint16_t ReportSize)
267 {
268 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
269 if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.IsActive))
270 return false;
271
272 if (HIDInterfaceInfo->State.DeviceUsesOUTPipe && (ReportType == HID_REPORT_ITEM_Out))
273 {
274 uint8_t ErrorCode;
275
276 Pipe_SelectPipe(HIDInterfaceInfo->Config.DataOUTPipeNumber);
277 Pipe_Unfreeze();
278
279 if (ReportID)
280 Pipe_Write_Stream_LE(&ReportID, sizeof(ReportID), NO_STREAM_CALLBACK);
281
282 if ((ErrorCode = Pipe_Write_Stream_LE(Buffer, ReportSize, NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
283 return ErrorCode;
284
285 Pipe_ClearOUT();
286 Pipe_Freeze();
287
288 return PIPE_RWSTREAM_NoError;
289 }
290 else
291 #endif
292 {
293 USB_ControlRequest = (USB_Request_Header_t)
294 {
295 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
296 .bRequest = HID_REQ_SetReport,
297 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
298 .wValue = ((ReportType + 1) << 8) | ReportID,
299 #else
300 .wValue = ((ReportType + 1) << 8),
301 #endif
302 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
303 .wLength = ReportSize,
304 };
305
306 Pipe_SelectPipe(PIPE_CONTROLPIPE);
307
308 return USB_Host_SendControlRequest(Buffer);
309 }
310 }
311
312 bool HID_Host_IsReportReceived(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo)
313 {
314 if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.IsActive))
315 return false;
316
317 bool ReportReceived;
318
319 Pipe_SelectPipe(HIDInterfaceInfo->Config.DataINPipeNumber);
320 Pipe_Unfreeze();
321
322 ReportReceived = Pipe_IsINReceived();
323
324 Pipe_Freeze();
325
326 return ReportReceived;
327 }
328
329 uint8_t HID_Host_SetBootProtocol(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo)
330 {
331 uint8_t ErrorCode;
332
333 USB_ControlRequest = (USB_Request_Header_t)
334 {
335 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
336 .bRequest = HID_REQ_SetProtocol,
337 .wValue = 0,
338 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
339 .wLength = 0,
340 };
341
342 Pipe_SelectPipe(PIPE_CONTROLPIPE);
343
344 if (!(HIDInterfaceInfo->State.SupportsBootProtocol))
345 return HID_ERROR_LOGICAL;
346
347 if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
348 return ErrorCode;
349
350 HIDInterfaceInfo->State.LargestReportSize = 8;
351 HIDInterfaceInfo->State.UsingBootProtocol = true;
352
353 return HOST_SENDCONTROL_Successful;
354 }
355
356 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
357 uint8_t HID_Host_SetReportProtocol(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo)
358 {
359 uint8_t ErrorCode;
360
361 uint8_t HIDReportData[HIDInterfaceInfo->State.HIDReportSize];
362
363 USB_ControlRequest = (USB_Request_Header_t)
364 {
365 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_INTERFACE),
366 .bRequest = REQ_GetDescriptor,
367 .wValue = (HID_DTYPE_Report << 8),
368 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
369 .wLength = HIDInterfaceInfo->State.HIDReportSize,
370 };
371
372 Pipe_SelectPipe(PIPE_CONTROLPIPE);
373
374 if ((ErrorCode = USB_Host_SendControlRequest(HIDReportData)) != HOST_SENDCONTROL_Successful)
375 return ErrorCode;
376
377 if (HIDInterfaceInfo->State.UsingBootProtocol)
378 {
379 USB_ControlRequest = (USB_Request_Header_t)
380 {
381 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
382 .bRequest = HID_REQ_SetProtocol,
383 .wValue = 1,
384 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
385 .wLength = 0,
386 };
387
388 if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
389 return ErrorCode;
390
391 HIDInterfaceInfo->State.UsingBootProtocol = false;
392 }
393
394 if (HIDInterfaceInfo->Config.HIDParserData == NULL)
395 return HID_ERROR_LOGICAL;
396
397 if ((ErrorCode = USB_ProcessHIDReport(HIDReportData, HIDInterfaceInfo->State.HIDReportSize,
398 HIDInterfaceInfo->Config.HIDParserData)) != HID_PARSE_Successful)
399 {
400 return HID_ERROR_LOGICAL | ErrorCode;
401 }
402
403 uint8_t LargestReportSizeBits = HIDInterfaceInfo->Config.HIDParserData->LargestReportSizeBits;
404 HIDInterfaceInfo->State.LargestReportSize = (LargestReportSizeBits >> 3) + ((LargestReportSizeBits & 0x07) != 0);
405
406 return 0;
407 }
408 #endif
409
410 #endif
411