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

📄 atapi_cf_load.c

📁 2443 wince5.0 bsp, source code
💻 C
字号:
//
// 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 ?1999 Intel Corp.  */
/*

Module Name:

  sdmmc_load.c

Abstract:

  Implementation of the WinCE SD-MMC Loader Device Driver.

Functions:

  
 
Notes:


*/

#include <windows.h>
#include <nkintr.h>

#include <pm.h> 
#include "pmplatform.h"
      
#include <memory.h>

#include "atapi_cf_load.h"

#ifdef DEBUG

    #define DBG_INIT    0x0001
    #define DBG_OPEN    0x0002
    #define DBG_READ    0x0004
    #define DBG_WRITE   0x0008
    #define DBG_CLOSE   0x0010
    #define DBG_IOCTL   0x0020
    #define DBG_THREAD  0x0040
    #define DBG_EVENTS  0x0080
    #define DBG_CRITSEC 0x0100
    #define DBG_FLOW    0x0200
    #define DBG_IR      0x0400
    #define DBG_NOTHING 0x0800
    #define DBG_ALLOC   0x1000
    #define DBG_FUNCTION 0x2000
    #define DBG_WARNING 0x4000
    #define DBG_ERROR   0x8000

DBGPARAM dpCurSettings = {
    TEXT("postdriver"), {
        TEXT("Init"),TEXT("Open"),TEXT("Read"),TEXT("Write"),
        TEXT("Close"),TEXT("Ioctl"),TEXT("Thread"),TEXT("Events"),
        TEXT("CritSec"),TEXT("FlowCtrl"),TEXT("Infrared"),TEXT("User Read"),
        TEXT("Alloc"),TEXT("Function"),TEXT("Warning"),TEXT("Error")},
    0
}; 

#define ZONE_INIT		DEBUGZONE(0)
#define ZONE_OPEN		DEBUGZONE(1)
#define ZONE_READ		DEBUGZONE(2)
#define ZONE_WRITE		DEBUGZONE(3)
#define ZONE_CLOSE		DEBUGZONE(4)
#define ZONE_IOCTL		DEBUGZONE(5)
#define ZONE_THREAD	DEBUGZONE(6)
#define ZONE_EVENTS		DEBUGZONE(7)
#define ZONE_CRITSEC	DEBUGZONE(8)
#define ZONE_FLOW		DEBUGZONE(9)
#define ZONE_IR			DEBUGZONE(10)
#define ZONE_USR_READ	DEBUGZONE(11)
#define ZONE_ALLOC		DEBUGZONE(12)
#define ZONE_FUNCTION	DEBUGZONE(13)
#define ZONE_WARN		DEBUGZONE(14)
#define ZONE_ERROR		DEBUGZONE(15)


#endif //DEBUG

// Global variables.

CEDEVICE_POWER_STATE m_Dx;
HANDLE hDevice = NULL;


WCHAR *szRegKey = L"System\\StorageManager\\Profiles\\2443_CF";

#define WRITE_REG_SZ(Name, Value) RegSetValueEx( hKey, Name, 0, REG_SZ, (LPBYTE)Value, (wcslen(Value)+1)*sizeof(WCHAR))
#define WRITE_REG_DWORD(Name, Value) { DWORD dwValue = Value; RegSetValueEx( hKey, Name, 0, REG_DWORD, (LPBYTE)&dwValue, sizeof(DWORD)); }

/************************************************************************************************************
* Function: GetRegistryValue
* 
* Purpose:  Given a Handle and a name, reads the Win CE registry and obtains the value for it.
*  
* Returns:  SUCCESS on reading the registry correctly or FAILURE otherwise.
*
************************************************************************************************************/


BOOL GetRegistryValue(HKEY hKey, PTSTR szValueName, PDWORD pdwValue)
{
    
    DWORD               dwValType, dwValLen;
    LONG                lStatus;
            
    dwValLen = sizeof(DWORD);
        
    lStatus = RegQueryValueEx( hKey, szValueName, NULL, &dwValType, (PBYTE)pdwValue, &dwValLen);
        
    if ((lStatus != ERROR_SUCCESS) || (dwValType != REG_DWORD)) 
	{           
		//RETAILMSG (1,(TEXT("RelFSD_Load::RegQueryValueEx(%s) failed -returned %d  Error=%08X\n\r"), szValueName, lStatus, GetLastError()));
        *pdwValue = -1;
        return FALSE;
    } 
	//RETAILMSG (1,(TEXT("RelFSD_Load::GetRegistryValue(%s) Value(%x) hKey: %x\n\r"), szValueName,*pdwValue,hKey));
    return TRUE;
}

/************************************************************************************************************
* Function: DoRegSetup
* 
* Purpose:  Given a pointer to a key, will fill it up correctly with the values needed to load the SD-MMC driver.
*  
* Returns:  void
*
************************************************************************************************************/




void DoSetHandleValue(WCHAR *szRegKey, HANDLE hDevice)
{

    HKEY hKey = NULL;
    LONG nErr = RegOpenKeyEx( HKEY_LOCAL_MACHINE, szRegKey, 0, 0, &hKey);
    if (nErr == ERROR_SUCCESS) 
	{
        WRITE_REG_DWORD(L"HDEVICE", (DWORD)hDevice)
    }    
    RegCloseKey( hKey);
}

void DoDeleteHandleValue(WCHAR *szRegKey)
{

    HKEY hKey = NULL;
    LONG nErr = RegOpenKeyEx( HKEY_LOCAL_MACHINE, szRegKey, 0, 0, &hKey);
    if (nErr == ERROR_SUCCESS) 
	{
        RegDeleteValue( hKey, L"HDEVICE");
    }    
    RegCloseKey( hKey);
}


/************************************************************************************************************
* Function: ACL_Init
* 
* Purpose:  performs some initialisation. spawns the main loader thread. 
*  
* Returns:  void
*
************************************************************************************************************/

BOOL WINAPI ACL_Init()
{
#if 0
	SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);

	 g_hSDMMCDetectInterrupt = CreateEvent( NULL, FALSE, FALSE, NULL);

	if (!g_hSDMMCDetectInterrupt)
	{
       	 //RETAILMSG(1,(TEXT("Fail to create SDMMC Detect Interrupt Event\n\r")));
	        return FALSE;
	}

	if(!(InterruptInitialize((DWORD)SYSINTR_SDMMC_CARD_DETECT, g_hSDMMCDetectInterrupt, NULL, 0)))
	{
    		//RETAILMSG(1,(TEXT("Fail to link the SDMMC Detect Interrupt to the Event\n\r")));
		return FALSE;
	}
	
	gSDMMCDetectThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) SDMMCDetectThread, NULL, 0, NULL);
		
	if ( gSDMMCDetectThread == NULL ) 
	{
       	 //RETAILMSG(1, (TEXT("Fatal Error!  Failed to create MMC card detect thread.\r\n")));
	        return (FALSE);
	}
#endif	
	m_Dx = D0;
	DevicePowerNotify(_T("ACL1:"),D0, POWER_NAME);

	RETAILMSG(1, (TEXT("################### card is detected\r\n")));
	hDevice = ActivateDevice( szRegKey, 0);
	DoSetHandleValue( szRegKey, hDevice);	

	//RETAILMSG(1, (TEXT("create MMC card detect thread successfull.\r\n")));
 	return TRUE;
	
}


BOOL WINAPI ACL_DllEntry( HANDLE  hInstDll,
						  DWORD   dwReason,
						  LPVOID  lpvReserved
						)
{
    switch ( dwReason ) 
	{
		case DLL_PROCESS_ATTACH:
			DEBUGREGISTER((HINSTANCE) hInstDll);
			//RETAILMSG(1, (TEXT("SDMMCLoader : DLL_PROCESS_ATTACH\r\n")));
			break;

		case DLL_PROCESS_DETACH:
			// should be signaling thread here
			//RETAILMSG(1, (TEXT("SDMMCLoader : DLL_PROCESS_DETACH\r\n")));
			break;
    }

    return (TRUE);
}

// The following functions are all stubbed out. They are not required but are provided for the sake 
// of code completion.

DWORD ACL_Open (DWORD dwData, DWORD dwAccess, DWORD dwShareMode) 
{
	return (4);
}

BOOL  ACL_Close(DWORD dwData) 
{
	return (TRUE);
}

DWORD ACL_Read (DWORD dwData,  LPVOID pBuf, DWORD Len) 
{
	return (0);
}

DWORD ACL_Write(DWORD dwData, LPCVOID pBuf, DWORD Len) 
{
	return (0);
}

DWORD ACL_Seek (DWORD dwData, long pos, DWORD type) 
{
	return (DWORD)-1;
}


VOID  ACL_PowerUp  (VOID) 
{

}

BOOL  ACL_PowerDown(VOID) 
{
	return (TRUE);
}

BOOL  ACL_Deinit(DWORD dwData) 
{
	return (TRUE);
}

BOOL  ACL_IOControl(DWORD  dwOpenData,
    				DWORD  dwCode,
    				PBYTE  pBufIn,
    				DWORD  dwLenIn,
				    PBYTE  pBufOut,
				    DWORD  dwLenOut,
				    PDWORD pdwActualOut)
{
    DWORD dwErr = ERROR_SUCCESS;    
    BOOL  bRc = TRUE;

    UNREFERENCED_PARAMETER(dwOpenData);
    
    //RETAILMSG(1,(TEXT("ACL_IOCONTROL: dwcode=%x\r\n"), dwCode));  
 	//RETAILMSG(1,(TEXT("ACL_IOCONTROL: IOCTL_POWER_CAPABILITIES=%x\r\n"), IOCTL_POWER_CAPABILITIES));     
 	//RETAILMSG(1,(TEXT("ACL_IOCONTROL: IOCTL_POWER_SET=%x\r\n"), IOCTL_POWER_SET)); 
 	//RETAILMSG(1,(TEXT("ACL_IOCONTROL: IOCTL_POWER_GET=%x\r\n"), IOCTL_POWER_GET)); 
 	//RETAILMSG(1,(TEXT("ACL_IOCONTROL: IOCTL_POWER_CAPABILITIES=%x\r\n"), IOCTL_POWER_CAPABILITIES));  
 	
    switch (dwCode) {
        //
        // Power Management
        //
        case IOCTL_POWER_CAPABILITIES: 
        {
            PPOWER_CAPABILITIES ppc;
            
            
            if ( !pdwActualOut || !pBufOut || (dwLenOut < sizeof(POWER_CAPABILITIES)) ) {
                bRc = FALSE;
                dwErr = ERROR_INVALID_PARAMETER;
                break;
            }
            
            ppc = (PPOWER_CAPABILITIES)pBufOut;
            
            memset(ppc, 0, sizeof(POWER_CAPABILITIES));

            // support D0, D4
            ppc->DeviceDx = 0x11;

            // no wake
            // no inrush

            // Report our nominal power consumption in uAmps rather than mWatts.
            ppc->Flags = POWER_CAP_PREFIX_MICRO | POWER_CAP_UNIT_AMPS;

            // REVIEW: Do we enable all these for normal playback?
            // D0: SPI + I2S + CODEC (Playback) + Headphone= 
            //     0.5 mA + 0.5 mA + (23 mW, into BUGBUG ohms ) + (30 mW, into 32 ohms)
            //     500 uA + 500 uA + 23000 uA + 32000 uA
            ppc->Power[D0] = 56000;
            
            *pdwActualOut = sizeof(POWER_CAPABILITIES);  
            //RETAILMSG(1,(TEXT("ACL_IOCONTROL: powerCapability")));
        } break;

        case IOCTL_POWER_SET: 
        {
            CEDEVICE_POWER_STATE NewDx;
            
			bRc = TRUE;

            if ( !pdwActualOut || !pBufOut || (dwLenOut < sizeof(CEDEVICE_POWER_STATE)) ) {
                bRc = FALSE;
                dwErr = ERROR_INVALID_PARAMETER;
                break;
            }

            NewDx = *(PCEDEVICE_POWER_STATE)pBufOut;

            if ( VALID_DX(NewDx) ) {
                // grab the CS since the normal Xxx_PowerXxx can not.
		switch ( NewDx ) 
		{
			case D0:
				if (m_Dx != D0)	
				{
					//DoDeleteHandleValue(szRegKey);
					//DeactivateDevice( hDevice);
					RETAILMSG(1, (TEXT("card is detected\r\n")));
					hDevice = ActivateDevice( szRegKey, 0);
					//DoSetHandleValue( szRegKey, hDevice);
					
					m_Dx = D0;
                        	}

                       	break;

			default:
				if (m_Dx != D4) 
				{
					DeactivateDevice( hDevice);
					m_Dx = D4;       
				}
				break;
		}
                
                // return our state
                *(PCEDEVICE_POWER_STATE)pBufOut = m_Dx;
                
                //RETAILMSG(1, (TEXT("SML: IOCTL_POWER_SET: D%u => D%u \r\n"), NewDx, m_Dx));

                
                *pdwActualOut = sizeof(CEDEVICE_POWER_STATE);
            } else {
                bRc = FALSE;
                dwErr = ERROR_INVALID_PARAMETER;
            }
            
        } break;

        case IOCTL_POWER_GET: 
            if ( !pdwActualOut || !pBufOut || (dwLenOut < sizeof(CEDEVICE_POWER_STATE)) ) {
                bRc = FALSE;
                dwErr = ERROR_INVALID_PARAMETER;
                break;
            }

            *(PCEDEVICE_POWER_STATE)pBufOut = m_Dx;

            //RETAILMSG(1, (TEXT("SML: IOCTL_POWER_GET: D%u \r\n"), m_Dx));

            *pdwActualOut = sizeof(CEDEVICE_POWER_STATE);
            break;
            
        default:
            bRc = FALSE;
            dwErr = ERROR_INVALID_FUNCTION;
            DEBUGMSG (ZONE_FUNCTION, (TEXT(" Unsupported ioctl 0x%X\r\n"), dwCode));
            break;            
    }
    
    if ( !bRc ) {
        SetLastError(dwErr);
    }
    
    return(bRc);	        
}



⌨️ 快捷键说明

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