mdio 的匹配与探测

2024-05-10 6213阅读

关键结构体定义

struct phy_device {

mdio 的匹配与探测 第1张
()

    struct mdio_device mdio;

}

mdio 的匹配与探测 第2张
()


struct phy_driver {

    struct mdio_driver_common mdiodrv;

}

struct mdio_driver_common {

    struct device_driver driver;

    int flags;

};

1.1 总线匹配函数

struct bus_type mdio_bus_type = {

        .name           = "mdio_bus",

        .match          = mdio_bus_match,

        .uevent         = mdio_uevent,

};

static int mdio_bus_match(struct device *dev, struct device_driver *drv)

{

        struct mdio_device *mdio = to_mdio_device(dev);

        if (of_driver_match_device(dev, drv)) //不会执行

                return 1;

        if (mdio->bus_match)

                return mdio->bus_match(dev, drv); //参考phy_device_create 调用

        return 0;

}

struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id,

                                                        bool is_c45, struct phy_c45_device_ids *c45_ids)

{

        struct phy_device *dev;

        struct mdio_device *mdiodev;

        mdiodev = &dev->mdio;

        mdiodev->dev.parent = &bus->dev;

        mdiodev->dev.bus = &mdio_bus_type;

        mdiodev->dev.type = &mdio_bus_phy_type;

        mdiodev->bus = bus;

        mdiodev->bus_match = phy_bus_match; 匹配函数最终调用phy_bus_match

        mdiodev->addr = addr;

        ......

}

1.2. 匹配函数phy_bus_match

static int phy_bus_match(struct device *dev, struct device_driver *drv)

{

    struct phy_device *phydev = to_phy_device(dev);

    struct phy_driver *phydrv = to_phy_driver(drv);

    const int num_ids = ARRAY_SIZE(phydev->c45_ids.device_ids);

    int i;

    if (!(phydrv->mdiodrv.flags & MDIO_DEVICE_IS_PHY))

        return 0;

    if (phydrv->match_phy_device)

        return phydrv->match_phy_device(phydev);

    if (phydev->is_c45) {

        for (i = 1; i

            if (!(phydev->c45_ids.devices_in_package & (1 phy_id & phydrv->phy_id_mask) ==

                (phydev->c45_ids.device_ids[i] &

                 phydrv->phy_id_mask))

                return 1;

        }

        return 0;

    } else {

        return (phydrv->phy_id & phydrv->phy_id_mask) ==

            (phydev->phy_id & phydrv->phy_id_mask);  //根据读取的phy_id 与phy_driver 匹配

    }

}

2.1 phy的probe 分析

       根据 phy_driver_register 函数,确定探测函数为phy_probe

int phy_driver_register(struct phy_driver *new_driver, struct module *owner)

{

    int retval;

    new_driver->mdiodrv.flags |= MDIO_DEVICE_IS_PHY;

    new_driver->mdiodrv.driver.name = new_driver->name;

    new_driver->mdiodrv.driver.bus = &mdio_bus_type;  
    new_driver->mdiodrv.driver.probe = phy_probe;

    //kernel 驱动模型,匹配后会调用probe 函数调用

    new_driver->mdiodrv.driver.remove = phy_remove;

    new_driver->mdiodrv.driver.owner = owner;

    return 0;

}

2.2 phy_probe 分析

        如果phy_driver 如果存在.probe ,会执行(一般不存在)

static int phy_probe(struct device *dev)

{

    struct phy_device *phydev = to_phy_device(dev);

    struct device_driver *drv = phydev->mdio.dev.driver;

    struct phy_driver *phydrv = to_phy_driver(drv);

    int err = 0;

    phydev->drv = phydrv;

    .......

    phydev->state = PHY_READY;

    if (phydev->drv->probe) {

        /* Deassert the reset signal */
        phy_device_reset(phydev, 0);

        err = phydev->drv->probe(phydev);

        if (err) {

            /* Assert the reset signal */

            phy_device_reset(phydev, 1);

        }

    }

    mutex_unlock(&phydev->lock);

    return err;

}


    免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

    目录[+]