#include <linux/module.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/ioctl.h>
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/string.h>
#include <linux/slab.h>
#include <asm/uaccess.h>
#include "gpio.h"
struct gpio_cdev
{
struct cdev cdev;
int count;
struct class *gpio_class;
struct device *gpio_device;
};
int gpio_open(struct inode * inode , struct file * filp )
{
printk("63-->[%s] %s %d : gpio_open\n", __FILE__, __func__, __LINE__);
return 0;
}
int gpio_release(struct inode *inode, struct file *filp)
{
printk("63-->[%s] %s %d : gpio_release\n", __FILE__, __func__, __LINE__);
return 0;
}
ssize_t gpio_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
{
char kernel_buf[64] = {0};
memset(kernel_buf, 0, sizeof(kernel_buf));
strncpy(kernel_buf, "63-->this is kernel space data", sizeof(kernel_buf));
kernel_buf[63] = '\0';
if (copy_to_user(buf, kernel_buf, strlen(kernel_buf)))
{
printk("63-->[%s] %s %d : gpio_get failed!\n", __FILE__, __func__, __LINE__);
return -EFAULT;
}
else
{
printk("63-->[%s] %s %d : kernel_buf = %s\n", __FILE__, __func__, __LINE__, kernel_buf);
}
return 0;
}
ssize_t gpio_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos)
{
char kernel_buf[64] = {0};
if (copy_from_user(kernel_buf, buf, sizeof(kernel_buf)))
{
printk("63-->[%s] %s %d : gpio_set failed!\n", __FILE__, __func__, __LINE__);
return -EFAULT;
}
printk("63-->[%s] %s %d : kernel_buf = %s\n", __FILE__, __func__, __LINE__, kernel_buf);
return 0;
}
static long gpio_ioctl(struct inode *inode,struct file *filp,unsigned int cmd,unsigned long arg)
{
static gpio_opt gpio;
int n = 0;
if (copy_from_user(&gpio, (gpio_opt *)arg, sizeof(gpio_opt)))
return -EFAULT;
switch(cmd){
case IOCTL_GPIO_GET:
gpio.gpio_status = 2;
n = copy_to_user((gpio_opt *)arg, &gpio, sizeof(gpio_opt));
if (n == 0)
{
printk("63-->[%s] %s %d : gpio.gpio_num = %d gpio.gpio_status = %d\n", __FILE__, __func__, __LINE__, gpio.gpio_num, gpio.gpio_status);
}
else
{
printk("63-->[%s] %s %d : gpio_get failed!\n", __FILE__, __func__, __LINE__);
return -EFAULT;
}
break;
case IOCTL_GPIO_SET:
if((gpio.gpio_num == 111) && (gpio.gpio_status == 1))
{
printk("63-->[%s] %s %d : gpio.gpio_num = %d gpio.gpio_status = %d\n", __FILE__, __func__, __LINE__, gpio.gpio_num, gpio.gpio_status);
}
else
{
printk("63-->[%s] %s %d : gpio_set failed!\n", __FILE__, __func__, __LINE__);
}
break;
default:
return -ENOTTY;
}
return 0;
}
static struct file_operations gpio_fops = {
.owner = THIS_MODULE,
.open = gpio_open,
.read = gpio_read,
.write = gpio_write,
.release = gpio_release,
};
static struct gpio_cdev *gpio_dev = NULL;
dev_t gpio_devno;
static int gpio_major = 111;
static int gpio_minor = 0;
static int __init gpio_init(void)
{
int ret = 0;
printk("63-->[%s] %s %d : gpio_init \n", __FILE__, __func__, __LINE__);
gpio_dev = kmalloc(sizeof(struct gpio_cdev), GFP_KERNEL);
if(gpio_dev == NULL)
{
printk("[%s] %s %d : kmalloc failed!\n", __FILE__, __func__, __LINE__);
return -ENOMEM;
}
memset(gpio_dev, 0, sizeof(struct gpio_cdev));
gpio_devno = MKDEV(gpio_major, 0);
if(gpio_major)
{
ret = register_chrdev_region(gpio_devno, 1, "63gpio_dev");
if(ret < 0)
{
printk("[%s] %s %d : register_chrdev_region failed!\n", __FILE__, __func__, __LINE__);
goto err_register_chrdev_region;
}
}
else
{
ret = alloc_chrdev_region(gpio_devno, 0, 1, "63gpio_dev");
if(ret < 0)
{
printk("[%s] %s %d : alloc_chrdev_region failed!\n", __FILE__, __func__, __LINE__);
goto err_alloc_chrdev_region;
}
}
gpio_major = MAJOR(gpio_devno);
gpio_minor = MINOR(gpio_devno);
cdev_init(&(gpio_dev->cdev), &gpio_fops);
gpio_dev->cdev.owner = THIS_MODULE;
ret = cdev_add(&(gpio_dev->cdev), gpio_devno, 1);
if(ret < 0)
{
printk("[%s] %s %d : cdev_add failed!\n", __FILE__, __func__, __LINE__);
goto err_cdev_add;
}
gpio_dev->gpio_class = class_create(THIS_MODULE, "63gpio_class");
if(IS_ERR(gpio_dev->gpio_class))
{
ret = PTR_ERR(gpio_dev->gpio_class);
printk("[%s] %s %d : class_create failed! ret = %d\n", __FILE__, __func__, __LINE__, ret);
goto err_class_create;
}
gpio_dev->gpio_device = device_create(gpio_dev->gpio_class, NULL, gpio_devno, "gpio_dev", 0);
if(IS_ERR(gpio_dev->gpio_device))
{
ret = PTR_ERR(gpio_dev->gpio_device);
printk("[%s] %s %d : device_create failed! ret = %d\n", __FILE__, __func__, __LINE__, ret);
goto err_device_create;
}
err_device_create:
device_destroy(gpio_dev->gpio_class, gpio_devno);
err_class_create:
class_destroy(gpio_dev->gpio_class);
err_cdev_add:
cdev_del(&(gpio_dev->cdev));
err_alloc_chrdev_region:
kfree(gpio_dev);
err_register_chrdev_region:
unregister_chrdev_region(MKDEV(gpio_devno, 0), 1);
return -1;
return 0;
}
void __exit gpio_exit(void)
{
device_destroy(gpio_dev->gpio_class, gpio_devno);
class_destroy(gpio_dev->gpio_class);
cdev_del(&(gpio_dev->cdev));
kfree(gpio_dev);
unregister_chrdev_region(MKDEV(gpio_devno, 0), 1);
printk(KERN_ALERT "63-->ptdrv gpio exit\n");
}
module_init(gpio_init);
module_exit(gpio_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("IS_ME");
MODULE_DESCRIPTION("gpio driver");