Applied STATIC_ENDPOINT_CONFIGURATION and FIXED_CONTROL_SIZE tokens to all Device...
[pub/lufa.git] / LUFA / Drivers / USB / LowLevel / Endpoint.c
index aa18358..efa1a2d 100644 (file)
@@ -59,7 +59,7 @@ bool Endpoint_ConfigureEndpointStatic(const uint8_t Number, const uint8_t UECFG0
        Endpoint_SelectEndpoint(Number);\r
        Endpoint_EnableEndpoint();\r
 \r
-       UECFG1X = 0;    \r
+       UECFG1X = 0;\r
 \r
        UECFG0X = UECFG0XData;\r
        UECFG1X = UECFG1XData;\r
@@ -88,8 +88,19 @@ uint8_t Endpoint_WaitUntilReady(void)
 \r
        USB_INT_Clear(USB_INT_SOFI);\r
 \r
-       while (!(Endpoint_ReadWriteAllowed()))\r
+       for (;;)\r
        {\r
+               if (Endpoint_GetEndpointDirection() == ENDPOINT_DIR_IN)\r
+               {\r
+                       if (Endpoint_IsINReady())\r
+                         return ENDPOINT_READYWAIT_NoError;\r
+               }\r
+               else\r
+               {\r
+                       if (Endpoint_IsOUTReceived())\r
+                         return ENDPOINT_READYWAIT_NoError;            \r
+               }\r
+               \r
                if (!(USB_IsConnected))\r
                  return ENDPOINT_READYWAIT_DeviceDisconnected;\r
                else if (Endpoint_IsStalled())\r
@@ -103,8 +114,6 @@ uint8_t Endpoint_WaitUntilReady(void)
                          return ENDPOINT_READYWAIT_Timeout;\r
                }\r
        }\r
-       \r
-       return ENDPOINT_READYWAIT_NoError;\r
 }\r
 \r
 uint8_t Endpoint_Discard_Stream(uint16_t Length\r
@@ -120,9 +129,9 @@ uint8_t Endpoint_Discard_Stream(uint16_t Length
 \r
        while (Length--)\r
        {\r
-               if (!(Endpoint_ReadWriteAllowed()))\r
+               if (!(Endpoint_IsReadWriteAllowed()))\r
                {\r
-                       Endpoint_ClearCurrentBank();\r
+                       Endpoint_ClearOUT();\r
 \r
                        #if !defined(NO_STREAM_CALLBACKS)\r
                        if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))\r
@@ -132,8 +141,10 @@ uint8_t Endpoint_Discard_Stream(uint16_t Length
                        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
                          return ErrorCode;\r
                }\r
-\r
-               Endpoint_Discard_Byte();\r
+               else\r
+               {\r
+                       Endpoint_Discard_Byte();\r
+               }\r
        }\r
        \r
        return ENDPOINT_RWSTREAM_ERROR_NoError;\r
@@ -153,9 +164,9 @@ uint8_t Endpoint_Write_Stream_LE(const void* Buffer, uint16_t Length
 \r
        while (Length--)\r
        {\r
-               if (!(Endpoint_ReadWriteAllowed()))\r
+               if (!(Endpoint_IsReadWriteAllowed()))\r
                {\r
-                       Endpoint_ClearCurrentBank();\r
+                       Endpoint_ClearIN();\r
                        \r
                        #if !defined(NO_STREAM_CALLBACKS)\r
                        if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))\r
@@ -165,8 +176,10 @@ uint8_t Endpoint_Write_Stream_LE(const void* Buffer, uint16_t Length
                        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
                          return ErrorCode;\r
                }\r
-\r
-               Endpoint_Write_Byte(*(DataStream++));\r
+               else\r
+               {\r
+                       Endpoint_Write_Byte(*(DataStream++));\r
+               }\r
        }\r
        \r
        return ENDPOINT_RWSTREAM_ERROR_NoError;\r
@@ -186,9 +199,9 @@ uint8_t Endpoint_Write_Stream_BE(const void* Buffer, uint16_t Length
 \r
        while (Length--)\r
        {\r
-               if (!(Endpoint_ReadWriteAllowed()))\r
+               if (!(Endpoint_IsReadWriteAllowed()))\r
                {\r
-                       Endpoint_ClearCurrentBank();\r
+                       Endpoint_ClearIN();\r
 \r
                        #if !defined(NO_STREAM_CALLBACKS)\r
                        if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))\r
@@ -198,8 +211,10 @@ uint8_t Endpoint_Write_Stream_BE(const void* Buffer, uint16_t Length
                        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
                          return ErrorCode;\r
                }\r
-\r
-               Endpoint_Write_Byte(*(DataStream--));\r
+               else\r
+               {\r
+                       Endpoint_Write_Byte(*(DataStream--));\r
+               }\r
        }\r
        \r
        return ENDPOINT_RWSTREAM_ERROR_NoError;\r
@@ -219,9 +234,9 @@ uint8_t Endpoint_Read_Stream_LE(void* Buffer, uint16_t Length
 \r
        while (Length--)\r
        {\r
-               if (!(Endpoint_ReadWriteAllowed()))\r
+               if (!(Endpoint_IsReadWriteAllowed()))\r
                {\r
-                       Endpoint_ClearCurrentBank();\r
+                       Endpoint_ClearOUT();\r
 \r
                        #if !defined(NO_STREAM_CALLBACKS)\r
                        if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))\r
@@ -231,8 +246,10 @@ uint8_t Endpoint_Read_Stream_LE(void* Buffer, uint16_t Length
                        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
                          return ErrorCode;\r
                }\r
-               \r
-               *(DataStream++) = Endpoint_Read_Byte();\r
+               else\r
+               {\r
+                       *(DataStream++) = Endpoint_Read_Byte();\r
+               }\r
        }\r
        \r
        return ENDPOINT_RWSTREAM_ERROR_NoError;\r
@@ -252,9 +269,9 @@ uint8_t Endpoint_Read_Stream_BE(void* Buffer, uint16_t Length
 \r
        while (Length--)\r
        {\r
-               if (!(Endpoint_ReadWriteAllowed()))\r
+               if (!(Endpoint_IsReadWriteAllowed()))\r
                {\r
-                       Endpoint_ClearCurrentBank();\r
+                       Endpoint_ClearOUT();\r
 \r
                        #if !defined(NO_STREAM_CALLBACKS)\r
                        if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))\r
@@ -264,8 +281,10 @@ uint8_t Endpoint_Read_Stream_BE(void* Buffer, uint16_t Length
                        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
                          return ErrorCode;\r
                }\r
-               \r
-               *(DataStream--) = Endpoint_Read_Byte();\r
+               else\r
+               {\r
+                       *(DataStream--) = Endpoint_Read_Byte();\r
+               }\r
        }\r
        \r
        return ENDPOINT_RWSTREAM_ERROR_NoError;\r
@@ -273,12 +292,13 @@ uint8_t Endpoint_Read_Stream_BE(void* Buffer, uint16_t Length
 \r
 uint8_t Endpoint_Write_Control_Stream_LE(const void* Buffer, uint16_t Length)\r
 {\r
-       uint8_t* DataStream = (uint8_t*)Buffer;\r
-       bool     SendZLP    = true;\r
+       uint8_t* DataStream    = (uint8_t*)Buffer;\r
+       bool     LastPacketFull = false;\r
+       bool     ShortTransfer = (Length < USB_ControlRequest.wLength);\r
        \r
-       while (Length && !(Endpoint_IsSetupOUTReceived()))\r
+       while (Length && !(Endpoint_IsOUTReceived()))\r
        {\r
-               while (!(Endpoint_IsSetupINReady()));\r
+               while (!(Endpoint_IsINReady()));\r
                \r
                while (Length && (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize))\r
                {\r
@@ -287,32 +307,33 @@ uint8_t Endpoint_Write_Control_Stream_LE(const void* Buffer, uint16_t Length)
                        Length--;\r
                }\r
                \r
-               SendZLP = (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize);\r
-               Endpoint_ClearSetupIN();\r
+               LastPacketFull = (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize);\r
+               Endpoint_ClearIN();\r
        }\r
        \r
-       if (Endpoint_IsSetupOUTReceived())\r
+       if (Endpoint_IsOUTReceived())\r
          return ENDPOINT_RWCSTREAM_ERROR_HostAborted;\r
        \r
-       if (SendZLP)\r
+       if (LastPacketFull || ShortTransfer)\r
        {\r
-               while (!(Endpoint_IsSetupINReady()));\r
-               Endpoint_ClearSetupIN();\r
+               while (!(Endpoint_IsINReady()));\r
+               Endpoint_ClearIN();\r
        }\r
        \r
-       while (!(Endpoint_IsSetupOUTReceived()));\r
+       while (!(Endpoint_IsOUTReceived()));\r
 \r
        return ENDPOINT_RWCSTREAM_ERROR_NoError;\r
 }\r
 \r
 uint8_t Endpoint_Write_Control_Stream_BE(const void* Buffer, uint16_t Length)\r
 {\r
-       uint8_t* DataStream = (uint8_t*)(Buffer + Length - 1);\r
-       bool     SendZLP    = true;\r
+       uint8_t* DataStream     = (uint8_t*)(Buffer + Length - 1);\r
+       bool     LastPacketFull = false;\r
+       bool     ShortTransfer  = (Length < USB_ControlRequest.wLength);\r
        \r
-       while (Length && !(Endpoint_IsSetupOUTReceived()))\r
+       while (Length && !(Endpoint_IsOUTReceived()))\r
        {\r
-               while (!(Endpoint_IsSetupINReady()));\r
+               while (!(Endpoint_IsINReady()));\r
                \r
                while (Length && (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize))\r
                {\r
@@ -321,20 +342,20 @@ uint8_t Endpoint_Write_Control_Stream_BE(const void* Buffer, uint16_t Length)
                        Length--;\r
                }\r
                \r
-               SendZLP = (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize);\r
-               Endpoint_ClearSetupIN();\r
+               LastPacketFull = (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize);\r
+               Endpoint_ClearIN();\r
        }\r
        \r
-       if (Endpoint_IsSetupOUTReceived())\r
+       if (Endpoint_IsOUTReceived())\r
          return ENDPOINT_RWCSTREAM_ERROR_HostAborted;\r
        \r
-       if (SendZLP)\r
+       if (LastPacketFull || ShortTransfer)\r
        {\r
-               while (!(Endpoint_IsSetupINReady()));\r
-               Endpoint_ClearSetupIN();\r
+               while (!(Endpoint_IsINReady()));\r
+               Endpoint_ClearIN();\r
        }\r
        \r
-       while (!(Endpoint_IsSetupOUTReceived()));\r
+       while (!(Endpoint_IsOUTReceived()));\r
 \r
        return ENDPOINT_RWCSTREAM_ERROR_NoError;\r
 }\r
@@ -345,7 +366,7 @@ uint8_t Endpoint_Read_Control_Stream_LE(void* Buffer, uint16_t Length)
        \r
        while (Length)\r
        {\r
-               while (!(Endpoint_IsSetupOUTReceived()));\r
+               while (!(Endpoint_IsOUTReceived()));\r
                \r
                while (Length && Endpoint_BytesInEndpoint())\r
                {\r
@@ -354,10 +375,10 @@ uint8_t Endpoint_Read_Control_Stream_LE(void* Buffer, uint16_t Length)
                        Length--;\r
                }\r
                \r
-               Endpoint_ClearSetupOUT();\r
+               Endpoint_ClearOUT();\r
        }\r
        \r
-       while (!(Endpoint_IsSetupINReady()));\r
+       while (!(Endpoint_IsINReady()));\r
        \r
        return ENDPOINT_RWCSTREAM_ERROR_NoError;\r
 }\r
@@ -368,7 +389,7 @@ uint8_t Endpoint_Read_Control_Stream_BE(void* Buffer, uint16_t Length)
        \r
        while (Length)\r
        {\r
-               while (!(Endpoint_IsSetupOUTReceived()));\r
+               while (!(Endpoint_IsOUTReceived()));\r
                \r
                while (Length && Endpoint_BytesInEndpoint())\r
                {\r
@@ -377,10 +398,10 @@ uint8_t Endpoint_Read_Control_Stream_BE(void* Buffer, uint16_t Length)
                        Length--;\r
                }\r
                \r
-               Endpoint_ClearSetupOUT();\r
+               Endpoint_ClearOUT();\r
        }\r
        \r
-       while (!(Endpoint_IsSetupINReady()));\r
+       while (!(Endpoint_IsINReady()));\r
 \r
        return ENDPOINT_RWCSTREAM_ERROR_NoError;\r
 }\r