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

📄 cmmdriver.c

📁 6410BSP3
💻 C
📖 第 1 页 / 共 3 页
字号:
//
// Copyright (c) Samsung Electronics. Co. LTD.  All rights reserved.
//
/*++
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

*/


#include <bsp.h>
#include "CMMDriver.h"
#include "CMMMisc.h"

#if (_WIN32_WCE >= 600)
#define E2E_CMM
#endif
#include <pm.h>
#include <pmplatform.h>
#include <windows.h>
#include <nkintr.h>

BOOL            instanceNo[MAX_INSTANCE_NUM];
ALLOC_MEM_T    *AllocMemHead;
ALLOC_MEM_T    *AllocMemTail;
FREE_MEM_T        *FreeMemHead;
FREE_MEM_T        *FreeMemTail;
UINT8            *CachedVirAddr;

#if DEBUG
#define ZONE_INIT              DEBUGZONE(0)

DBGPARAM dpCurSettings =                \
{                                       \
    TEXT("CMM_Driver"),                 \
    {                                   \
        TEXT("Init"),       /* 0  */    \
    },                                  \
    (0x0001)                            \
};
#endif

/*----------------------------------------------------------------------------
*Function: CMM_Init

*Parameters:         dwContext        :
*Return Value:        True/False
*Implementation Notes: Initialize JPEG Hardware 
-----------------------------------------------------------------------------*/
DWORD
CMM_Init(
    DWORD dwContext
    )
{
    //CODEC_MEM_CTX *CodecMem;
    HANDLE      h_Mutex=NULL;
    DWORD       ret;
    FREE_MEM_T *node = NULL;
    PHYSICAL_ADDRESS    ioPhysicalBase = {0,0};

    printD("\n[CMM_Init]\n");

    
    // Mutex initialization
    h_Mutex = CreateCMMmutex();
    if (h_Mutex == NULL) 
    {
        RETAILMSG(1, (TEXT("[CMM_Init] CMM Mutex Initialize error : %d \r\n"),GetLastError()));    
        return FALSE;
    }

    ret = LockCMMMutex();
    if(!ret){
        RETAILMSG(1, (TEXT("[CMM_Init] CMM Mutex Lock Fail\r\n")));
        return FALSE;
    }

    ioPhysicalBase.LowPart = CODEC_MEM_START;
    CachedVirAddr = (UINT8 *)MmMapIoSpace(ioPhysicalBase, CODEC_MEM_SIZE, TRUE);

    // init alloc list, if(AllocMemHead == AllocMemTail) then, the list is NULL
    node = (ALLOC_MEM_T *)malloc(sizeof(ALLOC_MEM_T));
    if(node == NULL)
    {
        RETAILMSG(1, (TEXT("[CMM_Init] Cannot allocate node for Alloc List\r\n")));
        UnlockCMMMutex();
        return FALSE;
    }

    memset(node, 0x00, sizeof(ALLOC_MEM_T));
    node->next = node;
    node->prev = node;
    AllocMemHead = node;
    AllocMemTail = AllocMemHead;

    // init free list, if(FreeMemHead == FreeMemTail) then, the list is NULL
    node = (FREE_MEM_T *)malloc(sizeof(FREE_MEM_T));

    if(node == NULL)
    {
        RETAILMSG(1, (TEXT("[CMM_Init] Cannot allocate node for Free List\r\n")));
        UnlockCMMMutex();
        return FALSE;
    }

    memset(node, 0x00, sizeof(FREE_MEM_T));
    node->next = node;
    node->prev = node;
    FreeMemHead = node;
    FreeMemTail = FreeMemHead;
    
    node = (FREE_MEM_T *)malloc(sizeof(FREE_MEM_T));

    if(node == NULL)
    {
        RETAILMSG(1, (TEXT("[CMM_Init] Cannot allocate node for Free List first item\r\n")));
        UnlockCMMMutex();
        return FALSE;
    }

    memset(node, 0x00, sizeof(FREE_MEM_T));
    node->startAddr = CODEC_MEM_START;
    node->size = CODEC_MEM_SIZE;
    InsertNodeToFreeList(node, -1);

    UnlockCMMMutex();
    
    return TRUE;
}


/*----------------------------------------------------------------------------
*Function: CMM_DeInit

*Parameters:         InitHandle        :
*Return Value:        True/False
*Implementation Notes: Deinitialize JPEG Hardware 
-----------------------------------------------------------------------------*/
BOOL
CMM_Deinit(
    DWORD InitHandle
    )
{
    CODEC_MEM_CTX *CodecMem;

    printD("[CMM_DeInit] CMM_Deinit\n");
    CodecMem = (CODEC_MEM_CTX *)InitHandle;

    if(!CodecMem){
        RETAILMSG(1, (TEXT("[CMM_DeInit] CMM Invalid Input Handle\r\n")));
        return FALSE;
    }

    DeleteCMMMutex();    
    return TRUE;
}


/*----------------------------------------------------------------------------
*Function: CMM_Open

*Parameters:         InitHandle        :Handle to JPEG  context
                    dwAccess        :
                    dwShareMode        :File share mode of JPEG
*Return Value:        This function returns a handle that identifies the 
                    open context of JPEG  to the calling application.
*Implementation Notes: Opens JPEG CODEC device for reading, writing, or both 
-----------------------------------------------------------------------------*/
DWORD
CMM_Open(
    DWORD InitHandle,
    DWORD dwAccess,
    DWORD dwShareMode
    )
{
    CODEC_MEM_CTX *CodecMem;
    DWORD    ret;
    UINT8    inst_no;


    ret = LockCMMMutex();
    if(!ret){
        RETAILMSG(1, (TEXT("[CMM_Open] CMM Mutex Lock Fail\r\n")));
        return FALSE;
    }

    // check the number of instance 
    if((inst_no = GetInstanceNo()) < 0){
        RETAILMSG(1, (TEXT("[CMM_Open] Instance Number error-too many instance\r\n")));
        UnlockCMMMutex();
        return FALSE;
    }

    CodecMem = (CODEC_MEM_CTX *)malloc(sizeof(CODEC_MEM_CTX));
    if(CodecMem == NULL){
        RETAILMSG(1, (TEXT("[CMM_Init] CodecMem allocatopn failed\r\n")));
        UnlockCMMMutex();
        return FALSE;
    }
    
    memset(CodecMem, 0x00, sizeof(CODEC_MEM_CTX));

    CodecMem->inst_no = inst_no;
    printD("\n*****************************\n[CMM_Open] instanceNo : %d\n*****************************\n", CodecMem->inst_no);
    PrintList();
    UnlockCMMMutex();
    return (DWORD)CodecMem;
}


/*----------------------------------------------------------------------------
*Function: CMM_Close

*Parameters:         OpenHandle        :
*Return Value:        True/False
*Implementation Notes: This function closes the device context identified by
                        OpenHandle 
-----------------------------------------------------------------------------*/
BOOL
CMM_Close(
    DWORD OpenHandle
    )
{
    CODEC_MEM_CTX *CodecMem;
    DWORD    ret;
    ALLOC_MEM_T *node, *tmp_node;
    int        count=0;

    ret = LockCMMMutex();
    if(!ret){
        RETAILMSG(1, (TEXT("[CMM_Close] CMM Mutex Lock Fail\r\n")));
        return FALSE;
    }

    CodecMem = (CODEC_MEM_CTX *)OpenHandle;
    
    if(!CodecMem){
        RETAILMSG(1, (TEXT("[CMM_Close] CMM Invalid Input Handle\r\n")));
        UnlockCMMMutex();
        return FALSE;
    }

    printD("[CMM_Close] CodecMem->inst_no : %d CodecMem->callerProcess : 0x%x\n", CodecMem->inst_no, CodecMem->callerProcess);

    __try
    {
        // release u_addr and v_addr accoring to inst_no
        for(node = AllocMemHead; node != AllocMemTail; node = node->next){
            if(node->inst_no == CodecMem->inst_no){
                tmp_node = node;
                node = node->prev;
                ReleaseAllocMem(tmp_node, CodecMem);
            }
        }
    }
    __except(GetExceptionCode()==STATUS_ACCESS_VIOLATION ?
               EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
    {
       RETAILMSG( 1, ( _T("CMM_Close:Exception in releasing memory\n")) );
        return FALSE;
    }
    
    printD("[%d][CMM Close] MergeFragmentation\n", CodecMem->inst_no);
    MergeFragmentation(CodecMem->inst_no);

    ReturnInstanceNo(CodecMem->inst_no);

    free(CodecMem);
    UnlockCMMMutex();


    return TRUE;
}


/*----------------------------------------------------------------------------
*Function: CMM_IOControl

*Parameters:         OpenHandle        :
                    dwIoControlCode    :
*Return Value:        True/False
*Implementation Notes: JPEG_IOControl sends commands to initiate different
*                       operations like Init,Decode and Deinit.The test 
*                       application uses the DeviceIOControl function to 
*                       specify an operation to perform 
-----------------------------------------------------------------------------*/
BOOL
CMM_IOControl(
    DWORD OpenHandle,
    DWORD dwIoControlCode,
    PBYTE pInBuf,
    DWORD nInBufSize,
    PBYTE pOutBuf,
    DWORD nOutBufSize,
    PDWORD pBytesReturned
    )
{
    CODEC_MEM_CTX       *CodecMem;
    BOOL                result = TRUE;
    DWORD               ret;
    UINT8               *u_addr;
    ALLOC_MEM_T         *node;
    CMM_ALLOC_PRAM_T    *allocParam;
    PVOID pDestMarshalledIn = NULL;
    PVOID pDestMarshalledOut = NULL;
    
    ret = LockCMMMutex();
    if(!ret){
        RETAILMSG(1, (TEXT("[CMM_IOControl] CMM Mutex Lock Fail\r\n")));
        return FALSE;
    }

    CodecMem = (CODEC_MEM_CTX *)OpenHandle;

    if(!CodecMem){
        RETAILMSG(1, (TEXT("[CMM_IOControl] CMM Invalid Input Handle\r\n")));
        UnlockCMMMutex();
        return FALSE;
    }

    if ((pInBuf == NULL) || (nInBufSize == 0)){

⌨️ 快捷键说明

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