⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sdmemdiskio.cpp

📁 6410BSP3
💻 CPP
📖 第 1 页 / 共 3 页
字号:

    /*
    dwStatus = SDMemDoErase(
        pMemCard,
        (LONG) pDSI->startsector,
        (LONG) pDSI->numsectors
        );
    */

statusReturn:

    DEBUGMSG(SDCARD_ZONE_FUNC, (TEXT("SDMemory: -SDMemErase\n")));

    return dwStatus;
}

///////////////////////////////////////////////////////////////////////////////
//  RequestPrologue  - pre-tasks before handling the ioctl
//  Input:  pMemCard - memory card instance
//          DeviceIoControl - device iocontrol to filter
//  Output:
//  Return: SD_API_STATUS code
//  Notes:
//          This function should be called from the Ioctl dispatch function
///////////////////////////////////////////////////////////////////////////////
SD_API_STATUS RequestPrologue(PSD_MEMCARD_INFO pMemCard, DWORD DeviceIoControl)
{
    SD_API_STATUS status = SD_API_STATUS_SUCCESS; // intermediate status

    if (pMemCard->CardEjected) {
        return SD_API_STATUS_DEVICE_REMOVED;
    }

        // check and see if we need to do power management tasks
    if (!pMemCard->EnablePowerManagement) {
        return SD_API_STATUS_SUCCESS;
    }

        // pass power Ioctls through without issuing card re-select
    if ((IOCTL_POWER_CAPABILITIES == DeviceIoControl) ||
        (IOCTL_POWER_QUERY == DeviceIoControl) ||
        (IOCTL_POWER_SET == DeviceIoControl)) {
        return SD_API_STATUS_SUCCESS;
    }

        // for all other ioctls, re-select the card
    AcquireLock(pMemCard);

        // cancel the idle timer
    pMemCard->CancelIdleTimeout= TRUE;

        // check to see if the card was deselected due to power
        // management
    if (pMemCard->CardDeSelected) {
        DEBUGMSG(SDMEM_ZONE_POWER, (TEXT("SDMemory: Re-Selecting Card \n")));
        status = IssueCardSelectDeSelect(pMemCard, TRUE);
        if (SD_API_SUCCESS(status)) {
            pMemCard->CardDeSelected = FALSE;
        }
    }

    ReleaseLock(pMemCard);

    return status;
}

///////////////////////////////////////////////////////////////////////////////
//  RequestEnd  - post-tasks before handling the ioctl
//  Input:  pMemCard - memory card instance
//  Output:
//  Return:
//  Notes:
//          this function should be called from the Ioctl dispatch function
///////////////////////////////////////////////////////////////////////////////
void RequestEnd(PSD_MEMCARD_INFO pMemCard)
{
    AcquireLock(pMemCard);
        // clear the idle timer cancel flag after ioctl request is end
    pMemCard->CancelIdleTimeout= FALSE;

        ReleaseLock(pMemCard);
}

///////////////////////////////////////////////////////////////////////////////
//  IssueCardSelectDeSelect - issue card select
//  Input:  pMemCard - memory card instance
//          Select - select the card
//  Output:
//  Return: SD_API_STATUS code
//  Notes:
///////////////////////////////////////////////////////////////////////////////
SD_API_STATUS IssueCardSelectDeSelect(PSD_MEMCARD_INFO pMemCard, BOOL Select)
{
    USHORT              relativeAddress;    // relative address
    SD_RESPONSE_TYPE    responseType;       // expected response
    SD_API_STATUS       status;             // intermediate status
    int                 retryCount;         // retryCount;
    SD_CARD_STATUS      cardStatus;         // card status

    if (Select) {
            // using the cards original address selects the card again
        relativeAddress = pMemCard->RCA;
        DEBUG_CHECK((relativeAddress != 0), (TEXT("IssueCardSelectDeSelect - Relative address is zero! \n")));
            // the selected card should return a response
        responseType = ResponseR1b;
    } else {
            // address of zero deselects the card
        relativeAddress = 0;
            // according to the spec no response will be returned
        responseType = NoResponse;
    }

    retryCount = DEFAULT_DESELECT_RETRY_COUNT;

    while (retryCount) {

        status = SDSynchronousBusRequest(pMemCard->hDevice,
                                         SD_CMD_SELECT_DESELECT_CARD,
                                         ((DWORD)relativeAddress) << 16,
                                         SD_COMMAND,
                                         responseType,
                                         NULL,
                                         0,
                                         0,
                                         NULL,
                                         0);

        if (!SD_API_SUCCESS(status)) {
            break;
        }

        if (!Select) {
                // if we deselected, get the card status and check for
                // standby state
            status = SDCardInfoQuery(pMemCard->hDevice,
                                     SD_INFO_CARD_STATUS,
                                     &cardStatus,
                                     sizeof(SD_CARD_STATUS));

            if (!SD_API_SUCCESS(status)){
                break;
            }

            if (SD_STATUS_CURRENT_STATE(cardStatus) ==
                     SD_STATUS_CURRENT_STATE_STDBY) {
                DEBUGMSG(SDMEM_ZONE_POWER, (TEXT("SDMemory: Card now in Standby \n")));
                break;
            } else {

                DEBUGMSG(SDCARD_ZONE_ERROR, (TEXT("SDMemory: Card not in standby! Card Status: 0x%08X \n")
                          , cardStatus));
                    // set unusuccessful for retry
                status = SD_API_STATUS_UNSUCCESSFUL;
            }


            retryCount--;

        } else {
            DEBUGMSG(SDMEM_ZONE_POWER, (TEXT("SDMemory: Card now in Transfer state \n")));
            break;
        }

    }

    return status;
}

///////////////////////////////////////////////////////////////////////////////
//  IdleThread - idle thread
//  Input:  pMemCard - memory card instance
//  Output:
//  Return: thread exit code
//  Notes:
//      This thread is created if power management is enabled.
//      Normally the thread lays dormant until signalled to perform
//      the idle timeout.  When an idle timeout occurs, if the
//      timer was not cancelled, the thread deselects the memory card
//      to reduce power consumption.
///////////////////////////////////////////////////////////////////////////////
DWORD IdleThread(PSD_MEMCARD_INFO pMemCard)
{
    while(1) {

            // wait for event
        WaitForSingleObject(pMemCard->hWakeUpIdleThread, INFINITE);

        if (pMemCard->ShutDownIdleThread) {
            return 0;
        }

            // while low power polling is enabled
        while (pMemCard->EnableLowPower) {

                // wait with a timeout
            WaitForSingleObject(pMemCard->hWakeUpIdleThread, pMemCard->IdleTimeout);

            if (pMemCard->EnableLowPower) {

                DEBUGMSG(SDMEM_ZONE_POWER, (TEXT("SDMemory: Idle Timeout Wakeup after %d MS \n"),pMemCard->IdleTimeout));

                AcquireLock(pMemCard);
                    // check for cancel
                if (!pMemCard->CancelIdleTimeout){
                        // make sure we haven't already deselected the card
                    if (!pMemCard->CardDeSelected) {
                        DEBUGMSG(SDMEM_ZONE_POWER, (TEXT("SDMemory: Idle Timout, De-Selecting Card \n")));
                            // we haven't been canceled, so issue the card de-select
                        if (SD_API_SUCCESS(IssueCardSelectDeSelect(pMemCard, FALSE))) {
                            pMemCard->CardDeSelected = TRUE;
                        }
                    }
                } else {
                    DEBUGMSG(SDMEM_ZONE_POWER, (TEXT("SDMemory: Idle Timout, Cancelled! \n")));
                }

                    // clear the cancel flag
                                    // SMC_IOControl() will clear pMemCard->CancelIdleTimeout once ioctl request is end

                ReleaseLock(pMemCard);
            }
        }

        if (pMemCard->ShutDownIdleThread) {
            return 0;
        }
    }

}

///////////////////////////////////////////////////////////////////////////////
//  HandleIoctlPowerSet
//  Input:  pMemCard - SD memory card structure
//          pDevicePowerState - device power state
//  Output:
//  Return:
//  Notes:
///////////////////////////////////////////////////////////////////////////////
VOID HandleIoctlPowerSet(PSD_MEMCARD_INFO       pMemCard,
                         PCEDEVICE_POWER_STATE  pDevicePowerState)
{
    AcquireLock(pMemCard);

    DEBUGMSG(SDMEM_ZONE_POWER, (TEXT("SDMemory: IOCTL_POWER_SET %d \n"),*pDevicePowerState));

    if (*pDevicePowerState < pMemCard->PowerStateForIdle)  {
            // everything above the power state for idle is treated as D0
        *pDevicePowerState = D0;
        pMemCard->CurrentPowerState = D0;
            // disable low power operation
        pMemCard->EnableLowPower = FALSE;

    } else {
            // everything above the IDLE power state is set to IDLE
        *pDevicePowerState = pMemCard->PowerStateForIdle;
        pMemCard->CurrentPowerState = pMemCard->PowerStateForIdle;
            // enable low power operation
        pMemCard->EnableLowPower = TRUE;
            // wake up the idle thread to go into power idle polling
        SetEvent(pMemCard->hWakeUpIdleThread);
    }

    ReleaseLock(pMemCard);

}

///////////////////////////////////////////////////////////////////////////////
//  InitializePowerManagement - initialize power management feature
//  Input:  pMemCard - SD memory card structure
//  Output:
//  Return:
//  Notes:
///////////////////////////////////////////////////////////////////////////////
VOID InitializePowerManagement(PSD_MEMCARD_INFO pMemCard)
{
    DWORD threadID;     // idle thread ID

    if (!pMemCard->EnablePowerManagement) {
        DEBUGMSG(SDCARD_ZONE_INIT, (TEXT("SDMemory: Power Management Disabled\n")));
        return;
    }

    pMemCard->EnableLowPower  = FALSE;
    pMemCard->CurrentPowerState = D0;
    pMemCard->ShutDownIdleThread = FALSE;
    pMemCard->CancelIdleTimeout = FALSE;
    pMemCard->CardDeSelected = FALSE;

        // create the wake up event
    pMemCard->hWakeUpIdleThread = CreateEvent(NULL, FALSE, FALSE, NULL);

    if (NULL == pMemCard->hWakeUpIdleThread) {
        pMemCard->EnablePowerManagement = FALSE;
        return;
    }

        // create the thread
    pMemCard->hIdleThread = CreateThread(NULL,
                                         0,
                                         (LPTHREAD_START_ROUTINE)IdleThread,
                                         pMemCard,
                                         0,
                                         &threadID);

    if (NULL == pMemCard->hIdleThread) {
        pMemCard->EnablePowerManagement = FALSE;
        return;
    }

    DEBUGMSG(SDCARD_ZONE_INIT, (TEXT("SDMemory: Power Management Setup complete \n")));

}


///////////////////////////////////////////////////////////////////////////////
//  DeinitializePowerManagent - clean up power management feature
//  Input:  pMemCard - SD memory card structure
//  Output:
//  Return:
//  Notes:
///////////////////////////////////////////////////////////////////////////////
VOID DeinitializePowerManagement(PSD_MEMCARD_INFO pMemCard)
{
    pMemCard->ShutDownIdleThread = TRUE;
    pMemCard->EnableLowPower = FALSE;
    pMemCard->CancelIdleTimeout = TRUE;

    if (NULL != pMemCard->hIdleThread) {
        SetEvent(pMemCard->hWakeUpIdleThread);
        WaitForSingleObject(pMemCard->hIdleThread, INFINITE);
        CloseHandle(pMemCard->hIdleThread);
        pMemCard->hIdleThread = NULL;
    }

    if (NULL != pMemCard->hWakeUpIdleThread) {
        CloseHandle(pMemCard->hWakeUpIdleThread);
        pMemCard->hWakeUpIdleThread = NULL;
    }

}

// DO NOT REMOVE --- END EXTERNALLY DEVELOPED SOURCE CODE ID --- DO NOT REMOVE

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -