evdev 输入事件驱动,为输入子系统提供了一个默认的事件处理方法。其接收来自底层驱动的大多数事件,并使用相应的逻辑对其进行处理。
1 evdev驱动初始化
1.1 evdev驱动加载
static int __init evdev_init(void)
{
return input_register_handler(&evdev_handler);
}
static void __exit evdev_exit(void)
{
input_unregister_handler(&evdev_handler);
}
module_init(evdev_init);
module_exit(evdev_exit);
1.2 evdev驱动注册
老样子,通过注释来分析流程:
int input_register_handler(struct input_handler *handler)
{
mutex_lock_interruptible(&input_mutex); // 加锁,睡眠可以被信号打断
INIT_LIST_HEAD(&handler->h_list); // 初始化evdev_handler的h_list链表
if (handler->fops != NULL) {
if (input_table[handler->minor >> 5]) { // **问题1. 为什么这里要右移5位**
retval = -EBUSY;
goto out;
}
input_table[handler->minor >> 5] = handler;
}
list_add_tail(&handler->node, &input_handler_list); // 将evdev_handler放入input子系统的全局handler链表中
list_for_each_entry(dev, &input_dev_list, node) // 遍历input子系统的所有input设备,通过handler去匹配dev
input_attach_handler(dev, handler); // 下面详细分析
input_wakeup_procfs_readers(); // 唤醒队列,不细说
out:
mutex_unlock(&input_mutex);
return retval;
}
EXPORT_SYMBOL(input_register_handler);
input_register_handler主要做了几件事:
- 初始化handler的h_list链表,该链表用来找到匹配后的input设备。
- 将handler加入input_handler_list全局链表的链尾。
- 遍历input dev,匹配设备
匹配的流程,参考上篇文章 “input子系统分析一(input设备注册)”,这里面已经说的很详细了。
2 evdev事件驱动的fops分析
3 问题解答
3.1 为什么这里要右移5位?
if (input_table[handler->minor >> 5]) { // **问题1. 为什么这里要右移5位**