#include
int main(int argc, const char* argv[])
{
int fd;
int status;
if ((fd = open("/dev/platform_irq_led", O_RDWR)) == -1) {
perror("open error");
exit(EXIT_FAILURE);
}
while (1) {
read(fd, &status, sizeof(status));
printf("status = %d\n", status);
}
close(fd);
return 0;
}
demo.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define CNAME "platform_irq_led"
int major;
struct class *cls;
struct device *dev;
wait_queue_head_t wq;
unsigned int condition=0;
unsigned int status=0;
struct gpio_desc *desc;
unsigned int irqno;
irqreturn_t irq_led_handle(int irq, void *dev)
{
status = gpiod_get_value(desc);
status = !status;
gpiod_set_value(desc,status);
condition=1;
wake_up_interruptible(&wq);
return IRQ_HANDLED;
}
int irq_led_open(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
ssize_t irq_led_read(struct file *file,
char __user *ubuf, size_t size, loff_t *offs)
{
int ret;
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
if(file->f_flags & O_NONBLOCK){
//非阻塞
return -EINVAL;
}else{
//阻塞
ret = wait_event_interruptible(wq,condition);
if(ret < 0){
printk("receive signal....\n");
return ret;
}
}
//将数据拷贝到用户空间
if(size > sizeof(status)) size = sizeof(status);
ret = copy_to_user(ubuf,(void *)&status,size);
if(ret){
printk("copy data to user error\n");
return -EIO;
}
//4.将条件清零
condition = 0;
return size;
}
int irq_led_close(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
return 0;
}
struct file_operations fops = {
.open = irq_led_open,
.read = irq_led_read,
.release = irq_led_close,
};
int pdrv_probe(struct platform_device* pdev) //在进入probe函数的时候节点已经被放在pdev->dev.of_node中了
{
int ret;
//1.注册字符设备驱动
major = register_chrdev(0,CNAME,&fops);
if(major < 0){
printk("register char device driver error\n");
ret = major;
goto ERR1;
}
//2.创建设备节点
cls = class_create(THIS_MODULE,CNAME);
if(IS_ERR(cls)){
printk("class create error\n");
ret = PTR_ERR(cls);
goto ERR2;
}
dev = device_create(cls,NULL,MKDEV(major,0),NULL,CNAME);
if(IS_ERR(dev)){
printk("device create error\n");
ret = PTR_ERR(dev);
goto ERR3;
}
//3.初始化等待队列头
init_waitqueue_head(&wq);
desc = gpiod_get_from_of_node(pdev->dev.of_node,"led1",0,GPIOD_OUT_LOW,NULL);
if(IS_ERR(desc)){
printk("get gpiod error\n");
ret = PTR_ERR(desc);
goto ERR4;
}
irqno = platform_get_irq(pdev,0); //获取中断的信息
if(irqno < 0){
printk("get irq resource error\n");
ret = irqno;
goto ERR5;
}
ret = request_irq(irqno,irq_led_handle,IRQF_TRIGGER_FALLING,CNAME,NULL);
if(ret){
printk("request irq error\n");
goto ERR5;
}
return 0;
ERR5:
gpiod_put(desc);
ERR4:
device_destroy(cls,MKDEV(major,0));
ERR3:
class_destroy(cls);
ERR2:
unregister_chrdev(major,CNAME);
ERR1:
return ret;
}
int pdrv_remove(struct platform_device* pdev)
{
printk("%s:%d\n", __func__, __LINE__);
free_irq(irqno,NULL);
gpiod_put(desc);
device_destroy(cls,MKDEV(major,0));
class_destroy(cls);
unregister_chrdev(major,CNAME);
return 0;
}
struct of_device_id oftable[] = {
{.compatible = "hqyj,platform",},
{/*end*/},
};
struct platform_driver pdrv = {
.probe = pdrv_probe,
.remove = pdrv_remove,
.driver = {
.name = "hahaha",
.of_match_table = oftable,
},
};
//这个宏能够实现入口,出口,注册,注销的过程
module_platform_driver(pdrv);
MODULE_LICENSE("GPL");