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 SI interface descriptor containing bulk IN and OUT data endpoints. 
  46  *  \return An error code from the \ref StillImageHost_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
* StillImageInterface 
= NULL
; 
  55         USB_Descriptor_Endpoint_t
*  DataINEndpoint      
= NULL
; 
  56         USB_Descriptor_Endpoint_t
*  DataOUTEndpoint     
= NULL
; 
  57         USB_Descriptor_Endpoint_t
*  EventsEndpoint      
= 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
)) 
  74                 /* See if we've found a likely compatible interface, and if there is an endpoint within that interface */ 
  75                 if (!(StillImageInterface
) || 
  76                     USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
, 
  77                                               DComp_NextStillImageInterfaceDataEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
) 
  79                         /* Get the next Still Image interface from the configuration descriptor */ 
  80                         if (USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
, 
  81                                                                                   DComp_NextStillImageInterface
) != DESCRIPTOR_SEARCH_COMP_Found
) 
  83                                 /* Descriptor not found, error out */ 
  84                                 return NoCompatibleInterfaceFound
; 
  87                         /* Save the interface in case we need to refer back to it later */ 
  88                         StillImageInterface 
= DESCRIPTOR_PCAST(CurrConfigLocation
, USB_Descriptor_Interface_t
); 
  90                         /* Clear any found endpoints */ 
  91                         DataINEndpoint  
= NULL
; 
  92                         DataOUTEndpoint 
= NULL
; 
  93                         EventsEndpoint  
= NULL
; 
  95                         /* Skip the remainder of the loop as we have not found an endpoint yet */ 
  99                 /* Retrieve the endpoint address from the endpoint descriptor */ 
 100                 USB_Descriptor_Endpoint_t
* EndpointData 
= DESCRIPTOR_PCAST(CurrConfigLocation
, USB_Descriptor_Endpoint_t
); 
 102                 /* If the endpoint is a IN type endpoint */ 
 103                 if ((EndpointData
->EndpointAddress 
& ENDPOINT_DIR_MASK
) == ENDPOINT_DIR_IN
) 
 105                         /* Check if the found endpoint is a interrupt or bulk type descriptor */ 
 106                         if ((EndpointData
->Attributes 
& EP_TYPE_MASK
) == EP_TYPE_INTERRUPT
) 
 107                           EventsEndpoint 
= EndpointData
; 
 109                           DataINEndpoint 
= EndpointData
; 
 113                         DataOUTEndpoint 
= EndpointData
; 
 117         /* Configure the Still Image data IN pipe */ 
 118         Pipe_ConfigurePipe(SIMAGE_DATA_IN_PIPE
, EP_TYPE_BULK
, PIPE_TOKEN_IN
, 
 119                            DataINEndpoint
->EndpointAddress
, DataINEndpoint
->EndpointSize
, PIPE_BANK_SINGLE
); 
 121         /* Configure the Still Image data OUT pipe */ 
 122         Pipe_ConfigurePipe(SIMAGE_DATA_OUT_PIPE
, EP_TYPE_BULK
, PIPE_TOKEN_OUT
, 
 123                                            DataOUTEndpoint
->EndpointAddress
, DataOUTEndpoint
->EndpointSize
, PIPE_BANK_SINGLE
); 
 125         /* Configure the Still Image events pipe */ 
 126         Pipe_ConfigurePipe(SIMAGE_EVENTS_PIPE
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_IN
, 
 127                                            EventsEndpoint
->EndpointAddress
, EventsEndpoint
->EndpointSize
, PIPE_BANK_SINGLE
); 
 128         Pipe_SetInterruptPeriod(EventsEndpoint
->PollingIntervalMS
); 
 130         /* Valid data found, return success */ 
 131         return SuccessfulConfigRead
; 
 134 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's 
 135  *  configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration 
 136  *  descriptor processing if an incompatible descriptor configuration is found. 
 138  *  This comparator searches for the next Interface descriptor of the correct Still Image Class, Subclass and Protocol values. 
 140  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum 
 142 uint8_t DComp_NextStillImageInterface(void* CurrentDescriptor
) 
 144         USB_Descriptor_Header_t
* Header 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
); 
 146         if (Header
->Type 
== DTYPE_Interface
) 
 148                 USB_Descriptor_Interface_t
* Interface 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Interface_t
); 
 150                 /* Check the descriptor class, subclass and protocol, break out if correct interface found */ 
 151                 if ((Interface
->Class    
== SI_CSCP_StillImageClass
)    && 
 152                     (Interface
->SubClass 
== SI_CSCP_StillImageSubclass
) && 
 153                     (Interface
->Protocol 
== SI_CSCP_BulkOnlyProtocol
)) 
 155                         return DESCRIPTOR_SEARCH_Found
; 
 159         return DESCRIPTOR_SEARCH_NotFound
; 
 162 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's 
 163  *  configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration 
 164  *  descriptor processing if an incompatible descriptor configuration is found. 
 166  *  This comparator searches for the next Interrupt or Bulk Endpoint descriptor of the current SI interface, aborting the 
 167  *  search if another interface descriptor is found before the next endpoint. 
 169  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum 
 171 uint8_t DComp_NextStillImageInterfaceDataEndpoint(void* CurrentDescriptor
) 
 173         USB_Descriptor_Header_t
* Header 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
); 
 175         if (Header
->Type 
== DTYPE_Endpoint
) 
 177                 USB_Descriptor_Endpoint_t
* Endpoint 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Endpoint_t
); 
 179                 /* Check the endpoint type, break out if correct BULK or INTERRUPT type endpoint found */ 
 180                 if (((Endpoint
->Attributes 
& EP_TYPE_MASK
) == EP_TYPE_BULK
) || 
 181                     ((Endpoint
->Attributes 
& EP_TYPE_MASK
) == EP_TYPE_INTERRUPT
)) 
 183                         return DESCRIPTOR_SEARCH_Found
; 
 186         else if (Header
->Type 
== DTYPE_Interface
) 
 188                 return DESCRIPTOR_SEARCH_Fail
; 
 191         return DESCRIPTOR_SEARCH_NotFound
;