应用程序

  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. (转载)Javascript removeChild()不能删除全部子节点的解决办法

    在Javascript中,只提供了一种删除节点的方法:removeChild().removeChild() 方法用来删除父节点的一个子节点. 语法: parent.removeChild(thisN ...

  2. 如何创建Windows定时任务

    我们经常使用电脑,有没有那么一个瞬间想着要是电脑可以每隔一段时间,自动处理一件事情就好了呢? 其实Windows还真有这样的功能,很多软件检测更新就是通过这个方法实现的. 这次我们来做一个简易的喝水提 ...

  3. Distinct powers (Project Euler 29 加强版)

    题目大意: $2<=a,b<=n$ 求 $a^b$能表示多少不同的正整数. 原题中n=100,可以直接暴力求解,常见的两种解法是写高精度或者取对数判断相等. 直觉告诉我应该有更加优秀的解法 ...

  4. tinycore Network card configuration during exec bootlocal.sh

    question: tinycore在boot时, 运行bootlocal.sh脚本,其中有局域网通信的部分,一直跑不通,测试了一下才知道是运行bootlocal.sh的阶段,网络可能没有配置好,ip ...

  5. Android初体验-D3

    1. UI界面布局. (即可用XML控制布局也可采用Java代码布局,不过在实际应用中是两者混合控制UI界面,为什么呢,因为XML适用于固定的不易改变的组件布局,Java程序控制常变的组件...其控制 ...

  6. PowerDesigner如何导出表到word的方法

    from:https://jingyan.baidu.com/article/295430f1c385970c7f005056.html PowerDesigner如何导出表到word的方法 听语音 ...

  7. 在Xcode中使用Git进行源码版本控制(转)

    http://www.cocoachina.com/ios/20140524/8536.html

  8. 锁(java, DB)

    知识点 事务 锁(java, DB) 多线程知识点整理 锁(java, DB) 什么是锁 对资源的访问权限进行控制 如果把一个资源(对象)比喻成屋子.就好像你进入了屋子锁上了门.你家人和贼都进不去了. ...

  9. sql duplicate key

    本文来自:高爽,转载请注明. 向数据库插入记录时,有时会有这种需求,当符合某种条件的数据存在时,去修改它,不存在时,则新增,也就是saveOrUpdate操作.这种控制可以放在业务层,也可以放在数据库 ...

  10. 三维空间直线最近点对hdu4741

    //求两条直线之间的关系(三维) //输入:两条不为点的直线 //输出:相交返回XIANGJIAO和交点p,平行返回PINGXING,共线返回GONGXIAN int LineAndLine(Line ...