📄 tlp3ce.c
字号:
//
// 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.
//
/*++
Module Name:
tlp3ce.c
Abstract:
Smart Card Reader Driver for Windows CE.
--*/
#include <stdio.h>
#include <smclib.h>
#include <devload.h>
#include <tchar.h>
#include "TLP3CE.h"
#include "BullTLP3.h"
#include <sci.h>
#include <windows.h>
#include <pkfuncs.h>
#include <platform.h>
#include <ceddk.h>
#include <oalintr.h>
extern DBGPARAM dpCurSettings;
void apSleep(unsigned int msec);
psci g_SCI0RegBase;
#ifdef DEBUG
// To get access to the debug zones (from MDD layer)
extern DBGPARAM dpCurSettings;
#endif
HANDLE hSCIevent;
// prototypes
BOOL TLP3UpdateReaderState(PSMARTCARD_EXTENSION SmartcardExtension);
const TCHAR szDriverName[] = TEXT("built-in Smart Card Reader ");
// Used to synchronize access to global driver data structures
CRITICAL_SECTION g_DriverCritSect;
#ifdef DEBUG
#define DEBUG_INIT 0x00000040
#define DEBUG_SERIAL 0x00000080
#define DEBUG_THREAD 0x00000200
DBGPARAM dpCurSettings = {
TEXT("BULLTLP3"), {
TEXT("Ioctl"), TEXT("ATR"), TEXT("Protocol"), TEXT("Driver"),
TEXT("Trace"),TEXT("Error"), TEXT("Initialization"),TEXT("Serial"),
TEXT(" I/O"),TEXT("Thread"),TEXT(""),TEXT(""),
TEXT(""),TEXT(""),TEXT(""),TEXT("Break On Open") },
0xFFFFFFFF // DEBUG_ERROR
};
#endif // DEBUG
// allow upto MAX_PSCR_DEVICES
PSMARTCARD_EXTENSION DeviceSlot[MAXIMUM_SERIAL_READERS];
BOOL AddDevice(PSMARTCARD_EXTENSION pDevice)
{
int i;
EnterCriticalSection(&g_DriverCritSect);
for (i=0; i< MAXIMUM_SERIAL_READERS; i++)
{
if (DeviceSlot[i] == NULL)
{
DeviceSlot[i] = pDevice;
break;
}
}
LeaveCriticalSection(&g_DriverCritSect);
return (i < MAXIMUM_SERIAL_READERS);
}
BOOL RemoveDevice(PSMARTCARD_EXTENSION pDevice)
{
int i;
EnterCriticalSection(&g_DriverCritSect);
for (i=0; i< MAXIMUM_SERIAL_READERS; i++)
{
if (DeviceSlot[i] == pDevice)
{
DeviceSlot[i] = NULL;
break;
}
}
LeaveCriticalSection(&g_DriverCritSect);
return (i < MAXIMUM_SERIAL_READERS);
}
BOOL ValidateAndEnterDevice(PSMARTCARD_EXTENSION pDevice)
{
int i;
EnterCriticalSection(&g_DriverCritSect);
for (i=0; i< MAXIMUM_SERIAL_READERS; i++)/////
{
if (DeviceSlot[i] == pDevice)
{
EnterDevice(pDevice);
break;
}
}
LeaveCriticalSection(&g_DriverCritSect);
#ifdef DEBUG
if (i >= MAXIMUM_SERIAL_READERS)
{
SmartcardDebug(DEBUG_ERROR,(TEXT("%s:ValidateDevice - Invalid Object %x\n"),szDriverName,pDevice));
}
#endif
return (i < MAXIMUM_SERIAL_READERS);
}
//
// DLL entry
//
BOOL WINAPI
DllEntry(HINSTANCE DllInstance, INT Reason, LPVOID Reserved)
{
switch(Reason) {
case DLL_PROCESS_ATTACH:
DEBUGREGISTER(DllInstance);
SmartcardDebug(DEBUG_INIT, (TEXT("%s: DLL_PROCESS_ATTACH\r\n"), szDriverName));
InitializeCriticalSection(&g_DriverCritSect);
memset(DeviceSlot,0,sizeof(DeviceSlot));
DisableThreadLibraryCalls((HMODULE) DllInstance);
break;
case DLL_PROCESS_DETACH:
SmartcardDebug(DEBUG_INIT, (TEXT("%s: DLL_PROCESS_DETACH\r\n"), szDriverName));
DeleteCriticalSection(&g_DriverCritSect);
break;
}
return TRUE;
}
//
// File system device entrypoints (DEV_????)
//
//
// Returns context data (PDISK) for this Init instance or 0 for failure.
//
// Arguments:
// dwContext - registry path for this device's active key
//
DWORD
SCR_Init(
DWORD dwContext
)
{
PSMARTCARD_EXTENSION pSmartcardExtension;
LPTSTR ActiveKey = (LPTSTR)dwContext;
HANDLE hSCIthread;
// Setup Virtual/Physical memory
PHYSICAL_ADDRESS PhysicalAddress;
PhysicalAddress.HighPart = 0;
PhysicalAddress.LowPart = 0x20038000;
#ifdef DEBUG
dpCurSettings.ulZoneMask = 0xFFFF;
#endif
RETAILMSG(1,(TEXT("\r\nBuilt in Smart Card Reader\r\n")));
g_SCI0RegBase = (psci)MmMapIoSpace(PhysicalAddress, (ULONG)sizeof(PL131_sReg),FALSE);
DEBUGMSG( 0, (TEXT("g_SCI0RegBase=0x%x\r\n"),g_SCI0RegBase));
if( !g_SCI0RegBase )
{
DEBUGMSG(1,(TEXT("Map SCI0 registers failed")));
}
else{
{
//we should disable 68K bus (close pin mux)
PHYSICAL_ADDRESS PhysicalIoBase;
volatile unsigned long *SC_ADD;
PhysicalIoBase.HighPart = 0;
PhysicalIoBase.LowPart = 0x2002010C;
SC_ADD = (unsigned long *)MmMapIoSpace( PhysicalIoBase, 0x100 ,FALSE );
RETAILMSG(0,(TEXT("SC_ADD = 0x%x\r\n"),SC_ADD));
*(volatile long *)SC_ADD &= 0xffff7fff;
DEBUGMSG(1,(TEXT("*SC_ADD = 0x%x\r\n"),*SC_ADD));
}
InitializeDevice( );
}
// Create the interrupt event for the SCI IST
hSCIevent = CreateEvent(NULL, FALSE, FALSE, NULL);
if(hSCIevent == NULL)
{
DEBUGMSG(1, (TEXT("Error creating SCI IST event\r\n")));
return FALSE;
}
// Enable the interrupts for the SCI subsystem
if(!InterruptInitialize(SYSINTR_SCI ,hSCIevent, NULL, 0))
{
DEBUGMSG(1,(TEXT("SCI_Init failed - Cannot initialise SYSINTR_SCI:%d\n"),
GetLastError()));
return FALSE;
}
// Launch the SCI IST
hSCIthread = CreateThread(NULL,0,SCR_ISTthread,0,0, NULL );
if(hSCIthread == NULL)
{
return FALSE;
}
else
{
// Make sure the thread has a higher priority than the driver
CeSetThreadPriority(hSCIthread, 20);
}
Sleep(100);
if (pSmartcardExtension=TLP3LoadDevice(ActiveKey)) {
if (AddDevice(pSmartcardExtension)) // check for device overflow
{
TCHAR szDeviceName[DEVNAME_LEN];
TCHAR szFriendlyName[MAXIMUM_ATTR_STRING_LENGTH+DEVNAME_LEN+5];
DWORD status;
// The device name should be available from the Active Key
// [On versions prior to CE 3.0, this wont work until the post-init IOCTL]
status = GetDeviceName(pSmartcardExtension->ReaderExtension->d_ActivePath,szDeviceName);
if (status == STATUS_SUCCESS)
{
// figure out the unit number from the device name
PTCHAR pch = szDeviceName;
while (*pch && (*pch < '0' || *pch > '9'))
++pch;
if (*pch)
pSmartcardExtension->VendorAttr.UnitNo = *pch - '0';
// Attempt to register a friendly name for this device
// for the benefit of the resource manager.
// The friendly name has the format "PRODUCTNAME [UNITNO]"
// For example, "SCM SwapSmart [1]"
//
MakeFriendlyName(pSmartcardExtension, szFriendlyName);
SmartcardCreateLink(szFriendlyName,szDeviceName);
}
//g_SCI0RegBase->RegIntMask = 0x7FF;
//g_SCI0RegBase->RegIntMask = 0x01;
Sleep(100);
g_SCI0RegBase->RegIntClear = 0x1FFF;
return (DWORD)pSmartcardExtension;
}
else {
SmartcardDebug(DEBUG_INIT|DEBUG_ERROR,
(TEXT("%s: TLP_Init Device Overflow error\r\n"),szDriverName));
TLP3UnloadDevice(pSmartcardExtension);
ASSERT(FALSE);
};
}
return (DWORD)TRUE;
// insert call to detection function if one is defined
//
// do device initialization
}
//
// Device deinit - devices are expected to close down.
// The device manager does not check the return code.
//
BOOL
SCR_Deinit(
DWORD dwContext // pointer to the per device structure
)
{
SmartcardDebug(DEBUG_INIT|DEBUG_TRACE,
(TEXT("%s: TLP_DeInit\r\n"),szDriverName));
SCR_Close(dwContext);
TLP3UnloadDevice((PSMARTCARD_EXTENSION)dwContext);
return TRUE;
}
//
// Returns handle value for the open instance.
// we only allow one open at a time because this is an inherently state-ful device
//
DWORD
SCR_Open(
DWORD dwData,
DWORD dwAccess,
DWORD dwShareMode
)
{
PSMARTCARD_EXTENSION pSmartcardExtension = (PSMARTCARD_EXTENSION) dwData;
PREADER_EXTENSION readerExtension = pSmartcardExtension->ReaderExtension;///????????????????
SmartcardDebug(DEBUG_TRACE,(TEXT("%s: Open(%x) - entered\r\n"),szDriverName,dwData));
/*#ifdef DEBUG
if (DEBUGZONE(15))
DebugBreak();
#endif*/
if (!ValidateAndEnterDevice(pSmartcardExtension))
{
SetLastError(ERROR_BAD_DEVICE);
ASSERT(FALSE);
return 0;
}
SmartcardLockDevice(pSmartcardExtension);
if (readerExtension->d_uReaderState != STATE_CLOSED)
{
SmartcardDebug(DEBUG_ERROR,(TEXT("%s: Open - invalid state %d\n"), szDriverName,
readerExtension->d_uReaderState
));
dwData = 0;
SetLastError(ERROR_SHARING_VIOLATION);
ASSERT(FALSE);
}
else
{
// clear card state
memset(&pSmartcardExtension->CardCapabilities, 0, sizeof(SCARD_CARD_CAPABILITIES));
// clear reader state
pSmartcardExtension->ReaderCapabilities.CurrentState = SCARD_UNKNOWN;
// save the current power state of the reader
readerExtension->ReaderPowerState = PowerReaderWorking;
Sleep(10);
// if(TLP3UpdateReaderState(pSmartcardExtension));
TLP3UpdateReaderState(pSmartcardExtension);
//***************************************************************************
// {CardActive( pSmartcardExtension,TRUE);
// pSmartcardExtension->ReaderCapabilities.CurrentState = SCARD_POWERED;
// }
//****************************************************************************
SmartcardDebug(DEBUG_TRACE,(TEXT("%s: CardState %d\n"),szDriverName,pSmartcardExtension->ReaderCapabilities.CurrentState));
{
// Create Backgroud thread for monitoring Card insertion
readerExtension->hBackgroundThread=
CreateThread(NULL,
0,
TLP3SerialEvent,
pSmartcardExtension,
CREATE_SUSPENDED,
&(readerExtension->dwThreadID));
// ASSERT(readerExtension->hBackgroundThread);
if (readerExtension->hBackgroundThread)
{
readerExtension->d_uReaderState = STATE_OPENED;
ResumeThread(readerExtension->hBackgroundThread);
}
}
if (readerExtension->d_uReaderState != STATE_OPENED)
{
// cleanup
dwData = 0;
}
}
SmartcardUnlockDevice(pSmartcardExtension);
LeaveDevice(pSmartcardExtension);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -