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 a HID interface descriptor containing at least one Interrupt type IN endpoint. 
  46  *  \return An error code from the \ref MouseHost_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
* HIDInterface   
= NULL
; 
  55         USB_Descriptor_Endpoint_t
*  DataINEndpoint 
= 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
; 
  70         while (!(DataINEndpoint
)) 
  72                 /* See if we've found a likely compatible interface, and if there is an endpoint within that interface */ 
  73                 if (!(HIDInterface
) || 
  74                     USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
, 
  75                                               DComp_NextMouseInterfaceDataEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
) 
  77                         /* Get the next HID interface from the configuration descriptor */ 
  78                         if (USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
, 
  79                                                                                   DComp_NextMouseInterface
) != DESCRIPTOR_SEARCH_COMP_Found
) 
  81                                 /* Descriptor not found, error out */ 
  82                                 return NoCompatibleInterfaceFound
; 
  85                         /* Save the interface in case we need to refer back to it later */ 
  86                         HIDInterface 
= DESCRIPTOR_PCAST(CurrConfigLocation
, USB_Descriptor_Interface_t
); 
  88                         /* Skip the remainder of the loop as we have not found an endpoint yet */ 
  92                 /* Retrieve the endpoint address from the endpoint descriptor */ 
  93                 USB_Descriptor_Endpoint_t
* EndpointData 
= DESCRIPTOR_PCAST(CurrConfigLocation
, USB_Descriptor_Endpoint_t
); 
  95                 /* If the endpoint is a IN type endpoint */ 
  96                 if ((EndpointData
->EndpointAddress 
& ENDPOINT_DIR_MASK
) == ENDPOINT_DIR_IN
) 
  97                   DataINEndpoint 
= EndpointData
; 
 100         /* Configure the HID data IN pipe */ 
 101         Pipe_ConfigurePipe(MOUSE_DATA_IN_PIPE
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_IN
, 
 102                            DataINEndpoint
->EndpointAddress
, DataINEndpoint
->EndpointSize
, PIPE_BANK_SINGLE
); 
 103         Pipe_SetInterruptPeriod(DataINEndpoint
->PollingIntervalMS
); 
 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 of the correct Mouse HID Class and Protocol values. 
 115  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum 
 117 uint8_t DComp_NextMouseInterface(void* CurrentDescriptor
) 
 119         USB_Descriptor_Header_t
* Header 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
); 
 121         /* Determine if the current descriptor is an interface descriptor */ 
 122         if (Header
->Type 
== DTYPE_Interface
) 
 124                 USB_Descriptor_Interface_t
* Interface 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Interface_t
); 
 126                 /* Check the HID descriptor class and protocol, break out if correct class/protocol interface found */ 
 127                 if ((Interface
->Class    
== HID_CSCP_HIDClass
) && 
 128                     (Interface
->Protocol 
== HID_CSCP_MouseBootProtocol
)) 
 130                         /* Indicate that the descriptor being searched for has been found */ 
 131                         return DESCRIPTOR_SEARCH_Found
; 
 135         /* Current descriptor does not match what this comparator is looking for */ 
 136         return DESCRIPTOR_SEARCH_NotFound
; 
 139 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's 
 140  *  configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration 
 141  *  descriptor processing if an incompatible descriptor configuration is found. 
 143  *  This comparator searches for the next Endpoint descriptor inside the current interface descriptor, aborting the 
 144  *  search if another interface descriptor is found before the required endpoint. 
 146  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum 
 148 uint8_t DComp_NextMouseInterfaceDataEndpoint(void* CurrentDescriptor
) 
 150         USB_Descriptor_Header_t
* Header 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
); 
 152         /* Determine the type of the current descriptor */ 
 153         if (Header
->Type 
== DTYPE_Endpoint
) 
 154           return DESCRIPTOR_SEARCH_Found
; 
 155         else if (Header
->Type 
== DTYPE_Interface
) 
 156           return DESCRIPTOR_SEARCH_Fail
; 
 158           return DESCRIPTOR_SEARCH_NotFound
;