2 * drivers/base/core.c - core driver model code (device registration, etc)
4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs
6 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (c) 2006 Novell, Inc.
9 * This file is released under the GPLv2
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <linux/kdev_t.h>
20 #include <linux/notifier.h>
22 #include <asm/semaphore.h>
25 #include "power/power.h"
27 int (*platform_notify)(struct device * dev) = NULL;
28 int (*platform_notify_remove)(struct device * dev) = NULL;
31 * sysfs bindings for devices.
35 * dev_driver_string - Return a device's driver name, if at all possible
36 * @dev: struct device to get the name of
38 * Will return the device's driver's name if it is bound to a device. If
39 * the device is not bound to a device, it will return the name of the bus
40 * it is attached to. If it is not attached to a bus either, an empty
41 * string will be returned.
43 const char *dev_driver_string(struct device *dev)
45 return dev->driver ? dev->driver->name :
46 (dev->bus ? dev->bus->name : "");
48 EXPORT_SYMBOL(dev_driver_string);
50 #define to_dev(obj) container_of(obj, struct device, kobj)
51 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
54 dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
56 struct device_attribute * dev_attr = to_dev_attr(attr);
57 struct device * dev = to_dev(kobj);
61 ret = dev_attr->show(dev, dev_attr, buf);
66 dev_attr_store(struct kobject * kobj, struct attribute * attr,
67 const char * buf, size_t count)
69 struct device_attribute * dev_attr = to_dev_attr(attr);
70 struct device * dev = to_dev(kobj);
74 ret = dev_attr->store(dev, dev_attr, buf, count);
78 static struct sysfs_ops dev_sysfs_ops = {
79 .show = dev_attr_show,
80 .store = dev_attr_store,
85 * device_release - free device structure.
86 * @kobj: device's kobject.
88 * This is called once the reference count for the object
89 * reaches 0. We forward the call to the device's release
90 * method, which should handle actually freeing the structure.
92 static void device_release(struct kobject * kobj)
94 struct device * dev = to_dev(kobj);
98 else if (dev->type && dev->type->release)
99 dev->type->release(dev);
100 else if (dev->class && dev->class->dev_release)
101 dev->class->dev_release(dev);
103 printk(KERN_ERR "Device '%s' does not have a release() function, "
104 "it is broken and must be fixed.\n",
110 static struct kobj_type ktype_device = {
111 .release = device_release,
112 .sysfs_ops = &dev_sysfs_ops,
116 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
118 struct kobj_type *ktype = get_ktype(kobj);
120 if (ktype == &ktype_device) {
121 struct device *dev = to_dev(kobj);
130 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
132 struct device *dev = to_dev(kobj);
135 return dev->bus->name;
137 return dev->class->name;
141 static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
142 int num_envp, char *buffer, int buffer_size)
144 struct device *dev = to_dev(kobj);
149 /* add the major/minor if present */
150 if (MAJOR(dev->devt)) {
151 add_uevent_var(envp, num_envp, &i,
152 buffer, buffer_size, &length,
153 "MAJOR=%u", MAJOR(dev->devt));
154 add_uevent_var(envp, num_envp, &i,
155 buffer, buffer_size, &length,
156 "MINOR=%u", MINOR(dev->devt));
160 add_uevent_var(envp, num_envp, &i,
161 buffer, buffer_size, &length,
162 "DRIVER=%s", dev->driver->name);
164 #ifdef CONFIG_SYSFS_DEPRECATED
166 struct device *parent = dev->parent;
168 /* find first bus device in parent chain */
169 while (parent && !parent->bus)
170 parent = parent->parent;
171 if (parent && parent->bus) {
174 path = kobject_get_path(&parent->kobj, GFP_KERNEL);
175 add_uevent_var(envp, num_envp, &i,
176 buffer, buffer_size, &length,
177 "PHYSDEVPATH=%s", path);
180 add_uevent_var(envp, num_envp, &i,
181 buffer, buffer_size, &length,
182 "PHYSDEVBUS=%s", parent->bus->name);
185 add_uevent_var(envp, num_envp, &i,
186 buffer, buffer_size, &length,
187 "PHYSDEVDRIVER=%s", parent->driver->name);
189 } else if (dev->bus) {
190 add_uevent_var(envp, num_envp, &i,
191 buffer, buffer_size, &length,
192 "PHYSDEVBUS=%s", dev->bus->name);
195 add_uevent_var(envp, num_envp, &i,
196 buffer, buffer_size, &length,
197 "PHYSDEVDRIVER=%s", dev->driver->name);
201 /* terminate, set to next free slot, shrink available space */
205 buffer = &buffer[length];
206 buffer_size -= length;
208 if (dev->bus && dev->bus->uevent) {
209 /* have the bus specific function add its stuff */
210 retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size);
212 pr_debug ("%s: bus uevent() returned %d\n",
213 __FUNCTION__, retval);
216 if (dev->class && dev->class->dev_uevent) {
217 /* have the class specific function add its stuff */
218 retval = dev->class->dev_uevent(dev, envp, num_envp, buffer, buffer_size);
220 pr_debug("%s: class uevent() returned %d\n",
221 __FUNCTION__, retval);
224 if (dev->type && dev->type->uevent) {
225 /* have the device type specific fuction add its stuff */
226 retval = dev->type->uevent(dev, envp, num_envp, buffer, buffer_size);
228 pr_debug("%s: dev_type uevent() returned %d\n",
229 __FUNCTION__, retval);
235 static struct kset_uevent_ops device_uevent_ops = {
236 .filter = dev_uevent_filter,
237 .name = dev_uevent_name,
238 .uevent = dev_uevent,
241 static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
242 const char *buf, size_t count)
244 kobject_uevent(&dev->kobj, KOBJ_ADD);
248 static int device_add_groups(struct device *dev)
254 for (i = 0; dev->groups[i]; i++) {
255 error = sysfs_create_group(&dev->kobj, dev->groups[i]);
258 sysfs_remove_group(&dev->kobj, dev->groups[i]);
267 static void device_remove_groups(struct device *dev)
271 for (i = 0; dev->groups[i]; i++) {
272 sysfs_remove_group(&dev->kobj, dev->groups[i]);
277 static int device_add_attrs(struct device *dev)
279 struct class *class = dev->class;
280 struct device_type *type = dev->type;
284 if (class && class->dev_attrs) {
285 for (i = 0; attr_name(class->dev_attrs[i]); i++) {
286 error = device_create_file(dev, &class->dev_attrs[i]);
292 device_remove_file(dev, &class->dev_attrs[i]);
295 if (type && type->attrs) {
296 for (i = 0; attr_name(type->attrs[i]); i++) {
297 error = device_create_file(dev, &type->attrs[i]);
303 device_remove_file(dev, &type->attrs[i]);
309 static void device_remove_attrs(struct device *dev)
311 struct class *class = dev->class;
312 struct device_type *type = dev->type;
315 if (class && class->dev_attrs) {
316 for (i = 0; attr_name(class->dev_attrs[i]); i++)
317 device_remove_file(dev, &class->dev_attrs[i]);
320 if (type && type->attrs) {
321 for (i = 0; attr_name(type->attrs[i]); i++)
322 device_remove_file(dev, &type->attrs[i]);
327 static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
330 return print_dev_t(buf, dev->devt);
334 * devices_subsys - structure to be registered with kobject core.
337 decl_subsys(devices, &ktype_device, &device_uevent_ops);
341 * device_create_file - create sysfs attribute file for device.
343 * @attr: device attribute descriptor.
346 int device_create_file(struct device * dev, struct device_attribute * attr)
349 if (get_device(dev)) {
350 error = sysfs_create_file(&dev->kobj, &attr->attr);
357 * device_remove_file - remove sysfs attribute file.
359 * @attr: device attribute descriptor.
362 void device_remove_file(struct device * dev, struct device_attribute * attr)
364 if (get_device(dev)) {
365 sysfs_remove_file(&dev->kobj, &attr->attr);
371 * device_create_bin_file - create sysfs binary attribute file for device.
373 * @attr: device binary attribute descriptor.
375 int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
379 error = sysfs_create_bin_file(&dev->kobj, attr);
382 EXPORT_SYMBOL_GPL(device_create_bin_file);
385 * device_remove_bin_file - remove sysfs binary attribute file
387 * @attr: device binary attribute descriptor.
389 void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
392 sysfs_remove_bin_file(&dev->kobj, attr);
394 EXPORT_SYMBOL_GPL(device_remove_bin_file);
396 static void klist_children_get(struct klist_node *n)
398 struct device *dev = container_of(n, struct device, knode_parent);
403 static void klist_children_put(struct klist_node *n)
405 struct device *dev = container_of(n, struct device, knode_parent);
412 * device_initialize - init device structure.
415 * This prepares the device for use by other layers,
416 * including adding it to the device hierarchy.
417 * It is the first half of device_register(), if called by
418 * that, though it can also be called separately, so one
419 * may use @dev's fields (e.g. the refcount).
422 void device_initialize(struct device *dev)
424 kobj_set_kset_s(dev, devices_subsys);
425 kobject_init(&dev->kobj);
426 klist_init(&dev->klist_children, klist_children_get,
428 INIT_LIST_HEAD(&dev->dma_pools);
429 INIT_LIST_HEAD(&dev->node);
430 init_MUTEX(&dev->sem);
431 device_init_wakeup(dev, 0);
432 set_dev_node(dev, -1);
435 #ifdef CONFIG_SYSFS_DEPRECATED
436 static struct kobject * get_device_parent(struct device *dev,
437 struct device *parent)
439 /* Set the parent to the class, not the parent device */
440 /* this keeps sysfs from having a symlink to make old udevs happy */
442 return &dev->class->subsys.kset.kobj;
444 return &parent->kobj;
449 static struct kobject * virtual_device_parent(struct device *dev)
452 return ERR_PTR(-ENODEV);
454 if (!dev->class->virtual_dir) {
455 static struct kobject *virtual_dir = NULL;
458 virtual_dir = kobject_add_dir(&devices_subsys.kset.kobj, "virtual");
459 dev->class->virtual_dir = kobject_add_dir(virtual_dir, dev->class->name);
462 return dev->class->virtual_dir;
465 static struct kobject * get_device_parent(struct device *dev,
466 struct device *parent)
468 /* if this is a class device, and has no parent, create one */
469 if ((dev->class) && (parent == NULL)) {
470 return virtual_device_parent(dev);
472 return &parent->kobj;
477 static int setup_parent(struct device *dev, struct device *parent)
479 struct kobject *kobj;
480 kobj = get_device_parent(dev, parent);
482 return PTR_ERR(kobj);
484 dev->kobj.parent = kobj;
489 * device_add - add device to device hierarchy.
492 * This is part 2 of device_register(), though may be called
493 * separately _iff_ device_initialize() has been called separately.
495 * This adds it to the kobject hierarchy via kobject_add(), adds it
496 * to the global and sibling lists for the device, then
497 * adds it to the other relevant subsystems of the driver model.
499 int device_add(struct device *dev)
501 struct device *parent = NULL;
502 char *class_name = NULL;
503 struct class_interface *class_intf;
506 dev = get_device(dev);
507 if (!dev || !strlen(dev->bus_id))
510 pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
512 parent = get_device(dev->parent);
514 error = setup_parent(dev, parent);
518 /* first, register with generic layer. */
519 kobject_set_name(&dev->kobj, "%s", dev->bus_id);
520 error = kobject_add(&dev->kobj);
524 /* notify platform of device entry */
526 platform_notify(dev);
528 /* notify clients of device entry (new way) */
530 blocking_notifier_call_chain(&dev->bus->bus_notifier,
531 BUS_NOTIFY_ADD_DEVICE, dev);
533 dev->uevent_attr.attr.name = "uevent";
534 dev->uevent_attr.attr.mode = S_IWUSR;
536 dev->uevent_attr.attr.owner = dev->driver->owner;
537 dev->uevent_attr.store = store_uevent;
538 error = device_create_file(dev, &dev->uevent_attr);
542 if (MAJOR(dev->devt)) {
543 struct device_attribute *attr;
544 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
547 goto ueventattrError;
549 attr->attr.name = "dev";
550 attr->attr.mode = S_IRUGO;
552 attr->attr.owner = dev->driver->owner;
553 attr->show = show_dev;
554 error = device_create_file(dev, attr);
557 goto ueventattrError;
560 dev->devt_attr = attr;
564 sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
566 /* If this is not a "fake" compatible device, then create the
567 * symlink from the class to the device. */
568 if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
569 sysfs_create_link(&dev->class->subsys.kset.kobj,
570 &dev->kobj, dev->bus_id);
571 #ifdef CONFIG_SYSFS_DEPRECATED
573 sysfs_create_link(&dev->kobj, &dev->parent->kobj,
575 class_name = make_class_name(dev->class->name,
578 sysfs_create_link(&dev->parent->kobj,
579 &dev->kobj, class_name);
584 if ((error = device_add_attrs(dev)))
586 if ((error = device_add_groups(dev)))
588 if ((error = device_pm_add(dev)))
590 if ((error = bus_add_device(dev)))
592 if (!dev->uevent_suppress)
593 kobject_uevent(&dev->kobj, KOBJ_ADD);
594 if ((error = bus_attach_device(dev)))
597 klist_add_tail(&dev->knode_parent, &parent->klist_children);
600 down(&dev->class->sem);
601 /* tie the class to the device */
602 list_add_tail(&dev->node, &dev->class->devices);
604 /* notify any interfaces that the device is here */
605 list_for_each_entry(class_intf, &dev->class->interfaces, node)
606 if (class_intf->add_dev)
607 class_intf->add_dev(dev, class_intf);
608 up(&dev->class->sem);
615 bus_remove_device(dev);
617 device_pm_remove(dev);
620 blocking_notifier_call_chain(&dev->bus->bus_notifier,
621 BUS_NOTIFY_DEL_DEVICE, dev);
622 device_remove_groups(dev);
624 device_remove_attrs(dev);
626 if (dev->devt_attr) {
627 device_remove_file(dev, dev->devt_attr);
628 kfree(dev->devt_attr);
631 device_remove_file(dev, &dev->uevent_attr);
633 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
634 kobject_del(&dev->kobj);
643 * device_register - register a device with the system.
644 * @dev: pointer to the device structure
646 * This happens in two clean steps - initialize the device
647 * and add it to the system. The two steps can be called
648 * separately, but this is the easiest and most common.
649 * I.e. you should only call the two helpers separately if
650 * have a clearly defined need to use and refcount the device
651 * before it is added to the hierarchy.
654 int device_register(struct device *dev)
656 device_initialize(dev);
657 return device_add(dev);
662 * get_device - increment reference count for device.
665 * This simply forwards the call to kobject_get(), though
666 * we do take care to provide for the case that we get a NULL
670 struct device * get_device(struct device * dev)
672 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
677 * put_device - decrement reference count.
678 * @dev: device in question.
680 void put_device(struct device * dev)
683 kobject_put(&dev->kobj);
688 * device_del - delete device from system.
691 * This is the first part of the device unregistration
692 * sequence. This removes the device from the lists we control
693 * from here, has it removed from the other driver model
694 * subsystems it was added to in device_add(), and removes it
695 * from the kobject hierarchy.
697 * NOTE: this should be called manually _iff_ device_add() was
698 * also called manually.
701 void device_del(struct device * dev)
703 struct device * parent = dev->parent;
704 struct class_interface *class_intf;
707 klist_del(&dev->knode_parent);
708 if (dev->devt_attr) {
709 device_remove_file(dev, dev->devt_attr);
710 kfree(dev->devt_attr);
713 sysfs_remove_link(&dev->kobj, "subsystem");
714 /* If this is not a "fake" compatible device, remove the
715 * symlink from the class to the device. */
716 if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
717 sysfs_remove_link(&dev->class->subsys.kset.kobj,
719 #ifdef CONFIG_SYSFS_DEPRECATED
721 char *class_name = make_class_name(dev->class->name,
724 sysfs_remove_link(&dev->parent->kobj,
727 sysfs_remove_link(&dev->kobj, "device");
731 down(&dev->class->sem);
732 /* notify any interfaces that the device is now gone */
733 list_for_each_entry(class_intf, &dev->class->interfaces, node)
734 if (class_intf->remove_dev)
735 class_intf->remove_dev(dev, class_intf);
736 /* remove the device from the class list */
737 list_del_init(&dev->node);
738 up(&dev->class->sem);
740 device_remove_file(dev, &dev->uevent_attr);
741 device_remove_groups(dev);
742 device_remove_attrs(dev);
743 bus_remove_device(dev);
745 /* Notify the platform of the removal, in case they
746 * need to do anything...
748 if (platform_notify_remove)
749 platform_notify_remove(dev);
751 blocking_notifier_call_chain(&dev->bus->bus_notifier,
752 BUS_NOTIFY_DEL_DEVICE, dev);
753 device_pm_remove(dev);
754 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
755 kobject_del(&dev->kobj);
761 * device_unregister - unregister device from system.
762 * @dev: device going away.
764 * We do this in two parts, like we do device_register(). First,
765 * we remove it from all the subsystems with device_del(), then
766 * we decrement the reference count via put_device(). If that
767 * is the final reference count, the device will be cleaned up
768 * via device_release() above. Otherwise, the structure will
769 * stick around until the final reference to the device is dropped.
771 void device_unregister(struct device * dev)
773 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
779 static struct device * next_device(struct klist_iter * i)
781 struct klist_node * n = klist_next(i);
782 return n ? container_of(n, struct device, knode_parent) : NULL;
786 * device_for_each_child - device child iterator.
787 * @parent: parent struct device.
788 * @data: data for the callback.
789 * @fn: function to be called for each device.
791 * Iterate over @parent's child devices, and call @fn for each,
794 * We check the return of @fn each time. If it returns anything
795 * other than 0, we break out and return that value.
797 int device_for_each_child(struct device * parent, void * data,
798 int (*fn)(struct device *, void *))
801 struct device * child;
804 klist_iter_init(&parent->klist_children, &i);
805 while ((child = next_device(&i)) && !error)
806 error = fn(child, data);
812 * device_find_child - device iterator for locating a particular device.
813 * @parent: parent struct device
814 * @data: Data to pass to match function
815 * @match: Callback function to check device
817 * This is similar to the device_for_each_child() function above, but it
818 * returns a reference to a device that is 'found' for later use, as
819 * determined by the @match callback.
821 * The callback should return 0 if the device doesn't match and non-zero
822 * if it does. If the callback returns non-zero and a reference to the
823 * current device can be obtained, this function will return to the caller
824 * and not iterate over any more devices.
826 struct device * device_find_child(struct device *parent, void *data,
827 int (*match)(struct device *, void *))
830 struct device *child;
835 klist_iter_init(&parent->klist_children, &i);
836 while ((child = next_device(&i)))
837 if (match(child, data) && get_device(child))
843 int __init devices_init(void)
845 return subsystem_register(&devices_subsys);
848 EXPORT_SYMBOL_GPL(device_for_each_child);
849 EXPORT_SYMBOL_GPL(device_find_child);
851 EXPORT_SYMBOL_GPL(device_initialize);
852 EXPORT_SYMBOL_GPL(device_add);
853 EXPORT_SYMBOL_GPL(device_register);
855 EXPORT_SYMBOL_GPL(device_del);
856 EXPORT_SYMBOL_GPL(device_unregister);
857 EXPORT_SYMBOL_GPL(get_device);
858 EXPORT_SYMBOL_GPL(put_device);
860 EXPORT_SYMBOL_GPL(device_create_file);
861 EXPORT_SYMBOL_GPL(device_remove_file);
864 static void device_create_release(struct device *dev)
866 pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
871 * device_create - creates a device and registers it with sysfs
872 * @class: pointer to the struct class that this device should be registered to
873 * @parent: pointer to the parent struct device of this new device, if any
874 * @devt: the dev_t for the char device to be added
875 * @fmt: string for the device's name
877 * This function can be used by char device classes. A struct device
878 * will be created in sysfs, registered to the specified class.
880 * A "dev" file will be created, showing the dev_t for the device, if
881 * the dev_t is not 0,0.
882 * If a pointer to a parent struct device is passed in, the newly created
883 * struct device will be a child of that device in sysfs.
884 * The pointer to the struct device will be returned from the call.
885 * Any further sysfs files that might be required can be created using this
888 * Note: the struct class passed to this function must have previously
889 * been created with a call to class_create().
891 struct device *device_create(struct class *class, struct device *parent,
892 dev_t devt, const char *fmt, ...)
895 struct device *dev = NULL;
896 int retval = -ENODEV;
898 if (class == NULL || IS_ERR(class))
901 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
909 dev->parent = parent;
910 dev->release = device_create_release;
913 vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
915 retval = device_register(dev);
923 return ERR_PTR(retval);
925 EXPORT_SYMBOL_GPL(device_create);
928 * device_destroy - removes a device that was created with device_create()
929 * @class: pointer to the struct class that this device was registered with
930 * @devt: the dev_t of the device that was previously registered
932 * This call unregisters and cleans up a device that was created with a
933 * call to device_create().
935 void device_destroy(struct class *class, dev_t devt)
937 struct device *dev = NULL;
938 struct device *dev_tmp;
941 list_for_each_entry(dev_tmp, &class->devices, node) {
942 if (dev_tmp->devt == devt) {
950 device_unregister(dev);
952 EXPORT_SYMBOL_GPL(device_destroy);
955 * device_rename - renames a device
956 * @dev: the pointer to the struct device to be renamed
957 * @new_name: the new name of the device
959 int device_rename(struct device *dev, char *new_name)
961 char *old_class_name = NULL;
962 char *new_class_name = NULL;
963 char *old_symlink_name = NULL;
966 dev = get_device(dev);
970 pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name);
972 #ifdef CONFIG_SYSFS_DEPRECATED
973 if ((dev->class) && (dev->parent))
974 old_class_name = make_class_name(dev->class->name, &dev->kobj);
978 old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
979 if (!old_symlink_name) {
981 goto out_free_old_class;
983 strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE);
986 strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
988 error = kobject_rename(&dev->kobj, new_name);
990 #ifdef CONFIG_SYSFS_DEPRECATED
991 if (old_class_name) {
992 new_class_name = make_class_name(dev->class->name, &dev->kobj);
993 if (new_class_name) {
994 sysfs_create_link(&dev->parent->kobj, &dev->kobj,
996 sysfs_remove_link(&dev->parent->kobj, old_class_name);
1002 sysfs_remove_link(&dev->class->subsys.kset.kobj,
1004 sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
1009 kfree(new_class_name);
1010 kfree(old_symlink_name);
1012 kfree(old_class_name);
1018 static int device_move_class_links(struct device *dev,
1019 struct device *old_parent,
1020 struct device *new_parent)
1022 #ifdef CONFIG_SYSFS_DEPRECATED
1026 class_name = make_class_name(dev->class->name, &dev->kobj);
1032 sysfs_remove_link(&dev->kobj, "device");
1033 sysfs_remove_link(&old_parent->kobj, class_name);
1036 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1040 error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
1043 sysfs_remove_link(&dev->kobj, "device");
1056 * device_move - moves a device to a new parent
1057 * @dev: the pointer to the struct device to be moved
1058 * @new_parent: the new parent of the device (can by NULL)
1060 int device_move(struct device *dev, struct device *new_parent)
1063 struct device *old_parent;
1064 struct kobject *new_parent_kobj;
1066 dev = get_device(dev);
1070 new_parent = get_device(new_parent);
1071 new_parent_kobj = get_device_parent (dev, new_parent);
1072 if (IS_ERR(new_parent_kobj)) {
1073 error = PTR_ERR(new_parent_kobj);
1074 put_device(new_parent);
1077 pr_debug("DEVICE: moving '%s' to '%s'\n", dev->bus_id,
1078 new_parent ? new_parent->bus_id : "<NULL>");
1079 error = kobject_move(&dev->kobj, new_parent_kobj);
1081 put_device(new_parent);
1084 old_parent = dev->parent;
1085 dev->parent = new_parent;
1087 klist_remove(&dev->knode_parent);
1089 klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
1092 error = device_move_class_links(dev, old_parent, new_parent);
1094 /* We ignore errors on cleanup since we're hosed anyway... */
1095 device_move_class_links(dev, new_parent, old_parent);
1096 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1098 klist_remove(&dev->knode_parent);
1100 klist_add_tail(&dev->knode_parent,
1101 &old_parent->klist_children);
1103 put_device(new_parent);
1107 put_device(old_parent);
1113 EXPORT_SYMBOL_GPL(device_move);