詳解Linux驅(qū)動(dòng)中,probe函數(shù)何時(shí)被調(diào)用
最近看到linux的設(shè)備驅(qū)動(dòng)模型,關(guān)于Kobject、Kset等還不是很清淅??吹搅藄truct device_driver這個(gè)結(jié)構(gòu)時(shí),想到一個(gè)問題:它的初始化函數(shù)到底在哪里調(diào)用呢?以前搞PCI驅(qū)動(dòng)時(shí)用pci驅(qū)動(dòng)注冊(cè)函數(shù)就可以調(diào)用它,搞s3c2410驅(qū)動(dòng)時(shí)只要在mach-smdk2410.c中的struct platform_device *smdk2410_devices {}中加入設(shè)備也會(huì)調(diào)用。但從來就沒有想過具體的驅(qū)動(dòng)注冊(cè)并調(diào)用probe的過程。
于是打開SourceInsight追蹤了一下:
從driver_register看起:
int driver_register(struct device_driver * drv) { klist_init(&drv->klist_devices, klist_devices_get, klist_devices_put); init_completion(&drv->unloaded); return bus_add_driver(drv); }
klist_init與init_completion沒去管它,可能是2.6的這個(gè)設(shè)備模型要做的一些工作。直覺告訴我要去bus_add_driver。
bus_add_driver中:
都是些Kobject 與 klist 、attr等。還是與設(shè)備模型有關(guān)的。但是其中有一句:
driver_attach(drv);
單聽名字就很像:
void driver_attach(struct device_driver * drv) { bus_for_each_dev(drv->bus, NULL, drv, __driver_attach); }
這個(gè)熟悉,遍歷總線上的設(shè)備并設(shè)用__driver_attach。
在__driver_attach中又主要是這樣:
driver_probe_device(drv, dev);
跑到driver_probe_device中去看看:
有一段很重要:
if (drv->bus->match && !drv->bus->match(dev, drv)) goto Done;
明顯,是調(diào)用的驅(qū)動(dòng)的總線上的match函數(shù)。如果返回1,則可以繼續(xù),否則就Done了。
繼承執(zhí)行的話:
if (drv->probe) { ret = drv->probe(dev); if (ret) { dev->driver = NULL; goto ProbeFailed; }
只要probe存在則調(diào)用之。至此就完成了probe的調(diào)用。
這個(gè)過程鏈的關(guān)鍵還是在drv->bus->match ,因?yàn)槠溆嗟牡胤匠鲥e(cuò)的話就是注冊(cè)失敗,而只要注冊(cè)不失敗且match返回1,那么就鐵定會(huì)調(diào)用驅(qū)程的probe了。你可以注冊(cè)一個(gè)總線類型和總線,并在match中總是返回 1, 會(huì)發(fā)現(xiàn),只要struct device_driver中的bus類型正確時(shí),probe函數(shù)總是被調(diào)用.
PCI設(shè)備有自己的總線模型,估計(jì)在它的match中就有一個(gè)判斷的條件。
static int pci_bus_match(struct device *dev, struct device_driver *drv) { struct pci_dev *pci_dev = to_pci_dev(dev); struct pci_driver *pci_drv = to_pci_driver(drv); const struct pci_device_id *found_id; found_id = pci_match_device(pci_drv, pci_dev); if (found_id) return 1; return 0; }
再往下跟蹤就知道主要是根據(jù)我們熟悉的id_table來的。
-------------------------------另解-----------------------------------------------------------------------------------------------
從driver_register看起,此處我的這里是:
int driver_register(struct device_driver * drv) { if ((drv->bus->probe && drv->probe) || (drv->bus->remove && drv->remove) || (drv->bus->shutdown && drv->shutdown)) { printk(KERN_WARNING "Driver '%s' needs updating - please use bus_type methods\n", drv->name); } klist_init(&drv->klist_devices, NULL, NULL); return bus_add_driver(drv); }
klist_init不相關(guān),不用管他,具體再去看bus_add_driver:
int bus_add_driver(struct device_driver *drv) { //1.先kobject_set_name(&drv->kobj, "%s", drv->name); //2.再kobject_register(&drv->kobj) //3.然后調(diào)用了:driver_attach(drv) }
int driver_attach(struct device_driver * drv) { return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach); }
真正起作用的是__driver_attach:
static int __driver_attach(struct device * dev, void * data) { ... if (!dev->driver) driver_probe_device(drv, dev); ... } int driver_probe_device(struct device_driver * drv, struct device * dev) { ... //1.先是判斷bus是否match: if (drv->bus->match && !drv->bus->match(dev, drv)) goto done; //2.再具體執(zhí)行probe: ret = really_probe(dev, drv); ... }
really_probe才是我們要找的函數(shù):
static int really_probe(struct device *dev, struct device_driver *drv) { ... //1.先是調(diào)用的驅(qū)動(dòng)所屬總線的probe函數(shù): if (dev->bus->probe) { ret = dev->bus->probe(dev); if (ret) goto probe_failed; } else if (drv->probe) { //2.再調(diào)用你的驅(qū)動(dòng)中的probe函數(shù): ret = drv->probe(dev); if (ret) goto probe_failed; } ... }
其中,drv->probe(dev),才是真正調(diào)用你的驅(qū)動(dòng)實(shí)現(xiàn)的具體的probe函數(shù)。
也就是對(duì)應(yīng)此文標(biāo)題所問的,probe函數(shù)此時(shí)被調(diào)用。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python下調(diào)用Linux的Shell命令的方法
- 利用Golang如何調(diào)用Linux命令詳解
- Linux makefile 和shell文件相互調(diào)用實(shí)例詳解
- 淺談在linux kernel中打印函數(shù)調(diào)用的堆棧的方法
- 三種方法實(shí)現(xiàn)Linux系統(tǒng)調(diào)用
- 舉例講解Linux系統(tǒng)下Python調(diào)用系統(tǒng)Shell的方法
- Python在Windows和在Linux下調(diào)用動(dòng)態(tài)鏈接庫的教程
- Linux下編寫Lua擴(kuò)展so文件和調(diào)用方法實(shí)例
- Linux下使用python調(diào)用top命令獲得CPU利用率
- Java調(diào)用linux shell腳本的方法
- 詳解linux系統(tǒng)調(diào)用原理
相關(guān)文章
CentOS平臺(tái)實(shí)現(xiàn)搭建rsync遠(yuǎn)程同步服務(wù)器的方法
這篇文章主要介紹了CentOS平臺(tái)實(shí)現(xiàn)搭建rsync遠(yuǎn)程同步服務(wù)器的方法,較為詳細(xì)的分析了CentOS搭建rsync遠(yuǎn)程同步服務(wù)器的具體步驟、相關(guān)命令與注意事項(xiàng),需要的朋友可以參考下2018-04-04Linux下9種優(yōu)秀的代碼比對(duì)工具推薦小結(jié)
這篇文章主要介紹了Linux下9種優(yōu)秀的代碼比對(duì)工具推薦小結(jié),不僅有命令行工具,還有 GUI 界面工具,讓你輕松進(jìn)行代碼比對(duì),感興趣的可以一起來了解一下2020-06-06centos6.5中用yum方式安裝php5.4與apache2.2的步驟
相信大家都知道lamp的安裝配置,最麻煩的是apache和php,網(wǎng)上關(guān)于apache和php的安裝配置有很多的相關(guān)文章,本文通過針對(duì)版本進(jìn)行詳細(xì)的介紹,文章主要介紹的是centos6.5中用yum方式安裝php5.4與apache2.2的步驟,感興趣的朋友們可以參考學(xué)習(xí)。2016-10-10ubuntu 16.04 LTS 安裝mongodb 3.2.8教程
本篇文章主要介紹了ubuntu 16.04 LTS 安裝mongodb 3.2.8教程,具有一定的參考價(jià)值,有需要的可以了解一下。2017-04-04菜鳥學(xué)Linux命令:ssh命令(遠(yuǎn)程登錄)
本篇文章主要介紹了菜鳥學(xué)Linux命令:ssh命令 遠(yuǎn)程登錄 ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06linux里daily_routine實(shí)例代碼詳解
在本篇文章里小編給大家整理的是關(guān)于linux里daily_routine實(shí)例代碼以及相關(guān)知識(shí)點(diǎn)內(nèi)容,有需要的朋友們參考下。2019-09-09windows中apache 301頁面跳轉(zhuǎn)實(shí)現(xiàn)方法
下面我們來總結(jié)windows中apache 301頁面跳轉(zhuǎn)實(shí)現(xiàn)方法,目前最主流的有二種一種是在寫.htaccess文件方法2013-01-01