#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_gpio.h"
#include "driver.h"
#include "stm32_hal_legacy.h"




static int major = MAJOR_GPIO;
static int minor = 0;
static dev_t devno;
static struct cdev *gpio_cdev = NULL;
static int count = 3;
#define DEVNAME "gpio"

static int gpio_open(struct inode *inode, struct file *filep);
static int gpio_close(struct inode *inode, struct file *filep);
static ssize_t gpio_read(struct file *filep, char __user *buf, size_t size, loff_t *offset);
static ssize_t gpio_write(struct file *filep, const char __user *buf, size_t size, loff_t *offset);
static int gpio_ioctl(struct inode *inode, struct file *filep, unsigned int cmd, unsigned long arg);
static struct file_operations gpio_ops = 
{
	.owner = THIS_MODULE,
	.open = gpio_open,
	.release = gpio_close,
	.ioctl = gpio_ioctl,
	.read = gpio_read,
	.write = gpio_write,
	
};

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

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

static int gpio_ioctl(struct inode *inode, struct file *filep, unsigned int cmd, unsigned long arg)
{
	uint32_t temp;
	uint32_t ioposition;
	uint32_t iocurrent;
	uint32_t position;
	gpio_t gpio_arg;
	if ( copy_from_user(&gpio_arg, (void*)arg, sizeof(gpio_t)) )
		return -EFAULT;
	
	switch(cmd)
	{
		case GPIO_INIT:
			switch((uint32_t)gpio_arg.GPIOx)
			{
			case (uint32_t)GPIOA: __GPIOA_CLK_ENABLE();	break;
			case (uint32_t)GPIOB: __GPIOB_CLK_ENABLE();	break;
			case (uint32_t)GPIOC: __GPIOC_CLK_ENABLE();	break;
			case (uint32_t)GPIOD: __GPIOD_CLK_ENABLE();	break;
			case (uint32_t)GPIOE: __GPIOE_CLK_ENABLE();	break;
			case (uint32_t)GPIOF: __GPIOF_CLK_ENABLE();	break;
			case (uint32_t)GPIOG: __GPIOG_CLK_ENABLE();	break;
			case (uint32_t)GPIOH: __GPIOH_CLK_ENABLE();	break;
			case (uint32_t)GPIOI: __GPIOI_CLK_ENABLE();	break;			
			}
			HAL_GPIO_Init(gpio_arg.GPIOx, &gpio_arg.GPIO_Init);
			break;
		case GPIO_WRITE_PIN:
			HAL_GPIO_WritePin(gpio_arg.GPIOx, gpio_arg.GPIO_pin, gpio_arg.Data);
			break;
		case GPIO_READ_PIN:
			gpio_arg.Data = HAL_GPIO_ReadPin(gpio_arg.GPIOx, gpio_arg.GPIO_pin);
			if ( copy_to_user((void*)arg, (void*)&gpio_arg, sizeof(gpio_t)))
				return -EFAULT;
			break;
		case GPIO_SET_DIR:
			/* Configure the port pins */
		  	for(position = 0U; position < 16; position++)
		  	{
		    	/* Get the IO position */
		    	ioposition = 0x01U << position;
		    	/* Get the current IO position */
		    	iocurrent = (uint32_t)(gpio_arg.GPIO_pin) & ioposition;

			    if(iocurrent == ioposition)
			    {
					temp = gpio_arg.GPIOx->MODER;
			      	temp &= ~(GPIO_MODER_MODER0 << (position * 2U));
			      	temp |= (gpio_arg.Direct << (position * 2U));
			      	gpio_arg.GPIOx->MODER = temp;
			    }
		    }
			break;
	default:
		printk("---> Invalid action\n");
		return -EINVAL;
		break;		

	}
	return 0;
}

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

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



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

	cdev_init(gpio_cdev,&gpio_ops);
	devno = MKDEV(major,minor);
	ret = register_chrdev_region(devno, count, DEVNAME);
	if(ret){
		goto ERR_STEP;
	}
	
	ret = cdev_add(gpio_cdev, devno, count);
	if(ret){
		goto ERR_STEP1;
	}
	return 0;

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

static void __exit gpio_exit(void)
{
	unregister_chrdev_region(MKDEV(major,minor),count);
	cdev_del(gpio_cdev);
}

module_init(gpio_init);
module_exit(gpio_exit);

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



