Fix documentation reference error.
[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 if (PipeNum == HIDInterfaceInfo->Config.DataINPipeNumber)
99 {
100 Pipe_ConfigurePipe(PipeNum, EP_TYPE_INTERRUPT, PIPE_TOKEN_IN,
101 DataINEndpoint->EndpointAddress, DataINEndpoint->EndpointSize,
102 HIDInterfaceInfo->Config.DataINPipeDoubleBank ? PIPE_BANK_DOUBLE : PIPE_BANK_SINGLE);
103 Pipe_SetInterruptPeriod(DataINEndpoint->PollingIntervalMS);
104
105 HIDInterfaceInfo->State.DataINPipeSize = DataINEndpoint->EndpointSize;
106 }
107 else if (PipeNum == HIDInterfaceInfo->Config.DataOUTPipeNumber)
108 {
109 Pipe_ConfigurePipe(PipeNum, EP_TYPE_INTERRUPT, PIPE_TOKEN_OUT,
110 DataOUTEndpoint->EndpointAddress, DataOUTEndpoint->EndpointSize,
111 HIDInterfaceInfo->Config.DataOUTPipeDoubleBank ? PIPE_BANK_DOUBLE : PIPE_BANK_SINGLE);
112 Pipe_SetInterruptPeriod(DataOUTEndpoint->PollingIntervalMS);
113
114 HIDInterfaceInfo->State.DataOUTPipeSize = DataOUTEndpoint->EndpointSize;
115 HIDInterfaceInfo->State.DeviceUsesOUTPipe = true;
116 }
117 }
118
119 HIDInterfaceInfo->State.InterfaceNumber = HIDInterface->InterfaceNumber;
120 HIDInterfaceInfo->State.HIDReportSize = HIDDescriptor->HIDReportLength;
121 HIDInterfaceInfo->State.SupportsBootProtocol = (HIDInterface->SubClass != HID_CSCP_NonBootProtocol);
122 HIDInterfaceInfo->State.LargestReportSize = 8;
123 HIDInterfaceInfo->State.IsActive = true;
124
125 return HID_ENUMERROR_NoError;
126 }
127
128 static uint8_t DCOMP_HID_Host_NextHIDInterface(void* const CurrentDescriptor)
129 {
130 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
131
132 if (Header->Type == DTYPE_Interface)
133 {
134 USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
135
136 if (Interface->Class == HID_CSCP_HIDClass)
137 return DESCRIPTOR_SEARCH_Found;
138 }
139
140 return DESCRIPTOR_SEARCH_NotFound;
141 }
142
143 static uint8_t DCOMP_HID_Host_NextHID(void* const CurrentDescriptor)
144 {
145 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
146
147 if (Header->Type == HID_DTYPE_HID)
148 return DESCRIPTOR_SEARCH_Found;
149 else if (Header->Type == DTYPE_Interface)
150 return DESCRIPTOR_SEARCH_Fail;
151 else
152 return DESCRIPTOR_SEARCH_NotFound;
153 }
154
155 static uint8_t DCOMP_HID_Host_NextHIDInterfaceEndpoint(void* const CurrentDescriptor)
156 {
157 USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
158
159 if (Header->Type == DTYPE_Endpoint)
160 {
161 USB_Descriptor_Endpoint_t* Endpoint = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Endpoint_t);
162
163 if (!(Pipe_IsEndpointBound(Endpoint->EndpointAddress)))
164 return DESCRIPTOR_SEARCH_Found;
165 }
166 else if (Header->Type == DTYPE_Interface)
167 {
168 return DESCRIPTOR_SEARCH_Fail;
169 }
170
171 return DESCRIPTOR_SEARCH_NotFound;
172 }
173
174 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
175 uint8_t HID_Host_ReceiveReportByID(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo,
176 const uint8_t ReportID,
177 void* Buffer)
178 {
179 USB_ControlRequest = (USB_Request_Header_t)
180 {
181 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
182 .bRequest = HID_REQ_SetReport,
183 .wValue = ((HID_REPORT_ITEM_In + 1) << 8) | ReportID,
184 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
185 .wLength = USB_GetHIDReportSize(HIDInterfaceInfo->Config.HIDParserData, ReportID, HID_REPORT_ITEM_In),
186 };
187
188 Pipe_SelectPipe(PIPE_CONTROLPIPE);
189
190 return USB_Host_SendControlRequest(Buffer);
191 }
192 #endif
193
194 uint8_t HID_Host_ReceiveReport(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo,
195 void* Buffer)
196 {
197 if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.IsActive))
198 return PIPE_READYWAIT_DeviceDisconnected;
199
200 uint8_t ErrorCode;
201
202 Pipe_SelectPipe(HIDInterfaceInfo->Config.DataINPipeNumber);
203 Pipe_Unfreeze();
204
205 uint16_t ReportSize;
206 uint8_t* BufferPos = Buffer;
207
208 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
209 if (!(HIDInterfaceInfo->State.UsingBootProtocol))
210 {
211 uint8_t ReportID = 0;
212
213 if (HIDInterfaceInfo->Config.HIDParserData->UsingReportIDs)
214 {
215 ReportID = Pipe_Read_Byte();
216 *(BufferPos++) = ReportID;
217 }
218
219 ReportSize = USB_GetHIDReportSize(HIDInterfaceInfo->Config.HIDParserData, ReportID, HID_REPORT_ITEM_In);
220 }
221 else
222 #endif
223 {
224 ReportSize = Pipe_BytesInPipe();
225 }
226
227 if ((ErrorCode = Pipe_Read_Stream_LE(BufferPos, ReportSize, NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
228 return ErrorCode;
229
230 Pipe_ClearIN();
231 Pipe_Freeze();
232
233 return PIPE_RWSTREAM_NoError;
234 }
235
236 uint8_t HID_Host_SendReportByID(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo,
237 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
238 const uint8_t ReportID,
239 #endif
240 const uint8_t ReportType,
241 void* Buffer,
242 const uint16_t ReportSize)
243 {
244 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
245 if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.IsActive))
246 return false;
247
248 if (HIDInterfaceInfo->State.DeviceUsesOUTPipe && (ReportType == HID_REPORT_ITEM_Out))
249 {
250 uint8_t ErrorCode;
251
252 Pipe_SelectPipe(HIDInterfaceInfo->Config.DataOUTPipeNumber);
253 Pipe_Unfreeze();
254
255 if (ReportID)
256 Pipe_Write_Stream_LE(&ReportID, sizeof(ReportID), NO_STREAM_CALLBACK);
257
258 if ((ErrorCode = Pipe_Write_Stream_LE(Buffer, ReportSize, NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
259 return ErrorCode;
260
261 Pipe_ClearOUT();
262 Pipe_Freeze();
263
264 return PIPE_RWSTREAM_NoError;
265 }
266 else
267 #endif
268 {
269 USB_ControlRequest = (USB_Request_Header_t)
270 {
271 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
272 .bRequest = HID_REQ_SetReport,
273 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
274 .wValue = ((ReportType + 1) << 8) | ReportID,
275 #else
276 .wValue = ((ReportType + 1) << 8),
277 #endif
278 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
279 .wLength = ReportSize,
280 };
281
282 Pipe_SelectPipe(PIPE_CONTROLPIPE);
283
284 return USB_Host_SendControlRequest(Buffer);
285 }
286 }
287
288 bool HID_Host_IsReportReceived(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo)
289 {
290 if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.IsActive))
291 return false;
292
293 bool ReportReceived;
294
295 Pipe_SelectPipe(HIDInterfaceInfo->Config.DataINPipeNumber);
296 Pipe_Unfreeze();
297
298 ReportReceived = Pipe_IsINReceived();
299
300 Pipe_Freeze();
301
302 return ReportReceived;
303 }
304
305 uint8_t HID_Host_SetBootProtocol(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo)
306 {
307 uint8_t ErrorCode;
308
309 USB_ControlRequest = (USB_Request_Header_t)
310 {
311 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
312 .bRequest = HID_REQ_SetProtocol,
313 .wValue = 0,
314 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
315 .wLength = 0,
316 };
317
318 Pipe_SelectPipe(PIPE_CONTROLPIPE);
319
320 if (!(HIDInterfaceInfo->State.SupportsBootProtocol))
321 return HID_ERROR_LOGICAL;
322
323 if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
324 return ErrorCode;
325
326 HIDInterfaceInfo->State.LargestReportSize = 8;
327 HIDInterfaceInfo->State.UsingBootProtocol = true;
328
329 return HOST_SENDCONTROL_Successful;
330 }
331
332 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
333 uint8_t HID_Host_SetReportProtocol(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo)
334 {
335 uint8_t ErrorCode;
336
337 uint8_t HIDReportData[HIDInterfaceInfo->State.HIDReportSize];
338
339 USB_ControlRequest = (USB_Request_Header_t)
340 {
341 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_INTERFACE),
342 .bRequest = REQ_GetDescriptor,
343 .wValue = (HID_DTYPE_Report << 8),
344 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
345 .wLength = HIDInterfaceInfo->State.HIDReportSize,
346 };
347
348 Pipe_SelectPipe(PIPE_CONTROLPIPE);
349
350 if ((ErrorCode = USB_Host_SendControlRequest(HIDReportData)) != HOST_SENDCONTROL_Successful)
351 return ErrorCode;
352
353 if (HIDInterfaceInfo->State.UsingBootProtocol)
354 {
355 USB_ControlRequest = (USB_Request_Header_t)
356 {
357 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
358 .bRequest = HID_REQ_SetProtocol,
359 .wValue = 1,
360 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
361 .wLength = 0,
362 };
363
364 if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
365 return ErrorCode;
366
367 HIDInterfaceInfo->State.UsingBootProtocol = false;
368 }
369
370 if (HIDInterfaceInfo->Config.HIDParserData == NULL)
371 return HID_ERROR_LOGICAL;
372
373 if ((ErrorCode = USB_ProcessHIDReport(HIDReportData, HIDInterfaceInfo->State.HIDReportSize,
374 HIDInterfaceInfo->Config.HIDParserData)) != HID_PARSE_Successful)
375 {
376 return HID_ERROR_LOGICAL | ErrorCode;
377 }
378
379 uint8_t LargestReportSizeBits = HIDInterfaceInfo->Config.HIDParserData->LargestReportSizeBits;
380 HIDInterfaceInfo->State.LargestReportSize = (LargestReportSizeBits >> 3) + ((LargestReportSizeBits & 0x07) != 0);
381
382 return 0;
383 }
384 #endif
385
386 #endif
387