Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2691438
  • 博文数量: 505
  • 博客积分: 1552
  • 博客等级: 上尉
  • 技术积分: 2514
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-23 18:24
文章分类

全部博文(505)

文章存档

2019年(12)

2018年(15)

2017年(1)

2016年(17)

2015年(14)

2014年(93)

2013年(233)

2012年(108)

2011年(1)

2009年(11)

分类:

2012-08-29 23:06:58

原文地址:Linux驱动学习笔记(5) 作者:jiaweijing

异步通知
前面三种检测按键的方式:查询(极度耗资源)、中断(读时会休眠,read(...)在没有检测到按键时,不会返回)、poll(在中断的基础上给定一个超时时间,时间到了,即使没有按键按下也会返回)。这三种方法都有一个共同点就是都需要应用程序主动去读。
异步通知可以在驱动程序里面检测,能过信号触发应用程序。
进程间发信号:
1,先注册一个信号处理函数;
2,谁发;
3,发给谁;
4,怎么发。

目标:按键按下时,驱动程序通知应用程序。
1,应用程序里需要注册信号处理函数
2,谁发,驱动程序发
3,发给谁,应用程序 (应用程序要告诉内核他的PID号)
4,怎么发,调用kill_fasync

点击(此处)折叠或打开

  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 <asm/irq.h>
  9. #include <asm/io.h>
  10. #include <asm/arch/regs-gpio.h>
  11. #include <asm/hardware.h>
  12. #include <linux/poll.h>


  13. static struct class *fifthdrv_class;
  14. static struct class_device    *fifthdrv_class_dev;

  15. volatile unsigned long *gpfcon;
  16. volatile unsigned long *gpfdat;

  17. volatile unsigned long *gpgcon;
  18. volatile unsigned long *gpgdat;


  19. static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

  20. /* 中断事件标志, 中断服务程序将它置1,fifth_drv_read将它清0 */
  21. static volatile int ev_press = 0;

  22. static struct fasync_struct *button_async;


  23. struct pin_desc{
  24.     unsigned int pin;
  25.     unsigned int key_val;
  26. };


  27. /* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
  28. /* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
  29. static unsigned char key_val;

  30. struct pin_desc pins_desc[4] = {
  31.     {S3C2410_GPF0, 0x01},
  32.     {S3C2410_GPF2, 0x02},
  33.     {S3C2410_GPG3, 0x03},
  34.     {S3C2410_GPG11, 0x04},
  35. };


  36. /*
  37.   * 确定按键值
  38.   */
  39. static irqreturn_t buttons_irq(int irq, void *dev_id)
  40. {
  41.     struct pin_desc * pindesc = (struct pin_desc *)dev_id;
  42.     unsigned int pinval;
  43.     
  44.     pinval = s3c2410_gpio_getpin(pindesc->pin);

  45.     if (pinval)
  46.     {
  47.         /* 松开 */
  48.         key_val = 0x80 | pindesc->key_val;
  49.     }
  50.     else
  51.     {
  52.         /* 按下 */
  53.         key_val = pindesc->key_val;
  54.     }

  55.     ev_press = 1; /* 表示中断发生了 */
  56.     wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */
  57.     
  58.     kill_fasync (&button_async, SIGIO, POLL_IN);    // 发送信号(发生中断后会通过kill_fasync发送信号到应用程序 SIGIO要发送的信号,POLL_IN表示有信号等待读取。
  59.     
  60.     return IRQ_RETVAL(IRQ_HANDLED);
  61. }

  62. static int fifth_drv_open(struct inode *inode, struct file *file)
  63. {
  64.     /* 配置GPF0,2为输入引脚 */
  65.     /* 配置GPG3,11为输入引脚 */
  66.     request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[0]);
  67.     request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "S3", &pins_desc[1]);
  68.     request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "S4", &pins_desc[2]);
  69.     request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "S5", &pins_desc[3]);    

  70.     return 0;
  71. }

  72. ssize_t fifth_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
  73. {
  74.     if (size != 1)
  75.         return -EINVAL;

  76.     /* 如果没有按键动作, 休眠 */
  77.     wait_event_interruptible(button_waitq, ev_press);

  78.     /* 如果有按键动作, 返回键值 */
  79.     copy_to_user(buf, &key_val, 1);
  80.     ev_press = 0;
  81.     
  82.     return 1;
  83. }


  84. int fifth_drv_close(struct inode *inode, struct file *file)
  85. {
  86.     free_irq(IRQ_EINT0, &pins_desc[0]);
  87.     free_irq(IRQ_EINT2, &pins_desc[1]);
  88.     free_irq(IRQ_EINT11, &pins_desc[2]);
  89.     free_irq(IRQ_EINT19, &pins_desc[3]);
  90.     return 0;
  91. }

  92. static unsigned fifth_drv_poll(struct file *file, poll_table *wait)
  93. {
  94.     unsigned int mask = 0;
  95.     poll_wait(file, &button_waitq, wait); // 不会立即休眠

  96.     if (ev_press)
  97.         mask |= POLLIN | POLLRDNORM;

  98.     return mask;
  99. }

  100. static int fifth_drv_fasync (int fd, struct file *filp, int on)
  101. {
  102.     printk("driver: fifth_drv_fasync\n");
  103.     return fasync_helper (fd, filp, on, &button_async);
  104. }


  105. static struct file_operations sencod_drv_fops = {
  106.     .owner = THIS_MODULE, /* 这是一个宏,指向编译模块时自动创建的__this_module变量 */
  107.     .open = fifth_drv_open,
  108.     .read     =    fifth_drv_read,    
  109.     .release = fifth_drv_close,
  110.     .poll = fifth_drv_poll,
  111.     .fasync     = fifth_drv_fasync,
  112. };


  113. int major;
  114. static int fifth_drv_init(void)
  115. {
  116.     major = register_chrdev(0, "fifth_drv", &sencod_drv_fops);

  117.     fifthdrv_class = class_create(THIS_MODULE, "fifth_drv");

  118.     fifthdrv_class_dev = class_device_create(fifthdrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/buttons */

  119.     gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
  120.     gpfdat = gpfcon + 1;

  121.     gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
  122.     gpgdat = gpgcon + 1;

  123.     return 0;
  124. }

  125. static void fifth_drv_exit(void)
  126. {
  127.     unregister_chrdev(major, "fifth_drv");
  128.     class_device_unregister(fifthdrv_class_dev);
  129.     class_destroy(fifthdrv_class);
  130.     iounmap(gpfcon);
  131.     iounmap(gpgcon);
  132.     return 0;
  133. }


  134. module_init(fifth_drv_init);

  135. module_exit(fifth_drv_exit);

  136. MODULE_LICENSE("GPL");
为了使设备支持异步通知机制,驱动程序中涉及以下3项工作:
1. 支持F_SETOWN命令,能在这个控制命令处理中设置filp->f_owner为对应进程ID。
   不过此项工作已由内核完成,设备驱动无须处理。
2. 支持F_SETFL命令的处理,每当FASYNC标志改变时,驱动程序中的fasync()函数将得以执行。
   驱动中应该实现fasync()函数。
  
3. 在设备资源可获得时,调用kill_fasync()函数激发相应的信号


应用程序:
fcntl(fd, F_SETOWN, getpid());  // 告诉内核,发给谁

Oflags = fcntl(fd, F_GETFL);  
fcntl(fd, F_SETFL, Oflags | FASYNC);  // 改变fasync标记,最终会调用到驱动的faync > fasync_helper:初始化/释放fasync_struct


点击(此处)折叠或打开

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <stdio.h>
  5. #include <poll.h>
  6. #include <signal.h>
  7. #include <sys/types.h>
  8. #include <unistd.h>
  9. #include <fcntl.h>


  10. /* fifthdrvtest
  11.   */
  12. int fd;

  13. void my_signal_fun(int signum)    
  14. {
  15.     unsigned char key_val;
  16.     read(fd, &key_val, 1);
  17.     printf("key_val: 0x%x\n", key_val);
  18. }

  19. int main(int argc, char **argv)
  20. {
  21.     unsigned char key_val;
  22.     int ret;
  23.     int Oflags;

  24.     signal(SIGIO, my_signal_fun);     // 接收驱动程序中kill_fasync()传来的信号,然后调用my_signal_fun()函数。
  25.     
  26.     fd = open("/dev/buttons", O_RDWR);
  27.     if (fd < 0)
  28.     {
  29.         printf("can't open!\n");
  30.     }

  31.     fcntl(fd, F_SETOWN, getpid());    // 获取进程PID
  32.     
  33.     Oflags = fcntl(fd, F_GETFL);
  34.     
  35.     fcntl(fd, F_SETFL, Oflags | FASYNC);   // 异步通知


  36.     while (1)
  37.     {
  38.         sleep(1000);
  39.     }
  40.     
  41.     return 0;
  42. }






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