/** @file watchdog.c
 
   Contains watchdog ioctl functions.
 
   @par CREATION  
   @author Krauth Julien
   @date   03.04.2007
   
   @par VERSION
   @verbatim
   $LastChangedRevision:$
   $LastChangedDate:$
   @endverbatim   
   
   @par LICENCE
   @verbatim
   Copyright (C) 2004,2005  ADDI-DATA GmbH for the source code of this module.
        
        ADDI-DATA GmbH 
        Dieselstrasse 3 
        D-77833 Ottersweier 
        Tel: +19(0)7223/9493-0 
        Fax: +49(0)7223/9493-92 
        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 "apci2200-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

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

/** Initialise the watchdog.                                                 
 * 
 * @param [in] pdev               : The device to initialize.
 * @param [in] arg (l_DelayValue) : Watchdog delay value in step of 20ms (1 to 255).
 *
 * @retval 0: No error.
 * @retval 1: The handle parameter of the board is wrong.
 * @retval 2: Wrong watchdog delay value (has to be 1 to 255).
 */
int do_CMD_APCI2200_InitWatchdog (struct pci_dev *pdev, unsigned int cmd, unsigned long arg)   
{			
	int ret;
	{
		unsigned long irqstate;
		if (APCI2200_LOCK(pdev,&irqstate))
 			return -ERESTARTSYS;
		{
			ret = i_APCI2200_InitWatchdog (pdev, arg);
		}
		APCI2200_UNLOCK(pdev,irqstate);
	}
	return ret;
}	

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

/** Start the watchdog.                                                 
 * 
 * @param [in] pdev         : The device to initialize.
 *
 * @retval 0: No error.
 * @retval 1: The handle parameter of the board is wrong.
 * @retval 2: Watchdog already started.
 */
int do_CMD_APCI2200_StartWatchdog (struct pci_dev *pdev, unsigned int cmd, unsigned long arg)   
{	
	int ret;
	{
		unsigned long irqstate;
		if (APCI2200_LOCK(pdev,&irqstate))
 			return -ERESTARTSYS;
		{
			ret = i_APCI2200_StartWatchdog (pdev);
		}
		APCI2200_UNLOCK(pdev,irqstate);
	}
	return ret;
}		

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

/** Stop the watchdog.                                                 
 * 
 * @param [in] pdev         : The device to initialize.
 *
 * @retval 0: No error.
 * @retval 1: The handle parameter of the board is wrong.
 * @retval 2: Watchdog not started.
 */
int do_CMD_APCI2200_StopWatchdog (struct pci_dev *pdev, unsigned int cmd, unsigned long arg)   
{	int ret;
	{
		unsigned long irqstate;
		if (APCI2200_LOCK(pdev,&irqstate))
 			return -ERESTARTSYS;
		{
			ret = i_APCI2200_StopWatchdog (pdev);
		}
		APCI2200_UNLOCK(pdev,irqstate);
	}
	return ret;
}		

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

/** Trigger the watchdog.                                                 
 * 
 * @param [in] pdev         : The device to initialize.
 *
 * @retval 0: No error.
 * @retval 1: The handle parameter of the board is wrong.
 * @retval 2: Watchdog not started.
 */
int do_CMD_APCI2200_TriggerWatchdog (struct pci_dev *pdev, unsigned int cmd, unsigned long arg)   
{
	int ret;
	{
		unsigned long irqstate;
		if (APCI2200_LOCK(pdev,&irqstate))
 			return -ERESTARTSYS;
		{
			ret = i_APCI2200_TriggerWatchdog (pdev);
		}
		APCI2200_UNLOCK(pdev,irqstate);
	}
	return ret;
}													

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

/** Get the watchdog status.                                                 
 * 
 * @param [in] pdev         : The device to initialize.
 * @param [out] b_Status    : Watchdog status:<br>
 *								0 : No overlow occur<br>
 *								1 : Overlow occur
 * 
 * @retval 0: No error.
 * @retval 1: The handle parameter of the board is wrong.
 * @retval 2: Watchdog not started.
 * @retval -EFAULT : Fail to export user data. 
 */
int do_CMD_APCI2200_GetWatchdogStatus (struct pci_dev *pdev, unsigned int cmd, unsigned long arg)
{											
	uint8_t b_Tmp = 0;	
	int ret;
	
	{
		unsigned long irqstate;
		if (APCI2200_LOCK(pdev,&irqstate))
 			return -ERESTARTSYS;
		{
			ret = i_APCI2200_GetWatchdogStatus (pdev, &b_Tmp);
		}
		APCI2200_UNLOCK(pdev,irqstate);
	}									
	if (ret != 0)
		return (ret);		
			
	if ( copy_to_user( (uint8_t __user *)arg , &b_Tmp, sizeof(b_Tmp) ) )
		return -EFAULT;													
												
	return (ret);	
}

//------------------------------------------------------------------------------
	
/** Get the watchdog software trigger status.                                                 
 * 
 * @param [in] pdev         : The device to initialize.
 * @param [out] b_Status    : Watchdog software trigger status:<br>
 *								0 : No software trigger occur <br>
 *								1 : Software trigger occur
 * 
 * @retval 0: No error.
 * @retval 1: The handle parameter of the board is wrong.
 * @retval 2: Watchdog not started.
 * @retval -EFAULT : Fail to export user data. 
 */
int do_CMD_APCI2200_GetWatchdogSoftwareTriggerStatus (struct pci_dev *pdev, unsigned int cmd, unsigned long arg)
{											
	uint8_t b_Tmp = 0;
	int ret;
	{
		unsigned long irqstate;
		if (APCI2200_LOCK(pdev,&irqstate))
 			return -ERESTARTSYS;
		{									
			ret = i_APCI2200_GetWatchdogSoftwareTriggerStatus (pdev, &b_Tmp);
		}
		APCI2200_UNLOCK(pdev,irqstate);
	}									
	if (ret)
		return (ret);		
			
	if ( copy_to_user( (uint8_t __user *)arg , &b_Tmp, sizeof(b_Tmp) ) )
		return -EFAULT;													
												
	return (ret);
}

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




