驱动程序之_1_字符设备_4_fasync机制
fasync机制是异步通知机制,当驱动程序向应用程序发送信号量,触发应用程序的信号处理函数,以达到类似中断的效果
驱动程序中:
1、在文件专属的fasync函数中,调用了fasync_helper,将属主信息通知给内核
2、当发生按键中断,进入按键中断服务程序,读取键值,调用kill_fasync,发送信号量给相应进程
应用程序中:
1、使用signal,设置信号处理函数
2、使用fcntl(fd,F_SETOWN,getpid());,指定一个进程作为文件的属主,属主的进程ID号就保存在filp->f_owner中
3、fcntl获取、设置标志,设置标志的时候,最终会调用到文件专属的fasync函数
4、当有按键中断发生,驱动程序发送信号量,触发应用程序的信号处理函数,读取键值
附上驱动程序和测试用的应用程序代码
驱动程序
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <linux/poll.h>
#include <linux/device.h>
#include <linux/irq.h>
static struct class *my_irq_class;
static struct class_device *my_irq_class_device;
static unsigned char key_val;
struct fasync_struct *button_fasync;
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
static struct pin_desc{
unsigned int pin;
unsigned int key_val;
};
struct pin_desc key_pins_desc[4]={
{S3C2410_GPF0,0x01},
{S3C2410_GPF2,0x02},
{S3C2410_GPG3,0x03},
{S3C2410_GPG11,0x04},
};
static irqreturn_t buttons_irq(int irq,void *dev_id)
{
struct pin_desc *pindesc = (struct pin_desc *)dev_id;
unsigned int pinval;
pinval = s3c2410_gpio_getpin(pindesc->pin);
if(pinval)
{
key_val = 0x80 | pindesc->key_val;
}
else
{
key_val = pindesc->key_val;
}
kill_fasync(&button_fasync, SIGIO, POLL_IN);
return IRQ_HANDLED;
}
static int my_irq_drv_open(struct inode *inode, struct file *file)
{
request_irq(IRQ_EINT0,buttons_irq,IRQT_BOTHEDGE,"S2",&key_pins_desc[0]);
request_irq(IRQ_EINT2,buttons_irq,IRQT_BOTHEDGE,"S3",&key_pins_desc[1]);
request_irq(IRQ_EINT11,buttons_irq,IRQT_BOTHEDGE,"S4",&key_pins_desc[2]);
request_irq(IRQ_EINT19,buttons_irq,IRQT_BOTHEDGE,"S5",&key_pins_desc[3]);
return 0;
}
int my_irq_drv_close(struct inode *inode,struct file *file)
{
free_irq(IRQ_EINT0,&key_pins_desc[0]);
free_irq(IRQ_EINT2,&key_pins_desc[1]);
free_irq(IRQ_EINT11,&key_pins_desc[2]);
free_irq(IRQ_EINT19,&key_pins_desc[3]);
return 0;
}
ssize_t my_irq_drv_read(struct file *file, char __user *buf, size_t size, loff_t * ppos)
{
if(size != 1)
return -EINVAL;
copy_to_user(buf,&key_val,1);
return 0;
}
static int my_irq_drv_fasync(int fd,struct file *filp,int on)
{
return fasync_helper(fd,filp,on,&button_fasync);
}
static struct file_operations irq_drv_fops = {
.owner = THIS_MODULE,
.open = my_irq_drv_open,
.read = my_irq_drv_read,
.release = my_irq_drv_close,
.fasync = my_irq_drv_fasync,
};
static int major;
static int my_irq_drv_init(void)
{
major = register_chrdev(0,"my_irq_drv",&irq_drv_fops);
my_irq_class = class_create(THIS_MODULE,"my_irq_drv");
my_irq_class_device = class_device_create(my_irq_class,NULL,MKDEV(major,0),NULL,"key_irq_drv");
return 0;
}
static void my_irq_drv_exit(void)
{
unregister_chrdev(major,"my_irq_drv");
class_device_unregister(my_irq_class_device);
class_destroy(my_irq_class);
}
module_init(my_irq_drv_init);
module_exit(my_irq_drv_exit);
MODULE_LICENSE("GPL");
应用程序
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
static int fd;
void my_signal_fun(int signum)
{
static int cnt = 0;
unsigned char key_val;
read(fd,&key_val,1);
printf("press key : 0x%02x , signum : %d , cnt : %d\r\n",key_val,signum,++cnt);
}
int main(int argc,char *args[])
{
int oflags;
signal(SIGIO,my_signal_fun);
fd = open("/dev/key_irq_drv",O_RDWR);
if(fd < 0)
{
printf("can't open\r\n");
return 0;
}
printf("open successfully\r\n");
fcntl(fd,F_SETOWN,getpid());
oflags = fcntl(fd,F_GETFL);
fcntl(fd,F_SETFL,oflags | FASYNC);
while(1)
{
sleep(1000);
}
return 0;
}