【韦东山驱动代码移植高通平台之七】通过poll机制读取按键值

通过poll机制读取按键值

poll允许进程在一个或者多个文件描述符上做无阻塞的读取和写入,这些调用也会阻塞进程,但不是阻塞在read或者write系统调用上

需要给file_operations添加poll函数

poll函数里通过poll_wait函数向poll_table添加一个等待队列,返回描述哪个操作可以立即执行的位掩码,比如POLLIN表明可以无阻塞地读取,POLLOUT可以无阻塞地写入

 

示例中是在中断函数里唤醒添加到poll table里的等待队列,然后poll函数会重新执行一下,然后返回立即可以执行的位掩码

 

参考代码

#include <linux/module.h>
#include <linux/init.h>
#include <linux/major.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/io.h>
#include <linux/sched.h>
//#include <asm/irq.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/poll.h>

#define TAG "keyvol "
#define INT_GPIO 91

static int key_major;
static struct cdev key_cdev;
static struct class *key_class;
volatile unsigned long *tlmm_gpio_cfg;
volatile unsigned long *tlmm_in_out;
static int key_press = 0;
static int key_value = 0;
static int irq;

static wait_queue_head_t kwait;

static irqreturn_t key_irq_thread(int irq, void *data)
{
	int value;

	printk(TAG" func:%s line:%d\n", __func__, __LINE__);
	value = *tlmm_in_out;
	value &= 0x1;
	if (value ) {
		key_value = 0;
	} else {
		key_value = 1;
	}
	wake_up_interruptible(&kwait);
	key_press = 1;

	return IRQ_HANDLED;
}
static ssize_t key_read(struct file *file, char __user *buffer,
		size_t count, loff_t *ppos)
{
	if(count != 1 )
		return -EINVAL;

	if((file->f_flags & O_NONBLOCK) && !key_press)
		return -EAGAIN;

	printk(TAG" func:%s line:%d\n", __func__, __LINE__);
	if (!key_press) {
		wait_event_interruptible(kwait, key_press);
	}
	if(key_press) {
		if(!copy_to_user(buffer, &key_value, 1)) {
			printk(TAG"%s key is press\n", __func__);
			key_press = 0;
		} else {
			printk(TAG"%s copy to user error\n", __func__);
			return -EFAULT;
		}
	}

	return count;
}

ssize_t key_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
{
	printk(TAG" func:%s line:%d\n", __func__, __LINE__);

	return count;
}

static int key_open(struct inode *inode, struct file *file)
{
	int ret;

	printk(TAG" func:%s line:%d\n", __func__, __LINE__);
	irq = gpio_to_irq(INT_GPIO);
	printk(TAG"%s irq is %d\n", __func__, irq);
	ret = request_threaded_irq(irq, NULL, key_irq_thread, IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING | IRQF_ONESHOT, "vol_key", NULL);
	printk(TAG"%s ret is %d\n", __func__, ret);

	return ret;
}

static unsigned int key_poll(struct file *file, poll_table *wait)
{
	unsigned int mask;

	printk(TAG" func:%s line:%d\n", __func__, __LINE__);
	poll_wait(file, &kwait, wait);
	if (key_press) {
		mask |= POLLIN | POLLRDNORM;
	}

	return mask;
}

static int key_release(struct inode *inode, struct file *file)
{
	printk(TAG" func:%s line:%d\n", __func__, __LINE__);
	free_irq(irq, NULL);

	return 0;
}

static const struct file_operations key_ops = {
	.owner = THIS_MODULE,
	.read = key_read,
	.write = key_write,
	.open = key_open,
	.poll = key_poll,
	.release = key_release,
};

static int my_key_init(void)
{
	int retval;
	dev_t dev_id;

	printk(TAG" func:%s line:%d\n", __func__, __LINE__);
	retval = alloc_chrdev_region(&dev_id, 0, 1, "key"); //0,1
	key_major = MAJOR(dev_id);
    printk(TAG"major is %d\n", key_major);
	if (retval < 0) {
		printk(TAG"can't get major number\n");
		goto error;
	}

	cdev_init(&key_cdev, &key_ops);
	retval = cdev_add(&key_cdev, dev_id, 1); //1
	if (retval < 0) {
		printk(TAG"cannot add cdev\n");
		goto cleanup_alloc_chrdev_region;
	}

	key_class = class_create(THIS_MODULE, "key");
	if (IS_ERR(key_class)) {
		printk(TAG "Error creating key class.\n");
		cdev_del(&key_cdev);
		retval = PTR_ERR(key_class);
		goto cleanup_alloc_chrdev_region;
	}

	device_create(key_class, NULL, MKDEV(key_major, 0), NULL, "keyvol");

	tlmm_gpio_cfg = (volatile unsigned long *)ioremap(0x105B000, 8);
	tlmm_in_out = tlmm_gpio_cfg + 1;
	*tlmm_gpio_cfg |= 0x3;

	init_waitqueue_head(&kwait);

	return 0;

cleanup_alloc_chrdev_region:
	unregister_chrdev_region(dev_id, 0);
error:
	return retval;
}

static void key_exit(void)
{
	dev_t dev_id = MKDEV(key_major, 0);

	iounmap(tlmm_gpio_cfg);
	device_destroy(key_class, MKDEV(key_major, 0));
	class_destroy(key_class);
	cdev_del(&key_cdev);  
	unregister_chrdev_region(dev_id, 0);
	printk(TAG" func:%s line:%d\n", __func__, __LINE__);
}

module_init(my_key_init);
module_exit(key_exit);
MODULE_LICENSE("GPL");

 

测试代码

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


int main(int argc, char **argv)
{
    int fd;
    char buf;
    struct pollfd pfd;

    if(argc != 2) {
    	printf("Usage: %s [node]\n", argv[0]);
    	exit(-1);
    }
    fd = open(argv[1], O_RDWR | O_NONBLOCK);
    if (fd < 0) {
    	printf("open %s failed\n", argv[1]);
    	exit(-1);
    }

    pfd.fd=fd;
    pfd.events = POLLIN;
    while (1) {  	
        if(poll(&pfd, 1, -1) > 0) {
        	read(fd, &buf, 1);
        	//printf("buf is %d\n", buf);
        	if (buf == 1) {
        		printf("key is press, buf is %d\n", buf);
        	} else () {
        		printf("key is release, buf is %d\n", buf);
            }
        }
    //usleep(500000);
    }

    close(fd);

    return 0;
}

假如编译出的测试程序为testkey_poll,运行测试程序

./testkey_poll /dev/keyvol

该测试程序大部分时间都是在poll函数里休眠,唤醒后会立即无阻塞读取

 

问题

当有数据准备就绪时,可以让驱动程序通知应用程序去读取吗?

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: poll机制是Linux驱动中一种用于实现I/O多路复用的机制。在使用poll机制时,驱动程序可以在多个文件描述符上同时等待可读、可写或异常事件的发生,从而避免了在相应的文件描述符上使用阻塞IO时CPU周期的浪费。poll机制的主要思路是向内核注册多个文件描述符,内核会将这些文件描述符上的I/O事件传递给用户程序,用户程序能够根据得到的事件类型继续执行下去。 ### 回答2: poll机制是Linux驱动程序中实现异步输入输出操作的一种方法,通过这种机制驱动程序可以将不同的设备进行组合,从而实现对这些设备进行统一的轮询操作。 在使用poll机制时,驱动程序需要提供一个poll方法,在该方法中,通过向用户空间返回一个特殊的文件描述符来表示设备状态的变化,然后用户空间通过读取这个描述符来判断设备是否有数据到达。如果有数据到达,用户空间再调用相应的读取方法来读取这些数据,从而实现异步读写。 在实现poll机制时,驱动程序通常需要调用内核提供的一些API接口,如file_operations结构体中的poll方法、wait_queue_head_t结构体、poll_table结构体等。 在使用poll机制时,用户空间可以通过调用poll函数来注册设备文件,并设置等待事件。当设备的状态改变时,内核会将读取操作挂起,并返回一个特殊的文件描述符。此时,用户空间可以通过调用相应的读取函数来读取数据,在读取完毕后,再次通过poll函数注册设备文件,等待下一次事件处理。 总之,poll机制可以帮助驱动程序实现异步输入输出操作,同时也可以帮助应用程序进行事件处理,提高了Linux的应用程序的可靠性和效率。 ### 回答3: poll机制是Linux内核提供的多路复用机制之一,它使得一个进程可以监视多个文件描述符上的读写情况。在进程需要同时监听多个文件描述符的读写情况时,若采用传统的阻塞式I/O方式,就需要使用多个线程或进程来处理,这种方式会导致系统资源的浪费。因此,poll机制使用,避免了上述情况发生。 poll机制最常用于网络编程中。在一个服务器程序中,可能需要同时监听多个客户端的连接请求。使用poll机制可以将这些客户端的连接描述符放入一个pollfd数组中,同时对这个数组进行监听,以便及时处理。 poll的基本原理是:进程调用poll系统调用时,内核会挂起该进程,直到poll所监听的文件描述符中有读写事件发生时,内核会唤醒进程,通过返回值告知进程哪些文件描述符发生了事件,并将该事件信息写入对应的pollfd结构体中。这样,进程就可以根据返回的信息对文件描述符进行相应的操作。 poll机制存在以下优点: 1.单个进程可以同时监视多个文件描述符的读写情况,可以更好的利用CPU资源。 2.和select相比,poll没有描述符数量限制,select有FD_SETSIZE的限制。 3.poll在内核中将打开文件描述符的等待队列挂起,直到设备就绪,因此在多处理器系统中poll的性能要优于select。 4.使用poll不需要重新初始化文件描述符,不需要进行文件描述符拷贝操作。这使得poll使用比较方便。 总之,poll机制是Linux系统中非常重要的一个多路复用机制,它极大地提高了系统的效率和可用性。在进行网络编程时,我们常常需要使用poll机制来提高代码的性能和实用性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值