S3C2440驱动简析——DM9000网卡驱动(5)

     本文是DM9000网卡驱动的最后一篇,具体分析dm9000_ethtool_ops里所实现的工作。为了支持ethtool,其中的函数主要是用于查询和设置网卡参数(当然也有的驱动程序可能不支持ethtool)。

 

static const struct ethtool_ops dm9000_ethtool_ops = { .get_drvinfo = dm9000_get_drvinfo, .get_settings = dm9000_get_settings, .set_settings = dm9000_set_settings, .get_msglevel = dm9000_get_msglevel, .set_msglevel = dm9000_set_msglevel, .nway_reset = dm9000_nway_reset, .get_link = dm9000_get_link, .get_wol = dm9000_get_wol, .set_wol = dm9000_set_wol, .get_eeprom_len = dm9000_get_eeprom_len, .get_eeprom = dm9000_get_eeprom, .set_eeprom = dm9000_set_eeprom, .get_rx_csum = dm9000_get_rx_csum, .set_rx_csum = dm9000_set_rx_csum, .get_tx_csum = ethtool_op_get_tx_csum, .set_tx_csum = dm9000_set_tx_csum, };

 

下面我们来逐个分析每一项的操作

 

1.dm9000_get_drvinfo

static void dm9000_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info) { board_info_t *dm = to_dm9000_board(dev); strcpy(info->driver, CARDNAME); strcpy(info->version, DRV_VERSION); strcpy(info->bus_info, to_platform_device(dm->dev)->name); }

顾名思义,函数完成了把网络设备net_device通过成员name绑定到info。

 

2.dm9000_get_settings

static int dm9000_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) { board_info_t *dm = to_dm9000_board(dev); mii_ethtool_gset(&dm->mii, cmd); return 0; }

里面的主角mii_ethtool_gset代码如下

int mii_ethtool_gset(struct mii_if_info *mii, struct ethtool_cmd *ecmd) { struct net_device *dev = mii->dev; u16 bmcr, bmsr, ctrl1000 = 0, stat1000 = 0; u32 nego; ecmd->supported = (SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full | SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full | SUPPORTED_Autoneg | SUPPORTED_TP | SUPPORTED_MII); if (mii->supports_gmii) ecmd->supported |= SUPPORTED_1000baseT_Half | SUPPORTED_1000baseT_Full; /* only supports twisted-pair */ ecmd->port = PORT_MII; /* only supports internal transceiver */ ecmd->transceiver = XCVR_INTERNAL; /* this isn't fully supported at higher layers */ ecmd->phy_address = mii->phy_id; ecmd->mdio_support = MDIO_SUPPORTS_C22; ecmd->advertising = ADVERTISED_TP | ADVERTISED_MII; bmcr = mii->mdio_read(dev, mii->phy_id, MII_BMCR); bmsr = mii->mdio_read(dev, mii->phy_id, MII_BMSR); if (mii->supports_gmii) { ctrl1000 = mii->mdio_read(dev, mii->phy_id, MII_CTRL1000); stat1000 = mii->mdio_read(dev, mii->phy_id, MII_STAT1000); } if (bmcr & BMCR_ANENABLE) { ecmd->advertising |= ADVERTISED_Autoneg; ecmd->autoneg = AUTONEG_ENABLE; ecmd->advertising |= mii_get_an(mii, MII_ADVERTISE); if (ctrl1000 & ADVERTISE_1000HALF) ecmd->advertising |= ADVERTISED_1000baseT_Half; if (ctrl1000 & ADVERTISE_1000FULL) ecmd->advertising |= ADVERTISED_1000baseT_Full; if (bmsr & BMSR_ANEGCOMPLETE) { ecmd->lp_advertising = mii_get_an(mii, MII_LPA); if (stat1000 & LPA_1000HALF) ecmd->lp_advertising |= ADVERTISED_1000baseT_Half; if (stat1000 & LPA_1000FULL) ecmd->lp_advertising |= ADVERTISED_1000baseT_Full; } else { ecmd->lp_advertising = 0; } nego = ecmd->advertising & ecmd->lp_advertising; if (nego & (ADVERTISED_1000baseT_Full | ADVERTISED_1000baseT_Half)) { ecmd->speed = SPEED_1000; ecmd->duplex = !!(nego & ADVERTISED_1000baseT_Full); } else if (nego & (ADVERTISED_100baseT_Full | ADVERTISED_100baseT_Half)) { ecmd->speed = SPEED_100; ecmd->duplex = !!(nego & ADVERTISED_100baseT_Full); } else { ecmd->speed = SPEED_10; ecmd->duplex = !!(nego & ADVERTISED_10baseT_Full); } } else { ecmd->autoneg = AUTONEG_DISABLE; ecmd->speed = ((bmcr & BMCR_SPEED1000 && (bmcr & BMCR_SPEED100) == 0) ? SPEED_1000 : (bmcr & BMCR_SPEED100) ? SPEED_100 : SPEED_10); ecmd->duplex = (bmcr & BMCR_FULLDPLX) ? DUPLEX_FULL : DUPLEX_HALF; } mii->full_duplex = ecmd->duplex; /* ignore maxtxpkt, maxrxpkt for now */ return 0; }

函数实现从mii接口取得的状态信息赋值到传入的结构体ecmd。

 

3.dm9000_set_settings

static int dm9000_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) { board_info_t *dm = to_dm9000_board(dev); return mii_ethtool_sset(&dm->mii, cmd); }

分析主角mii_ethtool_sset

int mii_ethtool_sset(struct mii_if_info *mii, struct ethtool_cmd *ecmd) { struct net_device *dev = mii->dev; if (ecmd->speed != SPEED_10 && ecmd->speed != SPEED_100 && ecmd->speed != SPEED_1000) return -EINVAL; if (ecmd->duplex != DUPLEX_HALF && ecmd->duplex != DUPLEX_FULL) return -EINVAL; if (ecmd->port != PORT_MII) return -EINVAL; if (ecmd->transceiver != XCVR_INTERNAL) return -EINVAL; if (ecmd->phy_address != mii->phy_id) return -EINVAL; if (ecmd->autoneg != AUTONEG_DISABLE && ecmd->autoneg != AUTONEG_ENABLE) return -EINVAL; if ((ecmd->speed == SPEED_1000) && (!mii->supports_gmii)) return -EINVAL; /* ignore supported, maxtxpkt, maxrxpkt */ if (ecmd->autoneg == AUTONEG_ENABLE) { u32 bmcr, advert, tmp; u32 advert2 = 0, tmp2 = 0; if ((ecmd->advertising & (ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full | ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full | ADVERTISED_1000baseT_Half | ADVERTISED_1000baseT_Full)) == 0) return -EINVAL; /* advertise only what has been requested */ advert = mii->mdio_read(dev, mii->phy_id, MII_ADVERTISE); tmp = advert & ~(ADVERTISE_ALL | ADVERTISE_100BASE4); if (mii->supports_gmii) { advert2 = mii->mdio_read(dev, mii->phy_id, MII_CTRL1000); tmp2 = advert2 & ~(ADVERTISE_1000HALF | ADVERTISE_1000FULL); } if (ecmd->advertising & ADVERTISED_10baseT_Half) tmp |= ADVERTISE_10HALF; if (ecmd->advertising & ADVERTISED_10baseT_Full) tmp |= ADVERTISE_10FULL; if (ecmd->advertising & ADVERTISED_100baseT_Half) tmp |= ADVERTISE_100HALF; if (ecmd->advertising & ADVERTISED_100baseT_Full) tmp |= ADVERTISE_100FULL; if (mii->supports_gmii) { if (ecmd->advertising & ADVERTISED_1000baseT_Half) tmp2 |= ADVERTISE_1000HALF; if (ecmd->advertising & ADVERTISED_1000baseT_Full) tmp2 |= ADVERTISE_1000FULL; } if (advert != tmp) { mii->mdio_write(dev, mii->phy_id, MII_ADVERTISE, tmp); mii->advertising = tmp; } if ((mii->supports_gmii) && (advert2 != tmp2)) mii->mdio_write(dev, mii->phy_id, MII_CTRL1000, tmp2); /* turn on autonegotiation, and force a renegotiate */ bmcr = mii->mdio_read(dev, mii->phy_id, MII_BMCR); bmcr |= (BMCR_ANENABLE | BMCR_ANRESTART); mii->mdio_write(dev, mii->phy_id, MII_BMCR, bmcr); mii->force_media = 0; } else { u32 bmcr, tmp; /* turn off auto negotiation, set speed and duplexity */ bmcr = mii->mdio_read(dev, mii->phy_id, MII_BMCR); tmp = bmcr & ~(BMCR_ANENABLE | BMCR_SPEED100 | BMCR_SPEED1000 | BMCR_FULLDPLX); if (ecmd->speed == SPEED_1000) tmp |= BMCR_SPEED1000; else if (ecmd->speed == SPEED_100) tmp |= BMCR_SPEED100; if (ecmd->duplex == DUPLEX_FULL) { tmp |= BMCR_FULLDPLX; mii->full_duplex = 1; } else mii->full_duplex = 0; if (bmcr != tmp) mii->mdio_write(dev, mii->phy_id, MII_BMCR, tmp); mii->force_media = 1; } return 0; }

其作用与mii_ethtool_gset恰好相反,取传入的结构体ecmd的信息写入mii接口里。

 

4.dm9000_get_msglevel

static u32 dm9000_get_msglevel(struct net_device *dev) { board_info_t *dm = to_dm9000_board(dev); return dm->msg_enable; }

返回结构体board_info_t 里成员msg_enable的状态。

 

5.dm9000_set_msglevel

static void dm9000_set_msglevel(struct net_device *dev, u32 value) { board_info_t *dm = to_dm9000_board(dev); dm->msg_enable = value; }

设置msg_enable的状态

 

6.dm9000_nway_reset

static int dm9000_nway_reset(struct net_device *dev) { board_info_t *dm = to_dm9000_board(dev); return mii_nway_restart(&dm->mii); }

mii_nway_restrart函数代码如下

int mii_nway_restart (struct mii_if_info *mii) { int bmcr; int r = -EINVAL; /* if autoneg is off, it's an error */ bmcr = mii->mdio_read(mii->dev, mii->phy_id, MII_BMCR); if (bmcr & BMCR_ANENABLE) { bmcr |= BMCR_ANRESTART; mii->mdio_write(mii->dev, mii->phy_id, MII_BMCR, bmcr); r = 0; } return r; }

重新启动nway功能,其中nway指通信速率通过双方协商,协商的结果是两个设备中能同时支持的最大速度和最好的双工模式,这个技术被称为Auto Negotiation或者NWAY。

 

7.dm9000_get_link

static u32 dm9000_get_link(struct net_device *dev) { board_info_t *dm = to_dm9000_board(dev); u32 ret; if (dm->flags & DM9000_PLATF_EXT_PHY) ret = mii_link_ok(&dm->mii); //返回1,链接状态OK。返回0,失败。 else ret = dm9000_read_locked(dm, DM9000_NSR) & NSR_LINKST ? 1 : 0; return ret; }

函数作用显而易见,返回链接状态。

 

8.dm9000_get_wol

static void dm9000_get_wol(struct net_device *dev, struct ethtool_wolinfo *w) { board_info_t *dm = to_dm9000_board(dev); memset(w, 0, sizeof(struct ethtool_wolinfo)); /* note, we could probably support wake-phy too */ w->supported = dm->wake_supported ? WAKE_MAGIC : 0; w->wolopts = dm->wake_state; }

ethtool_wolinfo结构体如下

struct ethtool_wolinfo { __u32 cmd; __u32 supported; __u32 wolopts; __u8 sopass[SOPASS_MAX]; /* SecureOn(tm) password */ };

把网络设备的成员wake_supported和wake_state传送到ethtool_wolinfo结构体的supported和wolopts里。

 

9.dm9000_set_wol

static int dm9000_set_wol(struct net_device *dev, struct ethtool_wolinfo *w) { board_info_t *dm = to_dm9000_board(dev); unsigned long flags; u32 opts = w->wolopts; u32 wcr = 0; if (!dm->wake_supported) return -EOPNOTSUPP; if (opts & ~WAKE_MAGIC) return -EINVAL; if (opts & WAKE_MAGIC) wcr |= WCR_MAGICEN; mutex_lock(&dm->addr_lock); spin_lock_irqsave(&dm->lock, flags); iow(dm, DM9000_WCR, wcr); spin_unlock_irqrestore(&dm->lock, flags); mutex_unlock(&dm->addr_lock); if (dm->wake_state != opts) { /* change in wol state, update IRQ state */ if (!dm->wake_state) set_irq_wake(dm->irq_wake, 1); else if (dm->wake_state & !opts) set_irq_wake(dm->irq_wake, 0); } dm->wake_state = opts; return 0; }

dm9000_set_wol函数完成更新中断状态,并读回wake_state(对应dm9000_get_wol)

 

10.dm9000_get_eeprom_len

static int dm9000_get_eeprom_len(struct net_device *dev) { return 128; }

eeprom长度默认128bytes

 

11.dm9000_get_eeprom

static int dm9000_get_eeprom(struct net_device *dev, struct ethtool_eeprom *ee, u8 *data) { board_info_t *dm = to_dm9000_board(dev); int offset = ee->offset; int len = ee->len; int i; /* EEPROM access is aligned to two bytes */ if ((len & 1) != 0 || (offset & 1) != 0) return -EINVAL; if (dm->flags & DM9000_PLATF_NO_EEPROM) //检查有没有EEPROM的信息 return -ENOENT; ee->magic = DM_EEPROM_MAGIC; for (i = 0; i < len; i += 2) //读取EEPROM数据,并存放在data指针指向的区域 dm9000_read_eeprom(dm, (offset + i) / 2, data + i); return 0; }

读取eeprom内容

 

12.dm9000_set_eeprom

static int dm9000_set_eeprom(struct net_device *dev, struct ethtool_eeprom *ee, u8 *data) { board_info_t *dm = to_dm9000_board(dev); int offset = ee->offset; int len = ee->len; int i; /* EEPROM access is aligned to two bytes */ if ((len & 1) != 0 || (offset & 1) != 0) return -EINVAL; if (dm->flags & DM9000_PLATF_NO_EEPROM) return -ENOENT; if (ee->magic != DM_EEPROM_MAGIC) return -EINVAL; for (i = 0; i < len; i += 2) dm9000_write_eeprom(dm, (offset + i) / 2, data + i); return 0; }

对应读取,本函数完成写入的工作

 

13.dm9000_get_rx_csum

static uint32_t dm9000_get_rx_csum(struct net_device *dev) { board_info_t *dm = to_dm9000_board(dev); return dm->rx_csum; }

说来惭愧,还不知道rx_csum所代表的具体含义,是接收的什么呢?希望有前辈指点一二

 

14.dm9000_set_rx_csum

static int dm9000_set_rx_csum(struct net_device *dev, uint32_t data) { board_info_t *dm = to_dm9000_board(dev); unsigned long flags; int ret; spin_lock_irqsave(&dm->lock, flags); ret = dm9000_set_rx_csum_unlocked(dev, data); spin_unlock_irqrestore(&dm->lock, flags); return ret; }

其中dm9000_set_rx_csum_unlocked代码如下

static int dm9000_set_rx_csum_unlocked(struct net_device *dev, uint32_t data) { board_info_t *dm = to_dm9000_board(dev); if (dm->can_csum) { dm->rx_csum = data; iow(dm, DM9000_RCSR, dm->rx_csum ? RCSR_CSUM : 0); return 0; } return -EOPNOTSUPP; }

设置rx_csum吧~

 

15.ethtool_op_get_tx_csum

u32 ethtool_op_get_tx_csum(struct net_device *dev) { return (dev->features & NETIF_F_ALL_CSUM) != 0; }

呵呵,有接收自然就有发送

 

16.dm9000_set_tx_csum

static int dm9000_set_tx_csum(struct net_device *dev, uint32_t data) { board_info_t *dm = to_dm9000_board(dev); int ret = -EOPNOTSUPP; if (dm->can_csum) ret = ethtool_op_set_tx_csum(dev, data); return ret; }

核心工作在于ret = ethtool_op_set_tx_csum(dev, data);
int ethtool_op_set_tx_csum(struct net_device *dev, u32 data) { if (data) dev->features |= NETIF_F_IP_CSUM; else dev->features &= ~NETIF_F_IP_CSUM; return 0; }

通过传入的参数data设置tx_csum是0或1,具体作用还不清楚。

 

 

 

呼呼,我还是以蜗牛般的速度、豆腐渣式的分析把DM9000网卡驱动完整的过了一遍。与其说是分析,更贴切来说,应该是贴出代码,提供一个针对这个驱动讨论的平台罢了。很多枝节功能由于本人功力有限,还理所不能及,如有令到各位看官感觉不知所云,实在抱歉,以后定当更加努力,提供更高质量的学习记录给大家分享!

 

 

 

本系列课程链接地址

DM9000网卡驱动(1)http://blog.csdn.net/jarvis_xian/archive/2011/06/10/6537446.aspx
DM9000网卡驱动(2)http://blog.csdn.net/jarvis_xian/archive/2011/06/12/6539931.aspx
DM9000网卡驱动(3)http://blog.csdn.net/jarvis_xian/archive/2011/06/13/6542411.aspx
DM9000网卡驱动(4)http://blog.csdn.net/jarvis_xian/archive/2011/06/15/6545109.aspx
DM9000网卡驱动(5)http://blog.csdn.net/jarvis_xian/archive/2011/06/15/6547203.aspx

 

你可能感兴趣的:(c,工作,网络,struct,cmd,Access)