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 
  31 #define  __INCLUDE_FROM_USB_DRIVER 
  32 #include "ConfigDescriptor.h" 
  34 #if defined(USB_CAN_BE_HOST) 
  35 uint8_t USB_Host_GetDeviceConfigDescriptor(uint8_t ConfigNumber
, uint16_t* const ConfigSizePtr
, 
  36                                            void* BufferPtr
, uint16_t BufferSize
) 
  39         uint8_t ConfigHeader
[sizeof(USB_Descriptor_Configuration_Header_t
)]; 
  41         USB_ControlRequest 
= (USB_Request_Header_t
) 
  43                         .bmRequestType 
= (REQDIR_DEVICETOHOST 
| REQTYPE_STANDARD 
| REQREC_DEVICE
), 
  44                         .bRequest      
= REQ_GetDescriptor
, 
  45                         .wValue        
= ((DTYPE_Configuration 
<< 8) | (ConfigNumber 
- 1)), 
  47                         .wLength       
= sizeof(USB_Descriptor_Configuration_Header_t
), 
  50         Pipe_SelectPipe(PIPE_CONTROLPIPE
); 
  52         if ((ErrorCode 
= USB_Host_SendControlRequest(ConfigHeader
)) != HOST_SENDCONTROL_Successful
) 
  55         *ConfigSizePtr 
= DESCRIPTOR_PCAST(ConfigHeader
, USB_Descriptor_Configuration_Header_t
)->TotalConfigurationSize
; 
  57         if (*ConfigSizePtr 
> BufferSize
) 
  58           return HOST_GETCONFIG_BuffOverflow
; 
  60         USB_ControlRequest
.wLength 
= *ConfigSizePtr
; 
  62         if ((ErrorCode 
= USB_Host_SendControlRequest(BufferPtr
)) != HOST_SENDCONTROL_Successful
) 
  65         if (DESCRIPTOR_TYPE(BufferPtr
) != DTYPE_Configuration
) 
  66           return HOST_GETCONFIG_InvalidData
; 
  68         return HOST_GETCONFIG_Successful
; 
  72 void USB_GetNextDescriptorOfType(uint16_t* const BytesRem
, 
  73                                  void** const CurrConfigLoc
, 
  78                 USB_GetNextDescriptor(BytesRem
, CurrConfigLoc
); 
  80                 if (DESCRIPTOR_TYPE(*CurrConfigLoc
) == Type
) 
  85 void USB_GetNextDescriptorOfTypeBefore(uint16_t* const BytesRem
, 
  86                                        void** const CurrConfigLoc
, 
  88                                        const uint8_t BeforeType
) 
  92                 USB_GetNextDescriptor(BytesRem
, CurrConfigLoc
); 
  94                 if (DESCRIPTOR_TYPE(*CurrConfigLoc
) == Type
) 
  98                 else if (DESCRIPTOR_TYPE(*CurrConfigLoc
) == BeforeType
) 
 106 void USB_GetNextDescriptorOfTypeAfter(uint16_t* const BytesRem
, 
 107                                       void** const CurrConfigLoc
, 
 109                                       const uint8_t AfterType
) 
 111         USB_GetNextDescriptorOfType(BytesRem
, CurrConfigLoc
, AfterType
); 
 114           USB_GetNextDescriptorOfType(BytesRem
, CurrConfigLoc
, Type
); 
 117 uint8_t USB_GetNextDescriptorComp(uint16_t* const BytesRem
, void** const CurrConfigLoc
, ConfigComparatorPtr_t 
const ComparatorRoutine
) 
 123                 uint8_t* PrevDescLoc  
= *CurrConfigLoc
; 
 124                 uint16_t PrevBytesRem 
= *BytesRem
; 
 126                 USB_GetNextDescriptor(BytesRem
, CurrConfigLoc
); 
 128                 if ((ErrorCode 
= ComparatorRoutine(*CurrConfigLoc
)) != DESCRIPTOR_SEARCH_NotFound
) 
 130                         if (ErrorCode 
== DESCRIPTOR_SEARCH_Fail
) 
 132                                 *CurrConfigLoc 
= PrevDescLoc
; 
 133                                 *BytesRem      
= PrevBytesRem
; 
 140         return DESCRIPTOR_SEARCH_COMP_EndOfDescriptor
;