Add const qualifiers to Host mode Class drivers.
[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* const 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.SupportsBootProtocol = (CurrentHIDInterface->SubClass != HID_NON_BOOT_PROTOCOL);
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 & 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 HIDInterfaceInfo->State.DeviceUsesOUTPipe = true;
101
102 FoundEndpoints |= HID_FOUND_DATAPIPE_OUT;
103 }
104 }
105
106 HIDInterfaceInfo->State.IsActive = true;
107 return HID_ENUMERROR_NoError;
108 }
109
110 static uint8_t DComp_HID_Host_NextHIDInterface(void* CurrentDescriptor)
111 {
112 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
113 {
114 USB_Descriptor_Interface_t* CurrentInterface = DESCRIPTOR_PCAST(CurrentDescriptor,
115 USB_Descriptor_Interface_t);
116
117 if (CurrentInterface->Class == HID_INTERFACE_CLASS)
118 return DESCRIPTOR_SEARCH_Found;
119 }
120
121 return DESCRIPTOR_SEARCH_NotFound;
122 }
123
124 static uint8_t DComp_NextHID(void* CurrentDescriptor)
125 {
126 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_HID)
127 return DESCRIPTOR_SEARCH_Found;
128 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
129 return DESCRIPTOR_SEARCH_Fail;
130 else
131 return DESCRIPTOR_SEARCH_NotFound;
132 }
133
134 static uint8_t DComp_HID_Host_NextHIDInterfaceEndpoint(void* CurrentDescriptor)
135 {
136 if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Endpoint)
137 {
138 USB_Descriptor_Endpoint_t* CurrentEndpoint = DESCRIPTOR_PCAST(CurrentDescriptor,
139 USB_Descriptor_Endpoint_t);
140
141 if (!(Pipe_IsEndpointBound(CurrentEndpoint->EndpointAddress)))
142 return DESCRIPTOR_SEARCH_Found;
143 }
144 else if (DESCRIPTOR_TYPE(CurrentDescriptor) == DTYPE_Interface)
145 {
146 return DESCRIPTOR_SEARCH_Fail;
147 }
148
149 return DESCRIPTOR_SEARCH_NotFound;
150 }
151
152 void HID_Host_USBTask(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo)
153 {
154
155 }
156
157 uint8_t HID_Host_ReceiveReport(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo, const bool ControlRequest,
158 uint8_t* ReportID, void* Buffer)
159 {
160 if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.IsActive))
161 return false;
162
163 if (ControlRequest)
164 {
165 USB_ControlRequest = (USB_Request_Header_t)
166 {
167 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
168 .bRequest = REQ_SetReport,
169 .wValue = *ReportID,
170 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
171 .wLength = USB_GetHIDReportSize(HIDInterfaceInfo->Config.HIDParserData, *ReportID, REPORT_ITEM_TYPE_In),
172 };
173
174 Pipe_SelectPipe(PIPE_CONTROLPIPE);
175
176 return USB_Host_SendControlRequest(Buffer);
177 }
178 else
179 {
180 uint8_t ErrorCode;
181
182 Pipe_SelectPipe(HIDInterfaceInfo->Config.DataINPipeNumber);
183 Pipe_Unfreeze();
184
185 uint16_t ReportSize;
186
187 if (HIDInterfaceInfo->State.UsingBootProtocol)
188 {
189 ReportSize = Pipe_BytesInPipe();
190 }
191 else
192 {
193 if (HIDInterfaceInfo->Config.HIDParserData->UsingReportIDs)
194 *ReportID = Pipe_Read_Byte();
195 else
196 *ReportID = 0;
197
198 ReportSize = USB_GetHIDReportSize(HIDInterfaceInfo->Config.HIDParserData, *ReportID, REPORT_ITEM_TYPE_In);
199 }
200
201 if ((ErrorCode = Pipe_Read_Stream_LE(Buffer, ReportSize, NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
202 return ErrorCode;
203
204 Pipe_ClearIN();
205 Pipe_Freeze();
206
207 return PIPE_RWSTREAM_NoError;
208 }
209 }
210
211 uint8_t HID_Host_SendReport(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo, uint8_t ReportID, void* Buffer,
212 const uint16_t ReportSize)
213 {
214 if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.IsActive))
215 return false;
216
217 if (HIDInterfaceInfo->State.DeviceUsesOUTPipe)
218 {
219 USB_ControlRequest = (USB_Request_Header_t)
220 {
221 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
222 .bRequest = REQ_SetReport,
223 .wValue = ReportID,
224 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
225 .wLength = ReportSize,
226 };
227
228 Pipe_SelectPipe(PIPE_CONTROLPIPE);
229
230 return USB_Host_SendControlRequest(Buffer);
231 }
232 else
233 {
234 uint8_t ErrorCode;
235
236 Pipe_SelectPipe(HIDInterfaceInfo->Config.DataOUTPipeNumber);
237 Pipe_Unfreeze();
238
239 if (ReportID)
240 Pipe_Write_Stream_LE(&ReportID, sizeof(ReportID), NO_STREAM_CALLBACK);
241
242 if ((ErrorCode = Pipe_Write_Stream_LE(Buffer, ReportSize, NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
243 return ErrorCode;
244
245 Pipe_ClearOUT();
246 Pipe_Freeze();
247
248 return PIPE_RWSTREAM_NoError;
249 }
250 }
251
252 bool HID_Host_IsReportReceived(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo)
253 {
254 if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.IsActive))
255 return false;
256
257 bool ReportReceived;
258
259 Pipe_SelectPipe(HIDInterfaceInfo->Config.DataINPipeNumber);
260 Pipe_Unfreeze();
261
262 ReportReceived = Pipe_IsINReceived();
263
264 Pipe_Freeze();
265
266 return ReportReceived;
267 }
268
269 uint8_t USB_HID_Host_SetBootProtocol(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo)
270 {
271 uint8_t ErrorCode;
272
273 USB_ControlRequest = (USB_Request_Header_t)
274 {
275 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
276 .bRequest = REQ_SetProtocol,
277 .wValue = 0,
278 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
279 .wLength = 0,
280 };
281
282 Pipe_SelectPipe(PIPE_CONTROLPIPE);
283
284 if (!(HIDInterfaceInfo->State.SupportsBootProtocol))
285 return HID_ERROR_LOGICAL;
286
287 if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
288 return ErrorCode;
289
290 HIDInterfaceInfo->State.UsingBootProtocol = true;
291
292 return HOST_SENDCONTROL_Successful;
293 }
294
295 uint8_t USB_HID_Host_SetReportProtocol(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo)
296 {
297 uint8_t ErrorCode;
298
299 uint8_t HIDReportData[HIDInterfaceInfo->State.HIDReportSize];
300
301 USB_ControlRequest = (USB_Request_Header_t)
302 {
303 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_INTERFACE),
304 .bRequest = REQ_GetDescriptor,
305 .wValue = (DTYPE_Report << 8),
306 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
307 .wLength = HIDInterfaceInfo->State.HIDReportSize,
308 };
309
310 Pipe_SelectPipe(PIPE_CONTROLPIPE);
311
312 if ((ErrorCode = USB_Host_SendControlRequest(HIDReportData)) != HOST_SENDCONTROL_Successful)
313 return ErrorCode;
314
315 USB_ControlRequest = (USB_Request_Header_t)
316 {
317 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
318 .bRequest = REQ_SetProtocol,
319 .wValue = 1,
320 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
321 .wLength = 0,
322 };
323
324 if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
325 return ErrorCode;
326
327 HIDInterfaceInfo->State.UsingBootProtocol = false;
328
329 if (HIDInterfaceInfo->Config.HIDParserData == NULL)
330 return HID_ERROR_LOGICAL;
331
332 if ((ErrorCode = USB_ProcessHIDReport(HIDReportData, HIDInterfaceInfo->State.HIDReportSize,
333 HIDInterfaceInfo->Config.HIDParserData)) != HID_PARSE_Successful)
334 {
335 return HID_ERROR_LOGICAL | ErrorCode;
336 }
337
338 return 0;
339 }
340
341 #endif