linux驱动的异步通知(kill_fasync,fasync)---- 驱动程序向应用程序发送信号 应用程序

应用程序

  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");

  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");

猜你喜欢

转载自blog.csdn.net/wlf_go/article/details/80924537