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 HID interface descriptor containing at least one Interrupt type IN endpoint. 
  46  *  \return An error code from the GenericHIDHost_GetConfigDescriptorDataCodes_t enum. 
  48 uint8_t ProcessConfigurationDescriptor(void) 
  50         uint8_t* ConfigDescriptorData
; 
  51         uint16_t ConfigDescriptorSize
; 
  53         uint8_t  FoundEndpoints 
= 0; 
  55         /* Get Configuration Descriptor size from the device */ 
  56         if (USB_GetDeviceConfigDescriptor(&ConfigDescriptorSize
, NULL
) != HOST_SENDCONTROL_Successful
) 
  59         /* Ensure that the Configuration Descriptor isn't too large */ 
  60         if (ConfigDescriptorSize 
> MAX_CONFIG_DESCRIPTOR_SIZE
) 
  61           return DescriptorTooLarge
; 
  63         /* Allocate enough memory for the entire config descriptor */ 
  64         ConfigDescriptorData 
= alloca(ConfigDescriptorSize
); 
  66         /* Retrieve the entire configuration descriptor into the allocated buffer */ 
  67         USB_GetDeviceConfigDescriptor(&ConfigDescriptorSize
, ConfigDescriptorData
); 
  69         /* Validate returned data - ensure first entry is a configuration header descriptor */ 
  70         if (DESCRIPTOR_TYPE(ConfigDescriptorData
) != DTYPE_Configuration
) 
  71           return InvalidConfigDataReturned
; 
  73         /* Get the HID interface from the configuration descriptor */ 
  74         if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
, NextHIDInterface
)) 
  76                 /* Descriptor not found, error out */ 
  77                 return NoHIDInterfaceFound
; 
  80         while (FoundEndpoints 
!= ((1 << HID_DATA_IN_PIPE
) | (1 << HID_DATA_OUT_PIPE
))) 
  82                 /* Get the next HID interface's data endpoint descriptor */ 
  83                 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
, NextInterfaceHIDDataEndpoint
)) 
  85                         /* Not all HID devices have an OUT endpoint - if we've reached the end of the HID descriptor 
  86                          * but only found the mandatory IN endpoint, it's safe to continue with the device enumeration */ 
  87                         if (FoundEndpoints 
== (1 << HID_DATA_IN_PIPE
)) 
  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(ConfigDescriptorData
, USB_Descriptor_Endpoint_t
); 
  97                 /* If the endpoint is a IN type endpoint */ 
  98                 if (EndpointData
->EndpointAddress 
& ENDPOINT_DESCRIPTOR_DIR_IN
) 
 100                         /* Configure the HID data IN pipe */ 
 101                         Pipe_ConfigurePipe(HID_DATA_IN_PIPE
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_IN
, 
 102                                                            EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
, PIPE_BANK_SINGLE
); 
 104                         Pipe_SetInfiniteINRequests(); 
 106                         #if defined(INTERRUPT_DATA_PIPE) 
 107                         Pipe_SetInterruptPeriod(EndpointData
->PollingIntervalMS
); 
 109                         /* Enable the pipe IN interrupt for the data pipe */ 
 110                         USB_INT_Enable(PIPE_INT_IN
);     
 113                         FoundEndpoints 
|= (1 << HID_DATA_IN_PIPE
); 
 117                         /* Configure the HID data OUT pipe */ 
 118                         Pipe_ConfigurePipe(HID_DATA_OUT_PIPE
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_OUT
, 
 119                                                            EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
, PIPE_BANK_SINGLE
); 
 121                         FoundEndpoints 
|= (1 << HID_DATA_OUT_PIPE
);              
 125         /* Valid data found, return success */ 
 126         return SuccessfulConfigRead
; 
 129 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's 
 130  *  configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration 
 131  *  descriptor processing if an incompatible descriptor configuration is found. 
 133  *  This comparator searches for the next Interface descriptor of the correct HID Class value. 
 135  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum 
 137 DESCRIPTOR_COMPARATOR(NextHIDInterface
) 
 139         /* Determine if the current descriptor is an interface descriptor */ 
 140         if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
) 
 142                 /* Check the HID descriptor class and protocol, break out if correct class/protocol interface found */ 
 143                 if (DESCRIPTOR_CAST(CurrentDescriptor
, USB_Descriptor_Interface_t
).Class 
== HID_CLASS
) 
 145                         /* Indicate that the descriptor being searched for has been found */ 
 146                         return DESCRIPTOR_SEARCH_Found
; 
 150         /* Current descriptor does not match what this comparator is looking for */ 
 151         return DESCRIPTOR_SEARCH_NotFound
; 
 154 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's 
 155  *  configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration 
 156  *  descriptor processing if an incompatible descriptor configuration is found. 
 158  *  This comparator searches for the next Endpoint descriptor inside the current interface descriptor, 
 159  *  aborting the search if another interface descriptor is found before the required endpoint. 
 161  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum 
 163 DESCRIPTOR_COMPARATOR(NextInterfaceHIDDataEndpoint
) 
 165         /* Determine the type of the current descriptor */ 
 166         if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
) 
 168                 /* Indicate that the descriptor being searched for has been found */ 
 169                 return DESCRIPTOR_SEARCH_Found
; 
 171         else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
) 
 173                 /* Indicate that the search has failed prematurely and should be aborted */ 
 174                 return DESCRIPTOR_SEARCH_Fail
; 
 177         /* Current descriptor does not match what this comparator is looking for */ 
 178         return DESCRIPTOR_SEARCH_NotFound
;