📄 ccid.c
字号:
/*++Copyright (c) 2004 QWY MicroSystem Inc.Module Name: ccid.c Abstract: USB SmartCard Reader driver for CCID/lsCCID compatible device.Environment: kernel mode onlyNotes:Revision History: 9/15/2004: created--*/#include "uscr.h"#if 0NTSTATUS WriteUsb( IN PREADER_EXTENSION ReaderExtension, ULONG Length, IN PUCHAR Buffer ){ return UsbDoInterruptOrBulkTransfer( ReaderExtension->deviceObject, ReaderExtension->BulkOut, USBD_TRANSFER_DIRECTION_OUT, Buffer, &Length); }NTSTATUS ReadUsb( IN PREADER_EXTENSION ReaderExtension, ULONG *Length, IN PUCHAR Buffer ){ return UsbDoInterruptOrBulkTransfer( ReaderExtension->deviceObject, ReaderExtension->BulkIn, USBD_TRANSFER_DIRECTION_IN | USBD_SHORT_TRANSFER_OK, Buffer, Length);}#define DEFAULT_VOLTAGE 1 /* start with 5 volts */NTSTATUSCmdPowerOn( PREADER_EXTENSION ReaderExtension, ULONG *nlength, PUCHAR buffer ){ CCID_BULKOUT_HEADER cmd; PCCID_BULKIN_HEADER response = (PCCID_BULKIN_HEADER)buffer; NTSTATUS status; ULONG atr_len, length, count = 1; UCHAR voltage; SmartcardDebug( DEBUG_TRACE, ("CmdPowerOn: Enter\n") ); /* store length of buffer[] */ voltage = DEFAULT_VOLTAGE; cmd.bMessageType = PC_to_RDR_IccPowerOn; cmd.dwLength = 0; cmd.bSlot= 0; cmd.bSeq = ReaderExtension->bSeq++; cmd.abMspec[0] = voltage; /*bPowerSelect*/ cmd.abMspec[1] = cmd.abMspec[2] = 0; /* RFU */ status = WriteUsb(ReaderExtension, sizeof(CCID_BULKOUT_HEADER), (PUCHAR)&cmd); if (status != STATUS_SUCCESS) return STATUS_IO_TIMEOUT; /* reset available buffer size */ /* needed if we go back after a switch to ISO mode */ length = *nlength + 10; status = ReadUsb(ReaderExtension, &length, buffer); hexdump(buffer,length); if (status != STATUS_SUCCESS) return STATUS_IO_TIMEOUT; if (response->bStatus & PROCESSED_FAILED) { SmartcardDebug( DEBUG_ERROR, ("CmdPowerOn: response->bError:%d\n",response->bError) ); return STATUS_NO_MEDIA; } /* extract the ATR */ atr_len = response->dwLength; /* ATR length */ if (atr_len > length) { return STATUS_BUFFER_TOO_SMALL; } else *nlength = atr_len; RtlMoveMemory(buffer, buffer+10, atr_len); SmartcardDebug( DEBUG_TRACE, ("CmdPowerOn: Exit atr_len:%d\n",atr_len) ); return status;} NTSTATUSCmdPowerOff( PREADER_EXTENSION ReaderExtension ){ CCID_BULKOUT_HEADER cmd; PCCID_BULKIN_HEADER response = (PCCID_BULKIN_HEADER)&cmd; NTSTATUS status; ULONG length; SmartcardDebug( DEBUG_TRACE, ("CmdPowerOff: Enter\n") ); cmd.bMessageType = PC_to_RDR_IccPowerOff; cmd.dwLength = 0; cmd.bSlot= 0; cmd.bSeq = ReaderExtension->bSeq++; cmd.abMspec[0] = cmd.abMspec[1] = cmd.abMspec[2] = 0; status = WriteUsb(ReaderExtension, sizeof(CCID_BULKOUT_HEADER),(PUCHAR)&cmd); if (status != STATUS_SUCCESS) return STATUS_IO_TIMEOUT; length = sizeof(CCID_BULKIN_HEADER); status = ReadUsb(ReaderExtension, &length, (PUCHAR)&cmd); hexdump((PUCHAR)&cmd,length); if (status != STATUS_SUCCESS) return STATUS_IO_TIMEOUT; if (response->bStatus & PROCESSED_FAILED) { SmartcardDebug( DEBUG_ERROR, ("CmdPowerOff: response->bError:%d\n",response->bError) ); return STATUS_NO_MEDIA; } SmartcardDebug( DEBUG_TRACE, ("CmdPowerOff: Exit\n") ); return status;}NTSTATUSCmdGetSlotStatus( PREADER_EXTENSION ReaderExtension, PUCHAR buffer ){ CCID_BULKOUT_HEADER cmd; PCCID_BULKIN_HEADER response = (PCCID_BULKIN_HEADER)&cmd; ULONG length; NTSTATUS status; SmartcardDebug( DEBUG_TRACE, ("CmdGetSlotStatus: Enter\n") ); cmd.bMessageType = PC_to_RDR_GetSlotStatus; cmd.dwLength = 0; cmd.bSlot= 0; cmd.bSeq = ReaderExtension->bSeq++; cmd.abMspec[0] = cmd.abMspec[1] = cmd.abMspec[2] = 0; status = WriteUsb(ReaderExtension, sizeof(CCID_BULKOUT_HEADER), (PUCHAR)&cmd); if (status != STATUS_SUCCESS) return STATUS_IO_TIMEOUT; length = sizeof(CCID_BULKIN_HEADER); status = ReadUsb(ReaderExtension, &length, (PUCHAR)&cmd); hexdump((PUCHAR)&cmd,length); if (status != STATUS_SUCCESS) return STATUS_IO_TIMEOUT; if (response->bStatus & PROCESSED_FAILED) { SmartcardDebug( DEBUG_ERROR, ("CmdGetSlotStatus: response->bError:%d\n",response->bError) ); return STATUS_NO_MEDIA; } SmartcardDebug( DEBUG_TRACE, ("CmdGetSlotStatus: Exit\n") ); return status;} NTSTATUSCCID_Transmit( PREADER_EXTENSION ReaderExtension, ULONG tx_length, PUCHAR tx_buffer, UCHAR bBWI ){ NTSTATUS status; UCHAR buf[10+MIN_BUFFER_SIZE]; PCCID_BULKOUT_HEADER cmd = (PCCID_BULKOUT_HEADER)buf; SmartcardDebug( DEBUG_TRACE, ("CCID_Transmit: Enter\n") ); cmd->bMessageType = PC_to_RDR_XfrBlock; cmd->dwLength = tx_length; cmd->bSlot= 0; cmd->bSeq = ReaderExtension->bSeq++; cmd->abMspec[0] = bBWI; cmd->abMspec[1] = cmd->abMspec[2] = 0; RtlCopyMemory(buf+10, tx_buffer, tx_length); hexdump(buf, sizeof(CCID_BULKOUT_HEADER)+tx_length); status = WriteUsb(ReaderExtension, sizeof(CCID_BULKOUT_HEADER)+tx_length, buf); if (status != STATUS_SUCCESS) return STATUS_IO_TIMEOUT; SmartcardDebug( DEBUG_TRACE, ("CCID_Transmit: Exit\n") ); return status;} NTSTATUSCCID_Receive( PREADER_EXTENSION ReaderExtension, ULONG *rx_length, UCHAR rx_buffer[]){ NTSTATUS status; ULONG length; UCHAR buf[10+MIN_BUFFER_SIZE]; PCCID_BULKIN_HEADER response = (PCCID_BULKIN_HEADER)buf; SmartcardDebug( DEBUG_TRACE, ("CCID_Receive: Enter\n") );time_request: length = sizeof(buf); status = ReadUsb(ReaderExtension, &length, buf); hexdump(buf,length); if (status != STATUS_SUCCESS) return STATUS_IO_TIMEOUT; if (response->bStatus & PROCESSED_FAILED) { SmartcardDebug( DEBUG_ERROR, ("CmdPowerOn: response->bError:%d\n",response->bError) ); return STATUS_NO_MEDIA; } if (response->bStatus & CCID_TIME_EXTENSION) { SmartcardDebug( DEBUG_ERROR, ("Time extension requested\n") ); goto time_request; } length = response->dwLength; if (length < *rx_length) *rx_length = length; else length = *rx_length; RtlCopyMemory(rx_buffer,buf+10,length); SmartcardDebug( DEBUG_TRACE, ("CCID_Receive: Exit\n") ); return status;} NTSTATUSCmdXfrBlockTPDU_T0( PREADER_EXTENSION ReaderExtension, ULONG tx_length, UCHAR tx_buffer[], ULONG *rx_length, UCHAR rx_buffer[] ){ NTSTATUS status = STATUS_SUCCESS; SmartcardDebug( DEBUG_TRACE, ("CmdXfrBlockTPDU_T0: Enter\n") ); status = CCID_Transmit(ReaderExtension, tx_length, tx_buffer, 0); if (status != STATUS_SUCCESS) return status; status = CCID_Receive(ReaderExtension, rx_length, rx_buffer); SmartcardDebug( DEBUG_TRACE, ("CmdXfrBlockTPDU_T0: Exit\n") ); return status;} #endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -