3      Copyright (C) Dean Camera, 2012. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2012  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 #include "../../../../Common/Common.h" 
  32 #if (ARCH == ARCH_AVR8) 
  34 #define  __INCLUDE_FROM_USB_DRIVER 
  35 #include "../USBMode.h" 
  37 #if defined(USB_CAN_BE_HOST) 
  39 #define  __INCLUDE_FROM_HOST_C 
  42 void USB_Host_ProcessNextHostState(void) 
  44         uint8_t ErrorCode    
= HOST_ENUMERROR_NoError
; 
  45         uint8_t SubErrorCode 
= HOST_ENUMERROR_NoError
; 
  47         static uint16_t WaitMSRemaining
; 
  48         static uint8_t  PostWaitState
; 
  50         switch (USB_HostState
) 
  52                 case HOST_STATE_WaitForDevice
: 
  55                                 if ((SubErrorCode 
= USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful
) 
  57                                         USB_HostState 
= PostWaitState
; 
  58                                         ErrorCode     
= HOST_ENUMERROR_WaitStage
; 
  62                                 if (!(--WaitMSRemaining
)) 
  63                                   USB_HostState 
= PostWaitState
; 
  67                 case HOST_STATE_Powered
: 
  68                         WaitMSRemaining 
= HOST_DEVICE_SETTLE_DELAY_MS
; 
  70                         USB_HostState 
= HOST_STATE_Powered_WaitForDeviceSettle
; 
  72                 case HOST_STATE_Powered_WaitForDeviceSettle
: 
  73                         if (WaitMSRemaining
--) 
  80                                 USB_Host_VBUS_Manual_Off(); 
  83                                 USB_Host_VBUS_Auto_Enable(); 
  84                                 USB_Host_VBUS_Auto_On(); 
  86                                 #if defined(NO_AUTO_VBUS_MANAGEMENT) 
  87                                 USB_Host_VBUS_Manual_Enable(); 
  88                                 USB_Host_VBUS_Manual_On(); 
  91                                 USB_HostState 
= HOST_STATE_Powered_WaitForConnect
; 
  95                 case HOST_STATE_Powered_WaitForConnect
: 
  96                         if (USB_INT_HasOccurred(USB_INT_DCONNI
)) 
  98                                 USB_INT_Clear(USB_INT_DCONNI
); 
  99                                 USB_INT_Clear(USB_INT_DDISCI
); 
 101                                 USB_INT_Clear(USB_INT_VBERRI
); 
 102                                 USB_INT_Enable(USB_INT_VBERRI
); 
 104                                 USB_Host_ResumeBus(); 
 107                                 HOST_TASK_NONBLOCK_WAIT(100, HOST_STATE_Powered_DoReset
); 
 111                 case HOST_STATE_Powered_DoReset
: 
 112                         USB_Host_ResetDevice(); 
 114                         HOST_TASK_NONBLOCK_WAIT(200, HOST_STATE_Powered_ConfigPipe
); 
 116                 case HOST_STATE_Powered_ConfigPipe
: 
 117                         if (!(Pipe_ConfigurePipe(PIPE_CONTROLPIPE
, EP_TYPE_CONTROL
, ENDPOINT_CONTROLEP
, PIPE_CONTROLPIPE_DEFAULT_SIZE
, 1))) 
 119                                 ErrorCode    
= HOST_ENUMERROR_PipeConfigError
; 
 124                         USB_HostState 
= HOST_STATE_Default
; 
 126                 case HOST_STATE_Default
: 
 127                         USB_ControlRequest 
= (USB_Request_Header_t
) 
 129                                         .bmRequestType 
= (REQDIR_DEVICETOHOST 
| REQTYPE_STANDARD 
| REQREC_DEVICE
), 
 130                                         .bRequest      
= REQ_GetDescriptor
, 
 131                                         .wValue        
= (DTYPE_Device 
<< 8), 
 136                         uint8_t DataBuffer
[8]; 
 138                         Pipe_SelectPipe(PIPE_CONTROLPIPE
); 
 139                         if ((SubErrorCode 
= USB_Host_SendControlRequest(DataBuffer
)) != HOST_SENDCONTROL_Successful
) 
 141                                 ErrorCode 
= HOST_ENUMERROR_ControlError
; 
 145                         USB_Host_ControlPipeSize 
= DataBuffer
[offsetof(USB_Descriptor_Device_t
, Endpoint0Size
)]; 
 147                         USB_Host_ResetDevice(); 
 149                         HOST_TASK_NONBLOCK_WAIT(200, HOST_STATE_Default_PostReset
); 
 151                 case HOST_STATE_Default_PostReset
: 
 152                         if (!(Pipe_ConfigurePipe(PIPE_CONTROLPIPE
, EP_TYPE_CONTROL
, ENDPOINT_CONTROLEP
, USB_Host_ControlPipeSize
, 1))) 
 154                                 ErrorCode    
= HOST_ENUMERROR_PipeConfigError
; 
 159                         USB_ControlRequest 
= (USB_Request_Header_t
) 
 161                                         .bmRequestType 
= (REQDIR_HOSTTODEVICE 
| REQTYPE_STANDARD 
| REQREC_DEVICE
), 
 162                                         .bRequest      
= REQ_SetAddress
, 
 163                                         .wValue        
= USB_HOST_DEVICEADDRESS
, 
 168                         if ((SubErrorCode 
= USB_Host_SendControlRequest(NULL
)) != HOST_SENDCONTROL_Successful
) 
 170                                 ErrorCode 
= HOST_ENUMERROR_ControlError
; 
 174                         HOST_TASK_NONBLOCK_WAIT(100, HOST_STATE_Default_PostAddressSet
); 
 176                 case HOST_STATE_Default_PostAddressSet
: 
 177                         USB_Host_SetDeviceAddress(USB_HOST_DEVICEADDRESS
); 
 179                         USB_HostState 
= HOST_STATE_Addressed
; 
 181                         EVENT_USB_Host_DeviceEnumerationComplete(); 
 185         if ((ErrorCode 
!= HOST_ENUMERROR_NoError
) && (USB_HostState 
!= HOST_STATE_Unattached
)) 
 187                 EVENT_USB_Host_DeviceEnumerationFailed(ErrorCode
, SubErrorCode
); 
 189                 USB_Host_VBUS_Auto_Off(); 
 191                 EVENT_USB_Host_DeviceUnattached(); 
 193                 USB_ResetInterface(); 
 197 uint8_t USB_Host_WaitMS(uint8_t MS
) 
 199         bool    BusSuspended 
= USB_Host_IsBusSuspended(); 
 200         uint8_t ErrorCode    
= HOST_WAITERROR_Successful
; 
 201         bool    HSOFIEnabled 
= USB_INT_IsEnabled(USB_INT_HSOFI
); 
 203         USB_INT_Disable(USB_INT_HSOFI
); 
 204         USB_INT_Clear(USB_INT_HSOFI
); 
 206         USB_Host_ResumeBus(); 
 210                 if (USB_INT_HasOccurred(USB_INT_HSOFI
)) 
 212                         USB_INT_Clear(USB_INT_HSOFI
); 
 216                 if ((USB_HostState 
== HOST_STATE_Unattached
) || (USB_CurrentMode 
!= USB_MODE_Host
)) 
 218                         ErrorCode 
= HOST_WAITERROR_DeviceDisconnect
; 
 226                         ErrorCode 
= HOST_WAITERROR_PipeError
; 
 231                 if (Pipe_IsStalled()) 
 234                         ErrorCode 
= HOST_WAITERROR_SetupStalled
; 
 241           USB_Host_SuspendBus(); 
 244           USB_INT_Enable(USB_INT_HSOFI
); 
 249 static void USB_Host_ResetDevice(void) 
 251         bool BusSuspended 
= USB_Host_IsBusSuspended(); 
 253         USB_INT_Disable(USB_INT_DDISCI
); 
 256         while (!(USB_Host_IsBusResetComplete())); 
 257         USB_Host_ResumeBus(); 
 259         USB_Host_ConfigurationNumber 
= 0; 
 261         bool HSOFIEnabled 
= USB_INT_IsEnabled(USB_INT_HSOFI
); 
 263         USB_INT_Disable(USB_INT_HSOFI
); 
 264         USB_INT_Clear(USB_INT_HSOFI
); 
 266         for (uint8_t MSRem 
= 10; MSRem 
!= 0; MSRem
--) 
 268                 /* Workaround for powerless-pull-up devices. After a USB bus reset, 
 269                    all disconnection interrupts are suppressed while a USB frame is 
 270                    looked for - if it is found within 10ms, the device is still 
 273                 if (USB_INT_HasOccurred(USB_INT_HSOFI
)) 
 275                         USB_INT_Clear(USB_INT_HSOFI
); 
 276                         USB_INT_Clear(USB_INT_DDISCI
); 
 284           USB_INT_Enable(USB_INT_HSOFI
); 
 287           USB_Host_SuspendBus(); 
 289         USB_INT_Enable(USB_INT_DDISCI
);