3 Copyright (C) Dean Camera, 2018.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2018 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all 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 disclaims 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 #define __INCLUDE_FROM_USB_DRIVER
32 #include "../../Core/USBMode.h"
34 #if defined(USB_CAN_BE_HOST)
36 #define __INCLUDE_FROM_HID_DRIVER
37 #define __INCLUDE_FROM_HID_HOST_C
38 #include "HIDClassHost.h"
40 uint8_t HID_Host_ConfigurePipes(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
,
41 uint16_t ConfigDescriptorSize
,
42 void* ConfigDescriptorData
)
44 USB_Descriptor_Endpoint_t
* DataINEndpoint
= NULL
;
45 USB_Descriptor_Endpoint_t
* DataOUTEndpoint
= NULL
;
46 USB_Descriptor_Interface_t
* HIDInterface
= NULL
;
47 USB_HID_Descriptor_HID_t
* HIDDescriptor
= NULL
;
49 memset(&HIDInterfaceInfo
->State
, 0x00, sizeof(HIDInterfaceInfo
->State
));
51 if (DESCRIPTOR_TYPE(ConfigDescriptorData
) != DTYPE_Configuration
)
52 return HID_ENUMERROR_InvalidConfigDescriptor
;
54 while (!(DataINEndpoint
) || !(DataOUTEndpoint
))
56 if (!(HIDInterface
) ||
57 USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
58 DCOMP_HID_Host_NextHIDInterfaceEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
)
65 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
66 DCOMP_HID_Host_NextHIDInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
68 return HID_ENUMERROR_NoCompatibleInterfaceFound
;
71 HIDInterface
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Interface_t
);
72 } while (HIDInterfaceInfo
->Config
.HIDInterfaceProtocol
&&
73 (HIDInterface
->Protocol
!= HIDInterfaceInfo
->Config
.HIDInterfaceProtocol
));
75 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
76 DCOMP_HID_Host_NextHIDDescriptor
) != DESCRIPTOR_SEARCH_COMP_Found
)
78 return HID_ENUMERROR_NoCompatibleInterfaceFound
;
81 HIDDescriptor
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_HID_Descriptor_HID_t
);
83 DataINEndpoint
= NULL
;
84 DataOUTEndpoint
= NULL
;
89 USB_Descriptor_Endpoint_t
* EndpointData
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Endpoint_t
);
91 if ((EndpointData
->EndpointAddress
& ENDPOINT_DIR_MASK
) == ENDPOINT_DIR_IN
)
92 DataINEndpoint
= EndpointData
;
94 DataOUTEndpoint
= EndpointData
;
97 HIDInterfaceInfo
->Config
.DataINPipe
.Size
= le16_to_cpu(DataINEndpoint
->EndpointSize
);
98 HIDInterfaceInfo
->Config
.DataINPipe
.EndpointAddress
= DataINEndpoint
->EndpointAddress
;
99 HIDInterfaceInfo
->Config
.DataINPipe
.Type
= EP_TYPE_INTERRUPT
;
101 if (!(Pipe_ConfigurePipeTable(&HIDInterfaceInfo
->Config
.DataINPipe
, 1)))
102 return HID_ENUMERROR_PipeConfigurationFailed
;
106 HIDInterfaceInfo
->Config
.DataOUTPipe
.Size
= le16_to_cpu(DataOUTEndpoint
->EndpointSize
);
107 HIDInterfaceInfo
->Config
.DataOUTPipe
.EndpointAddress
= DataOUTEndpoint
->EndpointAddress
;
108 HIDInterfaceInfo
->Config
.DataOUTPipe
.Type
= EP_TYPE_INTERRUPT
;
110 if (!(Pipe_ConfigurePipeTable(&HIDInterfaceInfo
->Config
.DataOUTPipe
, 1)))
111 return HID_ENUMERROR_PipeConfigurationFailed
;
114 HIDInterfaceInfo
->State
.InterfaceNumber
= HIDInterface
->InterfaceNumber
;
115 HIDInterfaceInfo
->State
.HIDReportSize
= LE16_TO_CPU(HIDDescriptor
->HIDReportLength
);
116 HIDInterfaceInfo
->State
.DeviceUsesOUTPipe
= DataOUTEndpoint
;
117 HIDInterfaceInfo
->State
.SupportsBootProtocol
= (HIDInterface
->SubClass
!= HID_CSCP_NonBootProtocol
);
118 HIDInterfaceInfo
->State
.LargestReportSize
= 8;
119 HIDInterfaceInfo
->State
.IsActive
= true;
121 return HID_ENUMERROR_NoError
;
124 static uint8_t DCOMP_HID_Host_NextHIDInterface(void* const CurrentDescriptor
)
126 USB_Descriptor_Header_t
* Header
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
);
128 if (Header
->Type
== DTYPE_Interface
)
130 USB_Descriptor_Interface_t
* Interface
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Interface_t
);
132 if (Interface
->Class
== HID_CSCP_HIDClass
)
133 return DESCRIPTOR_SEARCH_Found
;
136 return DESCRIPTOR_SEARCH_NotFound
;
139 static uint8_t DCOMP_HID_Host_NextHIDDescriptor(void* const CurrentDescriptor
)
141 USB_Descriptor_Header_t
* Header
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
);
143 if (Header
->Type
== HID_DTYPE_HID
)
144 return DESCRIPTOR_SEARCH_Found
;
145 else if (Header
->Type
== DTYPE_Interface
)
146 return DESCRIPTOR_SEARCH_Fail
;
148 return DESCRIPTOR_SEARCH_NotFound
;
151 static uint8_t DCOMP_HID_Host_NextHIDInterfaceEndpoint(void* const CurrentDescriptor
)
153 USB_Descriptor_Header_t
* Header
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
);
155 if (Header
->Type
== DTYPE_Endpoint
)
157 USB_Descriptor_Endpoint_t
* Endpoint
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Endpoint_t
);
159 if (!(Pipe_IsEndpointBound(Endpoint
->EndpointAddress
)))
160 return DESCRIPTOR_SEARCH_Found
;
162 else if (Header
->Type
== DTYPE_Interface
)
164 return DESCRIPTOR_SEARCH_Fail
;
167 return DESCRIPTOR_SEARCH_NotFound
;
170 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
171 uint8_t HID_Host_ReceiveReportByID(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
,
172 const uint8_t ReportID
,
175 USB_ControlRequest
= (USB_Request_Header_t
)
177 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
178 .bRequest
= HID_REQ_SetReport
,
179 .wValue
= ((HID_REPORT_ITEM_In
+ 1) << 8) | ReportID
,
180 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
181 .wLength
= USB_GetHIDReportSize(HIDInterfaceInfo
->Config
.HIDParserData
, ReportID
, HID_REPORT_ITEM_In
),
184 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
186 return USB_Host_SendControlRequest(Buffer
);
190 uint8_t HID_Host_ReceiveReport(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
,
193 if ((USB_HostState
!= HOST_STATE_Configured
) || !(HIDInterfaceInfo
->State
.IsActive
))
194 return PIPE_READYWAIT_DeviceDisconnected
;
198 Pipe_SelectPipe(HIDInterfaceInfo
->Config
.DataINPipe
.Address
);
202 uint8_t* BufferPos
= Buffer
;
204 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
205 if (!(HIDInterfaceInfo
->State
.UsingBootProtocol
))
207 uint8_t ReportID
= 0;
209 if (HIDInterfaceInfo
->Config
.HIDParserData
->UsingReportIDs
)
211 ReportID
= Pipe_Read_8();
212 *(BufferPos
++) = ReportID
;
215 ReportSize
= USB_GetHIDReportSize(HIDInterfaceInfo
->Config
.HIDParserData
, ReportID
, HID_REPORT_ITEM_In
);
220 ReportSize
= Pipe_BytesInPipe();
223 if ((ErrorCode
= Pipe_Read_Stream_LE(BufferPos
, ReportSize
, NULL
)) != PIPE_RWSTREAM_NoError
)
229 return PIPE_RWSTREAM_NoError
;
232 uint8_t HID_Host_SendReportByID(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
,
233 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
234 const uint8_t ReportID
,
236 const uint8_t ReportType
,
238 const uint16_t ReportSize
)
240 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
241 if ((USB_HostState
!= HOST_STATE_Configured
) || !(HIDInterfaceInfo
->State
.IsActive
))
242 return PIPE_RWSTREAM_NoError
;
244 if (HIDInterfaceInfo
->State
.DeviceUsesOUTPipe
&& (ReportType
== HID_REPORT_ITEM_Out
))
248 Pipe_SelectPipe(HIDInterfaceInfo
->Config
.DataOUTPipe
.Address
);
252 Pipe_Write_Stream_LE(&ReportID
, sizeof(ReportID
), NULL
);
254 if ((ErrorCode
= Pipe_Write_Stream_LE(Buffer
, ReportSize
, NULL
)) != PIPE_RWSTREAM_NoError
)
260 return PIPE_RWSTREAM_NoError
;
265 USB_ControlRequest
= (USB_Request_Header_t
)
267 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
268 .bRequest
= HID_REQ_SetReport
,
269 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
270 .wValue
= ((ReportType
+ 1) << 8) | ReportID
,
272 .wValue
= ((ReportType
+ 1) << 8),
274 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
275 .wLength
= ReportSize
,
278 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
280 return USB_Host_SendControlRequest(Buffer
);
284 bool HID_Host_IsReportReceived(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
)
286 if ((USB_HostState
!= HOST_STATE_Configured
) || !(HIDInterfaceInfo
->State
.IsActive
))
291 Pipe_SelectPipe(HIDInterfaceInfo
->Config
.DataINPipe
.Address
);
294 ReportReceived
= Pipe_IsINReceived();
298 return ReportReceived
;
301 uint8_t HID_Host_SetBootProtocol(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
)
305 if (!(HIDInterfaceInfo
->State
.SupportsBootProtocol
))
306 return HID_ERROR_LOGICAL
;
308 USB_ControlRequest
= (USB_Request_Header_t
)
310 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
311 .bRequest
= HID_REQ_SetProtocol
,
313 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
317 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
319 if ((ErrorCode
= USB_Host_SendControlRequest(NULL
)) != HOST_SENDCONTROL_Successful
)
322 HIDInterfaceInfo
->State
.LargestReportSize
= 8;
323 HIDInterfaceInfo
->State
.UsingBootProtocol
= true;
325 return HOST_SENDCONTROL_Successful
;
328 uint8_t HID_Host_SetIdlePeriod(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
,
331 USB_ControlRequest
= (USB_Request_Header_t
)
333 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
334 .bRequest
= HID_REQ_SetIdle
,
335 .wValue
= ((MS
<< 6) & 0xFF00),
336 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
340 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
342 return USB_Host_SendControlRequest(NULL
);
345 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
346 uint8_t HID_Host_SetReportProtocol(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
)
350 uint8_t HIDReportData
[HIDInterfaceInfo
->State
.HIDReportSize
];
352 USB_ControlRequest
= (USB_Request_Header_t
)
354 .bmRequestType
= (REQDIR_DEVICETOHOST
| REQTYPE_STANDARD
| REQREC_INTERFACE
),
355 .bRequest
= REQ_GetDescriptor
,
356 .wValue
= (HID_DTYPE_Report
<< 8),
357 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
358 .wLength
= HIDInterfaceInfo
->State
.HIDReportSize
,
361 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
363 if ((ErrorCode
= USB_Host_SendControlRequest(HIDReportData
)) != HOST_SENDCONTROL_Successful
)
366 if (HIDInterfaceInfo
->State
.UsingBootProtocol
)
368 USB_ControlRequest
= (USB_Request_Header_t
)
370 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
371 .bRequest
= HID_REQ_SetProtocol
,
373 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
377 if ((ErrorCode
= USB_Host_SendControlRequest(NULL
)) != HOST_SENDCONTROL_Successful
)
380 HIDInterfaceInfo
->State
.UsingBootProtocol
= false;
383 if (HIDInterfaceInfo
->Config
.HIDParserData
== NULL
)
384 return HID_ERROR_LOGICAL
;
386 if ((ErrorCode
= USB_ProcessHIDReport(HIDReportData
, HIDInterfaceInfo
->State
.HIDReportSize
,
387 HIDInterfaceInfo
->Config
.HIDParserData
)) != HID_PARSE_Successful
)
389 return HID_ERROR_LOGICAL
| ErrorCode
;
392 uint16_t LargestReportSizeBits
= HIDInterfaceInfo
->Config
.HIDParserData
->LargestReportSizeBits
;
393 HIDInterfaceInfo
->State
.LargestReportSize
= (LargestReportSizeBits
>> 3) + ((LargestReportSizeBits
& 0x07) != 0);