Skip to contentSkip to conversation

phylib: add mdiobus_{read,write}

PATCH In review Lennert Buytenhek 3 participants 5 messages Sep 29, 2008 – Sep 30, 2008

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/6 phylib: add mdiobus_{read,write}

Add mdiobus_{read,write} routines to allow direct reading/writing of registers on an mii bus without having to go through the PHY abstraction, and make phy_{read,write} use these primitives.

---
 drivers/net/phy/mdio_bus.c |   49 ++++++++++++++++++++++++++++++++++++++++++++
 drivers/net/phy/phy.c      |   22 +------------------
 include/linux/phy.h        |    2 +
 3 files changed, 53 insertions(+), 20 deletions(-)
drivers/net/phy/mdio_bus.c +49 -0
index f9c27ac..0db3605 100644
@@ -210,6 +210,55 @@ struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
 EXPORT_SYMBOL(mdiobus_scan);
 
 /**
+ * mdiobus_read - Convenience function for reading a given MII mgmt register
+ * @bus: the mii_bus struct
+ * @addr: the phy address
+ * @regnum: register number to read
+ *
+ * NOTE: MUST NOT be called from interrupt context,
+ * because the bus read/write functions may wait for an interrupt
+ * to conclude the operation.
+ */
+int mdiobus_read(struct mii_bus *bus, int addr, u16 regnum)
+{
+	int retval;
+
+	BUG_ON(in_interrupt());
+
+	mutex_lock(&bus->mdio_lock);
+	retval = bus->read(bus, addr, regnum);
+	mutex_unlock(&bus->mdio_lock);
+
+	return retval;
+}
+EXPORT_SYMBOL(mdiobus_read);
+
+/**
+ * mdiobus_write - Convenience function for writing a given MII mgmt register
+ * @bus: the mii_bus struct
+ * @addr: the phy address
+ * @regnum: register number to write
+ * @val: value to write to @regnum
+ *
+ * NOTE: MUST NOT be called from interrupt context,
+ * because the bus read/write functions may wait for an interrupt
+ * to conclude the operation.
+ */
+int mdiobus_write(struct mii_bus *bus, int addr, u16 regnum, u16 val)
+{
+	int err;
+
+	BUG_ON(in_interrupt());
+
+	mutex_lock(&bus->mdio_lock);
+	err = bus->write(bus, addr, regnum, val);
+	mutex_unlock(&bus->mdio_lock);
+
+	return err;
+}
+EXPORT_SYMBOL(mdiobus_write);
+
+/**
  * mdio_bus_match - determine if given PHY driver supports the given PHY device
  * @dev: target PHY device
  * @drv: given PHY driver
drivers/net/phy/phy.c +2 -20
index 0433fcd..a58cbd6 100644
@@ -68,16 +68,7 @@ EXPORT_SYMBOL(phy_print_status);
  */
 int phy_read(struct phy_device *phydev, u16 regnum)
 {
-	int retval;
-	struct mii_bus *bus = phydev->bus;
-
-	BUG_ON(in_interrupt());
-
-	mutex_lock(&bus->mdio_lock);
-	retval = bus->read(bus, phydev->addr, regnum);
-	mutex_unlock(&bus->mdio_lock);
-
-	return retval;
+	return mdiobus_read(phydev->bus, phydev->addr, regnum);
 }
 EXPORT_SYMBOL(phy_read);
 
@@ -93,16 +84,7 @@ EXPORT_SYMBOL(phy_read);
  */
 int phy_write(struct phy_device *phydev, u16 regnum, u16 val)
 {
-	int err;
-	struct mii_bus *bus = phydev->bus;
-
-	BUG_ON(in_interrupt());
-
-	mutex_lock(&bus->mdio_lock);
-	err = bus->write(bus, phydev->addr, regnum, val);
-	mutex_unlock(&bus->mdio_lock);
-
-	return err;
+	return mdiobus_write(phydev->bus, phydev->addr, regnum, val);
 }
 EXPORT_SYMBOL(phy_write);
 
include/linux/phy.h +2 -0
index 891f27f..ddfb7fd 100644
@@ -421,6 +421,8 @@ int mdiobus_register(struct mii_bus *bus);
 void mdiobus_unregister(struct mii_bus *bus);
 void mdiobus_free(struct mii_bus *bus);
 struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr);
+int mdiobus_read(struct mii_bus *bus, int addr, u16 regnum);
+int mdiobus_write(struct mii_bus *bus, int addr, u16 regnum, u16 val);
 
 void phy_sanitize_settings(struct phy_device *phydev);
 int phy_stop_interrupts(struct phy_device *phydev);
Signature · 1 line
1.5.6.4
#1 lore raw
Quoted 17 lines · Lennert Buytenhek

Add mdiobus_{read,write} routines to allow direct reading/writing of registers on an mii bus without having to go through the PHY abstraction, and make phy_{read,write} use these primitives.

int phy_read(struct phy_device *phydev, u16 regnum) {

drivers/net/phy/phy.c +1 -10
-	int retval;
-	struct mii_bus *bus = phydev->bus;
-
-	BUG_ON(in_interrupt());
-
-	mutex_lock(&bus->mdio_lock);
-	retval = bus->read(bus, phydev->addr, regnum);
-	mutex_unlock(&bus->mdio_lock);
-
-	return retval;
+	return mdiobus_read(phydev->bus, phydev->addr, regnum);
Quoted 2 lines

}
EXPORT_SYMBOL(phy_read);

Might want to make these function static inline. Since they are external, gcc won't be able to inline them automatically.

#2 lore raw
Quoted 14 lines · Trent Piepho
Quoted 14 lines

Add mdiobus_{read,write} routines to allow direct reading/writing of registers on an mii bus without having to go through the PHY abstraction, and make phy_{read,write} use these primitives.

int phy_read(struct phy_device *phydev, u16 regnum) {

drivers/net/phy/phy.c +0 -8
-	int retval;
-	struct mii_bus *bus = phydev->bus;
-
-	BUG_ON(in_interrupt());
-
-	mutex_lock(&bus->mdio_lock);
-	retval = bus->read(bus, phydev->addr, regnum);
-	mutex_unlock(&bus->mdio_lock);
Quoted 8 lines
Quoted 5 lines
drivers/net/phy/phy.c +1 -2
-
-	return retval;
+	return mdiobus_read(phydev->bus, phydev->addr, regnum);

}
EXPORT_SYMBOL(phy_read);

Might want to make these function static inline. Since they are external, gcc won't be able to inline them automatically.

If i make them static inline, I won't be able to call them from external code, which was sort of the idea.

Or are you suggesting that I move them to include/linux/phy.h ?

#3 lore raw
In reply to Lennert Buytenhek
Quoted 5 lines · Lennert Buytenhek

Add mdiobus_{read,write} routines to allow direct reading/writing of registers on an mii bus without having to go through the PHY abstraction, and make phy_{read,write} use these primitives.

Looks pretty good. I was actually just thinking this might be handy for some code we're working on.

Just one comment...

Quoted 10 lines
drivers/net/phy/phy.c +0 -3
@@ -68,16 +68,7 @@ EXPORT_SYMBOL(phy_print_status);
 */
 int phy_read(struct phy_device *phydev, u16 regnum)
 {
-	int retval;
-	struct mii_bus *bus = phydev->bus;
-

drivers/net/phy/phy.c EXPORT_SYMBOL(phy_print_status); · line 73

Quoted 10 lines
drivers/net/phy/phy.c +1 -7
-	BUG_ON(in_interrupt());
-
-	mutex_lock(&bus->mdio_lock);
-	retval = bus->read(bus, phydev->addr, regnum);
-	mutex_unlock(&bus->mdio_lock);
-
-	return retval;
+	return mdiobus_read(phydev->bus, phydev->addr, regnum);
 }
 EXPORT_SYMBOL(phy_read);

I agree with Trent. Move these into phy.h and make them static inline

#4 lore raw
Quoted 25 lines · Andy Fleming
Quoted 5 lines

Add mdiobus_{read,write} routines to allow direct reading/writing of registers on an mii bus without having to go through the PHY abstraction, and make phy_{read,write} use these primitives.

Looks pretty good. I was actually just thinking this might be handy for some code we're working on.

Just one comment...

Quoted 14 lines
drivers/net/phy/phy.c +0 -7
@@ -68,16 +68,7 @@ EXPORT_SYMBOL(phy_print_status);
 */
 int phy_read(struct phy_device *phydev, u16 regnum)
 {
-	int retval;
-	struct mii_bus *bus = phydev->bus;
-
-	BUG_ON(in_interrupt());
-
-	mutex_lock(&bus->mdio_lock);
-	retval = bus->read(bus, phydev->addr, regnum);
Quoted 8 lines

drivers/net/phy/phy.c EXPORT_SYMBOL(phy_print_status); · line 73

Quoted 6 lines
drivers/net/phy/phy.c +1 -3
-	mutex_unlock(&bus->mdio_lock);
-
-	return retval;
+	return mdiobus_read(phydev->bus, phydev->addr, regnum);
 }
 EXPORT_SYMBOL(phy_read);

I agree with Trent. Move these into phy.h and make them static inline

OK. New version below.

commit 0cb6fabc764218f14c0adf9d37a4748fb722e3f3

Author: Lennert Buytenhek <[email protected]>
Date:   Wed Jul 9 14:31:39 2008 +0200

phylib: add mdiobus_{read,write}

Add mdiobus_{read,write} routines to allow direct reading/writing of registers on an mii bus without having to go through the PHY abstraction, and make phy_{read,write} use these primitives.

drivers/net/phy/mdio_bus.c +49 -0
index f9c27ac..0db3605 100644
@@ -210,6 +210,55 @@ struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr)
 EXPORT_SYMBOL(mdiobus_scan);
 
 /**
+ * mdiobus_read - Convenience function for reading a given MII mgmt register
+ * @bus: the mii_bus struct
+ * @addr: the phy address
+ * @regnum: register number to read
+ *
+ * NOTE: MUST NOT be called from interrupt context,
+ * because the bus read/write functions may wait for an interrupt
+ * to conclude the operation.
+ */
+int mdiobus_read(struct mii_bus *bus, int addr, u16 regnum)
+{
+	int retval;
+
+	BUG_ON(in_interrupt());
+
+	mutex_lock(&bus->mdio_lock);
+	retval = bus->read(bus, addr, regnum);
+	mutex_unlock(&bus->mdio_lock);
+
+	return retval;
+}
+EXPORT_SYMBOL(mdiobus_read);
+
+/**
+ * mdiobus_write - Convenience function for writing a given MII mgmt register
+ * @bus: the mii_bus struct
+ * @addr: the phy address
+ * @regnum: register number to write
+ * @val: value to write to @regnum
+ *
+ * NOTE: MUST NOT be called from interrupt context,
+ * because the bus read/write functions may wait for an interrupt
+ * to conclude the operation.
+ */
+int mdiobus_write(struct mii_bus *bus, int addr, u16 regnum, u16 val)
+{
+	int err;
+
+	BUG_ON(in_interrupt());
+
+	mutex_lock(&bus->mdio_lock);
+	err = bus->write(bus, addr, regnum, val);
+	mutex_unlock(&bus->mdio_lock);
+
+	return err;
+}
+EXPORT_SYMBOL(mdiobus_write);
+
+/**
  * mdio_bus_match - determine if given PHY driver supports the given PHY device
  * @dev: target PHY device
  * @drv: given PHY driver
drivers/net/phy/phy.c +0 -49

55 lines not on this page · raw message

include/linux/phy.h +38 -8

64 lines not on this page · raw message

--