Linux输入子系统

                                     Linux输入子系统

      Linux 的输入子系统不仅支持鼠标、键盘等常规输入设备,而且还支持蜂鸣器、触摸屏等设备。本章将对 Linux 输入子系统进行详细的分析。

 

一 总述      

        输入子系统又叫 input 子系统。其构建非常灵活,只需要调用一些简单的函数,就可以将一个输入设备的功能呈现

给应用程序。

                                        

二   设备驱动层

        这个输入设备只有一个按键,按键被连接到一条中断线上,当按键被按下时,将产生一个中断,内核将检测到这个中

断,并对其进行处理。该实例的代码如下:

#include <asm/irq.h>
#include <asm/io.h>

static struct input_dev *button_dev;   /*输入设备结构体*/
static irqreturn_t button_interrupt(int irq, void *dummy)     /*中断处理函数*/
{
        input_report_key(button_dev, BTN_0, inb(BUTTON_PORT) & 1);  /*报告产生按键事件*/
        input_sync(button_dev);    /*通知接收者,一个报告发送完毕*/
        return IRQ_HANDLED;
 }

 static int __init button_init(void)      /*加载函数*/
 {
        int error;
        if (request_irq(BUTTON_IRQ, button_interrupt, 0, "button", NULL))  /*申请中断*/
        {
                 printk(KERN_ERR "button.c: Can't allocate irq %d\n", button_irq);
                 return -EBUSY;
         }

        button_dev = input_allocate_device();     /*分配一个设备结构体*/
         if (!button_dev)
        {
              printk(KERN_ERR "button.c: Not enough memory\n");
              error = -ENOMEM;
              goto err_free_irq;

         }

        //分别用来设置设备支持事件以及上报的按键值。
         button_dev->evbit[0] = BIT_MASK(EV_KEY);    /*设置按键信息*/
         button_dev->keybit[BIT_WORD(BTN_0)] = BIT_MASK(BTN_0);
        error =input_register_device(button_dev);      /*注册一个输入设备*/
         if (error)
         {
                 printk(KERN_ERR "button.c: Failed to register device\n");
                 goto err_free_dev;
         }

        return 0;

        err_free_dev:
         input_free_device(button_dev);
        err_free_irq:
          free_irq(BUTTON_IRQ, button_interrupt);
          return error;                   
}

static void __exit button_exit(void)      /*卸载函数*/
{
        input_unregister_device(button_dev);         /*注销按键设备*/
        free_irq(BUTTON_IRQ, button_interrupt);        /*释放按键占用的中断线*/
}
module_init(button_init);
module_exit(button_exit);

这个实例程序代码比较简单,在初始化函数 button_init()中注册了一个中断处理函数,然后调用

input_allocate_device()函数分配了一个 input_dev 结构体,并调用 input_register_device()函数对其进行了注册。在中

断处理函数 button_interrupt()中,实例将接收到的按键信息上报给 input 子系统。从而通过 input 子系统,向用户态程序

提供按键输入信息。本实例采用了中断方式,除了中断相关的代码外,实例中包含了一些 input 子系统提供的函数,现对

 

三  核心层

创建输入设备:input_allocate_device()

struct input_dev *input_allocate_device(void)
{
       struct input_dev *dev;
       dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL); /*分配一个 input_dev 结构体*/
       if (dev)
       {
             dev->dev.type = &input_dev_type;   /*初始化设备的类型*/
             dev->dev.class = &input_class; 
             device_initialize(&dev->dev);
             mutex_init(&dev->mutex);   // 初始话互斥锁
             spin_lock_init(&dev->event_lock); // 初始化自旋锁
             INIT_LIST_HEAD(&dev->h_list);   //初始化链表
             INIT_LIST_HEAD(&dev->node); 
             __module_get(THIS_MODULE);
      }

      return dev;
}

Input设备注册的接口为:input_register_device()。代码如下:


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

       /* 调用__set_bit()函数设置 input_dev 所支持的事件类型。事件类型由 input_dev 的evbit 
          成员来表示,在这里将其 EV_SYN 置位,表示设备支持所有的事件。注意,一个设备可以支持一种或
          者多种事件类型。常用的事件类型如下:
          1. #define EV_SYN     0x00   /*表示设备支持所有的事件*/
          2. #define EV_KEY     0x01  /*键盘或者按键,表示一个键码*/
          3. #define EV_REL     0x02  /*鼠标设备,表示一个相对的光标位置结果*/
          4. #define EV_ABS     0x03  /*手写板产生的值,其是一个绝对整数值*/
          5. #define EV_MSC     0x04  /*其他类型*/
          6. #define EV_LED     0x11   /*LED 灯设备*/
          7. #define EV_SND     0x12  /*蜂鸣器,输入声音*/
          8. #define EV_REP     0x14   /*允许重复按键类型*/
          9. #define EV_PWR     0x16   /*电源管理事件*/
        */
    
      /*
        * 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;
//input_default_getkeycode()用来得到指定位置的键值
//input_default_setkeycode()函数用来设置键值。具体啥用处,我也没搞清楚?

//设置input_dev中的device的名字,以input0、input1、input2、input3、input4等的形式出现在 sysfs文件系统中. 
       snprintf(dev->dev.bus_id, sizeof(dev->dev.bus_id),
                "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);

//使用 device_add()函数将 input_dev 包含的 device 结构注册到 Linux 设备模型中,并可以在 sysfs
//文件系统中表现出来。

         error = device_add(&dev->dev);
         if (error)
             return error;

         path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
         printk(KERN_INFO "input: %s as %s/n",
                   dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
         kfree(path);
         error = mutex_lock_interruptible(&input_mutex);
         if (error) {
                   device_del(&dev->dev);
                   return error;
         }
 
         list_add_tail(&dev->node, &input_dev_list);
         list_for_each_entry(handler, &input_handler_list, node)
            input_attach_handler(dev, handler); //去匹配handler

         input_wakeup_procfs_readers();
         mutex_unlock(&input_mutex);
         return 0;
}

匹配是在input_attach_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))
                   return -ENODEV;

         id = input_match_device(handler->id_table, dev);
         if (!id)
             return -ENODEV;
         error = handler->connect(handler, dev, id);

//如果匹配成功,则调用 handler->connect()函数将 handler 与 input_dev 连接起来。
//在connect() 中会调用input_register_handle,而这些都需要handler的注册。

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

input_match_device()代码如下:

static const struct input_device_id *input_match_device(const struct input_device_id *id,struct input_dev *dev)
{
         int i;
         for (; id->flags || id->driver_info; id++) {
        //匹配标志特别说明INPUT_DEVICE_ID_MATCH_BUS,则需匹配bustype,下同

                   if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
                       if (id->bustype != dev->id.bustype)
                           continue;
                   if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
                       if (id->vendor != dev->id.vendor)
                             continue;
                   if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
                        if (id->product != dev->id.product)
                            continue;
                   if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
                        if (id->version != dev->id.version)
                            continue;
                   MATCH_BIT(evbit,  EV_MAX);
                   MATCH_BIT(,, KEY_MAX);
                   MATCH_BIT(relbit, REL_MAX);
                   MATCH_BIT(absbit, ABS_MAX);
                   MATCH_BIT(mscbit, MSC_MAX);
                   MATCH_BIT(ledbit, LED_MAX);
                   MATCH_BIT(sndbit, SND_MAX);
                   MATCH_BIT(ffbit,  FF_MAX);
                   MATCH_BIT(swbit,  SW_MAX);
                  /*
                   MATCH_BIT宏的定义如下:
                   #define MATCH_BIT(bit, max)
                   for (i = 0; i < BITS_TO_LONGS(max); i++)
                            if ((id->bit[i] & dev->bit[i]) != id->bit[i])
                               break;
                   if (i != BITS_TO_LONGS(max))
                            continue;
                  */

                   return id;
         }
       return NULL;
}


//从MATCH_BIT宏的定义可以看出。只有当iput deviceinput handlerid成员在evbit, keybit,… swbit项相同才会匹配成功。而且匹配的顺序是从evbit, keybitswbit.只要有一项不同,就会循环到id中的下一项进行比较

 

我们先把核心层的东西讲完,在前面的设备驱动层中的中断响应函数里面,有input_report_key 函数 ,下面我们来看看他

input_report_key()函数向输入子系统报告发生的事件,这里就是一个按键事件。在 button_interrupt()中断函数中,

不需要考虑重复按键的重复点击情况,input_report_key()函数会自动检查这个问题,并报告一次事件给输入子系统。该

函数的代码如下:

static inline void input_report_key(struct input_dev *dev, unsigned int code, int value)

{
        input_event(dev, EV_KEY, code, !!value);

}

//该函数的第 1 个参数是产生事件的输入设备, 2 个参数是产生的事件, 3 个参数是事件的值。需要注意的是, 第2 个参数可以取类似 BTN_0、 BTN_1、BTN_LEFT、BTN_RIGHT 等值,这些键值被定义在 include/linux/input.h 文件中。当第 2 个参数为按键时,第 3 个参数表示按键的状态,value 值为 0 表示按键释放,非 0 表示按键按下。


void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
 {
        unsigned long flags;
        if (is_event_supported(type, dev->evbit, EV_MAX)) {  //检查输入设备是否支持该事件
        spin_lock_irqsave(&dev->event_lock, flags);
        add_input_randomness(type, code, value);
        input_handle_event(dev, type, code, value); //继续输入子系统的相关模块发送数据。
        spin_unlock_irqrestore(&dev->event_lock, flags);
}
//input_report_key()函数中正在起作用的函数是 input_event()函数,该函数用来向输入子系统报告输入设备产生的事件,这个函数非常重要,它的代码如下:

浏览一下该函数的大部分代码,主要由一个 switch 结构组成。该结构用来对不同的事件类型,分别处理。其中 case

语句包含了 EV_SYN、 EV_KEY、EV_SW、EV_SW、EV_SND 等事件类型。在这么多事件中,本例只要关注

EV_KEY 事件,因为本节的实例发送的是按键事件。其实,只要对一个事件的处理过程了解后,对其他事件的处理过程也

就清楚了。该函数的代码如下:

static void input_handle_event(struct input_dev *dev,
{
        unsigned int type, unsigned int code, int value)
        int disposition = INPUT_IGNORE_EVENT; 

        switch (type) {
                case EV_SYN:
                     switch (code)
                     {
                                case SYN_CONFIG:
                                        disposition = INPUT_PASS_TO_ALL;
                                        break;
                                case SYN_REPORT:
                                if (!dev->sync)
                                {
                                        dev->sync = 1;
                                        disposition = INPUT_PASS_TO_HANDLERS;
                                }
                                break;
                        }       
                        break;
               case EV_KEY:
                         if (is_event_supported(code, dev->keybit, KEY_MAX) 
                             &&!!test_bit(code, dev->key) != value)//判断是否支持该按键
                         {
                                if (value != 2)
                                {
                                        __change_bit(code, dev->key);
                                        if (value)
                                           input_start_autorepeat(dev, code); //处理重复按键
                                  }
                                disposition = INPUT_PASS_TO_HANDLERS;

 /*将 disposition变量设置为 INPUT_PASS_TO_HANDLERS,表示事件需要 handler 来处理。

disposition 的取值有如下几种:
1. #define INPUT_IGNORE_EVENT           0
2. #define INPUT_PASS_TO_HANDLERS         1
3. #define INPUT_PASS_TO_DEVICE         2
4. #define INPUT_PASS_TO_ALL                 (INPUT_PASS_TO_HANDLERS | INPUT_PASS_TO_DEVICE)
*/

                     }
                        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))
                            {
                                        value = input_defuzz_abs_event(value,
                                        dev->abs[code], dev->absfuzz[code]);
                                        if (dev->abs[code] != value)
                                        {
                                                dev->abs[code] = value;
                                                disposition = INPUT_PASS_TO_HANDLERS;
                                         }
                            }
                            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;
        }
//首先判断 disposition 等于 INPUT_PASS_TO_DEVICE,然后判断 dev->event 是否对其指定了一个处理函数,如果这些条件都满足,则调用自定义的 dev->event()函数处理事件。

//有些事件是发送给设备,而不是发送给 handler 处理的。event()函数用来向输入子系统报告一个将要发送给设备的事件,例如让 LED 灯点亮事件、蜂鸣器鸣叫事件等。当事件报告给输入子系统后,就要求设备处理这个事件。
        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);
       if (disposition & INPUT_PASS_TO_HANDLERS)
               input_pass_event(dev, type, code, value);
}

input_pass_event()函数将事件传递到合适的函数,然后对其进行处理,该函数的代码如下

 static void input_pass_event(struct input_dev *dev, unsigned int type, unsigned int 
  code, int value)
 {
        struct input_handle *handle;
       rcu_read_lock();
       handle = rcu_dereference(dev->grab);
       if (handle)
              handle->handler->event(handle, type, code, value);
        else
              list_for_each_entry_rcu(handle, &dev->h_list, d_node)  //handle注册在handler注册函数里。input_dev,handle,handler 相互关联
        if (handle->open) //被应用打开了 则传递信息
              handle->handler->event(handle,type, code, value);

//如果该 handle 被打开,表示该设备已经被一个用户进程使用,见下文evdev的open分析。就会调用与输入设备handler 的 event()函数。
//注意,只有在 handle 被打开的情况下才会接收到事件,这就是说,只有设备被用户程序使用时,才有必要向用户空间导出信息

        rcu_read_unlock();
 }

//核心层就到此为止,前面也讲过在device和handler  connect() 时会调用input_register_handle,而这些都需要handler的注册,所以接下来我们看看事件层

 

四   事件层

      input_handler 是输入子系统的主要数据结构,一般将其称为 handler 处理器,表示对输入事件的具体处理。
      一个输入事件,如鼠标移动,键盘按键按下等通过驱动层->系统核心层->事件处理层->用户空间的顺序到达用户空间并传给应用程序使用。其中 Input Core 即输入子系统核心层由 driver/input/input.c 及相关头文件实现。其对下提供了设备驱动的接口,对上提供了事件处理层的编程接口。输入子系统主要设计 input_dev、input_handler、input_handle 等数据结构.

struct input_dev物理输入设备的基本数据结构,包含设备相关的一些信息

struct input_handler 事件处理结构体,定义怎么处理事件的逻辑

struct input_handle用来创建 input_dev 和 input_handler 之间关系的结构体

在evdev.c 中:

static struct input_handler evdev_handler = {

    .event        = evdev_event,  // 前面讲的传递信息是调用,在 input_pass_event 中     
    .connect    = evdev_connect,  //device 和 handler 匹配调用                           
    .disconnect    = evdev_disconnect,
    .fops        = &evdev_fops,                        // event 、connect、fops 会在后面详细讲                                 
    .minor        = EVDEV_MINOR_BASE,
    .name        = "evdev",
    .id_table    = evdev_ids,
};
struct input_handler {

      void *private;
      void (*event)(struct input_handle *handle, unsigned int type,
      unsigned int code, int value);
      int (*connect)(struct input_handler *handler, struct input_dev* dev, const struct input_device_id *id);
      void (*disconnect)(struct input_handle *handle);
      void (*start)(struct input_handle *handle);
      const struct file_operations *fops;
      int minor;  //表示设备的次设备号
      const char *name;//定义了一个 name,表示 handler 的名字,显示在/proc/bus/input/handlers 目录里

      const struct input_device_id *id_table;
      const struct input_device_id *blacklist; 
      struct list_head h_list;
      struct list_head node;
 };

事件层注册

static int __init evdev_init(void)
{
    return input_register_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->minor 表示对应 input 设备结点的次设备号。 handler->minor以右移 5 位作为索引值插入到input_table[ ]中

        if (handler->fops != NULL)
        {

               if (input_table[handler->minor >> 5])
               {
                      retval = -EBUSY;
                        goto out;
                }
                input_table[handler->minor >> 5] = handler;
        }

        list_add_tail(&handler->node, &input_handler_list);
        list_for_each_entry(dev, &input_dev_list, node)
            input_attach_handler(dev, handler);

        input_wakeup_procfs_readers();
out:
        mutex_unlock(&input_mutex);
        return retval;
 }

在前面r结构体中,有一个.connect    = evdev_connect,注册input设备时会调用到这里,同时也会注册handle。

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++)
                   if (!evdev_table[minor])
                            break;
         if (minor == EVDEV_MINORS) { //最多允许EVDEV_MINORS个设备
                   printk(KERN_ERR "evdev: no more free evdev devices/n");
                   return -ENFILE;
         }
         evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);//创建evdev设备
         if (!evdev)
                   return -ENOMEM;
         INIT_LIST_HEAD(&evdev->client_list);
         spin_lock_init(&evdev->client_lock);
         mutex_init(&evdev->mutex);
         init_waitqueue_head(&evdev->wait);
 
         snprintf(evdev->name, sizeof(evdev->name), "event%d", minor);//节点名称eventX
         evdev->exist = 1;
         evdev->minor = minor;
 
         evdev->handle.dev = input_get_device(dev);
         evdev->handle.name = evdev->name;
         evdev->handle.handler = handler;
         evdev->handle.private = evdev;

//分配了一个 evdev结构 ,并对这个结构进行初始化 .在这里我们可以看到 ,这个结构封装了一个 handle结构 ,这结构与我们之前所讨论的 handler是不相同的 .注意有一个字母的差别哦 .我们可以把 handle看成是 handler和 input device的信息集合体 .在这个结构里集合了匹配成功的 handler和 input device

         strlcpy(evdev->dev.bus_id, evdev->name, sizeof(evdev->dev.bus_id));
         evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor);
         evdev->dev.class = &input_class;//设备类 - 不清楚是否这边的操作可以在/dev/input/下见节点 ?
         evdev->dev.parent = &dev->dev;  //父设备
         evdev->dev.release = evdev_free;
         device_initialize(&evdev->dev);

//在这段代码里主要完成 evdev里头device的初始化 .注意在这里 ,使它所属的类指向 input_class.这样在 /sysfs中创建的设备目录就会在 /sys/class/input/下面显示 .

         error = input_register_handle(&evdev->handle);
         if (error)
                   goto err_free_evdev;
         error = evdev_install_chrdev(evdev);
         if (error)
                   goto err_unregister_handle;
 
         error = device_add(&evdev->dev);
         if (error)
                   goto err_cleanup_evdev;
 
         return 0;
 
 err_cleanup_evdev:
         evdev_cleanup(evdev);
 err_unregister_handle:
         input_unregister_handle(&evdev->handle);
 err_free_evdev:
         put_device(&evdev->dev);
         return error;
}


int input_register_handle(struct input_handle *handle)
{
         struct input_handler *handler = handle->handler;
         struct input_dev *dev = handle->dev;
         int error;
 
         /*
          * We take dev->mutex here to prevent race with
          * input_release_device().
          */
         error = mutex_lock_interruptible(&dev->mutex);
         if (error)
                   return error;
         list_add_tail_rcu(&handle->d_node, &dev->h_list);// handle挂在input_dev的链表
         mutex_unlock(&dev->mutex);
         synchronize_rcu();
 
         /*
          * Since we are supposed to be called from ->connect()
          * which is mutually exclusive with ->disconnect()
          * we can't be racing with input_unregister_handle()
          * and so separate lock is not needed here.
          */
         list_add_tail(&handle->h_node, &handler->h_list);//handle也挂在handler的链表
 
         if (handler->start)
                   handler->start(handle);
 
         return 0;
}

handle挂到所对应input deviceh_list链表上.还将handle挂到对应的handlerhlist链表上.

handle中包含handler和input_dev,这里又将handle添加到input_dev h_list,handler的h_list链表中

到这里,我们已经看到了input device, handlerhandle已经关联起来了

 

接下来我们看看上报信息是调用的  .event        = evdev_event

 每当input device上报一个事件时,会将其交给和它匹配的handler的event函数处理.在evdev中.这个event函数

对应的代码为:

static void evdev_event(struct input_handle *handle,
                        unsigned int type, unsigned int code, int value)
{
         struct evdev *evdev = handle->private;
         struct evdev_client *client;
         struct input_event event;
 
         do_gettimeofday(&event.time);
         event.type = type;
         event.code = code;
         event.value = value;
 
         rcu_read_lock();
 
         client = rcu_dereference(evdev->grab);
         if (client)
                   evdev_pass_event(client, &event);
         else
                   list_for_each_entry_rcu(client, &evdev->client_list, node)  
                            evdev_pass_event(client, &event);
 
         rcu_read_unlock();
 
         wake_up_interruptible(&evdev->wait);
}


static void evdev_pass_event(struct evdev_client *client,
                                 struct input_event *event)
{
         /*
          * Interrupts are disabled, just acquire the lock
          */
         spin_lock(&client->buffer_lock);
         client->buffer[client->head++] = *event;//添加到循环链表中
         client->head &= EVDEV_BUFFER_SIZE - 1;
         spin_unlock(&client->buffer_lock);
 
         kill_fasync(&client->fasync, SIGIO, POLL_IN);
}

    这里的操作很简单.就是将event(上传数据)保存到client->buffer中.而client->head就是当前的数据位置.注意这里

是一个环形缓存区.写数据是从client->head.而读数据则是从client->tail中读.

 

最后我们看下evdev的相关操作函数    .fops        = &evdev_fops, 

对主设备号为INPUT_MAJOR的设备节点进行操作,会将操作集转换成handler的操作集(后面分析).在evdev中,这个

操作集就是evdev_fops.对应的open函数如下示

static int evdev_open(struct inode *inode, struct file *file)
{ 
         struct evdev *evdev; 
         struct evdev_client *client; 
         int i = iminor(inode) - EVDEV_MINOR_BASE;//次偏移设备号偏移
         int error; 
  
         if (i >= EVDEV_MINORS) 
                   return -ENODEV; 
  
         error = mutex_lock_interruptible(&evdev_table_mutex);
         if (error) 
                   return error; 
         evdev = evdev_table[i]; //获取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; 
         evdev_attach_client(evdev, client); 
  
         error = evdev_open_device(evdev);//打开 
         if (error) 
                   goto err_free_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; 
}


static int evdev_open_device(struct evdev *evdev)
{
         int retval;
 
         retval = mutex_lock_interruptible(&evdev->mutex);
         if (retval)
                   return retval;
 
         if (!evdev->exist)
                   retval = -ENODEV;
         else if (!evdev->open++) { // 未开启则打开
                   retval = input_open_device(&evdev->handle);
                   if (retval)
                            evdev->open--;
         }
 
         mutex_unlock(&evdev->mutex);
         return retval;
} 



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

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

    if (dev->going_away) {
        retval = -ENODEV;
        goto out;
    }

    handle->open++; //表示应用层已经打开设备,允许数据传输

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

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

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


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;
         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,
                   client->head != client->tail || !evdev->exist);//阻塞等待 循环队列头尾不相等 则说明有数据
         if (retval)
                   return retval;
 
         if (!evdev->exist)
                   return -ENODEV;
 
         while (retval + evdev_event_size() <= count &&
                evdev_fetch_next_event(client, &event)) {
 
                   if (evdev_event_to_user(buffer + retval, &event)) // 数据拷贝到用户层
                            return -EFAULT;
 
                   retval += evdev_event_size();
         }
 
         return retval;
} 

      首先,它判断缓存区大小是否足够.在读取数据的情况下,可能当前缓存区内没有数据可读.在这里先睡眠等待缓存

区中有数据.如果在睡眠的时候,.条件满足.是不会进行睡眠状态而直接返回的.然后根据read()提够的缓存区大小.

client中的数据写入到用户空间的缓存区中 

 

最后介绍下,应用层打开后如何调用到evdev的fops

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

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;

	err = register_chrdev(INPUT_MAJOR, "input", &input_fops);//注册一系列设备号并与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;

	lock_kernel();
	/* No load-on-demand here? */
	handler = input_table[iminor(inode) >> 5];
	if (!handler || !(new_fops = fops_get(handler->fops))) {//获取evdev的fops
		err = -ENODEV;
		goto out;
	}

	/*
	 * That's _really_ odd. Usually NULL ->open means "nothing special",
	 * not "no device". Oh, well...
	 */
	if (!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);//将fops替换为evdev的fops。完成替换

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

eventX为什么会挂载在/dev/input/下呢? 不确定

static char *input_devnode(struct device *dev, mode_t *mode)
{
    return kasprintf(GFP_KERNEL, "input/%s", dev_name(dev)); // 不知道是否这边的缘故 ?
}

struct class input_class = {
    .name        = "input",
    .devnode    = input_devnode,
};
EXPORT_SYMBOL_GPL(input_class);
。。。。。。

	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 = device_add(&evdev->dev);

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值