1、查詢方法:一直在查詢,不斷去查詢是否有事件發(fā)生,整個過程都是占用CPU資源,非常消耗CPU資源。 2、中斷方式:當有事件發(fā)生時,就去跳轉(zhuǎn)到相應事件去處理,CPU占用時間少。 3、poll方式: 中斷方式雖然占用CPU資源少,但是在應用程序上需要不斷在死循環(huán)里面執(zhí)行讀取函數(shù),應用程序不能去做其它事情。poll機制解決了這個問題,當有事件發(fā)生時,才去執(zhí)行讀read函數(shù),按鍵事件沒有按下時<如果規(guī)定了時間,超過時間后返回無按鍵信息>,去執(zhí)行其它的處理函數(shù)。 poll機制分析 一、poll內(nèi)核框架: 所有的系統(tǒng)調(diào)用,都可以基于它的名字前加上“sys_”前綴,這就是它在內(nèi)核中對應的函數(shù)。比如系統(tǒng)調(diào)用open、read、write、poll,與之對應的內(nèi)核函數(shù)為:sys_open、sys_read、sys_write、sys_poll。先來看三種按鍵驅(qū)動方式:
① sys_poll函數(shù)位于fs/Select.c文件中,代碼如下:對于系統(tǒng)調(diào)用poll或select,它們對應的內(nèi)核函數(shù)都是sys_poll。分析sys_poll,即可理解poll機制。
asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds,
long timeout_msecs)
{
s64 timeout_jiffies;
if (timeout_msecs > 0) {
#if HZ > 1000
/* We can only overflow if HZ > 1000 */
if (timeout_msecs / 1000 > (s64)0x7fffffffffffffffULL / (s64)HZ)
timeout_jiffies = -1;
else
#endif
timeout_jiffies = msecs_to_jiffies(timeout_msecs);
} else {
/* Infinite (< 0) or no (0) timeout */
timeout_jiffies = timeout_msecs;
}
return do_sys_poll(ufds, nfds, &timeout_jiffies);
}
它對超時參數(shù)稍作處理后,直接調(diào)用do_sys_poll。
② do_sys_poll函數(shù)也位于位于fs/Select.c文件中,我們忽略其他代碼:
int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, s64 *timeout)
{
……
poll_initwait(&table);
……
fdcount = do_poll(nfds, head, &table, timeout);
……
}
②-Ⅰ
poll_initwait函數(shù)非常簡單,它初始化一個poll_wqueues變量table:
poll_initwait > init_poll_funcptr(&pwq->pt, __pollwait); > pt->qproc = qproc;
即table->pt->qproc = __pollwait
,__pollwait將在驅(qū)動的poll函數(shù)里用到。
static int do_poll(unsigned int nfds, struct poll_list *list,
struct poll_wqueues *wait, s64 *timeout)
{
01 ……
02 for (;;) {
03 ……
04 if (do_pollfd(pfd, pt)) {
05 count++;
06 pt = NULL;
07 }
08 ……
09 if (count || !*timeout || signal_pending(current))
10 break;
11 count = wait->error;
12 if (count)
13 break;
14
15 if (*timeout < 0) {
16 /* Wait indefinitely */
17 __timeout = MAX_SCHEDULE_TIMEOUT;
18 } else if (unlikely(*timeout >= (s64)MAX_SCHEDULE_TIMEOUT-1)) {
19 /*
20 * Wait for longer than MAX_SCHEDULE_TIMEOUT. Do it in
21 * a loop
22 */
23 __timeout = MAX_SCHEDULE_TIMEOUT - 1;
24 *timeout -= __timeout;
25 } else {
26 __timeout = *timeout;
27 *timeout = 0;
28 }
29
30 __timeout = schedule_timeout(__timeout);
31 if (*timeout >= 0)
32 *timeout += __timeout;
33 }
34 __set_current_state(TASK_RUNNING);
35 return count;
36 }
分析其中的代碼,可以發(fā)現(xiàn),它的作用如下:
① 從02行可以知道,這是個循環(huán),它退出的條件為:
a. 09行的3個條件之一(count非0,超時、有信號等待處理)
count非0表示04行的do_pollfd至少有一個成功。
b. 11、12行:發(fā)生錯誤
②重點在do_pollfd函數(shù),后面再分析
③第30行,讓本進程休眠一段時間,注意:應用程序執(zhí)行poll調(diào)用后,如果①②的條件不滿足,進程就會進入休眠。那么,誰喚醒呢?除了休眠到指定時間被系統(tǒng)喚醒外,還可以被驅(qū)動程序喚醒──記住這點,這就是為什么驅(qū)動的poll里要調(diào)用poll_wait的原因,后面分析。
②-Ⅱ->do_pollfd函數(shù)位于fs/Select.c文件中,代碼如下:
static inline unsigned int do_pollfd(struct pollfd *pollfd, poll_table *pwait)
{
……
if (file->f_op && file->f_op->poll)
mask = file->f_op->poll(file, pwait);
……
}
可見,它就是調(diào)用我們的驅(qū)動程序里注冊的poll函數(shù)。
二、驅(qū)動程序:
驅(qū)動程序里與poll相關的地方有兩處:一是構(gòu)造file_operation結(jié)構(gòu)時,要定義自己的poll函數(shù)。二是通過poll_wait來調(diào)用上面說到的__pollwait函數(shù),pollwait的代碼如下:
static inline void poll_wait(struct file * filp, wait_queue_head_t * wait_address, poll_table *p)
{
if (p && wait_address)
p->qproc(filp, wait_address, p);
}
p->qproc就是__pollwait函數(shù),從它的代碼可知,它只是把當前進程掛入我們驅(qū)動程序里定義的一個隊列里而已。它的代碼如下:
static void __pollwait(struct file *filp, wait_queue_head_t *wait_address,
poll_table *p)
{
struct poll_table_entry *entry = poll_get_entry(p);
if (!entry)
return;
get_file(filp);
entry->filp = filp;
entry->wait_address = wait_address;
init_waitqueue_entry(&entry->wait, current);
add_wait_queue(wait_address, &entry->wait);
}
執(zhí)行到驅(qū)動程序的poll_wait函數(shù)時,進程并沒有休眠,我們的驅(qū)動程序里實現(xiàn)的poll函數(shù)是不會引起休眠的。讓進程進入休眠,是前面分析的do_sys_poll函數(shù)的30行“__timeout = schedule_timeout(__timeout)”。
poll_wait只是把本進程掛入某個隊列,應用程序調(diào)用poll > sys_poll > do_sys_poll > poll_initwait,do_poll > do_pollfd > 我們自己寫的poll函數(shù)后,再調(diào)用schedule_timeout進入休眠。如果我們的驅(qū)動程序發(fā)現(xiàn)情況就緒,可以把這個隊列上掛著的進程喚醒。可見,poll_wait的作用,只是為了讓驅(qū)動程序能找到要喚醒的進程。即使不用poll_wait,我們的程序也有機會被喚醒:chedule_timeout(__timeout),只是要休眠__time_out這段時間。
總結(jié)一下poll機制:
1. poll > sys_poll > do_sys_poll > poll_initwait,poll_initwait函數(shù)注冊一下回調(diào)函數(shù)__pollwait,它就是我們的驅(qū)動程序執(zhí)行poll_wait時,真正被調(diào)用的函數(shù)。
2. 接下來執(zhí)行file->f_op->poll,即我們驅(qū)動程序里自己實現(xiàn)的poll函數(shù),它會調(diào)用poll_wait把自己掛入某個隊列,這個隊列也是我們的驅(qū)動自己定義的;它還判斷一下設備是否就緒。
3. 如果設備未就緒,do_sys_poll里會讓進程休眠一定時間
4. 進程被喚醒的條件有兩個:一是上面說的“一定時間”到了,二是被驅(qū)動程序喚醒。驅(qū)動程序發(fā)現(xiàn)條件就緒時,就把“某個隊列”上掛著的進程喚醒,這個隊列,就是前面通過poll_wait把本進程掛過去的隊列。
5. 如果驅(qū)動程序沒有去喚醒進程,那么chedule_timeout(__timeou)超時后,會重復2、3動作,直到應用程序的poll調(diào)用傳入的時間到達。
下面為用poll機制操控按鍵例子:
驅(qū)動程序:fourth_drv.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
static struct class *fourthdrv_class;
static struct class_device *fourthdrv_class_dev;
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
/* 中斷事件標志, 中斷服務程序?qū)⑺?,s3c24xx_fourth_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;
};
/* 鍵值: 按下時,0x01、0x02、0x03 */
/* 鍵值: 松開時,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); /* 喚醒休眠的進程 */
return IRQ_RETVAL(IRQ_HANDLED);
}
static int fourth_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 fourth_drv_read (struct file *file, char __user *buf, size_t count, loff_t *ppos)
{
//看用戶需要讀取的空間,和這里的是否相同
if(count != 1)
return -EINVAL;
/* 如果無按鍵動作發(fā)生,則進行休眠狀態(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 fourth_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 fourth_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 struct file_operations fourth_drv_fops = {
.owner = THIS_MODULE, /* 這是一個宏,推向編譯模塊時自動創(chuàng)建的__this_module變量 */
.open = fourth_drv_open,
.read = fourth_drv_read,
.release = fourth_drv_close,
.poll = fourth_drv_poll
};
int major;
static int fourth_drv_init(void)
{
major = register_chrdev(0,"fourth_drv",&fourth_drv_fops);
fourthdrv_class = class_create(THIS_MODULE,"fourthdrv");
fourthdrv_class_dev = class_device_create(fourthdrv_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 fourth_drv_exit(void)
{
unregister_chrdev(major,"fourth_drv");
class_device_unregister(fourthdrv_class_dev);
class_destroy(fourthdrv_class);
iounmap(gpfcon);
iounmap(gpgcon);
return 0;
}
module_init(fourth_drv_init);
module_exit(fourth_drv_exit);
MODULE_LICENSE("GPL");
驅(qū)動測試程序:fourthdrvtest.c
#include
#include
#include
#include
#include
/*
* fourthdrvtest
*/
int main(int argc, char **argv)
{
int fd;
unsigned char key_val;
struct pollfd fds[1];
int ret;
fd = open("/dev/buttons",O_RDWR);
if(fd < 0)
{
printf("can't open!n");
}
fds[0].fd = fd;
fds[0].events = POLLIN;
while(1)
{
ret = poll(fds,1,5000);
if(ret == 0)
{
printf("Time out!n");
}
else
{
read(fd,&key_val,1);
printf("key_val = 0x%xn",key_val);
}
}
return 0;
}
Makefile
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 += fourth_drv.o
測試命令:
make
arm-linux-gcc -o fourthdrvtest fourthdrvtest.c
cp fourthdrvtest fourth_drv.ko /work/nfs_root/czg
insmod fourth_drv.ko
./fourthdrvtest &
top