从零开始之驱动发开、linux驱动(二十二、输入子系统)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_16777851/article/details/82936790

前面章节对按键程序已经进行了多次修改,分别引入了中断,阻塞方式的等待队列,非阻塞方式的poll,以及异步通知,以及定时器的延时消抖,这几种方式对一个按键程序进行了优化。

但前面我们写的传的按键值,以及具体的创建的button设备描述符,都不是标准的。只能自己用,而无法作为通用的输入类设备使用。本节我们引入标准的输入子系统,输入子系统是对所有的标准的输入类设备的一个统一的管理,使用这个模型可以跨平台的处理所有的输入类设备。

之前我已经对2.6.35.7内核的输入子系统进行了全面的分析。

https://blog.csdn.net/qq_16777851/article/category/7916623

本节主要对3.16.57和2.6.36.7中的不同点进行指出,并对主要的输入子系统的架构进行总结,同时对我在2.6.35.7内核中没说明的部分进行补充。

首先要说明的是上面连两个内核版本的输入子系统的机制(架构)本身并无改变,改动点只是策略(算法)。

首先我对的输入子系统的架构进行小结。

首先,输入子系统是分为三层的,面对应用层的是输入事件层(handler),处理底层驱动的是输入设备层(driver(或者device))。而衔接dev和handler的则是输入核心层(core)。整个输入子系统的核心层起到承上启下的作用那个。

输入子系统整体由核心层维护,主要使用两条链表,维护着两个层。

static LIST_HEAD(input_dev_list);
static LIST_HEAD(input_handler_list);

input_handler_list:维护着所有的hanlder

input_dev_list:维护着所有的dev

说明三个主要的结构体,他们分别描述了三个重要的机制。

1. input_handler 

/**
 * struct input_handler - implements one of interfaces for input devices
 * @private: driver-specific data
 * @event: event handler. This method is being called by input core with
 *	interrupts disabled and dev->event_lock spinlock held and so
 *	it may not sleep
 * @events: event sequence handler. This method is being called by
 *	input core with interrupts disabled and dev->event_lock
 *	spinlock held and so it may not sleep
 * @filter: similar to @event; separates normal event handlers from
 *	"filters".
 * @match: called after comparing device's id with handler's id_table
 *	to perform fine-grained matching between device and handler
 * @connect: called when attaching a handler to an input device
 * @disconnect: disconnects a handler from input device
 * @start: starts handler for given handle. This function is called by
 *	input core right after connect() method and also when a process
 *	that "grabbed" a device releases it
 * @legacy_minors: set to %true by drivers using legacy minor ranges
 * @minor: beginning of range of 32 legacy minors for devices this driver
 *	can provide
 * @name: name of the handler, to be shown in /proc/bus/input/handlers
 * @id_table: pointer to a table of input_device_ids this driver can
 *	handle
 * @h_list: list of input handles associated with the handler
 * @node: for placing the driver onto input_handler_list
 *
 * Input handlers attach to input devices and create input handles. There
 * are likely several handlers attached to any given input device at the
 * same time. All of them will get their copy of input event generated by
 * the device.
 *
 * The very same structure is used to implement input filters. Input core
 * allows filters to run first and will not pass event to regular handlers
 * if any of the filters indicate that the event should be filtered (by
 * returning %true from their filter() method).
 *
 * Note that input core serializes calls to connect() and disconnect()
 * methods.
 */
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;
};

它用来描述一个hanlder,这个主要是面向应用层的。

这里强调最后的两个链表节点

node用于把该hanlder连接在input_handler_list链表上。

h_list用于指向hanlde

2. input_handle 

/**
 * struct input_handle - links input device with an input handler
 * @private: handler-specific data
 * @open: counter showing whether the handle is 'open', i.e. should deliver
 *	events from its device
 * @name: name given to the handle by handler that created it
 * @dev: input device the handle is attached to
 * @handler: handler that works with the device through this handle
 * @d_node: used to put the handle on device's list of attached handles
 * @h_node: used to put the handle on handler's list of handles from which
 *	it gets events
 */
struct input_handle {

	void *private;

	int open;
	const char *name;

	struct input_dev *dev;
	struct input_handler *handler;

	struct list_head	d_node;
	struct list_head	h_node;
};

这个结构用于连接dev和hanlder

这个主要用于在匹配好dev和handler后,把彼此连接起来。

这里要说明的是一个dev可能会连接多个hanlder,所以会有多个hanlde负责连接dev和多个hanlder

后面的两个链表也主要是负责链接dev和hanlder。

同时因为hanlder不能直接找到dev所以还要通过hanlder里面的h_node查找到hanlde(dev找hanlder也是相同的道理)

3. input_dev 


/**
 * struct input_dev - represents an input device
 * @name: name of the device
 * @phys: physical path to the device in the system hierarchy
 * @uniq: unique identification code for the device (if device has it)
 * @id: id of the device (struct input_id)
 * @propbit: bitmap of device properties and quirks
 * @evbit: bitmap of types of events supported by the device (EV_KEY,
 *	EV_REL, etc.)
 * @keybit: bitmap of keys/buttons this device has
 * @relbit: bitmap of relative axes for the device
 * @absbit: bitmap of absolute axes for the device
 * @mscbit: bitmap of miscellaneous events supported by the device
 * @ledbit: bitmap of leds present on the device
 * @sndbit: bitmap of sound effects supported by the device
 * @ffbit: bitmap of force feedback effects supported by the device
 * @swbit: bitmap of switches present on the device
 * @hint_events_per_packet: average number of events generated by the
 *	device in a packet (between EV_SYN/SYN_REPORT events). Used by
 *	event handlers to estimate size of the buffer needed to hold
 *	events.
 * @keycodemax: size of keycode table
 * @keycodesize: size of elements in keycode table
 * @keycode: map of scancodes to keycodes for this device
 * @getkeycode: optional legacy method to retrieve current keymap.
 * @setkeycode: optional method to alter current keymap, used to implement
 *	sparse keymaps. If not supplied default mechanism will be used.
 *	The method is being called while holding event_lock and thus must
 *	not sleep
 * @ff: force feedback structure associated with the device if device
 *	supports force feedback effects
 * @repeat_key: stores key code of the last key pressed; used to implement
 *	software autorepeat
 * @timer: timer for software autorepeat
 * @rep: current values for autorepeat parameters (delay, rate)
 * @mt: pointer to multitouch state
 * @absinfo: array of &struct input_absinfo elements holding information
 *	about absolute axes (current value, min, max, flat, fuzz,
 *	resolution)
 * @key: reflects current state of device's keys/buttons
 * @led: reflects current state of device's LEDs
 * @snd: reflects current state of sound effects
 * @sw: reflects current state of device's switches
 * @open: this method is called when the very first user calls
 *	input_open_device(). The driver must prepare the device
 *	to start generating events (start polling thread,
 *	request an IRQ, submit URB, etc.)
 * @close: this method is called when the very last user calls
 *	input_close_device().
 * @flush: purges the device. Most commonly used to get rid of force
 *	feedback effects loaded into the device when disconnecting
 *	from it
 * @event: event handler for events sent _to_ the device, like EV_LED
 *	or EV_SND. The device is expected to carry out the requested
 *	action (turn on a LED, play sound, etc.) The call is protected
 *	by @event_lock and must not sleep
 * @grab: input handle that currently has the device grabbed (via
 *	EVIOCGRAB ioctl). When a handle grabs a device it becomes sole
 *	recipient for all input events coming from the device
 * @event_lock: this spinlock is is taken when input core receives
 *	and processes a new event for the device (in input_event()).
 *	Code that accesses and/or modifies parameters of a device
 *	(such as keymap or absmin, absmax, absfuzz, etc.) after device
 *	has been registered with input core must take this lock.
 * @mutex: serializes calls to open(), close() and flush() methods
 * @users: stores number of users (input handlers) that opened this
 *	device. It is used by input_open_device() and input_close_device()
 *	to make sure that dev->open() is only called when the first
 *	user opens device and dev->close() is called when the very
 *	last user closes the device
 * @going_away: marks devices that are in a middle of unregistering and
 *	causes input_open_device*() fail with -ENODEV.
 * @dev: driver model's view of this device
 * @h_list: list of input handles associated with the device. When
 *	accessing the list dev->mutex must be held
 * @node: used to place the device onto input_dev_list
 * @num_vals: number of values queued in the current frame
 * @max_vals: maximum number of values queued in a frame
 * @vals: array of values queued in the current frame
 * @devres_managed: indicates that devices is managed with devres framework
 *	and needs not be explicitly unregistered or freed.
 */
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)];
	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;
	struct list_head	node;

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

	bool devres_managed;
};

input_dev负责底层的实现,包括,该输入设备支持那种输入类型,键值等等。

它里面也有两个的链表节点

node用于连接到核心层的input_dev_list维护的所有设备

h_list用于连接负责它和hanlder连接的hanlde,这里和hanlder里面不一样的是,这个h_list可以不止连接一个hanlder。

即dev里面的h_list是可以连接多个hanlde的,将来如果有按键发生,会依次对该链表上的多个hanlder的event都执行。

使用这位朋友的这个图来说明就是:

https://www.cnblogs.com/deng-tao/p/6094049.html

dev1,可以匹配到handler1和hanlder2。所以dev1的h_list链表上连接了两个hanlde.

整体架构就是这样,具体的匹配机制,新内核和老内核的没大的区别。

下面我对新老输入子系统的几个不同之处进行说明

字符设的注册,老的内核版本采用,一次性注册完所有,并采用一个统一的fops,在每次打开open函数时,再次绑定具体hanlder的

fops.

static int __init input_init(void)
{
	int err;

	input_init_abs_bypass();

	err = class_register(&input_class);
	if (err) {
		printk(KERN_ERR "input: unable to register input_dev class\n");
		return err;
	}

	err = input_proc_init();
	if (err)
		goto fail1;
    /* 一次注册完所有的此设备号,而所有的fpos也是统一的 */
	err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
	if (err) {
		printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
		goto fail2;
	}

	return 0;

 fail2:	input_proc_exit();
 fail1:	class_unregister(&input_class);
	return err;
}
static int input_open_file(struct inode *inode, struct file *file)
{
	struct input_handler *handler;
	const struct file_operations *old_fops, *new_fops = NULL;
	int err;

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

	/* No load-on-demand here? */
	handler = input_table[iminor(inode) >> 5];    /* 查找到具体的handler */
	if (handler)
		new_fops = fops_get(handler->fops);    /* 使用该hnadler的fops */

	mutex_unlock(&input_mutex);

	/*
	 * That's _really_ odd. Usually NULL ->open means "nothing special",
	 * not "no device". Oh, well...
	 */
	if (!new_fops || !new_fops->open) {
		fops_put(new_fops);
		err = -ENODEV;
		goto out;
	}
        
    /* 新的替换掉老的 */
	old_fops = file->f_op;
	file->f_op = new_fops;

	err = new_fops->open(inode, file);
	if (err) {
		fops_put(file->f_op);
		file->f_op = fops_get(old_fops);
	}
	fops_put(old_fops);
out:
	return err;
}

static const struct file_operations input_fops = {
	.owner = THIS_MODULE,
	.open = input_open_file,
};

新内核则分开处理的

static int __init input_init(void)
{
	int err;

	err = class_register(&input_class);
	if (err) {
		pr_err("unable to register input_dev class\n");
		return err;
	}

	err = input_proc_init();
	if (err)
		goto fail1;

    /* 一次性注册完所有的次设备 */
	err = register_chrdev_region(MKDEV(INPUT_MAJOR, 0),
				     INPUT_MAX_CHAR_DEVICES, "input");
	if (err) {
		pr_err("unable to register char major %d", INPUT_MAJOR);
		goto fail2;
	}

	return 0;

 fail2:	input_proc_exit();
 fail1:	class_unregister(&input_class);
	return err;
}

在dev和handler匹配上后,会调用conect

/*
 * Create new evdev device. Note that input core serializes calls
 * to connect and disconnect.
 */
static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
			 const struct input_device_id *id)
{
	struct evdev *evdev;
	int minor;
	int dev_no;
	int error;

	minor = input_get_new_minor(EVDEV_MINOR_BASE, EVDEV_MINORS, true);
	if (minor < 0) {
		error = minor;
		pr_err("failed to reserve new minor: %d\n", error);
		return error;
	}

	evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
	if (!evdev) {
		error = -ENOMEM;
		goto err_free_minor;
	}

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

	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, 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);
	if (error)
		goto err_free_evdev;

	cdev_init(&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);
	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);
 err_free_minor:
	input_free_minor(minor);
	return error;
}

新内核多增加了这么几句

	cdev_init(&evdev->cdev, &evdev_fops);
	evdev->cdev.kobj.parent = &evdev->dev.kobj;
	error = cdev_add(&evdev->cdev, evdev->dev.devt, 1);

是在这里注册的具体的handler的fops

真正的注册字符设备,起始也是做两件事,

一件是申请注册次设备号__register_chrdev_region

另一件是添加fops,通过cdev_add

新的内核只不过是分开做了。

int __register_chrdev(unsigned int major, unsigned int baseminor,
		      unsigned int count, const char *name,
		      const struct file_operations *fops)
{
	struct char_device_struct *cd;
	struct cdev *cdev;
	int err = -ENOMEM;

	cd = __register_chrdev_region(major, baseminor, count, name);
	if (IS_ERR(cd))
		return PTR_ERR(cd);

	cdev = cdev_alloc();
	if (!cdev)
		goto out2;

	cdev->owner = fops->owner;
	cdev->ops = fops;
	kobject_set_name(&cdev->kobj, "%s", name);

	err = cdev_add(cdev, MKDEV(cd->major, baseminor), count);
	if (err)
		goto out;

	cd->cdev = cdev;

	return major ? 0 : cd->major;
out:
	kobject_put(&cdev->kobj);
out2:
	kfree(__unregister_chrdev_region(cd->major, baseminor, count));
	return err;
}

老的内核次设备号在connect里采用数组查询方式查找

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

新的次设备号采用动态分配方式

	minor = input_get_new_minor(EVDEV_MINOR_BASE, EVDEV_MINORS, true);
	if (minor < 0) {
		error = minor;
		pr_err("failed to reserve new minor: %d\n", error);
		return error;
	}

关于ida和idr,可以查看下面几篇博文

https://blog.csdn.net/Morphad/article/details/9051261

https://blog.csdn.net/zjy900507/article/details/75022051

https://blog.csdn.net/gjq_1988/article/details/73723998

基本上不同点也就这两点

接下来分析一下我在老内核中没分析过的内容。

关于同步事件。

为什么要有同步事件?

因为一帧数据,到底有几个包,上层不知道,如何确定。

需要驱动这边发送一个同步包给上层。

比如触摸类事件一次要上报多个数据包,在所有的数据包上传完后,再统一发送一个同步包,上层收到同步报后再处理这一帧数据。

static inline void input_sync(struct input_dev *dev)
{
	input_event(dev, EV_SYN, SYN_REPORT, 0);
}

这里对数据包的上传再进行详细的分析一下。

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_report_rel(struct input_dev *dev, unsigned int code, int value)
{
	input_event(dev, EV_REL, code, value);
}

static inline void input_report_abs(struct input_dev *dev, unsigned int code, int value)
{
	input_event(dev, EV_ABS, code, value);
}

static inline void input_report_ff_status(struct input_dev *dev, unsigned int code, int value)
{
	input_event(dev, EV_FF_STATUS, code, value);
}

static inline void input_report_switch(struct input_dev *dev, unsigned int code, int value)
{
	input_event(dev, EV_SW, code, !!value);
}

static inline void input_sync(struct input_dev *dev)
{
	input_event(dev, EV_SYN, SYN_REPORT, 0);
}

static inline void input_mt_sync(struct input_dev *dev)
{
	input_event(dev, EV_SYN, SYN_MT_REPORT, 0);
}

数据包的发送有多种方式,每种事件都有自己的发送函数,但里面都是调用的统一的。input_event接口。

下面就看一下这个接口处理的事情

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);
		input_handle_event(dev, type, code, value);    /* 发送evnet,通过handle */
		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;

    /* 得到该包数据的特性 */
	disposition = input_get_disposition(dev, type, code, &value);

    /* 该包是否支持发送(有些包是忽略的) */
	if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)
		dev->event(dev, type, code, value);

	if (!dev->vals)        /* 检查缓冲区是否存在,数据包在缓冲区打包,所以必须存在 */
		return;

	if (disposition & INPUT_PASS_TO_HANDLERS) {
		struct input_value *v;

		if (disposition & INPUT_SLOT) {        /* ABS类事件,要多发送一个ABS_MT_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++];    /* 打包数据到发送缓冲区 */
		v->type = type;
		v->code = code;
		v->value = value;
	}

	if (disposition & INPUT_FLUSH) {    /* 该帧数据是否立刻刷上去(接收到同步包) */
		if (dev->num_vals >= 2)
			input_pass_values(dev, dev->vals, dev->num_vals);
		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;
	}

}

接下来分析两个点,数据包的特性有哪些,缓冲区如何以及何时分配的。


#define INPUT_IGNORE_EVENT	0
#define INPUT_PASS_TO_HANDLERS	1
#define INPUT_PASS_TO_DEVICE	2
#define INPUT_SLOT		4
#define INPUT_FLUSH		8
#define INPUT_PASS_TO_ALL	(INPUT_PASS_TO_HANDLERS | INPUT_PASS_TO_DEVICE)



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:
            /* 只有在这包数据是同步包的时候才刷INPUT_FLUSH */
			disposition = INPUT_PASS_TO_HANDLERS | INPUT_FLUSH;    
			break;
		case SYN_MT_REPORT:
			disposition = INPUT_PASS_TO_HANDLERS;
			break;
		}
		break;

	case EV_KEY:
		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) {

				__change_bit(code, dev->key);
				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;
	}

	*pval = value;
	return disposition;
}
默认是INPUT_IGNORE_EVENT不理睬的状态,找到满足INPUT_PASS_TO_HANDLERS才表明确实是可以认识的事件

当然,正常情况都是可以认识的事件

关于缓冲区的分配。

/**
 * input_register_device - register device with input core
 * @dev: device to be registered
 *
 * This function registers device with input core. The device must be
 * allocated with input_allocate_device() and all it's capabilities
 * set up before registering.
 * If function fails the device must be freed with input_free_device().
 * Once device has been successfully registered it can be unregistered
 * with input_unregister_device(); input_free_device() should not be
 * called in this case.
 *
 * Note that this function is also used to register managed input devices
 * (ones allocated with devm_input_allocate_device()). Such managed input
 * devices need not be explicitly unregistered or freed, their tear down
 * is controlled by the devres infrastructure. It is also worth noting
 * that tear down of managed input devices is internally a 2-step process:
 * registered managed input device is first unregistered, but stays in
 * memory and can still handle input_event() calls (although events will
 * not be delivered anywhere). The freeing of managed input device will
 * happen later, when devres stack is unwound to the point where device
 * allocation was made.
 */
int input_register_device(struct input_dev *dev)
{
	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);
		if (!devres)
			return -ENOMEM;

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

    /* 根据具体的事件类型,分配固定长度的pcaket,如果驱动这边有设置hint_events_per_packet的话按两个的较大者处理 */
	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 = dev->hint_events_per_packet + 2;
    /* 申请空间,用来做缓冲 */
	dev->vals = kcalloc(dev->max_vals, sizeof(*dev->vals), GFP_KERNEL);
	if (!dev->vals) {
		error = -ENOMEM;
		goto err_devres_free;
	}

	/*
	 * 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.
        * 这里的重复上报事件,也关注一下,后面也要分析
	 */
	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;

	error = device_add(&dev->dev);
	if (error)
		goto err_free_vals;

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

	list_add_tail(&dev->node, &input_dev_list);

	list_for_each_entry(handler, &input_handler_list, node)
		input_attach_handler(dev, handler);

	input_wakeup_procfs_readers();

	mutex_unlock(&input_mutex);

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

err_device_del:
	device_del(&dev->dev);
err_free_vals:
	kfree(dev->vals);
	dev->vals = NULL;
err_devres_free:
	devres_free(devres);
	return error;
}

在分析数据发送之前我们要说一个功能。重复上报功能。

什么叫重复上报功能?

比如我们按下键盘的某个按键,不松手,比如我们打开记事本,上面就会连续的打印出字符。

想要实现这个功能,就需要支持这种事件类型。

下面我们会用到这种功能,先了解一下这种功能的作用。

接下来继续分析事件的发送。

/*
 * Pass values first through all filters and then, if event has not been
 * filtered out, through all open handles. This function is called with
 * dev->event_lock held and interrupts disabled.
 */
static void input_pass_values(struct input_dev *dev,
			      struct input_value *vals, unsigned int count)
{
	struct input_handle *handle;
	struct input_value *v;

	if (!count)
		return;

	rcu_read_lock();

	handle = rcu_dereference(dev->grab);    /* 是否有独享这个设备的句柄 */
	if (handle) {
		count = input_to_handler(handle, vals, count);    /* 如果有则只给这个设备发数据 */
	} else {
        /* 否则,给这个dev的所有hanlde都发生数据 */
		list_for_each_entry_rcu(handle, &dev->h_list, d_node)
			if (handle->open)    /* 这个handle有应用打开的情况下才向上发送数据,否则有事件也不发生 */
				count = input_to_handler(handle, vals, count);
	}

	rcu_read_unlock();

    /*增加随机事件熵池  */
	add_input_randomness(vals->type, vals->code, vals->value);

	/* trigger auto repeat for key events */
    /* 重复上报时间开启,注意这里只有KEY事件,且value = 1才能开启,否则就关闭了 */
	for (v = vals; v != vals + count; v++) {
		if (v->type == EV_KEY && v->value != 2) {
			if (v->value)
				input_start_autorepeat(dev, v->code);
			else
				input_stop_autorepeat(dev);
		}
	}
}


当然,重复性的东西,肯定就是采用定时器来解决了

static void input_start_autorepeat(struct input_dev *dev, int code)
{
    /* 注意:这里检查了,要支持重复事件才会启动定时器 */
	if (test_bit(EV_REP, dev->evbit) &&
	    dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] &&
	    dev->timer.data) {
		dev->repeat_key = code;    /* 注意这里的code是一帧是数据中非同步包的最后一个数据(当前有持续按键才会真正有效) */
		mod_timer(&dev->timer,
			  jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));    /* 第一次启动,间隔长一些  */
	}
}

在注册设备的时候,如果没设备重复的两个事件参数,会使用默认的

	/*
	 * 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.
	 */
	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;        /* 按键延时,即按下某个按键250ms后才会发这个键值的第二个包 */
		dev->rep[REP_PERIOD] = 33;        /* 定时向上发送的时间间隔 ms */
	}





static void input_stop_autorepeat(struct input_dev *dev)
{
	del_timer(&dev->timer);
}


重复按键处理(注册设备时绑定的,定时器处理函数)

/*
 * Generate software autorepeat event. Note that we take
 * dev->event_lock here to avoid racing with input_event
 * which may cause keys get "stuck".
 */
static void input_repeat_key(unsigned long data)
{
	struct input_dev *dev = (void *) data;
	unsigned long flags;

	spin_lock_irqsave(&dev->event_lock, flags);

    /* 检查有效性,是都支持该键值 */
	if (test_bit(dev->repeat_key, dev->key) &&
	    is_event_supported(dev->repeat_key, dev->keybit, KEY_MAX)) {
		struct input_value vals[] =  {
			{ EV_KEY, dev->repeat_key, 2 },    /* 注意这里value是2  */
			input_value_sync                   /* 这里有同步包(按键事件,每按下一个键都要有同步包) */
		};

        /* 发送事件 */
		input_pass_values(dev, vals, ARRAY_SIZE(vals));

        /* 重复按键类定时,下一次继续发生这个键值 */
		if (dev->rep[REP_PERIOD])
			mod_timer(&dev->timer, jiffies +
					msecs_to_jiffies(dev->rep[REP_PERIOD]));
	}

	spin_unlock_irqrestore(&dev->event_lock, flags);
}

发送事件已经说过,这里再看一次

static void input_pass_values(struct input_dev *dev,
			      struct input_value *vals, unsigned int count)
{
	struct input_handle *handle;
	struct input_value *v;

	if (!count)
		return;

	rcu_read_lock();

	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)
			if (handle->open)
				count = input_to_handler(handle, vals, count);
	}

	rcu_read_unlock();

	add_input_randomness(vals->type, vals->code, vals->value);

	/* trigger auto repeat for key events */
	for (v = vals; v != vals + count; v++) {
		if (v->type == EV_KEY && v->value != 2) {    /* 因为这包数据的value是2,所以不会再次启动定时器的 */
			if (v->value)
				input_start_autorepeat(dev, v->code);
			else
				input_stop_autorepeat(dev);
		}
	}
}

那什么时候定时器才能停呢?

肯定是按键松开后才能停,即下一次我们写的上报松开的程序(非定时器)发送一个松手事件(和按下事件相同唯一就是value是0)

会执行input_pass_values里面的最后面的input_stop_autorepeat(dev);删除该定时器。

注意input_repeat_key里面用一对自旋锁spin_lock_irqsave和spin_unlock_irqrestore保护了起来,所以在执行这个期间不会有input_pass_values来删除定时器。肯定是一个数据发生完毕后才会删除。

数据发送

/*
 * Pass event first through all filters and then, if event has not been
 * filtered out, through all open handles. This function is called with
 * dev->event_lock held and interrupts disabled.
 */
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;

    /* handler筛选数据掉一些包(一般都不会筛选的) */
	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);

	return count;
}

通用的已经说完了,

下面我们以evdev为例,说明一下event的打包流程。

/*
 * Pass incoming event to all connected clients.
 * 发送一包数据,也是用的发送多包的方式
 */
static void evdev_event(struct input_handle *handle,
			unsigned int type, unsigned int code, int value)
{
	struct input_value vals[] = { { type, code, value } };

	evdev_events(handle, vals, 1);
}

所以这里就以发送多包为例,说明

 * Pass incoming events to all connected clients.
 */
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());

	rcu_read_lock();

    /* 独占式的发送? */
	client = rcu_dereference(evdev->grab);

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

	rcu_read_unlock();
}

打包和通知

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;

	if (client->revoked)
		return;

    /* 得到事件发生的事件 */
	event.time = ktime_to_timeval(client->clkid == CLOCK_MONOTONIC ?
				      mono : real);

	/* Interrupts are disabled, just acquire the lock. */
	spin_lock(&client->buffer_lock);

	for (v = vals; v != vals + count; v++) { /* 打包好所有 */
		event.type = v->type;        
		event.code = v->code;
		event.value = v->value;
		__pass_event(client, &event);    /* 每次打包一包数据 */
		if (v->type == EV_SYN && v->code == SYN_REPORT)    
			wakeup = true;        /* 只有收同步帧才唤醒读函数 */
	}

	spin_unlock(&client->buffer_lock);

	if (wakeup)
		wake_up_interruptible(&evdev->wait);    /* 唤醒等待队列睡眠的进程 */
}


static void __pass_event(struct evdev_client *client,
			 const struct input_event *event)
{
    /* 把打包好的数据放到环形缓冲区中 */
	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);
	}
}


现在我们已经通过两种方式通知了应用进程(唤醒等待队列,异步通知)

接下来应用程序读数据就可以了。

static ssize_t evdev_read(struct file *file, char __user *buffer,
			  size_t count, loff_t *ppos)
{
	struct evdev_client *client = file->private_data;
	struct evdev *evdev = client->evdev;
	struct input_event event;
	size_t read = 0;
	int error;

    /* 读的字节数至少要大于一包的字节数 */
	if (count != 0 && count < input_event_size())
		return -EINVAL;

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

        /* 在无是数据,且非阻塞打开的情况下直接退出 */
		if (client->packet_head == client->tail &&
		    (file->f_flags & O_NONBLOCK))
			return -EAGAIN;

		/*
		 * count == 0 is special - no IO is done but we check
		 * for error conditions (see above). 读0个数据?你是在逗我?
		 */
		if (count == 0)
			break;

        /* 读数据  每次拷贝一包数据  */
		while (read + input_event_size() <= count &&
		       evdev_fetch_next_event(client, &event)) {

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

			read += input_event_size();   //input_event_size为一包数据的大小
		}

		if (read)    /* 读到数据了,则退出循环 */
			break;

        /* 到这里肯定是没读到数据,且以阻塞方式打开的,所以就要睡眠,唤醒的条件是有数据或设备不存在
        时,或撤销设备访问权限    
        下次唤醒就是在这个位置,所以有会执行循环,执行上面的操作 */
		if (!(file->f_flags & O_NONBLOCK)) {
			error = wait_event_interruptible(evdev->wait,
					client->packet_head != client->tail ||
					!evdev->exist || client->revoked);
			if (error)
				return error;
		}
	}

	return read;
}


static int evdev_fetch_next_event(struct evdev_client *client,
				  struct input_event *event)
{
	int have_event;

	spin_lock_irq(&client->buffer_lock);

	have_event = client->packet_head != client->tail;    /* 确认缓冲区还有数据 */
	if (have_event) {
		*event = client->buffer[client->tail++];    /* 读缓冲区的数据 */
		client->tail &= client->bufsize - 1;
	}

	spin_unlock_irq(&client->buffer_lock);

	return have_event;
}

/* 数据拷贝到用户空间 */
int input_event_from_user(const char __user *buffer,
			 struct input_event *event)
{
	if (copy_from_user(event, buffer, sizeof(struct input_event)))
		return -EFAULT;

	return 0;
}

我在按键驱动中使用read分别采用了阻塞,非阻塞。等待队列唤醒唤醒,异步通知,以及poll查询方式。

当然输入子系统也给我们实现了这些方式。

阻塞和非阻塞在上面读的时候以及看到了。

而等待队列睡眠与唤醒上面也已经体现了出现。

下面看一下异步通知的注册和poll函数的调用。

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

poll查询方式

/* No kernel lock - fine */
static unsigned int evdev_poll(struct file *file, poll_table *wait)
{
	struct evdev_client *client = file->private_data;
	struct evdev *evdev = client->evdev;
	unsigned int mask;

    /* 把大年打开这个设备的进程挂到该设备的等待队列上,是否睡眠则看应用层的打开方式 */
	poll_wait(file, &evdev->wait, wait);

	if (evdev->exist && !client->revoked)        
		mask = POLLOUT | POLLWRNORM;             /* 设备存在,返回可写 */
	else
		mask = POLLHUP | POLLERR;                /* 设备不存在或没权限则返回错误 */

	if (client->packet_head != client->tail)        /* 有数据,则返回数输入信息 */
		mask |= POLLIN | POLLRDNORM;

	return mask;
}

异步通知注册

static int evdev_fasync(int fd, struct file *file, int on)
{
	struct evdev_client *client = file->private_data;

    /* 绑定fd等数据到该设备的异步通知队列上,当事件发生后,会通知这个队列书上的所有进程 */
	return fasync_helper(fd, file, on, &client->fasync);
}

可以看到这个所有的操作我们前面的在按键输入驱动中都一点一点涉及到了。

输入子系统只是对这些的统一使用而已。

最后说一点,这个关于输入子系统还可以写write.

这里主要说一下他的作用。

我们前面已经看到了,在有输入事件的时候,是可以在dev层,做一些事的。

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

	if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)
		dev->event(dev, type, code, value);    /* 输入事件报道过程中,输入设备做事  */

	if (!dev->vals)
		return;

	if (disposition & INPUT_PASS_TO_HANDLERS) {
		struct input_value *v;

		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++];
		v->type = type;
		v->code = code;
		v->value = value;
	}

	if (disposition & INPUT_FLUSH) {
		if (dev->num_vals >= 2)
			input_pass_values(dev, dev->vals, dev->num_vals);
		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;
	}

}

比如鼠标,或手机触摸屏。在按下按键的时候,我们可以让闪烁led等,提醒。

那向输入类设备写这里有时做什么呢,起始也很容易理解。我发送数据,用来控制具体的设备。

比如鼠标刚和电脑插接上时,必须鼠标的指示灯快速闪烁,表明正在匹配连接中。

比如自动调节手机屏蔽的亮暗度,需要光纤输入,需要lcd的背光输出。

这些都需要都需要和输入子系统的输入相关联,可以使用输入子系统的写来控制,也是一种好的方法。

下面简单看一下写函数。

static ssize_t evdev_write(struct file *file, const 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;
	int retval = 0;

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

	retval = mutex_lock_interruptible(&evdev->mutex);
	if (retval)
		return retval;

	if (!evdev->exist || client->revoked) {
		retval = -ENODEV;
		goto out;
	}

	while (retval + input_event_size() <= count) {
        
        /* 用户空间到内核空间拷贝数据 */
		if (input_event_from_user(buffer + retval, &event)) {
			retval = -EFAULT;
			goto out;
		}
		retval += input_event_size();

        /*写数据 */
		input_inject_event(&evdev->handle,
				   event.type, event.code, event.value);
	}

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

/**
 * input_inject_event() - send input event from input handler
 * @handle: input handle to send event through
 * @type: type of the event
 * @code: event code
 * @value: value of the event
 *
 * Similar to input_event() but will ignore event if device is
 * "grabbed" and handle injecting event is not the one that owns
 * the device.
 */
void input_inject_event(struct input_handle *handle,
			unsigned int type, unsigned int code, int value)
{
	struct input_dev *dev = handle->dev;
	struct input_handle *grab;
	unsigned long flags;

	if (is_event_supported(type, dev->evbit, EV_MAX)) {
		spin_lock_irqsave(&dev->event_lock, flags);

		rcu_read_lock();
		grab = rcu_dereference(dev->grab);
        /* 不是独享或刚好对这个hanlder独享 */
		if (!grab || grab == handle)
			input_handle_event(dev, type, code, value);    /* 调用输入处理事件 */
		rcu_read_unlock();

		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;

	disposition = input_get_disposition(dev, type, code, &value);

	if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)
        /* 最终还是调用到这个函数,只要这个设备注册的时候是支持写的,具体执行动作可以自由发挥了
            比如不同code不同的动作,比如value不同执行不同的动作等等
         */
		dev->event(dev, type, code, value);

	if (!dev->vals)
		return;

	if (disposition & INPUT_PASS_TO_HANDLERS) {
		struct input_value *v;

		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++];
		v->type = type;
		v->code = code;
		v->value = value;
	}

	if (disposition & INPUT_FLUSH) {
		if (dev->num_vals >= 2)
			input_pass_values(dev, dev->vals, dev->num_vals);
		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;
	}

}

前面对输入子系统说了这么多,这里我们以按键事件为例写一个,标准的输入事件。

要做的事

1.我的开发板删有4个按键,我们就假设它四个分别代表字母 “l”、“s”、enter、左边的shift按键。

2.使用这四个按键处理作为我们的标准输入(之前的标准输入默认是串口console),打印数ls字样

先看一下软件代码

#include <linux/fs.h>   
#include <linux/init.h> 
#include <linux/module.h>
#include <linux/gpio.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <linux/irqreturn.h>        /* 中断函数的返回值用到 */
#include <mach/irqs.h>              /* 中断号用到 */
#include <linux/interrupt.h>        /* 中断req和free用到 */
#include <linux/input.h>
#include <linux/timer.h>


struct pin_desc {
    int irq;
    char *name;
    int pin;
    int pin_val;
};

static struct input_dev *button_dev;
static struct timer_list button_timer;
static struct pin_desc *pin_dev;

static struct pin_desc pins_desc[] = { 
    {IRQ_EINT(16), "SW7", S5PV210_GPH2(0), KEY_L},
    {IRQ_EINT(17), "SW8", S5PV210_GPH2(1), KEY_S},
    {IRQ_EINT(18), "SW9", S5PV210_GPH2(2), KEY_ENTER},
    {IRQ_EINT(19), "SW+", S5PV210_GPH2(3), KEY_LEFTSHIFT},
};


static irqreturn_t gpio_keys_irq_isr(int irq, void *dev_id)
{
    /* 保存当前中断的信息 */
    pin_dev = dev_id;

    /* 中断函数中,修改定时器值为 10个jiffies后处理定时函数(消抖) */
    mod_timer(&button_timer, jiffies + 10);

    return IRQ_HANDLED;
}




static void button_timer_func(unsigned long date)
{
    struct pin_desc *p = NULL;
    int pin_val;

    /* 判断当add_timer时jiffies为0时会不会进定时中断函数 */
    if(pin_dev == NULL) {
        printk(KERN_INFO"pin_dev is NULL\n");
        return;
    }

    p = pin_dev;
    pin_val =  gpio_get_value(p->pin);

    /* 得到键值,判断时按下还是松开 */
    if(pin_val) {
        /* 松开 */
        input_report_key(button_dev, p->pin_val, 0);    /* 松开是0 */
        input_sync(button_dev);
    } else  {
        /* 按下 */
        input_report_key(button_dev, p->pin_val, 1);    /* 按下是1 */
        input_sync(button_dev);
    }
}





static int button_init(void)
{
    int error;
    int i;

    /* 1.分配一个input_dev结构体 */
    button_dev =  input_allocate_device();
    if(!button_dev)
    {
        printk(KERN_ERR "input_button.c: Not enough memory\n");
        error = -ENOMEM;
        goto err_input_allocate_device;
    }

    /* 2.设置结构体 */
    /* 2.1 能产生那类事件 */
//  set_bit(EV_KEY, button_dev->evbit);     /* 按键类事件 */
    set_bit(EV_REP, button_dev->evbit);     /* 重复上报事件 */

    /* 2.2 能产生上面事件的那些结果 */
//  set_bit(KEY_L,          button_dev->keybit);    /* l按键 */
//  set_bit(KEY_S,          button_dev->keybit);    /* s按键 */
//  set_bit(KEY_ENTER,      button_dev->keybit);    /* enter按键 */
//  set_bit(KEY_LEFTSHIFT,  button_dev->keybit);    /* 左边的shitf按键 */

    /* 使用这种方式,里面代码会设置好evbit事件类型,两种方式都是可以实现功能的 */
    input_set_capability(button_dev, EV_KEY, KEY_L);
    input_set_capability(button_dev, EV_KEY, KEY_S);
    input_set_capability(button_dev, EV_KEY, KEY_ENTER);
    input_set_capability(button_dev, EV_KEY, KEY_LEFTSHIFT);

    /* 2.3 设置厂家,版本,总线类型等... */


    /* 3.注册input_dev结构体 */
    error = input_register_device(button_dev);
    if(error) {
        printk(KERN_ERR"input_button.c: input_register_device fail\n");
        goto err_input_register_device;
    }


    /* 4.硬件相关的初始化 */
    /* 4.1 申请按键中断 */
    for(i = 0; i < 4; i++)  {
        if(request_irq(pins_desc[i].irq, gpio_keys_irq_isr, IRQF_TRIGGER_RISING |
                    IRQF_TRIGGER_FALLING, pins_desc[i].name, &pins_desc[i])) {
            printk(KERN_ERR"input_button.c: Irq request fail\n");
            error = -EBUSY;
            goto err_request_irq;
        }
    }
    /* 注册消抖定时器 */
    init_timer(&button_timer);
    button_timer.function = button_timer_func;
    add_timer(&button_timer);


    return 0;


err_request_irq:
    for( i -= 1; i >= 0; i--)
        free_irq(pins_desc[i].irq, &pins_desc[i]);
    input_unregister_device(button_dev);
err_input_register_device:
    input_free_device(button_dev);
err_input_allocate_device:

    return error;
}


static void button_exit(void)
{
    int i;

    del_timer(&button_timer);
    for(i = 0; i < 4; i++)
        free_irq(pins_desc[i].irq, &pins_desc[i]);

    input_unregister_device(button_dev);

    input_free_device(button_dev);

}

module_init(button_init);
module_exit(button_exit);

MODULE_LICENSE("GPL");
                                 

先看一下,一个数据包的信息包括哪些东西。

/*
 * The event structure itself
 */

struct input_event {
	struct timeval time;
	__u16 type;
	__u16 code;
	__s32 value;
};

struct timeval {
	__kernel_time_t		tv_sec;		/* seconds */
	__kernel_suseconds_t	tv_usec;	/* microseconds */
};

typedef __kernel_long_t	__kernel_time_t;
typedef __kernel_long_t		__kernel_suseconds_t;

time包含两个long类型的事件数据,共占用8字节

剩下的type和code分别占用2字节,value占用4字节

测试,使用下面命令打开设备查看打印信息

hexdump /dev/input/event0

我们这里就不看时间了,只看具体信息。

第一行type为1,code为0x1c,value为1

type为1,表示按键类事件,0x1c是28,表示enter键,value为1,表示按下。

第二行是一个同步包,所有的都为0

第三行value为0,表示松开按键

后面的0x26 == 38为 l键值

至此我们可以正确的读到按键值了。

试一下连续按下不松开。

可以发现,按下和松开是正确的。

而重复类事件的value是2,同步帧的value是1

static inline void input_sync(struct input_dev *dev)
{
	input_event(dev, EV_SYN, SYN_REPORT, 0);
}

我们主动发送的同步包。

重复按键事件发送的同步包。

static const struct input_value input_value_sync = { EV_SYN, SYN_REPORT, 1 };

/*
 * Generate software autorepeat event. Note that we take
 * dev->event_lock here to avoid racing with input_event
 * which may cause keys get "stuck".
 */
static void input_repeat_key(unsigned long data)
{
	struct input_dev *dev = (void *) data;
	unsigned long flags;

	spin_lock_irqsave(&dev->event_lock, flags);

	if (test_bit(dev->repeat_key, dev->key) &&
	    is_event_supported(dev->repeat_key, dev->keybit, KEY_MAX)) {
		struct input_value vals[] =  {
			{ EV_KEY, dev->repeat_key, 2 },
			input_value_sync
		};

		input_pass_values(dev, vals, ARRAY_SIZE(vals));

		if (dev->rep[REP_PERIOD])
			mod_timer(&dev->timer, jiffies +
					msecs_to_jiffies(dev->rep[REP_PERIOD]));
	}

	spin_unlock_irqrestore(&dev->event_lock, flags);
}

查看代码,发现,确实是一致的。

把当前shell的标准输入设置为我们自己的键盘

exec 0</dev/tty1

首先把当前设备重定位到当前sh的标准输入上

之后通过我们开发板的按键,l或s,用enter作为回车表示输入完毕

可以发现,重复上报可以正确显示。

输入ls + enter可以正确的处理命令,说明输入子系统已经正常挂接到标准的输入上。

按下我们之前设置的左边的shift按键,再按下l或s,可以正确的打印大写的L和S。

说明四个按键都可以正确上报信息。

输入驱动层就做这么多。

具体的shift+s --->S属于 别的层的事情了,就不属于输入子系统部分的内容了。

输入子系统就到这里,明天搞fb。

猜你喜欢

转载自blog.csdn.net/qq_16777851/article/details/82936790