From 8aeab24a364e109b881ccf5222ec9da8907cc1d7 Mon Sep 17 00:00:00 2001 From: roger Date: Wed, 6 Apr 2016 18:07:56 +0800 Subject: [PATCH] net: phy: add sysfs node for reading PHY's registers Change-Id: I76468dd235a39b6f79699b1cc931c2c7bb7bdbc5 Signed-off-by: roger --- drivers/net/phy/mdio_bus.c | 47 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/drivers/net/phy/mdio_bus.c b/drivers/net/phy/mdio_bus.c index 88cb4592b6fb..c9a62692c1ea 100644 --- a/drivers/net/phy/mdio_bus.c +++ b/drivers/net/phy/mdio_bus.c @@ -656,10 +656,57 @@ phy_has_fixups_show(struct device *dev, struct device_attribute *attr, char *buf } static DEVICE_ATTR_RO(phy_has_fixups); +static ssize_t +phy_registers_show(struct device *dev, struct device_attribute *attr, char *buf) +{ + struct phy_device *phydev = to_phy_device(dev); + int index; + + for (index = 0; index < 32; index++) + sprintf(buf, "%s%2d: 0x%x\n", buf, index, + phy_read(phydev, index)); + + return strlen(buf); +} + +static ssize_t +phy_registers_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + struct phy_device *phydev = to_phy_device(dev); + long long index, val; + int i; + + for (i = 0; i < count; i++) { + if (*(buf + i) == ' ') + break; + } + + if (!kstrtoll(buf, 0, &index)) { + pr_err("Please input like1: \n"); + return count; + } + + if (!kstrtoll(buf + i + 1, 0, &val)) { + pr_err("Please input like2: \n"); + return count; + } + + pr_info("Set Ethernet PHY register %d to 0x%x\n", (int)index, (int)val); + + phy_write(phydev, index, val); + + return count; +} + +static DEVICE_ATTR_RW(phy_registers); + static struct attribute *mdio_dev_attrs[] = { &dev_attr_phy_id.attr, &dev_attr_phy_interface.attr, &dev_attr_phy_has_fixups.attr, + &dev_attr_phy_registers.attr, NULL, }; ATTRIBUTE_GROUPS(mdio_dev); -- 2.34.1