一、实验说明
异步通知提供一种类似中断的机制,当驱动程序可以访问的时候主动告诉应用程序,类似中断。“信号”为此应运而生,信号类似于我们硬件上使用的“中断”,
只不过信号是软件层次上的。算是在软件层次上对中断的一种模拟,驱动可以通过主动向应用程序发送信号的方式来报告自己可以访问了,应用程序获取到信号以后就可以从驱动设备中读取或者写入数据了。整个过程就相当于应用程序收到了驱动发送过来了的一个中断,然后应用程序去响应这个中断,在整个处理过程中应用程序并没有去查询驱动设备是否可以访问,一切都是由驱动设备自己告诉给应用程序的。
阻塞、非阻塞、异步通知,这三种是针对不同的场合提出来的不同的解决方法,没有优劣之分,在实际的工作和学习中,根据自己的实际需求选择合适的处理方法即可。
异步通知的核心就是信号,在 arch/xtensa/include/uapi/asm/signal.h 文件中定义了 Linux 所支持的所有信号,这些信号如下所示:
示例代码 Linux 信号
#define SIGHUP 1 /* 终端挂起或控制进程终止 */
#define SIGINT 2 /* 终端中断(Ctrl+C 组合键) */
#define SIGQUIT 3 /* 终端退出(Ctrl+\组合键) */
#define SIGILL 4 /* 非法指令 */
#define SIGTRAP 5 /* debug 使用,有断点指令产生 */
#define SIGABRT 6 /* 由 abort(3)发出的退出指令 */
#define SIGIOT 6 /* IOT 指令 */
#define SIGBUS 7 /* 总线错误 */
#define SIGFPE 8 /* 浮点运算错误 */
#define SIGKILL 9 /* 杀死、终止进程 */
#define SIGUSR1 10 /* 用户自定义信号 1 */
#define SIGSEGV 11 /* 段违例(无效的内存段) */
#define SIGUSR2 12 /* 用户自定义信号 2 */
#define SIGPIPE 13 /* 向非读管道写入数据 */
#define SIGALRM 14 /* 闹钟 */
#define SIGTERM 15 /* 软件终止 */
#define SIGSTKFLT 16 /* 栈异常 */
#define SIGCHLD 17 /* 子进程结束 */
#define SIGCONT 18 /* 进程继续 */
#define SIGSTOP 19 /* 停止进程的执行,只是暂停 */
#define SIGTSTP 20 /* 停止进程的运行(Ctrl+Z 组合键) */
#define SIGTTIN 21 /* 后台进程需要从终端读取数据 */
#define SIGTTOU 22 /* 后台进程需要向终端写数据 */
#define SIGURG 23 /* 有"紧急"数据 */
#define SIGXCPU 24 /* 超过 CPU 资源限制 */
#define SIGXFSZ 25 /* 文件大小超额 */
#define SIGVTALRM 26 /* 虚拟时钟信号 */
#define SIGPROF 27 /* 时钟信号描述 */
#define SIGWINCH 28 /* 窗口大小改变 */
#define SIGIO 29 /* 可以进行输入/输出操作 */
#define SIGPOLL SIGIO
/* #define SIGLOS 29 */
#define SIGPWR 30 /* 断点重启 */
#define SIGSYS 31 /* 非法的系统调用 */
#define SIGUNUSED 31 /* 未使用信号 */
在示例代码 53.1.1.1 中的这些信号中,除了 SIGKILL(9)和 SIGSTOP(19)这两个信号不能被忽略外,其他的信号都可以忽略。这些信号就相当于中断号,不同的中断号代表了不同的中断,不同的中断所做的处理不同,因此,驱动程序可以通过向应用程序发送不同的信号来实现不同的功能。(重点->重写APP接收到信号的处理函数)
二、APP重写信号处理函数举例
/*把SIGINT信号的处理函数重定义为sigint_handler函数*/
/*ctrl+c组合键将向当前控制台执行的程序发送SIGINT信号*/
#include "stdlib.h"
#include "stdio.h"
#include "signal.h"
void sigint_handler(int num)
{
printf("\r\nSIGINT signal!\r\n");
exit(0);/*退出当前程序*/
}
int main(void)
{
signal(SIGINT, sigint_handler);
while(1);
return 0;
}
三、原理图
四、设备树
五、驱动程序
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/slab.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_irq.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/string.h>
#include <linux/irq.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/interrupt.h>
#include <linux/wait.h>
#include <linux/ide.h>
#define DeviceName "devicetree-key0-fasync" //设备驱动名称
#define DeviceNodes 1 //设备节点个数
#define KEY_NUM 0X01
#define KEY0VALUE 0X01
#define INVAKEY 0XFF
struct irq_keydesc{
int gpio_num; /* io编号 */
int irq_num; /* 中断号 */
unsigned char value; /* 键值 */
char name[25]; /* 名字 */
irqreturn_t (*handler) (int, void *); /* 中断处理函数 */
};
/* 定义一个设备结构体 */
struct mod_struct {
dev_t devid; /* 设备号 */
int major; /* 主设备号 */
int minor; /* 次设备号 */
struct cdev cdev; /* cdev */
struct class *class; /* 类 */
struct device *device; /* 设备 */
struct device_node *nd; /* 设备节点 */
struct irq_keydesc irqkey0; /* 按键中断 */
struct timer_list timer; /* 内核定时器 */
atomic_t keyvalue; /* 原子变量1->整形*/
atomic_t releasekey; /* 原子变量2->整形*/
wait_queue_head_t r_wait; /* 读(读函数中使用)等待队列头 */
struct fasync_struct *fasync_queue; /* 异步相关结构体 */
};
struct mod_struct mod_device; /* 定义一个设备 */
/*
* @description : 打开设备
* @param - inode : 传递给驱动的inode
* @param - filp : 设备文件,file结构体有个叫做private_data的成员变量
* 一般在open的时候将private_data指向设备结构体。
* @return : 0 成功;其他 失败
*/
static int key0_interrupt_open(struct inode *inode, struct file *filp)
{
filp->private_data = &mod_device; /* 设置私有数据 */
return 0;
}
/*
* @description : 从设备读取数据
* @param - filp : 要打开的设备文件(文件描述符)
* @param - buf : 返回给用户空间的数据缓冲区
* @param - cnt : 要读取的数据长度
* @param - offt : 相对于文件首地址的偏移
* @return : 读取的字节数,如果为负值,表示读取失败
*/
static ssize_t key0_interrupt_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
{
int ret = 0;
unsigned char keyvalue;
unsigned char releasekey;
struct mod_struct *dev = filp->private_data;
keyvalue = atomic_read(&dev->keyvalue);
releasekey = atomic_read(&dev->releasekey);
if(releasekey) { /* 有效按键 */
if(keyvalue & 0x80) {
keyvalue &= ~0x80;
ret = copy_to_user(buf, &keyvalue, sizeof(keyvalue));
} else {
ret = -EINVAL;
goto data_error;
}
atomic_set(&dev->releasekey, 0); /* 按下标志清零 */
} else {
ret = -EINVAL;
goto data_error;
}
data_error:
return ret;
}
/*
* @description : 向设备写数据
* @param - filp : 设备文件,表示打开的文件描述符
* @param - buf : 要写给设备写入的数据
* @param - cnt : 要写入的数据长度
* @param - offt : 相对于文件首地址的偏移
* @return : 写入的字节数,如果为负值,表示写入失败
*/
static ssize_t key0_interrupt_write(struct file *filp, const char __user *buf,size_t cnt, loff_t *offt)
{
return 0;
}
static int key0_interrupt_fasync(int fd,struct file *filp,int on)
{
struct mod_struct *dev = filp->private_data;
return fasync_helper(fd,filp,on,&dev->fasync_queue);
}
/*
* @description : 关闭/释放设备
* @param - filp : 要关闭的设备文件(文件描述符)
* @return : 0 成功;其他 失败
*/
static int key0_interrupt_release(struct inode *inode, struct file *filp)
{
key0_interrupt_fasync(-1,filp,0);/*删除异步通知*/
return 0;
}
/* 设备操作函数 */
static struct file_operations beep_fops = {
.owner = THIS_MODULE,
.open = key0_interrupt_open,
.read = key0_interrupt_read,
.write = key0_interrupt_write,
.fasync = key0_interrupt_fasync,
.release = key0_interrupt_release,
};
/* 按键中断处理函数 */
static irqreturn_t key0_handler(int irq, void *dev_id)
{
struct mod_struct *dev = dev_id;
dev->timer.data = (volatile long)dev_id;
mod_timer(&dev->timer, jiffies + msecs_to_jiffies(20)); /* 20ms定时 */
return IRQ_HANDLED;
}
/* 定时器处理函数 */
static void timer_func(unsigned long arg) {
int value = 0;
struct mod_struct *dev = (struct mod_struct*)arg;
value = gpio_get_value(dev->irqkey0.gpio_num);
if(value == 0) { /* 按下 */
atomic_set(&dev->keyvalue, dev->irqkey0.value);/*=0x01*/
} else if(value == 1) { /* 释放 */
atomic_set(&dev->keyvalue, 0X80 | (dev->irqkey0.value));
atomic_set(&dev->releasekey, 1); /* 完成的按键过程 */
}
if(atomic_read(&dev->releasekey)) {/* 按键有效 */
/*发送SIGIO信号*/
kill_fasync(&dev->fasync_queue,SIGIO,POLL_IN);
}
}
/*
* @description : 按键初始化函数
* @param : dev
* @return : 无
*/
static int key_initx(struct mod_struct *dev)
{
int ret = 0;
/* 1、获取设备节点:/devicetree-leds-pincrl */
dev->nd = of_find_node_by_path("/devicetree-key0-interrupt");
if(dev->nd == NULL) {
printk("devicetree-key0-interrupt node not find!\r\n");
ret = -EINVAL;
goto fail_fd;
} else {
printk("devicetree-key0-interrupt node find!\r\n");
}
/* 2、从设备节点下的led-gpio属性里获取gpio编号 */
dev->irqkey0.gpio_num = of_get_named_gpio(dev->nd,"key-gpio", 0);
if(dev->irqkey0.gpio_num < 0) {
printk("can't get key-gpio\r\n");
ret = -EINVAL;
goto fail_gpio;
}
printk("key-gpio num = %d\r\n", dev->irqkey0.gpio_num);
/*3、申请gpio(检查是否被占用)*/
sprintf(dev->irqkey0.name,"%s","key0-gpio-interrupt");
ret = gpio_request(dev->irqkey0.gpio_num,dev->irqkey0.name);
if(ret)
{
ret = -EBUSY;
printk("IO %d busy,can't request!\r\n",dev->irqkey0.gpio_num);
goto fail_ioreq;
}
/*4、设置key0为输入 */
ret = gpio_direction_input(dev->irqkey0.gpio_num);
if(ret < 0) {
printk("can't set gpio!\r\n");
goto fail_ioset;
}
/*5、根据IO编号获取其中断号*/
dev->irqkey0.irq_num=gpio_to_irq(dev->irqkey0.gpio_num);
#if 0
dev->irqkey0.irq_num = irq_of_parse_and_map(dev->nd,0);
#endif
/*6、设置中断函数*/
dev->irqkey0.handler = key0_handler;
dev->irqkey0.value = KEY0VALUE;
/*7、注册中断*/
ret = request_irq(dev->irqkey0.irq_num,
dev->irqkey0.handler,
IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING,
dev->irqkey0.name,
&mod_device);
if(ret){
printk("irq %d request failed!\r\n", dev->irqkey0.irq_num);
goto fail_irq;
}
/*8、初始化定时器*/
init_timer(&mod_device.timer);
mod_device.timer.function = timer_func;
return 0;
fail_irq:
fail_ioset:
gpio_free(dev->irqkey0.gpio_num);
fail_ioreq:
fail_gpio:
fail_fd:
return ret;
}
/*
* @description : 驱动出口函数
* @param : 无
* @return : 无
*/
static int __init key0_interrupt_init(void)
{
int ret = 0;
/* 注册字符设备驱动 */
/* 1、创建设备号 */
if (mod_device.major)
{
mod_device.devid = MKDEV(mod_device.major,0);//设备号起始点(major,0)
//设备号起始点开始,申请DeviceNodes个设备号
ret = register_chrdev_region(mod_device.devid,DeviceNodes,DeviceName);
}
else {
ret = alloc_chrdev_region(&mod_device.devid,0,DeviceNodes,DeviceName); /* 申请设备号 */
mod_device.major = MAJOR(mod_device.devid); /* 获取分配号的主设备号 */
mod_device.minor = MINOR(mod_device.devid); /* 获取分配号的次设备号 */
}
if(ret < 0){ printk("设备号申请失败\r\n");goto fail_chrdev; }
printk("mod_device major=%d,minor=%d\r\n",mod_device.major,mod_device.minor);
/* 2、初始化cdev并添加cdev */
mod_device.cdev.owner = THIS_MODULE;
cdev_init(&mod_device.cdev,&beep_fops);
ret = cdev_add(&mod_device.cdev,mod_device.devid,DeviceNodes);
if(ret < 0){ printk("添加cdev失败!\r\n");goto fail_cdev; }
/* 3、创建类 */
mod_device.class = class_create(THIS_MODULE,DeviceName);
if (IS_ERR(mod_device.class)) { ret = PTR_ERR(mod_device.class);goto fail_class; }
/* 4、创建设备节点*/
mod_device.device = device_create(mod_device.class,NULL,mod_device.devid,NULL,DeviceName);/*和设备设为一个名字*/
if (IS_ERR(mod_device.device)) { ret = PTR_ERR(mod_device.device);goto fail_device; }
/* 5、初始化IO */
ret = key_initx(&mod_device);
if(ret < 0) {
goto fail_keyinit;
}
/* 6、初始化原子变量 */
atomic_set(&mod_device.keyvalue, INVAKEY);/*mod_device.keyvalue=INVAKEY*/
atomic_set(&mod_device.releasekey, 0); /*mod_device.releasekey=0*/
/* 7、初始化等待队列头 */
init_waitqueue_head(&mod_device.r_wait);
/*X、模块加载失败处理部分*/
fail_keyinit:
device_destroy(mod_device.class,mod_device.devid);
fail_device:
class_destroy(mod_device.class);
fail_class:
cdev_del(&mod_device.cdev);
fail_cdev:
unregister_chrdev_region(mod_device.devid,DeviceNodes);
fail_chrdev:
return ret;
}
/*
* @description : 驱动出口函数
* @param : 无
* @return : 无
*/
static void __exit key0_interrupt_exit(void)/*按照相反顺序注销*/
{
/*1、释放中断*/
free_irq(mod_device.irqkey0.irq_num, &mod_device);
/*2、内核定定时器注销部分*/
del_timer_sync(&mod_device.timer);/* 删除timer */
/*3、LED设备注销部分*/
gpio_free(mod_device.irqkey0.gpio_num);/*注销gpio*/
/*4、设备驱动注销部分*/
device_destroy(mod_device.class,mod_device.devid);/*注销设备节点*/
class_destroy(mod_device.class);/*注销类*/
cdev_del(&mod_device.cdev);/*注销cdev*/
unregister_chrdev_region(mod_device.devid,DeviceNodes);/*注销chrdev*/
}
module_init(key0_interrupt_init);
module_exit(key0_interrupt_exit);
MODULE_LICENSE("GPL");
六、APP测试程序
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <signal.h>
/*
*argc:应用程序参数个数
*argv[]:具体的参数内容,字符串形式
*/
int fd;
static void sigio_signal_func(int num)
{
int err;
unsigned int keyvalue = 0;
printf("num = %d \r\n",num);
err = read(fd,&keyvalue,sizeof(keyvalue));
if(err < 0){
printf("error = %d \r\n",err);
}
else
{
printf("sigio singal! keyvalue = %d \r\n",keyvalue);
}
}
int main(int argc, char *argv[])
{
int flags = 0;
char *filename;
if(argc != 2) {
printf("Error Usage!\r\n");
return -1;
}
filename = argv[1];
fd = open(filename,O_RDWR);/*open函数默认是阻塞方式访问设备*/
if(fd < 0) {
printf("file %s open failed!\r\n", filename);
return -1;
}
signal(SIGIO,sigio_signal_func);/*重新定义SIGIO信号处理函数*/
fcntl(fd,F_SETOWN,getpid()); /*设置当前进程可接收到fd的SINIO信号*/
flags = fcntl(fd, F_GETFL); /*获取文件描述符fd的状态*/
fcntl(fd,F_SETFL,flags|FASYNC); /*设置文件描述符fd的状态(开启异步通知)*/
while(1) {
sleep(2);
}
close(fd);
return 0;
}