Linux输入子系统框架分析

输入子系统简介:

1.linux系统支持的输入设备很多,例如键盘、鼠标、触摸屏、手柄或者一些体感设备等等,linux是如何管理如此之多的不同类型、不同原理、不同输入信息的设备呢?其实就是通过input输入子系统这套软件体系来完成的。

2.输入子系统有设备驱动层、输入子系统核心、事件处理层三部分组成。

·  驱动层:将底层的硬件输入转化为统一事件形式,向输入核心(input core)汇报。

    输入子系统核心:承上启下,为驱动层提供输入设备注册与操作的接口,如:input_register_device;通知事件处理层对事件进行处理;在/proc下产生相应的设备信息

事件处理层:主要和用户控件交互。

输入子系统框架

一个输入事件,如鼠标的移动通过driver->input core -> event handler->user speace的顺序到达用户空间。

input core层分析:

input core 层位于 drivers/input/input.c
 

static int __init input_init(void)
{

    err = class_register(&input_class); //注册类,在/sys/class/下

    err = input_proc_init(); //在proc下建立相应文件

    err = register_chrdev_region(MKDEV(INPUT_MAJOR, 0), //注册input字符设备 主设备号为13 
                     INPUT_MAX_CHAR_DEVICES, "input")
}

在这里我们并没有看到file_operation结构体,这是因为register_chrdev_region需要配合cdev_init 和cdev_add使用。

在input.c中向设备驱动层和事件处理层提供了统一的接口。

先来认识一下input_dev结构:

struct input_dev {
	const char *name;//设备名
	const char *phys;//设备的物理路径
	const char *uniq;//设备的唯一识别码
	struct input_id id;//设备id,用于和event handler匹配

	unsigned long propbit[BITS_TO_LONGS(INPUT_PROP_CNT)];//bitmap of device properties and quirks

	unsigned long evbit[BITS_TO_LONGS(EV_CNT)];//设备支持的事件类型
	unsigned long keybit[BITS_TO_LONGS(KEY_CNT)];//按键事件
	unsigned long relbit[BITS_TO_LONGS(REL_CNT)];//相对坐标
	unsigned long absbit[BITS_TO_LONGS(ABS_CNT)];//绝对坐标
	unsigned long mscbit[BITS_TO_LONGS(MSC_CNT)];//其他杂项事件
	unsigned long ledbit[BITS_TO_LONGS(LED_CNT)];//指示灯
	unsigned long sndbit[BITS_TO_LONGS(SND_CNT)];//声音或报警
	unsigned long ffbit[BITS_TO_LONGS(FF_CNT)];//作用力事件
	unsigned long swbit[BITS_TO_LONGS(SW_CNT)];//开关

	unsigned int hint_events_per_packet;

	unsigned int keycodemax; //设备支持按键值最大个数
	unsigned int keycodesize; //键值表中元素的大小
	void *keycode;   //按键值

	int (*setkeycode)(struct input_dev *dev,
			  const struct input_keymap_entry *ke,
			  unsigned int *old_keycode);//设置键值
	int (*getkeycode)(struct input_dev *dev,
			  struct input_keymap_entry *ke);//获取键值

	struct ff_device *ff;

	unsigned int repeat_key;
	struct timer_list timer; //连击时定时器

	int rep[REP_CNT];

	struct input_mt *mt;

	struct input_absinfo *absinfo;

	unsigned long key[BITS_TO_LONGS(KEY_CNT)];
	unsigned long led[BITS_TO_LONGS(LED_CNT)];
	unsigned long snd[BITS_TO_LONGS(SND_CNT)];
	unsigned long sw[BITS_TO_LONGS(SW_CNT)];

	int (*open)(struct input_dev *dev);
	void (*close)(struct input_dev *dev);
	int (*flush)(struct input_dev *dev, struct file *file);
	int (*event)(struct input_dev *dev, unsigned int type, unsigned int code, int value); //事件处理

	struct input_handle __rcu *grab;

	spinlock_t event_lock;
	struct mutex mutex;

	unsigned int users;
	bool going_away;

	struct device dev; //设备结构

	struct list_head	h_list; //handle list
	struct list_head	node;   //input dev list

	unsigned int num_vals;
	unsigned int max_vals;
	struct input_value *vals;

	bool devres_managed;
};
struct input_handler {

	void *private;

	void (*event)(struct input_handle *handle, unsigned int type, unsigned int code, int value);
	void (*events)(struct input_handle *handle,
		       const struct input_value *vals, unsigned int count);
	bool (*filter)(struct input_handle *handle, unsigned int type, unsigned int code, int value);
	bool (*match)(struct input_handler *handler, struct input_dev *dev);
	int (*connect)(struct input_handler *handler, struct input_dev *dev, const struct input_device_id *id);
	void (*disconnect)(struct input_handle *handle);
	void (*start)(struct input_handle *handle);

	bool legacy_minors;
	int minor;
	const char *name;

	const struct input_device_id *id_table;

	struct list_head	h_list;
	struct list_head	node;
};

input.c向事件层提供了统一的接口  int input_register_handler(struct input_handler *handler)

在evdev.c中:

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

在event_handler中提供了几个关键接口在后面绑定的时候会用到。

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,
};

input_handler中id_table是与input_dev中的id进行匹配的关键。
 

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

正式来看一下int input_register_handler(struct input_handler *handler)

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

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

	list_add_tail(&handler->node, &input_handler_list); //将handler->node挂在input_handler_list中

	list_for_each_entry(dev, &input_dev_list, node) //遍历input_dev_list
		input_attach_handler(dev, handler); //对input_dev_list中的每一项都执行attach函数

	input_wakeup_procfs_readers();

	return 0;
}

从上可以看出,对于input_handler_list中的每一项都执行input_attach_handler来进行匹配。

具体匹配过程如下:

static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
{
	const struct input_device_id *id;

	id = input_match_device(handler, dev); //匹配过程
	if (!id)
		return -ENODEV;
error = handler->connect(handler, dev, id); //如果匹配成功则调用handler->connect函数去绑定。
	
}
static const struct input_device_id *input_match_device(struct input_handler *handler,
							struct input_dev *dev)
{
	const struct input_device_id *id;
    //遍历handler->id_table与dev进行匹配。
	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;

		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)) //匹配成功返回handler->id_table
			return id;
	}

	return NULL;
}

匹配成功以后就会调用handler->connect(handler, dev, id);那么handler->connect是在哪里呢? 其实就是上文中的evdev_connect函数。

input core还为设备驱动层提供了统一的注册函数。int input_register_device(struct input_dev *dev)

int input_register_device(struct input_dev *dev)
{

	struct input_handler *handler;

	packet_size = input_estimate_events_per_packet(dev);
	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) + 2;
	dev->vals = kcalloc(dev->max_vals, sizeof(*dev->vals), GFP_KERNEL);

	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); //将设备添加进系统中去


	path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);

	error = mutex_lock_interruptible(&input_mutex);

	list_add_tail(&dev->node, &input_dev_list); //将设备添加进 input_dev_list链表中

list_for_each_entry(handler, &input_handler_list, node) //遍历input_handler_list链表
		input_attach_handler(dev, handler);

	input_wakeup_procfs_readers();

}

在input_register_device函数中,比较关键的是对于input_handler_list链表的遍历,对于链表中的每一项,都调用input_attach_handler去进行匹配,匹配过程同上,不在赘述。

接着看一下connect过程。

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;

	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;

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

	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); //注册input_handle
	if (error)
		goto err_free_evdev;

	cdev_init(&evdev->cdev, &evdev_fops); //将evdev->cdev注册为字符设备驱动程序,evdev_fops提供了相应的读写操作函数。
	evdev->cdev.kobj.parent = &evdev->dev.kobj;
	error = cdev_add(&evdev->cdev, evdev->dev.devt, 1);
	if (error)
		goto err_unregister_handle;

	error = device_add(&evdev->dev);

}

在input_register_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);

	list_add_tail_rcu(&handle->h_node, &handler->h_list);
}

handle->dev是指向input_dev的,因此上面就是把handle->d_node 放入input_dev驱动设备的h_list链表中,即input_dev驱动设备的h_list链表就指向handle->d_node.

同样input_handler驱动处理结构体的h_list也指向了handle->h_node。

最终如下所示:

input_dev的h_list和 input_handler的h_list都指向了同一个handle结构体,然后通过.h_list来找到handle的成员dev和handler,便能找到对方,如此建立了连接。

建立连接后,如何读取evdev.c的evdev_handler->.fops->.read函数?

事件驱动的.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;
	size_t read = 0;

	for (;;) {

		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 (!(file->f_flags & O_NONBLOCK)) { //非阻塞情况下没有数据就休眠
			error = wait_event_interruptible(evdev->wait,
					client->packet_head != client->tail ||
					!evdev->exist); 
		}
	}
	return read;
}

上面可以看出如果没有数据则read函数就会休眠,那么休眠以后由谁来唤醒呢?

通过搜索发现是在evdev_event函数中唤醒的。

evdev_event-> evdev_events -> evdev_pass_values -> wake_up_interruptible(&evdev->wait);

那么又是谁调用的evdev_event函数呢?

直接看内核例子 (driver/input/keyboard/gpio_key.c)中 gpio_keys_isr()函数代码就知道

static irqreturn_t gpio_keys_irq_isr(int irq, void *dev_id)
{
    ...
		input_event(input, EV_KEY, button->code, 1);
		input_sync(input);
    ...

}

应该是通过input_event函数

  input_event
     input_handle_event(dev, type, code, value);
        input_pass_values(dev, dev->vals, dev->num_vals);
             
        

在input_pass_values中

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);
	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 &&
		    handler->filter(handle, v->type, v->code, v->value))
			continue;
		if (end != v)
			*end = *v;
		end++;
	}

	count = end - vals;
	if (!count)
		return 0;

	if (handler->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);//最终调用到evdev_event

	return count;
}

参考:https://www.cnblogs.com/lifexy/p/7542989.html

https://www.cnblogs.com/jason-lu/p/3156411.html

https://blog.csdn.net/qq_35865125/article/details/80637809

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值