Fixed broken DFU bootloader, added XPLAIN support for bootloader start when XCK jumpe...
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / Host.c
index 6c66083..ac7898a 100644 (file)
@@ -1,21 +1,21 @@
 /*
              LUFA Library
      Copyright (C) Dean Camera, 2010.
-              
+
   dean [at] fourwalledcubicle [dot] com
-      www.fourwalledcubicle.com
+           www.lufa-lib.org
 */
 
 /*
   Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com)
 
-  Permission to use, copy, modify, distribute, and sell this 
+  Permission to use, copy, modify, distribute, and sell this
   software and its documentation for any purpose is hereby granted
-  without fee, provided that the above copyright notice appear in 
+  without fee, provided that the above copyright notice appear in
   all copies and that both that the copyright notice and this
-  permission notice and warranty disclaimer appear in supporting 
-  documentation, and that the name of the author not be used in 
-  advertising or publicity pertaining to distribution of the 
+  permission notice and warranty disclaimer appear in supporting
+  documentation, and that the name of the author not be used in
+  advertising or publicity pertaining to distribution of the
   software without specific, written prior permission.
 
   The author disclaim all warranties with regard to this
@@ -55,15 +55,15 @@ void USB_Host_ProcessNextHostState(void)
                                        ErrorCode     = HOST_ENUMERROR_WaitStage;
                                        break;
                                }
-                               
+
                                if (!(--WaitMSRemaining))
                                  USB_HostState = PostWaitState;
                        }
-               
+
                        break;
                case HOST_STATE_Powered:
                        WaitMSRemaining = HOST_DEVICE_SETTLE_DELAY_MS;
-               
+
                        USB_HostState = HOST_STATE_Powered_WaitForDeviceSettle;
                        break;
                case HOST_STATE_Powered_WaitForDeviceSettle:
@@ -79,23 +79,23 @@ void USB_Host_ProcessNextHostState(void)
                                USB_OTGPAD_On();
                                USB_Host_VBUS_Auto_Enable();
                                USB_Host_VBUS_Auto_On();
-                               
+
                                USB_HostState = HOST_STATE_Powered_WaitForConnect;
                        }
-                       
+
                        break;
-               case HOST_STATE_Powered_WaitForConnect:         
+               case HOST_STATE_Powered_WaitForConnect:
                        if (USB_INT_HasOccurred(USB_INT_DCONNI))
-                       {       
+                       {
                                USB_INT_Clear(USB_INT_DCONNI);
                                USB_INT_Clear(USB_INT_DDISCI);
 
                                USB_INT_Clear(USB_INT_VBERRI);
                                USB_INT_Enable(USB_INT_VBERRI);
-                                       
+
                                USB_Host_ResumeBus();
                                Pipe_ClearPipes();
-                               
+
                                HOST_TASK_NONBLOCK_WAIT(100, HOST_STATE_Powered_DoReset);
                        }
 
@@ -108,8 +108,8 @@ void USB_Host_ProcessNextHostState(void)
                case HOST_STATE_Powered_ConfigPipe:
                        Pipe_ConfigurePipe(PIPE_CONTROLPIPE, EP_TYPE_CONTROL,
                                                           PIPE_TOKEN_SETUP, ENDPOINT_CONTROLEP,
-                                                          PIPE_CONTROLPIPE_DEFAULT_SIZE, PIPE_BANK_SINGLE);            
-               
+                                                          PIPE_CONTROLPIPE_DEFAULT_SIZE, PIPE_BANK_SINGLE);
+
                        if (!(Pipe_IsConfigured()))
                        {
                                ErrorCode    = HOST_ENUMERROR_PipeConfigError;
@@ -138,9 +138,9 @@ void USB_Host_ProcessNextHostState(void)
                        }
 
                        USB_ControlPipeSize = DataBuffer[offsetof(USB_Descriptor_Device_t, Endpoint0Size)];
-       
+
                        USB_Host_ResetDevice();
-                       
+
                        HOST_TASK_NONBLOCK_WAIT(200, HOST_STATE_Default_PostReset);
                        break;
                case HOST_STATE_Default_PostReset:
@@ -196,14 +196,13 @@ uint8_t USB_Host_WaitMS(uint8_t MS)
 {
        bool    BusSuspended = USB_Host_IsBusSuspended();
        uint8_t ErrorCode    = HOST_WAITERROR_Successful;
-       
-       USB_Host_ResumeBus();
-
-       bool HSOFIEnabled = USB_INT_IsEnabled(USB_INT_HSOFI);
+       bool    HSOFIEnabled = USB_INT_IsEnabled(USB_INT_HSOFI);
 
        USB_INT_Disable(USB_INT_HSOFI);
        USB_INT_Clear(USB_INT_HSOFI);
 
+       USB_Host_ResumeBus();
+
        while (MS)
        {
                if (USB_INT_HasOccurred(USB_INT_HSOFI))
@@ -211,11 +210,11 @@ uint8_t USB_Host_WaitMS(uint8_t MS)
                        USB_INT_Clear(USB_INT_HSOFI);
                        MS--;
                }
-                                       
-               if ((USB_HostState == HOST_STATE_Unattached) || (USB_CurrentMode == USB_MODE_DEVICE))
+
+               if ((USB_HostState == HOST_STATE_Unattached) || (USB_CurrentMode != USB_MODE_Host))
                {
                        ErrorCode = HOST_WAITERROR_DeviceDisconnect;
-                       
+
                        break;
                }
 
@@ -223,25 +222,25 @@ uint8_t USB_Host_WaitMS(uint8_t MS)
                {
                        Pipe_ClearError();
                        ErrorCode = HOST_WAITERROR_PipeError;
-                       
+
                        break;
                }
-               
+
                if (Pipe_IsStalled() == true)
                {
                        Pipe_ClearStall();
                        ErrorCode = HOST_WAITERROR_SetupStalled;
-                       
-                       break;                  
+
+                       break;
                }
        }
 
-       if (HSOFIEnabled)
-         USB_INT_Enable(USB_INT_HSOFI);
-
        if (BusSuspended)
          USB_Host_SuspendBus();
 
+       if (HSOFIEnabled)
+         USB_INT_Enable(USB_INT_HSOFI);
+
        return ErrorCode;
 }
 
@@ -250,7 +249,7 @@ static void USB_Host_ResetDevice(void)
        bool BusSuspended = USB_Host_IsBusSuspended();
 
        USB_INT_Disable(USB_INT_DDISCI);
-       
+
        USB_Host_ResetBus();
        while (!(USB_Host_IsBusResetComplete()));
        USB_Host_ResumeBus();
@@ -259,7 +258,7 @@ static void USB_Host_ResetDevice(void)
 
        USB_INT_Disable(USB_INT_HSOFI);
        USB_INT_Clear(USB_INT_HSOFI);
-       
+
        for (uint8_t MSRem = 10; MSRem != 0; MSRem--)
        {
                /* Workaround for powerless-pull-up devices. After a USB bus reset,
@@ -273,7 +272,7 @@ static void USB_Host_ResetDevice(void)
                        USB_INT_Clear(USB_INT_DDISCI);
                        break;
                }
-               
+
                _delay_ms(1);
        }
 
@@ -298,7 +297,7 @@ uint8_t USB_Host_SetDeviceConfiguration(const uint8_t ConfigNumber)
                };
 
        Pipe_SelectPipe(PIPE_CONTROLPIPE);
-       
+
        return USB_Host_SendControlRequest(NULL);
 }
 
@@ -314,7 +313,7 @@ uint8_t USB_Host_GetDeviceDescriptor(void* const DeviceDescriptorPtr)
                };
 
        Pipe_SelectPipe(PIPE_CONTROLPIPE);
-       
+
        return USB_Host_SendControlRequest(DeviceDescriptorPtr);
 }
 
@@ -332,27 +331,25 @@ uint8_t USB_Host_GetDeviceStringDescriptor(const uint8_t Index,
                };
 
        Pipe_SelectPipe(PIPE_CONTROLPIPE);
-       
+
        return USB_Host_SendControlRequest(Buffer);
 }
 
-uint8_t USB_Host_ClearPipeStall(uint8_t EndpointNum)
+uint8_t USB_Host_ClearPipeStall(const uint8_t EndpointNum)
 {
-       if (Pipe_GetPipeToken() == PIPE_TOKEN_IN)
-         EndpointNum |= ENDPOINT_DESCRIPTOR_DIR_IN;
-
        USB_ControlRequest = (USB_Request_Header_t)
                {
                        .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_ENDPOINT),
                        .bRequest      = REQ_ClearFeature,
-                       .wValue        = FEATURE_ENDPOINT_HALT,
+                       .wValue        = FEATURE_SEL_EndpointHalt,
                        .wIndex        = EndpointNum,
                        .wLength       = 0,
                };
 
        Pipe_SelectPipe(PIPE_CONTROLPIPE);
-       
+
        return USB_Host_SendControlRequest(NULL);
 }
 
 #endif
+