原文點擊打開鏈接
fasync是爲了使驅動的讀寫和應用程序的讀寫分開,使得應用程序可以在驅動讀寫的時候去做別的事。
應用程序通過fcntl給自己的SIGIO信號安裝自己的響應函數,
驅動通過kill_fasync(&async, SIGIO, POLL_IN); 發SIGIO信號給應用程序,應用程序就調用自己安裝的響應函數去處理。
fasync_helper作用就是初始化fasync,包括分配內存和設置屬性,最後在驅動的release裏把fasync_helper初始化的東西free掉。
例子:
驅動程序:
/*******************************************
使用linux3.2.81內核
********************************************/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/sched.h>
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <mach/regs-gpio.h>
#include <mach/hardware.h>
#include <linux/poll.h>
static struct class *buttonsdrv_class; //類結構體
static struct device *buttonsdrv_class_dev; //設備結構體
volatile unsigned long *gpfcon = NULL ;
volatile unsigned long *gpfdat = NULL;
volatile unsigned long *gpgcon = NULL ;
volatile unsigned long *gpgdat = NULL;
unsigned int press_cnt[4]; //記錄該按鍵按下的次數
static DECLARE_WAIT_QUEUE_HEAD(button_waitq); //聲明(定義)等待隊列
/* 中斷事件標誌, 中斷服務程序將它置1,buttons_drv_read將它清0 */
static volatile int ev_press = 0;
static struct fasync_struct *button_async;
//中斷處理函數
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
volatile int *press_cnt = (volatile int *)dev_id;
*press_cnt = *press_cnt +1; //按鍵計數值加一
ev_press = 1; /* 表示中斷髮生了 */
wake_up_interruptible(&button_waitq); /* 喚醒休眠的進程 */
//發送信號SIGIO信號給fasync_struct 結構體所描述的PID,觸發應用程序的SIGIO信號處理函數
kill_fasync (&button_async, SIGIO, POLL_IN);
return IRQ_RETVAL(IRQ_HANDLED);
}
static int buttons_drv_open(struct inode *inode, struct file *file)
{
/*
參數解析:中斷號,中斷處理函數,中斷處理標誌(如:雙邊沿觸發),中斷名稱(任意取),
dev_id(中斷處理函數)在中斷共享時會用到,一般設置爲這個設備的設備結構體或者NULL
*/
request_irq(IRQ_EINT0,buttons_irq, IRQ_TYPE_EDGE_BOTH, "S2",&press_cnt[0]); /* 配置GPF0爲輸入引腳 ,註冊中斷處理函數*/
request_irq(IRQ_EINT2,buttons_irq, IRQ_TYPE_EDGE_BOTH, "S3",&press_cnt[1]);
request_irq(IRQ_EINT11,buttons_irq,IRQ_TYPE_EDGE_BOTH, "S4",&press_cnt[2]);
request_irq(IRQ_EINT19,buttons_irq,IRQ_TYPE_EDGE_BOTH, "S5",&press_cnt[3]);
return 0;
}
static int buttons_drv_close(struct inode *inode, struct file *file)
{
//釋放已經註冊的中斷
free_irq(IRQ_EINT0, &press_cnt[0]);
free_irq(IRQ_EINT2, &press_cnt[1]);
free_irq(IRQ_EINT11, &press_cnt[2]);
free_irq(IRQ_EINT19, &press_cnt[3]);
return 0;
}
static int buttons_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
/* 如果沒有按鍵動作, 休眠 wait_event_interruptible的第二個參數是一個判斷條件,如果條件成立則退出休眠,否則進入休眠*/
wait_event_interruptible(button_waitq, ev_press); //按鍵按下中斷處理函數中會將ev_press=1
/* 如果有按鍵動作, 返回鍵值 */
copy_to_user(buf, press_cnt, min(sizeof(press_cnt),size)); //根據實際需要讀出對應個數的值到用戶空間
ev_press = 0; //讀取完後清0
return 1;
}
static int buttons_drv_fasync (int fd, struct file *filp, int on)
{
printk("driver: buttons_drv_fasync\n");
//初始化fasync_struct 結構體 (fasync_struct->fa_file->f_owner->pid)
return fasync_helper (fd, filp, on, &button_async);
}
/*
當應用程序操作設備文件時所調用的open、read、write等函數,最終會調用這個結構體中上的對應函數
*/
static struct file_operations buttons_drv_fops = {
.owner = THIS_MODULE, /* 這是一個宏,推向編譯模塊時自動創建的__this_module變量 */
.open = buttons_drv_open,
.release = buttons_drv_close,
.read = buttons_drv_read,
.fasync = buttons_drv_fasync,
};
int major;
//指定insmod命令時會調用這個函數
static int buttons_drv_init(void)
{
//第一個參數如果等於0,則表示採用系統動態分配的主設備號;不爲0,則表示靜態註冊
major = register_chrdev(0, "buttons_drv", &buttons_drv_fops); // 註冊, 告訴內核,返回動態創建的主設備號
//以下兩條語句是爲了實現自動創建設備
buttonsdrv_class = class_create(THIS_MODULE, "buttonsdrv"); //class_create爲該設備創建一個class
buttonsdrv_class_dev = device_create(buttonsdrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/buttons */ //class_device_create創建對應的設備
//驅動中要使用虛擬地址,不能直接使用物理地址
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16); //gpfcon映射爲0x56000050,映射長度爲16個字節(gpfcon爲虛地址,0x56000050爲物理地址)
gpfdat = gpfcon + 1; //gpfcon + 1即爲0x56000050+4
gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16); //gpgcon映射爲0x56000060,映射長度爲16個字節(gpgcon爲虛地址,0x560000650爲物理地址)
gpgdat = gpgcon + 1; //gpgcon + 1即爲0x56000060+4
return 0;
}
//執行rmmod時會調用這個函數
static void buttons_drv_exit(void)
{
unregister_chrdev(major, "buttons_drv"); // 卸載
device_unregister(buttonsdrv_class_dev); //將自動創建的設備註銷
class_destroy(buttonsdrv_class); //刪除創建的類
iounmap(gpfcon); //取消gpfcon的映射
iounmap(gpgcon); //取消gpgcon的映射
}
//指定驅動程序的初始化函數和卸載函數
module_init(buttons_drv_init);
module_exit(buttons_drv_exit);
MODULE_LICENSE("GPL"); //防止出現“module license”unspecified taints kernel的警告
應用程序:
/*
字符設備測試程序
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <signal.h>
int fd;
//信號處理函數
void my_signal_fun(int signum)
{
int i;
int press_cnt[4];
read(fd, press_cnt, sizeof(press_cnt)); //讀取值到press_cnt緩存區,讀取長度sizeof(press_cnt)
for(i = 0 ; i<sizeof(press_cnt)/sizeof(press_cnt[0]);i++)
{
if(press_cnt[i]) //如果按下次數不爲0,打印出來
printf("K%d has been pressed %d times \n", i+1, press_cnt[i]);
}
}
int main(int argc , char ** argv)
{
int Oflags;
//在應用程序中捕捉SIGIO信號(由驅動程序發送)
signal(SIGIO, my_signal_fun);
fd = open("/dev/buttons", O_RDWR); //打開設備
if(fd<0)
{
printf("Can't open");
return -1;
}
//將當前進程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(5); //掛起進程5秒,信號會提前打斷休眠
printf("wait!\n"); //5秒後輸出wait! ,一定要加上“\n”,否則無法刷新緩存區,會一直沒有輸出
}
}