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

📄 backlight_wince.c

📁 WINCE下,背光驱动源码.
💻 C
📖 第 1 页 / 共 2 页
字号:
//  File: backlight.c
//
//  Backlight driver source code
//
#include <windows.h>
#include <pm.h>
#include <ceddkex.h>
#include <ceddk.h>
#include <omap730.h>
#include <gpio.h>
#include <tf_oal_ioctl.h>
#ifndef BUILD_ULDR
#include <Regext.h>
#include <winuserm.h>
#endif
//------------------------------------------------------------------------------
//
//  Global:  dpCurSettings
//
//  Set debug zones names and initial setting for driver
//
#ifdef DEBUG

#define ZONE_ERROR          DEBUGZONE(0)
#define ZONE_WARN           DEBUGZONE(1)
#define ZONE_FUNCTION       DEBUGZONE(2)
#define ZONE_INIT           DEBUGZONE(3)

DBGPARAM dpCurSettings = {
    L"Backlight", {
        L"Errors",      L"Warnings",    L"Function",    L"Init",
        L"Info",        L"IOCTL",       L"Undefined",   L"Undefined",
        L"Undefined",   L"Undefined",   L"Undefined",   L"Undefined",
        L"Undefined",   L"Undefined",   L"Undefined",   L"Undefined"
    },
    0xC000
};
#endif

//------------------------------------------------------------------------------
//  Local Definitions

#define BKL_DEVICE_COOKIE       'bklD'
#define BKL_INSTANCE_COOKIE     'bklI'
#define RETAILMSGZONE           0
#define BACKLIGHTDIMLEVEL       5
//------------------------------------------------------------------------------
//  Local Structures

typedef struct {
    DWORD cookie;
    LONG instances;
    BOOL fForceOFF;
    CEDEVICE_POWER_STATE ps;
    CEDEVICE_POWER_STATE Lastps;
    DWORD dwBrightness;
#ifndef BUILD_ULDR
    HREGNOTIFY hBrightNotify;
    HREGNOTIFY hBattOnTapNotify;
    BOOL fBattOnTap;
    HREGNOTIFY hACOnTapNotify;
    BOOL fACOnTap;
    HREGNOTIFY hACLineStatus;
    DWORD ACLineStatus;
    BOOL JustChanged;
	HREGNOTIFY hACTimeoutNotify;
    HREGNOTIFY hBatteryTimeoutNotify;
	DWORD ACTimeout;
	DWORD BatteryTimeout;
#endif
} BKL_DEVICE;

typedef struct {
    DWORD cookie;
    BKL_DEVICE *pDevice;
} BKL_INSTANCE;

#ifndef BUILD_ULDR
#define BACKLIGHT_SET_PATH          TEXT("ControlPanel\\BackLight")
#define BACKLIGHT_SET_KEY           TEXT("BatteryTimeout")
#define BACKLIGHT_SET_BACKKEY       TEXT("TimeoutSaved")
#endif

typedef volatile struct {
    UINT8 PWL_LEVEL_REG;
    UINT8 PWL_CTRL_REG;
} OMAP730_PWL_REGS;
static OMAP730_PWL_REGS *g_pPWLMAPAddress = NULL;
#define     PWL_PWL_BASE_REG    0xfffb5800
PHYSICAL_ADDRESS pa;

//------------------------------------------------------------------------------
//  Local Functions

BOOL  BKL_Deinit(DWORD context);
void RefreshBKL(BKL_DEVICE *pDevice);
#ifndef BUILD_ULDR
void CheckBacklightTimeout();
void BrightCallback(HREGNOTIFY hNotify, DWORD dwUserData, const PBYTE pData, const UINT cbData);
#endif

//------------------------------------------------------------------------------
//
//  Function:  BKL_Init
//
//  Called by device manager to initialize device.
//
DWORD BKL_Init(LPCTSTR szContext, LPCVOID pBusContext)
{
    DWORD rc = (DWORD)NULL;
    BKL_DEVICE *pDevice = NULL;	
  
    RETAILMSG(1, (
        L"+BKL_Init(%s, 0x%08x)\r\n", szContext, pBusContext
    ));

    // Create device structure
    pDevice = (BKL_DEVICE *)LocalAlloc(LPTR, sizeof(BKL_DEVICE));
    if (pDevice == NULL) {
        DEBUGMSG(ZONE_ERROR, (L"ERROR: BKL_Init: "
            L"Failed allocate BKL device structure\r\n"
        ));
        goto cleanUp;
    }

    // Set cookie
    pDevice->cookie = BKL_DEVICE_COOKIE;
    pDevice->ps = D0;
    pDevice->fForceOFF = FALSE;
#ifndef BUILD_ULDR
    pDevice->hBrightNotify = 0;
    pDevice->hBattOnTapNotify = 0;
    pDevice->hACOnTapNotify = 0;
	pDevice->hBatteryTimeoutNotify = 0;
    pDevice->hACTimeoutNotify = 0;
#endif

    //map the configure register physical address into the virtual address space
    pa.QuadPart = PWL_PWL_BASE_REG;
    g_pPWLMAPAddress = (OMAP730_PWL_REGS*)MmMapIoSpace(pa, sizeof(OMAP730_PWL_REGS), FALSE);
    if( g_pPWLMAPAddress == NULL )
	{
        RETAILMSG(1, (L"ERROR: OMAP730GPE:Init: Failed to map PWL registers\r\n"));
        goto cleanUp;
    }
  
	//Init the backlight
	pDevice->dwBrightness = 0x9b;
#ifndef BUILD_ULDR
    RegistryGetDWORD(HKEY_CURRENT_USER, L"ControlPanel\\Backlight",
                     L"Brightness", &pDevice->dwBrightness);
    RegistryNotifyCallback(HKEY_CURRENT_USER, L"ControlPanel\\Backlight",
                           L"Brightness", BrightCallback, (DWORD)pDevice,
                           NULL, &pDevice->hBrightNotify);

//Set the Brightness
   OUTREG8(&g_pPWLMAPAddress->PWL_CTRL_REG, 0x01);
   OUTREG8(&g_pPWLMAPAddress->PWL_LEVEL_REG, pDevice->dwBrightness);


    RegistryGetDWORD(HKEY_CURRENT_USER, L"ControlPanel\\Backlight",
                     L"BacklightOnTap", (DWORD *)&pDevice->fBattOnTap);
    if(pDevice->fBattOnTap)
    {
        RETAILMSG(RETAILMSGZONE, (TEXT("fBattOnTap TRUE\r\n")));  
    }
    else
    {
        RETAILMSG(RETAILMSGZONE, (TEXT("fBattOnTap FALSE\r\n")));  
    }
    RegistryNotifyCallback(HKEY_CURRENT_USER, L"ControlPanel\\Backlight",
                           L"BacklightOnTap", BrightCallback, (DWORD)pDevice,
                           NULL, &pDevice->hBattOnTapNotify);

    RegistryGetDWORD(HKEY_CURRENT_USER, L"ControlPanel\\Backlight",
                     L"ACBacklightOnTap", (DWORD *)&pDevice->fACOnTap);
    if(pDevice->fACOnTap)
    {
        RETAILMSG(RETAILMSGZONE, (TEXT("fACOnTap TRUE\r\n")));  
    }
    else
    {
        RETAILMSG(RETAILMSGZONE, (TEXT("fACOnTap FALSE\r\n")));  
    }
    RegistryNotifyCallback(HKEY_CURRENT_USER, L"ControlPanel\\Backlight",
                           L"ACBacklightOnTap", BrightCallback, (DWORD)pDevice,
                           NULL, &pDevice->hACOnTapNotify);

    RegistryNotifyCallback(HKEY_LOCAL_MACHINE, L"System\\State\\Battery",
                           L"ACLine", BrightCallback, (DWORD)pDevice,
                           NULL, &pDevice->hACLineStatus);

    RegistryGetDWORD(HKEY_LOCAL_MACHINE, L"System\\State\\Battery",
                     L"ACLine", &pDevice->ACLineStatus);
    RegistryNotifyCallback(HKEY_CURRENT_USER, L"ControlPanel\\Backlight",
                           L"BatteryTimeout", BrightCallback, (DWORD)pDevice,
                           NULL, &pDevice->hBatteryTimeoutNotify);

    RegistryNotifyCallback(HKEY_CURRENT_USER, L"ControlPanel\\Backlight",
                           L"ACTimeout", BrightCallback, (DWORD)pDevice,
                           NULL, &pDevice->hACTimeoutNotify);
    pDevice->JustChanged = FALSE;
    pDevice->Lastps = D4;
    CheckBacklightTimeout();
#endif
    RefreshBKL(pDevice);

    // Return non-null value
    rc = (DWORD)pDevice;

cleanUp:
    if (rc == 0) BKL_Deinit((DWORD)pDevice);
    DEBUGMSG(ZONE_FUNCTION, (L"-BKL_Init(rc = %d\r\n", rc));
    return rc;
}

//------------------------------------------------------------------------------
//
//  Function:  BKL_Deinit
//
//  Called by device manager to uninitialize device.
//
BOOL BKL_Deinit(DWORD context)
{
    BOOL rc = FALSE;
    BKL_DEVICE *pDevice = (BKL_DEVICE*)context;

    DEBUGMSG(ZONE_FUNCTION, (L"+BKL_Deinit(0x%08x)\r\n", context));

    // Check if we get correct context
    if (pDevice == NULL || pDevice->cookie != BKL_DEVICE_COOKIE) {
        DEBUGMSG (ZONE_ERROR, (L"ERROR: BKL_Deinit: "
            L"Incorrect context paramer\r\n"
        ));
        goto cleanUp;
    }

    // Check for open instances
    if (pDevice->instances > 0) {
        DEBUGMSG (ZONE_ERROR, (L"ERROR: BKL_Deinit: "
            L"Deinit with active instance (%d instances active)\r\n",
            pDevice->instances
        ));
        goto cleanUp;
    }

#ifndef BUILD_ULDR
    RegistryCloseNotification(pDevice->hACOnTapNotify);
    RegistryCloseNotification(pDevice->hBrightNotify);
    RegistryCloseNotification(pDevice->hBattOnTapNotify);
    RegistryCloseNotification(pDevice->hACLineStatus);
	RegistryCloseNotification(pDevice->hACTimeoutNotify);
    RegistryCloseNotification(pDevice->hBatteryTimeoutNotify);
#endif
    // Free device structure
    LocalFree(pDevice);

    // Done
    rc = TRUE;
    
cleanUp:
    DEBUGMSG(ZONE_FUNCTION, (L"-BKL_Deinit(rc = %d)\r\n", rc));
    return rc;
}

//------------------------------------------------------------------------------
//
//  Function:  BKL_Open
//
//  Called by device manager to open a device for reading and/or writing.
//
DWORD BKL_Open(DWORD context, DWORD accessCode, DWORD shareMode)
{
    DWORD rc = (DWORD)NULL;
    BKL_DEVICE *pDevice = (BKL_DEVICE*)context;
    BKL_INSTANCE *pInstance = NULL;

    DEBUGMSG(ZONE_FUNCTION, (
        L"+BKL_Open(0x%08x, 0x%08x, 0x%08x\r\n", context, accessCode, shareMode
    ));

    // Check if we get correct context
    if (pDevice == NULL || pDevice->cookie != BKL_DEVICE_COOKIE) {
        DEBUGMSG (ZONE_ERROR, (L"ERROR: BKL_Open: "
            L"Incorrect context parameter\r\n"
        ));
        goto cleanUp;
    }

    // Create device structure
    pInstance = (BKL_INSTANCE*)LocalAlloc(LPTR, sizeof(BKL_INSTANCE));
    if (pInstance == NULL) {
        DEBUGMSG(ZONE_ERROR, (L"ERROR: BKL_Open: "
            L"Failed allocate BKL instance structure\r\n"
        ));
        goto cleanUp;
    }

    // Set cookie
    pInstance->cookie = BKL_INSTANCE_COOKIE;

    // Save device reference
    pInstance->pDevice = pDevice;

    // Increment number of open instances
    InterlockedIncrement(&pDevice->instances);

    // sanity check number of instances
    ASSERT(pDevice->instances > 0);

    // Done...
    rc = (DWORD)pInstance;
  
cleanUp:
    DEBUGMSG(ZONE_FUNCTION, (L"-BKL_Open(rc = 0x%08x)\r\n", rc));
    return rc;
}

//------------------------------------------------------------------------------
//
//  Function:  BKL_Close
//
//  This function closes the device context.

⌨️ 快捷键说明

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