/**************************************************************
$Workfile:   iposapi.c  $
***************************************************************
**
** FIRMWARE, Copyright (C) 2000-2002
** Ikanos Communications, Fremont, CA
** All rights reserved. 
**
** The information and contents of this file are the
** proprietary information of Ikanos Communication and may not
** be disclosed or used without the formal written approval of
** Ikanos Communication
**
$Revision: 1.1 $
$Date: 2006/10/16 04:34:51 $
***************************************************************/
#include <stdio.h>
#include <string.h>
#include "ipos.h"
#include "dapi.h"
#include "apilocal.h"
#include "host_ver.h"
#ifdef INI_SUPPORT
#include "HostPtmfApi.h"
#endif																
#ifndef USE_VXWORKS /* IXDP425 */
#include <v2pthread.h>
#endif
typedef	int	 (*iposDapiMap) (tBmeStruct	*pDABme, int portNum,	void	*par);																	
extern eApiRv HostHotReset(tBmeStruct *pBme, int portNum);
extern eApiRv   ClrNeCntrs(tBmeStruct *pBme, int PortNum);
extern U32 eiuPioRead (int BmeNum, unsigned long addr);
extern void tpw (int BmeNum, unsigned long addr, unsigned long data);

/* local direct API mapping functions	*/

/* bme provision	*/
int	dampBmeProvision(tBmeStruct	*pDABme, int portNum, void	*par);

/* bme version	*/
int	dampBmeVersion(tBmeStruct	*pDABme, int portNum, void	*par);

/* clear all counters */
int	dampBmeClearAllCounters(tBmeStruct	*pDABme, int portNum, void	*par);

/* get BME status	*/
int	dampGetBmeStatus(tBmeStruct	*pDABme, int portNum,	void  *par);

/* port instantiate	*/
int	dampPortInst(tBmeStruct	*pDABme,  int portNum,  void	* par);

/* port provision 	*/
int dampPortProv(tBmeStruct	*pDABme,  int portNum,  void *par);

/* port start  */
int dampPortStart(tBmeStruct	*pDABme,  int portNum,  void *par);

/* port stop	*/
int dampPortStop(tBmeStruct	*pDABme,  int portNum,  void *par);

/* port destroy	*/
int dampPortDestroy(tBmeStruct	*pDABme,  int portNum,  void *par);

/* port reset	*/
int dampPortReset(tBmeStruct	*pDABme,  int portNum,  void *par);

/* periodic report enable	*/
int dampPReportEnable(tBmeStruct	*pDABme,  int portNum,  void *par);

/* updated report	*/
int dampUReport(tBmeStruct	*pDABme,  int portNum,  void *par);

/* EOC control  send	*/
int dampEocCtlSnd (tBmeStruct	*pDABme,  int portNum,  void *par);

/* EOC get data	*/
int	dampEocGetData(tBmeStruct	*pDABme, int portNum, void	* par);

/* EOC relay data	*/

int	dampEocRelayData(tBmeStruct	*pDABme, int portNum, void	* par);

/* EOC Protocol Get Control Parameters. */
int	dampEocProtocolGet(tBmeStruct	*pDABme, int portNum, void	* par);
/* Port Status Brief	*/
int dampPortStatusBrief(tBmeStruct	*pDABme,  int portNum,  void *par);

/* port status	*/
int dampPortStatus(tBmeStruct	*pDABme,  int portNum,  void *par);

/* counter	*/
int dampPortCnt(tBmeStruct	*pDABme,  int portNum,  void *par);

/* PM counter	*/
int dampPortPMCnt(tBmeStruct	*pDABme,  int portNum,  void *par);

/* PM Configuration */
int dampPmConfig (tBmeStruct	*pDABme,  int portNum,  void *par);

/* PM Send Timer */
int dampPmTimer (tBmeStruct	*pDABme,  int portNum,  void *par);

/* Trigger PM Failures */
int dampPortPmTriggerFailures (tBmeStruct	*pDABme, int portNum, void *par);

/* get SNR margin and attenuation	*/
int dampPortSnr(tBmeStruct	*pDABme,  int portNum,  void *par);

int dampOptionalCmd(tBmeStruct	*pDABme,  int portNum,  void *par);

int iposLbStart(ipos_port_id pid, U8 UID, U32 uPar,  iposCbCtl pCompleteFunc);
int dampLbStart(tBmeStruct	*pDABme,  int portNum,  void *par);

int dampSendSignals(tBmeStruct *pDABme, int portNum, void *par);
int dampDetectSignals(tBmeStruct *pDABme, int portNum, void *par);

int dampHostHotReset(tBmeStruct *pDABme, int portNum, void *par);
void dumpBmeStruct (void);
void dumpApiBmeStruct (void);

/* BME objects initantiation	*/
tBmeStruct  	BmeStruct[IPOS_MAX_BME+1];	  		 /* BME objects for direct API */
ipos_api_bme	apiBme[IPOS_MAX_BME+1];  				/* BME objects for user API	   */


/* local statuc inline functions	*/


/* set the index to the source of outgoing msg	*/
void	iposSetMsg(tBmeStruct *pDABme,ipos_msg_buf *pMsgBuf)
{

	   pDABme->TxBmeMsg[_BmeMsgSRC_]=(U8) iposMsgGetIndex(pMsgBuf); 
}


/*********************************************************************** 
		
		The Core User API mechanism 
		
		
***********************************************************************/
int		iposUApiCore(ipos_port_id pid, U8 UID, U32 uPar,  iposCbCtl pCompleteFunc, void *par, iposDapiMap pFuncDapi)
{


	int		errCode,i;
	int		bmeIdx,portIdx;
	ipos_msg_buf	*pMsgBuf;
	tBmeStruct		*pDABme; 			/* direct API BME structure	*/
	ipos_api_bme	*pApiBme;			/* user PPI BME structure	*/ 

	bmeIdx=pid.bmeNum;

	pApiBme=&apiBme[bmeIdx];
	pDABme=pApiBme->pBme;
    
/* if BME is under IDLE state: Return	*/
	if(iposGetState(pApiBme)==IPOS_BME_IDLE)
	{
		printf("IPOS:Unable to run the API.  BME is idle.\n");
		return (IPOS_ERROR_BME_IDLE);
	}
	
	iposApiEnter(pApiBme);				/*  BME access protcetion	*/
	portIdx=pid.portNum;

	pMsgBuf= iposMsgGet();                  /* get iposMsg and set callback function	*/

	if(pMsgBuf==NULL)
	{
		printf("Unable to get msg buffer \n");
		return(IPOS_ERROR_NO_MSGBUF);
	}
	iposSetCallback(pMsgBuf, pCompleteFunc, pid, UID, uPar);

	/* call direct API mapping function here	*/
	iposSetMsg(pDABme,pMsgBuf);
	errCode=pFuncDapi(pDABme,portIdx, par);

	
	if(errCode!=IN_PROGRESS)						/* return failed? fatal error	*/
	{
		printf("Direct API Failed in iposUApiCore!\n");
		iposMsgFree(pMsgBuf);
		iposApiLeave(pApiBme);
		return(IPOS_ERROR_DAPI_FAILED);
	}

	i=0;
	while(pollDmaDone(pDABme)!=TRUE) 	/* Read txrdy?	*/
	{
		iposTaskDelay(10);						/* not ready?   wait until ready	*/ 
  		i++;
		if(i>=IPOS_TX_TIMEOUT)	break;
	}  

	if(i>=IPOS_TX_TIMEOUT)
	{
		printf("Tx Time Out  \n");
		iposMsgFree(pMsgBuf);
		iposApiLeave(pApiBme);
		return(IPOS_ERROR_TX_TIMEOUT);
	}

	iposApiLeave(&apiBme[bmeIdx]);			/* leave the API: release the bmeloack	*/
	return(IPOS_SUCCESS);

}

/*******************************************************
	
	IPOS initialization:
		
		initialize iposAPI datastructures, callbacks,etc.
		
		initialize msgBuf
		
		initialize direct APIs
		
		Configure BME parameters based on definitions in  bmeconfig.h
	
*******************************************************/

int	iposSysInit()
{

	int i,j;

#ifdef SW_HIC_DTACK
	setupForSwHICDelay();
#endif /* SW_HIC_DTACK */

/* initialize msg buffer	*/
	iposMsgBufInit(IPOS_MAX_MSG);
	
/* initialize per BME	*/
	for(i=0;i<=IPOS_MAX_BME;i++)
	{
	
                memset (&BmeStruct[i], 0, sizeof (tBmeStruct));
		apiBme[i].pBme=&BmeStruct[i];
		apiBme[i].bmeNum=i;
		
	/* initialize semaphore for each BME	*/
	
	#ifdef	IPOS_USE_RTOS
		if((apiBme[i].semBme=iposSemCreate())==NULL)
		{
			printf("Failed to create semaphore in API \n"); 
			return (IPOS_ERROR_SEM_ALLOCATION);
		}
		
	#endif

	/* initialize initial BME state 		*/
		apiBme[i].bmeState=IPOS_BME_IDLE;			/* initial state is BME idle	*/

	/* initialize the MaxPort			*/
		apiBme[i].MaxPort=IPOS_MAX_PORT_PER_BME;	/* Initial maximum port. 
													will change when the bandplan initializaed */
	/* initialzie system callback functions	*/
		apiBme[i].pCbStatus=NULL;					/* no callback function	*/
		apiBme[i].uidStatus=0;						/* user id =0			*/
	
		/* initialize the direct API BME objects	*/
		apiBme[i].pBme->BmeNum=i;
		BmeStructInit(apiBme[i].pBme);
	
	/* initialize each port	*/
		for(j=0;j<=IPOS_MAX_PORT_PER_BME;j++)
		{

			apiBme[i].port[j].portNum=j;
	
	/* initialize autonomous callback handling	*/
			apiBme[i].port[j].uidEocDataRelay=0;		/* eco relay data stream	*/
			apiBme[i].port[j].pCbEocDataRelay=NULL;
		
			apiBme[i].port[j].uidEocMsgRcv=0;	  		/* eoc msg receive			*/
			apiBme[i].port[j].pCbEocMsgRcv=NULL;
		
			apiBme[i].port[j].uidCntReport=0;		  	/* port counter report		*/
			apiBme[i].port[j].pCbCntReport=NULL;

			apiBme[i].port[j].uidError=0;		  	/* application error handler */
			apiBme[i].port[j].pCbError=NULL;

			apiBme[i].port[j].uidUpdateReport=0;
			apiBme[i].port[j].pCbUpdateReport=0;
		}

	}
	return IPOS_SUCCESS;
}


/***********************************************************************

		get bme handle from the index of the bme	
		return: NULL or pointer to apiBme object
		
************************************************************************/

U32 iposGetBmeHandle(int i)
{

	if(i<1||i>IPOS_MAX_BME)
	{
		return NULL;
	}

	return (U32) &apiBme[i];
}


/* EOC relay stream	*/
int iposSetCbEocDataStream(ipos_port_id	pid, U8 UID, iposCbEocDataRelay pCallbackFunc)
{
	ipos_api_bme	*pBme;
	ipos_api_port	*pPort;
	int intLevel;

	intLevel=iposIntLock();

	pBme=&(apiBme[pid.bmeNum]);
	pPort=&(pBme->port[pid.portNum]);
    pPort->uidEocDataRelay=UID;
	pPort->pCbEocDataRelay=pCallbackFunc;
		
	iposIntUnlock(intLevel);
	return	IPOS_SUCCESS;
}


/* EOC message		*/
int iposSetCbEocMsg(ipos_port_id  pid, U8 UID, iposCbEocMsgRcv pCallbackFunc)
{
	ipos_api_bme	*pBme;
	ipos_api_port	*pPort;
	int intLevel;

	intLevel=iposIntLock();

	pBme=&(apiBme[pid.bmeNum]);
	pPort=&(pBme->port[pid.portNum]);

    pPort->uidEocMsgRcv=UID;
	pPort->pCbEocMsgRcv=pCallbackFunc;
	
	iposIntUnlock(intLevel);
	return	IPOS_SUCCESS;
}

/* Internal EOC Trace Messages */
int iposSetCbEocEventLog(ipos_port_id	pid, U8 UID, iposCbEocEventLog pCallbackFunc)
{
	ipos_api_bme	*pBme;
	ipos_api_port	*pPort;
	int intLevel;

	intLevel=iposIntLock();

	pBme=&(apiBme[pid.bmeNum]);
	pPort=&(pBme->port[pid.portNum]);
    pPort->uidEocEventLog=UID;
	pPort->pCbEocEventLog=pCallbackFunc;
		
	iposIntUnlock(intLevel);
	return	IPOS_SUCCESS;
}



/* counter Report	*/
int iposSetCbCntReport(ipos_port_id	pid, U8 UID,  iposCbCntReport pCbReport)
{
	ipos_api_bme	*pBme;
	ipos_api_port	*pPort;
	int intLevel;

	intLevel=iposIntLock();

	pBme=&(apiBme[pid.bmeNum]);
	pPort=&(pBme->port[pid.portNum]);

    pPort->uidCntReport=UID;
	pPort->pCbCntReport=pCbReport;
		
	iposIntUnlock(intLevel);
	return	IPOS_SUCCESS;
}

/* PM Alert */
int iposSetCbPmAlert(ipos_port_id pid, U8 UID, iposCbPmAlert pCbAlert)
{
  ipos_api_bme  *pBme;
  ipos_api_port *pPort;
  int intLevel;

  intLevel=iposIntLock();

  pBme=&(apiBme[pid.bmeNum]);
  pPort=&(pBme->port[pid.portNum]);

  pPort->uidPmAlert=UID;
  pPort->pCbPmAlert=pCbAlert;
    
  iposIntUnlock(intLevel);

  return  IPOS_SUCCESS;
}


/* PM Counters */
int iposSetCbPmCounters(ipos_port_id pid, U8 UID,  iposCbPmCounters pPmCntr)
{
  ipos_api_bme  *pBme;
  ipos_api_port *pPort;
  int intLevel;

  intLevel=iposIntLock();

  pBme=&(apiBme[pid.bmeNum]);
  pPort=&(pBme->port[pid.portNum]);

  pPort->uidPMReport=UID;
  pPort->pCbPmCounters=pPmCntr;
    
  iposIntUnlock(intLevel);
  
  return  IPOS_SUCCESS;
}


/* status report	*/
int iposSetCbStatusReport(ipos_port_id	pid, U8 UID, iposCbStatusReport pCbReport)
{
	ipos_api_bme	*pBme;
	int intLevel;

	intLevel=iposIntLock();

	pBme=&(apiBme[pid.bmeNum]);
	pBme->uidStatus=UID;
	pBme->pCbStatus=pCbReport;

	iposIntUnlock(intLevel);
	return	IPOS_SUCCESS;
}

/* Retrieve System Config	*/
int iposSetCbHostRetrieveBme(ipos_port_id	pid, U8 UID, iposCbRetrieveSys pCbSystemConfig)
{
	ipos_api_bme	*pBme;
	int intLevel;

	intLevel=iposIntLock();

	pBme=&(apiBme[pid.bmeNum]);
	pBme->uidStatus=UID;
	pBme->pCbSystemConfig=pCbSystemConfig;

	iposIntUnlock(intLevel);
	return	IPOS_SUCCESS;
}

/* Retrieve Port Config */
int iposSetCbHostRetrievePort(ipos_port_id pid, U8 UID,  iposCbRetrievePort pCbPortConfig)
{
  ipos_api_bme  *pBme;
  ipos_api_port *pPort;
  int intLevel;

  intLevel=iposIntLock();

  pBme=&(apiBme[pid.bmeNum]);
  pPort=&(pBme->port[pid.portNum]);

  pPort->uidRetrievePortConfig=UID;
  pPort->pCbPortConfig=pCbPortConfig;
    
  iposIntUnlock(intLevel);
  
  return  IPOS_SUCCESS;
}

int
iposSetCbUReport(ipos_port_id pid, U8 UID, iposCbUReport pCallbackFunc)
{
	ipos_api_bme	*pBme;
	ipos_api_port	*pPort;
	int intLevel;

	intLevel=iposIntLock();

	pBme=&(apiBme[pid.bmeNum]);
	pPort=&(pBme->port[pid.portNum]);

	pPort->uidUpdateReport=UID;
	pPort->pCbUpdateReport=pCallbackFunc;

	iposIntUnlock(intLevel);
	return	IPOS_SUCCESS;
}

/* errors */
int iposSetCbError( ipos_port_id  pid, U8 UID, iposCbError pCbError )
{
	ipos_api_bme	*pBme;
	ipos_api_port	*pPort;
	int intLevel;

	intLevel=iposIntLock();

	pBme=&(apiBme[pid.bmeNum]);
	pPort=&(pBme->port[pid.portNum]);

	pPort->uidError=UID;
	pPort->pCbError=pCbError;

	iposIntUnlock(intLevel);
	return	IPOS_SUCCESS;
}


/**************************************************************

	   int  iposBmeCpuStart

	  	* load BME firmware
		* start BME firmware

	 return function: IPOS_SUCCESS or IPOS error code
				 
	 callback function: status (IPOS_SUCCESS, or error code) indication the completion of the function
	
	Note this function is blocking the thread until firmware download complete		 


***************************************************************/

int iposBmeStartBlk(int BmeNum, ipos_bme_load  *pBmeLoad,U8 UID, U32 uPar, iposCbCtl pCompleteFunc)
{

	tBmeStruct		*pDABme; 			/* direct API BME structure	*/
	ipos_port_id	pid;
	ipos_api_bme	*pApiBme;			/* user PPI BME structure	*/ 


	pApiBme=&apiBme[BmeNum];
	pDABme=pApiBme->pBme;

	pid.bmeNum=BmeNum;
	pid.portNum=0;

/* set BME start parameters:EMICOnfig, BmeBaseAddr, BmeResetlineAddr, BmeCodeDnldAddr, BmeCodeSoze	*/
	if(BmeStart(pDABme)!=COMPLETE)
	{
		
		if(pCompleteFunc!=NULL) pCompleteFunc(pid,UID,uPar,IPOS_ERROR_BME_START,NULL);

		return (IPOS_ERROR_BME_START);
	
	}

	iposStateUpdate(pApiBme, IPOS_BME_STARTED);

	if(pCompleteFunc!=NULL) pCompleteFunc(pid,UID,uPar,IPOS_SUCCESS,NULL);
	
	return(IPOS_SUCCESS);

};


int iposBmeResetBlk(int BmeNum, ipos_bme_load  *pBmeLoad,U8 UID, U32 uPar,  iposCbCtl pCompleteFunc)
{


	tBmeStruct		*pDABme; 			/* direct API BME structure	*/
	ipos_port_id	pid;
	ipos_api_bme	*pApiBme;			/* user PPI BME structure	*/ 


	pApiBme=&apiBme[BmeNum];
	pDABme=pApiBme->pBme;


	pid.bmeNum=BmeNum;
	pid.portNum=0;


	iposStateUpdate(pApiBme,IPOS_BME_RESTART);

/* set BME start parameters:EMICOnfig, BmeBaseAddr, BmeResetlineAddr, BmeCodeDnldAddr, BmeCodeSoze	*/
	
	if(BmeReset(pDABme)!=COMPLETE)
	{
		
		if(pCompleteFunc!=NULL) pCompleteFunc(pid,UID,uPar,IPOS_ERROR_BME_START,NULL);

		return (IPOS_ERROR_BME_START);
	
	}


	iposStateUpdate(pApiBme, IPOS_BME_STARTED);

	if(pCompleteFunc!=NULL) pCompleteFunc(pid,UID,uPar,IPOS_SUCCESS,NULL);
	
	return(IPOS_SUCCESS);

}

/* Get the State of the BME and the Ports. */
int iposHostReset(int bmeNum, U8 UID, U32 uPar, iposCbCtl pCompleteFunc)
{

   tBmeStruct	*pBme; 			/* direct API BME structure	*/
   ipos_api_bme  *pApiBme;			/* user PPI BME structure	*/ 
   ipos_port_id pid;

   pid.bmeNum = bmeNum;
   pid.portNum = 0;

   pApiBme=&apiBme[pid.bmeNum];
   pBme=pApiBme->pBme;

   /* Set the hostResync to 1, to indicate that the HOST resync is in progress */
   pBme->hostResync = 1;

    return iposUApiCore(pid, UID, uPar, pCompleteFunc, NULL, dampHostHotReset);
	 
}

int dampHostHotReset(tBmeStruct *pDABme, int portNum, void *par)
{
	 return HostHotReset(pDABme,portNum);
}

/**************************************************************************
 
			 provision a BME system		
			 
**************************************************************************/

int iposBmeProvision(int bmeNum,ipos_bme_provision *pBmeProvision, U8 UID, U32 uPar, iposCbCtl pCompleteFunc)
{

	ipos_port_id	pid;
	pid.bmeNum=bmeNum;
	pid.portNum=0;

	return iposUApiCore(pid, UID, uPar, pCompleteFunc, (void *)pBmeProvision, dampBmeProvision );
}

int	dampBmeProvision(tBmeStruct	*pDABme, int portNum, void	*par)
{

	memcpy(&(pDABme->BmeParms.SysProvParms), par, sizeof (ipos_bme_provision));
	return	SystemProvision(pDABme);

}


/**************************************************************************
 
			 Get BME version		
			 
**************************************************************************/

int iposDeviceVersion(int bmeNum, U8 UID, U32 uPar,  
					 void (*pCompleteFunc)(ipos_port_id pid, U8 uid, U32 uPar, int status, ipos_bme_ver *par))

{

	ipos_port_id	pid;
	pid.bmeNum=bmeNum;
	pid.portNum=0;

	return iposUApiCore(pid, UID, uPar, (iposCbCtl) pCompleteFunc, NULL, dampBmeVersion);
}

int	dampBmeVersion(tBmeStruct	*pDABme, int portNum, void	*par)
{

	return	DeviceVersions(pDABme, portNum);

}

/**************************************************************************
 
			 clear all counters for a BME		
			 
**************************************************************************/

int iposBmeClearAllCounters(ipos_port_id pid, U8 UID, U32 uPar,  iposCbCtl pCompleteFunc)
{
	return iposUApiCore(pid, UID, uPar, pCompleteFunc, NULL, dampBmeClearAllCounters );
}

int	dampBmeClearAllCounters(tBmeStruct	*pDABme, int portNum, void	* par)
{
	return ClrNeCntrs(pDABme, portNum);
}


/**************************************************************************

			get the BME status
			 
**************************************************************************/


int iposGetBmeStatus(int bmeNum, U8 UID, U32 uPar,  
						void (*pCompleteFunc)(ipos_port_id pid, U8 uid, U32 uPar, int status, ipos_system_status *par))
{

	ipos_port_id	pid;
	pid.bmeNum=bmeNum;
	pid.portNum=0;

	return iposUApiCore(pid, UID, uPar, (iposCbCtl) pCompleteFunc, NULL, dampGetBmeStatus);

}

int	dampGetBmeStatus(tBmeStruct	*pDABme, int portNum,	void  *par)
{
	  return SystemStatus(pDABme);
}


/********************************************************

	Port	APIs		
	
*******************************************************/

int iposPortInstantiate(ipos_port_id pid, U8 UID, U32 uPar,  iposCbCtl pCompleteFunc)
{
	
	return iposUApiCore(pid, UID, uPar, pCompleteFunc, NULL,dampPortInst );

};


int	dampPortInst(tBmeStruct	*pDABme,  int portNum,  void *par)
{

	  return PortInstantiate(pDABme, portNum);

}



/**************************************************************
**
**	   int  iposPortProvision()
**
**	  	* provision a port within a BME
**		* 
**
**	 return function: IPOS_SUCCESS or IPOS error code
**				 
**	 callback function: status (IPOS_SUCCESS, or error code) indication the completion of the function
**	
**
**
***************************************************************/

int iposPortProvision(ipos_port_id pid, ipos_port_profile *pPortProfile, U8 UID, U32 uPar,  iposCbCtl pCompleteFunc)
{
	return iposUApiCore(pid, UID, uPar, pCompleteFunc, pPortProfile,dampPortProv );

}

int dampPortProv(tBmeStruct	*pDABme,  int portNum,  void *par)
{
	pDABme->Port[portNum].PortParms.pPtrPortProfile	= (	tPortProfile *) par;
	return PortProvision(pDABme, portNum);

}


/**************************************************************
**
**	   int  iposPortStart()
**
**	  	* start a port within a BME
**		* 
**
**	 return function: IPOS_SUCCESS or IPOS error code
**				 
**	 callback function: status (IPOS_SUCCESS, or error code) indication the completion of the function
**	
**
**
***************************************************************/

int iposPortStart(ipos_port_id pid, U8 UID, U32 uPar,  iposCbCtl pCompleteFunc)
{
	return iposUApiCore(pid, UID, uPar, pCompleteFunc, NULL,dampPortStart );

}

int dampPortStart(tBmeStruct	*pDABme,  int portNum,  void *par)
{
	return PortStart(pDABme, portNum);
}


int iposLbStart(ipos_port_id pid, U8 UID, U32 uPar,  iposCbCtl pCompleteFunc)
{
	return iposUApiCore(pid, UID, uPar, pCompleteFunc, (void *)uPar, dampLbStart);

}

extern eApiRv testLb (tBmeStruct *pBme, int PortNum);

int dampLbStart(tBmeStruct	*pDABme,  int portNum,  void *par)
{
    int lbType = (int)(par);

    pDABme->Port[portNum].PortParms.SetLpbkParms.LoopbackMode = (unsigned char)lbType;
	return testLb(pDABme, portNum);
}

/**************************************************************
**
**	   int  iposPortStop()
**
**	  	* stop a port within a BME
**		* 
**
**	 return function: IPOS_SUCCESS or IPOS error code
**				 
**	 callback function: status (IPOS_SUCCESS, or error code) indication the completion of the function
**	
**
**
***************************************************************/


int iposPortStop(ipos_port_id pid, U8 UID, U32 uPar,  iposCbCtl pCompleteFunc)
{
	return iposUApiCore(pid, UID, uPar, pCompleteFunc, NULL,dampPortStop );

}

int dampPortStop(tBmeStruct	*pDABme,  int portNum,  void *par)
{
	return PortStop(pDABme, portNum);
}



/**************************************************************
**
**	   int  iposPortDestroy()
**
**	  	* destroy a port within a BME
**		* 
**
**	 return function: IPOS_SUCCESS or IPOS error code
**				 
**	 callback function: status (IPOS_SUCCESS, or error code) indication the completion of the function
**	
**
**
***************************************************************/

int iposPortDestroy(ipos_port_id pid, U8 UID, U32 uPar,  iposCbCtl pCompleteFunc)
{

	return iposUApiCore(pid, UID, uPar, pCompleteFunc, NULL,dampPortDestroy );

}

int dampPortDestroy(tBmeStruct	*pDABme,  int portNum,  void *par)
{
	return PortDestroy(pDABme, portNum);
}

/**************************************************************
**
**	   int  iposPortReset()
**
**	  	* destroy a port within a BME
**		* 
**
**	 return function: IPOS_SUCCESS or IPOS error code
**				 
**	 callback function: status (IPOS_SUCCESS, or error code) indication the completion of the function
**	
**
**
***************************************************************/

int iposPortReset(ipos_port_id pid, U8 portResetParm,U8 UID, U32 uPar,  iposCbCtl pCompleteFunc)
{

	return iposUApiCore(pid, UID, uPar, pCompleteFunc, &portResetParm,dampPortReset );

}

int dampPortReset(tBmeStruct	*pDABme,  int portNum,  void *par)
{						   							/* parameters not set */
	return PortReset(pDABme, portNum);

}


/**************************************************************
**
**	   	int  iposPeriodicRepoartEnable()
**
**	  	Enable period report
**
**	 	return function: IPOS_SUCCESS or IPOS error code
**				 
**	 	callback function: status (IPOS_SUCCESS, or error code) indication the completion of the function
**	
**
**
***************************************************************/

int iposPeriodicReportsEnable(ipos_port_id	pid, ipos_report_config *pReportCfg, U8 UID, U32 uPar,  iposCbCtl pCompleteFunc)
{

	return iposUApiCore(pid, UID, uPar, pCompleteFunc, pReportCfg, dampPReportEnable );
}

int dampPReportEnable(tBmeStruct	*pDABme,  int portNum,  void *par)
{						   							/* parameters not set */
	memcpy(&(pDABme->Port[portNum].PortParms.PerReportParms),par,sizeof(ipos_report_config));
	return PeriodicReportsEnable(pDABme, portNum);

}



/**************************************************************
**
**	   	int  iposUpdatedRepoartEnable()
**
**	  	Enable updated report
**
**	 	return function: IPOS_SUCCESS or IPOS error code
**				 
**	 	callback function: status (IPOS_SUCCESS, or error code) indication the completion of the function
**	
**
**
***************************************************************/

int iposUpdatedReportEnable(ipos_port_id pid, U8 reportsEnableParm, U8 UID, U32 uPar,  iposCbCtl pCompleteFunc)
{
	return iposUApiCore(pid, UID, uPar, pCompleteFunc, (void *) (&reportsEnableParm), dampUReport );
}

int dampUReport(tBmeStruct	*pDABme,  int portNum,  void *par)
{
	pDABme->Port[portNum].PortParms.UpdatedReportParms.ReportsEnable = *((BYTE *) par);
	return UpdatedReportsEnabled(pDABme, portNum);
}
	

/**************************************************************
**
**	   	int  iposUpdateCounter()
**
**	  	Update a selected counter in the BME
**
**	 	return function: IPOS_SUCCESS or IPOS error code
**				 
**	 	callback function: status (IPOS_SUCCESS, or error code)
**		indication the completion of the function
**	
***************************************************************/

static int
dampUCounter(tBmeStruct	*pDABme,  int portNum,  void *par)
{
	tUpdateCounterReq	 dapiUpdateCounterReq;
	ipos_update_counter_req	*pUpdateCounterReq = (ipos_update_counter_req *) par;

	dapiUpdateCounterReq.value = pUpdateCounterReq->value;
	dapiUpdateCounterReq.index = pUpdateCounterReq->index;
    
	return UpdateCounter(pDABme, portNum, &dapiUpdateCounterReq );
}

int
iposUpdateCounter(ipos_port_id pid, ipos_update_counter_req *pUpdateCounterReq, U8 UID, U32 uPar,  iposCbCtl pCompleteFunc)
{
	return iposUApiCore(pid, UID, uPar, pCompleteFunc, (void *) pUpdateCounterReq, dampUCounter );
}

/**************************************************************
**
**	   int  iposEocControlSend()
**
**	  	Send a EOC control message to a port 
**
**	 return function: IPOS_SUCCESS or IPOS error code
**				 
**	 callback function: status (IPOS_SUCCESS, or error code) indication the completion of the function
**	
**
**
***************************************************************/
int	iposEocControlSend(ipos_port_id pid, U8 opCode, U8 UID, U32 uPar,  iposCbCtl pCompleteFunc) 
{

	return iposUApiCore(pid, UID, uPar, pCompleteFunc, &opCode, dampEocCtlSnd );
}

int dampEocCtlSnd (tBmeStruct	*pDABme,  int portNum,  void *par)
{						   							/* parameters not set */
	pDABme->Port[portNum].PortParms.EocControlParms.EocMode= *((U8 *) par);
	return EocControlSend(pDABme,portNum);

}


/**************************************************************
**	 int  iposEocProtocolSet()
**	 Set  a parameter of the Internal EOC Protocol corresponding to a port 
**	 return function: IPOS_SUCCESS or IPOS error code
**	 callback function: The function passed in pCompleteFunc (eocProtocolSetComplete) .
**                      Used for updating Status (SUCCESS or FAILURE).
***************************************************************/
int iposEocProtocolSet(ipos_port_id pid, U8 opCode, U8 *regData, U8 UID, U32 uPar,  iposCbCtl pCompleteFunc)
{
    int		errCode, i, bmeIdx, portIdx;
    ipos_msg_buf	*pMsgBuf;
    tBmeStruct		*pDABme; 			/* direct API BME structure	*/
    ipos_api_bme	*pApiBme;			/* user PPI BME structure	*/ 

    bmeIdx=pid.bmeNum;
    pApiBme=&apiBme[bmeIdx];
    pDABme=pApiBme->pBme;

    /* if BME is under IDLE state: Return	*/
    if(iposGetState(pApiBme)==IPOS_BME_IDLE) {
        printf("IPOS:Unable to run the API.  BME is idle.\n");
        return (IPOS_ERROR_BME_IDLE);
    }
	
    iposApiEnter(pApiBme);				/*  BME access protcetion	*/
    portIdx=pid.portNum;
    pMsgBuf= iposMsgGet();        /* get iposMsg and set callback function	*/

    if(pMsgBuf==NULL) {
        printf("Unable to get msg buffer \n");
        return(IPOS_ERROR_NO_MSGBUF);
    }
  
		/* Set the Callback function. */
		iposSetCallback(pMsgBuf, pCompleteFunc, pid, UID, uPar);

    pDABme->Port[portIdx].PortParms.EocSetDataParms.EocOpcode=opCode;
    memcpy(&(pDABme->Port[portIdx].PortParms.EocSetDataParms.EocRegisterData),regData,128);
	
    iposSetMsg(pDABme, pMsgBuf);					/* set the msgbuf index to the outgoing msg */
    errCode=EocProtocolSet(pDABme,portIdx);

    if(errCode!=IN_PROGRESS) {
		    /* In case return failed or we get a fatal error. */	 
        printf("Eoc Set Protocol Direct API Failed!  \n");
        iposMsgFree(pMsgBuf);
        iposApiLeave(pApiBme);
        return(IPOS_ERROR_DAPI_FAILED);
    }

    i=0;
    while(pollDmaDone(pDABme)!=TRUE) {
		    /* Read txrdy?	*/
        iposTaskDelay(10);						/* not ready?   wait until ready	*/ 
        i++;
        if(i>=IPOS_TX_TIMEOUT)	break;
    }  

    if(i>=IPOS_TX_TIMEOUT) {
        printf("Tx Time Out  \n");
        iposMsgFree(pMsgBuf);
        iposApiLeave(pApiBme);
        return(IPOS_ERROR_TX_TIMEOUT);
    }
		
    iposApiLeave(&apiBme[bmeIdx]);			/* leave the API: release the bmeloack	*/
    return(IPOS_SUCCESS);
}

/**************************************************************
**	 int  iposEocProtocolSet()
**	 Get  a parameter of the Internal EOC Protocol corresponding to a port 
**	 return function: IPOS_SUCCESS or IPOS error code
**	 callback function: The function passed in pCompleteFunc (eocProtocolGetComplete) .
**                      Used for updating Status (SUCCESS or FAILURE).
***************************************************************/

int iposEocProtocolGet(ipos_port_id pid, U8 opCode, U8 UID, U32 uPar,  iposCbCtl pCompleteFunc)
{
    return iposUApiCore(pid, UID, uPar, pCompleteFunc, &opCode ,dampEocProtocolGet);
}

int	dampEocProtocolGet(tBmeStruct	*pDABme, int portNum, void	* par)
{
    pDABme->Port[portNum].PortParms.EocGetDataParms.EocOpcode= *((U8 *) par);
    return EocProtocolGet(pDABme,portNum);
}


/**************************************************************
**
**	   int  iposEocSetData()
**
**	  	Set  EOC data to a port 
**
**	 return function: IPOS_SUCCESS or IPOS error code
**				 
**	 callback function: status (IPOS_SUCCESS, or error code) indication the completion of the function
**	
**
**
***************************************************************/
int iposEocSetData(ipos_port_id pid, U8 opCode, U8 *msg, U8 UID, U32 uPar,  iposCbCtl pCompleteFunc)
{

	int		errCode,i;
	int		bmeIdx,portIdx;
	ipos_msg_buf	*pMsgBuf;
	tBmeStruct		*pDABme; 			/* direct API BME structure	*/
	ipos_api_bme	*pApiBme;			/* user PPI BME structure	*/ 

	bmeIdx=pid.bmeNum;

	pApiBme=&apiBme[bmeIdx];
	pDABme=pApiBme->pBme;

/* if BME is under IDLE state: Return	*/
	if(iposGetState(pApiBme)==IPOS_BME_IDLE)
	{
		printf("IPOS:Unable to run the API.  BME is idle.\n");
		return (IPOS_ERROR_BME_IDLE);
	}
	
	iposApiEnter(pApiBme);				/*  BME access protcetion	*/
	portIdx=pid.portNum;


	pMsgBuf= iposMsgGet();                  /* get iposMsg and set callback function	*/

	if(pMsgBuf==NULL)
	{
		printf("Unable to get msg buffer \n");
		return(IPOS_ERROR_NO_MSGBUF);
	}
	iposSetCallback(pMsgBuf, pCompleteFunc, pid, UID, uPar);

/* set direct API call	*/

	pDABme->Port[portIdx].PortParms.EocSetDataParms.EocOpcode=opCode;
	memcpy(&(pDABme->Port[portIdx].PortParms.EocSetDataParms.EocRegisterData),msg,128);

	
	iposSetMsg(pDABme, pMsgBuf);					/* set the msgbuf index to the outgoing msg */
	errCode=EocSetData(pDABme,portIdx);

	if(errCode!=IN_PROGRESS)						/* return failed? fatal error	*/
	{
		printf("Port Instantiate Direct API Failed!  \n");
		iposMsgFree(pMsgBuf);
		iposApiLeave(pApiBme);
		return(IPOS_ERROR_DAPI_FAILED);
	}

	i=0;
	while(pollDmaDone(pDABme)!=TRUE) 	/* Read txrdy?	*/
	{
		iposTaskDelay(10);						/* not ready?   wait until ready	*/ 
  		i++;
		if(i>=IPOS_TX_TIMEOUT)	break;
	}  

	if(i>=IPOS_TX_TIMEOUT)
	{
		printf("Tx Time Out  \n");
		iposMsgFree(pMsgBuf);
		iposApiLeave(pApiBme);
		return(IPOS_ERROR_TX_TIMEOUT);
	}


	iposApiLeave(&apiBme[bmeIdx]);			/* leave the API: release the bmeloack	*/
	return(IPOS_SUCCESS);

}


/**************************************************************
**
**	   int  iposBitSwapReq()
**
**	  	Send  Bit Swap Message data to a port 
**
**	 return function: IPOS_SUCCESS or IPOS error code
**				 
**	 callback function: status (IPOS_SUCCESS, or error code) indication the completion of the function
**	
**
**
***************************************************************/
int iposBitSwapReq(ipos_port_id pid, U8 opCode, U8 *msg, U8 UID, U32 uPar,  iposCbCtl pCompleteFunc)
{

	int		errCode,i;
	int		bmeIdx,portIdx;
	ipos_msg_buf	*pMsgBuf;
	tBmeStruct		*pDABme; 			/* direct API BME structure	*/
	ipos_api_bme	*pApiBme;			/* user PPI BME structure	*/ 

	bmeIdx=pid.bmeNum;

	pApiBme=&apiBme[bmeIdx];
	pDABme=pApiBme->pBme;

/* if BME is under IDLE state: Return	*/
	if(iposGetState(pApiBme)==IPOS_BME_IDLE)
	{
		printf("IPOS:Unable to run the API.  BME is idle.\n");
		return (IPOS_ERROR_BME_IDLE);
	}
	
	iposApiEnter(pApiBme);				/*  BME access protcetion	*/
	portIdx=pid.portNum;


	pMsgBuf= iposMsgGet();                  /* get iposMsg and set callback function	*/

	if(pMsgBuf==NULL)
	{
		printf("Unable to get msg buffer \n");
		return(IPOS_ERROR_NO_MSGBUF);
	}
	iposSetCallback(pMsgBuf, pCompleteFunc, pid, UID, uPar);

/* set direct API call	*/

	pDABme->Port[portIdx].PortParms.BitSwapReqParms.BitSwapOpcode=opCode;
	memcpy(&(pDABme->Port[portIdx].PortParms.BitSwapReqParms.BitSwapMsg),msg,30);

	
	iposSetMsg(pDABme, pMsgBuf);					/* set the msgbuf index to the outgoing msg */
	errCode=BitSwapReq(pDABme,portIdx);

	if(errCode!=IN_PROGRESS)						/* return failed? fatal error	*/
	{
		printf("Port Instantiate Direct API Failed!  \n");
		iposMsgFree(pMsgBuf);
		iposApiLeave(pApiBme);
		return(IPOS_ERROR_DAPI_FAILED);
	}

	i=0;
	while(pollDmaDone(pDABme)!=TRUE) 	/* Read txrdy?	*/
	{
		iposTaskDelay(10);						/* not ready?   wait until ready	*/ 
  		i++;
		if(i>=IPOS_TX_TIMEOUT)	break;
	}  

	if(i>=IPOS_TX_TIMEOUT)
	{
		printf("Tx Time Out  \n");
		iposMsgFree(pMsgBuf);
		iposApiLeave(pApiBme);
		return(IPOS_ERROR_TX_TIMEOUT);
	}


	iposApiLeave(&apiBme[bmeIdx]);			/* leave the API: release the bmeloack	*/
	return(IPOS_SUCCESS);

}



/**************************************************************
**
**	   	int  iposEocDataReply()
**
**	  	Reply  EOC data to  a port 
**
**	 	return function: IPOS_SUCCESS or IPOS error code
**				 
**	 	callback function: status (IPOS_SUCCESS, or error code) indication the completion of the function
**	
**
**
***************************************************************/

int iposEocDataReply(ipos_port_id pid,U8 opCode, U8 len, U8 *regData, U8 UID, U32 uPar,  iposCbCtl pCompleteFunc)
{

	int		errCode,i;
	int		bmeIdx,portIdx;
	ipos_msg_buf	*pMsgBuf;
	tBmeStruct		*pDABme; 			/* direct API BME structure	*/
	ipos_api_bme	*pApiBme;			/* user PPI BME structure	*/ 

	bmeIdx=pid.bmeNum;

	pApiBme=&apiBme[bmeIdx];
	pDABme=pApiBme->pBme;

/* if BME is under IDLE state: Return	*/
	if(iposGetState(pApiBme)==IPOS_BME_IDLE)
	{
		printf("IPOS:Unable to run the API.  BME is idle.\n");
		return (IPOS_ERROR_BME_IDLE);
	}
	
	iposApiEnter(pApiBme);				/*  BME access protcetion	*/
	portIdx=pid.portNum;

	pMsgBuf= iposMsgGet();                  /* get iposMsg and set callback function	*/

	if(pMsgBuf==NULL)
	{
		printf("Unable to get msg buffer \n");
		return(IPOS_ERROR_NO_MSGBUF);
	}
	iposSetCallback(pMsgBuf, pCompleteFunc, pid, UID, uPar);

/* set direct API call	*/

	pDABme->Port[portIdx].PortParms.EocDataReply.EocOpcode=opCode;
	pDABme->Port[portIdx].PortParms.EocDataReply.EocLength=len;
	memcpy(&(pDABme->Port[portIdx].PortParms.EocDataReply.EocData),regData,len);

	
	iposSetMsg(pDABme, pMsgBuf);					/* set the msgbuf index to the outgoing msg */
	errCode=EocSetData(pDABme,portIdx);

	if(errCode!=IN_PROGRESS)						/* return failed? fatal error	*/
	{
		printf("Port Instantiate Direct API Failed!  \n");
		iposMsgFree(pMsgBuf);
		iposApiLeave(pApiBme);
		return(IPOS_ERROR_DAPI_FAILED);
	}

	i=0;
	while(pollDmaDone(pDABme)!=TRUE) 	/* Read txrdy?	*/
	{
		iposTaskDelay(10);						/* not ready?   wait until ready	*/ 
  		i++;
		if(i>=IPOS_TX_TIMEOUT)	break;
	}  

	if(i>=IPOS_TX_TIMEOUT)
	{
		printf("Tx Time Out  \n");
		iposMsgFree(pMsgBuf);
		iposApiLeave(pApiBme);
		return(IPOS_ERROR_TX_TIMEOUT);
	}

	iposApiLeave(&apiBme[bmeIdx]);			/* leave the API: release the bmeloack	*/
	return(IPOS_SUCCESS);


}


/**************************************************************
**
**	   	int  iposEocSendDataStream()
**
**	  	Send  EOC data stream  to  a port 
**
**	 	return function: IPOS_SUCCESS or IPOS error code
**				 
**	 	callback function: status (IPOS_SUCCESS, or error code) indication the completion of the function
**	
**
**
***************************************************************/

int iposEocSendDataStream(ipos_port_id pid, U8 len, U8 *regData, U8 UID, U32 uPar,  iposCbCtl pCompleteFunc)
{

	int		errCode,i;
	int		bmeIdx,portIdx;
	ipos_msg_buf	*pMsgBuf;
	tBmeStruct		*pDABme; 			/* direct API BME structure	*/
	ipos_api_bme	*pApiBme;			/* user PPI BME structure	*/ 

	bmeIdx=pid.bmeNum;

	pApiBme=&apiBme[bmeIdx];
	pDABme=pApiBme->pBme;

/* if BME is under IDLE state: Return	*/
	if(iposGetState(pApiBme)==IPOS_BME_IDLE)
	{
		printf("IPOS:Unable to run the API.  BME is idle.\n");
		return (IPOS_ERROR_BME_IDLE);
	}

	iposApiEnter(pApiBme);				/*  BME access protcetion	*/
	portIdx=pid.portNum;

	pMsgBuf= iposMsgGet();                  /* get iposMsg and set callback function	*/

	if(pMsgBuf==NULL)
	{
		printf("Unable to get msg buffer \n");
		return(IPOS_ERROR_NO_MSGBUF);
	}
	iposSetCallback(pMsgBuf, pCompleteFunc, pid, UID, uPar);

/* set direct API call	*/

	pDABme->Port[portIdx].PortParms.EocSendDataStrParms.EocLength=len;
	memcpy(&(pDABme->Port[portIdx].PortParms.EocSendDataStrParms.DataStream),regData,len);

#ifdef	DEBUG
	printf("len= %d, regData= %d %d %d %d %d %d %d %d  \n",len, regData[0],
		   regData[1],regData[2],regData[3],regData[4],regData[5],regData[6],regData[7]);

#endif
	
	iposSetMsg(pDABme, pMsgBuf);					/* set the msgbuf index to the outgoing msg */
	errCode=EocSendDataStream(pDABme,portIdx);

	if(errCode!=IN_PROGRESS)						/* return failed? fatal error	*/
	{
		printf("Direct API Failed in iposEocSendDataStream with error code %d\n",
			errCode);
		iposMsgFree(pMsgBuf);
		iposApiLeave(pApiBme);
		return(IPOS_ERROR_DAPI_FAILED);
	}

	i=0;
	while(pollDmaDone(pDABme)!=TRUE) 	/* Read txrdy?	*/
	{
		iposTaskDelay(10);						/* not ready?   wait until ready	*/ 
  		i++;
		if(i>=IPOS_TX_TIMEOUT)	break;
	}  

	if(i>=IPOS_TX_TIMEOUT)
	{
		printf("Tx Time Out  \n");
		iposMsgFree(pMsgBuf);
		iposApiLeave(pApiBme);
		return(IPOS_ERROR_TX_TIMEOUT);
	}

	iposApiLeave(&apiBme[bmeIdx]);			/* leave the API: release the bmeloack	*/
	return(IPOS_SUCCESS);


}

/**************************************************************
**
**	   	int  iposEocGetData()
**
**	  	Get  EOC data stream  from  a port 
**
**	 	return function: IPOS_SUCCESS or IPOS error code
**				 
**	 	callback function: status (IPOS_SUCCESS, or error code) indication the completion of the function
**	
**
**
***************************************************************/


int iposEocGetData(ipos_port_id pid, U8 opCode, U8 UID, U32 uPar,  iposCbCtl pCompleteFunc)
{

	return iposUApiCore(pid, UID, uPar, pCompleteFunc, &opCode,dampEocGetData );

}

int	dampEocGetData(tBmeStruct	*pDABme, int portNum, void	* par)
{
	pDABme->Port[portNum].PortParms.EocGetDataParms.EocOpcode= *((U8 *) par);
	return EocGetData(pDABme,portNum);

}


/**************************************************************
**
**	   	int  iposEocRelayData()
**
**	  	Get  EOC data stream  from  a port 
**
**	 	return function: IPOS_SUCCESS or IPOS error code
**				 
**	 	callback function: status (IPOS_SUCCESS, or error code) indication the completion of the function
**	
**
**
***************************************************************/

int	iposEocRelayData(ipos_port_id pid,  U8 UID, U32 uPar,  iposCbCtl pCompleteFunc)
{

	return iposUApiCore(pid, UID, uPar, pCompleteFunc, NULL,dampEocRelayData );

}

int	dampEocRelayData(tBmeStruct	*pDABme, int portNum, void	* par)
{

/*	return	ClearPrimitiveCounts(pDABme,portNum); */
	return	IPOS_ERROR_NOT_IMPLEMENTED;

}



/**************************************************************
**
**	   	int  iposPortStatusBrief()
**
**	  	Get brief status from a port
**
**	 	return function: IPOS_SUCCESS or IPOS error code
**				 
**	 	callback function: status (IPOS_SUCCESS, or error code) indication the completion of the function
**	
**
***************************************************************/

int iposPortStatusBrief(ipos_port_id pid, U8 UID, U32 uPar,  
			void (*pCompleteFunc)(ipos_port_id pid, U8 uid, U32 uPar, int status, ipos_port_status_brief *par))       

{
	return iposUApiCore(pid, UID, uPar, (iposCbCtl) pCompleteFunc, NULL,dampPortStatusBrief );

}

int dampPortStatusBrief(tBmeStruct	*pDABme,  int portNum,  void *par)
{
	return PortStatusBrief(pDABme, portNum);
}



/**************************************************************
**
**	   	int  iposPortStatusExtended()
**
**	  	Get extended status from a port
**
**	 	return function: IPOS_SUCCESS or IPOS error code
**				 
**	 	callback function: status (IPOS_SUCCESS, or error code) indication the completion of the function
**	
**
**
***************************************************************/

int iposPortStatusExtended(ipos_port_id pid, U8 UID, U32 uPar,
		void (*pCompleteFunc)(ipos_port_id pid, U8 uid, U32 uPar, int status, ipos_port_status *par))       
		
{
	return iposUApiCore(pid, UID, uPar, (iposCbCtl) pCompleteFunc, NULL, dampPortStatus );
}

int dampPortStatus(tBmeStruct	*pDABme,  int portNum,  void *par)
{
	return PortStatusExtended(pDABme, portNum);
}



/**************************************************************
**
**	   	int  iposPortGetCounters()
**
**	  	Get  counters for  a port
**
**	 	return function: IPOS_SUCCESS or IPOS error code
**				 
**	 	callback function: status (IPOS_SUCCESS, or error code) indication the completion of the function
**	
**
**
***************************************************************/

int iposPortGetCounters(ipos_port_id pid, U8 UID, U32 uPar,  
		void (*pCompleteFunc)(ipos_port_id pid, U8 uid, U32 uPar, int status, ipos_port_counters *par))       

{
	return iposUApiCore(pid, UID, uPar, (iposCbCtl) pCompleteFunc, NULL, dampPortCnt );
}

int dampPortCnt(tBmeStruct	*pDABme,  int portNum,  void *par)
{
	return GetCounters(pDABme, portNum);
}


/**************************************************************
**
**	  int  iposPortGetPMCounters()
**
**	  Get PM counters for  a port
**
**	 	return function: IPOS_SUCCESS or IPOS error code
**				 
**	 	callback function: status (IPOS_SUCCESS, or error code)
**	                     indicates the completion of the function
**
**
***************************************************************/

int iposPortGetPMCounters(ipos_port_id pid, U8 UID, U32 uPar,  
		void (*pCompleteFunc)(ipos_port_id pid, U8 uid, U32 uPar, int status, ipos_pm_counters *par))

{
	return iposUApiCore(pid, UID, uPar, (iposCbCtl) pCompleteFunc, NULL, dampPortPMCnt );
}

int dampPortPMCnt(tBmeStruct	*pDABme,  int portNum,  void *par)
{
	return GetPMCounters(pDABme, portNum);
}


/**************************************************************
**
**	  int  iposPmConfig()
**
**    Configure Performance Management Parameters	  	
**
**	 	return function: IPOS_SUCCESS or IPOS error code
**				 
**	 	callback function: status (IPOS_SUCCESS, or error code)
**	                     indicates the completion of the function
**
**
***************************************************************/

int iposPmConfig (ipos_port_id pid, ipos_pm_config *pPmConfig, U8 UID, U32 uPar,
                  iposCbCtl pCompleteFunc) 
{
	return iposUApiCore(pid, UID, uPar, pCompleteFunc, (void *)pPmConfig, dampPmConfig );
}

int dampPmConfig (tBmeStruct	*pDABme,  int portNum,  void *par)
{
  // Copy Config parameters
	memcpy(&(pDABme->Port[portNum].PortParms.PmConfigParms), par, sizeof(ipos_pm_config));

	return PmConfig(pDABme, portNum);
}


/**************************************************************
**
**	  int  iposPmTimer()
**
**    Configure Performance Management Parameters	  	
**
**	 	return function: IPOS_SUCCESS or IPOS error code
**				 
**	 	callback function: status (IPOS_SUCCESS, or error code)
**	                     indicates the completion of the function
**
**
***************************************************************/

int iposPmTimer (ipos_port_id pid, U8 UID, U32 uPar,  iposCbCtl pCompleteFunc)
{
	return iposUApiCore(pid, UID, uPar, pCompleteFunc, NULL, dampPmTimer );
}

int dampPmTimer (tBmeStruct	*pDABme,  int portNum,  void *par)
{
	return PmSendTimer(pDABme);
}



/**************************************************************
**
**	  int  iposPortPmTriggerFailures()
**
**	  Get PM counters for  a port
**
**	 	return function: IPOS_SUCCESS or IPOS error code
**				 
**	 	callback function: status (IPOS_SUCCESS, or error code)
**	                     indicates the completion of the function
**
**
***************************************************************/

int iposPortPmTriggerFailures(ipos_port_id pid, ipos_pm_trigger *pTrigger,
                              U8 UID, U32 uPar, iposCbCtl pCompleteFunc)  
{
	return iposUApiCore(pid, UID, uPar, pCompleteFunc, pTrigger,
                      dampPortPmTriggerFailures);
}

int dampPortPmTriggerFailures (tBmeStruct	*pDABme, int portNum, void *par)
{

  // Copy Trigger parameters
	memcpy(&(pDABme->Port[portNum].PortParms.PmTriggerParms), par, sizeof(ipos_pm_trigger));

	return PmTriggerFailures(pDABme, portNum);
}


/**************************************************************
**
**	   	int  iposGetNeSNRAttn()
**
**
**	 	return function: IPOS_SUCCESS or IPOS error code
**				 
**	 	callback function: status (IPOS_SUCCESS, or error code) indication the completion of the function
**	
**
**
***************************************************************/


int iposGetNeSNRAttn(ipos_port_id pid, U8 UID, U32 uPar,  
		void (*pCompleteFunc)(ipos_port_id pid, U8 uid, U32 uPar, int status, ipos_port_nesnrattn *par))       
{

	return iposUApiCore(pid, UID, uPar, (iposCbCtl) pCompleteFunc, NULL, dampPortSnr );
}

int dampPortSnr(tBmeStruct	*pDABme,  int portNum,  void *par)
{
	return GetNeSnrAttn(pDABme, portNum);
}

int iposOptionalCmd(ipos_port_id pid, U8 UID, U32 uPar, U32 Par,  
		void (*pCompleteFunc)(ipos_port_id pid, U8 uid, U32 uPar, int status, ipos_opCmd_status *par))       
{

	return iposUApiCore(pid, UID, uPar, (iposCbCtl) pCompleteFunc, (void *)(Par), dampOptionalCmd);
}

int dampOptionalCmd(tBmeStruct	*pDABme,  int portNum,  void *par)
{
	ipos_op_cmd	*p = (ipos_op_cmd *)par;
	
	// printf("buf 0x%x size %d", p->buf, p->len);
	pDABme->BmeParms.OptionalCmd.buf = p->buf;
	pDABme->BmeParms.OptionalCmd.length = p->len;
	return GetOpCmdStatus(pDABme, portNum);
}
/* Test API */


/**************************************************************
**
**	   	int  iposSetLoopback()
**		Set a port to Loopback mode
**
**	 	return function: IPOS_SUCCESS or IPOS error code
**				 
**	 	callback function: status (IPOS_SUCCESS, or error code) indication the completion of the function
**	
**
**
***************************************************************/

int iposSetLoopback(ipos_port_id pid, U8 UID, U32 uPar,  iposCbCtl pCompleteFunc)
{

	int	bmeIdx,portIdx;

	ipos_msg_buf	*pMsgBuf;

	bmeIdx=pid.bmeNum;
	
	iposApiEnter(&apiBme[bmeIdx]);			/*  BME access protcetion	*/
	
	portIdx=pid.portNum;
	
	pMsgBuf= iposMsgGet();                  /* get iposMsg and set callback function	*/


	if(pMsgBuf==NULL)
	{
		printf("Unable to get msg buffer \n");
		
		return(IPOS_ERROR_NO_MSGBUF);

	}

	iposSetCallback(pMsgBuf,  pCompleteFunc, pid, UID, uPar);


/* set direct API call	*/


	iposApiLeave(&apiBme[bmeIdx]);			/* leave the API: release the bmeloack	*/
	
	return(IPOS_SUCCESS);

}


/**************************************************************
**
**	   	int  iposSendTestSignals()
**		Send the test signals
**
**	 	return function: IPOS_SUCCESS or IPOS error code
**				 
**	 	callback function: status (IPOS_SUCCESS, or error code) indication the completion of the function
**	
**
**
***************************************************************/

int iposSendTestSignals(ipos_port_id pid, ipos_test_signals * pTestSig, U8 UID, U32 uPar,  iposCbCtl pCompleteFunc)
{
	return iposUApiCore(pid, UID, uPar, pCompleteFunc, pTestSig, dampSendSignals );

}

int dampSendSignals(tBmeStruct *pDABme, int portNum, void *par)
{
	memcpy ((char *)&(pDABme->Port[portNum].PortParms.SendSigParms), (char *)par, sizeof(ipos_test_signals));
	return SendSignals (pDABme, portNum);
}



/**************************************************************
**
**	   	int  iposDetecTestSignals()
**		Detect the test signals
**
**	 	return function: IPOS_SUCCESS or IPOS error code
**				 
**	 	callback function: status (IPOS_SUCCESS, or error code) indication the completion of the function
**	
**
**
***************************************************************/
int iposDetectTestSignals(ipos_port_id pid, ipos_test_signals * pTestSig, U8 UID, U32 uPar,  iposCbCtl pCompleteFunc)
{
	return iposUApiCore(pid, UID, uPar, pCompleteFunc, pTestSig, dampDetectSignals );
}

int dampDetectSignals(tBmeStruct *pDABme, int portNum, void *par)
{
	memcpy ((char *)&(pDABme->Port[portNum].PortParms.SendSigParms), (char *)par, sizeof(ipos_test_signals));
	return DetectSignals (pDABme, portNum);
}


/* Debug API */

/**************************************************************
**
**	   int  iposBmeMon()
**
**	  * send debug mon command to BME
**		* 
**
**	 return function: IPOS_SUCCESS or IPOS error code
**				 
**	 callback function: status (IPOS_SUCCESS, or error code) indication the completion of the function
**	
**
**
***************************************************************/

static int dampBmeMon(tBmeStruct	*pDABme,  int portNum,  void *par)
{
	return SendDebugMon(pDABme, portNum);
}

int iposBmeMon(ipos_port_id pid, U8 UID, U32 uPar,  iposCbCtl pCompleteFunc)
{

	int		errCode,i;
	int		bmeIdx,portIdx;
	tBmeStruct		*pDABme; 			/* direct API BME structure	*/
	ipos_api_bme	*pApiBme;			/* user PPI BME structure	*/ 

	bmeIdx=pid.bmeNum;

	pApiBme=&apiBme[bmeIdx];
	pDABme=pApiBme->pBme;
    
/* if BME is under IDLE state: Return	*/
	if(iposGetState(pApiBme)==IPOS_BME_IDLE)
	{
		printf("IPOS:Unable to run the API.  BME is idle.\n");
		return (IPOS_ERROR_BME_IDLE);
	}
	
	iposApiEnter(pApiBme);				/*  BME access protcetion	*/
	portIdx=pid.portNum;


	/* call direct API mapping function here	*/
	 pDABme->TxBmeMsg[_BmeMsgSRC_]=0;
	 errCode=dampBmeMon(pDABme,portIdx, NULL);

	
	if(errCode!=IN_PROGRESS)						/* return failed? fatal error	*/
	{
		printf("Direct API Failed in iposUApiCore!\n");
		iposApiLeave(pApiBme);
		return(IPOS_ERROR_DAPI_FAILED);
	}

	i=0;
	while(pollDmaDone(pDABme)!=TRUE) 	/* Read txrdy?	*/
	{
		iposTaskDelay(10);						/* not ready?   wait until ready	*/ 
  		i++;
		if(i>=IPOS_TX_TIMEOUT)	break;
	}  

	if(i>=IPOS_TX_TIMEOUT)
	{
		printf("Tx Time Out  \n");
		iposApiLeave(pApiBme);
		return(IPOS_ERROR_TX_TIMEOUT);
	}

	iposApiLeave(&apiBme[bmeIdx]);			/* leave the API: release the bmeloack	*/
	return(IPOS_SUCCESS);
}



void dumpBmeStruct (void)
{
  int i;

  for (i = 1; i <= IPOS_MAX_BME; i++)
  {
    printf("\nBme #%d, Base address 0x%8lx, State: 0x%x", BmeStruct[i].BmeNum,
           BmeStruct[i].BMEbaseAddr, BmeStruct[i].BmeState);
  }
}

void dumpApiBmeStruct (void)
{
  int i;

  for (i = 1; i <= IPOS_MAX_BME; i++)
  {
    printf("\nBme #%d, Bme address 0x%8lx, State: 0x%x", apiBme[i].bmeNum,
           (U32)apiBme[i].pBme, apiBme[i].bmeState);
  }
}

extern void dmaRead (int bmeNum, UINT32 srcAddr, BYTE  * destAddr, UINT32 size);


#define BME_VER_LOCATION  0x40008500
#define BME_VER_MAX_LEN   256
void iPosPrintVer(int bmeNum)
{
    char buf[512];
    
    buf[0]='\0';

#if defined(INI_SUPPORT)
    iposPtmfGetVersion(buf);
    printf("%s\n",buf);
#endif //INI_SUPPORT
    dmaRead(bmeNum, BME_VER_LOCATION, buf, BME_VER_MAX_LEN);
    printf("%s\n", buf);
    printf("iPOS Ver %s\n", VERSION_STR);
}

// BME memory configuration for CPE20 
#ifdef	BME_CPE_20
int iposBmeConfigMemBlk(int BmeNum, ipos_emi_config *pMemCfg)
{
	tBmeStruct  * bmeStruct;
	bmeStruct = &BmeStruct[BmeNum];	 

	bmeStruct->EMIConfig.memType=pMemCfg->memType;
	if(pMemCfg->memType==IPOS_BME_MEM_SRAM)
	{

		bmeStruct->EMIConfig.sramMemSize=pMemCfg->memSize;
	}
	else
	{
		bmeStruct->EMIConfig.sdramMemSize=pMemCfg->memSize;
	}
	return(IPOS_SUCCESS);
}
/******************************************************************************
*                                                                             *
* Function.. : iposBmeGpioConfig                         	                    *
*                                                   	                        *
* Purpose... : Configures the BME CPE2.0 GPIO pins for either input or output *
*									                                                            *
* Parameters : Bme Number, GPIO pin number and direction                      *
*                                                                             *
* Returns... : Success or Failure                                             *
*                                                                             *
******************************************************************************/
int iposBmeGpioConfig(int BmeNum, U8 uiGpioPinNumber, U32 uiGpioDirection)
{
  U32 uiBmeRegValue;
  U32 uiBmeGpioPinDir = 1;

  /* If Passed GPIO pin number is zero, then apply the direction setting for
     all the PINS */
  if (uiGpioPinNumber == 0)
  {
    uiBmeRegValue = uiGpioDirection;
  }
  else
  {
    /* First read the existing GPIO pin direction mask  */
    uiBmeRegValue = eiuPioRead(BmeNum, GPIO_PDR(0));

    uiBmeGpioPinDir = uiBmeGpioPinDir << (uiGpioPinNumber - 1);

    if (uiGpioDirection)
      uiBmeRegValue |= uiBmeGpioPinDir;   // GPIO = Output
    else
      uiBmeRegValue &= ~uiBmeGpioPinDir;  // GPIO = Input
  }
  tpw (BmeNum,GPIO_PDR(0), uiBmeRegValue);

	return(IPOS_SUCCESS);
}

/******************************************************************************
*                                                                             *
* Function.. : iposBmeGpioMaskRead                                            *
*                                                   	                      *
* Purpose... : Read the BME CPE2.0 GPIO pin Mask for to know either input     *
*              or output (1 = Output, 0 = Input)                              *
* Parameters : Bme Number, direction                                          *
*                                                                             *
* Returns... : Success or Failure                                             *
*                                                                             *
******************************************************************************/
int iposBmeGpioMaskRead(int BmeNum, U32 *uiGpioMaskValue)
{
  *uiGpioMaskValue = eiuPioRead(BmeNum, GPIO_PDR(0));
  return(IPOS_SUCCESS);
}

/******************************************************************************
*                                                                             *
* Function.. : iposBmeGpioWrite                         	                    *
*                                                   	                        *
* Purpose... : writes a one or zero to a particular GPIO pin                  *
*									                                                            *
* Parameters : Bme Number, GPIO pin number and direction                      *
*                                                                             *
* Returns... : Success or Failure                                             *
*                                                                             *
******************************************************************************/
int iposBmeGpioWrite(int BmeNum, U8 uiGpioPinNumber, U32 uiGpioOut)
{
  U32 uiBmeRegValue;
  U32 uiBmeGpioPinOut = 1;

  /* If Passed GPIO pin number is zero, then apply the output setting for
     all the PINS */
  if (uiGpioPinNumber == 0)
  {
    uiBmeRegValue = uiGpioOut & 0xff;
  }
  else
  {
    /* First read the existing GPIO pin direction mask  */
    uiBmeRegValue = eiuPioRead(BmeNum, GPIO_PDR(0));
    uiBmeRegValue &= 0xff;

    uiBmeGpioPinOut = uiBmeGpioPinOut << (uiGpioPinNumber - 1);

    if ((uiBmeRegValue & uiBmeGpioPinOut) != uiBmeGpioPinOut)
      return IPOS_ERROR_NOT_IMPLEMENTED;

    /* Now read the Write data register (Read-Modiy-Write)  */
    uiBmeRegValue = eiuPioRead(BmeNum, GPIO_WDR(0));

    if (uiGpioOut)
      uiBmeRegValue |= uiBmeGpioPinOut;   // GPIO PIN = 1
    else
      uiBmeRegValue &= ~uiBmeGpioPinOut;  // GPIO PIN = 0
  }
  tpw (BmeNum,GPIO_WDR(0), uiBmeRegValue);

	return(IPOS_SUCCESS);
}

/******************************************************************************
*                                                                             *
* Function.. : iposBmeGpioRead                          	                    *
*                                                   	                        *
* Purpose... : Reads the GPIO pin                                             *
*									                                                            *
* Parameters : Bme Number, GPIO pin number                                    *
*                                                                             *
* Returns... : Value of the GPIO pin(either 0 or 1)                           *
*                                                                             *
******************************************************************************/
U32 iposBmeGpioRead(int BmeNum, U8 uiGpioPinNumber)
{
  U32 uiBmeRegValue;
  U32 uiBmeGpioPinRead = 1;

  /* Now read the GPIO Read Data Register  */
  uiBmeRegValue = eiuPioRead(BmeNum, GPIO_RDR(0));
  uiBmeRegValue &= 0xff;

  /* If Passed GPIO pin number is zero, then return the complete register */
  if (uiGpioPinNumber == 0)
    return(uiBmeRegValue);
  else
  {
    uiBmeGpioPinRead = uiBmeGpioPinRead << (uiGpioPinNumber - 1);
    if (uiBmeGpioPinRead & uiBmeRegValue)
      return 1;
    else
      return 0;
  }
}

#endif

