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

📄 sdhceventhandlers.cpp

📁 S3C2443 WINCE6.0 BSP
💻 CPP
📖 第 1 页 / 共 5 页
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//

// Copyright (c) 2002 BSQUARE Corporation.  All rights reserved.
// DO NOT REMOVE --- BEGIN EXTERNALLY DEVELOPED SOURCE CODE ID 40973--- DO NOT REMOVE

// SD Host Controller Event Handlers

#include "SDbusdriver.h"

#define GET_HC_NAME_FROM_SLOT(s) (s)->pHostController->HostControllerName	// added by JJG 06.11.17
// macro to get the function number from an R4 response 
#define SD_GET_NUMBER_OF_FUNCTIONS(pResponse) (((pResponse)->ResponseBuffer[4] >> 4) & 0x7)
// macro to get the memory present bit from an R4 response
#define SD_MEMORY_PRESENT_WITH_IO(pResponse) (((pResponse)->ResponseBuffer[4] >> 3) &  0x1)
// macro to get the I/O ready flag from an R4 response
#define SD_IS_IO_READY(pResponse)             ((pResponse)->ResponseBuffer[4] & 0x80)
// macro to get the Memory card ready flag from an R3 response
#define SD_IS_MEM_READY(pResponse)            ((pResponse)->ResponseBuffer[4] & 0x80)

// macro to call slot option handler for power setting
#define SDSetSlotPower(pSlot,Setting) \
    (pSlot)->pHostController->pSlotOptionHandler((pSlot)->pHostController,  \
    (pSlot)->SlotIndex,        \
    SDHCDSetSlotPower,         \
    &(Setting),                \
    sizeof(Setting))

// macro for synchronous bus commands
#define SendSDCommand(hDevice, Command,Argument,ResponseType,pResponse) \
    SDSynchronousBusRequest__X(hDevice,         \
    Command,         \
    Argument,        \
    SD_COMMAND,      \
    ResponseType,    \
    pResponse,       \
    0,               \
    0,               \
    NULL,            \
    (DWORD)0)            


// macro to send an application specific command with no data transfer 
#define SendSDAppCommand(hDevice,AppCommand,Argument,ResponseType,pResponse) \
    SendSDAppCmd(hDevice,         \
    AppCommand,      \
    Argument,        \
    SD_COMMAND,      \
    ResponseType,    \
    pResponse,       \
    0,               \
    0,               \
    0)

#define GetDeviceVoltageRangeFromOCR(pVoltageRange, pDevice)            \
{                                                                       \
    *pVoltageRange = (DWORD)pDevice->CachedRegisters.OCR[0] & ~0xF;      \
    *pVoltageRange |= ((DWORD)pDevice->CachedRegisters.OCR[1]) << 8;     \
    *pVoltageRange |= ((DWORD)pDevice->CachedRegisters.OCR[2]) << 16;    \
}

#define GetDeviceVoltageRangeFromIO_OCR(pVoltageRange, pDevice)         \
{                                                                       \
    *pVoltageRange = (DWORD)pDevice->CachedRegisters.IO_OCR[0] & ~0xF;   \
    *pVoltageRange |= ((DWORD)pDevice->CachedRegisters.IO_OCR[1]) << 8;  \
    *pVoltageRange |= ((DWORD)pDevice->CachedRegisters.IO_OCR[2]) << 16; \
}

#define GetOEMIDFromCID(pOID, pDevice)                          \
{                                                               \
    *pOID = (USHORT)((pDevice)->CachedRegisters.CID[SD_CID_OID_OFFSET]); \
    *pOID |= ((USHORT)((pDevice)->CachedRegisters.CID[SD_CID_OID_OFFSET + 1])) << 8; \
}

#define CIS_CSA_BYTES (SD_IO_CIS_PTR_BYTES + SD_IO_CSA_PTR_BYTES)
#define CIS_OFFSET_BYTE_0 0
#define CIS_OFFSET_BYTE_1 1
#define CIS_OFFSET_BYTE_2 2
#define CSA_OFFSET_BYTE_0 3
#define CSA_OFFSET_BYTE_1 4
#define CSA_OFFSET_BYTE_2 5
#define UNKNOWN_PRODUCT_INFO_STRING_LENGTH 64

#define USE_MMCPLUS 1

#define IS_PARENT(p) (p == p->pParentDevice)

#if USE_MMCPLUS
UINT32 g_bIsHSMMC=FALSE;
UINT32 g_bIsMMC = FALSE;
#endif

///////////////////////////////////////////////////////////////////////////////
//  FormatProductString - format a raw CISPTPL_VERS_1 data buffer into a 
//                        displayable string
//  Input:  pAsciiString  - raw product string buffer retreived from VERS_1 tuple
//                    the tuple is stored as ASCII characters
//          pString - the wide char string to store the version 1 tuple
//          
//  Output:
//  Return:
//  Notes:  
//         This function converts the raw version tuple data buffer
//         into a wide char displayable string 
//         All non-displayable characters and spaces are converted to 
//         an underscore.  
///////////////////////////////////////////////////////////////////////////////
VOID FormatProductString(PCHAR pAsciiString, PWCHAR pString ) 
{
    ULONG  ii;  // loop variable

    for (ii = 0; ii < strlen(pAsciiString); ii++) {
        // convert to Wide char
        pString[ii] = (WCHAR)pAsciiString[ii];

        // if the character is outside the range of displayable characters
        // then substitute with an under score
        if ((pAsciiString[ii] <= 0x20) || (pAsciiString[ii] > 0x7E)) {
            pString[ii] = TEXT('_');
        }
    }

    // terminate
    pString[ii] = NULL;
}

///////////////////////////////////////////////////////////////////////////////
//  SDSetCardInterfaceForSlot - set Card interface for the slot
//  Input:  pSlot - the slot to set the card interface for
//          pSetting - card interface settings
//  Output:
//  Return: SD_API_STATUS
//  Notes:  
//         this function calls the slot option handler in the host controller
//         driver
//         
///////////////////////////////////////////////////////////////////////////////
SD_API_STATUS SDSetCardInterfaceForSlot(PSDBUS_HC_SLOT_CONTEXT   pSlot, 
                                        PSD_CARD_INTERFACE       pSetting)
{
    SD_API_STATUS   status;             // intermediate status
    DWORD           origClockSetting;   // original clock setting upon entry

    origClockSetting = pSetting->ClockRate;

    status = pSlot->pHostController->pSlotOptionHandler(pSlot->pHostController,
        pSlot->SlotIndex, 
        SDHCDSetSlotInterface,
        pSetting,                
        sizeof(SD_CARD_INTERFACE));

    if (SD_API_SUCCESS(status)) {
        if (origClockSetting != pSetting->ClockRate){
            DEBUGMSG(SDCARD_ZONE_WARN, (TEXT("SDSetCardInterfaceForSlot - HC ClockRate differs desired setting: desired: %d Hz, Actual : %d Hz \n"),
                origClockSetting, pSetting->ClockRate));
        }

    }

    return status;
}

///////////////////////////////////////////////////////////////////////////////
//  SDGetSDIOPnpInformation - Get SDIO PnP information for an SDIO device function
//  Input:  pDevice - the device context
//          Parent - this is the parent device
//  Output:
//  Return: SD_API_STATUS code
//  Notes:  
//         This function collects SDIO Pnp information for the device.
//         
///////////////////////////////////////////////////////////////////////////////
SD_API_STATUS SDGetSDIOPnpInformation(PSDCARD_DEVICE_CONTEXT pDevice, BOOL Parent) 
{

    SD_API_STATUS          status;           // intermediate status
    UCHAR                  regValue[4];      // most tuples are 4 bytes
    UCHAR                  CSA_CISBuffer[CIS_CSA_BYTES]; // CIS and CSA are in contiguous locations
    DWORD                  FBROffset;        // calculated FBR offset
    DWORD                  manFid;           // manufacturer ID
    ULONG                  length;           // buffer length
    BOOL                   found;            // CIS found flag
    PCHAR                  pVersionBuffer;   // tuple buffer for vers1 info

    status = SD_API_STATUS_SUCCESS;

    // for the parent device save the common information
    if (Parent) {

        // allocate the common information
        pDevice->SDCardInfo.SDIOInformation.pCommonInformation = (PSDIO_COMMON_INFORMATION)
            SDAllocateMemoryWithTag(sizeof(SDIO_COMMON_INFORMATION),
            SD_BUS_DRIVER_TAG);

        if (NULL == pDevice->SDCardInfo.SDIOInformation.pCommonInformation) {
            return SD_API_STATUS_INSUFFICIENT_RESOURCES;
        }

        // get the CCCR
        status = SDReadWriteRegistersDirect__X((SD_DEVICE_HANDLE)pDevice,
            SD_IO_READ,          
            0,      // all from function 0
            SD_IO_REG_CCCR,
            FALSE,
            regValue,
            1); 

        if (!SD_API_SUCCESS(status)) {
            DEBUGMSG(SDCARD_ZONE_ERROR, (TEXT("SDBusDriver: Failed to get CCCR \n")));
            return status;
        }

        pDevice->SDCardInfo.SDIOInformation.pCommonInformation->CCCRRev = regValue[0];

        // get the card capabilities register
        status = SDReadWriteRegistersDirect__X((SD_DEVICE_HANDLE)pDevice,
            SD_IO_READ,          
            0,      // all from function 0
            SD_IO_REG_CARD_CAPABILITY,
            FALSE,
            regValue,
            1); 

        if (!SD_API_SUCCESS(status)) {
            DEBUGMSG(SDCARD_ZONE_ERROR, (TEXT("SDBusDriver: Failed to get CARD CAPABILITY register \n")));
            return status;
        }

        pDevice->SDCardInfo.SDIOInformation.pCommonInformation->CardCapability = regValue[0];

        // get the SD Spec rev
        status = SDReadWriteRegistersDirect__X((SD_DEVICE_HANDLE)pDevice,
            SD_IO_READ,          
            0,      // all from function 0
            SD_IO_REG_SPEC_REV,
            FALSE,
            regValue,
            1); 

        if (!SD_API_SUCCESS(status)) {
            DEBUGMSG(SDCARD_ZONE_ERROR, (TEXT("SDBusDriver: Failed to get SD Spec revision \n")));
            return status;
        }

        pDevice->SDCardInfo.SDIOInformation.pCommonInformation->SDSpec = regValue[0];

        DEBUGMSG(SDBUS_ZONE_DEVICE, (TEXT("SDBusDriver: SDIO Card Function %d, CCCR 0x%02X , Card Caps: 0x%02X, Spec Ver: %d \n"), 
            pDevice->SDCardInfo.SDIOInformation.Function,
            pDevice->SDCardInfo.SDIOInformation.pCommonInformation->CCCRRev,
            pDevice->SDCardInfo.SDIOInformation.pCommonInformation->CardCapability,
            pDevice->SDCardInfo.SDIOInformation.pCommonInformation->SDSpec)); 


        // get the CIS pointer
        // function 0 only has a CIS, no CSA
        status = SDReadWriteRegistersDirect__X((SD_DEVICE_HANDLE)pDevice,
            SD_IO_READ,          
            0,      // all from function 0
            SD_IO_REG_COMMON_CIS_POINTER,
            FALSE,
            CSA_CISBuffer,
            SD_IO_CIS_PTR_BYTES); 
        if (!SD_API_SUCCESS(status)) {
            return status;
        }

        pDevice->SDCardInfo.SDIOInformation.pCommonInformation->CommonCISPointer = CSA_CISBuffer[CIS_OFFSET_BYTE_0] | 
            (CSA_CISBuffer[CIS_OFFSET_BYTE_1] << 8) |
            (CSA_CISBuffer[CIS_OFFSET_BYTE_2] << 16);    

#if 0
        // for Debugging a bad CIS only
        {
            UCHAR tupleBuffer[256];
            status = SDReadWriteRegistersDirect((SD_DEVICE_HANDLE)pDevice,
                SD_IO_READ,          
                0,      // all from function 0
                pDevice->SDCardInfo.SDIOInformation.pCommonInformation->CommonCISPointer,
                FALSE,
                tupleBuffer,
                256); 
            if (SD_API_SUCCESS(status)) {
                DEBUGMSG(SDCARD_ZONE_ERROR, (TEXT("SDBusDriver:  Common CIS Pointer: 0x%08X \n"),
                    pDevice->SDCardInfo.SDIOInformation.pCommonInformation->CommonCISPointer));     
                SDOutputBuffer(tupleBuffer, 256);         
            }

        }

#endif
        length = SD_CISTPL_MANFID_BODY_SIZE;

        // for function 0 get the Manufacturer ID (this is not the same as the VER1 string)
        status = SDGetTuple__X((SD_DEVICE_HANDLE)pDevice,
            SD_CISTPL_MANFID,
            (PUCHAR)&manFid,
            &length,
            TRUE);

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

        if (0 == length) {
            DEBUGMSG(SDCARD_ZONE_ERROR, (TEXT("SDBusDriver:  Card is missing CISTPL_MANFID \n")));
            status = SD_API_STATUS_DEVICE_UNSUPPORTED;
            return status;   
        }
        // set Manufacturer and CardID 
        pDevice->SDCardInfo.SDIOInformation.pCommonInformation->ManufacturerID = (USHORT)manFid;
        pDevice->SDCardInfo.SDIOInformation.pCommonInformation->CardID = (USHORT)(manFid >> 16);

        // retrieve the ver_1 tuple to retrieve the manufacturer string
        length = 0;


        // query the size of the tuple from the common CIS
        // the VERS_1 tuple is a variable length tuple
        status = SDGetTuple__X((SD_DEVICE_HANDLE)pDevice,
            SD_CISTPL_VERS_1,
            NULL,
            &length,
            TRUE);

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

        if (0 != length ) {
            found = TRUE;

⌨️ 快捷键说明

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