KernelIoControl和OEMIoControl的…


 

    对于KernelIoControl这个函数我们并不陌生,在2440 5.0BSP当中,这个函数在很多驱动中出现了,主要是用来申请中断,比如下面

BOOL RetVal = KernelIoControl( IOCTL_HAL_REQUEST_SYSINTR,
                                   &Irq,
                                   sizeof( Irq ),
                                   pSysIntr,
                                   sizeof( *pSysIntr ),
                                   NULL );

-------------------其实他的作用远远不止申请/释放中断那么简单,下面来看看PB的帮助文档。

This function provides the kernel with a generic I/O control for carrying out I/O operations.

BOOL KernelIoControl( 
  DWORD dwIoControlCode, 
  LPVOID lpInBuf, 
  DWORD nInBufSize, 
  LPVOID lpOutBuf, 
  DWORD nOutBufSize, 
  LPDWORD lpBytesReturned 
);对于这个函数的参数,非常类似EVC中的DeviceIoControl,从说明可以了解参数的使用方法。
Parameters
dwIoControlCode 
[in] I/O control code, which should support the OAL I/O controls. For a list of these I/O controls, see OAL IOCTLs. 
lpInBuf 
[out] Pointer to a buffer that contains the data required to perform the operation. 
Set to NULL if the dwIoControlCode parameter specifies an operation that does not require input data.

nInBufSize 
[in] Size, in bytes, of the buffer pointed to by lpInBuf. 
lpOutBuf 
[out] Pointer to a buffer that receives the output data for the operation. 
Set to NULL if the dwIoControlCode parameter specifies an operation that does not produce output data.

nOutBufSize 
[in] Size, in bytes, of the buffer pointed to by lpOutBuf. 
lpBytesReturned 
[in] Long pointer to a variable that receives the size, in bytes, of the data stored in the buffer pointed to by lpOutBuf. Even when an operation produces no output data, and lpOutBuf is NULL, the KernelIoControl function uses the variable pointed to by lpBytesReturned. After such an operation, the value of the variable has no meaning. 
Return Values
TRUE indicates success; FALSE indicates failure.

Remarks
The kernel calls the OEMIoControl function when a device driver or application calls the kernel function KernelIoControl and passes an I/O control code.

(当应用程序或者驱动调用KernelIoControl 的时候,KernelIoControl 就会调用OEMIoControl 去实现。)

This function is also called when the SystemParametersInfo function is called with SPI_GETOEMINFO or SPI_GETPLATFORMINFO.

The system is fully preemptible when this function is called. The kernel does no processing, but it passes all parameters directly to the function supplied by you. (当这个函数被调用的时候系统完全可能被抢占,内核没有处理,直接传递参数到你提供的函数。这个我觉得说的很别扭,估计是直接传递参数到OEMIoControl )

This function is provided solely to allow your device driver or application to communicate with an OAL and its specific functionality.

(该函数用来提供驱动/应用程序和OAL的通信)

Requirements
OS Versions: Windows CE 2.10 and later.
Header: Pkfuncs.h.(原来是个不开源的函数)
Link Library: Coredll.lib.

 

========================现在来看看OEMIoControl 这个函数============================

C:\WINCE500\PLATFORM\COMMON\SRC\COMMON\IOCTL\ioctl.c(45)://  Function:  OEMIoControl

//  File:  ioctl.c 
// 
//  File implements OEMIoControl function. 
// 
#include  
#include    //这个很关键,不然oal_ioctl_tab.h设置就没有办法传递进来。

//------------------------------------------------------------------------------ 
// 
//  Global:  g_ioctlState; 
// 
//  This state variable contains critical section used to serialize IOCTL 
//  calls. 
// 
static struct { 
    BOOL postInit; 
    CRITICAL_SECTION cs; 
} g_ioctlState = { FALSE };

//------------------------------------------------------------------------------ 
// 
//  Include: intioctl.c 
// 
//  This include file is used to add internal testing IOCTL hadlers. 
// 
#ifdef OAL_HAL_INTERNAL_TESTING 
#include "intioctl.c" 
#endif


//------------------------------------------------------------------------------ 
// 
//  Function:  OEMIoControl 
// 
//  The function is called by kernel a device driver or application calls  
//  KernelIoControl. The system is fully preemtible when this function is  
//  called. The kernel does no processing of this API. It is provided to  
//  allow an OEM device driver to communicate with kernel mode code. 
// 
BOOL OEMIoControl( 
    DWORD code, VOID *pInBuffer, DWORD inSize, VOID *pOutBuffer, DWORD outSize, 
    DWORD *pOutSize 
) { 
    BOOL rc = FALSE; 
    UINT32 i;

    OALMSG(OAL_IOCTL&&OAL_FUNC, ( 
        L"+OEMIoControl(0x%x, 0x%x, %d, 0x%x, %d, 0x%x)\r\n",  
        code, pInBuffer, inSize, pOutBuffer, outSize, pOutSize 
    ));

    // Search the IOCTL table for the requested code. 
    for (i = 0; g_oalIoCtlTable.pfnHandler != NULL; i++) { 
        if (g_oalIoCtlTable.code == code) break; 
    }

    // Indicate unsupported code 
    if (g_oalIoCtlTable.pfnHandler == NULL) { 
#ifdef OAL_HAL_INTERNAL_TESTING 
        rc = InternalHalTesting( 
            code, pInBuffer, inSize, pOutBuffer, outSize, pOutSize 
        ); 
#else 
        NKSetLastError(ERROR_NOT_SUPPORTED); 
        OALMSG(OAL_WARN, ( 
            L"OEMIoControl: Unsupported Code 0x%x - device 0xx func %d\r\n",  
            code, code >> 16, (code >> 2) 
        )); 
#endif 
        goto cleanUp; 
          

    // Take critical section if required (after postinit & no flag) 
    if ( 
        g_ioctlState.postInit &&  
        (g_oalIoCtlTable.flags & OAL_IOCTL_FLAG_NOCS) == 0 
    ) { 
        // Take critical section             
        EnterCriticalSection(&g_ioctlState.cs); 
    }

    // Execute the handler 调用函数指针,用来实现相应功能 
    rc = g_oalIoCtlTable.pfnHandler( 
        code, pInBuffer, inSize, pOutBuffer, outSize, pOutSize 
    );

    // Release critical section if it was taken above 
    if ( 
        g_ioctlState.postInit &&  
        (g_oalIoCtlTable.flags & OAL_IOCTL_FLAG_NOCS) == 0 
    ) { 
        // Take critical section             
        LeaveCriticalSection(&g_ioctlState.cs); 
    } else if (!g_ioctlState.postInit && code == IOCTL_HAL_POSTINIT) { 
        // Initialize critical section 
        InitializeCriticalSection(&g_ioctlState.cs); 
        g_ioctlState.postInit = TRUE; 
                  

cleanUp: 
    OALMSG(OAL_IOCTL&&OAL_FUNC, (L"-OEMIoControl(rc = %d)\r\n", rc )); 
    return rc; 
}

//------------------------------------------------------------------------------

——再来看oal_ioctl.h

//------------------------------------------------------------------------------ 
// 
//  File:  oal_ioctl.h 
// 
//  This header file defines IO Control OAL module. This module implements 
//  OEMIoControl function which is used to call kernel functions from user 
//  space. 
// 
#ifndef __OAL_IOCTL_H 
#define __OAL_IOCTL_H

#if __cplusplus 
extern "C" { 
#endif

//------------------------------------------------------------------------------ 
// 
//  Definition: OAL_IOCTL_FLAG_xxx 
// 
//  This definition specifies flag codes for IOCTL table. When NOCS flag is 
//  set handler function will be called in deserialized mode (so no critical 
//  section will be taken/release before/after handler is called). 
// 
#define OAL_IOCTL_FLAG_NOCS     (1 << 0)

//------------------------------------------------------------------------------ 
// 
//  Type: IOCTL_HANDLER     
// 
//  This type defines the procedure to be called for an IOCTL code. The 
//  global g_oalIoctlTable is an array of these types. 
// 
typedef struct { 
    UINT32  code; 
    UINT32  flags; 
    BOOL    (*pfnHandler)(UINT32, VOID*, UINT32, VOID*, UINT32, UINT32*); 
} OAL_IOCTL_HANDLER, *POAL_IOCTL_HANDLER;

//------------------------------------------------------------------------------ 
// 
//  Extern: g_oalIoCtlPlatformType/OEM 
// 
//  Platform Type/OEM 
// 
extern LPCWSTR g_oalIoCtlPlatformType; 
extern LPCWSTR g_oalIoCtlPlatformOEM;

//------------------------------------------------------------------------------ 
// 
//  Global: g_oalIoCtlProcessorVendor/Name/Core 
// 
//  Processor information 
// 
extern LPCWSTR g_oalIoCtlProcessorVendor; 
extern LPCWSTR g_oalIoCtlProcessorName; 
extern LPCWSTR g_oalIoCtlProcessorCore;

//------------------------------------------------------------------------------ 
// 
//  Global:  g_oalIoCtlInstructionSet/g_oalIoCtlClockSpeed 
// 
//  Processor instruction set identifier and clock speed 
//

extern UINT32 g_oalIoCtlInstructionSet; 
extern UINT32 g_oalIoCtlClockSpeed;

//------------------------------------------------------------------------------ 
// 
//  Globaal:  g_oalIoctlTable 
// 
//  This extern references the global IOCTL table that is defined in 
//  the platform code. 
// 
extern const OAL_IOCTL_HANDLER g_oalIoCtlTable[];

//------------------------------------------------------------------------------ 
// 
//  Function: OALIoCtlXxx 
// 
//  This functions implement basic IOCTL code handlers. 
//  这些函数在哪里实现的呢?功能好强大 
BOOL OALIoCtlHalGetDeviceId(UINT32, VOID*, UINT32, VOID*, UINT32, UINT32*); 
BOOL OALIoCtlHalGetDeviceInfo(UINT32, VOID*, UINT32, VOID*, UINT32, UINT32*); 
BOOL OALIoCtlProcessorInfo(UINT32, VOID*, UINT32, VOID*, UINT32, UINT32*); 
BOOL OALIoCtlHalInitRegistry(UINT32, VOID*, UINT32, VOID*, UINT32, UINT32*); 
BOOL OALIoCtlHalReboot(UINT32, VOID*, UINT32, VOID*, UINT32, UINT32*); 
BOOL OALIoCtlHalGetUUID (UINT32, VOID *, UINT32, VOID *, UINT32, UINT32 *);

//------------------------------------------------------------------------------ 
// 
//  Function: OALIoCtlHalDDIXxx 
// 
//  This functions implement IOCTL code handler used by HAL flat display 
//  driver. 
// 
BOOL OALIoCtlHalDDI(UINT32, VOID*, UINT32, VOID*, UINT32, UINT32*);

//------------------------------------------------------------------------------


#if __cplusplus 

#endif

#endif // __OAL_IOCTL_H
    再看oal_ioctl_tab.h

//------------------------------------------------------------------------------ 
//  这个头文件很关键,只要在这里填入相应的IOCTL_XXXXX以及相应的函数 
// (在别的地方实现这个函数)就大功告成了。 
//  File:  oal_ioctl_tab.h 
// 
//  This file contains part of global IOCTL handler table for codes which 
//  must (or should) be implemented on all platforms. Table in platform 
//  will usually include this file. 
// 
//  This file is included by the platform's IOCTL table, g_oalIoCtlTable[]. 
//  Therefore, this file may ONLY define OAL_IOCTL_HANDLER entries.  
// 
// IOCTL CODE,                          Flags   Handler Function 
//------------------------------------------------------------------------------

{ IOCTL_HAL_TRANSLATE_IRQ,                  0,  OALIoCtlHalRequestSysInt  }, 
{ IOCTL_HAL_REQUEST_SYSINTR,                0,  OALIoCtlHalRequestSysInt  }, 
{ IOCTL_HAL_RELEASE_SYSINTR,                0,  OALIoCtlHalReleaseSysInt  }, 
{ IOCTL_HAL_REQUEST_IRQ,                    0,  OALIoCtlHalRequestIrq       },

{ IOCTL_HAL_INITREGISTRY,                   0,  OALIoCtlHalInitRegistry     }, 
{ IOCTL_HAL_INIT_RTC,                       0,  OALIoCtlHalInitRTC          }, 
{ IOCTL_HAL_REBOOT,                         0,  OALIoCtlHalReboot           },

{ IOCTL_HAL_DDK_CALL,                       0,  OALIoCtlHalDdkCall          },

{ IOCTL_HAL_DISABLE_WAKE,                   0,  OALIoCtlHalDisableWake      }, 
{ IOCTL_HAL_ENABLE_WAKE,                    0,  OALIoCtlHalEnableWake       }, 
{ IOCTL_HAL_GET_WAKE_SOURCE,                0,  OALIoCtlHalGetWakeSource    },

{ IOCTL_HAL_GET_CACHE_INFO,                 0,  OALIoCtlHalGetCacheInfo     }, 
{ IOCTL_HAL_GET_DEVICEID,                   0,  OALIoCtlHalGetDeviceId      }, 
{ IOCTL_HAL_GET_DEVICE_INFO,                0,  OALIoCtlHalGetDeviceInfo    }, 
{ IOCTL_HAL_GET_UUID,                       0,  OALIoCtlHalGetUUID          }, 
{ IOCTL_PROCESSOR_INFORMATION,              0,  OALIoCtlProcessorInfo       },

{ IOCTL_VBRIDGE_802_3_MULTICAST_LIST,       0,  OALIoCtlVBridge             }, 
{ IOCTL_VBRIDGE_ADD_MAC,                    0,  OALIoCtlVBridge             }, 
{ IOCTL_VBRIDGE_CURRENT_PACKET_FILTER,      0,  OALIoCtlVBridge             }, 
{ IOCTL_VBRIDGE_GET_ETHERNET_MAC,           0,  OALIoCtlVBridge             }, 
{ IOCTL_VBRIDGE_GET_RX_PACKET,              0,  OALIoCtlVBridge             }, 
{ IOCTL_VBRIDGE_GET_RX_PACKET_COMPLETE,     0,  OALIoCtlVBridge             }, 
{ IOCTL_VBRIDGE_GET_TX_PACKET,              0,  OALIoCtlVBridge             }, 
{ IOCTL_VBRIDGE_GET_TX_PACKET_COMPLETE,     0,  OALIoCtlVBridge             }, 
{ IOCTL_VBRIDGE_SHARED_ETHERNET,            0,  OALIoCtlVBridge             }, 
{ IOCTL_VBRIDGE_WILD_CARD,                  0,  OALIoCtlVBridge             }, 
{ IOCTL_VBRIDGE_WILD_CARD_RESET_BUFFER,     0,  OALIoCtlVBridge             }, 
{ IOCTL_VBRIDGE_WILD_CARD_VB_INITIALIZED,   0,  OALIoCtlVBridge             },

在初始化阶段内核就会调用KernelIoControl来和OAL通信,其实就是通过调用KernelIoControl来执行OEMIoControl

很神奇吧。申请中断什么的,都在这里弄好了。牛B!

==============================例子:重启

如何在程序中关闭、重起和硬起动Pocket PC?

--------------------------------------------------------------------------------

时间:2003-2-28 18:18:44 来源:BIPLIP.com 作者:Daric 阅读210次


关闭(suspend)
方法1:
//虚拟关机键
::keybd_event(VK_OFF, 0, 0, 0); 
::keybd_event(VK_OFF, 0, KEYEVENTF_KEYUP, 0);
方法2:
//调用未公开函数PowerOffSystem()
extern "C" __declspec(dllimport) void PowerOffSystem();
重起(soft reset)
//Soft reset the device
#include 
#define IOCTL_HAL_REBOOT CTL_CODE(FILE_DEVICE_HAL, 15, METHOD_BUFFERED, FILE_ANY_ACCESS)
extern "C" __declspec(dllimport) BOOL KernelIoControl(
DWORD dwIoControlCode, 
LPVOID lpInBuf, 
DWORD nInBufSize, 
LPVOID lpOutBuf, 
DWORD nOutBufSize, 
LPDWORD lpBytesReturned);
BOOL ResetPocketPC()
{
return KernelIoControl(IOCTL_HAL_REBOOT, NULL, 0, NULL, 0, NULL);
}
硬起动(hard reset)
//注意!!!使用此段代码会将您的Pocket PC的用户数据全部清空,
//请勿非法使用,用者后果自负.
#include 
#define IOCTL_HAL_REBOOT CTL_CODE(FILE_DEVICE_HAL, 15, METHOD_BUFFERED, FILE_ANY_ACCESS)
extern "C" __declspec(dllimport)void SetCleanRebootFlag(void);
extern "C" __declspec(dllimport) BOOL KernelIoControl(
DWORD dwIoControlCode, 
LPVOID lpInBuf, 
DWORD nInBufSize, 
LPVOID lpOutBuf, 
DWORD nOutBufSize, 
LPDWORD lpBytesReturned);
BOOL HardResetPocketPC()
{
SetCleanRebootFlag();
return KernelIoControl(IOCTL_HAL_REBOOT, NULL, 0, NULL, 0, NULL);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值