任务要求
通过platform总线驱动实现
a.应用程序通过阻塞的io模型来读取number变量的值
b.number是内核驱动中的一个变量
c.number的值随着按键按下而改变(按键中断) 例如number=0 按下按键number=1 ,再次按下按键number=0
e.驱动中需要编写字符设备驱动
f.驱动中需要自动创建设备节点
g.这个驱动需要的所有设备信息放在设备树的同一个节点中
代码实现
驱动代码
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define CNAME "keyirq"
struct device_node *node;
struct device_node *node1;
struct resource *res;
struct gpio_desc *desc;
unsigned int irqno;
char number = '0';
int count;
int minor=0;
#if 1
unsigned int major = 0;
#else
unsigned int major = 500;
#endif
char kbuf[128]={};
struct cdev *cdev;
struct class *cls;
struct device *dev;
struct gpio_desc *desc;
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;
wake_up_interruptible(&wq_head);
if(file->f_flags&O_NONBLOCK)
{
return -EINVAL;
}
else{
ret=wait_event_interruptible(wq_head,condition);
if(ret)
{
return ret;
}
}
if(size>sizeof(kbuf))
size=sizeof(kbuf);
ret=copy_to_user(ubuf,&number,sizeof(number));
if(ret)
{
printk("数据从内核向用户拷贝失败\n");
return -EIO;
}
condition=0;
return size;
}
ssize_t mycdev_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
{
return size;
}
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_handler(int irq, void *dev)
{
printk("key2 interrupt....%d...\n",number);
number=number=='1'?'0':'1';
gpiod_set_value(desc,!gpiod_get_value(desc));
condition=1;
wake_up_interruptible(&wq_head);
return IRQ_HANDLED;
}
struct of_device_id oftable[]={
{.compatible="hqyj,platform",},
{},
};
int pdrv_remove(struct platform_device *pdr)
{
printk("%s:%d\n",__FILE__,__LINE__);
return 0;
}
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);
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;
}
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,i;
dev_t devno;
cdev=cdev_alloc();
platform_driver_register(&pdrv);
if(cdev==NULL)
{
printk("cdev alloc memory err\n");
ret = -ENOMEM;
goto ERR1;
}
printk("对象分配成功\n");
cdev_init(cdev,&fops);
if(major==0)
{
ret=alloc_chrdev_region(&devno,minor,1,"keyirq");
if(ret)
{
printk("动态申请设备号失败\n");
goto ERR2;
}
major = MAJOR(devno);
minor = MINOR(devno);
printk("动态申请设备号成功\n");
}
else
{
ret=register_chrdev_region(MKDEV(major,minor),1,"keyirq");
if(ret)
{
printk("静态申请设备号失败\n");
goto ERR2;
}
printk("静态申请设备号成功\n");
}
ret=cdev_add(cdev,MKDEV(major,minor),1);
if(ret)
{
printk("字符设备驱动注册失败\n");
goto ERR3;
}
printk("注册字符设备驱动成功\n");
cls = class_create(THIS_MODULE,"keyirq1");
if(IS_ERR(cls))
{
printk("创建逻辑节点目录失败\n");
ret=PTR_ERR(cls);
goto ERR4;
}
printk("创建逻辑节点目录成功\n");
dev = device_create(cls,NULL,MKDEV(major,0),NULL,"keyirq");
if(IS_ERR(dev))
{
printk("创建逻辑节点失败\n");
ret = PTR_ERR(dev);
goto ERR5;
}
init_waitqueue_head(&wq_head);
node=of_find_node_by_name(NULL,"keyirq");
if(node==NULL)
{
printk("解析设备树节点失败\n");
return EAGAIN;
}
printk("解析设备树节点成功\n");
irqno=irq_of_parse_and_map(node,0);
if(irqno==0)
{
printk("获取软中断号失败\n");
return EINVAL;
}
printk("获取软中断号成功\n");
ret=request_irq(irqno,irq_handler,IRQF_TRIGGER_FALLING,"key2_inte",NULL);
if(ret)
{
printk("注册中断失败\n");
return ret;
}
printk("注册中断成功\n");
printk("创建逻辑节点成功\n");
return 0;
ERR5:
for(--i;i>=0;i--)
{
device_destroy(cls,MKDEV(major,i));
}
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),count);
kfree(cdev);
free_irq(irqno,NULL);
gpiod_set_value(desc,0);
gpiod_put(desc);
platform_driver_unregister(&pdrv);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
测试代码
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char const *argv[])
{
int fd=open("/dev/keyirq",O_RDWR);
char number;
if(fd<0)
{
printf("打开设备文件失败\n");
exit(-1);
}
printf("设备文件打开成功\n");
while(1)
{
read(fd,&number,sizeof(number));
printf("number=%c\n",number);
}
close(fd);
return 0;
}
分析思路
1
1.需要通过阻塞IO方式读取内核数据信息,所以在测试文件中调用read函数
内核中需要使用copy_to_user函数实现从内核到用户的数据拷贝
/read()
ssize_t mycdev_read(struct file *file, char __user *ubuf, size_t size, loff_t *loff)
{
int ret;
wake_up_interruptible(&wq_head);
if(file->f_flags&O_NONBLOCK)
{
return -EINVAL;
}
else{
ret=wait_event_interruptible(wq_head,condition);
if(ret)
{
return ret;
}
}
if(size>sizeof(kbuf))
size=sizeof(kbuf);
ret=copy_to_user(ubuf,&number,sizeof(number));
if(ret)
{
printk("数据从内核向用户拷贝失败\n");
return -EIO;
}
condition=0;
return size;
}
2
number的值需要随着按键按下而改变(按键中断),所以在中断处理函数中,在唤醒之前对number进行0/1变换操作
irqreturn_t irq_handler(int irq, void *dev)
{
printk("key2 interrupt....%d...\n",number);
number=number=='1'?'0':'1';
gpiod_set_value(desc,!gpiod_get_value(desc));
condition=1;
wake_up_interruptible(&wq_head);
return IRQ_HANDLED;
}
3
在按下按键的时候需要同时将led1的状态取反
GPIO设备信息由platform总线从设备树中获取
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);
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;
}