Linux輸入子系統(Input Subsystem)
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(); /*配置設定一個裝置結構體*/
//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);
//分别用來設定裝置所産生的事件以及上報的按鍵值。Struct iput_dev中有兩個成員,一個是evbit.一個是keybit.分别用
//表示裝置所支援的動作和鍵值。
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()函數,驅動開發人員為了更深入的了解 input 子系統,應該對其代碼有一點的認識,該函數的代碼
如下:
struct input_dev *input_allocate_device(void)
struct input_dev *dev;
dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL); /*配置設定一個 input_dev 結構體,并初始化為 0*/
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_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); //初始化一個 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;
}
//如果dev->rep[REP_DELAY]和dev->rep[REP_PERIOD]沒有設值,則将其賦預設值。這主要是處理重複按鍵的.
if (!dev->getkeycode)
dev->getkeycode = input_default_getkeycode;
if (!dev->setkeycode)
dev->setkeycode = input_default_setkeycode;
//檢查 getkeycode()函數和 setkeycode()函數是否被定義,如果沒定義,則使用預設的處理函數,這兩個函數為
//input_default_getkeycode()和 input_default_setkeycode()。input_default_getkeycode()函數用來得到指定位置的鍵
//值。input_default_setkeycode()函數用來設定鍵值。具體啥用處,我也沒搞清楚?
snprintf(dev->dev.bus_id, sizeof(dev->dev.bus_id),
"input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);
//設定 input_dev 中的 device 的名字,名字以 input0、input1、input2、input3、input4等的形式出現在 sysfs
//檔案系統中.
error = device_add(&dev->dev);
if (error)
return error;
//使用 device_add()函數将 input_dev 包含的 device 結構注冊到 Linux 裝置模型中,并可以在 sysfs
//檔案系統中表現出來。
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);
list_add_tail(&dev->node, &input_dev_list);
//調用 list_add_tail()函數将 input_dev 加入 input_dev_list 連結清單中,input_dev_list 鍊
//表中包含了系統中所有的 input_dev 裝置。
list_for_each_entry(handler, &input_handler_list, node)
input_attach_handler(dev, handler);
//将input device 挂到input_dev_list連結清單上.然後,對每一個挂在input_handler_list的handler調用
//input_attach_handler().在這裡的情況有好比裝置模型中的device和driver的比對。所有的input device都挂在
//input_dev_list鍊上。所有的handler都挂在input_handler_list上。
input_wakeup_pro***s_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;
if (handler->blacklist && input_match_device(handler->blacklist, dev))
return -ENODEV;
//首先判斷 handler的 blacklist 是否被指派,如果被指派,則比對 blacklist 中的資料跟 dev->id 的資料是否比對。blacklist
//是一個 input_device_id*的類型,其指向 input_device_ids的一個表,這個表中存放了驅動程式應該忽略的裝置。即使在
//id_table 中找到支援的項,也應該忽略這種裝置。
id = input_match_device(handler->id_table, dev);
//調用 input_match_device()函數比對 handler->>id_table 和 dev->id 中的資料。如果不成功則傳回。
handle->id_table 也是一個 input_device_id 類型的指針,其表示驅動支援的裝置清單。
if (!id)
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;
//如果handler的blacklist被指派。要先比對blacklist中的資料跟dev->id的資料是否比對。比對成功過後再來比對
//handle->id和dev->id中的資料。如果比對成功,則調用handler->connect().
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++) {
//比對裝置廠商的資訊,裝置号的資訊。
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)
if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
if (id->product != dev->id.product)
if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
if (id->version != dev->id.version)
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 device和input handler的id成員在evbit, keybit,… swbit項相同才會匹//配成功。而且比對的順序是從evbit, keybit到swbit.隻要有一項不同,就會循環到id中的下一項進行比較.
//簡而言之,注冊input device的過程就是為input device設定預設值,并将其挂以input_dev_list.與挂載在//input_handler_list中的handler相比對。如果比對成功,就會調用handler的connect函數.
這一條線先講到這裡因為接下去就要講handler ,那就是事件層的東西了, 我們先把核心層的東西講完,
在前面的裝置驅動層中的中斷響應函數裡面,有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 表示按鍵按下。
在 input_report_key()函數中正在起作用的函數是 input_event()函數,該函數用來向輸入子系統報告輸入裝置産生
的事件,這個函數非常重要,它的代碼如下:
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);
//調用 input_handle_event()函數來繼續輸入子系統的相關子產品發送資料。
spin_unlock_irqrestore(&dev->event_lock, flags);
input_handle_event()函數向輸入子系統傳送事件資訊。第 1 個參數是輸入裝置 input_dev,第 2 個參數是事件的類
型,第 3 個參數是鍵碼,第 4 個參數是鍵值。
浏覽一下該函數的大部分代碼,主要由一個 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;
//定義了一個 disposition 變量,該變量表示使用什麼樣的方式處理事件
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;
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,表示事件需要 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)
INPUT_IGNORE_EVENT 表示忽略事件,不對其進行處理。
INPUT_PASS_ TO_HANDLERS 表示将事件交給 handler 處理。
INPUT_PASS_TO_DEVICE 表示将事件交給 input_dev 處理。
INPUT_PASS_TO_ALL 表示将事件交給 handler 和 input_dev 共同處理。
--------------------------------------------------------------------------------------------------------------------
case EV_SW:
if (is_event_supported(code, dev->swbit, SW_MAX) &&!!test_bit(code, dev->sw) != value)
__change_bit(code, dev->sw);
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;
break;
case EV_REL:
if (is_event_supported(code, dev->relbit, REL_MAX) && value)
case EV_MSC:
if (is_event_supported(code, dev->mscbit, MSC_MAX))
case EV_LED:
if (is_event_supported(code, dev->ledbit, LED_MAX) &&!!test_bit(code, dev->led) != value)
__change_bit(code, dev->led);
case EV_SND:
if (is_event_supported(code, dev->sndbit, SND_MAX))
if (!!test_bit(code, dev->snd) != !!value)
__change_bit(code, dev->snd);
case EV_REP:
if (code <= REP_MAX && value >= 0 && dev->rep[code] != value)
dev->rep[code] = value;
case EV_FF:
if (value >= 0)
case EV_PWR:
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);
//首先判斷 disposition 等于 INPUT_PASS_TO_DEVICE,然後判斷 dev->event 是否對其指定了一個處理函數,如果這些
//條件都滿足,則調用自定義的 dev->event()函數處理事件。
//有些事件是發送給裝置,而不是發送給 handler 處理的。event()函數用來向輸入子系統報告一個将要發送給裝置的事
//件,例如讓 LED 燈點亮事件、蜂鳴器鳴叫事件等。當事件報告給輸入子系統後,就要求裝置處理這個事件。
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);
//得到 dev->grab 的指針。grab 是強制為 input device 的 handler,這時要調用 handler的 event 函數。
if (handle)
handle->handler->event(handle, type, code, value);
else
list_for_each_entry_rcu(handle, &dev->h_list, d_node) //一般情況下走這裡
if (handle->open)
handle->handler->event(handle,type, code, value);
//如果該 handle 被打開,表示該裝置已經被一個使用者程序使用。就會調用與輸入裝置對應的 handler 的 event()函數。
//注意,隻有在 handle 被打開的情況下才會接收到事件,這就是說,隻有裝置被使用者程式使用時,才有必要向使用者空間導出
//資訊
//此處亦是用到了handle ,核心層就到此為止,前面也講過在device和handler connect() 時會調用
//input_register_handle,而這些都需要handler的注冊,是以接下來我們看看事件層
rcu_read_unlock();
四 事件層
input_handler 是輸入子系統的主要資料結構,一般将其稱為 handler 處理器,表示對輸入事件的具體處理。
input_handler 為輸入裝置的功能實作了一個接口,輸入事件最終傳遞到handler 處理器,handler 處理器根據一定的規則,
然後對事件進行處理,具體的規則将在下面詳細介紹。
輸入子系統由驅動層、輸入子系統核心層(Input Core)和事件處理層(Event Handler)3 部分組成。一個輸入事件,
如滑鼠移動,鍵盤按鍵按下等通過驅動層->系統核心層->事件處理層->使用者空間的順序到達使用者空間并傳給應用程式使
用。其中 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;
const struct input_device_id *id_table; //定義了一個 name, 表示 handler 的名字,顯示在/proc/bus/input/handlers 目錄
//中。
const struct input_device_id *blacklist; //指向一個 input_device_id 表,這個表包含 handler 應該忽略的裝置
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)
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_add_tail()函數,将 handler 加入全局的 input_handler_list 連結清單中,該連結清單包含了系統中所有的 input_handler
list_for_each_entry(dev, &input_dev_list, node)
input_attach_handler(dev, handler);
//主 要 調 用 了 input_attach_handler() 函 數 。 該 函 數 在 input_register_device()函數的第 35 行曾詳細的介紹過。//input_attach_handler()函數的作用是比對 input_dev_list 連結清單中的 input_dev 與 handler。如果成功會将 input_dev
//與 handler 聯系起來。也就是說在注冊handler和dev時都會去調用該函數。
input_wakeup_pro***s_readers();
out:
mutex_unlock(&input_mutex);
ok下面我們來看下handle的注冊,在前面evdev_handler結構體中,有一個.connect = evdev_connect, 在
connect裡面會注冊handle,在前面注冊dev,比對成功後調用。
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) {
printk(KERN_ERR "evdev: no more free evdev devices/n");
return -ENFILE;
evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
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);
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;
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);
goto err_free_evdev;
error = evdev_install_chrdev(evdev);
goto err_unregister_handle;
error = device_add(&evdev->dev);
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;
/*
* We take dev->mutex here to prevent race with
* input_release_device().
*/
error = mutex_lock_interruptible(&dev->mutex);
return error;
list_add_tail_rcu(&handle->d_node, &dev->h_list);
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);
if (handler->start)
handler->start(handle);
将handle挂到所對應input device的h_list連結清單上.還将handle挂到對應的handler的hlist連結清單上.如果handler定
義了start函數,将調用之.到這裡,我們已經看到了input device, handler和handle是怎麼關聯起來的了
接下來我們看看上報資訊是調用的 .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中讀.
最後我們看下handler的相關操作函數 .fops = &evdev_fops,
我們知道.對主裝置号為INPUT_MAJOR的裝置節點進行操作,會将操作集轉換成handler的操作集.在evdev中,這個
操作集就是evdev_fops.對應的open函數如下示:
static intevdev_open(struct inode *inode, struct file *file)
struct evdev *evdev;
int i = iminor(inode) - EVDEV_MINOR_BASE;
if (i >= EVDEV_MINORS)
error = mutex_lock_interruptible(&evdev_table_mutex);
evdev = evdev_table[i];
if (evdev)
get_device(&evdev->dev);
mutex_unlock(&evdev_table_mutex);
if (!evdev)
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);
goto err_free_client;
file->private_data = client;
err_free_client:
evdev_detach_client(evdev, client);
kfree(client);
err_put_evdev:
put_device(&evdev->dev);
evdev_open_device()函數用來打開相應的輸入裝置,使裝置準備好接收或者發送資料。evdev_open_device()函
數先獲得互斥鎖,然後檢查裝置是否存在,并判斷裝置是否已經被打開。如果沒有打開,則調用 input_open_device()
函數打開裝置.
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;
對于evdev裝置節點的read操作都會由evdev_read()完成.它的代碼如下:
static ssize_t evdev_read(struct file *file, char __user *buffer,
size_t count, loff_t *ppos)
struct evdev_client *client = file->private_data;
struct evdev *evdev = client->evdev;
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);
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();
首先,它判斷緩存區大小是否足夠.在讀取資料的情況下,可能目前緩存區内沒有資料可讀.在這裡先睡眠等待緩存
區中有資料.如果在睡眠的時候,.條件滿足.是不會進行睡眠狀态而直接傳回的.然後根據read()提夠的緩存區大小.将
client中的資料寫入到使用者空間的緩存區中.
五 使用者空間