3      Copyright (C) Dean Camera, 2011. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2011  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 
  33  *  USB Device Configuration Descriptor processing routines, to determine the correct pipe configurations 
  34  *  needed to communication with an attached USB device. Descriptors are special  computer-readable structures 
  35  *  which the host requests upon device enumeration, to determine the device's capabilities and functions. 
  38 #include "ConfigDescriptor.h" 
  40 /** Index of the currently used Audio Streaming Interface within the device. */ 
  41 uint8_t StreamingInterfaceIndex      
= 0; 
  43 /** Alternative Setting of the currently used Audio Streaming Interface within the device. */ 
  44 uint8_t StreamingInterfaceAltSetting 
= 0; 
  46 /** Address of the streaming audio endpoint currently in use within the device. */ 
  47 uint8_t StreamingEndpointAddress     
= 0; 
  50 /** Reads and processes an attached device's descriptors, to determine compatibility and pipe configurations. This 
  51  *  routine will read in the entire configuration descriptor, and configure the hosts pipes to correctly communicate 
  52  *  with compatible devices. 
  54  *  This routine searches for a Streaming Audio interface descriptor containing a valid Isochronous audio endpoint. 
  56  *  \return An error code from the \ref RNDISHost_GetConfigDescriptorDataCodes_t enum. 
  58 uint8_t ProcessConfigurationDescriptor(void) 
  60         uint8_t  ConfigDescriptorData
[512]; 
  61         void*    CurrConfigLocation 
= ConfigDescriptorData
; 
  62         uint16_t CurrConfigBytesRem
; 
  64         USB_Descriptor_Interface_t
* AudioControlInterface   
= NULL
; 
  65         USB_Descriptor_Interface_t
* AudioStreamingInterface 
= NULL
; 
  66         USB_Descriptor_Endpoint_t
*  DataINEndpoint          
= NULL
; 
  68         /* Retrieve the entire configuration descriptor into the allocated buffer */ 
  69         switch (USB_Host_GetDeviceConfigDescriptor(1, &CurrConfigBytesRem
, ConfigDescriptorData
, sizeof(ConfigDescriptorData
))) 
  71                 case HOST_GETCONFIG_Successful
: 
  73                 case HOST_GETCONFIG_InvalidData
: 
  74                         return InvalidConfigDataReturned
; 
  75                 case HOST_GETCONFIG_BuffOverflow
: 
  76                         return DescriptorTooLarge
; 
  81         while (!(DataINEndpoint
)) 
  83                 /* See if we've found a likely compatible interface, and if there is an endpoint within that interface */ 
  84                 if (!(AudioControlInterface
) || 
  85                     USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
, 
  86                                               DComp_NextAudioInterfaceDataEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
) 
  88                         /* Check if we haven't found an Audio Control interface yet, or if we have run out of related Audio Streaming interfaces */ 
  89                         if (!(AudioControlInterface
) || 
  90                             USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
, 
  91                                                       DComp_NextAudioStreamInterface
) != DESCRIPTOR_SEARCH_COMP_Found
) 
  93                                 /* Find a new Audio Control interface if the current one doesn't contain a compatible streaming interface */ 
  94                                 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
, 
  95                                                                                           DComp_NextAudioControlInterface
) != DESCRIPTOR_SEARCH_COMP_Found
) 
  97                                         /* Descriptor not found, error out */ 
  98                                         return NoCompatibleInterfaceFound
; 
 101                                 /* Save the interface in case we need to refer back to it later */ 
 102                                 AudioControlInterface 
= DESCRIPTOR_PCAST(CurrConfigLocation
, USB_Descriptor_Interface_t
);                        
 104                                 /* Find the next Audio Streaming interface within that Audio Control interface */ 
 105                                 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
, 
 106                                                                                   DComp_NextAudioStreamInterface
) != DESCRIPTOR_SEARCH_COMP_Found
) 
 108                                         /* Descriptor not found, error out */ 
 109                                         return NoCompatibleInterfaceFound
; 
 113                         /* Save the interface in case we need to refer back to it later */ 
 114                         AudioStreamingInterface 
= DESCRIPTOR_PCAST(CurrConfigLocation
, USB_Descriptor_Interface_t
); 
 116                         /* Skip the remainder of the loop as we have not found an endpoint yet */ 
 120                 /* Retrieve the endpoint address from the endpoint descriptor */ 
 121                 USB_Descriptor_Endpoint_t
* EndpointData 
= DESCRIPTOR_PCAST(CurrConfigLocation
, USB_Descriptor_Endpoint_t
); 
 123                 /* Save the endpoint if it is an IN type endpoint */ 
 124                 if (EndpointData
->EndpointAddress 
& ENDPOINT_DESCRIPTOR_DIR_IN
) 
 125                   DataINEndpoint 
= EndpointData
; 
 128         StreamingInterfaceIndex      
= AudioStreamingInterface
->InterfaceNumber
; 
 129         StreamingInterfaceAltSetting 
= AudioStreamingInterface
->AlternateSetting
; 
 130         StreamingEndpointAddress     
= DataINEndpoint
->EndpointAddress
; 
 132         /* Configure the Audio data IN pipe */ 
 133         Pipe_ConfigurePipe(AUDIO_DATA_IN_PIPE
, EP_TYPE_ISOCHRONOUS
, PIPE_TOKEN_IN
, 
 134                            DataINEndpoint
->EndpointAddress
, DataINEndpoint
->EndpointSize
, PIPE_BANK_DOUBLE
); 
 136         /* Valid data found, return success */ 
 137         return SuccessfulConfigRead
; 
 140 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's 
 141  *  configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration 
 142  *  descriptor processing if an incompatible descriptor configuration is found. 
 144  *  This comparator searches for the next Interface descriptor of the correct Audio Control Class, Subclass and Protocol values. 
 146  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum 
 148 uint8_t DComp_NextAudioControlInterface(void* CurrentDescriptor
) 
 150         USB_Descriptor_Header_t
* Header 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
); 
 152         if (Header
->Type 
== DTYPE_Interface
) 
 154                 USB_Descriptor_Interface_t
* Interface 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Interface_t
); 
 156                 if ((Interface
->Class    
== AUDIO_CSCP_AudioClass
) && 
 157                     (Interface
->SubClass 
== AUDIO_CSCP_ControlSubclass
) && 
 158                     (Interface
->Protocol 
== AUDIO_CSCP_ControlProtocol
)) 
 160                         return DESCRIPTOR_SEARCH_Found
; 
 164         return DESCRIPTOR_SEARCH_NotFound
; 
 167 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's 
 168  *  configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration 
 169  *  descriptor processing if an incompatible descriptor configuration is found. 
 171  *  This comparator searches for the next Interface descriptor of the correct Audio Streaming Class, Subclass and Protocol values. 
 173  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum 
 175 uint8_t DComp_NextAudioStreamInterface(void* CurrentDescriptor
) 
 177         USB_Descriptor_Header_t
* Header 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
); 
 179         if (Header
->Type 
== DTYPE_Interface
) 
 181                 USB_Descriptor_Interface_t
* Interface 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Interface_t
); 
 183                 if ((Interface
->Class    
== AUDIO_CSCP_AudioClass
) && 
 184                     (Interface
->SubClass 
== AUDIO_CSCP_AudioStreamingSubclass
) && 
 185                     (Interface
->Protocol 
== AUDIO_CSCP_StreamingProtocol
)) 
 187                         return DESCRIPTOR_SEARCH_Found
; 
 191         return DESCRIPTOR_SEARCH_NotFound
; 
 194 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's 
 195  *  configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration 
 196  *  descriptor processing if an incompatible descriptor configuration is found. 
 198  *  This comparator searches for the next Isochronous Endpoint descriptor within the current interface, aborting the 
 199  *  search if another interface descriptor is found before the next endpoint. 
 201  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum 
 203 uint8_t DComp_NextAudioInterfaceDataEndpoint(void* CurrentDescriptor
) 
 205         USB_Descriptor_Header_t
* Header 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
); 
 207         if (Header
->Type 
== DTYPE_Endpoint
) 
 209                 USB_Descriptor_Endpoint_t
* Endpoint 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Endpoint_t
); 
 211                 if ((Endpoint
->Attributes 
& EP_TYPE_MASK
) == EP_TYPE_ISOCHRONOUS
) 
 212                   return DESCRIPTOR_SEARCH_Found
; 
 214         else if (Header
->Type 
== DTYPE_Interface
) 
 216                 return DESCRIPTOR_SEARCH_Fail
; 
 219         return DESCRIPTOR_SEARCH_NotFound
;