Fixed inability to enumerate any devices while in host mode (broken in a previous...
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / Endpoint.c
index da2925e..bd264b1 100644 (file)
 uint8_t USB_ControlEndpointSize = ENDPOINT_CONTROLEP_DEFAULT_SIZE;\r
 #endif\r
 \r
-#if !defined(STATIC_ENDPOINT_CONFIGURATION)\r
-bool Endpoint_ConfigureEndpoint(const uint8_t  Number, const uint8_t Type, const uint8_t Direction,\r
-                                           const uint16_t Size, const uint8_t Banks)\r
+uint8_t Endpoint_BytesToEPSizeMaskDynamic(const uint16_t Size)\r
 {\r
-       Endpoint_SelectEndpoint(Number);\r
-       Endpoint_EnableEndpoint();\r
-\r
-       UECFG1X = 0;    \r
-\r
-       UECFG0X = ((Type << EPTYPE0) | Direction);\r
-       UECFG1X = ((1 << ALLOC) | Banks | Endpoint_BytesToEPSizeMask(Size));\r
-\r
-       return Endpoint_IsConfigured();\r
+       return Endpoint_BytesToEPSizeMask(Size);\r
 }\r
-#else\r
-bool Endpoint_ConfigureEndpointStatic(const uint8_t Number, const uint8_t UECFG0XData, const uint8_t UECFG1XData)\r
+\r
+bool Endpoint_ConfigureEndpoint_Prv(const uint8_t Number, const uint8_t UECFG0XData, const uint8_t UECFG1XData)\r
 {\r
        Endpoint_SelectEndpoint(Number);\r
        Endpoint_EnableEndpoint();\r
@@ -66,7 +56,6 @@ bool Endpoint_ConfigureEndpointStatic(const uint8_t Number, const uint8_t UECFG0
 \r
        return Endpoint_IsConfigured();\r
 }\r
-#endif\r
 \r
 void Endpoint_ClearEndpoints(void)\r
 {\r
@@ -128,7 +117,7 @@ uint8_t Endpoint_Discard_Stream(uint16_t Length
        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
          return ErrorCode;\r
 \r
-       while (Length--)\r
+       while (Length)\r
        {\r
                if (!(Endpoint_IsReadWriteAllowed()))\r
                {\r
@@ -136,7 +125,7 @@ uint8_t Endpoint_Discard_Stream(uint16_t Length
 \r
                        #if !defined(NO_STREAM_CALLBACKS)\r
                        if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))\r
-                         return ENDPOINT_RWSTREAM_ERROR_CallbackAborted;\r
+                         return ENDPOINT_RWSTREAM_CallbackAborted;\r
                        #endif\r
 \r
                        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
@@ -145,10 +134,11 @@ uint8_t Endpoint_Discard_Stream(uint16_t Length
                else\r
                {\r
                        Endpoint_Discard_Byte();\r
+                       Length--;\r
                }\r
        }\r
        \r
-       return ENDPOINT_RWSTREAM_ERROR_NoError;\r
+       return ENDPOINT_RWSTREAM_NoError;\r
 }\r
 \r
 uint8_t Endpoint_Write_Stream_LE(const void* Buffer, uint16_t Length\r
@@ -163,7 +153,7 @@ uint8_t Endpoint_Write_Stream_LE(const void* Buffer, uint16_t Length
        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
          return ErrorCode;\r
 \r
-       while (Length--)\r
+       while (Length)\r
        {\r
                if (!(Endpoint_IsReadWriteAllowed()))\r
                {\r
@@ -171,7 +161,7 @@ uint8_t Endpoint_Write_Stream_LE(const void* Buffer, uint16_t Length
                        \r
                        #if !defined(NO_STREAM_CALLBACKS)\r
                        if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))\r
-                         return ENDPOINT_RWSTREAM_ERROR_CallbackAborted;\r
+                         return ENDPOINT_RWSTREAM_CallbackAborted;\r
                        #endif\r
 \r
                        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
@@ -180,10 +170,11 @@ uint8_t Endpoint_Write_Stream_LE(const void* Buffer, uint16_t Length
                else\r
                {\r
                        Endpoint_Write_Byte(*(DataStream++));\r
+                       Length--;\r
                }\r
        }\r
        \r
-       return ENDPOINT_RWSTREAM_ERROR_NoError;\r
+       return ENDPOINT_RWSTREAM_NoError;\r
 }\r
 \r
 uint8_t Endpoint_Write_Stream_BE(const void* Buffer, uint16_t Length\r
@@ -198,7 +189,7 @@ uint8_t Endpoint_Write_Stream_BE(const void* Buffer, uint16_t Length
        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
          return ErrorCode;\r
 \r
-       while (Length--)\r
+       while (Length)\r
        {\r
                if (!(Endpoint_IsReadWriteAllowed()))\r
                {\r
@@ -206,7 +197,7 @@ uint8_t Endpoint_Write_Stream_BE(const void* Buffer, uint16_t Length
 \r
                        #if !defined(NO_STREAM_CALLBACKS)\r
                        if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))\r
-                         return ENDPOINT_RWSTREAM_ERROR_CallbackAborted;\r
+                         return ENDPOINT_RWSTREAM_CallbackAborted;\r
                        #endif\r
 \r
                        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
@@ -215,10 +206,11 @@ uint8_t Endpoint_Write_Stream_BE(const void* Buffer, uint16_t Length
                else\r
                {\r
                        Endpoint_Write_Byte(*(DataStream--));\r
+                       Length--;\r
                }\r
        }\r
        \r
-       return ENDPOINT_RWSTREAM_ERROR_NoError;\r
+       return ENDPOINT_RWSTREAM_NoError;\r
 }\r
 \r
 uint8_t Endpoint_Read_Stream_LE(void* Buffer, uint16_t Length\r
@@ -233,7 +225,7 @@ uint8_t Endpoint_Read_Stream_LE(void* Buffer, uint16_t Length
        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
          return ErrorCode;\r
 \r
-       while (Length--)\r
+       while (Length)\r
        {\r
                if (!(Endpoint_IsReadWriteAllowed()))\r
                {\r
@@ -241,7 +233,7 @@ uint8_t Endpoint_Read_Stream_LE(void* Buffer, uint16_t Length
 \r
                        #if !defined(NO_STREAM_CALLBACKS)\r
                        if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))\r
-                         return ENDPOINT_RWSTREAM_ERROR_CallbackAborted;\r
+                         return ENDPOINT_RWSTREAM_CallbackAborted;\r
                        #endif\r
 \r
                        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
@@ -250,10 +242,11 @@ uint8_t Endpoint_Read_Stream_LE(void* Buffer, uint16_t Length
                else\r
                {\r
                        *(DataStream++) = Endpoint_Read_Byte();\r
+                       Length--;\r
                }\r
        }\r
        \r
-       return ENDPOINT_RWSTREAM_ERROR_NoError;\r
+       return ENDPOINT_RWSTREAM_NoError;\r
 }\r
 \r
 uint8_t Endpoint_Read_Stream_BE(void* Buffer, uint16_t Length\r
@@ -268,7 +261,7 @@ uint8_t Endpoint_Read_Stream_BE(void* Buffer, uint16_t Length
        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
          return ErrorCode;\r
 \r
-       while (Length--)\r
+       while (Length)\r
        {\r
                if (!(Endpoint_IsReadWriteAllowed()))\r
                {\r
@@ -276,7 +269,7 @@ uint8_t Endpoint_Read_Stream_BE(void* Buffer, uint16_t Length
 \r
                        #if !defined(NO_STREAM_CALLBACKS)\r
                        if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))\r
-                         return ENDPOINT_RWSTREAM_ERROR_CallbackAborted;\r
+                         return ENDPOINT_RWSTREAM_CallbackAborted;\r
                        #endif\r
 \r
                        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
@@ -285,18 +278,21 @@ uint8_t Endpoint_Read_Stream_BE(void* Buffer, uint16_t Length
                else\r
                {\r
                        *(DataStream--) = Endpoint_Read_Byte();\r
+                       Length--;\r
                }\r
        }\r
        \r
-       return ENDPOINT_RWSTREAM_ERROR_NoError;\r
+       return ENDPOINT_RWSTREAM_NoError;\r
 }\r
 #endif\r
 \r
 uint8_t Endpoint_Write_Control_Stream_LE(const void* Buffer, uint16_t Length)\r
 {\r
-       uint8_t* DataStream    = (uint8_t*)Buffer;\r
+       uint8_t* DataStream     = (uint8_t*)Buffer;\r
        bool     LastPacketFull = false;\r
-       bool     ShortTransfer = (Length < USB_ControlRequest.wLength);\r
+       \r
+       if (Length > USB_ControlRequest.wLength)\r
+         Length = USB_ControlRequest.wLength;\r
        \r
        while (Length && !(Endpoint_IsOUTReceived()))\r
        {\r
@@ -305,7 +301,6 @@ uint8_t Endpoint_Write_Control_Stream_LE(const void* Buffer, uint16_t Length)
                while (Length && (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize))\r
                {\r
                        Endpoint_Write_Byte(*(DataStream++));\r
-                       \r
                        Length--;\r
                }\r
                \r
@@ -314,9 +309,9 @@ uint8_t Endpoint_Write_Control_Stream_LE(const void* Buffer, uint16_t Length)
        }\r
        \r
        if (Endpoint_IsOUTReceived())\r
-         return ENDPOINT_RWCSTREAM_ERROR_HostAborted;\r
+         return ENDPOINT_RWCSTREAM_HostAborted;\r
        \r
-       if (LastPacketFull || ShortTransfer)\r
+       if (LastPacketFull)\r
        {\r
                while (!(Endpoint_IsINReady()));\r
                Endpoint_ClearIN();\r
@@ -324,34 +319,36 @@ uint8_t Endpoint_Write_Control_Stream_LE(const void* Buffer, uint16_t Length)
        \r
        while (!(Endpoint_IsOUTReceived()));\r
 \r
-       return ENDPOINT_RWCSTREAM_ERROR_NoError;\r
+       return ENDPOINT_RWCSTREAM_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     LastPacketFull = false;\r
-       bool     ShortTransfer  = (Length < USB_ControlRequest.wLength);\r
        \r
+       if (Length > USB_ControlRequest.wLength)\r
+         Length = USB_ControlRequest.wLength;\r
+\r
        while (Length && !(Endpoint_IsOUTReceived()))\r
        {\r
-               while (!(Endpoint_IsINReady()));\r
-               \r
-               while (Length && (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize))\r
+               if (Endpoint_IsINReady())\r
                {\r
-                       Endpoint_Write_Byte(*(DataStream--));\r
+                       while (Length && (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize))\r
+                       {\r
+                               Endpoint_Write_Byte(*(DataStream--));\r
+                               Length--;\r
+                       }\r
                        \r
-                       Length--;\r
+                       LastPacketFull = (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize);\r
+                       Endpoint_ClearIN();\r
                }\r
-               \r
-               LastPacketFull = (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize);\r
-               Endpoint_ClearIN();\r
        }\r
        \r
        if (Endpoint_IsOUTReceived())\r
-         return ENDPOINT_RWCSTREAM_ERROR_HostAborted;\r
+         return ENDPOINT_RWCSTREAM_HostAborted;\r
        \r
-       if (LastPacketFull || ShortTransfer)\r
+       if (LastPacketFull)\r
        {\r
                while (!(Endpoint_IsINReady()));\r
                Endpoint_ClearIN();\r
@@ -359,7 +356,7 @@ uint8_t Endpoint_Write_Control_Stream_BE(const void* Buffer, uint16_t Length)
        \r
        while (!(Endpoint_IsOUTReceived()));\r
 \r
-       return ENDPOINT_RWCSTREAM_ERROR_NoError;\r
+       return ENDPOINT_RWCSTREAM_NoError;\r
 }\r
 \r
 uint8_t Endpoint_Read_Control_Stream_LE(void* Buffer, uint16_t Length)\r
@@ -368,21 +365,21 @@ uint8_t Endpoint_Read_Control_Stream_LE(void* Buffer, uint16_t Length)
        \r
        while (Length)\r
        {\r
-               while (!(Endpoint_IsOUTReceived()));\r
-               \r
-               while (Length && Endpoint_BytesInEndpoint())\r
+               if (Endpoint_IsOUTReceived())\r
                {\r
-                       *(DataStream++) = Endpoint_Read_Byte();\r
+                       while (Length && Endpoint_BytesInEndpoint())\r
+                       {\r
+                               *(DataStream++) = Endpoint_Read_Byte();\r
+                               Length--;\r
+                       }\r
                        \r
-                       Length--;\r
+                       Endpoint_ClearOUT();\r
                }\r
-               \r
-               Endpoint_ClearOUT();\r
        }\r
        \r
        while (!(Endpoint_IsINReady()));\r
        \r
-       return ENDPOINT_RWCSTREAM_ERROR_NoError;\r
+       return ENDPOINT_RWCSTREAM_NoError;\r
 }\r
 \r
 uint8_t Endpoint_Read_Control_Stream_BE(void* Buffer, uint16_t Length)\r
@@ -391,21 +388,21 @@ uint8_t Endpoint_Read_Control_Stream_BE(void* Buffer, uint16_t Length)
        \r
        while (Length)\r
        {\r
-               while (!(Endpoint_IsOUTReceived()));\r
-               \r
-               while (Length && Endpoint_BytesInEndpoint())\r
+               if (Endpoint_IsOUTReceived())\r
                {\r
-                       *(DataStream--) = Endpoint_Read_Byte();\r
+                       while (Length && Endpoint_BytesInEndpoint())\r
+                       {\r
+                               *(DataStream--) = Endpoint_Read_Byte();\r
+                               Length--;\r
+                       }\r
                        \r
-                       Length--;\r
+                       Endpoint_ClearOUT();\r
                }\r
-               \r
-               Endpoint_ClearOUT();\r
        }\r
        \r
        while (!(Endpoint_IsINReady()));\r
 \r
-       return ENDPOINT_RWCSTREAM_ERROR_NoError;\r
+       return ENDPOINT_RWCSTREAM_NoError;\r
 }\r
 \r
 #endif\r