3      Copyright (C) Dean Camera, 2011. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2011  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 the first interface containing bulk IN and OUT data endpoints. 
  46  *  \return An error code from the \ref AndroidHost_GetConfigDescriptorDataCodes_t enum. 
  48 uint8_t ProcessConfigurationDescriptor(void) 
  50         uint8_t  ConfigDescriptorData
[512]; 
  51         void*    CurrConfigLocation 
= ConfigDescriptorData
; 
  52         uint16_t CurrConfigBytesRem
; 
  54         USB_Descriptor_Endpoint_t
* DataINEndpoint  
= NULL
; 
  55         USB_Descriptor_Endpoint_t
* DataOUTEndpoint 
= NULL
; 
  57         /* Retrieve the entire configuration descriptor into the allocated buffer */ 
  58         switch (USB_Host_GetDeviceConfigDescriptor(1, &CurrConfigBytesRem
, ConfigDescriptorData
, sizeof(ConfigDescriptorData
))) 
  60                 case HOST_GETCONFIG_Successful
: 
  62                 case HOST_GETCONFIG_InvalidData
: 
  63                         return InvalidConfigDataReturned
; 
  64                 case HOST_GETCONFIG_BuffOverflow
: 
  65                         return DescriptorTooLarge
; 
  67                         return DevControlError
; 
  70         /* There should be only one compatible Android Accessory Mode interface in the device, attempt to find it */ 
  71         if (USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
, 
  72                                       DCOMP_NextAndroidAccessoryInterface
) != DESCRIPTOR_SEARCH_COMP_Found
) 
  74                 return NoCompatibleInterfaceFound
; 
  77         while (!(DataINEndpoint
) || !(DataOUTEndpoint
)) 
  79                 /* Get the next Android Accessory Mode interface's data endpoint descriptor */ 
  80                 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
, 
  81                                               DCOMP_NextInterfaceBulkEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
) 
  83                         /* Data endpoints not found within the first Android Accessory device interface, error out */ 
  84                         return NoCompatibleInterfaceFound
; 
  87                 /* Retrieve the endpoint address from the endpoint descriptor */ 
  88                 USB_Descriptor_Endpoint_t
* EndpointData 
= DESCRIPTOR_PCAST(CurrConfigLocation
, USB_Descriptor_Endpoint_t
); 
  90                 /* If the endpoint is a IN type endpoint */ 
  91                 if ((EndpointData
->EndpointAddress 
& ENDPOINT_DIR_MASK
) == ENDPOINT_DIR_IN
) 
  92                   DataINEndpoint  
= EndpointData
; 
  94                   DataOUTEndpoint 
= EndpointData
; 
  97         /* Configure the Android Accessory data IN pipe */ 
  98         Pipe_ConfigurePipe(ANDROID_DATA_IN_PIPE
, EP_TYPE_BULK
, PIPE_TOKEN_IN
, 
  99                            DataINEndpoint
->EndpointAddress
, DataINEndpoint
->EndpointSize
, PIPE_BANK_SINGLE
); 
 101         /* Configure the Android Accessory data OUT pipe */ 
 102         Pipe_ConfigurePipe(ANDROID_DATA_OUT_PIPE
, EP_TYPE_BULK
, PIPE_TOKEN_OUT
, 
 103                                            DataOUTEndpoint
->EndpointAddress
, DataOUTEndpoint
->EndpointSize
, PIPE_BANK_SINGLE
); 
 105         /* Valid data found, return success */ 
 106         return SuccessfulConfigRead
; 
 109 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's 
 110  *  configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration 
 111  *  descriptor processing if an incompatible descriptor configuration is found. 
 113  *  This comparator searches for the next Interface descriptor containing the correct Android Accessory Mode Class, Subclass 
 114  *  and Protocol values. 
 116  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum 
 118 uint8_t DCOMP_NextAndroidAccessoryInterface(void* const CurrentDescriptor
) 
 120         USB_Descriptor_Header_t
* Header 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
); 
 122         if (Header
->Type 
== DTYPE_Interface
) 
 124                 USB_Descriptor_Interface_t
* Interface 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Interface_t
); 
 126                 if ((Interface
->Class    
== ANDROID_INTERFACE_CLASS
)    && 
 127                     (Interface
->SubClass 
== ANDROID_INTERFACE_SUBCLASS
) && 
 128                     (Interface
->Protocol 
== ANDROID_INTERFACE_PROTOCOL
)) 
 130                         return DESCRIPTOR_SEARCH_Found
; 
 134         return DESCRIPTOR_SEARCH_NotFound
; 
 137 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's 
 138  *  configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration 
 139  *  descriptor processing if an incompatible descriptor configuration is found. 
 141  *  This comparator searches for the next bulk Endpoint descriptor inside the current interface descriptor, aborting the 
 142  *  search if another interface descriptor is found before the required endpoint. 
 144  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum 
 146 uint8_t DCOMP_NextInterfaceBulkEndpoint(void* const CurrentDescriptor
) 
 148         USB_Descriptor_Header_t
* Header 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
); 
 150         if (Header
->Type 
== DTYPE_Endpoint
) 
 152                 USB_Descriptor_Endpoint_t
* Endpoint 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Endpoint_t
); 
 154                 uint8_t EndpointType 
= (Endpoint
->Attributes 
& EP_TYPE_MASK
); 
 156                 if ((EndpointType 
== EP_TYPE_BULK
) && (!(Pipe_IsEndpointBound(Endpoint
->EndpointAddress
)))) 
 157                   return DESCRIPTOR_SEARCH_Found
; 
 159         else if (Header
->Type 
== DTYPE_Interface
) 
 161                 return DESCRIPTOR_SEARCH_Fail
; 
 164         return DESCRIPTOR_SEARCH_NotFound
;