字符設(shè)備驅(qū)動-異步通知
異步通知:意思就是,一旦設(shè)備就緒,則主動通知應(yīng)用程序,這樣應(yīng)用程序根本不需要查詢設(shè)備狀態(tài),非常類似于硬件上”中斷的概念” 我們先來看一個(gè)例子:我們前面的三種按鍵操作中,都需要通過應(yīng)用程序不斷地主動通過read()來讀驅(qū)動程序或者通過poll機(jī)制利用返回的信息做出決定。
我們想要當(dāng)按下按鍵時(shí)利用驅(qū)動程序來通知應(yīng)用程序則需要引入異步通知
Signal.c
#include
#include
void czg_signal_handler(int signum)
{
static int cnt = 0;
printf("signal = %d, %d timesn",signum,++cnt);
}
int main(int argc,char **argv)
{
signal(SIGUSR1,czg_signal_handler);
while(1)
{
sleep(1000);
}
return 0;
}
編譯放到nfs服務(wù)器上運(yùn)行:
arm-linux-gcc -o Signal Signal.c
cp Signal /work/nfs_root/czg
測試:
./Signal &
ps
kill -SIGUSR1 776
//kill -9 776 殺死進(jìn)程
① 注冊信號處理函數(shù):在應(yīng)用程序中注冊 ② 誰來發(fā):驅(qū)動來發(fā)進(jìn)而我們引入到驅(qū)動中實(shí)現(xiàn):
③ 發(fā)給誰: 發(fā)給應(yīng)用程序(應(yīng)用程序先告訴驅(qū)動所調(diào)用進(jìn)程的PID) ④ 怎么發(fā):驅(qū)動調(diào)用kill_fasyn()驅(qū)動的faync > fasync_helper:初始化/釋放fasync_struct
Ⅰ. 支持F_SETOWN命令,能在這個(gè)控制命令處理中設(shè)置filp->f_owner為對應(yīng)進(jìn)程ID。為了使設(shè)備支持異步通知機(jī)制,驅(qū)動程序中涉及以下3項(xiàng)工作:
不過此項(xiàng)工作已由內(nèi)核完成,設(shè)備驅(qū)動無須處理。
Ⅱ. 支持F_SETFL命令的處理,每當(dāng)FASYNC標(biāo)志改變時(shí),驅(qū)動程序中的fasync()函數(shù)將得以執(zhí)行。驅(qū)動中實(shí)現(xiàn)fasync()函數(shù)。
Ⅲ. 在設(shè)備資源可獲得時(shí),調(diào)用kill_fasync()函數(shù)激發(fā)相應(yīng)的信號
驅(qū)動程序:fifth_drv.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
static struct fasync_struct *button_async;
static struct class *fifthdrv_class;
static struct class_device *fifthdrv_class_dev;
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
/* 中斷事件標(biāo)志, 中斷服務(wù)程序?qū)⑺?,s3c24xx_fifth_read將它清0 */
static volatile int ev_press = 0;
volatile unsigned long *gpfcon;
volatile unsigned long *gpfdat;
volatile unsigned long *gpgcon;
volatile unsigned long *gpgdat;
struct pin_desc{
unsigned int pin;
unsigned int key_val;
};
/* 鍵值: 按下時(shí),0x01、0x02、0x03 */
/* 鍵值: 松開時(shí),0x81、0x82、0x83 */
static unsigned char key_val;
struct pin_desc pin_desc[3] = {
{S3C2410_GPF0,0X01},
{S3C2410_GPF2,0X02},
{S3C2410_GPG3,0X03},
};
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);
*gpfdat |= ((1<<4) | (1<<5) | (1<<6));
}
else
{
/* 按下 */
key_val = pindesc->key_val;
*gpfdat &= ~((1<<4) | (1<<5) | (1<<6));
}
ev_press = 1; /* 表示中斷發(fā)生了 */
wake_up_interruptible(&button_waitq); /* 喚醒休眠的進(jìn)程 */
kill_fasync (&button_async, SIGIO, POLL_IN);
return IRQ_RETVAL(IRQ_HANDLED);
}
static int fifth_drv_open(struct inode *inode,struct file *file)
{
/* 配置GPF0,2、GPG3為中斷引腳 */
request_irq(IRQ_EINT0, buttons_irq,IRQT_BOTHEDGE,"s2",&pin_desc[0]);
request_irq(IRQ_EINT2, buttons_irq,IRQT_BOTHEDGE,"s3",&pin_desc[1]);
request_irq(IRQ_EINT11,buttons_irq,IRQT_BOTHEDGE,"s4",&pin_desc[2]);
/* 配置GPF4、5、6為輸入引腳 */
*gpfcon &= ~((0x3<<4*2) | (0x3<<5*2) | (0x3<<6*2));
*gpfcon |= ((1<<4*2) | (1<<5*2) | (1<<6*2));
return 0;
}
static ssize_t fifth_drv_read (struct file *file, char __user *buf, size_t count, loff_t *ppos)
{
//看用戶需要讀取的空間,和這里的是否相同
if(count != 1)
return -EINVAL;
/* 如果無按鍵動作發(fā)生,則進(jìn)行休眠狀態(tài) */
/* 如果ev_press等于0,休眠 */
wait_event_interruptible(button_waitq,ev_press);
/* 如果有按鍵動作發(fā)生,則返回按鍵的值 */
copy_to_user(buf,&key_val,1);
ev_press = 0;
return 1;
}
static int fifth_drv_close (struct inode *inode, struct file *file)
{
free_irq(IRQ_EINT0, &pin_desc[0]);
free_irq(IRQ_EINT2, &pin_desc[1]);
free_irq(IRQ_EINT11, &pin_desc[2]);
return 0;
}
static unsigned int fifth_drv_poll(struct file *file, struct poll_table_struct *wait)
{
unsigned int mask = 0;
poll_wait(file, &button_waitq, wait);
if (ev_press)
mask |= POLLIN | POLLRDNORM;
return mask;
}
static int fifth_drv_fasync (int fd, struct file *filp, int on)
{
printk("driver: fifth_drv_fasyncn");
return fasync_helper (fd, filp, on, &button_async);
}
static struct file_operations fifth_drv_fops = {
.owner = THIS_MODULE, /* 這是一個(gè)宏,推向編譯模塊時(shí)自動創(chuàng)建的__this_module變量 */
.open = fifth_drv_open,
.read = fifth_drv_read,
.release = fifth_drv_close,
.poll = fifth_drv_poll,
.fasync = fifth_drv_fasync,
};
int major;
static int fifth_drv_init(void)
{
major = register_chrdev(0,"fifth_drv",&fifth_drv_fops);
fifthdrv_class = class_create(THIS_MODULE,"fifthdrv");
fifthdrv_class_dev = class_device_create(fifthdrv_class,NULL,MKDEV(major,0),NULL,"buttons");
gpfcon = (volatile unsigned long *)ioremap(0x56000050,16);
gpfdat = gpfcon + 1;
gpgcon = (volatile unsigned long *)ioremap(0x56000060,16);
gpgdat = gpgcon + 1;
return 0;
}
static int fifth_drv_exit(void)
{
unregister_chrdev(major,"fifth_drv");
class_device_unregister(fifthdrv_class_dev);
class_destroy(fifthdrv_class);
iounmap(gpfcon);
iounmap(gpgcon);
return 0;
}
module_init(fifth_drv_init);
module_exit(fifth_drv_exit);
MODULE_LICENSE("GPL");
驅(qū)動測試程序:fifthdrvtest.c
#include
#include
#include
#include
#include
#include
#include
#include
/*
* fifthdrvtest
*/
int fd;
void czg_signal_handler(int signum)
{
unsigned char key_val = 0;
read(fd,&key_val,1);
printf("key_val: 0x%xn",key_val);
}
int main(int argc, char **argv)
{
int ret;
int oflags;
signal(SIGIO,czg_signal_handler);
fd = open("/dev/buttons",O_RDWR);
if(fd < 0)
{
printf("can't open!n");
}
fcntl(fd,F_SETOWN,getpid()); // 告訴內(nèi)核,發(fā)給誰
oflags = fcntl(fd,F_GETFL);
fcntl(fd,F_SETFL,oflags | FASYNC); // 改變fasync標(biāo)記,
//最終會調(diào)用到驅(qū)動的faync > fasync_helper:初始化/釋放fasync_struct
//然后當(dāng)按鍵按下時(shí)候,在irqreturn_t buttons_irq中斷處理中調(diào)用kill_fasync
while(1)
{
sleep(1000);
}
return 0;
}
Mafefile
KERN_DIR = /work/system/linux-2.6.22.6
all:
make -C $(KERN_DIR) M=`pwd` modules
clean:
make -C $(KERN_DIR) M=`pwd` modules clean
rm -rf modules.order
obj-m += fifth_drv.o
測試命令
make
arm-linux-gcc -o fifthdrvtest fifthdrvtest.c
cp fifthdrvtest fifth_drv.ko /work/nfs_root/czg
insmod fifth_drv.ko
./fifthdrvtest &
ps