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 CDC interface descriptor containing bulk data IN and OUT endpoints, and an interrupt event endpoint. 
  46  *  \return An error code from the \ref CDCHost_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
* CDCControlInterface  
= NULL
; 
  55         USB_Descriptor_Endpoint_t
*  DataINEndpoint       
= NULL
; 
  56         USB_Descriptor_Endpoint_t
*  DataOUTEndpoint      
= NULL
; 
  57         USB_Descriptor_Endpoint_t
*  NotificationEndpoint 
= NULL
; 
  59         /* Retrieve the entire configuration descriptor into the allocated buffer */ 
  60         switch (USB_Host_GetDeviceConfigDescriptor(1, &CurrConfigBytesRem
, ConfigDescriptorData
, sizeof(ConfigDescriptorData
))) 
  62                 case HOST_GETCONFIG_Successful
: 
  64                 case HOST_GETCONFIG_InvalidData
: 
  65                         return InvalidConfigDataReturned
; 
  66                 case HOST_GETCONFIG_BuffOverflow
: 
  67                         return DescriptorTooLarge
; 
  72         while (!(DataINEndpoint
) || !(DataOUTEndpoint
) || !(NotificationEndpoint
)) 
  74                 /* See if we've found a likely compatible interface, and if there is an endpoint within that interface */ 
  75                 if (!(CDCControlInterface
) || 
  76                     USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
, 
  77                                               DComp_NextCDCDataInterfaceEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
) 
  79                         /* Check if we have already found the control interface's notification endpoint or not */ 
  80                         if (NotificationEndpoint
) 
  82                                 /* Get the next CDC data interface from the configuration descriptor */ 
  83                                 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
, 
  84                                                                                           DComp_NextCDCDataInterface
) != DESCRIPTOR_SEARCH_COMP_Found
) 
  86                                         /* Descriptor not found, error out */ 
  87                                         return NoCompatibleInterfaceFound
; 
  90                                 /* Clear any found endpoints */ 
  91                                 DataINEndpoint       
= NULL
; 
  92                                 DataOUTEndpoint      
= NULL
; 
  96                                 /* Get the next CDC control interface from the configuration descriptor */ 
  97                                 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
, 
  98                                                                                           DComp_NextCDCControlInterface
) != DESCRIPTOR_SEARCH_COMP_Found
) 
 100                                         /* Descriptor not found, error out */ 
 101                                         return NoCompatibleInterfaceFound
; 
 104                                 /* Save the interface in case we need to refer back to it later */ 
 105                                 CDCControlInterface 
= DESCRIPTOR_PCAST(CurrConfigLocation
, USB_Descriptor_Interface_t
); 
 107                                 /* Clear any found endpoints */ 
 108                                 NotificationEndpoint 
= NULL
; 
 111                         /* Skip the remainder of the loop as we have not found an endpoint yet */ 
 115                 /* Retrieve the endpoint address from the endpoint descriptor */ 
 116                 USB_Descriptor_Endpoint_t
* EndpointData 
= DESCRIPTOR_PCAST(CurrConfigLocation
, USB_Descriptor_Endpoint_t
); 
 118                 /* If the endpoint is a IN type endpoint */ 
 119                 if (EndpointData
->EndpointAddress 
& ENDPOINT_DESCRIPTOR_DIR_IN
) 
 121                         /* Check if the found endpoint is a interrupt or bulk type descriptor */ 
 122                         if ((EndpointData
->Attributes 
& EP_TYPE_MASK
) == EP_TYPE_INTERRUPT
) 
 123                           NotificationEndpoint 
= EndpointData
; 
 125                           DataINEndpoint 
= EndpointData
; 
 129                         DataOUTEndpoint 
= EndpointData
; 
 133         /* Configure the CDC data IN pipe */ 
 134         Pipe_ConfigurePipe(CDC_DATA_IN_PIPE
, EP_TYPE_BULK
, PIPE_TOKEN_IN
, 
 135                            DataINEndpoint
->EndpointAddress
, DataINEndpoint
->EndpointSize
, PIPE_BANK_SINGLE
); 
 137         /* Configure the CDC data OUT pipe */ 
 138         Pipe_ConfigurePipe(CDC_DATA_OUT_PIPE
, EP_TYPE_BULK
, PIPE_TOKEN_OUT
, 
 139                                            DataOUTEndpoint
->EndpointAddress
, DataOUTEndpoint
->EndpointSize
, PIPE_BANK_SINGLE
); 
 141         /* Configure the CDC notification pipe */ 
 142         Pipe_ConfigurePipe(CDC_NOTIFICATION_PIPE
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_IN
, 
 143                                            NotificationEndpoint
->EndpointAddress
, NotificationEndpoint
->EndpointSize
, PIPE_BANK_SINGLE
); 
 144         Pipe_SetInterruptPeriod(NotificationEndpoint
->PollingIntervalMS
); 
 146         /* Valid data found, return success */ 
 147         return SuccessfulConfigRead
; 
 150 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's 
 151  *  configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration 
 152  *  descriptor processing if an incompatible descriptor configuration is found. 
 154  *  This comparator searches for the next Interface descriptor of the correct CDC control Class, Subclass and Protocol values. 
 156  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum 
 158 uint8_t DComp_NextCDCControlInterface(void* CurrentDescriptor
) 
 160         USB_Descriptor_Header_t
* Header 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
); 
 162         if (Header
->Type 
== DTYPE_Interface
) 
 164                 USB_Descriptor_Interface_t
* Interface 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Interface_t
); 
 166                 /* Check the CDC descriptor class, subclass and protocol, break out if correct control interface found */ 
 167                 if ((Interface
->Class    
== CDC_CSCP_CDCClass
)    && 
 168                     (Interface
->SubClass 
== CDC_CSCP_ACMSubclass
) && 
 169                     (Interface
->Protocol 
== CDC_CSCP_ATCommandProtocol
)) 
 171                         return DESCRIPTOR_SEARCH_Found
; 
 175         return DESCRIPTOR_SEARCH_NotFound
; 
 178 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's 
 179  *  configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration 
 180  *  descriptor processing if an incompatible descriptor configuration is found. 
 182  *  This comparator searches for the next Interface descriptor of the correct CDC data Class, Subclass and Protocol values. 
 184  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum 
 186 uint8_t DComp_NextCDCDataInterface(void* CurrentDescriptor
) 
 188         USB_Descriptor_Header_t
* Header 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
); 
 190         if (Header
->Type 
== DTYPE_Interface
) 
 192                 USB_Descriptor_Interface_t
* Interface 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Interface_t
); 
 194                 /* Check the CDC descriptor class, subclass and protocol, break out if correct data interface found */ 
 195                 if ((Interface
->Class    
== CDC_CSCP_CDCDataClass
)   && 
 196                     (Interface
->SubClass 
== CDC_CSCP_NoDataSubclass
) && 
 197                     (Interface
->Protocol 
== CDC_CSCP_NoDataProtocol
)) 
 199                         return DESCRIPTOR_SEARCH_Found
; 
 203         return DESCRIPTOR_SEARCH_NotFound
; 
 206 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's 
 207  *  configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration 
 208  *  descriptor processing if an incompatible descriptor configuration is found. 
 210  *  This comparator searches for the next bulk IN or OUT endpoint, or interrupt IN endpoint within the current interface, 
 211  *  aborting the search if another interface descriptor is found before the required endpoint (so that it may be compared 
 212  *  using a different comparator to determine if it is another CDC class interface). 
 214  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum 
 216 uint8_t DComp_NextCDCDataInterfaceEndpoint(void* CurrentDescriptor
) 
 218         USB_Descriptor_Header_t
* Header 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
); 
 220         if (Header
->Type 
== DTYPE_Endpoint
) 
 222                 USB_Descriptor_Endpoint_t
* Endpoint 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Endpoint_t
); 
 224                 if (((Endpoint
->Attributes 
& EP_TYPE_MASK
) == EP_TYPE_BULK
) ||  
 225                     ((Endpoint
->Attributes 
& EP_TYPE_MASK
) == EP_TYPE_INTERRUPT
)) 
 227                         return DESCRIPTOR_SEARCH_Found
; 
 230         else if (Header
->Type 
== DTYPE_Interface
) 
 232                 return DESCRIPTOR_SEARCH_Fail
; 
 235         return DESCRIPTOR_SEARCH_NotFound
;