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

📄 stcusbce.c

📁 Windows CE 5.0 下的SmartCard驱动。
💻 C
📖 第 1 页 / 共 3 页
字号:
            DeviceSlot[i] = NULL;
            break;
        }
    }
    LeaveCriticalSection(&g_DriverCritSect);

    return (i < STCUSB_MAX_DEVICE);
}

static BOOL ValidateAndEnterDevice(PSMARTCARD_EXTENSION pDevice)
{
    int i;
    EnterCriticalSection(&g_DriverCritSect);
    
    for (i=0; i< STCUSB_MAX_DEVICE; i++)
    {
        if (DeviceSlot[i] == pDevice)
        {
            EnterDevice(pDevice);
            break;
        }
    }
    LeaveCriticalSection(&g_DriverCritSect);
#ifdef DEBUG
    if (i >= STCUSB_MAX_DEVICE)
    {
        SmartcardDebug(DEBUG_ERROR,(TEXT("%s:ValidateDevice - Invalid Object %x\n"),szDriverName,pDevice));
    }
#endif
    return (i < STCUSB_MAX_DEVICE);
}


//
// DLL entry
//
BOOL WINAPI
DllEntry(HINSTANCE DllInstance, INT Reason, LPVOID Reserved)
{
    switch(Reason) {
    case DLL_PROCESS_ATTACH:
        DEBUGREGISTER(DllInstance);
        SmartcardDebug(DEBUG_TRACE, (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_TRACE, (TEXT("%s: DLL_PROCESS_DETACH\r\n"), szDriverName));
        DeleteCriticalSection(&g_DriverCritSect);
        break;
    }
    return TRUE;
}


NTSTATUS
VendorIoctl(
    PSMARTCARD_EXTENSION SmartcardExtension
    )
{
    NTSTATUS status;
    static TCHAR answer[] = _T("Vendor IOCTL");

    SmartcardDebug(
        DEBUG_PROTOCOL,
        (TEXT("%s!VendorIoctl: Enter\n"),
        szDriverName)
        );
    switch (SmartcardExtension->MajorIoControlCode) {
#if (CE_MAJOR_VER < 0x0003)
    case IOCTL_INIT_COMPLETE:
        {
        //
        // Called by the device manager after SCR_Init (assuming the "Ioctl" registry value has been set)
        // 
        TCHAR szDeviceName[DEVNAME_LEN];
        TCHAR szFriendlyName[MAXIMUM_ATTR_STRING_LENGTH+DEVNAME_LEN+5];
        // The device name should now be available
        status = GetDeviceName(SmartcardExtension->ReaderExtension->d_ActivePath,szDeviceName, NULL);
        if (NT_SUCCESS(status)) {
            PTCHAR pch = szDeviceName;
            while (*pch && (*pch < '0' || *pch > '9'))
                ++pch;
            if (*pch)
                SmartcardExtension->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(SmartcardExtension, szFriendlyName);
            SmartcardCreateLink(szFriendlyName,szDeviceName); 
            status=STATUS_SUCCESS;
        }
        break;
        }
#endif        
    default:
    
        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("%s!VendorIoctl: Exit(%lx)\n"),
        szDriverName,
        status)
        );

    return status;
}


static DWORD CALLBACK 
StcUsbCardDetectionThread(
    PVOID pData )
/*++

StcUsbCardDetectionThread:
    create the card detection thread
Arguments:
    SmartcardExtension    context of call

Return Value:
    -

--*/
{
    NTSTATUS    NTStatus = STATUS_SUCCESS;
    DWORD OldState;
    PSMARTCARD_EXTENSION SmartcardExtension=(PSMARTCARD_EXTENSION)pData;

    SmartcardDebug( 
        DEBUG_TRACE, 
        (TEXT("%s!StcUsbCardDetectionThread Entering polling thread\n"),
        DRIVER_NAME));


    while (SmartcardExtension->ReaderExtension->d_uReaderState == STATE_OPENED)  // infinit loop
    {


        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("%s!StcUsbCardDetectionThread - Card %s\n"),
                DRIVER_NAME,
                (SmartcardExtension->ReaderCapabilities.CurrentState <= SCARD_ABSENT)? TEXT("removed"): TEXT("inserted"))
                );
            SmartcardCompleteCardTracking(SmartcardExtension);
            SmartcardExtension->ReaderExtension->IoctlPending=FALSE;

        }
        Sleep(POLLING_PERIOD);


    };
    

    SmartcardDebug( 
        DEBUG_TRACE, 
        (TEXT("%s!StcUsbCardDetectionThread Terminate polling thread\n"),
        DRIVER_NAME));
    return 0; 
}


//
// File system device entrypoints (DEV_XXX)
//

//
// 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;

    if (pSmartcardExtension=StcUsbLoadDevice(ActiveKey)) {
        if (AddDevice(pSmartcardExtension)) // check for device overflow
        {
            return (DWORD)pSmartcardExtension;
        }
        else {
            SmartcardDebug(DEBUG_TRACE|DEBUG_ERROR,
                (TEXT("%s: SCR_Init Device Overflow error\r\n"),szDriverName));
            StcUsbUnloadDevice(pSmartcardExtension);
            ASSERT(FALSE);
        };

    }
    return (DWORD)pSmartcardExtension;
    // 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_TRACE,
        (TEXT("%s: TLP_DeInit\r\n"),szDriverName));
    SCR_Close(dwContext);
    StcUsbUnloadDevice((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\n"),szDriverName,dwData));
    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;

        STCConfigureSTC(readerExtension,STCInitialize);
    
        Sleep(10);
        CBUpdateCardState(pSmartcardExtension);
        SmartcardDebug(DEBUG_TRACE,(TEXT("%s: CardState %d\n"),szDriverName,pSmartcardExtension->ReaderCapabilities.CurrentState));
        {
            // Create Backgroud thread for monitoring Card insertion
            ASSERT(!readerExtension->hBackgroundThread);
                readerExtension->hBackgroundThread=

            CreateThread(NULL,
                        0,
                        StcUsbCardDetectionThread,
                        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);

    return dwData;
}

BOOL
SCR_Close(
    DWORD Handle
    )
{
    BOOL fRet;
    DWORD retry = 0;
    NTSTATUS status;
    PSMARTCARD_EXTENSION   pSmartcardExtension = (PSMARTCARD_EXTENSION) Handle;

    SmartcardDebug(DEBUG_TRACE,(TEXT("%s: Close(%x) - entered\n"),szDriverName,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("%s: Close - waiting for %d threads to exit\n"),
         szDriverName, pSmartcardExtension->ReaderExtension->d_RefCount -1
        ));
        SmartcardDeviceControl(pSmartcardExtension, IOCTL_SMARTCARD_CANCEL_BLOCKING,
                            NULL,0,NULL,0,NULL);
        Sleep(10);
    }

⌨️ 快捷键说明

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