加poll机制来实现有数据的时候就去读,没数据的时候,自己规定一个时间,如果还没有数据,就表示超时时间。在此以前,我们都是让应用程序主动去读,那有没有一种情况,当驱动程序有数据时,主动去告诉应用程序,告诉它,有数据了,你赶紧来读吧。答案当然是有的,这种情况在Linux里的专业术语就叫异步通知。
问:如何实现异步通知,有哪些要素?
答:有四个要素:
一、应用程序要实现有:注册信号处理函数,使用signal函数
二、谁来发?驱动来发
三、发给谁?发给应用程序,但应用程序必须告诉驱动PID
四、怎么发?驱动程序使用kill_fasync函数
问:应该在驱动的哪里调用kill_fasync函数?
答:kill_fasync函数的作用是,当有数据时去通知应用程序,理所当然的应该在用户终端处理函数里调用。
问:file_operations需要添加什么函数指针成员吗?
答:要的,需要添加fasync函数指针,要实现这个函数指针,幸运的是,这个函数仅仅调用了fasync_helper函数,而且这个函数是内核帮我们实现好了,驱动工程师不用修改,fasync_helper函数的作用是初始化/释放fasync_struct
应用程序:
fcntl(fd, F_SETOWN, getpid()); // 告诉内核,发给谁
Oflags = fcntl(fd, F_GETFL);
fcntl(fd, F_SETFL, Oflags | FASYNC); // 改变fasync标记,最终会调用到驱动的faync > fasync_helper:初始化/释放fasync_struct
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/interrupt.h> //request_irq
#include <mach/irqs.h> //中断号,已包含plat/irqs.h
#include <linux/fs.h>
#include <linux/device.h> //class_create device_create
#include <mach/regs-gpio.h>
#include <linux/io.h> //ioremap ioread32 iowrite32
#include <linux/sched.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include <asm/uaccess.h>
#include <linux/sched.h>
#define DEV_NAME "myfasyn-dev"
static struct class *fasyncdrv_class;
static struct class_device *fasyncdrv_class_dev;
struct led_reg {
u32 gpm4con;
u8 gpm4dat;
};
static struct led_reg *led_reg;
struct key_reg {
u32 gpm4con;
u8 gpm4dat;
};
static struct key_reg *key_reg;
struct beep_reg {
u32 gpm4con;
u8 gpm4dat;
};
static struct key_reg *beep_reg;
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
/* 中断事件标志, 中断服务程序将它置1,fasync_drv_read将它清0 */
static volatile int ev_press = 0;
static struct fasync_struct *button_async;
static unsigned char key_val;
int major;
/*
* 确定按键值
*/
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
printk("buttons_irq\n");
//获取按键的键值,因为按键是从该寄存器的第二位开始的,所以需要左移2位,接着与上0xf---1111
//这样,如果用户按下按键,就会返回一个键值保存在key_val这个变量里
key_val = ((key_reg->gpm4dat) >> 2) & 0xf ;
printk("key_val_irq= %d\n",key_val);
ev_press = 1; /* 表示中断发生了 */
wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */
kill_fasync (&button_async, SIGIO, POLL_IN);
return IRQ_RETVAL(IRQ_HANDLED);
}
static int fasync_drv_open(struct inode *inode, struct file *file)
{
int ret;
printk("fasync_open\n");
//配置4个按键为输入状态,因为按键是从GPXCON[2]开始的,所以要左移8位到对应的位置,将8位以后的16位清0
//这样的话就将按键配置的寄存器设置为输入状态了,因为输入是0x0
key_reg->gpm4con &= ~((0xf<<(2*4)) | (0xf<<(3*4)) | (0xf<<(4*4)) | (0xf<<(5*4)));
//先对LED的端口进行清0操作
led_reg->gpm4con &= ~((0xf<<(3*4)) | (0xf<<(2*4)) | (0xf<<(1*4)) | (0xf<<(0*4)));
//将4个IO口16位都设置为Output输出状态
led_reg->gpm4con |= ((0x1<<(3*4)) | (0x1<<(2*4)) | (0x1<<(1*4)) | (0x1<<(0*4)));
//清寄存器
beep_reg->gpm4con &= ~(0xf);
//设置io为输出
beep_reg->gpm4con |= (0x1);
ret =request_irq(IRQ_EINT(27), buttons_irq, IRQF_TRIGGER_FALLING, "k2", "k2");
ret =request_irq(IRQ_EINT(28), buttons_irq, IRQF_TRIGGER_FALLING, "k3", "k3");
ret =request_irq(IRQ_EINT(29), buttons_irq, IRQF_TRIGGER_FALLING, "k4", "k4");
ret = request_irq(IRQ_EINT(26), buttons_irq, IRQF_TRIGGER_FALLING, "k1", "k1");
if (ret < 0)
{
printk("request_irq faild\n");
return -EINVAL;
}
return 0;
}
ssize_t fasync_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
if (size != 1)
return -EINVAL;
/* 如果没有按键动作, 休眠 */
wait_event_interruptible(button_waitq, ev_press);
/* 如果有按键动作, 返回键值 */
copy_to_user(buf, &key_val, 1);
printk("read_val_irq= %d\n",key_val);
ev_press = 0;
return 1;
}
//write方法
int fasync_drv_write(struct file *filp , const char __user *buf , size_t count , loff_t *f_pos)
{
int val ;
//注意,这里是在内核中进行操作,我们需要使用copy_from_user这个函数将用户态的内容拷贝到内核态
copy_from_user(&val , buf , count);
//以下就是当val是哪个值的时候,led就执行相应的操作,这里不多说
switch(val)
{
case 0 :
//对状态寄存器进行赋值,以下雷同
printk(KERN_EMERG"led1_on\n");
led_reg->gpm4dat &= ~(1<<0) ;
beep_reg->gpm4dat |= 0x1 ;
break ;
case 1 :
printk(KERN_EMERG"led2_on\n");
led_reg->gpm4dat &= ~(1<<1) ;
//beep_reg->gpm4dat |= 0x1 ;
beep_reg->gpm4dat &=~0x1 ; //蜂鸣器不响
break ;
case 2 :
printk(KERN_EMERG"led3_on\n");
led_reg->gpm4dat &= ~(1<<2) ;
beep_reg->gpm4dat |= 0x1 ;
break ;
case 3 :
printk(KERN_EMERG"ledall_off\n");
led_reg->gpm4dat |= ((1<<0) | (1<<1) |(1<<2)| (1<<3)) ;
beep_reg->gpm4dat &=~0x1 ;
break ;
case 4 :
printk(KERN_EMERG"ledall_on\n");
led_reg->gpm4dat &=~ ((1<<0) | (1<<1) |(1<<2)| (1<<3)) ;
beep_reg->gpm4dat &=~0x1 ;
break ;
case 5 :
printk(KERN_EMERG"ledall_off\n");
led_reg->gpm4dat |= ((1<<0) | (1<<1) |(1<<2)| (1<<3)) ;
beep_reg->gpm4dat |= 0x1 ;
break ;
}
return 0;
}
int fasync_drv_close(struct inode *inode, struct file *file)
{
printk("fasync_close\n");
led_reg->gpm4dat |= (1<<0) | (1<<1) | (1<<2) | (1<<3); //全灭,因为高电平是灭的,0xf ----> 1111
//关闭蜂鸣器
beep_reg->gpm4dat &= ~0x1 ;
free_irq(IRQ_EINT(26), "k1");
free_irq(IRQ_EINT(27), "k2");
free_irq(IRQ_EINT(28), "k3");
free_irq(IRQ_EINT(29), "k4");
return 0;
}
static unsigned fasync_drv_poll(struct file *file, poll_table *wait)
{
unsigned int mask = 0;
poll_wait(file, &button_waitq, wait); // 不会立即休眠
if (ev_press)
mask |= POLLIN | POLLRDNORM;
return mask;
}
static int fasync_drv_fasync (int fd, struct file *filp, int on)
{
printk("driver: fifth_drv_fasync\n");
return fasync_helper (fd, filp, on, &button_async);
}
static struct file_operations fasync_drv_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = fasync_drv_open,
.read = fasync_drv_read,
.release = fasync_drv_close,
.poll = fasync_drv_poll,
.write = fasync_drv_write,
.fasync = fasync_drv_fasync,
};
static int fasync_drv_init(void)
{
printk("fasync_drv_init\n");
major = register_chrdev(0, DEV_NAME, &fasync_drv_fops);
fasyncdrv_class = class_create(THIS_MODULE, DEV_NAME);
fasyncdrv_class_dev = device_create(fasyncdrv_class, NULL, MKDEV(major, 0), NULL, DEV_NAME); /* /dev/buttons */
led_reg = ioremap(0x110002e0, sizeof(struct led_reg));
beep_reg = ioremap(0x114000A0, sizeof(struct beep_reg));
key_reg = ioremap(0x11000C60, sizeof(struct key_reg));
return 0;
}
static void fasync_drv_exit(void)
{
printk("fasync_exit\n");
unregister_chrdev(major, "fasync_drv");
device_destroy(fasyncdrv_class, MKDEV(major, 0));
class_destroy(fasyncdrv_class);
iounmap(led_reg);
iounmap(beep_reg);
iounmap(key_reg);
}
module_init(fasync_drv_init);
module_exit(fasync_drv_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("hai");
MODULE_VERSION("2017.5.3");
应用
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#define DEV_NAME "/dev/myfasyn-dev "
int fd;
/* polldrvtest
*/
void my_signal_fun(int signum)
{
unsigned char key_val;
int var_tmp=0;
read(fd, &key_val, 1);
printf("key_val = 0x%x\n", key_val);
switch(key_val)
{
case 7 :
{
var_tmp=0;
printf("the first_key press! key_val=%u\n",key_val);
//delay();
write(fd , &var_tmp , 4);
break;
}
case 11:
{
var_tmp=1;
printf("the second_key press! key_val=%u\n",key_val);
//delay();
write(fd , &var_tmp , 4);
break ;
}
case 13:
{
var_tmp=2;
write(fd , &var_tmp , 4);
printf("the third_key press! key_val=%u\n",key_val);
//delay();
break ;
}
case 14:
{
var_tmp=3;
write(fd , &var_tmp , 4);
printf("the fourth_key press! key_val=%u\n",key_val);
//delay();
break ;
}
default :
{
var_tmp=5;
write(fd , &var_tmp , 4);
printf("no key is press!\n");
//delay();
}
}
}
int main(int argc, char **argv)
{
int Oflags;
fd = open(DEV_NAME, O_RDWR);
if (fd < 0)
{
printf("can't open!\n");
}
//在应用程序中捕捉SIGIO信号(由驱动程序发送)
signal(SIGIO, my_signal_fun);
//将当前进程PID设置为fd文件所对应驱动程序将要发送SIGIO,SIGUSR信号进程PID
fcntl(fd, F_SETOWN, getpid());
//获取fd的打开方式
Oflags = fcntl(fd, F_GETFL);
//将fd的打开方式设置为FASYNC --- 即 支持异步通知
//该行代码执行会触发 驱动程序中 file_operations->fasync 函数 ------fasync函数调用fasync_helper初始化一个fasync_struct结构体,该结构体描述了将要发送信号的进程PID (fasync_struct->fa_file->f_owner->pid)
fcntl(fd, F_SETFL, Oflags | FASYNC);
while (1)
{
sleep(1000);
}
return 0;
}
Makefile
KERN_DIR = /home/tools/linux-3.5
all:
make -C $(KERN_DIR) M=`pwd` modules
clean:
make -C $(KERN_DIR) M=`pwd` clean
rm -rf modules.order
obj-m += fasync_driver.o