Input系统由驱动层、核心层和事件处理层三部分组成。如下是一张事件的处理顺序图,图中驱动层部分捕捉到事件输入,如按键、移动鼠标等,然后将事件传递至核心层,接着核心层将事件交由事件处理层处理,最后再传递至用户空间层。
在内核中,input_dev表示一个input设备,input_handler表示input设备的接口,所有的input_dev都会使用双向链表input_dev_list连起来,在设备驱动中,可以使用input_register_device将input_dev加入这个链表中。而所有的input_handler也使用双向链表input_handler_list链接起来,在调用input_register_handler的时候会将input_handler加入链表中。而input_dev和input_handler都需要彼此关联,如同device和driver一样,当注册新的input_dev时,就会遍历input_handler_list,找到匹配的input_handler,然后调用input_handler的connect函数将它们联系起来,而注册input_handler时也会遍历input_dev_list,然后将dev和handler联系起来。而在input_handler的connect函数中,会创建input_handle结构体,input_handle就负责将input_dev和input_handler联系在一起。
当设备产生一个输入事件,如按下按键,驱动程序就会调用匹配的input_handler中的event函数记录该事件,当用户层需要获取该事件时,就会调用input_handler中的文件操作函数fops来获取这个事件,例如read。
下面来介绍input子系统的具体实现。
首先是input设备的注册函数input_register_device
- int input_register_device(struct input_dev *dev)
- {
- static atomic_t input_no = ATOMIC_INIT(0);
- struct input_handler *handler;
- const char *path;
- int error;
- /* Every input device generates EV_SYN/SYN_REPORT events. */
- __set_bit(EV_SYN, dev->evbit); // evbit表示该设备支持的事件,每个设备都支持该事件,
- // EV_SYN表示事件结束之后的同步事件
- /* KEY_RESERVED is not supposed to be transmitted to userspace. */
- __clear_bit(KEY_RESERVED, dev->keybit); // keybit表示事件的按键类型
- /* Make sure that bitmasks not mentioned in dev->evbit are clean. */
- input_cleanse_bitmasks(dev);
- if (!dev->hint_events_per_packet)
- dev->hint_events_per_packet = input_estimate_events_per_packet(dev);
- /*
- * If delay and period are pre-set by the driver, then autorepeating
- * is handled by the driver itself and we don't do it in input.c.
- */ // 主要用于处理重复按键
- init_timer(&dev->timer);
- if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
- dev->timer.data = (long) dev;
- dev->timer.function = input_repeat_key;
- dev->rep[REP_DELAY] = 250;
- dev->rep[REP_PERIOD] = 33;
- }
- if (!dev->getkeycode)
- dev->getkeycode = input_default_getkeycode;
- if (!dev->setkeycode)
- dev->setkeycode = input_default_setkeycode;
- dev_set_name(&dev->dev, "input%ld",
- (unsigned long) atomic_inc_return(&input_no) - 1);
- error = device_add(&dev->dev); // 注册device
- if (error)
- return error;
- path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
- pr_info("%s as %s\n",
- dev->name ? dev->name : "Unspecified device",
- path ? path : "N/A");
- kfree(path);
- error = mutex_lock_interruptible(&input_mutex);
- if (error) {
- device_del(&dev->dev);
- return error;
- }
- // 增加设备至input_dev_list中
- list_add_tail(&dev->node, &input_dev_list);
- // 遍历input_handler_list,寻找匹配的input_handler,使用input_attach_handler匹配
- list_for_each_entry(handler, &input_handler_list, node)
- input_attach_handler(dev, handler);
- input_wakeup_procfs_readers();
- mutex_unlock(&input_mutex);
- return 0;
- }
- EXPORT_SYMBOL(input_register_device);
在注册函数input_register_device中,主要是对input_dev进行设置,然后将input_dev加入input_dev_list链表中,最后就遍历input_handler_list链表,使用input_attach_handler函数进行匹配,下面看看input_attach_handler函数的实现。
- static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
- {
- const struct input_device_id *id;
- int error;
- id = input_match_device(handler, dev);
- if (!id)
- return -ENODEV;
- error = handler->connect(handler, dev, id);
- if (error && error != -ENODEV)
- pr_err("failed to attach handler %s to device %s, error: %d\n",
- handler->name, kobject_name(&dev->dev.kobj), error);
- return error;
- }
该函数会首先使用input_match_device函数进行匹配操作,如果匹配成功,则使用handler->connect回调函数将handler和dev联系起来。下面看看input_match_device的实现。
- static const struct input_device_id *input_match_device(struct input_handler *handler,
- struct input_dev *dev)
- {
- const struct input_device_id *id;
- int i;
- for (id = handler->id_table; id->flags || id->driver_info; id++) {
- // 比较dev和handler的总线类型、设备厂商、设备号和设备版本
- if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
- if (id->bustype != dev->id.bustype)
- continue;
- if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
- if (id->vendor != dev->id.vendor)
- continue;
- if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
- if (id->product != dev->id.product)
- continue;
- if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
- if (id->version != dev->id.version)
- continue;
- // 比较dev和handler的各个比特项,如果有1项不同,则匹配失败
- MATCH_BIT(evbit, EV_MAX);
- MATCH_BIT(keybit, KEY_MAX);
- MATCH_BIT(relbit, REL_MAX);
- MATCH_BIT(absbit, ABS_MAX);
- MATCH_BIT(mscbit, MSC_MAX);
- MATCH_BIT(ledbit, LED_MAX);
- MATCH_BIT(sndbit, SND_MAX);
- MATCH_BIT(ffbit, FF_MAX);
- MATCH_BIT(swbit, SW_MAX);
- // 如果上述全部匹配成功,则再使用handler的match,如果也匹配成功,则返回id,表明完全匹配成功
- if (!handler->match || handler->match(handler, dev))
- return id;
- }
- return NULL;
- }
函数input_match_device会匹配dev和handler的总线类型、设备厂商、设备号、设备版本,然后匹配各个比特项,最后使用handler的match进行匹配,全部成功后即匹配成功。
总之,在input设备的注册函数中,会将设备加入设备链表,然后就遍历handler链表进行匹配,如果完全匹配成功则调用handler的connect函数将input_dev和input_handler联系起来。
接下来分析handler的注册函数input_register_handler。
- int input_register_handler(struct input_handler *handler)
- {
- struct input_dev *dev;
- int retval;
- retval = mutex_lock_interruptible(&input_mutex);
- if (retval)
- return retval;
- INIT_LIST_HEAD(&handler->h_list);
- // 如果存在文件操作函数fops,则将handler加入input_table中
- if (handler->fops != NULL) {
- if (input_table[handler->minor >> 5]) {
- retval = -EBUSY;
- goto out;
- }
- input_table[handler->minor >> 5] = handler;
- }
- // 将handler加入input_handler_list链表中
- list_add_tail(&handler->node, &input_handler_list);
- // 遍历input_dev_list链表,匹配dev和handler
- list_for_each_entry(dev, &input_dev_list, node)
- 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是否存在文件操作函数fops,如果存在则将handler加入input_table中,然后会将handler加入input_handler_list链表中,最后再遍历input_dev_list,使用input_attach_handler来匹配dev和handler。
当使用input_register_device或者input_register_handler注册dev或handler时,都会遍历链表进行匹配操作,如果匹配成功会调用handler的connect函数,在该函数中会注册一个handle来将dev和handler联系起来,下面看看handle的注册函数input_register_handle。
- int input_register_handle(struct input_handle *handle)
- {
- struct input_handler *handler = handle->handler;
- struct input_dev *dev = handle->dev;
- int error;
- /*
- * We take dev->mutex here to prevent race with
- * input_release_device().
- */
- error = mutex_lock_interruptible(&dev->mutex);
- if (error)
- return error;
- /*
- * Filters go to the head of the list, normal handlers
- * to the tail.
- */ // 将handle加入dev链表上
- if (handler->filter)
- list_add_rcu(&handle->d_node, &dev->h_list);
- else
- list_add_tail_rcu(&handle->d_node, &dev->h_list);
- mutex_unlock(&dev->mutex);
- /*
- * Since we are supposed to be called from ->connect()
- * which is mutually exclusive with ->disconnect()
- * we can't be racing with input_unregister_handle()
- * and so separate lock is not needed here.
- */ // 将handle加入handler链表上
- list_add_tail_rcu(&handle->h_node, &handler->h_list);
- // 如果handle有start回调函数,则调用start回调函数
- if (handler->start)
- handler->start(handle);
- return 0;
- }
- EXPORT_SYMBOL(input_register_handle);
该注册函数input_register_handle主要是将handle加入对应的input_dev的h_list上和对应的input_handler的h_list上,如果handle有start回调函数则调用handle的回调函数start。
以上就是dev、handler和handle的注册函数,通过上述3个注册函数可以建立这三个结构体的关系,有了这个对应关系,就可以了解到event的处理过程,下面来进行分析。
在驱动层中如果接收到某个事件的中断,就会使用input_report_xxx函数(input_report_key、input_report_rel等等)来通知input子系统,然后使用input_sync来通知事件结束,而函数input_report_xxx和input_sync都是调用input_event来完成通知的,下面来看看input_event。
- 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);
- add_input_randomness(type, code, value);
- input_handle_event(dev, type, code, value); // 传递事件
- spin_unlock_irqrestore(&dev->event_lock, flags);
- }
- }
- EXPORT_SYMBOL(input_event);
input_event方法首先判断设备产生的这个事件是否合法,如果合法则调用input_handle_event传递事件。
- static void input_handle_event(struct input_dev *dev,
- unsigned int type, unsigned int code, int value)
- {
- int disposition = INPUT_IGNORE_EVENT;
- switch (type) {
- case EV_SYN:
- ......
- break;
- case EV_KEY:
- ......
- break;
- case EV_SW:
- ......
- break;
- case EV_ABS:
- ......
- break;
- case EV_REL:
- ......
- break;
- case EV_MSC:
- ......
- break;
- case EV_LED:
- ......
- break;
- case EV_SND:
- ......
- break;
- case EV_REP:
- ......
- break;
- case EV_FF:
- ......
- break;
- case EV_PWR:
- ......
- break;
- }
- if (disposition != INPUT_IGNORE_EVENT && type != EV_SYN)
- dev->sync = false;
- if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)
- dev->event(dev, type, code, value);
- if (disposition & INPUT_PASS_TO_HANDLERS)
- input_pass_event(dev, type, code, value);
- }
在input_handle_event函数中,会首先根据事件的类型来设置disposition的值,disposition有四个选项,INPUT_IGNORE_EVENT忽视事件,INPUT_PASS_TO_HANDLERS传递事件至handler,INPUT_PASS_TO_DEVICE传递事件至device,INPUT_PASS_TO_ALL传递事件至handler和device。如果disposition为INPUT_PASS_TO_DEVICE则调用dev的event函数,如果disposition为INPUT_PASS_TO_HANDLERS则调用input_pass_event函数,下面分析input_pass_event函数。
- static void input_pass_event(struct input_dev *dev,
- unsigned int type, unsigned int code, int value)
- {
- struct input_handler *handler;
- struct input_handle *handle;
- int i;
- bool not_gsensor = true;
- ......
- rcu_read_lock();
- handle = rcu_dereference(dev->grab);
- if (handle)
- handle->handler->event(handle, type, code, value);
- else {
- bool filtered = false;
- list_for_each_entry_rcu(handle, &dev->h_list, d_node) {
- if (!handle->open)
- continue;
- handler = handle->handler;
- if (!handler->filter) {
- if (filtered)
- break;
- handler->event(handle, type, code, value);
- } else if (handler->filter(handle, type, code, value))
- filtered = true;
- }
- }
- rcu_read_unlock();
- }
函数input_pass_event会先判断dev是否强制指定了handle,如果有则调用handle->handler的event函数,如果没有则遍历dev的h_list上的所有handle再调用handle->handler的event函数。
下面来具体的看看handler是如何connect、event之类的操作的,在kernel/drivers/input/文件夹下有evdev.c文件,其中描述了evdev模块,该模块注册了一个handler,所有的input_dev都会匹配上该handler。下面进行简要的分析,首先是模块初始化函数evdev_init(),其调用了input_register_handler注册了一个handler,evdev_handler。定义如下:
- static struct input_handler evdev_handler = {
- .event = evdev_event,
- .connect = evdev_connect,
- .disconnect = evdev_disconnect,
- .fops = &evdev_fops,
- .minor = EVDEV_MINOR_BASE,
- .name = "evdev",
- .id_table = evdev_ids,
- };
- static const struct input_device_id evdev_ids[] = {
- { .driver_info = 1 }, /* Matches all devices */
- { }, /* Terminating zero entry */
- };
而进行dev和handler匹配时,主要是看id_table,该id_table中没有定义flags,也没有定义匹配的属性值,所以这个handler可以匹配所有的input_dev。
当匹配成功之后会调用handler中的connect回调函数。
- static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
- const struct input_device_id *id)
- {
- struct evdev *evdev;
- int minor;
- int error;
- for (minor = 0; minor < EVDEV_MINORS; minor++)
- if (!evdev_table[minor])
- break;
- if (minor == EVDEV_MINORS) {
- pr_err("no more free evdev devices\n");
- return -ENFILE;
- }
- // evdev结构体声明空间
- evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
- if (!evdev)
- return -ENOMEM;
- INIT_LIST_HEAD(&evdev->client_list);
- spin_lock_init(&evdev->client_lock);
- mutex_init(&evdev->mutex);
- init_waitqueue_head(&evdev->wait); // 初始化等待队列
- dev_set_name(&evdev->dev, "event%d", minor);
- evdev->exist = true;
- evdev->minor = minor;
- // 初始化evdev中的handle
- evdev->handle.dev = input_get_device(dev);
- evdev->handle.name = dev_name(&evdev->dev);
- evdev->handle.handler = handler;
- evdev->handle.private = evdev;
- evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor);
- evdev->dev.class = &input_class;
- evdev->dev.parent = &dev->dev;
- evdev->dev.release = evdev_free;
- device_initialize(&evdev->dev);
- // 使用input_register_handle注册handle
- error = input_register_handle(&evdev->handle);
- if (error)
- goto err_free_evdev;
- error = evdev_install_chrdev(evdev);
- if (error)
- goto err_unregister_handle;
- error = device_add(&evdev->dev);
- if (error)
- goto err_cleanup_evdev;
- return 0;
- err_cleanup_evdev:
- evdev_cleanup(evdev);
- err_unregister_handle:
- input_unregister_handle(&evdev->handle);
- err_free_evdev:
- put_device(&evdev->dev);
- return error;
- }
在evdev_connect连接函数中,会声明一个evdev的结构体,然后使用input_register_handle注册一个handle,最后将该evdev设备注册至sysfs中。而当有事件发生时会调用匹配handler的event方法,在此模块中即为evdev_event方法
- static void evdev_event(struct input_handle *handle,
- unsigned int type, unsigned int code, int value)
- {
- struct evdev *evdev = handle->private;
- struct evdev_client *client;
- struct input_event event;
- struct timespec ts;
- ktime_get_ts(&ts);
- event.time.tv_sec = ts.tv_sec;
- event.time.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
- event.type = type;
- event.code = code;
- event.value = value;
- rcu_read_lock();
- client = rcu_dereference(evdev->grab);
- if (client)
- evdev_pass_event(client, &event);
- else
- list_for_each_entry_rcu(client, &evdev->client_list, node)
- evdev_pass_event(client, &event);
- rcu_read_unlock();
- if (type == EV_SYN && code == SYN_REPORT)
- wake_up_interruptible(&evdev->wait);
- }
在该方法中,会初始化一个input_event结构体,并通过evdev_pass_event将该事件放入client的buffer之中,最后使用wake_up_interruptible来唤醒一个等待事件,而此等待事件发生在上层的读取事件回调函数中,下面来具体的分析下input子系统是如何供上层调用的。
首先是input子系统的初始化函数input_init()。
- static int __init input_init(void)
- {
- int err;
- err = class_register(&input_class);
- if (err) {
- pr_err("unable to register input_dev class\n");
- return err;
- }
- err = input_proc_init();
- if (err)
- goto fail1;
- err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
- if (err) {
- pr_err("unable to register char major %d", INPUT_MAJOR);
- goto fail2;
- }
- #ifdef CONFIG_CPU_FREQ_USR_EVNT_NOTIFY
- cpufreq_usrevent = create_workqueue("cpufreq_uevent");
- if (!cpufreq_usrevent) {
- printk(KERN_ERR "Creation of cpufreq_usrevent failed\n");
- goto fail3;
- }
- #endif
- return 0;
- #ifdef CONFIG_CPU_FREQ_USR_EVNT_NOTIFY
- fail3: unregister_chrdev(INPUT_MAJOR, "input");
- #endif
- fail2: input_proc_exit();
- fail1: class_unregister(&input_class);
- return err;
- }
在此初始化函数中,首先会注册一个名为“input”的类,所有的input device都属于这个类。即在sysfs中,所有的输入设备所代表的目录都在/dev/class/input下面;然后再调用input_proc_init()在/proc下面建立相关的交互文件;最后使用register_chrdev注册了主设备号为INPUT_MAJOR(13),次设备号为0~255的字符设备,其操作函数指针为input_fops。
- static const struct file_operations input_fops = {
- .owner = THIS_MODULE,
- .open = input_open_file,
- .llseek = noop_llseek,
- };
上层可以通过open设备节点来打开设备,其会调用到这个回调函数input_open_file。
- static int input_open_file(struct inode *inode, struct file *file)
- {
- struct input_handler *handler;
- const struct file_operations *old_fops, *new_fops = NULL;
- int err;
- err = mutex_lock_interruptible(&input_mutex);
- if (err)
- return err;
- // 获取input_table当中的handler,如果存在则将其fops赋值给new_fops
- /* No load-on-demand here? */
- handler = input_table[iminor(inode) >> 5];
- if (handler)
- new_fops = fops_get(handler->fops);
- mutex_unlock(&input_mutex);
- /*
- * That's _really_ odd. Usually NULL ->open means "nothing special",
- * not "no device". Oh, well...
- */
- if (!new_fops || !new_fops->open) {
- fops_put(new_fops);
- err = -ENODEV;
- goto out;
- }
- // 将new_fops赋值给这个file的f_op
- old_fops = file->f_op;
- file->f_op = new_fops;
- // 调用新的open函数
- err = new_fops->open(inode, file);
- if (err) {
- fops_put(file->f_op);
- file->f_op = fops_get(old_fops);
- }
- fops_put(old_fops);
- out:
- return err;
- }
在input_open_file打开函数中,会首先根据minor来获取input_table中的handler,然后将handler中fops赋值给file作为该file新的fops,之后调用新的open方法。而在evdev模块中,有fops如下:
- static const struct file_operations evdev_fops = {
- .owner = THIS_MODULE,
- .read = evdev_read,
- .write = evdev_write,
- .poll = evdev_poll,
- .open = evdev_open,
- .release = evdev_release,
- .unlocked_ioctl = evdev_ioctl,
- #ifdef CONFIG_COMPAT
- .compat_ioctl = evdev_ioctl_compat,
- #endif
- .fasync = evdev_fasync,
- .flush = evdev_flush,
- .llseek = no_llseek,
- };
则当open文件时会调用到evdev_fops中的open方法evdev_open,然后上层空间可以通过read sysfs下的设备节点来调用回调函数read获取事件信息,在evdev模块中的read回调函数即为evdev_read。
- static ssize_t evdev_read(struct file *file, char __user *buffer,
- size_t count, loff_t *ppos)
- {
- struct evdev_client *client = file->private_data;
- struct evdev *evdev = client->evdev;
- struct input_event event;
- int retval = 0;
- if (count < input_event_size())
- return -EINVAL;
- if (!(file->f_flags & O_NONBLOCK)) {
- retval = wait_event_interruptible(evdev->wait,
- client->packet_head != client->tail || !evdev->exist);
- if (retval)
- return retval;
- }
- if (!evdev->exist)
- return -ENODEV;
- while (retval + input_event_size() <= count &&
- evdev_fetch_next_event(client, &event)) {
- if (input_event_to_user(buffer + retval, &event))
- return -EFAULT;
- retval += input_event_size();
- }
- if (retval == 0 && file->f_flags & O_NONBLOCK)
- retval = -EAGAIN;
- return retval;
- }
该方法会使用wait_event_interruptible等待事件的发生,此方法会中断在这里等待,当有时间发生时,最终会使用evdev_event来将时间存在client中,之后使用wake_up_interruptible唤醒此处等待事件,然后会通过input_event_to_user方法将client中的事件信息传递至用户层空间。