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 
  34 #if defined(USB_CAN_BE_HOST) 
  36 #define  __INCLUDE_FROM_HOSTSTDREQ_C 
  37 #include "HostStandardReq.h" 
  39 uint8_t USB_Host_SendControlRequest(void* const BufferPtr
) 
  41         uint8_t* DataStream   
= (uint8_t*)BufferPtr
; 
  42         bool     BusSuspended 
= USB_Host_IsBusSuspended(); 
  43         uint8_t  ReturnStatus 
= HOST_SENDCONTROL_Successful
; 
  44         uint16_t DataLen      
= USB_ControlRequest
.wLength
; 
  48         if ((ReturnStatus 
= USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful
) 
  49           goto End_Of_Control_Send
; 
  51         Pipe_SetPipeToken(PIPE_TOKEN_SETUP
); 
  56         Pipe_Write_8(USB_ControlRequest
.bmRequestType
); 
  57         Pipe_Write_8(USB_ControlRequest
.bRequest
); 
  58         Pipe_Write_16_LE(USB_ControlRequest
.wValue
); 
  59         Pipe_Write_16_LE(USB_ControlRequest
.wIndex
); 
  60         Pipe_Write_16_LE(USB_ControlRequest
.wLength
); 
  64         if ((ReturnStatus 
= USB_Host_WaitForIOS(USB_HOST_WAITFOR_SetupSent
)) != HOST_SENDCONTROL_Successful
) 
  65           goto End_Of_Control_Send
; 
  69         if ((ReturnStatus 
= USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful
) 
  70           goto End_Of_Control_Send
; 
  72         if ((USB_ControlRequest
.bmRequestType 
& CONTROL_REQTYPE_DIRECTION
) == REQDIR_DEVICETOHOST
) 
  74                 Pipe_SetPipeToken(PIPE_TOKEN_IN
); 
  76                 if (DataStream 
!= NULL
) 
  82                                 if ((ReturnStatus 
= USB_Host_WaitForIOS(USB_HOST_WAITFOR_InReceived
)) != HOST_SENDCONTROL_Successful
) 
  83                                   goto End_Of_Control_Send
; 
  85                                 if (!(Pipe_BytesInPipe())) 
  88                                 while (Pipe_BytesInPipe() && DataLen
) 
  90                                         *(DataStream
++) = Pipe_Read_8(); 
  99                 Pipe_SetPipeToken(PIPE_TOKEN_OUT
); 
 102                 if ((ReturnStatus 
= USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady
)) != HOST_SENDCONTROL_Successful
) 
 103                   goto End_Of_Control_Send
; 
 107                 if ((ReturnStatus 
= USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady
)) != HOST_SENDCONTROL_Successful
) 
 108                   goto End_Of_Control_Send
; 
 112                 if (DataStream 
!= NULL
) 
 114                         Pipe_SetPipeToken(PIPE_TOKEN_OUT
); 
 119                                 if ((ReturnStatus 
= USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady
)) != HOST_SENDCONTROL_Successful
) 
 120                                   goto End_Of_Control_Send
; 
 122                                 while (DataLen 
&& (Pipe_BytesInPipe() < USB_ControlPipeSize
)) 
 124                                         Pipe_Write_8(*(DataStream
++)); 
 131                         if ((ReturnStatus 
= USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady
)) != HOST_SENDCONTROL_Successful
) 
 132                           goto End_Of_Control_Send
; 
 137                 Pipe_SetPipeToken(PIPE_TOKEN_IN
); 
 140                 if ((ReturnStatus 
= USB_Host_WaitForIOS(USB_HOST_WAITFOR_InReceived
)) != HOST_SENDCONTROL_Successful
) 
 141                   goto End_Of_Control_Send
; 
 150           USB_Host_SuspendBus(); 
 152         Pipe_ResetPipe(PIPE_CONTROLPIPE
); 
 157 static uint8_t USB_Host_WaitForIOS(const uint8_t WaitType
) 
 159         #if (USB_HOST_TIMEOUT_MS < 0xFF) 
 160         uint8_t  TimeoutCounter 
= USB_HOST_TIMEOUT_MS
; 
 162         uint16_t TimeoutCounter 
= USB_HOST_TIMEOUT_MS
; 
 165         while (!(((WaitType 
== USB_HOST_WAITFOR_SetupSent
)  && Pipe_IsSETUPSent())  || 
 166                  ((WaitType 
== USB_HOST_WAITFOR_InReceived
) && Pipe_IsINReceived()) || 
 167                  ((WaitType 
== USB_HOST_WAITFOR_OutReady
)   && Pipe_IsOUTReady()))) 
 171                 if ((ErrorCode 
= USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful
) 
 174                 if (!(TimeoutCounter
--)) 
 175                   return HOST_SENDCONTROL_SoftwareTimeOut
; 
 178         return HOST_SENDCONTROL_Successful
;