以下是一个简单的Linux PHY驱动示例,支持10Mbps、100Mbps和1000Mbps的链路速度。该驱动程序可以与常见的以太网PHY设备配合使用,并实现了不同速率的自动协商。
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/phy.h>
#include <linux/netdevice.h>
#include <linux/ethtool.h>
#include <linux/platform_device.h>
static struct phy_device *phydev;
static struct net_device *netdev;
// 自定义链路速度和双工模式回调函数
static int my_phy_config_init(struct phy_device *phydev)
{
// 配置PHY支持的链路速度和双工模式
phydev->supported &= (SUPPORTED_10baseT_Half |
SUPPORTED_10baseT_Full |
SUPPORTED_100baseT_Half |
SUPPORTED_100baseT_Full |
SUPPORTED_1000baseT_Full);
phydev->advertising = phydev->supported;
printk(KERN_INFO "PHY configured to support 10M/100M/1000M speeds\n");
return 0;
}
// 自定义自动协商配置函数
static int my_phy_config_aneg(struct phy_device *phydev)
{
int