nfsd4: fix null dereference creating nfsv4 callback client
[safe/jmp/linux-2.6] / drivers / i2c / i2c-core.c
index 06c428b..0e45c29 100644 (file)
 #include <linux/completion.h>
 #include <linux/hardirq.h>
 #include <linux/irqflags.h>
+#include <linux/rwsem.h>
 #include <asm/uaccess.h>
 
 #include "i2c-core.h"
 
 
+/* core_lock protects i2c_adapter_idr, userspace_devices, and guarantees
+   that device detection, deletion of detected devices, and attach_adapter
+   and detach_adapter calls are serialized */
 static DEFINE_MUTEX(core_lock);
 static DEFINE_IDR(i2c_adapter_idr);
-
-#define is_newstyle_driver(d) ((d)->probe || (d)->remove || (d)->detect)
+static LIST_HEAD(userspace_devices);
 
 static int i2c_check_addr(struct i2c_adapter *adapter, int addr);
 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
@@ -64,12 +67,6 @@ static int i2c_device_match(struct device *dev, struct device_driver *drv)
        struct i2c_client       *client = to_i2c_client(dev);
        struct i2c_driver       *driver = to_i2c_driver(drv);
 
-       /* make legacy i2c drivers bypass driver model probing entirely;
-        * such drivers scan each i2c adapter/bus themselves.
-        */
-       if (!is_newstyle_driver(driver))
-               return 0;
-
        /* match on an id table if there is one */
        if (driver->id_table)
                return i2c_match_id(driver->id_table, client) != NULL;
@@ -172,12 +169,6 @@ static int i2c_device_resume(struct device *dev)
        return driver->resume(to_i2c_client(dev));
 }
 
-static void i2c_client_release(struct device *dev)
-{
-       struct i2c_client *client = to_i2c_client(dev);
-       complete(&client->released);
-}
-
 static void i2c_client_dev_release(struct device *dev)
 {
        kfree(to_i2c_client(dev));
@@ -282,12 +273,7 @@ i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
 
        client->dev.parent = &client->adapter->dev;
        client->dev.bus = &i2c_bus_type;
-
-       if (client->driver && !is_newstyle_driver(client->driver)) {
-               client->dev.release = i2c_client_release;
-               dev_set_uevent_suppress(&client->dev, 1);
-       } else
-               client->dev.release = i2c_client_dev_release;
+       client->dev.release = i2c_client_dev_release;
 
        dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
                     client->addr);
@@ -295,10 +281,6 @@ i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
        if (status)
                goto out_err;
 
-       mutex_lock(&adap->clist_lock);
-       list_add_tail(&client->list, &adap->clients);
-       mutex_unlock(&adap->clist_lock);
-
        dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
                client->name, dev_name(&client->dev));
 
@@ -320,20 +302,6 @@ EXPORT_SYMBOL_GPL(i2c_new_device);
  */
 void i2c_unregister_device(struct i2c_client *client)
 {
-       struct i2c_adapter      *adapter = client->adapter;
-       struct i2c_driver       *driver = client->driver;
-
-       if (driver && !is_newstyle_driver(driver)) {
-               dev_err(&client->dev, "can't unregister devices "
-                       "with legacy drivers\n");
-               WARN_ON(1);
-               return;
-       }
-
-       mutex_lock(&adapter->clist_lock);
-       list_del(&client->list);
-       mutex_unlock(&adapter->clist_lock);
-
        device_unregister(&client->dev);
 }
 EXPORT_SYMBOL_GPL(i2c_unregister_device);
@@ -407,8 +375,128 @@ show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
        return sprintf(buf, "%s\n", adap->name);
 }
 
+/*
+ * Let users instantiate I2C devices through sysfs. This can be used when
+ * platform initialization code doesn't contain the proper data for
+ * whatever reason. Also useful for drivers that do device detection and
+ * detection fails, either because the device uses an unexpected address,
+ * or this is a compatible device with different ID register values.
+ *
+ * Parameter checking may look overzealous, but we really don't want
+ * the user to provide incorrect parameters.
+ */
+static ssize_t
+i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
+                    const char *buf, size_t count)
+{
+       struct i2c_adapter *adap = to_i2c_adapter(dev);
+       struct i2c_board_info info;
+       struct i2c_client *client;
+       char *blank, end;
+       int res;
+
+       dev_warn(dev, "The new_device interface is still experimental "
+                "and may change in a near future\n");
+       memset(&info, 0, sizeof(struct i2c_board_info));
+
+       blank = strchr(buf, ' ');
+       if (!blank) {
+               dev_err(dev, "%s: Missing parameters\n", "new_device");
+               return -EINVAL;
+       }
+       if (blank - buf > I2C_NAME_SIZE - 1) {
+               dev_err(dev, "%s: Invalid device name\n", "new_device");
+               return -EINVAL;
+       }
+       memcpy(info.type, buf, blank - buf);
+
+       /* Parse remaining parameters, reject extra parameters */
+       res = sscanf(++blank, "%hi%c", &info.addr, &end);
+       if (res < 1) {
+               dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
+               return -EINVAL;
+       }
+       if (res > 1  && end != '\n') {
+               dev_err(dev, "%s: Extra parameters\n", "new_device");
+               return -EINVAL;
+       }
+
+       if (info.addr < 0x03 || info.addr > 0x77) {
+               dev_err(dev, "%s: Invalid I2C address 0x%hx\n", "new_device",
+                       info.addr);
+               return -EINVAL;
+       }
+
+       client = i2c_new_device(adap, &info);
+       if (!client)
+               return -EEXIST;
+
+       /* Keep track of the added device */
+       mutex_lock(&core_lock);
+       list_add_tail(&client->detected, &userspace_devices);
+       mutex_unlock(&core_lock);
+       dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
+                info.type, info.addr);
+
+       return count;
+}
+
+/*
+ * And of course let the users delete the devices they instantiated, if
+ * they got it wrong. This interface can only be used to delete devices
+ * instantiated by i2c_sysfs_new_device above. This guarantees that we
+ * don't delete devices to which some kernel code still has references.
+ *
+ * Parameter checking may look overzealous, but we really don't want
+ * the user to delete the wrong device.
+ */
+static ssize_t
+i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
+                       const char *buf, size_t count)
+{
+       struct i2c_adapter *adap = to_i2c_adapter(dev);
+       struct i2c_client *client, *next;
+       unsigned short addr;
+       char end;
+       int res;
+
+       /* Parse parameters, reject extra parameters */
+       res = sscanf(buf, "%hi%c", &addr, &end);
+       if (res < 1) {
+               dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
+               return -EINVAL;
+       }
+       if (res > 1  && end != '\n') {
+               dev_err(dev, "%s: Extra parameters\n", "delete_device");
+               return -EINVAL;
+       }
+
+       /* Make sure the device was added through sysfs */
+       res = -ENOENT;
+       mutex_lock(&core_lock);
+       list_for_each_entry_safe(client, next, &userspace_devices, detected) {
+               if (client->addr == addr && client->adapter == adap) {
+                       dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
+                                "delete_device", client->name, client->addr);
+
+                       list_del(&client->detected);
+                       i2c_unregister_device(client);
+                       res = count;
+                       break;
+               }
+       }
+       mutex_unlock(&core_lock);
+
+       if (res < 0)
+               dev_err(dev, "%s: Can't find device in list\n",
+                       "delete_device");
+       return res;
+}
+
 static struct device_attribute i2c_adapter_attrs[] = {
        __ATTR(name, S_IRUGO, show_adapter_name, NULL),
+       __ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device),
+       __ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device),
        { },
 };
 
@@ -422,7 +510,7 @@ static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
 {
        struct i2c_devinfo      *devinfo;
 
-       mutex_lock(&__i2c_board_lock);
+       down_read(&__i2c_board_lock);
        list_for_each_entry(devinfo, &__i2c_board_list, list) {
                if (devinfo->busnum == adapter->nr
                                && !i2c_new_device(adapter,
@@ -431,7 +519,7 @@ static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
                                "Can't create device at 0x%02x\n",
                                devinfo->board_info.addr);
        }
-       mutex_unlock(&__i2c_board_lock);
+       up_read(&__i2c_board_lock);
 }
 
 static int i2c_do_add_adapter(struct device_driver *d, void *data)
@@ -455,14 +543,12 @@ static int i2c_register_adapter(struct i2c_adapter *adap)
        int res = 0, dummy;
 
        /* Can't register until after driver model init */
-       if (unlikely(WARN_ON(!i2c_bus_type.p)))
-               return -EAGAIN;
+       if (unlikely(WARN_ON(!i2c_bus_type.p))) {
+               res = -EAGAIN;
+               goto out_list;
+       }
 
        mutex_init(&adap->bus_lock);
-       mutex_init(&adap->clist_lock);
-       INIT_LIST_HEAD(&adap->clients);
-
-       mutex_lock(&core_lock);
 
        /* Set default timeout to 1 second if not already set */
        if (adap->timeout == 0)
@@ -482,16 +568,18 @@ static int i2c_register_adapter(struct i2c_adapter *adap)
                i2c_scan_static_board_info(adap);
 
        /* Notify drivers */
+       mutex_lock(&core_lock);
        dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
                                 i2c_do_add_adapter);
-
-out_unlock:
        mutex_unlock(&core_lock);
-       return res;
+
+       return 0;
 
 out_list:
+       mutex_lock(&core_lock);
        idr_remove(&i2c_adapter_idr, adap->nr);
-       goto out_unlock;
+       mutex_unlock(&core_lock);
+       return res;
 }
 
 /**
@@ -610,6 +698,14 @@ static int i2c_do_del_adapter(struct device_driver *d, void *data)
        return res;
 }
 
+static int __unregister_client(struct device *dev, void *dummy)
+{
+       struct i2c_client *client = i2c_verify_client(dev);
+       if (client)
+               i2c_unregister_device(client);
+       return 0;
+}
+
 /**
  * i2c_del_adapter - unregister I2C adapter
  * @adap: the adapter being unregistered
@@ -620,29 +716,30 @@ static int i2c_do_del_adapter(struct device_driver *d, void *data)
  */
 int i2c_del_adapter(struct i2c_adapter *adap)
 {
-       struct i2c_client *client, *_n;
        int res = 0;
-
-       mutex_lock(&core_lock);
+       struct i2c_adapter *found;
 
        /* First make sure that this adapter was ever added */
-       if (idr_find(&i2c_adapter_idr, adap->nr) != adap) {
+       mutex_lock(&core_lock);
+       found = idr_find(&i2c_adapter_idr, adap->nr);
+       mutex_unlock(&core_lock);
+       if (found != adap) {
                pr_debug("i2c-core: attempting to delete unregistered "
                         "adapter [%s]\n", adap->name);
-               res = -EINVAL;
-               goto out_unlock;
+               return -EINVAL;
        }
 
        /* Tell drivers about this removal */
+       mutex_lock(&core_lock);
        res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
                               i2c_do_del_adapter);
+       mutex_unlock(&core_lock);
        if (res)
-               goto out_unlock;
+               return res;
 
-       /* Detach any active clients */
-       list_for_each_entry_safe_reverse(client, _n, &adap->clients, list) {
-               i2c_unregister_device(client);
-       }
+       /* Detach any active clients. This can't fail, thus we do not
+          checking the returned value. */
+       res = device_for_each_child(&adap->dev, NULL, __unregister_client);
 
        /* clean up the sysfs representation */
        init_completion(&adap->dev_released);
@@ -652,7 +749,9 @@ int i2c_del_adapter(struct i2c_adapter *adap)
        wait_for_completion(&adap->dev_released);
 
        /* free bus id */
+       mutex_lock(&core_lock);
        idr_remove(&i2c_adapter_idr, adap->nr);
+       mutex_unlock(&core_lock);
 
        dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
 
@@ -660,9 +759,7 @@ int i2c_del_adapter(struct i2c_adapter *adap)
           added again */
        memset(&adap->dev, 0, sizeof(adap->dev));
 
- out_unlock:
-       mutex_unlock(&core_lock);
-       return res;
+       return 0;
 }
 EXPORT_SYMBOL(i2c_del_adapter);
 
@@ -707,16 +804,15 @@ int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
        if (res)
                return res;
 
-       mutex_lock(&core_lock);
-
        pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
 
        INIT_LIST_HEAD(&driver->clients);
        /* Walk the adapters that are already present */
+       mutex_lock(&core_lock);
        class_for_each_device(&i2c_adapter_class, NULL, driver,
                              __attach_adapter);
-
        mutex_unlock(&core_lock);
+
        return 0;
 }
 EXPORT_SYMBOL(i2c_register_driver);
@@ -736,9 +832,6 @@ static int __detach_adapter(struct device *dev, void *data)
                i2c_unregister_device(client);
        }
 
-       if (is_newstyle_driver(driver))
-               return 0;
-
        if (driver->detach_adapter) {
                if (driver->detach_adapter(adapter))
                        dev_err(&adapter->dev,
@@ -757,14 +850,12 @@ static int __detach_adapter(struct device *dev, void *data)
 void i2c_del_driver(struct i2c_driver *driver)
 {
        mutex_lock(&core_lock);
-
        class_for_each_device(&i2c_adapter_class, NULL, driver,
                              __detach_adapter);
+       mutex_unlock(&core_lock);
 
        driver_unregister(&driver->driver);
        pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
-
-       mutex_unlock(&core_lock);
 }
 EXPORT_SYMBOL(i2c_del_driver);