3      Copyright (C) Dean Camera, 2010. 
   5   dean [at] fourwalledcubicle [dot] com 
   6       www.fourwalledcubicle.com 
  10   Copyright 2010  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 HID interface descriptor containing at least one Interrupt type IN endpoint and HID descriptor. 
  46  *  \return An error code from the \ref KeyboardHostWithParser_GetConfigDescriptorDataCodes_t enum. 
  48 uint8_t ProcessConfigurationDescriptor(void) 
  50         uint8_t  ConfigDescriptorData
[512]; 
  51         void*    CurrConfigLocation 
= ConfigDescriptorData
; 
  52         uint16_t CurrConfigBytesRem
; 
  54         /* Retrieve the entire configuration descriptor into the allocated buffer */ 
  55         switch (USB_Host_GetDeviceConfigDescriptor(1, &CurrConfigBytesRem
, ConfigDescriptorData
, sizeof(ConfigDescriptorData
))) 
  57                 case HOST_GETCONFIG_Successful
: 
  59                 case HOST_GETCONFIG_InvalidData
: 
  60                         return InvalidConfigDataReturned
; 
  61                 case HOST_GETCONFIG_BuffOverflow
: 
  62                         return DescriptorTooLarge
; 
  67         /* Get the keyboard interface from the configuration descriptor */ 
  68         if (USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
, 
  69                                       DComp_NextKeyboardInterface
) != DESCRIPTOR_SEARCH_COMP_Found
) 
  71                 /* Descriptor not found, error out */ 
  72                 return NoHIDInterfaceFound
; 
  75         /* Get the keyboard interface's HID descriptor */ 
  76         if (USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
, 
  77                                       DComp_NextHID
) != DESCRIPTOR_SEARCH_COMP_Found
) 
  79                 /* Descriptor not found, error out */ 
  80                 return NoHIDDescriptorFound
; 
  83         /* Save the HID report size for later use */ 
  84         HIDReportSize 
= DESCRIPTOR_CAST(CurrConfigLocation
, USB_Descriptor_HID_t
).HIDReportLength
; 
  86         /* Get the keyboard interface's data endpoint descriptor */ 
  87         if (USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
, 
  88                                       DComp_NextKeyboardInterfaceDataEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
) 
  90                 /* Descriptor not found, error out */ 
  91                 return NoEndpointFound
; 
  94         /* Retrieve the endpoint address from the endpoint descriptor */ 
  95         USB_Descriptor_Endpoint_t
* EndpointData 
= DESCRIPTOR_PCAST(CurrConfigLocation
, USB_Descriptor_Endpoint_t
); 
  97         /* Configure the keyboard data pipe */ 
  98         Pipe_ConfigurePipe(KEYBOARD_DATAPIPE
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_IN
, 
  99                            EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
, PIPE_BANK_SINGLE
); 
 101         /* Valid data found, return success */ 
 102         return SuccessfulConfigRead
; 
 105 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's 
 106  *  configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration 
 107  *  descriptor processing if an incompatible descriptor configuration is found. 
 109  *  This comparator searches for the next Interface descriptor of the correct Keyboard HID Class and Protocol values. 
 111  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum 
 113 uint8_t DComp_NextKeyboardInterface(void* CurrentDescriptor
) 
 115         if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
) 
 117                 /* Check the HID descriptor class and protocol, break out if correct class/protocol interface found */ 
 118                 if ((DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Class    
== KEYBOARD_CLASS
) && 
 119                     (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Protocol 
== KEYBOARD_PROTOCOL
)) 
 121                         return DESCRIPTOR_SEARCH_Found
; 
 125         return DESCRIPTOR_SEARCH_NotFound
; 
 128 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's 
 129  *  configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration 
 130  *  descriptor processing if an incompatible descriptor configuration is found. 
 132  *  This comparator searches for the next IN Endpoint descriptor inside the current interface descriptor, 
 133  *  aborting the search if another interface descriptor is found before the required endpoint. 
 135  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum 
 137 uint8_t DComp_NextKeyboardInterfaceDataEndpoint(void* CurrentDescriptor
) 
 139         if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
) 
 141                 if (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Endpoint_t
).EndpointAddress 
& ENDPOINT_DESCRIPTOR_DIR_IN
) 
 142                   return DESCRIPTOR_SEARCH_Found
; 
 144         else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
) 
 146                 return DESCRIPTOR_SEARCH_Fail
; 
 149         return DESCRIPTOR_SEARCH_NotFound
; 
 152 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's 
 153  *  configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration 
 154  *  descriptor processing if an incompatible descriptor configuration is found. 
 156  *  This comparator searches for the next HID descriptor within the current HID interface descriptor. 
 158  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum 
 160 uint8_t DComp_NextHID(void* CurrentDescriptor
) 
 162         if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_HID
) 
 163           return DESCRIPTOR_SEARCH_Found
; 
 165           return DESCRIPTOR_SEARCH_NotFound
;