Finish initial draft of the Host Mode HID Class driver.
[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 uint8_t HID_Host_ConfigurePipes(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo, uint16_t ConfigDescriptorSize,
38 uint8_t* ConfigDescriptorData)
39 {
40 uint8_t FoundEndpoints = 0;
41
42 memset(&HIDInterfaceInfo->State, 0x00, sizeof(HIDInterfaceInfo->State));
43
44 if (DESCRIPTOR_TYPE(ConfigDescriptorData) != DTYPE_Configuration)
45 return HID_ENUMERROR_InvalidConfigDescriptor;
46
47 USB_Descriptor_Interface_t* CurrentHIDInterface;
48
49 do
50 {
51 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
52 DComp_HID_Host_NextHIDInterface) != DESCRIPTOR_SEARCH_COMP_Found)
53 {
54 return HID_ENUMERROR_NoHIDInterfaceFound;
55 }
56
57 CurrentHIDInterface = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Interface_t);
58 } while (HIDInterfaceInfo->Config.HIDInterfaceProtocol &&
59 (CurrentHIDInterface->Protocol != HIDInterfaceInfo->Config.HIDInterfaceProtocol));
60
61 HIDInterfaceInfo->State.InterfaceNumber = CurrentHIDInterface->InterfaceNumber;
62 HIDInterfaceInfo->State.SupportsBootProtocol = (CurrentHIDInterface->SubClass != HID_NON_BOOT_PROTOCOL);
63
64 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData, DComp_NextHID) != DESCRIPTOR_SEARCH_COMP_Found)
65 {
66 return HID_ENUMERROR_NoHIDDescriptorFound;
67 }
68
69 HIDInterfaceInfo->State.HIDReportSize = DESCRIPTOR_CAST(ConfigDescriptorData, USB_HID_Descriptor_t).HIDReportLength;
70
71 while (FoundEndpoints != (HID_FOUND_DATAPIPE_IN | HID_FOUND_DATAPIPE_OUT))
72 {
73 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize, &ConfigDescriptorData,
74 DComp_HID_Host_NextHIDInterfaceEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
75 {
76 if (FoundEndpoints & HID_FOUND_DATAPIPE_IN)
77 break;
78
79 return HID_ENUMERROR_EndpointsNotFound;
80 }
81
82 USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(ConfigDescriptorData, USB_Descriptor_Endpoint_t);
83
84 if (EndpointData->EndpointAddress & ENDPOINT_DESCRIPTOR_DIR_IN)
85 {
86 Pipe_ConfigurePipe(HIDInterfaceInfo->Config.DataINPipeNumber, EP_TYPE_INTERRUPT, PIPE_TOKEN_IN,
87 EndpointData->EndpointAddress, EndpointData->EndpointSize, PIPE_BANK_SINGLE);
88 HIDInterfaceInfo->State.DataINPipeSize = EndpointData->EndpointSize;
89
90 FoundEndpoints |= HID_FOUND_DATAPIPE_IN;
91 }
92 else
93 {
94 Pipe_ConfigurePipe(HIDInterfaceInfo->Config.DataOUTPipeNumber, EP_TYPE_INTERRUPT, PIPE_TOKEN_OUT,
95 EndpointData->EndpointAddress, EndpointData->EndpointSize, PIPE_BANK_SINGLE);
96 HIDInterfaceInfo->State.DataOUTPipeSize = EndpointData->EndpointSize;
97
98 HIDInterfaceInfo->State.DeviceUsesOUTPipe = true;
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* const HIDInterfaceInfo)
151 {
152
153 }
154
155 uint8_t HID_Host_ReceiveReportByID(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo, const uint8_t ReportID, void* Buffer)
156 {
157 USB_ControlRequest = (USB_Request_Header_t)
158 {
159 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
160 .bRequest = REQ_SetReport,
161 .wValue = ReportID,
162 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
163 .wLength = USB_GetHIDReportSize(HIDInterfaceInfo->Config.HIDParserData, ReportID, REPORT_ITEM_TYPE_In),
164 };
165
166 Pipe_SelectPipe(PIPE_CONTROLPIPE);
167
168 return USB_Host_SendControlRequest(Buffer);
169 }
170
171 uint8_t HID_Host_ReceiveReport(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo, void* Buffer)
172 {
173 if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.IsActive))
174 return false;
175
176 uint8_t ErrorCode;
177
178 Pipe_SelectPipe(HIDInterfaceInfo->Config.DataINPipeNumber);
179 Pipe_Unfreeze();
180
181 uint16_t ReportSize;
182
183 if (HIDInterfaceInfo->State.UsingBootProtocol)
184 {
185 ReportSize = Pipe_BytesInPipe();
186 }
187 else
188 {
189 uint8_t ReportID = 0;
190
191 if (HIDInterfaceInfo->Config.HIDParserData->UsingReportIDs)
192 {
193 ReportID = Pipe_Read_Byte();
194 *((uint8_t*)Buffer++) = ReportID;
195 }
196
197 ReportSize = USB_GetHIDReportSize(HIDInterfaceInfo->Config.HIDParserData, ReportID, REPORT_ITEM_TYPE_In);
198 }
199
200 if ((ErrorCode = Pipe_Read_Stream_LE(Buffer, ReportSize, NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
201 return ErrorCode;
202
203 Pipe_ClearIN();
204 Pipe_Freeze();
205
206 return PIPE_RWSTREAM_NoError;
207 }
208
209 uint8_t HID_Host_SendReportByID(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo, uint8_t ReportID, void* Buffer,
210 const uint16_t ReportSize)
211 {
212 if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.IsActive))
213 return false;
214
215 if (HIDInterfaceInfo->State.DeviceUsesOUTPipe)
216 {
217 USB_ControlRequest = (USB_Request_Header_t)
218 {
219 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
220 .bRequest = REQ_SetReport,
221 .wValue = ReportID,
222 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
223 .wLength = ReportSize,
224 };
225
226 Pipe_SelectPipe(PIPE_CONTROLPIPE);
227
228 return USB_Host_SendControlRequest(Buffer);
229 }
230 else
231 {
232 uint8_t ErrorCode;
233
234 Pipe_SelectPipe(HIDInterfaceInfo->Config.DataOUTPipeNumber);
235 Pipe_Unfreeze();
236
237 if (ReportID)
238 Pipe_Write_Stream_LE(&ReportID, sizeof(ReportID), NO_STREAM_CALLBACK);
239
240 if ((ErrorCode = Pipe_Write_Stream_LE(Buffer, ReportSize, NO_STREAM_CALLBACK)) != PIPE_RWSTREAM_NoError)
241 return ErrorCode;
242
243 Pipe_ClearOUT();
244 Pipe_Freeze();
245
246 return PIPE_RWSTREAM_NoError;
247 }
248 }
249
250 bool HID_Host_IsReportReceived(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo)
251 {
252 if ((USB_HostState != HOST_STATE_Configured) || !(HIDInterfaceInfo->State.IsActive))
253 return false;
254
255 bool ReportReceived;
256
257 Pipe_SelectPipe(HIDInterfaceInfo->Config.DataINPipeNumber);
258 Pipe_Unfreeze();
259
260 ReportReceived = Pipe_IsINReceived();
261
262 Pipe_Freeze();
263
264 return ReportReceived;
265 }
266
267 uint8_t USB_HID_Host_SetBootProtocol(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo)
268 {
269 if (HIDInterfaceInfo->State.UsingBootProtocol)
270 return HOST_SENDCONTROL_Successful;
271
272 uint8_t ErrorCode;
273
274 USB_ControlRequest = (USB_Request_Header_t)
275 {
276 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
277 .bRequest = REQ_SetProtocol,
278 .wValue = 0,
279 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
280 .wLength = 0,
281 };
282
283 Pipe_SelectPipe(PIPE_CONTROLPIPE);
284
285 if (!(HIDInterfaceInfo->State.SupportsBootProtocol))
286 return HID_ERROR_LOGICAL;
287
288 if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
289 return ErrorCode;
290
291 HIDInterfaceInfo->State.UsingBootProtocol = true;
292
293 return HOST_SENDCONTROL_Successful;
294 }
295
296 uint8_t USB_HID_Host_SetReportProtocol(USB_ClassInfo_HID_Host_t* const HIDInterfaceInfo)
297 {
298 uint8_t ErrorCode;
299
300 uint8_t HIDReportData[HIDInterfaceInfo->State.HIDReportSize];
301
302 USB_ControlRequest = (USB_Request_Header_t)
303 {
304 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_STANDARD | REQREC_INTERFACE),
305 .bRequest = REQ_GetDescriptor,
306 .wValue = (DTYPE_Report << 8),
307 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
308 .wLength = HIDInterfaceInfo->State.HIDReportSize,
309 };
310
311 Pipe_SelectPipe(PIPE_CONTROLPIPE);
312
313 if ((ErrorCode = USB_Host_SendControlRequest(HIDReportData)) != HOST_SENDCONTROL_Successful)
314 return ErrorCode;
315
316 if (HIDInterfaceInfo->State.UsingBootProtocol)
317 {
318 USB_ControlRequest = (USB_Request_Header_t)
319 {
320 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
321 .bRequest = REQ_SetProtocol,
322 .wValue = 1,
323 .wIndex = HIDInterfaceInfo->State.InterfaceNumber,
324 .wLength = 0,
325 };
326
327 if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
328 return ErrorCode;
329
330 HIDInterfaceInfo->State.UsingBootProtocol = false;
331 }
332
333 if (HIDInterfaceInfo->Config.HIDParserData == NULL)
334 return HID_ERROR_LOGICAL;
335
336 if ((ErrorCode = USB_ProcessHIDReport(HIDReportData, HIDInterfaceInfo->State.HIDReportSize,
337 HIDInterfaceInfo->Config.HIDParserData)) != HID_PARSE_Successful)
338 {
339 return HID_ERROR_LOGICAL | ErrorCode;
340 }
341
342 return 0;
343 }
344
345 #endif