一. 简介
前面几篇文章学习了 Linux异步通知机制,以及驱动或应用程序关于异步通知机制是如何处理的。
驱动对异步通知的处理方法如下:
Linux应用程序对异步通知的处理-CSDN博客
本文开始通过实验,学习异步通知机制方法。本文具体来学习驱动对异步通知的处理。
二. Linux异步通知实验:驱动中异步通知的处理
本实验是在按键中断实验代码的基础上,进行更改或添加。
1. 创建工程
(1)首先,创建 15_async_notice 工程目录:
wangtian@wangtian-virtual-machine:~/zhengdian_Linux/Linux_Drivers$ mkdir 15_async_notice
(2)其次,进入15_async_notice 目录下,将 14_block _io工程目录下所有文件拷贝到 其目录下:
wangtian@wangtian-virtual-machine:~/zhengdian_Linux/Linux_Drivers/15_async_notice$ cp ../13_key_irq/* ./ -rf
将 .vscode及其下文件也拷贝到 14_block_io目录下:
wangtian@wangtian-virtual-machine:~/zhengdian_Linux/Linux_Drivers/15_async_notice$ cp ../13_key_irq/.vscode/ ./ -rf
注意: .vscode目录下的文件,设置了驱动可能会调用到的 Linux内核源码(这里是NXP官方提供的Linux内核源码)的路径。
2. 驱动对异步通知的处理实现
驱动对异步通知的处理方法,可以参考 Linux内核源码中(这里为 NXP官方的Linux内核源码)一些驱动中的实现。
key_irq.c文件添加了驱动异步通知的处理代码后,内容如下:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <linux/timer.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/types.h>
#include <asm/atomic.h>
#include <linux/signal.h>#define IRQ_NAME "key_irq"
#define IRQ_CNT 1
#define KEY_NUM 1
#define KEY0_VALUE 0x01 //key0按键值
#define INVAL_KEY_VALUE 0xFF //无效的按键值static int irq_dev_open(struct inode *inode, struct file *filp);
ssize_t irq_dev_read(struct file * filp, char __user * buf, size_t count, loff_t * ppos);
static int irq_dev_fasync(int fd, struct file *filp, int mode);
int irq_dev_close(struct inode * inode, struct file * filp);static const struct file_operations irq_fops = {.open = irq_dev_open,.owner = THIS_MODULE,.read = irq_dev_read,.fasync = irq_dev_fasync,.release = irq_dev_close,
};/*key按键结构体 */
struct key_dev{int gpio_number; //IO编号int interrupt_number; //中断号unsigned char value; //键值unsigned char name[50]; //按键名字irqreturn_t (*handler)(int, void*); //中断处理函数
};/*imx6ull_irq设备结构体 */
struct irq_dev{dev_t devid; //主设备号+次设备号int major; //主设备号int minor; //次设备号struct cdev cdev;struct class* class;struct device* device;struct device_node * dev_node;//设备节点struct key_dev key[KEY_NUM];struct timer_list timer; //定时器atomic_t key_value; //按键值atomic_t key_release; //按键释放标志struct fasync_struct * async_queue; //异步通知相关的结构体
};struct irq_dev irq;/* 打开设备函数*/
static int irq_dev_open(struct inode *inode, struct file *filp)
{filp->private_data = &irq;return 0;
}/*读取设备数据函数 */
ssize_t irq_dev_read(struct file * filp, char __user * buf, size_t count, loff_t * ppos)
{int ret = 0;struct irq_dev* dev = filp->private_data;unsigned char key_release = 0;unsigned char key_value = 0;key_release = atomic_read(&dev->key_release);key_value = atomic_read(&dev->key_value);if(key_release) //判断按键是否释放{if(key_value & 0x80){key_value &= ~(0x80); //去掉标志位ret = copy_to_user(buf, &key_value, sizeof(key_value));if(ret != 0){printk("copy_to_user failed!\n");goto invalid_key;}}key_release = 0;atomic_set(&dev->key_release, 0);}else{goto invalid_key;}return ret;invalid_key: return -EINVAL;
}/*驱动异步通知函数 */
static int irq_dev_fasync(int fd, struct file *filp, int mode)
{int ret = 0;struct irq_dev* dev = filp->private_data;ret = fasync_helper(fd, filp, mode, &dev->async_queue);if(ret < 0){return -EIO;}return ret;
}/* 关闭设备函数 */
int irq_dev_close(struct inode * inode, struct file * filp)
{return irq_dev_fasync(-1, filp, 0); //释放异步通知结构体
}/*定时器处理函数 */
void timer_handler(unsigned long data)
{int value = 0;struct irq_dev* dev = (struct irq_dev*)data;//读取按键值value = gpio_get_value(dev->key[0].gpio_number);if(value == 0) //按键按下{// printk("KEY0 Press!\n");atomic_set(&dev->key_value, dev->key[0].value);}else if(value == 1) //按键释放{// printk("KEY0 Release!\n");//按键值最高位置1,打上标志atomic_set(&dev->key_value, (0x80|dev->key[0].value));atomic_set(&dev->key_release, 1); //一次完整的按键标志} if(atomic_read(&dev->key_release)) //当有一次有效按键时,发送信号{kill_fasync(&dev->async_queue, SIGIO, POLL_IN);}
}/* 中断处理函数 */
static irqreturn_t key0_irq_handler(int irq, void *param)
{struct irq_dev* dev = param;dev->timer.data = (volatile unsigned long)param;//设置定时器超时时间,开启定时器mod_timer(&dev->timer, jiffies + msecs_to_jiffies(20)); //按键按下后延时20msreturn IRQ_HANDLED;
}/*按键初始化 */
static int key_io_init(struct irq_dev* dev)
{int ret = 0;int i = 0;int n = 0;/*1.获取设备节点 */dev->dev_node = of_find_node_by_path("/key");if(NULL == dev->dev_node){printk("find dev_node failed!\n");goto find_dev_node;}/*2.获取IO编号 */for(i=0; i< KEY_NUM; i++){ dev->key[i].gpio_number = of_get_named_gpio(dev->dev_node, "key-gpio", i);if(dev->key[i].gpio_number < 0){printk("get gpio number failed!\n");goto get_gpio_number;}}/*3.申请IO */for(i = 0; i< KEY_NUM; i++){memset(dev->key[i].name, 0, sizeof(dev->key[i].name));sprintf(dev->key[i].name, "KEY%d", i);ret = gpio_request(dev->key[i].gpio_number, dev->key[i].name);if(ret != 0){printk("gpio request fail!\n");goto gpio_request;}/* 将GPIO设置为输入*/gpio_direction_input(dev->key[i].gpio_number);/* 获取中断号*///dev->key[i].interrupt_number = irq_of_parse_and_map(dev->dev_node, i);dev->key[i].interrupt_number = gpio_to_irq(dev->key[i].gpio_number);if(!dev->key[i].interrupt_number){printk("irq_of_parse_and_map fail!\n");goto get_irq_number;}}dev->key[0].handler = key0_irq_handler;dev->key[0].value = KEY0_VALUE;/*申请中断 */for(i=0; i<KEY_NUM; i++){ret = request_irq(dev->key[i].interrupt_number, dev->key[i].handler, (IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING), dev->key[i].name, dev); printk("request_irq ret: %d\n", ret);if(ret != 0){printk("%d request interrupt fail!\n", i);goto irq_request;} }/*定时器初始化 */init_timer(&dev->timer);dev->timer.function = timer_handler;return 0;irq_request:
get_irq_number:for(i=0; i<KEY_NUM; i++){gpio_free(dev->key[i].gpio_number);}
gpio_request:for(n = 0; n<i; n++){gpio_free(dev->key[n].gpio_number);}
get_gpio_number:
find_dev_node:return ret;
}/*驱动入口函数 */
static int __init imx6ull_irq_init(void)
{int ret = 0;/*1. 注册/申请设备号 */irq.major = 0; if(irq.major) //如果给出主设备号,则注册设备号{irq.devid = MKDEV(irq.major, 0);ret = register_chrdev_region(irq.devid, IRQ_CNT, IRQ_NAME); }else //否则申请设备号{ret = alloc_chrdev_region(&irq.devid, 0, IRQ_CNT, IRQ_NAME);irq.major = MAJOR(irq.devid);irq.minor = MINOR(irq.devid);}if(ret < 0){printk("devid apply failed!\n");goto devid_failed;}printk("dev: major: %d minor: %d\n", irq.major,irq.minor);/*2. 设备初始化,添加设备*/irq.cdev.owner = THIS_MODULE;cdev_init(&irq.cdev, &irq_fops);ret = cdev_add(&irq.cdev, irq.devid, IRQ_CNT);if(ret < 0){printk("cdev_add failed!\n");goto cdev_init_failed;}/*3. 自动创建设备节点 */irq.class = class_create(THIS_MODULE, IRQ_NAME);if(IS_ERR(irq.class)){printk(KERN_ERR "class_create failed!\n");ret = PTR_ERR(irq.class);goto class_create_failed;}irq.device = device_create(irq.class, NULL, irq.devid, NULL, IRQ_NAME);if (IS_ERR(irq.device)) {printk(KERN_ERR "device_create failed!\n");ret = PTR_ERR(irq.device);goto device_create_failed;}/*初始原子变量*/atomic_set(&irq.key_value, INVAL_KEY_VALUE);atomic_set(&irq.key_release, 0); /*4. 按键初始化*/ret = key_io_init(&irq);if(ret != 0){printk("key_io_init failed!\n");goto key_io_init_failed;}return 0;key_io_init_failed:device_destroy(irq.class, irq.devid);
device_create_failed:class_destroy(irq.class);
class_create_failed:cdev_del(&irq.cdev);
cdev_init_failed: unregister_chrdev_region(irq.devid, IRQ_CNT);
devid_failed:return ret;
}/*驱动出口函数 */
static void __exit imx6ull_irq_exit(void)
{int i = 0;/*删除定时器 */del_timer(&irq.timer);/*释放中断 */for(i=0; i < KEY_NUM; i++){free_irq(irq.key[i].interrupt_number, &irq);}/*释放GPIO编号 */for(i=0; i < KEY_NUM; i++){gpio_free(irq.key[i].gpio_number);}/*2. 删除设备 */cdev_del(&irq.cdev);/*3. 注销设备号*/unregister_chrdev_region(irq.devid, IRQ_CNT);/*4. 销毁设备 & 类*/device_destroy(irq.class, irq.devid);class_destroy(irq.class);
}/*驱动入口与出口函数 */
module_init(imx6ull_irq_init);
module_exit(imx6ull_irq_exit);
MODULE_LICENSE("GPL"); //驱动 License
MODULE_AUTHOR("WeiYing"); //作者
三. 驱动模块编译
对以上的驱动代码进行编译,编译驱动模块:
wangtian@wangtian-virtual-machine:~/zhengdian_Linux/Linux_Drivers/15_async_notice$ make
make -C /home/wangtian/zhengdian_Linux/linux/kernel/linux-imx-rel_imx_4.1.15_2.1.0_ga M=/home/wangtian/zhengdian_Linux/Linux_Drivers/15_async_notice modules
make[1]: 进入目录“/home/wangtian/zhengdian_Linux/linux/kernel/linux-imx-rel_imx_4.1.15_2.1.0_ga”CC [M] /home/wangtian/zhengdian_Linux/Linux_Drivers/15_async_notice/key_irq.oBuilding modules, stage 2.MODPOST 1 modulesCC /home/wangtian/zhengdian_Linux/Linux_Drivers/15_async_notice/key_irq.mod.oLD [M] /home/wangtian/zhengdian_Linux/Linux_Drivers/15_async_notice/key_irq.ko
make[1]: 离开目录“/home/wangtian/zhengdian_Linux/linux/kernel/linux-imx-rel_imx_4.1.15_2.1.0_ga”
wangtian@wangtian-virtual-machine:~/zhengdian_Linux/Linux_Drivers/15_async_notice$
可以看出,驱动模块可以正常编译通过。
接下来编写应用程序对异步通知的处理代码,然后对驱动模块进行测试。