/** @file utils.c
 
	General use utility functions
 
   @par CREATION  
   @author Sylvain Nahas
   @date   19.11.2007
   
   @par VERSION
   @verbatim
   $LastChangedRevision:$
   $LastChangedDate:$
   @endverbatim   
   
   @par LICENCE
   @verbatim
    Copyright (C) 2009  ADDI-DATA GmbH for the source code of this module.
        
    ADDI-DATA GmbH
    Airpark Business Center
    Airport Boulevard B210
    77836 Rheinm�nster
    Germany
    Tel: +49(0)7229/1847-0
    Fax: +49(0)7229/1847-200
    http://www.addi-data-com
    info@addi-data.com
        
   This program is free software; you can redistribute it and/or modify it under 
   the terms of the GNU General Public License as published by the Free Software 
   Foundation; either version 2 of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful, 
   but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
   or FITNESS FOR A PARTICULAR PURPOSE. 
   See the GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along with 
   this program; if not, write to the Free Software Foundation, 
   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

   You shoud also find the complete GPL in the COPYING file 
   accompanying this source code.
   @endverbatim   
 */ 

#include "xpci3009-private.h"

/**@def EXPORT_NO_SYMBOLS
 * Function in this file are not exported.
 */
EXPORT_NO_SYMBOLS;

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,27)
	#define __user
#endif

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
  #define __iomem
#endif

//------------------------------------------------------------------------------

/** returns the content of the 4 modules configuration register (ID).
 * 
 * This information helps to identify the type of module that is installed on the board.
 * 
 * @param [in] argv                  : A pointer to an array of 4 unsigned long
 * 
 * @retval 0: No error.
 * @retval -EFAULT : Failed to copy data in user space (incorrect pointer). 
 * 
 * The 2 first bytes identify the functionality ( see enumeration below )
 * the two last byte is the firmware version 
 *  
 */ 
int do_CMD_XPCI3009_GetModulesId (struct pci_dev *pdev, unsigned int cmd, unsigned long arg)
{
	uint32_t Functionalities[4];
	uint8_t i = 0;
	uint8_t number_module = NUMBER_OF_MODULE(pdev);
	
	/* note: call to i_APCI1710_GetFunctionality() should not fail */

	i_XPCI3009_ReadModulesConfiguration(pdev);

	for ( i = 0 ; i < number_module ; i++ )
	{
		i_XPCI3009_GetModuleId (pdev,i, &(Functionalities[i]) );
	}
	
	if ( copy_to_user( (unsigned int __user *)arg , Functionalities, sizeof(Functionalities) ) )
		return -EFAULT;	

	return 0;
}

//------------------------------------------------------------------------------

/** Get the board type
 *
 * @param [out] arg : Board type:
 * 					  - APCI1710_10K10_BOARD_TYPE
 * 					  - APCI1710_10K20_BOARD_TYPE
 * 					  - APCIE1711_BOARD_TYPE
 * 					  - UNKNOWN_BOARD_TYPE
 *
 * @retval 0: No error.
 * @retval -EFAULT : Failed to copy data in user space (incorrect pointer).
 */
int do_CMD_XPCI3009_GetBoardType (struct pci_dev *pdev, unsigned int cmd, unsigned long arg)
{
	uint32_t boardType = 0;

	if (i_XPCI3009_GetBoardType (pdev, &boardType) != 0)
		return -EFAULT;

	if ( copy_to_user( (uint32_t __user *)arg , &boardType, sizeof(uint32_t) ) )
		return -EFAULT;

	return 0;
}

//------------------------------------------------------------------------------

/** Read data directly from the board
 * 	In the user space, the user can specify an address to read
 *
 * This function can be changed, it is recommended NOT TO USE IT!
 *
 * @param [in] arg :	arg[0] : Bar to access
 * 						arg[1] : How many bytes (1, 2, 4, 8)
 * 						arg[2] : Offset
 *
 * @param [out] arg :
 * 						arg[0] : Value
 *
 * @retval 0: No error.
 * @retval -EFAULT : Failed to copy data in user space (incorrect pointer).
 * @retval -EADDRNOTAVAIL : Type of access not available.
 */
int do_CMD_XPCI3009_READ (struct pci_dev *pdev, unsigned int cmd, unsigned long arg)
{
	uint32_t dw_ArgArray[3];
	unsigned long irqstate;
	uint32_t base_address = 0;
	int method = -1;

	if ( copy_from_user (dw_ArgArray, (uint32_t __user *)arg, sizeof(dw_ArgArray) ) )
		return -EFAULT;

	/* What is to be accessed */
	switch (dw_ArgArray[0])
	{
		case 0: // BAR 0
			break;
		case 1: // BAR 1
			break;
		case 2: // BAR 2
			// IO MAPPED
			method = 0;
			base_address = GET_BAR2(pdev);
			break;
		case 3: // BAR 3
			// MEMORY MAPPED
			method = 1;
			base_address = (uint32_t) GET_MEM_BAR3(pdev);
			break;

		default:
			return -EFAULT;
			break;
	}

	switch ( method )
	{
		case 0:
			// IO MAPPING ACCESS
			XPCI3009_LOCK(pdev,&irqstate);
			{
				INPDW( base_address + dw_ArgArray[2] , (uint32_t*) &dw_ArgArray[0] );
			}
			XPCI3009_UNLOCK(pdev,irqstate);
			break;
		case 1:
			// MEMORY MAPPING ACCESS
			XPCI3009_LOCK(pdev,&irqstate);
			{
				INPDW_MEM( (void __iomem *)base_address, dw_ArgArray[2], (uint32_t*) &dw_ArgArray[0] );
			}
			XPCI3009_UNLOCK(pdev,irqstate);
			break;

		default:
			return -EFAULT;
			break;
	}


	if ( copy_to_user( (uint32_t __user *)arg , &dw_ArgArray[0], sizeof(uint32_t) ) )
		return -EFAULT;

	return 0;
}

/** Write data directly the board
 * 	In the user space, the user can specify an address to read
 *
 * This function can be changed, it is recommended NOT TO USE IT!
 *
 * @param [in] arg :	arg[0] : Bar to access
 * 						arg[1] : How many bytes (1, 2, 4, 8)
 * 						arg[2] : Offset
 * 						arg[3] : Value
 *
 *
 * @retval 0: No error.
 * @retval -EFAULT : Failed to copy data in user space (incorrect pointer).
 * @retval -EADDRNOTAVAIL : Type of access not available.
 */
int do_CMD_XPCI3009_WRITE (struct pci_dev *pdev, unsigned int cmd, unsigned long arg)
{
	uint32_t dw_ArgArray[4];
	unsigned long irqstate;
	uint32_t base_address = 0;
	int method = -1;

	if ( copy_from_user (dw_ArgArray, (uint32_t __user *)arg, sizeof(dw_ArgArray) ) )
		return -EFAULT;

	/* What is to be accessed */
	switch (dw_ArgArray[0])
	{
		case 0: // BAR 0
			break;
		case 1: // BAR 1
			break;
		case 2: // BAR 2
			// IO MAPPED
			method = 0;
			base_address = GET_BAR2(pdev);
			break;
		case 3: // BAR 3
			// MEMORY MAPPED
			method = 1;
			base_address = (uint32_t) GET_MEM_BAR3(pdev);
			break;

		default:
			return -EFAULT;
			break;
	}

	switch ( method )
	{
		case 0:
			// IO MAPPING ACCESS
			XPCI3009_LOCK(pdev,&irqstate);
			{
				OUTPDW( base_address + dw_ArgArray[2] , (uint32_t) dw_ArgArray[3] );
			}
			XPCI3009_UNLOCK(pdev,irqstate);
			break;
		case 1:
			// MEMORY MAPPING ACCESS
			XPCI3009_LOCK(pdev,&irqstate);
			{
				OUTPDW_MEM( (void __iomem *)base_address, dw_ArgArray[2], (uint32_t) dw_ArgArray[3] );
			}
			XPCI3009_UNLOCK(pdev,irqstate);
			break;
		default:
			return -EFAULT;
			break;
	}

	return 0;
}
