最近需要在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);
}