?? serialcsr.cpp
字號(hào):
//
// Copyright (c) Cambridge Silicon Radio. 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.
//
// Module Name: SerialCSR.cpp
//
//
// Abstract:
//
// This file contains the SERIALCSR code. This is a wrapper library
// intended to provide a shim layer above an OEM serial library for
// updating the persistent store on CSR Bluecore based devices for
// the WinCE Operating System. Wrapped functions include all those
// defined in mdd.c from the Microsoft sample serial driver code.
//
// Wrapped Functions:
//
// EXTERNAL
// COM_Init
// COM_Open
// COM_Close
// COM_Deinit
// COM_Read
// COM_Write
// COM_Seek
// COM_PowerUp
// COM_PowerDown
// COM_IOControl
//
// INTERNAL
// SerialEventHandler
// SerialDispatchThread
// ApplyDCB
// SerialGetDroppedByteNumber
// WaitCommEvent
// EvaluateEventFlag
// ProcessExiting
//
//
// Notes:
// 1) COM_Init is modified to update the persistent store using uBCSP
// via UART.
// 2) When COM_Powerup is detected a flag is set indicating a rewrite
// should be attempted on next access.
// 3) This library provides pass through for all other standard serial
// MDD functions.
//
/* Int'l Corp. 2005
$Modifier sign Description
==========================================================================================
*/
#include <windows.h>
#include <types.h>
#include "Pegdser.h"
#include "SerialCSR.h"
#include "ubcsp.h"
#include "PSConfig.h"
#include "S3c2440a_base_regs.h"
#include "S3c2440a_ioport.h"
#include <pm.h>
#include "serpriv.h"
//#define SC_DBG TRUE
#define SC_DBG 1
#define DEFAULTBAUD 115200
#define PAGE_SIZE 0x1000
#define ALIGNMENT_MASK (PAGE_SIZE-1)
//jack debug
#define IOCTL_HAL_GET_BTPARAM CTL_CODE( FILE_DEVICE_HAL, 2075, METHOD_BUFFERED, FILE_ANY_ACCESS )
#define IOCTL_HAL_SET_BTPARAM CTL_CODE( FILE_DEVICE_HAL, 2076, METHOD_BUFFERED, FILE_ANY_ACCESS )
//jack debug
//bryant del
//wchar_t* gchwLibName = L"BTSerial.dll";
wchar_t* gchwLibName = L"serial_smdk2440.dll";
HINSTANCE ghLibInst = NULL;
HANDLE ghSerialHead = NULL;
HMODULE ghInstSerLib = NULL;
BOOL gbUpdatePS = FALSE;
HANDLE hBTTest = NULL;
BOOL bTestMode = FALSE;
BOOL gbBTPower = FALSE;
HANDLE hBTinitEvent = NULL;
static volatile S3C2440A_IOPORT_REG *s2440IOP = NULL;
PSKEY_UPDATE pSkey;
static DWORD PSKeyUpdate(PPSKEY_UPDATE pSkey);
BOOL BTEnable(BOOL bIsOn);
PVOID VirtualAllocCopyPhysical(unsigned size, char *str, PVOID pPhysicalAddress);
void ShowIOPort(void);
// Function pointers
PFNCOM_Init gpfnCOM_Init = NULL;
PFNCOM_Open gpfnCOM_Open = NULL;
PFNCOM_Close gpfnCOM_Close = NULL;
PFNCOM_Deinit gpfnCOM_Deinit = NULL;
PFNCOM_Read gpfnCOM_Read = NULL;
PFNCOM_Write gpfnCOM_Write = NULL;
PFNCOM_Seek gpfnCOM_Seek = NULL;
PFNCOM_Power gpfnCOM_PowerUp = NULL;
PFNCOM_Power gpfnCOM_PowerDown = NULL;
PFNCOM_IOControl gpfnCOM_IOControl = NULL;
//PFNSerialEventHandler gpfnSerialEventHandler = NULL;
//PFNSerialDispatchThread gpfnSerialDispatchThread = NULL;
PFNSerialGetDroppedByteNumber gpfnSerialGetDroppedByteNumber = NULL;
//PFNWaitCommEvent gpfnWaitCommEvent = NULL;
//PFNEvaluateEventFlag gpfnEvaluateEventFlag = NULL;
//PFNProcessExiting gpfnProcessExiting = NULL;
//PFNApplyDCB gpfnApplyDCB = NULL;
////////////////////////////////////////////////////////////////////////
// DllMain
//
// Entry point
//
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call,
LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
DisableThreadLibraryCalls((HMODULE) hModule);
ghLibInst = LoadLibrary(gchwLibName);
if(!ghLibInst)
{
RETAILMSG(TRUE,(TEXT("bryant: init load lib failed !!!!\r\n")));
return FALSE;
}
else
RETAILMSG(SC_DBG,(TEXT("bryant: bluetooth -----------\r\n")));
RETAILMSG(SC_DBG,(TEXT("bryant: init load lib ok !!!!\r\n")));
// Setup Function pointers
if(!GetLibraryPointers())
{
RETAILMSG(TRUE,(TEXT("!!!! init get lib pointer failed !!!!\r\n")));
return FALSE;
}
else
RETAILMSG(SC_DBG,(TEXT("@@ init get lib ok !!!!\r\n")));
RETAILMSG(SC_DBG,(TEXT("DLL_PROCESS_ATTACH finish\r\n")));
}
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
{
// Free
RETAILMSG(TRUE,(TEXT("DLL_PROCESS_DETACH finish\r\n")));
FreeLibrary(ghLibInst);
}
break;
}
return TRUE;
}
BOOL GetLibraryPointers(void)
{
RETAILMSG(SC_DBG,(TEXT("GetLibraryPointer:enter\r\n")));
//Init
gpfnCOM_Init = (PFNCOM_Init)GetProcAddress(ghLibInst, _T("COM_Init"));
if(!gpfnCOM_Init)
return FALSE;
//Open
gpfnCOM_Open = (PFNCOM_Open)GetProcAddress(ghLibInst, _T("COM_Open"));
if(!gpfnCOM_Open)
return FALSE;
// Close
gpfnCOM_Close = (PFNCOM_Close)GetProcAddress(ghLibInst, _T("COM_Close"));
if(!gpfnCOM_Close)
return FALSE;
// Deinit
gpfnCOM_Deinit = (PFNCOM_Deinit)GetProcAddress(ghLibInst, _T("COM_Deinit"));
if(!gpfnCOM_Deinit)
return FALSE;
// Read
gpfnCOM_Read = (PFNCOM_Read)GetProcAddress(ghLibInst, _T("COM_Read"));
if(!gpfnCOM_Read)
return FALSE;
// Write
gpfnCOM_Write = (PFNCOM_Write)GetProcAddress(ghLibInst, _T("COM_Write"));
if(!gpfnCOM_Write)
return FALSE;
// PowerUp
gpfnCOM_PowerUp = (PFNCOM_Power)GetProcAddress(ghLibInst, _T("COM_PowerUp"));
if(!gpfnCOM_PowerUp)
return FALSE;
// PowerDown
gpfnCOM_PowerDown = (PFNCOM_Power)GetProcAddress(ghLibInst, _T("COM_PowerDown"));
if(!gpfnCOM_PowerDown)
return FALSE;
// IOControl
gpfnCOM_IOControl = (PFNCOM_IOControl)GetProcAddress(ghLibInst, _T("COM_IOControl"));
if(!gpfnCOM_IOControl)
return FALSE;
return TRUE;
}
/////////////////////////////////////////////////////////////////////
// COM_Init Wrapper
//
// By design this function returns a pointer to the serial head,
// which then is passed as a device handle into the entry points
// COM_Open and COM_Deinit.
//
// The CSR implementation of this function ensures the persistent
// store of the CSR chip is updated on first access to the device
//
BOOL
WINAPI
CSR_Deinit(void)
{
BOOL bResult = NULL;
if(ghLibInst)
{
if(gpfnCOM_Deinit)
bResult = gpfnCOM_Deinit();
}
if (pSkey.PSKeyEvent && CloseHandle(pSkey.PSKeyEvent))
pSkey.PSKeyEvent = NULL;
if (pSkey.PSKey && CloseHandle(pSkey.PSKey))
pSkey.PSKey = NULL;
return bResult;
}
/////////////////////////////////////////////////////////////////////
// COM_Open Wrapper
//
// The CSR implementation of this function ensures the persistent
// store of the CSR chip is updated on first access to the device
//
HANDLE
WINAPI
CSR_Open(HANDLE pContext, DWORD AccessCode, DWORD ShareMode)
{
HANDLE hResult = NULL;
if (ghLibInst)
{
if (gpfnCOM_Open)
{
RETAILMSG(SC_DBG,(TEXT("CSR_Open Start\r\n")));
hResult = gpfnCOM_Open(pContext, AccessCode, ShareMode);
hBTTest = hResult ; ////0505 for BT test
// Update persistent store
if(gbUpdatePS)
{
BTEnable(TRUE);
gbUpdatePS = FALSE;
UpdatePersistentStore(hResult);
}
}
}
RETAILMSG(SC_DBG,(TEXT("CSR_Open Finish\r\n")));
return hResult;
}
/////////////////////////////////////////////////////////////////////
// COM_Close Wrapper
//
// The CSR implementation of this function ensures the persistent
// store of the CSR chip is updated on first access to the device
//
BOOL
WINAPI
CSR_Close(DWORD pContext)
{
BOOL bResult = NULL;
if(ghLibInst)
{
if(gpfnCOM_Close)
{
RETAILMSG(SC_DBG,(TEXT("CSR_Close Start\r\n")));
bResult = gpfnCOM_Close(pContext);
if(!bTestMode)
{
BTEnable(FALSE);
}
bTestMode = FALSE;
}
gbUpdatePS = TRUE;
}
RETAILMSG(SC_DBG,(TEXT("CSR_Close Finish\r\n")));
return bResult;
}
/////////////////////////////////////////////////////////////////////
// COM_Deinit Wrapper
//
// The CSR implementation of this function is a simple passthrough
//
HANDLE
WINAPI
CSR_Init(ULONG Identifier)
{
HANDLE hPort = NULL;
DWORD dwErr = ERROR_SUCCESS;
ULONG lpBytesReturned;
RETAILMSG(SC_DBG,(TEXT("CSR_Init:enter---------------------------\r\n")));
RETAILMSG(1,(TEXT("CSR_Init:enter\r\n")));
s2440IOP = ( S3C2440A_IOPORT_REG *) VirtualAllocCopyPhysical(sizeof(S3C2440A_IOPORT_REG),(char *)TEXT("SerailCSR: s2440IOP"), (PVOID)(S3C2440A_BASE_REG_PA_IOPORT));
if (s2440IOP==NULL)
//bryant add
{
RETAILMSG(TRUE,(TEXT("@@ CSR init VirtualAllocCopy fail !!!!\r\n")));
goto _error_exit;
}
//bryant del BTEnable(FALSE);
BTEnable(TRUE);
if(ghLibInst)
{ //ghlibinst,serial lib
if(gpfnCOM_Init)
{
RETAILMSG(SC_DBG,(TEXT("CSR_Init Start\r\n")));
ghSerialHead = gpfnCOM_Init(Identifier);
if (!ghSerialHead)
{
RETAILMSG(TRUE,(TEXT("!!!! COM init failed head=%x id=%s!!!!\r\n"),ghSerialHead,Identifier));
goto _error_exit;
}
pSkey.PSKeyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if ((pSkey.PSKey = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) PSKeyUpdate, (LPVOID)&pSkey, 0, NULL)) == NULL)
{
dwErr = GetLastError();
RETAILMSG(TRUE,(TEXT("CreateThread ERROR: Unable to create PSKey: %u\r\n"), dwErr));
goto _error_exit;
}
if ( !CeSetThreadPriority(pSkey.PSKey, 101)) {
dwErr = GetLastError();
RETAILMSG(TRUE, (TEXT("CeSetThreadPriority ERROR:%d \r\n"), dwErr));
goto _error_exit;
}
}
}
RETAILMSG(SC_DBG,(TEXT("CSR_Init Finish\r\n")));
_error_exit:
return ghSerialHead;
}
/////////////////////////////////////////////////////////////////////
// COM_Read Wrapper
//
// The CSR implementation of this function ensures the persistent
// store of the CSR chip is updated on first access to the device
//
ULONG
WINAPI
CSR_Read(HANDLE pContext, PUCHAR pTargetBuffer,
ULONG BufferLength, PULONG pBytesRead)
{
ULONG lResult = NULL;
RETAILMSG(SC_DBG,(TEXT("CSR_Read:Enter\r\n")));
if(ghLibInst)
{
if(gpfnCOM_Read)
{
lResult = gpfnCOM_Read(pContext, pTargetBuffer, BufferLength, pBytesRead);
RETAILMSG(SC_DBG,(TEXT("CSR_Read 2: len=%d,readBytes=%d,data=%x\r\n"),BufferLength,*pBytesRead,pTargetBuffer[0]));
}
}
RETAILMSG(SC_DBG,(TEXT("CSR_Read:Finish : lResult=%x\r\n"),lResult));
return lResult;
}
/////////////////////////////////////////////////////////////////////
// COM_Write Wrapper
//
// The CSR implementation of this function is a simple passthrough
//
DWORD
WINAPI
CSR_Write(HANDLE pContext, PUCHAR pSourceBytes, ULONG NumberOfBytes)
{
ULONG lResult = NULL;
RETAILMSG(SC_DBG,(TEXT("CSR_Write:Enter\r\n")));
if(ghLibInst)
{
if(gpfnCOM_Write)
{
RETAILMSG(SC_DBG,(TEXT("CSR_Write:gpfnCOM_Write==TRUE\r\n")));
lResult = gpfnCOM_Write(pContext, pSourceBytes, NumberOfBytes);
}
}
RETAILMSG(SC_DBG,(TEXT("CSR_Write:Finish : lResult=%x\r\n"),lResult));
return lResult;
}
/////////////////////////////////////////////////////////////////////
// COM_Seek Wrapper
//
// The CSR implementation of this function is a simple passthrough
//
ULONG
WINAPI
CSR_Seek(HANDLE pHead, LONG Position, DWORD Type)
{
ULONG lResult = NULL;
if(ghLibInst)
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -