platform裝置驅動架構搭建分析

來源:互聯網
上載者:User

platform裝置驅動架構搭建分析

之前接觸到的字元裝置驅動是非常單純的Linux字元裝置驅動,他不具備工程中Linux驅動中的裝置與驅動分離思想和裝置驅動的分層思想,不具備“匯流排-裝置-驅動”模型的概念。接下來通過分析platform裝置驅動模型的搭建過程來看看Linux的裝置驅動模型究竟是怎樣的?

platform驅動模型搭建:

(1)platform核心層:為裝置層和驅動層提供註冊介面、為裝置層和驅動層的匹配提供標準

①搭建匯流排架構:
匯流排類結構體:
struct bus_type {const char*name;struct bus_attribute*bus_attrs;struct device_attribute*dev_attrs;struct driver_attribute*drv_attrs;int (*match)(struct device *dev, struct device_driver *drv); //#####int (*uevent)(struct device *dev, struct kobj_uevent_env *env);int (*probe)(struct device *dev);int (*remove)(struct device *dev);void (*shutdown)(struct device *dev);int (*suspend)(struct device *dev, pm_message_t state);int (*suspend_late)(struct device *dev, pm_message_t state);int (*resume_early)(struct device *dev);int (*resume)(struct device *dev);struct dev_pm_ops *pm;struct bus_type_private *p; //看到這個private就有點C++類中的限定域關鍵字了,這個類的私人成員};
匯流排類執行個體化:platform匯流排
struct bus_type platform_bus_type = {.name= "platform",.dev_attrs= platform_dev_attrs,.match= platform_match, //關鍵成員.uevent= platform_uevent,.pm= PLATFORM_PM_OPS_PTR,};
註冊platform匯流排過程:
platform_bus_init(){.....error =  bus_register(&platform_bus_type);//註冊platform匯流排的核心工作.....}bus_register(struct bus_type *bus){//建立bus的屬性檔案       retval = bus_create_file(bus, &bus_attr_uevent);    ......//在/sys/bus/bus->name目錄下建立devices目錄priv->devices_kset = kset_create_and_add("devices", NULL,&priv->subsys.kobj);....//在/sys/bus/bus->name目錄下建立drivers目錄priv->drivers_kset = kset_create_and_add("drivers", NULL,&priv->subsys.kobj);//初始化匯流排裝置\匯流排驅動鏈表klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);klist_init(&priv->klist_drivers, NULL, NULL);}
核心層的功績:初始化了klist_devices和klist_drivers兩個鏈表,沒有談到如何判斷裝置和驅動匹配?“.match=platform_match”有初始化,但是什麼時候被調用?
當一個驅動掛接到該匯流排的時候,該匯流排的match方法被調用。同樣的,當一個裝置掛接到該匯流排時,platform_match也會被調用。也就是說核心層只提供匹配的方法!不會幫他們去匹配,這人生大事要他們自己去完成!
這就好辦了,都是掛接到匯流排上的時候,往後分析時肯定會遇到,先暫時放著,先看看他的實現:
platform_match(struct device *dev, struct device_driver *drv){struct platform_device *pdev = to_platform_device(dev);struct platform_driver *pdrv = to_platform_driver(drv);/* match against the id table first */if (pdrv->id_table) //看看drv的id_table中是否有現成匹配的裝置記錄return platform_match_id(pdrv->id_table, pdev) != NULL;/* fall-back to driver name match */return (strcmp(pdev->name, drv->name) == 0); /* match成功,strcmp返回0,語句邏輯返回1 */}
②為裝置層提供註冊API、提供自動匹配介面函數
裝置基類:
struct device {struct device*parent;struct device_private*p;struct kobject kobj;const char*init_name; /* initial name of the device 這個就是傳統的bus_id,具體到每一個裝置之後當做預設值 */struct device_type*type;......struct bus_type*bus;/* type of bus device is on */struct device_driver *driver;/* which driver has allocated this device */void*driver_data;/* data private to the driver */void*platform_data;/* Platform specific data, device core doesn't touch it */......void(*release)(struct device *dev);};
衍生類別:platform裝置
struct platform_device {const char*name;intid;  // 硬體裝置的象徵/代表struct devicedev; // 由此繼承基類u32num_resources;struct resource* resource;//這個驅動使用的資源struct platform_device_id*id_entry;};
註冊platform裝置函數調用關係:
platform_device_register(struct platform_device *pdev)
platform_device_add(struct platform_device *pdev)
pdev->dev.bus = &platform_bus_type;
device_add(&pdev->dev);
bus_attach_device(struct device *dev)
device_attach(dev);
bus_for_each_drv(dev->bus, NULL, dev, __device_attach);
bus_for_each_drv()函數的實現:
bus_for_each_drv(struct bus_type *bus, struct device_driver *start,     void *data, int (*fn)(struct device_driver *, void *)){......while ((drv = next_driver(&i)) && !error)error = fn(drv, data);......}
分析:
首先關心他的最後一個形參(*fn),他在註冊platform_device時最終被重新導向到__device_attach()函數,回呼函數的使用在核心源碼裡邊屢見不鮮!因為它可以減少很多重複的代碼。
現在分析的焦點轉移到__device_attach函數:
__device_attach(struct device_driver *drv, void *data){struct device *dev = data;if (!driver_match_device(drv, dev))return 0;return driver_probe_device(drv, dev); //match成功就執行這個函數,他最終調用really_probe()函數}driver_match_device(struct device_driver *drv,struct device *dev){return drv->bus->match ? drv->bus->match(dev, drv) : 1; //看到這一句,上面留下的疑問就解決了:原來核心層留下的匹配判斷標準match介面就是在這裡被調用的!!!好爽!^_^}really_probe(struct device *dev, struct device_driver *drv){......if (dev->bus->probe) //如果bus_type結構裡邊的probe成員有定義就優先調用他的{ret = dev->bus->probe(dev);if (ret)goto probe_failed;} else if (drv->probe) //沒有就調用匹配到的drv結構裡邊的probe成員函數{ret = drv->probe(dev);if (ret)goto probe_failed;}driver_bound(dev);//bound是綁定的意思,即將match成功的裝置加入驅動的裝置鏈表......}
③為驅動層提供API、提供自動匹配介面函數
驅動基類:
struct device_driver {const char*name;struct bus_type*bus;struct module*owner;const char *mod_name;/* used for built-in modules */int  (*probe) (struct device *dev);int  (*remove) (struct device *dev);void (*shutdown) (struct device *dev);int  (*suspend) (struct device *dev, pm_message_t state);int  (*resume) (struct device *dev);struct attribute_group **groups;struct dev_pm_ops *pm;struct driver_private *p;};
驅動衍生類別:
struct platform_driver {int (*probe)(struct platform_device *); //通常這個函數要自己去實現int (*remove)(struct platform_device *);void (*shutdown)(struct platform_device *);int (*suspend)(struct platform_device *, pm_message_t state);int (*suspend_late)(struct platform_device *, pm_message_t state);int (*resume_early)(struct platform_device *);int (*resume)(struct platform_device *);struct device_driver driver;  //繼承基類struct platform_device_id *id_table;};
註冊platform_driver驅動結構體函數執行流程:
platform_driver_register(struct platform_driver *drv){/*下面進行一系列的判斷,如果派生的platform_driver中沒有對特有成員進行初始化,設定成預設的 */drv->driver.bus = &platform_bus_type;   //指向這個驅動所屬的bus類型:platformif (drv->probe)  //有重新導向drv->driver.probe = platform_drv_probe;if (drv->remove) //有重新導向drv->driver.remove = platform_drv_remove;......return driver_register(&drv->driver); 【進入分析】//註冊的關鍵材料是platform_driver->driver->bus:關鍵是為了註冊匯流排的類型platform_bus_type}driver_register(struct device_driver *drv){......struct device_driver *other;......other = driver_find(drv->name, drv->bus); //在該匯流排上尋找是否有該裝置驅動名對應的驅動if (other) { //如果裝置已經存在對應的驅動就:出錯,驅動已經存在put_driver(other);printk(KERN_ERR "Error: Driver '%s' is already registered, ""aborting...\n", drv->name);return -EEXIST;}bus_add_driver(drv);  /* 在匯流排上添加這個驅動,成功的話最終結果:在bus/platform/drivers目錄下面產生“name”對應的目錄 ,並且會產生 bind  module  uevent  unbind 四個檔案*/......}
繼續深入分析:
bus_add_driver(struct device_driver *drv)
driver_attach(drv); /* 試圖將驅動和裝置綁定起來 */
bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);//到這裡可以非常明顯的發現和裝置層做的事情非常相似,幾乎是對稱出現
/* 對匯流排上的每一個裝置都會拿來執行__driver_attach,他在這裡被用作回呼函數,看看是否匹配,這個函數和__device_attach函數做的事情基本一樣這裡就不再累述了*/

(2)裝置層:主要工作就是把核心層提供的API用起來
1.設定好platform_device結構體成員:主要是name、resource、num_resources、id、dev->release、

2.通過platform_device_register()把這個結構體鏈入核心層的klist_devices鏈表

(3)驅動層:同樣是把核心層提供的介面函數用起來
1.設定好platform_driver結構體成員:probe、remove、driver->name
2.通過platform_driver_register()函數把這個結構體鏈入核心層的klist_drivers鏈表
3.實現probe成員函數
4.通常最後才去完成probe函數用到的材料,一般是file_operation結構體成員,這樣應用程式層就可以通過這個介面來操作裝置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.