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
; 
  51         uint16_t ConfigDescriptorSize
; 
  52         uint8_t  FoundEndpoints 
= 0; 
  54         /* Get Configuration Descriptor size from the device */ 
  55         if (USB_GetDeviceConfigDescriptor(&ConfigDescriptorSize
, NULL
) != HOST_SENDCONTROL_Successful
) 
  58         /* Ensure that the Configuration Descriptor isn't too large */ 
  59         if (ConfigDescriptorSize 
> MAX_CONFIG_DESCRIPTOR_SIZE
) 
  60           return DescriptorTooLarge
; 
  62         /* Allocate enough memory for the entire config descriptor */ 
  63         ConfigDescriptorData 
= alloca(ConfigDescriptorSize
); 
  65         /* Retrieve the entire configuration descriptor into the allocated buffer */ 
  66         USB_GetDeviceConfigDescriptor(&ConfigDescriptorSize
, ConfigDescriptorData
); 
  68         /* Validate returned data - ensure first entry is a configuration header descriptor */ 
  69         if (DESCRIPTOR_TYPE(ConfigDescriptorData
) != DTYPE_Configuration
) 
  70           return InvalidConfigDataReturned
; 
  72         /* Get the CDC control interface from the configuration descriptor */ 
  73         if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
, 
  74                                       DComp_NextCDCControlInterface
) != DESCRIPTOR_SEARCH_COMP_Found
) 
  76                 /* Descriptor not found, error out */ 
  77                 return NoCDCInterfaceFound
; 
  80         /* Get the IN and OUT data endpoints for the CDC interface */ 
  81         while (FoundEndpoints 
!= ((1 << CDC_NOTIFICATIONPIPE
) | (1 << CDC_DATAPIPE_IN
) | (1 << CDC_DATAPIPE_OUT
))) 
  83                 /* Fetch the next bulk or interrupt endpoint from the current CDC interface */ 
  84                 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
, 
  85                                               DComp_NextInterfaceCDCDataEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
) 
  87                         /* Check to see if the control interface's notification pipe has been found, if so search for the data interface */ 
  88                         if (FoundEndpoints 
& (1 << CDC_NOTIFICATIONPIPE
)) 
  90                                 /* Get the next CDC data interface from the configuration descriptor (CDC class has two CDC interfaces) */ 
  91                                 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
,  
  92                                                               DComp_NextCDCDataInterface
) != DESCRIPTOR_SEARCH_COMP_Found
) 
  94                                         /* Descriptor not found, error out */ 
  95                                         return NoCDCInterfaceFound
; 
 100                                 /* Clear the found endpoints mask, since any already processed endpoints aren't in the CDC interface we need */ 
 103                                 /* Disable any already configured pipes from the invalid CDC interfaces */ 
 104                                 Pipe_SelectPipe(CDC_NOTIFICATIONPIPE
); 
 106                                 Pipe_SelectPipe(CDC_DATAPIPE_IN
); 
 108                                 Pipe_SelectPipe(CDC_DATAPIPE_OUT
); 
 111                                 /* Get the next CDC control interface from the configuration descriptor (CDC class has two CDC interfaces) */ 
 112                                 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
, 
 113                                                               DComp_NextCDCControlInterface
) != DESCRIPTOR_SEARCH_COMP_Found
) 
 115                                         /* Descriptor not found, error out */ 
 116                                         return NoCDCInterfaceFound
; 
 120                         /* Fetch the next bulk or interrupt endpoint from the current CDC interface */ 
 121                         if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
, 
 122                                                       DComp_NextInterfaceCDCDataEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
) 
 124                                 /* Descriptor not found, error out */ 
 125                                 return NoEndpointFound
; 
 129                 USB_Descriptor_Endpoint_t
* EndpointData 
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Endpoint_t
); 
 131                 /* Check if the found endpoint is a interrupt or bulk type descriptor */ 
 132                 if ((EndpointData
->Attributes 
& EP_TYPE_MASK
) == EP_TYPE_INTERRUPT
) 
 134                         /* If the endpoint is a IN type interrupt endpoint */ 
 135                         if (EndpointData
->EndpointAddress 
& ENDPOINT_DESCRIPTOR_DIR_IN
) 
 137                                 /* Configure the notification pipe */ 
 138                                 Pipe_ConfigurePipe(CDC_NOTIFICATIONPIPE
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_IN
, 
 139                                                                    EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
, PIPE_BANK_SINGLE
); 
 141                                 Pipe_SetInfiniteINRequests(); 
 142                                 Pipe_SetInterruptPeriod(EndpointData
->PollingIntervalMS
); 
 144                                 /* Set the flag indicating that the notification pipe has been found */ 
 145                                 FoundEndpoints 
|= (1 << CDC_NOTIFICATIONPIPE
); 
 150                         /* Check if the endpoint is a bulk IN or bulk OUT endpoint */ 
 151                         if (EndpointData
->EndpointAddress 
& ENDPOINT_DESCRIPTOR_DIR_IN
) 
 153                                 /* Configure the data IN pipe */ 
 154                                 Pipe_ConfigurePipe(CDC_DATAPIPE_IN
, EP_TYPE_BULK
, PIPE_TOKEN_IN
, 
 155                                                                    EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
, PIPE_BANK_SINGLE
); 
 157                                 Pipe_SetInfiniteINRequests(); 
 160                                 /* Set the flag indicating that the data IN pipe has been found */ 
 161                                 FoundEndpoints 
|= (1 << CDC_DATAPIPE_IN
); 
 165                                 /* Configure the data OUT pipe */ 
 166                                 Pipe_ConfigurePipe(CDC_DATAPIPE_OUT
, EP_TYPE_BULK
, PIPE_TOKEN_OUT
, 
 167                                                                    EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
, PIPE_BANK_SINGLE
); 
 171                                 /* Set the flag indicating that the data OUT pipe has been found */ 
 172                                 FoundEndpoints 
|= (1 << CDC_DATAPIPE_OUT
); 
 177         /* Valid data found, return success */ 
 178         return SuccessfulConfigRead
; 
 181 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's 
 182  *  configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration 
 183  *  descriptor processing if an incompatible descriptor configuration is found. 
 185  *  This comparator searches for the next Interface descriptor of the correct CDC control Class, Subclass and Protocol values. 
 187  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum 
 189 uint8_t DComp_NextCDCControlInterface(void* CurrentDescriptor
) 
 191         if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
) 
 193                 /* Check the CDC descriptor class, subclass and protocol, break out if correct control interface found */ 
 194                 if ((DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Class    
== CDC_CONTROL_CLASS
)    && 
 195                     (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).SubClass 
== CDC_CONTROL_SUBCLASS
) && 
 196                     (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Protocol 
== CDC_CONTROL_PROTOCOL
)) 
 198                         return DESCRIPTOR_SEARCH_Found
; 
 202         return DESCRIPTOR_SEARCH_NotFound
; 
 205 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's 
 206  *  configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration 
 207  *  descriptor processing if an incompatible descriptor configuration is found. 
 209  *  This comparator searches for the next Interface descriptor of the correct CDC data Class, Subclass and Protocol values. 
 211  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum 
 213 uint8_t DComp_NextCDCDataInterface(void* CurrentDescriptor
) 
 215         if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
) 
 217                 /* Check the CDC descriptor class, subclass and protocol, break out if correct data interface found */ 
 218                 if ((DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Class    
== CDC_DATA_CLASS
)    && 
 219                     (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).SubClass 
== CDC_DATA_SUBCLASS
) && 
 220                     (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Protocol 
== CDC_DATA_PROTOCOL
)) 
 222                         return DESCRIPTOR_SEARCH_Found
; 
 226         return DESCRIPTOR_SEARCH_NotFound
; 
 229 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's 
 230  *  configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration 
 231  *  descriptor processing if an incompatible descriptor configuration is found. 
 233  *  This comparator searches for the next bulk IN or OUT endpoint, or interrupt IN endpoint within the current interface, 
 234  *  aborting the search if another interface descriptor is found before the required endpoint (so that it may be compared 
 235  *  using a different comparator to determine if it is another CDC class interface). 
 237  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum 
 239 uint8_t DComp_NextInterfaceCDCDataEndpoint(void* CurrentDescriptor
) 
 241         if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
) 
 243                 uint8_t EndpointType 
= (DESCRIPTOR_CAST(CurrentDescriptor
, 
 244                                                         USB_Descriptor_Endpoint_t
).Attributes 
& EP_TYPE_MASK
); 
 246                 if ((EndpointType 
== EP_TYPE_BULK
) || (EndpointType 
== EP_TYPE_INTERRUPT
)) 
 247                   return DESCRIPTOR_SEARCH_Found
; 
 249         else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
) 
 251                 return DESCRIPTOR_SEARCH_Fail
; 
 254         return DESCRIPTOR_SEARCH_NotFound
;