#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/mii.h>
#include <linux/phy.h>
#define TAG "HELLO# "
static int my_phy_device_addr = 6;
static struct mii_bus *mii_bus = NULL;
static int my_mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
{
int regdata = 0xffff;
/* assume we have a phy here */
if(phy_addr == my_phy_device_addr)
{
switch(regnum)
{
case 0x2:
regdata = 0x1234;
break;
case 0x3:
regdata = 0x0056;
break;
default:
/* nothing to do */
break;
}
}
else
{
/* nothing to do */
}
return regdata;
}
static int my_mdiobus_write(struct mii_bus *bus, int phy_addr, int regnum, u16 value)
{
printk(TAG "%s called\n", __func__);
return 0;
}
static int my_mdiobus_reset(struct mii_bus *bus)
{
printk(TAG "%s called\n", __func__);
return 0;
}
static int my_mdiodev_init(void)
{
printk(TAG "%s called\n", __func__);
mii_bus = mdiobus_alloc();
mii_bus->read = my_mdiobus_read;
mii_bus->write = my_mdiobus_write;
mii_bus->reset = my_mdiobus_reset;
mii_bus->name = "my_mdiodev_mii";
snprintf(mii_bus->id, MII_BUS_ID_SIZE, "my_mdiobus");
mdiobus_register(mii_bus);
/* check if mii scan and found my_phy_device */
struct mdio_device *mdiodev = mii_bus->mdio_map[my_phy_device_addr];
if(!mdiodev)
{
/* nothing to do */
}
else
{
printk(TAG "found phy device, address %d\n", mdiodev->addr);
}
return 0;
}
static void my_mdiodev_exit(void)
{
printk(TAG "%s called\n", __func__);
mdiobus_unregister(mii_bus);
mdiobus_free(mii_bus);
}
module_init(my_mdiodev_init);
module_exit(my_mdiodev_exit);
MODULE_LICENSE("GPL");
效果
可以看到mii_bus注册成功,并识别到定义的phy设备,地址6