Added support for the officially recommended layout of the external peripherals conne...
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Host / HID.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 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 #include "../../HighLevel/USBMode.h"
32 #if defined(USB_CAN_BE_HOST)
33
34 #define INCLUDE_FROM_HID_CLASS_HOST_C
35 #include "HID.h"
36
37 #warning The HID Host mode Class driver is currently incomplete and is for preview purposes only.
38
39 uint8_t HID_Host_ConfigurePipes(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo, uint16_t ConfigDescriptorSize,
40 uint8_t* 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.SupportsBootSubClass = (CurrentHIDInterface->SubClass != 0);
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 == (1 << 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, PIPE_BANK_SINGLE);
90 HIDInterfaceInfo->State.DataINPipeSize = EndpointData->EndpointSize;
91
92 FoundEndpoints |= HID_FOUND_DATAPIPE_IN;
93 }
94 else
95 {
96 Pipe_ConfigurePipe(HIDInterfaceInfo->Config.DataOUTPipeNumber, EP_TYPE_INTERRUPT, PIPE_TOKEN_OUT,
97 EndpointData->EndpointAddress, EndpointData->EndpointSize, PIPE_BANK_SINGLE);
98 HIDInterfaceInfo->State.DataOUTPipeSize = EndpointData->EndpointSize;
99
100 FoundEndpoints |= HID_FOUND_DATAPIPE_OUT;
101 }
102 }
103
104 HIDInterfaceInfo->State.IsActive = true;
105 return HID_ENUMERROR_NoError;
106 }
107
108 static uint8_t DComp_HID_Host_NextHIDInterface(void* CurrentDescriptor)
109 {
110 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
111 {
112 USB_Descriptor_Interface_t* CurrentInterface = DESCRIPTOR_PCAST(CurrentDescriptor,
113 USB_Descriptor_Interface_t);
114
115 if (CurrentInterface->Class == HID_INTERFACE_CLASS)
116 return DESCRIPTOR_SEARCH_Found;
117 }
118
119 return DESCRIPTOR_SEARCH_NotFound;
120 }
121
122 static uint8_t DComp_NextHID(void* CurrentDescriptor)
123 {
124 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_HID)
125 return DESCRIPTOR_SEARCH_Found;
126 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
127 return DESCRIPTOR_SEARCH_Fail;
128 else
129 return DESCRIPTOR_SEARCH_NotFound;
130 }
131
132 static uint8_t DComp_HID_Host_NextHIDInterfaceEndpoint(void* CurrentDescriptor)
133 {
134 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
135 {
136 USB_Descriptor_Endpoint_t* CurrentEndpoint = DESCRIPTOR_PCAST(CurrentDescriptor,
137 USB_Descriptor_Endpoint_t);
138
139 if (!(Pipe_IsEndpointBound(CurrentEndpoint->EndpointAddress)))
140 return DESCRIPTOR_SEARCH_Found;
141 }
142 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
143 {
144 return DESCRIPTOR_SEARCH_Fail;
145 }
146
147 return DESCRIPTOR_SEARCH_NotFound;
148 }
149
150 void HID_Host_USBTask(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo)
151 {
152
153 }
154
155 bool HID_Host_IsReportReceived(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo)
156 {
157 if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.IsActive))
158 return false;
159
160 bool ReportReceived;
161
162 Pipe_SelectPipe(HIDInterfaceInfo->Config.DataINPipeNumber);
163 Pipe_Unfreeze();
164
165 ReportReceived = Pipe_IsReadWriteAllowed();
166
167 Pipe_Freeze();
168
169 return ReportReceived;
170 }
171
172 uint8_t USB_HID_Host_SetBootProtocol(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo)
173 {
174 if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.IsActive))
175 return false;
176
177 USB_ControlRequest = (USB_Request_Header_t)
178 {
179 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
180 .bRequest = REQ_SetProtocol,
181 .wValue = 1,
182 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
183 .wLength = 0,
184 };
185
186 Pipe_SelectPipe(PIPE_CONTROLPIPE);
187
188 if (!(HIDInterfaceInfo->State.SupportsBootProtocol))
189 return HID_ERROR_LOGICAL;
190
191 if ((ErrorCode = USB_Host_SendControlRequest(HIDReportData)) != HOST_SENDCONTROL_Successful)
192 return ErrorCode;
193
194 HIDInterfaceInfo->State.UsingBootProtocol = true;
195
196 return HOST_SENDCONTROL_Successful;
197 }
198
199 uint8_t USB_HID_Host_SetReportProtocol(USB_ClassInfo_HID_Host_t* HIDInterfaceInfo)
200 {
201 if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.IsActive))
202 return false;
203
204 uint8_t ErrorCode;
205
206 uint8_t HIDReportData[HIDInterfaceInfo->State.HIDReportSize];
207
208 USB_ControlRequest = (USB_Request_Header_t)
209 {
210 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_INTERFACE),
211 .bRequest = REQ_GetDescriptor,
212 .wValue = (DTYPE_Report << 8),
213 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
214 .wLength = HIDInterfaceInfo->State.HIDReportSize,
215 };
216
217 Pipe_SelectPipe(PIPE_CONTROLPIPE);
218
219 if ((ErrorCode = USB_Host_SendControlRequest(HIDReportData)) != HOST_SENDCONTROL_Successful)
220 return ErrorCode;
221
222 USB_ControlRequest = (USB_Request_Header_t)
223 {
224 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
225 .bRequest = REQ_SetProtocol,
226 .wValue = 0,
227 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
228 .wLength = 0,
229 };
230
231 if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
232 return ErrorCode;
233
234 HIDInterfaceInfo->State.UsingBootProtocol = false;
235
236 if (HIDInterfaceInfo->Config.HIDParserData == NULL)
237 return HID_ERROR_LOGICAL;
238
239 if ((ErrorCode = USB_ProcessHIDReport(HIDReportData, HIDInterfaceInfo->State.HIDReportSize,
240 HIDInterfaceInfo->Config.HIDParserData)) != HID_PARSE_Successful)
241 {
242 return HID_ERROR_LOGICAL | ErrorCode;
243 }
244
245 return 0;
246 }
247
248 #endif