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

📄 sdmain.cpp

📁 2443 wince5.0 bsp, source code
💻 CPP
字号:
//
// 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

// Driver entry points for bus driver    

#include "SDBusDriver.h"
#include <bldver.h>

// initialize debug zones
SD_DEBUG_INSTANTIATE_ZONES(
                           TEXT("SDBusDriver"), // module name
                           ZONE_ENABLE_INIT | ZONE_ENABLE_ERROR | ZONE_ENABLE_WARN,
                           TEXT("HC Driver Load"),
                           TEXT("Dispatcher"), 
                           TEXT("Shutdown"), 
                           TEXT("Power"),                  
                           TEXT("Device Load"), 
                           TEXT("Bus Requests"), 
                           TEXT("Buffer Dumps"), 
                           TEXT(""),
                           TEXT(""),
                           TEXT(""),
                           TEXT(""));

// the ONLY instance of the bus driver
static CSDBusDriver        *s_pSDBusDriver;
static BOOL                 s_fBusAccessContext; // Dummy data to use as a pointer


///////////////////////////////////////////////////////////////////////////////
//  DllEntry - the main dll entry point
//  Input:  hInstance - the instance that is attaching
//          Reason - the reason for attaching
//          pReserved - not much
//  Output: 
//  Return: Always returns TRUE
//  Notes:  this is only used to initialize the zones
///////////////////////////////////////////////////////////////////////////////
extern "C"
BOOL DllEntry(HINSTANCE  hInstance,
              ULONG      Reason,
              LPVOID     pReserved)
{
    BOOL fRet = TRUE;

    if ( Reason == DLL_PROCESS_ATTACH ) {
        s_pSDBusDriver = NULL;
        DEBUGMSG(SDCARD_ZONE_INIT, (TEXT("SDBusDriver: PROCESS_ATTACH \n")));
        DisableThreadLibraryCalls((HMODULE) hInstance);
        SD_DEBUG_ZONE_REGISTER(hInstance, NULL);

        if (!SDInitializeCardLib()) {
            fRet = FALSE;
        }
    }
    else if ( Reason == DLL_PROCESS_DETACH ) {
        DEBUGMSG(SDCARD_ZONE_INIT, (TEXT("SDBusDriver: PROCESS_DETACH \n")));
        SDDeinitializeCardLib();
    }

    return fRet;
}

///////////////////////////////////////////////////////////////////
//  ShutDownBusDriverInstance - shutdown the bus driver instance
//  Input:  dwContext - context passed into XXX_Deinit
//  Output:
//  Return: BOOL
//  Notes:  
//         
////////////////////////////////////////////////////////////////////
BOOL ShutDownBusDriverInstance(DWORD dwContext)
{
    BOOLEAN ret = TRUE; // return value

    // check the single instance
    if (s_pSDBusDriver != NULL)  {        
        RETAILMSG(1, ((TEXT("SDBusDriver: Shutting Down Bus Driver...\n"))));
        delete s_pSDBusDriver;
        s_pSDBusDriver = NULL;
    } else {
        RETAILMSG(1, ((TEXT("SDBusDriver: Bus Driver already shutdown! \n"))));
        ret = FALSE;
    }

    return ret;
}



///////////////////////////////////////////////////////////////////////////////
//  SDC_Close - the close entry point for the bus driver
//  Input:  hOpenContext - the context returned from SDC_Open
//  Output: 
//  Notes:  always returns TRUE
///////////////////////////////////////////////////////////////////////////////
extern "C"
BOOL SDC_Close(DWORD hOpenContext)
{
    DEBUGMSG(SDCARD_ZONE_FUNC, (TEXT("SDBusDriver: +-SDC_Close\n")));

    return TRUE;
}


///////////////////////////////////////////////////////////////////////////////
//  SDC_PreDeinit - the predeinit entry point for the SD bus driver
//  Input:  hDeviceContext - the context returned from SDC_Init
//  Output: 
//  Notes:  always returns TRUE.  
///////////////////////////////////////////////////////////////////////////////
extern "C"
BOOL SDC_PreDeinit(DWORD hDeviceContext)
{
    DEBUGMSG(SDCARD_ZONE_INIT, (TEXT("SDBusDriver: +SDC_PreDeinit\n")));

    // Nothing to do here. Just make sure that the device manager won't
    // call Deinit until everyone is out of SDC_IOControl.

    DEBUGMSG(SDCARD_ZONE_INIT, (TEXT("SDBusDriver: -SDC_PreDeinit\n")));
    return TRUE;
}


///////////////////////////////////////////////////////////////////////////////
//  SDC_Deinit - the deinit entry point for the SD bus driver
//  Input:  hDeviceContext - the context returned from SDC_Init
//  Output: 
//  Notes:  always returns TRUE.  
///////////////////////////////////////////////////////////////////////////////
extern "C"
BOOL SDC_Deinit(DWORD hDeviceContext)
{
    DEBUGMSG(SDCARD_ZONE_INIT, (TEXT("SDBusDriver: +SDC_Deinit\n")));

    // shutdown the bus driver
    ShutDownBusDriverInstance(hDeviceContext);

    DEBUGMSG(SDCARD_ZONE_INIT, (TEXT("SDBusDriver: -SDC_Deinit\n")));
    return TRUE;
}

///////////////////////////////////////////////////////////////////////////////
//  SDC_Init - the init entry point for the bus driver
//  Input:  dwContext - the context for this init
//  Output:
//  Return:  returns a non-zero value (pointer to the bus driver instance)
//  Notes:  
///////////////////////////////////////////////////////////////////////////////
extern "C"
DWORD SDC_Init(DWORD dwContext)
{
    LPCTSTR         pszActiveKey = (LPCTSTR) dwContext;

    DEBUGMSG(SDCARD_ZONE_INIT, (TEXT("SDBusDriver: +SDC_Init\n")));

    if (s_pSDBusDriver != NULL) {
        DEBUGMSG(SDCARD_ZONE_ERROR,
            (TEXT("SDBusDriver: Bus Driver Already loaded, only one instance allowed \n")));
        return 0;   
    }

    // create the instance
    s_pSDBusDriver = new CSDBusDriver(pszActiveKey);

    if (s_pSDBusDriver == NULL) {
        DEBUGMSG(SDCARD_ZONE_ERROR, (TEXT("SDC_Init: Failed to create instance of the bus driver\n")));
        return 0;
    }

    SD_API_STATUS status = s_pSDBusDriver->Initialize();
    if (!SD_API_SUCCESS(status)) {
        DEBUGMSG(SDCARD_ZONE_ERROR, (TEXT("SDC_Init: Bus driver failed to initialize : status: 0x%08X  \n"), status));
        delete s_pSDBusDriver;
        return 0; 
    }

    DEBUGMSG(SDCARD_ZONE_INIT, (TEXT("SDBusDriver: Bus Driver instance created : 0x%08X ! -SDC_Init \n"), s_pSDBusDriver));

    // return pointer to bus driver object as a context
    return (DWORD)s_pSDBusDriver;
}

///////////////////////////////////////////////////////////////////////////////
//  SDGetSystemContext - get the system context
//  Input: 
//  Output:
//  Return: returns the system context, NULL if the system is not initialized
//  Notes:  
///////////////////////////////////////////////////////////////////////////////
PVOID SDGetSystemContext()
{
    if (s_pSDBusDriver != NULL) {
        if (s_pSDBusDriver->IsReady()) {
            // return the bus driver object as a context
            return (PVOID)s_pSDBusDriver;
        }
    }

    return NULL;
}

///////////////////////////////////////////////////////////////////////////////
//  SDC_IOControl - the I/O control entry point for the bus driver
//  Input:  hOpenContext - the context returned from SDC_Open
//          dwCode - the ioctl code
//          pBufIn - the input buffer from the user
//          dwLenIn - the length of the input buffer
//          pBufOut - the output buffer from the user
//          dwLenOut - the length of the output buffer
//          pdwActualOut - the size of the transfer
//  Output:
//  Return: TRUE if ioctl was successfully handled 
//  Notes:  
///////////////////////////////////////////////////////////////////////////////
extern "C"
BOOL SDC_IOControl(DWORD   hOpenContext,
                   DWORD   dwCode,
                   PBYTE   pBufIn,
                   DWORD   dwLenIn,
                   PBYTE   pBufOut,
                   DWORD   dwLenOut,
                   PDWORD  pdwActualOut)
{
    BOOL fRet = TRUE;
    CSDBusDriver *pBusDriver = s_pSDBusDriver;

    DEBUGMSG(SDCARD_ZONE_FUNC, (TEXT("SDBusDriver: +SDC_IOControl\n")));

    if (hOpenContext == (DWORD) &s_fBusAccessContext) {
        // Bus access
        fRet = pBusDriver->BusIOControl(dwCode, pBufIn, dwLenIn, pBufOut, 
            dwLenOut, pdwActualOut);
    }
    else {
        // Normal access
        DEBUGCHK(hOpenContext == (DWORD) pBusDriver);

        switch (dwCode) {
            case IOCTL_SD_BUS_DRIVER_GET_SLOT_COUNT:
                if (dwLenOut != sizeof(DWORD)) {
                    SetLastError(ERROR_INVALID_PARAMETER);
                    fRet = FALSE;
                } else{
                    *((DWORD *)pBufOut) = pBusDriver->GetSlotInfo(NULL, 0);
                    *pdwActualOut = sizeof(DWORD);
                }
                break;

            case IOCTL_SD_BUS_DRIVER_GET_SLOT_INFO:
                if (0 == pBusDriver->GetSlotInfo(pBufOut, dwLenOut)) {
                    SetLastError(ERROR_INVALID_PARAMETER);
                    fRet = FALSE;
                } else {
                    *pdwActualOut = dwLenOut;
                }

                break;

            case IOCTL_SD_BUS_DRIVER_GET_VERSION:
                if (dwLenOut != sizeof(DWORD)) {
                    SetLastError(ERROR_INVALID_PARAMETER);
                    fRet = FALSE;
                } else{
                    *((DWORD *)pBufOut) = MAKELONG(CE_MINOR_VER, CE_MAJOR_VER);
                    *pdwActualOut = sizeof(DWORD);
                }

                break;

            default:
                SetLastError(ERROR_INVALID_PARAMETER);
                fRet = FALSE;
        }
    }

    DEBUGMSG(SDCARD_ZONE_FUNC, (TEXT("SDBusDriver: -SDC_IOControl 0x%X\n"), fRet));
    return fRet;
}

///////////////////////////////////////////////////////////////////////////////
//  SDC_Open - the open entry point for the bus driver
//  Input:  hDeviceContext - the device context from SDC_Init
//          AccessCode - the desired access
//          ShareMode - the desired share mode
//  Output: 
//  Return: returns open context (in this case, just the context from SDC_Init)
//  Notes:  
///////////////////////////////////////////////////////////////////////////////
extern "C"
DWORD SDC_Open(DWORD    hDeviceContext,
               DWORD    AccessCode,
               DWORD    ShareMode)
{
    DWORD dwRet;

    DEBUGMSG(SDCARD_ZONE_FUNC, (TEXT("SDBusDriver: +-SDC_Open\n")));

    DEBUGCHK(hDeviceContext == (DWORD) s_pSDBusDriver);

    if (AccessCode & DEVACCESS_BUSNAMESPACE) {
        // This is a bus access
        dwRet = (DWORD) &s_fBusAccessContext;
    }
    else {
        // Standard access
        dwRet = hDeviceContext;
    }

    return dwRet;
}


// Fill in the host controller's function table.
extern "C"
SD_API_STATUS 
SDHCDGetHCFunctions(
                    PSDHOST_API_FUNCTIONS pFunctions
                    )
{    
    SD_API_STATUS status = SD_API_STATUS_INVALID_PARAMETER;

    DEBUGMSG(SDCARD_ZONE_INIT, (TEXT("SDHCDGetHCFunctions: +Init\n")));

    if ( pFunctions && (pFunctions->dwSize == sizeof(*pFunctions)) ) {
        pFunctions->pAllocateContext = &SDHCDAllocateContext__X;
        pFunctions->pDeleteContext = &SDHCDDeleteContext__X;
        pFunctions->pRegisterHostController = &SDHCDRegisterHostController__X;
        pFunctions->pDeregisterHostController = &SDHCDDeregisterHostController__X;
        pFunctions->pIndicateSlotStateChange = &SDHCDIndicateSlotStateChange__X;
        pFunctions->pIndicateBusRequestComplete = &SDHCDIndicateBusRequestComplete__X;
        pFunctions->pUnlockRequest = &SDHCDUnlockRequest__X;
        pFunctions->pGetAndLockCurrentRequest = &SDHCDGetAndLockCurrentRequest__X;
        pFunctions->pPowerUpDown = &SDHCDPowerUpDown__X;

        status = SD_API_STATUS_SUCCESS;
    }
    else {
        DEBUGMSG(SDCARD_ZONE_ERROR, (TEXT("SDHCDGetHCFunctions: Invalid parameter\n")));
    }

    DEBUGMSG(SDCARD_ZONE_INIT, (TEXT("SDHCDGetHCFunctions: -Init\n")));

    return status;
}

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

⌨️ 快捷键说明

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