Fix up the incomplete Webserver project so that it integrates with the uIP stack...
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Device / MassStorage.c
index fce8643..941db68 100644 (file)
@@ -1,21 +1,21 @@
 /*\r
              LUFA Library\r
-     Copyright (C) Dean Camera, 2009.\r
+     Copyright (C) Dean Camera, 2010.\r
               \r
   dean [at] fourwalledcubicle [dot] com\r
       www.fourwalledcubicle.com\r
 */\r
 \r
 /*\r
-  Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
-\r
-  Permission to use, copy, modify, and distribute this software\r
-  and its documentation for any purpose and without fee is hereby\r
-  granted, provided that the above copyright notice appear in all\r
-  copies and that both that the copyright notice and this\r
-  permission notice and warranty disclaimer appear in supporting\r
-  documentation, and that the name of the author not be used in\r
-  advertising or publicity pertaining to distribution of the\r
+  Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
+\r
+  Permission to use, copy, modify, distribute, and sell this \r
+  software and its documentation for any purpose is hereby granted\r
+  without fee, provided that the above copyright notice appear in \r
+  all copies and that both that the copyright notice and this\r
+  permission notice and warranty disclaimer appear in supporting \r
+  documentation, and that the name of the author not be used in \r
+  advertising or publicity pertaining to distribution of the \r
   software without specific, written prior permission.\r
 \r
   The author disclaim all warranties with regard to this\r
@@ -34,7 +34,7 @@
 #define  INCLUDE_FROM_MS_CLASS_DEVICE_C\r
 #include "MassStorage.h"\r
 \r
-static USB_ClassInfo_MS_Device_t* CallbackMSInterfaceInfo;\r
+static volatile bool* CallbackIsResetSource;\r
 \r
 void MS_Device_ProcessControlRequest(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)\r
 {\r
@@ -120,32 +120,37 @@ void MS_Device_USBTask(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
                        }\r
                        \r
                        MS_Device_ReturnCommandStatus(MSInterfaceInfo);\r
-                       \r
-                       if (MSInterfaceInfo->State.IsMassStoreReset)\r
-                       {\r
-                               Endpoint_ResetFIFO(MSInterfaceInfo->Config.DataOUTEndpointNumber);\r
-                               Endpoint_ResetFIFO(MSInterfaceInfo->Config.DataINEndpointNumber);\r
-                               \r
-                               Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);\r
-                               Endpoint_ClearStall();\r
-                               Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber);\r
-                               Endpoint_ClearStall();\r
-                       }\r
                }\r
        }\r
        \r
-       MSInterfaceInfo->State.IsMassStoreReset = false;\r
+       if (MSInterfaceInfo->State.IsMassStoreReset)\r
+       {\r
+               Endpoint_ResetFIFO(MSInterfaceInfo->Config.DataOUTEndpointNumber);\r
+               Endpoint_ResetFIFO(MSInterfaceInfo->Config.DataINEndpointNumber);\r
+               \r
+               Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);\r
+               Endpoint_ClearStall();\r
+               Endpoint_ResetDataToggle();\r
+               Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataINEndpointNumber);\r
+               Endpoint_ClearStall();\r
+               Endpoint_ResetDataToggle();\r
+\r
+               MSInterfaceInfo->State.IsMassStoreReset = false;\r
+       }\r
 }\r
 \r
 static bool MS_Device_ReadInCommandBlock(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)\r
 {\r
        Endpoint_SelectEndpoint(MSInterfaceInfo->Config.DataOUTEndpointNumber);\r
 \r
-       CallbackMSInterfaceInfo = MSInterfaceInfo;\r
-       Endpoint_Read_Stream_LE(&MSInterfaceInfo->State.CommandBlock,\r
-                               (sizeof(MS_CommandBlockWrapper_t) - 16),\r
-                               StreamCallback_MS_Device_AbortOnMassStoreReset);\r
-\r
+       CallbackIsResetSource = &MSInterfaceInfo->State.IsMassStoreReset;\r
+       if (Endpoint_Read_Stream_LE(&MSInterfaceInfo->State.CommandBlock,\r
+                                   (sizeof(MS_CommandBlockWrapper_t) - 16),\r
+                                   StreamCallback_MS_Device_AbortOnMassStoreReset))\r
+       {\r
+               return false;\r
+       }\r
+       \r
        if ((MSInterfaceInfo->State.CommandBlock.Signature         != MS_CBW_SIGNATURE)                  ||\r
            (MSInterfaceInfo->State.CommandBlock.LUN               >= MSInterfaceInfo->Config.TotalLUNs) ||\r
                (MSInterfaceInfo->State.CommandBlock.Flags              & 0x1F)                              ||\r
@@ -159,16 +164,16 @@ static bool MS_Device_ReadInCommandBlock(USB_ClassInfo_MS_Device_t* const MSInte
                return false;\r
        }\r
 \r
-       CallbackMSInterfaceInfo = MSInterfaceInfo;\r
-       Endpoint_Read_Stream_LE(&MSInterfaceInfo->State.CommandBlock.SCSICommandData,\r
-                               MSInterfaceInfo->State.CommandBlock.SCSICommandLength,\r
-                               StreamCallback_MS_Device_AbortOnMassStoreReset);\r
-                                                       \r
-       Endpoint_ClearOUT();\r
-         \r
-       if (MSInterfaceInfo->State.IsMassStoreReset)\r
-         return false;\r
+       CallbackIsResetSource = &MSInterfaceInfo->State.IsMassStoreReset;\r
+       if (Endpoint_Read_Stream_LE(&MSInterfaceInfo->State.CommandBlock.SCSICommandData,\r
+                                   MSInterfaceInfo->State.CommandBlock.SCSICommandLength,\r
+                                   StreamCallback_MS_Device_AbortOnMassStoreReset))\r
+       {\r
+               return false;\r
+       }\r
 \r
+       Endpoint_ClearOUT();\r
+       \r
        return true;\r
 }\r
 \r
@@ -178,7 +183,9 @@ static void MS_Device_ReturnCommandStatus(USB_ClassInfo_MS_Device_t* const MSInt
 \r
        while (Endpoint_IsStalled())\r
        {\r
+               #if !defined(INTERRUPT_CONTROL_ENDPOINT)\r
                USB_USBTask();\r
+               #endif\r
 \r
                if (MSInterfaceInfo->State.IsMassStoreReset)\r
                  return;\r
@@ -188,27 +195,31 @@ static void MS_Device_ReturnCommandStatus(USB_ClassInfo_MS_Device_t* const MSInt
 \r
        while (Endpoint_IsStalled())\r
        {\r
+               #if !defined(INTERRUPT_CONTROL_ENDPOINT)\r
                USB_USBTask();\r
-\r
+               #endif\r
+               \r
                if (MSInterfaceInfo->State.IsMassStoreReset)\r
                  return;\r
        }\r
        \r
-       CallbackMSInterfaceInfo = MSInterfaceInfo;\r
-       Endpoint_Write_Stream_LE(&MSInterfaceInfo->State.CommandStatus, sizeof(MS_CommandStatusWrapper_t),\r
-                                StreamCallback_MS_Device_AbortOnMassStoreReset);\r
-       \r
-       Endpoint_ClearIN();\r
+       CallbackIsResetSource = &MSInterfaceInfo->State.IsMassStoreReset;\r
+       if (Endpoint_Write_Stream_LE(&MSInterfaceInfo->State.CommandStatus, sizeof(MS_CommandStatusWrapper_t),\r
+                                    StreamCallback_MS_Device_AbortOnMassStoreReset))\r
+       {\r
+               return;\r
+       }\r
 \r
-       if (MSInterfaceInfo->State.IsMassStoreReset)\r
-         return;\r
+       Endpoint_ClearIN();\r
 }\r
 \r
 static uint8_t StreamCallback_MS_Device_AbortOnMassStoreReset(void)\r
 {\r
-       MS_Device_USBTask(CallbackMSInterfaceInfo);\r
+       #if !defined(INTERRUPT_CONTROL_ENDPOINT)\r
+       USB_USBTask();\r
+       #endif\r
 \r
-       if (CallbackMSInterfaceInfo->State.IsMassStoreReset)\r
+       if (*CallbackIsResetSource)\r
          return STREAMCALLBACK_Abort;\r
        else\r
          return STREAMCALLBACK_Continue;\r