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