LINUX-Input子系统源代码分析

static int input_open_file(struct inode *inode, struct file *file)
{
struct input_handler *handler = input_table[iminor(inode) >> 5]; //根据次设备号找到对应的input_handler。
//次设备号64-95的input_device与放在input_table[1]的input_handler匹配,
// input_table[1]存放evdev_handler(内核启动注册,支持所有输入设备:id_table中flag=0;driver=1),
// 所以内核默认支持32个输入设备;
//大于95的次设备号的设备需要自己注册input_handler到input_table[],
//存放对应注册的input_device
const struct file_operations *old_fops, *new_fops = NULL;
int err;

/* No load-on-demand here? */
if (!handler || !(new_fops = fops_get(handler->fops)))
	return -ENODEV;

/*
 * That's _really_ odd. Usually NULL ->open means "nothing special",
 * not "no device". Oh, well...
 */
if (!new_fops->open) {
	fops_put(new_fops);
	return -ENODEV;
}
old_fops = file->f_op;  //保存文件老的fop
file->f_op = new_fops;  //将注册的input_handler的fop给文件,后面应用程序打开文件后对于的读写操作就是调用这个fop

err = new_fops->open(inode, file);    //打开文件

if (err) {
	fops_put(file->f_op);
	file->f_op = fops_get(old_fops);
}
fops_put(old_fops);
return err;

}

static int evdev_open(struct inode *inode, struct file *file)
{
struct evdev_client *client;
struct evdev *evdev;
int i = iminor(inode) - EVDEV_MINOR_BASE; //计算次设备号
int error;

if (i >= EVDEV_MINORS)
	return -ENODEV;

evdev = evdev_table[i];   //根据次设备号找到保存的evdev,evdev保存了对应的handle,一个handle保存了对应的handler和device,
                          //注意:一个handler可以和多个device建立连接,evdev_handler可以与64-95共计32个device建立连接

if (!evdev || !evdev->exist)
	return -ENODEV;

client = kzalloc(sizeof(struct evdev_client), GFP_KERNEL);  //分配client
if (!client)
	return -ENOMEM;

client->evdev = evdev; //赋值给client->evdev 
list_add_tail(&client->node, &evdev->client_list);  

if (!evdev->open++ && evdev->exist) {
	error = input_open_device(&evdev->handle);  //打开文件
	if (error) {
		list_del(&client->node);
		kfree(client);
		return error;
	}
}

file->private_data = client; //将client赋值给文件的私有数据
return 0;

}

int input_open_device(struct input_handle *handle)
{
struct input_dev *dev = handle->dev;
int err;

err = mutex_lock_interruptible(&dev->mutex);
if (err)
	return err;

handle->open++;   //打开次数计数

if (!dev->users++ && dev->open)
	err = dev->open(dev);

if (err)
	handle->open--;

mutex_unlock(&dev->mutex);

return err;

}

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;
int retval;

if (count < evdev_event_size())
	return -EINVAL;

if (client->head == client->tail && evdev->exist && (file->f_flags & O_NONBLOCK))
	return -EAGAIN;

retval = wait_event_interruptible(evdev->wait,                 //等待文件队列有数据  file->private_data保存了
	client->head != client->tail || !evdev->exist);
if (retval)
	return retval;

if (!evdev->exist)
	return -ENODEV;

while (client->head != client->tail && retval + evdev_event_size() <= count) {

	struct input_event *event = (struct input_event *) client->buffer + client->tail;

	if (evdev_event_to_user(buffer + retval, event))  //读取
		return -EFAULT;

	client->tail = (client->tail + 1) & (EVDEV_BUFFER_SIZE - 1);
	retval += evdev_event_size();
}

return retval;

}

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

INIT_LIST_HEAD(&handler->h_list);

if (handler->fops != NULL) {
	if (input_table[handler->minor >> 5])
		return -EBUSY;

	input_table[handler->minor >> 5] = handler;     //根据次设备号存入input_handler
	                                                //次设备号64-95的input_device与放在input_table[1]的input_handler匹配,
                                                    // input_table[1]存放evdev_handler(内核启动注册,支持所有输入设备),
													// 所以内核默认支持32个输入设备;
		                                            //大于95的次设备号的设备需要自己注册input_handler到input_table[],
									                //存放对应注册的input_device
}

list_add_tail(&handler->node, &input_handler_list);

list_for_each_entry(dev, &input_dev_list, node)
	input_attach_handler(dev, handler);            //注册完input_handler后,搜索input_device链表,匹配input_device与input_handler

input_wakeup_procfs_readers();
return 0;

}

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

if (handler->blacklist && input_match_device(handler->blacklist, dev))
	return -ENODEV;

id = input_match_device(handler->id_table, dev);  //根据input_handler中id_table的flag与driver标志匹配相应字段
if (!id)
	return -ENODEV;

error = handler->connect(handler, dev, id);      //匹配成功,建立连接
if (error && error != -ENODEV)
	printk(KERN_ERR
		"input: failed to attach handler %s to device %s, "
		"error: %d\n",
		handler->name, kobject_name(&dev->cdev.kobj), error);

return error;

}

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 int evdev_connect(struct input_handler *handler, struct input_dev *dev,
const struct input_device_id *id)
{
struct evdev *evdev;
struct class_device *cdev;
dev_t devt;
int minor;
int error;

for (minor = 0; minor < EVDEV_MINORS && evdev_table[minor]; minor++);
if (minor == EVDEV_MINORS) {
	printk(KERN_ERR "evdev: 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);
init_waitqueue_head(&evdev->wait);

evdev->exist = 1;
evdev->minor = minor;
evdev->handle.dev = dev;            //handle.dev指向input_dev
evdev->handle.name = evdev->name;
evdev->handle.handler = handler;    //handle.handler指向input_handler,一个input_handler可以对应多个input_dev,
                                    //并与每一个input_dev建立连接,生成一个input_handle
									//所以evdev_table保存evdev,evdev保存input_handle,input_handle保存input_handler和input_dev
evdev->handle.private = evdev;
sprintf(evdev->name, "event%d", minor);

evdev_table[minor] = evdev;    //保存evdev

devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor),

cdev = class_device_create(&input_class, &dev->cdev, devt,
			   dev->cdev.dev, evdev->name);
if (IS_ERR(cdev)) {
	error = PTR_ERR(cdev);
	goto err_free_evdev;
}

/* temporary symlink to keep userspace happy */
error = sysfs_create_link(&input_class.subsys.kobj,
			  &cdev->kobj, evdev->name);
if (error)
	goto err_cdev_destroy;

error = input_register_handle(&evdev->handle);  //注册evdev->handle,handle、handler、device三者互相有指针指向对方
if (error)
	goto err_remove_link;

return 0;

err_remove_link:
sysfs_remove_link(&input_class.subsys.kobj, evdev->name);
err_cdev_destroy:
class_device_destroy(&input_class, devt);
err_free_evdev:
kfree(evdev);
evdev_table[minor] = NULL;
return error;
}

int input_register_handle(struct input_handle *handle)
{
struct input_handler *handler = handle->handler;

list_add_tail(&handle->d_node, &handle->dev->h_list);   //device->h_list指向handle
list_add_tail(&handle->h_node, &handler->h_list);       //handler->h_list指向handle

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

return 0;

}

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;

set_bit(EV_SYN, dev->evbit);

/*
 * If delay and period are pre-set by the driver, then autorepeating
 * is handled by the driver itself and we don't do it in input.c.
 */

init_timer(&dev->timer);
if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
	dev->timer.data = (long) dev;
	dev->timer.function = input_repeat_key;
	dev->rep[REP_DELAY] = 250;
	dev->rep[REP_PERIOD] = 33;
}

if (!dev->getkeycode)
	dev->getkeycode = input_default_getkeycode;

if (!dev->setkeycode)
	dev->setkeycode = input_default_setkeycode;

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

snprintf(dev->cdev.class_id, sizeof(dev->cdev.class_id),
	 "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);

if (!dev->cdev.dev)
	dev->cdev.dev = dev->dev.parent;

error = class_device_add(&dev->cdev);
if (error)
	return error;

path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
printk(KERN_INFO "input: %s as %s\n",
	dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
kfree(path);

list_for_each_entry(handler, &input_handler_list, node)
	input_attach_handler(dev, handler);           //匹配

input_wakeup_procfs_readers();

return 0;

}

小结:LINUX Input子系统:
(1)input_init注册 input_fops,input_fops包含input_open_file

(2)evdev_exit注册input_handler放在input_table[1],匹配input_device与input_handler,如果匹配成功调用evdev_connect,新建、注册input_handle与input_device与input_handle连接

(3)设备驱动注册input_register_device,匹配input_device与input_handler,如果匹配成功调用evdev_connect,新建、注册input_handle与input_device与input_handle连接

(4)应用程序open调用注册的input_open_file,根据次设备号找到input_table中的input_handler,将input_handler的fop赋值到文件描述符,再调用
fop->open(即evdev_open)打开文件,evdev_open根据次设备号找到保存的evdev,evdev保存了对应的handle,input_handle保存了input_handler和input_dev,
然后分配client结构体,将evdev赋值给client->evdev,最后将client赋值个文件的私有数据file->private_data = client,保证了打开的一个设备文件对应一个
client,其中保存input_handle、input_device、input_handler
(5)应用程序调用read函数时最终调用input_handler的read函数(即evdev_read),在file->private_data = client的队列上等待数据到来
(6)硬件中断后,调用input_event和input_sync上报数据,input_event会调用input_handler的event函数唤醒读取进程,让读取进程读数据

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值