字符設備之poll機制
主要用途就是:進程設置一段時間用來等待資源,假設時間到了資源還沒有到來,進程就立馬從睡眠狀態喚醒不再等待。當然這僅僅是使用於這段時間以後資源對於該進程已經沒用的情況。
內核中poll機制的實現過程:
sys_poll函數在include/linux/syscalls.h中聲明
//函數定義前加宏asmlinkage ,表示這些函數通過堆棧而不是通過寄存器傳遞參數。 asmlinkage long sys_poll(struct pollfd __user *ufds, unsigned int nfds,long timeout);
CALL(sys_poll) //系統調用跳轉表的一項
關於系統調用表的初始化在arch/arm/kernel/entry-common.S中
.equ NR_syscalls,0 //將NR_syscalls初始化為0 #define CALL(x) .equ NR_syscalls,NR_syscalls+1 //將CALL(x) 定義為: NR_syscalls = NR_syscalls + 1 #include "calls.S"//將calls.S的內容包進來,CALL(x)上面已經有了定義,這裏就相當於運行了多次NR_syscalls++,最後就統計了系統調用的個數。並對NR_syscalls進行了4的倍數對齊。這一招,特麽好厲害!#undef CALL //撤銷CALL宏定義 #define CALL(x) .long x //對CALL又一次進行宏定義。也是4字節對齊 arch/arm/kernel/entry-common.S中: sys_syscall: bic scno, r0, #__NR_OABI_SYSCALL_BASE cmp scno, #__NR_syscall - __NR_SYSCALL_BASE cmpne scno, #NR_syscalls @ check range stmloia sp, {r5, r6} @ shuffle args movlo r0, r1 movlo r1, r2 movlo r2, r3 movlo r3, r4 ldrlo pc, [tbl, scno, lsl #2] b sys_ni_syscall
SYSCALL_DEFINE3(poll, struct pollfd __user *, ufds, unsigned int, nfds,long, timeout_msecs) { ...... ret = do_sys_poll(ufds, nfds, to);//調用 ...... }好,看看應用層調用poll函數時的底層驅動運行線路
【app:】 poll(); 【kernel: 】 sys_poll do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,struct timespec *end_time) poll_initwait(&table); init_poll_funcptr(&table->pt, __pollwait);-->pt->qproc = __pollwait; //初始化qproc函數指針,讓他指向__pollwait函數 do_poll(nfds, head, &table, end_time); for(;;) { for (; pfd != pfd_end; pfd++) //查詢多個驅動程序 { if (do_pollfd(pfd, pt)) -> mask = file->f_op->poll(file, pwait);return mask; { //do_pollfd函數相當於調用驅動裏面的xxx_poll函數,以下另外再進行分析,返回值mask非零。count++,記錄等待事件發生的進程數 count++; pt = NULL; } } if (count || timed_out) //若count不為0(有等待的事件發生了)或者timed_out不為0(有信號發生或超時),則推出休眠 break; //上述條件不滿足以下開始進入休眠,若有等待的事件發生了,超時或收到信號則喚醒 poll_schedule_timeout(wait, TASK_INTERRUPTIBLE, to, slack) }驅動裏邊的xxx_poll()函數分析
xxx_poll(struct file *file, poll_table *wait) poll_wait(file, &xxxx_waitq, wait); ////////////////////////////////////////////////////////////////// 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); //調用之前poll_initwait()函數設置的函數qproc即__pollwait,__pollwait函數僅僅是把當前進程掛到等待隊列,僅僅是add_wait_queue(wait_address, &entry->wait);不進入休眠 }
測試驅動程序:poll_dev.c
#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> //S3C2440_GPF1 #include <mach/hardware.h> #include <linux/interrupt.h> //wait_event_interruptible #include <linux/fs.h> #include <linux/poll.h> //poll /* 定義並初始化等待隊列頭 */ static DECLARE_WAIT_QUEUE_HEAD(button_waitq); static struct class *buttondev_class; static struct device *buttons_device; static struct pin_desc{ unsigned int pin; unsigned int key_val; }; static struct pin_desc pins_desc[4] = { {S3C2410_GPF1,0x01}, //S3C2410_GPF1是對GPF1引腳這樣的“設備”的編號dev_id {S3C2410_GPF4,0x02}, {S3C2410_GPF2,0x03}, {S3C2410_GPF0,0x04}, }; static int ev_press = 0; static unsigned char key_val; int major; /* 中斷處理函數 */ static irqreturn_t handle_irq(int irq, void *dev_id) { struct pin_desc *irq_pindesc = (struct pin_desc *)dev_id;// unsigned int pinval; pinval = s3c2410_gpio_getpin(irq_pindesc->pin);//獲取按鍵值:有按鍵按下返回按鍵值0 /* 鍵值: 按下時, 0x01, 0x02, 0x03, 0x04 */ /* 鍵值: 松開時, 0x81, 0x82, 0x83, 0x84 */ if(pinval) { /* 松開 */ key_val = 0x80 | (irq_pindesc->key_val); } else { /* 按下 */ key_val = irq_pindesc->key_val; } ev_press = 1; /* 表示中斷已經發生 */ wake_up_interruptible(&button_waitq); /* 喚醒休眠的進程 */ return IRQ_HANDLED; } static int buttons_dev_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, handle_irq, IRQ_TYPE_EDGE_FALLING, "K1",&pins_desc[0]); request_irq(IRQ_EINT4, handle_irq, IRQ_TYPE_EDGE_FALLING, "K2",&pins_desc[1]); request_irq(IRQ_EINT2, handle_irq, IRQ_TYPE_EDGE_FALLING, "K3",&pins_desc[2]); request_irq(IRQ_EINT0, handle_irq, IRQ_TYPE_EDGE_FALLING, "K4",&pins_desc[3]); return 0; } static int buttons_dev_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 ssize_t buttons_dev_read(struct file *file, char __user *user, size_t size,loff_t *ppos) { if (size != 1) return -EINVAL; //wait_event_interruptible(button_waitq, ev_press);//使用poll機制。這裏就不須要再推斷要不要進入睡眠了。copy_to_user(user, &key_val, 1); /* 將ev_press清零 */ ev_press = 0; return 1; } //////////////////////////關鍵點/////////////////////////////////////// static unsigned int buttons_dev_poll(struct file *file, poll_table *wait) //該函數一旦被調用就觸發poll機制 { unsigned int mask = 0; /* 該函數,僅僅是將進程掛在button_waitq隊列上。而不是馬上休眠 */ poll_wait(file, &button_waitq, wait); /*** * 如果進程還poll在上面這一函數裏邊。尚未超時,如果此時有中斷到來,中斷處理程序將ev_press置位,然後喚醒休眠隊列上相應的進程 ***/ /* 進程喚醒之後,立刻往下運行。喚醒的可能原因:超時/中斷處理 */ if(ev_press) { mask |= POLLIN | POLLRDNORM; /* 有數據可讀 */ } /* 如果有按鍵按下時,mask |= POLLIN | POLLRDNORM,否則mask = 0 */ return mask; } /////////////////////////////////////////////////////////////////////////////// /* File operations struct for character device */ static const struct file_operations buttons_dev_fops = { .owner = THIS_MODULE, .open = buttons_dev_open, .read = buttons_dev_read, .release = buttons_dev_close, .poll = buttons_dev_poll, }; /* 驅動入口函數 */ static int buttons_dev_init(void) { /* 主設備號設置為0表示由系統自己主動分配主設備號 */ major = register_chrdev(0, "buttons_dev", &buttons_dev_fops); /* 創建buttondev類 */ buttondev_class = class_create(THIS_MODULE, "buttondev"); /* 在buttondev類下創建buttons設備,供應用程序打開設備*/ buttons_device = device_create(buttondev_class, NULL, MKDEV(major, 0), NULL, "buttons");// return 0; } /* 驅動出口函數 */ static void buttons_dev_exit(void) { unregister_chrdev(major, "buttons_dev"); device_unregister(buttons_device); //卸載類下的設備 class_destroy(buttondev_class); //卸載類 } /* 模塊載入和卸載函數的修飾 */ module_init(buttons_dev_init); module_exit(buttons_dev_exit); MODULE_AUTHOR("CLBIAO"); MODULE_DESCRIPTION("Just for Demon"); MODULE_LICENSE("GPL"); //遵循GPL協議
測試應用程序:app_poll.c
/* 文件的編譯指令是arm-linux-gcc -static -o app_poll app_poll.c */ #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; //期待收到poll_in值。表示有數據 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; }測試結果:
小結:poll流程圖
字符設備之poll機制