#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <asm/current.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/device.h>
#include <linux/err.h>
#include <asm/uaccess.h>
//#include <stdint.h>
#include "stm32f4xx.h"
#include "stm32f4xx_hal_flash.h"
#include "driver.h"
#include "stm32_hal_legacy.h"




static int major = MAJOR_IFLASH;
static int minor = 0;
static dev_t devno;
static struct cdev *iflash_cdev = NULL;
static int count = 1;
#define DEVNAME "iflash"

static int iflash_open(struct inode *inode, struct file *filep);
static int iflash_close(struct inode *inode, struct file *filep);
static ssize_t iflash_read(struct file *filep, char __user *buf, size_t size, loff_t *offset);
static ssize_t iflash_write(struct file *filep, const char __user *buf, size_t size, loff_t *offset);
static int iflash_ioctl(struct inode *inode, struct file *filep, unsigned int cmd, unsigned long arg);
static struct file_operations iflash_ops = 
{
	.owner = THIS_MODULE,
	.open = iflash_open,
	.release = iflash_close,
	.ioctl = iflash_ioctl,
	.read = iflash_read,
	.write = iflash_write,
	
};

static int iflash_open(struct inode *inode, struct file *filep)
{
	return 0;
}

static int iflash_close(struct inode *inode, struct file *filep)
{
	return 0;
}

static int iflash_ioctl(struct inode *inode, struct file *filep, unsigned int cmd, unsigned long arg)
{
	iflash_t iflash_arg;
	uint32_t sector = 0;
	uint32_t address = 0;
	int i;
	int ret = 0;
	int try = 3;
	if ( copy_from_user(&iflash_arg, (void*)arg, sizeof(iflash_t)) )
		return -EFAULT;
	
	switch(cmd)
	{
		case IFLASH_UNLOCK:
			//printk("IFLASH_UNLOCK()\n");
			ret = HAL_FLASH_Unlock();
			break;
		case IFLASH_LOCK:
			//printk("IFLASH_LOCK()\n");
			ret = HAL_FLASH_Lock();
			break;
		case IFLASH_ERASESECTOR:
			//printk("IFLASH_ERASESECTOR()\n");
			switch(iflash_arg.sector)
			{
				case 0:
					sector = FLASH_SECTOR_0;
					break;
				case 1:
					sector = FLASH_SECTOR_1;
					break;
				case 2:
					sector = FLASH_SECTOR_2;
					break;
				case 3:
					sector = FLASH_SECTOR_3;
					break;
				case 4:
					sector = FLASH_SECTOR_4;
					break;
				case 5:
					sector = FLASH_SECTOR_5;
					break;
				case 6:
					sector = FLASH_SECTOR_6;
					break;
				case 7:
					sector = FLASH_SECTOR_7;
					break;
				case 8:
					sector = FLASH_SECTOR_8;
					break;
				case 9:
					sector = FLASH_SECTOR_9;
					break;
				case 10:
					sector = FLASH_SECTOR_10;
					break;
				case 11:
					sector = FLASH_SECTOR_11;
					break;
				case 12:
					sector = FLASH_SECTOR_12;
					break;
				case 13:
					sector = FLASH_SECTOR_13;
					break;
				case 14:
					sector = FLASH_SECTOR_14;
					break;
				case 15:
					sector = FLASH_SECTOR_15;
					break;
				case 16:
					sector = FLASH_SECTOR_16;
					break;
				case 17:
					sector = FLASH_SECTOR_17;
					break;
				case 18:
					sector = FLASH_SECTOR_18;
					break;
				case 19:
					sector = FLASH_SECTOR_19;
					break;
				case 20:
					sector = FLASH_SECTOR_20;
					break;
				case 21:
					sector = FLASH_SECTOR_21;
					break;
				case 22:
					sector = FLASH_SECTOR_22;
					break;
				case 23:
					sector = FLASH_SECTOR_23;
					break;
			}
			FLASH_Erase_Sector(sector, FLASH_VOLTAGE_RANGE_3);
			break;
		case IFLASH_WRITEWORD:
			//printk("IFLASH_WRITEWORD()\n");
			address = iflash_arg.address;
			for(i=0;i<iflash_arg.len;i+=4)
			{
				try = 3;
				while(try>0)
				{
					ret = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address+i, ((uint32_t*)&iflash_arg.data[i]));
					if(ret = 0)
						try=0;
					else if(try > 0)
					{
						try--;
					}
					else
					{
						return ret;
					}
				}
			}
			break;
		case IFLASH_WRITEHALFWORD:
			//printk("IFLASH_HALFWORD()\n");
			address = iflash_arg.address;
			for(i=0;i<iflash_arg.len;i+=2)
			{
				try = 3;
				while(try>0)
				{
					ret = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, address+i, *((uint16_t*)&iflash_arg.data[i]));
					if(ret = 0)
						try=0;
					else if(try > 0)
					{
						try--;
					}
					else
					{
						return ret;
					}
				}
			}
			break;
		case IFLASH_WRITEBYTE:
			//printk("IFLASH_WRITEBYTE()\n");
			address = iflash_arg.address;
			for(i=0;i<iflash_arg.len;i++)
			{
				try = 3;
				while(try>0)
				{
					ret = HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, address+i, (uint32_t)iflash_arg.data[i]);
					if(ret = 0)
						try=0;
					else if(try > 0)
					{
						try--;
					}
					else
					{
						return ret;
					}
				}
			}
			break;
		case IFLASH_WRITE_BYTES:
			ret = HAL_FLASH_Unlock();
			if(ret != 0)
			{
				printk("Unlock flash error.");
				return ret;
			}

			switch(iflash_arg.sector)
			{
				case 0:
					sector = FLASH_SECTOR_0;
					break;
				case 1:
					sector = FLASH_SECTOR_1;
					break;
				case 2:
					sector = FLASH_SECTOR_2;
					break;
				case 3:
					sector = FLASH_SECTOR_3;
					break;
				case 4:
					sector = FLASH_SECTOR_4;
					break;
				case 5:
					sector = FLASH_SECTOR_5;
					break;
				case 6:
					sector = FLASH_SECTOR_6;
					break;
				case 7:
					sector = FLASH_SECTOR_7;
					break;
				case 8:
					sector = FLASH_SECTOR_8;
					break;
				case 9:
					sector = FLASH_SECTOR_9;
					break;
				case 10:
					sector = FLASH_SECTOR_10;
					break;
				case 11:
					sector = FLASH_SECTOR_11;
					break;
				case 12:
					sector = FLASH_SECTOR_12;
					break;
				case 13:
					sector = FLASH_SECTOR_13;
					break;
				case 14:
					sector = FLASH_SECTOR_14;
					break;
				case 15:
					sector = FLASH_SECTOR_15;
					break;
				case 16:
					sector = FLASH_SECTOR_16;
					break;
				case 17:
					sector = FLASH_SECTOR_17;
					break;
				case 18:
					sector = FLASH_SECTOR_18;
					break;
				case 19:
					sector = FLASH_SECTOR_19;
					break;
				case 20:
					sector = FLASH_SECTOR_20;
					break;
				case 21:
					sector = FLASH_SECTOR_21;
					break;
				case 22:
					sector = FLASH_SECTOR_22;
					break;
				case 23:
					sector = FLASH_SECTOR_23;
					break;
			}
			FLASH_Erase_Sector(sector, FLASH_VOLTAGE_RANGE_3);


			address = iflash_arg.address;
			for(i=0;i<iflash_arg.len;i++)
			{				
				try = 3;
				while(try>0)
				{
					ret = HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, address+i, (uint32_t)iflash_arg.data[i]);
					if(ret = 0)
						try=0;
					else if(try > 0)
					{
						try--;
					}
					else
					{
						return ret;
					}
				}
			}

			ret = HAL_FLASH_Lock();
			if(ret != 0)
			{
				printk("Lock flash error.");
				return ret;
			}

	default:
		printk("---> Invalid action\n");
		return -EINVAL;
		break;		

	}
	return ret;
}

static ssize_t iflash_read(struct file *filep, char __user *buf, size_t size, loff_t *offset)
{
	return 0;
}

static ssize_t iflash_write(struct file *filep, const char __user *buf, size_t size, loff_t *offset)
{
	return 0;
}



static int __init iflash_init(void)
{
	int ret;
	iflash_cdev = cdev_alloc();
	if(iflash_cdev == NULL){
		return -ENOMEM;
	}


	cdev_init(iflash_cdev,&iflash_ops);
	devno = MKDEV(major,minor);
	ret = register_chrdev_region(devno, count, DEVNAME);
	if(ret){
		goto ERR_STEP;
	}
	
	ret = cdev_add(iflash_cdev, devno, count);
	if(ret){
		goto ERR_STEP1;
	}
	printk("iflash module start...\n");
	return 0;

ERR_STEP1:
	unregister_chrdev_region(devno,count);
ERR_STEP:
	cdev_del(iflash_cdev);
	return ret;
}

static void __exit iflash_exit(void)
{
	unregister_chrdev_region(MKDEV(major,minor),count);
	cdev_del(iflash_cdev);
}

module_init(iflash_init);
module_exit(iflash_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Jimbo");
MODULE_DESCRIPTION("this is iflash module");



