3      Copyright (C) Dean Camera, 2012. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2012  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_DIR_MASK
) == ENDPOINT_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
, DataINEndpoint
->EndpointAddress
, DataINEndpoint
->EndpointSize
, 1); 
 136         /* Configure the CDC data OUT pipe */ 
 137         Pipe_ConfigurePipe(CDC_DATA_OUT_PIPE
, EP_TYPE_BULK
, DataOUTEndpoint
->EndpointAddress
, DataOUTEndpoint
->EndpointSize
, 1); 
 139         /* Configure the CDC notification pipe */ 
 140         Pipe_ConfigurePipe(CDC_NOTIFICATION_PIPE
, EP_TYPE_INTERRUPT
, NotificationEndpoint
->EndpointAddress
, NotificationEndpoint
->EndpointSize
, 1); 
 141         Pipe_SetInterruptPeriod(NotificationEndpoint
->PollingIntervalMS
); 
 143         /* Valid data found, return success */ 
 144         return SuccessfulConfigRead
; 
 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 Interface descriptor of the correct CDC control Class, Subclass and Protocol values. 
 153  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum 
 155 uint8_t DComp_NextCDCControlInterface(void* CurrentDescriptor
) 
 157         USB_Descriptor_Header_t
* Header 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
); 
 159         if (Header
->Type 
== DTYPE_Interface
) 
 161                 USB_Descriptor_Interface_t
* Interface 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Interface_t
); 
 163                 /* Check the CDC descriptor class, subclass and protocol, break out if correct control interface found */ 
 164                 if ((Interface
->Class    
== CDC_CSCP_CDCClass
)    && 
 165                     (Interface
->SubClass 
== CDC_CSCP_ACMSubclass
) && 
 166                     (Interface
->Protocol 
== CDC_CSCP_ATCommandProtocol
)) 
 168                         return DESCRIPTOR_SEARCH_Found
; 
 172         return DESCRIPTOR_SEARCH_NotFound
; 
 175 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's 
 176  *  configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration 
 177  *  descriptor processing if an incompatible descriptor configuration is found. 
 179  *  This comparator searches for the next Interface descriptor of the correct CDC data Class, Subclass and Protocol values. 
 181  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum 
 183 uint8_t DComp_NextCDCDataInterface(void* CurrentDescriptor
) 
 185         USB_Descriptor_Header_t
* Header 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
); 
 187         if (Header
->Type 
== DTYPE_Interface
) 
 189                 USB_Descriptor_Interface_t
* Interface 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Interface_t
); 
 191                 /* Check the CDC descriptor class, subclass and protocol, break out if correct data interface found */ 
 192                 if ((Interface
->Class    
== CDC_CSCP_CDCDataClass
)   && 
 193                     (Interface
->SubClass 
== CDC_CSCP_NoDataSubclass
) && 
 194                     (Interface
->Protocol 
== CDC_CSCP_NoDataProtocol
)) 
 196                         return DESCRIPTOR_SEARCH_Found
; 
 200         return DESCRIPTOR_SEARCH_NotFound
; 
 203 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's 
 204  *  configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration 
 205  *  descriptor processing if an incompatible descriptor configuration is found. 
 207  *  This comparator searches for the next bulk IN or OUT endpoint, or interrupt IN endpoint within the current interface, 
 208  *  aborting the search if another interface descriptor is found before the required endpoint (so that it may be compared 
 209  *  using a different comparator to determine if it is another CDC class interface). 
 211  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum 
 213 uint8_t DComp_NextCDCDataInterfaceEndpoint(void* CurrentDescriptor
) 
 215         USB_Descriptor_Header_t
* Header 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
); 
 217         if (Header
->Type 
== DTYPE_Endpoint
) 
 219                 USB_Descriptor_Endpoint_t
* Endpoint 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Endpoint_t
); 
 221                 if (((Endpoint
->Attributes 
& EP_TYPE_MASK
) == EP_TYPE_BULK
) || 
 222                     ((Endpoint
->Attributes 
& EP_TYPE_MASK
) == EP_TYPE_INTERRUPT
)) 
 224                         return DESCRIPTOR_SEARCH_Found
; 
 227         else if (Header
->Type 
== DTYPE_Interface
) 
 229                 return DESCRIPTOR_SEARCH_Fail
; 
 232         return DESCRIPTOR_SEARCH_NotFound
;