3      Copyright (C) Dean Camera, 2014. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2014  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_AUDIO_DRIVER 
  37 #define  __INCLUDE_FROM_AUDIO_HOST_C 
  38 #include "AudioClassHost.h" 
  40 uint8_t Audio_Host_ConfigurePipes(USB_ClassInfo_Audio_Host_t
* const AudioInterfaceInfo
, 
  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
* AudioControlInterface   
= NULL
; 
  47         USB_Descriptor_Interface_t
* AudioStreamingInterface 
= NULL
; 
  49         memset(&AudioInterfaceInfo
->State
, 0x00, sizeof(AudioInterfaceInfo
->State
)); 
  51         if (DESCRIPTOR_TYPE(ConfigDescriptorData
) != DTYPE_Configuration
) 
  52           return AUDIO_ENUMERROR_InvalidConfigDescriptor
; 
  54         while ((AudioInterfaceInfo
->Config
.DataINPipe
.Address  
&& !(DataINEndpoint
)) || 
  55                (AudioInterfaceInfo
->Config
.DataOUTPipe
.Address 
&& !(DataOUTEndpoint
))) 
  57                 if (!(AudioControlInterface
) || 
  58                     USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
, 
  59                                               DCOMP_Audio_Host_NextAudioInterfaceDataEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
) 
  61                         if (!(AudioControlInterface
) || 
  62                             USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
, 
  63                                                       DCOMP_Audio_Host_NextAudioStreamInterface
) != DESCRIPTOR_SEARCH_COMP_Found
) 
  65                                 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
, 
  66                                                               DCOMP_Audio_Host_NextAudioControlInterface
) != DESCRIPTOR_SEARCH_COMP_Found
) 
  68                                         return AUDIO_ENUMERROR_NoCompatibleInterfaceFound
; 
  71                                 AudioControlInterface 
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Interface_t
); 
  73                                 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
, 
  74                                                               DCOMP_Audio_Host_NextAudioStreamInterface
) != DESCRIPTOR_SEARCH_COMP_Found
) 
  76                                         return AUDIO_ENUMERROR_NoCompatibleInterfaceFound
; 
  80                         AudioStreamingInterface 
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Interface_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_DIR_MASK
) == ENDPOINT_DIR_IN
) 
  91                   DataINEndpoint  
= EndpointData
; 
  93                   DataOUTEndpoint 
= EndpointData
; 
  96         AudioInterfaceInfo
->Config
.DataINPipe
.Size   
= le16_to_cpu(DataINEndpoint
->EndpointSize
); 
  97         AudioInterfaceInfo
->Config
.DataINPipe
.EndpointAddress 
= DataINEndpoint
->EndpointAddress
; 
  98         AudioInterfaceInfo
->Config
.DataINPipe
.Type   
= EP_TYPE_ISOCHRONOUS
; 
  99         AudioInterfaceInfo
->Config
.DataINPipe
.Banks  
= 2; 
 101         AudioInterfaceInfo
->Config
.DataOUTPipe
.Size  
= le16_to_cpu(DataOUTEndpoint
->EndpointSize
); 
 102         AudioInterfaceInfo
->Config
.DataOUTPipe
.EndpointAddress 
= DataOUTEndpoint
->EndpointAddress
; 
 103         AudioInterfaceInfo
->Config
.DataOUTPipe
.Type  
= EP_TYPE_ISOCHRONOUS
; 
 104         AudioInterfaceInfo
->Config
.DataOUTPipe
.Banks 
= 2; 
 106         if (!(Pipe_ConfigurePipeTable(&AudioInterfaceInfo
->Config
.DataINPipe
, 1))) 
 107           return AUDIO_ENUMERROR_PipeConfigurationFailed
; 
 109         if (!(Pipe_ConfigurePipeTable(&AudioInterfaceInfo
->Config
.DataOUTPipe
, 1))) 
 110           return AUDIO_ENUMERROR_PipeConfigurationFailed
; 
 112         AudioInterfaceInfo
->State
.ControlInterfaceNumber    
= AudioControlInterface
->InterfaceNumber
; 
 113         AudioInterfaceInfo
->State
.StreamingInterfaceNumber  
= AudioStreamingInterface
->InterfaceNumber
; 
 114         AudioInterfaceInfo
->State
.EnabledStreamingAltIndex  
= AudioStreamingInterface
->AlternateSetting
; 
 115         AudioInterfaceInfo
->State
.IsActive 
= true; 
 117         return AUDIO_ENUMERROR_NoError
; 
 120 static uint8_t DCOMP_Audio_Host_NextAudioControlInterface(void* CurrentDescriptor
) 
 122         USB_Descriptor_Header_t
* Header 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
); 
 124         if (Header
->Type 
== DTYPE_Interface
) 
 126                 USB_Descriptor_Interface_t
* Interface 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Interface_t
); 
 128                 if ((Interface
->Class    
== AUDIO_CSCP_AudioClass
) && 
 129                     (Interface
->SubClass 
== AUDIO_CSCP_ControlSubclass
) && 
 130                     (Interface
->Protocol 
== AUDIO_CSCP_ControlProtocol
)) 
 132                         return DESCRIPTOR_SEARCH_Found
; 
 136         return DESCRIPTOR_SEARCH_NotFound
; 
 139 static uint8_t DCOMP_Audio_Host_NextAudioStreamInterface(void* CurrentDescriptor
) 
 141         USB_Descriptor_Header_t
* Header 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
); 
 143         if (Header
->Type 
== DTYPE_Interface
) 
 145                 USB_Descriptor_Interface_t
* Interface 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Interface_t
); 
 147                 if ((Interface
->Class    
== AUDIO_CSCP_AudioClass
) && 
 148                     (Interface
->SubClass 
== AUDIO_CSCP_AudioStreamingSubclass
) && 
 149                     (Interface
->Protocol 
== AUDIO_CSCP_StreamingProtocol
)) 
 151                         return DESCRIPTOR_SEARCH_Found
; 
 155         return DESCRIPTOR_SEARCH_NotFound
; 
 158 static uint8_t DCOMP_Audio_Host_NextAudioInterfaceDataEndpoint(void* CurrentDescriptor
) 
 160         USB_Descriptor_Header_t
* Header 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
); 
 162         if (Header
->Type 
== DTYPE_Endpoint
) 
 164                 USB_Descriptor_Endpoint_t
* Endpoint 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Endpoint_t
); 
 166                 if ((Endpoint
->Attributes 
& EP_TYPE_MASK
) == EP_TYPE_ISOCHRONOUS
) 
 167                   return DESCRIPTOR_SEARCH_Found
; 
 169         else if (Header
->Type 
== DTYPE_Interface
) 
 171                 return DESCRIPTOR_SEARCH_Fail
; 
 174         return DESCRIPTOR_SEARCH_NotFound
; 
 177 uint8_t Audio_Host_StartStopStreaming(USB_ClassInfo_Audio_Host_t
* const AudioInterfaceInfo
, 
 178                                                   const bool EnableStreaming
) 
 180         if (!(AudioInterfaceInfo
->State
.IsActive
)) 
 181           return HOST_SENDCONTROL_DeviceDisconnected
; 
 183         return USB_Host_SetInterfaceAltSetting(AudioInterfaceInfo
->State
.StreamingInterfaceNumber
, 
 184                                                EnableStreaming ? AudioInterfaceInfo
->State
.EnabledStreamingAltIndex 
: 0); 
 187 uint8_t Audio_Host_GetSetEndpointProperty(USB_ClassInfo_Audio_Host_t
* const AudioInterfaceInfo
, 
 188                                                       const uint8_t DataPipeIndex
, 
 189                                                       const uint8_t EndpointProperty
, 
 190                                                       const uint8_t EndpointControl
, 
 191                                                       const uint16_t DataLength
, 
 194         if (!(AudioInterfaceInfo
->State
.IsActive
)) 
 195           return HOST_SENDCONTROL_DeviceDisconnected
; 
 198         uint8_t EndpointAddress
; 
 200         if (EndpointProperty 
& 0x80) 
 201           RequestType 
= (REQDIR_DEVICETOHOST 
| REQTYPE_CLASS 
| REQREC_ENDPOINT
); 
 203           RequestType 
= (REQDIR_HOSTTODEVICE 
| REQTYPE_CLASS 
| REQREC_ENDPOINT
); 
 205         Pipe_SelectPipe(DataPipeIndex
); 
 206         EndpointAddress 
= Pipe_GetBoundEndpointAddress(); 
 208         USB_ControlRequest 
= (USB_Request_Header_t
) 
 210                         .bmRequestType 
= RequestType
, 
 211                         .bRequest      
= EndpointProperty
, 
 212                         .wValue        
= ((uint16_t)EndpointControl 
<< 8), 
 213                         .wIndex        
= EndpointAddress
, 
 214                         .wLength       
= DataLength
, 
 217         Pipe_SelectPipe(PIPE_CONTROLPIPE
); 
 219         return USB_Host_SendControlRequest(Data
);