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
.IsActive
= true;
105 return HID_ENUMERROR_NoError
;
108 static uint8_t DComp_HID_Host_NextHIDInterface(void* CurrentDescriptor
)
110 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
112 USB_Descriptor_Interface_t
* CurrentInterface
= DESCRIPTOR_PCAST(CurrentDescriptor
,
113 USB_Descriptor_Interface_t
);
115 if (CurrentInterface
->Class
== HID_INTERFACE_CLASS
)
116 return DESCRIPTOR_SEARCH_Found
;
119 return DESCRIPTOR_SEARCH_NotFound
;
122 static uint8_t DComp_NextHID(void* CurrentDescriptor
)
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
;
129 return DESCRIPTOR_SEARCH_NotFound
;
132 static uint8_t DComp_HID_Host_NextHIDInterfaceEndpoint(void* CurrentDescriptor
)
134 if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
)
136 USB_Descriptor_Endpoint_t
* CurrentEndpoint
= DESCRIPTOR_PCAST(CurrentDescriptor
,
137 USB_Descriptor_Endpoint_t
);
139 if (!(Pipe_IsEndpointBound(CurrentEndpoint
->EndpointAddress
)))
140 return DESCRIPTOR_SEARCH_Found
;
142 else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
)
144 return DESCRIPTOR_SEARCH_Fail
;
147 return DESCRIPTOR_SEARCH_NotFound
;
150 void HID_Host_USBTask(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
)
155 uint8_t HID_Host_ReceiveReportByID(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
, const uint8_t ReportID
, void* Buffer
)
157 USB_ControlRequest
= (USB_Request_Header_t
)
159 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
160 .bRequest
= REQ_SetReport
,
162 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
163 .wLength
= USB_GetHIDReportSize(HIDInterfaceInfo
->Config
.HIDParserData
, ReportID
, REPORT_ITEM_TYPE_In
),
166 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
168 return USB_Host_SendControlRequest(Buffer
);
171 uint8_t HID_Host_ReceiveReport(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
, void* Buffer
)
173 if ((USB_HostState
!= HOST_STATE_Configured
) || !(HIDInterfaceInfo
->State
.IsActive
))
178 Pipe_SelectPipe(HIDInterfaceInfo
->Config
.DataINPipeNumber
);
183 if (HIDInterfaceInfo
->State
.UsingBootProtocol
)
185 ReportSize
= Pipe_BytesInPipe();
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
);
200 if ((ErrorCode
= Pipe_Read_Stream_LE(Buffer
, ReportSize
, NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
206 return PIPE_RWSTREAM_NoError
;
209 uint8_t HID_Host_SendReportByID(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
, uint8_t ReportID
, void* Buffer
,
210 const uint16_t ReportSize
)
212 if ((USB_HostState
!= HOST_STATE_Configured
) || !(HIDInterfaceInfo
->State
.IsActive
))
215 if (HIDInterfaceInfo
->State
.DeviceUsesOUTPipe
)
217 USB_ControlRequest
= (USB_Request_Header_t
)
219 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
220 .bRequest
= REQ_SetReport
,
222 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
223 .wLength
= ReportSize
,
226 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
228 return USB_Host_SendControlRequest(Buffer
);
234 Pipe_SelectPipe(HIDInterfaceInfo
->Config
.DataOUTPipeNumber
);
238 Pipe_Write_Stream_LE(&ReportID
, sizeof(ReportID
), NO_STREAM_CALLBACK
);
240 if ((ErrorCode
= Pipe_Write_Stream_LE(Buffer
, ReportSize
, NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
246 return PIPE_RWSTREAM_NoError
;
250 bool HID_Host_IsReportReceived(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
)
252 if ((USB_HostState
!= HOST_STATE_Configured
) || !(HIDInterfaceInfo
->State
.IsActive
))
257 Pipe_SelectPipe(HIDInterfaceInfo
->Config
.DataINPipeNumber
);
260 ReportReceived
= Pipe_IsINReceived();
264 return ReportReceived
;
267 uint8_t USB_HID_Host_SetBootProtocol(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
)
269 if (HIDInterfaceInfo
->State
.UsingBootProtocol
)
270 return HOST_SENDCONTROL_Successful
;
274 USB_ControlRequest
= (USB_Request_Header_t
)
276 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
277 .bRequest
= REQ_SetProtocol
,
279 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
283 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
285 if (!(HIDInterfaceInfo
->State
.SupportsBootProtocol
))
286 return HID_ERROR_LOGICAL
;
288 if ((ErrorCode
= USB_Host_SendControlRequest(NULL
)) != HOST_SENDCONTROL_Successful
)
291 HIDInterfaceInfo
->State
.UsingBootProtocol
= true;
293 return HOST_SENDCONTROL_Successful
;
296 uint8_t USB_HID_Host_SetReportProtocol(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
)
300 uint8_t HIDReportData
[HIDInterfaceInfo
->State
.HIDReportSize
];
302 USB_ControlRequest
= (USB_Request_Header_t
)
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
,
311 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
313 if ((ErrorCode
= USB_Host_SendControlRequest(HIDReportData
)) != HOST_SENDCONTROL_Successful
)
316 if (HIDInterfaceInfo
->State
.UsingBootProtocol
)
318 USB_ControlRequest
= (USB_Request_Header_t
)
320 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
321 .bRequest
= REQ_SetProtocol
,
323 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
327 if ((ErrorCode
= USB_Host_SendControlRequest(NULL
)) != HOST_SENDCONTROL_Successful
)
330 HIDInterfaceInfo
->State
.UsingBootProtocol
= false;
333 if (HIDInterfaceInfo
->Config
.HIDParserData
== NULL
)
334 return HID_ERROR_LOGICAL
;
336 if ((ErrorCode
= USB_ProcessHIDReport(HIDReportData
, HIDInterfaceInfo
->State
.HIDReportSize
,
337 HIDInterfaceInfo
->Config
.HIDParserData
)) != HID_PARSE_Successful
)
339 return HID_ERROR_LOGICAL
| ErrorCode
;