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

to be myself

文章分类

全部博文(59)

文章存档

2017年(5)

2013年(47)

2012年(3)

2011年(4)

分类: LINUX

2012-03-29 13:32:20

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

详细的驱动程序为:

点击(此处)折叠或打开

  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 FIFTHDEV MKDEV(250, 0)
  15. struct cdev fifthdrv_cdev;

  16. static struct class *fifthdrv_class;

  17. struct button {
  18.     int irq;
  19.     char *name;
  20.     int pin;
  21.     int val;
  22. };
  23. //static volatile int pressed = 0;
  24. static unsigned char key_val;

  25. struct fasync_struct *button_async;

  26. //DECLARE_WAIT_QUEUE_HEAD(button_wqh);

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

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

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

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

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

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

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

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

  80.     return sz;
  81. }

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

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

  97.     if (pressed) {
  98.         res |= POLLIN | POLLRDNORM;
  99.     }
  100.     return res;
  101. }
  102. */
  103. static int fifthdrv_fasync(int fd, struct file *filp, int on)
  104. {
  105.     return fasync_helper(fd, filp, on, &button_async);
  106. }


  107. static struct file_operations fifthdrv_ops = {
  108.     .owner = THIS_MODULE,
  109.     .open = fifthdrv_open,
  110.     .read = fifthdrv_read,
  111.     //.poll = fifthdrv_poll,
  112.     .release = fifthdrv_close,
  113.     .fasync = fifthdrv_fasync,
  114. };
  115. static int fifthdrv_init(void)
  116. {
  117.     int ret;
  118.     int devt = FIFTHDEV;
  119.     
  120.     ret = register_chrdev_region(devt, 1, "fifthdrv");
  121.     if (ret) {
  122.         printk(KERN_ERR "Unable to register minors for fifthdrv\n");
  123.         goto fail;
  124.     }
  125.     fifthdrv_class = class_create(THIS_MODULE, "fifthdrv_class");
  126.     if (IS_ERR(fifthdrv_class)) {
  127.         printk(KERN_ERR "can't register device class\n");
  128.         return PTR_ERR(fifthdrv_class);
  129.     }
  130.     device_create(fifthdrv_class, NULL, devt, NULL, "buttons");
  131.     cdev_init(&fifthdrv_cdev, &fifthdrv_ops);
  132.     ret = cdev_add(&fifthdrv_cdev, devt, 1);
  133.     if (ret < 0)
  134.         goto fail_cdev;
  135.     return 0;
  136.     
  137. fail_cdev:
  138.     class_unregister(fifthdrv_class);
  139.     device_destroy(fifthdrv_class, devt);
  140.     cdev_del(&fifthdrv_cdev);
  141. fail:
  142.     unregister_chrdev_region(devt, 1);
  143.     
  144.     return 0;
  145. }

  146. static void fifthdrv_exit(void)
  147. {
  148.     class_unregister(fifthdrv_class);
  149.     device_destroy(fifthdrv_class, FIFTHDEV);
  150.     cdev_del(&fifthdrv_cdev);
  151.     unregister_chrdev_region(FIFTHDEV, 1);
  152. }

  153. module_init(fifthdrv_init);
  154. module_exit(fifthdrv_exit);
  155. 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. }

运行状态:

文章说明:
此博文只为总结分享学习经验,许多内容不能也并未详细描述,如每个函数的参数、返回值,何为内核空间、用户空间,怎么操作寄存器, Makefile的编写,编译应用程序等等,这些知识点都在相关资料中能得到更专业、详细的解答。

阅读(4251) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~