3      Copyright (C) Dean Camera, 2010. 
   5   dean [at] fourwalledcubicle [dot] com 
   6       www.fourwalledcubicle.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_CLASS_HOST_C 
  36 #define  __INCLUDE_FROM_HID_DRIVER 
  39 uint8_t HID_Host_ConfigurePipes(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
, uint16_t ConfigDescriptorSize
, 
  40                                 void* ConfigDescriptorData
) 
  42         uint8_t FoundEndpoints 
= 0; 
  44         memset(&HIDInterfaceInfo
->State
, 0x00, sizeof(HIDInterfaceInfo
->State
)); 
  46         if (DESCRIPTOR_TYPE(ConfigDescriptorData
) != DTYPE_Configuration
) 
  47           return HID_ENUMERROR_InvalidConfigDescriptor
; 
  49         USB_Descriptor_Interface_t
* CurrentHIDInterface
; 
  53                 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
, 
  54                                                                           DCOMP_HID_Host_NextHIDInterface
) != DESCRIPTOR_SEARCH_COMP_Found
) 
  56                         return HID_ENUMERROR_NoHIDInterfaceFound
; 
  59                 CurrentHIDInterface 
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Interface_t
); 
  60         } while (HIDInterfaceInfo
->Config
.HIDInterfaceProtocol 
&& 
  61                  (CurrentHIDInterface
->Protocol 
!= HIDInterfaceInfo
->Config
.HIDInterfaceProtocol
)); 
  63         HIDInterfaceInfo
->State
.InterfaceNumber      
= CurrentHIDInterface
->InterfaceNumber
; 
  64         HIDInterfaceInfo
->State
.SupportsBootProtocol 
= (CurrentHIDInterface
->SubClass 
!= HID_NON_BOOT_PROTOCOL
); 
  66         if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
, DCOMP_HID_NextHID
) != DESCRIPTOR_SEARCH_COMP_Found
) 
  68                 return HID_ENUMERROR_NoHIDDescriptorFound
; 
  71         HIDInterfaceInfo
->State
.HIDReportSize 
= DESCRIPTOR_CAST(ConfigDescriptorData
, USB_HID_Descriptor_t
).HIDReportLength
; 
  73         while (FoundEndpoints 
!= (HID_FOUND_DATAPIPE_IN 
| HID_FOUND_DATAPIPE_OUT
)) 
  75                 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
, 
  76                                               DCOMP_HID_Host_NextHIDInterfaceEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
) 
  78                         if (FoundEndpoints 
& HID_FOUND_DATAPIPE_IN
) 
  81                         return HID_ENUMERROR_EndpointsNotFound
; 
  84                 USB_Descriptor_Endpoint_t
* EndpointData 
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Endpoint_t
); 
  86                 if (EndpointData
->EndpointAddress 
& ENDPOINT_DESCRIPTOR_DIR_IN
) 
  88                         Pipe_ConfigurePipe(HIDInterfaceInfo
->Config
.DataINPipeNumber
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_IN
, 
  89                                                            EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
, 
  90                                                            HIDInterfaceInfo
->Config
.DataINPipeDoubleBank ? PIPE_BANK_DOUBLE 
: PIPE_BANK_SINGLE
); 
  91                         HIDInterfaceInfo
->State
.DataINPipeSize 
= EndpointData
->EndpointSize
; 
  93                         FoundEndpoints 
|= HID_FOUND_DATAPIPE_IN
; 
  97                         Pipe_ConfigurePipe(HIDInterfaceInfo
->Config
.DataOUTPipeNumber
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_OUT
, 
  98                                                            EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
, 
  99                                                            HIDInterfaceInfo
->Config
.DataOUTPipeDoubleBank ? PIPE_BANK_DOUBLE 
: PIPE_BANK_SINGLE
); 
 100                         HIDInterfaceInfo
->State
.DataOUTPipeSize 
= EndpointData
->EndpointSize
; 
 102                         HIDInterfaceInfo
->State
.DeviceUsesOUTPipe 
= true; 
 104                         FoundEndpoints 
|= HID_FOUND_DATAPIPE_OUT
;                
 108         HIDInterfaceInfo
->State
.LargestReportSize 
= 8; 
 109         HIDInterfaceInfo
->State
.IsActive 
= true; 
 110         return HID_ENUMERROR_NoError
; 
 113 static uint8_t DCOMP_HID_Host_NextHIDInterface(void* const CurrentDescriptor
) 
 115         if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
) 
 117                 USB_Descriptor_Interface_t
* CurrentInterface 
= DESCRIPTOR_PCAST(CurrentDescriptor
, 
 118                                                                                 USB_Descriptor_Interface_t
); 
 120                 if (CurrentInterface
->Class 
== HID_INTERFACE_CLASS
) 
 121                   return DESCRIPTOR_SEARCH_Found
; 
 124         return DESCRIPTOR_SEARCH_NotFound
; 
 127 static uint8_t DCOMP_HID_NextHID(void* const CurrentDescriptor
) 
 129         if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_HID
) 
 130           return DESCRIPTOR_SEARCH_Found
; 
 131         else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
) 
 132           return DESCRIPTOR_SEARCH_Fail
; 
 134           return DESCRIPTOR_SEARCH_NotFound
;       
 137 static uint8_t DCOMP_HID_Host_NextHIDInterfaceEndpoint(void* const CurrentDescriptor
) 
 139         if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
) 
 141                 USB_Descriptor_Endpoint_t
* CurrentEndpoint 
= DESCRIPTOR_PCAST(CurrentDescriptor
, 
 142                                                                               USB_Descriptor_Endpoint_t
);        
 144                 if (!(Pipe_IsEndpointBound(CurrentEndpoint
->EndpointAddress
))) 
 145                   return DESCRIPTOR_SEARCH_Found
; 
 147         else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
) 
 149                 return DESCRIPTOR_SEARCH_Fail
; 
 152         return DESCRIPTOR_SEARCH_NotFound
; 
 155 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY) 
 156 uint8_t HID_Host_ReceiveReportByID(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
, const uint8_t ReportID
, void* Buffer
) 
 158         USB_ControlRequest 
= (USB_Request_Header_t
) 
 160                 .bmRequestType 
= (REQDIR_HOSTTODEVICE 
| REQTYPE_CLASS 
| REQREC_INTERFACE
), 
 161                 .bRequest      
= REQ_SetReport
, 
 162                 .wValue        
= ((REPORT_ITEM_TYPE_In 
+ 1) << 8) | ReportID
, 
 163                 .wIndex        
= HIDInterfaceInfo
->State
.InterfaceNumber
, 
 164                 .wLength       
= USB_GetHIDReportSize(HIDInterfaceInfo
->Config
.HIDParserData
, ReportID
, REPORT_ITEM_TYPE_In
), 
 167         Pipe_SelectPipe(PIPE_CONTROLPIPE
); 
 169         return USB_Host_SendControlRequest(Buffer
); 
 173 uint8_t HID_Host_ReceiveReport(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
, void* Buffer
) 
 175         if ((USB_HostState 
!= HOST_STATE_Configured
) || !(HIDInterfaceInfo
->State
.IsActive
)) 
 176           return PIPE_READYWAIT_DeviceDisconnected
; 
 180         Pipe_SelectPipe(HIDInterfaceInfo
->Config
.DataINPipeNumber
); 
 184         uint8_t* BufferPos 
= Buffer
; 
 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                         *(BufferPos
++) = ReportID
; 
 197                 ReportSize 
= USB_GetHIDReportSize(HIDInterfaceInfo
->Config
.HIDParserData
, ReportID
, REPORT_ITEM_TYPE_In
); 
 202                 ReportSize 
= Pipe_BytesInPipe(); 
 205         if ((ErrorCode 
= Pipe_Read_Stream_LE(BufferPos
, 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                                 const uint8_t ReportType
, void* Buffer
, const uint16_t ReportSize
) 
 220 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY) 
 221         if ((USB_HostState 
!= HOST_STATE_Configured
) || !(HIDInterfaceInfo
->State
.IsActive
)) 
 224         if (HIDInterfaceInfo
->State
.DeviceUsesOUTPipe 
&& (ReportType 
== REPORT_ITEM_TYPE_Out
)) 
 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) 
 250                         .wValue        
= ((ReportType 
+ 1) << 8) | ReportID
, 
 252                         .wValue        
= ((ReportType 
+ 1) << 8), 
 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
) 
 285         USB_ControlRequest 
= (USB_Request_Header_t
) 
 287                         .bmRequestType 
= (REQDIR_HOSTTODEVICE 
| REQTYPE_CLASS 
| REQREC_INTERFACE
), 
 288                         .bRequest      
= REQ_SetProtocol
, 
 290                         .wIndex        
= HIDInterfaceInfo
->State
.InterfaceNumber
, 
 294         Pipe_SelectPipe(PIPE_CONTROLPIPE
); 
 296         if (!(HIDInterfaceInfo
->State
.SupportsBootProtocol
)) 
 297           return HID_ERROR_LOGICAL
; 
 299         if ((ErrorCode 
= USB_Host_SendControlRequest(NULL
)) != HOST_SENDCONTROL_Successful
) 
 302         HIDInterfaceInfo
->State
.LargestReportSize 
= 8; 
 303         HIDInterfaceInfo
->State
.UsingBootProtocol 
= true; 
 305         return HOST_SENDCONTROL_Successful
; 
 308 #if !defined(HID_HOST_BOOT_PROTOCOL_ONLY) 
 309 uint8_t HID_Host_SetReportProtocol(USB_ClassInfo_HID_Host_t
* const HIDInterfaceInfo
) 
 313         uint8_t HIDReportData
[HIDInterfaceInfo
->State
.HIDReportSize
]; 
 315         USB_ControlRequest 
= (USB_Request_Header_t
) 
 317                         .bmRequestType 
= (REQDIR_DEVICETOHOST 
| REQTYPE_STANDARD 
| REQREC_INTERFACE
), 
 318                         .bRequest      
= REQ_GetDescriptor
, 
 319                         .wValue        
= (DTYPE_Report 
<< 8), 
 320                         .wIndex        
= HIDInterfaceInfo
->State
.InterfaceNumber
, 
 321                         .wLength       
= HIDInterfaceInfo
->State
.HIDReportSize
, 
 324         Pipe_SelectPipe(PIPE_CONTROLPIPE
); 
 326         if ((ErrorCode 
= USB_Host_SendControlRequest(HIDReportData
)) != HOST_SENDCONTROL_Successful
) 
 329         if (HIDInterfaceInfo
->State
.UsingBootProtocol
) 
 331                 USB_ControlRequest 
= (USB_Request_Header_t
) 
 333                                 .bmRequestType 
= (REQDIR_HOSTTODEVICE 
| REQTYPE_CLASS 
| REQREC_INTERFACE
), 
 334                                 .bRequest      
= REQ_SetProtocol
, 
 336                                 .wIndex        
= HIDInterfaceInfo
->State
.InterfaceNumber
, 
 340                 if ((ErrorCode 
= USB_Host_SendControlRequest(NULL
)) != HOST_SENDCONTROL_Successful
) 
 343                 HIDInterfaceInfo
->State
.UsingBootProtocol 
= false; 
 346         if (HIDInterfaceInfo
->Config
.HIDParserData 
== NULL
) 
 347           return HID_ERROR_LOGICAL
; 
 349         if ((ErrorCode 
= USB_ProcessHIDReport(HIDReportData
, HIDInterfaceInfo
->State
.HIDReportSize
, 
 350                                               HIDInterfaceInfo
->Config
.HIDParserData
)) != HID_PARSE_Successful
) 
 352                 return HID_ERROR_LOGICAL 
| ErrorCode
; 
 355         uint8_t LargestReportSizeBits 
= HIDInterfaceInfo
->Config
.HIDParserData
->LargestReportSizeBits
; 
 356         HIDInterfaceInfo
->State
.LargestReportSize 
= (LargestReportSizeBits 
>> 3) + ((LargestReportSizeBits 
& 0x07) != 0);