mdio 的匹配与探测

关键结构体定义

struct phy_device {
    struct mdio_device mdio;

}


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 < num_ids; i++) {
            if (!(phydev->c45_ids.devices_in_package & (1 << i)))
                continue;

            if ((phydrv->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;
}

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乐分享-程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值