LINUX 输入子系统架构分析

内核的输入子系统是对分散的,多种不同类别的输入设备(如键盘,鼠标,跟踪球,操纵杆,触摸屏,加速计和手写板)等字符设备进行统一处理的一层抽象,就是在字符设备驱动上抽象出的一层。输入子系统包括两类驱动程序:事件驱动程序和设备驱动程序。事件驱动程序负责和应用程序的接口,而设备驱动程序负责和底层输入设备的通信。鼠标事件生成文件mousedev属于事件驱动程序,而PS/2鼠标驱动程序是设备驱动程序。事件驱动程序是标准的,对所有的输入类都是可用的,所以要实现的是设备驱动程序而不是事件驱动程序。设备驱动程序可以利用一个已经存在的,合适的事件驱动程序通过输入核心和用户应用程序接口。
输入子系统带来了如下好处:

1.统一了物理形态各异的相似的输入设备的处理功能
2.提供了用于分发输入报告给用户应用程序的简单的事件接口
3.抽取出了输入驱动程序的通用部分,简化了驱动,并引入了一致性

现在 Android、X windows、qt等众多应用对于linux系统中键盘、鼠标、触摸屏等输入设备的支持都通过、或越来越倾向于标准的input输入子系统。因为input子系统已经完成了字符驱动的文件操作接口,所以编写驱动的核心工作是完成input系统留出的接口,工作量不大。但如果你想更灵活的应用它,就需要好好的分析下input子系统了。


先来看一下输入子系统体系架构图:


再对照 下图( input输入子系统框架 ),很清楚的知道输入子系统是由输入子系统核心层( Input Core ),驱动层和事件处理层(Event Handler)三部份组成。一个输入事件,如鼠标移动,键盘按键按下,joystick的移动等等通过 input driver -> Input core -> Event handler -> userspace 到达用户空间传给应用程序。

注意:keyboard.c不会在/dev/input下产生节点,而是作为ttyn终端(不包括串口终端)的输入。

设备描述
在linux内核中,input设备用input_dev结构体描述,使用input子系统实现输入设备驱动的时候,驱动的核心工作是向系统报告按键、触摸屏、键盘、鼠标等输入事件(event,通过input_event结构体描述),一再需要关心文件操作接口,因为input子系统已经完成了文件操作接口。驱动报告的事件经过InputCore和EventHandler最终到达用户空间。
现在了解了input子系统的基本思想,下面来看一下input子系统的3个基本的数据结构:
  1. struct input_dev {
  2. constchar *name; //名称
  3. constchar *phys; //设备在系统中的物理路径
  4. constchar *uniq; //设备唯一识别符
  5. struct input_id id; //设备ID,包含总线ID(PCI、USB)、厂商ID,与input_handler匹配的时会用到
  6. unsignedlong evbit[BITS_TO_LONGS(EV_CNT)];//支持的所有事件类型
  7. unsignedlong keybit[BITS_TO_LONGS(KEY_CNT)];//支持的键盘事件
  8. unsignedlong relbit[BITS_TO_LONGS(REL_CNT)];//支持的鼠标相对值事件
  9. unsignedlong absbit[BITS_TO_LONGS(ABS_CNT)];//支持的鼠标绝对值事件
  10. unsignedlong mscbit[BITS_TO_LONGS(MSC_CNT)];//支持的其它事件类型
  11. unsignedlong ledbit[BITS_TO_LONGS(LED_CNT)];//支持的LED灯事件
  12. unsignedlong sndbit[BITS_TO_LONGS(SND_CNT)];//支持的声效事件
  13. unsignedlong ffbit[BITS_TO_LONGS(FF_CNT)];//支持的力反馈事件
  14. unsignedlong swbit[BITS_TO_LONGS(SW_CNT)];//支持的开关事件
  15. unsignedint keycodemax;//keycode表的大小
  16. unsignedint keycodesize;//keycode表中元素个数
  17. void *keycode; //设备的键盘表
  18. int (*setkeycode)(struct input_dev *dev, int scancode, int keycode);//配置keycode表
  19. int (*getkeycode)(struct input_dev *dev, int scancode, int *keycode);//获取keycode表
  20. struct ff_device *ff;
  21. unsignedint repeat_key;//保存上一个键值
  22. struct timer_list timer;
  23. int sync;
  24. int abs[ABS_MAX + 1]; //绝对坐标上报的当前值
  25. int rep[REP_MAX + 1]; //这个参数主要是处理重复按键,后面遇到再讲
  26. unsignedlong key[BITS_TO_LONGS(KEY_CNT)];//按键有两种状态,按下和抬起,这个字段就是记录这两个状态。
  27. unsignedlong led[BITS_TO_LONGS(LED_CNT)];
  28. unsignedlong snd[BITS_TO_LONGS(SND_CNT)];
  29. unsignedlong sw[BITS_TO_LONGS(SW_CNT)];
  30. int absmax[ABS_MAX + 1]; //绝对坐标的最大值
  31. int absmin[ABS_MAX + 1]; //绝对坐标的最小值
  32. int absfuzz[ABS_MAX + 1];
  33. int absflat[ABS_MAX + 1];
  34. //操作接口
  35. int (*open)(struct input_dev *dev);
  36. void (*close)(struct input_dev *dev);
  37. int (*flush)(struct input_dev *dev, struct file *file);
  38. int (*event)(struct input_dev *dev, unsigned int type, unsigned int code, int value);
  39. struct input_handle *grab; //当前使用的handle
  40. spinlock_t event_lock;
  41. struct mutex mutex;
  42. unsignedint users;
  43. int going_away;
  44. struct device dev;
  45. struct list_head h_list; //h_list是一个链表头,用来把handle挂载在这个上
  46. struct list_head node; //这个node是用来连到input_dev_list上的
  47. };
  48. //input_dev->evbit表示设备支持的事件类型,可以是下列值的组合
    #define EV_SYN 0x00//同步事件
    #define EV_KEY 0x01//绝对二进制值,如键盘或按钮
    #define EV_REL 0x02//绝对结果,如鼠标设备
    #define EV_ABS 0x03//绝对整数值,如操纵杆或书写板
    #define EV_MSC 0x04//其它类
    #define EV_SW 0x05//开关事件
    #define EV_LED 0x11//LED或其它指示设备
    #define EV_SND 0x12//声音输出,如蜂鸣器
    #define EV_REP 0x14//允许按键自重复
    #define EV_FF 0x15//力反馈
    #define EV_PWR 0x16//电源管理事件
    include/linux/input.h中定义了支持的类型
  49. struct input_handler {
  50. void *private;
  51. void (*event)(struct input_handle *handle, unsigned int type, unsigned int code, int value);
  52. int (*connect)(struct input_handler *handler, struct input_dev *dev, conststruct input_device_id *id);
  53. void (*disconnect)(struct input_handle *handle);
  54. void (*start)(struct input_handle *handle);
  55. conststruct file_operations *fops;
  56. int minor; //次设备号
  57. constchar *name;
  58. conststruct input_device_id *id_table;
  59. conststruct input_device_id *blacklist;
  60. struct list_head h_list; //h_list是一个链表头,用来把handle挂载在这个上
  61. struct list_head node; //这个node是用来连到input_handler_list上的
  62. };
  63. struct input_handle {
  64. void *private;
  65. int open;
  66. constchar *name;
  67. struct input_dev *dev; //指向input_dev
  68. struct input_handler *handler; //指向input_handler
  69. struct list_head d_node; //连到input_dev的h_list上
  70. struct list_head h_node; //连到input_handler的h_list上
  71. };
如下图代表了input_dev,input_handler,input_handle,3者之间的关系。一类handler可以和多个硬件设备相关联,一个硬件设备可以和多个handler相关联。例如:一个触摸屏设备可以作为一个event设备,作为一个鼠标设备,也可以作为一个触摸设备,所以一个设备需要与多个平台驱动进行连接。而一个平台驱动也不只为一个设备服务,一个触摸平台驱动可能要为A,B,C3个触摸设备提供上层驱动,所以需要这样一对多的连接。


下面来看看input字符设备注册过程:

  1. staticint __init input_init(void)
  2. {
  3. int err;
  4. input_init_abs_bypass();
  5. /*创建一个类input_class*/
  6. err = class_register(&input_class);
  7. if (err) {
  8. printk(KERN_ERR"input: unable to register input_dev class/n");
  9. return err;
  10. }
  11. /*在/proc下创建入口项*/
  12. err = input_proc_init();
  13. if (err)
  14. goto fail1;
  15. /*注册设备号INPUT_MAJOR的设备,记住input子系统的设备的主设备号都是13,即INPUT_MAJOR为13,并与input_fops相关联*/
  16. err = register_chrdev(INPUT_MAJOR,"input", &input_fops);
  17. if (err) {
  18. printk(KERN_ERR"input: unable to register char major %d", INPUT_MAJOR);
  19. goto fail2;
  20. }
  21. return 0;
  22. fail2: input_proc_exit();
  23. fail1: class_unregister(&input_class);
  24. return err;
  25. }
  26. subsys_initcall(input_init);
下面来看input子系统的file_operations,这里只有一个打开函数input_open_file,这个在事件传递部分讲解。
  1. staticconststruct file_operations input_fops = {
  2. .owner = THIS_MODULE,
  3. .open = input_open_file,
  4. };
下边来看input_dev设备的注册:
  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. constchar *path;
  6. int error;
  7. __set_bit(EV_SYN, dev->evbit);
  8. /*
  9. * If delay and period are pre-set by the driver, then autorepeating
  10. * is handled by the driver itself and we don't do it in input.c.
  11. */
  12. init_timer(&dev->timer);
  13. /*
  14. *rep主要是处理重复按键,如果没有定义dev->rep[REP_DELAY]和dev->rep[REP_PERIOD],
  15. *则将其赋值为默认值。dev->rep[REP_DELAY]是指第一次按下多久算一次,这里是250ms,
  16. *dev->rep[REP_PERIOD]指如果按键没有被抬起,每33ms算一次。
  17. */
  18. if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
  19. dev->timer.data = (long) dev;
  20. dev->timer.function = input_repeat_key;
  21. dev->rep[REP_DELAY] = 250;
  22. dev->rep[REP_PERIOD] = 33;
  23. }
  24. /*如果dev没有定义getkeycode和setkeycode,则赋默认值。他们的作用一个是获得键的扫描码,一个是设置键的扫描码*/
  25. if (!dev->getkeycode)
  26. dev->getkeycode = input_default_getkeycode;
  27. if (!dev->setkeycode)
  28. dev->setkeycode = input_default_setkeycode;
  29. dev_set_name(&dev->dev,"input%ld",
  30. (unsignedlong) atomic_inc_return(&input_no) - 1);
  31. /*将input_dev封装的dev注册到sysfs*/
  32. error = device_add(&dev->dev);
  33. if (error)
  34. return error;
  35. path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
  36. printk(KERN_INFO"input: %s as %s/n",
  37. dev->name ? dev->name :"Unspecified device", path ? path :"N/A");
  38. kfree(path);
  39. error = mutex_lock_interruptible(&input_mutex);
  40. if (error) {
  41. device_del(&dev->dev);
  42. return error;
  43. }
  44. /*将input_dev挂在input_dev_list上*/
  45. list_add_tail(&dev->node, &input_dev_list);
  46. /*匹配所有的input_handler,这个就是刚才那幅图里的一个设备对应多个handler的由来*/
  47. list_for_each_entry(handler, &input_handler_list, node)
  48. input_attach_handler(dev, handler);
  49. input_wakeup_procfs_readers();
  50. mutex_unlock(&input_mutex);
  51. return 0;
  52. }
跟踪程序,来看看input_attach_handler的实现:
  1. staticint input_attach_handler(struct input_dev *dev, struct input_handler *handler)
  2. {
  3. conststruct input_device_id *id;
  4. int error;
  5. /*handler有一个黑名单,如果存在黑名单,并且这个id匹配就退出*/
  6. if (handler->blacklist && input_match_device(handler->blacklist, dev))
  7. return -ENODEV;
  8. /*匹配id,实现在下边可以看到*/
  9. id = input_match_device(handler->id_table, dev);
  10. if (!id)
  11. return -ENODEV;
  12. /*如果匹配,则调用具体的handler的connect函数*/
  13. error = handler->connect(handler, dev, id);
  14. if (error && error != -ENODEV)
  15. printk(KERN_ERR
  16. "input: failed to attach handler %s to device %s, "
  17. "error: %d/n",
  18. handler->name, kobject_name(&dev->dev.kobj), error);
  19. return error;
  20. }
下边来看看这个匹配函数:如果id->flags存在,并且相应的标志为被设定则进行比较。
  1. staticconststruct input_device_id *input_match_device(conststruct input_device_id *id,
  2. struct input_dev *dev)
  3. {
  4. int i;
  5. for (; id->flags || id->driver_info; id++) {
  6. if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
  7. if (id->bustype != dev->id.bustype)
  8. continue;
  9. if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
  10. if (id->vendor != dev->id.vendor)
  11. continue;
  12. if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
  13. if (id->product != dev->id.product)
  14. continue;
  15. if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
  16. if (id->version != dev->id.version)
  17. continue;
  18. MATCH_BIT(evbit, EV_MAX);
  19. MATCH_BIT(keybit, KEY_MAX);
  20. MATCH_BIT(relbit, REL_MAX);
  21. MATCH_BIT(absbit, ABS_MAX);
  22. MATCH_BIT(mscbit, MSC_MAX);
  23. MATCH_BIT(ledbit, LED_MAX);
  24. MATCH_BIT(sndbit, SND_MAX);
  25. MATCH_BIT(ffbit, FF_MAX);
  26. MATCH_BIT(swbit, SW_MAX);
  27. return id;
  28. }
  29. return NULL;
  30. }
  1. #define MATCH_BIT(bit, max) /
  2. for (i = 0; i < BITS_TO_LONGS(max); i++) /
  3. if ((id->bit[i] & dev->bit[i]) != id->bit[i]) /
  4. break; /
  5. if (i != BITS_TO_LONGS(max)) /
  6. continue;
Input_dev和input_handler匹配后调用input_handler的connect。以evdev_handler为例: 如果匹配上了就会创建一个evdev,它里边封装了一个handle,会把input_dev和input_handler关联到一起。
  1. /*
  2. * Create new evdev device. Note that input core serializes calls
  3. * to connect and disconnect so we don't need to lock evdev_table here.
  4. */
  5. staticint evdev_connect(struct input_handler *handler, struct input_dev *dev,
  6. conststruct input_device_id *id)
  7. {
  8. struct evdev *evdev;
  9. int minor;
  10. int error;
  11. /*
  12. *首先补充几个知识点:
  13. *static struct input_handler *input_table[8];
  14. *#define INPUT_DEVICES 256
  15. *一共有8个input_handler,对应256个设备,所以一个handler对应32个设备。
  16. *这个问题在我参加的一次linux驱动的面试中被问到,当时真是汗啊!!!
  17. *static struct evdev *evdev_table[EVDEV_MINORS];
  18. *#define EVDEV_MINORS 32
  19. *evdev理论上可对应32个设备,其对应的设备节点一般位于/dev/input/event0~/dev/input/event4
  20. *下边的for循环,在evdev_table数组中找一个未使用的地方
  21. */
  22. for (minor = 0; minor < EVDEV_MINORS; minor++)
  23. if (!evdev_table[minor])
  24. break;
  25. if (minor == EVDEV_MINORS) {
  26. printk(KERN_ERR"evdev: no more free evdev devices/n");
  27. return -ENFILE;
  28. }
  29. /*下边的代码是为每一个匹配的设备分配一个evdev结构体,并对成员进行初始化*/
  30. evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
  31. if (!evdev)
  32. return -ENOMEM;
  33. INIT_LIST_HEAD(&evdev->client_list);
  34. spin_lock_init(&evdev->client_lock);
  35. mutex_init(&evdev->mutex);
  36. init_waitqueue_head(&evdev->wait);
  37. snprintf(evdev->name,sizeof(evdev->name),"event%d", minor);
  38. evdev->exist = 1;
  39. evdev->minor = minor;
  40. evdev->handle.dev = input_get_device(dev);
  41. evdev->handle.name = evdev->name;
  42. evdev->handle.handler = handler;
  43. evdev->handle.private = evdev;
  44. dev_set_name(&evdev->dev, evdev->name);
  45. evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor);
  46. evdev->dev.class = &input_class; /*调用函数创建字符设备节点*/
  47. evdev->dev.parent = &dev->dev;
  48. evdev->dev.release = evdev_free;
  49. /**/
  50. device_initialize(&evdev->dev);
  51. /*
  52. *input_register_handle完成的主要功能是:
  53. *list_add_tail_rcu(&handle->d_node, &dev->h_list);
  54. *list_add_tail(&handle->h_node, &handler->h_list);
  55. */
  56. error = input_register_handle(&evdev->handle);
  57. if (error)
  58. goto err_free_evdev;
  59. /*evdev_install_chrdev完成的功能是evdev_table[evdev->minor]=evdev;*/
  60. error = evdev_install_chrdev(evdev);
  61. if (error)
  62. goto err_unregister_handle;
  63. error = device_add(&evdev->dev);
  64. if (error)
  65. goto err_cleanup_evdev;
  66. return 0;
  67. 。。。。。。。。。。
  68. }
input子系统最重要的部分就是向上层report了。这里还是先介绍几个数据结构:
  1. struct input_event {
  2. struct timeval time; //事件发生的时间
  3. __u16 type;//事件类型
  4. __u16 code;//子事件
  5. __s32 value;//事件的value
  6. };

  1. struct evdev_client {
  2. struct input_event buffer[EVDEV_BUFFER_SIZE]; //可以同时管理EVDEV_BUFFER_SIZE(64)个事件
  3. int head; //存储事件从head开始
  4. int tail; //取出事件从tail开始
  5. spinlock_t buffer_lock;/* protects access to buffer, head and tail */
  6. struct fasync_struct *fasync; //异步通知事件发生
  7. struct evdev *evdev; //指向本evdev_client归属的evdev
  8. struct list_head node; //用于挂载到evdev的链表头client_list上
  9. };

  1. staticstruct input_handler evdev_handler = {
  2. .event = evdev_event, //向系统报告input事件,系统通过read方法读取
  3. .connect = evdev_connect,//和input_dev匹配后调用connect构建
  4. .disconnect = evdev_disconnect,
  5. .fops = &evdev_fops,//event设备文件的操作方法
  6. .minor = EVDEV_MINOR_BASE,//次设备号基准值
  7. .name ="evdev",
  8. .id_table = evdev_ids,//匹配规则
  9. };
这里的次设备号是EVDEV_MINOR_BASE(64),也就是说evdev_handler所表示的设备文件范围(13,64)~(13,64+32)。
如下一个结构体:evdev_handler匹配所有设备。
  1. staticconststruct input_device_id evdev_ids[] = {
  2. { .driver_info = 1 },/* Matches all devices */
  3. { },/* Terminating zero entry */
  4. };
看一下这张图会对上边的结构有清楚的认知了:

这个是evdev_handler是fops,下面的讲解中会用到其中的open,read函数。
  1. staticconststruct file_operations evdev_fops = {
  2. .owner = THIS_MODULE,
  3. .read = evdev_read,
  4. .write = evdev_write,
  5. .poll = evdev_poll,
  6. .open = evdev_open,
  7. .release = evdev_release,
  8. .unlocked_ioctl = evdev_ioctl,
  9. #ifdef CONFIG_COMPAT
  10. .compat_ioctl = evdev_ioctl_compat,
  11. #endif
  12. .fasync = evdev_fasync,
  13. .flush = evdev_flush
  14. };
在驱动程序中我们会调用input_report_abs等函数:

设备驱动通过宏set_bit()告诉input子系统它支持哪些事件,如下所示
set_bit(EV_KEY, input_dev->keybit); //EV_KEY事件支持的事件码
struct input_dev中有两个成员,一个是 unsigned long evbit ,一个是 unsigned long keybit ,分别用来表示设备所支持的事件类型和按键类型。

用于报告EV_KEY、EV_REL、EV_ABS、EV_FF、EV_SW等事件的函数有:

void input_report_key(struct input_dev *dev, unsigned int code, int value)
void input_report_rel(struct input_dev *dev, unsigned int code, int value)
void input_report_abs(struct input_dev *dev, unsigned int code, int value)
void input_report_ff_status(struct input_dev *dev, unsigned int code, int value)

void input_report_switch(struct input_dev *dev, unsigned int code, int value)

如果你觉得麻烦,你也可以只记住1个函数(因为上述函数都是通过它实现的)

void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)

相关参数介绍:
code:
事件的代码。如果事件的类型是EV_KEY,该代码code为设备键盘代码。代码值0-127为键盘上的按键代码,0x110-0x116为鼠标上按键代码,其中0x110(BTN_LEFT)为鼠标左键,0x111(BTN_RIGHT)为鼠标右键,0x112(BTN_MIDDLE)为鼠标中键。其它代码含义请参看include/linux/input.h文件。

value:
事件的值。如果事件的类型是EV_KEY,当按键按下时值为1,松开时值为0.

事件报告完毕后,设备驱动需要使用input_sync函数告诉输入子系统一个完整的报告已经发送。
void input_sync(struct input_dev *dev)
{
input_event(dev,EV_SYN,SYN_REPORT,0);
}
这一点在鼠标移动处理中很重要,因为鼠标坐标的X分量和Y分量是分开传送的,需要利用
input_sync函数来同步。
跟踪input_event如下:
  1. void input_event(struct input_dev *dev,
  2. unsignedint type, unsignedint code, int value)
  3. {
  4. unsignedlong flags;
  5. if (is_event_supported(type, dev->evbit, EV_MAX)) {
  6. spin_lock_irqsave(&dev->event_lock, flags);
  7. /*利用输入值调正随机数产生器*/
  8. add_input_randomness(type, code, value);
  9. input_handle_event(dev, type, code, value);
  10. spin_unlock_irqrestore(&dev->event_lock, flags);
  11. }
  12. }
跟踪input_handle_event如下:
  1. staticvoid input_handle_event(struct input_dev *dev,
  2. unsignedint type, unsignedint code, int value)
  3. {
  4. int disposition = INPUT_IGNORE_EVENT;
  5. switch (type) {
  6. 。。。。。。。。。。。。。。。。
  7. if (disposition != INPUT_IGNORE_EVENT && type != EV_SYN)
  8. dev->sync = 0;
  9. if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)
  10. dev->event(dev, type, code, value);
  11. if (disposition & INPUT_PASS_TO_HANDLERS)
  12. input_pass_event(dev, type, code, value);
  13. }
如果该事件需要input device来完成,就会将disposition设置成INPUT_PASS_TO_DEVICE,如果需要input handler来完成,就会将disposition设置成INPUT_PASS_TO_DEVICE,如果需要两者都参与,则将disposition设置成INPUT_PASS_TO_ALL。
跟踪input_pass_event如下:
  1. staticvoid input_pass_event(struct input_dev *dev,
  2. unsignedint type, unsignedint code, int value)
  3. {
  4. struct input_handle *handle;
  5. rcu_read_lock();
  6. /**/
  7. handle = rcu_dereference(dev->grab);
  8. if (handle)
  9. /*如果input_dev的grab指向了一个handle,就用这个handle关联的handler的event,否则遍历整个挂在input_dev的h_list上的handle关联的handler*/
  10. handle->handler->event(handle, type, code, value);
  11. else
  12. list_for_each_entry_rcu(handle, &dev->h_list, d_node)
  13. if (handle->open)
  14. handle->handler->event(handle,
  15. type, code, value);
  16. rcu_read_unlock();
  17. }
比如下边的evdev_handler的evdev_event:
  1. staticvoid evdev_event(struct input_handle *handle,
  2. unsignedint type, unsignedint code, int value)
  3. {
  4. struct evdev *evdev = handle->private;
  5. struct evdev_client *client;
  6. struct input_event event;
  7. do_gettimeofday(&event.time);
  8. event.type = type;
  9. event.code = code;
  10. event.value = value;
  11. rcu_read_lock();
  12. client = rcu_dereference(evdev->grab);
  13. if (client)
  14. /*如果evdev->grab指向一个当前使用的client就将event放到这个client的buffer中,否则放到整个client_list上的client的链表中*/
  15. evdev_pass_event(client, &event);
  16. else
  17. list_for_each_entry_rcu(client, &evdev->client_list, node)
  18. evdev_pass_event(client, &event);
  19. rcu_read_unlock();
  20. wake_up_interruptible(&evdev->wait);
  21. }
  1. staticvoid evdev_pass_event(struct evdev_client *client,
  2. struct input_event *event)
  3. {
  4. /*
  5. * Interrupts are disabled, just acquire the lock
  6. */
  7. spin_lock(&client->buffer_lock);
  8. /*将event装入client的buffer中,buffer是一个环形缓存区*/
  9. client->buffer[client->head++] = *event;
  10. client->head &= EVDEV_BUFFER_SIZE - 1;
  11. spin_unlock(&client->buffer_lock);
  12. kill_fasync(&client->fasync, SIGIO, POLL_IN);
  13. }

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

当用户空间打开设备节点/dev/input/event0~/dev/input/event4的时候,会使用input_fops中的input_open_file()函数,input_open_file()->evdev_open()(如果handler是evdev的话)->evdev_open_device()->input_open_device()->dev->open()。也就是struct file_operations input_fops提供了通用接口,最终会调用具体input_dev的open函数。下边看一下用户程序打开文件时的过程,首先调用了input_open_file:

  1. staticint input_open_file(struct inode *inode, struct file *file)
  2. {
  3. struct input_handler *handler;
  4. conststruct file_operations *old_fops, *new_fops = NULL;
  5. int err;
  6. lock_kernel();
  7. /* No load-on-demand here? */
  8. /*因为32个input_dev公共一个handler所以低5位应该是相同的*/
  9. handler = input_table[iminor(inode) >> 5];
  10. if (!handler || !(new_fops = fops_get(handler->fops))) {
  11. err = -ENODEV;
  12. gotoout;
  13. }
  14. /*
  15. * That's _really_ odd. Usually NULL ->open means "nothing special",
  16. * not "no device". Oh, well...
  17. */
  18. if (!new_fops->open) {
  19. fops_put(new_fops);
  20. err = -ENODEV;
  21. gotoout;
  22. }
  23. /*保存以前的fops,使用相应的handler的fops*/
  24. old_fops = file->f_op;
  25. file->f_op = new_fops;
  26. err = new_fops->open(inode, file);
  27. if (err) {
  28. fops_put(file->f_op);
  29. file->f_op = fops_get(old_fops);
  30. }
  31. fops_put(old_fops);
  32. out:
  33. unlock_kernel();
  34. return err;
  35. }

这里还是假设handler是evdev_handler。
  1. staticint evdev_open(struct inode *inode, struct file *file)
  2. {
  3. struct evdev *evdev;
  4. struct evdev_client *client;
  5. /*因为次设备号是从EVDEV_MINOR_BASE开始的*/
  6. int i = iminor(inode) - EVDEV_MINOR_BASE;
  7. int error;
  8. if (i >= EVDEV_MINORS)
  9. return -ENODEV;
  10. error = mutex_lock_interruptible(&evdev_table_mutex);
  11. if (error)
  12. return error;
  13. /*evdev_table一共可容纳32个成员,找到次设备号对应的那个*/
  14. evdev = evdev_table[i];
  15. if (evdev)
  16. get_device(&evdev->dev);
  17. mutex_unlock(&evdev_table_mutex);
  18. if (!evdev)
  19. return -ENODEV;
  20. /*打开的时候创建一个client*/
  21. client = kzalloc(sizeof(struct evdev_client), GFP_KERNEL);
  22. if (!client) {
  23. error = -ENOMEM;
  24. goto err_put_evdev;
  25. }
  26. spin_lock_init(&client->buffer_lock);
  27. /*下边两句的作用就是将evdev和client绑定到一起*/
  28. client->evdev = evdev;
  29. evdev_attach_client(evdev, client);
  30. error = evdev_open_device(evdev);
  31. if (error)
  32. goto err_free_client;
  33. /*将file->private_data指向刚刚建的client,后边会用到的*/
  34. file->private_data = client;
  35. return 0;
  36. err_free_client:
  37. evdev_detach_client(evdev, client);
  38. kfree(client);
  39. err_put_evdev:
  40. put_device(&evdev->dev);
  41. return error;
  42. }

  1. staticint evdev_open_device(struct evdev *evdev)
  2. {
  3. int retval;
  4. retval = mutex_lock_interruptible(&evdev->mutex);
  5. if (retval)
  6. return retval;
  7. /*如果设备不存在,返回错误*/
  8. if (!evdev->exist)
  9. retval = -ENODEV;
  10. /*如果是被第一次打开,则调用input_open_device*/
  11. elseif (!evdev->open++) {
  12. retval = input_open_device(&evdev->handle);
  13. if (retval)
  14. evdev->open--;
  15. }
  16. mutex_unlock(&evdev->mutex);
  17. return retval;
  18. }

  1. int input_open_device(struct input_handle *handle)
  2. {
  3. struct input_dev *dev = handle->dev;
  4. int retval;
  5. retval = mutex_lock_interruptible(&dev->mutex);
  6. if (retval)
  7. return retval;
  8. if (dev->going_away) {
  9. retval = -ENODEV;
  10. gotoout;
  11. }
  12. handle->open++;
  13. if (!dev->users++ && dev->open)
  14. retval = dev->open(dev);
  15. if (retval) {
  16. dev->users--;
  17. if (!--handle->open) {
  18. /*
  19. * Make sure we are not delivering any more events
  20. * through this handle
  21. */
  22. synchronize_rcu();
  23. }
  24. }
  25. out:
  26. mutex_unlock(&dev->mutex);
  27. return retval;
  28. }
下面是用户进程读取event的底层实现:
  1. static ssize_t evdev_read(struct file *file, char __user *buffer,
  2. size_t count, loff_t *ppos)
  3. {
  4. /*这个就是刚才在open函数中*/
  5. struct evdev_client *client = file->private_data;
  6. struct evdev *evdev = client->evdev;
  7. struct input_event event;
  8. int retval;
  9. if (count < input_event_size())
  10. return -EINVAL;
  11. /*如果client的环形缓冲区中没有数据并且是非阻塞的,那么返回-EAGAIN,也就是try again*/
  12. if (client->head == client->tail && evdev->exist &&
  13. (file->f_flags & O_NONBLOCK))
  14. return -EAGAIN;
  15. /*如果没有数据,并且是阻塞的,则在等待队列上等待吧*/
  16. retval = wait_event_interruptible(evdev->wait,
  17. client->head != client->tail || !evdev->exist);
  18. if (retval)
  19. return retval;
  20. if (!evdev->exist)
  21. return -ENODEV;
  22. /*如果获得了数据则取出来,调用evdev_fetch_next_event*/
  23. while (retval + input_event_size() <= count &&
  24. evdev_fetch_next_event(client, &event)) {
  25. /*input_event_to_user调用copy_to_user传入用户程序中,这样读取完成*/
  26. if (input_event_to_user(buffer + retval, &event))
  27. return -EFAULT;
  28. retval += input_event_size();
  29. }
  30. return retval;
  31. }

  1. staticint evdev_fetch_next_event(struct evdev_client *client,
  2. struct input_event *event)
  3. {
  4. int have_event;
  5. spin_lock_irq(&client->buffer_lock);
  6. /*先判断一下是否有数据*/
  7. have_event = client->head != client->tail;
  8. /*如果有就从环形缓冲区的取出来,记得是从head存储,tail取出*/
  9. if (have_event) {
  10. *event = client->buffer[client->tail++];
  11. client->tail &= EVDEV_BUFFER_SIZE - 1;
  12. }
  13. spin_unlock_irq(&client->buffer_lock);
  14. return have_event;
  15. }

  1. int input_event_to_user(char __user *buffer,
  2. conststruct input_event *event)
  3. {
  4. /*如果设置了标志INPUT_COMPAT_TEST就将事件event包装成结构体compat_event*/
  5. if (INPUT_COMPAT_TEST) {
  6. struct input_event_compat compat_event;
  7. compat_event.time.tv_sec =event->time.tv_sec;
  8. compat_event.time.tv_usec =event->time.tv_usec;
  9. compat_event.type =event->type;
  10. compat_event.code =event->code;
  11. compat_event.value =event->value;
  12. /*将包装成的compat_event拷贝到用户空间*/
  13. if (copy_to_user(buffer, &compat_event,
  14. sizeof(struct input_event_compat)))
  15. return -EFAULT;
  16. }else {
  17. /*否则,将event拷贝到用户空间*/
  18. if (copy_to_user(buffer, event,sizeof(struct input_event)))
  19. return -EFAULT;
  20. }
  21. return 0;
  22. }
这里总结一下:如果两个进程打开同一个文件,每个进程在打开时都会生成一个evdev_client,evdev_client被挂在evdev的client_list,在handle收到一个事件的时候,会把事件copy到挂在client_list上的所有evdev_client的buffer中。这样所有打开同一个设备的进程都会收到这个消息而唤醒。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值