按键驱动程序(poll机制)
此驱动程序可以在
按键驱动程序(中断方式)的基础之上稍作修改,最大的改变在于file_operations结构体中加入xxx_poll函数.
poll用于监测多个等待事件,若事件未发生,进程睡眠,放弃CPU控制权,
若监测的任何一个事件发生,poll将唤醒睡眠的进程,并判断发生事件的类型,执行相应的操作。
static unsigned int xxx_poll(struct file *file, poll_table *wait);
在2.6的低版本号里会出现用户空间使用xxx函数就会调用内核sys_xxx函数,如read对应sys_read;但我用的是2.6.32的内核,
调用机制有所更改,但去搜索内核代码是,还是有迹可循的,我使用的方法尽量去搜索poll相关的代码。最终找到了“根”。
就是从SYSCALL_DEFINE3开始。下面是搜索的过程。
SYSCALL_DEFINE3(poll, struct pollfd __user *, ufds, unsigned int, nfds,
long, timeout_msecs)
│
│─do_sys_poll(ufds, nfds, to)
│─poll_initwait(&table)
│ │─init_poll_funcptr(&pwq->pt, __pollwait)
│ │─pt->qproc = qproc,此处初始化了后面驱动程序用到poll_wait函数
│─do_poll(nfds, head, &table, end_time)
│─for (;;) { //一个死循环,应当有个出口
...
for (; pfd != pfd_end; pfd++) {
if (do_pollfd(pfd, pt)) { //do_pollfd的返回值决定count的值
count++;
...
}
}
if (count || timed_out) //出口在此,当count非零或超时跳出函数
break;
if (!poll_schedule_timeout(wait, TASK_INTERRUPTIBLE, to, slack)) //休眠
timed_out = 1;
}
do_pollfd(pfd, pt)
│───if (fd >= 0) {
mask = POLLNVAL;
if (file != NULL) {
mask = DEFAULT_POLLMASK;
if (file->f_op && file->f_op->poll) {
...
mask = file->f_op->poll(file, pwait);//此处调用了我们所写驱动程序的poll函数,下面介绍
}
...
}
}
...
return mask;
驱动程序的poll函数
static unsigned int forthdrv_poll(struct file *file, poll_table *wait)
{
unsigned int res=0;
poll_wait(file, &button_wqh, wait);
if (pressed) {
res |= POLLIN | POLLRDNORM;
}
return res;
}
poll_wait(file, &button_wqh, 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); //在前面的init_poll_funcptr函数已经初始化,即
p->qproc调用static void __pollwait(struct file *filp, wait_queue_head_t *wait_address, poll_table *p)
│─add_wait_queue(wait_address, &entry->wait);
就是将button_wqh添加到等待队列。poll如何得到的通知呢?
中断程序修改全局按键状态pressed,唤醒等待队列,并将事件通知存到res以mask由do_pollfd返回,决定是否增加count退出for(;;)循环。
详细驱动程序如下:
- #include <linux/module.h>
- #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 <linux/cdev.h>
- #include <linux/interrupt.h>
- #include <linux/device.h>
- #include <linux/sched.h>
- #include <linux/gpio.h>
- #include <linux/poll.h>
- #define FORTHDEV MKDEV(250, 0)
- struct cdev forthdrv_cdev;
- static struct class forthdrv_class = {
- .name = "forthdrv",
- .owner = THIS_MODULE,
- };
- struct button {
- int irq;
- char *name;
- int pin;
- int val;
- };
- static volatile int pressed = 0;
- static unsigned char key_val;
- DECLARE_WAIT_QUEUE_HEAD(button_wqh);
- /* 六个按键的相关定义整合到结构体 */
- static struct button buttons[6] = {
- {IRQ_EINT8, "K1", S3C2410_GPG(0), 0x1},
- {IRQ_EINT11, "K2", S3C2410_GPG(3), 0x2},
- {IRQ_EINT13, "K3", S3C2410_GPG(5), 0x3},
- {IRQ_EINT14, "K4", S3C2410_GPG(6), 0x4},
- {IRQ_EINT15, "K5", S3C2410_GPG(7), 0x5},
- {IRQ_EINT19, "K6", S3C2410_GPG(11),0x6},
- };
- /* 中断处理函数 */
- static irqreturn_t forthdrv_intr(int irq, void *data)
- {
- struct button *buttonp;
- int val;
-
- buttonp = (struct button*)data;
- val = s3c2410_gpio_getpin(buttonp->pin);
-
- if (!val) {/* 按下按键*/
- key_val = buttonp->val;
- } else { /* 释放按键*/
- key_val = buttonp->val | 0x10; //将第4位置1,做标记
- }
- pressed = 1; //此处改变按下标志,以使队列不继续睡眠
- wake_up_interruptible(&button_wqh);
-
- return IRQ_RETVAL(IRQ_HANDLED);
- }
- static int forthdrv_open(struct inode * inode, struct file * file)
- {
- int i=6;
- while(i--){
- request_irq(buttons[i].irq, &forthdrv_intr, IRQ_TYPE_EDGE_BOTH,
- buttons[i].name, &buttons[i]);
- }
- return 0;
-
- }
- static ssize_t forthdrv_read(struct file *file, char __user *user, size_t size,loff_t*o)
- {
- int sz = sizeof(key_val) ;
-
- if (sz != size) {
- return -EINVAL;
- }
-
- /* 使用poll机制,此处不必休眠 */
- //wait_event_interruptible(button_wqh, pressed);
-
- copy_to_user(user, &key_val, sz);
- /* 重新清除按下标志 */
- pressed = 0;
- return sz;
- }
- static int forthdrv_close(struct inode *inode, struct file *file)
- {
- int i=6;
-
- while(i--) {
- free_irq(buttons[i].irq, &buttons[i]);
- }
-
- return 0;
- }
- static unsigned int forthdrv_poll(struct file *file, poll_table *wait)
- {
- unsigned int res=0;
-
- poll_wait(file, &button_wqh, wait);
- if (pressed) {
- res |= POLLIN | POLLRDNORM;
- }
- return res;
- }
- static struct file_operations forthdrv_ops = {
- .owner = THIS_MODULE,
- .open = forthdrv_open,
- .read = forthdrv_read,
- .poll = forthdrv_poll,
- .release = forthdrv_close,
-
- };
- static int forthdrv_init(void)
- {
- int ret;
- int devt = FORTHDEV;
-
- ret = register_chrdev_region(devt, 1, "forthdrv");
- if (ret) {
- printk(KERN_ERR "Unable to register minors for forthdrv\n");
- goto fail;
- }
- class_register(&forthdrv_class);
- device_create(&forthdrv_class, NULL, devt, NULL, "buttons");
- cdev_init(&forthdrv_cdev, &forthdrv_ops);
- ret = cdev_add(&forthdrv_cdev, devt, 1);
- if (ret < 0)
- goto fail_cdev;
- return 0;
-
- fail_cdev:
- class_unregister(&forthdrv_class);
- device_destroy(&forthdrv_class, devt);
- cdev_del(&forthdrv_cdev);
- fail:
- unregister_chrdev_region(devt, 1);
-
- return 0;
- }
- static void forthdrv_exit(void)
- {
- class_unregister(&forthdrv_class);
- device_destroy(&forthdrv_class, FORTHDEV);
- cdev_del(&forthdrv_cdev);
- unregister_chrdev_region(FORTHDEV, 1);
- }
- module_init(forthdrv_init);
- module_exit(forthdrv_exit);
- MODULE_LICENSE("GPL");
这样在应用程序上,就不用一直read,在一定的时间内如果没有得到相应的事件通知,就返回。
应用程序会用到
int poll(struct pollfd *fds, nfds_t nfds, int timeout);
struct pollfd {
int fd; /* file descriptor */
short events; /* requested events */
short revents; /* returned events */
};
应用程序如下:
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <poll.h>
- int main(int argc, char *argv[])
- {
- int fd, ret, cnt=1;
- unsigned char val;
- struct pollfd fds[1];
- fd = open("/dev/buttons", O_RDWR);
- if (fd < 0)
- {
- printf("Can not open device\n");
- }
- fds[0].fd = fd; //监听的文件描述符
- fds[0].events = POLLIN; //有数据可读标志,与驱动保持一致
- while(1)
- {
- ret = poll(fds, 1, 5000); //5000毫秒超时
- if (0 == ret)
- {
- printf("A value of 0 indicates that the call timed out and no file descriptors were ready.\n");
- }
- else
- {
- read(fd, &val, 1);
- printf("cnt:%d, val:0x%x\n", cnt++, val);
- }
- }
- return 0;
- }
运行情况: