转自:http://blog.csdn.net/kingdragonfly120/article/details/10858647

版权声明:本文为博主原创文章,未经博主允许不得转载。

Linux内核空间发生中断后怎么使用户空间的应用程序运行相应的函数呢,当芯片有数据到来时内核会产生一个中断,但是怎样通知应用程序来取数据,以前这个问题一直困扰我很长时间,后来发现linux中有异步通知机制,在用户程序中用signal注册一个响应SIGIO信号的回调函数,然后在驱动程序中向该进程发出SIGIO信号便完成该功能,下面是该功能具体实施方法:

1.在驱动中定义一个static struct fasync_struct *async;

2.在fasync系统调用中注册fasync_helper(fd, filp, mode, &async);

3.在中断服务程序(顶半部、底半部都可以)发出信号kill_fasync(&async, SIGIO, POLL_IN);

4.在用户应用程序中用signal注册一个响应SIGIO的回调函数signal(SIGIO, sig_handler);

5.通过fcntl(fd, F_SETOWN, getpid())将将进程pid传入内核

6.通过fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | FASYNC)设置异步通知

驱动部分代码:

  1. #include <linux/kernel.h>
  2. #include <linux/errno.h>
  3. #include <linux/module.h>
  4. #include <linux/fs.h>
  5. #include <linux/miscdevice.h>
  6. #include <asm/io.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/irq.h>
  9. #include <linux/gpio.h>
  10. #include <mach/regs-gpio.h>
  11. #include <asm-generic/siginfo.h>
  12. #include <linux/init.h>
  13. #include <asm/signal.h>
  14. #include <linux/timer.h>
  15. #include <asm/uaccess.h>
  16. #define DEVICE_NAME "mybeep"
  17. volatile unsigned long *GPBCON;
  18. volatile unsigned long *GPBDAT;
  19. volatile unsigned long *GPBUP;
  20. void beep_start(void);
  21. void beep_stop(void);
  22. int  beep_irq_register(void);
  23. unsigned int flag=1;
  24. static struct fasync_struct *async; //声明fasync_struct
  25. struct key_irq_desc {
  26. unsigned int irq;
  27. int pin;
  28. int pin_setting;
  29. int number;
  30. char *name;
  31. };
  32. static int beep_fasync(int fd, struct file *filp, int mode)
  33. {
  34. printk("application  fasync!\n");
  35. return fasync_helper(fd, filp, mode, &async);         //注册上层调用进程的信息,上层调用fcntl设置FASYNC会调用这个系统调用
  36. }
  37. static struct key_irq_desc key_irqs [] = {
  38. {IRQ_EINT8, S3C2410_GPG(0), S3C2410_GPG0_EINT8, 0, "KEY1"},
  39. };
  40. static irqreturn_t key_interrupt(int irq, void *dev_id)
  41. {
  42. kill_fasync(&async, SIGIO, POLL_IN);  //向打开设备文件的进程发出SIGIO信号
  43. return (IRQ_HANDLED);
  44. }
  45. void beep_gpiob_init(void)
  46. {
  47. *GPBCON&=~((1<<0)|(1<<1));
  48. *GPBCON|=(1<<0);
  49. *GPBUP&=~(1<<0);
  50. }
  51. void beep_start(void)
  52. {
  53. *GPBDAT|=(1<<0);
  54. }
  55. void beep_stop(void)
  56. {
  57. *GPBDAT&=~(1<<0);
  58. }
  59. int beep_open(struct inode *inode, struct file *filp)
  60. {
  61. if(beep_irq_register() != 0)
  62. {
  63. printk("Request irq error!\n");
  64. }
  65. printk(KERN_ALERT "application  open!\n");
  66. return 0;
  67. }
  68. ssize_t beep_read(struct file *file, char __user *buff, size_t count, loff_t *offp)
  69. {
  70. printk("application  read!\n");
  71. return 0;
  72. }
  73. ssize_t beep_write(struct file *file, const char __user *buff, size_t count, loff_t *offp)
  74. {
  75. printk("application  write!\n");
  76. return 0;
  77. }
  78. static int beep_release(struct inode *inode, struct file *file)
  79. {
  80. disable_irq(key_irqs[0].irq);
  81. free_irq(key_irqs[0].irq, (void *)&key_irqs[0]);
  82. printk("application  close!\n");
  83. return beep_fasync(-1, file, 0);
  84. }
  85. static int beep_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
  86. {
  87. switch(cmd)
  88. {
  89. case 0:
  90. beep_start();
  91. break;
  92. case 1:
  93. beep_stop();
  94. break;
  95. default:
  96. break;
  97. }
  98. return 0;
  99. }
  100. static struct file_operations beep_ops = {
  101. .owner = THIS_MODULE,
  102. .open = beep_open,
  103. .release = beep_release,
  104. .ioctl = beep_ioctl,
  105. .read = beep_read,
  106. .write = beep_write,
  107. .fasync = beep_fasync,
  108. };
  109. static struct miscdevice beep_misc = {
  110. .minor = MISC_DYNAMIC_MINOR,
  111. .name = DEVICE_NAME,
  112. .fops = &beep_ops,
  113. };
  114. int beep_irq_register(void)
  115. {
  116. int err;
  117. err = request_irq(key_irqs[0].irq, key_interrupt, 0, key_irqs[0].name, (void *)&key_irqs[0]);
  118. set_irq_type(key_irqs[0].irq, IRQ_TYPE_EDGE_RISING);
  119. if(err)
  120. {
  121. disable_irq(key_irqs[0].irq);
  122. free_irq(key_irqs[0].irq, (void *)&key_irqs[0]);
  123. return -EBUSY;
  124. }
  125. return 0;
  126. }
  127. static int __init beep_init(void)
  128. {
  129. int ret;
  130. ret=misc_register(&beep_misc);
  131. if(ret <0)
  132. {
  133. printk("register miscdevice error code:%d\n",ret);
  134. return ret;
  135. }
  136. printk("beep device create!\n");
  137. GPBCON=(volatile unsigned long *)ioremap(0x56000010,12);
  138. GPBDAT=GPBCON+1;
  139. GPBUP=GPBCON+2;
  140. beep_gpiob_init();
  141. return 0;
  142. }
  143. static void __exit beep_exit(void)
  144. {
  145. iounmap(GPBCON);
  146. misc_deregister(&beep_misc);
  147. printk("beep device delete!\n");
  148. }
  149. MODULE_LICENSE("GPL");
  150. MODULE_AUTHOR("kingdragonfly");
  151. module_init(beep_init);
  152. module_exit(beep_exit);

用户应用程序代码:

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <signal.h>
  4. #include <fcntl.h>
  5. void sig_handler(int sig)
  6. {
  7. if(sig == SIGIO)
  8. {
  9. printf("Receive io signal from kernel!\n");
  10. }
  11. }
  12. int main(void)
  13. {
  14. int fd;
  15. signal(SIGIO, sig_handler);
  16. fd = open("/dev/mybeep",O_RDWR);
  17. fcntl(fd, F_SETOWN, getpid());
  18. fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | FASYNC);
  19. printf("waiting key interrupt:\n");
  20. while(1)
  21. {
  22. }
  23. }

当内核里发生中断时在中断服务程序中发出SIGIO信号从而自动调用相应的回调函数,在回调函数中可以进行相应处理。

上面程序在mini2440开发板实现了按K1键,用户程序自动调用void sig_handler(int sig)功能

最新文章

  1. 使用SecureCRT连接虚拟机(ubuntu)配置记录
  2. SharePoint 2103 Check user permission on list
  3. C#开发微信门户及应用(34)--微信裂变红包
  4. textview设置drawable
  5. HDU2048
  6. cocoapods 更新
  7. java定时器的使用(Timer)
  8. Storm(4) - Distributed Remote Procedure Calls
  9. 通过ros节点发布Twist Messages控制机器人--10
  10. 深入浅出Attribute (转载)
  11. Activate、Deactivate 事件 Activate ThrottleEvent;
  12. JS同源策略和跨域问题
  13. JAVA提高九:集合体系
  14. 【深入理解Java内存模型】
  15. 三十六、fetch
  16. Xapian使用入门
  17. GIT学习笔记——常用命令
  18. JSF action actionListner 详解
  19. 几种RAID介绍(总结)
  20. 深入理解 Python yield

热门文章

  1. Nginx技术深入剖析
  2. 前端各种mate积累
  3. &lt;jsp:param&gt;传参乱码问题
  4. JVM——九大工具助你玩转Java性能优化
  5. TouTiao开源项目 分析笔记18 视频详情页面
  6. Docker构建nginx+uwsgi+flask镜像(一)
  7. laravel5.5开发composer扩展包
  8. Visual Studio 2017 的 JavaScript 调试功能的关闭
  9. codeforces Registration system
  10. leetcode 179. 最大数 解题报告