Fixed issue in the HID Host class driver's HID_Host_SendReportByID() routine using...
[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* 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* 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* 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 uint8_t HID_Host_ReceiveReportByID(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo, const uint8_t ReportID, void* Buffer)
157 {
158 USB_ControlRequest = (USB_Request_Header_t)
159 {
160 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
161 .bRequest = REQ_SetReport,
162 .wValue = ReportID,
163 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
164 .wLength = USB_GetHIDReportSize(HIDInterfaceInfo->Config.HIDParserData, ReportID, REPORT_ITEM_TYPE_In),
165 };
166
167 Pipe_SelectPipe(PIPE_CONTROLPIPE);
168
169 return USB_Host_SendControlRequest(Buffer);
170 }
171
172 uint8_t HID_Host_ReceiveReport(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo, void* Buffer)
173 {
174 if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.IsActive))
175 return false;
176
177 uint8_t ErrorCode;
178
179 Pipe_SelectPipe(HIDInterfaceInfo->Config.DataINPipeNumber);
180 Pipe_Unfreeze();
181
182 uint16_t ReportSize;
183
184 if (HIDInterfaceInfo->State.UsingBootProtocol)
185 {
186 ReportSize = Pipe_BytesInPipe();
187 }
188 else
189 {
190 uint8_t ReportID = 0;
191
192 if (HIDInterfaceInfo->Config.HIDParserData->UsingReportIDs)
193 {
194 ReportID = Pipe_Read_Byte();
195 *((uint8_t*)Buffer++) = ReportID;
196 }
197
198 ReportSize = USB_GetHIDReportSize(HIDInterfaceInfo->Config.HIDParserData, ReportID, REPORT_ITEM_TYPE_In);
199 }
200
201 if ((ErrorCode = Pipe_Read_Stream_LE(Buffer, ReportSize, NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
202 return ErrorCode;
203
204 Pipe_ClearIN();
205 Pipe_Freeze();
206
207 return PIPE_RWSTREAM_NoError;
208 }
209
210 uint8_t HID_Host_SendReportByID(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo, uint8_t ReportID, void* Buffer,
211 const uint16_t ReportSize)
212 {
213 if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.IsActive))
214 return false;
215
216 if (!(HIDInterfaceInfo->State.DeviceUsesOUTPipe))
217 {
218 USB_ControlRequest = (USB_Request_Header_t)
219 {
220 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
221 .bRequest = REQ_SetReport,
222 .wValue = ReportID,
223 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
224 .wLength = ReportSize,
225 };
226
227 Pipe_SelectPipe(PIPE_CONTROLPIPE);
228
229 return USB_Host_SendControlRequest(Buffer);
230 }
231 else
232 {
233 uint8_t ErrorCode;
234
235 Pipe_SelectPipe(HIDInterfaceInfo->Config.DataOUTPipeNumber);
236 Pipe_Unfreeze();
237
238 if (ReportID)
239 Pipe_Write_Stream_LE(&ReportID, sizeof(ReportID), NO_STREAM_CALLBACK);
240
241 if ((ErrorCode = Pipe_Write_Stream_LE(Buffer, ReportSize, NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
242 return ErrorCode;
243
244 Pipe_ClearOUT();
245 Pipe_Freeze();
246
247 return PIPE_RWSTREAM_NoError;
248 }
249 }
250
251 bool HID_Host_IsReportReceived(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo)
252 {
253 if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.IsActive))
254 return false;
255
256 bool ReportReceived;
257
258 Pipe_SelectPipe(HIDInterfaceInfo->Config.DataINPipeNumber);
259 Pipe_Unfreeze();
260
261 ReportReceived = Pipe_IsINReceived();
262
263 Pipe_Freeze();
264
265 return ReportReceived;
266 }
267
268 uint8_t HID_Host_SetBootProtocol(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo)
269 {
270 if (HIDInterfaceInfo->State.UsingBootProtocol)
271 return HOST_SENDCONTROL_Successful;
272
273 uint8_t ErrorCode;
274
275 USB_ControlRequest = (USB_Request_Header_t)
276 {
277 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
278 .bRequest = REQ_SetProtocol,
279 .wValue = 0,
280 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
281 .wLength = 0,
282 };
283
284 Pipe_SelectPipe(PIPE_CONTROLPIPE);
285
286 if (!(HIDInterfaceInfo->State.SupportsBootProtocol))
287 return HID_ERROR_LOGICAL;
288
289 if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
290 return ErrorCode;
291
292 HIDInterfaceInfo->State.LargestReportSize = 8;
293 HIDInterfaceInfo->State.UsingBootProtocol = true;
294
295 return HOST_SENDCONTROL_Successful;
296 }
297
298 uint8_t HID_Host_SetReportProtocol(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo)
299 {
300 uint8_t ErrorCode;
301
302 uint8_t HIDReportData[HIDInterfaceInfo->State.HIDReportSize];
303
304 USB_ControlRequest = (USB_Request_Header_t)
305 {
306 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_INTERFACE),
307 .bRequest = REQ_GetDescriptor,
308 .wValue = (DTYPE_Report << 8),
309 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
310 .wLength = HIDInterfaceInfo->State.HIDReportSize,
311 };
312
313 Pipe_SelectPipe(PIPE_CONTROLPIPE);
314
315 if ((ErrorCode = USB_Host_SendControlRequest(HIDReportData)) != HOST_SENDCONTROL_Successful)
316 return ErrorCode;
317
318 if (HIDInterfaceInfo->State.UsingBootProtocol)
319 {
320 USB_ControlRequest = (USB_Request_Header_t)
321 {
322 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
323 .bRequest = REQ_SetProtocol,
324 .wValue = 1,
325 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
326 .wLength = 0,
327 };
328
329 if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
330 return ErrorCode;
331
332 HIDInterfaceInfo->State.UsingBootProtocol = false;
333 }
334
335 if (HIDInterfaceInfo->Config.HIDParserData == NULL)
336 return HID_ERROR_LOGICAL;
337
338 if ((ErrorCode = USB_ProcessHIDReport(HIDReportData, HIDInterfaceInfo->State.HIDReportSize,
339 HIDInterfaceInfo->Config.HIDParserData)) != HID_PARSE_Successful)
340 {
341 return HID_ERROR_LOGICAL | ErrorCode;
342 }
343
344 uint8_t LargestReportSizeBits = HIDInterfaceInfo->Config.HIDParserData->LargestReportSizeBits;
345 HIDInterfaceInfo->State.LargestReportSize = (LargestReportSizeBits >> 3) + ((LargestReportSizeBits & 0x07) != 0);
346
347 return 0;
348 }
349
350 #endif