diff options
-rw-r--r-- | drivers/base/core.c | 55 | ||||
-rw-r--r-- | include/linux/device.h | 1 |
2 files changed, 56 insertions, 0 deletions
diff --git a/drivers/base/core.c b/drivers/base/core.c index f1228f25efe0..0db47561331e 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c | |||
@@ -736,3 +736,58 @@ void device_destroy(struct class *class, dev_t devt) | |||
736 | device_unregister(dev); | 736 | device_unregister(dev); |
737 | } | 737 | } |
738 | EXPORT_SYMBOL_GPL(device_destroy); | 738 | EXPORT_SYMBOL_GPL(device_destroy); |
739 | |||
740 | /** | ||
741 | * device_rename - renames a device | ||
742 | * @dev: the pointer to the struct device to be renamed | ||
743 | * @new_name: the new name of the device | ||
744 | */ | ||
745 | int device_rename(struct device *dev, char *new_name) | ||
746 | { | ||
747 | char *old_class_name = NULL; | ||
748 | char *new_class_name = NULL; | ||
749 | char *old_symlink_name = NULL; | ||
750 | int error; | ||
751 | |||
752 | dev = get_device(dev); | ||
753 | if (!dev) | ||
754 | return -EINVAL; | ||
755 | |||
756 | pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name); | ||
757 | |||
758 | if ((dev->class) && (dev->parent)) | ||
759 | old_class_name = make_class_name(dev->class->name, &dev->kobj); | ||
760 | |||
761 | if (dev->class) { | ||
762 | old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL); | ||
763 | if (!old_symlink_name) | ||
764 | return -ENOMEM; | ||
765 | strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE); | ||
766 | } | ||
767 | |||
768 | strlcpy(dev->bus_id, new_name, BUS_ID_SIZE); | ||
769 | |||
770 | error = kobject_rename(&dev->kobj, new_name); | ||
771 | |||
772 | if (old_class_name) { | ||
773 | new_class_name = make_class_name(dev->class->name, &dev->kobj); | ||
774 | if (new_class_name) { | ||
775 | sysfs_create_link(&dev->parent->kobj, &dev->kobj, | ||
776 | new_class_name); | ||
777 | sysfs_remove_link(&dev->parent->kobj, old_class_name); | ||
778 | } | ||
779 | } | ||
780 | if (dev->class) { | ||
781 | sysfs_remove_link(&dev->class->subsys.kset.kobj, | ||
782 | old_symlink_name); | ||
783 | sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj, | ||
784 | dev->bus_id); | ||
785 | } | ||
786 | put_device(dev); | ||
787 | |||
788 | kfree(old_class_name); | ||
789 | kfree(new_class_name); | ||
790 | kfree(old_symlink_name); | ||
791 | |||
792 | return error; | ||
793 | } | ||
diff --git a/include/linux/device.h b/include/linux/device.h index 3122bd25ce42..3400e09bf458 100644 --- a/include/linux/device.h +++ b/include/linux/device.h | |||
@@ -380,6 +380,7 @@ extern int device_add(struct device * dev); | |||
380 | extern void device_del(struct device * dev); | 380 | extern void device_del(struct device * dev); |
381 | extern int device_for_each_child(struct device *, void *, | 381 | extern int device_for_each_child(struct device *, void *, |
382 | int (*fn)(struct device *, void *)); | 382 | int (*fn)(struct device *, void *)); |
383 | extern int device_rename(struct device *dev, char *new_name); | ||
383 | 384 | ||
384 | /* | 385 | /* |
385 | * Manual binding of a device to driver. See drivers/base/bus.c | 386 | * Manual binding of a device to driver. See drivers/base/bus.c |