//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
/*++
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

Module Name:  pcmciaen.h

Abstract:

Platform dependent PCMCIA initialization functions

Notes: 
*/
#ifndef __PCMCIAEN_H_
#define __PCMCIAEN_H_
#include <CRegEdit.h>
#include "PCCardEn.h"

#define MAX_WINDOWS_RANGES   4   // limit this to 4, it can be as high as 16

//
// @struct PARSED_PCMCFTABLE | Structure used by CardGetParsedTuple for parsed
// CISTPL_CFTABLE_ENTRY tuples.
//
typedef struct _PARSED_PCMCFTABLE
{
    POWER_DESCR VccDescr;   // @field Power description of Vcc
    POWER_DESCR Vpp1Descr;  // @field Power description of Vpp1
    POWER_DESCR Vpp2Descr;  // @field Power description of Vpp2

    DWORD       IOLength[MAX_WINDOWS_RANGES];    // @field IOLength[i] is the number of bytes in 
    DWORD       IOBase[MAX_WINDOWS_RANGES];      // @field the I/O address range starting at IOBase[i]
    DWORD       MemLength[MAX_WINDOWS_RANGES];    // @field IOLength[i] is the number of bytes in 
    DWORD       MemBase[MAX_WINDOWS_RANGES];      // @field the I/O address range starting at IOBase[i]

    // There are NumIOEntries in each array.
    UINT8       NumIOEntries;   // @field Number of I/O address range entries in the two arrays:
    // IOLength[] and IOBase[]
    UINT8       NumMemEntries;  // @field Number of Memory address range entries in the two arrays:
    // MemLength[] and MemBase[]

    UINT8       ConfigIndex;    // @field Value to be written to the configuration option
    // register to enable this CFTABLE_ENTRY.
    UINT8       ContainsDefaults;   // @field True if this entry contains defaults to be used by subsequent entries.

    UINT8       IFacePresent;   // @field True if the following 5 fields are valid:
    UINT8       IFaceType;      // @field 0 = memory, 1 = I/O, 2,3,8-15 = rsvd, 4-7 = custom
    UINT8       BVDActive;      // @field True if BVD1 and BVD2 are active in the Pin Replacement Register (PRR)
    UINT8       WPActive;       // @field True if the card write protect is active in the PRR
    UINT8       ReadyActive;    // @field True if READY is active in the PRR
    UINT8       WaitRequired;   // @field True if the WAIT signal is required for memory cycles

    UINT8       IOAccess;       // @field 1 = 8 bit only, 2 = 8 bit access to 16 bit registers is not supported,
    // 3 = 8 bit access to 16 bit registers is supported

    UINT8       NumIOAddrLines; // @field 0 = socket controller provides address decode
    // 1-26 = number of address lines decoded
    UINT16      wIrqMask;      // BitMask For supported IRQ;
    DWORD       dwSysIntr;      // System Interrupt ID allocated for driver.
    UINT8       bIrqNumber;     // Irq number associated with this configuration.
} PARSED_PCMCFTABLE, * PPARSED_PCMCFTABLE;

#include "PCMBus.h"

class CPCEnablePcmcia : public CPCEnableCard, public CMiniThread
{
public:
    CPCEnablePcmcia( CARD_SOCKET_HANDLE hSocket, CARD_CLIENT_HANDLE hClient );
    ~CPCEnablePcmcia();
    virtual BOOL IsCardBusCard()
    {
        return FALSE;
    };
    virtual STATUS      HandleEvent( CARD_EVENT cEvent,
                                     PCARD_EVENT_PARMS pParms );

    // Tuple Parsr
    STATUS              ParseCfTable( PVOID pBuf,
                                      DWORD dwLength,
                                      PPARSED_PCMCFTABLE pCfTable,
                                      PPARSED_PCMCFTABLE pDefaultCfTable );
private:
    //    UINT ParseVoltageDescr(  PUCHAR pVolt, PPOWER_DESCR pDescr ) ;
    //    UINT ConvertVoltage(  PUCHAR pCIS,  PUSHORT pDescr  ) ;

    // Configure & Unconfigure the card.
    BOOL                UnconfigurePcmciaCard();
    BOOL                ConfigurePcmciaCard();
    BOOL                ConfigurePcmciaCardResource( PPARSED_PCMCFTABLE curTable,
                                                     BOOL bSetIoAddrHighestBit,
                                                     DWORD dwDevFlags );
    BOOL                ReleasePcmciaCardResources( PPARSED_PCMCFTABLE curTable );
    BOOL                WriteAddressReg( DWORD dwBaseAddr, DWORD dwLength );
    BOOL                CheckPnp();
    BOOL                RegSetKey( CRegistryEdit& IKey );
    BOOL                RequestSysIntr( DWORD Irq, DWORD* pSysIntr ) const;
    BOOL                ReleaseSysIntr( DWORD SysIntr ) const;
    UINT32              VarToFixed( UINT32 VarSize, PUCHAR pBytes ) const;
#ifdef DEBUG
    BOOL                Testing();
    BOOL                DumpCfTable( PPARSED_PCMCFTABLE pcurTable );
#endif


public:
    // Driver Load and Unload.
    void                UnLoadDriver() ; 
    BOOL                LoadDriver() ; 
    // Load driver registry info
    BOOL                LocateDriverByPnP( LPTSTR DriverRegPath,
                                           DWORD dwRegLength );
    BOOL                LocateDriverByFunction( LPTSTR DriverRegPath,
                                                DWORD dwRegLength,
                                                UCHAR FuncID,
                                                PPARSED_PCMCFTABLE pCurTable );
    BOOL                MatchDriver( LPTSTR DriverRegPath,
                                     DWORD dwRegLength,
                                     PPARSED_PCMCFTABLE pCurTable );
private:
    BOOL                m_bTerminated;
    virtual DWORD       ThreadRun();

    // Driver Locate
    CPcmciaEnum*        m_pPcmciaEnum;
    HANDLE              m_hDriver;
    // IO Window Handle And Address.
    CARD_WINDOW_HANDLE  m_IoHandle[MAX_WINDOWS_RANGES];
    DWORD               m_IOPhysicalAddr[MAX_WINDOWS_RANGES];

    // Mem Window
    CARD_WINDOW_HANDLE  m_MemHandle[MAX_WINDOWS_RANGES] ;
    DWORD               m_MemPhysicalAddr[MAX_WINDOWS_RANGES];

    BOOL                m_Configured;
    BYTE                m_bIrq;
    BOOL                m_bIsSysIntr;

    TCHAR               m_DeviceId[DEVID_LEN + 1];
    TCHAR               m_DriverInstanceRegPath[DEVKEY_LEN];
    UCHAR               m_FuncID;
    PARSED_PCMCFTABLE   m_CurConfigure;

#define EVENT_NUMBER 3
#define CARD_READY 0
#define TERMINAGE_THREAD 1
#define CARD_RESUME 2
    HANDLE              m_EventList[EVENT_NUMBER];
};

#endif

