4

我想从 linux 用户空间访问以太网 phy 驱动程序,

在 uboot 中我们可以使用mii命令直接访问 phy 寄存器

同样,我想从 linux 用户空间读取和写入 phy 寄存器。

因为在 phy 驱动程序的情况下没有主要或次要数字(可能是网络驱动程序)怎么做。有可能吗

4

2 回答 2

2

ioctl为此目的提出以下要求:

#define SIOCGMIIREG 0x8948      /* Read MII PHY register.   */
#define SIOCSMIIREG 0x8949      /* Write MII PHY register.  */ 

MII 寄存器常量定义在:

#include <linux/mii.h>

例子:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <linux/mii.h>
#include <linux/sockios.h>

int main()
{
    struct ifreq ifr;

    memset(&ifr, 0, sizeof(ifr));
    strcpy(ifr.ifr_name, "eth1");

    struct mii_ioctl_data* mii = (struct mii_ioctl_data*)(&ifr.ifr_data);
    mii->phy_id  = 1;
    mii->reg_num = MII_BMSR;
    mii->val_in  = 0;
    mii->val_out = 0;

    const int fd = socket(AF_INET, SOCK_DGRAM, 0);
    if (fd != -1)
    {
        if (ioctl(fd, SIOCGMIIREG, &ifr) != -1)
        {
            printf("MII_BMSR     = 0x%04hX \n", mii->val_out);
            printf("BMSR_LSTATUS = %d \n", (mii->val_out & BMSR_LSTATUS) ? 1 : 0);
        }
        close(fd);
    }

    return 0;
}
于 2018-08-31T10:58:48.337 回答
0

尝试使用 mii-tool 或 ethtool。查看这些程序的来源如何访问 phy api。

于 2018-08-31T08:24:19.373 回答