Added new USB_DeviceState variable to keep track of the current Device mode USB state.
[pub/lufa.git] / LUFA / Drivers / USB / LowLevel / DevChapter9.c
index 10dce72..8de4bd6 100644 (file)
@@ -117,13 +117,22 @@ void USB_Device_ProcessControlPacket(void)
 \r
 static void USB_Device_SetAddress(void)\r
 {\r
+       uint8_t DeviceAddress = (USB_ControlRequest.wValue & 0x7F);\r
+\r
        Endpoint_ClearSETUP();\r
        \r
        Endpoint_ClearIN();\r
        \r
-       while (!(Endpoint_IsINReady()));\r
+       while (!(Endpoint_IsINReady()))\r
+       {\r
+               if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                 return;\r
+       }\r
+\r
+       UDADDR = ((1 << ADDEN) | DeviceAddress);\r
 \r
-       UDADDR = ((1 << ADDEN) | ((uint8_t)USB_ControlRequest.wValue & 0x7F));\r
+       if (DeviceAddress)\r
+         USB_DeviceState = DEVICE_STATE_Addressed;\r
 \r
        return;\r
 }\r
@@ -185,8 +194,17 @@ static void USB_Device_SetConfiguration(void)
 \r
        Endpoint_ClearIN();\r
 \r
-       if (!(AlreadyConfigured) && USB_ConfigurationNumber)\r
-         EVENT_USB_DeviceEnumerationComplete();\r
+       if (USB_ConfigurationNumber)\r
+       {\r
+               USB_DeviceState = DEVICE_STATE_Configured;\r
+\r
+               if (!(AlreadyConfigured))\r
+                 EVENT_USB_DeviceEnumerationComplete();\r
+       }\r
+       else\r
+       {\r
+               USB_DeviceState = DEVICE_STATE_Addressed;\r
+       }\r
 \r
        EVENT_USB_ConfigurationChanged();\r
 }\r
@@ -199,7 +217,12 @@ void USB_Device_GetConfiguration(void)
        \r
        Endpoint_ClearIN();\r
 \r
-       while (!(Endpoint_IsOUTReceived()));\r
+       while (!(Endpoint_IsOUTReceived()))\r
+       {\r
+               if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                 return;       \r
+       }\r
+\r
        Endpoint_ClearOUT();\r
 }\r
 \r
@@ -332,7 +355,12 @@ static void USB_Device_GetStatus(void)
 \r
        Endpoint_ClearIN();\r
        \r
-       while (!(Endpoint_IsOUTReceived()));\r
+       while (!(Endpoint_IsOUTReceived()))\r
+       {\r
+               if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                 return;       \r
+       }\r
+       \r
        Endpoint_ClearOUT();\r
 }\r
 \r