【韦东山驱动代码移植高通平台之八】通过异步通知读取按键值

博客介绍了通过异步通知读取按键值的方法。阻塞和非阻塞组合及select、poll方法查询设备有时效率不高,而异步通知可让进程在数据有效时收到信号。使用异步通知需在驱动的file_operations添加fasync函数,数据到达时用kill_fasync通知进程,还给出参考代码和测试程序编写方法。

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

通过异步通知读取按键值

阻塞型和非阻塞型组合以及select和poll方法可以有效地查询设备,但是在有的时候效率就不高了。

假设一个进程在低优先级执行很长的计算,但又需要尽快地处理输入数据。可以让程序周期性地调用poll方法,但是在很多场景下还有更好的办法。通过使用异步通知,进程可以在数据有效时收到一个信号,而不需要不停地使用poll来关注数据。

为了可以使用异步通知,需要在驱动的file_operations添加fasync函数。当一个打开文件的FASYNC标志修改时,fasync函数会被调用。

示例中在fasync函数里调用fasync_helper将在相关进程列表中增加或者删除文件。

数据到达时,使用kill_fasync通知相关进程

 

参考代码

#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 struct fasync_struct *fasync;

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);
	kill_fasync(&fasync, SIGIO, POLL_IN);
	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_press, 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_fasync(int fd, struct file *file, int on)
{
	printk(TAG" func:%s line:%d\n", __func__, __LINE__);

	return fasync_helper(fd, file, on, &fasync);
}


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,
	.fasync = key_fasync,
	.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");

 

测试程序编写

进程使用fcntl系统调用执行F_SETOWN命令,将进程的ID保存在filp->fowner里,目的是让内核知道该通知哪个进程,然后用fcntl设置FASYNC标志,通过F_SETFL来完成。

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

static char key_val;
static int fd;

void catch_signal_fun(int signum)
{
	unsigned char key_val;
	read(fd, &key_val, 1);
	printf("key_val: 0x%x, signum is %d\n", key_val, signum);
}


int main(int argc, char **argv)
{
    int oflags;

    if(argc != 2) {
    	printf("Usage: %s [node]\n", argv[0]);
    	exit(-1);
    }

    signal(SIGIO, catch_signal_fun);

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

    fcntl(fd, F_SETOWN, getpid());
    oflags = fcntl(fd, F_GETFL);
    fcntl(fd, F_SETFL, oflags | FASYNC);

    while (1) {
    sleep(10000);
    }

    close(fd);

    return 0;
}

 

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

./testkey_sig /dev/keyvol

按键按下和松开的时候都会打印出按键的值

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值