按键驱动程序(异步通知)

 此驱动程序之前的按键驱动程序(中断方式)上加以优化。用到异步通知。对于内核来讲,既然用户想得到的是按键后的状态,那么自然不必时时都要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");

应用程序为:

  1. #include <stdio.h>
  2. #include <signal.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <sys/types.h>
  6.  
  7. int fd;

  8. void signal_f(int signum)
  9. {
  10.     unsigned char val;
  11.     static unsigned int cnt = 1;
  12.     read(fd, &val, sizeof(val));
  13.     printf("cnt:%d, val:0x%x\n", cnt++, val);
  14. }

  15. int main(int argc, char *argv[]) 
  16. {
  17.     int flag;
  18.     
  19.     fd = open("/dev/buttons", O_RDWR);
  20.     if (fd < 0)
  21.     {
  22.         printf("can't open!\n");
  23.     }
  24.     signal(SIGIO, signal_f);
  25.     fcntl(fd, F_SETOWN, getpid());
  26.     flag = fcntl(fd, F_GETFL);
  27.     fcntl(fd, F_SETFL, flag|FASYNC);
  28.     while (1)
  29.     {
  30.         sleep(1000);
  31.     }
  32.     return 0;
  33. }

运行状态:




本文转自:http://blog.chinaunix.net/uid-22609852-id-3153120.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值