Linux输入子系统框架
輸入子系統
自己寫的驅動程序,自己可以調用,我們自己寫驅動的流程一般是,建立fops結構,使用register_chrdev在初始化函數中進行注冊,在應用中使用open函數打開該設備。這種驅動不標準只能在公司內部,別人知道驅動用法的情況下才能使用,當我們使用QT等標準程序時,這類標準程序不能打開像我們這樣的野驅動,我們應該讓我們的驅動程序融入“標準”中去,linux提供的輸入子系統
輸入子系統分析
在input.c \drivers\input.c核心層可以分析輸入子系統源碼,首先看他的初始化函數
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; }在初始化函數中我們可以看出這只是執行了一個普通的字符設備注冊過程,創建了一個input類,在該類下并沒有創建具體設備,其余沒有什么特別。在字符設備注冊時的操作函數集合中只有一個open函數,直觀上看一個open函數并不能執行read等操作。那我們分析一下這個open函數究竟做了些什么:
static const struct file_operations input_fops = {.owner = THIS_MODULE,.open = input_open_file, };那我們分析一下這個open函數做了些什么
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;/** 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; }(1)其中iminor(inode)函數調用了MINOR(inode->i_rdev);讀取子設備號,然后將子設備除以32,找到新掛載的input驅動的數組號,然后放在input_handler 驅動處理函數handler中,輸入子系統支持的設備大類就那么幾項,每項支持最多32個設備,除32意味著可將在這32區段的設備都能準確定位到自己對應大類上,這些設備都可以使用對應大類的公共fops ,(例如:輸入子系統的事件設備evdev,次設備號起始位置為64,之后32個設備都屬于evdev設備,input_table[iminor(inode) >> 5],次設備號64~95的設備都對應input_table[2],這個數組位置指向的是evdev的handler,這個區段內設備的fops,在這個函數中都會指向evdev設備共用的fops,這樣以來不用驅動編寫者自己編寫fops,直接使用該設備類型下別人寫出的fops即可)
(2)若handler有值,說明掛載有這個驅動,就將handler結構體里的成員file_operations * fops賦到新的file_operations *new_fops里面
(3)再將新的file_operations *new_fops賦到file-> file_operations *f_op里, 此時input子系統的file_operations就等于新掛載的input驅動的file_operations結構體,實現一個偷天換日的效果.
(4)然后調用新掛載的input驅動的*old_fops里面的成員.open函數,打開驅動open函數
這里需要說明:除以32的意義,linux輸入子系統,作為將輸入設備標準化處理的一種方式,使別人使用這類驅動時不必關心驅動細節即可使用,輸入設備分為好多類型,鍵盤類、鼠標類、觸摸屏類等等,linux將輸入子系統設備主設備定為13,次設備號以32為間隔細分了幾大類,例如事件設備evdev,屬于次設備號為64起始向后32個成員都屬于事件設備分段,64-95這些設備都屬于事件設備,這些設備次設備號除以32結果都是2,在調用驅動操作時他們都可以使用事件設備提供的fops,從而不用驅動編寫者自己編寫fops,大大提高了驅動易用性
分析:
struct input_handler *handler = input_table[iminor(inode) >> 5];將傳入的節點的次設備號除32放入input_table數組中,并將其賦值給handle,創建文件操作結構體new_fops,并將其用剛剛傳入節點的fops初始化,實現復制,這里input_table首次出現,那么它由誰構造,怎么初始化的呢:全局搜索一下
全局搜索input_register_handler被誰調用:搜索結果如下:
---- input_register_handler Matches (10 in 9 files) ---- evbug_init in evbug.c (F:\SourceInsightProj\JZ2440_2.6\linux-2.6.22.6\drivers\input) : return input_register_handler(&evbug_handler); evdev_init in evdev.c (F:\SourceInsightProj\JZ2440_2.6\linux-2.6.22.6\drivers\input) : return input_register_handler(&evdev_handler); input.c (F:\SourceInsightProj\JZ2440_2.6\linux-2.6.22.6\drivers\input) line 1182 : int input_register_handler(struct input_handler *handler) input.c (F:\SourceInsightProj\JZ2440_2.6\linux-2.6.22.6\drivers\input) line 1203 : EXPORT_SYMBOL(input_register_handler); input.h (F:\SourceInsightProj\JZ2440_2.6\linux-2.6.22.6\include\linux) line 1130 : int input_register_handler(struct input_handler *); joydev_init in joydev.c (F:\SourceInsightProj\JZ2440_2.6\linux-2.6.22.6\drivers\input) : return input_register_handler(&joydev_handler); kbd_init in keyboard.c (F:\SourceInsightProj\JZ2440_2.6\linux-2.6.22.6\drivers\char) : error = input_register_handler(&kbd_handler); mousedev_init in mousedev.c (F:\SourceInsightProj\JZ2440_2.6\linux-2.6.22.6\drivers\input) : error = input_register_handler(&mousedev_handler); rfkill_handler_init in rfkill-input.c (F:\SourceInsightProj\JZ2440_2.6\linux-2.6.22.6\net\rfkill) : return input_register_handler(&rfkill_handler); tsdev_init in tsdev.c (F:\SourceInsightProj\JZ2440_2.6\linux-2.6.22.6\drivers\input) : return input_register_handler(&tsdev_handler);我們可以看到在evdev.c joydev.c mousedev.c等等都通過input_register_handler向核心層注冊自己的結構,分析其中一個evdev.c,在其入口函數中:
static int __init evdev_init(void) {return input_register_handler(&evdev_handler); }看看evdev_handler這個結構體是怎樣定義的:
static struct input_handler evdev_handler = {.event = evdev_event,//.connect:連接函數,將設備input_dev和某個input_handler建立連接.connect = evdev_connect,.disconnect = evdev_disconnect,//.fops:文件操作結構體,其中evdev_fops函數就是自己的寫的操作函數,然后賦到.fops中.fops = &evdev_fops,//.minor:用來存放次設備號/*其中EVDEV_MINOR_BASE=64, 然后調用input_register_handler(&evdev_handler)后,由于EVDEV_MINOR_BASE/32=2,所以存到input_table[2]中,所以當open打開這個input設備,就會進入 input_open_file()函數,執行evdev_handler-> evdev_fops -> .open函數*/.minor = EVDEV_MINOR_BASE,.name = "evdev",/*.id_table : 表示能支持哪些輸入設備,比如某個驅動設備的input_dev->的id和某個input_handler的id_table相匹配,就會調用.connect連接函數*/.id_table = evdev_ids,}; static const struct file_operations evdev_fops = {.owner = THIS_MODULE,.read = evdev_read,.write = evdev_write,.poll = evdev_poll,.open = evdev_open,.release = evdev_release,.unlocked_ioctl = evdev_ioctl, #ifdef CONFIG_COMPAT.compat_ioctl = evdev_ioctl_compat, #endif.fasync = evdev_fasync,.flush = evdev_flush,.llseek = no_llseek, };input_register_device()函數,如何創建驅動設備的
int input_register_device(struct input_dev *dev) //*dev:要注冊的驅動設備 {... ...list_add_tail(&dev->node, &input_dev_list); //(1)放入鏈表中... ...list_for_each_entry(handler, &input_handler_list, node) //(2)input_attach_handler(dev, handler); ... ... }(1)將要注冊的input_dev驅動設備放在input_dev_list鏈表中
(2)其中input_handler_list在前面講過,就是存放每個input_handle驅動處理結構體,然后list_for_each_entry()函數會將每個input_handle從鏈表中取出,放到handler中,最后會調用input_attach_handler()函數,將每個input_handle的id_table進行判斷,若兩者支持便進行連接。
然后我們在回過頭來看注冊input_handler的input_register_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);//設備結構體中的id與handler中的id進行匹配input_wakeup_procfs_readers();out:mutex_unlock(&input_mutex);return retval; }所以,不管新添加input_dev還是input_handler,都會進入input_attach_handler()判斷兩者id是否有支持, 若兩者支持便進行連接
我們來看看input_attach_handler()如何實現匹配兩者id的:
static int input_attach_handler(struct input_dev *dev, struct input_handler *handler) { ... ... id = input_match_device(handler->id_table, dev); //匹配兩者if (!id) //若不匹配,return退出 return -ENODEV; error = handler->connect(handler, dev, id); //調用input_handler ->connect函數建立連接 ... ... }若兩者匹配成功,就會自動進入input_handler 的connect函數建立連接
我們還是以evdev.c(事件驅動) 的evdev_handler->connect函數來分析是怎樣建立連接的
evdev_handler的.connect函數是evdev_connect(),代碼如下:
static int evdev_connect(struct input_handler *handler, struct input_dev *dev, const struct input_device_id *id) { ... ... for (minor = 0; minor < EVDEV_MINORS && evdev_table[minor]; minor++); //查找驅動設備的子設備號if (minor == EVDEV_MINORS) { // EVDEV_MINORS=32,所以該事件下的驅動設備最多存32個,printk(KERN_ERR "evdev: no more free evdev devices\n");return -ENFILE; //沒找到驅動設備}... ...evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL); //分配一個input_handle全局結構體(沒有r)... ...evdev->handle.dev = dev; //指向參數input_dev驅動設備 evdev->handle.name = evdev->name; evdev->handle.handler = handler; //指向參數 input_handler驅動處理結構體 evdev->handle.private = evdev; sprintf(evdev->name, "event%d", minor); //(1)保存驅動設備名字, event%d ... ... devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor), //(2) 將主設備號和次設備號轉換成dev_t類型 cdev = class_device_create(&input_class, &dev->cdev, devt,dev->cdev.dev, evdev->name); // (3)在input類下創建驅動設備... ... error = input_register_handle(&evdev->handle); //(4)注冊這個input_handle結構體... ... }(1) 是在保存驅動設備名字,名為event%d, 因為沒有設置子設備號,默認從小到大排列,其中event0是表示這個input子系統,所以這個鍵盤驅動名字就是event1
(2)是在保存驅動設備的主次設備號,其中主設備號INPUT_MAJOR=13,因為EVDEV_MINOR_BASE=64,所以此設備號=64+驅動程序本事子設備號
(3)在之前在2小結里就分析了input_class類結構,會在/sys/class/input類下創建驅動設備event%d
(4)最終會進入input_register_handle()函數來注冊,代碼在下面
(1) 因為handle->dev指向input_dev驅動設備,所以就是將handle->d_node放入到input_dev驅動設備的h_list鏈表中,即input_dev驅動設備的h_list鏈表就指向handle->d_node
(2) 同樣, input_handler驅動處理結構體的h_list也指向了handle->h_node,兩者的.h_list都指向了同一個handle結構體,然后通過.h_list 來找到handle的成員.dev和handler,便能找到對方,便建立了連接
建立了連接后,又如何讀取evdev.c(事件驅動) 的evdev_handler->.fops->.read函數
事件驅動的.read函數是evdev_read()函數,我們來分析下
static ssize_t evdev_read(struct file *file, char __user * buffer, size_t count, loff_t *ppos) {... ... /*判斷應用層要讀取的數據是否正確*/ if (count < evdev_event_size()) return -EINVAL;/*在非阻塞操作情況下,若client->head == client->tail|| evdev->exist時(沒有數據),則return返回*/if (client->head == client->tail && evdev->exist && (file->f_flags & O_NONBLOCK)) return -EAGAIN;/*若client->head == client->tail|| evdev->exist時(沒有數據),等待中斷進入睡眠狀態 */retval = wait_event_interruptible(evdev->wait,client->head != client->tail || !evdev->exist);... ... //上傳數據 }若read函數進入了休眠狀態,又是誰來喚醒
我們搜索這個evdev->wait這個等待隊列變量,找到evdev_event函數里喚醒:
static void evdev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value) { ... ...wake_up_interruptible(&evdev->wait); //有事件觸發,便喚醒等待中斷 }其中evdev_event()是evdev.c(事件驅動)的evdev_handler->.event成員,當有事件發生了,比如對于按鍵驅動,當有按鍵按下時,就會進入.event函數中處理事件
分析下,是誰調用evdev_event()這個.event事件驅動函數
應該就是之前分析的input_dev那層調用的,我們來看看內核 gpio_keys_isr()函數代碼例子就知道了(driver/input/keyboard/gpio_key.c)
static irqreturn_t gpio_keys_isr(int irq, void *dev_id) {/*獲取按鍵值,賦到state里*/... ... /*上報事件*/ input_event(input, type, button->code, !!state); input_sync(input); //同步信號通知,表示事件發送完畢 }顯然就是通過input_event()來調用.event事件函數,我們來看看:
void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value) { struct input_handle *handle; ... .../* 通過input_dev ->h_list鏈表找到input_handle驅動處理結構體*/ list_for_each_entry(handle, &dev->h_list, d_node) if (handle->open) //如果input_handle之前open 過,那么這個就是我們的驅動處理結構體handle->handler->event(handle, type, code, value); //調用evdev_event()的.event事件函數 }若之前驅動input_dev和處理input_handler已經通過input_handler的.connect函數建立起了連接,那么就調用evdev_event()的.event事件函數
本節總結分析:
1.注冊輸入子系統,進入put_init():
1)創建主設備號為13的”input“字符設備
err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
2.open打開驅動,進入input_open_file():
1)更新設備的file_oprations
file->f_op=fops_get(handler->fops);
2)執行file_oprations->open函數
err = new_fops->open(inode, file);
3.注冊input_handler,進入input_register_handler():
1)添加到input_table[]處理數組中
input_table[handler->minor >> 5] = handler;
2)添加到input_handler_list鏈表中
list_add_tail(&handler->node, &input_handler_list);
3)判斷input_dev的id,是否有支持這個驅動的設備 list_for_each_entry(dev, &input_dev_list, node) //遍歷查找input_dev_list鏈表里所有input_dev
input_attach_handler(dev, handler); //判斷兩者id,若兩者支持便進行連接。
4.注冊input_dev,進入input_register_device():
1)放在input_dev_list鏈表中
list_add_tail(&dev->node, &input_dev_list);
2)判斷input_handler的id,是否有支持這個設備的驅動
list_for_each_entry(handler, &input_handler_list, node) //遍歷查找input_handler_list鏈表里所有input_handler
input_attach_handler(dev, handler); //判斷兩者id,若兩者支持便進行連接。
5.判斷input_handler和input_dev的id,進入input_attach_handler():
1)匹配兩者id,
input_match_device(handler->id_table, dev); //匹配input_handler和dev的id,不成功退出函數
2)匹配成功調用input_handler ->connect
handler->connect(handler, dev, id); //建立連接
6.建立input_handler和input_dev的連接,進入input_handler->connect():
1)創建全局結構體,通過input_handle結構體連接雙方
evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL); //創建兩者連接的input_handle全局結構體
list_add_tail(&handle->d_node, &handle->dev->h_list); //連接input_dev->h_list
list_add_tail(&handle->h_node, &handler->h_list); // 連接input_handle->h_list
7.有事件發生時,比如按鍵中斷,在中斷函數中需要進入input_event()上報事件:
1)找到驅動處理結構體,然后執行input_handler->event()
list_for_each_entry(handle, &dev->h_list, d_node) // 通過input_dev ->h_list鏈表找到input_handle驅動處理結構體
if (handle->open) //如果input_handle之前open 過,那么這個就是我們的驅動處理結構體(有可能一個驅動設備在不同情況下有不同的驅動處理方式)
handle->handler->event(handle, type, code, value); //調用evdev_event()的.event事件函數
參考鏈接:http://www.cnblogs.com/lifexy/p/7542989.html
http://www.cnblogs.com/lifexy/p/7553861.html
總結
以上是生活随笔為你收集整理的Linux输入子系统框架的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: RK3399 Debian 制作xxx.
- 下一篇: 官宣!CSDN“2019 优秀 AI、I