linux input event简单使用

最近需要在kernel  的驱动里边将事件传到用户空间,打算用input子系统做,用户空间开一个线程用epoll方式来读取数据,

记录一下如何快速简单使用input子系统上报事件。

//定义一个input_dev 类型结构体变量
static struct input_dev* test_input_dev = NULL;

//创建并初始化	
test_input_dev  = input_allocate_device();
if (test_input_dev == NULL) {
	printk(KERN_ERR"failed to allocate test input device\n");
	return -1;
}
test_input_dev->name = "test_input_event";
set_bit(EV_MSC,  test_input_dev->evbit);
set_bit(MSC_RAW, test_input_dev->mscbit);

//注册节点
if (input_register_device(test_input_dev)) {
	pr_err("%s: failed to register test input device\n", __func__);
	input_free_device(test_input_dev);
	return -1;
}

/*逻辑处理*/
//上报事件
input_event(test_input_dev, EV_MSC, MSC_RAW, value);
input_sync(test_input_dev);


//随驱动取消注册
input_unregister_device(test_input_dev);
input_free_device(test_input_dev);

关于为何要用EV_MSC类型,是因为这次上报事件的类型input里边没有定义,并且有时若你要上报的值范围不在input子系统对应类型规定的值范围内会出现值报不上去的情况,所以干脆用EV_MSC比较好。另外一个原因就是以前在Android 8.1(高通骁龙845平台,kernel 4.9)上用ABS类型可以报上去的值,到了android 9.0(同样高通骁龙845平台,kernel 4.9)就报不上去了,具体原因还没有去深究。

简单跟了一下源码,为何报不上去,应该是初始化的时候需要设定一个上报值的范围,后面调用input_event()上报事件的时候有一个过滤检测:

//android/kernel/msm-4.9/drivers/input/input.c

void input_event(struct input_dev *dev,
		 unsigned int type, unsigned int code, int value)
{
	unsigned long flags;

	if (is_event_supported(type, dev->evbit, EV_MAX)) {

		spin_lock_irqsave(&dev->event_lock, flags);
		input_handle_event(dev, type, code, value);
		spin_unlock_irqrestore(&dev->event_lock, flags);
	}
}


static inline int is_event_supported(unsigned int code,
				     unsigned long *bm, unsigned int max)
{
	return code <= max && test_bit(code, bm);
}

 

 

 

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值