]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/base/core.c
Driver core: warn when userspace writes to the uevent file in a non-supported way
[linux-2.6-omap-h63xx.git] / drivers / base / core.c
index 68ad11af22b41e307277f3826be989c37dbe2bbf..f69305c7269d1cf7fb294c2576a67c66118f7d64 100644 (file)
@@ -17,6 +17,7 @@
 #include <linux/slab.h>
 #include <linux/string.h>
 #include <linux/kdev_t.h>
+#include <linux/notifier.h>
 
 #include <asm/semaphore.h>
 
@@ -42,7 +43,8 @@ int (*platform_notify_remove)(struct device * dev) = NULL;
 const char *dev_driver_string(struct device *dev)
 {
        return dev->driver ? dev->driver->name :
-                       (dev->bus ? dev->bus->name : "");
+                       (dev->bus ? dev->bus->name :
+                       (dev->class ? dev->class->name : ""));
 }
 EXPORT_SYMBOL(dev_driver_string);
 
@@ -94,6 +96,8 @@ static void device_release(struct kobject * kobj)
 
        if (dev->release)
                dev->release(dev);
+       else if (dev->type && dev->type->release)
+               dev->type->release(dev);
        else if (dev->class && dev->class->dev_release)
                dev->class->dev_release(dev);
        else {
@@ -116,6 +120,8 @@ static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
 
        if (ktype == &ktype_device) {
                struct device *dev = to_dev(kobj);
+               if (dev->uevent_suppress)
+                       return 0;
                if (dev->bus)
                        return 1;
                if (dev->class)
@@ -153,21 +159,52 @@ static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
                               "MINOR=%u", MINOR(dev->devt));
        }
 
-       /* add bus name (same as SUBSYSTEM, deprecated) */
-       if (dev->bus)
+       if (dev->type && dev->type->name)
                add_uevent_var(envp, num_envp, &i,
                               buffer, buffer_size, &length,
-                              "PHYSDEVBUS=%s", dev->bus->name);
+                              "DEVTYPE=%s", dev->type->name);
 
-       /* add driver name (PHYSDEV* values are deprecated)*/
-       if (dev->driver) {
+       if (dev->driver)
                add_uevent_var(envp, num_envp, &i,
                               buffer, buffer_size, &length,
                               "DRIVER=%s", dev->driver->name);
+
+#ifdef CONFIG_SYSFS_DEPRECATED
+       if (dev->class) {
+               struct device *parent = dev->parent;
+
+               /* find first bus device in parent chain */
+               while (parent && !parent->bus)
+                       parent = parent->parent;
+               if (parent && parent->bus) {
+                       const char *path;
+
+                       path = kobject_get_path(&parent->kobj, GFP_KERNEL);
+                       add_uevent_var(envp, num_envp, &i,
+                                      buffer, buffer_size, &length,
+                                      "PHYSDEVPATH=%s", path);
+                       kfree(path);
+
+                       add_uevent_var(envp, num_envp, &i,
+                                      buffer, buffer_size, &length,
+                                      "PHYSDEVBUS=%s", parent->bus->name);
+
+                       if (parent->driver)
+                               add_uevent_var(envp, num_envp, &i,
+                                              buffer, buffer_size, &length,
+                                              "PHYSDEVDRIVER=%s", parent->driver->name);
+               }
+       } else if (dev->bus) {
                add_uevent_var(envp, num_envp, &i,
                               buffer, buffer_size, &length,
-                              "PHYSDEVDRIVER=%s", dev->driver->name);
+                              "PHYSDEVBUS=%s", dev->bus->name);
+
+               if (dev->driver)
+                       add_uevent_var(envp, num_envp, &i,
+                                      buffer, buffer_size, &length,
+                                      "PHYSDEVDRIVER=%s", dev->driver->name);
        }
+#endif
 
        /* terminate, set to next free slot, shrink available space */
        envp[i] = NULL;
@@ -179,19 +216,25 @@ static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
        if (dev->bus && dev->bus->uevent) {
                /* have the bus specific function add its stuff */
                retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size);
-                       if (retval) {
-                       pr_debug ("%s - uevent() returned %d\n",
+               if (retval)
+                       pr_debug ("%s: bus uevent() returned %d\n",
                                  __FUNCTION__, retval);
-               }
        }
 
        if (dev->class && dev->class->dev_uevent) {
                /* have the class specific function add its stuff */
                retval = dev->class->dev_uevent(dev, envp, num_envp, buffer, buffer_size);
-                       if (retval) {
-                               pr_debug("%s - dev_uevent() returned %d\n",
-                                        __FUNCTION__, retval);
-               }
+               if (retval)
+                       pr_debug("%s: class uevent() returned %d\n",
+                                __FUNCTION__, retval);
+       }
+
+       if (dev->type && dev->type->uevent) {
+               /* have the device type specific fuction add its stuff */
+               retval = dev->type->uevent(dev, envp, num_envp, buffer, buffer_size);
+               if (retval)
+                       pr_debug("%s: dev_type uevent() returned %d\n",
+                                __FUNCTION__, retval);
        }
 
        return retval;
@@ -203,76 +246,167 @@ static struct kset_uevent_ops device_uevent_ops = {
        .uevent =       dev_uevent,
 };
 
+static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
+                          char *buf)
+{
+       struct kobject *top_kobj;
+       struct kset *kset;
+       char *envp[32];
+       char data[PAGE_SIZE];
+       char *pos;
+       int i;
+       size_t count = 0;
+       int retval;
+
+       /* search the kset, the device belongs to */
+       top_kobj = &dev->kobj;
+       if (!top_kobj->kset && top_kobj->parent) {
+               do {
+                       top_kobj = top_kobj->parent;
+               } while (!top_kobj->kset && top_kobj->parent);
+       }
+       if (!top_kobj->kset)
+               goto out;
+       kset = top_kobj->kset;
+       if (!kset->uevent_ops || !kset->uevent_ops->uevent)
+               goto out;
+
+       /* respect filter */
+       if (kset->uevent_ops && kset->uevent_ops->filter)
+               if (!kset->uevent_ops->filter(kset, &dev->kobj))
+                       goto out;
+
+       /* let the kset specific function add its keys */
+       pos = data;
+       retval = kset->uevent_ops->uevent(kset, &dev->kobj,
+                                         envp, ARRAY_SIZE(envp),
+                                         pos, PAGE_SIZE);
+       if (retval)
+               goto out;
+
+       /* copy keys to file */
+       for (i = 0; envp[i]; i++) {
+               pos = &buf[count];
+               count += sprintf(pos, "%s\n", envp[i]);
+       }
+out:
+       return count;
+}
+
 static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
                            const char *buf, size_t count)
 {
+       if (memcmp(buf, "add", 3) != 0)
+               dev_err(dev, "uevent: unsupported action-string; this will "
+                       "be ignored in a future kernel version");
        kobject_uevent(&dev->kobj, KOBJ_ADD);
        return count;
 }
 
-static int device_add_groups(struct device *dev)
+static int device_add_attributes(struct device *dev,
+                                struct device_attribute *attrs)
+{
+       int error = 0;
+       int i;
+
+       if (attrs) {
+               for (i = 0; attr_name(attrs[i]); i++) {
+                       error = device_create_file(dev, &attrs[i]);
+                       if (error)
+                               break;
+               }
+               if (error)
+                       while (--i >= 0)
+                               device_remove_file(dev, &attrs[i]);
+       }
+       return error;
+}
+
+static void device_remove_attributes(struct device *dev,
+                                    struct device_attribute *attrs)
 {
        int i;
+
+       if (attrs)
+               for (i = 0; attr_name(attrs[i]); i++)
+                       device_remove_file(dev, &attrs[i]);
+}
+
+static int device_add_groups(struct device *dev,
+                            struct attribute_group **groups)
+{
        int error = 0;
+       int i;
 
-       if (dev->groups) {
-               for (i = 0; dev->groups[i]; i++) {
-                       error = sysfs_create_group(&dev->kobj, dev->groups[i]);
+       if (groups) {
+               for (i = 0; groups[i]; i++) {
+                       error = sysfs_create_group(&dev->kobj, groups[i]);
                        if (error) {
                                while (--i >= 0)
-                                       sysfs_remove_group(&dev->kobj, dev->groups[i]);
-                               goto out;
+                                       sysfs_remove_group(&dev->kobj, groups[i]);
+                               break;
                        }
                }
        }
-out:
        return error;
 }
 
-static void device_remove_groups(struct device *dev)
+static void device_remove_groups(struct device *dev,
+                                struct attribute_group **groups)
 {
        int i;
-       if (dev->groups) {
-               for (i = 0; dev->groups[i]; i++) {
-                       sysfs_remove_group(&dev->kobj, dev->groups[i]);
-               }
-       }
+
+       if (groups)
+               for (i = 0; groups[i]; i++)
+                       sysfs_remove_group(&dev->kobj, groups[i]);
 }
 
 static int device_add_attrs(struct device *dev)
 {
        struct class *class = dev->class;
-       int error = 0;
-       int i;
+       struct device_type *type = dev->type;
+       int error;
 
-       if (!class)
-               return 0;
+       if (class) {
+               error = device_add_attributes(dev, class->dev_attrs);
+               if (error)
+                       return error;
+       }
 
-       if (class->dev_attrs) {
-               for (i = 0; attr_name(class->dev_attrs[i]); i++) {
-                       error = device_create_file(dev, &class->dev_attrs[i]);
-                       if (error)
-                               break;
-               }
+       if (type) {
+               error = device_add_groups(dev, type->groups);
+               if (error)
+                       goto err_remove_class_attrs;
        }
+
+       error = device_add_groups(dev, dev->groups);
        if (error)
-               while (--i >= 0)
-                       device_remove_file(dev, &class->dev_attrs[i]);
+               goto err_remove_type_groups;
+
+       return 0;
+
+ err_remove_type_groups:
+       if (type)
+               device_remove_groups(dev, type->groups);
+ err_remove_class_attrs:
+       if (class)
+               device_remove_attributes(dev, class->dev_attrs);
+
        return error;
 }
 
 static void device_remove_attrs(struct device *dev)
 {
        struct class *class = dev->class;
-       int i;
+       struct device_type *type = dev->type;
 
-       if (!class)
-               return;
+       device_remove_groups(dev, dev->groups);
 
-       if (class->dev_attrs) {
-               for (i = 0; attr_name(class->dev_attrs[i]); i++)
-                       device_remove_file(dev, &class->dev_attrs[i]);
-       }
+       if (type)
+               device_remove_groups(dev, type->groups);
+
+       if (class)
+               device_remove_attributes(dev, class->dev_attrs);
 }
 
 
@@ -345,6 +479,35 @@ void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
 }
 EXPORT_SYMBOL_GPL(device_remove_bin_file);
 
+/**
+ * device_schedule_callback - helper to schedule a callback for a device
+ * @dev: device.
+ * @func: callback function to invoke later.
+ *
+ * Attribute methods must not unregister themselves or their parent device
+ * (which would amount to the same thing).  Attempts to do so will deadlock,
+ * since unregistration is mutually exclusive with driver callbacks.
+ *
+ * Instead methods can call this routine, which will attempt to allocate
+ * and schedule a workqueue request to call back @func with @dev as its
+ * argument in the workqueue's process context.  @dev will be pinned until
+ * @func returns.
+ *
+ * Returns 0 if the request was submitted, -ENOMEM if storage could not
+ * be allocated.
+ *
+ * NOTE: This routine won't work if CONFIG_SYSFS isn't set!  It uses an
+ * underlying sysfs routine (since it is intended for use by attribute
+ * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
+ */
+int device_schedule_callback(struct device *dev,
+               void (*func)(struct device *))
+{
+       return sysfs_schedule_callback(&dev->kobj,
+                       (void (*)(void *)) func, dev);
+}
+EXPORT_SYMBOL_GPL(device_schedule_callback);
+
 static void klist_children_get(struct klist_node *n)
 {
        struct device *dev = container_of(n, struct device, knode_parent);
@@ -380,7 +543,87 @@ void device_initialize(struct device *dev)
        INIT_LIST_HEAD(&dev->dma_pools);
        INIT_LIST_HEAD(&dev->node);
        init_MUTEX(&dev->sem);
+       spin_lock_init(&dev->devres_lock);
+       INIT_LIST_HEAD(&dev->devres_head);
        device_init_wakeup(dev, 0);
+       set_dev_node(dev, -1);
+}
+
+#ifdef CONFIG_SYSFS_DEPRECATED
+static struct kobject * get_device_parent(struct device *dev,
+                                         struct device *parent)
+{
+       /* Set the parent to the class, not the parent device */
+       /* this keeps sysfs from having a symlink to make old udevs happy */
+       if (dev->class)
+               return &dev->class->subsys.kset.kobj;
+       else if (parent)
+               return &parent->kobj;
+
+       return NULL;
+}
+#else
+static struct kobject *virtual_device_parent(struct device *dev)
+{
+       static struct kobject *virtual_dir = NULL;
+
+       if (!virtual_dir)
+               virtual_dir = kobject_add_dir(&devices_subsys.kset.kobj, "virtual");
+
+       return virtual_dir;
+}
+
+static struct kobject * get_device_parent(struct device *dev,
+                                         struct device *parent)
+{
+       if (dev->class) {
+               struct kobject *kobj = NULL;
+               struct kobject *parent_kobj;
+               struct kobject *k;
+
+               /*
+                * 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.
+                */
+               if (parent == NULL)
+                       parent_kobj = virtual_device_parent(dev);
+               else if (parent->class)
+                       return &parent->kobj;
+               else
+                       parent_kobj = &parent->kobj;
+
+               /* find our class-directory at the parent and reference it */
+               spin_lock(&dev->class->class_dirs.list_lock);
+               list_for_each_entry(k, &dev->class->class_dirs.list, entry)
+                       if (k->parent == parent_kobj) {
+                               kobj = kobject_get(k);
+                               break;
+                       }
+               spin_unlock(&dev->class->class_dirs.list_lock);
+               if (kobj)
+                       return kobj;
+
+               /* or create a new class-directory at the parent device */
+               return kobject_kset_add_dir(&dev->class->class_dirs,
+                                           parent_kobj, dev->class->name);
+       }
+
+       if (parent)
+               return &parent->kobj;
+       return NULL;
+}
+#endif
+
+static int 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;
 }
 
 /**
@@ -405,34 +648,34 @@ int device_add(struct device *dev)
        if (!dev || !strlen(dev->bus_id))
                goto Error;
 
-       /* if this is a class device, and has no parent, create one */
-       if ((dev->class) && (dev->parent == NULL)) {
-               error = virtual_device_parent(dev);
-               if (error)
-                       goto Error;
-       }
+       pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
 
        parent = get_device(dev->parent);
-
-       pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
+       error = setup_parent(dev, parent);
+       if (error)
+               goto Error;
 
        /* first, register with generic layer. */
        kobject_set_name(&dev->kobj, "%s", dev->bus_id);
-       if (parent)
-               dev->kobj.parent = &parent->kobj;
-
-       if ((error = kobject_add(&dev->kobj)))
+       error = kobject_add(&dev->kobj);
+       if (error)
                goto Error;
 
        /* notify platform of device entry */
        if (platform_notify)
                platform_notify(dev);
 
+       /* notify clients of device entry (new way) */
+       if (dev->bus)
+               blocking_notifier_call_chain(&dev->bus->bus_notifier,
+                                            BUS_NOTIFY_ADD_DEVICE, dev);
+
        dev->uevent_attr.attr.name = "uevent";
-       dev->uevent_attr.attr.mode = S_IWUSR;
+       dev->uevent_attr.attr.mode = S_IRUGO | S_IWUSR;
        if (dev->driver)
                dev->uevent_attr.attr.owner = dev->driver->owner;
        dev->uevent_attr.store = store_uevent;
+       dev->uevent_attr.show = show_uevent;
        error = device_create_file(dev, &dev->uevent_attr);
        if (error)
                goto attrError;
@@ -461,26 +704,32 @@ int device_add(struct device *dev)
        if (dev->class) {
                sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
                                  "subsystem");
-               sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
-                                 dev->bus_id);
+               /* If this is not a "fake" compatible device, then create the
+                * symlink from the class to the device. */
+               if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
+                       sysfs_create_link(&dev->class->subsys.kset.kobj,
+                                         &dev->kobj, dev->bus_id);
                if (parent) {
-                       sysfs_create_link(&dev->kobj, &dev->parent->kobj, "device");
-                       class_name = make_class_name(dev->class->name, &dev->kobj);
-                       sysfs_create_link(&dev->parent->kobj, &dev->kobj, class_name);
+                       sysfs_create_link(&dev->kobj, &dev->parent->kobj,
+                                                       "device");
+#ifdef CONFIG_SYSFS_DEPRECATED
+                       class_name = make_class_name(dev->class->name,
+                                                       &dev->kobj);
+                       if (class_name)
+                               sysfs_create_link(&dev->parent->kobj,
+                                                 &dev->kobj, class_name);
+#endif
                }
        }
 
        if ((error = device_add_attrs(dev)))
                goto AttrsError;
-       if ((error = device_add_groups(dev)))
-               goto GroupError;
        if ((error = device_pm_add(dev)))
                goto PMError;
        if ((error = bus_add_device(dev)))
                goto BusError;
        kobject_uevent(&dev->kobj, KOBJ_ADD);
-       if ((error = bus_attach_device(dev)))
-               goto AttachError;
+       bus_attach_device(dev);
        if (parent)
                klist_add_tail(&dev->knode_parent, &parent->klist_children);
 
@@ -496,22 +745,41 @@ int device_add(struct device *dev)
                up(&dev->class->sem);
        }
  Done:
-       kfree(class_name);
+       kfree(class_name);
        put_device(dev);
        return error;
- AttachError:
-       bus_remove_device(dev);
  BusError:
        device_pm_remove(dev);
  PMError:
-       device_remove_groups(dev);
- GroupError:
-       device_remove_attrs(dev);
+       if (dev->bus)
+               blocking_notifier_call_chain(&dev->bus->bus_notifier,
+                                            BUS_NOTIFY_DEL_DEVICE, dev);
+       device_remove_attrs(dev);
  AttrsError:
        if (dev->devt_attr) {
                device_remove_file(dev, dev->devt_attr);
                kfree(dev->devt_attr);
        }
+
+       if (dev->class) {
+               sysfs_remove_link(&dev->kobj, "subsystem");
+               /* If this is not a "fake" compatible device, remove the
+                * symlink from the class to the device. */
+               if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
+                       sysfs_remove_link(&dev->class->subsys.kset.kobj,
+                                         dev->bus_id);
+               if (parent) {
+#ifdef CONFIG_SYSFS_DEPRECATED
+                       char *class_name = make_class_name(dev->class->name,
+                                                          &dev->kobj);
+                       if (class_name)
+                               sysfs_remove_link(&dev->parent->kobj,
+                                                 class_name);
+                       kfree(class_name);
+#endif
+                       sysfs_remove_link(&dev->kobj, "device");
+               }
+       }
  ueventattrError:
        device_remove_file(dev, &dev->uevent_attr);
  attrError:
@@ -586,22 +854,33 @@ void put_device(struct device * dev)
 void device_del(struct device * dev)
 {
        struct device * parent = dev->parent;
-       char *class_name = NULL;
        struct class_interface *class_intf;
 
        if (parent)
                klist_del(&dev->knode_parent);
-       if (dev->devt_attr)
+       if (dev->devt_attr) {
                device_remove_file(dev, dev->devt_attr);
+               kfree(dev->devt_attr);
+       }
        if (dev->class) {
                sysfs_remove_link(&dev->kobj, "subsystem");
-               sysfs_remove_link(&dev->class->subsys.kset.kobj, dev->bus_id);
-               class_name = make_class_name(dev->class->name, &dev->kobj);
+               /* If this is not a "fake" compatible device, remove the
+                * symlink from the class to the device. */
+               if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
+                       sysfs_remove_link(&dev->class->subsys.kset.kobj,
+                                         dev->bus_id);
                if (parent) {
+#ifdef CONFIG_SYSFS_DEPRECATED
+                       char *class_name = make_class_name(dev->class->name,
+                                                          &dev->kobj);
+                       if (class_name)
+                               sysfs_remove_link(&dev->parent->kobj,
+                                                 class_name);
+                       kfree(class_name);
+#endif
                        sysfs_remove_link(&dev->kobj, "device");
-                       sysfs_remove_link(&dev->parent->kobj, class_name);
                }
-               kfree(class_name);
+
                down(&dev->class->sem);
                /* notify any interfaces that the device is now gone */
                list_for_each_entry(class_intf, &dev->class->interfaces, node)
@@ -610,17 +889,51 @@ void device_del(struct device * dev)
                /* remove the device from the class list */
                list_del_init(&dev->node);
                up(&dev->class->sem);
+
+               /* If we live in a parent class-directory, unreference it */
+               if (dev->kobj.parent->kset == &dev->class->class_dirs) {
+                       struct device *d;
+                       int other = 0;
+
+                       /*
+                        * if we are the last child of our class, delete
+                        * our class-directory at this parent
+                        */
+                       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);
+               }
        }
        device_remove_file(dev, &dev->uevent_attr);
-       device_remove_groups(dev);
        device_remove_attrs(dev);
+       bus_remove_device(dev);
+
+       /*
+        * Some platform devices are driven without driver attached
+        * and managed resources may have been acquired.  Make sure
+        * all resources are released.
+        */
+       devres_release_all(dev);
 
        /* Notify the platform of the removal, in case they
         * need to do anything...
         */
        if (platform_notify_remove)
                platform_notify_remove(dev);
-       bus_remove_device(dev);
+       if (dev->bus)
+               blocking_notifier_call_chain(&dev->bus->bus_notifier,
+                                            BUS_NOTIFY_DEL_DEVICE, dev);
        device_pm_remove(dev);
        kobject_uevent(&dev->kobj, KOBJ_REMOVE);
        kobject_del(&dev->kobj);
@@ -679,12 +992,45 @@ int device_for_each_child(struct device * parent, void * data,
        return error;
 }
 
+/**
+ * device_find_child - device iterator for locating a particular device.
+ * @parent: parent struct device
+ * @data: Data to pass to match function
+ * @match: Callback function to check device
+ *
+ * This is similar to the device_for_each_child() function above, but it
+ * returns a reference to a device that is 'found' for later use, as
+ * determined by the @match callback.
+ *
+ * The callback should return 0 if the device doesn't match and non-zero
+ * if it does.  If the callback returns non-zero and a reference to the
+ * 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 klist_iter i;
+       struct device *child;
+
+       if (!parent)
+               return NULL;
+
+       klist_iter_init(&parent->klist_children, &i);
+       while ((child = next_device(&i)))
+               if (match(child, data) && get_device(child))
+                       break;
+       klist_iter_exit(&i);
+       return child;
+}
+
 int __init devices_init(void)
 {
        return subsystem_register(&devices_subsys);
 }
 
 EXPORT_SYMBOL_GPL(device_for_each_child);
+EXPORT_SYMBOL_GPL(device_find_child);
 
 EXPORT_SYMBOL_GPL(device_initialize);
 EXPORT_SYMBOL_GPL(device_add);
@@ -807,8 +1153,10 @@ int device_rename(struct device *dev, char *new_name)
 
        pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name);
 
+#ifdef CONFIG_SYSFS_DEPRECATED
        if ((dev->class) && (dev->parent))
                old_class_name = make_class_name(dev->class->name, &dev->kobj);
+#endif
 
        if (dev->class) {
                old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
@@ -823,6 +1171,7 @@ int device_rename(struct device *dev, char *new_name)
 
        error = kobject_rename(&dev->kobj, new_name);
 
+#ifdef CONFIG_SYSFS_DEPRECATED
        if (old_class_name) {
                new_class_name = make_class_name(dev->class->name, &dev->kobj);
                if (new_class_name) {
@@ -831,6 +1180,8 @@ int device_rename(struct device *dev, char *new_name)
                        sysfs_remove_link(&dev->parent->kobj, old_class_name);
                }
        }
+#endif
+
        if (dev->class) {
                sysfs_remove_link(&dev->class->subsys.kset.kobj,
                                  old_symlink_name);
@@ -846,3 +1197,106 @@ int device_rename(struct device *dev, char *new_name)
 
        return error;
 }
+EXPORT_SYMBOL_GPL(device_rename);
+
+static int device_move_class_links(struct device *dev,
+                                  struct device *old_parent,
+                                  struct device *new_parent)
+{
+       int error = 0;
+#ifdef CONFIG_SYSFS_DEPRECATED
+       char *class_name;
+
+       class_name = make_class_name(dev->class->name, &dev->kobj);
+       if (!class_name) {
+               error = -ENOMEM;
+               goto out;
+       }
+       if (old_parent) {
+               sysfs_remove_link(&dev->kobj, "device");
+               sysfs_remove_link(&old_parent->kobj, class_name);
+       }
+       if (new_parent) {
+               error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
+                                         "device");
+               if (error)
+                       goto out;
+               error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
+                                         class_name);
+               if (error)
+                       sysfs_remove_link(&dev->kobj, "device");
+       }
+       else
+               error = 0;
+out:
+       kfree(class_name);
+       return error;
+#else
+       if (old_parent)
+               sysfs_remove_link(&dev->kobj, "device");
+       if (new_parent)
+               error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
+                                         "device");
+       return error;
+#endif
+}
+
+/**
+ * device_move - moves a device to a new parent
+ * @dev: the pointer to the struct device to be moved
+ * @new_parent: the new parent of the device (can by NULL)
+ */
+int device_move(struct device *dev, struct device *new_parent)
+{
+       int error;
+       struct device *old_parent;
+       struct kobject *new_parent_kobj;
+
+       dev = get_device(dev);
+       if (!dev)
+               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;
+       }
+       pr_debug("DEVICE: moving '%s' to '%s'\n", dev->bus_id,
+                new_parent ? new_parent->bus_id : "<NULL>");
+       error = kobject_move(&dev->kobj, new_parent_kobj);
+       if (error) {
+               put_device(new_parent);
+               goto out;
+       }
+       old_parent = dev->parent;
+       dev->parent = new_parent;
+       if (old_parent)
+               klist_remove(&dev->knode_parent);
+       if (new_parent)
+               klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
+       if (!dev->class)
+               goto out_put;
+       error = device_move_class_links(dev, old_parent, new_parent);
+       if (error) {
+               /* We ignore errors on cleanup since we're hosed anyway... */
+               device_move_class_links(dev, new_parent, old_parent);
+               if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
+                       if (new_parent)
+                               klist_remove(&dev->knode_parent);
+                       if (old_parent)
+                               klist_add_tail(&dev->knode_parent,
+                                              &old_parent->klist_children);
+               }
+               put_device(new_parent);
+               goto out;
+       }
+out_put:
+       put_device(old_parent);
+out:
+       put_device(dev);
+       return error;
+}
+
+EXPORT_SYMBOL_GPL(device_move);