//
// 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:  CBCardEn.h

Abstract:

Platform dependent PCMCIA initialization functions

Notes: 
--*/
#ifndef __CBCARDEN_H_
#define __CBCARDEN_H_
#include "PCCardEn.h"

class CPCIEnum;

typedef struct
{
    TCHAR   m_DeviceId[DEVID_LEN + 1];
    DWORD   ManfID;
    UCHAR   FuncID;
    UCHAR   Major_Version;
    UCHAR   Minor_Version;
    BYTE    m_BARType[PCI_TYPE0_ADDRESSES];
    DWORD   m_BARSize[PCI_TYPE0_ADDRESSES];
} CBCARD_INFO, * PCBCARD_INFO;

typedef struct
{
    PCI_RSRC_LIST   MemLen;
    PCI_RSRC_LIST   IoLen;
    BYTE            bIrq;
} RESOURCE_REQUIREMENT;


class CPCEnableCBCard : public CPCEnableCard, public CMiniThread
{
public:
    CPCEnableCBCard( CARD_SOCKET_HANDLE hSocket, CARD_CLIENT_HANDLE hClient );
    ~CPCEnableCBCard();
    virtual BOOL IsCardBusCard()
    {
        return TRUE;
    };
    virtual STATUS  HandleEvent( CARD_EVENT cEvent, PCARD_EVENT_PARMS pParms );

private:
    // Tuple Parsr
    STATUS          ParseCfTable( PVOID pBuf,
                                  DWORD dwLength,
                                  PPARSED_CBCFTABLE pCfTable,
                                  PPARSED_CBCFTABLE pDefaultCfTable );

    UINT            ParseVoltageDescr( PUCHAR pVolt, PPOWER_DESCR pDescr ) ;
    UINT            ConvertVoltage( PUCHAR pCIS, PUSHORT pDescr ) ;

    DWORD PCIConfig_Read( DWORD dwOffset )
    {
        DWORD   RetVal = 0;
        HalGetBusDataByOffset( PCIConfiguration,
                               m_dwBusNumber,
                               m_SlotNumber.u.AsULONG,
                               &RetVal,
                               dwOffset,
                               sizeof( RetVal ) );
        return RetVal;
    }

    void PCIConfig_Write( DWORD dwOffset, DWORD dwValue )
    {
        HalSetBusDataByOffset( PCIConfiguration,
                               m_dwBusNumber,
                               m_SlotNumber.u.AsULONG,
                               &dwValue,
                               dwOffset,
                               sizeof( dwValue ) );
    }
    void    CreateDefaultInfo( PPARSED_CBCFTABLE pCfTable );
    // Configure & Unconfigure the card.
    BOOL    UnconfigureCBCard();
    BOOL    ConfigureCBCard();
private:
    BOOL    ConfigureCBCardResource( PPARSED_CBCFTABLE curTable );
    BOOL    MapBar( DWORD dwIndex );
    BOOL    UnMapBar( DWORD dwIndex );


public:
    // Driver Load and Unload.
    void    UnLoadDriver() ; 
    BOOL    LoadDriver() ; 
    // Load driver registry info
    BOOL    LoadDeviceRegistry() ;
    BOOL IsSysIntrOnly()
    {
        return m_bIsSysIntr;
    };
private:
    BOOL                    m_bTerminated;
    virtual DWORD           ThreadRun();

    CPCIEnum*               m_pPCIEnum;

    BOOL                    m_Configured;
    BYTE                    m_bIrq;
    BOOL                    m_bIsSysIntr;
    CBCARD_INFO             m_CBCardCfgInfo;
    RESOURCE_REQUIREMENT    m_RequiredResources;
    CARD_WINDOW_HANDLE      m_WinHandle[PCI_TYPE0_ADDRESSES];


#define EVENT_NUMBER 2
#define CARD_READY 0
#define TERMINAGE_THREAD 1
    HANDLE                  m_EventList[EVENT_NUMBER];
};

#endif
