驱动程序之_1_字符设备_3_poll机制

本文深入探讨了驱动程序中的poll机制,阐述了其在减少CPU资源占用方面的作用,详细介绍了poll机制的基本调度流程,以及在应用程序和驱动程序层面的具体实现方式。通过实例,展示了poll机制在字符设备驱动中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

驱动程序之_1_字符设备_3_poll机制

本文主要介绍驱动程序的poll机制
poll机制的主要作用是:把当前的文件挂到设备内部定义的等待队列中,等待唤醒或者超时,由此减少占用cpu资源

基本调度关系:
在应用程序中调用poll
poll调用sys_poll
sys_poll调用do_sys_poll
do_sys_poll调用do_poll
do_poll调用do_pollfd
do_pollfd调用我们的驱动程序实现的my_irq_drv_poll,my_irq_drv_poll调用poll_wait设置参数,然后返回do_poll,再进程休眠

在驱动程序中
1、在文件结构体中定义了poll函数:my_irq_drv_poll
2、在poll函数实现中,根据应用程序要求挂入等待队列,此时进程并没有马上休眠,而是在返回do_poll函数后才休眠
3、当按键按下,进入中断服务程序,唤醒进程,进入中断服务程序,退出再回到poll,再返回到应用程序
4、当到达超时时间,进程同样也被唤醒,从poll返回到应用程序

在应用程序中
1、定义了一个pollfd类型结构体,指定了使用poll机制的文件和处理事件(通过设置事件标志符)
2、调用poll函数,指定需要挂入队列的文件、文件数、超时时间5s
3、当进程被唤醒,应用程序根据返回的掩码,判断是否有数据可读,没有就是超时,有就是按键中断,若有,则进入read函数,读取键值

事件标志符如下表
在这里插入图片描述

附上驱动程序和测试用的应用程序代码

驱动程序

#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>
#include <linux/device.h>
#include <linux/irq.h>

static struct class *my_irq_class;
static struct class_device *my_irq_class_device;

static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
static volatile int ev_press = 0;

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

struct pin_desc key_pins_desc[4]={
	{S3C2410_GPF0,0x01},
	{S3C2410_GPF2,0x02},
	{S3C2410_GPG3,0x03},
	{S3C2410_GPG11,0x04},
};

static unsigned char key_val;
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_HANDLED;
}

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

	return 0;
}

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

ssize_t my_irq_drv_read(struct file *file, char __user *buf, size_t size, loff_t * ppos)
{
	if(size != 1)
		return -EINVAL;

	wait_event_interruptible(button_waitq,ev_press);	//不会立即休眠

	ev_press = 0;
	
	copy_to_user(buf,&key_val,1);

	return 0;
}

static unsigned int my_irq_drv_poll(struct file *filp, poll_table *wait)
{
	unsigned int mask = 0;
	
	poll_wait(filp,&button_waitq,wait);
	if(ev_press)
		mask |= POLLIN | POLLRDNORM;

	return mask;
}

static struct file_operations irq_drv_fops = {
	.owner = THIS_MODULE,
	.open = my_irq_drv_open,
	.read = my_irq_drv_read,
	.release = my_irq_drv_close,
	.poll = my_irq_drv_poll,
};

static int major;
static int my_irq_drv_init(void)
{
	major = register_chrdev(0,"my_irq_drv",&irq_drv_fops);
	my_irq_class = class_create(THIS_MODULE,"my_irq_drv");
	my_irq_class_device = class_device_create(my_irq_class,NULL,MKDEV(major,0),NULL,"key_irq_drv");

	return 0;
}

static void my_irq_drv_exit(void)
{
	unregister_chrdev(major,"my_irq_drv");
	class_device_unregister(my_irq_class_device);
	class_destroy(my_irq_class);
}

module_init(my_irq_drv_init);
module_exit(my_irq_drv_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 *args[])
{
	int fd;
	unsigned char key_val;
	int ret;
	struct pollfd fds[1];
	
	fd = open("/dev/key_irq_drv",O_RDWR);
	if(fd < 0)
	{
		printf("can't open\r\n");
		return 0;
	}
	printf("open successfully\r\n");

	fds[0].fd = fd;
	fds[0].events = POLLIN;

	while(1)
	{
		ret = poll(fds,1,5000);	
		if(ret == 0)
		{	
			printf("time out\r\n");
		}
		else
		{
			read(fd,&key_val,1);		
			printf("press key : 0x%02x\r\n",key_val);
		}
	}

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值