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 SI interface descriptor containing bulk IN and OUT data endpoints. 
  46  *  \return An error code from the StillImageHost_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 Still Image interface from the configuration descriptor */ 
  73         if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
, NextStillImageInterface
)) 
  75                 /* Descriptor not found, error out */ 
  76                 return NoInterfaceFound
; 
  79         /* Get the IN and OUT data and event endpoints for the Still Image interface */ 
  80         while (FoundEndpoints 
!= ((1 << SIMAGE_EVENTS_PIPE
) | (1 << SIMAGE_DATA_IN_PIPE
) | (1 << SIMAGE_DATA_OUT_PIPE
))) 
  82                 /* Fetch the next endpoint from the current Still Image interface */ 
  83                 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
, 
  84                                                    NextSImageInterfaceDataEndpoint
)) 
  86                         /* Descriptor not found, error out */ 
  87                         return NoEndpointFound
; 
  90                 USB_Descriptor_Endpoint_t
* EndpointData 
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Endpoint_t
); 
  92                 /* Check if the found endpoint is a interrupt or bulk type descriptor */ 
  93                 if ((EndpointData
->Attributes 
& EP_TYPE_MASK
) == EP_TYPE_INTERRUPT
) 
  95                         /* If the endpoint is a IN type interrupt endpoint */ 
  96                         if (EndpointData
->EndpointAddress 
& ENDPOINT_DESCRIPTOR_DIR_IN
) 
  98                                 /* Configure the events pipe */ 
  99                                 Pipe_ConfigurePipe(SIMAGE_EVENTS_PIPE
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_IN
, 
 100                                                                    EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
, 
 103                                 Pipe_SetInfiniteINRequests(); 
 104                                 Pipe_SetInterruptPeriod(EndpointData
->PollingIntervalMS
); 
 106                                 /* Set the flag indicating that the events pipe has been found */ 
 107                                 FoundEndpoints 
|= (1 << SIMAGE_EVENTS_PIPE
); 
 112                         /* Check if the endpoint is a bulk IN or bulk OUT endpoint */ 
 113                         if (EndpointData
->EndpointAddress 
& ENDPOINT_DESCRIPTOR_DIR_IN
) 
 115                                 /* Configure the data IN pipe */ 
 116                                 Pipe_ConfigurePipe(SIMAGE_DATA_IN_PIPE
, EP_TYPE_BULK
, PIPE_TOKEN_IN
, 
 117                                                                    EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
, 
 120                                 Pipe_SetInfiniteINRequests(); 
 122                                 /* Set the flag indicating that the data IN pipe has been found */ 
 123                                 FoundEndpoints 
|= (1 << SIMAGE_DATA_IN_PIPE
); 
 127                                 /* Configure the data OUT pipe */ 
 128                                 Pipe_ConfigurePipe(SIMAGE_DATA_OUT_PIPE
, EP_TYPE_BULK
, PIPE_TOKEN_OUT
, 
 129                                                                    EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
, 
 132                                 /* Set the flag indicating that the data OUT pipe has been found */ 
 133                                 FoundEndpoints 
|= (1 << SIMAGE_DATA_OUT_PIPE
); 
 138         /* Valid data found, return success */ 
 139         return SuccessfulConfigRead
; 
 142 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's 
 143  *  configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration 
 144  *  descriptor processing if an incompatible descriptor configuration is found. 
 146  *  This comparator searches for the next Interface descriptor of the correct Still Image Class, Subclass and Protocol values. 
 148  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum 
 150 DESCRIPTOR_COMPARATOR(NextStillImageInterface
) 
 152         if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
) 
 154                 /* Check the descriptor class and protocol, break out if correct class/protocol interface found */ 
 155                 if ((DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Class    
== SIMAGE_CLASS
)    && 
 156                     (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).SubClass 
== SIMAGE_SUBCLASS
) && 
 157                     (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Protocol 
== SIMAGE_PROTOCOL
)) 
 159                         return DESCRIPTOR_SEARCH_Found
; 
 163         return DESCRIPTOR_SEARCH_NotFound
; 
 166 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's 
 167  *  configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration 
 168  *  descriptor processing if an incompatible descriptor configuration is found. 
 170  *  This comparator searches for the next Interrupt or Bulk Endpoint descriptor of the current SI interface, aborting the 
 171  *  search if another interface descriptor is found before the next endpoint. 
 173  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum 
 175 DESCRIPTOR_COMPARATOR(NextSImageInterfaceDataEndpoint
) 
 177         if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
) 
 179                 uint8_t EndpointType 
= (DESCRIPTOR_CAST(CurrentDescriptor
, 
 180                                                         USB_Descriptor_Endpoint_t
).Attributes 
& EP_TYPE_MASK
); 
 182                 if ((EndpointType 
== EP_TYPE_BULK
) || (EndpointType 
== EP_TYPE_INTERRUPT
)) 
 183                   return DESCRIPTOR_SEARCH_Found
; 
 185         else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
) 
 187                 return DESCRIPTOR_SEARCH_Fail
; 
 190         return DESCRIPTOR_SEARCH_NotFound
;