/******************************************************************************/
/*                 Copyright C 1998-2003 Broadcom Corporation                 */
/*                                                                            */
/* This material is the confidential trade secret and proprietary information */
/* of Broadcom Corporation.  It may not be reproduced, used, sold or          */
/* transferred to any third party without the prior written consent of        */
/* Broadcom Corporation.   All rights reserved.                               */
/******************************************************************************/
/*   FileName        : bcm_configureLine.c                                    */
/*   Purpose         : Generic line configuration function                    */
/*   Limitations     : None                                                   */
/*   Author          : Emmanuel Hanssens                                      */
/*   Creation Date   : 25-Feb-2003                                            */
/*   History         : Compiled by CVS                                        */
/******************************************************************************/
/*   $Id: bcm_configureLine.c,v 1.16 2004/09/27 15:33:11 hanssens Exp $  */
/******************************************************************************/

#include <stdio.h>
#include <string.h>
#include "bcm_utils.h"
#include "bcm_devObject.h"
#include "bcm_lineObject.h"
#include "bcm_hmiLineServices.h"
#include "bcm_errorHandler.h"
#include "bcm_psdToolbox.h"

/* {{{ ConfigureLine */
int ConfigureLine(LineObject *linePtr)
{
  SetLineTrafficConfigurationMsgReq lineTraffic;
  SetLinePhysicalLayerConfigurationMsgReq linePhysicalLayer;
  uint16 i;
  
  /* {{{ first reset the line */
  if (CALL_HMIQA_NPL(ResetLine, linePtr->devPtr,
                     LINE_MANAGER_ID(linePtr->lineId)) != BCM_SUCCESS)
    /* line can't be reset => fatal */
    FATAL_ERROR("Couldn't reset the line %d", linePtr->lineId);
  /* }}} */

  /* {{{ Traffic configuration */
  memset((void *) &lineTraffic, 0, sizeof(lineTraffic));

  lineTraffic.bearer[B0].config[US].minBitRate =    32;
  lineTraffic.bearer[B0].config[US].maxBitRate =  2048;
  lineTraffic.bearer[B0].config[US].maxDelay   =    20;
  lineTraffic.bearer[B0].config[US].INPmin     =     1;
  lineTraffic.bearer[B0].config[US].ratio_BC   =   100;

  lineTraffic.bearer[B0].config[DS].minBitRate =    32;
  lineTraffic.bearer[B0].config[DS].maxBitRate = 32000;
  lineTraffic.bearer[B0].config[DS].maxDelay   =    20;
  lineTraffic.bearer[B0].config[DS].INPmin     =     1;
  lineTraffic.bearer[B0].config[DS].ratio_BC   =   100;

  lineTraffic.bearer[B0].alpha                 = 7;
  lineTraffic.bearer[B0].delta                 = 6;  
  lineTraffic.bearer[B0].L2LowRateSlots        = 10;
  lineTraffic.bearer[B0].isEnabled             = 1;

  if (CALL_HMIQA_REQ(SetLineTrafficConfiguration, linePtr->devPtr,
                     LINE_MANAGER_ID(linePtr->lineId),
                     &lineTraffic) != BCM_SUCCESS)
    /* line can't be configured => return anomaly */
    return BCM_FAILURE;
  /* }}} */
  
  /* {{{ Physical layer configuration */
  memset((void *) &linePhysicalLayer, 0, sizeof(linePhysicalLayer));

  /* only set data different from 0 */
  linePhysicalLayer.linkConfig[US].targetMarginDB             = 10*6;
  linePhysicalLayer.linkConfig[US].maxAddMarginDB             = 10*0;
  linePhysicalLayer.linkConfig[US].maxTxPower                 = 10*(13);
  linePhysicalLayer.linkConfig[DS].targetMarginDB             = 10*6;
  linePhysicalLayer.linkConfig[DS].maxAddMarginDB             = 10*0;
  linePhysicalLayer.linkConfig[DS].maxTxPower                 = 10*(20);
  linePhysicalLayer.fixedPSD                                  = 1;
  for (i=0; i<sizeof(linePhysicalLayer.carrierMaskDn); i++)
    linePhysicalLayer.carrierMaskDn[i] = 255;
  for (i=0; i<sizeof(linePhysicalLayer.carrierMaskUp); i++)
    linePhysicalLayer.carrierMaskUp[i] = 255;
  linePhysicalLayer.automodePIalpha                           = 4;
  linePhysicalLayer.automodePIbeta                            = 1;
  /* L2 power modes */
  linePhysicalLayer.L0Time                                    = 10;
  linePhysicalLayer.L2Time                                    = 1;
  linePhysicalLayer.L2Atpr                                    = 4;
  linePhysicalLayer.L2Atprt                                   = 10;
  
  { /* ADSL2+ special PSD configuration through the psd toolbox */
    int16 maxPsd    = PHY_STANDARD_DS_PSD_PLUS;
    uint8 nRFIbands = 1;
    uint16 RFIbands[2*BCM_N_RFI_BANDS];
    uint16 startTone = 0;       /* first DS tone in cabinet deployment */
    int16  oobLevel  = -950;    /* -95 dBm (unit is .1 dBm/Hz) */
        
    /* set RFI band 1810kHz - 2000kHz */
    RFIbands[0] = (uint16) (1810/4.3125);        /* start tone index */
    RFIbands[1] = (uint16) (2000/4.3125)+1;      /* stop tone index */
    
    psdToolbox(BCM_PSD_CO, maxPsd, nRFIbands, RFIbands, startTone, oobLevel,
               &linePhysicalLayer.customTssiShape,
               &linePhysicalLayer.protocolSpecificLinkConfig[DS].maxTxDeltaPsd[PSD_PLUS],
               &linePhysicalLayer.adslPlusTxFilterId);
  }
  
  if (CALL_HMIQA_REQ(SetLinePhysicalLayerConfiguration, 
                     linePtr->devPtr, LINE_MANAGER_ID(linePtr->lineId),
                     &linePhysicalLayer) != BCM_SUCCESS)
    /* line can't be configured => return anomaly */
    return BCM_FAILURE;
  /* }}} */

  return BCM_SUCCESS;
}
/* }}} */

