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 
  31 #include "ConfigDescriptor.h" 
  33 uint8_t ProcessConfigurationDescriptor(void) 
  35         uint8_t* ConfigDescriptorData
; 
  36         uint16_t ConfigDescriptorSize
; 
  37         uint8_t  FoundEndpoints 
= 0; 
  39         /* Get Configuration Descriptor size from the device */ 
  40         if (USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize
, NULL
) != HOST_SENDCONTROL_Successful
) 
  41           return ControlErrorDuringConfigRead
; 
  43         /* Ensure that the Configuration Descriptor isn't too large */ 
  44         if (ConfigDescriptorSize 
> MAX_CONFIG_DESCRIPTOR_SIZE
) 
  45           return DescriptorTooLarge
; 
  47         /* Allocate enough memory for the entire config descriptor */ 
  48         ConfigDescriptorData 
= alloca(ConfigDescriptorSize
); 
  50         /* Retrieve the entire configuration descriptor into the allocated buffer */ 
  51         USB_GetDeviceConfigDescriptor(1, &ConfigDescriptorSize
, ConfigDescriptorData
); 
  53         /* Validate returned data - ensure first entry is a configuration header descriptor */ 
  54         if (DESCRIPTOR_TYPE(ConfigDescriptorData
) != DTYPE_Configuration
) 
  55           return InvalidConfigDataReturned
; 
  57         /* The bluetooth USB transport addendium mandates that the data (not streaming voice) endpoints 
  58            be in the first interface descriptor (interface 0) */ 
  59         USB_GetNextDescriptorOfType(&ConfigDescriptorSize
, &ConfigDescriptorData
, DTYPE_Interface
); 
  61         /* Ensure that an interface was found, and the end of the descriptor was not reached */ 
  62         if (!(ConfigDescriptorSize
)) 
  63           return NoInterfaceFound
; 
  65         /* Get the data IN, data OUT and event notification endpoints for the bluetooth interface */ 
  66         while (FoundEndpoints 
!= ((1 << BLUETOOTH_DATA_IN_PIPE
) | (1 << BLUETOOTH_DATA_OUT_PIPE
) | 
  67                                   (1 << BLUETOOTH_EVENTS_PIPE
))) 
  69                 /* Fetch the next endpoint from the current bluetooth interface */ 
  70                 if (USB_GetNextDescriptorComp(&ConfigDescriptorSize
, &ConfigDescriptorData
, 
  71                                                    NextInterfaceBluetoothDataEndpoint
)) 
  73                         /* Descriptor not found, error out */ 
  74                         return NoEndpointFound
; 
  77                 USB_Descriptor_Endpoint_t
* EndpointData 
= DESCRIPTOR_PCAST(ConfigDescriptorData
, USB_Descriptor_Endpoint_t
); 
  79                 /* Check if the endpoint is a bulk or interrupt type endpoint */ 
  80                 if ((EndpointData
->Attributes 
& EP_TYPE_MASK
) == EP_TYPE_INTERRUPT
) 
  82                         if (EndpointData
->EndpointAddress 
& ENDPOINT_DESCRIPTOR_DIR_IN
) 
  84                                 /* Configure the events IN pipe */ 
  85                                 Pipe_ConfigurePipe(BLUETOOTH_EVENTS_PIPE
, EP_TYPE_INTERRUPT
, PIPE_TOKEN_IN
, 
  86                                                                    EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
, 
  89                                 Pipe_SetInterruptPeriod(EndpointData
->PollingIntervalMS
); 
  91                                 /* Set the flag indicating that the events notification pipe has been found */ 
  92                                 FoundEndpoints 
|= (1 << BLUETOOTH_EVENTS_PIPE
);  
  97                         if (EndpointData
->EndpointAddress 
& ENDPOINT_DESCRIPTOR_DIR_IN
) 
  99                                 /* Configure the data IN pipe */ 
 100                                 Pipe_ConfigurePipe(BLUETOOTH_DATA_IN_PIPE
, EP_TYPE_BULK
, PIPE_TOKEN_IN
, 
 101                                                                    EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
, 
 104                                 /* Set the flag indicating that the data IN pipe has been found */ 
 105                                 FoundEndpoints 
|= (1 << BLUETOOTH_DATA_IN_PIPE
); 
 109                                 /* Configure the data OUT pipe */ 
 110                                 Pipe_ConfigurePipe(BLUETOOTH_DATA_OUT_PIPE
, EP_TYPE_BULK
, PIPE_TOKEN_OUT
, 
 111                                                                    EndpointData
->EndpointAddress
, EndpointData
->EndpointSize
, 
 114                                 /* Set the flag indicating that the data OUT pipe has been found */ 
 115                                 FoundEndpoints 
|= (1 << BLUETOOTH_DATA_OUT_PIPE
); 
 121         /* Valid data found, return success */ 
 122         return SuccessfulConfigRead
; 
 125 uint8_t NextInterfaceBluetoothDataEndpoint(void* CurrentDescriptor
) 
 127         /* PURPOSE: Find next interface endpoint descriptor before next interface descriptor */ 
 129         if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Endpoint
) 
 130           return DESCRIPTOR_SEARCH_Found
; 
 131         else if (DESCRIPTOR_TYPE(CurrentDescriptor
) == DTYPE_Interface
) 
 132           return DESCRIPTOR_SEARCH_Fail
; 
 134         return DESCRIPTOR_SEARCH_NotFound
;