Chinaunix首页 | 论坛 | 博客
  • 博客访问: 473333
  • 博文数量: 59
  • 博客积分: 345
  • 博客等级: 二等列兵
  • 技术积分: 1380
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-18 22:44
个人简介

to be myself

文章分类

全部博文(59)

文章存档

2017年(5)

2013年(47)

2012年(3)

2011年(4)

分类: LINUX

2012-03-27 21:42:34

按键驱动程序(poll机制)
此驱动程序可以在按键驱动程序(中断方式)的基础之上稍作修改,最大的改变在于file_operations结构体中加入xxx_poll函数.
poll用于监测多个等待事件,若事件未发生,进程睡眠,放弃CPU控制权,
若监测的任何一个事件发生,poll将唤醒睡眠的进程,并判断发生事件的类型,执行相应的操作。
static unsigned int xxx_poll(struct file *file, poll_table *wait);

在2.6的低版本号里会出现用户空间使用xxx函数就会调用内核sys_xxx函数,如read对应sys_read;但我用的是2.6.32的内核,
调用机制有所更改,但去搜索内核代码是,还是有迹可循的,我使用的方法尽量去搜索poll相关的代码。最终找到了“根”。
就是从SYSCALL_DEFINE3开始。下面是搜索的过程。
SYSCALL_DEFINE3(poll, struct pollfd __user *, ufds, unsigned int, nfds,
long, timeout_msecs)
│─do_sys_poll(ufds, nfds, to)
    │─poll_initwait(&table)
    │ │─init_poll_funcptr(&pwq->pt, __pollwait)
    │     │─pt->qproc = qproc,此处初始化了后面驱动程序用到poll_wait函数
    │─do_poll(nfds, head, &table, end_time)
        │─for (;;) { //一个死循环,应当有个出口
...
for (; pfd != pfd_end; pfd++) {
    if (do_pollfd(pfd, pt)) { //do_pollfd的返回值决定count的值
        count++;
        ...
    }
}
if (count || timed_out) //出口在此,当count非零或超时跳出函数
    break;
if (!poll_schedule_timeout(wait, TASK_INTERRUPTIBLE, to, slack)) //休眠
    timed_out = 1;
}
        do_pollfd(pfd, pt)
│───if (fd >= 0) {
    mask = POLLNVAL;
    if (file != NULL) {
        mask = DEFAULT_POLLMASK;
if (file->f_op && file->f_op->poll) {
    ...
    mask = file->f_op->poll(file, pwait);//此处调用了我们所写驱动程序的poll函数,下面介绍
}
...
    }
}
...
return mask;
驱动程序的poll函数
static unsigned int forthdrv_poll(struct file *file, poll_table *wait)
{
    unsigned int res=0;
    poll_wait(file, &button_wqh, wait);
    if (pressed) {
        res |= POLLIN | POLLRDNORM;
    }
    return res;
}
poll_wait(file, &button_wqh, wait);
<=>
static inline void poll_wait(struct file * filp, wait_queue_head_t * wait_address, poll_table *p)
│─if (p && wait_address)
p->qproc(filp, wait_address, p); //在前面的init_poll_funcptr函数已经初始化,即
p->qproc调用static void __pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *p)
│─add_wait_queue(wait_address, &entry->wait);
就是将button_wqh添加到等待队列。poll如何得到的通知呢?
中断程序修改全局按键状态pressed,唤醒等待队列,并将事件通知存到res以mask由do_pollfd返回,决定是否增加count退出for(;;)循环。

详细驱动程序如下:

点击(此处)折叠或打开

  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/fs.h>
  4. #include <linux/init.h>
  5. #include <linux/delay.h>
  6. #include <linux/irq.h>
  7. #include <asm/uaccess.h>
  8. #include <linux/cdev.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/device.h>
  11. #include <linux/sched.h>
  12. #include <linux/gpio.h>
  13. #include <linux/poll.h>

  14. #define FORTHDEV MKDEV(250, 0)
  15. struct cdev forthdrv_cdev;

  16. static struct class forthdrv_class = {
  17.     .name = "forthdrv",
  18.     .owner =    THIS_MODULE,
  19. };

  20. struct button {
  21.     int irq;
  22.     char *name;
  23.     int pin;
  24.     int val;
  25. };
  26. static volatile int pressed = 0;
  27. static unsigned char key_val;

  28. DECLARE_WAIT_QUEUE_HEAD(button_wqh);

  29. /* 六个按键的相关定义整合到结构体 */
  30. static struct button buttons[6] = {
  31.         {IRQ_EINT8, "K1", S3C2410_GPG(0), 0x1},
  32.         {IRQ_EINT11, "K2", S3C2410_GPG(3), 0x2},
  33.         {IRQ_EINT13, "K3", S3C2410_GPG(5), 0x3},
  34.         {IRQ_EINT14, "K4", S3C2410_GPG(6), 0x4},
  35.         {IRQ_EINT15, "K5", S3C2410_GPG(7), 0x5},
  36.         {IRQ_EINT19, "K6", S3C2410_GPG(11),0x6},
  37. };

  38. /* 中断处理函数 */
  39. static irqreturn_t forthdrv_intr(int irq, void *data)
  40. {
  41.     struct button *buttonp;
  42.     int val;
  43.     
  44.     buttonp = (struct button*)data;

  45.     val = s3c2410_gpio_getpin(buttonp->pin);

  46.     
  47.     if (!val) {/* 按下按键*/
  48.         key_val = buttonp->val;    
  49.     } else { /* 释放按键*/
  50.         key_val = buttonp->val | 0x10; //将第4位置1,做标记
  51.     }

  52.     pressed = 1; //此处改变按下标志,以使队列不继续睡眠
  53.     wake_up_interruptible(&button_wqh);
  54.     
  55.     return IRQ_RETVAL(IRQ_HANDLED);
  56. }

  57. static int forthdrv_open(struct inode * inode, struct file * file)
  58. {
  59.     int i=6;
  60.     while(i--){
  61.         request_irq(buttons[i].irq, &forthdrv_intr, IRQ_TYPE_EDGE_BOTH,
  62.                     buttons[i].name, &buttons[i]);
  63.     }
  64.     return 0;
  65.     
  66. }

  67. static ssize_t forthdrv_read(struct file *file, char __user *user, size_t size,loff_t*o)
  68. {
  69.     int sz = sizeof(key_val) ;
  70.     
  71.     if (sz != size) {
  72.         return -EINVAL;
  73.     }
  74.     
  75.     /* 使用poll机制,此处不必休眠 */
  76.     //wait_event_interruptible(button_wqh, pressed);
  77.     
  78.     copy_to_user(user, &key_val, sz);

  79.     /* 重新清除按下标志 */
  80.     pressed = 0;

  81.     return sz;
  82. }

  83. static int forthdrv_close(struct inode *inode, struct file *file)
  84. {
  85.     int i=6;
  86.     
  87.     while(i--) {
  88.         free_irq(buttons[i].irq, &buttons[i]);
  89.     }
  90.     
  91.     return 0;
  92. }

  93. static unsigned int forthdrv_poll(struct file *file, poll_table *wait)
  94. {
  95.     unsigned int res=0;
  96.     
  97.     poll_wait(file, &button_wqh, wait);

  98.     if (pressed) {
  99.         res |= POLLIN | POLLRDNORM;
  100.     }
  101.     return res;
  102. }

  103. static struct file_operations forthdrv_ops = {
  104.     .owner = THIS_MODULE,
  105.     .open = forthdrv_open,
  106.     .read = forthdrv_read,
  107.     .poll = forthdrv_poll,
  108.     .release = forthdrv_close,
  109.     
  110. };
  111. static int forthdrv_init(void)
  112. {
  113.     int ret;
  114.     int devt = FORTHDEV;
  115.     
  116.     ret = register_chrdev_region(devt, 1, "forthdrv");
  117.     if (ret) {
  118.         printk(KERN_ERR "Unable to register minors for forthdrv\n");
  119.         goto fail;
  120.     }
  121.     class_register(&forthdrv_class);
  122.     device_create(&forthdrv_class, NULL, devt, NULL, "buttons");
  123.     cdev_init(&forthdrv_cdev, &forthdrv_ops);
  124.     ret = cdev_add(&forthdrv_cdev, devt, 1);
  125.     if (ret < 0)
  126.         goto fail_cdev;
  127.     return 0;
  128.     
  129. fail_cdev:
  130.     class_unregister(&forthdrv_class);
  131.     device_destroy(&forthdrv_class, devt);
  132.     cdev_del(&forthdrv_cdev);
  133. fail:
  134.     unregister_chrdev_region(devt, 1);
  135.     
  136.     return 0;
  137. }

  138. static void forthdrv_exit(void)
  139. {
  140.     class_unregister(&forthdrv_class);
  141.     device_destroy(&forthdrv_class, FORTHDEV);
  142.     cdev_del(&forthdrv_cdev);
  143.     unregister_chrdev_region(FORTHDEV, 1);
  144. }

  145. module_init(forthdrv_init);
  146. module_exit(forthdrv_exit);
  147. MODULE_LICENSE("GPL");

这样在应用程序上,就不用一直read,在一定的时间内如果没有得到相应的事件通知,就返回。
应用程序会用到
#include

int poll(struct pollfd *fds, nfds_t nfds, int timeout);

struct pollfd {
               int   fd;         /* file descriptor */
               short events;     /* requested events */
               short revents;    /* returned events */
           };

应用程序如下:

点击(此处)折叠或打开

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <stdio.h>
  5. #include <poll.h>

  6. int main(int argc, char *argv[])
  7. {
  8.     int fd, ret, cnt=1;
  9.     unsigned char val;
  10.     struct pollfd fds[1];

  11.     fd = open("/dev/buttons", O_RDWR);
  12.     if (fd < 0)
  13.     {
  14.         printf("Can not open device\n");
  15.     }

  16.     fds[0].fd = fd; //监听的文件描述符
  17.     fds[0].events = POLLIN; //有数据可读标志,与驱动保持一致


  18.     while(1)
  19.     {
  20.         ret = poll(fds, 1, 5000); //5000毫秒超时

  21.         if (== ret)
  22.         {
  23.             printf("A value of 0 indicates that the call timed out and no file descriptors were ready.\n");
  24.         }
  25.         else
  26.         {
  27.             read(fd, &val, 1);
  28.             printf("cnt:%d, val:0x%x\n", cnt++, val);
  29.         }
  30.     }
  31.     return 0;
  32. }

运行情况:
阅读(3460) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~