input子系统框架

废话不多说,直接进入主题。在驱动insmod后,我们应用层对input设备如何操作?以下以全志a64为实例。

在/dev/input/eventX下(X的形成为后续会分析),是内核把接口暴露给应用层,一切操作都在这个文件上。

input子系统有两大部分,分别是input_dev和input_handler组成。

这两个的关系与device和driver类似,不同的是device只能对应一个driver,driver可以对应多个devcie,而handler可以对应多个device,device同样可以对应多个handler。

在linux-3.10/include/linux/input.h:

struct input_dev和struct input_handler都包含struct list_head h_list和struct list_head node

node:device(driver)列表,一旦内核注册了一个input(hander)设备会加入device(driver)列表。

h_list:handl列表,一旦node匹配成功,会加入handle新的列表。

inputhander是通过struct list_head node这个纽带连接起来的,在input_dev中代表device,在input_handler中代表driver。

当input_dev和input_handler匹配成功后会初始化struct input_handle(注意这里不是er!),这是input子系统的句柄,一切操作都通过handle。

input_handle包含struct list_head d_node和struct list_head h_node

d_node:连接input_dev的h_list。

h_node:连接input_handler的h_list。

 

input_handle会记录input_dev和input_handler相关信息。

            /----------h_list<-------------->d_node

input_dev----------node                          |       

                              |                             |

                              |<------------->input_handle

                              |                             |

input_handler-----node                           |

           \----------h_list<------------>h_node

 

大概流程知道了,就从代码分析吧!

这里从input子系统的注册开始,代码在linux-3.10/drivers/input/input.c除了初始化函数,还有其他函数,后续分析会调用到。

以下忽略初始化部分冗余代码:

 1 static char *input_devnode(struct device *dev, umode_t *mode)
 2 {
 3     return kasprintf(GFP_KERNEL, "input/%s", dev_name(dev));
 4 }
 5 
 6 struct class input_class = {
 7     .name        = "input",
 8     .devnode    = input_devnode,
 9 };
10 static int __init input_proc_init(void)
11 {
12     struct proc_dir_entry *entry;
13 
14     proc_bus_input_dir = proc_mkdir("bus/input", NULL);
15 
16     //input_devices_fileops会调用seq_operations
17     entry = proc_create("devices", 0, proc_bus_input_dir,
18                 &input_devices_fileops);
19 
20     entry = proc_create("handlers", 0, proc_bus_input_dir,
21                 &input_handlers_fileops);
22 
23     return 0;
24 }
25 
26 static int __init input_init(void)
27 {
28     int err;
29 
30     err = class_register(&input_class);    
31 
32     err = input_proc_init();
33 
34     err = register_chrdev_region(MKDEV(INPUT_MAJOR, 0),    //#define INPUT_MAJOR    13
35                      INPUT_MAX_CHAR_DEVICES, "input");    //#define INPUT_MAX_CHAR_DEVICES        1024
36 
37     return 0;
38 }
39 
40 subsys_initcall(input_init);
41 module_exit(input_exit);

从初始化的代码看,input_init只干了3件事情。

1:注册input class;

2:在/proc/bus/input创建devices和handlers属性,具体input_devices_fileops不分析,感兴趣可以cat里面的内容分析源码。

3:注册input字符设备。

 

input子系统初始化后继续分析,先从input_dev这部分看,以下以本人在全志a64平台上写的"my_key"为实例,my_key.c代码如下:

  1 #include <linux/io.h>
  2 #include <linux/gpio.h>
  3 #include <linux/interrupt.h>
  4 #include <linux/module.h>
  5 #include <linux/of.h>
  6 #include <linux/of_platform.h>
  7 #include <linux/of_address.h>
  8 #include <linux/of_device.h>
  9 #include <linux/of_gpio.h>
 10 #include <linux/pinctrl/consumer.h>
 11 #include <linux/platform_device.h>
 12 #include <linux/slab.h>
 13 #include <linux/sys_config.h>
 14 #include <linux/string.h>
 15 #include <linux/delay.h>
 16 #include <linux/input.h>
 17 #include <linux/spinlock.h>
 18 
 19 #define DEBUG
 20 #ifdef DEBUG
 21 #define dprintk(fmt, arg...) printk(KERN_DEBUG fmt, ##arg)
 22 #else
 23 #define dprintk(fmt, arg...)
 24 #endif
 25 
 26 static struct of_device_id mykey_of_match[] = {
 27     { .compatible = "allwinner,mykey"},
 28 };
 29 
 30 
 31 struct key_dev {
 32     struct pinctrl *pin;
 33     int gpio;
 34     int irq;
 35     spinlock_t irq_lock;
 36     struct work_struct work;
 37     struct input_dev *input_dev;
 38 };
 39 
 40 static void handle_mykey(struct work_struct *work)
 41 {
 42     int val;
 43     unsigned long irqflags;
 44     struct key_dev *p_key  = container_of(work, struct key_dev, work);
 45 
 46     val = __gpio_get_value(p_key->gpio);
 47 
 48     msleep(50);
 49     if (val == __gpio_get_value(p_key->gpio)) {
 50         dprintk("The key val is %d.\n", val);
 51         if (val)
 52             input_report_key(p_key->input_dev, KEY_ENTER, 1);    //松开
 53         else
 54             input_report_key(p_key->input_dev, KEY_ENTER, 0);    //按下
 55         input_sync(p_key->input_dev);
 56     }
 57     
 58     spin_lock_irqsave(&p_key->irq_lock, irqflags);
 59     enable_irq(p_key->irq);
 60     spin_unlock_irqrestore(&p_key->irq_lock, irqflags);
 61 
 62 }
 63 
 64 static irqreturn_t mykey_irq_handler(int irq, void *dev_id)
 65 {
 66     struct key_dev *p_key =  dev_id;
 67     unsigned long irqflags;
 68 
 69     spin_lock_irqsave(&p_key->irq_lock, irqflags);
 70     disable_irq_nosync(p_key->irq);
 71     spin_unlock_irqrestore(&p_key->irq_lock, irqflags);
 72 
 73     dprintk("in interrupt\n");
 74     schedule_work(&p_key->work);    //工作队列中断下半部分
 75     return IRQ_HANDLED;
 76 }
 77 
 78 static int mykey_probe(struct platform_device *pdev)
 79 {
 80     struct device_node *node = pdev->dev.of_node;
 81     struct key_dev *p_key;
 82 
 83     int ret;
 84     dprintk("Initializing my_key.\n");
 85 
 86     p_key = devm_kzalloc(&pdev->dev, sizeof(*p_key), GFP_KERNEL);
 87     if (IS_ERR(p_key)) {
 88         printk(KERN_ERR "Failed to kzalloc p_key.\n");
 89         ret = -1;
 90         goto out;
 91     }
 92 
 93     p_key->pin = devm_pinctrl_get_select(&pdev->dev, "default");    //初始化IO口状态配置
 94     if (IS_ERR(p_key->pin)) {
 95         printk(KERN_ERR "Failed to get_select pin.\n");
 96         ret = -1;
 97         goto out;
 98     }
 99 
100     p_key->gpio = of_get_named_gpio(node, "mykey-gpio", 0);    //从设备树得到IO句柄
101 
102     ret = devm_gpio_request(&pdev->dev, p_key->gpio, NULL);
103     if (ret) {
104         printk(KERN_ERR "Failed to request gpio:%d.\n", p_key->gpio);
105         goto out;
106     }
107 
108     //获取gpio对应的irq号并申请中断
109     p_key->irq = gpio_to_irq(p_key->gpio);
110     ret = devm_request_irq(&pdev->dev, p_key->irq, mykey_irq_handler, IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, "MYKEY_EINT", (void *)p_key);
111     if (ret) {
112         printk(KERN_ERR "Failed to request irq: %d.\n", p_key->irq);
113         goto out;
114     }
115 
116     p_key->input_dev = devm_input_allocate_device(&pdev->dev);    //开启资源回收 & 分配input_dev结构空间并初始化h_list和node
117     if (IS_ERR(p_key->input_dev)) {
118         printk(KERN_ERR "Failed to allocate input device.\n");
119         ret = -1;
120         goto out;
121     }
122 
123     p_key->input_dev->name = "my_key";
124     p_key->input_dev->evbit[0] = BIT_MASK(EV_KEY);    //设置KEY事件
125     set_bit(KEY_ENTER, p_key->input_dev->keybit);        //key事件对应具体操作码
126     set_bit(KEY_ENTER, p_key->input_dev->key);        //把key事件对应具体操作码的状态置1(电路常态是高电平)
127     ret = input_register_device(p_key->input_dev);
128     if (ret) {
129         printk(KERN_ERR "Failed to register input_device.\n");
130         goto out;
131     }
132     INIT_WORK (&p_key->work, handle_mykey);    //初始化队列
133     spin_lock_init(&p_key->irq_lock);
134 
135     platform_set_drvdata(pdev, p_key);
136     return 0;
137 out:
138     return ret;
139 }
140 
141 static int mykey_remove(struct platform_device *pdev)
142 {
143     struct key_dev *p_key  = platform_get_drvdata(pdev);
144     flush_work(&p_key->work);
145     return 0;
146 }
147 
148 static struct platform_driver mykey_driver = {
149     .probe  = mykey_probe,
150     .remove = mykey_remove,
151     .driver = {
152         .name   = "mykey",
153         .owner  = THIS_MODULE,
154         .of_match_table = of_match_ptr(mykey_of_match),
155     },
156 };
157 module_platform_driver(mykey_driver);
158 
159 MODULE_AUTHOR("Kevin Hwang <kevin.hwang@live.com");
160 MODULE_LICENSE("GPL v2");

注释已经很明白,重点分析input_register_device,在linux-3.10/drivers/input/input.c定义。

忽略部分代码如下:

 1 int input_register_device(struct input_dev *dev)
 2 {
 3     static atomic_t input_no = ATOMIC_INIT(0);
 4     struct input_devres *devres = NULL;
 5     struct input_handler *handler;
 6     unsigned int packet_size;
 7     const char *path;
 8     int error;
 9 
10     if (dev->devres_managed) {
11         devres = devres_alloc(devm_input_device_unregister,
12                       sizeof(struct input_devres), GFP_KERNEL);
13         devres->input = dev;
14     }
15 
16     /* Every input device generates EV_SYN/SYN_REPORT events. */
17     __set_bit(EV_SYN, dev->evbit);
18 
19     /* KEY_RESERVED is not supposed to be transmitted to userspace. */
20     __clear_bit(KEY_RESERVED, dev->keybit);
21 
22     /* Make sure that bitmasks not mentioned in dev->evbit are clean. */
23     input_cleanse_bitmasks(dev);
24 
25     packet_size = input_estimate_events_per_packet(dev);    //估算input_dev上报数据需要多少缓存空间,这里pack_size=8
26     if (dev->hint_events_per_packet < packet_size)
27         dev->hint_events_per_packet = packet_size;
28 
29     dev->max_vals = max(dev->hint_events_per_packet, packet_size) + 2;    //对于my_key,max_vals=10
30     dev->vals = kcalloc(dev->max_vals, sizeof(*dev->vals), GFP_KERNEL);
31 
32     /*
33      * If delay and period are pre-set by the driver, then autorepeating
34      * is handled by the driver itself and we don't do it in input.c.
35      */
36     init_timer(&dev->timer);
37     if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
38         dev->timer.data = (long) dev;
39         dev->timer.function = input_repeat_key;
40         dev->rep[REP_DELAY] = 250;
41         dev->rep[REP_PERIOD] = 33;
42     }
43 
44     if (!dev->getkeycode)
45         dev->getkeycode = input_default_getkeycode;
46 
47     if (!dev->setkeycode)
48         dev->setkeycode = input_default_setkeycode;
49 
50     dev_set_name(&dev->dev, "input%ld",
51              (unsigned long) atomic_inc_return(&input_no) - 1);
52 
53     error = device_add(&dev->dev);
54 
55     path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
56     pr_info("%s as %s\n",
57         dev->name ? dev->name : "Unspecified device",
58         path ? path : "N/A");
59     kfree(path);
60 
61     list_add_tail(&dev->node, &input_dev_list);    //把input_dev的node成员插入到全局input_dev_list
62 
63     list_for_each_entry(handler, &input_handler_list, node)    //input_dev和input_handler匹配
64         input_attach_handler(dev, handler);
65 
66 
67     if (dev->devres_managed) {
68         dev_dbg(dev->dev.parent, "%s: registering %s with devres.\n",
69             __func__, dev_name(&dev->dev));
70         devres_add(dev->dev.parent, devres);
71     }
72     return 0;
73 }

input_attach_handler的实现后续再分析,现在input_dev(device)已经准备就绪,就差input_handler(driver)。

 

对应"my_key"的handler在linux3.10/drivers/input/evdev.c,初始化代码如下:

 1 static const struct input_device_id evdev_ids[] = {
 2     { .driver_info = 1 },    /* Matches all devices */
 3     { },            /* Terminating zero entry */
 4 };
 5 
 6 MODULE_DEVICE_TABLE(input, evdev_ids);
 7 
 8 static struct input_handler evdev_handler = {
 9     .event        = evdev_event,
10     .events        = evdev_events,
11     .connect    = evdev_connect,
12     .disconnect    = evdev_disconnect,
13     .legacy_minors    = true,
14     .minor        = EVDEV_MINOR_BASE, //#define EVDEV_MINOR_BASE    64
15     .name        = "evdev",
16     .id_table    = evdev_ids,
17 };
18 
19 static int __init evdev_init(void)
20 {
21     return input_register_handler(&evdev_handler);
22 }
23 
24 static void __exit evdev_exit(void)
25 {
26     input_unregister_handler(&evdev_handler);
27 }
28 
29 module_init(evdev_init);
30 module_exit(evdev_exit);

对于evdev而言,他的次设备是从64开始的。evdev_init只调用了input_register_handler,在linux-3.10/drivers/input/input.c定义。

忽略部分代码:

 1 int input_register_handler(struct input_handler *handler)
 2 {
 3     struct input_dev *dev;
 4 
 5     INIT_LIST_HEAD(&handler->h_list);    //初始化input_handler的h_list
 6 
 7     list_add_tail(&handler->node, &input_handler_list);    //把input_handler的node成员插入到全局input_handler_list
 8 
 9     list_for_each_entry(dev, &input_dev_list, node)
10         input_attach_handler(dev, handler);    //input_dev和input_handler匹配
11 
12     return 0;
13 }

这里又出现了input_attach_handler,现在input_dev和input_handler都有了,看看里面如何匹配。

 

 1 static const struct input_device_id *input_match_device(struct input_handler *handler,
 2                             struct input_dev *dev)
 3 {
 4     const struct input_device_id *id;
 5 
 6     for (id = handler->id_table; id->flags || id->driver_info; id++) {
 7 
 8         if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
 9             if (id->bustype != dev->id.bustype)
10                 continue;
11 
12         if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
13             if (id->vendor != dev->id.vendor)
14                 continue;
15 
16         if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
17             if (id->product != dev->id.product)
18                 continue;
19 
20         if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
21             if (id->version != dev->id.version)
22                 continue;
23         //bitmap_subset:argv1是否是argv2子集,若真返回1,假返回0。argv3是要校验的位数
24         //这里evdev是空集(id->xxxbit都是0),是所有input_dev的子集
25         if (!bitmap_subset(id->evbit, dev->evbit, EV_MAX))    
26             continue;
27 
28         if (!bitmap_subset(id->keybit, dev->keybit, KEY_MAX))
29             continue;
30 
31         if (!bitmap_subset(id->relbit, dev->relbit, REL_MAX))
32             continue;
33 
34         if (!bitmap_subset(id->absbit, dev->absbit, ABS_MAX))
35             continue;
36 
37         if (!bitmap_subset(id->mscbit, dev->mscbit, MSC_MAX))
38             continue;
39 
40         if (!bitmap_subset(id->ledbit, dev->ledbit, LED_MAX))
41             continue;
42 
43         if (!bitmap_subset(id->sndbit, dev->sndbit, SND_MAX))
44             continue;
45 
46         if (!bitmap_subset(id->ffbit, dev->ffbit, FF_MAX))
47             continue;
48 
49         if (!bitmap_subset(id->swbit, dev->swbit, SW_MAX))
50             continue;
51 
52         if (!handler->match || handler->match(handler, dev))
53             return id;
54     }
55 
56     return NULL;
57 }
58 
59 static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
60 {
61     const struct input_device_id *id;
62     int error;
63 
64     id = input_match_device(handler, dev);    
65     if (!id)
66         return -ENODEV;
67 
68     error = handler->connect(handler, dev, id);
69     if (error && error != -ENODEV)
70         pr_err("failed to attach handler %s to device %s, error: %d\n",
71                handler->name, kobject_name(&dev->dev.kobj), error);
72 
73     return error;
74 }

input_attach_handler做了两件事情,匹配和连接相应handler,因evdev_ids匹配所有input_device,所以这里就直接调用handler->connect = evdev_connect。

 

看看evdev_connect做了什么事情,回到linux3.10/drivers/input/evdev.c,

忽略部分冗余代码,并加上部分源码注释:

 1 static const struct file_operations evdev_fops = {
 2     .owner        = THIS_MODULE,
 3     .read        = evdev_read,
 4     .write        = evdev_write,
 5     .poll        = evdev_poll,
 6     .open        = evdev_open,
 7     .release    = evdev_release,
 8     .unlocked_ioctl    = evdev_ioctl,
 9 #ifdef CONFIG_COMPAT
10     .compat_ioctl    = evdev_ioctl_compat,
11 #endif
12     .fasync        = evdev_fasync,
13     .flush        = evdev_flush,
14     .llseek        = no_llseek,
15 };
16 
17 static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
18              const struct input_device_id *id)
19 {
20     struct evdev *evdev;
21     int minor;
22     int dev_no;
23     int error;
24     //生成64~64+32的次设备号
25     minor = input_get_new_minor(EVDEV_MINOR_BASE, EVDEV_MINORS, true);
26 
27     evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
28 
29     INIT_LIST_HEAD(&evdev->client_list);
30     spin_lock_init(&evdev->client_lock);
31     mutex_init(&evdev->mutex);
32     init_waitqueue_head(&evdev->wait);
33     evdev->exist = true;
34 
35     dev_no = minor;
36     /* Normalize device number if it falls into legacy range */
37     if (dev_no < EVDEV_MINOR_BASE + EVDEV_MINORS)
38         dev_no -= EVDEV_MINOR_BASE;    //次设备号减去偏移
39     dev_set_name(&evdev->dev, "event%d", dev_no);
40 
41     //初始化handle
42     evdev->handle.dev = input_get_device(dev);
43     evdev->handle.name = dev_name(&evdev->dev);
44     evdev->handle.handler = handler;
45     evdev->handle.private = evdev;
46 
47     //初始化device
48     evdev->dev.devt = MKDEV(INPUT_MAJOR, minor);
49     evdev->dev.class = &input_class;
50     evdev->dev.parent = &dev->dev;
51     evdev->dev.release = evdev_free;
52     device_initialize(&evdev->dev);
53 
54     error = input_register_handle(&evdev->handle);    
55 
56     // int input_register_handle(struct input_handle *handle)
57     // {
58     //     struct input_handler *handler = handle->handler;
59     //     struct input_dev *dev = handle->dev;
60 
61     //     list_add_tail_rcu(&handle->d_node, &dev->h_list);    把input_handle的d_node加入input_dev的h_list
62 
63     //     list_add_tail_rcu(&handle->h_node, &handler->h_list);    把input_handle的h_node加入input_handler的h_list
64 
65     //     return 0;
66     // }
67     
68     // 初始化并注册字符设备
69     cdev_init(&evdev->cdev, &evdev_fops);
70     evdev->cdev.kobj.parent = &evdev->dev.kobj;
71     error = cdev_add(&evdev->cdev, evdev->dev.devt, 1);
72 
73     error = device_add(&evdev->dev);
74 
75     return 0;
76 }

比较有趣的evdev_connect并没有用到argv3的struct input_device_id *id,因为evdev这个handler是可以匹配全部device的。

现在handle已经连接好dev和handler并且注册eventX,对eventX的操作都在evdev_fops。

 

在应用层open&read eventX,eventX到底是如何上报数据的?

由以上分析得到evdev_fops.open =  evdev_open。

忽略部分冗余代码,并加上部分源码注释:

 1 static int evdev_open(struct inode *inode, struct file *file)
 2 {
 3     struct evdev *evdev = container_of(inode->i_cdev, struct evdev, cdev);
 4     unsigned int bufsize = evdev_compute_buffer_size(evdev->handle.dev);    //bufsize=64
 5     // static unsigned int evdev_compute_buffer_size(struct input_dev *dev)  函数定义
 6     // {
 7     //     unsigned int n_events =
 8     //         max(dev->hint_events_per_packet * EVDEV_BUF_PACKETS,    #define EVDEV_BUF_PACKETS    8
 9     //             EVDEV_MIN_BUFFER_SIZE);
10 
11     //     return roundup_pow_of_two(n_events);        8*8=64刚好是2^6
12     // }
13     unsigned int size = sizeof(struct evdev_client) +
14                     bufsize * sizeof(struct input_event);
15     struct evdev_client *client;
16     int error;
17 
18     client = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
19 
20     client->bufsize = bufsize;
21     spin_lock_init(&client->buffer_lock);
22     snprintf(client->name, sizeof(client->name), "%s-%d",
23             dev_name(&evdev->dev), task_tgid_vnr(current));
24     client->evdev = evdev;
25     evdev_attach_client(evdev, client);    //把client的node插入到evdev的client_list
26 
27     // static void evdev_attach_client(struct evdev *evdev, struct evdev_client *client)    函数定义
28     // {
29     //     spin_lock(&evdev->client_lock);
30     //     list_add_tail_rcu(&client->node, &evdev->client_list);    
31     //     spin_unlock(&evdev->client_lock);
32     // }
33 
34     error = evdev_open_device(evdev);
35     // static int evdev_open_device(struct evdev *evdev)  函数定义
36     // {
37     //     int retval;
38     //     if (!evdev->exist)
39     //         retval = -ENODEV;
40     //     else if (!evdev->open++) {    第一次open才调用input_open_device
41     //         retval = input_open_device(&evdev->handle);
42     //     }
43     //    return retval;
44     //}
45     file->private_data = client;    //以后操作文件都通过client
46     return 0;
47 }

注释已经很清晰,如果第一次调用open,需要调用input_open_device,在linux-3.10/drivers/input/input.c定义。

忽略部分冗余代码:

 1 int input_open_device(struct input_handle *handle)
 2 {
 3     struct input_dev *dev = handle->dev;
 4     int retval;
 5 
 6     handle->open++;
 7 
 8     if (!dev->users++ && dev->open)    //"my_key"的dev->open = NULL,不执行dev->open(dev)
 9         retval = dev->open(dev);
10 
11     return retval;
12 }

在"my_key"的实例中,input_open_device只干了一件有意义的事情就是handle->open++,到这里open的操作很清晰,主要是初始化client,因为后续的read都是在client操作的。

 

既然已经open成功了,后面看看evdev_read做了什么。

忽略部分冗余代码,并加上部分源码注释:

 1 static ssize_t evdev_read(struct file *file, char __user *buffer,
 2               size_t count, loff_t *ppos)
 3 {
 4     struct evdev_client *client = file->private_data;
 5     struct evdev *evdev = client->evdev;
 6     struct input_event event;
 7     size_t read = 0;
 8     int error;
 9 
10     for (;;) {
11         if (!evdev->exist)
12             return -ENODEV;
13 
14         if (client->packet_head == client->tail &&
15             (file->f_flags & O_NONBLOCK))
16             return -EAGAIN;
17 
18         while (read + input_event_size() <= count &&
19                evdev_fetch_next_event(client, &event)) {    //evdev_fetch_next_event到bufsize次就会逻辑假
20             // static int evdev_fetch_next_event(struct evdev_client *client, struct input_event *event)  函数定义
21             // {
22             //     int have_event;
23             //     通过client->tail &= client->bufsize - 1会使client->packet_head == client->tail
24             //     have_event = client->packet_head != client->tail;    
25             //     if (have_event) {
26             //         *event = client->buffer[client->tail++];
27             //         client->tail &= client->bufsize - 1;
28             //     }
29             //     return have_event;
30             // }
31 
32             if (input_event_to_user(buffer + read, &event))    //copy_to_user多了一层壳而已
33                 return -EFAULT;
34             // int input_event_to_user(char __user *buffer, const struct input_event *event)  函数定义
35             // {
36             //     if (copy_to_user(buffer, event, sizeof(struct input_event)))
37             //         return -EFAULT;
38             // }
39             read += input_event_size();
40         }
41 
42         if (read)    //如果读到数据
43             break;
44 
45         if (!(file->f_flags & O_NONBLOCK)) {    //如果阻塞
46             error = wait_event_interruptible(evdev->wait,    //唤醒判断client有无数据或者evdev release
47                     client->packet_head != client->tail ||
48                     !evdev->exist);
49             if (error)
50                 return error;
51         }
52     }
53 
54     return read;
55 }

 

添加了注释不能理解,这里有两个疑问。

1:什么函数使client->packet_head != client->tail并填充client->buffer?

2:若读阻塞,什么函数唤醒队列?

 

回到my_key.c,当按键触发中断的时候,会调用:

松开input_report_key(input_mykey_dev, KEY_ENTER, 1);input_sync(input_mykey_dev);

按下input_report_key(input_mykey_dev, KEY_ENTER, 0);input_sync(input_mykey_dev);

input_report_key和input_sync均在linux-3.10/drivers/input/input.c定义。

1 static inline void input_report_key(struct input_dev *dev, unsigned int code, int value)
2 {
3     input_event(dev, EV_KEY, code, !!value);
4 }
5 
6 static inline void input_sync(struct input_dev *dev)
7 {
8     input_event(dev, EV_SYN, SYN_REPORT, 0);
9 }

可见最终还是调用input_event,其在linux-3.10/drivers/input/input.c定义。

忽略部分冗余代码,并加上部分源码注释:

  1 void input_event(struct input_dev *dev,
  2          unsigned int type, unsigned int code, int value)
  3 {
  4     unsigned long flags;
  5 
  6     if (is_event_supported(type, dev->evbit, EV_MAX)) {
  7         input_handle_event(dev, type, code, value);
  8         // static void input_handle_event(struct input_dev *dev,
  9         //                    unsigned int type, unsigned int code, int value)  函数定义
 10         // {
 11         //     int disposition;
 12 
 13         //     disposition = input_get_disposition(dev, type, code, &value);
 14         //    // static int input_get_disposition(struct input_dev *dev, unsigned int type, unsigned int code, int *pval)    函数定义
 15         //    // {
 16         //    //     int disposition = INPUT_IGNORE_EVENT;
 17         //    //     int value = *pval;
 18 
 19         //    //     switch (type) {
 20 
 21         //    //     case EV_SYN:        
 22         //    //         switch (code) {
 23         //    //         case SYN_CONFIG:
 24         //    //             disposition = INPUT_PASS_TO_ALL;
 25         //    //             break;
 26 
 27         //    //         case SYN_REPORT:    "my_key"调用input_event(dev, EV_SYN, SYN_REPORT, 0);
 28         //    //             disposition = INPUT_PASS_TO_HANDLERS | INPUT_FLUSH;
 29         //    //             break;
 30         //    //         case SYN_MT_REPORT:
 31         //    //             disposition = INPUT_PASS_TO_HANDLERS;
 32         //    //             break;
 33         //    //         }
 34         //    //         break;
 35 
 36         //    //     case EV_KEY:            "my_key"调用input_event(dev, EV_KEY, code, !!value);
 37         //    //         if (is_event_supported(code, dev->keybit, KEY_MAX)) {
 38 
 39         //    //             /* auto-repeat bypasses state updates */
 40         //    //             if (value == 2) {
 41         //    //                 disposition = INPUT_PASS_TO_HANDLERS;
 42         //    //                 break;
 43         //    //             }
 44 
 45         //    //             if (!!test_bit(code, dev->key) != !!value) {    若dev->key对应code的状态(0/1)和value不同则执行
 46         //    //                 __change_bit(code, dev->key);        反向dev->key对应code的状态
 47         //    //                 disposition = INPUT_PASS_TO_HANDLERS;
 48         //    //             }
 49         //    //         }
 50         //    //         break;
 51         //    //     case EV_SW:    ......    这里分析忽略不相关的type
 52         //    //     case EV_ABS:    ......                    
 53         //    //     case EV_REL:    ......
 54         //    //     case EV_MSC:    ......
 55         //    //     case EV_LED:    ......
 56         //    //     case EV_SND:    ......
 57         //    //     case EV_REP:    ......
 58         //    //     case EV_FF:    ......
 59         //    //     case EV_PWR:    ......
 60         //    //     }
 61         //    //     *pval = value;
 62         //    //     return disposition;
 63         //    // }
 64             
 65         //     if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)
 66         //         dev->event(dev, type, code, value);
 67 
 68         //     if (disposition & INPUT_PASS_TO_HANDLERS) {        input_event(dev, EV_KEY, code, !!value)和input_event(dev, EV_SYN, SYN_REPORT, 0)
 69         //         struct input_value *v;                下disposition都有INPUT_PASS_TO_HANDLERS状态,执行两次
 70 
 71         //         if (disposition & INPUT_SLOT) {
 72         //             v = &dev->vals[dev->num_vals++];
 73         //             v->type = EV_ABS;
 74         //             v->code = ABS_MT_SLOT;
 75         //             v->value = dev->mt->slot;
 76         //         }
 77 
 78         //         v = &dev->vals[dev->num_vals++];    //调用input_event(dev, EV_SYN, SYN_REPORT, 0)后,dev->num_vals=2
 79         //         v->type = type;
 80         //         v->code = code;
 81         //         v->value = value;
 82         //     }
 83 
 84         //     if (disposition & INPUT_FLUSH) {    input_event(dev, EV_SYN, SYN_REPORT, 0),disposition有INPUT_FLUSH状态
 85         //         if (dev->num_vals >= 2)        
 86         //             input_pass_values(dev, dev->vals, dev->num_vals);    
 87         //            // static void input_pass_values(struct input_dev *dev, struct input_value *vals, unsigned int count)    函数定义
 88         //            // {
 89         //            //     struct input_handle *handle;
 90         //            //     struct input_value *v;
 91 
 92         //            //     handle = rcu_dereference(dev->grab);    这里dev->grab=0,可以通过EVIOCGRAB ioctl改变
 93         //            //     if (handle) {
 94         //            //         count = input_to_handler(handle, vals, count);
 95         //            //     } else {
 96         //            //         list_for_each_entry_rcu(handle, &dev->h_list, d_node)    通过dev->h_list找到handle
 97         //            //             if (handle->open)
 98         //            //                 count = input_to_handler(handle, vals, count);
 99         //            //                 //static unsigned int input_to_handler(struct input_handle *handle,    函数定义
100         //            //                 //            struct input_value *vals, unsigned int count)
101         //            //                 //{
102         //            //                 //    struct input_handler *handler = handle->handler;
103         //            //                 //    struct input_value *end = vals;
104         //            //                 //    struct input_value *v;
105 
106         //            //                 //    for (v = vals; v != vals + count; v++) {    
107         //            //                 //        if (handler->filter &&        evdev的handler->filte=NULL
108         //            //                 //            handler->filter(handle, v->type, v->code, v->value))
109         //            //                 //            continue;
110         //            //                 //        if (end != v)
111         //            //                 //            *end = *v;
112         //            //                 //        end++;
113         //            //                 //    }
114 
115         //            //                 //    count = end - vals;    这里count=2
116 
117         //            //                 //    if (handler->events)    直接调用evdev_handler.events=evdev_events
118         //            //                 //        handler->events(handle, vals, count);
119         //            //                 //    else if (handler->event)
120         //            //                 //        for (v = vals; v != end; v++)
121         //            //                 //            handler->event(handle, v->type, v->code, v->value);
122 
123         //            //                 //    return count;
124         //            //                 //}
125         //            //     }
126         //            //     /* trigger auto repeat for key events */
127         //            //     for (v = vals; v != vals + count; v++) {
128         //            //         if (v->type == EV_KEY && v->value != 2) {   能进来这里v->type=EV_SYN,故不执行
129         //            //             if (v->value)
130         //            //                 input_start_autorepeat(dev, v->code);
131         //            //             else
132         //            //                 input_stop_autorepeat(dev);
133         //            //         }
134         //            //     }
135         //            // }
136         //         dev->num_vals = 0;
137         //     } else if (dev->num_vals >= dev->max_vals - 2) {
138         //         dev->vals[dev->num_vals++] = input_value_sync;
139         //         input_pass_values(dev, dev->vals, dev->num_vals);
140         //         dev->num_vals = 0;
141         //     }
142 
143         // }
144     }
145 }

从以上源码可以得知,单一调用input_report_key(input_mykey_dev, KEY_ENTER, 0/1),无法上报,因为没有EV_SYN刷新。

除了evdev_events的源码没分析外,我们仍然不能解决我们上面提的两个问题,那么解决那两个问题肯定是在evdev_events里面。

 

evdev_events在linux3.10/drivers/input/evdev.c定义。

忽略部分冗余代码,并加上部分源码注释:

 1 static void evdev_events(struct input_handle *handle,
 2              const struct input_value *vals, unsigned int count)
 3 {
 4     struct evdev *evdev = handle->private;
 5     struct evdev_client *client;
 6     ktime_t time_mono, time_real;
 7 
 8     time_mono = ktime_get();
 9     time_real = ktime_sub(time_mono, ktime_get_monotonic_offset());
10 
11 
12     client = rcu_dereference(evdev->grab);        //这里evdev->grab=0,可以通过EVIOCGRAB ioctl改变
13 
14     if (client)
15         evdev_pass_values(client, vals, count, time_mono, time_real);
16     else
17         list_for_each_entry_rcu(client, &evdev->client_list, node)
18             evdev_pass_values(client, vals, count, time_mono, time_real);
19             //static void evdev_pass_values(struct evdev_client *client, const struct input_value *vals, unsigned int count,
20             //            ktime_t mono, ktime_t real)    函数定义
21             //{
22             //    struct evdev *evdev = client->evdev;
23             //    const struct input_value *v;
24             //    struct input_event event;
25             //    bool wakeup = false;
26 
27             //    event.time = ktime_to_timeval(client->clkid == CLOCK_MONOTONIC ? mono : real);
28 
29             //    for (v = vals; v != vals + count; v++) {
30             //        event.type = v->type;
31             //        event.code = v->code;
32             //        event.value = v->value;
33             //        __pass_event(client, &event);
34             //        static void __pass_event(struct evdev_client *client, const struct input_event *event)
35             //        {
36             //            client->buffer[client->head++] = *event;    这里head比tail领先
37             //            client->head &= client->bufsize - 1;
38 
39             //            if (unlikely(client->head == client->tail)) {    太久没同步client->head饶了一圈
40             //                /*
41             //                 * This effectively "drops" all unconsumed events, leaving
42             //                 * EV_SYN/SYN_DROPPED plus the newest event in the queue.
43             //                 */
44             //                client->tail = (client->head - 2) & (client->bufsize - 1);
45 
46             //                client->buffer[client->tail].time = event->time;
47             //                client->buffer[client->tail].type = EV_SYN;
48             //                client->buffer[client->tail].code = SYN_DROPPED;
49             //                client->buffer[client->tail].value = 0;
50 
51             //                client->packet_head = client->tail;
52             //            }
53 
54             //            if (event->type == EV_SYN && event->code == SYN_REPORT) {
55             //                client->packet_head = client->head;        问题1:改变packet_head
56             //                kill_fasync(&client->fasync, SIGIO, POLL_IN);    异步通知应用
57             //            }
58             //        }
59             //        if (v->type == EV_SYN && v->code == SYN_REPORT)
60             //            wakeup = true;        
61             //    }
62             //    
63             //    if (wakeup)
64             //        wake_up_interruptible(&evdev->wait);        问题2:在这里唤醒
65             //}
66 
67 }

解决以上两个问题都在__pass_event,对于第一个问题这里需要注意的是client->packet_head领先于client->tail,client->tail追赶client->packet_head。

总结上报过程(前提已经打开设备,这样才会生成client):input_report_key--->input_event--->input_handle_event--->等待input_sync执行

input_sync--->input_event--->input_handle_event--->input_pass_values--->input_to_handler--->evdev_events--->__pass_event

 

在控制台执行busybox hexdump /dev/input/event5 (X=5)

[ 3915.739133] in interrupt
[ 3915.792895] The key val is 0.
00000c0 c772 57bd c3f6 000d 0001 001c 0000 0000
00000d0 c772 57bd c3f6 000d 0000 0000 0000 0000
[ 3915.860499] in interrupt
[ 3915.920814] The key val is 1.
00000e0 c773 57bd 7d86 0000 0001 001c 0001 0000
00000f0 c773 57bd 7d86 0000 0000 0000 0000 0000
注意,这里是小端
0x57bdc772 是秒,0x000dc3f6是毫秒,0x0001是type=EV_KEY,0x001c是code=KEY_ENTER,0x00000001是value

 

到此input子系统的按键例子已经分析完~

 

posted @ 2016-08-25 01:02  Kevin_Hwang  阅读(733)  评论(0编辑  收藏  举报