按鍵驅動增加poll機制

2021-07-27 14:39:07 字數 2494 閱讀 7249

poll機制的原理

運用層中的open會呼叫sys_open,那我們的運用程式中的poll也會呼叫到我們的sys_poll

sys_poll又會呼叫到do_sys_poll(.... , ...... ,&timeout_jiffies);最後乙個引數就是超時引數

do_sys_poll又會呼叫到

poll_initwait(&table);

poll_initwait>init_poll_funcptr(&pwp->pt,__pollwait); >pt->qproc=qproc//table->gproc=__pollwait

do_poll(nfds,head,&table,timeout)

do_poll裡面做的事情是:

一來便有乙個死迴圈

for(::)

if(do_poll(pfd,pt){ >mask=file->f_op->poll(file,pwait);return mask;//呼叫驅動中的poll函式

count++;//如果驅動中的poll函式返回的不是0的話,那麼count就++。

pt=null;

}break的條件是1.count非0

2.超時

3.有訊號等待處理

if(count || !*timeout || signal_pending(current)) //如果count不等於0的話就跳出了這個死迴圈,返回到應用程式裡面去了,

break;

假設這些條件都不成立的話,就會休眠,休眠__timeout這麼一段時間

__timeout=schedule_timeout(__timeout);//休眠

休眠這段時間之後,沒什麼事件發生,又會重新開始執行那個for迴圈,但是到達if條件的時候因為timeout等於0而返回

驅動中的pollwait(filp,&button_waitq,p);

就是把當前這個程序掛到button_waitq佇列裡面去 ,只是放到這個程序裡面去,並不會休眠,休眠是在__timeout=schedule_timeout(__timeout)這裡開始休眠的,如果當前有資料返還給使用者的話,就是 mask |= pollin | pollrdnorm; 否則的話就返回0. 如果返回0的話上面的count++就不會執行。

驅動**如下

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

int key_id;

struct cdev cdev;

dev_t devno;

/*等待佇列頭*/

wait_queue_head_t key_qaq;

/*中斷事件標誌,中斷服務程式將它置為1,read函式將它置為0*/

unsigned int wait_val=0;

struct class *my_class;

static irqreturn_t key_func(int irq,void *dev_id)

static int key_open(struct inode *inode,struct file *file)

static int key_close(struct inode *inode,struct file *file)

ssize_t key_read(struct file *filp, char __user *buf, size_t size,loff_t *pos)

static unsigned key_poll(struct file *file, poll_table *wait)

static struct file_operations key_fops =

;static int __init key_poll_init(void)

device_create(my_class, null, devno,null,"key_poll_driver");

init_waitqueue_head(&key_qaq);

return 0;

}static void key_poll_exit(void)

module_init(key_poll_init);

module_exit(key_poll_exit);

module_license("gpl");

module_author("eight_five");

測試程式如下

#include

#include

#include

#include

#include

int main(int argv,char **argc)

else

}return 0;

}

按鍵驅動 poll機制

應用程式 open,read,write,poll 驅動程式 open,read,write,poll 所有的系統呼叫,基於都可以在它的名字前加上 sys 字首,這就是它在核心中對應的函式。比如系統呼叫open read write poll,與之對應的核心函式為 sys open sys read...

tiny6410 按鍵中斷驅動 poll機制

驅動程式key drv int.c include include include include include include include include include include include include include include include include incl...

字元裝置驅動程式之按鍵 poll機制

本節裡我們在按鍵中斷機制的基礎上新增了poll機制來優化程式 我們知道,應用程式中的open read write函式會呼叫核心裡的sys open sys read sys write函式,而核心裡的這些函式又會對應到驅動程式裡的.open read write函式。我們的poll機制也不例外,使...