input subsystem (二) input子系统相关流程

1,input core初始化

static int __init input_init(void)
{
    int err;
 
    //注册input class, /sys/class/input
    err = class_register(&input_class);
    if (err) {
        pr_err("unable to register input_dev class\n");
        return err;
    }
    
    //Proc文件创建,主要用于input_handler和devices信息查看
    err = input_proc_init();
    if (err)
        goto fail1;

    //注册一组字符设备编号,主设备号13
    err = register_chrdev_region(MKDEV(INPUT_MAJOR, 0),
                     INPUT_MAX_CHAR_DEVICES, "input");
    if (err) {
        pr_err("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_dev注册

input_register_device()
    device_add(&dev->dev);//添加到设备模型
    list_add_tail(&dev->node, &input_dev_list); //添加到input_dev_list全局链表
    list_for_each_entry(handler, &input_handler_list, node) //通过遍历input_handler_list与所有的input_handler进行匹配
        input_attach_handler(dev, handler);

3,input_handler注册

static const struct input_device_id evdev_ids[] = {
    { .driver_info = 1 },    /* Matches all devices */
    { },            /* Terminating zero entry */
};

MODULE_DEVICE_TABLE(input, evdev_ids);

static struct input_handler evdev_handler = {
    .event        = evdev_event,
    .events        = evdev_events,
    .connect    = evdev_connect,
    .disconnect    = evdev_disconnect,
    .legacy_minors    = true,
    .minor        = EVDEV_MINOR_BASE,
    .name        = "evdev",
    .id_table    = evdev_ids,
};

static int __init evdev_init(void)
{
    return input_register_handler(&evdev_handler);
}

static void __exit evdev_exit(void)
{
    input_unregister_handler(&evdev_handler);
}

int input_register_handler(struct input_handler *handler)
{
    struct input_dev *dev;
    int error;

    error = mutex_lock_interruptible(&input_mutex);
    if (error)
        return error;

    INIT_LIST_HEAD(&handler->h_list);//初始化h_list链表

    list_add_tail(&handler->node, &input_handler_list);//将该handler添加到input_handler_list全局链表

    list_for_each_entry(dev, &input_dev_list, node) //通过遍历所有的input_dev_list,与input_dev匹配判定
        input_attach_handler(dev, handler);

    input_wakeup_procfs_readers();

    mutex_unlock(&input_mutex);
    return 0;
}

4,input_handler与input_dev匹配

Input_handler与input_dev的注册最终都会调用input_attach_handler完成自己与“相亲对象”的配对,配对完成后input_dev、input_handler、input_handle之间的关系如图3所示,设备驱动和事件处理层驱动都可以通过自身访问到input_handle,然后通过input_handle访问到自己的“对象”,具体的匹配代码说明如下:
图1 input_handle与owner关系图
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); //匹配成功后调用handler的连接函数
    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;
}
当handler中match回调没有实现时只用根据 input_dev中的id与input_handler中id_table包含的id进行匹配 ,如evdev_handler匹配所有Input设备,故所有的input设备都可以通过dev/input/event*路径获取原始上报数据。
static const struct input_device_id *input_match_device(struct input_handler *handler,
                            struct input_dev *dev)
{
    const struct input_device_id *id;

    for (id = handler->id_table; id->flags || id->driver_info; id++) {
        if (input_match_device_id(dev, id) &&
            (!handler->match || handler->match(handler, dev))) {
            return id;
        }
    }

    return NULL;
}
在匹配成功后会调用connect回调,举例evdev_handler中的connect内容如下:
 
/*
* Create new evdev device. Note that input core serializes calls
* to connect and disconnect.
*/
static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
             const struct input_device_id *id)
{
    struct evdev *evdev;
    int minor;
    int dev_no;
    int error;
    
    //申请次设备号
    minor = input_get_new_minor(EVDEV_MINOR_BASE, EVDEV_MINORS, true);
    if (minor < 0) {
        error = minor;
        pr_err("failed to reserve new minor: %d\n", error);
        return error;
    }

    //创建event device
    evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
    if (!evdev) {
        error = -ENOMEM;
        goto err_free_minor;
    }

    INIT_LIST_HEAD(&evdev->client_list);//初始化client_list链表(用于保存打开该设备的user),在事件传递时遍历传递给client_list上的client
    spin_lock_init(&evdev->client_lock);
    mutex_init(&evdev->mutex);
    evdev->exist = true;

    dev_no = minor;
    /* Normalize device number if it falls into legacy range */
    if (dev_no < EVDEV_MINOR_BASE + EVDEV_MINORS)
        dev_no -= EVDEV_MINOR_BASE;
    dev_set_name(&evdev->dev, "event%d", dev_no);

    //初始化handle
    evdev->handle.dev = input_get_device(dev);
    evdev->handle.name = dev_name(&evdev->dev);
    evdev->handle.handler = handler;
    evdev->handle.private = evdev;

    //初始化event device
    evdev->dev.devt = MKDEV(INPUT_MAJOR, minor);
    evdev->dev.class = &input_class;
    evdev->dev.parent = &dev->dev;
    evdev->dev.release = evdev_free;
    device_initialize(&evdev->dev);

    //注册handle
    error = input_register_handle(&evdev->handle);
    if (error)
        goto err_free_evdev;

    //字符设备初始化,evdev_fops:字符设备操作函数(open,read,write)
    cdev_init(&evdev->cdev, &evdev_fops);
    
    //添加字符设备event device, /dev/input/event*
    error = cdev_device_add(&evdev->cdev, &evdev->dev);
    if (error)
        goto err_cleanup_evdev;

    return 0;

err_cleanup_evdev:
    evdev_cleanup(evdev);
    input_unregister_handle(&evdev->handle);
err_free_evdev:
    put_device(&evdev->dev);
err_free_minor:
    input_free_minor(minor);
    return error;
}

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.
     */
    if (handler->filter)
        list_add_rcu(&handle->d_node, &dev->h_list); //将handle添加到关联的input_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.
     */
    list_add_tail_rcu(&handle->h_node, &handler->h_list); //将handle添加到关联的input_handler的h_list上

    if (handler->start)
        handler->start(handle);

    return 0;
}

5,input事件传递

input_report_key()
input_report_rel()
input_report_abs()
input_sync()
    input_event()
        input_handle_event(dev, type, code, value);
            input_pass_values(dev, dev->vals, dev->num_vals);
                
  static void input_pass_values(struct input_dev *dev,
                  struct input_value *vals, unsigned int count)
{
......
  handle = rcu_dereference(dev->grab);
    if (handle) {
        count = input_to_handler(handle, vals, count);
    } else {
        list_for_each_entry_rcu(handle, &dev->h_list, d_node) //发给input_dev h_list上所有的handle
            if (handle->open) {
                count = input_to_handler(handle, vals, count);
                if (!count)
                    break;
            }
    }
......

input_to_handler()
    handler->events(handle, vals, count);
        evdev_events()
        
    client = rcu_dereference(evdev->grab);

    if (client)
        evdev_pass_values(client, vals, count, ev_time); //单独发给某个指定的client
    else
        list_for_each_entry_rcu(client, &evdev->client_list, node) //发给client_list链表上的每个client
            evdev_pass_values(client, vals, count, ev_time);

evdev_pass_values()
    __pass_event(client, &event);

6,input事件传递给用户空间

当应用层或框架层调用read函数读取/dev/input/event*文件时,会调用evdev_read返回数据,其中event_fetch_next_event是判断client->buffer这个循环缓冲区中的头尾指针是否相等(相等时buffer中没有数据),不相等时取出一个input_event类型的事件放入到event中;input_event_to_user函数是将此事件copy到应用层,input_event_size函数是用来获取一个input_event事件的大小,循环复制client->buffer中的事件到应用层的buffer中。
 
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,
    .llseek        = no_llseek,
};


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;
    size_t read = 0;
    int error;

    if (count != 0 && count < input_event_size())
        return -EINVAL;

    for (;;) {
        if (!evdev->exist || client->revoked)
            return -ENODEV;

        if (client->packet_head == client->tail &&
            (file->f_flags & O_NONBLOCK))
            return -EAGAIN;

        /*
         * count == 0 is special - no IO is done but we check
         * for error conditions (see above).
         */
        if (count == 0)
            break;

        while (read + input_event_size() <= count &&
               evdev_fetch_next_event(client, &event)) {

            if (input_event_to_user(buffer + read, &event))
                return -EFAULT;

            read += input_event_size();
        }

        if (read)
            break;

        if (!(file->f_flags & O_NONBLOCK)) {
            error = wait_event_interruptible(client->wait,
                    client->packet_head != client->tail ||
                    !evdev->exist || client->revoked);
            if (error)
                return error;
        }
    }

    return read;
}
参考链接:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值