字符设备驱动之按键驱动(poll机制实现)

        一般来说上层调用poll(),到了内核层会调用sys_poll(),类似的还有open->sys_open、read->sys_read(),所以类比于open和read,驱动程序中也有一个poll调用。       

       之前用了轮询和中断的方式来实现,现在用poll机制来实现。poll机制有个好处就是能超时处理,当超过一定的时间后就能自动返回。下面我们看一下poll机制的流程是什么样的。

下面的源码是基于Linux 2.6版本的源码:

app: poll(struct pollfd fd[], nfds_t nfds, int timeout);
      |
     \|/
*********************
kernel:
    sys_poll:
        do_sys_poll(ufds, nfds, &timeout_jiffies);
            poll_initwait(&table); //从名字可以看出是初始化等待
                init_poll_funcptr(&pwq->pt, __pollwait);
                    pt->qproc = qproc; 相当于 table->pt->qproc = __pollwait
            do_poll(nfds, head, &table, timeout);
                for (;;)
                {
                    for (; pfd != pfd_end; pfd++) { //poll机制可以查询多个驱动程序
                        if (do_pollfd(pfd, pt)) --> 调用驱动程序中的poll函数:mask = file->f_op->poll(file, pwait); return mask;
                                                    //驱动中会调用poll_wait(),然后会调用到下面的函数
                                                    p->qproc(filp, wait_address, p);
                                                    相当于
                                                    __pollwait(filp, &button_waitq, p)
                                                        init_waitqueue_entry(&entry->wait, current);//current就指向我们的当前进程
                                                        add_wait_queue(&button_waitq, &entry->wait);//把当前进程加入等待button_waitq队列
                                                        
                        {
                            count++; //如果do_pollfd返回1,这里才加一
                            pt = NULL;
                        }
                    }
                    
                    //break的条件:count非0,超时,有信号在等待处理
                    if (count || !*timeout || signal_pending(current))
                        break;
                    
                    //开始休眠__timeout这么多时间
                    __timeout = schedule_timeout(__timeout);
                }

       根据上面的流程分析,其实当我们的驱动程序中的poll调用poll_wait()时,就是把我们的进程挂到休眠队列中,当有中断来的时候才从休眠队列中唤醒我们的进程。下面就看一下我们的驱动程序怎么实现利用poll机制来监控按键事件。

#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 <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <linux/poll.h>

static int major;
static struct class *forthdrv_class;
static struct class_device *forthdrv_class_dev;

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

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

static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

/*中断事件标识,中断服务程序将它置为1,forth_drv_read将它清零*/
static volatile int ev_press = 0;

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

/*键值:按下时,0x01 0x02 0x03 0x04 */
/*键值:松开时,0x81 0x82 0x83 0x84 */
static unsigned char key_val;

struct pin_desc pins_desc[4] = {
	{S3C2410_GPG0, 0x01},	
	{S3C2410_GPG2, 0x02},	
	{S3C2410_GPG3, 0x03},		
	{S3C2410_GPG11, 0x04},
};

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 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;
}

static int forth_drv_open(struct inode *inode, struct file *file)
{
	//GPF0,2配置为输入引脚
	//GPG3,11配置为输入引脚
	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 forth_drv_read(struct file *file, const __user *buf, size_t count, loff_t *ppos)
{
	if (count != 1)
		return -EINVAL;

	/*如果没有按键动作,就休眠,
	  *当ev_press为0就进入休眠
	  */
	wait_event_interruptible(button_waitq, ev_press);

	/*如果没有按键动作,返回键值*/
	copy_to_user(buf, &key_val, 1);
	ev_press = 0;
	return 1;
}

static int forth_drv_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]);
}

static struct file_operations forth_fops = {
	.owner   = THIS_MODULE,
	.open    = forth_drv_open,
	.read    = forth_drv_read,
	.release = forth_drv_close,
	.poll    = forth_drv_poll,
};

static int forth_drv_init(void)
{
	major = register_chrdev(0, "forth_drv", &forth_fops);
	forthdrv_class = class_create(THIS_MODULE, "forth_drv");
	forthdrv_class_dev = class_device_create(forthdrv_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;
}

void forth_drv_exit(void)
{
	unregister_chrdev(major, "forth_drv");
	class_device_unregister(forthdrv_class_dev);
	class_destroy(forthdrv_class);
	iounmap(gpfcon);
	iounmap(gpgcon);
}

module_init(forth_drv_init);
module_exit(forth_drv_exit);
MODULE_LICENSE("GPL");

       从上面的代码可以看出来,大部分和我上一个利用中断方式的检测按键的代码差不多。这里主要实现了forth_drv_poll(),并且在file_operations中多了一个poll接口,就是这么简单,就能给上层应用通过调用poll()函数来实现超时操作。这里要注意的是,由文章开头分析的,当我们的驱动程序中的poll函数返回非0才表示有按键事件发生,如果没有按键来临的时候,我们需要返回0。

       下面我们给出上层的测试函数的代码:


#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>


int main(int argc, char **argv)
{
	unsigned char key_val;
	int fd;
	int ret;
	struct pollfd fds[1]; //这里只检测一个文件
	
	fd = open("/dev/buttons", O_RDWR);
	if (fd < 0)
	{
		printf("open fd failed\n");
		return 0;
	}

	fds[0].fd     = fd;
	fds[0].events = POLLIN; //POLLIN :There is data to read.
	while (1)
	{
		//int poll(struct pollfd *fds, nfds_t nfds, int timeout);
		ret = poll(fds, 1, 5000); // 5000ms
		if (0 == ret)
		{
			printf("time out\n");
		}
		else
		{
			read(fd, &key_val, 1);
			printf("key_val = 0x%x\n", key_val);
		}
	}
	
	return 1;
}

当poll返回0就代表没有按键事件发生,否则就代表按了按键,并且返回了数据。而且这种机制占用的资源也不多:

  PID  PPID USER     STAT   VSZ %MEM %CPU COMMAND
  802   772 0        R     3096   5%   0% top 
  771     1 0        S    15984  26%   0% /opt/Qtopia/bin/qpe 
  786   771 0        S N  11492  19%   0% /opt/Qtopia/bin/quicklauncher 
  785   771 0        S <   9044  15%   0% /opt/Qtopia/bin/qss 
  772     1 0        S     3096   5%   0% -sh 
    1     0 0        S     3092   5%   0% init     
  801   772 0        S     1312   2%   0% ./forthDrvTest

猜你喜欢

转载自blog.csdn.net/lee_jimmy/article/details/82975435
今日推荐