/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   (C) Copyright Intel Corp., 1996

   File: process.c

   Universal Serial Bus 930 Test firmware

   Revision History
   ---------------------------------------------
    0.1     05-27-96          Abdul Rahman Ismail
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

#include <8x930ax.h>
#include "defs.h"

#include "process.h"

#ifdef	ALT_X_FACE
//	external functions. - altxface.asm
// extern	void FillEP1Fifo( void );
// extern	void FillEP2Fifo( void );
 extern	void TestThing( void );
 extern	void SOF_Init_FIFOs( void );
#endif

// External variables.  - Assemdbly file
// #ifndef	IR_DONGLE
// extern	WORD	EP1RxPtr;
// extern	WORD	EP1TxPtr;
// extern	WORD	EP1DataCount;

// extern	WORD	EP2RxPtr;
// extern	WORD	EP2TxPtr;
// extern	WORD	EP2DataCount;

// extern	WORD	EP3RxPtr;
// extern	WORD	EP3TxPtr;
// extern	WORD	EP3DataCount;
// #endif

#ifdef	ALT_X_FACE
// extern	BYTE	gbEP1Running;
// extern	BYTE	gbEP2Running;
extern	BYTE	gbMPSEndPoint1;
extern	BYTE	gbMPSEndPoint2;
#endif

// Global function prototypes.
// reentrant void SetFeature( BYTE bRequestType, WORD wValue, WORD wIndex );
// reentrant void StallEndPoint( WORD wEndPointAddress );

// reentrant void ClearFeature( BYTE bRequestType, WORD wValue, WORD wIndex );
// reentrant void EnableEndPoint( WORD wEndPointAddress );

// reentrant WORD GetStatus( BYTE bRequestType, WORD wIndex  );

#ifdef	ALT_X_FACE
reentrant void SetInterface( WORD wAlternateSetting, WORD wInterfaceNo  );
reentrant void SetLEDs( BYTE val );
#endif

/*-----------------------------------------------------------------------------
Function name     : StallEndPoint
Brief Description : Stalls the endpoint if valid endpoint recd.
-----------------------------------------------------------------------------*/

// reentrant void StallEndPoint( WORD wEndPointAddress )
// {
//     BYTE	bStallEPMask;

//     if ((wEndPointAddress & GET_EP_DIRECTION) == HOST_2_DEV)
//     	bStallEPMask = STALL_RX;
//     else
//     	bStallEPMask = STALL_TX;

//     wEndPointAddress &= GET_EP_NUMBER;

//     EPINDEX = (BYTE) wEndPointAddress;

//     switch (wEndPointAddress)
// 	{
// 	case 0:
// 	    RXSTL = TRUE;
// 	    TXSTL = TRUE;
// 	    break;

// 	case 1:
// 	case 2:
// 	case 3:
// 	    EPCON |= bStallEPMask;
//             break;
// 	}
// }

// /*-----------------------------------------------------------------------------
// Function name     : SetFeature
// Brief Description : Decodes the setup packet and performs the reqd. task.
// -----------------------------------------------------------------------------*/

// reentrant void SetFeature( BYTE bRequestType, WORD wValue, WORD wIndex )
// {
//     BYTE	bRecipient;

//     bRecipient = bRequestType & GET_RECIPIENT;

//     if (bRecipient == RECEIPIENT_INTERFACE)
//     	return;

//     if (bRecipient == RECEIPIENT_DEVICE) 
// 	{  
// 	    if (wValue == DEVICE_REMOTE_WAKEUP)
//         	; // Do something....
// 	}
//     else
// 	{
// 	    if (wValue == ENDPOINT_STALL)
//         	StallEndPoint( wIndex );
// 	}
// }

// /*-----------------------------------------------------------------------------
// Function name     : EnableEndPoint
// Brief Description : Enables the endpoint if valid endpoint recd.
// -----------------------------------------------------------------------------*/

// reentrant void EnableEndPoint( WORD wEndPointAddress )
// {
//     BYTE	bEnableEPTX;

//     if ((wEndPointAddress & GET_EP_DIRECTION) == HOST_2_DEV)
//     	bEnableEPTX = FALSE;
//     else
//     	bEnableEPTX = TRUE;

//     wEndPointAddress &= GET_EP_NUMBER;
//     EPINDEX = (BYTE) wEndPointAddress;
//     switch (wEndPointAddress)
// 	{
//     	case 0:
// 	    RXSTL = FALSE;
// 	    TXSTL = FALSE;
//             TXCLR = TRUE;
//             RXCLR = TRUE;
// 	    break;

// 	case 1:
// 	    if( bEnableEPTX == TRUE )
// 		{
// 		    TXSTL = FALSE;
// 		    TXCLR  = TRUE;
// 		}
// 	    else
// 		{
// 		    RXSTL = FALSE;
// 		    RXCLR  = TRUE;
// 		}
// #ifndef		IR_DONGLE
//             EP1TxPtr	 = 0;
//             EP1RxPtr	 = 0;
//             EP1DataCount = 0;
// #endif
//             break;

// 	case 2:
// 	    if( bEnableEPTX == TRUE )
// 		{
// 		    TXSTL = FALSE;
// 		    TXCLR  = TRUE;
// 		}
// 	    else
// 		{
// 		    RXSTL = FALSE;
// 		    RXCLR  = TRUE;
// 		}
// #ifndef		IR_DONGLE
//             EP2TxPtr	 = 0;
//             EP2RxPtr	 = 0;
//             EP2DataCount = 0;
// #endif
//             break;

// 	case 3:
// 	    if( bEnableEPTX == TRUE )
// 		{
// 		    TXSTL = FALSE;
// 		    TXCLR  = TRUE;
// 		}
// 	    else
// 		{
// 		    RXSTL = FALSE;
// 		    RXCLR  = TRUE;
// 		}
// #ifndef		IR_DONGLE
//             EP3TxPtr	 = 0;
//             EP3RxPtr	 = 0;
//             EP3DataCount = 0;
// #endif
//             break;
// 	}
// }

// /*-----------------------------------------------------------------------------
// Function name     : ClearFeature
// Brief Description : Decodes the setup packet and performs the reqd. task.
// -----------------------------------------------------------------------------*/

// reentrant void ClearFeature( BYTE bRequestType, WORD wValue, WORD wIndex )
// {
//     BYTE	bRecipient;

//     bRecipient = bRequestType & GET_RECIPIENT;

//     if (bRecipient == RECEIPIENT_INTERFACE)
//     	return;

//     if (bRecipient == RECEIPIENT_DEVICE) 
// 	{  
// 	    if (wValue == DEVICE_REMOTE_WAKEUP)
//         	P1 = 0x55;
// 	    ; // Do something....
// 	}
//     else
// 	{
// 	    if (wValue == ENDPOINT_STALL)
//         	EnableEndPoint( wIndex );
// 	}
// }

// /*-----------------------------------------------------------------------------
// Function name     : GetStatus
// Brief Description : Decodes the setup packet and performs the reqd. task.
// -----------------------------------------------------------------------------*/

// reentrant WORD GetStatus( BYTE bRequestType, WORD wIndex  )
// {
//     BYTE bRecipient;
//     WORD wStatus = 0;
//     BYTE bDirection;

//     // Get the recipient bits so you know what to do
//     bRecipient = bRequestType & GET_RECIPIENT;

//     switch (bRecipient) {
//     case RECEIPIENT_DEVICE:
// 	/* TBD!!!  In the future, look at the real descriptor
// 	   data (config descr) and return based on that value */
// 	wStatus = 1; /* Device not a remote wakeup source
// 			Device is self powered 
// 			*/
//         break;
        
//     case RECEIPIENT_INTERFACE:
// 	wStatus = 0;
//         break;
        
//     case  RECEIPIENT_ENDPOINT:
// 	if ((wIndex & GET_EP_DIRECTION) == HOST_2_DEV)
// 	    bDirection = HOST_2_DEV;
// 	else 
// 	    bDirection = DEV_2_HOST;
        
// 	wIndex &= GET_EP_NUMBER;

// 	/* Endpoint number */
// 	EPINDEX = (BYTE) wIndex;

// 	switch (wIndex) {
// 	case 0:
// 	    /* We always return OK for EP 0 (can never be stalled) */
// 	    break;

// 	case 1:
// 	case 2:
// 	case 3:
// 	    if (bDirection == HOST_2_DEV) {
// 		if (RXSTL == TRUE)
// 		    wStatus = TRUE;
// 	    }
// 	    else {
// 		if (TXSTL == TRUE)
// 		    wStatus = TRUE;
// 	    } /*if-else*/

// 	    break;

// 	default:
// 	    break;
            
// 	} /* switch */

//         break;

//     default:
//         break;

//     } // switch

//     return (wStatus);

// }/* GetStatus */

#ifdef	ALT_X_FACE
/*-----------------------------------------------------------------------------
Function name     : SetLEDs
Brief Description : Just does an out to P1
-----------------------------------------------------------------------------*/
reentrant void SetLEDs( BYTE val )
{
  //  P1 = val;
  TestThing();
}

/*-----------------------------------------------------------------------------
Function name     : SetInterface
Brief Description : Decodes the setup packet and performs the reqd. task.
-----------------------------------------------------------------------------*/

reentrant void SetInterface( WORD wAlternateSetting, WORD wInterfaceNo  )
{
    if (wInterfaceNo != 0)
    	return;

    switch (wAlternateSetting)
	{
    	case	0x00:
	    gbMPSEndPoint1 = MPS_EP1_AX0;
            gbMPSEndPoint2 = MPS_EP2_AX0;
            break;

    	case	0x01:
	    gbMPSEndPoint1 = MPS_EP1_AX1;
            gbMPSEndPoint2 = MPS_EP2_AX1;
            break;

    	case	0x02:
	    gbMPSEndPoint1 = MPS_EP1_AX2;
            gbMPSEndPoint2 = MPS_EP2_AX2;
            break;

    	case	0x03:
	    gbMPSEndPoint1 = MPS_EP1_AX3;
            gbMPSEndPoint2 = MPS_EP2_AX3;
            break;

    	case	0x04:
	    gbMPSEndPoint1 = MPS_EP1_AX4;
            gbMPSEndPoint2 = MPS_EP2_AX4;
            break;

	case	0x05:
	    gbMPSEndPoint1 = MPS_EP1_AX5;
            gbMPSEndPoint2 = MPS_EP2_AX5;
            break;
	}


    SOF_Init_FIFOs();
}
#endif

