按键驱动程序(异步通知)
来源:互联网 发布:星摩小妖妖淘宝 编辑:程序博客网 时间:2024/06/03 06:43
此驱动程序之前的按键驱动程序(中断方式)上加以优化。用到异步通知。对于内核来讲,既然用户想得到的是按键后的状态,那么自然不必时时都要read状态。当它检测到中断发生变主动通知用户,用户再来读。这样,用户空间、内核就可以着手干点其它的事情,而不必忙等按键按下或释放。那么就先从应用程序上面看。
怎么设置相关联到“异步通知”呢?
flag = fcntl(fd, F_GETFL);
fcntl(fd, F_SETFL, flag|FASYNC);
这样的两句就可以将文件描述符fd与异步通知关联,到时候将会接到相关通知。
怎么知道通知道本程序呢?
fcntl(fd, F_SETOWN, getpid());
将其与进程号关联,这样内核就知道要将通知发往本进程。
得到通知后该怎么做呢?
sighandler_t signal(int signum, sighandler_t handler);
后面的handler是接到信号后的处理函数,而对于一般的IO操作,signum通常为SIGIO,这点在内核驱动中要保持一致。handler的定义为
typedef void (*sighandler_t)(int); 在这个函数体内就可以去read数据了。
驱动程序上面又是如何实现的呢?
需要在file_operations结构体里设置.fasync = xxx_fasync,并定义xxx_fasync函数.
static int xxx_fasync(int fd, struct file *filp, int on)
当fcntl(fd, F_SETFL, flag|FASYNC);设置异步通知标志后,就会调用驱动里的xxx_fasync函数,我们需要在驱动程序里先定义一个fasync_struct结构体指针,然后通过xxx_fasync函数再调用int fasync_helper(int fd, struct file * filp, int on, struct fasync_struct **fapp)去初始化这个结构体。以便在中断服务程序里发送信号。当产生按键中断时,再不是像poll机制那样唤醒等待队列,而是用kill_fasync(struct fasync_struct **fp, int sig, int band)发送信号。应用程序收到信号后会调用自定义的handler做相关处理(本程序是read内核的按键状态)。
详细的驱动程序为:
#include <linux/module.h>#include <linux/kernel.h>#include <linux/fs.h>#include <linux/init.h>#include <linux/delay.h>#include <linux/irq.h>#include <asm/uaccess.h>#include <linux/cdev.h>#include <linux/interrupt.h>#include <linux/device.h>#include <linux/sched.h>#include <linux/gpio.h>#include <linux/poll.h>#define FIFTHDEV MKDEV(250, 0)struct cdev fifthdrv_cdev;static struct class *fifthdrv_class;struct button { int irq; char *name; int pin; int val;};//static volatile int pressed = 0;static unsigned char key_val;struct fasync_struct *button_async;//DECLARE_WAIT_QUEUE_HEAD(button_wqh);/* 六个按键的相关定义整合到结构体 */static struct button buttons[6] = { {IRQ_EINT8, "K1", S3C2410_GPG(0), 0x1}, {IRQ_EINT11, "K2", S3C2410_GPG(3), 0x2}, {IRQ_EINT13, "K3", S3C2410_GPG(5), 0x3}, {IRQ_EINT14, "K4", S3C2410_GPG(6), 0x4}, {IRQ_EINT15, "K5", S3C2410_GPG(7), 0x5}, {IRQ_EINT19, "K6", S3C2410_GPG(11),0x6},};/* 中断处理函数 */static irqreturn_t fifthdrv_intr(int irq, void *data){ struct button *buttonp; int val; buttonp = (struct button*)data; val = s3c2410_gpio_getpin(buttonp->pin); if (!val) {/* 按下按键*/ key_val = buttonp->val; } else { /* 释放按键*/ key_val = buttonp->val | 0x10; //将第4位置1,做标记 } //pressed = 1; //此处改变按下标志,以使队列不继续睡眠 //wake_up_interruptible(&button_wqh); kill_fasync(&button_async, SIGIO, POLL_IN); return IRQ_RETVAL(IRQ_HANDLED);}static int fifthdrv_open(struct inode * inode, struct file * file){ int i=6; while(i--){ request_irq(buttons[i].irq, &fifthdrv_intr, IRQ_TYPE_EDGE_BOTH, buttons[i].name, &buttons[i]); } return 0; }static ssize_t fifthdrv_read(struct file *file, char __user *user, size_t size,loff_t*o){ int sz = sizeof(key_val) ; if (sz != size) { return -EINVAL; } /* 使用异步通知,此处不必休眠 */ //wait_event_interruptible(button_wqh, pressed); copy_to_user(user, &key_val, sz); /* 重新清除按下标志 */ //pressed = 0; return sz;}static int fifthdrv_close(struct inode *inode, struct file *file){ int i=6; while(i--) { free_irq(buttons[i].irq, &buttons[i]); } return 0;}/*static unsigned int fifthdrv_poll(struct file *file, poll_table *wait){ unsigned int res=0; poll_wait(file, &button_wqh, wait); if (pressed) { res |= POLLIN | POLLRDNORM; } return res;}*/static int fifthdrv_fasync(int fd, struct file *filp, int on){ return fasync_helper(fd, filp, on, &button_async);}static struct file_operations fifthdrv_ops = { .owner = THIS_MODULE, .open = fifthdrv_open, .read = fifthdrv_read, //.poll = fifthdrv_poll, .release = fifthdrv_close, .fasync = fifthdrv_fasync,};static int fifthdrv_init(void){ int ret; int devt = FIFTHDEV; ret = register_chrdev_region(devt, 1, "fifthdrv"); if (ret) { printk(KERN_ERR "Unable to register minors for fifthdrv\n"); goto fail; } fifthdrv_class = class_create(THIS_MODULE, "fifthdrv_class"); if (IS_ERR(fifthdrv_class)) { printk(KERN_ERR "can't register device class\n"); return PTR_ERR(fifthdrv_class); } device_create(fifthdrv_class, NULL, devt, NULL, "buttons"); cdev_init(&fifthdrv_cdev, &fifthdrv_ops); ret = cdev_add(&fifthdrv_cdev, devt, 1); if (ret < 0) goto fail_cdev; return 0; fail_cdev: class_unregister(fifthdrv_class); device_destroy(fifthdrv_class, devt); cdev_del(&fifthdrv_cdev);fail: unregister_chrdev_region(devt, 1); return 0;}static void fifthdrv_exit(void){ class_unregister(fifthdrv_class); device_destroy(fifthdrv_class, FIFTHDEV); cdev_del(&fifthdrv_cdev); unregister_chrdev_region(FIFTHDEV, 1);}module_init(fifthdrv_init);module_exit(fifthdrv_exit);MODULE_LICENSE("GPL");
应用程序为:
- #include <stdio.h>
- #include <signal.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/types.h>
-
- int fd;
- void signal_f(int signum)
- {
- unsigned char val;
- static unsigned int cnt = 1;
- read(fd, &val, sizeof(val));
- printf("cnt:%d, val:0x%x\n", cnt++, val);
- }
- int main(int argc, char *argv[])
- {
- int flag;
-
- fd = open("/dev/buttons", O_RDWR);
- if (fd < 0)
- {
- printf("can't open!\n");
- }
- signal(SIGIO, signal_f);
- fcntl(fd, F_SETOWN, getpid());
- flag = fcntl(fd, F_GETFL);
- fcntl(fd, F_SETFL, flag|FASYNC);
- while (1)
- {
- sleep(1000);
- }
- return 0;
- }
运行状态:
本文转自:http://blog.chinaunix.net/uid-22609852-id-3153120.html
- 按键驱动程序(异步通知)
- 异步通知的按键驱动程序
- TQ2440 按键驱动程序 异步通知(发送信号)
- linux驱动程序之增加按键异步通知机制
- 【Linux设备驱动程序(第三版)】----异步通知
- Linux设备驱动程序学习(6)-高级字符驱动程序操作[(4)异步通知fasync]
- 按键驱动程序(1)
- tiny6410_按键中断驱动_异步通知
- 按键驱动的恩恩怨怨之异步通知
- 按键驱动深化-异步通知机制
- tiny6410按键驱动(六)---异步通知
- 字符设备实验之按键异步通知
- 嵌入式linux之按键驱动,异步通知
- 基于异步通知机制的按键驱动
- linux 驱动程序中异步通知机制
- 驱动程序异步通知应用程序,数据是否可读
- 异步通知《来自Linux驱动程序开发实例》
- 字符设备驱动程序——异步通知
- CCNP路由实验之十二 MPLS
- 笔试面试题解备忘1:字符串转换成整数
- java读写Properties文件中文问题
- 大数据与医学的艺术
- bug:error:too few arguments to function ‘fopen’
- 按键驱动程序(异步通知)
- 2014php最受欢迎的框架
- QTP的基本功能介绍
- (六)SSO之CAS框架扩展 修改CAS源码实现与ESS动态密码验证对接
- HttpServlet详解
- ubuntu14.04 64位安装arm-linux-gcc 4.3.2的注意点
- 多线程断点下载-handler-thread-HttpURLConnection
- 激励任何人都管用的八种方法
- mciSendCommand函数实例