Merge branch 'misc' into release
[safe/jmp/linux-2.6] / drivers / net / phy / phy_device.c
index 1716274..8212b2b 100644 (file)
@@ -39,20 +39,21 @@ MODULE_DESCRIPTION("PHY library");
 MODULE_AUTHOR("Andy Fleming");
 MODULE_LICENSE("GPL");
 
-static struct phy_driver genphy_driver;
-extern int mdio_bus_init(void);
-extern void mdio_bus_exit(void);
-
 void phy_device_free(struct phy_device *phydev)
 {
        kfree(phydev);
 }
+EXPORT_SYMBOL(phy_device_free);
 
 static void phy_device_release(struct device *dev)
 {
        phy_device_free(to_phy_device(dev));
 }
 
+static struct phy_driver genphy_driver;
+extern int mdio_bus_init(void);
+extern void mdio_bus_exit(void);
+
 static LIST_HEAD(phy_fixup_list);
 static DEFINE_MUTEX(phy_fixup_lock);
 
@@ -74,7 +75,7 @@ int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
        if (!fixup)
                return -ENOMEM;
 
-       strncpy(fixup->bus_id, bus_id, BUS_ID_SIZE);
+       strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id));
        fixup->phy_uid = phy_uid;
        fixup->phy_uid_mask = phy_uid_mask;
        fixup->run = run;
@@ -109,7 +110,7 @@ EXPORT_SYMBOL(phy_register_fixup_for_id);
  */
 static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
 {
-       if (strcmp(fixup->bus_id, phydev->dev.bus_id) != 0)
+       if (strcmp(fixup->bus_id, dev_name(&phydev->dev)) != 0)
                if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
                        return 0;
 
@@ -133,8 +134,10 @@ int phy_scan_fixups(struct phy_device *phydev)
 
                        err = fixup->run(phydev);
 
-                       if (err < 0)
+                       if (err < 0) {
+                               mutex_unlock(&phy_fixup_lock);
                                return err;
+                       }
                }
        }
        mutex_unlock(&phy_fixup_lock);
@@ -166,6 +169,10 @@ struct phy_device* phy_device_create(struct mii_bus *bus, int addr, int phy_id)
        dev->addr = addr;
        dev->phy_id = phy_id;
        dev->bus = bus;
+       dev->dev.parent = bus->parent;
+       dev->dev.bus = &mdio_bus_type;
+       dev->irq = bus->irq != NULL ? bus->irq[addr] : PHY_POLL;
+       dev_set_name(&dev->dev, PHY_ID_FMT, bus->id, addr);
 
        dev->state = PHY_DOWN;
 
@@ -227,14 +234,46 @@ struct phy_device * get_phy_device(struct mii_bus *bus, int addr)
        if (r)
                return ERR_PTR(r);
 
-       /* If the phy_id is all Fs, there is no device there */
-       if (0xffffffff == phy_id)
+       /* If the phy_id is mostly Fs, there is no device there */
+       if ((phy_id & 0x1fffffff) == 0x1fffffff)
                return NULL;
 
        dev = phy_device_create(bus, addr, phy_id);
 
        return dev;
 }
+EXPORT_SYMBOL(get_phy_device);
+
+/**
+ * phy_device_register - Register the phy device on the MDIO bus
+ * @phydev: phy_device structure to be added to the MDIO bus
+ */
+int phy_device_register(struct phy_device *phydev)
+{
+       int err;
+
+       /* Don't register a phy if one is already registered at this
+        * address */
+       if (phydev->bus->phy_map[phydev->addr])
+               return -EINVAL;
+       phydev->bus->phy_map[phydev->addr] = phydev;
+
+       /* Run all of the fixups for this PHY */
+       phy_scan_fixups(phydev);
+
+       err = device_register(&phydev->dev);
+       if (err) {
+               pr_err("phy %d failed to register\n", phydev->addr);
+               goto out;
+       }
+
+       return 0;
+
+ out:
+       phydev->bus->phy_map[phydev->addr] = NULL;
+       return err;
+}
+EXPORT_SYMBOL(phy_device_register);
 
 /**
  * phy_prepare_link - prepares the PHY layer to monitor link status
@@ -255,6 +294,33 @@ void phy_prepare_link(struct phy_device *phydev,
 }
 
 /**
+ * phy_connect_direct - connect an ethernet device to a specific phy_device
+ * @dev: the network device to connect
+ * @phydev: the pointer to the phy device
+ * @handler: callback function for state change notifications
+ * @flags: PHY device's dev_flags
+ * @interface: PHY device's interface
+ */
+int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
+                      void (*handler)(struct net_device *), u32 flags,
+                      phy_interface_t interface)
+{
+       int rc;
+
+       rc = phy_attach_direct(dev, phydev, flags, interface);
+       if (rc)
+               return rc;
+
+       phy_prepare_link(phydev, handler);
+       phy_start_machine(phydev, NULL);
+       if (phydev->irq > 0)
+               phy_start_interrupts(phydev);
+
+       return 0;
+}
+EXPORT_SYMBOL(phy_connect_direct);
+
+/**
  * phy_connect - connect an ethernet device to a PHY device
  * @dev: the network device to connect
  * @bus_id: the id string of the PHY device to connect
@@ -275,18 +341,21 @@ struct phy_device * phy_connect(struct net_device *dev, const char *bus_id,
                phy_interface_t interface)
 {
        struct phy_device *phydev;
+       struct device *d;
+       int rc;
 
-       phydev = phy_attach(dev, bus_id, flags, interface);
-
-       if (IS_ERR(phydev))
-               return phydev;
-
-       phy_prepare_link(phydev, handler);
-
-       phy_start_machine(phydev, NULL);
+       /* Search the list of PHY devices on the mdio bus for the
+        * PHY with the requested name */
+       d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id);
+       if (!d) {
+               pr_err("PHY %s not found\n", bus_id);
+               return ERR_PTR(-ENODEV);
+       }
+       phydev = to_phy_device(d);
 
-       if (phydev->irq > 0)
-               phy_start_interrupts(phydev);
+       rc = phy_connect_direct(dev, phydev, handler, flags, interface);
+       if (rc)
+               return ERR_PTR(rc);
 
        return phydev;
 }
@@ -309,15 +378,24 @@ void phy_disconnect(struct phy_device *phydev)
 }
 EXPORT_SYMBOL(phy_disconnect);
 
-static int phy_compare_id(struct device *dev, void *data)
+int phy_init_hw(struct phy_device *phydev)
 {
-       return strcmp((char *)data, dev->bus_id) ? 0 : 1;
+       int ret;
+
+       if (!phydev->drv || !phydev->drv->config_init)
+               return 0;
+
+       ret = phy_scan_fixups(phydev);
+       if (ret < 0)
+               return ret;
+
+       return phydev->drv->config_init(phydev);
 }
 
 /**
- * phy_attach - attach a network device to a particular PHY device
+ * phy_attach_direct - attach a network device to a given PHY device pointer
  * @dev: network device to attach
- * @bus_id: PHY device to attach
+ * @phydev: Pointer to phy_device to attach
  * @flags: PHY device's dev_flags
  * @interface: PHY device's interface
  *
@@ -328,23 +406,10 @@ static int phy_compare_id(struct device *dev, void *data)
  *     the attaching device, and given a callback for link status
  *     change.  The phy_device is returned to the attaching driver.
  */
-struct phy_device *phy_attach(struct net_device *dev,
-               const char *bus_id, u32 flags, phy_interface_t interface)
+int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
+                     u32 flags, phy_interface_t interface)
 {
-       struct bus_type *bus = &mdio_bus_type;
-       struct phy_device *phydev;
-       struct device *d;
-
-       /* Search the list of PHY devices on the mdio bus for the
-        * PHY with the requested name */
-       d = bus_find_device(bus, NULL, (void *)bus_id, phy_compare_id);
-
-       if (d) {
-               phydev = to_phy_device(d);
-       } else {
-               printk(KERN_ERR "%s not found\n", bus_id);
-               return ERR_PTR(-ENODEV);
-       }
+       struct device *d = &phydev->dev;
 
        /* Assume that if there is no driver, that it doesn't
         * exist, and we should use the genphy driver. */
@@ -357,13 +422,12 @@ struct phy_device *phy_attach(struct net_device *dev,
                        err = device_bind_driver(d);
 
                if (err)
-                       return ERR_PTR(err);
+                       return err;
        }
 
        if (phydev->attached_dev) {
-               printk(KERN_ERR "%s: %s already attached\n",
-                               dev->name, bus_id);
-               return ERR_PTR(-EBUSY);
+               dev_err(&dev->dev, "PHY already attached\n");
+               return -EBUSY;
        }
 
        phydev->attached_dev = dev;
@@ -375,19 +439,40 @@ struct phy_device *phy_attach(struct net_device *dev,
        /* Do initial configuration here, now that
         * we have certain key parameters
         * (dev_flags and interface) */
-       if (phydev->drv->config_init) {
-               int err;
-
-               err = phy_scan_fixups(phydev);
-
-               if (err < 0)
-                       return ERR_PTR(err);
+       return phy_init_hw(phydev);
+}
+EXPORT_SYMBOL(phy_attach_direct);
 
-               err = phydev->drv->config_init(phydev);
+/**
+ * phy_attach - attach a network device to a particular PHY device
+ * @dev: network device to attach
+ * @bus_id: Bus ID of PHY device to attach
+ * @flags: PHY device's dev_flags
+ * @interface: PHY device's interface
+ *
+ * Description: Same as phy_attach_direct() except that a PHY bus_id
+ *     string is passed instead of a pointer to a struct phy_device.
+ */
+struct phy_device *phy_attach(struct net_device *dev,
+               const char *bus_id, u32 flags, phy_interface_t interface)
+{
+       struct bus_type *bus = &mdio_bus_type;
+       struct phy_device *phydev;
+       struct device *d;
+       int rc;
 
-               if (err < 0)
-                       return ERR_PTR(err);
+       /* Search the list of PHY devices on the mdio bus for the
+        * PHY with the requested name */
+       d = bus_find_device_by_name(bus, NULL, bus_id);
+       if (!d) {
+               pr_err("PHY %s not found\n", bus_id);
+               return ERR_PTR(-ENODEV);
        }
+       phydev = to_phy_device(d);
+
+       rc = phy_attach_direct(dev, phydev, flags, interface);
+       if (rc)
+               return ERR_PTR(rc);
 
        return phydev;
 }
@@ -514,23 +599,6 @@ int genphy_setup_forced(struct phy_device *phydev)
        
        err = phy_write(phydev, MII_BMCR, ctl);
 
-       if (err < 0)
-               return err;
-
-       /*
-        * Run the fixups on this PHY, just in case the
-        * board code needs to change something after a reset
-        */
-       err = phy_scan_fixups(phydev);
-
-       if (err < 0)
-               return err;
-
-       /* We just reset the device, so we'd better configure any
-        * settings the PHY requires to operate */
-       if (phydev->drv->config_init)
-               err = phydev->drv->config_init(phydev);
-
        return err;
 }
 
@@ -557,6 +625,7 @@ int genphy_restart_aneg(struct phy_device *phydev)
 
        return ctl;
 }
+EXPORT_SYMBOL(genphy_restart_aneg);
 
 
 /**
@@ -569,20 +638,32 @@ int genphy_restart_aneg(struct phy_device *phydev)
  */
 int genphy_config_aneg(struct phy_device *phydev)
 {
-       int result = 0;
+       int result;
 
-       if (AUTONEG_ENABLE == phydev->autoneg) {
-               int result = genphy_config_advert(phydev);
+       if (AUTONEG_ENABLE != phydev->autoneg)
+               return genphy_setup_forced(phydev);
+
+       result = genphy_config_advert(phydev);
+
+       if (result < 0) /* error */
+               return result;
+
+       if (result == 0) {
+               /* Advertisment hasn't changed, but maybe aneg was never on to
+                * begin with?  Or maybe phy was isolated? */
+               int ctl = phy_read(phydev, MII_BMCR);
 
-               if (result < 0) /* error */
-                       return result;
+               if (ctl < 0)
+                       return ctl;
 
-               /* Only restart aneg if we are advertising something different
-                * than we were before.  */
-               if (result > 0)
-                       result = genphy_restart_aneg(phydev);
-       } else
-               result = genphy_setup_forced(phydev);
+               if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
+                       result = 1; /* do restart aneg */
+       }
+
+       /* Only restart aneg if we are advertising something different
+        * than we were before.  */
+       if (result > 0)
+               result = genphy_restart_aneg(phydev);
 
        return result;
 }
@@ -763,7 +844,35 @@ static int genphy_config_init(struct phy_device *phydev)
 
        return 0;
 }
+int genphy_suspend(struct phy_device *phydev)
+{
+       int value;
+
+       mutex_lock(&phydev->lock);
+
+       value = phy_read(phydev, MII_BMCR);
+       phy_write(phydev, MII_BMCR, (value | BMCR_PDOWN));
+
+       mutex_unlock(&phydev->lock);
+
+       return 0;
+}
+EXPORT_SYMBOL(genphy_suspend);
+
+int genphy_resume(struct phy_device *phydev)
+{
+       int value;
+
+       mutex_lock(&phydev->lock);
+
+       value = phy_read(phydev, MII_BMCR);
+       phy_write(phydev, MII_BMCR, (value & ~BMCR_PDOWN));
 
+       mutex_unlock(&phydev->lock);
+
+       return 0;
+}
+EXPORT_SYMBOL(genphy_resume);
 
 /**
  * phy_probe - probe and init a PHY device
@@ -839,7 +948,6 @@ int phy_driver_register(struct phy_driver *new_driver)
 {
        int retval;
 
-       memset(&new_driver->driver, 0, sizeof(new_driver->driver));
        new_driver->driver.name = new_driver->name;
        new_driver->driver.bus = &mdio_bus_type;
        new_driver->driver.probe = phy_probe;
@@ -874,6 +982,8 @@ static struct phy_driver genphy_driver = {
        .features       = 0,
        .config_aneg    = genphy_config_aneg,
        .read_status    = genphy_read_status,
+       .suspend        = genphy_suspend,
+       .resume         = genphy_resume,
        .driver         = {.owner= THIS_MODULE, },
 };