按键驱动深化-异步通知机制

来源:互联网 发布:资金流向分析软件 编辑:程序博客网 时间:2024/05/16 11:46

在以前的实验中获得按键值的方法有:

查询方式:此类方式耗CPU资源高达98%

中断方式:应用调用read后进而调用驱动的drv_read,在执行wait_event_interruptible(buttons_waitq, condition)时休眠,即read阻塞了,若有中断发生,进入中断模式,最后中中断处理中唤醒buttons_waitq,此时drv_read的wait_event_interruptible(buttons_waitq, condition)返回,接着copy_to_user命令将键值复制给应用程序的缓区keys_val,condition = 0,然后drv_read返回并带回返回值1,然后可打印键值和返回值

对于②的方式,如果一直没有按键中断发生的话,应用的read一直阻塞,导致在驱动drv_read代码上休眠,只有当按键中断发生,中断唤醒,drv_read代码继续执行,并返回键值给应用,然后调用下次read阻塞并在对应的驱动代码上休眠

poll 机制:平时poll阻塞并休眠,一到超时时间,do_poll中的代码schedule_timeout(__timeout)由定时器唤醒,schedule_timeout(__timeout)返回,然后从执行schedule_timeout(__timeout)后面的代码,直到if (count || !*timeout || signal_pending(current))第二个条件为真,就跳出for(;;;)死循环,并一层层带回返回值0,使得poll的返回值为0,根据这个返回值打印超时

若在休眠时发生了中断,中断处理wake_up_interruptible会唤醒do_poll中的代码schedule_timeout(__timeout),schedule_timeout(__timeout)返回后,一步步到do_pollfd调用驱动的poll函数,驱动poll将下次唤醒的进程挂到队列中,然后返回值mask,在do_pollfd对mask进行运算,使得mask=1在返回,然后判断这个返回值导致count++,从而导致if (count || !*timeout || signal_pending(current))第一个条件为真,跳出死循环,然后一步步将返回值1返回给应用poll,应用根据poll的返回值1表明有按键中断发生了,进而调用read函数去读取键值,读取完后打印键值,然后第二次调用poll阻塞并休眠,这个休眠是在代码do_poll中的代码schedule_timeout(__timeout)处,因为,poll的返回值能明确表示有否按键发生,平时poll又是阻塞并休眠,所以一旦poll的返回值为1表示有中断发生了,这时只要读取键值即可,不需要像方式②样read阻塞并休眠,所以 应将②

的驱动wait_event_interruptible行去掉,当然也可以不去,因为这时condition =1,read也不会休眠

所以方式是read阻塞并导致drv_read上的代码休眠,而poll方式是poll阻塞并导致do_poll 代码schedule_timeout(__timeout)休眠,并根据poll返回值,直接read或打印超时,其read并不阻塞和休眠,相比,poll一段时间没有按键,也返回

不过3种都是应用主动去读取按键值,若没有则阻塞并休眠,现在介绍一种异步方式,平时应用做别的事情(应用不使用read,poll阻塞并休眠),有按键产生,驱动提醒应用(比如通过kill对应用发信号SIGUSR1给应用),然后应用收到后在相应的信号处理函数中去读取按键值,然后应用又继续干别的事情

现在先手工用kill -USR1 PID的方式来介绍下异步通知机制的要点:

现有源码test.c:

#include <stdio.h>#include <signal.h>void my_signal_func(int signum){static int cnt = 0;/*这里的换行符不能省,否则给此进程发信号,会没有这行打印,和printf行缓冲有关*/printf("signum = %d, times = %d\n", signum, ++cnt); }int main(int argc, char *argv[]){signal(SIGUSR1, my_signal_func);while(1){sleep(1000);}return 0;}
其中test.c像中断注册函数一样注册了个信号注册函数,并定义了发生相应信号后调用的信号处理函数

arm-linux-gcc -o test test.c

 cp test /work/nfs_root/xyc_first_fs/

./test &

ps  //查看./test进程的PID,假设PID = 889

kill -USR1 889

signum = 10, times = 1//查看给应用使用kill发信号,应用是否收到并调用相关的信号处理函数没有,这个打印显然已经收到了信号并处理了

signum = 10, times =2


所以对于驱动中的异步通知机制要点是

①注册相应信号处理函数:应用完成

②谁发信号:驱动

③怎么发:kill_fasync函数

④发给谁:发给应用,所以应用应该主动告诉内核应用的PID


firth.drv.c

/****************************************************************filename: firth_drv.c*description:通过异步机制获取按键值*author:    xyc*create time:2014/6/6*version:  1*modify info:****************************************************************/#include <linux/module.h>#include <linux/kernel.h>#include <linux/fs.h>#include <linux/init.h>#include <linux/delay.h>#include <linux/irqreturn.h>#include <linux/irq.h>#include <linux/wait.h>#include <asm/irq.h>#include <asm/arch/regs-gpio.h>#include <asm/hardware.h>#include <asm-arm/io.h>#include <asm-arm/uaccess.h>static struct class *firthdrv_class;static struct class_device*firthdrv_class_dev;static struct fasync_struct *button_q;static unsigned char key_val;static DECLARE_WAIT_QUEUE_HEAD(buttons_waitq); struct pin_desc {unsigned int pin;unsigned int key_val;};/* *按下:0x01, 0x02 *松开:0x81, 0x82 */static struct pin_desc pins_desc[2] = {{S3C2410_GPF0,0x01},{S3C2410_GPF2,0x02},};static volatile unsigned int condition= 0;static irqreturn_t buttons_irq(int irq, void *dev_id){struct pin_desc * pindesc= (struct pin_desc * )dev_id;unsigned int pinval;/*读取引脚值*/pinval = s3c2410_gpio_getpin(pindesc->pin);/*确定键值*/if(pinval){/*松开*/key_val = 0x80 | pindesc->key_val ;//printk("kernel key_val = 0x%x\n", key_val);}else{/*按下*/key_val = pindesc->key_val ;//printk("kernel key_val = 0x%x\n", key_val);}#if 0/*按键按下或松开中断发生将condition = 1使得满足wait_event_interruptible唤醒的条件*/condition = 1;/*唤醒休眠的进程./firth_drv_test */wake_up_interruptible(&buttons_waitq);#endif/*驱动用kill_fasync发信号,button_q结构体由fasync调用驱动firth_drv_fasync *fasync_helper初始化*/kill_fasync(&button_q, SIGIO, POLL_IN);return IRQ_HANDLED;}static int firth_drv_open(struct inode *inode, struct file *file){request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "s2", &pins_desc[0]);request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "s3", &pins_desc[1]);return 0;}static ssize_t firth_drv_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos){if(nbytes != 1)return EINVAL;/*没有按键按下时,./firth_drv_test 进程休眠,*///wait_event_interruptible(buttons_waitq, condition);/*有按键按下或松开时,结束休眠,并将键值传给应用程序*/copy_to_user(buf, &key_val, 1);/*当下一次应用调用read时,condition = 0使得./firth_drv_test 进程休眠*///condition = 0;return 1;  /* 这里是5,应用read返回值=5 */}static int firth_drv_release(struct inode * inode, struct file * file){free_irq(IRQ_EINT0, &pins_desc[0]);free_irq(IRQ_EINT2, &pins_desc[1]);return 0;}static int firth_drv_fasync(int fd, struct file *file, int on){int result;result = fasync_helper(fd, file, on, &button_q);return (result);}static const struct file_operations firth_drv_fops = {.owner= THIS_MODULE,.read= firth_drv_read,.open= firth_drv_open,.release= firth_drv_release,.fasync  = firth_drv_fasync,};int major;static int __init firth_drv_init(void){major = register_chrdev(0, "firth_drv", &firth_drv_fops);firthdrv_class = class_create(THIS_MODULE, "firth_drv");    firthdrv_class_dev = class_device_create(firthdrv_class, NULL, MKDEV(major, 0), NULL, "buttons");return 0;}static void __exit firth_drv_exit(void){    /* 卸载驱动程序 */    unregister_chrdev(major, "firth_drv");    class_device_unregister(firthdrv_class_dev);    class_destroy(firthdrv_class);}/* 这两行指定驱动程序的初始化函数和卸载函数 */module_init(firth_drv_init);module_exit(firth_drv_exit);MODULE_LICENSE("GPL");

测试代码firthdrvtest.c

#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdio.h>#include <unistd.h>#include <signal.h>/*firthdrvtest   */int fd;void my_signal_func(int signum){unsigned char key_val;printf("signum = %d\n", signum);read(fd, &key_val, 1);printf("key_val = 0x%x\n", key_val);}int main(int argc, char **argv){int O_FLAGS;signal( SIGIO, my_signal_func); /*安装信号处理函数*/fd = open("/dev/buttons", O_RDWR);if (fd < 0){printf("can't open!\n");}fcntl( fd, F_SETOWN, getpid());O_FLAGS = fcntl(fd, F_GETFL);fcntl(fd, F_SETFL, O_FLAGS|O_ASYNC);while (1){sleep(1000);}return 0;}

测试看按键按下或松开时,信号处理函数是否打印信号值,键值是否打印,若打印则 驱动发异步信号给应用,应用接收到了,并调用相应的信号处理函数,处理了,

0 0
原创粉丝点击