Oops - with new changes to the way the device Configuration Descriptor is retrieved...
[pub/USBasp.git] / LUFA / Drivers / USB / Class / Device / CDC.c
index 9da0ed1..31fd8f1 100644 (file)
@@ -115,68 +115,79 @@ bool CDC_Device_ConfigureEndpoints(USB_ClassInfo_CDC_Device_t* CDCInterfaceInfo)
 \r
 void CDC_Device_USBTask(USB_ClassInfo_CDC_Device_t* CDCInterfaceInfo)\r
 {\r
-       if (USB_DeviceState != DEVICE_STATE_Configured)\r
-         return;\r
-\r
-       Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataINEndpointNumber);\r
-\r
-       if (!(Endpoint_BytesInEndpoint()))\r
+       if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS))\r
          return;\r
-         \r
-       if (!(Endpoint_IsReadWriteAllowed()))\r
-       {\r
-               Endpoint_ClearIN();\r
 \r
-               while (!(Endpoint_IsReadWriteAllowed()))\r
-               {\r
-                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
-                         return;\r
-               }\r
-       }       \r
-       \r
-       Endpoint_ClearIN();\r
+       CDC_Device_Flush(CDCInterfaceInfo);\r
 }\r
 \r
-void CDC_Device_SendString(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo, char* const Data, const uint16_t Length)\r
+uint8_t CDC_Device_SendString(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo, char* const Data, const uint16_t Length)\r
 {\r
        if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS))\r
-         return;\r
+         return ENDPOINT_READYWAIT_NoError;\r
        \r
        Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataINEndpointNumber);\r
-       Endpoint_Write_Stream_LE(Data, Length, NO_STREAM_CALLBACK);\r
+       return Endpoint_Write_Stream_LE(Data, Length, NO_STREAM_CALLBACK);\r
 }\r
 \r
-void CDC_Device_SendByte(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo, const uint8_t Data)\r
+uint8_t CDC_Device_SendByte(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo, const uint8_t Data)\r
 {\r
        if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS))\r
-         return;\r
+         return ENDPOINT_READYWAIT_NoError;\r
 \r
        Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataINEndpointNumber);\r
 \r
        if (!(Endpoint_IsReadWriteAllowed()))\r
        {\r
+               uint8_t ErrorCode;\r
+       \r
                Endpoint_ClearIN();\r
-               \r
-               while (!(Endpoint_IsReadWriteAllowed()))\r
-               {\r
-                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
-                         return;               \r
-               }\r
+\r
+               if ((ErrorCode = Endpoint_WaitUntilReady()) != ENDPOINT_READYWAIT_NoError)\r
+                 return ErrorCode;\r
        }\r
 \r
-       Endpoint_Write_Byte(Data);      \r
+       Endpoint_Write_Byte(Data);\r
+       return ENDPOINT_READYWAIT_NoError;\r
+}\r
+\r
+uint8_t CDC_Device_Flush(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)\r
+{\r
+       if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS))\r
+         return ENDPOINT_READYWAIT_NoError;\r
+\r
+       Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataINEndpointNumber);\r
+       \r
+       if (Endpoint_BytesInEndpoint())\r
+       {\r
+               uint8_t ErrorCode;\r
+\r
+               Endpoint_ClearIN();\r
+               \r
+               if ((ErrorCode = Endpoint_WaitUntilReady()) != ENDPOINT_READYWAIT_NoError)\r
+                 return ErrorCode;\r
+       }\r
+       \r
+       Endpoint_ClearIN();\r
+       return Endpoint_WaitUntilReady();\r
 }\r
 \r
 uint16_t CDC_Device_BytesReceived(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)\r
 {\r
+       if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS))\r
+         return 0;\r
+\r
        Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataOUTEndpointNumber);\r
 \r
+       if (Endpoint_IsOUTReceived() && !(Endpoint_BytesInEndpoint()))\r
+         Endpoint_ClearOUT();\r
+\r
        return Endpoint_BytesInEndpoint();\r
 }\r
 \r
 uint8_t CDC_Device_ReceiveByte(USB_ClassInfo_CDC_Device_t* CDCInterfaceInfo)\r
 {\r
-       if (USB_DeviceState != DEVICE_STATE_Configured)\r
+       if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS))\r
          return 0;\r
 \r
        Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.DataOUTEndpointNumber);\r
@@ -191,7 +202,7 @@ uint8_t CDC_Device_ReceiveByte(USB_ClassInfo_CDC_Device_t* CDCInterfaceInfo)
 \r
 void CDC_Device_SendControlLineStateChange(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)\r
 {\r
-       if (USB_DeviceState != DEVICE_STATE_Configured)\r
+       if ((USB_DeviceState != DEVICE_STATE_Configured) || !(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS))\r
          return;\r
 \r
        Endpoint_SelectEndpoint(CDCInterfaceInfo->Config.NotificationEndpointNumber);\r
@@ -202,11 +213,13 @@ void CDC_Device_SendControlLineStateChange(USB_ClassInfo_CDC_Device_t* const CDC
                        .bRequest      = NOTIF_SerialState,\r
                        .wValue        = 0,\r
                        .wIndex        = 0,\r
-                       .wLength       = sizeof(uint16_t),\r
+                       .wLength       = sizeof(CDCInterfaceInfo->State.ControlLineStates.DeviceToHost),\r
                };\r
 \r
        Endpoint_Write_Stream_LE(&Notification, sizeof(Notification), NO_STREAM_CALLBACK);\r
-       Endpoint_Write_Stream_LE(&CDCInterfaceInfo->State.ControlLineStates.DeviceToHost, sizeof(uint8_t), NO_STREAM_CALLBACK);\r
+       Endpoint_Write_Stream_LE(&CDCInterfaceInfo->State.ControlLineStates.DeviceToHost,\r
+                                sizeof(CDCInterfaceInfo->State.ControlLineStates.DeviceToHost),\r
+                                NO_STREAM_CALLBACK);\r
        Endpoint_ClearIN();\r
 }\r
 \r