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

📄 stccmd.c

📁 Windows CE 5.0 下的SmartCard驱动。
💻 C
📖 第 1 页 / 共 2 页
字号:
//
// 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) 1997 SCM Microsystems, Inc.


--*/

#include "common.h"
#include "StcCmd.h"
#include "usbcom.h"
#include "stcusbce.h"


STC_REGISTER STCInitialize[] = 
{
    { ADR_SC_CONTROL,        0x01,    0x00        },        //    reset
    { ADR_CLOCK_CONTROL,    0x01,    0x01        },
    { ADR_CLOCK_CONTROL,    0x01,    0x03        },
    { ADR_UART_CONTROL,        0x01,    0x27        },
    { ADR_UART_CONTROL,        0x01,    0x4F        },
    { ADR_IO_CONFIG,        0x01,    0x02        },        //    0x10 eva board
    { ADR_FIFO_CONFIG,        0x01,    0x81        },
    { ADR_INT_CONTROL,        0x01,    0x11        },
    { 0x0E,                    0x01,    0xC0        },
    { 0x00,                    0x00,    0x00        },
};

STC_REGISTER STCClose[] = 
{
    { ADR_INT_CONTROL,        0x01,    0x00        },
    { ADR_SC_CONTROL,        0x01,    0x00        },        //    reset
    { ADR_UART_CONTROL,        0x01,    0x40        },
    { ADR_CLOCK_CONTROL,    0x01,    0x01        },
    { ADR_CLOCK_CONTROL,    0x01,    0x00        },
    { 0x00,                    0x00,    0x00        },
};





NTSTATUS 
STCResetInterface(
    PREADER_EXTENSION    ReaderExtension)
/*++
Description:

Arguments:
    ReaderExtension        context of call

Return Value:

--*/
{
    NTSTATUS NtStatus = STATUS_SUCCESS;
    DWORD dwETU;
    
    dwETU = 0x7401 | 0x0080; 
    NtStatus=IFWriteSTCRegister(
        ReaderExtension,
        ADR_ETULENGTH15,
        2,
        (UCHAR *)&dwETU);

    return(NtStatus);
}

NTSTATUS 
STCReset( 
    PREADER_EXTENSION    ReaderExtension,
    UCHAR                Device,
    BOOLEAN                WarmReset,
    PUCHAR                pATR,
    PULONG                pATRLength)
/*++
Description:
    performs a reset of ICC

Arguments:
    ReaderExtension        context of call
    Device                device requested 
    WarmReset            kind of ICC reset
    pATR                ptr to ATR buffer, NULL if no ATR required
    pATRLength            size of ATR buffer / length of ATR

Return Value:
    STATUS_SUCCESS
    STATUS_NO_MEDIA
    STATUS_UNRECOGNIZED_MEDIA
    error values from IFRead / IFWrite

--*/
{
    NTSTATUS    NTStatus = STATUS_SUCCESS;

    //    set UART to autolearn mode
    NTStatus = STCInitUART( ReaderExtension, TRUE );    


    if( NTStatus == STATUS_SUCCESS)
    {
        //
        //    set default frequency for ATR
        //
        NTStatus = STCSetFDIV( ReaderExtension, FREQ_DIV );    

        if( NTStatus == STATUS_SUCCESS && ( !WarmReset ))
        {
            //
            //    deactivate contacts
            //
            NTStatus = STCPowerOff( ReaderExtension );
        }

        //
        //    set power to card
        //
        if( NTStatus == STATUS_SUCCESS)
        {
            NTStatus = STCPowerOn( ReaderExtension );


            if( NTStatus == STATUS_SUCCESS)
            {
                NTStatus = STCReadATR( ReaderExtension, pATR, pATRLength );

            }
        }
    }
    
    if( NTStatus != STATUS_SUCCESS )
    {
        STCPowerOff( ReaderExtension );
    }
    return( NTStatus );
}

NTSTATUS 
STCReadATR(
    PREADER_EXTENSION    ReaderExtension, 
    PUCHAR                pATR, 
    PULONG                pATRLen)
/*++
Description:
    Read and analyze the ATR

Arguments:
    ReaderExtension        context of call
    pATR                ptr to ATR buffer, 
    pATRLen                size of ATR buffer / length of ATR

Return Value:

--*/

{
    NTSTATUS    NTStatus = STATUS_SUCCESS;
    UCHAR        T0_Yx,
                T0_K,                //    number of historical bytes
                Protocol;
    ULONG        ATRLen;
    //
    //    set read timeout for ATR
    //
    ReaderExtension->ReadTimeout = 250; // only 250ms for this firs ATR
    //
    //    read TS if active low reset
    //
    NTStatus = IFReadSTCData( ReaderExtension, pATR, 1 );

    if( NTStatus == STATUS_IO_TIMEOUT )
    {
        ReaderExtension->ReadTimeout = 2500;
        NTStatus = STCSetRST( ReaderExtension, TRUE );

        if( NTStatus == STATUS_SUCCESS )
        {
            NTStatus = IFReadSTCData( ReaderExtension, pATR, 1 );
        }
    }


    Protocol    = PROTOCOL_TO;
    ATRLen        = 1;

    if( NTStatus == STATUS_SUCCESS )
    {
        //    T0
        NTStatus = IFReadSTCData( ReaderExtension, pATR + ATRLen, 1 );
        ATRLen++;
    
        /* Convention management */
        if ( pATR[0] == 0x03 )        /* Direct convention */
        {
            pATR[0] = 0x3F;
        }

        if ( ( pATR[0] != 0x3F ) && ( pATR[0] != 0x3B ) )
        {
            NTStatus = STATUS_DATA_ERROR;
        }
            
        if( NTStatus == STATUS_SUCCESS )
        {
            ULONG    Request;

            //    number of historical bytes
            T0_K = (UCHAR) ( pATR[ATRLen-1] & 0x0F );

            //    coding of TA, TB, TC, TD
            T0_Yx = (UCHAR) ( pATR[ATRLen-1] & 0xF0 ) >> 4;    

            while(( NTStatus == STATUS_SUCCESS ) && T0_Yx )    
            {    
                UCHAR Mask;

                //    evaluate presence of TA, TB, TC, TD 
                Mask    = T0_Yx;
                Request    = 0;
                while( Mask )
                {
                    if( Mask & 1 )
                    {
                        Request++;
                    }
                    Mask >>= 1;
                }
                NTStatus = IFReadSTCData( ReaderExtension, pATR + ATRLen, Request );
                ATRLen += Request;

                if( T0_Yx & TDx )
                {
                    //    high nibble of TD codes the next set of TA, TB, TC, TD
                    T0_Yx = ( pATR[ATRLen-1] & 0xF0 ) >> 4;
                    //    low nibble of TD codes the protocol
                    Protocol = pATR[ATRLen-1] & 0x0F;
                }
                else
                {
                    break;
                }
            }

            if( NTStatus == STATUS_SUCCESS )
            {
                //    historical bytes
                NTStatus = IFReadSTCData( ReaderExtension, pATR + ATRLen, T0_K );

                //    check sum
                if( NTStatus == STATUS_SUCCESS )
                {
                    ATRLen += T0_K;

                    if( Protocol == PROTOCOL_T1 )
                    {
                        NTStatus = IFReadSTCData( ReaderExtension, pATR + ATRLen, 1 );
                        if( NTStatus == STATUS_SUCCESS )
                        {
                            ATRLen++;
                        }
                        else if( NTStatus == STATUS_IO_TIMEOUT )
                        {
                            //    some cards don't support the TCK
                            NTStatus = STATUS_SUCCESS;
                        }
                    }
                }
            }
        }
    }

    if( NTStatus == STATUS_IO_TIMEOUT )
    {
        NTStatus = STATUS_UNRECOGNIZED_MEDIA;
    }

    if(( NTStatus == STATUS_SUCCESS ) && ( pATRLen != NULL ))
    {
        *pATRLen = ATRLen;
    }
    return( NTStatus );
}


NTSTATUS
STCPowerOff( 
    PREADER_EXTENSION    ReaderExtension )
/*++
Description:
    Deactivates the requested device

Arguments:
    ReaderExtension        context of call

Return Value:
    STATUS_SUCCESS
    error values from IFRead / IFWrite

--*/
{
    NTSTATUS    NTStatus = STATUS_SUCCESS;
    UCHAR        SCCtrl;

    // clear SIM
    SCCtrl=0x11;
    NTStatus=IFWriteSTCRegister(
        ReaderExtension,
        ADR_INT_CONTROL,
        1,
        &SCCtrl);
 
    SCCtrl = 0x00;
    NTStatus = IFWriteSTCRegister( ReaderExtension, ADR_SC_CONTROL, 1, &SCCtrl );

    return( NTStatus );
}

NTSTATUS
STCPowerOn( 
    PREADER_EXTENSION ReaderExtension )
/*++
Description:
    Deactivates the requested device

Arguments:
    ReaderExtension        context of call

Return Value:
    STATUS_SUCCESS
    error values from IFRead / IFWrite

--*/
{
    NTSTATUS    NTStatus = STATUS_SUCCESS;
    UCHAR        SCCtrl,Byte;

    Byte=0x02;
    NTStatus=IFWriteSTCRegister(ReaderExtension,ADR_IO_CONFIG,1,&Byte);

    SCCtrl = 0x40;            //    vcc
    NTStatus = IFWriteSTCRegister( ReaderExtension, ADR_SC_CONTROL, 1, &SCCtrl );

    if( NTStatus == STATUS_SUCCESS )
    {
        SCCtrl = 0x41;        //    vpp
        NTStatus = IFWriteSTCRegister( ReaderExtension, ADR_SC_CONTROL, 1, &SCCtrl );


        // set SIM
        SCCtrl=0x13;
        NTStatus=IFWriteSTCRegister(
            ReaderExtension,
            ADR_INT_CONTROL,
            1,
            &SCCtrl);

⌨️ 快捷键说明

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