/*******************************************************************************
** Document: xmgr_bcm_utils.c
** Copyright (c) 2005 R&D Center, Tailyn Communication Company, Ltd.  
** Created by: Chien-Chung Lo
** Date: Mar. 16, 2005
** Description: The BROADCOM utility.
** Version: 0.1
*******************************************************************************/
#include <unistd.h>
#include <string.h>

#include "xmgr_bcm_utils.h"
#include "debug_msg.h"
#include "xmgr_proc_fun.h"
#include "xmgr_proc.h"
#include "xmgr.h"

#include "umap_type.h"
#include "umap_global.h"
#include "sharememory.h"

#include <FW_cardinfo/FW_cardinfo_api.h>

#include "bcm_genericOptions.h"
#include "bcm_utils.h"
#include "bcm_devObject.h"
#include "bcm_lineObject.h"
#include "bcm_hmiCoreServices.h"
#include "bcm_hmiLineServices.h"
#include "bcm_errorHandler.h"
#include "bcm_globalObject.h"
#include "bcm_transfer.h"
#include "bcm_loadImages.h"
#include "bcm_boot.h"
# include "bcm_hostFunctions.h"
# include "bcm_interrupt.h"

#define TEST	0

typedef enum
{
    ADSL24_LINE_MODE_01_ANSI_T1413                            =(1UL<<  0),
    ADSL24_LINE_MODE_01_ETSI_DTS_TM06006                      =(1UL<<  1),
    ADSL24_LINE_MODE_01_992_1_A_Pots_NonOverlapped            =(1UL<<  2),
    ADSL24_LINE_MODE_01_992_1_A_Pots_Overlapped               =(1UL<<  3),
    ADSL24_LINE_MODE_01_992_1_B_Isdn_NonOverlapped            =(1UL<<  4),
    ADSL24_LINE_MODE_01_992_1_B_Isdn_Overlapped               =(1UL<<  5),
    ADSL24_LINE_MODE_01_992_1_C_TcmIsdn_NonOverlapped         =(1UL<<  6),
    ADSL24_LINE_MODE_01_992_1_C_TcmIsdn_Overlapped            =(1UL<<  7),
    ADSL24_LINE_MODE_01_992_2_A_Pots_NonOverlapped            =(1UL<<  8),
    ADSL24_LINE_MODE_01_992_2_B_Pots_Overlapped               =(1UL<<  9),
    ADSL24_LINE_MODE_01_992_2_C_TcmIsdn_NonOverlapped         =(1UL<< 10),
    ADSL24_LINE_MODE_01_992_2_C_TcmIsdn_Overlapped            =(1UL<< 11),
//                                                            =(1UL<< 12),
//                                                            =(1UL<< 13),
//                                                            =(1UL<< 14),
//                                                            =(1UL<< 15),
//                                                            =(1UL<< 16),
//                                                            =(1UL<< 17),
    ADSL24_LINE_MODE_01_992_3_A_Pots_NonOverlapped            =(1UL<< 18),
    ADSL24_LINE_MODE_01_992_3_A_Pots_Overlapped               =(1UL<< 19),
    ADSL24_LINE_MODE_01_992_3_B_Isdn_NonOverlapped            =(1UL<< 20),
    ADSL24_LINE_MODE_01_992_3_B_Isdn_Overlapped               =(1UL<< 21),
//                                                            =(1UL<< 22),
//                                                            =(1UL<< 23),
    ADSL24_LINE_MODE_01_992_4_A_Pots_NonOverlapped            =(1UL<< 24),
    ADSL24_LINE_MODE_01_992_4_A_Pots_Overlapped               =(1UL<< 25),
//                                                            =(1UL<< 26),
//                                                            =(1UL<< 27),
    ADSL24_LINE_MODE_01_992_3_I_AllDigital_NonOverlapped      =(1UL<< 28),
    ADSL24_LINE_MODE_01_992_3_I_AllDigital_Overlapped         =(1UL<< 29),
    ADSL24_LINE_MODE_01_992_3_J_AllDigital_NonOverlapped      =(1UL<< 30),
    ADSL24_LINE_MODE_01_992_3_J_AllDigital_Overlapped         =(1UL<< 31)
} ADSL24_LINE_OP_MODE_01_ENUM;

typedef enum
{
    ADSL24_LINE_MODE_02_992_4_I_AllDigital_NonOverlapped      =(1UL<<  0),
    ADSL24_LINE_MODE_02_992_4_I_AllDigital_Overlapped         =(1UL<<  1),
    ADSL24_LINE_MODE_02_992_3_L_Pots_NonOverlapped_Mode1      =(1UL<<  2),
    ADSL24_LINE_MODE_02_992_3_L_Pots_NonOverlapped_Mode2      =(1UL<<  3),
    ADSL24_LINE_MODE_02_992_3_L_Pots_Overlapped_Mode3         =(1UL<<  4),
    ADSL24_LINE_MODE_02_992_3_L_Pots_Overlapped_Mode4         =(1UL<<  5),
    ADSL24_LINE_MODE_02_992_3_M_Pots_Extend_US_Overlapped     =(1UL<<  6),
    ADSL24_LINE_MODE_02_992_3_M_Pots_Extend_US_NonOverlapped  =(1UL<<  7),
    ADSL24_LINE_MODE_02_992_5_A_Pots_NonOverlapped            =(1UL<<  8),
    ADSL24_LINE_MODE_02_992_5_A_Pots_Overlapped               =(1UL<<  9),
    ADSL24_LINE_MODE_02_992_5_B_Isdn_NonOverlapped            =(1UL<< 10),
    ADSL24_LINE_MODE_02_992_5_B_Isdn_Overlapped               =(1UL<< 11),
//                                                            =(1UL<< 12),
//                                                            =(1UL<< 13),
    ADSL24_LINE_MODE_02_992_5_I_AllDigital_NonOverlapped      =(1UL<< 14),
    ADSL24_LINE_MODE_02_992_5_I_AllDigital_Overlapped         =(1UL<< 15),
    ADSL24_LINE_MODE_02_ANSI_T1424                            =(1UL<< 16),
    ADSL24_LINE_MODE_02_ETSI_TS_101_270                       =(1UL<< 17),
    ADSL24_LINE_MODE_02_993_1                                 =(1UL<< 18),
    ADSL24_LINE_MODE_02_IEEE_8023ah                           =(1UL<< 19),
//                                                            =(1UL<< 20),
//                                                            =(1UL<< 21),
//                                                            =(1UL<< 22),
//                                                            =(1UL<< 23),
    ADSL24_LINE_MODE_02_992_5_J_AllDigital_NonOverlapped      =(1UL<< 24),
    ADSL24_LINE_MODE_02_992_5_J_AllDigital_Overlapped         =(1UL<< 25),
    ADSL24_LINE_MODE_02_992_5_M_Pots_Extend_US_NonOverlapped  =(1UL<< 26),
    ADSL24_LINE_MODE_02_992_5_M_Pots_Extend_US_Overlapped     =(1UL<< 27)
//                                                            =(1UL<< 28),
//                                                            =(1UL<< 29),
//                                                            =(1UL<< 30),
//                                                            =(1UL<< 31)
} ADSL24_LINE_OP_MODE_02_ENUM;

#define LINE_PROTOCOL_OVERLAP_COMMON   (LINE_MODE_MASK_ANSI | LINE_MODE_MASK_ETSI | \
                                        LINE_MODE_MASK_T1_VDSL | LINE_MODE_MASK_ETSI_VDSL)


//#include "bcm_hmiQA.h"
//#include "bcm_message.h"

// Card Port ReMapping //
UMAP_U8 PORTS_MAP[24] = { 11, 10,  9,  8,  7,  6,  5,  4,  3,  2,  1,  0,
                   23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12};

//----------------------------------------------------------------------
UMAP_S32 ResetAndInitialCard(UMAP_U32 cardNo)
{
	LT_host_bus_enable(cardNo);
	
	if(InitialLineCard(cardNo) != BCM_SUCCESS)
	{
		MSG("Initial Line card fail!!\n");
		return BCM_FAILURE;
	}

	return BCM_SUCCESS;
}

//----------------------------------------------------------------------
UMAP_S32 ConfigureProfile(UMAP_U32 cardNo, UMAP_U32 portNo)
{
	SetLineTrafficConfigurationMsgReq traffic;
    SetLinePhysicalLayerConfigurationMsgReq physicalLayer;
    SetLineTestConfigurationMsgReq testConfiguration;
    SetLineOAMthresholdsMsgReq lineOAMthresholds;
	UMAP_U32 bitMapNonOverlap=0x0;
    UMAP_U32 bitMapOverlap=0x0;
    UMAP_U32 boardCapFilter_01=0x0;
    UMAP_U32 boardCapFilter_02=0x0;
	UMAP_U8 i;
    LineObject *linePtr = getLinePtr(CALC_LINE_NO(cardNo, portNo));
	struct UMAP_Database_S *g_pDb;
	
	MSG("card %d  port %d\n", cardNo, portNo);
	MSG("LINE_MANAGER_ID(linePtr->lineId)):%d   linePtr->lineId:%d\n", (UMAP_U32)(LINE_MANAGER_ID(linePtr->lineId)), (UMAP_U32)linePtr->lineId);


	g_pDb = (struct UMAP_Database_S *)open_sharememory(KEY_DATABASE);
	if(g_pDb == NULL)
	{
		MSG("Open Share Memory Failed!\n");
		return BCM_FAILURE;
	}
	
	ProcessSetProtocol(((g_pDb->arrayXdslLineSpectrumProfile[portNo+1].xdslLineOpMode_01)&(boardCapFilter_01)),
						((g_pDb->arrayXdslLineSpectrumProfile[portNo+1].xdslLineOpMode_02)&(boardCapFilter_02)),
                        &bitMapNonOverlap, &bitMapOverlap);
	
	/* BCM  */
    /* first reset the line */
    if (CALL_HMIQA_NPL(ResetLine, linePtr->devPtr, LINE_MANAGER_ID(linePtr->lineId)) != BCM_SUCCESS)
	{
		//FATAL_ERROR("Couldn't reset the line %d", linePtr->lineId); //??
		MSG("Couldn't reset the line %d\n", (UMAP_U32)linePtr->lineId);
	}

    /* Traffic configuration */
    memset((void *) &traffic, 0, sizeof(traffic));
	
#ifdef TEST	

	g_pDb->arrayXdslLineServiceProfileChannel[portNo+1].xdslPlannedBitrateDownstream = 32;
	g_pDb->arrayXdslLineServiceProfileChannel[portNo+1].xdslMinBitrateDownstream = 32;
	g_pDb->arrayXdslLineServiceProfileChannel[portNo+1].xdslMaxBitrateDownstream =  32000;
	g_pDb->arrayXdslLineServiceProfileChannel[portNo+1].xdslMaxInterleavingDelayDownstream =20;
	g_pDb->arrayXdslLineServiceProfileChannel[portNo+1].xdslMinImpNoiseProtDownstream = 1;
	g_pDb->arrayXdslLineServiceProfileChannel[portNo+1].xdslRARatioDownstream = 100;
	
	g_pDb->arrayXdslLineServiceProfileChannel[portNo+1].xdslPlannedBitrateUpstream = 32;
	g_pDb->arrayXdslLineServiceProfileChannel[portNo+1].xdslMinBitrateUpstream = 32;
	g_pDb->arrayXdslLineServiceProfileChannel[portNo+1].xdslMaxBitrateUpstream = 2048;
	g_pDb->arrayXdslLineServiceProfileChannel[portNo+1].xdslMaxInterleavingDelayUpstream = 20;
	g_pDb->arrayXdslLineServiceProfileChannel[portNo+1].xdslMinImpNoiseProtUpstream = 1;
	g_pDb->arrayXdslLineServiceProfileChannel[portNo+1].xdslRARatioUpstream = 100;

#endif // end TEST
	
	/* Service Profile */
	if(g_pDb->arrayXdslLineServiceProfile[portNo+1].xdslLineRAModeDownstream==XDSLLINERAMODEDOWNSTREAM_OPERATORCONTROLLEDRATEADAPTIVE)
    {
        traffic.bearer[B0].config[DS].minBitRate = (g_pDb->arrayXdslLineServiceProfileChannel[portNo+1].xdslPlannedBitrateDownstream/8)*8;
        traffic.bearer[B0].config[DS].maxBitRate = traffic.bearer[B0].config[DS].minBitRate;
    }
    else
    {
        traffic.bearer[B0].config[DS].minBitRate = (g_pDb->arrayXdslLineServiceProfileChannel[portNo+1].xdslMinBitrateDownstream/8)*8;
        traffic.bearer[B0].config[DS].maxBitRate = (g_pDb->arrayXdslLineServiceProfileChannel[portNo+1].xdslMaxBitrateDownstream/8)*8;
    }
    traffic.bearer[B0].config[DS].maxDelay   = g_pDb->arrayXdslLineServiceProfileChannel[portNo+1].xdslMaxInterleavingDelayDownstream;
    traffic.bearer[B0].config[DS].INPmin     = g_pDb->arrayXdslLineServiceProfileChannel[portNo+1].xdslMinImpNoiseProtDownstream;
    traffic.bearer[B0].config[DS].ratio_BC   = g_pDb->arrayXdslLineServiceProfileChannel[portNo+1].xdslRARatioDownstream;
	
	if(g_pDb->arrayXdslLineServiceProfile[portNo+1].xdslLineRAModeUpstream==XDSLLINERAMODEUPSTREAM_OPERATORCONTROLLEDRATEADAPTIVE)
    {
        traffic.bearer[B0].config[US].minBitRate = g_pDb->arrayXdslLineServiceProfileChannel[portNo+1].xdslPlannedBitrateUpstream;
        traffic.bearer[B0].config[US].maxBitRate = traffic.bearer[B0].config[US].minBitRate;
    }
    else
    {
        traffic.bearer[B0].config[US].minBitRate = g_pDb->arrayXdslLineServiceProfileChannel[portNo+1].xdslMinBitrateUpstream;
        traffic.bearer[B0].config[US].maxBitRate = g_pDb->arrayXdslLineServiceProfileChannel[portNo+1].xdslMaxBitrateUpstream;
    }
    traffic.bearer[B0].config[US].maxDelay   = g_pDb->arrayXdslLineServiceProfileChannel[portNo+1].xdslMaxInterleavingDelayUpstream;
    traffic.bearer[B0].config[US].INPmin     = g_pDb->arrayXdslLineServiceProfileChannel[portNo+1].xdslMinImpNoiseProtUpstream;
    traffic.bearer[B0].config[US].ratio_BC   = g_pDb->arrayXdslLineServiceProfileChannel[portNo+1].xdslRARatioUpstream;

	traffic.bearer[B0].alpha                 = 7; //Fixed
    traffic.bearer[B0].delta                 = 6;
    traffic.bearer[B0].isEnabled             = 1; //Fixed
    //traffic.bearer[B0].minL2BitRate          = g_pDb->arrayXdslLineServiceProfileChannel[portNo+1].xdslL2LowPowerBitrateDownstream;
    traffic.bearer[B0].L2LowRateSlots        = 10; //Fixed

    if(CALL_HMIQA_REQ(SetLineTrafficConfiguration, linePtr->devPtr, LINE_MANAGER_ID(linePtr->lineId), &traffic)!= BCM_SUCCESS)
        MSG("Config SetLineTrafficConfiguration profile Error!!\n");

	/* Physical layer configuration */
    memset((void *) &physicalLayer, 0, sizeof(physicalLayer));

    physicalLayer.linkConfig[DS].targetMarginDB             = g_pDb->arrayXdslLineSpectrumProfile[portNo+1].xdslLineTargetNoiseMarginDownstream;
    //physicalLayer.linkConfig[DS].maxAddMarginDB             = (ADSLProfileDB[cardNo][portNo].xdslLineSpectrumProfile.xdslLineMaxNoiseMarginDownstream
    //                                                            - ADSLProfileDB[cardNo][portNo].xdslLineSpectrumProfile.xdslLineTargetNoiseMarginDownstream);
    physicalLayer.linkConfig[DS].minMarginDB                = g_pDb->arrayXdslLineSpectrumProfile[portNo+1].xdslLineMinNoiseMarginDownstream;
//    physicalLayer.linkConfig[DS].rateAdaptationFlags        = ADSLProfileDB[cardNo][portNo].xdslLineServiceProfile.xdslLineRAModeDownstream;
    physicalLayer.linkConfig[DS].rateAdaptationFlags        = 1;

    physicalLayer.linkConfig[DS].downShiftNoiseMargin       = 0; //Fixed
    physicalLayer.linkConfig[DS].minimumDownshiftRAInterval = 0; //Fixed
    physicalLayer.linkConfig[DS].upShiftNoiseMargin         = 0; //Fixed
    physicalLayer.linkConfig[DS].minimumUpshiftRAInterval   = 0; //Fixed

    physicalLayer.linkConfig[US].targetMarginDB             = g_pDb->arrayXdslLineSpectrumProfile[portNo+1].xdslLineTargetNoiseMarginUpstream;
    //physicalLayer.linkConfig[US].maxAddMarginDB             = (ADSLProfileDB[cardNo][portNo].xdslLineSpectrumProfile.xdslLineMaxNoiseMarginUpstream
    //                                                             - ADSLProfileDB[cardNo][portNo].xdslLineSpectrumProfile.xdslLineTargetNoiseMarginUpstream);
    physicalLayer.linkConfig[US].minMarginDB                = g_pDb->arrayXdslLineSpectrumProfile[portNo+1].xdslLineMinNoiseMarginUpstream;
//    physicalLayer.linkConfig[US].rateAdaptationFlags        = ADSLProfileDB[cardNo][portNo].xdslLineServiceProfile.xdslLineRAModeUpstream;
    physicalLayer.linkConfig[US].rateAdaptationFlags        = 1;
    physicalLayer.linkConfig[US].downShiftNoiseMargin       = 0; //Fixed
    physicalLayer.linkConfig[US].minimumDownshiftRAInterval = 0; //Fixed
    physicalLayer.linkConfig[US].upShiftNoiseMargin         = 0; //Fixed
    physicalLayer.linkConfig[US].minimumUpshiftRAInterval   = 0; //Fixed

//    physicalLayer.maxRxPower                = ADSLProfileDB[cardNo][portNo].xdslLineSpectrumProfileAdslAdsl2.adslMaxRxAggregatePowerLevelUpstream;
//    physicalLayer.linkConfig[DS].maxTxPower = ADSLProfileDB[cardNo][portNo].xdslLineSpectrumProfileAdslAdsl2.adslMaxAggregatePowerLevelDownstream;
//    physicalLayer.linkConfig[US].maxTxPower = ADSLProfileDB[cardNo][portNo].xdslLineSpectrumProfileAdslAdsl2.adslMaxAggregatePowerLevelUpstream;

    //physicalLayer.fixedPSD          = (ADSLProfileDB[cardNo][portNo].xdslLineSpectrumProfileAdslAdsl2.adslPboModeUpstream==0)?0:1;
    physicalLayer.fixedPSD          = 1;
    physicalLayer.powerAdaptivity   = (g_pDb->arrayXdslLineSpectrumProfileAdslAdsl2[portNo+1].adslPboModeUpstream==1)?1:0;

    if(bitMapOverlap&(~LINE_PROTOCOL_OVERLAP_COMMON)) //Current support NonOverlap
    {
        physicalLayer.optionFlags = 1; //Enable OverLapping
        //Adsl24PortOverLap[cardNo][portNo] = 1;
    }
    physicalLayer.protocolSpecificLinkConfig[DS].maxTxDeltaPsd[PSD_NORMAL] = g_pDb->arrayXdslLineSpectrumProfileAdslAdsl2[portNo+1].adslMaxPsdLevelDownstream - PHY_STANDARD_DS_PSD_NORMAL;
    physicalLayer.protocolSpecificLinkConfig[DS].maxTxDeltaPsd[PSD_PLUS] = g_pDb->arrayXdslLineSpectrumProfileAdslAdsl2[portNo+1].adslMaxPsdLevelDownstream - PHY_STANDARD_DS_PSD_PLUS;
    physicalLayer.protocolSpecificLinkConfig[DS].maxTxDeltaPsd[PSD_REACH] = g_pDb->arrayXdslLineSpectrumProfileAdslAdsl2[portNo+1].adslMaxPsdLevelDownstream - PHY_STANDARD_DS_PSD_REACH;
    physicalLayer.protocolSpecificLinkConfig[US].maxTxDeltaPsd[PSD_NORMAL] = g_pDb->arrayXdslLineSpectrumProfileAdslAdsl2[portNo+1].adslMaxPsdLevelUpstream - PHY_STANDARD_US_PSD_NORMAL;
    physicalLayer.protocolSpecificLinkConfig[US].maxTxDeltaPsd[PSD_PLUS] = g_pDb->arrayXdslLineSpectrumProfileAdslAdsl2[portNo+1].adslMaxPsdLevelUpstream - PHY_STANDARD_US_PSD_PLUS;
    physicalLayer.protocolSpecificLinkConfig[US].maxTxDeltaPsd[PSD_REACH] = g_pDb->arrayXdslLineSpectrumProfileAdslAdsl2[portNo+1].adslMaxPsdLevelUpstream - PHY_STANDARD_US_PSD_REACH;
    physicalLayer.adslPlusTxFilterId    = 0;//??

    memcpy(physicalLayer.carrierMaskUp, g_pDb->arrayXdslLineSpectrumProfile[portNo+1].xdslLineCarrierMaskUpstream, sizeof(physicalLayer.carrierMaskUp));
    memcpy(physicalLayer.carrierMaskDn, g_pDb->arrayXdslLineSpectrumProfile[portNo+1].xdslLineCarrierMaskDownstream, sizeof(physicalLayer.carrierMaskDn));
    for (i=0; i<sizeof(physicalLayer.carrierMaskUp); i++) physicalLayer.carrierMaskUp[i]=(~physicalLayer.carrierMaskUp[i]);
    for (i=0; i<sizeof(physicalLayer.carrierMaskDn); i++) physicalLayer.carrierMaskDn[i]=(~physicalLayer.carrierMaskDn[i]);

    physicalLayer.automodePIalpha   = 4; //Fixed
    physicalLayer.automodePIbeta    = 1; //Fixed
    physicalLayer.L0Time    = g_pDb->arrayXdslLineSpectrumProfile[portNo+1].xdslLineL0Time;

    physicalLayer.L2Time    = g_pDb->arrayXdslLineSpectrumProfile[portNo+1].xdslLineL2Time;
    physicalLayer.L2Atpr    = g_pDb->arrayXdslLineSpectrumProfile[portNo+1].xdslLineL2Atpr;
    physicalLayer.L2Atprt   = 10; //Fixed

    if(CALL_HMIQA_REQ(SetLinePhysicalLayerConfiguration, linePtr->devPtr, LINE_MANAGER_ID(linePtr->lineId), &physicalLayer)!= BCM_SUCCESS)
        MSG("Config SetLinePhysicalLayerConfiguration profile Error!!\n");

	/* Set Line Test Configuration */
    memset((void *) &testConfiguration, 0, sizeof(testConfiguration));

//    if(physicalLayer.optionFlags)
//        testConfiguration.disableProtocolBitmap = ~bitMapOverlap;
//    else
//        testConfiguration.disableProtocolBitmap = ~bitMapNonOverlap;

//    testConfiguration.disableNitro          = ADSLProfileDB[cardNo][portNo].xdslLineSpectrumProfileAdslAdsl2.adslModemFeatures;

//    if((ADSLProfileDB[cardNo][portNo].xdslLineSpectrumProfile.xdslLinePowerMgtMode&ADSL24_L2_ALLOWED)==0)
//        testConfiguration.L2TestConfig      = 1;//Disable L2 Mode

    if(CALL_HMIQA_REQ(SetLineTestConfiguration, linePtr->devPtr, LINE_MANAGER_ID(linePtr->lineId), &testConfiguration)!= BCM_SUCCESS)
        MSG("\nConfig SetLineTestConfiguration profile Error!!");

	/* Set Line OAM thresholds */
    memset((void *) &lineOAMthresholds, 0, sizeof(lineOAMthresholds));

    if(g_pDb->arrayXdslLineTcaThreshold[portNo+1].xdslLineTcaEnable)
    {
        lineOAMthresholds.threshold15min[NE].ESthreshold    = g_pDb->arrayXdslLineTcaThreshold[portNo+1].xdslLineIntervalESsTcaThreshold;
        lineOAMthresholds.threshold15min[NE].SESthreshold   = g_pDb->arrayXdslLineTcaThreshold[portNo+1].xdslLineIntervalSESsTcaThreshold;
        lineOAMthresholds.threshold15min[NE].UASthreshold   = g_pDb->arrayXdslLineTcaThreshold[portNo+1].xdslLineIntervalUASsTcaThreshold;

        lineOAMthresholds.threshold24hour[NE].ESthreshold    = g_pDb->arrayXdslLineTcaThreshold[portNo+1].xdslLine1DayESsTcaThreshold;
        lineOAMthresholds.threshold24hour[NE].SESthreshold   = g_pDb->arrayXdslLineTcaThreshold[portNo+1].xdslLine1DaySESsTcaThreshold;
        lineOAMthresholds.threshold24hour[NE].UASthreshold   = g_pDb->arrayXdslLineTcaThreshold[portNo+1].xdslLine1DayUASsTcaThreshold;

        lineOAMthresholds.threshold15min[FE].ESthreshold    = g_pDb->arrayXdslLineTcaThreshold[portNo+1].xdslFarEndLineIntervalESsTcaThreshold;
        lineOAMthresholds.threshold15min[FE].SESthreshold   = g_pDb->arrayXdslLineTcaThreshold[portNo+1].xdslFarEndLineIntervalSESsTcaThreshold;
        lineOAMthresholds.threshold15min[FE].UASthreshold   = g_pDb->arrayXdslLineTcaThreshold[portNo+1].xdslFarEndLineIntervalUASsTcaThreshold;

        lineOAMthresholds.threshold24hour[FE].ESthreshold    = g_pDb->arrayXdslLineTcaThreshold[portNo+1].xdslFarEndLine1DayESsTcaThreshold;
        lineOAMthresholds.threshold24hour[FE].SESthreshold   = g_pDb->arrayXdslLineTcaThreshold[portNo+1].xdslFarEndLine1DaySESsTcaThreshold;
        lineOAMthresholds.threshold24hour[FE].UASthreshold   = g_pDb->arrayXdslLineTcaThreshold[portNo+1].xdslFarEndLine1DayUASsTcaThreshold;
    }
		//For test
//    lineOAMthresholds.threshold15min[NE].ESthreshold    = 1;
//    lineOAMthresholds.threshold15min[NE].SESthreshold   = 2;
//    lineOAMthresholds.threshold15min[NE].UASthreshold   = 3;
//
//    lineOAMthresholds.threshold24hour[NE].ESthreshold    = 1;
//    lineOAMthresholds.threshold24hour[NE].SESthreshold   = 2;
//    lineOAMthresholds.threshold24hour[NE].UASthreshold   = 3;
//
//    lineOAMthresholds.threshold15min[FE].ESthreshold    = 11;
//    lineOAMthresholds.threshold15min[FE].SESthreshold   = 22;
//    lineOAMthresholds.threshold15min[FE].UASthreshold   = 33;
//
//    lineOAMthresholds.threshold24hour[FE].ESthreshold    = 11;
//    lineOAMthresholds.threshold24hour[FE].SESthreshold   = 22;
//    lineOAMthresholds.threshold24hour[FE].UASthreshold   = 33;

    if(CALL_HMIQA_REQ(SetLineOAMthresholds, linePtr->devPtr, LINE_MANAGER_ID(linePtr->lineId), &lineOAMthresholds)!= BCM_SUCCESS)
		MSG("\nConfig SetLineOAMthresholds profile Error!!");

	return BCM_SUCCESS;
}

void ProcessSetProtocol(UMAP_U32 opMode01, UMAP_U32 opMode02, UMAP_U32* bitMapNonOverlap, UMAP_U32* bitMapOverlap)
{

    if(opMode01&ADSL24_LINE_MODE_01_ANSI_T1413)
        { *bitMapNonOverlap|=LINE_MODE_MASK_ANSI; *bitMapOverlap|=LINE_MODE_MASK_ANSI; }
    if(opMode01&ADSL24_LINE_MODE_01_ETSI_DTS_TM06006)
        { *bitMapNonOverlap|=LINE_MODE_MASK_ETSI; *bitMapOverlap|=LINE_MODE_MASK_ETSI; }
    if(opMode01&ADSL24_LINE_MODE_01_992_1_A_Pots_NonOverlapped)
        *bitMapNonOverlap|=LINE_MODE_MASK_992_1_A;
    if(opMode01&ADSL24_LINE_MODE_01_992_1_A_Pots_Overlapped)
        *bitMapOverlap|=LINE_MODE_MASK_992_1_A;
    if(opMode01&ADSL24_LINE_MODE_01_992_1_B_Isdn_NonOverlapped)
        *bitMapNonOverlap|=LINE_MODE_MASK_992_1_B;
    if(opMode01&ADSL24_LINE_MODE_01_992_1_B_Isdn_Overlapped)
        *bitMapOverlap|=LINE_MODE_MASK_992_1_B;
    if(opMode01&ADSL24_LINE_MODE_01_992_1_C_TcmIsdn_NonOverlapped)
        *bitMapNonOverlap|=LINE_MODE_MASK_992_1_C;
    if(opMode01&ADSL24_LINE_MODE_01_992_1_C_TcmIsdn_Overlapped)
        *bitMapOverlap|=LINE_MODE_MASK_992_1_C;
    if(opMode01&ADSL24_LINE_MODE_01_992_2_A_Pots_NonOverlapped)
        *bitMapNonOverlap|=LINE_MODE_MASK_992_2_A;
    if(opMode01&ADSL24_LINE_MODE_01_992_2_B_Pots_Overlapped)
        *bitMapOverlap|=LINE_MODE_MASK_992_2_A;
    if(opMode01&ADSL24_LINE_MODE_01_992_2_C_TcmIsdn_NonOverlapped)
        *bitMapNonOverlap|=LINE_MODE_MASK_992_2_C;
    if(opMode01&ADSL24_LINE_MODE_01_992_2_C_TcmIsdn_Overlapped)
        *bitMapOverlap|=LINE_MODE_MASK_992_2_C;
    if(opMode01&ADSL24_LINE_MODE_01_992_3_A_Pots_NonOverlapped)
        *bitMapNonOverlap|=LINE_MODE_MASK_992_3_A;
    if(opMode01&ADSL24_LINE_MODE_01_992_3_A_Pots_Overlapped)
        *bitMapOverlap|=LINE_MODE_MASK_992_3_A;
    if(opMode01&ADSL24_LINE_MODE_01_992_3_B_Isdn_NonOverlapped)
        *bitMapNonOverlap|=LINE_MODE_MASK_992_3_B;
    if(opMode01&ADSL24_LINE_MODE_01_992_3_B_Isdn_Overlapped)
        *bitMapOverlap|=LINE_MODE_MASK_992_3_B;
    if(opMode01&ADSL24_LINE_MODE_01_992_4_A_Pots_NonOverlapped)
        *bitMapNonOverlap|=LINE_MODE_MASK_992_4_A;
    if(opMode01&ADSL24_LINE_MODE_01_992_4_A_Pots_Overlapped)
        *bitMapOverlap|=LINE_MODE_MASK_992_4_A;
    if(opMode01&ADSL24_LINE_MODE_01_992_3_I_AllDigital_NonOverlapped)
        *bitMapNonOverlap|=LINE_MODE_MASK_992_3_I;
    if(opMode01&ADSL24_LINE_MODE_01_992_3_I_AllDigital_Overlapped)
        *bitMapOverlap|=LINE_MODE_MASK_992_3_I;
    if(opMode01&ADSL24_LINE_MODE_01_992_3_J_AllDigital_NonOverlapped)
        *bitMapNonOverlap|=LINE_MODE_MASK_992_3_M;
    if(opMode01&ADSL24_LINE_MODE_01_992_3_J_AllDigital_Overlapped)
        *bitMapOverlap|=LINE_MODE_MASK_992_3_M;

    if(opMode02&ADSL24_LINE_MODE_02_992_4_I_AllDigital_NonOverlapped)
        *bitMapNonOverlap|=LINE_MODE_MASK_992_4_I;
    if(opMode02&ADSL24_LINE_MODE_02_992_4_I_AllDigital_Overlapped)
        *bitMapOverlap|=LINE_MODE_MASK_992_4_I;
    if(opMode02&ADSL24_LINE_MODE_02_992_3_L_Pots_NonOverlapped_Mode1)
        *bitMapNonOverlap|=LINE_MODE_MASK_992_3_L1;
    if(opMode02&ADSL24_LINE_MODE_02_992_3_L_Pots_NonOverlapped_Mode2)
        *bitMapNonOverlap|=LINE_MODE_MASK_992_3_L2;
    if(opMode02&ADSL24_LINE_MODE_02_992_3_L_Pots_Overlapped_Mode3)
        *bitMapOverlap|=LINE_MODE_MASK_992_3_L1;
    if(opMode02&ADSL24_LINE_MODE_02_992_3_L_Pots_Overlapped_Mode4)
        *bitMapOverlap|=LINE_MODE_MASK_992_3_L2;
    if(opMode02&ADSL24_LINE_MODE_02_992_3_M_Pots_Extend_US_Overlapped)
        *bitMapOverlap|=LINE_MODE_MASK_992_3_M;
    if(opMode02&ADSL24_LINE_MODE_02_992_3_M_Pots_Extend_US_NonOverlapped)
        *bitMapNonOverlap|=LINE_MODE_MASK_992_3_M;
    if(opMode02&ADSL24_LINE_MODE_02_992_5_A_Pots_NonOverlapped)
        *bitMapNonOverlap|=LINE_MODE_MASK_992_5_A;
    if(opMode02&ADSL24_LINE_MODE_02_992_5_A_Pots_Overlapped)
        *bitMapOverlap|=LINE_MODE_MASK_992_5_A;
    if(opMode02&ADSL24_LINE_MODE_02_992_5_B_Isdn_NonOverlapped)
        *bitMapNonOverlap|=LINE_MODE_MASK_992_5_B;
    if(opMode02&ADSL24_LINE_MODE_02_992_5_B_Isdn_Overlapped)
        *bitMapOverlap|=LINE_MODE_MASK_992_5_B;
    if(opMode02&ADSL24_LINE_MODE_02_992_5_I_AllDigital_NonOverlapped)
        *bitMapNonOverlap|=LINE_MODE_MASK_992_5_I;
    if(opMode02&ADSL24_LINE_MODE_02_992_5_I_AllDigital_Overlapped)
        *bitMapOverlap|=LINE_MODE_MASK_992_5_I;
    if(opMode02&ADSL24_LINE_MODE_02_ANSI_T1424)
        { *bitMapNonOverlap|=LINE_MODE_MASK_T1_VDSL; *bitMapOverlap|=LINE_MODE_MASK_T1_VDSL; }
    if(opMode02&ADSL24_LINE_MODE_02_ETSI_TS_101_270)
        { *bitMapNonOverlap|=LINE_MODE_MASK_ETSI_VDSL; *bitMapOverlap|=LINE_MODE_MASK_ETSI_VDSL; }
// No support VDSL //
//    if(opMode02&ADSL24_LINE_MODE_02_993_1)
//        { *bitMapNonOverlap|=0x0; *bitMapOverlap|=0x0; }
//    if(opMode02&ADSL24_LINE_MODE_02_IEEE_8023ah)
//        { *bitMapNonOverlap|=0x0; *bitMapOverlap|=0x0; }
/////////////////////
    if(opMode02&ADSL24_LINE_MODE_02_992_5_J_AllDigital_NonOverlapped)
        *bitMapNonOverlap|=LINE_MODE_MASK_992_5_M;
    if(opMode02&ADSL24_LINE_MODE_02_992_5_J_AllDigital_Overlapped)
        *bitMapOverlap|=LINE_MODE_MASK_992_5_M;
    if(opMode02&ADSL24_LINE_MODE_02_992_5_M_Pots_Extend_US_NonOverlapped)
        *bitMapNonOverlap|=LINE_MODE_MASK_992_5_M;
    if(opMode02&ADSL24_LINE_MODE_02_992_5_M_Pots_Extend_US_Overlapped)
        *bitMapOverlap|=LINE_MODE_MASK_992_5_M;

}




//----------------------------------------------------------------------
// unexpectedLineError
void unexpectedLineError(int lineId) 
{
	DevObject  *devPtr  = getLinePtr(lineId)->devPtr;

	if (CALL_HMIQA_NPL(ResetLine, devPtr, LINE_MANAGER_ID(lineId)) != BCM_SUCCESS)
	{
		//FATAL_ERROR("HMI communication problem with service %s", ResetLineLayout.name);
		MSG("HMI communication problem with service %s\n", ResetLineLayout.name);
	}	

	/* and print reset action */
	MSG("... unexpected line error, reseting.\n");
}

//----------------------------------------------------------------------
void auto_run(UMAP_U8 cardNo)
{
    /* message definitions */
    GetLineStatusMsgRep   lineStatusRep;
    GetLineCountersMsgRep lineCountersRep;
	
	LineObject *linePtr;
	DevObject  *devPtr;

	//UMAP_S32 lineId;
	UMAP_S32 lineNumber = -1;
	
	UMAP_U8 devId, lineId, realLineID, maxdevId;
	
	UMAP_U8 temp[4];
	
	LT_host_bus_enable(cardNo);
	//LT_reset(cardNo);
	
	transferReq(DEV_ADDRESS(cardNo*2), BCM_READ, 0x4250, temp, 4);
		
	MSG("card %d::dsp  chip ID:%x \n",cardNo , *(UMAP_U32 *)&temp);
		
	LT_unit_led(cardNo, UNIT_LED_GREEN);
	LT_link_led(cardNo, LINK_LED_RED);
	
	InitialLineCard(cardNo);
	LT_link_led(cardNo, LINK_LED_GREEN);
	LT_unit_led(cardNo, UNIT_LED_RED);
	//portNo = PORTS_MAP[0];
	//MSG("\n Start Card %d, Line %d !!!", CARD1, portNo);
	//SetBcmStartLine(CARD1, portNo);
	LT_utopia_enable(cardNo);

	/* infinite loop for line manager configuration, start and monitoring */
	while (1)
	{
    
		MSG("\nLine Status   AS  ES UAS  CV HEC  BEC  AS  ES UAS  CV HEC RAT[DS] RAT[US]\n");

		devId = (cardNo * 2);	/* C/D= 0-1, 2-3, 4-5, 6-7, 8-9 */
		maxdevId = devId + 1;	/* C/D=   1,   3,   5,   7,   9 */
		
		for ( ; devId <= maxdevId; devId++)
		{

		/* loop on all lines */
		for (lineId = devId*BCM_DEV_NB_LINES;
			lineId < min((devId+1)*BCM_DEV_NB_LINES, NB_LINES);
			lineId++)
		{
		
			
			realLineID = (devId * 12) + PORTS_MAP[lineId % 12];
			
			linePtr = &lineObjects[realLineID];
			devPtr  = linePtr->devPtr;
            
			/* only loop on one line if lineNumber option given */
			if (lineNumber != -1 && lineNumber != realLineID) continue;
      
			MSG("%2d %8x ", lineId, (unsigned int)getLinePtr(realLineID));
			fflush(stdout);

			if (CALL_HMIQA_REP(GetLineStatus, devPtr, LINE_MANAGER_ID(realLineID),
                         &lineStatusRep) != BCM_SUCCESS)
			{
				/* only thing we can do here is to reset the line */
				unexpectedLineError(realLineID);
      
				/* and reset the line status to process it further */
				lineStatusRep.state = 0;
			}

			/* process the line status */
			switch (lineStatusRep.state)
			{
				/* {{{ Idle not configured */
			case LINE_STATUS_IDL_NCON: 

				MSG("IDLE_NCO ");
				fflush(stdout);

				/* configure the line for both traffic and physical layer */
				if (ConfigureLine(linePtr) != BCM_SUCCESS)
				{
					/* configuration failed => try to reset line and skip */
					unexpectedLineError(realLineID);
					continue;
				}

				MSG("... configuration successfull.\n");
				break;
				/* }}} */

				/* {{{ Idle configured */
			case LINE_STATUS_IDL_CONF:

				MSG("IDLE_CON ");
				fflush(stdout);
				LT_link_led(cardNo, LINK_LED_YELLOW);
				LT_unit_led(cardNo, UNIT_LED_YELLOW);

				/* start line */
				if (CALL_HMIQA_NPL(StartLine, devPtr,
                             LINE_MANAGER_ID(realLineID)) != BCM_SUCCESS)
				{
					/* only thing we can do here is to reset the line */
					unexpectedLineError(realLineID);
					continue;
				}
            
				MSG("... line is started.\n");
				break;
				/* }}} */

				/* {{{ running activation */
			case LINE_STATUS_RUN_ACTI:
            
				MSG("RUN_ACTI\n");
				/* do nothing, wait for next state */
				break;
				/* }}} */

				/* {{{ running initialization */
			case LINE_STATUS_RUN_INIT:
            
				MSG("RUN_INIT\n");
				/* do nothing, wait for next state */
				break;
				/* }}} */

				/* {{{ running showtime */
			case LINE_STATUS_RUN_SHOW:
			case LINE_STATUS_RUN_SHOW_L2:
            
				MSG("SHOWTIME ");
				fflush(stdout);

				/* get line counters */
				if (CALL_HMIQA_REP(GetLineCounters, devPtr, LINE_MANAGER_ID(realLineID),
                             &lineCountersRep) != BCM_SUCCESS)
				{
					/* only thing we can do here is to reset the line */
					unexpectedLineError(realLineID);
					continue;
				}
            
				/* print different counters */
				MSG("%3ld %3ld %3ld %3ld -%3ld %4ld %4ld %4ld %4ld- -%3ld %4ld %4ld %4ld %4ld-%3ld %3ld %3ld %3ld %3ld %7d %7d\n",
						lineCountersRep.derivedCounts[NE].AS,
						lineCountersRep.derivedCounts[NE].ES,
						lineCountersRep.derivedCounts[NE].UAS,
						lineCountersRep.adslAnomalyCounts[B0].ne.CV,
						lineCountersRep.atmPerfCounts[B0].ne.hecErrors,
						lineCountersRep.atmPerfCounts[B0].ne.idleCellBitErrors,
						lineCountersRep.atmPerfCounts[B0].ne.totalCells,
						lineCountersRep.atmPerfCounts[B0].ne.totalUserCells,
						lineCountersRep.atmPerfCounts[B0].ne.overflowCells,
						lineCountersRep.atmPerfCounts[B0].fe.hecErrors,
						lineCountersRep.atmPerfCounts[B0].fe.idleCellBitErrors,
						lineCountersRep.atmPerfCounts[B0].fe.totalCells,
						lineCountersRep.atmPerfCounts[B0].fe.totalUserCells,
						lineCountersRep.atmPerfCounts[B0].fe.overflowCells,
						lineCountersRep.derivedCounts[FE].AS,
						lineCountersRep.derivedCounts[FE].ES,
						lineCountersRep.derivedCounts[FE].UAS,
						lineCountersRep.adslAnomalyCounts[B0].fe.CV,
						lineCountersRep.atmPerfCounts[B0].fe.hecErrors,
						lineStatusRep.linkStatus[DS].bearer[B0].actualBitRate,
						lineStatusRep.linkStatus[US].bearer[B0].actualBitRate);

				break;
				/* }}} */

				default:
				/* either test or unkown state => reset */
				unexpectedLineError(realLineID);
				continue;
				break;
			}
		} /* loop on NB_LINES lines */

		}
		sleep(2);                   /* wait some time between passes */
	} /* while loop */
}

//----------------------------------------------------------------------
void SetBcmStartLine(UMAP_U8 cardNo, UMAP_U8 portNo)
{
    //LineObject *linePtr = getLinePtr(CALC_LINE_NO(cardNo, portNo));
	LineObject *linePtr = getLinePtr(CALC_LINE_NO(cardNo, portNo));
    DevObject  *devPtr  = linePtr->devPtr;


	/* start line */
    if (CALL_HMIQA_NPL(StartLine, devPtr, LINE_MANAGER_ID(linePtr->lineId)) != BCM_SUCCESS)
    {
        /* only thing we can do here is to reset the line */
        if (CALL_HMIQA_NPL(ResetLine, devPtr, LINE_MANAGER_ID(linePtr->lineId)) != BCM_SUCCESS)
		{
            //FATAL_ERROR("HMI communication problem with service %s", ResetLineLayout.name);
			MSG("HMI communication problem with service %s\n", ResetLineLayout.name);
		}
        
        MSG("\nStart Line Fail !!");
        return;
    }
    MSG("\nStart Line Success !!");
    
	return;
}

//----------------------------------------------------------------------
// showBcmDspSelfTestOutput: print out failing test
//----------------------------------------------------------------------
void showBcmDspSelfTestOutput(SelfTest *selfTest)
{
  int i;
  uint32 mask;

  for (i=0, mask=1; i<32; i++, mask<<=1)
  {
    if (selfTest->progress & mask)
      MSG("POST output: test %i did not complete or has not been started\n", 
             i);
    else if (selfTest->results & mask)
      MSG("POST output: test %i failed\n", i);
  }
}
  
//----------------------------------------------------------------------
// initBcmDev: load images and boot BCM6410 if requested
//----------------------------------------------------------------------
UMAP_S32 initBcmDspDev(DevObject *devPtr)
{
	static UMAP_S32 imagesLoaded = 0;
	extern UMAP_S32 skipBoot;
	SelfTest selfTest;
  
	hostDriverInit();

	/* booting is done via bootp protocol for ipdslam */
	if (!skipBoot)
	{
		static TargetImage bootImage, fwImage;
    
		if (!imagesLoaded)
		{
			loadImages(&bootImage, &fwImage);
			imagesLoaded = 1;
		}
    
		switch (boot(&bootImage, &fwImage, devPtr->devId, &selfTest))
		// process boot service result
		{
        case BCM_SUCCESS:       /* nothing to do */
			MSG("Device %d: Initialize Success.\n", devPtr->devId);
			return BCM_SUCCESS;

        case BCM_BOOTER_NO_START:
			if ((selfTest.progress | selfTest.results) != 0)
			{
				showBcmDspSelfTestOutput(&selfTest);
				//FATAL_ERROR("Device %d: booter did not start, and POST failed", devPtr->devId);
				MSG("Device %d: booter did not start, and POST failed\n", devPtr->devId);
				return BCM_FAILURE;
			}
			else
			{
				//FATAL_ERROR("Device %d: booter did not start, while POST passed", devPtr->devId);
				MSG("Device %d: booter did not start, while POST passed\n", devPtr->devId);
				return BCM_FAILURE;
			}

        case BCM_BOOTER_POST_FAILED:
			showBcmDspSelfTestOutput(&selfTest);
			//FATAL_ERROR("Device %d: booter did start, while POST failed", devPtr->devId);
			MSG("Device %d: booter did start, while POST failed\n", devPtr->devId);
			return BCM_FAILURE;

        case BCM_DOWNLOAD_PROBLEM:
			//FATAL_ERROR("Device %d: downloading firmware image failed", devPtr->devId);
			MSG("Device %d: downloading firmware image failed\n", devPtr->devId);
			return BCM_FAILURE;
			
        case BCM_FIRMWARE_NO_START:
			if ((selfTest.progress | selfTest.results) != 0)
			{
				showBcmDspSelfTestOutput(&selfTest);
				//FATAL_ERROR("Device %d: firmware did not start, and POST failed", devPtr->devId);
				MSG("Device %d: firmware did not start, and POST failed\n", devPtr->devId);
				return BCM_FAILURE;
			}
			else
			{
				//FATAL_ERROR("Device %d: firmware did not start, while POST passed", devPtr->devId);
				MSG("Device %d: firmware did not start, while POST passed\n", devPtr->devId);
				return BCM_FAILURE;
			}

        case BCM_FIRMWARE_POST_FAILED:
			showBcmDspSelfTestOutput(&selfTest);
			//FATAL_ERROR("Device %d: firmware did start, while POST failed", devPtr->devId);
			MSG("Device %d: firmware did start, while POST failed\n", devPtr->devId);
			return BCM_FAILURE;
			
		default:
			return BCM_FAILURE;
		} 
	}
	else
	{   
		return BCM_FAILURE;
	}
	return BCM_SUCCESS;
}

//----------------------------------------------------------------------
UMAP_S32 ConfigureBcmDSP(DevObject *devPtr)
{
	SetDeviceConfigurationMsgReq deviceConfigReq;
	UMAP_U8 lineId, realLineUtopiaAddr;
  
	// Check if config exists
	if (CALL_HMIQA_REP(GetDeviceConfiguration, devPtr, CORE_MANAGER_ID,
                     &deviceConfigReq) != HMI_RESULT_CANT_PROCESS_REQUEST)
	{ 
		/* device config can only be set once, so when the get device config */
		/* does not return a 'cant_process_request', meaning there is already a */
		/* config, we cannot reconfigure. */
		MSG("Device %d is already configured, keeping current config\n",
           devPtr->devId);
		return BCM_SUCCESS;
	}
  
	// Send new config
	MSG("Configuring device %d.\n", devPtr->devId);
  
	/* reset message */
	memset((void *) &deviceConfigReq, 0, sizeof(deviceConfigReq));

	/* First disable all lines (necessary in case there are more physical */
	/* lines than logical lines, or when using pco build) */
	for (lineId=0; lineId < BCM_DEV_NB_LINES; lineId++)
	{
		deviceConfigReq.lineConfig[lineId].bearerConfig[B0].utopiaAddr = -1;
		deviceConfigReq.lineConfig[lineId].bearerConfig[B1].utopiaAddr = -1;
	}
	
	/* Now, use logical line id to fill in the configuration data for the */
	/* device (as utopia addresses are given here) */
	for (lineId=devPtr->devId*BCM_DEV_NB_LINES;
		lineId < min((devPtr->devId+1)*BCM_DEV_NB_LINES, NB_LINES);
		lineId++)
	{
		//realLineUtopiaAddr = ((devPtr->devId) * 12) + PORTS_MAP[BCM_DEV_LINE_ID(lineId)];
		realLineUtopiaAddr = ((devPtr->devId)%2 * 12) + PORTS_MAP[BCM_DEV_LINE_ID(lineId)];

		MSG("line:%d   Utopia address:%d\n", lineId, realLineUtopiaAddr);
		deviceConfigReq.lineConfig[BCM_DEV_LINE_ID(lineId)].bearerConfig[B0].utopiaAddr = realLineUtopiaAddr;
		deviceConfigReq.lineConfig[BCM_DEV_LINE_ID(lineId)].bearerConfig[B1].utopiaAddr = -1;
	}
	
	deviceConfigReq.enableUtopiaFlushing = 0;
  
	/* in-band control: autonomous messages will use (VP,VC)=(0,400) */
	deviceConfigReq.controlUtopiaAddr      =  -1; /* -2 keep current setting */
	deviceConfigReq.utopia16bitBusSelect   =   0; /* 8 bit utopia bus */
	deviceConfigReq.autoMsgATMheader[0]    =   0;
	deviceConfigReq.autoMsgATMheader[1]    =   0;
	deviceConfigReq.autoMsgATMheader[2]    =  25;
	deviceConfigReq.autoMsgATMheader[3]    =   2;
	deviceConfigReq.autoMsgATMheader[4]    = 177;
	deviceConfigReq.autonomousRef          =   1;
	deviceConfigReq.txPowerOptimizationIlv = 128;
	
	deviceConfigReq.autoMsgConfig		   =   2;

	/* calibration */
	deviceConfigReq.txPowerCalibration     = BCM_TX_POWER_CALIBRATION;
	deviceConfigReq.rxPowerCalibration     = BCM_RX_POWER_CALIBRATION;

#ifdef BCM_MAX_HW_OUTPUT_POWER
	/* overwrite default value if requested */
	deviceConfigReq.hwMaxOutputPower = BCM_MAX_HW_OUTPUT_POWER;
#endif

	if (CALL_HMIQA_REQ(SetDeviceConfiguration, devPtr, CORE_MANAGER_ID,
		&deviceConfigReq) != BCM_SUCCESS)
		/* device can't be configured => return anomaly */
		return BCM_FAILURE;

	// Set the proper line driver power modes
#ifdef BCM_SPECIAL_LINE_DRIVER_SETTING
	{
		SetLineDriverPowerModesMsgReq lineDriverPowerModes;
    
		BCM_SPECIAL_LINE_DRIVER_SETTING(lineDriverPowerModes);

		if (CALL_HMIQA_REQ(SetLineDriverPowerModes, devPtr, CORE_MANAGER_ID,
                       &lineDriverPowerModes) != BCM_SUCCESS)
			/* device can't be configured => return anomaly */
			return BCM_FAILURE;
	}
#endif

	return BCM_SUCCESS;
}

//----------------------------------------------------------------------
UMAP_S32 InitialLineCard(UMAP_U8 cardNo)
{
	DevObject *devPtr;
	UMAP_U8 devId, lineId, maxdevId;
	UMAP_U8 chipId[4];
//	UMAP_U8 chipOkFlag = 0;

	devId = (cardNo * 2);	/* C/D= 0-1, 2-3, 4-5, 6-7, 8-9 */
	maxdevId = devId + 1;	/* C/D=   1,   3,   5,   7,   9 */
	

	for ( ; devId <= maxdevId; devId++)
	{
		transferReq(DEV_ADDRESS(devId), BCM_READ, 0x4250, &chipId[3], 1);
		transferReq(DEV_ADDRESS(devId), BCM_READ, 0x4251, &chipId[2], 1);
		transferReq(DEV_ADDRESS(devId), BCM_READ, 0x4252, &chipId[1], 1);
		transferReq(DEV_ADDRESS(devId), BCM_READ, 0x4253, &chipId[0], 1);
		if(*(UMAP_U32 *)&chipId != BCM6411_CHIP_ID) //check chipId.
		{
			MSG("The card %d dsp %d Chip Id is incorrect!!\n", cardNo, devId%2);
			return BCM_FAILURE;
		}
		MSG("The card %1d dsp %1d Chip Id:%8x\n",cardNo , devId%2, *(UMAP_U32 *)&chipId);
	
		devPtr = getDevPtr(devId);

		/* init dev & line objects */
		initDevObject(devId);
		for (lineId = devId*BCM_DEV_NB_LINES;
		lineId < min((devId+1)*BCM_DEV_NB_LINES, NB_LINES);
		lineId++)
		{
			initLineObject(lineId, devPtr);
		}
		
		/* boot the device */
		if(initBcmDspDev(devPtr) != BCM_SUCCESS)
		{
			MSG("The card %1d dsp %1d boot fail!!\n", cardNo , devId%2);
			return BCM_FAILURE;
		}

		/* open the hmi driver */
		devPtr->hmiDev = hmiOpen(devId);
		hmiInit(devPtr->hmiDev);
		devPtr->devState = DEVICE_HMI_READY;

		/* get image info (same for all dev -> only get for devId 0) */
		GetBcmDspFirmwareInfo(devPtr);
		
		/* Configure the Core Manager application */
		if (ConfigureBcmDSP(devPtr) != BCM_SUCCESS)
		{
			//FATAL_ERROR("Can't configure the device %d", devId);
			MSG("Can't configure the device %d\n", devId);
			return BCM_FAILURE;
		}
	}
	return BCM_SUCCESS;
}

//----------------------------------------------------------------------
// Get firmware image info
UMAP_S32 GetBcmDspFirmwareInfo(DevObject *devPtr)
{
	CALL_HMIQA_REQ(GetFirmwareVersionInfo, devPtr, CORE_MANAGER_ID,
                 &devPtr->fwVersion);
  
	MSG("Chip Id: %lx\n", devPtr->fwVersion.hwChipId);
	MSG("ADSL firmware release: %02d.%02d.%02d\n",
         devPtr->fwVersion.fwMajorVersion, devPtr->fwVersion.fwMinorVersion,
         devPtr->fwVersion.fwFixVersion);
	MSG("HMI protocol: %d.%d\n",
         devPtr->fwVersion.hmiMajorVersion, devPtr->fwVersion.hmiMinorVersion);
	MSG("Firmware info: %s\n", devPtr->fwVersion.info);

	if (strstr(devPtr->fwVersion.info, "Annex C") != NULL) 
		return ANNEX_C;
	else if (strstr(devPtr->fwVersion.info, "Annex I") != NULL) 
		return ANNEX_I;
	else if (strstr(devPtr->fwVersion.info, "Annex A") != NULL) 
		return ANNEX_A;
	else 
		return ANNEX_B;
}

//----------------------------------------------------------------------
void LT_host_bus_enable(UMAP_U8 cardNo)
{
	switch(cardNo)
	{
	case CARD0:
		FW_SetBpHostBusControl0(LT_ENABLE);
		break;
		
	case CARD1:
		FW_SetBpHostBusControl1(LT_ENABLE);
		break;
		
	case CARD2:	
		FW_SetBpHostBusControl2(LT_ENABLE);
		break;
		
	case CARD3:	
		FW_SetBpHostBusControl3(LT_ENABLE);
		break;
		
	case CARD4:
		FW_SetBpHostBusControl4(LT_ENABLE);
		break;
	}

}

//----------------------------------------------------------------------
void LT_host_bus_disable(UMAP_U8 cardNo)
{
	switch(cardNo)
	{
	case CARD0:
		FW_SetBpHostBusControl0(LT_DISABLE);
		break;
		
	case CARD1:
		FW_SetBpHostBusControl1(LT_DISABLE);
		break;
		
	case CARD2:	
		FW_SetBpHostBusControl2(LT_DISABLE);
		break;
		
	case CARD3:	
		FW_SetBpHostBusControl3(LT_DISABLE);
		break;
		
	case CARD4:
		FW_SetBpHostBusControl4(LT_DISABLE);
		break;
	}

}

//----------------------------------------------------------------------
/*
void LT_reset(UMAP_U8 cardNo)
{
	switch(cardNo)
	{
	case CARD0:
		FW_SetBpSwRst0(LT_RESET);
		sleep(1);
		FW_SetBpSwRst0(LT_NORMAL);
		break;
		
	case CARD1:
		FW_SetBpSwRst1(LT_RESET);
		sleep(1);
		FW_SetBpSwRst1(LT_NORMAL);
		break;
		
	case CARD2:	
		FW_SetBpSwRst2(LT_RESET);
		sleep(1);
		FW_SetBpSwRst2(LT_NORMAL);
		break;
		
	case CARD3:	
		FW_SetBpSwRst3(LT_RESET);
		sleep(1);
		FW_SetBpSwRst3(LT_NORMAL);
		break;
		
	case CARD4:
		FW_SetBpSwRst4(LT_RESET);
		sleep(1);
		FW_SetBpSwRst4(LT_NORMAL);
		break;
	}

}
*/

//----------------------------------------------------------------------
void LT_link_led(UMAP_U8 cardNo, UMAP_U8 led)
{
	FW_SetLtDslLed(cardNo, led);
}

//----------------------------------------------------------------------
void LT_unit_led(UMAP_U8 cardNo, UMAP_U8 led)
{
	FW_SetLtSystemLed(cardNo, led);
}

//----------------------------------------------------------------------
void LT_utopia_enable(UMAP_U8 cardNo)
{
	UMAP_U8 i = 0;
	
	MSG("LT %d UTOPIA Bus endable\n", cardNo);

	switch(cardNo)
	{
	case CARD0:
		transferReq(CARD0_UTOPIA_ENABLE_ADDR, BCM_WRITE, 0, &i, 1);
		break;
		
	case CARD1:
		transferReq(CARD1_UTOPIA_ENABLE_ADDR, BCM_WRITE, 0, &i, 1);
		break;
		
	case CARD2:
		transferReq(CARD2_UTOPIA_ENABLE_ADDR, BCM_WRITE, 0, &i, 1);
		break;
		
	case CARD3:	
		transferReq(CARD3_UTOPIA_ENABLE_ADDR, BCM_WRITE, 0, &i, 1);
		break;
		
	case CARD4:
		transferReq(CARD4_UTOPIA_ENABLE_ADDR, BCM_WRITE, 0, &i, 1);
		break;
	}
}

//----------------------------------------------------------------------
void LT_utopia_disable(UMAP_U8 cardNo)
{
	UMAP_U8 i = 1;
	
	MSG("LT %d UTOPIA Bus disable\n", cardNo);

	switch(cardNo)
	{
	case CARD0:
		transferReq(CARD0_UTOPIA_ENABLE_ADDR, BCM_WRITE, 0, &i, 1);
		break;
		
	case CARD1:
		transferReq(CARD1_UTOPIA_ENABLE_ADDR, BCM_WRITE, 0, &i, 1);
		break;
		
	case CARD2:
		transferReq(CARD2_UTOPIA_ENABLE_ADDR, BCM_WRITE, 0, &i, 1);
		break;
		
	case CARD3:	
		transferReq(CARD3_UTOPIA_ENABLE_ADDR, BCM_WRITE, 0, &i, 1);
		break;
		
	case CARD4:
		transferReq(CARD4_UTOPIA_ENABLE_ADDR, BCM_WRITE, 0, &i, 1);
		break;
	}
}
//----------------------------------------------------------------------

