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