废话不多说,直接进入主题。在驱动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新的列表。

input和hander是通过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除了初始化函数,还有其他函数,后续分析会调用到。

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

 static char *input_devnode(struct device *dev, umode_t *mode)
{
return kasprintf(GFP_KERNEL, "input/%s", dev_name(dev));
} struct class input_class = {
.name = "input",
.devnode = input_devnode,
};
static int __init input_proc_init(void)
{
struct proc_dir_entry *entry; proc_bus_input_dir = proc_mkdir("bus/input", NULL); //input_devices_fileops会调用seq_operations
entry = proc_create("devices", , proc_bus_input_dir,
&input_devices_fileops); entry = proc_create("handlers", , proc_bus_input_dir,
&input_handlers_fileops); return ;
} static int __init input_init(void)
{
int err; err = class_register(&input_class); err = input_proc_init(); err = register_chrdev_region(MKDEV(INPUT_MAJOR, ), //#define INPUT_MAJOR 13
INPUT_MAX_CHAR_DEVICES, "input"); //#define INPUT_MAX_CHAR_DEVICES 1024 return ;
} subsys_initcall(input_init);
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代码如下:

 #include <linux/io.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_platform.h>
#include <linux/of_address.h>
#include <linux/of_device.h>
#include <linux/of_gpio.h>
#include <linux/pinctrl/consumer.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/sys_config.h>
#include <linux/string.h>
#include <linux/delay.h>
#include <linux/input.h>
#include <linux/spinlock.h> #define DEBUG
#ifdef DEBUG
#define dprintk(fmt, arg...) printk(KERN_DEBUG fmt, ##arg)
#else
#define dprintk(fmt, arg...)
#endif static struct of_device_id mykey_of_match[] = {
{ .compatible = "allwinner,mykey"},
}; struct key_dev {
struct pinctrl *pin;
int gpio;
int irq;
spinlock_t irq_lock;
struct work_struct work;
struct input_dev *input_dev;
}; static void handle_mykey(struct work_struct *work)
{
int val;
unsigned long irqflags;
struct key_dev *p_key = container_of(work, struct key_dev, work); val = __gpio_get_value(p_key->gpio); msleep();
if (val == __gpio_get_value(p_key->gpio)) {
dprintk("The key val is %d.\n", val);
if (val)
input_report_key(p_key->input_dev, KEY_ENTER, ); //松开
else
input_report_key(p_key->input_dev, KEY_ENTER, ); //按下
input_sync(p_key->input_dev);
} spin_lock_irqsave(&p_key->irq_lock, irqflags);
enable_irq(p_key->irq);
spin_unlock_irqrestore(&p_key->irq_lock, irqflags); } static irqreturn_t mykey_irq_handler(int irq, void *dev_id)
{
struct key_dev *p_key = dev_id;
unsigned long irqflags; spin_lock_irqsave(&p_key->irq_lock, irqflags);
disable_irq_nosync(p_key->irq);
spin_unlock_irqrestore(&p_key->irq_lock, irqflags); dprintk("in interrupt\n");
schedule_work(&p_key->work); //工作队列中断下半部分
return IRQ_HANDLED;
} static int mykey_probe(struct platform_device *pdev)
{
struct device_node *node = pdev->dev.of_node;
struct key_dev *p_key; int ret;
dprintk("Initializing my_key.\n"); p_key = devm_kzalloc(&pdev->dev, sizeof(*p_key), GFP_KERNEL);
if (IS_ERR(p_key)) {
printk(KERN_ERR "Failed to kzalloc p_key.\n");
ret = -;
goto out;
} p_key->pin = devm_pinctrl_get_select(&pdev->dev, "default"); //初始化IO口状态配置
if (IS_ERR(p_key->pin)) {
printk(KERN_ERR "Failed to get_select pin.\n");
ret = -;
goto out;
} p_key->gpio = of_get_named_gpio(node, "mykey-gpio", ); //从设备树得到IO句柄 ret = devm_gpio_request(&pdev->dev, p_key->gpio, NULL);
if (ret) {
printk(KERN_ERR "Failed to request gpio:%d.\n", p_key->gpio);
goto out;
} //获取gpio对应的irq号并申请中断
p_key->irq = gpio_to_irq(p_key->gpio);
ret = devm_request_irq(&pdev->dev, p_key->irq, mykey_irq_handler, IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, "MYKEY_EINT", (void *)p_key);
if (ret) {
printk(KERN_ERR "Failed to request irq: %d.\n", p_key->irq);
goto out;
} p_key->input_dev = devm_input_allocate_device(&pdev->dev); //开启资源回收 & 分配input_dev结构空间并初始化h_list和node
if (IS_ERR(p_key->input_dev)) {
printk(KERN_ERR "Failed to allocate input device.\n");
ret = -;
goto out;
} p_key->input_dev->name = "my_key";
p_key->input_dev->evbit[] = BIT_MASK(EV_KEY); //设置KEY事件
set_bit(KEY_ENTER, p_key->input_dev->keybit); //key事件对应具体操作码
set_bit(KEY_ENTER, p_key->input_dev->key); //把key事件对应具体操作码的状态置1(电路常态是高电平)
ret = input_register_device(p_key->input_dev);
if (ret) {
printk(KERN_ERR "Failed to register input_device.\n");
goto out;
}
INIT_WORK (&p_key->work, handle_mykey); //初始化队列
spin_lock_init(&p_key->irq_lock); platform_set_drvdata(pdev, p_key);
return ;
out:
return ret;
} static int mykey_remove(struct platform_device *pdev)
{
struct key_dev *p_key = platform_get_drvdata(pdev);
flush_work(&p_key->work);
return ;
} static struct platform_driver mykey_driver = {
.probe = mykey_probe,
.remove = mykey_remove,
.driver = {
.name = "mykey",
.owner = THIS_MODULE,
.of_match_table = of_match_ptr(mykey_of_match),
},
};
module_platform_driver(mykey_driver); MODULE_AUTHOR("Kevin Hwang <kevin.hwang@live.com");
MODULE_LICENSE("GPL v2");

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

忽略部分代码如下:

 int input_register_device(struct input_dev *dev)
{
static atomic_t input_no = ATOMIC_INIT();
struct input_devres *devres = NULL;
struct input_handler *handler;
unsigned int packet_size;
const char *path;
int error; if (dev->devres_managed) {
devres = devres_alloc(devm_input_device_unregister,
sizeof(struct input_devres), GFP_KERNEL);
devres->input = dev;
} /* Every input device generates EV_SYN/SYN_REPORT events. */
__set_bit(EV_SYN, dev->evbit); /* KEY_RESERVED is not supposed to be transmitted to userspace. */
__clear_bit(KEY_RESERVED, dev->keybit); /* Make sure that bitmasks not mentioned in dev->evbit are clean. */
input_cleanse_bitmasks(dev); packet_size = input_estimate_events_per_packet(dev); //估算input_dev上报数据需要多少缓存空间,这里pack_size=8
if (dev->hint_events_per_packet < packet_size)
dev->hint_events_per_packet = packet_size; dev->max_vals = max(dev->hint_events_per_packet, packet_size) + ; //对于my_key,max_vals=10
dev->vals = kcalloc(dev->max_vals, sizeof(*dev->vals), GFP_KERNEL); /*
* 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] = ;
dev->rep[REP_PERIOD] = ;
} 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) - ); error = device_add(&dev->dev); 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); list_add_tail(&dev->node, &input_dev_list); //把input_dev的node成员插入到全局input_dev_list list_for_each_entry(handler, &input_handler_list, node) //input_dev和input_handler匹配
input_attach_handler(dev, handler); if (dev->devres_managed) {
dev_dbg(dev->dev.parent, "%s: registering %s with devres.\n",
__func__, dev_name(&dev->dev));
devres_add(dev->dev.parent, devres);
}
return ;
}

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

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

 static const struct input_device_id evdev_ids[] = {
{ .driver_info = }, /* 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, //#define EVDEV_MINOR_BASE 64
.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);
} module_init(evdev_init);
module_exit(evdev_exit);

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

忽略部分代码:

 int input_register_handler(struct input_handler *handler)
{
struct input_dev *dev; INIT_LIST_HEAD(&handler->h_list); //初始化input_handler的h_list list_add_tail(&handler->node, &input_handler_list); //把input_handler的node成员插入到全局input_handler_list list_for_each_entry(dev, &input_dev_list, node)
input_attach_handler(dev, handler); //input_dev和input_handler匹配 return ;
}

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

 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 (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;
//bitmap_subset:argv1是否是argv2子集,若真返回1,假返回0。argv3是要校验的位数
//这里evdev是空集(id->xxxbit都是0),是所有input_dev的子集
if (!bitmap_subset(id->evbit, dev->evbit, EV_MAX))
continue; if (!bitmap_subset(id->keybit, dev->keybit, KEY_MAX))
continue; if (!bitmap_subset(id->relbit, dev->relbit, REL_MAX))
continue; if (!bitmap_subset(id->absbit, dev->absbit, ABS_MAX))
continue; if (!bitmap_subset(id->mscbit, dev->mscbit, MSC_MAX))
continue; if (!bitmap_subset(id->ledbit, dev->ledbit, LED_MAX))
continue; if (!bitmap_subset(id->sndbit, dev->sndbit, SND_MAX))
continue; if (!bitmap_subset(id->ffbit, dev->ffbit, FF_MAX))
continue; if (!bitmap_subset(id->swbit, dev->swbit, SW_MAX))
continue; if (!handler->match || handler->match(handler, dev))
return id;
} return NULL;
} 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_attach_handler做了两件事情,匹配和连接相应handler,因evdev_ids匹配所有input_device,所以这里就直接调用handler->connect = evdev_connect。

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

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

 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,
}; 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;
//生成64~64+32的次设备号
minor = input_get_new_minor(EVDEV_MINOR_BASE, EVDEV_MINORS, true); evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL); INIT_LIST_HEAD(&evdev->client_list);
spin_lock_init(&evdev->client_lock);
mutex_init(&evdev->mutex);
init_waitqueue_head(&evdev->wait);
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; //初始化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); error = input_register_handle(&evdev->handle); // int input_register_handle(struct input_handle *handle)
// {
// struct input_handler *handler = handle->handler;
// struct input_dev *dev = handle->dev; // list_add_tail_rcu(&handle->d_node, &dev->h_list); 把input_handle的d_node加入input_dev的h_list // list_add_tail_rcu(&handle->h_node, &handler->h_list); 把input_handle的h_node加入input_handler的h_list // return 0;
// } // 初始化并注册字符设备
cdev_init(&evdev->cdev, &evdev_fops);
evdev->cdev.kobj.parent = &evdev->dev.kobj;
error = cdev_add(&evdev->cdev, evdev->dev.devt, ); error = device_add(&evdev->dev); return ;
}

比较有趣的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。

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

 static int evdev_open(struct inode *inode, struct file *file)
{
struct evdev *evdev = container_of(inode->i_cdev, struct evdev, cdev);
unsigned int bufsize = evdev_compute_buffer_size(evdev->handle.dev); //bufsize=64
// static unsigned int evdev_compute_buffer_size(struct input_dev *dev)  函数定义
// {
// unsigned int n_events =
// max(dev->hint_events_per_packet * EVDEV_BUF_PACKETS, #define EVDEV_BUF_PACKETS 8
// EVDEV_MIN_BUFFER_SIZE); // return roundup_pow_of_two(n_events); 8*8=64刚好是2^6
// }
unsigned int size = sizeof(struct evdev_client) +
bufsize * sizeof(struct input_event);
struct evdev_client *client;
int error; client = kzalloc(size, GFP_KERNEL | __GFP_NOWARN); client->bufsize = bufsize;
spin_lock_init(&client->buffer_lock);
snprintf(client->name, sizeof(client->name), "%s-%d",
dev_name(&evdev->dev), task_tgid_vnr(current));
client->evdev = evdev;
evdev_attach_client(evdev, client); //把client的node插入到evdev的client_list // static void evdev_attach_client(struct evdev *evdev, struct evdev_client *client) 函数定义
// {
// spin_lock(&evdev->client_lock);
// list_add_tail_rcu(&client->node, &evdev->client_list);
// spin_unlock(&evdev->client_lock);
// } error = evdev_open_device(evdev);
// static int evdev_open_device(struct evdev *evdev)  函数定义
// {
// int retval;
// if (!evdev->exist)
// retval = -ENODEV;
// else if (!evdev->open++) { 第一次open才调用input_open_device
// retval = input_open_device(&evdev->handle);
// }
// return retval;
//}
file->private_data = client; //以后操作文件都通过client
return ;
}

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

忽略部分冗余代码:

 int input_open_device(struct input_handle *handle)
{
struct input_dev *dev = handle->dev;
int retval; handle->open++; if (!dev->users++ && dev->open) //"my_key"的dev->open = NULL,不执行dev->open(dev)
retval = dev->open(dev); return retval;
}

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

既然已经open成功了,后面看看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;
size_t read = ;
int error; for (;;) {
if (!evdev->exist)
return -ENODEV; if (client->packet_head == client->tail &&
(file->f_flags & O_NONBLOCK))
return -EAGAIN; while (read + input_event_size() <= count &&
evdev_fetch_next_event(client, &event)) { //evdev_fetch_next_event到bufsize次就会逻辑假
// static int evdev_fetch_next_event(struct evdev_client *client, struct input_event *event)  函数定义
// {
// int have_event;
// 通过client->tail &= client->bufsize - 1会使client->packet_head == client->tail
// have_event = client->packet_head != client->tail;
// if (have_event) {
// *event = client->buffer[client->tail++];
// client->tail &= client->bufsize - 1;
// }
// return have_event;
// } if (input_event_to_user(buffer + read, &event)) //copy_to_user多了一层壳而已
return -EFAULT;
// int input_event_to_user(char __user *buffer, const struct input_event *event)  函数定义
// {
// if (copy_to_user(buffer, event, sizeof(struct input_event)))
// return -EFAULT;
// }
read += input_event_size();
} if (read) //如果读到数据
break; if (!(file->f_flags & O_NONBLOCK)) { //如果阻塞
error = wait_event_interruptible(evdev->wait, //唤醒判断client有无数据或者evdev release
client->packet_head != client->tail ||
!evdev->exist);
if (error)
return error;
}
} return read;
}

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

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定义。

 static inline void input_report_key(struct input_dev *dev, unsigned int code, int value)
{
input_event(dev, EV_KEY, code, !!value);
} static inline void input_sync(struct input_dev *dev)
{
input_event(dev, EV_SYN, SYN_REPORT, );
}

可见最终还是调用input_event,其在linux-3.10/drivers/input/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)) {
input_handle_event(dev, type, code, value);
// static void input_handle_event(struct input_dev *dev,
// unsigned int type, unsigned int code, int value)  函数定义
// {
// int disposition; // disposition = input_get_disposition(dev, type, code, &value);
// // static int input_get_disposition(struct input_dev *dev, unsigned int type, unsigned int code, int *pval) 函数定义
// // {
// // int disposition = INPUT_IGNORE_EVENT;
// // int value = *pval; // // switch (type) { // // case EV_SYN:
// // switch (code) {
// // case SYN_CONFIG:
// // disposition = INPUT_PASS_TO_ALL;
// // break; // // case SYN_REPORT: "my_key"调用input_event(dev, EV_SYN, SYN_REPORT, 0);
// // disposition = INPUT_PASS_TO_HANDLERS | INPUT_FLUSH;
// // break;
// // case SYN_MT_REPORT:
// // disposition = INPUT_PASS_TO_HANDLERS;
// // break;
// // }
// // break; // // case EV_KEY: "my_key"调用input_event(dev, EV_KEY, code, !!value);
// // if (is_event_supported(code, dev->keybit, KEY_MAX)) { // // /* auto-repeat bypasses state updates */
// // if (value == 2) {
// // disposition = INPUT_PASS_TO_HANDLERS;
// // break;
// // } // // if (!!test_bit(code, dev->key) != !!value) { 若dev->key对应code的状态(0/1)和value不同则执行
// // __change_bit(code, dev->key); 反向dev->key对应code的状态
// // disposition = INPUT_PASS_TO_HANDLERS;
// // }
// // }
// // break;
// // case EV_SW: ...... 这里分析忽略不相关的type
// // case EV_ABS: ......
// // case EV_REL: ......
// // case EV_MSC: ......
// // case EV_LED: ......
// // case EV_SND: ......
// // case EV_REP: ......
// // case EV_FF: ......
// // case EV_PWR: ......
// // }
// // *pval = value;
// // return disposition;
// // } // if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)
// dev->event(dev, type, code, value); // if (disposition & INPUT_PASS_TO_HANDLERS) { input_event(dev, EV_KEY, code, !!value)和input_event(dev, EV_SYN, SYN_REPORT, 0)
// struct input_value *v; 下disposition都有INPUT_PASS_TO_HANDLERS状态,执行两次 // if (disposition & INPUT_SLOT) {
// v = &dev->vals[dev->num_vals++];
// v->type = EV_ABS;
// v->code = ABS_MT_SLOT;
// v->value = dev->mt->slot;
// } // v = &dev->vals[dev->num_vals++]; //调用input_event(dev, EV_SYN, SYN_REPORT, 0)后,dev->num_vals=2
// v->type = type;
// v->code = code;
// v->value = value;
// } // if (disposition & INPUT_FLUSH) { input_event(dev, EV_SYN, SYN_REPORT, 0),disposition有INPUT_FLUSH状态
// if (dev->num_vals >= 2)
// 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) 函数定义
// // {
// // struct input_handle *handle;
// // struct input_value *v; // // handle = rcu_dereference(dev->grab); 这里dev->grab=0,可以通过EVIOCGRAB ioctl改变
// // if (handle) {
// // count = input_to_handler(handle, vals, count);
// // } else {
// // list_for_each_entry_rcu(handle, &dev->h_list, d_node) 通过dev->h_list找到handle
// // if (handle->open)
// // count = input_to_handler(handle, vals, count);
// // //static unsigned int input_to_handler(struct input_handle *handle, 函数定义
// // // struct input_value *vals, unsigned int count)
// // //{
// // // struct input_handler *handler = handle->handler;
// // // struct input_value *end = vals;
// // // struct input_value *v; // // // for (v = vals; v != vals + count; v++) {
// // // if (handler->filter && evdev的handler->filte=NULL
// // // handler->filter(handle, v->type, v->code, v->value))
// // // continue;
// // // if (end != v)
// // // *end = *v;
// // // end++;
// // // } // // // count = end - vals; 这里count=2 // // // if (handler->events) 直接调用evdev_handler.events=evdev_events
// // // handler->events(handle, vals, count);
// // // else if (handler->event)
// // // for (v = vals; v != end; v++)
// // // handler->event(handle, v->type, v->code, v->value); // // // return count;
// // //}
// // }
// // /* trigger auto repeat for key events */
// // for (v = vals; v != vals + count; v++) {
// // if (v->type == EV_KEY && v->value != 2) { 能进来这里v->type=EV_SYN,故不执行
// // if (v->value)
// // input_start_autorepeat(dev, v->code);
// // else
// // input_stop_autorepeat(dev);
// // }
// // }
// // }
// dev->num_vals = 0;
// } else if (dev->num_vals >= dev->max_vals - 2) {
// dev->vals[dev->num_vals++] = input_value_sync;
// input_pass_values(dev, dev->vals, dev->num_vals);
// dev->num_vals = 0;
// } // }
}
}

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

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

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

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

 static void evdev_events(struct input_handle *handle,
const struct input_value *vals, unsigned int count)
{
struct evdev *evdev = handle->private;
struct evdev_client *client;
ktime_t time_mono, time_real; time_mono = ktime_get();
time_real = ktime_sub(time_mono, ktime_get_monotonic_offset()); client = rcu_dereference(evdev->grab); //这里evdev->grab=0,可以通过EVIOCGRAB ioctl改变 if (client)
evdev_pass_values(client, vals, count, time_mono, time_real);
else
list_for_each_entry_rcu(client, &evdev->client_list, node)
evdev_pass_values(client, vals, count, time_mono, time_real);
//static void evdev_pass_values(struct evdev_client *client, const struct input_value *vals, unsigned int count,
// ktime_t mono, ktime_t real) 函数定义
//{
// struct evdev *evdev = client->evdev;
// const struct input_value *v;
// struct input_event event;
// bool wakeup = false; // event.time = ktime_to_timeval(client->clkid == CLOCK_MONOTONIC ? mono : real); // for (v = vals; v != vals + count; v++) {
// event.type = v->type;
// event.code = v->code;
// event.value = v->value;
// __pass_event(client, &event);
// static void __pass_event(struct evdev_client *client, const struct input_event *event)
// {
// client->buffer[client->head++] = *event; 这里head比tail领先
// client->head &= client->bufsize - 1; // if (unlikely(client->head == client->tail)) { 太久没同步client->head饶了一圈
// /*
// * This effectively "drops" all unconsumed events, leaving
// * EV_SYN/SYN_DROPPED plus the newest event in the queue.
// */
// client->tail = (client->head - 2) & (client->bufsize - 1); // client->buffer[client->tail].time = event->time;
// client->buffer[client->tail].type = EV_SYN;
// client->buffer[client->tail].code = SYN_DROPPED;
// client->buffer[client->tail].value = 0; // client->packet_head = client->tail;
// } // if (event->type == EV_SYN && event->code == SYN_REPORT) {
// client->packet_head = client->head; 问题1:改变packet_head
// kill_fasync(&client->fasync, SIGIO, POLL_IN); 异步通知应用
// }
// }
// if (v->type == EV_SYN && v->code == SYN_REPORT)
// wakeup = true;
// }
//
// if (wakeup)
// wake_up_interruptible(&evdev->wait); 问题2:在这里唤醒
//} }

解决以上两个问题都在__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子系统的按键例子已经分析完~

最新文章

  1. Go语言实战 - revel框架教程之CSRF(跨站请求伪造)保护
  2. href=&quot;javascript:xxx(this);&quot;和onclick=&quot;javascript:xxx(this);&quot;的区别
  3. ie下不显示图片
  4. Spring学习8-SSH+Log4j黄金整合
  5. telnet的使用
  6. 使用charles proxy for Mac来抓取手机App的网络包
  7. 给小班讲stl 之 map、sort、优先队列
  8. python切片练习
  9. struts2对action中的方法进行输入校验(2)
  10. 更改zendstudio花括号匹配显示的方法
  11. 剑指Offer——记中国银行体检之旅
  12. java.util.zip.ZipException: invalid entry size
  13. cxf+spring+restful简单接口搭建
  14. [leetcode]97. Interleaving String能否构成交错字符串
  15. 【问题解决】连接mysql 8错误:authentication plugin &#39;caching_sha2_password
  16. Mysql 5.7 忘记root密码或重置密码的详细方法
  17. 最完整Android Studio插件整理 (转)
  18. Windows server2012 IIs 8 自定义日志记录
  19. Duplicate spring bean id
  20. Flask系列(六)Flask实例化补充及信号

热门文章

  1. 新机git及github sshkey简单配置
  2. 自学 iOS 开发的一些经验 - 转自无网不剩的博客
  3. 转 : Java的版本特性与历史
  4. (转)Java编译后产生class文件的命名规则
  5. java socket - 传递对象
  6. Kubernetes 在知乎上的应用
  7. vue2 遇到的问题汇集ing
  8. 腾讯开源手游热更新方案,Unity3D下的Lua编程
  9. 调用http接口的工具类
  10. Ceph配置项动态变更机制浅析