惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

W
WeLiveSecurity
T
Tenable Blog
Project Zero
Project Zero
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
S
Schneier on Security
Scott Helme
Scott Helme
S
Securelist
Know Your Adversary
Know Your Adversary
Vercel News
Vercel News
IT之家
IT之家
V
V2EX
F
Fortinet All Blogs
Simon Willison's Weblog
Simon Willison's Weblog
K
Kaspersky official blog
博客园_首页
T
Tailwind CSS Blog
The GitHub Blog
The GitHub Blog
Spread Privacy
Spread Privacy
Microsoft Security Blog
Microsoft Security Blog
Cisco Talos Blog
Cisco Talos Blog
The Register - Security
The Register - Security
有赞技术团队
有赞技术团队
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
The Hacker News
The Hacker News
L
LINUX DO - 热门话题
Hugging Face - Blog
Hugging Face - Blog
博客园 - 三生石上(FineUI控件)
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
CXSECURITY Database RSS Feed - CXSecurity.com
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Threat Research - Cisco Blogs
P
Proofpoint News Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Privacy & Cybersecurity Law Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
CERT Recently Published Vulnerability Notes
S
SegmentFault 最新的问题
AWS News Blog
AWS News Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
Apple Machine Learning Research
Apple Machine Learning Research
P
Proofpoint News Feed
The Cloudflare Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Vulnerabilities – Threatpost

博客园 - woaiusd

claude各种客户端的关系 pytorch对大模型推理,实现简单的对话 Agent原理与模拟 为红米Note 5 Pro编译Lineage OS 15.1的各种坑 C语言调用DIRECT3D的实例代码,通过lpVtbl字段进行 OBS插件学习入门:一个非常简单的、调节音量的filter 从工程角度看C++观察者模式中的接口是否需要提供默认的实现 Windows音频SDK的发展历程 闲话__stdcall, __cdecl, __fastcall出现的历史背景以及各自解决的问题 为什么需要超出48K的音频采样率,以及PCM到DSD的演进 wireshark使用笔记 managed_shared_memory.construct造成的性能损失 java.lang.NullPointerException Ubuntu中QT使用FFmpeg的奇怪问题 QT开启C++11的支持 尝鲜CodeBlocks ops中set_sysclk set_clkdiv set_pll详解 在内核中异步请求设备固件firmware的测试代码 探究MaxxBass音效 Alsa驱动snd_soc_read的底层实现 Device Drivers Should Not Do Power Management 探究platform_driver中的shutdown用途
探究platform_driver中“多态”思想
woaiusd · 2015-01-19 · via 博客园 - woaiusd

问题最初是下面的两段代码引出的:

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,
};
#endif

static 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,
};