📄 simce.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.
//
//------------------------------------------------------------------------------
//
// Copyright (C) 2004, Motorola Inc. All Rights Reserved
//
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//
// File: simce.c
//
// This file implements the stream interface functions for sim
//
//------------------------------------------------------------------------------
#include <windows.h>
#include <devload.h>
#include <smclib.h>
#include <winsmcrd.h>
#include "mxarm11.h"
#include "simce.h"
#include "simcb.h"
#include "simhw.h"
//------------------------------------------------------------------------------
// External Functions
//------------------------------------------------------------------------------
// External Variables
//------------------------------------------------------------------------------
// Defines
#define MAXIMUM_SIM_DEVICES 2
//------------------------------------------------------------------------------
// Types
//------------------------------------------------------------------------------
// Global Variables
CRITICAL_SECTION g_DriverCritSect; // Used to synchronize access to global driver data structures
PSMARTCARD_EXTENSION g_DeviceSlot[MAXIMUM_SIM_DEVICES]; // allow upto MAX_SIM_DEVICES
BOOL g_TrackingThreadTerminate; //flag to determine whether to terminate the tracking thread
//------------------------------------------------------------------------------
// Local Variables
//------------------------------------------------------------------------------
// Local Functions
DWORD GetDeviceName( LPTSTR ActivePath, LPTSTR szDeviceName );
void MakeFriendlyName(PSMARTCARD_EXTENSION SmartcardExtension, LPWSTR szFriendlyName);
DWORD GetIOIndex(void);
PSMARTCARD_EXTENSION SIMLoadDevice( LPTSTR ActiveKey );
void SIMUnloadDevice(PSMARTCARD_EXTENSION SmartcardExtension);
#ifdef DEBUG
DBGPARAM dpCurSettings = {
TEXT("SIM"), {
TEXT("Ioctl"), TEXT("ATR"), TEXT("Protocol"), TEXT("Driver"),
TEXT("Trace"),TEXT("Error"), TEXT("break"),TEXT("all"),
TEXT(" unused"),TEXT("unused"),TEXT("unused"),TEXT("unused"),
TEXT("unused"),TEXT("unused"),TEXT("unused"),TEXT("unused") },
0x00000400 // DEBUG_ERROR
};
#endif // DEBUG
//-----------------------------------------------------------------------------
//
// Function: AddDevice
//
// This function registers the device context
//
// Parameters:
// pDevice
// [in] Pointer of the context of device
//
// Returns:
// BOOL true for success, false for fail
//
//-----------------------------------------------------------------------------
static BOOL AddDevice(PSMARTCARD_EXTENSION pDevice)
{
int i;
EnterCriticalSection(&g_DriverCritSect);
for (i=0; i< MAXIMUM_SIM_DEVICES; i++)
{
if (g_DeviceSlot[i] == NULL)
{
g_DeviceSlot[i] = pDevice;
break;
}
}
LeaveCriticalSection(&g_DriverCritSect);
return (i < MAXIMUM_SIM_DEVICES);
}
//-----------------------------------------------------------------------------
//
// Function: RemoveDevice
//
// This function removes the device context
//
// Parameters:
// pDevice
// [in]Pointer of the context of device
//
// Returns:
// BOOL true for success, false for fail
//
//-----------------------------------------------------------------------------
static BOOL RemoveDevice(PSMARTCARD_EXTENSION pDevice)
{
int i;
EnterCriticalSection(&g_DriverCritSect);
for (i=0; i< MAXIMUM_SIM_DEVICES; i++)
{
if (g_DeviceSlot[i] == pDevice)
{
g_DeviceSlot[i] = NULL;
break;
}
}
LeaveCriticalSection(&g_DriverCritSect);
return (i < MAXIMUM_SIM_DEVICES);
}
//-----------------------------------------------------------------------------
//
// Function: ValidateAndEnterDevice
//
// This function verifies and then registers the device context
//
// Parameters:
// pDevice
// [in]Pointer of the context of device
//
// Returns:
// BOOL true for success, false for fail
//
//-----------------------------------------------------------------------------
static BOOL ValidateAndEnterDevice(PSMARTCARD_EXTENSION pDevice)
{
int i;
EnterCriticalSection(&g_DriverCritSect);
for (i=0; i< MAXIMUM_SIM_DEVICES; i++)
{
if (g_DeviceSlot[i] == pDevice)
{
EnterDevice(pDevice);
break;
}
}
LeaveCriticalSection(&g_DriverCritSect);
#ifdef DEBUG
if (i >= MAXIMUM_SIM_DEVICES)
{
SmartcardDebug(DEBUG_ERROR,(TEXT("ValidateDevice - Invalid Object %x\n"),pDevice));
}
#endif
return (i < MAXIMUM_SIM_DEVICES);
}
//-----------------------------------------------------------------------------
//
// Function: DllEntry
//
// This function provides the dll entry for the driver
//
// Parameters:
// DllInstance
// [in] the instance to the dll
// Reason
// [in] command for attach and detach
// Reserved
// [in] reserved for future use
//
// Returns:
// true for success, false for fail
//
//-----------------------------------------------------------------------------
BOOL WINAPI DllEntry(HINSTANCE DllInstance, INT Reason, LPVOID Reserved)
{
switch(Reason) {
case DLL_PROCESS_ATTACH:
DEBUGREGISTER(DllInstance);
SmartcardDebug(DEBUG_TRACE, (TEXT("DLL_PROCESS_ATTACH\r\n")));
InitializeCriticalSection(&g_DriverCritSect);
memset(g_DeviceSlot,0,sizeof(g_DeviceSlot));
DisableThreadLibraryCalls((HMODULE) DllInstance);
break;
case DLL_PROCESS_DETACH:
SmartcardDebug(DEBUG_TRACE, (TEXT("DLL_PROCESS_DETACH\r\n")));
DeleteCriticalSection(&g_DriverCritSect);
break;
}
return TRUE;
}
//-----------------------------------------------------------------------------
//
// Function: SIMCardDetectionThread
//
// This function creates the card detection thread
//
// Parameters:
// pData
// [in] context of call
//
// Returns:
// 0
//
//-----------------------------------------------------------------------------
static DWORD CALLBACK SIMCardDetectionThread(PVOID pData )
{
NTSTATUS NTStatus = STATUS_SUCCESS;
DWORD OldState;
PSMARTCARD_EXTENSION SmartcardExtension=(PSMARTCARD_EXTENSION)pData;
SmartcardDebug(DEBUG_TRACE,(TEXT("+SIMCardDetectionThread Entering polling thread\n")));
while (SmartcardExtension->ReaderExtension->d_uReaderState == STATE_OPENED) // infinit loop
{
if (g_TrackingThreadTerminate == TRUE)
break;
OldState = SmartcardExtension->ReaderCapabilities.CurrentState;
NTStatus = CBUpdateCardState(SmartcardExtension);
if(((OldState > SCARD_ABSENT) && (SmartcardExtension->ReaderCapabilities.CurrentState <= SCARD_ABSENT))
|| ((OldState <= SCARD_ABSENT) && (SmartcardExtension->ReaderCapabilities.CurrentState > SCARD_ABSENT)))
{
SmartcardDebug(DEBUG_TRACE, (TEXT("SIMCardDetectionThread - Card %s\n"),
(SmartcardExtension->ReaderCapabilities.CurrentState <= SCARD_ABSENT)? TEXT("removed"): TEXT("inserted")));
SmartcardCompleteCardTracking(SmartcardExtension);
SmartcardExtension->ReaderExtension->IoctlPending=FALSE;
}
Sleep(POLLING_PERIOD);
};
SmartcardDebug(DEBUG_TRACE,(TEXT("-SIMCardDetectionThread Terminate polling thread\n")));
return 0;
}
//-----------------------------------------------------------------------------
//
// Function: SIMVendorIoctl
//
// This function performs the vendor io control
//
// Parameters:
// SmartcardExtension
// [in] context of call
//
// Returns:
// NTSTATUS
//
//-----------------------------------------------------------------------------
static NTSTATUS SIMVendorIoctl(PSMARTCARD_EXTENSION SmartcardExtension)
{
NTSTATUS status;
static TCHAR answer[] = _T("Vendor IOCTL");
SmartcardDebug(DEBUG_PROTOCOL,(TEXT("+SIMVendorIoctl\n")));
if (SmartcardExtension->IoRequest.ReplyBuffer != NULL &&
SmartcardExtension->IoRequest.ReplyBufferLength >= (_tcslen(answer) + 1)*sizeof(TCHAR))
{
_tcscpy((TCHAR *)SmartcardExtension->IoRequest.ReplyBuffer, answer);
*SmartcardExtension->IoRequest.Information = _tcslen(answer);
status = STATUS_SUCCESS;
}
else
{
status = STATUS_BUFFER_TOO_SMALL;
}
SmartcardDebug(DEBUG_PROTOCOL,(TEXT("-SIMVendorIoctl (%lx)\n"),status));
return status;
}
//-----------------------------------------------------------------------------
//
// Function: SCR_Init
//
// This function does the initialization to the device
//
// Parameters:
// dwContext
// [in] registry path for this device's active key
//
// Returns:
// DWORD context data (PDISK) for this Init instance or 0 for failure.
//
//-----------------------------------------------------------------------------
DWORD SCR_Init(DWORD dwContext)
{
PSMARTCARD_EXTENSION pSmartcardExtension;
LPTSTR ActiveKey = (LPTSTR)dwContext;
SmartcardDebug(DEBUG_PROTOCOL,(TEXT("+SCR_Init\n")));
if (pSmartcardExtension=SIMLoadDevice(ActiveKey))
{
if (AddDevice(pSmartcardExtension)) // check for device overflow
{
return (DWORD)pSmartcardExtension;
}
else
{
SmartcardDebug(DEBUG_TRACE|DEBUG_ERROR, (TEXT("SCR_Init Device Overflow error\r\n")));
SIMUnloadDevice(pSmartcardExtension);
ASSERT(FALSE);
};
SmartcardDebug(DEBUG_PROTOCOL,(TEXT("-SCR_Init\n")));
}
return (DWORD)pSmartcardExtension;
// insert call to detection function if one is defined
//
// do device initialization
}
//-----------------------------------------------------------------------------
//
// Function: SCR_Deinit
//
// This function does the de-initialization to the device
//
// Parameters:
// dwContext
// [in] registry path for this device's active key
//
// Returns:
// BOOL The device manager does not check the return code
//
//-----------------------------------------------------------------------------
BOOL SCR_Deinit(DWORD dwContext)
{
SmartcardDebug(DEBUG_TRACE,(TEXT("+SCR_Deinit\r\n")));
SIMUnloadDevice((PSMARTCARD_EXTENSION)dwContext);
SmartcardDebug(DEBUG_TRACE,(TEXT("-SCR_Deinit\r\n")));
return TRUE;
}
//-----------------------------------------------------------------------------
//
// Function: SCR_Open
//
// This function open the device instance
//
// Parameters:
// dwData
// [in] context of call
// dwAccess
// [in] not used
// dwShareMode
// [in] not used
//
// Returns:
// DWORD handle value for the open instance
//
//-----------------------------------------------------------------------------
DWORD SCR_Open(DWORD dwData,DWORD dwAccess,DWORD dwShareMode)
{
PSMARTCARD_EXTENSION pSmartcardExtension = (PSMARTCARD_EXTENSION) dwData;
PREADER_EXTENSION readerExtension = pSmartcardExtension->ReaderExtension;
SmartcardDebug(DEBUG_TRACE,(TEXT("+SCR_Open(%x)\n"),dwData));
if (!ValidateAndEnterDevice(pSmartcardExtension))
{
SetLastError(ERROR_BAD_DEVICE);
ASSERT(FALSE);
return 0;
}
SmartcardLockDevice(pSmartcardExtension);
if (readerExtension->d_uReaderState != STATE_CLOSED)
{
SmartcardDebug(DEBUG_ERROR,(TEXT("Open - invalid state %d\n"),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;
//initialize the SIM module
SIM_Init(readerExtension->pSIMReg);
Sleep(10);
CBUpdateCardState(pSmartcardExtension);
SmartcardDebug(DEBUG_TRACE,(TEXT("CardState %d\n"),pSmartcardExtension->ReaderCapabilities.CurrentState));
readerExtension->d_uReaderState = STATE_OPENED;
}
SmartcardUnlockDevice(pSmartcardExtension);
LeaveDevice(pSmartcardExtension);
//may not be necessary
//SIM_Open(readerExtension->pSIMReg);
SmartcardDebug(DEBUG_TRACE,(TEXT("-SCR_Open\n")));
return dwData;
}
//-----------------------------------------------------------------------------
//
// Function: SCR_Close
//
// This function close the device instance
//
// Parameters:
// Handle
// [in] context of call
//
// Returns:
// BOOL true means success false means fail
//
//-----------------------------------------------------------------------------
BOOL SCR_Close(DWORD Handle)
{
BOOL fRet;
DWORD retry = 0;
PSMARTCARD_EXTENSION pSmartcardExtension = (PSMARTCARD_EXTENSION) Handle;
SmartcardDebug(DEBUG_TRACE,(TEXT("+SCR_Close(%x) - entered\n"),Handle));
if (!ValidateAndEnterDevice(pSmartcardExtension))
{
SetLastError(ERROR_BAD_DEVICE);
return FALSE;
}
pSmartcardExtension->ReaderExtension->d_uReaderState = STATE_CLOSED;
while (pSmartcardExtension->ReaderExtension->d_RefCount > 1 && ++retry < 3)
{
// cancel any outstanding blocking calls
SmartcardDebug(DEBUG_TRACE,(TEXT("Close - waiting for %d threads to exit\n"),
pSmartcardExtension->ReaderExtension->d_RefCount -1));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -