📄 tlp3cb.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:
tlp3cb.c
Abstract:
Callback functions for smart card library
--*/
#include <stdio.h>
#include <smclib.h>
#include <devload.h>
#include <tchar.h>
#include "sci.h"
#include "TLP3CE.h"
#include "BullTLP3.h"
#define ZQ 1
NTSTATUS
TLP3ReaderPower(
PSMARTCARD_EXTENSION SmartcardExtension
)
/*++
Routine Description:
The smart card lib requires to have this function. It is called
for certain power requests to the card. We do nothing here, because
this action is performed in the StartIo function.
--*/
{
ULONG step, waitTime, numTry = 0, minWaitTime;
NTSTATUS status = STATUS_SUCCESS;
PREADER_EXTENSION pReaderExtension = SmartcardExtension->ReaderExtension;
LARGE_INTEGER liProcessClock;
SmartcardDebug(
DEBUG_TRACE,
(TEXT("%s!TLP3ReaderPower: Enter (%lx)\n"),
szDriverName,
SmartcardExtension->MinorIoControlCode)
);
SysFillMemory( SmartcardExtension->CardCapabilities.ATR.Buffer, 0x00, 0x40 );
SmartcardExtension->CardCapabilities.ATR.Length = 0;
SmartcardExtension->CardCapabilities.Protocol.Selected = SCARD_PROTOCOL_UNDEFINED;
SmartcardExtension->SmartcardReply.BufferLength =
MAXIMUM_ATR_LENGTH - SmartcardExtension->CardCapabilities.ATR.Length;
step = SmartcardExtension->MinorIoControlCode;
switch (step) {
case SCARD_WARM_RESET:
DEBUGMSG( ZQ, (TEXT("SCARD_WARM_RESET\r\n")));
if (SmartcardExtension->ReaderCapabilities.CurrentState >
SCARD_SWALLOWED) {
// Read back the echo of the reader
SmartcardExtension->ReaderExtension->SerialIoControlCode =
SMARTCARD_READ;
CardActive(SmartcardExtension,FALSE);
SmartcardExtension->ReaderExtension->CardStatus=PL131_STATE_ACTIVECARD;
status=TLP3SerialIo( SmartcardExtension);
break;
}
case SCARD_COLD_RESET:
DEBUGMSG( ZQ, (TEXT("SCARD_COLD_RESET\r\n")));
CardActive(SmartcardExtension,TRUE);
SmartcardExtension->ReaderExtension->CardStatus=PL131_STATE_ACTIVECARD;
break;
case SCARD_POWER_DOWN:
DEBUGMSG( ZQ, (TEXT("SCARD_POWER_DOWN\r\n")));
SmartcardExtension->CardCapabilities.Protocol.Selected = //SCARD_PROTOCOL_T0;
SCARD_PROTOCOL_UNDEFINED;
status = STATUS_SUCCESS;
if(SmartcardExtension->ReaderExtension->CardStatus==PL131_STATE_ACTIVECARD)
{
POWERDOWN();
}
SmartcardExtension->ReaderExtension->CardStatus=PL131_STATE_INACTIVECARD;
SmartcardExtension->ReaderCapabilities.CurrentState = SCARD_SWALLOWED ;
waitTime = 1000;
break;
}
// We now try to get the ATR as fast as possible.
// Therefor we prev. set a very short read timeout and
// expect that the card delivered its ATR within this
// short time. To verify the correctness of the ATR we call
// SmartcardUpdateCardCapabilities(). If this call returns
// with STATUS_SUCCESS we know that the ATR is complete.
// Otherwise we read again and append the new data to the
// ATR buffer in the CardCapabilities and try again.
if (SmartcardExtension->SmartcardReply.BufferLength != 0) {
ASSERT(
SmartcardExtension->CardCapabilities.ATR.Length +
SmartcardExtension->SmartcardReply.BufferLength <
MAXIMUM_ATR_LENGTH
);
if( SmartcardExtension->CardCapabilities.ATR.Length +
SmartcardExtension->SmartcardReply.BufferLength >=
MAXIMUM_ATR_LENGTH) {
status = STATUS_UNRECOGNIZED_MEDIA;
// leave;
}
// we got some ATR bytes.
RtlCopyMemory(
SmartcardExtension->CardCapabilities.ATR.Buffer +
SmartcardExtension->CardCapabilities.ATR.Length,
SmartcardExtension->SmartcardReply.Buffer,
SmartcardExtension->SmartcardReply.BufferLength
);
SmartcardExtension->CardCapabilities.ATR.Length +=
(UCHAR) SmartcardExtension->SmartcardReply.BufferLength;
status = SmartcardUpdateCardCapabilities(
SmartcardExtension
);
}
// Copy ATR to user space
if (SmartcardExtension->IoRequest.ReplyBuffer) {
RtlCopyMemory(
SmartcardExtension->IoRequest.ReplyBuffer,
SmartcardExtension->CardCapabilities.ATR.Buffer,
SmartcardExtension->CardCapabilities.ATR.Length
);
// Tell user length of ATR
*SmartcardExtension->IoRequest.Information =
SmartcardExtension->CardCapabilities.ATR.Length;
}
// Now make some adjustments depending on the system speed
if (!QueryPerformanceFrequency(&liProcessClock)) { // not supported
// ASSUME it is 66 Mhz CPU Clock
liProcessClock.HighPart=0;
liProcessClock.LowPart=66*1000000L;
};
// Minimum wait set to 5k instruction in Milli Second.
liProcessClock.LowPart/=1000; // Frequncy in Milli Second.
minWaitTime = (DWORD) ((5000L*5+liProcessClock.QuadPart/2)/(liProcessClock.QuadPart));
// We're done anyway, so leave
SmartcardDebug(
DEBUG_TRACE,
(TEXT("%s!TLP3ReaderPower: Exit (%lx)\n"),
szDriverName,
status)
);
DEBUGMSG( 1, (TEXT("<<==TLP3ReaderPower( )\r\n")));
return status;
}
NTSTATUS
TLP3SetProtocol(
PSMARTCARD_EXTENSION SmartcardExtension
)
/*++
Routine Description:
The smart card lib requires to have this function. It is called
to set a the transmission protocol and parameters. If this function
is called with a protocol mask (which means the caller doesn't card
about a particular protocol to be set) we first look if we can
set T=1 and the T=0
Arguments:
SmartcardExtension - Pointer to smart card data struct.
Return Value:
NTSTATUS
--*/
{
NTSTATUS status;
LARGE_INTEGER liProcessClock;
PREADER_EXTENSION pReaderExtension=SmartcardExtension->ReaderExtension;
SmartcardDebug(
DEBUG_TRACE,
(TEXT("%s!TLP3SetProtocol: Enter\n"),
szDriverName)
);
try {
PUCHAR ptsRequest = SmartcardExtension->SmartcardRequest.Buffer;
PUCHAR ptsReply = SmartcardExtension->SmartcardReply.Buffer;
// PSERIAL_READER_CONFIG serialConfigData =
// &SmartcardExtension->ReaderExtension->SerialConfigData;
ULONG minWaitTime, newProtocol;
//
// Check if the card is already in specific state
// and if the caller wants to have the already selected protocol.
// We return success if this is the case.
//
if (SmartcardExtension->ReaderCapabilities.CurrentState == SCARD_SPECIFIC &&
(SmartcardExtension->CardCapabilities.Protocol.Selected &
SmartcardExtension->MinorIoControlCode)) {
status = STATUS_SUCCESS;
leave;
}
//
// Assemble and send a pts selection
//
newProtocol = SmartcardExtension->MinorIoControlCode;
while(TRUE) {
// set initial character of PTS
ptsRequest[0] = 0xff;
// set the format character
if (SmartcardExtension->CardCapabilities.Protocol.Supported &
newProtocol &
SCARD_PROTOCOL_T1) {
// select T=1 and indicate that pts1 follows
ptsRequest[1] = 0x11;
SmartcardExtension->CardCapabilities.Protocol.Selected = SCARD_PROTOCOL_T1;
} else if (SmartcardExtension->CardCapabilities.Protocol.Supported &
newProtocol &
SCARD_PROTOCOL_T0) {
// select T=0 and indicate that pts1 follows
ptsRequest[1] = 0x10;
SmartcardExtension->CardCapabilities.Protocol.Selected = SCARD_PROTOCOL_T0;
} else {
status = STATUS_INVALID_DEVICE_REQUEST;
leave;
}
// set pts1 which codes Fl and Dl
ptsRequest[2] =
SmartcardExtension->CardCapabilities.PtsData.Fl << 4 |
SmartcardExtension->CardCapabilities.PtsData.Dl;
// set pck (check character)
ptsRequest[3] = ptsRequest[0] ^ ptsRequest[1] ^ ptsRequest[2];
SmartcardExtension->SmartcardRequest.BufferLength = 4;
SmartcardExtension->ReaderExtension->SerialIoControlCode = SMARTCARD_WRITE;
status = TLP3SerialIo(SmartcardExtension);
if (status != STATUS_SUCCESS) {
leave;
}
// read back the echo of the reader
SmartcardExtension->SmartcardReply.BufferLength = 4;
SmartcardExtension->ReaderExtension->SerialIoControlCode = SMARTCARD_READ;
/*????? read twice
status = TLP3SerialIo(SmartcardExtension);
if (status != STATUS_SUCCESS) {
leave;
}
*/
// read back the pts data
status = TLP3SerialIo(SmartcardExtension);
if (status != STATUS_SUCCESS &&
status != STATUS_TIMEOUT) {
leave;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -