//================================================================================================
//
//   RestoreControllerStateFromSleep
//
//================================================================================================
//
IOReturn				
AppleUSBUHCI::RestoreControllerStateFromSleep(void)
{
	int		i;
	UInt16	value;
	bool	wakeMsg = false;

	USBLog(5, "AppleUSBUHCI[%p]::RestoreControllerStateFromSleep RUN - resuming controller", this);
	for (i=0; i< 2; i++)
	{
		value = ReadPortStatus(i);
		if (value & kUHCI_PORTSC_CSC)
		{
			USBLog(5, "AppleUSBUHCI[%p]::RestoreControllerStateFromSleep  Port %d on bus 0x%x connected or disconnected", this, (int)i+1, (uint32_t)_busNumber);
			// IOLog("USB (UHCI):Port %d on bus 0x%x connected or disconnected\n", (int)i+1, (uint32_t)_busNumber);
		}
		else if (value & kUHCI_PORTSC_RD)
		{
			USBLog(5, "AppleUSBUHCI[%p]::RestoreControllerStateFromSleep  Port %d on bus 0x%x has remote wakeup from some device", this, (int)i+1, (uint32_t)_busNumber);

            // because of how UHCI works, the root hub driver might not be able to detect that there was a remote wakeup 
			// on a port if the upper level driver issues a Resume before the root hub interrupt timer runs
			// Let the hub driver know that from here to make sure we get the log

            if (_rootHubDevice && _rootHubDevice->GetPolicyMaker())
            {
                _rootHubDevice->GetPolicyMaker()->message(kIOUSBMessageRootHubWakeEvent, this, (void *)(uintptr_t) i);
            }
			else
			{
				IOLog("USB (UHCI):Port %d on bus 0x%x has remote wakeup from some device\n", (int)i+1, (uint32_t)_busNumber);
			}
        }
	}		
	ResumeController();

	return kIOReturnSuccess;
}
ECode MyActivityController::Run()
{
    PrintMessageForState();

    mHost->GetAm()->SetActivityController(this);
    mState = STATE_NORMAL;

    AutoPtr<IInputStreamReader> converter;
    AutoPtr<IBufferedReader> in;

    AutoPtr<ISystem> system;
    Elastos::Core::CSystem::AcquireSingleton((ISystem**)&system);
    AutoPtr<IInputStream> systemIn;
    system->GetIn((IInputStream**)&systemIn);
    CInputStreamReader::New(systemIn, (IInputStreamReader**)&converter);
    CBufferedReader::New(converter, (IBufferedReader**)&in);
    String line;
    while (!(in->ReadLine(&line), line).IsNull()) {
        Boolean addNewline = TRUE;
        if (line.GetLength() <= 0) {
            addNewline = FALSE;
        }
        else if (line.Equals("q") || line.Equals("quit")) {
            ResumeController(RESULT_DEFAULT);
            break;
        }
        else if (mState == STATE_CRASHED) {
            if (line.Equals("c") || line.Equals("continue")) {
                ResumeController(RESULT_CRASH_DIALOG);
            }
            else if (line.Equals("k") || line.Equals("kill")) {
                ResumeController(RESULT_CRASH_KILL);
            }
            else {
                PFL_EX("Invalid command: %s", line.string());
            }
        }
        else if (mState == STATE_ANR) {
            if (line.Equals("c") || line.Equals("continue")) {
                ResumeController(RESULT_ANR_DIALOG);
            }
            else if (line.Equals("k") || line.Equals("kill")) {
                ResumeController(RESULT_ANR_KILL);
            }
            else if (line.Equals("w") || line.Equals("wait")) {
                ResumeController(RESULT_ANR_WAIT);
            }
            else {
                PFL_EX("Invalid command: %s", line.string());
            }
        }
        else if (mState == STATE_EARLY_ANR) {
            if (line.Equals("c") || line.Equals("continue")) {
                ResumeController(RESULT_EARLY_ANR_CONTINUE);
            }
            else if (line.Equals("k") || line.Equals("kill")) {
                ResumeController(RESULT_EARLY_ANR_KILL);
            }
            else {
                PFL_EX("Invalid command: %s", line.string());
            }
        }
        else {
            PFL_EX("Invalid command: %s", line.string());
        }

        AutoLock lock(mLock);
        if (addNewline) {
            PFL_EX("");
        }
        PrintMessageForState();
    }

    return mHost->GetAm()->SetActivityController(NULL);
}