i2c-core: Erase pointer to clientdata on removal
[safe/jmp/linux-2.6] / drivers / i2c / i2c-core.c
index fdfaebd..b9306b1 100644 (file)
@@ -34,6 +34,7 @@
 #include <linux/hardirq.h>
 #include <linux/irqflags.h>
 #include <linux/rwsem.h>
+#include <linux/pm_runtime.h>
 #include <asm/uaccess.h>
 
 #include "i2c-core.h"
@@ -116,8 +117,10 @@ static int i2c_device_probe(struct device *dev)
        dev_dbg(dev, "probe\n");
 
        status = driver->probe(client, i2c_match_id(driver->id_table, client));
-       if (status)
+       if (status) {
                client->driver = NULL;
+               i2c_set_clientdata(client, NULL);
+       }
        return status;
 }
 
@@ -138,8 +141,10 @@ static int i2c_device_remove(struct device *dev)
                dev->driver = NULL;
                status = 0;
        }
-       if (status == 0)
+       if (status == 0) {
                client->driver = NULL;
+               i2c_set_clientdata(client, NULL);
+       }
        return status;
 }
 
@@ -155,6 +160,81 @@ static void i2c_device_shutdown(struct device *dev)
                driver->shutdown(client);
 }
 
+#ifdef CONFIG_SUSPEND
+static int i2c_device_pm_suspend(struct device *dev)
+{
+       const struct dev_pm_ops *pm;
+
+       if (!dev->driver)
+               return 0;
+       pm = dev->driver->pm;
+       if (!pm || !pm->suspend)
+               return 0;
+       return pm->suspend(dev);
+}
+
+static int i2c_device_pm_resume(struct device *dev)
+{
+       const struct dev_pm_ops *pm;
+
+       if (!dev->driver)
+               return 0;
+       pm = dev->driver->pm;
+       if (!pm || !pm->resume)
+               return 0;
+       return pm->resume(dev);
+}
+#else
+#define i2c_device_pm_suspend  NULL
+#define i2c_device_pm_resume   NULL
+#endif
+
+#ifdef CONFIG_PM_RUNTIME
+static int i2c_device_runtime_suspend(struct device *dev)
+{
+       const struct dev_pm_ops *pm;
+
+       if (!dev->driver)
+               return 0;
+       pm = dev->driver->pm;
+       if (!pm || !pm->runtime_suspend)
+               return 0;
+       return pm->runtime_suspend(dev);
+}
+
+static int i2c_device_runtime_resume(struct device *dev)
+{
+       const struct dev_pm_ops *pm;
+
+       if (!dev->driver)
+               return 0;
+       pm = dev->driver->pm;
+       if (!pm || !pm->runtime_resume)
+               return 0;
+       return pm->runtime_resume(dev);
+}
+
+static int i2c_device_runtime_idle(struct device *dev)
+{
+       const struct dev_pm_ops *pm = NULL;
+       int ret;
+
+       if (dev->driver)
+               pm = dev->driver->pm;
+       if (pm && pm->runtime_idle) {
+               ret = pm->runtime_idle(dev);
+               if (ret)
+                       return ret;
+       }
+
+       return pm_runtime_suspend(dev);
+}
+#else
+#define i2c_device_runtime_suspend     NULL
+#define i2c_device_runtime_resume      NULL
+#define i2c_device_runtime_idle                NULL
+#endif
+
 static int i2c_device_suspend(struct device *dev, pm_message_t mesg)
 {
        struct i2c_client *client = i2c_verify_client(dev);
@@ -219,6 +299,14 @@ static const struct attribute_group *i2c_dev_attr_groups[] = {
        NULL
 };
 
+static const struct dev_pm_ops i2c_device_pm_ops = {
+       .suspend = i2c_device_pm_suspend,
+       .resume = i2c_device_pm_resume,
+       .runtime_suspend = i2c_device_runtime_suspend,
+       .runtime_resume = i2c_device_runtime_resume,
+       .runtime_idle = i2c_device_runtime_idle,
+};
+
 struct bus_type i2c_bus_type = {
        .name           = "i2c",
        .match          = i2c_device_match,
@@ -227,6 +315,7 @@ struct bus_type i2c_bus_type = {
        .shutdown       = i2c_device_shutdown,
        .suspend        = i2c_device_suspend,
        .resume         = i2c_device_resume,
+       .pm             = &i2c_device_pm_ops,
 };
 EXPORT_SYMBOL_GPL(i2c_bus_type);
 
@@ -558,11 +647,9 @@ static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
        up_read(&__i2c_board_lock);
 }
 
-static int i2c_do_add_adapter(struct device_driver *d, void *data)
+static int i2c_do_add_adapter(struct i2c_driver *driver,
+                             struct i2c_adapter *adap)
 {
-       struct i2c_driver *driver = to_i2c_driver(d);
-       struct i2c_adapter *adap = data;
-
        /* Detect supported devices on that bus, and instantiate them */
        i2c_detect(adap, driver);
 
@@ -574,6 +661,11 @@ static int i2c_do_add_adapter(struct device_driver *d, void *data)
        return 0;
 }
 
+static int __process_new_adapter(struct device_driver *d, void *data)
+{
+       return i2c_do_add_adapter(to_i2c_driver(d), data);
+}
+
 static int i2c_register_adapter(struct i2c_adapter *adap)
 {
        int res = 0, dummy;
@@ -614,7 +706,7 @@ static int i2c_register_adapter(struct i2c_adapter *adap)
        /* Notify drivers */
        mutex_lock(&core_lock);
        dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
-                                i2c_do_add_adapter);
+                                __process_new_adapter);
        mutex_unlock(&core_lock);
 
        return 0;
@@ -715,10 +807,9 @@ retry:
 }
 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
 
-static int i2c_do_del_adapter(struct device_driver *d, void *data)
+static int i2c_do_del_adapter(struct i2c_driver *driver,
+                             struct i2c_adapter *adapter)
 {
-       struct i2c_driver *driver = to_i2c_driver(d);
-       struct i2c_adapter *adapter = data;
        struct i2c_client *client, *_n;
        int res;
 
@@ -750,6 +841,11 @@ static int __unregister_client(struct device *dev, void *dummy)
        return 0;
 }
 
+static int __process_removed_adapter(struct device_driver *d, void *data)
+{
+       return i2c_do_del_adapter(to_i2c_driver(d), data);
+}
+
 /**
  * i2c_del_adapter - unregister I2C adapter
  * @adap: the adapter being unregistered
@@ -777,7 +873,7 @@ int i2c_del_adapter(struct i2c_adapter *adap)
        /* Tell drivers about this removal */
        mutex_lock(&core_lock);
        res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
-                              i2c_do_del_adapter);
+                              __process_removed_adapter);
        mutex_unlock(&core_lock);
        if (res)
                return res;
@@ -801,6 +897,9 @@ int i2c_del_adapter(struct i2c_adapter *adap)
                                 adap->dev.parent);
 #endif
 
+       /* device name is gone after device_unregister */
+       dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
+
        /* clean up the sysfs representation */
        init_completion(&adap->dev_released);
        device_unregister(&adap->dev);
@@ -813,8 +912,6 @@ int i2c_del_adapter(struct i2c_adapter *adap)
        idr_remove(&i2c_adapter_idr, adap->nr);
        mutex_unlock(&core_lock);
 
-       dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
-
        /* Clear the device structure in case this adapter is ever going to be
           added again */
        memset(&adap->dev, 0, sizeof(adap->dev));
@@ -826,22 +923,11 @@ EXPORT_SYMBOL(i2c_del_adapter);
 
 /* ------------------------------------------------------------------------- */
 
-static int __attach_adapter(struct device *dev, void *data)
+static int __process_new_driver(struct device *dev, void *data)
 {
-       struct i2c_adapter *adapter;
-       struct i2c_driver *driver = data;
-
        if (dev->type != &i2c_adapter_type)
                return 0;
-       adapter = to_i2c_adapter(dev);
-
-       i2c_detect(adapter, driver);
-
-       /* Legacy drivers scan i2c busses directly */
-       if (driver->attach_adapter)
-               driver->attach_adapter(adapter);
-
-       return 0;
+       return i2c_do_add_adapter(data, to_i2c_adapter(dev));
 }
 
 /*
@@ -873,40 +959,18 @@ int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
        INIT_LIST_HEAD(&driver->clients);
        /* Walk the adapters that are already present */
        mutex_lock(&core_lock);
-       bus_for_each_dev(&i2c_bus_type, NULL, driver, __attach_adapter);
+       bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_new_driver);
        mutex_unlock(&core_lock);
 
        return 0;
 }
 EXPORT_SYMBOL(i2c_register_driver);
 
-static int __detach_adapter(struct device *dev, void *data)
+static int __process_removed_driver(struct device *dev, void *data)
 {
-       struct i2c_adapter *adapter;
-       struct i2c_driver *driver = data;
-       struct i2c_client *client, *_n;
-
        if (dev->type != &i2c_adapter_type)
                return 0;
-       adapter = to_i2c_adapter(dev);
-
-       /* Remove the devices we created ourselves as the result of hardware
-        * probing (using a driver's detect method) */
-       list_for_each_entry_safe(client, _n, &driver->clients, detected) {
-               dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
-                       client->name, client->addr);
-               list_del(&client->detected);
-               i2c_unregister_device(client);
-       }
-
-       if (driver->detach_adapter) {
-               if (driver->detach_adapter(adapter))
-                       dev_err(&adapter->dev,
-                               "detach_adapter failed for driver [%s]\n",
-                               driver->driver.name);
-       }
-
-       return 0;
+       return i2c_do_del_adapter(data, to_i2c_adapter(dev));
 }
 
 /**
@@ -917,7 +981,7 @@ static int __detach_adapter(struct device *dev, void *data)
 void i2c_del_driver(struct i2c_driver *driver)
 {
        mutex_lock(&core_lock);
-       bus_for_each_dev(&i2c_bus_type, NULL, driver, __detach_adapter);
+       bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_removed_driver);
        mutex_unlock(&core_lock);
 
        driver_unregister(&driver->driver);
@@ -1123,7 +1187,7 @@ EXPORT_SYMBOL(i2c_transfer);
  * i2c_master_send - issue a single I2C message in master transmit mode
  * @client: Handle to slave device
  * @buf: Data that will be written to the slave
- * @count: How many bytes to write
+ * @count: How many bytes to write, must be less than 64k since msg.len is u16
  *
  * Returns negative errno, or else the number of bytes written.
  */
@@ -1150,7 +1214,7 @@ EXPORT_SYMBOL(i2c_master_send);
  * i2c_master_recv - issue a single I2C message in master receive mode
  * @client: Handle to slave device
  * @buf: Where to store data read from slave
- * @count: How many bytes to read
+ * @count: How many bytes to read, must be less than 64k since msg.len is u16
  *
  * Returns negative errno, or else the number of bytes read.
  */
@@ -1180,7 +1244,7 @@ EXPORT_SYMBOL(i2c_master_recv);
  * ----------------------------------------------------
  */
 
-static int i2c_detect_address(struct i2c_client *temp_client, int kind,
+static int i2c_detect_address(struct i2c_client *temp_client,
                              struct i2c_driver *driver)
 {
        struct i2c_board_info info;
@@ -1199,22 +1263,18 @@ static int i2c_detect_address(struct i2c_client *temp_client, int kind,
        if (i2c_check_addr(adapter, addr))
                return 0;
 
-       /* Make sure there is something at this address, unless forced */
-       if (kind < 0) {
-               if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
-                                  I2C_SMBUS_QUICK, NULL) < 0)
-                       return 0;
+       /* Make sure there is something at this address */
+       if (i2c_smbus_xfer(adapter, addr, 0, 0, 0, I2C_SMBUS_QUICK, NULL) < 0)
+               return 0;
 
-               /* prevent 24RF08 corruption */
-               if ((addr & ~0x0f) == 0x50)
-                       i2c_smbus_xfer(adapter, addr, 0, 0, 0,
-                                      I2C_SMBUS_QUICK, NULL);
-       }
+       /* Prevent 24RF08 corruption */
+       if ((addr & ~0x0f) == 0x50)
+               i2c_smbus_xfer(adapter, addr, 0, 0, 0, I2C_SMBUS_QUICK, NULL);
 
        /* Finally call the custom detection function */
        memset(&info, 0, sizeof(struct i2c_board_info));
        info.addr = addr;
-       err = driver->detect(temp_client, kind, &info);
+       err = driver->detect(temp_client, &info);
        if (err) {
                /* -ENODEV is returned if the detection fails. We catch it
                   here as this isn't an error. */
@@ -1244,13 +1304,13 @@ static int i2c_detect_address(struct i2c_client *temp_client, int kind,
 
 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
 {
-       const struct i2c_client_address_data *address_data;
+       const unsigned short *address_list;
        struct i2c_client *temp_client;
        int i, err = 0;
        int adap_id = i2c_adapter_id(adapter);
 
-       address_data = driver->address_data;
-       if (!driver->detect || !address_data)
+       address_list = driver->address_list;
+       if (!driver->detect || !address_list)
                return 0;
 
        /* Set up a temporary client to help detect callback */
@@ -1265,7 +1325,7 @@ static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
 
        /* Stop here if we can't use SMBUS_QUICK */
        if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
-               if (address_data->normal_i2c[0] == I2C_CLIENT_END)
+               if (address_list[0] == I2C_CLIENT_END)
                        goto exit_free;
 
                dev_warn(&adapter->dev, "SMBus Quick command not supported, "
@@ -1274,12 +1334,11 @@ static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
                goto exit_free;
        }
 
-       for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
+       for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
                dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
-                       "addr 0x%02x\n", adap_id,
-                       address_data->normal_i2c[i]);
-               temp_client->addr = address_data->normal_i2c[i];
-               err = i2c_detect_address(temp_client, -1, driver);
+                       "addr 0x%02x\n", adap_id, address_list[i]);
+               temp_client->addr = address_list[i];
+               err = i2c_detect_address(temp_client, driver);
                if (err)
                        goto exit_free;
        }