input子系统框架分析

1. input框架介绍:

Linux input子系统主要分为三层:驱动、输入core、事件处理层。

驱动根据core提供的接口,向上报告发生的动作(input_report_**)。

core根据驱动的类型,分发这个报告给对应的事件处理层处理(input_event——>input_handle_event——>input_pass_event)

事件处理层把数据变化反应到设备模型的文件中(事件缓冲区),并通知这些设备文件上等待的进程。

驱动把输入设备注册到输入子系统,然后子系统调用匹配到的input handler的connect函数,来创建一个具体的设备结点。

一类input  handler可以和多个硬件设备相关联,创建多个设备结点(如evdev handler可以创建/dev/input/event0,/dev/input/event1)。一个设备也可以与多个input handler关联,创建多个设备结点(如触摸屏可以创建/dev/input/event2,/dev/input/ts0)。


2. input框架的主要数据结构

[cpp] view plain copy
print ?
  1. struct input_dev {  
  2.     const char *name;  
  3.     const char *phys;  
  4.     const char *uniq;  
  5.     struct input_id id; //输入设备的设备信息  
  6.   
  7.     unsigned long propbit[BITS_TO_LONGS(INPUT_PROP_CNT)];  
  8.   
  9.     unsigned long evbit[BITS_TO_LONGS(EV_CNT)]; //设备支持的事件类型  
  10.     unsigned long keybit[BITS_TO_LONGS(KEY_CNT)];//按键事件支持的子事件  
  11.     unsigned long relbit[BITS_TO_LONGS(REL_CNT)];//相对坐标事件支持的子事件  
  12.     unsigned long absbit[BITS_TO_LONGS(ABS_CNT)];//绝对坐标事件支持的子事件  
  13.     unsigned long mscbit[BITS_TO_LONGS(MSC_CNT)];//其它事件支持的子事件  
  14.     unsigned long ledbit[BITS_TO_LONGS(LED_CNT)];//LED事件的子事件  
  15.     unsigned long sndbit[BITS_TO_LONGS(SND_CNT)];//声音事件的子事件  
  16.     unsigned long ffbit[BITS_TO_LONGS(FF_CNT)];//力反馈事件的子事件  
  17.     unsigned long swbit[BITS_TO_LONGS(SW_CNT)];//开关事件的子事件  
  18.   
  19.     unsigned int hint_events_per_packet;  
  20.   
  21.     unsigned int keycodemax;  
  22.     unsigned int keycodesize;  
  23.     void *keycode;  
  24.   
  25.     int (*setkeycode)(struct input_dev *dev,  
  26.               const struct input_keymap_entry *ke,  
  27.               unsigned int *old_keycode);  
  28.     int (*getkeycode)(struct input_dev *dev,  
  29.               struct input_keymap_entry *ke);  
  30.   
  31.     struct ff_device *ff;  
  32.   
  33.     unsigned int repeat_key;//重复按键  
  34.     struct timer_list timer;//定时器  
  35.   
  36.     int rep[REP_CNT];//重复次数  
  37.   
  38.     struct input_mt_slot *mt;  
  39.     int mtsize;  
  40.     int slot;  
  41.     int trkid;  
  42.   
  43.     struct input_absinfo *absinfo;  
  44.   
  45.     unsigned long key[BITS_TO_LONGS(KEY_CNT)];  
  46.     unsigned long led[BITS_TO_LONGS(LED_CNT)];  
  47.     unsigned long snd[BITS_TO_LONGS(SND_CNT)];  
  48.     unsigned long sw[BITS_TO_LONGS(SW_CNT)];  
  49.   
  50.     int (*open)(struct input_dev *dev);  
  51.     void (*close)(struct input_dev *dev);  
  52.     int (*flush)(struct input_dev *dev, struct file *file);  
  53.     int (*event)(struct input_dev *dev, unsigned int type, unsigned int code, int value);  
  54.   
  55.     struct input_handle __rcu *grab;//指向input_handle  
  56.   
  57.     spinlock_t event_lock;  
  58.     struct mutex mutex;  
  59.   
  60.     unsigned int users;  
  61.     bool going_away;  
  62.   
  63.     bool sync;  
  64.   
  65.     struct device dev;  
  66.   
  67.     struct list_head    h_list;//链表头链接该设备关联的input_handle  
  68.     struct list_head    node;//链接到全局input_dev_list  
  69. };  
  70. struct input_handle {  
  71.   
  72.     void *private;  
  73.   
  74.     int open;  
  75.     const char *name;  
  76.   
  77.     struct input_dev *dev;  //指向input_dev  
  78.     struct input_handler *handler;//指向input_handler  
  79.   
  80.     struct list_head    d_node; //通过d_node把input_handler链接到input_dev上的h_list链表头  
  81.     struct list_head    h_node;//通过h_node把input_dev链接到input_handler的h_list链表头  
  82. };  
  83. struct input_handler {  
  84.   
  85.     void *private;  
  86.     /*处理事件函数接口*/  
  87.     void (*event)(struct input_handle *handle, unsigned int type, unsigned int code, int value);  
  88.     bool (*filter)(struct input_handle *handle, unsigned int type, unsigned int code, int value);  
  89.     bool (*match)(struct input_handler *handler, struct input_dev *dev);  
  90.     /*建立handler和device的联系*/  
  91.     int (*connect)(struct input_handler *handler, struct input_dev *dev, const struct input_device_id *id);  
  92.     void (*disconnect)(struct input_handle *handle);  
  93.     void (*start)(struct input_handle *handle);  
  94.   
  95.     const struct file_operations *fops;  
  96.     int minor;  
  97.     const char *name;  
  98.   
  99.     const struct input_device_id *id_table; //该handler所支持的input dev id  
  100.   
  101.     struct list_head    h_list;//链表头,指向它所支持的设备链表  
  102.     struct list_head    node;   //链接到全局input_handler_list  
  103. };  
struct input_dev {
	const char *name;
	const char *phys;
	const char *uniq;
	struct input_id id;	//输入设备的设备信息

	unsigned long propbit[BITS_TO_LONGS(INPUT_PROP_CNT)];

	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)];//LED事件的子事件
	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_slot *mt;
	int mtsize;
	int slot;
	int trkid;

	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;//指向input_handle

	spinlock_t event_lock;
	struct mutex mutex;

	unsigned int users;
	bool going_away;

	bool sync;

	struct device dev;

	struct list_head	h_list;//链表头链接该设备关联的input_handle
	struct list_head	node;//链接到全局input_dev_list
};
struct input_handle {

	void *private;

	int open;
	const char *name;

	struct input_dev *dev;	//指向input_dev
	struct input_handler *handler;//指向input_handler

	struct list_head	d_node;	//通过d_node把input_handler链接到input_dev上的h_list链表头
	struct list_head	h_node;//通过h_node把input_dev链接到input_handler的h_list链表头
};
struct input_handler {

	void *private;
	/*处理事件函数接口*/
	void (*event)(struct input_handle *handle, unsigned int type, unsigned int code, int value);
	bool (*filter)(struct input_handle *handle, unsigned int type, unsigned int code, int value);
	bool (*match)(struct input_handler *handler, struct input_dev *dev);
	/*建立handler和device的联系*/
	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);

	const struct file_operations *fops;
	int minor;
	const char *name;

	const struct input_device_id *id_table;	//该handler所支持的input dev id

	struct list_head	h_list;//链表头,指向它所支持的设备链表
	struct list_head	node;	//链接到全局input_handler_list
};
一个input_dev可以关联多个input_handler,每关联一个就通过input_handle的d_node把input_handler链接到input_dev中的h_list链表头,因为input_handle有一个指针指向input_handler。

一个input_handler可以关联多个input_dev,每关联一个就通过input_handle的h_node把input_dev链接到input_handler中的h_list链表头,因为input_handle有一个指针指向input_dev。


3. input  core层的API

[cpp] view plain copy
print ?
  1. int input_register_device(struct input_dev *dev)  
  2. {  
  3.     static atomic_t input_no = ATOMIC_INIT(0);  
  4.     struct input_handler *handler;  
  5.     const char *path;  
  6.     int error;  
  7.   
  8.     /* Every input device generates EV_SYN/SYN_REPORT events. */  
  9.     __set_bit(EV_SYN, dev->evbit); //每个input_dev都支持EV_SYN事件  
  10.   
  11.     /* KEY_RESERVED is not supposed to be transmitted to userspace. */  
  12.     __clear_bit(KEY_RESERVED, dev->keybit);  
  13.   
  14.     /* Make sure that bitmasks not mentioned in dev->evbit are clean. */  
  15.     input_cleanse_bitmasks(dev);  
  16.   
  17.     if (!dev->hint_events_per_packet)  
  18.         dev->hint_events_per_packet =  
  19.                 input_estimate_events_per_packet(dev);  
  20.   
  21.     /* 
  22.      * If delay and period are pre-set by the driver, then autorepeating 
  23.      * is handled by the driver itself and we don't do it in input.c. 
  24.      */  
  25.     init_timer(&dev->timer); //为处理重复击键定义的timer  
  26.     //为自动重复按键定义的  
  27.     if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {  
  28.         dev->timer.data = (long) dev;  
  29.         dev->timer.function = input_repeat_key;  
  30.         dev->rep[REP_DELAY] = 250;  
  31.         dev->rep[REP_PERIOD] = 33;  
  32.     }  
  33.     //获取键值  
  34.     if (!dev->getkeycode)  
  35.         dev->getkeycode = input_default_getkeycode;  
  36.   
  37.     if (!dev->setkeycode)  
  38.         dev->setkeycode = input_default_setkeycode;  
  39.   
  40.     dev_set_name(&dev->dev, "input%ld",  
  41.              (unsigned long) atomic_inc_return(&input_no) - 1);  
  42.     //注册到设备模型中  
  43.     error = device_add(&dev->dev);  
  44.     if (error)  
  45.         return error;  
  46.   
  47.     path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);  
  48.     pr_info("%s as %s\n",  
  49.         dev->name ? dev->name : "Unspecified device",  
  50.         path ? path : "N/A");  
  51.     kfree(path);  
  52.   
  53.     error = mutex_lock_interruptible(&input_mutex);  
  54.     if (error) {  
  55.         device_del(&dev->dev);  
  56.         return error;  
  57.     }  
  58.     //加入到全局链表  
  59.     list_add_tail(&dev->node, &input_dev_list);  
  60.     //对每一个input_handler,执行input_attach_handler  
  61.     list_for_each_entry(handler, &input_handler_list, node)  
  62.         input_attach_handler(dev, handler);  
  63.   
  64.     input_wakeup_procfs_readers();  
  65.   
  66.     mutex_unlock(&input_mutex);  
  67.   
  68.     return 0;  
  69. }  
  70. int input_register_handler(struct input_handler *handler)  
  71. {  
  72.     struct input_dev *dev;  
  73.     int retval;  
  74.   
  75.     retval = mutex_lock_interruptible(&input_mutex);  
  76.     if (retval)  
  77.         return retval;  
  78.   
  79.     INIT_LIST_HEAD(&handler->h_list);  
  80.   
  81.     if (handler->fops != NULL) {  
  82.         if (input_table[handler->minor >> 5]) {  
  83.             retval = -EBUSY;  
  84.             goto out;  
  85.         }  
  86.         input_table[handler->minor >> 5] = handler;    //以input_handler的次设备号右移5位为索引赋值给全局input_table,便于快速查找  
  87.     }  
  88.   
  89.     list_add_tail(&handler->node, &input_handler_list); //插入到全局input_handler_list链表中  
  90.   
  91.     list_for_each_entry(dev, &input_dev_list, node)  
  92.         input_attach_handler(dev, handler); //对每一个input_dev,调用input_attach_handler  
  93.   
  94.     input_wakeup_procfs_readers();  
  95.   
  96.  out:  
  97.     mutex_unlock(&input_mutex);  
  98.     return retval;  
  99. }  
  100. static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)  
  101. {  
  102.     const struct input_device_id *id;  
  103.     int error;  
  104.   
  105.     id = input_match_device(handler, dev);  //input_dev和input_handler之间的匹配  
  106.     if (!id)  
  107.         return -ENODEV;  
  108.   
  109.     error = handler->connect(handler, dev, id);  //调用input_handler的connect建立连接  
  110.     if (error && error != -ENODEV)  
  111.         pr_err("failed to attach handler %s to device %s, error: %d\n",  
  112.                handler->name, kobject_name(&dev->dev.kobj), error);  
  113.   
  114.     return error;  
  115. }  
int input_register_device(struct input_dev *dev)
{
	static atomic_t input_no = ATOMIC_INIT(0);
	struct input_handler *handler;
	const char *path;
	int error;

	/* Every input device generates EV_SYN/SYN_REPORT events. */
	__set_bit(EV_SYN, dev->evbit); //每个input_dev都支持EV_SYN事件

	/* 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);

	if (!dev->hint_events_per_packet)
		dev->hint_events_per_packet =
				input_estimate_events_per_packet(dev);

	/*
	 * 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); //为处理重复击键定义的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);
	if (error)
		return error;

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

	error = mutex_lock_interruptible(&input_mutex);
	if (error) {
		device_del(&dev->dev);
		return error;
	}
	//加入到全局链表
	list_add_tail(&dev->node, &input_dev_list);
	//对每一个input_handler,执行input_attach_handler
	list_for_each_entry(handler, &input_handler_list, node)
		input_attach_handler(dev, handler);

	input_wakeup_procfs_readers();

	mutex_unlock(&input_mutex);

	return 0;
}
int input_register_handler(struct input_handler *handler)
{
	struct input_dev *dev;
	int retval;

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

	INIT_LIST_HEAD(&handler->h_list);

	if (handler->fops != NULL) {
		if (input_table[handler->minor >> 5]) {
			retval = -EBUSY;
			goto out;
		}
		input_table[handler->minor >> 5] = handler;	//以input_handler的次设备号右移5位为索引赋值给全局input_table,便于快速查找
	}

	list_add_tail(&handler->node, &input_handler_list); //插入到全局input_handler_list链表中

	list_for_each_entry(dev, &input_dev_list, node)
		input_attach_handler(dev, handler);	//对每一个input_dev,调用input_attach_handler

	input_wakeup_procfs_readers();

 out:
	mutex_unlock(&input_mutex);
	return retval;
}
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);	//input_dev和input_handler之间的匹配
	if (!id)
		return -ENODEV;

	error = handler->connect(handler, dev, id);	//调用input_handler的connect建立连接
	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_handler evdev来看下connect函数

[cpp] view plain copy
print ?
  1. static int evdev_connect(struct input_handler *handler, struct input_dev *dev,  
  2.              const struct input_device_id *id)  
  3. {  
  4.     struct evdev *evdev;    //evdev结构里面内嵌了input_handle  
  5.     int minor;  
  6.     int error;  
  7.   
  8.     for (minor = 0; minor < EVDEV_MINORS; minor++)  
  9.         if (!evdev_table[minor])  
  10.             break;  
  11.   
  12.     if (minor == EVDEV_MINORS) {  
  13.         pr_err("no more free evdev devices\n");  
  14.         return -ENFILE;  
  15.     }  
  16.   
  17.     evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);  
  18.     if (!evdev)  
  19.         return -ENOMEM;  
  20.   
  21.     INIT_LIST_HEAD(&evdev->client_list);  
  22.     spin_lock_init(&evdev->client_lock);  
  23.     mutex_init(&evdev->mutex);  
  24.     init_waitqueue_head(&evdev->wait);  
  25.   
  26.     dev_set_name(&evdev->dev, "event%d", minor);  
  27.     evdev->exist = true;  
  28.     evdev->minor = minor;  
  29.   
  30.     evdev->handle.dev = input_get_device(dev);  
  31.     evdev->handle.name = dev_name(&evdev->dev);  
  32.     evdev->handle.handler = handler;  
  33.     evdev->handle.private = evdev;  
  34.   
  35.     evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor);  
  36.     evdev->dev.class = &input_class;  
  37.     evdev->dev.parent = &dev->dev;  
  38.     evdev->dev.release = evdev_free;  
  39.     device_initialize(&evdev->dev);  
  40.   
  41.     error = input_register_handle(&evdev->handle);   //注册input_handle,其实就是建立input_dev和input_handler之间关系  
  42.     if (error)  
  43.         goto err_free_evdev;  
  44.   
  45.     error = evdev_install_chrdev(evdev);  
  46.     if (error)  
  47.         goto err_unregister_handle;  
  48.   
  49.     error = device_add(&evdev->dev); //注册到设备模型中  
  50.     if (error)  
  51.         goto err_cleanup_evdev;  
  52.   
  53.     return 0;  
  54.   
  55.  err_cleanup_evdev:  
  56.     evdev_cleanup(evdev);  
  57.  err_unregister_handle:  
  58.     input_unregister_handle(&evdev->handle);  
  59.  err_free_evdev:  
  60.     put_device(&evdev->dev);  
  61.     return error;  
  62. }  
  63. int input_register_handle(struct input_handle *handle)  
  64. {  
  65.     struct input_handler *handler = handle->handler;  
  66.     struct input_dev *dev = handle->dev;  
  67.     int error;  
  68.   
  69.     /* 
  70.      * We take dev->mutex here to prevent race with 
  71.      * input_release_device(). 
  72.      */  
  73.     error = mutex_lock_interruptible(&dev->mutex);  
  74.     if (error)  
  75.         return error;  
  76.   
  77.     /* 
  78.      * Filters go to the head of the list, normal handlers 
  79.      * to the tail. 
  80.      */  
  81.     if (handler->filter)  
  82.         list_add_rcu(&handle->d_node, &dev->h_list);  
  83.     else  
  84.         list_add_tail_rcu(&handle->d_node, &dev->h_list);  
  85.   
  86.     mutex_unlock(&dev->mutex);  
  87.   
  88.     /* 
  89.      * Since we are supposed to be called from ->connect() 
  90.      * which is mutually exclusive with ->disconnect() 
  91.      * we can't be racing with input_unregister_handle() 
  92.      * and so separate lock is not needed here. 
  93.      */  
  94.     list_add_tail_rcu(&handle->h_node, &handler->h_list);  
  95.   
  96.     if (handler->start)  
  97.         handler->start(handle);  
  98.   
  99.     return 0;  
  100. }  
static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
			 const struct input_device_id *id)
{
	struct evdev *evdev;	//evdev结构里面内嵌了input_handle
	int minor;
	int error;

	for (minor = 0; minor < EVDEV_MINORS; minor++)
		if (!evdev_table[minor])
			break;

	if (minor == EVDEV_MINORS) {
		pr_err("no more free evdev devices\n");
		return -ENFILE;
	}

	evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
	if (!evdev)
		return -ENOMEM;

	INIT_LIST_HEAD(&evdev->client_list);
	spin_lock_init(&evdev->client_lock);
	mutex_init(&evdev->mutex);
	init_waitqueue_head(&evdev->wait);

	dev_set_name(&evdev->dev, "event%d", minor);
	evdev->exist = true;
	evdev->minor = minor;

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

	evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + 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,其实就是建立input_dev和input_handler之间关系
	if (error)
		goto err_free_evdev;

	error = evdev_install_chrdev(evdev);
	if (error)
		goto err_unregister_handle;

	error = device_add(&evdev->dev);	//注册到设备模型中
	if (error)
		goto err_cleanup_evdev;

	return 0;

 err_cleanup_evdev:
	evdev_cleanup(evdev);
 err_unregister_handle:
	input_unregister_handle(&evdev->handle);
 err_free_evdev:
	put_device(&evdev->dev);
	return error;
}
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);
	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);

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

	return 0;
}

 上面是这些数据结构的建立,以及它们之间关系建立的分析。下面分析下事件传递的过程。input_report_xxx都是调用input_event函数。 
   
[cpp] view plain copy
print ?
  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.   
  8.         spin_lock_irqsave(&dev->event_lock, flags);  
  9.         add_input_randomness(type, code, value);  
  10.         input_handle_event(dev, type, code, value); //直接调用input_handle_event  
  11.         spin_unlock_irqrestore(&dev->event_lock, flags);  
  12.     }  
  13. }  
  14. static void input_handle_event(struct input_dev *dev,  
  15.                    unsigned int type, unsigned int code, int value)  
  16. {  
  17.     int disposition = INPUT_IGNORE_EVENT;  
  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:  
  28.             if (!dev->sync) {  
  29.                 dev->sync = true;  
  30.                 disposition = INPUT_PASS_TO_HANDLERS;  
  31.             }  
  32.             break;  
  33.         case SYN_MT_REPORT:  
  34.             dev->sync = false;  
  35.             disposition = INPUT_PASS_TO_HANDLERS;  
  36.             break;  
  37.         }  
  38.         break;  
  39.   
  40.     case EV_KEY:  
  41.         if (is_event_supported(code, dev->keybit, KEY_MAX) &&  
  42.             !!test_bit(code, dev->key) != value) {  
  43.   
  44.             if (value != 2) {  
  45.                 __change_bit(code, dev->key);  
  46.                 if (value)  
  47.                     input_start_autorepeat(dev, code);  
  48.                 else  
  49.                     input_stop_autorepeat(dev);  
  50.             }  
  51.   
  52.             disposition = INPUT_PASS_TO_HANDLERS;  
  53.         }  
  54.         break;  
  55.   
  56.     case EV_SW:  
  57.         if (is_event_supported(code, dev->swbit, SW_MAX) &&  
  58.             !!test_bit(code, dev->sw) != value) {  
  59.   
  60.             __change_bit(code, dev->sw);  
  61.             disposition = INPUT_PASS_TO_HANDLERS;  
  62.         }  
  63.         break;  
  64.   
  65.     case EV_ABS:  
  66.         if (is_event_supported(code, dev->absbit, ABS_MAX))  
  67.             disposition = input_handle_abs_event(dev, code, &value);  
  68.   
  69.         break;  
  70.   
  71.     case EV_REL:  
  72.         if (is_event_supported(code, dev->relbit, REL_MAX) && value)  
  73.             disposition = INPUT_PASS_TO_HANDLERS;  
  74.   
  75.         break;  
  76.   
  77.     case EV_MSC:  
  78.         if (is_event_supported(code, dev->mscbit, MSC_MAX))  
  79.             disposition = INPUT_PASS_TO_ALL;  
  80.   
  81.         break;  
  82.   
  83.     case EV_LED:  
  84.         if (is_event_supported(code, dev->ledbit, LED_MAX) &&  
  85.             !!test_bit(code, dev->led) != value) {  
  86.   
  87.             __change_bit(code, dev->led);  
  88.             disposition = INPUT_PASS_TO_ALL;  
  89.         }  
  90.         break;  
  91.   
  92.     case EV_SND:  
  93.         if (is_event_supported(code, dev->sndbit, SND_MAX)) {  
  94.   
  95.             if (!!test_bit(code, dev->snd) != !!value)  
  96.                 __change_bit(code, dev->snd);  
  97.             disposition = INPUT_PASS_TO_ALL;  
  98.         }  
  99.         break;  
  100.   
  101.     case EV_REP:  
  102.         if (code <= REP_MAX && value >= 0 && dev->rep[code] != value) {  
  103.             dev->rep[code] = value;  
  104.             disposition = INPUT_PASS_TO_ALL;  
  105.         }  
  106.         break;  
  107.   
  108.     case EV_FF:  
  109.         if (value >= 0)  
  110.             disposition = INPUT_PASS_TO_ALL;  
  111.         break;  
  112.   
  113.     case EV_PWR:  
  114.         disposition = INPUT_PASS_TO_ALL;  
  115.         break;  
  116.     }  
  117.   
  118.     if (disposition != INPUT_IGNORE_EVENT && type != EV_SYN)  
  119.         dev->sync = false;  
  120.   
  121.     if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)  
  122.         dev->event(dev, type, code, value);  
  123.   
  124.     if (disposition & INPUT_PASS_TO_HANDLERS) //如果需要处理,调用input_pass_event  
  125.         input_pass_event(dev, type, code, value);  
  126. }  
  127. static void input_pass_event(struct input_dev *dev,  
  128.                  unsigned int type, unsigned int code, int value)  
  129. {  
  130.     struct input_handler *handler;  
  131.     struct input_handle *handle;  
  132.   
  133.     rcu_read_lock();  
  134.   
  135.     handle = rcu_dereference(dev->grab); //强制为dev赋的handler  
  136.     if (handle)  
  137.         handle->handler->event(handle, type, code, value);  
  138.     else {  
  139.         bool filtered = false;  
  140.         //如果没有为grab赋值,就会遍历该dev的handle链表  
  141.         list_for_each_entry_rcu(handle, &dev->h_list, d_node) {  
  142.             if (!handle->open)  
  143.                 continue;  
  144.             //如果handle打开,表示该设备已经被一个用户进程使用  
  145.             handler = handle->handler;  
  146.             if (!handler->filter) {  
  147.                 if (filtered)  
  148.                     break;  
  149.   
  150.                 handler->event(handle, type, code, value); //调用event函数  
  151.   
  152.             } else if (handler->filter(handle, type, code, value))  
  153.                 filtered = true;  
  154.         }  
  155.     }  
  156.   
  157.     rcu_read_unlock();  
  158. }  
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);	//直接调用input_handle_event
		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_SYN:
		switch (code) {
		case SYN_CONFIG:
			disposition = INPUT_PASS_TO_ALL;
			break;

		case SYN_REPORT:
			if (!dev->sync) {
				dev->sync = true;
				disposition = INPUT_PASS_TO_HANDLERS;
			}
			break;
		case SYN_MT_REPORT:
			dev->sync = false;
			disposition = INPUT_PASS_TO_HANDLERS;
			break;
		}
		break;

	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;

	case EV_SW:
		if (is_event_supported(code, dev->swbit, SW_MAX) &&
		    !!test_bit(code, dev->sw) != value) {

			__change_bit(code, dev->sw);
			disposition = INPUT_PASS_TO_HANDLERS;
		}
		break;

	case EV_ABS:
		if (is_event_supported(code, dev->absbit, ABS_MAX))
			disposition = input_handle_abs_event(dev, code, &value);

		break;

	case EV_REL:
		if (is_event_supported(code, dev->relbit, REL_MAX) && value)
			disposition = INPUT_PASS_TO_HANDLERS;

		break;

	case EV_MSC:
		if (is_event_supported(code, dev->mscbit, MSC_MAX))
			disposition = INPUT_PASS_TO_ALL;

		break;

	case EV_LED:
		if (is_event_supported(code, dev->ledbit, LED_MAX) &&
		    !!test_bit(code, dev->led) != value) {

			__change_bit(code, dev->led);
			disposition = INPUT_PASS_TO_ALL;
		}
		break;

	case EV_SND:
		if (is_event_supported(code, dev->sndbit, SND_MAX)) {

			if (!!test_bit(code, dev->snd) != !!value)
				__change_bit(code, dev->snd);
			disposition = INPUT_PASS_TO_ALL;
		}
		break;

	case EV_REP:
		if (code <= REP_MAX && value >= 0 && dev->rep[code] != value) {
			dev->rep[code] = value;
			disposition = INPUT_PASS_TO_ALL;
		}
		break;

	case EV_FF:
		if (value >= 0)
			disposition = INPUT_PASS_TO_ALL;
		break;

	case EV_PWR:
		disposition = INPUT_PASS_TO_ALL;
		break;
	}

	if (disposition != INPUT_IGNORE_EVENT && type != EV_SYN)
		dev->sync = false;

	if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)
		dev->event(dev, type, code, value);

	if (disposition & INPUT_PASS_TO_HANDLERS) //如果需要处理,调用input_pass_event
		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_handler *handler;
	struct input_handle *handle;

	rcu_read_lock();

	handle = rcu_dereference(dev->grab);	//强制为dev赋的handler
	if (handle)
		handle->handler->event(handle, type, code, value);
	else {
		bool filtered = false;
		//如果没有为grab赋值,就会遍历该dev的handle链表
		list_for_each_entry_rcu(handle, &dev->h_list, d_node) {
			if (!handle->open)
				continue;
			//如果handle打开,表示该设备已经被一个用户进程使用
			handler = handle->handler;
			if (!handler->filter) {
				if (filtered)
					break;

				handler->event(handle, type, code, value); //调用event函数

			} else if (handler->filter(handle, type, code, value))
				filtered = true;
		}
	}

	rcu_read_unlock();
}

4. input  handler例子evdev的分析

[cpp] view plain copy
print ?
  1. static int __init evdev_init(void)  
  2. {  
  3.     return input_register_handler(&evdev_handler);  
  4. }  
  5. static struct input_handler evdev_handler = {  
  6.     .event      = evdev_event,  
  7.     .connect    = evdev_connect,  
  8.     .disconnect = evdev_disconnect,  
  9.     .fops       = &evdev_fops,  
  10.     .minor      = EVDEV_MINOR_BASE,  
  11.     .name       = "evdev",  
  12.     .id_table   = evdev_ids,  
  13. };  
  14. static const struct file_operations evdev_fops = {  
  15.     .owner      = THIS_MODULE,  
  16.     .read       = evdev_read,  
  17.     .write      = evdev_write,  
  18.     .poll       = evdev_poll,  
  19.     .open       = evdev_open,  
  20.     .release    = evdev_release,  
  21.     .unlocked_ioctl = evdev_ioctl,  
  22. #ifdef CONFIG_COMPAT  
  23.     .compat_ioctl   = evdev_ioctl_compat,  
  24. #endif  
  25.     .fasync     = evdev_fasync,  
  26.     .flush      = evdev_flush,  
  27.     .llseek     = no_llseek,  
  28. };  
static int __init evdev_init(void)
{
	return input_register_handler(&evdev_handler);
}
static struct input_handler evdev_handler = {
	.event		= evdev_event,
	.connect	= evdev_connect,
	.disconnect	= evdev_disconnect,
	.fops		= &evdev_fops,
	.minor		= EVDEV_MINOR_BASE,
	.name		= "evdev",
	.id_table	= evdev_ids,
};
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,
};
evdev的数据结构

[cpp] view plain copy
print ?
  1. struct evdev {  
  2.     int open;  
  3.     int minor;  
  4.     struct input_handle handle; //所属的input_handler  
  5.     wait_queue_head_t wait; //等待队列头  
  6.     struct evdev_client __rcu *grab;  
  7.     struct list_head client_list;   //evdev_client链表头  
  8.     spinlock_t client_lock; /* protects client_list */  
  9.     struct mutex mutex;  
  10.     struct device dev;  
  11.     bool exist;  
  12. };  
  13. struct evdev_client {  
  14.     unsigned int head;  //针对buffer的索引  
  15.     unsigned int tail;  //针对buffer的索引  
  16.     unsigned int packet_head; /* [future] position of the first element of next packet */  
  17.     spinlock_t buffer_lock; /* protects access to buffer, head and tail */  
  18.     struct fasync_struct *fasync;   //异步通知  
  19.     struct evdev *evdev;    //evdev设备  
  20.     struct list_head node;  //evdev_client链表项  
  21.     unsigned int bufsize;  
  22.     struct input_event buffer[]; //事件缓冲  
  23. };  
struct evdev {
	int open;
	int minor;
	struct input_handle handle;	//所属的input_handler
	wait_queue_head_t wait;	//等待队列头
	struct evdev_client __rcu *grab;
	struct list_head client_list;	//evdev_client链表头
	spinlock_t client_lock; /* protects client_list */
	struct mutex mutex;
	struct device dev;
	bool exist;
};
struct evdev_client {
	unsigned int head;	//针对buffer的索引
	unsigned int tail;	//针对buffer的索引
	unsigned int packet_head; /* [future] position of the first element of next packet */
	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链表项
	unsigned int bufsize;
	struct input_event buffer[]; //事件缓冲
};
evdev设备结点的(/dev/input/event0)的打开

对主设备号为INPUT_MAJOR的设备节点进行操作,会转换为对input_handler的操作。该过程在input_open_file中。直接看evdev_open

[cpp] view plain copy
print ?
  1. static int evdev_open(struct inode *inode, struct file *file)  
  2. {  
  3.     struct evdev *evdev;  
  4.     struct evdev_client *client;  
  5.     int i = iminor(inode) - EVDEV_MINOR_BASE;  
  6.     unsigned int bufsize;  
  7.     int error;  
  8.   
  9.     if (i >= EVDEV_MINORS)  
  10.         return -ENODEV;  
  11.   
  12.     error = mutex_lock_interruptible(&evdev_table_mutex);  
  13.     if (error)  
  14.         return error;  
  15.     evdev = evdev_table[i];  
  16.     if (evdev)  
  17.         get_device(&evdev->dev);  
  18.     mutex_unlock(&evdev_table_mutex);  
  19.   
  20.     if (!evdev)  
  21.         return -ENODEV;  
  22.   
  23.     bufsize = evdev_compute_buffer_size(evdev->handle.dev);  
  24.     //分配evdev_client和事件缓冲  
  25.     client = kzalloc(sizeof(struct evdev_client) +  
  26.                 bufsize * sizeof(struct input_event),  
  27.              GFP_KERNEL);  
  28.     if (!client) {  
  29.         error = -ENOMEM;  
  30.         goto err_put_evdev;  
  31.     }  
  32.   
  33.     client->bufsize = bufsize;  
  34.     spin_lock_init(&client->buffer_lock);  
  35.     client->evdev = evdev;  
  36.     evdev_attach_client(evdev, client);  
  37.   
  38.     error = evdev_open_device(evdev); //调用真正的底层设备函数  
  39.     if (error)  
  40.         goto err_free_client;  
  41.   
  42.     file->private_data = client;  
  43.     nonseekable_open(inode, file);  
  44.   
  45.     return 0;  
  46.   
  47.  err_free_client:  
  48.     evdev_detach_client(evdev, client);  
  49.     kfree(client);  
  50.  err_put_evdev:  
  51.     put_device(&evdev->dev);  
  52.     return error;  
  53. }  
  54. static int evdev_open_device(struct evdev *evdev)  
  55. {  
  56.     int retval;  
  57.   
  58.     retval = mutex_lock_interruptible(&evdev->mutex);  
  59.     if (retval)  
  60.         return retval;  
  61.   
  62.     if (!evdev->exist)  
  63.         retval = -ENODEV;  
  64.     else if (!evdev->open++) {  
  65.         retval = input_open_device(&evdev->handle); //第一次打开,调用input_open_device  
  66.         if (retval)  
  67.             evdev->open--;  
  68.     }  
  69.   
  70.     mutex_unlock(&evdev->mutex);  
  71.     return retval;  
  72. }  
  73. int input_open_device(struct input_handle *handle)  
  74. {  
  75.     struct input_dev *dev = handle->dev;  
  76.     int retval;  
  77.   
  78.     retval = mutex_lock_interruptible(&dev->mutex);  
  79.     if (retval)  
  80.         return retval;  
  81.   
  82.     if (dev->going_away) {  
  83.         retval = -ENODEV;  
  84.         goto out;  
  85.     }  
  86.   
  87.     handle->open++;  //递增handle的打开计数  
  88.   
  89.     if (!dev->users++ && dev->open)  
  90.         retval = dev->open(dev); //如果是第一次打开,调用input_dev的open函数  
  91.   
  92.     if (retval) {  
  93.         dev->users--;  
  94.         if (!--handle->open) {  
  95.             /* 
  96.              * Make sure we are not delivering any more events 
  97.              * through this handle 
  98.              */  
  99.             synchronize_rcu();  
  100.         }  
  101.     }  
  102.   
  103.  out:  
  104.     mutex_unlock(&dev->mutex);  
  105.     return retval;  
  106. }  
static int evdev_open(struct inode *inode, struct file *file)
{
	struct evdev *evdev;
	struct evdev_client *client;
	int i = iminor(inode) - EVDEV_MINOR_BASE;
	unsigned int bufsize;
	int error;

	if (i >= EVDEV_MINORS)
		return -ENODEV;

	error = mutex_lock_interruptible(&evdev_table_mutex);
	if (error)
		return error;
	evdev = evdev_table[i];
	if (evdev)
		get_device(&evdev->dev);
	mutex_unlock(&evdev_table_mutex);

	if (!evdev)
		return -ENODEV;

	bufsize = evdev_compute_buffer_size(evdev->handle.dev);
	//分配evdev_client和事件缓冲
	client = kzalloc(sizeof(struct evdev_client) +
				bufsize * sizeof(struct input_event),
			 GFP_KERNEL);
	if (!client) {
		error = -ENOMEM;
		goto err_put_evdev;
	}

	client->bufsize = bufsize;
	spin_lock_init(&client->buffer_lock);
	client->evdev = evdev;
	evdev_attach_client(evdev, client);

	error = evdev_open_device(evdev); //调用真正的底层设备函数
	if (error)
		goto err_free_client;

	file->private_data = client;
	nonseekable_open(inode, file);

	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;
	else if (!evdev->open++) {
		retval = input_open_device(&evdev->handle); //第一次打开,调用input_open_device
		if (retval)
			evdev->open--;
	}

	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的打开计数

	if (!dev->users++ && dev->open)
		retval = dev->open(dev);	//如果是第一次打开,调用input_dev的open函数

	if (retval) {
		dev->users--;
		if (!--handle->open) {
			/*
			 * Make sure we are not delivering any more events
			 * through this handle
			 */
			synchronize_rcu();
		}
	}

 out:
	mutex_unlock(&dev->mutex);
	return retval;
}

可以看到open操作,先是在input core层,再进入input handler层,后面又进入input core层,最后进入input dev的驱动层。内核中这种偷梁换柱的操作很多。

再来看下evdev_event

[cpp] view plain copy
print ?
  1. static void evdev_event(struct input_handle *handle,  
  2.             unsigned int type, unsigned int code, int value)  
  3. {  
  4.     struct evdev *evdev = handle->private;  
  5.     struct evdev_client *client;  
  6.     struct input_event event;  
  7.   
  8.     do_gettimeofday(&event.time);  
  9.     event.type = type;  
  10.     event.code = code;  
  11.     event.value = value;  
  12.   
  13.     rcu_read_lock();  
  14.   
  15.     client = rcu_dereference(evdev->grab);  
  16.     if (client)  
  17.         evdev_pass_event(client, &event);  
  18.     else  
  19.         //遍历client链表,调用evdev_pass_event  
  20.         list_for_each_entry_rcu(client, &evdev->client_list, node)  
  21.             evdev_pass_event(client, &event);  
  22.   
  23.     rcu_read_unlock();  
  24.   
  25.     if (type == EV_SYN && code == SYN_REPORT)  
  26.         wake_up_interruptible(&evdev->wait);  
  27. }  
  28. static void evdev_pass_event(struct evdev_client *client,  
  29.                  struct input_event *event)  
  30. {  
  31.     /* Interrupts are disabled, just acquire the lock. */  
  32.     spin_lock(&client->buffer_lock);  
  33.   
  34.     client->buffer[client->head++] = *event;  
  35.     client->head &= client->bufsize - 1;  
  36.   
  37.     if (unlikely(client->head == client->tail)) {  
  38.         /* 
  39.          * This effectively "drops" all unconsumed events, leaving 
  40.          * EV_SYN/SYN_DROPPED plus the newest event in the queue. 
  41.          */  
  42.         client->tail = (client->head - 2) & (client->bufsize - 1);  
  43.   
  44.         client->buffer[client->tail].time = event->time;  
  45.         client->buffer[client->tail].type = EV_SYN;  
  46.         client->buffer[client->tail].code = SYN_DROPPED;  
  47.         client->buffer[client->tail].value = 0;  
  48.   
  49.         client->packet_head = client->tail;  
  50.     }  
  51.   
  52.     if (event->type == EV_SYN && event->code == SYN_REPORT) {  
  53.         client->packet_head = client->head;  
  54.         kill_fasync(&client->fasync, SIGIO, POLL_IN);  
  55.     }  
  56.   
  57.     spin_unlock(&client->buffer_lock);  
  58. }  
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;

	rcu_read_lock();

	client = rcu_dereference(evdev->grab);
	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();

	if (type == EV_SYN && code == SYN_REPORT)
		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;
	client->head &= client->bufsize - 1;

	if (unlikely(client->head == client->tail)) {
		/*
		 * 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;
		kill_fasync(&client->fasync, SIGIO, POLL_IN);
	}

	spin_unlock(&client->buffer_lock);
}

总结一下事件的传递过程:首先在驱动层中,调用inport_report_abs,然后他调用了input core层的input_event,input_event调用了input_handle_event对事件进行分派,调用input_pass_event,在这里他会把事件传递给具体的handler层,然后在相应handler的event处理函数中,封装一个event,然后把它投入evdev的那个client_list上的client的事件buffer中,等待用户空间来读取。


你可能感兴趣的:(Linux设备驱动,input子系统框架)