3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
31 #include "../../HighLevel/USBMode.h"
32 #if defined(USB_CAN_BE_HOST)
34 #define INCLUDE_FROM_HID_CLASS_HOST_C
37 uint8_t HID_Host_ConfigurePipes(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
, uint16_t ConfigDescriptorSize
,
38 uint8_t* ConfigDescriptorData
)
40 uint8_t FoundEndpoints
= 0;
42 memset(&HIDInterfaceInfo
->State
, 0x00, sizeof(HIDInterfaceInfo
->State
));
44 if (DESCRIPTOR_TYPE(ConfigDescriptorData
) != DTYPE_Configuration
)
45 return HID_ENUMERROR_InvalidConfigDescriptor
;
47 USB_Descriptor_Interface_t
* CurrentHIDInterface
;
51 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
52 DComp_HID_Host_NextHIDInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
54 return HID_ENUMERROR_NoHIDInterfaceFound
;
57 CurrentHIDInterface
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Interface_t
);
58 } while (HIDInterfaceInfo
->Config
.HIDInterfaceProtocol
&&
59 (CurrentHIDInterface
->Protocol
!= HIDInterfaceInfo
->Config
.HIDInterfaceProtocol
));
61 HIDInterfaceInfo
->State
.InterfaceNumber
= CurrentHIDInterface
->InterfaceNumber
;
62 HIDInterfaceInfo
->State
.SupportsBootProtocol
= (CurrentHIDInterface
->SubClass
!= HID_NON_BOOT_PROTOCOL
);
64 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
, DComp_NextHID
) != DESCRIPTOR_SEARCH_COMP_Found
)
66 return HID_ENUMERROR_NoHIDDescriptorFound
;
69 HIDInterfaceInfo
->State
.HIDReportSize
= DESCRIPTOR_CAST(ConfigDescriptorData
, USB_HID_Descriptor_t
).HIDReportLength
;
71 while (FoundEndpoints
!= (HID_FOUND_DATAPIPE_IN
| HID_FOUND_DATAPIPE_OUT
))
73 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
74 DComp_HID_Host_NextHIDInterfaceEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
)
76 if (FoundEndpoints
& HID_FOUND_DATAPIPE_IN
)
79 return HID_ENUMERROR_EndpointsNotFound
;
82 USB_Descriptor_Endpoint_t
* EndpointData
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Endpoint_t
);
84 if (EndpointData
->EndpointAddress
& ENDPOINT_DESCRIPTOR_DIR_IN
)
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
;
90 FoundEndpoints
|= HID_FOUND_DATAPIPE_IN
;
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
;
98 HIDInterfaceInfo
->State
.DeviceUsesOUTPipe
= true;
100 FoundEndpoints
|= HID_FOUND_DATAPIPE_OUT
;
104 HIDInterfaceInfo
->State
.LargestReportSize
= 8;
105 HIDInterfaceInfo
->State
.IsActive
= true;
106 return HID_ENUMERROR_NoError
;
109 static uint8_t DComp_HID_Host_NextHIDInterface(void* const CurrentDescriptor
)
111 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
113 USB_Descriptor_Interface_t
* CurrentInterface
= DESCRIPTOR_PCAST(CurrentDescriptor
,
114 USB_Descriptor_Interface_t
);
116 if (CurrentInterface
->Class
== HID_INTERFACE_CLASS
)
117 return DESCRIPTOR_SEARCH_Found
;
120 return DESCRIPTOR_SEARCH_NotFound
;
123 static uint8_t DComp_NextHID(void* const CurrentDescriptor
)
125 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_HID
)
126 return DESCRIPTOR_SEARCH_Found
;
127 else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
128 return DESCRIPTOR_SEARCH_Fail
;
130 return DESCRIPTOR_SEARCH_NotFound
;
133 static uint8_t DComp_HID_Host_NextHIDInterfaceEndpoint(void* const CurrentDescriptor
)
135 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
)
137 USB_Descriptor_Endpoint_t
* CurrentEndpoint
= DESCRIPTOR_PCAST(CurrentDescriptor
,
138 USB_Descriptor_Endpoint_t
);
140 if (!(Pipe_IsEndpointBound(CurrentEndpoint
->EndpointAddress
)))
141 return DESCRIPTOR_SEARCH_Found
;
143 else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
145 return DESCRIPTOR_SEARCH_Fail
;
148 return DESCRIPTOR_SEARCH_NotFound
;
151 void HID_Host_USBTask(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
)
156 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
157 uint8_t HID_Host_ReceiveReportByID(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
, const uint8_t ReportID
, void* Buffer
)
159 USB_ControlRequest
= (USB_Request_Header_t
)
161 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
162 .bRequest
= REQ_SetReport
,
164 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
165 .wLength
= USB_GetHIDReportSize(HIDInterfaceInfo
->Config
.HIDParserData
, ReportID
, REPORT_ITEM_TYPE_In
),
168 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
170 return USB_Host_SendControlRequest(Buffer
);
174 uint8_t HID_Host_ReceiveReport(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
, void* Buffer
)
176 if ((USB_HostState
!= HOST_STATE_Configured
) || !(HIDInterfaceInfo
->State
.IsActive
))
181 Pipe_SelectPipe(HIDInterfaceInfo
->Config
.DataINPipeNumber
);
186 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
187 if (!(HIDInterfaceInfo
->State
.UsingBootProtocol
))
189 uint8_t ReportID
= 0;
191 if (HIDInterfaceInfo
->Config
.HIDParserData
->UsingReportIDs
)
193 ReportID
= Pipe_Read_Byte();
194 *((uint8_t*)Buffer
++) = ReportID
;
197 ReportSize
= USB_GetHIDReportSize(HIDInterfaceInfo
->Config
.HIDParserData
, ReportID
, REPORT_ITEM_TYPE_In
);
202 ReportSize
= Pipe_BytesInPipe();
205 if ((ErrorCode
= Pipe_Read_Stream_LE(Buffer
, ReportSize
, NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
211 return PIPE_RWSTREAM_NoError
;
214 uint8_t HID_Host_SendReportByID(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
,
215 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
216 const uint8_t ReportID
,
218 void* Buffer
, const uint16_t ReportSize
)
220 if ((USB_HostState
!= HOST_STATE_Configured
) || !(HIDInterfaceInfo
->State
.IsActive
))
223 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
224 if (HIDInterfaceInfo
->State
.DeviceUsesOUTPipe
)
228 Pipe_SelectPipe(HIDInterfaceInfo
->Config
.DataOUTPipeNumber
);
232 Pipe_Write_Stream_LE(&ReportID
, sizeof(ReportID
), NO_STREAM_CALLBACK
);
234 if ((ErrorCode
= Pipe_Write_Stream_LE(Buffer
, ReportSize
, NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
240 return PIPE_RWSTREAM_NoError
;
245 USB_ControlRequest
= (USB_Request_Header_t
)
247 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
248 .bRequest
= REQ_SetReport
,
249 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
254 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
255 .wLength
= ReportSize
,
258 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
260 return USB_Host_SendControlRequest(Buffer
);
264 bool HID_Host_IsReportReceived(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
)
266 if ((USB_HostState
!= HOST_STATE_Configured
) || !(HIDInterfaceInfo
->State
.IsActive
))
271 Pipe_SelectPipe(HIDInterfaceInfo
->Config
.DataINPipeNumber
);
274 ReportReceived
= Pipe_IsINReceived();
278 return ReportReceived
;
281 uint8_t HID_Host_SetBootProtocol(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
)
283 if (HIDInterfaceInfo
->State
.UsingBootProtocol
)
284 return HOST_SENDCONTROL_Successful
;
288 USB_ControlRequest
= (USB_Request_Header_t
)
290 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
291 .bRequest
= REQ_SetProtocol
,
293 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
297 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
299 if (!(HIDInterfaceInfo
->State
.SupportsBootProtocol
))
300 return HID_ERROR_LOGICAL
;
302 if ((ErrorCode
= USB_Host_SendControlRequest(NULL
)) != HOST_SENDCONTROL_Successful
)
305 HIDInterfaceInfo
->State
.LargestReportSize
= 8;
306 HIDInterfaceInfo
->State
.UsingBootProtocol
= true;
308 return HOST_SENDCONTROL_Successful
;
311 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
312 uint8_t HID_Host_SetReportProtocol(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
)
316 uint8_t HIDReportData
[HIDInterfaceInfo
->State
.HIDReportSize
];
318 USB_ControlRequest
= (USB_Request_Header_t
)
320 .bmRequestType
= (REQDIR_DEVICETOHOST
| REQTYPE_STANDARD
| REQREC_INTERFACE
),
321 .bRequest
= REQ_GetDescriptor
,
322 .wValue
= (DTYPE_Report
<< 8),
323 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
324 .wLength
= HIDInterfaceInfo
->State
.HIDReportSize
,
327 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
329 if ((ErrorCode
= USB_Host_SendControlRequest(HIDReportData
)) != HOST_SENDCONTROL_Successful
)
332 if (HIDInterfaceInfo
->State
.UsingBootProtocol
)
334 USB_ControlRequest
= (USB_Request_Header_t
)
336 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
337 .bRequest
= REQ_SetProtocol
,
339 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
343 if ((ErrorCode
= USB_Host_SendControlRequest(NULL
)) != HOST_SENDCONTROL_Successful
)
346 HIDInterfaceInfo
->State
.UsingBootProtocol
= false;
349 if (HIDInterfaceInfo
->Config
.HIDParserData
== NULL
)
350 return HID_ERROR_LOGICAL
;
352 if ((ErrorCode
= USB_ProcessHIDReport(HIDReportData
, HIDInterfaceInfo
->State
.HIDReportSize
,
353 HIDInterfaceInfo
->Config
.HIDParserData
)) != HID_PARSE_Successful
)
355 return HID_ERROR_LOGICAL
| ErrorCode
;
358 uint8_t LargestReportSizeBits
= HIDInterfaceInfo
->Config
.HIDParserData
->LargestReportSizeBits
;
359 HIDInterfaceInfo
->State
.LargestReportSize
= (LargestReportSizeBits
>> 3) + ((LargestReportSizeBits
& 0x07) != 0);