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

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

### 异步通知机制的实现方法 在Linux设备驱动中,异步通知是一种高效的事件处理机制。它允许驱动程序在特定条件满足时主动通知用户空间的应用程序,从而避免了轮询和长时间阻塞的需求。 #### 1. 基本概念 异步通知类似于硬件中断的概念,但它是在软件层面上模拟的。当设备发生某些事件(例如数据可读、按键按下等)时,驱动程序会向用户空间发送一个信号(如SIGIO),通知应用程序立即处理这些事件[^3]。 #### 2. 关键组件 - **信号**:这是从内核到用户空间进程的一种通信方式。 - **fasync_struct结构体**:用于管理异步通知的相关信息。 - **FASYNC标志**:当应用程序通过`fcntl(fd, F_SETFL, flags | FASYNC)`改变文件状态标志时,触发驱动中的fasync函数执行[^5]。 #### 3. 实现步骤 ##### 3.1 定义并初始化fasync_struct结构体 在驱动程序中需要定义一个`fasync_struct`类型的指针,并对其进行初始化。 ```c struct fasync_struct *async_queue; // 异步通知队列 ``` ##### 3.2 实现fasync函数 在`file_operations`操作集中实现`fasync`函数,该函数负责维护异步通知队列。 ```c static int my_driver_fasync(int fd, struct file *filp, int mode) { return fasync_helper(fd, filp, mode, &async_queue); } ``` ##### 3.3 发送信号给用户空间 当驱动检测到某个事件发生时,使用`kill_fasync`函数发送信号给用户空间的应用程序。 ```c void send_signal_to_user(void) { if (async_queue) { kill_fasync(&async_queue, SIGIO, POLL_IN); } } ``` ##### 3.4 用户空间配置 用户空间的应用程序需要设置FASYNC标志,并注册SIGIO信号的处理函数。 ```c #include <signal.h> #include <fcntl.h> void signal_handler(int signum) { printf("Received signal %d\n", signum); } int main() { int fd = open("/dev/my_device", O_RDWR); if (fd < 0) { perror("Failed to open the device"); return -1; } // 设置异步通知 fcntl(fd, F_SETOWN, getpid()); int oflags = fcntl(fd, F_GETFL); fcntl(fd, F_SETFL, oflags | FASYNC); // 注册信号处理函数 signal(SIGIO, signal_handler); while(1) { sleep(1); // 等待信号 } close(fd); return 0; } ``` #### 4. 注意事项 - 在多线程环境中,确保对异步通知队列的操作是原子的。 - 当关闭设备文件描述符时,应清理相关的异步通知队列以防止内存泄漏。 - 应用程序在退出前应当取消FASYNC标志,以便释放相关资源。 ### 总结 异步通知机制提供了一种高效的方式来处理设备驱动中的事件通知问题。通过这种方式,可以显著减少CPU占用率并提高系统的响应速度。在实际开发过程中,开发者可以根据具体需求选择是否采用这种机制,并结合其他技术(如阻塞/非阻塞I/O、poll/select等)来优化系统性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值