/*****************************************************************************

Copyright (c) 2006 Analog Devices.  All Rights Reserved.

This software is proprietary and confidential.  By using this software you agree
to the terms of the associated Analog Devices License Agreement.  
******************************************************************************

Project:    Audio codec demonstration
Title:      Install services
Author(s):  BMK
Revised by:
			DW
			SS

Description:
		This file contains functions to setup and configure the USB file I/O. 

References:
			
*********************************************************************************/

#include <system.h>							// system service includes
#include <SDK-ezkitutilities.h> 	// SDK EZ-Kit utilities
#include <adi_ssl_Init.h>

// USB includes
#include <drivers\usb\usb_core\adi_usb_objects.h>
#include <drivers\usb\usb_core\adi_usb_core.h>
#include <adi_usbcmd.h>							// supported USB commands
#include <drivers\usb\class\peripheral\vendor_specific\adi\bulkadi\adi_usb_bulkadi.h>	// adi bulk driver header file
#include <drivers\usb\controller\peripheral\plx\net2272\adi_usb_net2272.h>				// net2272 driver header file
#include <adi_usbio_blackfin.h>					// USB I/O functions for Blackfin

#include <usb_services.h>
#include <stdio.h>
#include <codec.h>




// handle to the USB controller device driver
ADI_DEV_PDD_HANDLE PeripheralDevHandle;	


/*****************
Functions prototype
******************/
// USB driver callback
static void USB_Callback (void *AppHandle, u32  Event, void *pArg);
//void EnableUSBdriver	(void);
//void InstallUSBdriver(void);
//u32 PerformUSBIO(ADI_DEV_DEVICE_HANDLE USB_DevHandle);



/*********************************************************************

	Function:		PerformIO

	Description:	Performs USB I/O

*********************************************************************/
//section("sdram0_bank1")
u32	PerformUSBIO(ADI_DEV_DEVICE_HANDLE USB_DevHandle)
{
    u32	Result = ADI_DEV_RESULT_SUCCESS;

	// Set USB as default I/O device
	Result = ConfigUSB_IO(USB_DevHandle);
		
	// continue only if USB IO config is a success
	if (Result == ADI_DEV_RESULT_SUCCESS)
	{
		do_decode();
		
		// Done with audio decoding. Stop USB I/O
    	StopUSB_IO (USB_DevHandle);
	}
	
	return (Result);
}






/*********************************************************************

	Function:		NET2272 callback handler

					We'll get a callback when the NET2272
					has completed processing of the buffer.  We don't
					really need the callback function as the driver
					uses the ProcessedFlag field of the buffer to
					determine when the buffer has completed processing,
					but it's included here for illustrative purposes.

*********************************************************************/
static void USB_Callback(
	void *AppHandle,
	u32  Event,
	void *pArg)
{

	switch (Event)
	{
		// case (NET2272 device configured)
		case ADI_USB_EVENT_SET_CONFIG:

			// pArg holds the configuration number
			if (0x01 == (u32)pArg)
				Net2272Ready = TRUE;	// Net2272 configuration Done
			else
				Net2272Ready = FALSE;	// Net2272 configuration Failed

			break;

		// case (reset signaling detected)
		case ADI_USB_EVENT_ROOT_PORT_RESET:
		// case (cable unplugged)
		case ADI_USB_EVENT_VBUS_FALSE:
		
			// Net2272 yet to be configured
			Net2272Ready = FALSE;

			break;
	    
		// case (USB data Tx complete)
		case ADI_USB_EVENT_DATA_TX:

			Net2272TxDone = TRUE;	// Tx Done

			break;

		// case (USB data Rx complete)
		case ADI_USB_EVENT_DATA_RX:

			Net2272RxDone = TRUE;	// Rx Done

			break;
									
		// CASE (an error)
		case ADI_DEV_EVENT_DMA_ERROR_INTERRUPT:

			// turn on all LEDs and wait for help
			ezTurnOnAllLEDs();
			while (1) ;
		
		default:
			break;
	}
	    
	return;
}



/*********************************************************************

    Function:       InstallUSBdriver

    Description:    Installs USB device

*********************************************************************/

void InstallUSBdriver(void)	
{

	void *pConfig = NULL;
									
	// initialize USB Core
    adi_usb_CoreInit(pConfig);

    // open the USB controller driver
    ezErrorCheck( adi_dev_Open(	adi_dev_ManagerHandle, 			// DevMgr handle 
								&ADI_USB_NET2272_Entrypoint,	// pdd entry point 
								0,                          	// device instance 
								(void*)1,						// client handle callback identifier 
								&PeripheralDevHandle,	        // device handle 
								ADI_DEV_DIRECTION_BIDIRECTIONAL,// data direction for this device 
								adi_dma_ManagerHandle,			// handle to DmaMgr for this device 
								NULL,							// handle to deferred callback service 
								USB_Callback));		  	    	// client's callback function 
			
	// open the vendor specific bulk USB class driver
	ezErrorCheck( adi_dev_Open(	adi_dev_ManagerHandle,			// DevMgr handle
								&ADI_USB_VSBulk_Entrypoint,		// pdd entry point
								0,								// device instance
								(void*)0x1,						// client handle callback identifier
								&USBDeviceHandle,						// DevMgr handle for this device
								ADI_DEV_DIRECTION_BIDIRECTIONAL,// data direction for this device
								adi_dma_ManagerHandle,			// handle to DmaMgr for this device
								NULL,							// handle to deferred callback service
								USB_Callback));					// client's callback function
	
		
		
	// give USB controller driver handle to the class drivers.
	ezErrorCheck( adi_dev_Control(USBDeviceHandle, ADI_USB_CMD_CLASS_SET_CONTROLLER_HANDLE, (void*)PeripheralDevHandle) );
	// configure the class driver
	ezErrorCheck( adi_dev_Control(USBDeviceHandle, ADI_USB_CMD_CLASS_CONFIGURE, (void*)0) );
	// configure the mode
	ezErrorCheck( adi_dev_Control(USBDeviceHandle, ADI_DEV_CMD_SET_DATAFLOW_METHOD, (void*)ADI_DEV_MODE_CHAINED) );
	// enable data flow
	ezErrorCheck( adi_dev_Control(USBDeviceHandle, ADI_DEV_CMD_SET_DATAFLOW, (void*)TRUE) );

	return;
}


/*********************************************************************

    Function:       EnableUSBdriver

    Description:    Enables USB device

*********************************************************************/
void EnableUSBdriver(void)	
{
	// enable USB
	ezErrorCheck( adi_dev_Control(PeripheralDevHandle, ADI_USB_CMD_ENABLE_USB, (void*)0 ));

	return;
}



