前面的按键驱动方式都是应用程序通过主动查询的方式获得按键值的:
下面介绍第四种按键驱动的方式
4、异步通知:它可以做到应用程序不用随时去查询按键的状态,而等待有按键被按下后,驱动程序主动发消息给应用程序,应用程序再去处理。
比如说:kill -9 pid即是一种发信号的过程:其中9就是信号值,pid就是被发送的进程的进程号
a、一个简单的异步通知的例子
b、编写测试程序实现异步通知
c、更改按键驱动实现异步通知
1、一个简单的异步通知的例子
直接看到程序源码,可以看到这个程序在主程序里面什么事情也没有做,一直处于睡眠状态。
#include <signal.h> void my_signal_test(int signum) { static int cnt = 0; printf("signal = %d,%d times ",signum,++cnt); } int main(int argc,char **argv) { signal(SIGUSR1, my_signal_test);//建立一个信号函数,接收的信号是SIGUSR1表示用户可用的信号值 while(1) { sleep(1000); } return 0; }
首先这个程序调用了signal这个C库中的函数,在linux下查询它的用法输入man 2 signal
#include <signal.h> //需要包含的头文件 typedef void (*sighandler_t)(int);//信号函数原型,它的参数是信号值 sighandler_t signal(int signum, sighandler_t handler);//函数,其中signum代表发送的信号值,handler表示信号函数
编译这个程序,然后在命令行输入kill -USR1 2333,my_signal函数被运行。
2、更改测试程序实现异步通知
直接看代码,从代码可以看出,实现异步通知在应用层需要如下几步:
1、利用signal(SIGIO, fifth_testsignal)函数注册一个信号,信号处理的函数为fifth_testsignal。SISGIO说明是IO信号量,因为按键驱动属于IO型的。
2、利用fcntl(fd, F_SETOWN, getpid())函数将本应用程序的进程号告诉给内核,最终使得驱动程序可以成功发送信号给应用程序。
3、利用fcntl(fd, F_SETFL, oflags | FASYNC)函数改变fasync标记,最终会调用到驱动的faync > fasync_helper。
4、signal、与fcntl的系统调用过程比较复杂,后面再去分析。这里只能记住是怎么使用它们来是实现异步通知的功能。
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <poll.h> #include <signal.h> static int fd; static void fifth_testsignal(int signum) { unsigned char key_val; printf("signal = %d ",signum); read(fd, &key_val, 1);//读取按键数据,只有收到按键数据驱动层才会发送消息给应用层。 printf("signumkey_val: 0x%x ",key_val); } /* *usage ./buttonstest */ int main(int argc, char **argv) { char* filename="dev/buttons"; int oflags; fd = open(filename, O_RDWR);//打开dev/firstdrv设备文件 if (fd < 0)//小于0说明没有成功 { printf("error, can't open %s ", filename); return 0; } if(argc !=1) { printf("Usage : %s ",argv[0]); return 0; } signal(SIGIO, fifth_testsignal);//注册一个信号,函数为fifth_testsignal fcntl(fd, F_SETOWN, getpid()); // 告诉内核,发给谁 oflags = fcntl(fd, F_GETFL); //取得当前的状态 fcntl(fd, F_SETFL, oflags | FASYNC); // 改变fasync标记,最终会调用到驱动的faync > fasync_helper:初始化/释放fasync_struct while(1) { sleep(1000); } return 0; }
3、更改按键驱动实现异步通知功能
与原先的按键驱动程序相比:
1、定义fasync_struct结构
struct fasync_struct *fifth_fasync;//定义fasync_struct结构
2、在fifth_drv_ops 结构体中增加fifth_drv_fasync异步通知处理函数
static struct file_operations fifth_drv_ops = { .owner = THIS_MODULE, .open = fifth_drv_open, .read = fifth_drv_read, .release = fifth_drv_close, .poll = fifth_drv_poll, .fasync = fifth_drv_fasync,//增加异步通知处理的函数 };
3、编写fifth_drv_fasync异步通知处理函数,这个函数会在C库函数fcntl设置FASYNC时被调用
static int fifth_drv_fasync(int fd, struct file * file, int on) { int err; printk("fansync_helper "); err = fasync_helper(fd, file, on, &fifth_fasync);//利用fasync_helper初始化fifth_fasync if (err < 0) return err; return 0; }
下面是完整的代码
#include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> #include <linux/init.h> #include <asm/io.h> //含有iomap函数iounmap函数 #include <asm/uaccess.h>//含有copy_from_user函数 #include <linux/device.h>//含有类相关的处理函数 #include <asm/arch/regs-gpio.h>//含有S3C2410_GPF0等相关的 #include <linux/irq.h> //含有IRQ_HANDLEDIRQ_TYPE_EDGE_RISING #include <asm-arm/irq.h> //含有IRQT_BOTHEDGE触发类型 #include <linux/interrupt.h> //含有request_irq、free_irq函数 #include <linux/poll.h> //#include <asm-armarch-s3c2410irqs.h> static struct class *fifth_drv_class;//类 static struct class_device *fifth_drv_class_dev;//类下面的设备 static int fifthmajor; static unsigned long *gpfcon = NULL; static unsigned long *gpfdat = NULL; static unsigned long *gpgcon = NULL; static unsigned long *gpgdat = NULL; struct fasync_struct *fifth_fasync;//定义fasync_struct结构 static unsigned int key_val; struct pin_desc { unsigned int pin; unsigned int key_val; }; static struct pin_desc pins_desc[4] = { {S3C2410_GPF0,0x01}, {S3C2410_GPF2,0x02}, {S3C2410_GPG3,0x03}, {S3C2410_GPG11,0x04} }; unsigned int ev_press; DECLARE_WAIT_QUEUE_HEAD(button_waitq);//注册一个等待队列button_waitq /* *0x01、0x02、0x03、0x04表示按键被按下 */ /* *0x81、0x82、0x83、0x84表示按键被松开 */ /* *利用dev_id的值为pins_desc来判断是哪一个按键被按下或松开 */ static irqreturn_t buttons_irq(int irq, void *dev_id) { unsigned int pin_val; struct pin_desc * pin_desc = (struct pin_desc *)dev_id;//取得哪个按键被按下的状态 pin_val = s3c2410_gpio_getpin(pin_desc->pin); if(pin_val) //按键松开 key_val = 0x80 | pin_desc->key_val; else key_val = pin_desc->key_val; wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */ ev_press = 1; kill_fasync(&fifth_fasync, SIGIO, POLL_IN);//发生信号给进程 return IRQ_HANDLED; } static int fifth_drv_open (struct inode * inode, struct file * file) { int ret; ret = request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "s1", (void * )&pins_desc[0]); if(ret) { printk("open failed 1 "); return -1; } ret = request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "s2", (void * )& pins_desc[1]); if(ret) { printk("open failed 2 "); return -1; } ret = request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "s3", (void * )&pins_desc[2]); if(ret) { printk("open failed 3 "); return -1; } ret = request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "s4", (void * )&pins_desc[3]); if(ret) { printk("open failed 4 "); return -1; } return 0; } static int fifth_drv_close(struct inode * inode, struct file * file) { free_irq(IRQ_EINT0 ,(void * )&pins_desc[0]); free_irq(IRQ_EINT2 ,(void * )& pins_desc[1]); free_irq(IRQ_EINT11 ,(void * )&pins_desc[2]); free_irq(IRQ_EINT19 ,(void * )&pins_desc[3]); return 0; } static ssize_t fifth_drv_read(struct file * file, char __user * userbuf, size_t count, loff_t * off) { int ret; if(count != 1) { printk("read error "); return -1; } // wait_event_interruptible(button_waitq, ev_press);//将当前进程放入等待队列button_waitq中 ret = copy_to_user(userbuf, &key_val, 1); ev_press = 0;//按键已经处理可以继续睡眠 if(ret) { printk("copy error "); return -1; } return 1; } static unsigned int fifth_drv_poll(struct file *file, poll_table *wait) { unsigned int ret = 0; poll_wait(file, &button_waitq, wait);//将当前进程放到button_waitq列表 if(ev_press) ret |=POLLIN;//说明有数据被取到了 return ret; } static int fifth_drv_fasync(int fd, struct file * file, int on) { int err; printk("fansync_helper "); err = fasync_helper(fd, file, on, &fifth_fasync);//利用fasync_helper初始化fifth_fasync if (err < 0) return err; return 0; } static struct file_operations fifth_drv_ops = { .owner = THIS_MODULE, .open = fifth_drv_open, .read = fifth_drv_read, .release = fifth_drv_close, .poll = fifth_drv_poll, .fasync = fifth_drv_fasync,//增加异步通知处理的函数 }; static int fifth_drv_init(void) { fifthmajor = register_chrdev(0, "buttons", &fifth_drv_ops);//注册驱动程序 if(fifthmajor < 0) printk("failes 1 buttons_drv register "); fifth_drv_class = class_create(THIS_MODULE, "buttons");//创建类 if(fifth_drv_class < 0) printk("failes 2 buttons_drv register "); fifth_drv_class_dev = class_device_create(fifth_drv_class, NULL, MKDEV(fifthmajor,0), NULL,"buttons");//创建设备节点 if(fifth_drv_class_dev < 0) printk("failes 3 buttons_drv register "); gpfcon = ioremap(0x56000050, 16);//重映射 gpfdat = gpfcon + 1; gpgcon = ioremap(0x56000060, 16);//重映射 gpgdat = gpgcon + 1; printk("register buttons_drv "); return 0; } static void fifth_drv_exit(void) { unregister_chrdev(fifthmajor,"buttons"); class_device_unregister(fifth_drv_class_dev); class_destroy(fifth_drv_class); iounmap(gpfcon); iounmap(gpgcon); printk("unregister buttons_drv "); } module_init(fifth_drv_init); module_exit(fifth_drv_exit); MODULE_LICENSE("GPL");
将测试程序与驱动程序编译后测试成功。以上就是异步通知的应用实现。