WorkStruct: make allyesconfig
[safe/jmp/linux-2.6] / drivers / net / phy / phy.c
index d3e4363..a443976 100644 (file)
@@ -14,7 +14,6 @@
  * option) any later version.
  *
  */
-#include <linux/config.h>
 #include <linux/kernel.h>
 #include <linux/sched.h>
 #include <linux/string.h>
@@ -30,7 +29,6 @@
 #include <linux/spinlock.h>
 #include <linux/mm.h>
 #include <linux/module.h>
-#include <linux/version.h>
 #include <linux/mii.h>
 #include <linux/ethtool.h>
 #include <linux/phy.h>
 #include <asm/irq.h>
 #include <asm/uaccess.h>
 
-static void phy_timer(unsigned long data);
-static int phy_disable_interrupts(struct phy_device *phydev);
-static void phy_sanitize_settings(struct phy_device *phydev);
-static int phy_stop_interrupts(struct phy_device *phydev);
+/* Convenience function to print out the current phy status
+ */
+void phy_print_status(struct phy_device *phydev)
+{
+       pr_info("PHY: %s - Link is %s", phydev->dev.bus_id,
+                       phydev->link ? "Up" : "Down");
+       if (phydev->link)
+               printk(" - %d/%s", phydev->speed,
+                               DUPLEX_FULL == phydev->duplex ?
+                               "Full" : "Half");
+
+       printk("\n");
+}
+EXPORT_SYMBOL(phy_print_status);
 
 
 /* Convenience functions for reading/writing a given PHY
@@ -114,42 +122,6 @@ static inline int phy_aneg_done(struct phy_device *phydev)
        return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
 }
 
-/* phy_start_aneg
- *
- * description: Calls the PHY driver's config_aneg, and then
- *   sets the PHY state to PHY_AN if auto-negotiation is enabled,
- *   and to PHY_FORCING if auto-negotiation is disabled. Unless
- *   the PHY is currently HALTED.
- */
-static int phy_start_aneg(struct phy_device *phydev)
-{
-       int err;
-
-       spin_lock(&phydev->lock);
-
-       if (AUTONEG_DISABLE == phydev->autoneg)
-               phy_sanitize_settings(phydev);
-
-       err = phydev->drv->config_aneg(phydev);
-
-       if (err < 0)
-               goto out_unlock;
-
-       if (phydev->state != PHY_HALTED) {
-               if (AUTONEG_ENABLE == phydev->autoneg) {
-                       phydev->state = PHY_AN;
-                       phydev->link_timeout = PHY_AN_TIMEOUT;
-               } else {
-                       phydev->state = PHY_FORCING;
-                       phydev->link_timeout = PHY_FORCE_TIMEOUT;
-               }
-       }
-
-out_unlock:
-       spin_unlock(&phydev->lock);
-       return err;
-}
-
 /* A structure for mapping a particular speed and duplex
  * combination to a particular SUPPORTED and ADVERTISED value */
 struct phy_setting {
@@ -159,7 +131,7 @@ struct phy_setting {
 };
 
 /* A mapping of all SUPPORTED settings to speed/duplex */
-static struct phy_setting settings[] = {
+static const struct phy_setting settings[] = {
        {
                .speed = 10000,
                .duplex = DUPLEX_FULL,
@@ -241,7 +213,7 @@ static inline int phy_find_valid(int idx, u32 features)
  *   duplexes.  Drop down by one in this order:  1000/FULL,
  *   1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF
  */
-static void phy_sanitize_settings(struct phy_device *phydev)
+void phy_sanitize_settings(struct phy_device *phydev)
 {
        u32 features = phydev->supported;
        int idx;
@@ -256,31 +228,7 @@ static void phy_sanitize_settings(struct phy_device *phydev)
        phydev->speed = settings[idx].speed;
        phydev->duplex = settings[idx].duplex;
 }
-
-/* phy_force_reduction
- *
- * description: Reduces the speed/duplex settings by
- *   one notch.  The order is so:
- *   1000/FULL, 1000/HALF, 100/FULL, 100/HALF,
- *   10/FULL, 10/HALF.  The function bottoms out at 10/HALF.
- */
-static void phy_force_reduction(struct phy_device *phydev)
-{
-       int idx;
-
-       idx = phy_find_setting(phydev->speed, phydev->duplex);
-       
-       idx++;
-
-       idx = phy_find_valid(idx, phydev->supported);
-
-       phydev->speed = settings[idx].speed;
-       phydev->duplex = settings[idx].duplex;
-
-       pr_info("Trying %d/%s\n", phydev->speed,
-                       DUPLEX_FULL == phydev->duplex ?
-                       "FULL" : "HALF");
-}
+EXPORT_SYMBOL(phy_sanitize_settings);
 
 /* phy_ethtool_sset:
  * A generic ethtool sset function.  Handles all the details
@@ -291,6 +239,7 @@ static void phy_force_reduction(struct phy_device *phydev)
  * - phy_start_aneg() will make sure forced settings are sane, and
  *   choose the next best ones from the ones selected, so we don't
  *   care if ethtool tries to give us bad values
+ *
  */
 int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
 {
@@ -406,6 +355,48 @@ int phy_mii_ioctl(struct phy_device *phydev,
        return 0;
 }
 
+/* phy_start_aneg
+ *
+ * description: Sanitizes the settings (if we're not
+ *   autonegotiating them), and then calls the driver's
+ *   config_aneg function.  If the PHYCONTROL Layer is operating,
+ *   we change the state to reflect the beginning of
+ *   Auto-negotiation or forcing.
+ */
+int phy_start_aneg(struct phy_device *phydev)
+{
+       int err;
+
+       spin_lock(&phydev->lock);
+
+       if (AUTONEG_DISABLE == phydev->autoneg)
+               phy_sanitize_settings(phydev);
+
+       err = phydev->drv->config_aneg(phydev);
+
+       if (err < 0)
+               goto out_unlock;
+
+       if (phydev->state != PHY_HALTED) {
+               if (AUTONEG_ENABLE == phydev->autoneg) {
+                       phydev->state = PHY_AN;
+                       phydev->link_timeout = PHY_AN_TIMEOUT;
+               } else {
+                       phydev->state = PHY_FORCING;
+                       phydev->link_timeout = PHY_FORCE_TIMEOUT;
+               }
+       }
+
+out_unlock:
+       spin_unlock(&phydev->lock);
+       return err;
+}
+EXPORT_SYMBOL(phy_start_aneg);
+
+
+static void phy_change(struct work_struct *work);
+static void phy_timer(unsigned long data);
+
 /* phy_start_machine:
  *
  * description: The PHY infrastructure can run a state machine
@@ -428,9 +419,8 @@ void phy_start_machine(struct phy_device *phydev,
 
 /* phy_stop_machine
  *
- * description: Stops the state machine timer, sets the state to
- *   UP (unless it wasn't up yet), and then frees the interrupt,
- *   if it is in use. This function must be called BEFORE
+ * description: Stops the state machine timer, sets the state to UP
+ *   (unless it wasn't up yet). This function must be called BEFORE
  *   phy_detach.
  */
 void phy_stop_machine(struct phy_device *phydev)
@@ -442,12 +432,35 @@ void phy_stop_machine(struct phy_device *phydev)
                phydev->state = PHY_UP;
        spin_unlock(&phydev->lock);
 
-       if (phydev->irq != PHY_POLL)
-               phy_stop_interrupts(phydev);
-
        phydev->adjust_state = NULL;
 }
 
+/* phy_force_reduction
+ *
+ * description: Reduces the speed/duplex settings by
+ *   one notch.  The order is so:
+ *   1000/FULL, 1000/HALF, 100/FULL, 100/HALF,
+ *   10/FULL, 10/HALF.  The function bottoms out at 10/HALF.
+ */
+static void phy_force_reduction(struct phy_device *phydev)
+{
+       int idx;
+
+       idx = phy_find_setting(phydev->speed, phydev->duplex);
+       
+       idx++;
+
+       idx = phy_find_valid(idx, phydev->supported);
+
+       phydev->speed = settings[idx].speed;
+       phydev->duplex = settings[idx].duplex;
+
+       pr_info("Trying %d/%s\n", phydev->speed,
+                       DUPLEX_FULL == phydev->duplex ?
+                       "FULL" : "HALF");
+}
+
+
 /* phy_error:
  *
  * Moves the PHY to the HALTED state in response to a read
@@ -462,22 +475,44 @@ void phy_error(struct phy_device *phydev)
        spin_unlock(&phydev->lock);
 }
 
-static int phy_stop_interrupts(struct phy_device *phydev)
+/* phy_interrupt
+ *
+ * description: When a PHY interrupt occurs, the handler disables
+ * interrupts, and schedules a work task to clear the interrupt.
+ */
+static irqreturn_t phy_interrupt(int irq, void *phy_dat)
+{
+       struct phy_device *phydev = phy_dat;
+
+       /* The MDIO bus is not allowed to be written in interrupt
+        * context, so we need to disable the irq here.  A work
+        * queue will write the PHY to disable and clear the
+        * interrupt, and then reenable the irq line. */
+       disable_irq_nosync(irq);
+
+       schedule_work(&phydev->phy_queue);
+
+       return IRQ_HANDLED;
+}
+
+/* Enable the interrupts from the PHY side */
+int phy_enable_interrupts(struct phy_device *phydev)
 {
        int err;
 
-       err = phy_disable_interrupts(phydev);
+       err = phy_clear_interrupt(phydev);
 
-       if (err)
-               phy_error(phydev);
+       if (err < 0)
+               return err;
 
-       free_irq(phydev->irq, phydev);
+       err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
 
        return err;
 }
+EXPORT_SYMBOL(phy_enable_interrupts);
 
 /* Disable the PHY interrupts from the PHY side */
-static int phy_disable_interrupts(struct phy_device *phydev)
+int phy_disable_interrupts(struct phy_device *phydev)
 {
        int err;
 
@@ -500,6 +535,139 @@ phy_err:
 
        return err;
 }
+EXPORT_SYMBOL(phy_disable_interrupts);
+
+/* phy_start_interrupts
+ *
+ * description: Request the interrupt for the given PHY.  If
+ *   this fails, then we set irq to PHY_POLL.
+ *   Otherwise, we enable the interrupts in the PHY.
+ *   Returns 0 on success.
+ *   This should only be called with a valid IRQ number.
+ */
+int phy_start_interrupts(struct phy_device *phydev)
+{
+       int err = 0;
+
+       INIT_WORK(&phydev->phy_queue, phy_change);
+
+       if (request_irq(phydev->irq, phy_interrupt,
+                               IRQF_SHARED,
+                               "phy_interrupt",
+                               phydev) < 0) {
+               printk(KERN_WARNING "%s: Can't get IRQ %d (PHY)\n",
+                               phydev->bus->name,
+                               phydev->irq);
+               phydev->irq = PHY_POLL;
+               return 0;
+       }
+
+       err = phy_enable_interrupts(phydev);
+
+       return err;
+}
+EXPORT_SYMBOL(phy_start_interrupts);
+
+int phy_stop_interrupts(struct phy_device *phydev)
+{
+       int err;
+
+       err = phy_disable_interrupts(phydev);
+
+       if (err)
+               phy_error(phydev);
+
+       free_irq(phydev->irq, phydev);
+
+       return err;
+}
+EXPORT_SYMBOL(phy_stop_interrupts);
+
+
+/* Scheduled by the phy_interrupt/timer to handle PHY changes */
+static void phy_change(struct work_struct *work)
+{
+       int err;
+       struct phy_device *phydev =
+               container_of(work, struct phy_device, phy_queue);
+
+       err = phy_disable_interrupts(phydev);
+
+       if (err)
+               goto phy_err;
+
+       spin_lock(&phydev->lock);
+       if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
+               phydev->state = PHY_CHANGELINK;
+       spin_unlock(&phydev->lock);
+
+       enable_irq(phydev->irq);
+
+       /* Reenable interrupts */
+       err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
+
+       if (err)
+               goto irq_enable_err;
+
+       return;
+
+irq_enable_err:
+       disable_irq(phydev->irq);
+phy_err:
+       phy_error(phydev);
+}
+
+/* Bring down the PHY link, and stop checking the status. */
+void phy_stop(struct phy_device *phydev)
+{
+       spin_lock(&phydev->lock);
+
+       if (PHY_HALTED == phydev->state)
+               goto out_unlock;
+
+       if (phydev->irq != PHY_POLL) {
+               /* Clear any pending interrupts */
+               phy_clear_interrupt(phydev);
+
+               /* Disable PHY Interrupts */
+               phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
+       }
+
+       phydev->state = PHY_HALTED;
+
+out_unlock:
+       spin_unlock(&phydev->lock);
+}
+
+
+/* phy_start
+ *
+ * description: Indicates the attached device's readiness to
+ *   handle PHY-related work.  Used during startup to start the
+ *   PHY, and after a call to phy_stop() to resume operation.
+ *   Also used to indicate the MDIO bus has cleared an error
+ *   condition.
+ */
+void phy_start(struct phy_device *phydev)
+{
+       spin_lock(&phydev->lock);
+
+       switch (phydev->state) {
+               case PHY_STARTING:
+                       phydev->state = PHY_PENDING;
+                       break;
+               case PHY_READY:
+                       phydev->state = PHY_UP;
+                       break;
+               case PHY_HALTED:
+                       phydev->state = PHY_RESUMING;
+               default:
+                       break;
+       }
+       spin_unlock(&phydev->lock);
+}
+EXPORT_SYMBOL(phy_stop);
+EXPORT_SYMBOL(phy_start);
 
 /* PHY timer which handles the state machine */
 static void phy_timer(unsigned long data)