Cleanups to the MassStorage Device demos, and the MassStorage Device Class driver.
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Device / MassStorage.c
index 43dc3f8..f061ec8 100644 (file)
@@ -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
@@ -130,8 +130,10 @@ void MS_Device_USBTask(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)
                \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
@@ -141,11 +143,14 @@ static bool MS_Device_ReadInCommandBlock(USB_ClassInfo_MS_Device_t* const MSInte
 {\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,14 +164,17 @@ 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
+       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 !(MSInterfaceInfo->State.IsMassStoreReset);\r
+       return true;\r
 }\r
 \r
 static void MS_Device_ReturnCommandStatus(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo)\r
@@ -175,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
@@ -185,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