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

📄 pcsc_drv.cpp

📁 Skeleton for implementing a Windows PC/SC Smartcard Reader.
💻 CPP
📖 第 1 页 / 共 3 页
字号:

    PDEVICE_EXTENSION      pDeviceExtension     = (PDEVICE_EXTENSION)DeviceObject->DeviceExtension;
    PSMARTCARD_EXTENSION   pSmartcardExtension  =&pDeviceExtension->SmartcardExtension;
    NTSTATUS               status               = STATUS_SUCCESS;
    PIO_STACK_LOCATION     pIrpStack            = NULL;
    KdPrint(("%sVdDeviceControl\n",HEAD));

    pIrpStack = IoGetCurrentIrpStackLocation(pIrp);
    if ((status = SmartcardAcquireRemoveLock(pSmartcardExtension)))
    {
        pSmartcardExtension->IoRequest.Information = 0;
        // We should immediately return the control back, because the driver 
        // is about to unload.
    }
    else
    {
        if(pSmartcardExtension->MajorIoControlCode == IOCTL_SMARTCARD_EJECT)
        {
            KdPrint(("\t%sIOCTL_SMARTCARD_EJECT\n",HEAD)); 
        }
        if (pSmartcardExtension->MajorIoControlCode == IOCTL_SMARTCARD_POWER)
        {
            KdPrint(("\t%sIOCTL_SMARTCARD_POWER\n",HEAD)); 
            KdPrint(("\t%spIrp %X\n",HEAD,pIrp));
            if ((pIrp != NULL) && (pIrp->AssociatedIrp.SystemBuffer != NULL))
                KdPrint(("\t%sPOWER CODE - %0x\n",HEAD, *(ULONG*)pIrp->AssociatedIrp.SystemBuffer));
        }
        if (pSmartcardExtension->MajorIoControlCode == IOCTL_SMARTCARD_GET_ATTRIBUTE)
        {
            KdPrint(("\t%sIOCTL_SMARTCARD_GET_ATTRIBUTE\n",HEAD)); 
            //KdPrint(("ATTRIBUTE - %0x\n", *(ULONG*)pIrp->AssociatedIrp.SystemBuffer)); 
        }
        if (pSmartcardExtension->MajorIoControlCode == IOCTL_SMARTCARD_CONFISCATE)
        {
            KdPrint(("\t%sIOCTL_SMARTCARD_CONFISCATE\n",HEAD)); 
        }
        if (pSmartcardExtension->MajorIoControlCode == IOCTL_SMARTCARD_TRANSMIT)
        {
            KdPrint(("\t%sIOCTL_SMARTCARD_TRANSMIT\n",HEAD)); 
        }
        if (pSmartcardExtension->MajorIoControlCode == IOCTL_SMARTCARD_SWALLOW)
        {
            KdPrint(("\t%sIOCTL_SMARTCARD_SWALLOW\n",HEAD)); 
        }
        if (pSmartcardExtension->MajorIoControlCode == IOCTL_SMARTCARD_IS_PRESENT)
        {
            KdPrint(("\t%sIOCTL_SMARTCARD_IS_PRESENT\n",HEAD)); 
        }
        if (pSmartcardExtension->MajorIoControlCode == IOCTL_SMARTCARD_IS_ABSENT)
        {
            KdPrint(("\t%sIOCTL_SMARTCARD_IS_ABSENT\n",HEAD)); 
        }
        if (pSmartcardExtension->MajorIoControlCode == IOCTL_SMARTCARD_SET_PROTOCOL)
        { 
            KdPrint(("\t%sIOCTL_SMARTCARD_SET_PROTOCOL\n",HEAD)); 
            //KdPrint(("PROTOCOL - %0x\n", *(ULONG*)pIrp->AssociatedIrp.SystemBuffer)); 
        }
        if (pSmartcardExtension->MajorIoControlCode == IOCTL_SMARTCARD_GET_STATE)
        {
            KdPrint(("\t%sIOCTL_SMARTCARD_GET_STATE\n",HEAD)); 
            //KdPrint(("STATE - %#X\n", *(ULONG*)pIrp->AssociatedIrp.SystemBuffer)); 
        }
        if (pSmartcardExtension->MajorIoControlCode == IOCTL_SMARTCARD_GET_LAST_ERROR)
        {
            KdPrint(("\t%sIOCTL_SMARTCARD_GET_LAST_ERROR\n",HEAD)); 
        }
        if (pSmartcardExtension->MajorIoControlCode == IOCTL_SMARTCARD_GET_PERF_CNTR)
        {
            KdPrint(("\t%sIOCTL_SMARTCARD_GET_PERF_CNTR\n",HEAD)); 
        }
        if (status != STATUS_SUCCESS)
        {
            KdPrint(("\t%sIoControl Acquire\n",HEAD, (ULONG)status));
        }
        
        status = SmartcardDeviceControl(pSmartcardExtension, pIrp);
        SmartcardReleaseRemoveLock(pSmartcardExtension);
    }
    return status;
}

//<summary>
//  ======================================================================
//<newpara>	Author :BD		Date : 01-01-2001		</newpara>
//  ======================================================================
//<newpara>	Function :		VdDriverUnload</newpara>
//<newpara>	Comments :										</newpara>
//</summary>
//Parameters :
//	<param name="DriverObject">             				       </param>
//<returns> </returns>
// ======================================================================
void VdDriverUnload(IN PDRIVER_OBJECT DriverObject)
{
    PDEVICE_OBJECT          pDeviceObject       = DriverObject->DeviceObject;
    PDEVICE_EXTENSION       pDeviceExtension    = (PDEVICE_EXTENSION) pDeviceObject->DeviceExtension;
    KIRQL                   CancelIrql;
    PSMARTCARD_EXTENSION    pSmartcardExtension = NULL;
    PIRP                    pNotificationIrp    = NULL;
    KdPrint(("%sVdDriverUnload\n",HEAD));
#ifdef USE_SYSTEM_THREAD   
    IoFreeWorkItem(pDeviceExtension->_PIO_WORKITEM);
    ExFreePool(pDeviceExtension->pMemory_Buf);
    pDeviceExtension->pMemory_Buf = NULL;
#endif   
#ifdef USE_WORK_THREAD   
    // let's kill the auxilary WORK_THREAD
    if (DEVICE_READER_EXTENSION(pevWORK_THREAD_AWAKE) && DEVICE_READER_EXTENSION(pThreadObj))
        KillThread(pDeviceExtension);   
#endif USE_WORK_THREAD
    //	report to the lib that the device will be unloaded
    if (pDeviceExtension->SmartcardExtension.OsData != NULL)
    {
        pSmartcardExtension = &pDeviceExtension->SmartcardExtension;
        IoAcquireCancelSpinLock(&CancelIrql);        
        if (pSmartcardExtension->OsData->NotificationIrp != NULL)
        {
            pNotificationIrp = (PIRP)InterlockedExchangePointer(
                &(pSmartcardExtension->OsData->NotificationIrp),NULL);
            IoSetCancelRoutine(pNotificationIrp, NULL);
            IoReleaseCancelSpinLock(CancelIrql);
            //	finish the request
            pNotificationIrp->IoStatus.Status = STATUS_SUCCESS;
            pNotificationIrp->IoStatus.Information = 0;
            IoCompleteRequest(pNotificationIrp, IO_NO_INCREMENT);
        }
        else 
        {
            IoReleaseCancelSpinLock(CancelIrql);
        }
        // Wait until we can safely unload the device
        // SmartcardReleaseRemoveLockAndWait(SmartcardExtension);
        SmartcardExit(&pDeviceExtension->SmartcardExtension);
    }
    //	delete the symbolic link
    if (pDeviceExtension->DeviceName.Buffer != NULL)
    {
        RtlFreeUnicodeString(&pDeviceExtension->DeviceName);
        pDeviceExtension->DeviceName.Buffer = NULL;
    }
    if (pDeviceExtension->SmartcardExtension.ReaderExtension != NULL)
    {
        ExFreePool(pDeviceExtension->SmartcardExtension.ReaderExtension);
        pDeviceExtension->SmartcardExtension.ReaderExtension = NULL;
    }
    //	delete the device object
    IoDeleteDevice(pDeviceObject);
    KdPrint(("%sVdUnloadDevice: Exit\n",HEAD));
}

//<summary>
//  ======================================================================
//<newpara>	Author :BD		Date : 01-01-2001		</newpara>
//  ======================================================================
//<newpara>	Function :		VdPower</newpara>
//<newpara>	Comments :										</newpara>
//</summary>
//Parameters :
//	<param name="DeviceObject">             				       </param>
//	<param name="pIrp">                     				       </param>
//<returns> </returns>
// ======================================================================
NTSTATUS VdPower(IN PDEVICE_OBJECT DeviceObject, IN PIRP pIrp)
{
    NTSTATUS status = STATUS_SUCCESS;
    KdPrint(("%sVdPower\n",HEAD));
    pIrp->IoStatus.Information = 0;
    pIrp->IoStatus.Status = status;
    IoCompleteRequest(pIrp, IO_NO_INCREMENT);
    return status;
}

//<summary>
//  ======================================================================
//<newpara>	Author :BD		Date : 01-01-2001		</newpara>
//  ======================================================================
//<newpara>	Function :		VdCleanUp</newpara>
//<newpara>	Comments :										</newpara>
//</summary>
//Parameters :
//	<param name="DeviceObject">             				       </param>
//	<param name="pIrp">                     				       </param>
//<returns> </returns>
// ======================================================================
NTSTATUS VdCleanUp(IN PDEVICE_OBJECT DeviceObject, IN PIRP pIrp)
{
    NTSTATUS status = STATUS_SUCCESS;
    KdPrint(("%sVdCleanUp\n",HEAD));
    pIrp->IoStatus.Information = 0;
    pIrp->IoStatus.Status = status;
    IoCompleteRequest(pIrp, IO_NO_INCREMENT);
    return status;
}

//<summary>
//  ======================================================================
//<newpara>	Author :BD		Date : 01-01-2001		</newpara>
//  ======================================================================
//<newpara>	Function :		VdPowerReader</newpara>
//<newpara>	Comments :										
//input data:
//      pSmartcardExtension->MajorIoControlCode - Contains IOCTL_SMARTCARD_POWER. 
//      pSmartcardExtension->MinorIoControlCode 
//      Contains one of the following minor codes, depending on the function to be performed. 
//      pSmartcardExtension->IoRequest.ReplyBufferLength - Contains the length of the reply buffer. 
//output data:
//      pSmartcardExtension->IoRequest.ReplyBuffer - this buffer receives the ATR returned by the card. 
//      pSmartcardExtension->CardCapabilities.ATR.Buffer - transfer the ATR to let the library parse the ATR. 
//      pSmartcardExtension->IoRequest.Information - Receives the actual length of the ATR. 
//      pSmartcardExtension->CardCapabilities.ATR.Length - transfer the length of the ATR 
//</newpara>
//</summary>
//Parameters :
//	<param name="pSmartcardExtension">       				       </param>
//<returns> </returns>
// ======================================================================
NTSTATUS VdPowerReader(PSMARTCARD_EXTENSION pSmartcardExtension)
{
    NTSTATUS status;
    KdPrint(("%sVdPowerReader\n",HEAD));

    if ((status = SmartcardAcquireRemoveLock(pSmartcardExtension)) != STATUS_SUCCESS)
    {
        pSmartcardExtension->IoRequest.Information = 0;
        // We should immediately return the control back, because the driver 
        // is about to unload.
    }
    else
    {
        switch (pSmartcardExtension->MinorIoControlCode)
        {
        case SCARD_POWER_DOWN :
            KdPrint(("\t%sSCARD_POWER_DOWN\n",HEAD));
            status = VdPowerDownReader(pSmartcardExtension);
            break;
        case SCARD_COLD_RESET :
            KdPrint(("\t%sSCARD_COLD_RESET\n",HEAD));
            status = VdPowerUpReader(pSmartcardExtension);
            break;
        case SCARD_WARM_RESET :
            KdPrint(("\t%sSCARD_WARM_RESET\n",HEAD));
            status = VdPowerUpReader(pSmartcardExtension);
            break;
        default :
            KdPrint(("\t%sSomething else\n",HEAD));
            status = STATUS_UNSUCCESSFUL;
        }
        SmartcardReleaseRemoveLock(pSmartcardExtension);
    }
    return status;
}

//<summary>
//  ======================================================================
//<newpara>	Author :BD		Date : 01-01-2001		</newpara>
//  ======================================================================
//<newpara>	Function :		VdPowerUpReader</newpara>
//<newpara>	Comments :										</newpara>
//</summary>
//Parameters :
//	<param name="pSmartcardExtension">      				       </param>
//<returns> </returns>
// ======================================================================
NTSTATUS VdPowerUpReader(PSMARTCARD_EXTENSION pSmartcardExtension)
{
    NTSTATUS status = STATUS_SUCCESS;
    KdPrint(("%sVdPowerUpReader\n",HEAD));
    //----------------------------------------------
    ULONG ulState = pSmartcardExtension->ReaderCapabilities.CurrentState;
    KdPrint(("\t%sPowerUp, Current state %#x\n",HEAD, ulState));
    // Let's check the size of buffer
    if (ATRLength > pSmartcardExtension->IoRequest.ReplyBufferLength)
        status = STATUS_BUFFER_TOO_SMALL;
    else
    {
#ifdef VERSION_READER_2
        READER_EXTENSION(uReaderPowerState) = POWER_UP;
        KdPrint(("\t%spSmartcardExtension->IoRequest.ReplyBufferLength %#x\n",HEAD, pSmartcardExtension->IoRequest.ReplyBufferLength));
        RtlCopyMemory(pSmartcardExtension->IoRequest.ReplyBuffer, bATR, ATRLength);
        RtlCopyMemory(pSmartcardExtension->CardCapabilities.ATR.Buffer, bATR, ATRLength);
        *pSmartcardExtension->IoRequest.Information = ATRLength;
        pSmartcardExtension->CardCapabilities.ATR.Length = ATRLength;
        pSmartcardExtension->SmartcardReply.BufferLength = ATRLength;
        status = SmartcardUpdateCardCapabilities(pSmartcardExtension);            
#endif 
    }
    KdPrint(("\t%sStatus %#X\n",HEAD, status));       
    return status;
}

//<summary>
//  ======================================================================
//<newpara>	Author :BD		Date : 01-01-2001		</newpara>
//  ======================================================================
//<newpara>	Function :		VdPowerDownReader</newpara>
//<newpara>	Comments :										</newpara>
//</summary>
//Parameters :
//	<param name="pSmartcardExtension">      				       </param>
//<returns> </returns>
// ======================================================================
NTSTATUS VdPowerDownReader(PSMARTCARD_EXTENSION pSmartcardExtension)
{

    NTSTATUS status = STATUS_SUCCESS;
    KdPrint(("%sVdPowerDownReader\n",HEAD));

    READER_EXTENSION(uReaderPowerState) = POWER_DOWN;
#ifdef VERSION_READER_2
    if (READER_EXTENSION(uStateOfCard) == CARD_PRESENT)
        pSmartcardExtension->ReaderCapabilities.CurrentState = SCARD_PRESENT;
    else
        pSmartcardExtension->ReaderCapabilities.CurrentState = SCARD_ABSENT;
#endif   
    return status;
}

//<summary>
//  ======================================================================
//<newpara>	Author :BD		Date : 01-01-2001		</newpara>
//  ======================================================================
//<newpara>	Function :		VdSetProtocol</newpara>
//<newpara>	Comments :										</newpara>
//</summary>
//Parameters :
//	<param name="pSmartcardExtension">       				       </param>
//<returns> </returns>
// ======================================================================
NTSTATUS VdSetProtocol(PSMARTCARD_EXTENSION pSmartcardExtension)
{
    NTSTATUS    status = STATUS_SUCCESS;

⌨️ 快捷键说明

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