天天看點

輸入子系統架構分析

   核心的輸入子系統是對分散的,多種不同類别的輸入裝置(如鍵盤,滑鼠,跟蹤球,操縱杆,觸摸屏,加速計和手寫闆)等字元裝置進行統一處理的一層抽象,就是在字元裝置驅動上抽象出的一層。輸入子系統包括兩類驅動程式:事件驅動程式和裝置驅動程式。事件驅動程式負責和應用程式的接口,而裝置驅動程式負責和底層輸入裝置的通信。滑鼠事件生成檔案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.     const char *name;     //名稱                              
  3.     const char *phys;  //裝置在系統中的實體路徑
  4.     const char *uniq;  //裝置唯一識别符
  5.     struct input_id id; //裝置ID,包含總線ID(PCI、USB)、廠商ID,與input_handler比對的時會用到  
  6.     unsigned long evbit[BITS_TO_LONGS(EV_CNT)];     //支援的所有事件類型  
  7.     unsigned long keybit[BITS_TO_LONGS(KEY_CNT)];   //支援的鍵盤事件  
  8.     unsigned long relbit[BITS_TO_LONGS(REL_CNT)];   //支援的滑鼠相對值事件  
  9.     unsigned long absbit[BITS_TO_LONGS(ABS_CNT)];   //支援的滑鼠絕對值事件  
  10.     unsigned long mscbit[BITS_TO_LONGS(MSC_CNT)];   //支援的其它事件類型  
  11.     unsigned long ledbit[BITS_TO_LONGS(LED_CNT)];   //支援的LED燈事件  
  12.     unsigned long sndbit[BITS_TO_LONGS(SND_CNT)];   //支援的聲效事件 
  13.     unsigned long ffbit[BITS_TO_LONGS(FF_CNT)];     //支援的力回報事件  
  14.     unsigned long swbit[BITS_TO_LONGS(SW_CNT)];     //支援的開關事件  
  15.     unsigned int keycodemax;  //keycode表的大小
  16.     unsigned int 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.     unsigned int repeat_key;//儲存上一個鍵值  
  22.     struct timer_list timer;  
  23.     int sync;  
  24.     int abs[ABS_MAX + 1];             //絕對坐标上報的目前值  
  25.     int rep[REP_MAX + 1];             //這個參數主要是處理重複按鍵,後面遇到再講  
  26.     unsigned long key[BITS_TO_LONGS(KEY_CNT)]; //按鍵有兩種狀态,按下和擡起,這個字段就是記錄這兩個狀态。  
  27.     unsigned long led[BITS_TO_LONGS(LED_CNT)];  
  28.     unsigned long snd[BITS_TO_LONGS(SND_CNT)];  
  29.     unsigned long 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.     unsigned int 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, const struct input_device_id *id);  
  53.     void (*disconnect)(struct input_handle *handle);  
  54.     void (*start)(struct input_handle *handle);  
  55.     const struct file_operations *fops;  
  56.     int minor;                               //次裝置号  
  57.     const char *name;  
  58.     const struct input_device_id *id_table;  
  59.     const struct 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.     const char *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. static int __init input_init(void)  
  2. {  
  3.     int err;  
  4.     input_init_abs_bypass();  
  5.     err = class_register(&input_class);                       
  6.     if (err) {  
  7.         printk(KERN_ERR "input: unable to register input_dev class/n");  
  8.         return err;  
  9.     }  
  10.     err = input_proc_init();  
  11.     if (err)  
  12.         goto fail1;  
  13.     err = register_chrdev(INPUT_MAJOR, "input", &input_fops);       
  14.     if (err) {  
  15.         printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);  
  16.         goto fail2;  
  17.     }  
  18.     return 0;  
  19.  fail2: input_proc_exit();  
  20.  fail1: class_unregister(&input_class);  
  21.     return err;  
  22. }  
  23. subsys_initcall(input_init);  

下面來看input子系統的file_operations,這裡隻有一個打開函數input_open_file,這個在事件傳遞部分講解。

  1. static const struct 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.     const char *path;  
  6.     int error;  
  7.     __set_bit(EV_SYN, dev->evbit);  
  8.     init_timer(&dev->timer);  
  9.     if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {  
  10.         dev->timer.data = (long) dev;  
  11.         dev->timer.function = input_repeat_key;  
  12.         dev->rep[REP_DELAY] = 250;  
  13.         dev->rep[REP_PERIOD] = 33;  
  14.     }  
  15.     if (!dev->getkeycode)  
  16.         dev->getkeycode = input_default_getkeycode;  
  17.     if (!dev->setkeycode)  
  18.         dev->setkeycode = input_default_setkeycode;  
  19.     dev_set_name(&dev->dev, "input%ld",  
  20.              (unsigned long) atomic_inc_return(&input_no) - 1);  
  21.     error = device_add(&dev->dev);  
  22.     if (error)  
  23.         return error;  
  24.     path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);  
  25.     printk(KERN_INFO "input: %s as %s/n",  
  26.         dev->name ? dev->name : "Unspecified device", path ? path : "N/A");  
  27.     kfree(path);  
  28.     error = mutex_lock_interruptible(&input_mutex);  
  29.     if (error) {  
  30.         device_del(&dev->dev);  
  31.         return error;  
  32.     }  
  33.     list_add_tail(&dev->node, &input_dev_list);  
  34.     list_for_each_entry(handler, &input_handler_list, node)  
  35.         input_attach_handler(dev, handler);  
  36.     input_wakeup_procfs_readers();  
  37.     mutex_unlock(&input_mutex);  
  38.     return 0;  
  39. }  

跟蹤程式,來看看input_attach_handler的實作:

  1. static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)  
  2. {  
  3.     const struct input_device_id *id;  
  4.     int error;  
  5.     if (handler->blacklist && input_match_device(handler->blacklist, dev))  
  6.         return -ENODEV;  
  7.     id = input_match_device(handler->id_table, dev);  
  8.     if (!id)  
  9.         return -ENODEV;  
  10.     error = handler->connect(handler, dev, id);  
  11.     if (error && error != -ENODEV)  
  12.         printk(KERN_ERR  
  13.             "input: failed to attach handler %s to device %s, "  
  14.             "error: %d/n",  
  15.             handler->name, kobject_name(&dev->dev.kobj), error);  
  16.     return error;  
  17. }  

下邊來看看這個比對函數:如果id->flags存在,并且相應的标志為被設定則進行比較。

  1. static const struct input_device_id *input_match_device(const struct 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. static int evdev_connect(struct input_handler *handler, struct input_dev *dev,  
  2.              const struct input_device_id *id)  
  3. {  
  4.     struct evdev *evdev;  
  5.     int minor;  
  6.     int error;  
  7.     for (minor = 0; minor < EVDEV_MINORS; minor++)  
  8.         if (!evdev_table[minor])  
  9.             break;  
  10.     if (minor == EVDEV_MINORS) {  
  11.         printk(KERN_ERR "evdev: no more free evdev devices/n");  
  12.         return -ENFILE;  
  13.     }  
  14.     evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);  
  15.     if (!evdev)  
  16.         return -ENOMEM;  
  17.     INIT_LIST_HEAD(&evdev->client_list);  
  18.     spin_lock_init(&evdev->client_lock);  
  19.     mutex_init(&evdev->mutex);  
  20.     init_waitqueue_head(&evdev->wait);  
  21.     snprintf(evdev->name, sizeof(evdev->name), "event%d", minor);  
  22.     evdev->exist = 1;  
  23.     evdev->minor = minor;  
  24.     evdev->handle.dev = input_get_device(dev);  
  25.     evdev->handle.name = evdev->name;  
  26.     evdev->handle.handler = handler;  
  27.     evdev->handle.private = evdev;  
  28.     dev_set_name(&evdev->dev, evdev->name);  
  29.     evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor);  
  30.     evdev->dev.class = &input_class;    
  31.     evdev->dev.parent = &dev->dev;  
  32.     evdev->dev.release = evdev_free;  
  33.     device_initialize(&evdev->dev);  
  34.     error = input_register_handle(&evdev->handle);  
  35.     if (error)  
  36.         goto err_free_evdev;  
  37.     error = evdev_install_chrdev(evdev);  
  38.     if (error)  
  39.         goto err_unregister_handle;  
  40.     error = device_add(&evdev->dev);  
  41.     if (error)  
  42.         goto err_cleanup_evdev;  
  43.     return 0;  
  44.     。。。。。。。。。。  
  45. }  

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;      
  6.     struct fasync_struct *fasync;                        //異步通知事件發生  
  7.     struct evdev *evdev;                                 //指向本evdev_client歸屬的evdev  
  8.     struct list_head node;                               //用于挂載到evdev的連結清單頭client_list上  
  9. };  
  1. static struct 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. static const struct input_device_id evdev_ids[] = {  
  2.     { .driver_info = 1 },     
  3.     { },              
  4. };  

看一下這張圖會對上邊的結構有清楚的認知了:

輸入子系統架構分析

這個是evdev_handler是fops,下面的講解中會用到其中的open,read函數。

  1. static const struct 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.          unsigned int type, unsigned int code, int value)  
  3. {  
  4.     unsigned long flags;  
  5.     if (is_event_supported(type, dev->evbit, EV_MAX)) {  
  6.         spin_lock_irqsave(&dev->event_lock, flags);  
  7.         add_input_randomness(type, code, value);  
  8.         input_handle_event(dev, type, code, value);  
  9.         spin_unlock_irqrestore(&dev->event_lock, flags);  
  10.     }  
  11. }  

跟蹤input_handle_event如下:

  1. static void input_handle_event(struct input_dev *dev,  
  2.                    unsigned int type, unsigned int 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. static void input_pass_event(struct input_dev *dev,  
  2.                  unsigned int type, unsigned int code, int value)  
  3. {  
  4.     struct input_handle *handle;  
  5.     rcu_read_lock();  
  6.     handle = rcu_dereference(dev->grab);  
  7.     if (handle)  
  8.         handle->handler->event(handle, type, code, value);  
  9.     else  
  10.         list_for_each_entry_rcu(handle, &dev->h_list, d_node)  
  11.             if (handle->open)  
  12.                 handle->handler->event(handle,  
  13.                             type, code, value);  
  14.     rcu_read_unlock();  
  15. }  

比如下邊的evdev_handler的evdev_event:

  1. static void evdev_event(struct input_handle *handle,  
  2.             unsigned int type, unsigned int 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_pass_event(client, &event);  
  15.     else  
  16.         list_for_each_entry_rcu(client, &evdev->client_list, node)  
  17.             evdev_pass_event(client, &event);  
  18.     rcu_read_unlock();  
  19.     wake_up_interruptible(&evdev->wait);  
  20. }  
  1. static void evdev_pass_event(struct evdev_client *client,  
  2.                  struct input_event *event)  
  3. {  
  4.     spin_lock(&client->buffer_lock);  
  5.     client->buffer[client->head++] = *event;  
  6.     client->head &= EVDEV_BUFFER_SIZE - 1;  
  7.     spin_unlock(&client->buffer_lock);  
  8.     kill_fasync(&client->fasync, SIGIO, POLL_IN);  
  9. }  

這裡總結一下事件的傳遞過程:首先在驅動層中,調用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. static int input_open_file(struct inode *inode, struct file *file)  
  2. {  
  3.     struct input_handler *handler;  
  4.     const struct file_operations *old_fops, *new_fops = NULL;  
  5.     int err;  
  6.     lock_kernel();  
  7.     handler = input_table[iminor(inode) >> 5];  
  8.     if (!handler || !(new_fops = fops_get(handler->fops))) {  
  9.         err = -ENODEV;  
  10.         goto out;  
  11.     }  
  12.     if (!new_fops->open) {  
  13.         fops_put(new_fops);  
  14.         err = -ENODEV;  
  15.         goto out;  
  16.     }  
  17.     old_fops = file->f_op;  
  18.     file->f_op = new_fops;  
  19.     err = new_fops->open(inode, file);  
  20.     if (err) {  
  21.         fops_put(file->f_op);  
  22.         file->f_op = fops_get(old_fops);  
  23.     }  
  24.     fops_put(old_fops);  
  25. out:  
  26.     unlock_kernel();  
  27.     return err;  
  28. }  

這裡還是假設handler是evdev_handler。

  1. static int evdev_open(struct inode *inode, struct file *file)  
  2. {  
  3.     struct evdev *evdev;  
  4.     struct evdev_client *client;  
  5.     int i = iminor(inode) - EVDEV_MINOR_BASE;  
  6.     int error;  
  7.     if (i >= EVDEV_MINORS)  
  8.         return -ENODEV;  
  9.     error = mutex_lock_interruptible(&evdev_table_mutex);  
  10.     if (error)  
  11.         return error;  
  12.     evdev = evdev_table[i];  
  13.     if (evdev)  
  14.         get_device(&evdev->dev);  
  15.     mutex_unlock(&evdev_table_mutex);  
  16.     if (!evdev)  
  17.         return -ENODEV;  
  18.     client = kzalloc(sizeof(struct evdev_client), GFP_KERNEL);  
  19.     if (!client) {  
  20.         error = -ENOMEM;  
  21.         goto err_put_evdev;  
  22.     }  
  23.     spin_lock_init(&client->buffer_lock);  
  24.     client->evdev = evdev;  
  25.     evdev_attach_client(evdev, client);  
  26.     error = evdev_open_device(evdev);  
  27.     if (error)  
  28.         goto err_free_client;  
  29.     file->private_data = client;  
  30.     return 0;  
  31.  err_free_client:  
  32.     evdev_detach_client(evdev, client);  
  33.     kfree(client);  
  34.  err_put_evdev:  
  35.     put_device(&evdev->dev);  
  36.     return error;  
  37. }  
  1. static int evdev_open_device(struct evdev *evdev)  
  2. {  
  3.     int retval;  
  4.     retval = mutex_lock_interruptible(&evdev->mutex);  
  5.     if (retval)  
  6.         return retval;  
  7.     if (!evdev->exist)  
  8.         retval = -ENODEV;  
  9.     else if (!evdev->open++) {  
  10.         retval = input_open_device(&evdev->handle);  
  11.         if (retval)  
  12.             evdev->open--;  
  13.     }  
  14.     mutex_unlock(&evdev->mutex);  
  15.     return retval;  
  16. }  
  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.         goto out;  
  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.             synchronize_rcu();  
  19.         }  
  20.     }  
  21.  out:  
  22.     mutex_unlock(&dev->mutex);  
  23.     return retval;  
  24. }  

下面是使用者程序讀取event的底層實作:

  1. static ssize_t evdev_read(struct file *file, char __user *buffer,  
  2.               size_t count, loff_t *ppos)  
  3. {  
  4.     struct evdev_client *client = file->private_data;  
  5.     struct evdev *evdev = client->evdev;  
  6.     struct input_event event;  
  7.     int retval;  
  8.     if (count < input_event_size())  
  9.         return -EINVAL;  
  10.     if (client->head == client->tail && evdev->exist &&  
  11.         (file->f_flags & O_NONBLOCK))  
  12.         return -EAGAIN;  
  13.     retval = wait_event_interruptible(evdev->wait,  
  14.         client->head != client->tail || !evdev->exist);  
  15.     if (retval)  
  16.         return retval;  
  17.     if (!evdev->exist)  
  18.         return -ENODEV;  
  19.     while (retval + input_event_size() <= count &&  
  20.            evdev_fetch_next_event(client, &event)) {  
  21.         if (input_event_to_user(buffer + retval, &event))  
  22.             return -EFAULT;  
  23.         retval += input_event_size();  
  24.     }  
  25.     return retval;  
  26. }  
  1. static int 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.     have_event = client->head != client->tail;  
  7.     if (have_event) {  
  8.         *event = client->buffer[client->tail++];  
  9.         client->tail &= EVDEV_BUFFER_SIZE - 1;  
  10.     }  
  11.     spin_unlock_irq(&client->buffer_lock);  
  12.     return have_event;  
  13. }  
  1. int input_event_to_user(char __user *buffer,  
  2.             const struct input_event *event)  
  3. {  
  4.     if (INPUT_COMPAT_TEST) {  
  5.         struct input_event_compat compat_event;  
  6.         compat_event.time.tv_sec = event->time.tv_sec;  
  7.         compat_event.time.tv_usec = event->time.tv_usec;  
  8.         compat_event.type = event->type;  
  9.         compat_event.code = event->code;  
  10.         compat_event.value = event->value;  
  11.         if (copy_to_user(buffer, &compat_event,  
  12.                  sizeof(struct input_event_compat)))  
  13.             return -EFAULT;  
  14.     } else {  
  15.         if (copy_to_user(buffer, event, sizeof(struct input_event)))  
  16.             return -EFAULT;  
  17.     }  
  18.     return 0;  
  19. }  

這裡總結一下:如果兩個程序打開同一個檔案,每個程序在打開時都會生成一個evdev_client,evdev_client被挂在evdev的client_list,在handle收到一個事件的時候,會把事件copy到挂在client_list上的所有evdev_client的buffer中。這樣所有打開同一個裝置的程序都會收到這個消息而喚醒。