字符设备驱动程序之按键——poll机制

本节里我们在按键中断机制的基础上添加了poll机制来优化程序
我们知道,应用程序中的open、read、write函数会调用内核里的sys_open、sys_read、sys_write函数,而内核里的这些函数又会对应到驱动程序里的.open、.read、.write函数。我们的poll机制也不例外,用户空间里可以采用poll函数或者select函数,他们都会调用到内核空间的sys_poll函数,而sys_poll函数最后又会对应到驱动程序里的.poll函数。下面我们从sys_poll开始来分析poll机制,我们只列出分析的框架:
app:poll
kernel: sys_poll(struct pollfd __user *ufds, unsigned int nfds, long timeout_msecs)
timeout_jiffies = msecs_to_jiffies(timeout_msecs);    //配置超时时间
do_sys_poll(ufds, nfds, &timeout_jiffies);
poll_initwait(&table); //初始化等待队列
init_poll_funcptr(&pwq->pt, __pollwait);
pt->qproc = qproc;     //table->pt->qproc= __pollwait;,见注释1-2
do_poll(nfds, head, &table, timeout);
for (;;)
{
for (; pfd != pfd_end; pfd++) //针对多个进程
  {
if (do_pollfd(pfd, pt))   //见注释1-1
 {
  count++; pt = NULL;
  }
}
/*若count不为0或者超时或者有事件发生则会跳出死循环*/
if (count || !*timeout || signal_pending(current))
           break;
/*若count为0且未超时且无事件发生则会休眠__timeout时间*/
schedule_timeout(__timeout);
}
注释1-1:
do_pollfd(pfd, pt)
     mask = file->f_op->poll(file, pwait); //貌似调用了驱动程序中的poll函数,若poll函数返回值不为0则会使count++
注释1-2:
__pollwait函数中有这么一句: entry->wait_address = wait_address; 用于将当前进程挂到等待队列中, table->pt-> qproc=   __pollwait就说明 table->pt-> qproc函数可以将 当前进程挂到等待队列中 

以上是从sys_poll出发分析出的框架,下面我们可以从一个驱动中poll函数的例子出发在来分析一下,希望可以把跟上面相衔接上,先来看代码:
static unsigned forth_drv_poll(struct file *file, poll_table *wait)
{
unsigned int mask = 0;
poll_wait(file, &button_waitq, wait);  // 不会立即休眠

if (ev_press)
mask |= POLLIN | POLLRDNORM;

return mask;
}
分析如下:
poll_wait(file, &button_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); //注意这个东东,请看注释2-1
}
注释2-1:
注释1-2中,我们曾经分析出了qproc函数的作用是 当前进程挂到等待队列中  ,于是我们就知道了poll_wait函数是将当前进程挂到等待队列 button_waitq中 。要注意现在只是加入等待队列,但是并没有出现休眠。
OK!这样我们就联系起来了。总结一下下:
从sys_poll出发,首先定义了 qproc函数,然后调用驱动程序里的poll函数,首先将等待队列添加到等待队列列表中,然后判断是否有按键按下,如果有按键按下,则返回为非0。这时就会跳出死循环,如果没有按键按下的话返回为0,这时如果没有其他事件发生,就会进入休眠,休眠一段时间后,会重新循环,但此时经出现超时,所以会跳出循环,返回应用程序。
下面我们来看一下完整的驱动程序代码:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/irq.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <linux/poll.h>

#define DEVICE_NAME "keys"

volatile unsigned long *gpfcon=NULL;
volatile unsigned long *gpfdat=NULL;
volatile unsigned long *gpgcon=NULL;
volatile unsigned long *gpgdat=NULL;

static struct class *keys_class;
static struct class_device *keys_class_devs;

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

static unsigned char key_val;
struct pin_desc pins_desc[4] = {
{S3C2410_GPF0, 0x01},
{S3C2410_GPF2, 0x02},
{S3C2410_GPG3, 0x03},
{S3C2410_GPG11, 0x04},
};

static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

static volatile int ev_press = 0;
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_RETVAL(IRQ_HANDLED);
}

static int s3c24xx_keys_open(struct inode *inode, struct file *file)
{
    request_irq(IRQ_EINT0,  buttons_irq, IRQT_BOTHEDGE, "S2",&pins_desc[0]);
    request_irq(IRQ_EINT2,  buttons_irq, IRQT_BOTHEDGE, "S3",&pins_desc[1]);
    request_irq(IRQ_EINT11,buttons_irq, IRQT_BOTHEDGE, "S4",&pins_desc[2]);
    request_irq(IRQ_EINT19,buttons_irq, IRQT_BOTHEDGE, "S5",&pins_desc[3]);
    return 0;
}

static int s3c24xx_keys_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{
       copy_to_user(buff, &key_val, 1);
        ev_press=0;
        return 1;
}


int s3c24xx_keys_close(struct inode *inode, struct file *file)
{
    free_irq(IRQ_EINT0,  &pins_desc[0]);
    free_irq(IRQ_EINT2,  &pins_desc[1]);
    free_irq(IRQ_EINT11,&pins_desc[2]);
    free_irq(IRQ_EINT19,&pins_desc[3]);
    return 0;
}

static unsigned s3c24xx_keys_poll(struct file *file, poll_table *wait)
{
unsigned int mask = 0;
poll_wait(file, &button_waitq, wait); 

if (ev_press)
mask |= POLLIN | POLLRDNORM;

return mask;
}

static struct file_operations s3c24xx_keys_fops = {
    .owner  =   THIS_MODULE,   
    .open   =   s3c24xx_keys_open,     
    .read = s3c24xx_keys_read,   
    .release = s3c24xx_keys_close,
    .poll    =  s3c24xx_keys_poll,
};
int major;
static int __init s3c24xx_keys_init(void)
{
    major=register_chrdev(0, DEVICE_NAME, &s3c24xx_keys_fops);//auto distribute major
    keys_class = class_create(THIS_MODULE, "keys_class");
    keys_class_devs = class_device_create(keys_class, NULL, MKDEV(major, 0), NULL, "keys");
    gpfcon=(volatile unsigned long *)ioremap(0x56000050,16);
    gpfdat=gpfcon+1;
    gpgcon=(volatile unsigned long *)ioremap(0x56000060,16);
    gpgdat=gpgcon+1;
     printk(DEVICE_NAME " initialized\n");
     return 0;
}

static void __exit s3c24xx_keys_exit(void)
{
    unregister_chrdev(major, DEVICE_NAME);
    class_device_unregister(keys_class_devs);
    class_destroy(keys_class);
    iounmap(gpfcon);
    iounmap(gpgcon);
}
module_init(s3c24xx_keys_init);
module_exit(s3c24xx_keys_exit);
MODULE_LICENSE("GPL");

测试程序:
#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;
unsigned char key_val;
int ret;

struct pollfd fds[1];
fd = open("/dev/keys", 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%x\n", key_val);
}
}
return 0;
}
本程序实现的功能和上一节相同,不同的是,上一节我们是在read函数中,根据按键是否被按下判断是否进入休眠。而本节中我们引入了poll机制,poll函数自动完成将当前进程加入等待队列,这是还不会进入休眠,然后会判断是否有按键按下,如果有按键按下,这是返回值为非0,不会进入休眠,如果没有按键按下,则返回值为0,就会进入休眠。在休眠过程中,如果发生中断的话,就会唤醒进程,返回应用程序继续执行。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值