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 #warning The HID Host mode Class driver is currently incomplete and is for preview purposes only. 
  39 uint8_t HID_Host_ConfigurePipes(USB_ClassInfo_HID_Host_t
* HIDInterfaceInfo
, uint16_t ConfigDescriptorSize
, 
  40                                 uint8_t* 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      
= 
  64         #if defined(USE_NONSTANDARD_DESCRIPTOR_NAMES) 
  65                              CurrentHIDInterface
->InterfaceNumber
; 
  67                              CurrentHIDInterface
->bInterfaceNumber
; 
  69         HIDInterfaceInfo
->State
.SupportsBootSubClass 
= (CurrentHIDInterface
->SubClass 
!= 0); 
  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 
== (1 << 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                         FoundEndpoints 
|= HID_FOUND_DATAPIPE_OUT
;                
 102         HIDInterfaceInfo
->State
.IsActive 
= true; 
 103         return HID_ENUMERROR_NoError
; 
 106 static uint8_t DComp_HID_Host_NextHIDInterface(void* CurrentDescriptor
) 
 108         if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
) 
 110                 USB_Descriptor_Interface_t
* CurrentInterface 
= DESCRIPTOR_PCAST(CurrentDescriptor
, 
 111                                                                                 USB_Descriptor_Interface_t
); 
 113                 if (CurrentInterface
->Class 
== HID_INTERFACE_CLASS
) 
 114                   return DESCRIPTOR_SEARCH_Found
; 
 117         return DESCRIPTOR_SEARCH_NotFound
; 
 120 static uint8_t DComp_HID_Host_NextHIDInterfaceEndpoint(void* CurrentDescriptor
) 
 122         if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
) 
 124                 USB_Descriptor_Endpoint_t
* CurrentEndpoint 
= DESCRIPTOR_PCAST(CurrentDescriptor
, 
 125                                                                               USB_Descriptor_Endpoint_t
);        
 127                 if (!(Pipe_IsEndpointBound(CurrentEndpoint
->EndpointAddress
))) 
 128                   return DESCRIPTOR_SEARCH_Found
; 
 130         else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
) 
 132                 return DESCRIPTOR_SEARCH_Fail
; 
 135         return DESCRIPTOR_SEARCH_NotFound
; 
 138 void HID_Host_USBTask(USB_ClassInfo_HID_Host_t
* HIDInterfaceInfo
) 
 143 bool HID_Host_IsReportReceived(USB_ClassInfo_HID_Host_t
* HIDInterfaceInfo
) 
 145         if ((USB_HostState 
!= HOST_STATE_Configured
) || !(HIDInterfaceInfo
->State
.IsActive
)) 
 150         Pipe_SelectPipe(HIDInterfaceInfo
->Config
.DataINPipeNumber
); 
 153         ReportReceived 
= Pipe_IsReadWriteAllowed(); 
 157         return ReportReceived
; 
 160 uint8_t USB_HID_Host_SetProtocol(USB_ClassInfo_HID_Host_t
* HIDInterfaceInfo
, bool UseReportProtocol
) 
 162         USB_ControlRequest 
= (USB_Request_Header_t
) 
 164                         .bmRequestType 
= (REQDIR_HOSTTODEVICE 
| REQTYPE_CLASS 
| REQREC_INTERFACE
), 
 165                         .bRequest      
= REQ_SetProtocol
, 
 166                         .wValue        
= UseReportProtocol
, 
 167                         .wIndex        
= HIDInterfaceInfo
->State
.InterfaceNumber
, 
 171         Pipe_SelectPipe(PIPE_CONTROLPIPE
); 
 173         return USB_Host_SendControlRequest(NULL
);