/******************************************************************************/
/*                 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_configureDevice.c                                  */
/*   Purpose         : Generic device configuration function                  */
/*   Limitations     : None                                                   */
/*   Author          : Emmanuel Hanssens                                      */
/*   Creation Date   : 25-Feb-2003                                            */
/*   History         : Compiled by CVS                                        */
/******************************************************************************/
/*   $Id: bcm_configureDevice.c,v 1.19 2004/11/09 08:47:43 hanssens Exp $  */
/******************************************************************************/

#include <stdio.h>
#include <string.h>
#include "bcm_utils.h"
#include "bcm_devObject.h"
#include "bcm_hmiCoreServices.h"

/* {{{ ConfigureDevice */
int ConfigureDevice(DevObject *devPtr)
{
  SetDeviceConfigurationMsgReq deviceConfigReq;
  int lineId;
  
  /* {{{ 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. */
    printf("Device %d is already configured, keeping current config\n",
           devPtr->devId);
    return BCM_SUCCESS;
  }
  /* }}} */
  
  /* {{{ Send new config */
  printf("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++)
  {
    deviceConfigReq.lineConfig[BCM_DEV_LINE_ID(lineId)].bearerConfig[B0].utopiaAddr = lineId;
    deviceConfigReq.lineConfig[BCM_DEV_LINE_ID(lineId)].bearerConfig[B1].utopiaAddr = -1;
  }
  
  /* in-band control: autonomous messages will use (VP,VC)=(0,400) */
  deviceConfigReq.controlUtopiaAddr      =  -2; /* keep current setting */
  deviceConfigReq.utopia16bitBusSelect   =   2; /* keep current setting */
  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;

  /* 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
  /* }}} */

  /* {{{ Send afe config for BCM6510 */
#ifdef BCM_USE_BCM6510
  if (CALL_HMIQA_REQ(SetBCM6510afeConnect, devPtr, CORE_MANAGER_ID,
                     &defaultBCM6510afeConfig
                     [BCM6510_AFE_CONFIG(devPtr->devId)]) != BCM_SUCCESS)
    /* device can't be configured => return anomaly */
    return BCM_FAILURE;
#endif
  /* }}} */

  return BCM_SUCCESS;
}
/* }}} */

