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 BT interface descriptor containing bulk IN and OUT data endpoints. 
  46  *  \return An error code from the \ref BluetoothHost_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
; 
  56         USB_Descriptor_Endpoint_t
* EventsEndpoint  
= NULL
; 
  58         /* Retrieve the entire configuration descriptor into the allocated buffer */ 
  59         switch (USB_Host_GetDeviceConfigDescriptor(1, &CurrConfigBytesRem
, ConfigDescriptorData
, sizeof(ConfigDescriptorData
))) 
  61                 case HOST_GETCONFIG_Successful
: 
  63                 case HOST_GETCONFIG_InvalidData
: 
  64                         return InvalidConfigDataReturned
; 
  65                 case HOST_GETCONFIG_BuffOverflow
: 
  66                         return DescriptorTooLarge
; 
  68                         return DevControlError
; 
  71         /* The Bluetooth USB transport addendum mandates that the data (not streaming voice) endpoints 
  72            be in the first interface descriptor (interface 0) */ 
  73         USB_GetNextDescriptorOfType(&CurrConfigBytesRem
, &CurrConfigLocation
, DTYPE_Interface
); 
  75         /* Ensure that an interface was found, and the end of the descriptor was not reached */ 
  76         if (!(CurrConfigBytesRem
)) 
  77           return NoCompatibleInterfaceFound
; 
  79         while (!(DataINEndpoint
) || !(DataOUTEndpoint
)) 
  81                 /* Get the next Bluetooth interface's data endpoint descriptor */ 
  82                 if (USB_GetNextDescriptorComp(&CurrConfigBytesRem
, &CurrConfigLocation
, 
  83                                               DComp_NextInterfaceBluetoothDataEndpoint
) != DESCRIPTOR_SEARCH_COMP_Found
) 
  85                         /* Data endpoints not found within the first bluetooth device interface, error out */ 
  86                         return NoCompatibleInterfaceFound
; 
  89                 /* Retrieve the endpoint address from the endpoint descriptor */ 
  90                 USB_Descriptor_Endpoint_t
* EndpointData 
= DESCRIPTOR_PCAST(CurrConfigLocation
, USB_Descriptor_Endpoint_t
); 
  92                 /* If the endpoint is a IN type endpoint */ 
  93                 if ((EndpointData
->EndpointAddress 
& ENDPOINT_DIR_MASK
) == ENDPOINT_DIR_IN
) 
  95                         /* Check if the found endpoint is a interrupt or bulk type descriptor */ 
  96                         if ((EndpointData
->Attributes 
& EP_TYPE_MASK
) == EP_TYPE_INTERRUPT
) 
  97                           EventsEndpoint 
= EndpointData
; 
  99                           DataINEndpoint 
= EndpointData
; 
 103                         DataOUTEndpoint 
= EndpointData
; 
 107         /* Configure the Bluetooth data IN pipe */ 
 108         Pipe_ConfigurePipe(BLUETOOTH_DATA_IN_PIPE
, EP_TYPE_BULK
, DataINEndpoint
->EndpointAddress
, DataINEndpoint
->EndpointSize
, 1); 
 110         /* Configure the Bluetooth data OUT pipe */ 
 111         Pipe_ConfigurePipe(BLUETOOTH_DATA_OUT_PIPE
, EP_TYPE_BULK
, DataOUTEndpoint
->EndpointAddress
, DataOUTEndpoint
->EndpointSize
, 1); 
 113         /* Configure the Bluetooth events pipe */ 
 114         Pipe_ConfigurePipe(BLUETOOTH_EVENTS_PIPE
, EP_TYPE_INTERRUPT
, EventsEndpoint
->EndpointAddress
, EventsEndpoint
->EndpointSize
, 1); 
 115         Pipe_SetInterruptPeriod(EventsEndpoint
->PollingIntervalMS
); 
 117         /* Valid data found, return success */ 
 118         return SuccessfulConfigRead
; 
 121 /** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's 
 122  *  configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration 
 123  *  descriptor processing if an incompatible descriptor configuration is found. 
 125  *  This comparator searches for the next Endpoint descriptor inside the current interface descriptor, aborting the 
 126  *  search if another interface descriptor is found before the required endpoint. 
 128  *  \return A value from the DSEARCH_Return_ErrorCodes_t enum 
 130 uint8_t DComp_NextInterfaceBluetoothDataEndpoint(void* CurrentDescriptor
) 
 132         USB_Descriptor_Header_t
* Header 
= DESCRIPTOR_PCAST(CurrentDescriptor
, USB_Descriptor_Header_t
); 
 134         /* Determine the type of the current descriptor */ 
 135         if (Header
->Type 
== DTYPE_Endpoint
) 
 136           return DESCRIPTOR_SEARCH_Found
; 
 137         else if (Header
->Type 
== DTYPE_Interface
) 
 138           return DESCRIPTOR_SEARCH_Fail
; 
 140           return DESCRIPTOR_SEARCH_NotFound
;