#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
#include <asm/uaccess.h>
#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 <apci3120.h>
#include <apci3120-kapi.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("ADDI-DATA GmbH <info@addi-data.com>");
MODULE_DESCRIPTION("APCI-3120 Read More Sample");

EXPORT_NO_SYMBOLS;

static const char modulename[] = "sample_readmore";


static int sample_readmore_kernel (struct pci_dev *pdev)
   	{ 
   	unsigned char b_Gain[8];
   	unsigned char b_Polar[8];
   	unsigned char b_Channel[8];
   	unsigned int ui_ReadValueArray[8];
   	int i_ReturnValue;
	int i = 0;

   b_Channel[0] = APCI3120_CHANNEL_0;b_Gain[0] = APCI3120_1_GAIN;b_Polar[0] = APCI3120_UNIPOLAR;
   b_Channel[1] = APCI3120_CHANNEL_1;b_Gain[1] = APCI3120_1_GAIN;b_Polar[1] = APCI3120_UNIPOLAR;
   b_Channel[2] = APCI3120_CHANNEL_2;b_Gain[2] = APCI3120_1_GAIN;b_Polar[2] = APCI3120_UNIPOLAR;
   b_Channel[3] = APCI3120_CHANNEL_3;b_Gain[3] = APCI3120_1_GAIN;b_Polar[3] = APCI3120_UNIPOLAR;
   b_Channel[4] = APCI3120_CHANNEL_4;b_Gain[4] = APCI3120_1_GAIN;b_Polar[4] = APCI3120_UNIPOLAR;
   b_Channel[5] = APCI3120_CHANNEL_5;b_Gain[5] = APCI3120_1_GAIN;b_Polar[5] = APCI3120_UNIPOLAR;
   b_Channel[6] = APCI3120_CHANNEL_6;b_Gain[6] = APCI3120_1_GAIN;b_Polar[6] = APCI3120_UNIPOLAR;
   b_Channel[7] = APCI3120_CHANNEL_7;b_Gain[7] = APCI3120_1_GAIN;b_Polar[7] = APCI3120_UNIPOLAR;
    	
	printk("%s: Read more test\n",modulename);

	for (i = 0; i< 100; i++)
	{
		unsigned long irqstate;
		APCI3120_lock(pdev,&irqstate);
		{
			i_ReturnValue = i_APCI3120_ReadMoreAnalogInputs (pdev,
	        	                         8,
						  b_Channel,
						  b_Gain,
						  b_Polar,
						  10,
						  APCI3120_DISABLE,
						  ui_ReadValueArray);
		}
		APCI3120_unlock(pdev,irqstate);
		if(i_ReturnValue == 0)
	    {
	     		printk("%s: ui_ReadValue = %u %u %u %u %u %u %u %u \n",modulename,ui_ReadValueArray[0],
								ui_ReadValueArray[1],
								ui_ReadValueArray[2],
								ui_ReadValueArray[3],
								ui_ReadValueArray[4],
								ui_ReadValueArray[5],
								ui_ReadValueArray[6],
								ui_ReadValueArray[7]);
	    }
		else
		{
			printk("%s: Read More Error (%d)\n",modulename,i_ReturnValue);
			break;
		}
	}

    	
    	

   	return (i_ReturnValue);	          


   	}


/** Called when module loads. */
static int __init apci3120Sample_init(void)
{
	struct pci_dev *pdev;
	int board_index = 0;
	printk("%s: looking for board %u\n",modulename,board_index);
	pdev=apci3120_lookup_board_by_index(0);
	if( !pdev )
	{
		printk("%s: board %u not found\n",modulename,board_index);
		return -ENODEV;
	}
	printk("%s: beginning test\n",modulename);
        sample_readmore_kernel (pdev);

	return 0;
}
//-------------------------------------------------------------------
/** Called when module is unloaded. */
static void __exit apci3120Sample_exit(void)
{
 
	
}
//------------------------------------------------------------------------------
module_exit(apci3120Sample_exit);
module_init(apci3120Sample_init);
