📄 cfc_disk.c
字号:
/***************************************************************************
* File: cfc_disk.c - Control the instance of the card
*
* The content of this file or document is CONFIDENTIAL and PROPRIETARY
* to Jade Technologies Co., Ltd. It is subject to the terms of a
* License Agreement between Licensee and Jade Technologies Co., Ltd.
* restricting among other things, the use, reproduction, distribution
* and transfer. Each of the embodiments, including this information
* and any derivative work shall retain this copyright notice.
*
* Copyright (c) 2005 Jade Technologies Co., Ltd.
* All rights reserved.
****************************************************************************/
#include "cfcard.h"
//------------------------------------------------------------------------------
// Global Macros and Variables in CF_DISK.C
//------------------------------------------------------------------------------
//#define CFCARD_FREQ 0x030503 // (to be changed)
#define CFCARD_FREQ 0x040d04 // (to be changed)
HANDLE cfIntrThread = NULL;
HANDLE cfIntrEvent = NULL;
HANDLE cfDataEvent = NULL;
HANDLE cfIdleEvent = NULL;
DISK g_Disk; // global structure of the current instance
extern HANDLE DMAIntrEvent;
//------------------------------------------------------------------------------
// Functions in CF_DISK.C
//------------------------------------------------------------------------------
void CFC_InitEvent(PDISK pDisk);
void CFC_DeinitEvent(PDISK pDisk);
//------------------------------------------------------------------------------
//
// Create a structure of the card's control variables,
// and initialize the instance.
//
// Arguments:
// AcPath - registry path for this device's active key
//
//------------------------------------------------------------------------------
unsigned long
CFC_InitDisk(
LPWSTR AcPath
)
{
LPWSTR ActivePath = AcPath;
PDISK pDisk = &g_Disk;
PHYSICAL_ADDRESS regs;
if (pDisk != NULL)
{
RETAILMSG(MSG_DISK,(_T("CFCARD: InitDisk - entered\r\n")));
InitializeCriticalSection(&pDisk->d_DiskCardCrit);
EnterCriticalSection(&(pDisk->d_DiskCardCrit));
pDisk->d_ActivePath = NULL;
pDisk->d_DiskCardState = STATE_INITING;
regs.HighPart = 0;
regs.LowPart = CF_HOST_BASE_ADDRESS;
pDisk->d_pCFCardRegs = (PCFRegs)MmMapIoSpace(regs, (unsigned long)sizeof(CFRegs), FALSE);
pDisk->d_pCFCardRegs->CF_ADDR_CFReset = 0x01;
pDisk->d_pCFCardRegs->CF_ADDR_CFReset = 0x00;
pDisk->d_pCFCardRegs->CF_ADDR_FIFOreq = 0x0b00;
pDisk->d_pCFCardRegs->CF_ADDR_FIFOreq = 0x0400;
pDisk->d_pCFCardRegs->CF_ADDR_CFINTR = 0x3ffff;
pDisk->d_pCFCardRegs->CF_ADDR_config_val = CFCARD_FREQ;
#if CF_TEST
pDisk->d_pCFCardRegs->CF_ADDR_CardIns = 0;
#else
pDisk->d_pCFCardRegs->CF_ADDR_CardIns = 0x01;
#endif
pDisk->d_pCFCardRegs->CF_ADDR_CFINTena = (1<<CfRawIntr_CfInsert) | (1<<CfRawIntr_CfRemove);
}
if (ActivePath)
{
RETAILMSG(MSG_DISK, (_T("CFCARD: ActiveKey = %s\r\n"), ActivePath));
if (pDisk->d_ActivePath = (unsigned short *)LocalAlloc(LPTR, wcslen(ActivePath)*sizeof(WCHAR)+sizeof(WCHAR)))
{
wcscpy(pDisk->d_ActivePath, ActivePath);
}
RETAILMSG(MSG_DISK, (_T("CFCARD: ActiveKey (copy) = %s (@ 0x%08X)\r\n"), pDisk->d_ActivePath, pDisk->d_ActivePath));
}
CFC_InitEvent(pDisk);
RETAILMSG(MSG_DISK, (_T("CFCARD: Init Disk(0x%x)\r\n"), pDisk));
DMAIntrEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
CFC_AllocateDMA();
LeaveCriticalSection(&(pDisk->d_DiskCardCrit));
return 1;
}
//------------------------------------------------------------------------------
//
// Open the instance - ready for work
//
// Arguments:
// pDisk - handle of the instance
//
//------------------------------------------------------------------------------
unsigned long
CFC_OpenDisk(void)
{
PDISK pDisk = &g_Disk;
EnterCriticalSection(&(pDisk->d_DiskCardCrit));
RETAILMSG(MSG_DISK,(_T("CFCARD: OpenDisk - entered\r\n")));
// Finish initialization by determining the card's capacity
if (pDisk->d_DiskCardState == STATE_INITING)
{
pDisk->d_DiskCardState = STATE_OPENED;
if (CFC_GetDiskInfo(pDisk, &pDisk->d_DiskInfo) == 0)
{
RETAILMSG(MSG_DISK, (_T("CFCARD: OpenDisk - Get Disk Info failed\r\n")));
pDisk->d_DiskCardState = STATE_DEAD;
return 0;
}
}
if (pDisk->d_DiskCardState == STATE_CLOSED)
pDisk->d_DiskCardState = STATE_OPENED;
SetEvent(cfIdleEvent);
RETAILMSG(MSG_DISK,(_T("CFCARD: OpenDisk - leaved\r\n")));
LeaveCriticalSection(&(pDisk->d_DiskCardCrit));
return 1;
}
//------------------------------------------------------------------------------
//
// Close the instance - suspend the driver
//
// Arguments:
// pDisk - handle of the instance
//
//------------------------------------------------------------------------------
BOOL
CFC_CloseDisk(void)
{
PDISK pDisk = &g_Disk;
EnterCriticalSection(&(pDisk->d_DiskCardCrit));
RETAILMSG(MSG_DISK,(_T("CFCARD: CloseDisk - entered\r\n")));
if (pDisk->d_DiskCardState == STATE_OPENED || pDisk->d_DiskCardState == STATE_DEAD)
pDisk->d_DiskCardState = STATE_CLOSED;
SetEvent(cfIdleEvent);
LeaveCriticalSection(&(pDisk->d_DiskCardCrit));
RETAILMSG(MSG_DISK,(_T("CFCARD: CloseDisk - leaved\r\n")));
return TRUE;
}
//------------------------------------------------------------------------------
//
// Deinit the driver
//
// Arguments:
// pDisk - handle of the instance
//
//------------------------------------------------------------------------------
BOOL
CFC_DeinitDisk(void)
{
BOOL ret = FALSE;
PDISK pDisk = &g_Disk;
if (pDisk->d_DiskCardState == STATE_OPENED)
CFC_CloseDisk();
CFC_ReleaseDMA();
// close interrupt handle
if (DMAIntrEvent != NULL)
{
CloseHandle (DMAIntrEvent);
DMAIntrEvent = NULL;
}
CFC_DeinitEvent(pDisk);
RETAILMSG(MSG_DISK, (_T("CFCARD : Deinit Disk(0x%x)\r\n"), pDisk));
MmUnmapIoSpace((PVOID)pDisk->d_pCFCardRegs, sizeof(CFRegs));
if (!LocalFree(pDisk))
{
pDisk = NULL;
ret = TRUE;
}
return ret;
}
//------------------------------------------------------------------------------
//
// Initialize events and threads - called in CFC_InitDisk
//
// Arguments:
// pDisk - handle of the instance
//
//------------------------------------------------------------------------------
void
CFC_InitEvent(
PDISK pDisk
)
{
CFRegs *regs = pDisk->d_pCFCardRegs;
cfIntrEvent = CreateEvent(NULL, 0 , 0, NULL);
cfDataEvent = CreateEvent(NULL, 0 , 0, NULL);
cfIdleEvent = CreateEvent(NULL, 0 , 0, NULL);
regs->CF_ADDR_CFINTena = (1<<CfRawIntr_CfInsert) | (1<<CfRawIntr_CfRemove);
if(!InterruptInitialize(SYSINTR_CF, cfIntrEvent, NULL, 0))
{
InterruptDisable(SYSINTR_CF);
RETAILMSG(MSG_DISK, (_T("CFCARD: Init Event - Intr Init failed\r\n")));
}
cfIntrThread = CreateThread(NULL,0,CFC_WaitIntr,(LPVOID)pDisk,0,NULL);
}
//------------------------------------------------------------------------------
//
// Deinit events and threads - called in CFC_DeinitDisk
//
// Arguments:
// pDisk - handle of the instance
//
//------------------------------------------------------------------------------
void CFC_DeinitEvent(
PDISK pDisk
)
{
CFRegs *regs = pDisk->d_pCFCardRegs;
regs->CF_ADDR_CFINTena = 0;
if (cfIntrThread)
{
if (TerminateThread(cfIntrThread,1))
CloseHandle(cfIntrThread);
}
if (cfIntrEvent)
CloseHandle(cfIntrEvent);
if (cfDataEvent)
CloseHandle(cfDataEvent);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -