]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/base/core.c
Driver core: Call device_pm_add() after bus_add_device() in device_add()
[linux-2.6-omap-h63xx.git] / drivers / base / core.c
index e88170293ca0dfae01905abd9edc67a60ecc096b..adbc01788447c899b9f105ccd8aa67c01554b789 100644 (file)
 #include <linux/kdev_t.h>
 #include <linux/notifier.h>
 #include <linux/genhd.h>
+#include <linux/kallsyms.h>
 #include <asm/semaphore.h>
 
 #include "base.h"
 #include "power/power.h"
 
-int (*platform_notify)(struct device * dev) = NULL;
-int (*platform_notify_remove)(struct device * dev) = NULL;
+int (*platform_notify)(struct device *dev) = NULL;
+int (*platform_notify_remove)(struct device *dev) = NULL;
 
-/*
- * sysfs bindings for devices.
- */
+#ifdef CONFIG_BLOCK
+static inline int device_is_not_partition(struct device *dev)
+{
+       return !(dev->type == &part_type);
+}
+#else
+static inline int device_is_not_partition(struct device *dev)
+{
+       return 1;
+}
+#endif
 
 /**
  * dev_driver_string - Return a device's driver name, if at all possible
@@ -51,24 +60,27 @@ EXPORT_SYMBOL(dev_driver_string);
 #define to_dev(obj) container_of(obj, struct device, kobj)
 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
 
-static ssize_t
-dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
+static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
+                            char *buf)
 {
-       struct device_attribute * dev_attr = to_dev_attr(attr);
-       struct device * dev = to_dev(kobj);
+       struct device_attribute *dev_attr = to_dev_attr(attr);
+       struct device *dev = to_dev(kobj);
        ssize_t ret = -EIO;
 
        if (dev_attr->show)
                ret = dev_attr->show(dev, dev_attr, buf);
+       if (ret >= (ssize_t)PAGE_SIZE) {
+               print_symbol("dev_attr_show: %s returned bad count\n",
+                               (unsigned long)dev_attr->show);
+       }
        return ret;
 }
 
-static ssize_t
-dev_attr_store(struct kobject * kobj, struct attribute * attr,
-              const char * buf, size_t count)
+static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
+                             const char *buf, size_t count)
 {
-       struct device_attribute * dev_attr = to_dev_attr(attr);
-       struct device * dev = to_dev(kobj);
+       struct device_attribute *dev_attr = to_dev_attr(attr);
+       struct device *dev = to_dev(kobj);
        ssize_t ret = -EIO;
 
        if (dev_attr->store)
@@ -90,9 +102,9 @@ static struct sysfs_ops dev_sysfs_ops = {
  *     reaches 0. We forward the call to the device's release
  *     method, which should handle actually freeing the structure.
  */
-static void device_release(struct kobject * kobj)
+static void device_release(struct kobject *kobj)
 {
-       struct device * dev = to_dev(kobj);
+       struct device *dev = to_dev(kobj);
 
        if (dev->release)
                dev->release(dev);
@@ -101,8 +113,8 @@ static void device_release(struct kobject * kobj)
        else if (dev->class && dev->class->dev_release)
                dev->class->dev_release(dev);
        else {
-               printk(KERN_ERR "Device '%s' does not have a release() function, "
-                       "it is broken and must be fixed.\n",
+               printk(KERN_ERR "Device '%s' does not have a release() "
+                       "function, it is broken and must be fixed.\n",
                        dev->bus_id);
                WARN_ON(1);
        }
@@ -185,7 +197,8 @@ static int dev_uevent(struct kset *kset, struct kobject *kobj,
                add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
 
                if (dev->driver)
-                       add_uevent_var(env, "PHYSDEVDRIVER=%s", dev->driver->name);
+                       add_uevent_var(env, "PHYSDEVDRIVER=%s",
+                                      dev->driver->name);
        }
 #endif
 
@@ -327,7 +340,8 @@ static int device_add_groups(struct device *dev,
                        error = sysfs_create_group(&dev->kobj, groups[i]);
                        if (error) {
                                while (--i >= 0)
-                                       sysfs_remove_group(&dev->kobj, groups[i]);
+                                       sysfs_remove_group(&dev->kobj,
+                                                          groups[i]);
                                break;
                        }
                }
@@ -406,35 +420,28 @@ static struct device_attribute devt_attr =
 /* kset to create /sys/devices/  */
 struct kset *devices_kset;
 
-
 /**
- *     device_create_file - create sysfs attribute file for device.
- *     @dev:   device.
- *     @attr:  device attribute descriptor.
+ * device_create_file - create sysfs attribute file for device.
+ * @dev: device.
+ * @attr: device attribute descriptor.
  */
-
-int device_create_file(struct device * dev, struct device_attribute * attr)
+int device_create_file(struct device *dev, struct device_attribute *attr)
 {
        int error = 0;
-       if (get_device(dev)) {
+       if (dev)
                error = sysfs_create_file(&dev->kobj, &attr->attr);
-               put_device(dev);
-       }
        return error;
 }
 
 /**
- *     device_remove_file - remove sysfs attribute file.
- *     @dev:   device.
- *     @attr:  device attribute descriptor.
+ * device_remove_file - remove sysfs attribute file.
+ * @dev: device.
+ * @attr: device attribute descriptor.
  */
-
-void device_remove_file(struct device * dev, struct device_attribute * attr)
+void device_remove_file(struct device *dev, struct device_attribute *attr)
 {
-       if (get_device(dev)) {
+       if (dev)
                sysfs_remove_file(&dev->kobj, &attr->attr);
-               put_device(dev);
-       }
 }
 
 /**
@@ -510,22 +517,20 @@ static void klist_children_put(struct klist_node *n)
        put_device(dev);
 }
 
-
 /**
- *     device_initialize - init device structure.
- *     @dev:   device.
+ * device_initialize - init device structure.
+ * @dev: device.
  *
- *     This prepares the device for use by other layers,
- *     including adding it to the device hierarchy.
- *     It is the first half of device_register(), if called by
- *     that, though it can also be called separately, so one
- *     may use @dev's fields (e.g. the refcount).
+ * This prepares the device for use by other layers,
+ * including adding it to the device hierarchy.
+ * It is the first half of device_register(), if called by
+ * that, though it can also be called separately, so one
+ * may use @dev's fields (e.g. the refcount).
  */
-
 void device_initialize(struct device *dev)
 {
        dev->kobj.kset = devices_kset;
-       kobject_init_ng(&dev->kobj, &device_ktype);
+       kobject_init(&dev->kobj, &device_ktype);
        klist_init(&dev->klist_children, klist_children_get,
                   klist_children_put);
        INIT_LIST_HEAD(&dev->dma_pools);
@@ -552,6 +557,8 @@ static struct kobject *get_device_parent(struct device *dev,
 }
 
 static inline void cleanup_device_parent(struct device *dev) {}
+static inline void cleanup_glue_dir(struct device *dev,
+                                   struct kobject *glue_dir) {}
 #else
 static struct kobject *virtual_device_parent(struct device *dev)
 {
@@ -576,8 +583,8 @@ static struct kobject *get_device_parent(struct device *dev,
 
                /*
                 * If we have no parent, we live in "virtual".
-                * Class-devices with a bus-device as parent, live
-                * in a class-directory to prevent namespace collisions.
+                * Class-devices with a non class-device as parent, live
+                * in a "glue" directory to prevent namespace collisions.
                 */
                if (parent == NULL)
                        parent_kobj = virtual_device_parent(dev);
@@ -607,8 +614,7 @@ static struct kobject *get_device_parent(struct device *dev,
                        kobject_put(k);
                        return NULL;
                }
-               /* Do not emit a uevent, as it's not needed for this
-                * "class glue" directory. */
+               /* do not emit an uevent for this simple "glue" directory */
                return k;
        }
 
@@ -617,44 +623,28 @@ static struct kobject *get_device_parent(struct device *dev,
        return NULL;
 }
 
-static void cleanup_device_parent(struct device *dev)
+static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
 {
-       struct device *d;
-       int other = 0;
-
-       if (!dev->class)
+       /* see if we live in a "glue" directory */
+       if (!glue_dir || !dev->class ||
+           glue_dir->kset != &dev->class->class_dirs)
                return;
 
-       /* see if we live in a parent class directory */
-       if (dev->kobj.parent->kset != &dev->class->class_dirs)
-               return;
+       kobject_put(glue_dir);
+}
 
-       /* if we are the last child of our class, delete the directory */
-       down(&dev->class->sem);
-       list_for_each_entry(d, &dev->class->devices, node) {
-               if (d == dev)
-                       continue;
-               if (d->kobj.parent == dev->kobj.parent) {
-                       other = 1;
-                       break;
-               }
-       }
-       if (!other)
-               kobject_del(dev->kobj.parent);
-       kobject_put(dev->kobj.parent);
-       up(&dev->class->sem);
+static void cleanup_device_parent(struct device *dev)
+{
+       cleanup_glue_dir(dev, dev->kobj.parent);
 }
 #endif
 
-static int setup_parent(struct device *dev, struct device *parent)
+static void setup_parent(struct device *dev, struct device *parent)
 {
        struct kobject *kobj;
        kobj = get_device_parent(dev, parent);
-       if (IS_ERR(kobj))
-               return PTR_ERR(kobj);
        if (kobj)
                dev->kobj.parent = kobj;
-       return 0;
 }
 
 static int device_add_class_symlinks(struct device *dev)
@@ -672,14 +662,14 @@ static int device_add_class_symlinks(struct device *dev)
 #ifdef CONFIG_SYSFS_DEPRECATED
        /* stacked class devices need a symlink in the class directory */
        if (dev->kobj.parent != &dev->class->subsys.kobj &&
-           dev->type != &part_type) {
+           device_is_not_partition(dev)) {
                error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj,
                                          dev->bus_id);
                if (error)
                        goto out_subsys;
        }
 
-       if (dev->parent && dev->type != &part_type) {
+       if (dev->parent && device_is_not_partition(dev)) {
                struct device *parent = dev->parent;
                char *class_name;
 
@@ -708,11 +698,11 @@ static int device_add_class_symlinks(struct device *dev)
        return 0;
 
 out_device:
-       if (dev->parent && dev->type != &part_type)
+       if (dev->parent && device_is_not_partition(dev))
                sysfs_remove_link(&dev->kobj, "device");
 out_busid:
        if (dev->kobj.parent != &dev->class->subsys.kobj &&
-           dev->type != &part_type)
+           device_is_not_partition(dev))
                sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
 #else
        /* link in the class directory pointing to the device */
@@ -721,7 +711,7 @@ out_busid:
        if (error)
                goto out_subsys;
 
-       if (dev->parent && dev->type != &part_type) {
+       if (dev->parent && device_is_not_partition(dev)) {
                error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
                                          "device");
                if (error)
@@ -745,7 +735,7 @@ static void device_remove_class_symlinks(struct device *dev)
                return;
 
 #ifdef CONFIG_SYSFS_DEPRECATED
-       if (dev->parent && dev->type != &part_type) {
+       if (dev->parent && device_is_not_partition(dev)) {
                char *class_name;
 
                class_name = make_class_name(dev->class->name, &dev->kobj);
@@ -757,10 +747,10 @@ static void device_remove_class_symlinks(struct device *dev)
        }
 
        if (dev->kobj.parent != &dev->class->subsys.kobj &&
-           dev->type != &part_type)
+           device_is_not_partition(dev))
                sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
 #else
-       if (dev->parent && dev->type != &part_type)
+       if (dev->parent && device_is_not_partition(dev))
                sysfs_remove_link(&dev->kobj, "device");
 
        sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
@@ -770,15 +760,15 @@ static void device_remove_class_symlinks(struct device *dev)
 }
 
 /**
- *     device_add - add device to device hierarchy.
- *     @dev:   device.
+ * device_add - add device to device hierarchy.
+ * @dev: device.
  *
- *     This is part 2 of device_register(), though may be called
- *     separately _iff_ device_initialize() has been called separately.
+ * This is part 2 of device_register(), though may be called
+ * separately _iff_ device_initialize() has been called separately.
  *
- *     This adds it to the kobject hierarchy via kobject_add(), adds it
- *     to the global and sibling lists for the device, then
- *     adds it to the other relevant subsystems of the driver model.
+ * This adds it to the kobject hierarchy via kobject_add(), adds it
+ * to the global and sibling lists for the device, then
+ * adds it to the other relevant subsystems of the driver model.
  */
 int device_add(struct device *dev)
 {
@@ -786,25 +776,16 @@ int device_add(struct device *dev)
        struct class_interface *class_intf;
        int error;
 
-       error = pm_sleep_lock();
-       if (error) {
-               dev_warn(dev, "Suspicious %s during suspend\n", __FUNCTION__);
-               dump_stack();
-               return error;
-       }
-
        dev = get_device(dev);
        if (!dev || !strlen(dev->bus_id)) {
                error = -EINVAL;
-               goto Error;
+               goto Done;
        }
 
        pr_debug("device: '%s': %s\n", dev->bus_id, __FUNCTION__);
 
        parent = get_device(dev->parent);
-       error = setup_parent(dev, parent);
-       if (error)
-               goto Error;
+       setup_parent(dev, parent);
 
        /* first, register with generic layer. */
        error = kobject_add(&dev->kobj, dev->kobj.parent, "%s", dev->bus_id);
@@ -836,13 +817,12 @@ int device_add(struct device *dev)
        error = device_add_attrs(dev);
        if (error)
                goto AttrsError;
-       error = dpm_sysfs_add(dev);
-       if (error)
-               goto PMError;
-       device_pm_add(dev);
        error = bus_add_device(dev);
        if (error)
                goto BusError;
+       error = device_pm_add(dev);
+       if (error)
+               goto PMError;
        kobject_uevent(&dev->kobj, KOBJ_ADD);
        bus_attach_device(dev);
        if (parent)
@@ -861,12 +841,10 @@ int device_add(struct device *dev)
        }
  Done:
        put_device(dev);
-       pm_sleep_unlock();
        return error;
- BusError:
-       device_pm_remove(dev);
-       dpm_sysfs_remove(dev);
  PMError:
+       bus_remove_device(dev);
+ BusError:
        if (dev->bus)
                blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
                                             BUS_NOTIFY_DEL_DEVICE, dev);
@@ -882,75 +860,69 @@ int device_add(struct device *dev)
        kobject_uevent(&dev->kobj, KOBJ_REMOVE);
        kobject_del(&dev->kobj);
  Error:
+       cleanup_device_parent(dev);
        if (parent)
                put_device(parent);
        goto Done;
 }
 
-
 /**
- *     device_register - register a device with the system.
- *     @dev:   pointer to the device structure
+ * device_register - register a device with the system.
+ * @dev: pointer to the device structure
  *
- *     This happens in two clean steps - initialize the device
- *     and add it to the system. The two steps can be called
- *     separately, but this is the easiest and most common.
- *     I.e. you should only call the two helpers separately if
- *     have a clearly defined need to use and refcount the device
- *     before it is added to the hierarchy.
+ * This happens in two clean steps - initialize the device
+ * and add it to the system. The two steps can be called
+ * separately, but this is the easiest and most common.
+ * I.e. you should only call the two helpers separately if
+ * have a clearly defined need to use and refcount the device
+ * before it is added to the hierarchy.
  */
-
 int device_register(struct device *dev)
 {
        device_initialize(dev);
        return device_add(dev);
 }
 
-
 /**
- *     get_device - increment reference count for device.
- *     @dev:   device.
+ * get_device - increment reference count for device.
+ * @dev: device.
  *
- *     This simply forwards the call to kobject_get(), though
- *     we do take care to provide for the case that we get a NULL
- *     pointer passed in.
+ * This simply forwards the call to kobject_get(), though
+ * we do take care to provide for the case that we get a NULL
+ * pointer passed in.
  */
-
-struct device * get_device(struct device * dev)
+struct device *get_device(struct device *dev)
 {
        return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
 }
 
-
 /**
- *     put_device - decrement reference count.
- *     @dev:   device in question.
+ * put_device - decrement reference count.
+ * @dev: device in question.
  */
-void put_device(struct device * dev)
+void put_device(struct device *dev)
 {
        /* might_sleep(); */
        if (dev)
                kobject_put(&dev->kobj);
 }
 
-
 /**
- *     device_del - delete device from system.
- *     @dev:   device.
+ * device_del - delete device from system.
+ * @dev: device.
  *
- *     This is the first part of the device unregistration
- *     sequence. This removes the device from the lists we control
- *     from here, has it removed from the other driver model
- *     subsystems it was added to in device_add(), and removes it
- *     from the kobject hierarchy.
+ * This is the first part of the device unregistration
+ * sequence. This removes the device from the lists we control
+ * from here, has it removed from the other driver model
+ * subsystems it was added to in device_add(), and removes it
+ * from the kobject hierarchy.
  *
- *     NOTE: this should be called manually _iff_ device_add() was
- *     also called manually.
+ * NOTE: this should be called manually _iff_ device_add() was
+ * also called manually.
  */
-
-void device_del(struct device * dev)
+void device_del(struct device *dev)
 {
-       struct device * parent = dev->parent;
+       struct device *parent = dev->parent;
        struct class_interface *class_intf;
 
        device_pm_remove(dev);
@@ -996,47 +968,46 @@ void device_del(struct device * dev)
 }
 
 /**
- *     device_unregister - unregister device from system.
- *     @dev:   device going away.
+ * device_unregister - unregister device from system.
+ * @dev: device going away.
  *
- *     We do this in two parts, like we do device_register(). First,
- *     we remove it from all the subsystems with device_del(), then
- *     we decrement the reference count via put_device(). If that
- *     is the final reference count, the device will be cleaned up
- *     via device_release() above. Otherwise, the structure will
- *     stick around until the final reference to the device is dropped.
+ * We do this in two parts, like we do device_register(). First,
+ * we remove it from all the subsystems with device_del(), then
+ * we decrement the reference count via put_device(). If that
+ * is the final reference count, the device will be cleaned up
+ * via device_release() above. Otherwise, the structure will
+ * stick around until the final reference to the device is dropped.
  */
-void device_unregister(struct device * dev)
+void device_unregister(struct device *dev)
 {
        pr_debug("device: '%s': %s\n", dev->bus_id, __FUNCTION__);
        device_del(dev);
        put_device(dev);
 }
 
-
-static struct device * next_device(struct klist_iter * i)
+static struct device *next_device(struct klist_iter *i)
 {
-       struct klist_node * n = klist_next(i);
+       struct klist_node *n = klist_next(i);
        return n ? container_of(n, struct device, knode_parent) : NULL;
 }
 
 /**
- *     device_for_each_child - device child iterator.
- *     @parent: parent struct device.
- *     @data:  data for the callback.
- *     @fn:    function to be called for each device.
+ * device_for_each_child - device child iterator.
+ * @parent: parent struct device.
+ * @data: data for the callback.
+ * @fn: function to be called for each device.
  *
- *     Iterate over @parent's child devices, and call @fn for each,
- *     passing it @data.
+ * Iterate over @parent's child devices, and call @fn for each,
+ * passing it @data.
  *
- *     We check the return of @fn each time. If it returns anything
- *     other than 0, we break out and return that value.
+ * We check the return of @fn each time. If it returns anything
+ * other than 0, we break out and return that value.
  */
-int device_for_each_child(struct device * parent, void * data,
-                    int (*fn)(struct device *, void *))
+int device_for_each_child(struct device *parent, void *data,
+                         int (*fn)(struct device *dev, void *data))
 {
        struct klist_iter i;
-       struct device * child;
+       struct device *child;
        int error = 0;
 
        klist_iter_init(&parent->klist_children, &i);
@@ -1061,8 +1032,8 @@ int device_for_each_child(struct device * parent, void * data,
  * current device can be obtained, this function will return to the caller
  * and not iterate over any more devices.
  */
-struct device * device_find_child(struct device *parent, void *data,
-                                 int (*match)(struct device *, void *))
+struct device *device_find_child(struct device *parent, void *data,
+                                int (*match)(struct device *dev, void *data))
 {
        struct klist_iter i;
        struct device *child;
@@ -1165,25 +1136,11 @@ error:
 }
 EXPORT_SYMBOL_GPL(device_create);
 
-/**
- * find_device - finds a device that was created with device_create()
- * @class: pointer to the struct class that this device was registered with
- * @devt: the dev_t of the device that was previously registered
- */
-static struct device *find_device(struct class *class, dev_t devt)
+static int __match_devt(struct device *dev, void *data)
 {
-       struct device *dev = NULL;
-       struct device *dev_tmp;
+       dev_t *devt = data;
 
-       down(&class->sem);
-       list_for_each_entry(dev_tmp, &class->devices, node) {
-               if (dev_tmp->devt == devt) {
-                       dev = dev_tmp;
-                       break;
-               }
-       }
-       up(&class->sem);
-       return dev;
+       return dev->devt == *devt;
 }
 
 /**
@@ -1198,9 +1155,11 @@ void device_destroy(struct class *class, dev_t devt)
 {
        struct device *dev;
 
-       dev = find_device(class, devt);
-       if (dev)
+       dev = class_find_device(class, &devt, __match_devt);
+       if (dev) {
+               put_device(dev);
                device_unregister(dev);
+       }
 }
 EXPORT_SYMBOL_GPL(device_destroy);
 
@@ -1224,9 +1183,11 @@ void destroy_suspended_device(struct class *class, dev_t devt)
 {
        struct device *dev;
 
-       dev = find_device(class, devt);
-       if (dev)
+       dev = class_find_device(class, &devt, __match_devt);
+       if (dev) {
                device_pm_schedule_removal(dev);
+               put_device(dev);
+       }
 }
 EXPORT_SYMBOL_GPL(destroy_suspended_device);
 #endif /* CONFIG_PM_SLEEP */
@@ -1329,8 +1290,7 @@ static int device_move_class_links(struct device *dev,
                                          class_name);
                if (error)
                        sysfs_remove_link(&dev->kobj, "device");
-       }
-       else
+       } else
                error = 0;
 out:
        kfree(class_name);
@@ -1361,16 +1321,13 @@ int device_move(struct device *dev, struct device *new_parent)
                return -EINVAL;
 
        new_parent = get_device(new_parent);
-       new_parent_kobj = get_device_parent (dev, new_parent);
-       if (IS_ERR(new_parent_kobj)) {
-               error = PTR_ERR(new_parent_kobj);
-               put_device(new_parent);
-               goto out;
-       }
+       new_parent_kobj = get_device_parent(dev, new_parent);
+
        pr_debug("device: '%s': %s: moving to '%s'\n", dev->bus_id,
                 __FUNCTION__, new_parent ? new_parent->bus_id : "<NULL>");
        error = kobject_move(&dev->kobj, new_parent_kobj);
        if (error) {
+               cleanup_glue_dir(dev, new_parent_kobj);
                put_device(new_parent);
                goto out;
        }
@@ -1393,6 +1350,7 @@ int device_move(struct device *dev, struct device *new_parent)
                                klist_add_tail(&dev->knode_parent,
                                               &old_parent->klist_children);
                }
+               cleanup_glue_dir(dev, new_parent_kobj);
                put_device(new_parent);
                goto out;
        }
@@ -1409,7 +1367,7 @@ EXPORT_SYMBOL_GPL(device_move);
  */
 void device_shutdown(void)
 {
-       struct device * dev, *devn;
+       struct device *dev, *devn;
 
        list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list,
                                kobj.entry) {