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 #if defined(USB_CAN_BE_HOST) 
  34 uint8_t USB_Host_GetDeviceConfigDescriptor(uint8_t ConfigNumber
, uint16_t* const ConfigSizePtr
, 
  35                                            void* BufferPtr
, uint16_t BufferSize
) 
  38         uint8_t ConfigHeader
[sizeof(USB_Descriptor_Configuration_Header_t
)]; 
  40         USB_ControlRequest 
= (USB_Request_Header_t
) 
  42                         .bmRequestType 
= (REQDIR_DEVICETOHOST 
| REQTYPE_STANDARD 
| REQREC_DEVICE
), 
  43                         .bRequest      
= REQ_GetDescriptor
, 
  44                         .wValue        
= ((DTYPE_Configuration 
<< 8) | (ConfigNumber 
- 1)), 
  46                         .wLength       
= sizeof(USB_Descriptor_Configuration_Header_t
), 
  49         Pipe_SelectPipe(PIPE_CONTROLPIPE
); 
  51         if ((ErrorCode 
= USB_Host_SendControlRequest(ConfigHeader
)) != HOST_SENDCONTROL_Successful
) 
  54         *ConfigSizePtr 
= DESCRIPTOR_CAST(ConfigHeader
, USB_Descriptor_Configuration_Header_t
).TotalConfigurationSize
; 
  56         if (*ConfigSizePtr 
> BufferSize
) 
  57           return HOST_GETCONFIG_BuffOverflow
; 
  59         USB_ControlRequest
.wLength 
= *ConfigSizePtr
; 
  61         if ((ErrorCode 
= USB_Host_SendControlRequest(BufferPtr
)) != HOST_SENDCONTROL_Successful
) 
  64         if (DESCRIPTOR_TYPE(BufferPtr
) != DTYPE_Configuration
) 
  65           return HOST_GETCONFIG_InvalidData
; 
  67         return HOST_GETCONFIG_Successful
; 
  71 void USB_GetNextDescriptorOfType(uint16_t* const BytesRem
, 
  72                                  void** const CurrConfigLoc
, 
  77                 USB_GetNextDescriptor(BytesRem
, CurrConfigLoc
);    
  79                 if (DESCRIPTOR_TYPE(*CurrConfigLoc
) == Type
) 
  84 void USB_GetNextDescriptorOfTypeBefore(uint16_t* const BytesRem
, 
  85                                        void** const CurrConfigLoc
, 
  87                                        const uint8_t BeforeType
) 
  91                 USB_GetNextDescriptor(BytesRem
, CurrConfigLoc
); 
  93                 if (DESCRIPTOR_TYPE(*CurrConfigLoc
) == Type
) 
  97                 else if (DESCRIPTOR_TYPE(*CurrConfigLoc
) == BeforeType
) 
 105 void USB_GetNextDescriptorOfTypeAfter(uint16_t* const BytesRem
, 
 106                                       void** const CurrConfigLoc
, 
 108                                       const uint8_t AfterType
) 
 110         USB_GetNextDescriptorOfType(BytesRem
, CurrConfigLoc
, AfterType
); 
 113           USB_GetNextDescriptorOfType(BytesRem
, CurrConfigLoc
, Type
); 
 116 uint8_t USB_GetNextDescriptorComp(uint16_t* BytesRem
, void** CurrConfigLoc
, ConfigComparatorPtr_t ComparatorRoutine
) 
 122                 uint8_t*  PrevDescLoc  
= *CurrConfigLoc
; 
 123                 uint16_t  PrevBytesRem 
= *BytesRem
; 
 125                 USB_GetNextDescriptor(BytesRem
, CurrConfigLoc
); 
 127                 if ((ErrorCode 
= ComparatorRoutine(*CurrConfigLoc
)) != DESCRIPTOR_SEARCH_NotFound
) 
 129                         if (ErrorCode 
== DESCRIPTOR_SEARCH_Fail
) 
 131                                 *CurrConfigLoc 
= PrevDescLoc
; 
 132                                 *BytesRem      
= PrevBytesRem
; 
 139         return DESCRIPTOR_SEARCH_COMP_EndOfDescriptor
;