#include <linux/init.h>
#include <linux/module.h>
#include<linux/of.h>
#include<linux/of_irq.h>
#include<linux/interrupt.h>
#include<linux/of_gpio.h>
#include<linux/gpio.h>
#include<linux/fs.h>
#include<linux/io.h>
#include<linux/cdev.h>
#include<linux/ioctl.h>
#include<linux/device.h>
#include<linux/uaccess.h>
#include<linux/platform_device.h>
#include<linux/mod_devicetable.h>
struct resource *res;
int ret;
struct device_node *node;//指向设备节点指针
struct property *pr;//属性结构体指针
struct gpio_desc *desc; //新版接收gpio编号信息
int count=0;
unsigned int irqno; //接收软中断号
int minor=0;//次设备号从0开始
unsigned int major=0; //动态申请
struct cdev *cdev;
struct class *cls;
struct device *dev;
wait_queue_head_t wq_head;
unsigned int condition=0;
int mycdev_open(struct inode *inode,struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
ssize_t mycdev_read(struct file *file,char __user *ubuf,size_t size, loff_t *loff)
{
int ret;
if(file->f_flags&O_NONBLOCK)
{
//非阻塞
return -EINVAL;
}
else{
//阻塞
ret=wait_event_interruptible(wq_head,condition);
if(ret)
{
printk("接收错误\n");
return ret;
}
}
ret=copy_to_user(ubuf,&count,size);
if(ret)
{
printk("数据从内核向用户拷贝失败\n");
return -EIO;
}
//condition=0;
condition=0;
return size;
}
ssize_t mycdev_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
//操作方法结构体的初始化
struct file_operations fops=
{
.open=mycdev_open,
.read=mycdev_read,
.write=mycdev_write,
.release=mycdev_close,
};
//中断处理函数
irqreturn_t irq_hander(int irq,void *dev)
{
//灯状态取反
gpiod_set_value(desc,!gpiod_get_value(desc));
//数值取反
if(count)
count=0;
else
count=1;
//唤醒阻塞io读取
condition = 1;
wake_up_interruptible(&wq_head);
return IRQ_HANDLED;
}
int pdrv_probe(struct platform_device *pdr)
{
//获取设备信息
printk("%s:%d\n",__FILE__,__LINE__);
res=platform_get_resource(pdr,IORESOURCE_MEM,0);
if(res==NULL)
{
printk("获取资源失败\n");
return -ENODATA;
}
irqno=platform_get_irq(pdr,0);
if(irqno<0)
{
printk("获取资源失败\n");
return irqno;
}
printk("%#x %d\n",res->start,irqno);
ret=request_irq(irqno,irq_hander,IRQF_TRIGGER_FALLING,"key2_inte",NULL);
if(ret)
{
printk("注册中断失败\n");
return ret;
}
printk("注册中断成功\n");
//获取GPIO编号
desc=gpiod_get_from_of_node(pdr->dev.of_node,"led2",0,GPIOD_OUT_HIGH,NULL);
if(IS_ERR(desc))
{
printk("获取GPIO编号失败\n");
return PTR_ERR(desc);
}
return 0;
}
int pdrv_remove(struct platform_device *pdr)
{
printk("%s:%d\n",__FILE__,__LINE__);
return 0;
}
//定义compatible表
struct of_device_id oftable[]={
{.compatible="hqyj,platform",},
{},
};
//定义并初始化对象
struct platform_driver pdrv ={
.probe=pdrv_probe,
.remove=pdrv_remove,
.driver={
.name= "test",
.of_match_table= oftable,//设备树匹配
},
};
static int __init mycdev_init(void)
{
int ret;
dev_t devno;
//分配对象
cdev=cdev_alloc();
if(NULL==cdev)
{
printk("分配对象失败\n");
ret = -ENOMEM;
goto ERR1;
}
printk("分配对象成功\n");
//对象的初始化
cdev_init(cdev,&fops);
//申请设备号
//动态申请
ret=alloc_chrdev_region(&devno,minor,1,"my_led");
if(ret)
{
printk("动态申请设备号失败\n");
goto ERR2;
}
major = MAJOR(devno);
minor = MINOR(devno);
printk("动态申请设备号成功\n");
//注册字符设备驱动
ret=cdev_add(cdev,MKDEV(major,minor),1);
if(ret)
{
printk("字符设备驱动注册失败\n");
goto ERR3;
}
printk("字符设备驱动注册成功\n");
//向上提交设备节点目录
cls=class_create(THIS_MODULE,"led");
if(IS_ERR(cls))
{
printk("创建逻辑节点目录失败\n");
ret=PTR_ERR(cls);
goto ERR4;
}
printk("创建逻辑节点目录成功\n");
//向上提交设备节点信息
dev = device_create(cls,NULL,MKDEV(major,0),NULL,"mymy_led");
if(IS_ERR(dev))
{
printk("创建逻辑节点失败\n");
ret = PTR_ERR(dev);
goto ERR5;
}
printk("创建逻辑节点成功\n");
//注册对象
platform_driver_register(&pdrv);
//初始化队列头
init_waitqueue_head(&wq_head);
return 0;
ERR5:
device_destroy(cls,MKDEV(major,0));
class_destroy(cls);
ERR4:
cdev_del(cdev);
ERR3:
unregister_chrdev_region(MKDEV(major,minor),count);
ERR2:
kfree(cdev);
ERR1:
return ret;
}
static void __exit mycdev_exit(void)
{
//销毁设备节点
device_destroy(cls,MKDEV(major,0));
class_destroy(cls);
//注销字符设备驱动
cdev_del(cdev);
//释放设备号
unregister_chrdev_region(MKDEV(major,minor),1);
//释放动态申请的空间
kfree(cdev);
//释放设备号
gpiod_put(desc);
//注销中断
free_irq(irqno,NULL);
//注销
platform_driver_unregister(&pdrv);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");