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 "../HighLevel/USBMode.h" 
  33 #if defined(USB_CAN_BE_HOST) 
  35 #define  INCLUDE_FROM_HOSTCHAPTER9_C 
  36 #include "HostChapter9.h" 
  38 USB_Host_Request_Header_t USB_HostRequest
; 
  40 uint8_t USB_Host_SendControlRequest(void* BufferPtr
) 
  42         uint8_t* HeaderStream   
= (uint8_t*)&USB_HostRequest
; 
  43         uint8_t* DataStream     
= (uint8_t*)BufferPtr
; 
  44         bool     BusSuspended   
= USB_Host_IsBusSuspended(); 
  45         uint8_t  ReturnStatus   
= HOST_SENDCONTROL_Successful
; 
  46         uint16_t DataLen        
= USB_HostRequest
.wLength
; 
  50         if ((ReturnStatus 
= USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful
) 
  53         Pipe_SelectPipe(PIPE_CONTROLPIPE
); 
  54         Pipe_SetToken(PIPE_TOKEN_SETUP
); 
  55         Pipe_ClearErrorFlags(); 
  56         Pipe_ClearSetupSent(); 
  60         for (uint8_t HeaderByte 
= 0; HeaderByte 
< sizeof(USB_Host_Request_Header_t
); HeaderByte
++) 
  61           Pipe_Write_Byte(*(HeaderStream
++)); 
  65         if ((ReturnStatus 
= USB_Host_Wait_For_Setup_IOS(USB_HOST_WAITFOR_SetupSent
))) 
  66           goto End_Of_Control_Send
; 
  68         Pipe_ClearSetupSent(); 
  71         if ((ReturnStatus 
= USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful
) 
  72           goto End_Of_Control_Send
; 
  74         if ((USB_HostRequest
.bmRequestType 
& CONTROL_REQTYPE_DIRECTION
) == REQDIR_DEVICETOHOST
) 
  76                 Pipe_SetToken(PIPE_TOKEN_IN
); 
  78                 if (DataStream 
!= NULL
) 
  84                                 if ((ReturnStatus 
= USB_Host_Wait_For_Setup_IOS(USB_HOST_WAITFOR_InReceived
))) 
  85                                   goto End_Of_Control_Send
; 
  87                                 if (!(Pipe_BytesInPipe())) 
  90                                 while (Pipe_BytesInPipe() && DataLen
) 
  92                                         *(DataStream
++) = Pipe_Read_Byte(); 
 101                 Pipe_SetToken(PIPE_TOKEN_OUT
); 
 104                 if ((ReturnStatus 
= USB_Host_Wait_For_Setup_IOS(USB_HOST_WAITFOR_OutReady
))) 
 105                   goto End_Of_Control_Send
; 
 107                 Pipe_ClearSetupOUT(); 
 109                 if ((ReturnStatus 
= USB_Host_Wait_For_Setup_IOS(USB_HOST_WAITFOR_OutReady
))) 
 110                   goto End_Of_Control_Send
; 
 114                 if (DataStream 
!= NULL
) 
 116                         Pipe_SetToken(PIPE_TOKEN_OUT
); 
 121                                 if ((ReturnStatus 
= USB_Host_Wait_For_Setup_IOS(USB_HOST_WAITFOR_OutReady
))) 
 122                                   goto End_Of_Control_Send
; 
 124                                 while (DataLen 
&& (Pipe_BytesInPipe() < USB_ControlPipeSize
)) 
 126                                         Pipe_Write_Byte(*(DataStream
++)); 
 130                                 Pipe_ClearSetupOUT(); 
 133                         if ((ReturnStatus 
= USB_Host_Wait_For_Setup_IOS(USB_HOST_WAITFOR_OutReady
))) 
 134                           goto End_Of_Control_Send
; 
 139                 Pipe_SetToken(PIPE_TOKEN_IN
); 
 142                 if ((ReturnStatus 
= USB_Host_Wait_For_Setup_IOS(USB_HOST_WAITFOR_InReceived
))) 
 143                   goto End_Of_Control_Send
; 
 152           USB_Host_SuspendBus(); 
 154         Pipe_ResetPipe(PIPE_CONTROLPIPE
); 
 159 static uint8_t USB_Host_Wait_For_Setup_IOS(const uint8_t WaitType
) 
 161         uint16_t TimeoutCounter 
= USB_HOST_TIMEOUT_MS
; 
 163         while (!(((WaitType 
== USB_HOST_WAITFOR_SetupSent
)  && Pipe_IsSetupSent())       || 
 164                  ((WaitType 
== USB_HOST_WAITFOR_InReceived
) && Pipe_IsSetupINReceived()) || 
 165                  ((WaitType 
== USB_HOST_WAITFOR_OutReady
)   && Pipe_IsSetupOUTReady()))) 
 169                 if ((ErrorCode 
= USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful
) 
 172                 if (!(TimeoutCounter
--)) 
 173                   return HOST_SENDCONTROL_SoftwareTimeOut
; 
 176         return HOST_SENDCONTROL_Successful
;