欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
Linux設備模型之input子系統詳解
  Linux設備模型之input子系統詳解 收藏
------------------------------------------
本文系本站原創(chuàng ),歡迎轉載!
轉載請注明出處:http://ericxiao.cublog.cn/
------------------------------------------
一:前言
在鍵盤(pán)驅動(dòng)代碼分析的筆記中,接觸到了input子系統.鍵盤(pán)驅動(dòng),鍵盤(pán)驅動(dòng)將檢測到的所有按鍵都上報給了input子系統。Input子系統是所有I/O設備驅動(dòng)的中間層,為上層提供了一個(gè)統一的界面。例如,在終端系統中,我們不需要去管有多少個(gè)鍵盤(pán),多少個(gè)鼠標。它只要從input子系統中去取對應的事件(按鍵,鼠標移位等)就可以了。今天就對input子系統做一個(gè)詳盡的分析.
下面的代碼是基于linux kernel 2.6.25.分析的代碼主要位于kernel2.6.25/drivers/input下面.
二:使用input子系統的例子
在內核自帶的文檔Documentation/input/input-programming.txt中。有一個(gè)使用input子系統的例子,并附帶相應的說(shuō)明。以此為例分析如下:
#include <linux/input.h>
#include <linux/module.h>
#include <linux/init.h>
 
#include <asm/irq.h>
#include <asm/io.h>
 
static void button_interrupt(int irq, void *dummy, struct pt_regs *fp)
{
        input_report_key(&button_dev, BTN_1, inb(BUTTON_PORT) & 1);
        input_sync(&button_dev);
}
 
static int __init button_init(void)
{
        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.evbit[0] = BIT(EV_KEY);
        button_dev.keybit[LONG(BTN_0)] = BIT(BTN_0);
 
        input_register_device(&button_dev);
}
 
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);
 
這個(gè)示例module代碼還是比較簡(jiǎn)單,在初始化函數里注冊了一個(gè)中斷處理例程。然后注冊了一個(gè)input device.在中斷處理程序里,將接收到的按鍵上報給input子系統。
文檔的作者在之后的分析里又對這個(gè)module作了優(yōu)化。主要是在注冊中斷處理的時(shí)序上。在修改過(guò)后的代碼里,為input device定義了open函數,在open的時(shí)候再去注冊中斷處理例程。具體的信息請自行參考這篇文檔。在資料缺乏的情況下,kernel自帶的文檔就是剖析kernel相關(guān)知識的最好資料.
文檔的作者還分析了幾個(gè)api函數。列舉如下:
 
1):set_bit(EV_KEY, button_dev.evbit);
   set_bit(BTN_0, button_dev.keybit);
分別用來(lái)設置設備所產(chǎn)生的事件以及上報的按鍵值。Struct iput_dev中有兩個(gè)成員,一個(gè)是evbit.一個(gè)是keybit.分別用表示設備所支持的動(dòng)作和按鍵類(lèi)型。
2): input_register_device(&button_dev);
用來(lái)注冊一個(gè)input device.
3): input_report_key()
用于給上層上報一個(gè)按鍵動(dòng)作
4): input_sync()
用來(lái)告訴上層,本次的事件已經(jīng)完成了.
5): NBITS(x) - returns the length of a bitfield array in longs for x bits
    LONG(x)  - returns the index in the array in longs for bit x
BIT(x)   - returns the index in a long for bit x     
這幾個(gè)宏在input子系統中經(jīng)常用到。上面的英文解釋已經(jīng)很清楚了。
 
三:input設備注冊分析.
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);
 
         /*
          * 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;
         }
在前面的分析中曾分析過(guò)。Input_device的evbit表示該設備所支持的事件。在這里將其EV_SYN置位,即所有設備都支持這個(gè)事件.如果dev->rep[REP_DELAY]和dev->rep[REP_PERIOD]沒(méi)有設值,則將其賦默認值。這主要是處理重復按鍵的.
 
         if (!dev->getkeycode)
                   dev->getkeycode = input_default_getkeycode;
 
         if (!dev->setkeycode)
                   dev->setkeycode = input_default_setkeycode;
 
         snprintf(dev->dev.bus_id, sizeof(dev->dev.bus_id),
                    "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);
 
         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;
         }
如果input device沒(méi)有定義getkeycode和setkeycode.則將其賦默認值。還記得在鍵盤(pán)驅動(dòng)中的分析嗎?這兩個(gè)操作函數就可以用來(lái)取鍵的掃描碼和設置鍵的掃描碼。然后調用device_add()將input_dev中封裝的device注冊到sysfs
 
         list_add_tail(&dev->node, &input_dev_list);
 
         list_for_each_entry(handler, &input_handler_list, node)
                   input_attach_handler(dev, handler);
 
         input_wakeup_procfs_readers();
 
         mutex_unlock(&input_mutex);
 
         return 0;
}
這里就是重點(diǎn)了。將input device 掛到input_dev_list鏈表上.然后,對每一個(gè)掛在input_handler_list的handler調用input_attach_handler().在這里的情況有好比設備模型中的device和driver的匹配。所有的input device都掛在input_dev_list鏈上。所有的handle都掛在input_handler_list上。
看一下這個(gè)匹配的詳細過(guò)程。匹配是在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);
         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;
}
如果handle的blacklist被賦值。要先匹配blacklist中的數據跟dev->id的數據是否匹配。匹配成功過(guò)后再來(lái)匹配handle->id和dev->id中的數據。如果匹配成功,則調用handler->connect().
來(lái)看一下具體的數據匹配過(guò)程,這是在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)
                                     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);
 
                   return id;
         }
 
         return NULL;
}
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;
 
由此看到。在id->flags中定義了要匹配的項。定義INPUT_DEVICE_ID_MATCH_BUS。則是要比較input device和input handler的總線(xiàn)類(lèi)型。INPUT_DEVICE_ID_MATCH_VENDOR,INPUT_DEVICE_ID_MATCH_PRODUCT,INPUT_DEVICE_ID_MATCH_VERSION分別要求設備廠(chǎng)商。設備號和設備版本.
如果id->flags定義的類(lèi)型匹配成功?;蛘呤莍d->flags沒(méi)有定義,就會(huì )進(jìn)入到MATCH_BIT的匹配項了.從MATCH_BIT宏的定義可以看出。只有當iput device和input handler的id成員在evbit, keybit,… swbit項相同才會(huì )匹配成功。而且匹配的順序是從evbit, keybit到swbit.只要有一項不同,就會(huì )循環(huán)到id中的下一項進(jìn)行比較.
簡(jiǎn)而言之,注冊input device的過(guò)程就是為input device設置默認值,并將其掛以input_dev_list.與掛載在input_handler_list中的handler相匹配。如果匹配成功,就會(huì )調用handler的connect函數.
 
四:handler注冊分析
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);
 
         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;
}
handler->minor表示對應input設備節點(diǎn)的次設備號.以handler->minor右移五位做為索引值插入到input_table[ ]中..之后再來(lái)分析input_talbe[ ]的作用.
然后將handler掛到input_handler_list中.然后將其與掛在input_dev_list中的input device匹配.這個(gè)過(guò)程和input device的注冊有相似的地方.都是注冊到各自的鏈表,.然后與另外一條鏈表的對象相匹配.
 
五:handle的注冊
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);
         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);
 
         return 0;
}
在這個(gè)函數里所做的處理其實(shí)很簡(jiǎn)單.將handle掛到所對應input device的h_list鏈表上.還將handle掛到對應的handler的hlist鏈表上.如果handler定義了start函數,將調用之.
到這里,我們已經(jīng)看到了input device, handler和handle是怎么關(guān)聯(lián)起來(lái)的了.以圖的方式總結如下:
 
 
六:event事件的處理
我們在開(kāi)篇的時(shí)候曾以linux kernel文檔中自帶的代碼作分析.提出了幾個(gè)事件上報的API.這些API其實(shí)都是input_event()的封裝.代碼如下:
void input_event(struct input_dev *dev,
                    unsigned int type, unsigned int code, int value)
{
         unsigned long flags;
 
         //判斷設備是否支持這類(lèi)事件
         if (is_event_supported(type, dev->evbit, EV_MAX)) {
 
                   spin_lock_irqsave(&dev->event_lock, flags);
                   //利用鍵盤(pán)輸入來(lái)調整隨機數產(chǎn)生器
                   add_input_randomness(type, code, value);
                   input_handle_event(dev, type, code, value);
                   spin_unlock_irqrestore(&dev->event_lock, flags);
         }
}
首先,先判斷設備產(chǎn)生的這個(gè)事件是否合法.如果合法,流程轉入到input_handle_event()中.
代碼如下:
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;
                   }
                   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;
         }
 
         if (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 device來(lái)完成的,就會(huì )將disposition設置成INPUT_PASS_TO_DEVICE.如果需要handler來(lái)完成的,就將dispostion設為INPUT_PASS_TO_DEVICE.如果需要兩者都參與,將disposition設置為INPUT_PASS_TO_ALL.
需要輸入設備參與的,回調設備的event函數.如果需要handler參與的.調用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)
                            if (handle->open)
                                     handle->handler->event(handle,
                                                                 type, code, value);
         rcu_read_unlock();
}
如果input device被強制指定了handler,則調用該handler的event函數.
結合handle注冊的分析.我們知道.會(huì )將handle掛到input device的h_list鏈表上.
如果沒(méi)有為input device強制指定handler.就會(huì )遍歷input device->h_list上的handle成員.如果該handle被打開(kāi),則調用與輸入設備對應的handler的event()函數.注意,只有在handle被打開(kāi)的情況下才會(huì )接收到事件.
另外,輸入設備的handler強制設置一般是用帶EVIOCGRAB標志的ioctl來(lái)完成的.如下是發(fā)圖的方示總結evnet的處理過(guò)程:
 
 
 
我們已經(jīng)分析了input device,handler和handle的注冊過(guò)程以及事件的上報和處理.下面以evdev為實(shí)例做分析.來(lái)貫穿理解一下整個(gè)過(guò)程.
 
七:evdev概述
 Evdev對應的設備節點(diǎn)一般位于/dev/input/event0 ~ /dev/input/event4.理論上可以對應32個(gè)設備節點(diǎn).分別代表被handler匹配的32個(gè)input device.
可以用cat /dev/input/event0.然后移動(dòng)鼠標或者鍵盤(pán)按鍵就會(huì )有數據輸出(兩者之間只能選一.因為一個(gè)設備文件只能關(guān)能一個(gè)輸入設備).還可以往這個(gè)文件里寫(xiě)數據,使其產(chǎn)生特定的事件.這個(gè)過(guò)程我們之后再詳細分析.
為了分析這一過(guò)程,必須從input子系統的初始化說(shuō)起.
 
八:input子系統的初始化
Input子系統的初始化函數為input_init().代碼如下:
static int __init input_init(void)
{
         int err;
 
         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);
         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;
}
在這個(gè)初始化函數里,先注冊了一個(gè)名為”input”的類(lèi).所有input device都屬于這個(gè)類(lèi).在sysfs中表現就是.所有input device所代表的目錄都位于/dev/class/input下面.
然后調用input_proc_init()在/proc下面建立相關(guān)的交互文件.
再后調用register_chrdev()注冊了主設備號為INPUT_MAJOR(13).次設備號為0~255的字符設備.它的操作指針為input_fops.
在這里,我們看到.所有主設備號13的字符設備的操作最終都會(huì )轉入到input_fops中.在前面分析的/dev/input/event0~/dev/input/event4的主設備號為13.操作也不例外的落在了input_fops中.
Input_fops定義如下:
static const struct file_operations input_fops = {
         .owner = THIS_MODULE,
         .open = input_open_file,
};
打開(kāi)文件所對應的操作函數為input_open_file.代碼如下示:
static int input_open_file(struct inode *inode, struct file *file)
{
         struct input_handler *handler = input_table[iminor(inode) >> 5];
         const struct file_operations *old_fops, *new_fops = NULL;
         int err;
 
         /* No load-on-demand here? */
         if (!handler || !(new_fops = fops_get(handler->fops)))
                   return -ENODEV;
 
iminor(inode)為打開(kāi)文件所對應的次設備號.input_table是一個(gè)struct input_handler全局數組.在這里.它先設備結點(diǎn)的次設備號右移5位做為索引值到input_table中取對應項.從這里我們也可以看到.一個(gè)handle代表1<<5個(gè)設備節點(diǎn)(因為在input_table中取值是以次備號右移5位為索引的.即低5位相同的次備號對應的是同一個(gè)索引).在這里,終于看到了input_talbe大顯身手的地方了.input_talbe[ ]中取值和input_talbe[ ]的賦值,這兩個(gè)過(guò)程是相對應的.
 
在input_table中找到對應的handler之后,就會(huì )檢驗這個(gè)handle是否存,是否帶有fops文件操作集.如果沒(méi)有.則返回一個(gè)設備不存在的錯誤.
         /*
          * That's _really_ odd. Usually NULL ->open means "nothing special",
          * not "no device". Oh, well...
          */
         if (!new_fops->open) {
                   fops_put(new_fops);
                   return -ENODEV;
         }
         old_fops = file->f_op;
         file->f_op = new_fops;
 
         err = new_fops->open(inode, file);
 
         if (err) {
                   fops_put(file->f_op);
                   file->f_op = fops_get(old_fops);
         }
         fops_put(old_fops);
         return err;
}
然后將handler中的fops替換掉當前的fops.如果新的fops中有open()函數,則調用它.
 
九:evdev的初始化
Evdev的模塊初始化函數為evdev_init().代碼如下:
static int __init evdev_init(void)
{
         return input_register_handler(&evdev_handler);
}
它調用了input_register_handler注冊了一個(gè)handler.
注意到,在這里evdev_handler中定義的minor為EVDEV_MINOR_BASE(64).也就是說(shuō)evdev_handler所表示的設備文件范圍為(13,64)à(13,64+32).
從之前的分析我們知道.匹配成功的關(guān)鍵在于handler中的blacklist和id_talbe. Evdev_handler的id_table定義如下:
static const struct input_device_id evdev_ids[] = {
         { .driver_info = 1 },     /* Matches all devices */
         { },                       /* Terminating zero entry */
};
它沒(méi)有定義flags.也沒(méi)有定義匹配屬性值.這個(gè)handler是匹配所有input device的.從前面的分析我們知道.匹配成功之后會(huì )調用handler->connect函數.
在Evdev_handler中,該成員函數如下所示:
 
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_MINORS定義為32.表示evdev_handler所表示的32個(gè)設備文件.evdev_talbe是一個(gè)struct evdev類(lèi)型的數組.struct evdev是模塊使用的封裝結構.在接下來(lái)的代碼中我們可以看到這個(gè)結構的使用.
這一段代碼的在evdev_talbe找到為空的那一項.minor就是數組中第一項為空的序號.
 
         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;
接下來(lái),分配了一個(gè)evdev結構,并對這個(gè)結構進(jìn)行初始化.在這里我們可以看到,這個(gè)結構封裝了一個(gè)handle結構,這結構與我們之前所討論的handler是不相同的.注意有一個(gè)字母的差別哦.我們可以把handle看成是handler和input device的信息集合體.在這個(gè)結構里集合了匹配成功的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的初始化.注意在這里,使它所屬的類(lèi)指向input_class.這樣在/sysfs中創(chuàng )建的設備目錄就會(huì )在/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;
}
注冊handle,如果是成功的,那么調用evdev_install_chrdev將evdev_table的minor項指向evdev. 然后將evdev->device注冊到sysfs.如果失敗,將進(jìn)行相關(guān)的錯誤處理.
萬(wàn)事俱備了,但是要接收事件,還得要等”東風(fēng)”.這個(gè)”東風(fēng)”就是要打開(kāi)相應的handle.這個(gè)打開(kāi)過(guò)程是在文件的open()中完成的.
 
十:evdev設備結點(diǎn)的open()操作
我們知道.對主設備號為INPUT_MAJOR的設備節點(diǎn)進(jìn)行操作,會(huì )將操作集轉換成handler的操作集.在evdev中,這個(gè)操作集就是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];
         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;
}
iminor(inode) - EVDEV_MINOR_BASE就得到了在evdev_table[ ]中的序號.然后將數組中對應的evdev取出.遞增devdev中device的引用計數.
分配并初始化一個(gè)client.并將它和evdev關(guān)聯(lián)起來(lái): client->evdev指向它所表示的evdev. 將client掛到evdev->client_list上. 將client賦為file的私有區.
對應handle的打開(kāi)是在此evdev_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是第一次打開(kāi),就會(huì )調用input_open_device()打開(kāi)evdev對應的handle.跟蹤一下這個(gè)函數:
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;
}
在這個(gè)函數中,我們看到.遞增handle的打開(kāi)計數.如果是第一次打開(kāi).則調用input device的open()函數.
 
十一:evdev的事件處理
經(jīng)過(guò)上面的分析.每當input device上報一個(gè)事件時(shí),會(huì )將其交給和它匹配的handler的event函數處理.在evdev中.這個(gè)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);
}
首先構造一個(gè)struct input_event結構.并設備它的type.code,value為處理事件的相關(guān)屬性.如果該設備被強制設置了handle.則調用如之對應的client.
我們在open的時(shí)候分析到.會(huì )初始化clinet并將其鏈入到evdev->client_list. 這樣,就可以通過(guò)evdev->client_list找到這個(gè)client了.
對于找到的第一個(gè)client都會(huì )調用evdev_pass_event( ).代碼如下:
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);
}
這里的操作很簡(jiǎn)單.就是將event保存到client->buffer中.而client->head就是當前的數據位置.注意這里是一個(gè)環(huán)形緩存區.寫(xiě)數據是從client->head寫(xiě).而讀數據則是從client->tail中讀.
 
十二:設備節點(diǎn)的read處理
對于evdev設備節點(diǎn)的read操作都會(huì )由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;
         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;
}
首先,它判斷緩存區大小是否足夠.在讀取數據的情況下,可能當前緩存區內沒(méi)有數據可讀.在這里先睡眠等待緩存區中有數據.如果在睡眠的時(shí)候,.條件滿(mǎn)足.是不會(huì )進(jìn)行睡眠狀態(tài)而直接返回的.
然后根據read()提夠的緩存區大小.將client中的數據寫(xiě)入到用戶(hù)空間的緩存區中.
十三:設備節點(diǎn)的寫(xiě)操作
同樣.對設備節點(diǎn)的寫(xiě)操作是由evdev_write()完成的.代碼如下:
 
static ssize_t evdev_write(struct file *file, const 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;
 
         retval = mutex_lock_interruptible(&evdev->mutex);
         if (retval)
                   return retval;
 
         if (!evdev->exist) {
                   retval = -ENODEV;
                   goto out;
         }
 
         while (retval < count) {
 
                   if (evdev_event_from_user(buffer + retval, &event)) {
                            retval = -EFAULT;
                            goto out;
                   }
 
                   input_inject_event(&evdev->handle,
                                        event.type, event.code, event.value);
                   retval += evdev_event_size();
         }
 
 out:
         mutex_unlock(&evdev->mutex);
         return retval;
}
首先取得操作設備文件所對應的evdev.
實(shí)際上,這里寫(xiě)入設備文件的是一個(gè)event結構的數組.我們在之前分析過(guò),這個(gè)結構里包含了事件的type.code和event.
將寫(xiě)入設備的event數組取出.然后對每一項調用event_inject_event().
這個(gè)函數的操作和input_event()差不多.就是將第一個(gè)參數handle轉換為輸入設備結構.然后這個(gè)設備再產(chǎn)生一個(gè)事件.
代碼如下:
void input_inject_event(struct input_handle *handle,
                            unsigned int type, unsigned int code, int value)
{
         struct input_dev *dev = handle->dev;
         struct input_handle *grab;
         unsigned long flags;
 
         if (is_event_supported(type, dev->evbit, EV_MAX)) {
                   spin_lock_irqsave(&dev->event_lock, flags);
 
                   rcu_read_lock();
                   grab = rcu_dereference(dev->grab);
                   if (!grab || grab == handle)
                            input_handle_event(dev, type, code, value);
                   rcu_read_unlock();
 
                   spin_unlock_irqrestore(&dev->event_lock, flags);
         }
}
我們在這里也可以跟input_event()對比一下,這里設備可以產(chǎn)生任意事件,而不需要和設備所支持的事件類(lèi)型相匹配.
由此可見(jiàn).對于寫(xiě)操作而言.就是讓與設備文件相關(guān)的輸入設備產(chǎn)生一個(gè)特定的事件.
將上述設備文件的操作過(guò)程以圖的方式表示如下:
 
 
十四:小結
在這一節點(diǎn),分析了整個(gè)input子系統的架構,各個(gè)環(huán)節的流程.最后還以evdev為例.將各個(gè)流程貫穿在一起.以加深對input子系統的理解.由此也可以看出.linux設備驅動(dòng)采用了分層的模式.從最下層的設備模型到設備,驅動(dòng),總線(xiàn)再到input子系統最后到input device.這樣的分層結構使得最上層的驅動(dòng)不必關(guān)心下層是怎么實(shí)現的.而下層驅動(dòng)又為多種型號同樣功能的驅動(dòng)提供了一個(gè)統一的接口.
 
發(fā)表于 @ 2010年03月04日 11:48:00 | 評論( 0 ) | 編輯| 舉報| 收藏
舊一篇:什么是嵌入式系統 | 新一篇:linux 觸摸屏驅動(dòng)分析
查看最新精華文章 請訪(fǎng)問(wèn)博客首頁(yè)相關(guān)文章
[轉貼] linux下如何模擬按鍵輸入和模擬鼠標s3c2440觸摸屏驅動(dòng)分析(LINUX2.6)(2)s3c2440觸摸屏驅動(dòng)分析(LINUX2.6)(3)linux驅動(dòng)雜談1輸入子系統--event層分析Linux設備模型之input子系統詳解(一)Linux設備模型之input子系統詳解(二)淺析linux鍵盤(pán)設備工作和注冊流程
本文來(lái)自CSDN博客,轉載請標明出處:http://blog.csdn.net/funy_liu/archive/2010/03/04/5345266.aspx
本文來(lái)自CSDN博客,轉載請標明出處:http://blog.csdn.net/funy_liu/archive/2010/03/04/5345266.aspx
本文來(lái)自CSDN博客,轉載請標明出處:http://blog.csdn.net/funy_liu/archive/2010/03/04/5345266.aspx
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
Linux input子系統分析
input子系統
linux input 子系統分析 三
Input Core和evdev基本知識 - Kernel3.0.8 .
linux輸入子系統
六、2.6內核輸入子系統分析-續 - 輸入子系統:鍵盤(pán)/按鍵/觸摸屏等 - LinuxSm...
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久