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 /** Reads and processes an attached device's descriptors, to determine compatibility and pipe configurations. This 
  41  *  routine will read in the entire configuration descriptor, and configure the hosts pipes to correctly communicate 
  42  *  with compatible devices. 
  44  *  This routine searches for a MIDI interface descriptor pair containing bulk data IN and OUT endpoints. 
  46  *  \return An error code from the \ref MIDIHost_GetConfigDescriptorDataCodes_t enum. 
  48 uint8_t ProcessConfigurationDescriptor(void) 
  50         uint8_t  ConfigDescriptorData
[512]; 
  51         void*    CurrConfigLocation 
= ConfigDescriptorData
; 
  52         uint16_t CurrConfigBytesRem
; 
  54         USB_Descriptor_Interface_t
* MIDIInterface   
= NULL
; 
  55         USB_Descriptor_Endpoint_t
*  DataINEndpoint  
= NULL
; 
  56         USB_Descriptor_Endpoint_t
*  DataOUTEndpoint 
= NULL
; 
  58         /* Retrieve the entire configuration descriptor into the allocated buffer */ 
  59         switch (USB_Host_GetDeviceConfigDescriptor(1, &CurrConfigBytesRem
, ConfigDescriptorData
, sizeof(ConfigDescriptorData
))) 
  61                 case HOST_GETCONFIG_Successful
: 
  63                 case HOST_GETCONFIG_InvalidData
: 
  64                         return InvalidConfigDataReturned
; 
  65                 case HOST_GETCONFIG_BuffOverflow
: 
  66                         return DescriptorTooLarge
; 
  71         while (!(DataINEndpoint
) || !(DataOUTEndpoint
)) 
  73                 /* See if we've found a likely compatible interface, and if there is an endpoint within that interface */ 
  74                 if (!(MIDIInterface
) || 
  75                     USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
, 
  76                                               DComp_NextMIDIStreamingDataEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
) 
  78                         /* Get the next Mass Storage interface from the configuration descriptor */ 
  79                         if (USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
, 
  80                                                                                   DComp_NextMIDIStreamingInterface
) != DESCRIPTOR_SEARCH_COMP_Found
) 
  82                                 /* Descriptor not found, error out */ 
  83                                 return NoCompatibleInterfaceFound
; 
  86                         /* Save the interface in case we need to refer back to it later */ 
  87                         MIDIInterface 
= DESCRIPTOR_PCAST(CurrConfigLocation
, USB_Descriptor_Interface_t
); 
  89                         /* Clear any found endpoints */ 
  90                         DataINEndpoint  
= NULL
; 
  91                         DataOUTEndpoint 
= NULL
; 
  93                         /* Skip the remainder of the loop as we have not found an endpoint yet */ 
  97                 /* Retrieve the endpoint address from the endpoint descriptor */ 
  98                 USB_Descriptor_Endpoint_t
* EndpointData 
= DESCRIPTOR_PCAST(CurrConfigLocation
, USB_Descriptor_Endpoint_t
); 
 100                 /* If the endpoint is a IN type endpoint */ 
 101                 if (EndpointData
->EndpointAddress 
& ENDPOINT_DESCRIPTOR_DIR_IN
) 
 102                   DataINEndpoint  
= EndpointData
; 
 104                   DataOUTEndpoint 
= EndpointData
; 
 107         /* Configure the MIDI data IN pipe */ 
 108         Pipe_ConfigurePipe(MIDI_DATA_IN_PIPE
, EP_TYPE_BULK
, PIPE_TOKEN_IN
, 
 109                            DataINEndpoint
->EndpointAddress
, DataINEndpoint
->EndpointSize
, PIPE_BANK_SINGLE
); 
 111         /* Configure the MIDI data OUT pipe */ 
 112         Pipe_ConfigurePipe(MIDI_DATA_OUT_PIPE
, EP_TYPE_BULK
, PIPE_TOKEN_OUT
, 
 113                                            DataOUTEndpoint
->EndpointAddress
, DataOUTEndpoint
->EndpointSize
, PIPE_BANK_SINGLE
); 
 115         /* Valid data found, return success */ 
 116         return SuccessfulConfigRead
; 
 119 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's 
 120  *  configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration 
 121  *  descriptor processing if an incompatible descriptor configuration is found. 
 123  *  This comparator searches for the next Interface descriptor of the correct MIDI Streaming Class, Subclass and Protocol values. 
 125  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum 
 127 uint8_t DComp_NextMIDIStreamingInterface(void* CurrentDescriptor
) 
 129         USB_Descriptor_Header_t
* Header 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
); 
 131         if (Header
->Type 
== DTYPE_Interface
) 
 133                 USB_Descriptor_Interface_t
* Interface 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Interface_t
); 
 135                 /* Check the MIDI descriptor class, subclass and protocol, break out if correct data interface found */ 
 136                 if ((Interface
->Class    
== AUDIO_CSCP_AudioClass
)            && 
 137                     (Interface
->SubClass 
== AUDIO_CSCP_MIDIStreamingSubclass
) && 
 138                     (Interface
->Protocol 
== AUDIO_CSCP_StreamingProtocol
)) 
 140                         return DESCRIPTOR_SEARCH_Found
; 
 144         return DESCRIPTOR_SEARCH_NotFound
; 
 147 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's 
 148  *  configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration 
 149  *  descriptor processing if an incompatible descriptor configuration is found. 
 151  *  This comparator searches for the next bulk IN or OUT endpoint within the current interface, aborting the search if 
 152  *  another interface descriptor is found before the required endpoint. 
 154  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum 
 156 uint8_t DComp_NextMIDIStreamingDataEndpoint(void* CurrentDescriptor
) 
 158         USB_Descriptor_Header_t
* Header 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
); 
 160         if (Header
->Type 
== DTYPE_Endpoint
) 
 162                 USB_Descriptor_Endpoint_t
* Endpoint 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Endpoint_t
); 
 164                 /* Check the endpoint type, break out if correct BULK type endpoint found */ 
 165                 if ((Endpoint
->Attributes 
& EP_TYPE_MASK
) == EP_TYPE_BULK
) 
 166                   return DESCRIPTOR_SEARCH_Found
; 
 168         else if (Header
->Type 
== DTYPE_Interface
) 
 170                 return DESCRIPTOR_SEARCH_Fail
; 
 173         return DESCRIPTOR_SEARCH_NotFound
;