cameradriver.cpp

来自「该BSP是基于PXA270+WINCE的BSP」· C++ 代码 · 共 365 行

CPP
365
字号
//
// 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 2000-2004 Intel Corporation. All Rights Reserved.
**
** Portions of the source code contained or described herein and all documents
** related to such source code (Material) are owned by Intel Corporation
** or its suppliers or licensors and is licensed by Microsoft Corporation for distribution.  
** Title to the Material remains with Intel Corporation or its suppliers and licensors. 
** Use of the Materials is subject to the terms of the Microsoft license agreement which accompanied the Materials.  
** No other license under any patent, copyright, trade secret or other intellectual
** property right is granted to or conferred upon you by disclosure or
** delivery of the Materials, either expressly, by implication, inducement,
** estoppel or otherwise 
** Some portion of the Materials may be copyrighted by Microsoft Corporation.
** 
********************************************************************************/

/*++


Module Name:

    CameraDriver.cpp

Abstract:

    Intel PXA27x Mainstone II Direct Show camera driver

Notes:
    

Revision History:

--*/

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

#define CAMINTERFACE
#include "CameraDriver.h"

EXTERN_C
DWORD
CAM_Init(
    VOID * pContext
    )
{    
    CAMERADEVICE * pCamDev = NULL;
    DEBUGMSG(ZONE_INIT, (_T("CAM_Init: context %s\n"), pContext));
    
    if( OEM_CERTIFY_TRUST != CeGetCallerTrust() )
    {
        SetLastError(ERROR_ACCESS_DENIED);
    }
    else
    {
        pCamDev = new CAMERADEVICE;
        
        if ( pCamDev )
        {
            // NOTE: real drivers would need to validate pContext before dereferencing the pointer
            if ( false == pCamDev->Initialize( pContext ) )
            {
                SetLastError( ERROR_INVALID_HANDLE );
                SAFEDELETE( pCamDev );
                DEBUGMSG( ZONE_INIT|ZONE_ERROR, ( _T("CAM_Init: Initialization Failed") ) );
            }
        }
        else
        {
            SetLastError( ERROR_OUTOFMEMORY );
        }
    }    
    DEBUGMSG( ZONE_INIT, ( _T("CAM_Init: returning 0x%08x\r\n"), reinterpret_cast<DWORD>( pCamDev ) ) );

    return reinterpret_cast<DWORD>( pCamDev );
}


EXTERN_C
BOOL
CAM_Deinit(
    DWORD dwContext
    )
{
    DEBUGMSG( ZONE_INIT, ( _T("CAM_Deinit\r\n") ) );

    CAMERADEVICE * pCamDevice = reinterpret_cast<CAMERADEVICE *>( dwContext );

    SAFEDELETE( pCamDevice );

    return TRUE;
}


EXTERN_C
BOOL
CAM_IOControl(
    DWORD   dwContext,
    DWORD   Ioctl,
    UCHAR * pInBufUnmapped,
    DWORD   InBufLen, 
    UCHAR * pOutBufUnmapped,
    DWORD   OutBufLen,
    DWORD * pdwBytesTransferred
   )
{
    DWORD dwErr = ERROR_INVALID_PARAMETER;
    CAMERAOPENHANDLE * pCamOpenHandle = reinterpret_cast<CAMERAOPENHANDLE *>( dwContext );
    CAMERADEVICE     * pCamDevice     = pCamOpenHandle->pCamDevice;

    DEBUGMSG( ZONE_FUNCTION, ( _T("CAM_IOControl(%08x): IOCTL:0x%x, InBuf:0x%x, InBufLen:%d, OutBuf:0x%x, OutBufLen:0x%x)\r\n"), dwContext, Ioctl, pInBufUnmapped, InBufLen, pOutBufUnmapped, OutBufLen ) );

    
    switch ( Ioctl )
    {
        //
        // Power Management Support.
        //
        case IOCTL_POWER_CAPABILITIES:
        case IOCTL_POWER_QUERY:
        case IOCTL_POWER_SET:
        case IOCTL_POWER_GET:
        {
            DEBUGMSG( ZONE_IOCTL, ( _T("CAM_IOControl(%08x): Power Management IOCTL\r\n"), dwContext ) );
            dwErr = pCamDevice->AdapterHandlePowerRequests(Ioctl, pInBufUnmapped, InBufLen, pOutBufUnmapped, OutBufLen, pdwBytesTransferred );
            break;
        }

        //
        // Camera Support.
        //
        case IOCTL_CS_PROPERTY:
        {
            DEBUGMSG( ZONE_IOCTL, ( _T("CAM_IOControl(%08x): IOCTL_CS_PROPERTY\r\n"), dwContext ) );

            UCHAR * pInBuf = NULL;
            UCHAR * pOutBuf = NULL;
        
            if ( ( NULL == pInBufUnmapped )
             || ( InBufLen < sizeof ( CSPROPERTY ) )
             || ( NULL == pdwBytesTransferred ) )
            {
                break;
            }
        
            pInBuf = (UCHAR*) MapCallerPtr( pInBufUnmapped, InBufLen );
            if(( pInBuf == NULL ) && ( pInBufUnmapped != NULL ))
            {
            DEBUGMSG( ZONE_IOCTL|ZONE_ERROR, ( _T("CAM_IOControl(%08x): CAM_IOControl. MapCallerPtr failed for incoming buffer.\r\n"), dwContext ) );
        
                break;
            }
        
            pOutBuf = (UCHAR*) MapCallerPtr( pOutBufUnmapped, OutBufLen );
            if (( NULL == pOutBuf ) && ( pOutBufUnmapped != NULL ))
            {
            DEBUGMSG( ZONE_IOCTL|ZONE_ERROR, ( _T("CAM_IOControl(%08x): CAM_IOControl. MapCallerPtr failed for incoming buffer.\r\n"), dwContext ) );
        
                break;
            }

            if ( NULL == MapCallerPtr( pdwBytesTransferred, sizeof ( DWORD ) ) )
            {
            DEBUGMSG( ZONE_IOCTL|ZONE_ERROR, ( _T("CAM_IOControl(%08x): CAM_IOControl. MapCallerPtr failed for dwBytesTransferred.\r\n"), dwContext ) );
        
                break;
            }

            CSPROPERTY       * pCsProp        = reinterpret_cast<CSPROPERTY *>(pInBuf);

            __try 
            {
                if ( TRUE == IsEqualGUID( pCsProp->Set, CSPROPSETID_Pin ) )
                {   
                    dwErr = pCamDevice->AdapterHandlePinRequests( pInBuf, InBufLen, pOutBuf, OutBufLen, pdwBytesTransferred );
                    if((dwErr != ERROR_SUCCESS) && (dwErr != ERROR_MORE_DATA))
                    {
                        RETAILMSG(1, (TEXT("IOCTL AdapterHandlePinRequests returns error: 0x%x\r\n"), dwErr));
                    }
                }
                else if ( TRUE == IsEqualGUID( pCsProp->Set, CSPROPSETID_VERSION ) )
                {
                    dwErr = pCamDevice->AdapterHandleVersion( pOutBuf, OutBufLen, pdwBytesTransferred );
                    if(dwErr != ERROR_SUCCESS)
                    {
                        RETAILMSG(1, (TEXT("IOCTL AdapterHandleVersion returns error: 0x%x\r\n"), dwErr));
                    }
                }
                else if ( TRUE == IsEqualGUID( pCsProp->Set, PROPSETID_VIDCAP_VIDEOPROCAMP ) )
                {   
                    dwErr = pCamDevice->AdapterHandleVidProcAmpRequests( pInBuf,InBufLen, pOutBuf, OutBufLen, pdwBytesTransferred );
                    if(dwErr != ERROR_SUCCESS)
                    {
                        RETAILMSG(1, (TEXT("IOCTL AdapterHandleVidProcAmpRequests returns error: 0x%x\r\n"), dwErr));
                    }
                }
                else if ( TRUE == IsEqualGUID( pCsProp->Set, PROPSETID_VIDCAP_CAMERACONTROL ) )
                {   
                    dwErr = pCamDevice->AdapterHandleCamControlRequests( pInBuf,InBufLen, pOutBuf, OutBufLen, pdwBytesTransferred );
                    if(dwErr != ERROR_SUCCESS)
                    {
                        RETAILMSG(1, (TEXT("IOCTL AdapterHandleCamControlRequests returns error: 0x%x\r\n"), dwErr));
                    }
                }
                else if ( TRUE == IsEqualGUID( pCsProp->Set, PROPSETID_VIDCAP_VIDEOCOMPRESSION ) )
                {   
                    dwErr = pCamDevice->AdapterHandleCompressionRequests( pInBuf,InBufLen, pOutBuf, OutBufLen, pdwBytesTransferred );
                    if(dwErr != ERROR_SUCCESS)
                    {
                        RETAILMSG(1, (TEXT("IOCTL AdapterHandleCompressionRequests returns error: 0x%x\r\n"), dwErr));
                    }
                }
                else if ( TRUE == IsEqualGUID( pCsProp->Set, PROPSETID_VIDCAP_VIDEOCONTROL ) )
                {   
                    dwErr = pCamDevice->AdapterHandleVideoControlRequests( pInBuf,InBufLen, pOutBuf, OutBufLen, pdwBytesTransferred );
                    if(dwErr != ERROR_SUCCESS)
                    {
                        RETAILMSG(1, (TEXT("IOCTL AdapterHandleVideoControlRequests returns error: 0x%x\r\n"), dwErr));
                    }
                }
                else if ( TRUE == IsEqualGUID( pCsProp->Set, PROPSETID_VIDCAP_DROPPEDFRAMES) )
                {   
                    dwErr = pCamDevice->AdapterHandleDroppedFramesRequests( pInBuf,InBufLen, pOutBuf, OutBufLen, pdwBytesTransferred );
                    if(dwErr != ERROR_SUCCESS)
                    {
                        RETAILMSG(1, (TEXT("IOCTL AdapterHandleDroppedFramesRequests returns error: 0x%x\r\n"), dwErr));
                    }
                }
                else
                {
                    DEBUGMSG( ZONE_IOCTL, ( _T("CAM_IOControl(%08x): Unsupported PropertySet Request%u\r\n"), dwContext, pCsProp->Set ) );
                    dwErr = ERROR_NOT_SUPPORTED;
                }
            }
            __except ( EXCEPTION_EXECUTE_HANDLER )
            {
                DEBUGMSG( ZONE_IOCTL, ( _T("CAM_IOControl(%08x):Exception in IOCTL_CS_PROPERTY"), dwContext ) );
            }

            break;
        }

        default:
        {
            DEBUGMSG( ZONE_IOCTL, (_T("CAM_IOControl(%08x): Unsupported IOCTL code %u\r\n"), dwContext, Ioctl ) );
            dwErr = ERROR_NOT_SUPPORTED;

            break;
        }
    }
    
    // pass back appropriate response codes
    SetLastError( dwErr );

    return ( ( dwErr == ERROR_SUCCESS ) ? TRUE : FALSE );
}


EXTERN_C
DWORD
CAM_Open(
    DWORD Context, 
    DWORD Access,
    DWORD ShareMode
    )
{
    DEBUGMSG( ZONE_FUNCTION, ( _T("CAM_Open(%x, 0x%x, 0x%x)\r\n"), Context, Access, ShareMode ) );

    UNREFERENCED_PARAMETER( ShareMode );
    UNREFERENCED_PARAMETER( Access );

    CAMERADEVICE     * pCamDevice     = reinterpret_cast<CAMERADEVICE *>( Context );
    CAMERAOPENHANDLE * pCamOpenHandle = NULL;

    ASSERT( pCamDevice != NULL );

    pCamOpenHandle = pCamDevice->BindApplicationProc();

    return reinterpret_cast<DWORD>( pCamOpenHandle );
}


EXTERN_C
BOOL  
CAM_Close(
    DWORD Context
    ) 
{
    DEBUGMSG( ZONE_FUNCTION, ( _T("CAM_Close(%x)\r\n"), Context ) );
    
    PCAMERAOPENHANDLE pCamOpenHandle = reinterpret_cast<PCAMERAOPENHANDLE>( Context );

    pCamOpenHandle->pCamDevice->UnBindApplicationProc(pCamOpenHandle);

    if(!pCamOpenHandle->cRef)
    {
        SAFEDELETE( pCamOpenHandle ) ;
    }
    return TRUE;
}


BOOL
__stdcall
DllMain(
   HANDLE   hDllHandle, 
   DWORD    dwReason, 
   VOID   * lpReserved
   ) 
{
    BOOL bRc = TRUE;
    
    UNREFERENCED_PARAMETER( lpReserved );
    
    switch ( dwReason )
    {
        case DLL_PROCESS_ATTACH: 
        {
           DEBUGREGISTER( reinterpret_cast<HMODULE>( hDllHandle ) );
           DEBUGMSG( ZONE_INIT, ( _T("*** DLL_PROCESS_ATTACH - Current Process: 0x%x, ID: 0x%x ***\r\n"), GetCurrentProcess(), GetCurrentProcessId() ) );

           DisableThreadLibraryCalls( reinterpret_cast<HMODULE>( hDllHandle ) );

           break;
        } 

        case DLL_PROCESS_DETACH: 
        {
            DEBUGMSG( ZONE_INIT, ( _T("*** DLL_PROCESS_DETACH - Current Process: 0x%x, ID: 0x%x ***\r\n"), GetCurrentProcess(), GetCurrentProcessId() ) );

            break;
        } 
            
        case DLL_THREAD_DETACH:
        {
            break;
        }
            
        case DLL_THREAD_ATTACH:
        {
            break;
        }
            
        default:
        {
            break;
        }
    }
    
    return bRc;
}

⌨️ 快捷键说明

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