linux input子系统分析--子系统核心.事件处理层.事件传递过程
一. 输入子系统核心分析。
1.输入子系统核心对应与/drivers/input/input.c文件,这个也是作为一个模块注册到内核的。所以首先分析模块初始化函数。- static int __init input_init(void)
- {
- int err;
- input_init_abs_bypass();
- //这个暂时没有发现是做什么的
- err = class_register(&input_class);
- //向内核注册一个类,用于linux设备模型。注册后会在/sys/class下面出现input目录
- if (err) {
- printk(KERN_ERR "input: unable to register input_dev class\n");
- return err;
- }
- err = input_proc_init();
- //和proc文件系统有关,暂时不管
- if (err)
- goto fail1;
- err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
- //注册字符设备,接口是2.4内核的。以主设备号INPUT_MAJOR,次设备号0-255,注册266个设备,说明input设备最大只能有255个
- if (err) {
- printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
- goto fail2;
- }
- return 0;
- fail2: input_proc_exit();
- fail1: class_unregister(&input_class);
- return err;
- }
2. 输入子系统的核心其他部分都是提供的接口,向上连接事件处理层,向下连接驱动层。
向下对驱动层的接口主要有:
input_allocate_device 这个函数主要是分配一个input_dev接口,并初始化一些基本的成员,这就是我们不能简单用kmalloc分配input_dev结构的原因,因为缺少了一些初始化。
input_unregister_device 注册一个input设备
input_event 这个函数很重要,是驱动层向input子系统核心报告事件的函数,在事件传递过程中再分析。
input_allocate_device 分配并初始化一个input_dev结构
向上对事件处理层接口主要有:
input_register_handler 注册一个事件处理器
input_register_handle 注册一个input_handle结构
二. 事件处理层分析(以evdev事件处理器为例)
1.事件处理层与用户程序和输入子系统核心打交道,是他们两层的桥梁。一般内核有好几个事件处理器,像evdev mousedev jotdev。evdev事件处理器可以处理所有的事件,触摸屏驱动就是用的这个,所以下面分析这个事件处理器的实现。它也是作为模块注册到内核中的,首先分析它的模块初始化函数。
- static int __init evdev_init(void)
- {
- return input_register_handler(&evdev_handler);
- }
2.主要数据结构
(1) evdev设备结构
- struct evdev {
- int exist;
- int open; //打开标志
- int minor; //次设备号
- struct input_handle handle; //关联的input_handle
- wait_queue_head_t wait; //等待队列,当进程读取设备,而没有事件产生的时候,进程就会睡在其上面
- struct evdev_client *grab; //强制绑定的evdev_client结构,这个结构后面再分析
- struct list_head client_list; //evdev_client 链表,这说明一个evdev设备可以处理多个evdev_client,可以有多个进程访问evdev设备
- spinlock_t client_lock; /* protects client_list */
- struct mutex mutex;
- struct device dev; //device结构,说明这是一个设备结构
- };
索引值是minor
(2) evdev用户端结构
- struct evdev_client {
- struct input_event buffer[EVDEV_BUFFER_SIZE];
- //这个是一个input_event数据结构的数组,input_event代表一个事件,基本成员:类型(type),编码(code),值(value)
- int head; //针对buffer数组的索引
- int tail; //针对buffer数组的索引,当head与tail相等的时候,说明没有事件
- spinlock_t buffer_lock; /* protects access to buffer, head and tail */
- struct fasync_struct *fasync; //异步通知函数
- struct evdev *evdev; //evdev设备
- struct list_head node; // evdev_client 链表项
- };
3.主要函数
(1)evdev设备打开函数
- static int evdev_open(struct inode *inode, struct file *file)
- {
- struct evdev *evdev;
- struct evdev_client *client;
- int i = iminor(inode) - EVDEV_MINOR_BASE;
- int error;
- if (i >= EVDEV_MINORS)
- return -ENODEV;
- error = mutex_lock_interruptible(&evdev_table_mutex);
- if (error)
- return error;
- evdev = evdev_table[i];
- //得到evdev设备结构,每次调用evdev_connect配对成功后都会把分配的evdev结构以minor为索引,保存在evdev_table数组中
- if (evdev)
- get_device(&evdev->dev); //增加device引用计数
- mutex_unlock(&evdev_table_mutex);
- if (!evdev)
- return -ENODEV;
- client = kzalloc(sizeof(struct evdev_client), GFP_KERNEL); //分配用户端结构
- if (!client) {
- error = -ENOMEM;
- goto err_put_evdev;
- }
- spin_lock_init(&client->buffer_lock);
- client->evdev = evdev; //使用户端与evdev设备结构联系起来
- evdev_attach_client(evdev, client);
- //这个函数所做的就是把client连接到evdev的client链表中
- error = evdev_open_device(evdev);
- //这个函数打开设备,有很多层调用,后面详细分析
- if (error)
- goto err_free_client;
- file->private_data = client;
- return 0;
- err_free_client:
- evdev_detach_client(evdev, client);
- kfree(client);
- err_put_evdev:
- put_device(&evdev->dev);
- return error;
- }
- static int evdev_open_device(struct evdev *evdev)
- {
- int retval;
- retval = mutex_lock_interruptible(&evdev->mutex);
- if (retval)
- return retval;
- if (!evdev->exist)
- retval = -ENODEV;
- //判断设备结构是否存在,在evdev_connect中初始话此成员为1
- else if (!evdev->open++) {
- retval = input_open_device(&evdev->handle);
- if (retval)
- evdev->open--;
- }
- //evdev->open分配结构的时候没有初始化,默认为0,也就是没有打开,每次打开都会加1
- mutex_unlock(&evdev->mutex);
- return retval;
- }
- int input_open_device(struct input_handle *handle)
- {
- struct input_dev *dev = handle->dev;
- int retval;
- retval = mutex_lock_interruptible(&dev->mutex);
- if (retval)
- return retval;
- if (dev->going_away) {
- retval = -ENODEV;
- goto out;
- }
- handle->open++;
- //将handle的打开计数加1,注意和evdev的open的区别
- if (!dev->users++ && dev->open)
- retval = dev->open(dev);
- //如果此input_dev没有进程在引用,并且定义了open方法,就调用open方法
- if (retval) { //retval = 1 说明没有打开成功
- dev->users--;
- if (!--handle->open) { //说明有其他的进程已经打开了这个handle
- /*
- * Make sure we are not delivering any more events
- * through this handle
- */
- synchronize_rcu();
- }
- }
- out:
- mutex_unlock(&dev->mutex);
- return retval;
- }
- static ssize_t evdev_read(struct file *file, char __user *buffer,
- size_t count, loff_t *ppos)
- {
- struct evdev_client *client = file->private_data; //这个客户端结构在打开的时候分配并保存在file->private_data中
- struct evdev *evdev = client->evdev;
- struct input_event event;
- int retval;
- if (count < input_event_size())
- return -EINVAL;
- //这条语句提示,用户进程每次读取设备的字节数,不要少于input_event结构的大小
- if (client->head == client->tail && evdev->exist &&
- (file->f_flags & O_NONBLOCK))
- return -EAGAIN;
- //head等于tail说明目前还没有事件传回来,如果设置了非阻塞操作,则会立刻返回
- retval = wait_event_interruptible(evdev->wait,
- client->head != client->tail || !evdev->exist);
- //没有事件就会睡在evdev的等待队列上了,等待条件是有事件到来或者设备不存在了(设备关闭的时候,清这个标志)
- if (retval)
- return retval;
- //如果能执行上面这条语句说明有事件传来或者,设备被关闭了,或者内核发过来终止信号
- if (!evdev->exist)
- return -ENODEV;
- while (retval + input_event_size() <= count &&
- evdev_fetch_next_event(client, &event)) {
- // evdev_fetch_next_event这个函数遍历client里面的input_event buffer数组
- if (input_event_to_user(buffer + retval, &event))
- //将事件复制到用户空间
- return -EFAULT;
- retval += input_event_size();
- }
- return retval; //返回复制的数据字节数
- }
1. 事件产生
当按下触摸屏时,进入触摸屏按下中断,开始ad转换,ad转换完成进入ad完成中断,在这个终端中将事件发送出去,调用
input_report_abs(dev, ABS_X, xp);
input_report_abs(dev, ABS_Y, yp); 这两个函数调用了 input_event(dev, EV_ABS, code, value)
所有的事件报告函数都调用这个函数。
2. 事件报告
(1) input_event 函数分析,这个函数定义在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);
- add_input_randomness(type, code, value);
- //对系统随机熵池有贡献,因为这个也是一个随机过程
- input_handle_event(dev, type, code, value);
- //这个函数是事件处理的关键函数,下面详细分析
- spin_unlock_irqrestore(&dev->event_lock, flags);
- }
- }
- 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_KEY:
- if (is_event_supported(code, dev->keybit, KEY_MAX) &&
- !!test_bit(code, dev->key) != value) {
- if (value != 2) {
- __change_bit(code, dev->key);
- if (value)
- input_start_autorepeat(dev, code);
- else
- input_stop_autorepeat(dev);
- }
- disposition = INPUT_PASS_TO_HANDLERS;
- }
- break;
- ......
- if (disposition != INPUT_IGNORE_EVENT && type != EV_SYN)
- dev->sync = 0;
- 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);
- }
- static void input_pass_event(struct input_dev *dev,
- unsigned int type, unsigned int code, int value)
- {
- struct input_handle *handle;
- rcu_read_lock();
- handle = rcu_dereference(dev->grab); //如果是绑定的handle,则调用绑定的handler->event函数
- if (handle)
- handle->handler->event(handle, type, code, value);
- else
- //如果没有绑定,则遍历dev的h_list链表,寻找handle,如果handle已经打开,说明有进程读取设备关联的evdev。
- list_for_each_entry_rcu(handle, &dev->h_list, d_node)
- if (handle->open)
- handle->handler->event(handle,
- type, code, value);
- // 调用相关的事件处理器的event函数,进行事件的处理
- rcu_read_unlock();
- }
- 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;
- do_gettimeofday(&event.time);
- event.type = type;
- event.code = code;
- event.value = value;
- //将传过来的事件,赋值给input_event结构
- rcu_read_lock();
- client = rcu_dereference(evdev->grab);
- //如果evdev绑定了client那么,处理这个客户端,触摸屏驱动没有绑定
- if (client)
- evdev_pass_event(client, &event);
- else
- //遍历client链表,调用evdev_pass_event函数
- list_for_each_entry_rcu(client, &evdev->client_list, node)
- evdev_pass_event(client, &event);
- rcu_read_unlock();
- wake_up_interruptible(&evdev->wait); //唤醒等待的进程
- }
- static void evdev_pass_event(struct evdev_client *client,
- struct input_event *event)
- {
- /*
- * Interrupts are disabled, just acquire the lock
- */
- spin_lock(&client->buffer_lock);
- client->buffer[client->head++] = *event; //将事件赋值给客户端的input_event 数组
- client->head &= EVDEV_BUFFER_SIZE - 1;
- spin_unlock(&client->buffer_lock);
- kill_fasync(&client->fasync, SIGIO, POLL_IN);
- }