linux内核中有很多自带的input_handler,其中evdev_handler是做常见的,因为它可以匹配任何的input_dev设备。下面我们就对它做一番简要的介绍
一、evdev注册过程
evdev注册如下:
static struct input_handler evdev_handler = { .event = evdev_event,//当硬件有事件上传将调用 .connect = evdev_connect,//当有新的input_dev和它匹配成功时调用(来者不拒) .disconnect = evdev_disconnect, .fops = &evdev_fops,//提供读写函数 .minor = EVDEV_MINOR_BASE,//次设备起始号 .name = "evdev", .id_table = evdev_ids,//它可接受的input_dev条件 }; static int __init evdev_init(void) { return input_register_handler(&evdev_handler); } static void __exit evdev_exit(void) { input_unregister_handler(&evdev_handler); } 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);//初始化handler的handle链表头 if (handler->fops != NULL) { if (input_table[handler->minor >> 5]) { retval = -EBUSY; goto out; } input_table[handler->minor >> 5] = handler;//将该handler放到input维护的全局input_table数组 } list_add_tail(&handler->node, &input_handler_list);//将该handler放到input维护的全局input_handler_list链表 list_for_each_entry(dev, &input_dev_list, node)//取出input维护的全局input_dev_list链表 input_attach_handler(dev, handler);//找出和新注册的handler匹配的dev设备 input_wakeup_procfs_readers(); out: mutex_unlock(&input_mutex); return retval; }
input_attach_handler用来匹配dev和handler
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))//如果dev在handler的黑名单中就直接返回 return -ENODEV; id = input_match_device(handler->id_table, dev);//通过handler的id_table来匹配 if (!id) return -ENODEV; error = handler->connect(handler, dev, id);//匹配成功就调用handler的connect函数 if (error && error != -ENODEV) printk(KERN_ERR"input: failed to attach handler %s to device %s,""error: %d\n",handler->name,kobject_name(&dev->dev.kobj), error); return error; }
在介绍evdev_handler之前先介绍下evdev_handler里面几个重要的数据类型。
在介绍evdev_handler之前先介绍下evdev_handler里面几个重要的数据类型。 #define EVDEV_MINOR_BASE 64 //evdev的设备次设备起始为64 #define EVDEV_MINORS 32 //最大支持32个设备 #define EVDEV_BUFFER_SIZE 64 struct evdev { int exist;//该evdev是否存在,在connect中被赋值 int open;//该evdev是否被打开 int minor;//次设备号 char name[16]; struct input_handle handle;//evdev关联的handle wait_queue_head_t wait;//当有应用程序读evdev时,如果没有数据,就在上面睡起 struct evdev_client *grab; struct list_head client_list;//evdev管理的evdev_client链表 spinlock_t client_lock; /* protects client_list */ struct mutex mutex; struct device dev;//设备模型的东东 }; struct evdev_client { struct input_event buffer[EVDEV_BUFFER_SIZE];//缓冲区大小 int head;//环形缓冲区头 int tail;//环形缓冲区尾 spinlock_t buffer_lock; /* protects access to buffer, head and tail */ struct fasync_struct *fasync; struct evdev *evdev;//evdev_client挂靠的单位 struct list_head node;//用于将evdev_client挂靠在evdev的client_list链表上 }; static struct evdev *evdev_table[EVDEV_MINORS];//evdev最多能处理32的设备 现在来理下struct input_dev,struct input_handler,struct input_handle,struct evdev,struct evdev_client的关系吧 struct input_dev 和一个硬件相关联,主要描述了该硬件支持什么事件,上报事件; struct input_handler主要就提供了设备的操作函数,比如读写等 struct input_handle 当input_dev和struct input_handler相匹配的时候就新建一个struct input_handle作为它们的中间人 struct evdev 当某个struct input_dev和evdev_handler匹配成功后就新建一个struct evdev,看到这里是不是有点和 struct input_handle产生的时间一致呀,起始该结构体内部就包含一个struct input_handle;不过结合了 一些其他信息,可以把它看做是struct input_handle的升级版。 struct evdev_client 主要跟应用程序有关.每打开一次设备就新建一个struct evdev_client,里面保存了应用程序要读取的数据 以及数据的位置。
在dev和handler匹配成功时调用evdev_connect
static int evdev_connect(struct input_handler *handler, struct input_dev *dev,const struct input_device_id *id) { struct evdev *evdev; int minor; int error; for (minor = 0; minor < EVDEV_MINORS; minor++)//evdev支持32设备,如果分配使用完就返回 if (!evdev_table[minor]) break; if (minor == EVDEV_MINORS) { printk(KERN_ERR "evdev: no more free evdev devices\n"); return -ENFILE; } evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);//新建一个struct evdev if (!evdev) return -ENOMEM; INIT_LIST_HEAD(&evdev->client_list);//初始化client_list链表头,代表了有多少应用读写这个evdev设备 spin_lock_init(&evdev->client_lock); mutex_init(&evdev->mutex); init_waitqueue_head(&evdev->wait);//初始化等待队列,当evdev没有数据可读时,就在该等待队列上睡眠 snprintf(evdev->name, sizeof(evdev->name), "event%d", minor);//设备新产生的设备的名字 evdev->exist = 1; evdev->minor = minor;//新产生的evdev分配到的次设备号 evdev->handle.dev = input_get_device(dev);//初始化evdev里面的handle evdev->handle.name = evdev->name; evdev->handle.handler = handler; evdev->handle.private = evdev; dev_set_name(&evdev->dev, evdev->name);//初始化evdev里面的dev 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);//将handle挂靠在dev和handler的链表中 if (error) goto err_free_evdev; error = evdev_install_chrdev(evdev);//将新建的evdev放到evdev_table链表中去 if (error) goto err_unregister_handle; error = device_add(&evdev->dev);//将该设备加入到linux设备模型中去 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; }
到这里input_register_handler的注册工作就完成了,就是有几个input_dev需要我,我就建几个evdev来迎合它。
二、应用程序open操作
当我们使用应用程序的open函数打开一个event设备时,通过该设备的主设备号,调用input注册的input_open_file函数
该函数中通过此设备号在input_table数组中找到它的input_handler,然后调用input_handler里面的open函数。
static int evdev_open(struct inode *inode, struct file *file) { struct evdev *evdev; struct evdev_client *client;//evdev_open是应用程序open的调用才导致调用的,一个应用程序调用机会新建一个struct evdev_client int i = iminor(inode) - EVDEV_MINOR_BASE;//根据次设备得到要操作的设备在evdev_table中的下标 int error; if (i >= EVDEV_MINORS) return -ENODEV; error = mutex_lock_interruptible(&evdev_table_mutex); if (error) return error; evdev = evdev_table[i];//从evdev_table取出evdev设备 if (evdev) get_device(&evdev->dev); mutex_unlock(&evdev_table_mutex); if (!evdev) return -ENODEV; client = kzalloc(sizeof(struct evdev_client), GFP_KERNEL); if (!client) { error = -ENOMEM; goto err_put_evdev; } spin_lock_init(&client->buffer_lock); client->evdev = evdev;//说明client读取的数据来自evdev设备 evdev_attach_client(evdev, client);//将client挂靠在evdev的client_list链表中去 error = evdev_open_device(evdev);//将evdev设置为打开状态 if (error) goto err_free_client; /* *应用程序打开一个设备,系统就给它分配struct file *file *每个应用程序读这个evdev设备,就专门为它分配一个client为它服务 *此处就是将它们关联起来 */ file->private_data = client; return 0; err_free_client: evdev_detach_client(evdev, client); kfree(client); err_put_evdev: put_device(&evdev->dev); return error; }
evdev_open中调用evdev_open_device来设置evdev和它的handle的状态
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++) {//当evdev首次打开,那么就同时打开它的handle retval = input_open_device(&evdev->handle);//将evdev包含的handle也设置为打开状态 if (retval) evdev->open--; } mutex_unlock(&evdev->mutex); return retval; }
三、应用程序read操作
当应用程序调用read函数后将导致evdev_read函数被调用
static ssize_t evdev_read(struct file *file, char __user *buffer,size_t count, loff_t *ppos) { struct evdev_client *client = file->private_data;//在evdev_open末尾被赋值 struct evdev *evdev = client->evdev;//得到应用程序读取数据的设备 struct input_event event; int retval; if (count < input_event_size()) return -EINVAL; /*当client缓冲区无数据;evdev不存在;文件非阻塞打开,那个read直接返回错误*/ if (client->head == client->tail && evdev->exist &&(file->f_flags & O_NONBLOCK)) return -EAGAIN; /*当client里面没有数据时,将应用程序请到evdev->wait休息*/ retval = wait_event_interruptible(evdev->wait,client->head != client->tail || !evdev->exist); if (retval) return retval; if (!evdev->exist) return -ENODEV; /* *当应用程序读取的数据大于struct input_evevt *并且client的buffer里面有数据 */ while (retval + input_event_size() <= count &&evdev_fetch_next_event(client, &event)) { if (input_event_to_user(buffer + retval, &event)) return -EFAULT; retval += input_event_size(); } return retval; } 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->head != client->tail;//如果client的环形缓冲区不为空,说明有事件 if (have_event) { *event = client->buffer[client->tail++];//取出client缓冲区一个事件 client->tail &= EVDEV_BUFFER_SIZE - 1; } spin_unlock_irq(&client->buffer_lock); return have_event; } int input_event_to_user(char __user *buffer,const struct input_event *event) { if (copy_to_user(buffer, event, sizeof(struct input_event)))//将event事件拷贝到用户空间 return -EFAULT; return 0; }
当读取数据的时候,如果client里面没有数据,那么应用程序调用read函数将睡眠,那睡眠什么时候结束呀,当硬件有事件发生时input_dev将调用input_event函数上报事件,在该函数内部将调用和它匹配的input_handler的event函数,在该函数内部将唤醒休眠的数据。
static void evdev_event(struct input_handle *handle,unsigned int type, unsigned int code, int value) { struct evdev *evdev = handle->private;//该初始化在evdev_connect,获得handle对应的evdev设备 struct evdev_client *client; struct input_event event; do_gettimeofday(&event.time);//初始化事件结构 event.type = type; event.code = code; event.value = value; rcu_read_lock(); /* *如果该evdev有个专用的client,那么就将事件发给它 *如果该evdev不存在专用的client,那个就把该事件发送给evdev上client_list链表上所有的client */ client = rcu_dereference(evdev->grab); if (client) evdev_pass_event(client, &event); else list_for_each_entry_rcu(client, &evdev->client_list, node) evdev_pass_event(client, &event); rcu_read_unlock(); wake_up_interruptible(&evdev->wait); }
四、应用程序write操作
输入子系统一般用来处理硬件输入,但是有些情况比如LED设备需要write操作.
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;//在evdev_open末尾被赋值 struct evdev *evdev = client->evdev;//得到应用程序读取数据的设备 struct input_event event; int retval; retval = mutex_lock_interruptible(&evdev->mutex); if (retval) return retval; if (!evdev->exist) { retval = -ENODEV; goto out; } while (retval < count) { if (input_event_from_user(buffer + retval, &event)) {//拷贝应用程序到内核空间 retval = -EFAULT; goto out; } input_inject_event(&evdev->handle,event.type, event.code, event.value); retval += input_event_size(); } out: mutex_unlock(&evdev->mutex); return retval; } int input_event_from_user(const char __user *buffer,struct input_event *event) { if (copy_from_user(event, buffer, sizeof(struct input_event)))//将用户空间的数据拷贝到event里面 return -EFAULT; return 0; } 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)) {//查看将要接受数据的设备是否支持type事件类型 spin_lock_irqsave(&dev->event_lock, flags); rcu_read_lock(); grab = rcu_dereference(dev->grab); 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 = INPUT_IGNORE_EVENT; switch (type) { ....................... ....................... case EV_LED: /* *#define INPUT_PASS_TO_ALL (INPUT_PASS_TO_HANDLERS | INPUT_PASS_TO_DEVICE) *查看设备是否支持EV_LED类型的code if (is_event_supported(code, dev->ledbit, LED_MAX) &&!!test_bit(code, dev->led) != value) { __change_bit(code, dev->led);//设置dev中记录LED的数组 disposition = INPUT_PASS_TO_ALL;//数据传送方向 } break; ...................... } if (disposition != INPUT_IGNORE_EVENT && type != EV_SYN) dev->sync = 0; if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event) dev->event(dev, type, code, value);//调用dev的event函数,用于写设备 if (disposition & INPUT_PASS_TO_HANDLERS) input_pass_event(dev, type, code, value);//将数据传送到handler }
相关文章:
Linux输入子系统:input.c实现细节
Linux输入子系统:总体框架
参考文章:
http://blog.csdn.net/yaozhenguo2006/article/details/6775751
http://www.cnitblog.com/luofuchong/archive/2007/11/12/36157.html