/******************************************************************************
 *                                                                            *
 * Project: DOC Driver for Linux 2.6 Block device driver for mDOC H3  family  *
 * of devices under Linux kernel 2.6.                                         *
 *                                                                            *
 *   Version: 1.0                                                             *
 *   Email questions to: oemsupport@sandisk.com                               *
 *   Copyright (C) SanDisk IL Ltd. 1995 - 2007                                *
 *   SanDisk IL Ltd., 7 Atir Yeda Street, Kfar Saba 44425, Israel             *
 *                                                                            *
 ******************************************************************************
 *                                                                            *
 * 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 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, which is set forth in the readme.txt file.                   *
 * You should have received a copy of the GNU General Public License along    *
 * with this program; if not, write to the Free Software Foundation, Inc., 51 *
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA                    *
 *                                                                            *
 * This License does not grant you any right to use the trademarks, service   *
 * marks or logos of SanDisk IL Ltd. or SanDisk Corporation.                  *
 * Subject to the foregoing, SanDisk IL Ltd., for itself and on behalf of its *
 * licensors, hereby reserves all intellectual property rights in the program,*
 * except for the rights expressly granted in this License.                   *
 *                                                                            *
 ******************************************************************************/

/*
 * $Log$
 */


#ifndef __TFFSDRV_H__
#define __TFFSDRV_H__

/* this driver is for 2.[4/5/6].x versions of Linux kernels */
#include <linux/version.h>
#ifndef KERNEL_VERSION
# define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
#endif
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,7,0)) || (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
# error "unsupported Linux kernel version"
#endif

#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
# include <linux/config.h>
#endif

#if (LINUX_VERSION_CODE<KERNEL_VERSION(2,6,0))
# ifdef CONFIG_MODVERSIONS
#  ifndef MODVERSIONS
#   define MODVERSIONS
#  endif
#  ifdef REDHAT_KERNEL_EXTENSION
#   include <linux/modversions.h>
#  else
#   include <linux/modsetver.h>
#  endif
# endif
#endif

#include <linux/module.h>

#if (LINUX_VERSION_CODE>KERNEL_VERSION(2,5,0))
# include <linux/moduleparam.h>
#endif

#include <linux/slab.h>			/* kmalloc() */
#include <linux/hdreg.h>		/* HDIO_GETGEO */
#include <linux/smp_lock.h>		/* ...lock_kernel() */
#include <linux/ioport.h>		/* ...mem_region() */
#include <linux/init.h>
#include <asm/uaccess.h>		/* ok_access(), put_usr(), get_user() */
#include <linux/fs.h>
#include <linux/ext2_fs.h>		/* struct ext2_super_block */ /* only for ext filter */
#include <linux/delay.h>		/* mdelay() */
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/blkpg.h>		/* blk_ioctl() */
#include <linux/sockios.h>		/* for SIOCDEVPRIVATE */	/* only for 2.6.x */
#include <linux/vmalloc.h>		/* 2.4.18 for lubbock don't want to work w/o this */
#include <linux/autoconf.h>
#include <linux/interrupt.h>
#include <asm/irq.h>
#include <linux/highmem.h>

#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
# include <linux/dma-mapping.h>
# include <linux/completion.h>
#endif

#include "flcustom.h"
#include "tffsarch.h"

#ifdef CONFIG_PROC_FS
# include <linux/proc_fs.h>
#endif

/* definitions for blk.h */
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
extern int tffs_major;
# define MAJOR_NR			tffs_major
# define DEVICE_ON
# define DEVICE_OFF(d)		
# define DEVICE_NR(device)	(MINOR(device)>>DEV2PART_SHIFT)
# define DEVICE_NO_RANDOM
# include <linux/blk.h>
#else
# include <linux/blkdev.h>
#endif
/* end of blk.h definitions */

#ifdef TFFS_DEBUG_DRIVER
# define PrintkDebug(fmt,args...)				printk(KERN_DEBUG TFFS_DEVICE_NAME": "fmt"\n",## args)
/* # define PrintkDebug(fmt,args...)				printk(KERN_ERR TFFS_DEVICE_NAME": "fmt"\n",## args)  */
#else	/* TFFS_DEBUG_DRIVER */
# define PrintkDebug(fmt,args...)
#endif	/* TFFS_DEBUG_DRIVER */
#define PrintkInfo(fmt,args...)					printk(KERN_INFO TFFS_DEVICE_NAME": "fmt"\n",## args)
#define PrintkError(fmt,args...)				printk(KERN_ERR TFFS_DEVICE_NAME": "fmt"\n",## args)
#define PrintkWarning(fmt,args...)				printk(KERN_WARNING TFFS_DEVICE_NAME": "fmt"\n",## args)

#ifdef TFFS_STATISTIC

typedef struct
{
    unsigned long dwReadReq;           /* total 'read' requests received by driver */
    unsigned long dwReadReqNoThread;   /* number of these requests done by DeviceRequest() */
    unsigned long dwAbsReadSec;	       /* total sectors read */
    unsigned long dwAlReadSec;         /* aligned 512-byte sectors read */
    unsigned long dwUnalReadSec;       /* unaligned 512-byte sectors read */
    unsigned long dwAlReadShared;      /* aligned shared 512-byte sectors read */
    unsigned long dwUnalReadShared;    /* unaligned shared 512-byte sectors read */
    unsigned long dwAbsRead;           /* total flAbsRead() calls */
    unsigned long dwWriteReq;          /* total 'write' requests received by driver */
    unsigned long dwAbsWriteSec;       /* total sectors written */
    unsigned long dwAlWriteSec;        /* aligned 512-byte sectors written */
    unsigned long dwUnalWriteSec;      /* unaligned 512-byte sectors written */
    unsigned long dwAlWriteShared;     /* aligned shared 512-byte sectors written */
    unsigned long dwUnalWriteShared;   /* unaligned shared 512-byte sectors written */
    unsigned long dwAbsWrite;          /* total flAbsWrite() calls */
}TffsStatistic;

#endif /* TFFS_STATISTIC */

struct tagDeviceInfo;

typedef struct
{
	struct tagDeviceInfo*pDevices;
	unsigned short wDevices;				/* should be WORD to allow left shift to 5 */
    struct list_head  requestsList;         /* list of requests from IO queue */
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
    spinlock_t        request_list_lock;
#endif

#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
	struct completion wait_for_io_thread; /* wait for socket's I/O thread to start/stop */
	struct completion wait_for_irq;       /* wait for DiskOnChip interrupt */
#else
    	struct semaphore threadSemaphore;     /* wait for socket's I/O thread to start/stop */
	struct semaphore sleep_sem;           /* used to wait for erase/write completion */
#endif

	wait_queue_head_t waitQueueHead;   /* wait queue for thread and flSleep */
	int io_thread_state;
	unsigned char bHandle;					/* socket, as ioreq.irHandle */
	char * pCache;
	int fFound;
	unsigned long dwPhysAddr,dwVirtAddr,dwMappedLen;
	int fMemRequested;
}SocketInfo;

/* #ifdef TFFS_USE_EXT_FILTER */
struct tagSectInfo;
typedef struct tagSectInfo
{
	unsigned long dwSector;		/* no of sector on device */
	unsigned short bSectors;	/* length */
	struct tagSectInfo*pParent,*pPrev,*pNext;
	unsigned char bFlag;
#define EXTF_PT				0x01	/* empty part table space */
#define EXTF_HAS_PT			0x02	/* has written part table */
#define EXTF_SB				0x04	/* empty super block space */
#define EXTF_HAS_SB			0x08	/* has superblock written, but no group descriptors */
#define EXTF_HAS_GD			0x10	/* has superblock and group descriptors */
#define EXTF_BB				0x20	/* block bitmap was allocated and waiting for group description */
#define EXTF_HAS_BB			0x40	/* has block bitmap */
#ifdef TFFS_DEBUG_DRIVER
	int no;										/* part no */
	unsigned long dwOffset;						/* PT */
	unsigned long dwFirstSectorOfBitmap;		/* BB */
	unsigned long dwExt3Flags;					/* SB */
#else
	unsigned long dw;
#define dwOffset dw
#define dwFirstSectorOfBitmap dw
#define dwExt3Flags dw
#endif /* TFFS_DEBUG_DRIVER */
	unsigned long dwBlocksPerBitmap;			/* SB and BB */
	unsigned char bSect2BlockShift;				/* SB */
	unsigned short wGroups;						/* SB */
}SectInfo;
/* #endif TFFS_USE_EXT_FILTER */

typedef struct tagDeviceInfo
{
	SocketInfo*pSocket;
        unsigned char diskNo;           /* disk # (zero-based) */
	unsigned char bHandle;		/* socket & partition, as ioreq.irHandle */
	unsigned long dwSize;		/* device size */
	int in_use;
	unsigned short wHWSectorSize;		/* hw sector size in driver */
	unsigned short wTffsHWSectorSizeShift;	/* shared sectors size in tffs */
	unsigned char fAbsMounted;

#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
    struct gendisk       * gd;         /* struct gendisk allocated for this disk */
    struct request_queue * rq;         /* disk's request queue */ 
    spinlock_t             rq_lock;    /* request queue lock */
#else
	request_queue_t requestQueue;
#endif

/* #ifdef TFFS_USE_EXT_FILTER */
	SectInfo sect[2];
	SectInfo **pCache;
/* #endif */

	unsigned short wCyl;
	unsigned char bHead,bSect;

#ifdef TFFS_STATISTIC
       TffsStatistic stat;    /* various disk statistics */
#endif

#ifdef CONFIG_PROC_FS
	struct proc_dir_entry * pProcFile;
#endif
}DeviceInfo;

typedef struct
{
	unsigned char wDevices;
	SocketInfo sockets[FL_SOCKETS];
	DeviceInfo*ppDevices[FL_VOLUMES];

#if defined(TFFS_PARTITIONABLE) && (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
	struct gendisk genDisk;
#endif

#ifdef CONFIG_PROC_FS
	struct proc_dir_entry*pProcDir;
#endif

    int          channel;   /* assume only one DMA connected */
    dma_addr_t   dma_buf_paddr; /* physical address of DMA buffer */
    void       * dma_buf_vptr;  /* virtual address of DMA buffer */
    int          dma_buf_size;  /* size of DMA buffer in bytes */

}TffsInfo;

extern TffsInfo tffsInfo;
extern int tffs_sg_bufsize;

#define VMalloc(size) vmalloc((size)+4)
#define KMalloc(size) kmalloc((size)+4,GFP_KERNEL)

/* DiskOnChip driver's minor version number */
#define TFFS_DRV_VER  85


/* driver's generic suspend/resume power management routines */
extern void tffs_suspend (void);
extern void tffs_resume (void);

#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)) && (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
#  ifdef CONFIG_PM
#    include <linux/pm.h>
     extern int tffs_pm_init (void);
     extern int tffs_pm_exit (void);
     extern int tffs_pm_allowed (SocketInfo * pSoc, int flag);
#  endif

#  ifdef CONFIG_DPM
#    include <linux/device.h>
     extern int tffs_dpm_register (void);
     extern int tffs_dpm_unregister (void);
#  endif
#endif


#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)) && defined (CONFIG_PM)
#  include <linux/device.h>
#  if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,15))
#    include <linux/platform_device.h>
#  endif
   extern int tffs_pm_init (void);
   extern int tffs_pm_exit (void);
#endif


#endif /* __TFFSDRV_H__*/
