#ifndef __APCI3001_PRIVATE__
#define __APCI3001_PRIVATE__

/** @file apci3001-private.h
*
* @author Sylvain Nahas
*
* This header contains private definitions (not to be seen by external code)
*/

/** @par LICENCE
* @verbatim
 Copyright (C) 2004,2005  ADDI-DATA GmbH for the source code and the documentation.
        
        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 shoud find the complete GPL in the COPYING file accompanying 
    this source code. 
* @endverbatim
*/

#include <linux/version.h>
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
    #include <linux/config.h>
#else
	#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)
		#include <linux/autoconf.h>
	#else
		#include <generated/autoconf.h>
	#endif
#endif

#include <linux/spinlock.h>
#include <linux/ioctl.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/pci.h> // struct pci_dev
#include <asm/io.h>
#if LINUX_VERSION_CODE < KERNEL_VERSION(3,4,0)
	#include <asm/system.h>
#endif
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,12,0)
    #include <asm/uaccess.h>
#else
    #include <linux/uaccess.h>
#endif
#include <asm/bitops.h>
#include <linux/sched.h>

#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/proc_fs.h>

#include <apci.h>
#include <apci3001.h>
#include <apci3001-kapi.h>

//------------------------------------------------------------------------------
#define APCI3001_EOC					0x8000U
#define APCI3001_RD_STATUS				0x2
#define APCI3001_EOC_INT_MODE			1
#define APCI3001_FIFO_ADDRESS			0x6
#define APCI3001_TIMER_0_MODE_5			0x3
#define APCI3001_RESET_FIFO				0xC
#define APCI3001_TIMER_CRT1				0xC
#define APCI3001_ENABLE_EOC_INT			0x1
#define APCI3001_WRITE_MODE_SELECT		0xE
#define APCI3001_ENABLE_TIMER0			0x1000U
#define APCI3001_WR_ADDRESS				0x0
#define APCI3001_SELECT_TIMER_0_WORD	0x0
#define APCI3001_TIMER_VALUE			0x4
#define APCI3001_CLEAR_PA				0xFFF0U
#define APCI3001_CLEAR_PR				0xF0FFU
#define APCI3001_CLEAR_PA_PR			(APCI3001_CLEAR_PR & APCI3001_CLEAR_PA)
#define APCI3001_TIMER_CRT0				0xD
#define APCI3001_START_CONVERSION		0x2
#define APCI3001_EOS					0x2000U
#define APCI3001_TIMER_0_MODE_2			0x1
#define APCI3001_ENABLE_SCAN			0x8
#define APCI3001_REG_DIGITAL_INPUT		0x2
#define APCI3001_REG_DIGITAL_OUTPUT		0xD
//------------------------------------------------------------------------------

/** ressources of a board */

static __inline__ unsigned long GET_BAR0(struct pci_dev * dev)
{
	return dev->resource[0].start;
}

static __inline__ unsigned long GET_BAR1(struct pci_dev * dev)
{
	return dev->resource[1].start;
}

static __inline__ unsigned long GET_BAR2(struct pci_dev * dev)
{
	return dev->resource[2].start;
}

static __inline__ unsigned long GET_BAR3(struct pci_dev * dev)
{
	return dev->resource[3].start;
}

/** Utility fonction that returns 0 if the given index corresponds to a card already configured.
 * 
 * @param count Number of board 
 * @param index Minor number to check
 * 
 * @return 0 if minor number valid, not 0 otherwise
 * 
 * count ranges from 1 to ...
 * index ranges from 0 to ...
 * 
 * */

static __inline__ int APCI3001_INDEX_NOT_VALID(atomic_t * count , unsigned int index )
{
	volatile unsigned int __count =  atomic_read(count);
	if (index >= __count) return 1;
	return 0;
	
}


/* number of board detected by the kernel */
extern atomic_t apci3001_count;

#define __DRIVER_NAME "apci3001"

/* major number (attributed by the OS) */
extern unsigned int apci3001_majornumber;



/* /dev function */
/* Asynchronous IO signal implementation - send a SIGIO to all registered processes*/
/* If using signal to inform user mode, uncomment following line: */
// int apci3001_fasync_lookup(int fd, struct file *filp, int mode);
int apci3001_open_lookup (struct inode *inode, struct file *filp);
int apci3001_release_lookup (struct inode *inode,struct file *filp);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
int apci3001_ioctl_lookup (struct inode *inode,struct file *filp,unsigned int cmd,unsigned long arg);
#else
long apci3001_ioctl_lookup (struct file *filp,unsigned int cmd,unsigned long arg);
#endif

/*/proc functions  */
void apci3001_proc_init(void);
void apci3001_proc_release(void);
void apci3001_proc_create_device(struct pci_dev * dev, unsigned int minor_number);
void apci3001_proc_release_device(struct pci_dev * dev);

/* interrupt related function */
int apci3001_register_interrupt(struct pci_dev * pdev);
int apci3001_deregister_interrupt(struct pci_dev * pdev);

#include "api.h"
#include "privdata.h"

/** busy loops until the condition is true or a timeout occurs 
 * 
 * This function is intended to be used in context when interrupts are or could be deactivated.
 * 
 * Its use must be restrained to these cases, as busylooping is the most inefficient way of doing that.
 * 
 * @param[in] pdev The device concerned.
 * @param[in] checkf A function that performs the check. it must return 0 if the condition is not fullfilled, 1 otherwise
 * @param[in] timeout The timeout in ticks of 5 microseconds
 * 
 */
static inline int apci3001_busywait_with_timeout(struct pci_dev * pdev, int (*checkf) (struct pci_dev * pdev), unsigned int timeout)
{
	unsigned long count = 0;
	while(1)
	{
		if ( checkf(pdev) )
			return 0; /* condition becomes true */

		/* wait 5 microseconds */
		udelay(5);
		count++;
		if (count>=timeout)
			return 1; /* timeout */
	}
}


#endif //__APCI3001_PRIVATE__
