Accessing ethernet phy driver from linux user space

11.6k Views Asked by At

i want to access ethernet phy driver from linux user space,

In uboot we can directly access phy registers using mii commands

similarly i want to read and write phy registers from linux user space .

cause there is no major or minor number comes in case of phy driver(maybe cause its a network driver) how to do it.and is it possible

2

There are 2 best solutions below

1
On

There are the following ioctl requests for that purpose:

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

And MII register constants are defined in:

#include <linux/mii.h>

Example:

#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;
}
0
On

Try to use mii-tool or ethtool. Look at the sources of those programs how to get access to phy api.