/* @file apci1032-kapi.h
 *
 * @brief Define the public kernel-land API for the APCI XXXX 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 __APCI1032_KAPI_H__
#define __APCI1032_KAPI_H__

#include <linux/pci.h>
#include <apci1032.h>

#ifndef __KERNEL__
	#error This header is for inclusion in kernel code only !
#endif

#ifdef __KERNEL__

/* DIGITAL INPUT ***********************************************************************/

/** Read 1 digital input of the board xPCI-1032
 *
 * @param[in] pdev Pointer to the PCI device to access
 * @param[in] channel The index of the channel to read (1 to 32)
 * @param[out] inputValue Status of the digital input
 *
 * @retval	0 : No Error
 * @retval	-EINVAL : The channel index is wrong
 */
extern int APCI1032_Read1DigitalInput(struct pci_dev * pdev, uint8_t channel, uint8_t *inputValue);

/** Read 8 digital inputs of the board xPCI-1032
 *
 * @param[in] pdev Pointer to the PCI device to access
 * @param[in] port Port to read ( \b 1: channels 1-8, \b 2: channels 9-16, \b 3: channels 17-24, \b 4: channels 25-32)
 * @param[out] inputValue Status of the digital inputs
 *
 * @retval	0 : No Error
 * @retval	-EINVAL : The port index is wrong
 */
extern int APCI1032_Read8DigitalInputs(struct pci_dev * pdev, uint8_t port, uint8_t *inputValue);

/** Read 16 digital inputs of the board xPCI-1032
 *
 * @param[in] pdev Pointer to the PCI device to access
 * @param[in] port Port to read ( \b 1: channels 1-16, \b 2: channels 16-32)
 * @param[out] inputValue Status of the digital inputs
 *
 * @retval	0 : No Error
 * @retval	-EINVAL : The port index is wrong
 */
extern int APCI1032_Read16DigitalInputs(struct pci_dev * pdev, uint8_t port, uint16_t *inputValue);

/** Read 32 digital inputs of the board xPCI-1032
 *
 * bit D0 returns the state of the digital input 0, D1 returns the state of the digital input 1, and so on...
 *
 * @param[in] pdev Pointer to the PCI device to access.
 * @param[out] inputValue Status of the digital inputs
 *
 * @retval	0 : No Error
 */
extern int APCI1032_Read32DigitalInputs(struct pci_dev * pdev, uint32_t *inputValue);

/* ADMINISTRATIVE ***********************************************************************/

/** find a apci1032 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 * apci1032_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(APCI1032_BOARD_VENDOR_ID, APCI1032_BOARD_DEVICE_ID, dev)))
#else
	while ((dev = pci_get_device(APCI1032_BOARD_VENDOR_ID, APCI1032_BOARD_DEVICE_ID, dev)))
#endif
	{
		if (i == index)
			return dev;
		i++;
	}

	return NULL;
}

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

#endif // __KERNEL__

#endif // __APCI1032_KAPI_H__
