/** @file ioctl.c
*
* @author Sylvain Nahas
*
* This module implements the apci3200_do_ioctl function
*/

/** @par LICENCE
* @verbatim
 Copyright (C) 2004,2005  ADDI-DATA GmbH for the source code and the documentation.

        ADDI-DATA GmbH
        Airpark Business Center
        Airport Boulevard B210
        77836 Rheinmünster
        Germany
        Tel: +49(0)7229/1847-0

    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/module.h>
#include <linux/fs.h>
#include <linux/pci.h>
#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 "apci3200-private.h"
#include "vtable.h"

EXPORT_NO_SYMBOLS;

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,27)
#define __user
#endif

//------------------------------------------------------------------------------
/** dummy command to be called when a ioctl command is incorrect */
static int apci3200_do_dummy(struct pci_dev * pdev, unsigned int cmd, unsigned long arg)
{
	printk (KERN_WARNING "%s: %d: invalid ioctl\n",__DRIVER_NAME,_IOC_NR(cmd));
	return -EINVAL;
}
//------------------------------------------------------------------------------
/** add new ioctl handlers here */
void apci3200_init_vtable(vtable_t vtable)
{
	apci3200_dummyze_vtable(vtable, apci3200_do_dummy);


	__APCI_3200_DECLARE_IOCTL_HANDLER(vtable,CMD_APCI3200_CheckAndGetPCISlotNumber,do_CMD_APCI3200_CheckAndGetPCISlotNumber);
	__APCI_3200_DECLARE_IOCTL_HANDLER(vtable,CMD_APCI3200_GetHardwareInformation,do_CMD_APCI3200_GetHardwareInformation);
	__APCI_3200_DECLARE_IOCTL_HANDLER(vtable,CMD_APCI3200_SetBoardInformation,do_CMD_APCI3200_SetBoardInformation);

	/* Digital inputs functions */
	__APCI_3200_DECLARE_IOCTL_HANDLER(vtable,CMD_APCI3200_Read1DigitalInput,do_CMD_APCI3200_Read1DigitalInput);
	__APCI_3200_DECLARE_IOCTL_HANDLER(vtable,CMD_APCI3200_Read4DigitalInputs,do_CMD_APCI3200_Read4DigitalInputs);

	/* Digital outputs functions */
	__APCI_3200_DECLARE_IOCTL_HANDLER(vtable,CMD_APCI3200_SetDigitalOutputMemoryOn,do_CMD_APCI3200_SetDigitalOutputMemoryOn);
	__APCI_3200_DECLARE_IOCTL_HANDLER(vtable,CMD_APCI3200_SetDigitalOutputMemoryOff,do_CMD_APCI3200_SetDigitalOutputMemoryOff);
	__APCI_3200_DECLARE_IOCTL_HANDLER(vtable,CMD_APCI3200_Set1DigitalOutputOn,do_CMD_APCI3200_Set1DigitalOutputOn);
	__APCI_3200_DECLARE_IOCTL_HANDLER(vtable,CMD_APCI3200_Set1DigitalOutputOff,do_CMD_APCI3200_Set1DigitalOutputOff);
	__APCI_3200_DECLARE_IOCTL_HANDLER(vtable,CMD_APCI3200_Set3DigitalOutputsOn,do_CMD_APCI3200_Set3DigitalOutputsOn);
	__APCI_3200_DECLARE_IOCTL_HANDLER(vtable,CMD_APCI3200_Set3DigitalOutputsOff,do_CMD_APCI3200_Set3DigitalOutputsOff);

	/* Analog inputs functions */
	__APCI_3200_DECLARE_IOCTL_HANDLER(vtable,CMD_APCI3200_InitAnalogInputChannel,do_CMD_APCI3200_InitAnalogInputChannel);
	__APCI_3200_DECLARE_IOCTL_HANDLER(vtable,CMD_APCI3200_Read1AnalogInputChannel,do_CMD_APCI3200_Read1AnalogInputChannel);
	__APCI_3200_DECLARE_IOCTL_HANDLER(vtable,CMD_APCI3200_ReadCalibrationGainValue,do_CMD_APCI3200_ReadCalibrationGainValue);
	__APCI_3200_DECLARE_IOCTL_HANDLER(vtable,CMD_APCI3200_ReadCalibrationOffsetValue,do_CMD_APCI3200_ReadCalibrationOffsetValue);

	/* Temperature functions */
	__APCI_3200_DECLARE_IOCTL_HANDLER(vtable,CMD_APCI3200_ReadCJCValue,do_CMD_APCI3200_ReadCJCValue);
	__APCI_3200_DECLARE_IOCTL_HANDLER(vtable,CMD_APCI3200_ReadCJCCalibrationGainValue,do_CMD_APCI3200_ReadCJCCalibrationGainValue);
	__APCI_3200_DECLARE_IOCTL_HANDLER(vtable,CMD_APCI3200_ReadCJCCalibrationOffsetValue,do_CMD_APCI3200_ReadCJCCalibrationOffsetValue);
	__APCI_3200_DECLARE_IOCTL_HANDLER(vtable,CMD_APCI3200_ReadChannelCalibrationValue,do_CMD_APCI3200_ReadChannelCalibrationValue);

	/* Scan functions */
	__APCI_3200_DECLARE_IOCTL_HANDLER(vtable,CMD_APCI3200_InitScanModuleAcquisitionSensor, do_CMD_APCI3200_InitScanModuleAcquisitionSensor);
	__APCI_3200_DECLARE_IOCTL_HANDLER(vtable,CMD_APCI3200_InitScanModuleAcquisition, do_CMD_APCI3200_InitScanModuleAcquisition);
	__APCI_3200_DECLARE_IOCTL_HANDLER(vtable,CMD_APCI3200_StartScanModuleAcquisition, do_CMD_APCI3200_StartScanModuleAcquisition);
	__APCI_3200_DECLARE_IOCTL_HANDLER(vtable,CMD_APCI3200_StopScanModuleAcquisition, do_CMD_APCI3200_StopScanModuleAcquisition);
	__APCI_3200_DECLARE_IOCTL_HANDLER(vtable,CMD_APCI3200_ReadReferenceVoltage, do_CMD_APCI3200_ReadReferenceVoltage);

	/* "fast" version */
	__APCI_3200_DECLARE_IOCTL_HANDLER(vtable,CMD_APCI3200_SetConvertingTime, do_CMD_APCI3200_SetConvertingTime);
	__APCI_3200_DECLARE_IOCTL_HANDLER(vtable,CMD_APCI3200_Read1AnalogInputChannelFast, do_CMD_APCI3200_Read1AnalogInputChannelFast);
	__APCI_3200_DECLARE_IOCTL_HANDLER(vtable,CMD_APCI3200_ReadCJCValueFast, do_CMD_APCI3200_ReadCJCValueFast);
	__APCI_3200_DECLARE_IOCTL_HANDLER(vtable,CMD_APCI3200_ReadCalibrationOffsetValueFast, do_CMD_APCI3200_ReadCalibrationOffsetValueFast);
	__APCI_3200_DECLARE_IOCTL_HANDLER(vtable,CMD_APCI3200_ReadCalibrationGainValueFast, do_CMD_APCI3200_ReadCalibrationGainValueFast);

	/* Diagnostic functions */
	__APCI_3200_DECLARE_IOCTL_HANDLER(vtable,CMD_APCI3200_SetDiagnosticMode, do_CMD_APCI3200_SetDiagnosticMode);
	__APCI_3200_DECLARE_IOCTL_HANDLER(vtable,CMD_APCI3200_GetDiagnosticMode, do_CMD_APCI3200_GetDiagnosticMode);

	/* User mode interrupt */
	__APCI_3200_DECLARE_IOCTL_HANDLER(vtable,CMD_APCI3200_EventEmpty, do_CMD_APCI3200_EventEmpty);
	__APCI_3200_DECLARE_IOCTL_HANDLER(vtable,CMD_APCI3200_EventWait, do_CMD_APCI3200_EventWait);
}

//------------------------------------------------------------------------------
int apci3200_do_ioctl(struct pci_dev * pdev,unsigned int cmd,unsigned long arg)
{

	/* boundaries check
	 *
	 * VTABLE_ELEMENT_NB(vtable_t) = __APCI3200_UPPER_IOCTL_CMD +1
	 * maximum index value = __APCI3200_UPPER_IOCTL_CMD = VTABLE_ELEMENT_NB(vtable_t) -1
	 *
	 * the idea here is to favorize compilation-time
	 *
	 * */

	if (_IOC_NR(cmd) > (VTABLE_ELEMENT_NB(vtable_t)-1) )
		return apci3200_do_dummy(pdev,cmd,arg);

	/* call actual ioctl handler - should be safe now */
	return (apci3200_vtable[_IOC_NR(cmd)]) (pdev, cmd, arg);
}
//------------------------------------------------------------------------------
int do_CMD_APCI3200_CheckAndGetPCISlotNumber(struct pci_dev * pdev, unsigned int cmd, unsigned long arg)
{
	int retval = 0; /* return value of this call is the number of boards */

    /* this command returns an area of size CONFIG_APCI3200_MAX_BOARD_NBR+1 filled with the PCI_SLOT() value and as last field the major number associated to the device - this is deprecated and should not be used anymore.
	The call returns 0 when memory is too low or the number of boards  */

	char * b_SlotArray = kmalloc(CONFIG_APCI3200_MAX_BOARD_NBR+1 * sizeof(char),GFP_KERNEL);
    if (!b_SlotArray)
	{
		/* bad param. Let output some information for debug */
		printk("%s: CMD_APCI3200_CheckAndGetPCISlotNumber: -EINVAL\n",__DRIVER_NAME);
		return 0;
	}

	memset(b_SlotArray,0,CONFIG_APCI3200_MAX_BOARD_NBR+1 );

	printk("%s: the use of CMD_APCI3200_CheckAndGetPCISlotNumber is deprecated\n",__DRIVER_NAME);

	/* record the PCI_SLOT for each device from 0 to CONFIG_APCI3200_MAX_BOARD_NBR  */
	{
		int i;
		for(i = 0; ( ( i < atomic_read(&apci3200_count) ) || ( i< CONFIG_APCI3200_MAX_BOARD_NBR ) ); i++)
		{

			struct pci_dev * dev =  apci3200_lookup_board_by_index(i);

			if (!dev) break;

			b_SlotArray[i] = PCI_SLOT(dev->devfn);

		}
    }
    b_SlotArray[CONFIG_APCI3200_MAX_BOARD_NBR] = apci3200_majornumber;

    if ( copy_to_user ( (unsigned char __user *)arg, b_SlotArray, sizeof (BYTE[CONFIG_APCI3200_MAX_BOARD_NBR+1])) )
	{
    	/* bad address. Let output some information for debug */
    	printk("%s: CMD_APCI3200_CheckAndGetPCISlotNumber: -EFAULT\n",__DRIVER_NAME);
    	kfree(b_SlotArray);
    	return 0;
	}
	/* return the smaller value between CONFIG_APCI3200_MAX_BOARD_NBR and number of board - Note: apci3200_count is assumed to to be always consistent with the system PCI devices list */
    retval = ( atomic_read(&apci3200_count) < CONFIG_APCI3200_MAX_BOARD_NBR ? atomic_read(&apci3200_count) : CONFIG_APCI3200_MAX_BOARD_NBR );

	kfree(b_SlotArray);

	return retval;

}



//------------------------------------------------------------------------------
/** Returns the informations of base address, IRQ to the user.
 * @deprecated: use-space doesn't need it and kernel-space has better means to get it.
 *  */
int do_CMD_APCI3200_GetHardwareInformation(struct pci_dev * pdev, unsigned int cmd, unsigned long arg)
{
	str_BaseInformations s_BaseInformations;
	s_BaseInformations.ui_BaseAddress[0] = GET_BAR0(pdev);
	s_BaseInformations.ui_BaseAddress[1] = GET_BAR1(pdev);
	s_BaseInformations.ui_BaseAddress[2] = GET_BAR2(pdev);
	s_BaseInformations.ui_BaseAddress[3] = GET_BAR3(pdev);
	s_BaseInformations.ui_BaseAddress[4] = 0;
	s_BaseInformations.b_Interrupt = pdev->irq;
	s_BaseInformations.b_SlotNumber = PCI_SLOT(pdev->devfn);

    if ( copy_to_user ((str_BaseInformations __user *)arg,&s_BaseInformations, sizeof(s_BaseInformations)) )
		return -EFAULT;

	return 0;
}

//------------------------------------------------------------------------------
int do_CMD_APCI3200_SetBoardInformation(struct pci_dev * pdev, unsigned int cmd, unsigned long arg)
{
	return 0;
}
