3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2010 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 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 #define __INCLUDE_FROM_USB_DRIVER
32 #include "../../HighLevel/USBMode.h"
33 #if defined(USB_CAN_BE_HOST)
35 #define __INCLUDE_FROM_HID_DRIVER
36 #define __INCLUDE_FROM_HID_HOST_C
39 uint8_t HID_Host_ConfigurePipes(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
,
40 uint16_t ConfigDescriptorSize
,
41 void* ConfigDescriptorData
)
43 USB_Descriptor_Endpoint_t
* DataINEndpoint
= NULL
;
44 USB_Descriptor_Endpoint_t
* DataOUTEndpoint
= NULL
;
45 USB_Descriptor_Interface_t
* HIDInterface
= NULL
;
46 USB_HID_Descriptor_HID_t
* HIDDescriptor
= NULL
;
48 memset(&HIDInterfaceInfo
->State
, 0x00, sizeof(HIDInterfaceInfo
->State
));
50 if (DESCRIPTOR_TYPE(ConfigDescriptorData
) != DTYPE_Configuration
)
51 return HID_ENUMERROR_InvalidConfigDescriptor
;
53 while (!(DataINEndpoint
) || !(DataOUTEndpoint
))
55 if (!(HIDInterface
) ||
56 USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
57 DCOMP_HID_Host_NextHIDInterfaceEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
)
59 if (DataINEndpoint
|| DataOUTEndpoint
)
64 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
65 DCOMP_HID_Host_NextHIDInterface
) != DESCRIPTOR_SEARCH_COMP_Found
)
67 return HID_ENUMERROR_NoCompatibleInterfaceFound
;
70 HIDInterface
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Interface_t
);
71 } while (HIDInterfaceInfo
->Config
.HIDInterfaceProtocol
&&
72 (HIDInterface
->Protocol
!= HIDInterfaceInfo
->Config
.HIDInterfaceProtocol
));
74 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,
75 DCOMP_HID_Host_NextHID
) != DESCRIPTOR_SEARCH_COMP_Found
)
77 return HID_ENUMERROR_NoCompatibleInterfaceFound
;
80 HIDDescriptor
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_HID_Descriptor_HID_t
);
82 DataINEndpoint
= NULL
;
83 DataOUTEndpoint
= NULL
;
88 USB_Descriptor_Endpoint_t
* EndpointData
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Endpoint_t
);
90 if (EndpointData
->EndpointAddress
& ENDPOINT_DESCRIPTOR_DIR_IN
)
91 DataINEndpoint
= EndpointData
;
93 DataOUTEndpoint
= EndpointData
;
96 for (uint8_t PipeNum
= 1; PipeNum
< PIPE_TOTAL_PIPES
; PipeNum
++)
101 uint8_t EndpointAddress
;
102 uint8_t InterruptPeriod
;
105 if (PipeNum
== HIDInterfaceInfo
->Config
.DataINPipeNumber
)
107 Size
= DataINEndpoint
->EndpointSize
;
108 EndpointAddress
= DataINEndpoint
->EndpointAddress
;
109 Token
= PIPE_TOKEN_IN
;
110 Type
= EP_TYPE_INTERRUPT
;
111 DoubleBanked
= HIDInterfaceInfo
->Config
.DataINPipeDoubleBank
;
112 InterruptPeriod
= DataINEndpoint
->PollingIntervalMS
;
114 HIDInterfaceInfo
->State
.DataINPipeSize
= DataINEndpoint
->EndpointSize
;
116 else if (PipeNum
== HIDInterfaceInfo
->Config
.DataOUTPipeNumber
)
118 Size
= DataOUTEndpoint
->EndpointSize
;
119 EndpointAddress
= DataOUTEndpoint
->EndpointAddress
;
120 Token
= PIPE_TOKEN_OUT
;
121 Type
= EP_TYPE_INTERRUPT
;
122 DoubleBanked
= HIDInterfaceInfo
->Config
.DataOUTPipeDoubleBank
;
123 InterruptPeriod
= DataOUTEndpoint
->PollingIntervalMS
;
125 HIDInterfaceInfo
->State
.DataOUTPipeSize
= DataOUTEndpoint
->EndpointSize
;
126 HIDInterfaceInfo
->State
.DeviceUsesOUTPipe
= true;
133 if (!(Pipe_ConfigurePipe(PipeNum
, Type
, Token
, EndpointAddress
, Size
,
134 DoubleBanked ? PIPE_BANK_DOUBLE
: PIPE_BANK_SINGLE
)))
136 return HID_ENUMERROR_PipeConfigurationFailed
;
140 Pipe_SetInterruptPeriod(InterruptPeriod
);
143 HIDInterfaceInfo
->State
.InterfaceNumber
= HIDInterface
->InterfaceNumber
;
144 HIDInterfaceInfo
->State
.HIDReportSize
= HIDDescriptor
->HIDReportLength
;
145 HIDInterfaceInfo
->State
.SupportsBootProtocol
= (HIDInterface
->SubClass
!= HID_CSCP_NonBootProtocol
);
146 HIDInterfaceInfo
->State
.LargestReportSize
= 8;
147 HIDInterfaceInfo
->State
.IsActive
= true;
149 return HID_ENUMERROR_NoError
;
152 static uint8_t DCOMP_HID_Host_NextHIDInterface(void* const CurrentDescriptor
)
154 USB_Descriptor_Header_t
* Header
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
);
156 if (Header
->Type
== DTYPE_Interface
)
158 USB_Descriptor_Interface_t
* Interface
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Interface_t
);
160 if (Interface
->Class
== HID_CSCP_HIDClass
)
161 return DESCRIPTOR_SEARCH_Found
;
164 return DESCRIPTOR_SEARCH_NotFound
;
167 static uint8_t DCOMP_HID_Host_NextHID(void* const CurrentDescriptor
)
169 USB_Descriptor_Header_t
* Header
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
);
171 if (Header
->Type
== HID_DTYPE_HID
)
172 return DESCRIPTOR_SEARCH_Found
;
173 else if (Header
->Type
== DTYPE_Interface
)
174 return DESCRIPTOR_SEARCH_Fail
;
176 return DESCRIPTOR_SEARCH_NotFound
;
179 static uint8_t DCOMP_HID_Host_NextHIDInterfaceEndpoint(void* const CurrentDescriptor
)
181 USB_Descriptor_Header_t
* Header
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
);
183 if (Header
->Type
== DTYPE_Endpoint
)
185 USB_Descriptor_Endpoint_t
* Endpoint
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Endpoint_t
);
187 if (!(Pipe_IsEndpointBound(Endpoint
->EndpointAddress
)))
188 return DESCRIPTOR_SEARCH_Found
;
190 else if (Header
->Type
== DTYPE_Interface
)
192 return DESCRIPTOR_SEARCH_Fail
;
195 return DESCRIPTOR_SEARCH_NotFound
;
198 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
199 uint8_t HID_Host_ReceiveReportByID(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
,
200 const uint8_t ReportID
,
203 USB_ControlRequest
= (USB_Request_Header_t
)
205 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
206 .bRequest
= HID_REQ_SetReport
,
207 .wValue
= ((HID_REPORT_ITEM_In
+ 1) << 8) | ReportID
,
208 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
209 .wLength
= USB_GetHIDReportSize(HIDInterfaceInfo
->Config
.HIDParserData
, ReportID
, HID_REPORT_ITEM_In
),
212 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
214 return USB_Host_SendControlRequest(Buffer
);
218 uint8_t HID_Host_ReceiveReport(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
,
221 if ((USB_HostState
!= HOST_STATE_Configured
) || !(HIDInterfaceInfo
->State
.IsActive
))
222 return PIPE_READYWAIT_DeviceDisconnected
;
226 Pipe_SelectPipe(HIDInterfaceInfo
->Config
.DataINPipeNumber
);
230 uint8_t* BufferPos
= Buffer
;
232 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
233 if (!(HIDInterfaceInfo
->State
.UsingBootProtocol
))
235 uint8_t ReportID
= 0;
237 if (HIDInterfaceInfo
->Config
.HIDParserData
->UsingReportIDs
)
239 ReportID
= Pipe_Read_Byte();
240 *(BufferPos
++) = ReportID
;
243 ReportSize
= USB_GetHIDReportSize(HIDInterfaceInfo
->Config
.HIDParserData
, ReportID
, HID_REPORT_ITEM_In
);
248 ReportSize
= Pipe_BytesInPipe();
251 if ((ErrorCode
= Pipe_Read_Stream_LE(BufferPos
, ReportSize
, NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
257 return PIPE_RWSTREAM_NoError
;
260 uint8_t HID_Host_SendReportByID(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
,
261 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
262 const uint8_t ReportID
,
264 const uint8_t ReportType
,
266 const uint16_t ReportSize
)
268 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
269 if ((USB_HostState
!= HOST_STATE_Configured
) || !(HIDInterfaceInfo
->State
.IsActive
))
272 if (HIDInterfaceInfo
->State
.DeviceUsesOUTPipe
&& (ReportType
== HID_REPORT_ITEM_Out
))
276 Pipe_SelectPipe(HIDInterfaceInfo
->Config
.DataOUTPipeNumber
);
280 Pipe_Write_Stream_LE(&ReportID
, sizeof(ReportID
), NO_STREAM_CALLBACK
);
282 if ((ErrorCode
= Pipe_Write_Stream_LE(Buffer
, ReportSize
, NO_STREAM_CALLBACK
)) != PIPE_RWSTREAM_NoError
)
288 return PIPE_RWSTREAM_NoError
;
293 USB_ControlRequest
= (USB_Request_Header_t
)
295 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
296 .bRequest
= HID_REQ_SetReport
,
297 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
298 .wValue
= ((ReportType
+ 1) << 8) | ReportID
,
300 .wValue
= ((ReportType
+ 1) << 8),
302 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
303 .wLength
= ReportSize
,
306 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
308 return USB_Host_SendControlRequest(Buffer
);
312 bool HID_Host_IsReportReceived(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
)
314 if ((USB_HostState
!= HOST_STATE_Configured
) || !(HIDInterfaceInfo
->State
.IsActive
))
319 Pipe_SelectPipe(HIDInterfaceInfo
->Config
.DataINPipeNumber
);
322 ReportReceived
= Pipe_IsINReceived();
326 return ReportReceived
;
329 uint8_t HID_Host_SetBootProtocol(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
)
333 USB_ControlRequest
= (USB_Request_Header_t
)
335 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
336 .bRequest
= HID_REQ_SetProtocol
,
338 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
342 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
344 if (!(HIDInterfaceInfo
->State
.SupportsBootProtocol
))
345 return HID_ERROR_LOGICAL
;
347 if ((ErrorCode
= USB_Host_SendControlRequest(NULL
)) != HOST_SENDCONTROL_Successful
)
350 HIDInterfaceInfo
->State
.LargestReportSize
= 8;
351 HIDInterfaceInfo
->State
.UsingBootProtocol
= true;
353 return HOST_SENDCONTROL_Successful
;
356 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY)
357 uint8_t HID_Host_SetReportProtocol(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
)
361 uint8_t HIDReportData
[HIDInterfaceInfo
->State
.HIDReportSize
];
363 USB_ControlRequest
= (USB_Request_Header_t
)
365 .bmRequestType
= (REQDIR_DEVICETOHOST
| REQTYPE_STANDARD
| REQREC_INTERFACE
),
366 .bRequest
= REQ_GetDescriptor
,
367 .wValue
= (HID_DTYPE_Report
<< 8),
368 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
369 .wLength
= HIDInterfaceInfo
->State
.HIDReportSize
,
372 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
374 if ((ErrorCode
= USB_Host_SendControlRequest(HIDReportData
)) != HOST_SENDCONTROL_Successful
)
377 if (HIDInterfaceInfo
->State
.UsingBootProtocol
)
379 USB_ControlRequest
= (USB_Request_Header_t
)
381 .bmRequestType
= (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
382 .bRequest
= HID_REQ_SetProtocol
,
384 .wIndex
= HIDInterfaceInfo
->State
.InterfaceNumber
,
388 if ((ErrorCode
= USB_Host_SendControlRequest(NULL
)) != HOST_SENDCONTROL_Successful
)
391 HIDInterfaceInfo
->State
.UsingBootProtocol
= false;
394 if (HIDInterfaceInfo
->Config
.HIDParserData
== NULL
)
395 return HID_ERROR_LOGICAL
;
397 if ((ErrorCode
= USB_ProcessHIDReport(HIDReportData
, HIDInterfaceInfo
->State
.HIDReportSize
,
398 HIDInterfaceInfo
->Config
.HIDParserData
)) != HID_PARSE_Successful
)
400 return HID_ERROR_LOGICAL
| ErrorCode
;
403 uint8_t LargestReportSizeBits
= HIDInterfaceInfo
->Config
.HIDParserData
->LargestReportSizeBits
;
404 HIDInterfaceInfo
->State
.LargestReportSize
= (LargestReportSizeBits
>> 3) + ((LargestReportSizeBits
& 0x07) != 0);