通过MDIO读写phy寄存器返回值一直为0

Nvidia 工程师,您好

我使用ioctl()函数读取AQR113C的寄存器,但我得到的值全都是0x0,驱动程序是否不支持使用ioctl()读取AQR113C的寄存器?

这是我使用的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <linux/mii.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/sockios.h>
#include <linux/types.h>
#include <netinet/in.h>
#include <unistd.h>
 
int main(int argc, char *argv[])
{
    int sockfd;
    struct mii_ioctl_data *mii = NULL;
    struct ifreq ifr;
    memset(&ifr, 0, sizeof(ifr));
 
    if(argc == 4)
    {
        strncpy(ifr.ifr_name, argv[1], IFNAMSIZ - 1);
        sockfd = socket(PF_LOCAL, SOCK_DGRAM, 0);
        ioctl(sockfd, SIOCGMIIPHY, &ifr);
        mii = (struct mii_ioctl_data*)&ifr.ifr_data;
 
        mii->phy_id    = (uint16_t)strtoul(argv[2], NULL, 0);
        mii->reg_num    = (uint16_t)strtoul(argv[3], NULL, 0);
 
        ioctl(sockfd, SIOCGMIIREG, &ifr);
 
        printf("read --- value : 0x%x", mii->val_out);
    }
    else if(argc == 5)
    {
        strncpy(ifr.ifr_name, argv[1], IFNAMSIZ - 1);
        sockfd = socket(PF_LOCAL, SOCK_DGRAM, 0);
        ioctl(sockfd, SIOCGMIIPHY, &ifr);
        mii = (struct mii_ioctl_data*)&ifr.ifr_data;

        mii->phy_id    = (uint16_t)strtoul(argv[2], NULL, 0);
        mii->reg_num    = (uint16_t)strtoul(argv[3], NULL, 0);
        mii->val_in     = (uint16_t)strtoul(argv[4], NULL, 0);
 
        ioctl(sockfd, SIOCSMIIREG, &ifr);
    }
    else
    {
       printf("mdio ethX phyId addr value\n");
    }
 
    close(sockfd);
    return 0;
}

Hi,

可以試試看用C45 mdio tool讀看看結果嗎?

谢谢你的建议,当添加mdio_phy_id_c45(int PHY_ID, int MMD);后我的程序正确运行并得到了符合预期的结果。

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.