精品轉載:linux input 子系統分析 三

 linux input 子系統分析 三 2013-01-09 00:38:19

分類:

原文地址:linux input 子系統分析 三 作者:xieyancheng

一.  輸入子系統核心分析。

    1.輸入子系統核心對應與/drivers/input/input.c文件,這個也是作爲一個模塊註冊到內核的。所以首先分析模塊初始化函數。
  1. static int __init input_init(void)  
  2. {  
  3.     int err;  
  4.   
  5.     input_init_abs_bypass();  
  6.         //這個暫時沒有發現是做什麼的  
  7.     err = class_register(&input_class);  
  8.         //向內核註冊一個類,用於linux設備模型。註冊後會在/sys/class下面出現input目錄  
  9.     if (err) {  
  10.         printk(KERN_ERR "input: unable to register input_dev class\n");  
  11.         return err;  
  12.     }  
  13.   
  14.     err = input_proc_init();  
  15.         //和proc文件系統有關,暫時不管  
  16.     if (err)  
  17.         goto fail1;  
  18.   
  19.   
  20.     err = register_chrdev(INPUT_MAJOR, "input", &input_fops);  
  21.         //註冊字符設備,接口是2.4內核的。以主設備號INPUT_MAJOR,次設備號0-255,註冊266個設備,說明input設備最大只能有255個  
  22.     if (err) {  
  23.         printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);  
  24.         goto fail2;  
  25.     }  
  26.   
  27.     return 0;  
  28.   
  29.  fail2: input_proc_exit();  
  30.  fail1: class_unregister(&input_class);  
  31.     return err;  
  32. }    
    這個函數主要是註冊了字符設備,這裏和雜項設備的原理是一樣,所以input設備也是一類字符設備,只不過操作方法交給了輸入子系統。從這裏可以看出無論linux設備驅動這塊有多複雜,他們都是由一些基本的組件構成的,都是ldd3所講的基本驅動程序模型。 
    2. 輸入子系統的核心其他部分都是提供的接口,向上連接事件處理層,向下連接驅動層。
    向下對驅動層的接口主要有:
    input_allocate_device    這個函數主要是分配一個input_dev接口,並初始化一些基本的成員,這就是我們不能簡單用kmalloc分配input_dev結構的原因,因爲缺少了一些初始化。
    input_unregister_device  註冊一個input設備
    input_event              這個函數很重要,是驅動層向input子系統核心報告事件的函數,在事件傳遞過程中再分析。
    input_allocate_device    分配並初始化一個input_dev結構
    向上對事件處理層接口主要有:
    input_register_handler   註冊一個事件處理器
    input_register_handle    註冊一個input_handle結構
二.  事件處理層分析(以evdev事件處理器爲例)
    1.事件處理層與用戶程序和輸入子系統核心打交道,是他們兩層的橋樑。一般內核有好幾個事件處理器,像evdev mousedev jotdev。evdev事件處理器可以處理所有的事件,觸摸屏驅動就是用的這個,所以下面分析這個事件處理器的實現。它也是作爲模塊註冊到內核中的,首先分析它的模塊初始化函數。
  1. static int __init evdev_init(void)  
  2. {  
  3.     return input_register_handler(&evdev_handler);  
  4. }  
   模塊初始化函數就調用一個註冊handler函數,將evdev_handler註冊到系統中。
    2.主要數據結構
    (1) evdev設備結構
  1. struct evdev {  
  2.     int exist;  
  3.     int open;           //打開標誌  
  4.     int minor;          //次設備號  
  5.     struct input_handle handle;  //關聯的input_handle  
  6.     wait_queue_head_t wait;      //等待隊列,當進程讀取設備,而沒有事件產生的時候,進程就會睡在其上面  
  7.     struct evdev_client *grab;   //強制綁定的evdev_client結構,這個結構後面再分析  
  8.     struct list_head client_list;  //evdev_client 鏈表,這說明一個evdev設備可以處理多個evdev_client,可以有多個進程訪問evdev設備  
  9.     spinlock_t client_lock; /* protects client_list */  
  10.     struct mutex mutex;  
  11.     struct device dev;       //device結構,說明這是一個設備結構  
  12. };  
    evdev結構體在配對成功的時候生成,由handler->connect生成,對應設備文件爲/class/input/event(n),如觸摸屏驅動的event0,這個設備是用戶空間要訪問的設備,可以理解它是一個虛擬設備,因爲沒有對應的硬件,但是通過handle->dev 就可以找到input_dev結構,而它對應着觸摸屏,設備文件爲/class/input/input0。這個設備結構生成之後保存在evdev_table中,
    索引值是minor
   (2) evdev用戶端結構
  1. struct evdev_client {  
  2.     struct input_event buffer[EVDEV_BUFFER_SIZE];    
  3.         //這個是一個input_event數據結構的數組,input_event代表一個事件,基本成員:類型(type),編碼(code),值(value)  
  4.     int head;              //針對buffer數組的索引  
  5.     int tail;              //針對buffer數組的索引,當head與tail相等的時候,說明沒有事件  
  6.     spinlock_t buffer_lock; /* protects access to buffer, head and tail */  
  7.     struct fasync_struct *fasync;  //異步通知函數  
  8.     struct evdev *evdev;           //evdev設備  
  9.     struct list_head node;         // evdev_client 鏈表項  
  10. };  
   這個結構在進程打開event0設備的時候調用evdev的open方法,在open中創建這個結構,並初始化。在關閉設備文件的時候釋放這個結構。
   3.主要函數
   (1)evdev設備打開函數
  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.   
  8.   
  9.     if (i >= EVDEV_MINORS)  
  10.         return -ENODEV;  
  11.   
  12.   
  13.     error = mutex_lock_interruptible(&evdev_table_mutex);  
  14.     if (error)  
  15.         return error;  
  16.     evdev = evdev_table[i];  
  17.         //得到evdev設備結構,每次調用evdev_connect配對成功後都會把分配的evdev結構以minor爲索引,保存在evdev_table數組中  
  18.     if (evdev)  
  19.         get_device(&evdev->dev);  //增加device引用計數  
  20.     mutex_unlock(&evdev_table_mutex);  
  21.   
  22.   
  23.     if (!evdev)  
  24.         return -ENODEV;  
  25.   
  26.   
  27.     client = kzalloc(sizeof(struct evdev_client), GFP_KERNEL);  //分配用戶端結構  
  28.     if (!client) {  
  29.         error = -ENOMEM;  
  30.         goto err_put_evdev;  
  31.     }  
  32.   
  33.   
  34.     spin_lock_init(&client->buffer_lock);  
  35.     client->evdev = evdev;    //使用戶端與evdev設備結構聯繫起來  
  36.     evdev_attach_client(evdev, client);  
  37.         //這個函數所做的就是把client連接到evdev的client鏈表中  
  38.     error = evdev_open_device(evdev);  
  39.         //這個函數打開設備,有很多層調用,後面詳細分析  
  40.     if (error)  
  41.         goto err_free_client;  
  42.   
  43.   
  44.     file->private_data = client;  
  45.     return 0;  
  46.   
  47.   
  48.  err_free_client:  
  49.     evdev_detach_client(evdev, client);  
  50.     kfree(client);  
  51.  err_put_evdev:  
  52.     put_device(&evdev->dev);  
  53.     return error;  
  54. }   
   (2)evdev設備打開函數evdev_open_device,由evdev_open調用。
  1. static int evdev_open_device(struct evdev *evdev)  
  2. {  
  3.     int retval;  
  4.   
  5.   
  6.     retval = mutex_lock_interruptible(&evdev->mutex);  
  7.     if (retval)  
  8.         return retval;  
  9.   
  10.   
  11.     if (!evdev->exist)  
  12.         retval = -ENODEV;  
  13.         //判斷設備結構是否存在,在evdev_connect中初始話此成員爲1  
  14.     else if (!evdev->open++) {  
  15.         retval = input_open_device(&evdev->handle);  
  16.         if (retval)  
  17.             evdev->open--;  
  18.     }  
  19.         //evdev->open分配結構的時候沒有初始化,默認爲0,也就是沒有打開,每次打開都會加1  
  20.     mutex_unlock(&evdev->mutex);  
  21.     return retval;  
  22. }  
    此函數在判斷結構存在與否後,主要調用了input_open_device,這個函數是子系統核心函數,定義在input.c中,下面分析這個函數:
  1. int input_open_device(struct input_handle *handle)  
  2. {  
  3.     struct input_dev *dev = handle->dev;  
  4.     int retval;  
  5.   
  6.   
  7.     retval = mutex_lock_interruptible(&dev->mutex);  
  8.     if (retval)  
  9.         return retval;  
  10.   
  11.   
  12.     if (dev->going_away) {  
  13.         retval = -ENODEV;  
  14.         goto out;  
  15.     }  
  16.   
  17.   
  18.     handle->open++;  
  19.         //將handle的打開計數加1,注意和evdev的open的區別  
  20.     if (!dev->users++ && dev->open)  
  21.         retval = dev->open(dev);  
  22.         //如果此input_dev沒有進程在引用,並且定義了open方法,就調用open方法  
  23.     if (retval) {    //retval = 1 說明沒有打開成功  
  24.         dev->users--;    
  25.         if (!--handle->open) {  //說明有其他的進程已經打開了這個handle  
  26.             /* 
  27.              * Make sure we are not delivering any more events 
  28.              * through this handle 
  29.              */  
  30.             synchronize_rcu();  
  31.         }  
  32.     }  
  33.   
  34.   
  35.  out:  
  36.     mutex_unlock(&dev->mutex);  
  37.     return retval;  
  38. }  
   (3)讀操作函數 evdev_read
  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;    //這個客戶端結構在打開的時候分配並保存在file->private_data中  
  5.     struct evdev *evdev = client->evdev;  
  6.     struct input_event event;  
  7.     int retval;  
  8.   
  9.   
  10.     if (count < input_event_size())  
  11.         return -EINVAL;  
  12.         //這條語句提示,用戶進程每次讀取設備的字節數,不要少於input_event結構的大小  
  13.     if (client->head == client->tail && evdev->exist &&  
  14.         (file->f_flags & O_NONBLOCK))  
  15.         return -EAGAIN;  
  16.         //head等於tail說明目前還沒有事件傳回來,如果設置了非阻塞操作,則會立刻返回  
  17.     retval = wait_event_interruptible(evdev->wait,  
  18.         client->head != client->tail || !evdev->exist);  
  19.         //沒有事件就會睡在evdev的等待隊列上了,等待條件是有事件到來或者設備不存在了(設備關閉的時候,清這個標誌)  
  20.     if (retval)  
  21.         return retval;  
  22.         //如果能執行上面這條語句說明有事件傳來或者,設備被關閉了,或者內核發過來終止信號  
  23.     if (!evdev->exist)  
  24.         return -ENODEV;  
  25.   
  26.   
  27.     while (retval + input_event_size() <= count &&  
  28.            evdev_fetch_next_event(client, &event)) {  
  29.         // evdev_fetch_next_event這個函數遍歷client裏面的input_event buffer數組  
  30.         if (input_event_to_user(buffer + retval, &event))  
  31.         //將事件複製到用戶空間  
  32.             return -EFAULT;  
  33.   
  34.   
  35.         retval += input_event_size();  
  36.     }  
  37.   
  38.     return retval;   //返回複製的數據字節數  
  39. }  
三. 事件傳遞過程(以s3c2410_ts爲例)
   1. 事件產生
    當按下觸摸屏時,進入觸摸屏按下中斷,開始ad轉換,ad轉換完成進入ad完成中斷,在這個終端中將事件發送出去,調用
    input_report_abs(dev, ABS_X, xp);
    input_report_abs(dev, ABS_Y, yp); 這兩個函數調用了 input_event(dev, EV_ABS, code, value)
    所有的事件報告函數都調用這個函數。
   2. 事件報告
   (1) input_event 函數分析,這個函數定義在input.c中
  1. void input_event(struct input_dev *dev,  
  2.          unsigned int type, unsigned int code, int value)  
  3. {  
  4.     unsigned long flags;  
  5.   
  6.   
  7.     if (is_event_supported(type, dev->evbit, EV_MAX)) {  
  8.         //判斷是否支持此種事件類型和事件類型中的編碼類型  
  9.         spin_lock_irqsave(&dev->event_lock, flags);  
  10.         add_input_randomness(type, code, value);  
  11.         //對系統隨機熵池有貢獻,因爲這個也是一個隨機過程  
  12.         input_handle_event(dev, type, code, value);  
  13.         //這個函數是事件處理的關鍵函數,下面詳細分析  
  14.         spin_unlock_irqrestore(&dev->event_lock, flags);  
  15.     }  
  16. }   
   (2) input_handle_event 函數分析,這個函數定義在input.c中
  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.   
  6.   
  7.     switch (type) {  
  8.         ......  
  9.     case EV_KEY:  
  10.         if (is_event_supported(code, dev->keybit, KEY_MAX) &&  
  11.             !!test_bit(code, dev->key) != value) {  
  12.   
  13.   
  14.             if (value != 2) {  
  15.                 __change_bit(code, dev->key);  
  16.                 if (value)  
  17.                     input_start_autorepeat(dev, code);  
  18.                 else  
  19.                     input_stop_autorepeat(dev);  
  20.             }  
  21.             disposition = INPUT_PASS_TO_HANDLERS;  
  22.         }  
  23.         break;  
  24.         ......  
  25.     if (disposition != INPUT_IGNORE_EVENT && type != EV_SYN)  
  26.         dev->sync = 0;  
  27.   
  28.   
  29.     if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)  
  30.         dev->event(dev, type, code, value);  
  31.   
  32.   
  33.     if (disposition & INPUT_PASS_TO_HANDLERS)  
  34.         input_pass_event(dev, type, code, value);  
  35. }  
   這個函數主要是根據事件類型的不同,做相應的處理。這裏之關心EV_KEY類型,其他函數和事件傳遞關係不大,只要關心,disposition這個是事件處理的方式,默認的是INPUT_IGNORE_EVENT,忽略這個事件,如果是INPUT_PASS_TO_HANDLERS則是傳遞給事件處理器,如果是INPUT_PASS_TO_DEVICE,則是傳遞給設備處理,觸摸屏驅動沒有定義這個。下面分析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.   
  6.   
  7.     rcu_read_lock();  
  8.   
  9.   
  10.     handle = rcu_dereference(dev->grab);  //如果是綁定的handle,則調用綁定的handler->event函數  
  11.     if (handle)  
  12.         handle->handler->event(handle, type, code, value);  
  13.     else  
  14.         //如果沒有綁定,則遍歷dev的h_list鏈表,尋找handle,如果handle已經打開,說明有進程讀取設備關聯的evdev。  
  15.         list_for_each_entry_rcu(handle, &dev->h_list, d_node)  
  16.             if (handle->open)  
  17.                 handle->handler->event(handle,  
  18.                             type, code, value);  
  19.         // 調用相關的事件處理器的event函數,進行事件的處理  
  20.     rcu_read_unlock();  
  21. }  
下面分析 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.   
  8.   
  9.     do_gettimeofday(&event.time);  
  10.     event.type = type;  
  11.     event.code = code;  
  12.     event.value = value;  
  13.         //將傳過來的事件,賦值給input_event結構  
  14.     rcu_read_lock();  
  15.   
  16.   
  17.     client = rcu_dereference(evdev->grab);  
  18.         //如果evdev綁定了client那麼,處理這個客戶端,觸摸屏驅動沒有綁定  
  19.     if (client)  
  20.         evdev_pass_event(client, &event);  
  21.     else  
  22.         //遍歷client鏈表,調用evdev_pass_event函數  
  23.         list_for_each_entry_rcu(client, &evdev->client_list, node)  
  24.             evdev_pass_event(client, &event);  
  25.   
  26.   
  27.     rcu_read_unlock();  
  28.   
  29.   
  30.     wake_up_interruptible(&evdev->wait); //喚醒等待的進程  
  31. }  
下面分析 evdev_pass_event 函數
  1. static void 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.     client->buffer[client->head++] = *event;   //將事件賦值給客戶端的input_event 數組  
  9.     client->head &= EVDEV_BUFFER_SIZE - 1;  
  10.     spin_unlock(&client->buffer_lock);  
  11.   
  12.   
  13.     kill_fasync(&client->fasync, SIGIO, POLL_IN);  
  14. }  
可以看出, evdev_pass_event函數最終將事件傳遞給了用戶端的client結構中的input_event數組中,只需將這個input_event數組複製給用戶空間,進程就能收到觸摸屏按下的信息了。具體處理由具體的應用程序來完成。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章