3      Copyright (C) Dean Camera, 2009. 
   5   dean [at] fourwalledcubicle [dot] com 
   6       www.fourwalledcubicle.com 
  10   Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  12   Permission to use, copy, modify, and distribute this software 
  13   and its documentation for any purpose and without fee is hereby 
  14   granted, provided that the above copyright notice appear in all 
  15   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 CDCHost_GetConfigDescriptorDataCodes_t enum. 
  48 uint8_t ProcessConfigurationDescriptor(void) 
  50         uint8_t  ConfigDescriptorData
[512]; 
  51         uint8_t* CurrConfigLocation 
= ConfigDescriptorData
; 
  52         uint16_t CurrConfigBytesRem
; 
  53         uint8_t  FoundEndpoints 
= 0; 
  55         /* Retrieve the entire configuration descriptor into the allocated buffer */ 
  56         switch (USB_GetDeviceConfigDescriptor(1, &CurrConfigBytesRem
, ConfigDescriptorData
, sizeof(ConfigDescriptorData
))) 
  58                 case HOST_GETCONFIG_Successful
: 
  60                 case HOST_GETCONFIG_InvalidData
: 
  61                         return InvalidConfigDataReturned
; 
  62                 case HOST_GETCONFIG_BuffOverflow
: 
  63                         return DescriptorTooLarge
; 
  68         /* Get the CDC control interface from the configuration descriptor */ 
  69         if (USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
, 
  70                                       DComp_NextCDCControlInterface
) != DESCRIPTOR_SEARCH_COMP_Found
) 
  72                 /* Descriptor not found, error out */ 
  73                 return NoCDCInterfaceFound
; 
  76         /* Get the IN and OUT data and IN notification endpoints for the CDC interface */ 
  77         while (FoundEndpoints 
!= ((1 << CDC_NOTIFICATIONPIPE
) | (1 << CDC_DATAPIPE_IN
) | (1 << CDC_DATAPIPE_OUT
))) 
  79                 /* Fetch the next bulk or interrupt endpoint from the current CDC interface */ 
  80                 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
, 
  81                                               DComp_NextCDCDataInterfaceEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
) 
  83                         /* Check to see if the control interface's notification pipe has been found, if so search for the data interface */ 
  84                         if (FoundEndpoints 
& (1 << CDC_NOTIFICATIONPIPE
)) 
  86                                 /* Get the next CDC data interface from the configuration descriptor (CDC class has two CDC interfaces) */ 
  87                                 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
,  
  88                                                               DComp_NextCDCDataInterface
) != DESCRIPTOR_SEARCH_COMP_Found
) 
  90                                         /* Descriptor not found, error out */ 
  91                                         return NoCDCInterfaceFound
; 
  96                                 /* Clear the found endpoints mask, since any already processed endpoints aren't in the CDC interface we need */ 
  99                                 /* Disable any already configured pipes from the invalid CDC interfaces */ 
 100                                 Pipe_SelectPipe(CDC_NOTIFICATIONPIPE
); 
 102                                 Pipe_SelectPipe(CDC_DATAPIPE_IN
); 
 104                                 Pipe_SelectPipe(CDC_DATAPIPE_OUT
); 
 107                                 /* Get the next CDC control interface from the configuration descriptor (CDC class has two CDC interfaces) */ 
 108                                 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
, 
 109                                                               DComp_NextCDCControlInterface
) != DESCRIPTOR_SEARCH_COMP_Found
) 
 111                                         /* Descriptor not found, error out */ 
 112                                         return NoCDCInterfaceFound
; 
 116                         /* Fetch the next bulk or interrupt endpoint from the current CDC interface */ 
 117                         if (USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
, 
 118                                                       DComp_NextCDCDataInterfaceEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
) 
 120                                 /* Descriptor not found, error out */ 
 121                                 return NoEndpointFound
; 
 125                 USB_Descriptor_Endpoint_t
* EndpointData 
= DESCRIPTOR_PCAST(CurrConfigLocation
, USB_Descriptor_Endpoint_t
); 
 127                 /* Check if the found endpoint is a interrupt or bulk type descriptor */ 
 128                 if ((EndpointData
->Attributes 
& EP_TYPE_MASK
) == EP_TYPE_INTERRUPT
) 
 130                         /* If the endpoint is a IN type interrupt endpoint */ 
 131                         if (EndpointData
->EndpointAddress 
& ENDPOINT_DESCRIPTOR_DIR_IN
) 
 133                                 /* Configure the notification pipe */ 
 134                                 Pipe_ConfigurePipe(CDC_NOTIFICATIONPIPE
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_IN
, 
 135                                                                    EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
, PIPE_BANK_SINGLE
); 
 137                                 Pipe_SetInterruptPeriod(EndpointData
->PollingIntervalMS
); 
 139                                 /* Set the flag indicating that the notification pipe has been found */ 
 140                                 FoundEndpoints 
|= (1 << CDC_NOTIFICATIONPIPE
); 
 145                         /* Check if the endpoint is a bulk IN or bulk OUT endpoint */ 
 146                         if (EndpointData
->EndpointAddress 
& ENDPOINT_DESCRIPTOR_DIR_IN
) 
 148                                 /* Configure the data IN pipe */ 
 149                                 Pipe_ConfigurePipe(CDC_DATAPIPE_IN
, EP_TYPE_BULK
, PIPE_TOKEN_IN
, 
 150                                                                    EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
, PIPE_BANK_SINGLE
); 
 152                                 /* Set the flag indicating that the data IN pipe has been found */ 
 153                                 FoundEndpoints 
|= (1 << CDC_DATAPIPE_IN
); 
 157                                 /* Configure the data OUT pipe */ 
 158                                 Pipe_ConfigurePipe(CDC_DATAPIPE_OUT
, EP_TYPE_BULK
, PIPE_TOKEN_OUT
, 
 159                                                                    EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
, PIPE_BANK_SINGLE
); 
 161                                 /* Set the flag indicating that the data OUT pipe has been found */ 
 162                                 FoundEndpoints 
|= (1 << CDC_DATAPIPE_OUT
); 
 167         /* Valid data found, return success */ 
 168         return SuccessfulConfigRead
; 
 171 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's 
 172  *  configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration 
 173  *  descriptor processing if an incompatible descriptor configuration is found. 
 175  *  This comparator searches for the next Interface descriptor of the correct CDC control Class, Subclass and Protocol values. 
 177  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum 
 179 uint8_t DComp_NextCDCControlInterface(void* CurrentDescriptor
) 
 181         if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
) 
 183                 /* Check the CDC descriptor class, subclass and protocol, break out if correct control interface found */ 
 184                 if ((DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Class    
== CDC_CONTROL_CLASS
)    && 
 185                     (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).SubClass 
== CDC_CONTROL_SUBCLASS
) && 
 186                     (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Protocol 
== CDC_CONTROL_PROTOCOL
)) 
 188                         return DESCRIPTOR_SEARCH_Found
; 
 192         return DESCRIPTOR_SEARCH_NotFound
; 
 195 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's 
 196  *  configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration 
 197  *  descriptor processing if an incompatible descriptor configuration is found. 
 199  *  This comparator searches for the next Interface descriptor of the correct CDC data Class, Subclass and Protocol values. 
 201  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum 
 203 uint8_t DComp_NextCDCDataInterface(void* CurrentDescriptor
) 
 205         if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
) 
 207                 /* Check the CDC descriptor class, subclass and protocol, break out if correct data interface found */ 
 208                 if ((DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Class    
== CDC_DATA_CLASS
)    && 
 209                     (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).SubClass 
== CDC_DATA_SUBCLASS
) && 
 210                     (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Protocol 
== CDC_DATA_PROTOCOL
)) 
 212                         return DESCRIPTOR_SEARCH_Found
; 
 216         return DESCRIPTOR_SEARCH_NotFound
; 
 219 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's 
 220  *  configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration 
 221  *  descriptor processing if an incompatible descriptor configuration is found. 
 223  *  This comparator searches for the next bulk IN or OUT endpoint, or interrupt IN endpoint within the current interface, 
 224  *  aborting the search if another interface descriptor is found before the required endpoint (so that it may be compared 
 225  *  using a different comparator to determine if it is another CDC class interface). 
 227  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum 
 229 uint8_t DComp_NextCDCDataInterfaceEndpoint(void* CurrentDescriptor
) 
 231         if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
) 
 233                 uint8_t EndpointType 
= (DESCRIPTOR_CAST(CurrentDescriptor
, 
 234                                                         USB_Descriptor_Endpoint_t
).Attributes 
& EP_TYPE_MASK
); 
 236                 if ((EndpointType 
== EP_TYPE_BULK
) || (EndpointType 
== EP_TYPE_INTERRUPT
)) 
 237                   return DESCRIPTOR_SEARCH_Found
; 
 239         else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
) 
 241                 return DESCRIPTOR_SEARCH_Fail
; 
 244         return DESCRIPTOR_SEARCH_NotFound
;