Make Control Endpoint stream transfers more reliable by adding in early aborts for...
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / Template / Template_Endpoint_Control_W.c
index 43a9f3d..6c1445e 100644 (file)
@@ -6,36 +6,28 @@ uint8_t TEMPLATE_FUNC_NAME (void* Buffer, uint16_t Length)
        if (Length > USB_ControlRequest.wLength)\r
          Length = USB_ControlRequest.wLength;\r
        \r
-       while (Length && !(Endpoint_IsOUTReceived()))\r
+       while (Length || LastPacketFull)\r
        {\r
-               while (!(Endpoint_IsINReady()))\r
-               {\r
-                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
-                         return ENDPOINT_RWCSTREAM_DeviceDisconnected;\r
-               }\r
-               \r
-               while (Length && (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize))\r
-               {\r
-                       TEMPLATE_TRANSFER_BYTE(DataStream);\r
-                       Length--;\r
-               }\r
+               if (Endpoint_IsSETUPReceived())\r
+                 return ENDPOINT_RWCSTREAM_HostAborted;\r
+\r
+               if (Endpoint_IsOUTReceived())\r
+                 break;\r
                \r
-               LastPacketFull = (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize);\r
-               Endpoint_ClearIN();\r
-       }\r
-       \r
-       if (Endpoint_IsOUTReceived())\r
-         return ENDPOINT_RWCSTREAM_HostAborted;\r
-       \r
-       if (LastPacketFull)\r
-       {\r
-               while (!(Endpoint_IsINReady()))\r
+               if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                 return ENDPOINT_RWCSTREAM_DeviceDisconnected;\r
+                 \r
+               if (Endpoint_IsINReady())\r
                {\r
-                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
-                         return ENDPOINT_RWCSTREAM_DeviceDisconnected;         \r
+                       while (Length && (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize))\r
+                       {\r
+                               TEMPLATE_TRANSFER_BYTE(DataStream);\r
+                               Length--;\r
+                       }\r
+                       \r
+                       LastPacketFull = (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize);\r
+                       Endpoint_ClearIN();\r
                }\r
-               \r
-               Endpoint_ClearIN();\r
        }\r
        \r
        while (!(Endpoint_IsOUTReceived()))\r