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