]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/net/phy/phy.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-2.6-omap-h63xx.git] / drivers / net / phy / phy.c
index cb230f44d6fc3418cd899c42a76552a0091e9ee4..3c18bb594957a3e2e0320274ff88897189d8b594 100644 (file)
@@ -7,7 +7,7 @@
  * Author: Andy Fleming
  *
  * Copyright (c) 2004 Freescale Semiconductor, Inc.
- * Copyright (c) 2006  Maciej W. Rozycki
+ * Copyright (c) 2006, 2007  Maciej W. Rozycki
  *
  * This program is free software; you can redistribute  it and/or modify it
  * under  the terms of  the GNU General  Public License as published by the
@@ -26,7 +26,6 @@
 #include <linux/netdevice.h>
 #include <linux/etherdevice.h>
 #include <linux/skbuff.h>
-#include <linux/spinlock.h>
 #include <linux/mm.h>
 #include <linux/module.h>
 #include <linux/mii.h>
@@ -35,6 +34,7 @@
 #include <linux/timer.h>
 #include <linux/workqueue.h>
 
+#include <asm/atomic.h>
 #include <asm/io.h>
 #include <asm/irq.h>
 #include <asm/uaccess.h>
@@ -71,9 +71,11 @@ int phy_read(struct phy_device *phydev, u16 regnum)
        int retval;
        struct mii_bus *bus = phydev->bus;
 
-       spin_lock_bh(&bus->mdio_lock);
+       BUG_ON(in_interrupt());
+
+       mutex_lock(&bus->mdio_lock);
        retval = bus->read(bus, phydev->addr, regnum);
-       spin_unlock_bh(&bus->mdio_lock);
+       mutex_unlock(&bus->mdio_lock);
 
        return retval;
 }
@@ -94,9 +96,11 @@ int phy_write(struct phy_device *phydev, u16 regnum, u16 val)
        int err;
        struct mii_bus *bus = phydev->bus;
 
-       spin_lock_bh(&bus->mdio_lock);
+       BUG_ON(in_interrupt());
+
+       mutex_lock(&bus->mdio_lock);
        err = bus->write(bus, phydev->addr, regnum, val);
-       spin_unlock_bh(&bus->mdio_lock);
+       mutex_unlock(&bus->mdio_lock);
 
        return err;
 }
@@ -204,7 +208,7 @@ static const struct phy_setting settings[] = {
        },
 };
 
-#define MAX_NUM_SETTINGS (sizeof(settings)/sizeof(struct phy_setting))
+#define MAX_NUM_SETTINGS ARRAY_SIZE(settings)
 
 /**
  * phy_find_setting - find a PHY settings array entry that matches speed & duplex
@@ -402,9 +406,14 @@ int phy_mii_ioctl(struct phy_device *phydev,
                
                if (mii_data->reg_num == MII_BMCR 
                                && val & BMCR_RESET
-                               && phydev->drv->config_init)
+                               && phydev->drv->config_init) {
+                       phy_scan_fixups(phydev);
                        phydev->drv->config_init(phydev);
+               }
                break;
+
+       default:
+               return -ENOTTY;
        }
 
        return 0;
@@ -424,7 +433,7 @@ int phy_start_aneg(struct phy_device *phydev)
 {
        int err;
 
-       spin_lock(&phydev->lock);
+       mutex_lock(&phydev->lock);
 
        if (AUTONEG_DISABLE == phydev->autoneg)
                phy_sanitize_settings(phydev);
@@ -445,13 +454,14 @@ int phy_start_aneg(struct phy_device *phydev)
        }
 
 out_unlock:
-       spin_unlock(&phydev->lock);
+       mutex_unlock(&phydev->lock);
        return err;
 }
 EXPORT_SYMBOL(phy_start_aneg);
 
 
 static void phy_change(struct work_struct *work);
+static void phy_state_machine(struct work_struct *work);
 static void phy_timer(unsigned long data);
 
 /**
@@ -472,6 +482,7 @@ void phy_start_machine(struct phy_device *phydev,
 {
        phydev->adjust_state = handler;
 
+       INIT_WORK(&phydev->state_queue, phy_state_machine);
        init_timer(&phydev->phy_timer);
        phydev->phy_timer.function = &phy_timer;
        phydev->phy_timer.data = (unsigned long) phydev;
@@ -489,11 +500,12 @@ void phy_start_machine(struct phy_device *phydev,
 void phy_stop_machine(struct phy_device *phydev)
 {
        del_timer_sync(&phydev->phy_timer);
+       cancel_work_sync(&phydev->state_queue);
 
-       spin_lock(&phydev->lock);
+       mutex_lock(&phydev->lock);
        if (phydev->state > PHY_UP)
                phydev->state = PHY_UP;
-       spin_unlock(&phydev->lock);
+       mutex_unlock(&phydev->lock);
 
        phydev->adjust_state = NULL;
 }
@@ -537,9 +549,9 @@ static void phy_force_reduction(struct phy_device *phydev)
  */
 void phy_error(struct phy_device *phydev)
 {
-       spin_lock(&phydev->lock);
+       mutex_lock(&phydev->lock);
        phydev->state = PHY_HALTED;
-       spin_unlock(&phydev->lock);
+       mutex_unlock(&phydev->lock);
 }
 
 /**
@@ -562,6 +574,7 @@ static irqreturn_t phy_interrupt(int irq, void *phy_dat)
         * queue will write the PHY to disable and clear the
         * interrupt, and then reenable the irq line. */
        disable_irq_nosync(irq);
+       atomic_inc(&phydev->irq_disable);
 
        schedule_work(&phydev->phy_queue);
 
@@ -632,6 +645,7 @@ int phy_start_interrupts(struct phy_device *phydev)
 
        INIT_WORK(&phydev->phy_queue, phy_change);
 
+       atomic_set(&phydev->irq_disable, 0);
        if (request_irq(phydev->irq, phy_interrupt,
                                IRQF_SHARED,
                                "phy_interrupt",
@@ -662,13 +676,22 @@ int phy_stop_interrupts(struct phy_device *phydev)
        if (err)
                phy_error(phydev);
 
+       free_irq(phydev->irq, phydev);
+
        /*
-        * Finish any pending work; we might have been scheduled to be called
-        * from keventd ourselves, but cancel_work_sync() handles that.
+        * Cannot call flush_scheduled_work() here as desired because
+        * of rtnl_lock(), but we do not really care about what would
+        * be done, except from enable_irq(), so cancel any work
+        * possibly pending and take care of the matter below.
         */
        cancel_work_sync(&phydev->phy_queue);
-
-       free_irq(phydev->irq, phydev);
+       /*
+        * If work indeed has been cancelled, disable_irq() will have
+        * been left unbalanced from phy_interrupt() and enable_irq()
+        * has to be called so that other devices on the line work.
+        */
+       while (atomic_dec_return(&phydev->irq_disable) >= 0)
+               enable_irq(phydev->irq);
 
        return err;
 }
@@ -690,11 +713,12 @@ static void phy_change(struct work_struct *work)
        if (err)
                goto phy_err;
 
-       spin_lock(&phydev->lock);
+       mutex_lock(&phydev->lock);
        if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
                phydev->state = PHY_CHANGELINK;
-       spin_unlock(&phydev->lock);
+       mutex_unlock(&phydev->lock);
 
+       atomic_dec(&phydev->irq_disable);
        enable_irq(phydev->irq);
 
        /* Reenable interrupts */
@@ -708,6 +732,7 @@ static void phy_change(struct work_struct *work)
 
 irq_enable_err:
        disable_irq(phydev->irq);
+       atomic_inc(&phydev->irq_disable);
 phy_err:
        phy_error(phydev);
 }
@@ -718,13 +743,11 @@ phy_err:
  */
 void phy_stop(struct phy_device *phydev)
 {
-       spin_lock(&phydev->lock);
+       mutex_lock(&phydev->lock);
 
        if (PHY_HALTED == phydev->state)
                goto out_unlock;
 
-       phydev->state = PHY_HALTED;
-
        if (phydev->irq != PHY_POLL) {
                /* Disable PHY Interrupts */
                phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
@@ -733,8 +756,10 @@ void phy_stop(struct phy_device *phydev)
                phy_clear_interrupt(phydev);
        }
 
+       phydev->state = PHY_HALTED;
+
 out_unlock:
-       spin_unlock(&phydev->lock);
+       mutex_unlock(&phydev->lock);
 
        /*
         * Cannot call flush_scheduled_work() here as desired because
@@ -756,7 +781,7 @@ out_unlock:
  */
 void phy_start(struct phy_device *phydev)
 {
-       spin_lock_bh(&phydev->lock);
+       mutex_lock(&phydev->lock);
 
        switch (phydev->state) {
                case PHY_STARTING:
@@ -770,19 +795,26 @@ void phy_start(struct phy_device *phydev)
                default:
                        break;
        }
-       spin_unlock_bh(&phydev->lock);
+       mutex_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)
+/**
+ * phy_state_machine - Handle the state machine
+ * @work: work_struct that describes the work to be done
+ *
+ * Description: Scheduled by the state_queue workqueue each time
+ *   phy_timer is triggered.
+ */
+static void phy_state_machine(struct work_struct *work)
 {
-       struct phy_device *phydev = (struct phy_device *)data;
+       struct phy_device *phydev =
+                       container_of(work, struct phy_device, state_queue);
        int needs_aneg = 0;
        int err = 0;
 
-       spin_lock(&phydev->lock);
+       mutex_lock(&phydev->lock);
 
        if (phydev->adjust_state)
                phydev->adjust_state(phydev->attached_dev);
@@ -948,7 +980,7 @@ static void phy_timer(unsigned long data)
                        break;
        }
 
-       spin_unlock(&phydev->lock);
+       mutex_unlock(&phydev->lock);
 
        if (needs_aneg)
                err = phy_start_aneg(phydev);
@@ -959,3 +991,14 @@ static void phy_timer(unsigned long data)
        mod_timer(&phydev->phy_timer, jiffies + PHY_STATE_TIME * HZ);
 }
 
+/* PHY timer which schedules the state machine work */
+static void phy_timer(unsigned long data)
+{
+       struct phy_device *phydev = (struct phy_device *)data;
+
+       /*
+        * PHY I/O operations can potentially sleep so we ensure that
+        * it's done from a process context
+        */
+       schedule_work(&phydev->state_queue);
+}