探究platform_driver中“多态”思想
問題最初是下面的兩段代碼引出的:
static struct platform_driver sonypi_driver = {.driver = {.name = "sonypi",.owner = THIS_MODULE,},.probe = sonypi_probe,.remove = __devexit_p(sonypi_remove),.shutdown = sonypi_shutdown,.suspend = sonypi_suspend,.resume = sonypi_resume, }; static const struct dev_pm_ops aa5302_pm_ops = {.suspend = aa5302_suspend,.resume = aa5302_resume, }; #endifstatic struct platform_driver aa5302_driver = {.driver = {.name = "aa5302",.owner = THIS_MODULE, #ifdef CONFIG_PM.pm = &aa5302_pm_ops, #endif},.probe = aa5302_probe,.remove = __devexit_p(aa5302_remove),.shutdown = aa5302_shutdown, };注意到這兩個驅動都是platform driver,但是對于電源管理的定義方式卻不同:前者直接賦值platform_driver中的suspend/resume字段,后者間接賦值driver.pm字段。一直以來,我接觸到的原廠驅動,大部分是后面的定義方式,一直覺得其定義方式很羅嗦,這次要好好研究一下。
這里http://lists.kernelnewbies.org/pipermail/kernelnewbies/2013-October/009074.html有人問了一個類似的問題,可惜的是沒有回復!
我們先看看kernel/include/linux/platform_device.h中的定義:
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 (*resume)(struct platform_device *);struct device_driver driver;const struct platform_device_id *id_table; };再看看kernel/include/linux/device.h中的定義:
struct device_driver {const char *name;struct bus_type *bus;struct module *owner;const char *mod_name; /* used for built-in modules */bool suppress_bind_attrs; /* disables bind/unbind via sysfs */const struct of_device_id *of_match_table;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);const struct attribute_group **groups;const struct dev_pm_ops *pm;struct driver_private *p; };初看,它們都定義了probe, remove ... 等函數,但platform_driver里面包含了device_driver結構體,也就是說platform_driver中實際上有2套probe, remove ... 為什么會這樣的?我們再看看drivers/base/platform.c中關于platform_driver_register的定義:
int platform_driver_register(struct platform_driver *drv) {drv->driver.bus = &platform_bus_type;if (drv->probe)drv->driver.probe = platform_drv_probe;if (drv->remove)drv->driver.remove = platform_drv_remove;if (drv->shutdown)drv->driver.shutdown = platform_drv_shutdown;return driver_register(&drv->driver); }至此,你可能會更納悶,這不是畫蛇添足嗎?把platform_driver中的probe, remove字段又賦值給了driver中的相應字段,最終效果是,這2套probe, remove...實際上指向的分別是相同的回調函數!其實,如果細看platform_driver和device_driver中的probe, remove指針的定義,你會發現,他們還是有差別的:
int (*probe)(struct platform_device *);
int (*probe) (struct device *dev);
那就是,參數類型不同哦!前者是platform_device,后者是device!如果你學習過C++語言,頭腦中有“繼承”和“多態”的概念,我相信你已經知道為什么要這么做了:)
這么說吧,device和device_driver分別是驅動中的基類,而platform_device和platform_driver分別是PLATFORM BUS體系中對應的派生類;device_driver中的probe是基類的虛函數,platform_driver中的probe是派生類中的重載函數。如果我們要寫的是platform設備驅動,那么應該按照platform_driver中的回調函數聲明格式來定義自己的回調函數。
因此sonypi驅動的shutdown函數是這樣定義的:
?
static void sonypi_shutdown(struct platform_device *dev) {sonypi_disable(); }?
而不是:
static void sonypi_shutdown(struct device *dev) {sonypi_disable(); }如果你非要用這個“基類的函數”,那也是有辦法的,在聲明platform_driver的時候,用下面的方式,純屬猜測,沒有驗證哈。
static struct platform_driver sonypi_driver = {.driver = {.name = "sonypi",.owner = THIS_MODULE,.shutdown = sonypi_shutdown,},.probe = sonypi_probe,.remove = __devexit_p(sonypi_remove),.suspend = sonypi_suspend,.resume = sonypi_resume, };?
---------------------------------------------------------華麗的分割線-----------------------------------------------------------
?
下面再說說電源管理的問題,sonypi驅動使用了platform_driver.suspend/resume的方式來定義回調函數,而aa5302驅動使用platform_driver.driver.pm.suspend/resume的方式定義回調,這和上面說所講的“多態”似乎不完全契合。確實如此,這里涉及到另外一個legacy的問題,stackoverflow上的一篇解釋說的很明白:http://stackoverflow.com/questions/19462639/which-suspend-resume-pointer-is-the-right-one-to-use 我們看看platform_pm_suspend的實現,如果driver.pm字段不為空,則使用.pm提供的回調,否則使用platform_driver.suspend定義的lagacy方式的回調。
int platform_pm_suspend(struct device *dev) {struct device_driver *drv = dev->driver;int ret = 0;if (!drv)return 0;if (drv->pm) {if (drv->pm->suspend)ret = drv->pm->suspend(dev);} else {ret = platform_legacy_suspend(dev, PMSG_SUSPEND);}return ret; }device_driver.suspend/resume是舊的方式,他們在dev_pm_ops誕生以前就存在了,我們新的驅動中應該使用dev_pm_ops來定義回調函數。stackoverflow中提到的i2c驅動,跟這里的2個platform_driver還不完全一樣,i2c_driver僅僅提供一組通信接口,其并不提供設備的控制邏輯,在音頻codec中體現的非常明顯,音頻codec本質上是一個I2C芯片,在probe函數中注冊了一個更加“高級”的控制設備:codec,控制邏輯由codec來完成。因此其i2c_driver中并沒有提供電源管理功能:
static struct i2c_driver wm8900_i2c_driver = {.driver = {.name = "WM8900",.owner = THIS_MODULE,},.probe = wm8900_i2c_probe,.remove = __devexit_p(wm8900_i2c_remove),.shutdown = wm8900_i2c_shutdown,.id_table = wm8900_i2c_id, };而是轉移到了codec中:
static struct snd_soc_codec_driver soc_codec_dev_wm8900 = {.probe = wm8900_probe,.remove = wm8900_remove,.suspend = wm8900_suspend,.resume = wm8900_resume,.set_bias_level = wm8900_set_bias_level,.volatile_register = wm8900_volatile_register,.reg_cache_size = ARRAY_SIZE(wm8900_reg_defaults),.reg_word_size = sizeof(u16),.reg_cache_default = wm8900_reg_defaults, };?
轉載于:https://www.cnblogs.com/swnuwangyun/p/4233821.html
總結
以上是生活随笔為你收集整理的探究platform_driver中“多态”思想的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CentOS Tomcat6 修改默认端
- 下一篇: 【JS】检测插件