1. 程式人生 > >linux字元驅動之poll機制按鍵驅動

linux字元驅動之poll機制按鍵驅動

在上一節中,我們講解了如何自動建立裝置節點,實現一箇中斷方式的按鍵驅動。雖然中斷式的驅動,效率是蠻高的,但是大家有沒有發現,應用程式的死迴圈裡的讀函式是一直在讀的;在實際的應用場所裡,有沒有那麼一種情況,偶爾有資料、偶爾沒有資料,答案當然是有的。我們理想當然的就會想到,當有資料的時候,我們才去讀它,沒資料的時候我們讀它幹啥?豈不浪費勞動力?
上一節文章連結:http://blog.csdn.net/lwj103862095/article/details/17511867
這一節裡,我們在中斷的基礎上新增poll機制來實現有資料的時候就去讀,沒資料的時候,自己規定一個時間,如果還沒有資料,就表示超時時間。


poll機制總結:(韋老師總結的,不是我總結的哦!)

1. poll > sys_poll > do_sys_poll >poll_initwait,poll_initwait函式註冊一下回調函式__pollwait,它就是我們的驅動程式執行poll_wait時,真正被呼叫的函式。

2. 接下來執行file->f_op->poll,即我們驅動程式裡自己實現的poll函式

   它會呼叫poll_wait把自己掛入某個佇列,這個佇列也是我們的驅動自己定義的;

   它還判斷一下裝置是否就緒。

3. 如果裝置未就緒,do_sys_poll裡會讓程序休眠一定時間

4. 程序被喚醒的條件有2:一是上面說的“一定時間”到了,二是被驅動程式喚醒。驅動程式發現條件就緒時,就把“某個佇列”上掛著的程序喚醒,這個佇列,就是前面通過poll_wait把本程序掛過去的佇列。

5. 如果驅動程式沒有去喚醒程序,那麼chedule_timeout(__timeou)超時後,會重複2、3動作,直到應用程式的poll呼叫傳入的時間到達。

問:這一節與上一節的在驅動裡添加了哪些內容?

答:僅僅添加了poll函式,該函式如下:

static unsigned int fourth_drv_poll(struct file *file, poll_table *wait)
{
	unsigned int mask = 0;

	/* 該函式,只是將程序掛在button_waitq佇列上,而不是立即休眠 */
	poll_wait(file, &button_waitq, wait);

	/* 當沒有按鍵按下時,即不會進入按鍵中斷處理函式,此時ev_press = 0 
	 * 當按鍵按下時,就會進入按鍵中斷處理函式,此時ev_press被設定為1
	 */
	if(ev_press)
	{
		mask |= POLLIN | POLLRDNORM;  /* 表示有資料可讀 */
	}

	/* 如果有按鍵按下時,mask |= POLLIN | POLLRDNORM,否則mask = 0 */
	return mask;  
}

詳細請參考驅動原始碼:
#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 <linux/module.h>
#include <linux/device.h> 		//class_create
#include <mach/regs-gpio.h>		//S3C2410_GPF1
//#include <asm/arch/regs-gpio.h>  
#include <mach/hardware.h>
//#include <asm/hardware.h>
#include <linux/interrupt.h>  //wait_event_interruptible
#include <linux/poll.h>   //poll


/* 定義並初始化等待佇列頭 */
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);


static struct class *fourthdrv_class;
static struct device *fourthdrv_device;

static struct pin_desc{
	unsigned int pin;
	unsigned int key_val;
};

static struct pin_desc pins_desc[4] = {
		{S3C2410_GPF1,0x01},
		{S3C2410_GPF4,0x02},
		{S3C2410_GPF2,0x03},
		{S3C2410_GPF0,0x04},
}; 

static int ev_press = 0;

/* 鍵值: 按下時, 0x01, 0x02, 0x03, 0x04 */
/* 鍵值: 鬆開時, 0x81, 0x82, 0x83, 0x84 */
static unsigned char key_val;
int major;

/* 使用者中斷處理函式 */
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;
	}

	ev_press = 1;							 /* 表示中斷已經發生 */
	 wake_up_interruptible(&button_waitq);   /* 喚醒休眠的程序 */
	return IRQ_HANDLED;
}
static int fourth_drv_open(struct inode * inode, struct file * filp)
{
	/*  K1 ---- EINT1,K2 ---- EINT4,K3 ---- EINT2,K4 ---- EINT0
  	 *  配置GPF1、GPF4、GPF2、GPF0為相應的外部中斷引腳
  	 *  IRQT_BOTHEDGE應該改為IRQ_TYPE_EDGE_BOTH
	 */
	request_irq(IRQ_EINT1, buttons_irq, IRQ_TYPE_EDGE_BOTH, "K1",&pins_desc[0]);
	request_irq(IRQ_EINT4, buttons_irq, IRQ_TYPE_EDGE_BOTH, "K2",&pins_desc[1]);
	request_irq(IRQ_EINT2, buttons_irq, IRQ_TYPE_EDGE_BOTH, "K3",&pins_desc[2]);
	request_irq(IRQ_EINT0, buttons_irq, IRQ_TYPE_EDGE_BOTH, "K4",&pins_desc[3]);
	return 0;
}

static ssize_t fourth_drv_read(struct file *file, char __user *user, size_t size,loff_t *ppos)
{
	if (size != 1)
			return -EINVAL;
	
	/* 當沒有按鍵按下時,休眠。
	 * 即ev_press = 0;
	 * 當有按鍵按下時,發生中斷,在中斷處理函式會喚醒
	 * 即ev_press = 1; 
	 * 喚醒後,接著繼續將資料通過copy_to_user函式傳遞給應用程式
	 */
	wait_event_interruptible(button_waitq, ev_press);
	copy_to_user(user, &key_val, 1);
	
	/* 將ev_press清零 */
	ev_press = 0;
	return 1;	
}

static int fourth_drv_close(struct inode *inode, struct file *file)
{
	free_irq(IRQ_EINT1,&pins_desc[0]);
	free_irq(IRQ_EINT4,&pins_desc[1]);
	free_irq(IRQ_EINT2,&pins_desc[2]);
	free_irq(IRQ_EINT0,&pins_desc[3]);
	return 0;
}

static unsigned int fourth_drv_poll(struct file *file, poll_table *wait)
{
	unsigned int mask = 0;

	/* 該函式,只是將程序掛在button_waitq佇列上,而不是立即休眠 */
	poll_wait(file, &button_waitq, wait);

	/* 當沒有按鍵按下時,即不會進入按鍵中斷處理函式,此時ev_press = 0 
	 * 當按鍵按下時,就會進入按鍵中斷處理函式,此時ev_press被設定為1
	 */
	if(ev_press)
	{
		mask |= POLLIN | POLLRDNORM;  /* 表示有資料可讀 */
	}

	/* 如果有按鍵按下時,mask |= POLLIN | POLLRDNORM,否則mask = 0 */
	return mask;  
}


/* File operations struct for character device */
static const struct file_operations fourth_drv_fops = {
	.owner		= THIS_MODULE,
	.open		= fourth_drv_open,
	.read		= fourth_drv_read,
	.release    = fourth_drv_close,
	.poll       = fourth_drv_poll,
};


/* 驅動入口函式 */
static int fourth_drv_init(void)
{
	/* 主裝置號設定為0表示由系統自動分配主裝置號 */
	major = register_chrdev(0, "fourth_drv", &fourth_drv_fops);

	/* 建立fourthdrv類 */
	fourthdrv_class = class_create(THIS_MODULE, "fourthdrv");

	/* 在fourthdrv類下建立buttons裝置,供應用程式開啟裝置*/
	fourthdrv_device = device_create(fourthdrv_class, NULL, MKDEV(major, 0), NULL, "buttons");

	return 0;
}

/* 驅動出口函式 */
static void fourth_drv_exit(void)
{
	unregister_chrdev(major, "fourth_drv");
	device_unregister(fourthdrv_device);  //解除安裝類下的裝置
	class_destroy(fourthdrv_class);		//解除安裝類
}

module_init(fourth_drv_init);  //用於修飾入口函式
module_exit(fourth_drv_exit);  //用於修飾出口函式	

MODULE_AUTHOR("LWJ");
MODULE_DESCRIPTION("Just for Demon");
MODULE_LICENSE("GPL");  //遵循GPL協議

應用測試原始碼:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <poll.h>

/* fourth_test
 */ 
int main(int argc ,char *argv[])

{
	int fd;
	unsigned char key_val;
	struct pollfd fds;
	int ret;

	fd = open("/dev/buttons",O_RDWR);
	if (fd < 0)
	{
		printf("open error\n");
	}
	fds.fd = fd;
	fds.events = POLLIN;
	while(1)
	{
		/* A value of 0 indicates  that the call timed out and no file descriptors were ready
		 * poll函式返回0時,表示5s時間到了,而這段時間裡,沒有事件發生"資料可讀"
		 */
		ret = poll(&fds,1,5000);
		if(ret == 0)
		{
			printf("time out\n");
		}
		else	/* 如果沒有超時,則讀出按鍵值 */
		{
			read(fd,&key_val,1);
			printf("key_val = 0x%x\n",key_val);
		} 	
	}
	return 0;
}

測試步驟:
[WJ2440]# ls
Qt             etc            lib            sbin           third_test
TQLedtest      first_drv.ko   linuxrc        sddisk         tmp
app_test       first_test     mnt            second_drv.ko  udisk
bin            fourth_drv.ko  opt            second_test    usr
dev            fourth_test    proc           sys            var
driver_test    home           root           third_drv.ko   web
[WJ2440]# insmod fourth_drv.ko 
[WJ2440]# lsmod
fourth_drv 3164 0 - Live 0xbf003000
[WJ2440]# ls /dev/buttons -l
crw-rw----    1 root     root      252,   0 Jan  2 03:00 /dev/buttons
[WJ2440]# ./fourth_test 
time out
time out
key_val = 0x1
key_val = 0x81
key_val = 0x4
key_val = 0x84
key_val = 0x3
key_val = 0x83
key_val = 0x2
key_val = 0x82
^C
[WJ2440]# ./fourth_test &
[WJ2440]# time out
time out
time out
[WJ2440]#top

Mem: 10076K used, 50088K free, 0K shrd, 0K buff, 7224K cached
CPU:  0.1% usr  0.7% sys  0.0% nic 99.0% idle  0.0% io  0.0% irq  0.0% sirq
Load average: 0.00 0.00 0.00 1/23 637
  PID  PPID USER     STAT   VSZ %MEM CPU %CPU COMMAND
  637   589 root     R     2092  3.4   0  0.7 top
  589     1 root     S     2092  3.4   0  0.0 -/bin/sh
    1     0 root     S     2088  3.4   0  0.0 init
  590     1 root     S     2088  3.4   0  0.0 /usr/sbin/telnetd -l /bin/login
  587     1 root     S     1508  2.5   0  0.0 EmbedSky_wdg
  636   589 root     S     1432  2.3   0  0.0 ./fourth_test
  573     2 root     SW<      0  0.0   0  0.0 [rpciod/0]
    5     2 root     SW<      0  0.0   0  0.0 [khelper]
  329     2 root     SW<      0  0.0   0  0.0 [nfsiod]
    2     0 root     SW<      0  0.0   0  0.0 [kthreadd]
    3     2 root     SW<      0  0.0   0  0.0 [ksoftirqd/0]
    4     2 root     SW<      0  0.0   0  0.0 [events/0]
   11     2 root     SW<      0  0.0   0  0.0 [async/mgr]
  237     2 root     SW<      0  0.0   0  0.0 [kblockd/0]
  247     2 root     SW<      0  0.0   0  0.0 [khubd]
  254     2 root     SW<      0  0.0   0  0.0 [kmmcd]
  278     2 root     SW       0  0.0   0  0.0 [pdflush]
  279     2 root     SW       0  0.0   0  0.0 [pdflush]
  280     2 root     SW<      0  0.0   0  0.0 [kswapd0]
  325     2 root     SW<      0  0.0   0  0.0 [aio/0]

由測試結果可以看出,當按鍵沒有被按下時,5秒之後,會顯示出time out,表示時間已到,在這5秒時間裡,沒有按鍵被按下,即沒有資料可讀,當按鍵按下時,立即打印出按下的按鍵;同時,fourth_test程序,也幾乎不佔用CPU的利用率。