Add optional double-banking support to the Device mode Class Drivers, on a per-endpoi...
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Host / HID.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 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 uint8_t* 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, PIPE_BANK_SINGLE);
88 HIDInterfaceInfo->State.DataINPipeSize = EndpointData->EndpointSize;
89
90 FoundEndpoints |= HID_FOUND_DATAPIPE_IN;
91 }
92 else
93 {
94 Pipe_ConfigurePipe(HIDInterfaceInfo->Config.DataOUTPipeNumber, EP_TYPE_INTERRUPT, PIPE_TOKEN_OUT,
95 EndpointData->EndpointAddress, EndpointData->EndpointSize, PIPE_BANK_SINGLE);
96 HIDInterfaceInfo->State.DataOUTPipeSize = EndpointData->EndpointSize;
97
98 HIDInterfaceInfo->State.DeviceUsesOUTPipe = true;
99
100 FoundEndpoints |= HID_FOUND_DATAPIPE_OUT;
101 }
102 }
103
104 HIDInterfaceInfo->State.LargestReportSize = 8;
105 HIDInterfaceInfo->State.IsActive = true;
106 return HID_ENUMERROR_NoError;
107 }
108
109 static uint8_t DComp_HID_Host_NextHIDInterface(void* const CurrentDescriptor)
110 {
111 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
112 {
113 USB_Descriptor_Interface_t* CurrentInterface = DESCRIPTOR_PCAST(CurrentDescriptor,
114 USB_Descriptor_Interface_t);
115
116 if (CurrentInterface->Class == HID_INTERFACE_CLASS)
117 return DESCRIPTOR_SEARCH_Found;
118 }
119
120 return DESCRIPTOR_SEARCH_NotFound;
121 }
122
123 static uint8_t DComp_NextHID(void* const CurrentDescriptor)
124 {
125 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_HID)
126 return DESCRIPTOR_SEARCH_Found;
127 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
128 return DESCRIPTOR_SEARCH_Fail;
129 else
130 return DESCRIPTOR_SEARCH_NotFound;
131 }
132
133 static uint8_t DComp_HID_Host_NextHIDInterfaceEndpoint(void* const CurrentDescriptor)
134 {
135 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
136 {
137 USB_Descriptor_Endpoint_t* CurrentEndpoint = DESCRIPTOR_PCAST(CurrentDescriptor,
138 USB_Descriptor_Endpoint_t);
139
140 if (!(Pipe_IsEndpointBound(CurrentEndpoint->EndpointAddress)))
141 return DESCRIPTOR_SEARCH_Found;
142 }
143 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
144 {
145 return DESCRIPTOR_SEARCH_Fail;
146 }
147
148 return DESCRIPTOR_SEARCH_NotFound;
149 }
150
151 void HID_Host_USBTask(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo)
152 {
153
154 }
155
156 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
157 uint8_t HID_Host_ReceiveReportByID(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo, const uint8_t ReportID, void* Buffer)
158 {
159 USB_ControlRequest = (USB_Request_Header_t)
160 {
161 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
162 .bRequest = REQ_SetReport,
163 .wValue = ReportID,
164 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
165 .wLength = USB_GetHIDReportSize(HIDInterfaceInfo->Config.HIDParserData, ReportID, REPORT_ITEM_TYPE_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, void* Buffer)
175 {
176 if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.IsActive))
177 return false;
178
179 uint8_t ErrorCode;
180
181 Pipe_SelectPipe(HIDInterfaceInfo->Config.DataINPipeNumber);
182 Pipe_Unfreeze();
183
184 uint16_t ReportSize;
185
186 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
187 if (!(HIDInterfaceInfo->State.UsingBootProtocol))
188 {
189 uint8_t ReportID = 0;
190
191 if (HIDInterfaceInfo->Config.HIDParserData->UsingReportIDs)
192 {
193 ReportID = Pipe_Read_Byte();
194 *((uint8_t*)Buffer++) = ReportID;
195 }
196
197 ReportSize = USB_GetHIDReportSize(HIDInterfaceInfo->Config.HIDParserData, ReportID, REPORT_ITEM_TYPE_In);
198 }
199 else
200 #endif
201 {
202 ReportSize = Pipe_BytesInPipe();
203 }
204
205 if ((ErrorCode = Pipe_Read_Stream_LE(Buffer, ReportSize, NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
206 return ErrorCode;
207
208 Pipe_ClearIN();
209 Pipe_Freeze();
210
211 return PIPE_RWSTREAM_NoError;
212 }
213
214 uint8_t HID_Host_SendReportByID(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo,
215 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
216 const uint8_t ReportID,
217 #endif
218 void* Buffer, const uint16_t ReportSize)
219 {
220 if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.IsActive))
221 return false;
222
223 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
224 if (HIDInterfaceInfo->State.DeviceUsesOUTPipe)
225 {
226 uint8_t ErrorCode;
227
228 Pipe_SelectPipe(HIDInterfaceInfo->Config.DataOUTPipeNumber);
229 Pipe_Unfreeze();
230
231 if (ReportID)
232 Pipe_Write_Stream_LE(&ReportID, sizeof(ReportID), NO_STREAM_CALLBACK);
233
234 if ((ErrorCode = Pipe_Write_Stream_LE(Buffer, ReportSize, NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
235 return ErrorCode;
236
237 Pipe_ClearOUT();
238 Pipe_Freeze();
239
240 return PIPE_RWSTREAM_NoError;
241 }
242 else
243 #endif
244 {
245 USB_ControlRequest = (USB_Request_Header_t)
246 {
247 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
248 .bRequest = REQ_SetReport,
249 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
250 .wValue = ReportID,
251 #else
252 .wValue = 0,
253 #endif
254 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
255 .wLength = ReportSize,
256 };
257
258 Pipe_SelectPipe(PIPE_CONTROLPIPE);
259
260 return USB_Host_SendControlRequest(Buffer);
261 }
262 }
263
264 bool HID_Host_IsReportReceived(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo)
265 {
266 if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.IsActive))
267 return false;
268
269 bool ReportReceived;
270
271 Pipe_SelectPipe(HIDInterfaceInfo->Config.DataINPipeNumber);
272 Pipe_Unfreeze();
273
274 ReportReceived = Pipe_IsINReceived();
275
276 Pipe_Freeze();
277
278 return ReportReceived;
279 }
280
281 uint8_t HID_Host_SetBootProtocol(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo)
282 {
283 if (HIDInterfaceInfo->State.UsingBootProtocol)
284 return HOST_SENDCONTROL_Successful;
285
286 uint8_t ErrorCode;
287
288 USB_ControlRequest = (USB_Request_Header_t)
289 {
290 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
291 .bRequest = REQ_SetProtocol,
292 .wValue = 0,
293 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
294 .wLength = 0,
295 };
296
297 Pipe_SelectPipe(PIPE_CONTROLPIPE);
298
299 if (!(HIDInterfaceInfo->State.SupportsBootProtocol))
300 return HID_ERROR_LOGICAL;
301
302 if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
303 return ErrorCode;
304
305 HIDInterfaceInfo->State.LargestReportSize = 8;
306 HIDInterfaceInfo->State.UsingBootProtocol = true;
307
308 return HOST_SENDCONTROL_Successful;
309 }
310
311 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
312 uint8_t HID_Host_SetReportProtocol(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo)
313 {
314 uint8_t ErrorCode;
315
316 uint8_t HIDReportData[HIDInterfaceInfo->State.HIDReportSize];
317
318 USB_ControlRequest = (USB_Request_Header_t)
319 {
320 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_INTERFACE),
321 .bRequest = REQ_GetDescriptor,
322 .wValue = (DTYPE_Report << 8),
323 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
324 .wLength = HIDInterfaceInfo->State.HIDReportSize,
325 };
326
327 Pipe_SelectPipe(PIPE_CONTROLPIPE);
328
329 if ((ErrorCode = USB_Host_SendControlRequest(HIDReportData)) != HOST_SENDCONTROL_Successful)
330 return ErrorCode;
331
332 if (HIDInterfaceInfo->State.UsingBootProtocol)
333 {
334 USB_ControlRequest = (USB_Request_Header_t)
335 {
336 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
337 .bRequest = REQ_SetProtocol,
338 .wValue = 1,
339 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
340 .wLength = 0,
341 };
342
343 if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
344 return ErrorCode;
345
346 HIDInterfaceInfo->State.UsingBootProtocol = false;
347 }
348
349 if (HIDInterfaceInfo->Config.HIDParserData == NULL)
350 return HID_ERROR_LOGICAL;
351
352 if ((ErrorCode = USB_ProcessHIDReport(HIDReportData, HIDInterfaceInfo->State.HIDReportSize,
353 HIDInterfaceInfo->Config.HIDParserData)) != HID_PARSE_Successful)
354 {
355 return HID_ERROR_LOGICAL | ErrorCode;
356 }
357
358 uint8_t LargestReportSizeBits = HIDInterfaceInfo->Config.HIDParserData->LargestReportSizeBits;
359 HIDInterfaceInfo->State.LargestReportSize = (LargestReportSizeBits >> 3) + ((LargestReportSizeBits & 0x07) != 0);
360
361 return 0;
362 }
363 #endif
364
365 #endif