diff options
author | Cornelia Huck <cornelia.huck@de.ibm.com> | 2006-11-20 11:07:51 -0500 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@suse.de> | 2006-12-01 17:52:01 -0500 |
commit | 8a82472f86bf693b8e91ed56c9ca4f62fbbdcfa3 (patch) | |
tree | 79d148ee548f4b57e6f5a4a69cf6cdb81e7a1bf2 /drivers/base | |
parent | af9e0765362151b27372c14d9d6dc417184182d3 (diff) |
driver core: Introduce device_move(): move a device to a new parent.
Provide a function device_move() to move a device to a new parent device. Add
auxilliary functions kobject_move() and sysfs_move_dir().
kobject_move() generates a new uevent of type KOBJ_MOVE, containing the
previous path (DEVPATH_OLD) in addition to the usual values. For this, a new
interface kobject_uevent_env() is created that allows to add further
environmental data to the uevent at the kobject layer.
Signed-off-by: Cornelia Huck <cornelia.huck@de.ibm.com>
Acked-by: Kay Sievers <kay.sievers@vrfy.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'drivers/base')
-rw-r--r-- | drivers/base/core.c | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/drivers/base/core.c b/drivers/base/core.c index 75b45a10935a..e4eaf46c4d93 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c | |||
@@ -955,3 +955,95 @@ int device_rename(struct device *dev, char *new_name) | |||
955 | 955 | ||
956 | return error; | 956 | return error; |
957 | } | 957 | } |
958 | |||
959 | |||
960 | static int device_move_class_links(struct device *dev, | ||
961 | struct device *old_parent, | ||
962 | struct device *new_parent) | ||
963 | { | ||
964 | #ifdef CONFIG_SYSFS_DEPRECATED | ||
965 | int error; | ||
966 | char *class_name; | ||
967 | |||
968 | class_name = make_class_name(dev->class->name, &dev->kobj); | ||
969 | if (!class_name) { | ||
970 | error = PTR_ERR(class_name); | ||
971 | class_name = NULL; | ||
972 | goto out; | ||
973 | } | ||
974 | if (old_parent) { | ||
975 | sysfs_remove_link(&dev->kobj, "device"); | ||
976 | sysfs_remove_link(&old_parent->kobj, class_name); | ||
977 | } | ||
978 | error = sysfs_create_link(&dev->kobj, &new_parent->kobj, "device"); | ||
979 | if (error) | ||
980 | goto out; | ||
981 | error = sysfs_create_link(&new_parent->kobj, &dev->kobj, class_name); | ||
982 | if (error) | ||
983 | sysfs_remove_link(&dev->kobj, "device"); | ||
984 | out: | ||
985 | kfree(class_name); | ||
986 | return error; | ||
987 | #else | ||
988 | return 0; | ||
989 | #endif | ||
990 | } | ||
991 | |||
992 | /** | ||
993 | * device_move - moves a device to a new parent | ||
994 | * @dev: the pointer to the struct device to be moved | ||
995 | * @new_parent: the new parent of the device | ||
996 | */ | ||
997 | int device_move(struct device *dev, struct device *new_parent) | ||
998 | { | ||
999 | int error; | ||
1000 | struct device *old_parent; | ||
1001 | |||
1002 | dev = get_device(dev); | ||
1003 | if (!dev) | ||
1004 | return -EINVAL; | ||
1005 | |||
1006 | if (!device_is_registered(dev)) { | ||
1007 | error = -EINVAL; | ||
1008 | goto out; | ||
1009 | } | ||
1010 | new_parent = get_device(new_parent); | ||
1011 | if (!new_parent) { | ||
1012 | error = -EINVAL; | ||
1013 | goto out; | ||
1014 | } | ||
1015 | pr_debug("DEVICE: moving '%s' to '%s'\n", dev->bus_id, | ||
1016 | new_parent->bus_id); | ||
1017 | error = kobject_move(&dev->kobj, &new_parent->kobj); | ||
1018 | if (error) { | ||
1019 | put_device(new_parent); | ||
1020 | goto out; | ||
1021 | } | ||
1022 | old_parent = dev->parent; | ||
1023 | dev->parent = new_parent; | ||
1024 | if (old_parent) | ||
1025 | klist_del(&dev->knode_parent); | ||
1026 | klist_add_tail(&dev->knode_parent, &new_parent->klist_children); | ||
1027 | if (!dev->class) | ||
1028 | goto out_put; | ||
1029 | error = device_move_class_links(dev, old_parent, new_parent); | ||
1030 | if (error) { | ||
1031 | /* We ignore errors on cleanup since we're hosed anyway... */ | ||
1032 | device_move_class_links(dev, new_parent, old_parent); | ||
1033 | if (!kobject_move(&dev->kobj, &old_parent->kobj)) { | ||
1034 | klist_del(&dev->knode_parent); | ||
1035 | if (old_parent) | ||
1036 | klist_add_tail(&dev->knode_parent, | ||
1037 | &old_parent->klist_children); | ||
1038 | } | ||
1039 | put_device(new_parent); | ||
1040 | goto out; | ||
1041 | } | ||
1042 | out_put: | ||
1043 | put_device(old_parent); | ||
1044 | out: | ||
1045 | put_device(dev); | ||
1046 | return error; | ||
1047 | } | ||
1048 | |||
1049 | EXPORT_SYMBOL_GPL(device_move); | ||