Added new USB_DeviceState variable to keep track of the current Device mode USB state.
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / Template / Template_Endpoint_Control_W.c
index 2796c64..43a9f3d 100644 (file)
@@ -8,7 +8,11 @@ uint8_t TEMPLATE_FUNC_NAME (void* Buffer, uint16_t Length)
        \r
        while (Length && !(Endpoint_IsOUTReceived()))\r
        {\r
-               while (!(Endpoint_IsINReady()));\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
@@ -25,11 +29,20 @@ uint8_t TEMPLATE_FUNC_NAME (void* Buffer, uint16_t Length)
        \r
        if (LastPacketFull)\r
        {\r
-               while (!(Endpoint_IsINReady()));\r
+               while (!(Endpoint_IsINReady()))\r
+               {\r
+                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                         return ENDPOINT_RWCSTREAM_DeviceDisconnected;         \r
+               }\r
+               \r
                Endpoint_ClearIN();\r
        }\r
        \r
-       while (!(Endpoint_IsOUTReceived()));\r
+       while (!(Endpoint_IsOUTReceived()))\r
+       {\r
+               if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                 return ENDPOINT_RWCSTREAM_DeviceDisconnected; \r
+       }\r
 \r
        return ENDPOINT_RWCSTREAM_NoError;\r
 }\r