应用程序

  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. //信号处理函数
  14. void my_signal_fun(int signum)
  15. {
  16. unsigned char key_val;
  17. read(fd, &key_val, 1);
  18. printf("key_val: 0x%x\n", key_val);
  19. }
  20. int main(int argc, char **argv)
  21. {
  22. unsigned char key_val;
  23. int ret;
  24. int Oflags;
  25. //在应用程序中捕捉SIGIO信号(由驱动程序发送)
  26. signal(SIGIO, my_signal_fun);
  27. fd = open("/dev/buttons", O_RDWR);
  28. if (fd < 0)
  29. {
  30. printf("can't open!\n");
  31. }
  32. //将当前进程PID设置为fd文件所对应驱动程序将要发送SIGIO,SIGUSR信号进程PID
  33. fcntl(fd, F_SETOWN, getpid());
  34. //获取fd的打开方式
  35. Oflags = fcntl(fd, F_GETFL);
  36. //将fd的打开方式设置为FASYNC --- 即 支持异步通知
  37. // 该行代码执行会触发 驱动程序中 file_operations->fasync 函数 ------fasync函数调用 fasync_helper初始化一个fasync_struct结构体,该结构体描述了将要发送信号的进程 PID (fasync_struct->fa_file->f_owner->pid)
  38. fcntl(fd, F_SETFL, Oflags | FASYNC);
  39. while (1)
  40. {
  41. sleep(1000);
  42. }
  43. return 0;
  44. }

驱动程序

  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. static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
  18. /* 中断事件标志, 中断服务程序将它置1,fifth_drv_read将它清0 */
  19. static volatile int ev_press = 0;
  20. static struct fasync_struct *button_async;
  21. struct pin_desc{
  22. unsigned int pin;
  23. unsigned int key_val;
  24. };
  25. /* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
  26. /* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
  27. static unsigned char key_val;
  28. /*
  29. * K1,K2,K3,K4对应GPG0,GPG3,GPG5,GPG6
  30. */
  31. struct pin_desc pins_desc[4] = {
  32. {S3C2410_GPG0, 0x01},
  33. {S3C2410_GPG3, 0x02},
  34. {S3C2410_GPG5, 0x03},
  35. {S3C2410_GPG6, 0x04},
  36. };
  37. /*
  38. * 确定按键值
  39. */
  40. static irqreturn_t buttons_irq(int irq, void *dev_id)
  41. {
  42. struct pin_desc * pindesc = (struct pin_desc *)dev_id;
  43. unsigned int pinval;
  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. //发送信号SIGIO信号给fasync_struct 结构体所描述的PID,触发应用程序的SIGIO信号处理函数
  58. kill_fasync (&button_async, SIGIO, POLL_IN);
  59. return IRQ_RETVAL(IRQ_HANDLED);
  60. }
  61. static int fifth_drv_open(struct inode *inode, struct file *file)
  62. {
  63. /* GPG0,GPG3,GPG5,GPG6为中断引脚: EINT8,EINT11,EINT13,EINT14 */
  64. request_irq(IRQ_EINT8,  buttons_irq, IRQT_BOTHEDGE, "K1", &pins_desc[0]);
  65. request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "K2", &pins_desc[1]);
  66. request_irq(IRQ_EINT13, buttons_irq, IRQT_BOTHEDGE, "K3", &pins_desc[2]);
  67. request_irq(IRQ_EINT14, buttons_irq, IRQT_BOTHEDGE, "K4", &pins_desc[3]);
  68. return 0;
  69. }
  70. ssize_t fifth_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
  71. {
  72. if (size != 1)
  73. return -EINVAL;
  74. /* 如果没有按键动作, 休眠 */
  75. wait_event_interruptible(button_waitq, ev_press);
  76. /* 如果有按键动作, 返回键值 */
  77. copy_to_user(buf, &key_val, 1);
  78. ev_press = 0;
  79. return 1;
  80. }
  81. int fifth_drv_close(struct inode *inode, struct file *file)
  82. {
  83. free_irq(IRQ_EINT8,  &pins_desc[0]);
  84. free_irq(IRQ_EINT11, &pins_desc[1]);
  85. free_irq(IRQ_EINT13, &pins_desc[2]);
  86. free_irq(IRQ_EINT14, &pins_desc[3]);
  87. return 0;
  88. }
  89. static unsigned fifth_drv_poll(struct file *file, poll_table *wait)
  90. {
  91. unsigned int mask = 0;
  92. poll_wait(file, &button_waitq, wait); // 不会立即休眠
  93. if (ev_press)
  94. mask |= POLLIN | POLLRDNORM;
  95. return mask;
  96. }
  97. static int fifth_drv_fasync (int fd, struct file *filp, int on)
  98. {
  99. printk("driver: fifth_drv_fasync\n");
  100. //初始化/释放 fasync_struct 结构体 (fasync_struct->fa_file->f_owner->pid)
  101. return fasync_helper (fd, filp, on, &button_async);
  102. }
  103. static struct file_operations sencod_drv_fops = {
  104. .owner   =  THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
  105. .open    =  fifth_drv_open,
  106. .read    =  fifth_drv_read,
  107. .release =  fifth_drv_close,
  108. .poll    =  fifth_drv_poll,
  109. .fasync  =  fifth_drv_fasync,
  110. };
  111. int major;
  112. static int fifth_drv_init(void)
  113. {
  114. major = register_chrdev(0, "fifth_drv", &sencod_drv_fops);
  115. fifthdrv_class = class_create(THIS_MODULE, "fifth_drv");
  116. fifthdrv_class_dev = class_device_create(fifthdrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/buttons */
  117. //  gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
  118. //  gpfdat = gpfcon + 1;
  119. return 0;
  120. }
  121. static void fifth_drv_exit(void)
  122. {
  123. unregister_chrdev(major, "fifth_drv");
  124. class_device_unregister(fifthdrv_class_dev);
  125. class_destroy(fifthdrv_class);
  126. //  iounmap(gpfcon);
  127. return 0;
  128. }
  129. module_init(fifth_drv_init);
  130. module_exit(fifth_drv_exit);
  131. MODULE_LICENSE("GPL");

http://blog.csdn.net/psvoldemort/article/details/21184525

linux驱动的异步通知(kill_fasync,fasync)---- 驱动程序向应用程序发送信号的更多相关文章

  1. Linux驱动之异步通知的应用

    前面的按键驱动方式都是应用程序通过主动查询的方式获得按键值的: 1.查询方式 2.中断方式 3.poll机制 下面介绍第四种按键驱动的方式 4.异步通知:它可以做到应用程序不用随时去查询按键的状态,而 ...

  2. 蜕变成蝶~Linux设备驱动之异步通知和异步I/O

    在设备驱动中使用异步通知可以使得对设备的访问可进行时,由驱动主动通知应用程序进行访问.因此,使用无阻塞I/O的应用程序无需轮询设备是否可访问,而阻塞访问也可以被类似“中断”的异步通知所取代.异步通知类 ...

  3. Linux通信之异步通知模式

    [参考]韦东山 教学笔记 为了使设备支持异步通知机制,驱动程序中涉及以下3项工作:1. 支持F_SETOWN命令,能在这个控制命令处理中设置filp->f_owner为对应进程ID. 不过此项工 ...

  4. linux下使用异步通知

    阻塞式I/O是一直等待直到设备可以访问,非阻塞式I/O是定期轮询设备是否可以访问. 异步通知则是当设备可以访问时才主动通知应用程序,有点像设备的硬中断. 并不是所有的设备都支持异步通知,应用程序通常假 ...

  5. Linux驱动之异步OR同步,阻塞OR非阻塞概念介绍

    链接:https://www.zhihu.com/question/19732473/answer/20851256 1.同步与异步同步和异步关注的是消息通信机制 (synchronous commu ...

  6. Linux设备驱动中的异步通知与异步I/O

    异步通知概念: 异步通知的意识是,一旦设备就绪,则主动通知应用程序,这样应用程序根本就不需要查询设备状态,这一点非常类似于硬件上的“中断”概念,比较准确的称谓是“信号驱动的异步IO”,信号是在软件层次 ...

  7. Linux 进程间通信 --- 信号通信 --- signal --- signal(SIGINT, my_func); --- 按键驱动异步通知(转)

    信号  ( signal ) 机制是 UNIX 系统中最为古老的进程间通信机制,很多条件可以产生一个信号. 信号的产生: 1,当用户按下某些按键时,产生信号. 2,硬件异常产生信号:除数为 0 ,无效 ...

  8. linux异步通知

    简述 linux下异步方式有两种:异步通知和异步IO(AIO),aio请参考:linux异步IO--aio 异步通知的含义是:一旦设备就绪,则主动通知应用程序,这样应用程序就不需要查询设备状态,准确称 ...

  9. Linux内核开发之异步通知与异步I/O(一)

    “小王,听说过锦上添花吧..”我拍拍下王的头说. “还锦上添花你,为你上次提的几个东东,我是头上长包..”小王气愤地瞪着我. “啊,为啥这样呢,本来还特意拒绝了MM的月份,抽出时间打算给你说点高级的东 ...

随机推荐

  1. Servlet 环境设置

    开发环境是您可以开发.测试.运行 Servlet 的地方. 就像任何其他的 Java 程序,您需要通过使用 Java 编译器 javac 编译 Servlet,在编译 Servlet 应用程序后,将它 ...

  2. 从git上拉下来的严选weex项目demo

    项目地址 https://github.com/zwwill/yanxuan-weex-demo 在package.json里"author"之类后面加上 "privat ...

  3. 怎样利用kettle官方社区查找关于carte服务的设置

    原创作品,出自 "深蓝的blog" 博客,转载时请务必注明出处.否则有权追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlong/ar ...

  4. java httpSession 设置超时时间

    1.设置过期时间方式一:在tomcat/conf/web.xml下加入一下内容 <session-config> <session-timeout>90</session ...

  5. supervisor 与 yii定时任务

    https://www.jianshu.com/p/9abffc905645 https://www.cnblogs.com/ajianbeyourself/p/5534737.html https: ...

  6. APP全局异常捕获,并保存本地文件

    public class CrashHandler implements Thread.UncaughtExceptionHandler { public static final String TA ...

  7. cocos2d-x设计模式发掘之一:单例模式

       作者: firedragonpzy  原地址:http://www.firedragonpzy.com.cn/index.php/archives/1781 本系列文章我将和大家一起来发掘coc ...

  8. 使用EasyNVR无插件流媒体服务器接口和EasyPlayer.js播放器插件实现web网页H5播放无插件

    1.背景需求 很多客户在使用EasyNVR无插件流媒体服务器时,不喜欢产品化的界面,有时可能满足不了日常观看使用的需求.因此软件提供丰富的HTTP接口,供第三方平台调用集成.但是有时客户这边可能没有专 ...

  9. event对象及各种事件

    事件(event) event对象 (1)什么是event对象? Event 对象代表事件的状态,比如事件在其中发生的元素.键盘按键的状态.鼠标的位置.鼠标按钮的状态.事件通常与函数结合使用,函数不会 ...

  10. Python:itertools模块(转)

    原文:http://www.cnblogs.com/cython/articles/2169009.html itertools模块包含很多创建迭代器的函数,可以用各种方式对数据进行循环操作,此模块中 ...