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