 /* @file apci1710-kapi.h
  * 
  * @brief Define the public API for the apci1710 ADDI-DATA board. 
  * 
  *
  */

/** @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 library is free software; you can redistribute it and/or modify it under 
 the terms of the GNU Lesser General Public License as published by the 
 Free Software Foundation; either version 2.1 of the License, 
 or (at your option) any later version.
 
 This library 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 Lesser General Public License for more details.
 
 You should have received a copy of the GNU Lesser General Public License 
 along with this library; if not, write to the Free Software Foundation, 
 Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
 
 You also shoud find the complete LGPL in the LGPL.txt file accompanying 
 this source code.
 
 @endverbatim
**/

#ifndef __APCI2200_KAPI_H__
#define __APCI2200_KAPI_H__

#include "apci.h"

#ifndef __KERNEL__
#error This header is intended for use in kernel context only
#endif

#ifdef __KERNEL__

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

/** Read 8 digital input.                                                 
 * 
 * @param [in] pdev                 : The device to initialize.
 * @param [out] b_DigitalInputValue : Digital input value.
 *
 * @retval 0: No error.
 * @retval 1: The handle parameter of the board is wrong.
 */
int   i_APCI2200_Read8DigitalInput (struct pci_dev *pdev, uint8_t *b_DigitalInputValue);  

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

/** Set digital ouptput memory on.          
 * 
 * After having called up this function, the output you have previously
 * activated with the function "i_APCI2200_SetXDigitalOutputOn" are not reset.
 * You can reset them with the function "i_APCI2200_SetXDigitalOutputOff".                                         
 * 
 * @param [in] pdev                 : The device to initialize.
 *
 * @retval 0: No error.
 * @retval 1: The handle parameter of the board is wrong.
 * @retval 2: Output memory is already set on.
 */
int   i_APCI2200_SetDigitalOutputMemoryOn (struct pci_dev *pdev);

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

/** Set digital ouptput memory off.                                                 
 * 
 * @param [in] pdev                 : The device to initialize.
 *
 * @retval 0: No error.
 * @retval 1: The handle parameter of the board is wrong.
 * @retval 2: Output memory is already set off.
 */
int   i_APCI2200_SetDigitalOutputMemoryOff (struct pci_dev *pdev);

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

/** Set 1 digital output on.                                                 
 * 
 * @param [in] pdev            : The device to initialize.
 * @param [in] b_DigitalOutput : Digital output channel.
 *
 * @retval 0: No error.
 * @retval 1: The handle parameter of the board is wrong.
 * @retval 2: Wrong digital output (0 to 15).
 */
int   i_APCI2200_Set1DigitalOutputOn (struct pci_dev *pdev, uint8_t b_DigitalOutput); 

//----------------------------------------------------------------------------
      
/** Set 1 digital output off.                                                 
 * 
 * @param [in] pdev            : The device to initialize.
 * @param [in] b_DigitalOutput : Digital output channel.
 *
 * @retval 0: No error.
 * @retval 1: The handle parameter of the board is wrong.
 * @retval 2: Digital output memory is not activated.
 * @retval 3: Wrong digital output (0 to 15).
 */
int   i_APCI2200_Set1DigitalOutputOff (struct pci_dev *pdev, uint8_t b_DigitalOutput);

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

/** Set 8 digital output on.                                                 
 * 
 * @param [in] pdev        : The device to initialize.
 * @param [in] b_Port      : The port to set (0: Port 1, 1: Port 2).
 * @param [in] b_PortValue : Port value (0 to 0xFF).
 *
 * @retval 0: No error.
 * @retval 1: The handle parameter of the board is wrong.
 * @retval 2: The port parameter of the board is wrong (has to be 0: Port 1, 1: Port 2).
 */
int   i_APCI2200_Set8DigitalOutputOn (struct pci_dev *pdev, uint8_t b_Port, uint8_t b_PortValue);  

//----------------------------------------------------------------------------
      
/** Set 8 digital output off.                                                 
 * 
 * @param [in] pdev        : The device to initialize.
 * @param [in] b_Port      : The port to set (0: Port 1, 1: Port 2).
 * @param [in] b_PortValue : Port value (0 to 0xFF).
 *
 * @retval 0: No error.
 * @retval 1: The handle parameter of the board is wrong.
 * @retval 2: The port parameter of the board is wrong (has to be 0: Port 1, 1: Port 2).
 * @retval 3: Digital output memory is not activated.
 */
int   i_APCI2200_Set8DigitalOutputOff (struct pci_dev *pdev, uint8_t b_Port, uint8_t b_PortValue);

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

/** Set 16 digital output on.                                                 
 * 
 * @param [in] pdev                 : The device to initialize.
 * @param [in] w_DigitalOutputValue : Digital output value.
 *
 * @retval 0: No error.
 * @retval 1: The handle parameter of the board is wrong.
 */
int   i_APCI2200_Set16DigitalOutputOn (struct pci_dev *pdev, uint16_t w_DigitalOutputValue);    

//----------------------------------------------------------------------------
      
/** Set 16 digital output off.                                                 
 * 
 * @param [in] pdev                 : The device to initialize.
 * @param [in] w_DigitalOutputValue : Digital output value.
 *
 * @retval 0: No error.
 * @retval 1: The handle parameter of the board is wrong.
 * @retval 2: Digital output memory is not activated.
 */
int   i_APCI2200_Set16DigitalOutputOff (struct pci_dev *pdev, uint16_t w_DigitalOutputValue);  

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

/** Read 16 digital output.                                                 
 * 
 * @param [in] pdev                 : The device to initialize.
 * @param [in] w_DigitalOutputValue : Digital output value.
 *
 * @retval 0: No error.
 * @retval 1: The handle parameter of the board is wrong.
 */
int   i_APCI2200_Read16DigitalOutput (struct pci_dev *pdev, uint16_t *w_DigitalOutputValue);  

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

/** Initialise the watchdog.                                                 
 * 
 * @param [in] pdev         : The device to initialize.
 * @param [in] 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 1: Wrong watchdog delay value (has to be 1 to 255).
 */
int   i_APCI2200_InitWatchdog (struct pci_dev *pdev, uint32_t l_DelayValue);    

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

/** 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   i_APCI2200_StartWatchdog (struct pci_dev *pdev);  

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

/** 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   i_APCI2200_StopWatchdog (struct pci_dev *pdev);   

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

/** 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   i_APCI2200_TriggerWatchdog (struct pci_dev *pdev); 

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

/** 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.
 */
int   i_APCI2200_GetWatchdogStatus (struct pci_dev *pdev, uint8_t *b_Status); 

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

/** 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.
 */
int   i_APCI2200_GetWatchdogSoftwareTriggerStatus (struct pci_dev *pdev, uint8_t *b_Status);

//----------------------------------------------------------------------------
#ifdef __KERNEL__

/** find a apci2200 device of a given index in the system PCI device list.
 * @param index The index (minor number) to lookup
 * @return A pointer to the device or NULL
 *
 * This function is used to map a minor number to an actual device.
 */
static __inline__  struct pci_dev * apci2200_lookup_board_by_index(unsigned int index)
{
	struct pci_dev * dev = NULL;	
	int i = 0;
	
	#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
	while ( (dev = pci_find_device(APCI2200_BOARD_VENDOR_ID,APCI2200_BOARD_DEVICE_ID,dev)) )
	#else
	while ( (dev = pci_get_device(APCI2200_BOARD_VENDOR_ID,APCI2200_BOARD_DEVICE_ID,dev)) )
	#endif
	{
		if (i == index) return dev;
		i++;
	}      

	return NULL;
}


#endif // __KERNEL__
//----------------------------------------------------------------------------

#endif // __KERNEL__

#endif // __APCI2200_KAPI_H__
