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

📄 cameradriver.cpp

📁 freescale i.mx31 BSP CE5.0全部源码
💻 CPP
字号:
//-----------------------------------------------------------------------------
//
// Copyright (C) 2003-2004, MOTOROLA, INC. All Rights Reserved
// THIS SOURCE CODE IS CONFIDENTIAL AND PROPRIETARY AND MAY NOT
// BE USED OR DISTRIBUTED WITHOUT THE WRITTEN PERMISSION OF
// MOTOROLA, INC.
//
//------------------------------------------------------------------------------
//
//  Copyright (C) 2004, Freescale Semiconductor, Inc. All Rights Reserved
//  THIS SOURCE CODE IS CONFIDENTIAL AND PROPRIETARY AND MAY NOT
//  BE USED OR DISTRIBUTED WITHOUT THE WRITTEN PERMISSION OF
//  Freescale Semiconductor, Inc.
//
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//
//  File:  CameraDriver.cpp
//
//  Implementation of the camera driver interface 
//
//------------------------------------------------------------------------------
#include <windows.h>
#include <ceddk.h>
#include <devload.h>
#include <NKIntr.h>

#include "mxarm11.h"
#define CAMINTERFACE
#include "cameradbg.h"
#include "CameraDriver.h"


//------------------------------------------------------------------------------
// External Functions


//------------------------------------------------------------------------------
// External Variables


//------------------------------------------------------------------------------
// Defines


//------------------------------------------------------------------------------
// Types


//------------------------------------------------------------------------------
// Global Variables


//------------------------------------------------------------------------------
// Local Variables
static BOOL bCameraLocked = FALSE;
static CRITICAL_SECTION csLockAccess;


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


//TODO : More investigation needed for AdvertiseInterface

extern "C" DWORD CAM_Init(VOID * pContext)
{    
    DEBUGMSG(ZONE_FUNCTION, (_T("CAM_Init: context %s\r\n"), pContext));

    CAMERADEVICE * pCamDev = new CAMERADEVICE;

    if (NULL != pCamDev)
    {
        if (false == pCamDev->Initialize(pContext))
        {
            SAFEDELETE(pCamDev);
            //DEBUGMSG(ZONE_INIT | ZONE_ERROR, (_T("CAM_Init: Initialization Failed")));
            DEBUGMSG(ZONE_ERROR, (_T("CAM_Init: Initialization Failed")));
        }
    }
    else
    {
        SetLastError(ERROR_OUTOFMEMORY);
    }

    // Initialize critical section for camera lock access
    InitializeCriticalSection(&csLockAccess);

    DEBUGMSG(ZONE_DEVICE, ( _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 );

    // Delete critical section for camera lock access
    DeleteCriticalSection(&csLockAccess);

    return TRUE;
}


extern "C" BOOL CAM_IOControl(
    DWORD   dwContext,
    DWORD   Ioctl,
    UCHAR * pInBuf,
    DWORD   InBufLen, 
    UCHAR * pOutBuf,
    DWORD   OutBufLen,
    DWORD * pdwBytesTransferred
    )
{
    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, pInBuf, InBufLen, pOutBuf, OutBufLen));

    DWORD dwErr = ERROR_INVALID_PARAMETER;
    BOOL  bRc   = FALSE;

    if ((NULL == pInBuf) || (InBufLen < sizeof(CSPROPERTY)) || 
        (NULL == pdwBytesTransferred))
    {
        DEBUGMSG(ZONE_IOCTL | ZONE_ERROR, 
            (_T("CAM_IOControl(%08x): CAM_IOControl. faild..\r\n"), dwContext));
        SetLastError( dwErr );
        return bRc;
    }

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

    CAMERAOPENHANDLE * pCamOpenHandle = reinterpret_cast<CAMERAOPENHANDLE *>( dwContext );
    CAMERADEVICE     * pCamDevice     = pCamOpenHandle->pCamDevice;
    CSPROPERTY       * pCsProp        = reinterpret_cast<CSPROPERTY *>( MapCallerPtr( pInBuf, sizeof ( CSPROPERTY ) ) );
    
    if (NULL == pCsProp)
    {
        DEBUGMSG(ZONE_IOCTL|ZONE_ERROR, (_T("CAM_IOControl(%08x): MapCallerPtr failed for input buffer.\r\n"), dwContext ) );
        return dwErr;
    }
    
    switch (Ioctl)
    {
        case IOCTL_CS_PROPERTY:
        {
            DEBUGMSG(ZONE_IOCTL, (_T("CAM_IOControl(%08x): IOCTL_CS_PROPERTY\r\n"), dwContext));
            
            __try 
            {
                if (TRUE == IsEqualGUID(pCsProp->Set, CSPROPSETID_Pin))
                    dwErr = pCamDevice->AdapterHandlePinRequests(pInBuf, InBufLen, pOutBuf, OutBufLen, pdwBytesTransferred);
                else if (TRUE == IsEqualGUID(pCsProp->Set, PROPSETID_VIDCAP_VIDEOPROCAMP))
                    dwErr = pCamDevice->AdapterHandleVidProcAmpRequests(pInBuf,InBufLen, pOutBuf, OutBufLen, pdwBytesTransferred);
                else if (TRUE == IsEqualGUID(pCsProp->Set, PROPSETID_VIDCAP_CAMERACONTROL))
                    dwErr = pCamDevice->AdapterHandleCamControlRequests(pInBuf,InBufLen, pOutBuf, OutBufLen, pdwBytesTransferred);
                else if (TRUE == IsEqualGUID(pCsProp->Set, PROPSETID_VIDCAP_VIDEOCOMPRESSION))
                    dwErr = pCamDevice->AdapterHandleCompressionRequests(pInBuf,InBufLen, pOutBuf, OutBufLen, pdwBytesTransferred);
                else if (TRUE == IsEqualGUID(pCsProp->Set, PROPSETID_VIDCAP_VIDEOCONTROL))
                    dwErr = pCamDevice->AdapterHandleVideoControlRequests(pInBuf,InBufLen, pOutBuf, OutBufLen, pdwBytesTransferred);
                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);

    // TODO : HANDLE ShareMode and Access parameters

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

    // Critical section around lock access to ensure that
    // two requesters do not access camera simultaneously.
    EnterCriticalSection(&csLockAccess);

    // Only one device at a time may have access to the camera driver
    // If the driver is locked, an invalid handle is returned by CreateFile.
    if (bCameraLocked)
    {
        DEBUGMSG(ZONE_ERROR, (TEXT("%s : Camera is already being used by another device.\r\n"), __WFUNCTION__));
        return 0;
    }
    else
    {
        bCameraLocked = TRUE;
    }

    LeaveCriticalSection(&csLockAccess);

    hCurrentProc = GetCallerProcess();

    ASSERT(pCamDevice != NULL);

    if (pCamDevice->BindApplicationProc(hCurrentProc))
    {
        pCamOpenHandle = new CAMERAOPENHANDLE;

        if (NULL == pCamOpenHandle)
        {
            DEBUGMSG(ZONE_FUNCTION, (_T("CAM_Open(%x): Not enought memory to create open handle\r\n"), Context));
        }
        else
        {
            pCamOpenHandle->pCamDevice = pCamDevice;
        }
    }
    else
    {
        SetLastError(ERROR_ALREADY_INITIALIZED);
    }

    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();
    SAFEDELETE(pCamOpenHandle);

    // Now that the handle has been closed, another device
    // can gain access to the camera driver
    bCameraLocked = FALSE;

    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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -