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
->class && dev
->class->dev_release
)
99 dev
->class->dev_release(dev
);
101 printk(KERN_ERR
"Device '%s' does not have a release() function, "
102 "it is broken and must be fixed.\n",
108 static struct kobj_type ktype_device
= {
109 .release
= device_release
,
110 .sysfs_ops
= &dev_sysfs_ops
,
114 static int dev_uevent_filter(struct kset
*kset
, struct kobject
*kobj
)
116 struct kobj_type
*ktype
= get_ktype(kobj
);
118 if (ktype
== &ktype_device
) {
119 struct device
*dev
= to_dev(kobj
);
128 static const char *dev_uevent_name(struct kset
*kset
, struct kobject
*kobj
)
130 struct device
*dev
= to_dev(kobj
);
133 return dev
->bus
->name
;
135 return dev
->class->name
;
139 static int dev_uevent(struct kset
*kset
, struct kobject
*kobj
, char **envp
,
140 int num_envp
, char *buffer
, int buffer_size
)
142 struct device
*dev
= to_dev(kobj
);
147 /* add the major/minor if present */
148 if (MAJOR(dev
->devt
)) {
149 add_uevent_var(envp
, num_envp
, &i
,
150 buffer
, buffer_size
, &length
,
151 "MAJOR=%u", MAJOR(dev
->devt
));
152 add_uevent_var(envp
, num_envp
, &i
,
153 buffer
, buffer_size
, &length
,
154 "MINOR=%u", MINOR(dev
->devt
));
158 add_uevent_var(envp
, num_envp
, &i
,
159 buffer
, buffer_size
, &length
,
160 "DRIVER=%s", dev
->driver
->name
);
162 #ifdef CONFIG_SYSFS_DEPRECATED
164 struct device
*parent
= dev
->parent
;
166 /* find first bus device in parent chain */
167 while (parent
&& !parent
->bus
)
168 parent
= parent
->parent
;
169 if (parent
&& parent
->bus
) {
172 path
= kobject_get_path(&parent
->kobj
, GFP_KERNEL
);
173 add_uevent_var(envp
, num_envp
, &i
,
174 buffer
, buffer_size
, &length
,
175 "PHYSDEVPATH=%s", path
);
178 add_uevent_var(envp
, num_envp
, &i
,
179 buffer
, buffer_size
, &length
,
180 "PHYSDEVBUS=%s", parent
->bus
->name
);
183 add_uevent_var(envp
, num_envp
, &i
,
184 buffer
, buffer_size
, &length
,
185 "PHYSDEVDRIVER=%s", parent
->driver
->name
);
187 } else if (dev
->bus
) {
188 add_uevent_var(envp
, num_envp
, &i
,
189 buffer
, buffer_size
, &length
,
190 "PHYSDEVBUS=%s", dev
->bus
->name
);
193 add_uevent_var(envp
, num_envp
, &i
,
194 buffer
, buffer_size
, &length
,
195 "PHYSDEVDRIVER=%s", dev
->driver
->name
);
199 /* terminate, set to next free slot, shrink available space */
203 buffer
= &buffer
[length
];
204 buffer_size
-= length
;
206 if (dev
->bus
&& dev
->bus
->uevent
) {
207 /* have the bus specific function add its stuff */
208 retval
= dev
->bus
->uevent(dev
, envp
, num_envp
, buffer
, buffer_size
);
210 pr_debug ("%s - uevent() returned %d\n",
211 __FUNCTION__
, retval
);
215 if (dev
->class && dev
->class->dev_uevent
) {
216 /* have the class specific function add its stuff */
217 retval
= dev
->class->dev_uevent(dev
, envp
, num_envp
, buffer
, buffer_size
);
219 pr_debug("%s - dev_uevent() returned %d\n",
220 __FUNCTION__
, retval
);
227 static struct kset_uevent_ops device_uevent_ops
= {
228 .filter
= dev_uevent_filter
,
229 .name
= dev_uevent_name
,
230 .uevent
= dev_uevent
,
233 static ssize_t
store_uevent(struct device
*dev
, struct device_attribute
*attr
,
234 const char *buf
, size_t count
)
236 kobject_uevent(&dev
->kobj
, KOBJ_ADD
);
240 static int device_add_groups(struct device
*dev
)
246 for (i
= 0; dev
->groups
[i
]; i
++) {
247 error
= sysfs_create_group(&dev
->kobj
, dev
->groups
[i
]);
250 sysfs_remove_group(&dev
->kobj
, dev
->groups
[i
]);
259 static void device_remove_groups(struct device
*dev
)
263 for (i
= 0; dev
->groups
[i
]; i
++) {
264 sysfs_remove_group(&dev
->kobj
, dev
->groups
[i
]);
269 static int device_add_attrs(struct device
*dev
)
271 struct class *class = dev
->class;
278 if (class->dev_attrs
) {
279 for (i
= 0; attr_name(class->dev_attrs
[i
]); i
++) {
280 error
= device_create_file(dev
, &class->dev_attrs
[i
]);
287 device_remove_file(dev
, &class->dev_attrs
[i
]);
291 static void device_remove_attrs(struct device
*dev
)
293 struct class *class = dev
->class;
299 if (class->dev_attrs
) {
300 for (i
= 0; attr_name(class->dev_attrs
[i
]); i
++)
301 device_remove_file(dev
, &class->dev_attrs
[i
]);
306 static ssize_t
show_dev(struct device
*dev
, struct device_attribute
*attr
,
309 return print_dev_t(buf
, dev
->devt
);
313 * devices_subsys - structure to be registered with kobject core.
316 decl_subsys(devices
, &ktype_device
, &device_uevent_ops
);
320 * device_create_file - create sysfs attribute file for device.
322 * @attr: device attribute descriptor.
325 int device_create_file(struct device
* dev
, struct device_attribute
* attr
)
328 if (get_device(dev
)) {
329 error
= sysfs_create_file(&dev
->kobj
, &attr
->attr
);
336 * device_remove_file - remove sysfs attribute file.
338 * @attr: device attribute descriptor.
341 void device_remove_file(struct device
* dev
, struct device_attribute
* attr
)
343 if (get_device(dev
)) {
344 sysfs_remove_file(&dev
->kobj
, &attr
->attr
);
350 * device_create_bin_file - create sysfs binary attribute file for device.
352 * @attr: device binary attribute descriptor.
354 int device_create_bin_file(struct device
*dev
, struct bin_attribute
*attr
)
358 error
= sysfs_create_bin_file(&dev
->kobj
, attr
);
361 EXPORT_SYMBOL_GPL(device_create_bin_file
);
364 * device_remove_bin_file - remove sysfs binary attribute file
366 * @attr: device binary attribute descriptor.
368 void device_remove_bin_file(struct device
*dev
, struct bin_attribute
*attr
)
371 sysfs_remove_bin_file(&dev
->kobj
, attr
);
373 EXPORT_SYMBOL_GPL(device_remove_bin_file
);
375 static void klist_children_get(struct klist_node
*n
)
377 struct device
*dev
= container_of(n
, struct device
, knode_parent
);
382 static void klist_children_put(struct klist_node
*n
)
384 struct device
*dev
= container_of(n
, struct device
, knode_parent
);
391 * device_initialize - init device structure.
394 * This prepares the device for use by other layers,
395 * including adding it to the device hierarchy.
396 * It is the first half of device_register(), if called by
397 * that, though it can also be called separately, so one
398 * may use @dev's fields (e.g. the refcount).
401 void device_initialize(struct device
*dev
)
403 kobj_set_kset_s(dev
, devices_subsys
);
404 kobject_init(&dev
->kobj
);
405 klist_init(&dev
->klist_children
, klist_children_get
,
407 INIT_LIST_HEAD(&dev
->dma_pools
);
408 INIT_LIST_HEAD(&dev
->node
);
409 init_MUTEX(&dev
->sem
);
410 device_init_wakeup(dev
, 0);
411 set_dev_node(dev
, -1);
414 #ifdef CONFIG_SYSFS_DEPRECATED
415 static struct kobject
* get_device_parent(struct device
*dev
,
416 struct device
*parent
)
418 /* Set the parent to the class, not the parent device */
419 /* this keeps sysfs from having a symlink to make old udevs happy */
421 return &dev
->class->subsys
.kset
.kobj
;
423 return &parent
->kobj
;
428 static struct kobject
* virtual_device_parent(struct device
*dev
)
431 return ERR_PTR(-ENODEV
);
433 if (!dev
->class->virtual_dir
) {
434 static struct kobject
*virtual_dir
= NULL
;
437 virtual_dir
= kobject_add_dir(&devices_subsys
.kset
.kobj
, "virtual");
438 dev
->class->virtual_dir
= kobject_add_dir(virtual_dir
, dev
->class->name
);
441 return dev
->class->virtual_dir
;
444 static struct kobject
* get_device_parent(struct device
*dev
,
445 struct device
*parent
)
447 /* if this is a class device, and has no parent, create one */
448 if ((dev
->class) && (parent
== NULL
)) {
449 return virtual_device_parent(dev
);
451 return &parent
->kobj
;
456 static int setup_parent(struct device
*dev
, struct device
*parent
)
458 struct kobject
*kobj
;
459 kobj
= get_device_parent(dev
, parent
);
461 return PTR_ERR(kobj
);
463 dev
->kobj
.parent
= kobj
;
468 * device_add - add device to device hierarchy.
471 * This is part 2 of device_register(), though may be called
472 * separately _iff_ device_initialize() has been called separately.
474 * This adds it to the kobject hierarchy via kobject_add(), adds it
475 * to the global and sibling lists for the device, then
476 * adds it to the other relevant subsystems of the driver model.
478 int device_add(struct device
*dev
)
480 struct device
*parent
= NULL
;
481 char *class_name
= NULL
;
482 struct class_interface
*class_intf
;
485 dev
= get_device(dev
);
486 if (!dev
|| !strlen(dev
->bus_id
))
489 pr_debug("DEV: registering device: ID = '%s'\n", dev
->bus_id
);
491 parent
= get_device(dev
->parent
);
493 error
= setup_parent(dev
, parent
);
497 /* first, register with generic layer. */
498 kobject_set_name(&dev
->kobj
, "%s", dev
->bus_id
);
499 error
= kobject_add(&dev
->kobj
);
503 /* notify platform of device entry */
505 platform_notify(dev
);
507 /* notify clients of device entry (new way) */
509 blocking_notifier_call_chain(&dev
->bus
->bus_notifier
,
510 BUS_NOTIFY_ADD_DEVICE
, dev
);
512 dev
->uevent_attr
.attr
.name
= "uevent";
513 dev
->uevent_attr
.attr
.mode
= S_IWUSR
;
515 dev
->uevent_attr
.attr
.owner
= dev
->driver
->owner
;
516 dev
->uevent_attr
.store
= store_uevent
;
517 error
= device_create_file(dev
, &dev
->uevent_attr
);
521 if (MAJOR(dev
->devt
)) {
522 struct device_attribute
*attr
;
523 attr
= kzalloc(sizeof(*attr
), GFP_KERNEL
);
526 goto ueventattrError
;
528 attr
->attr
.name
= "dev";
529 attr
->attr
.mode
= S_IRUGO
;
531 attr
->attr
.owner
= dev
->driver
->owner
;
532 attr
->show
= show_dev
;
533 error
= device_create_file(dev
, attr
);
536 goto ueventattrError
;
539 dev
->devt_attr
= attr
;
543 sysfs_create_link(&dev
->kobj
, &dev
->class->subsys
.kset
.kobj
,
545 /* If this is not a "fake" compatible device, then create the
546 * symlink from the class to the device. */
547 if (dev
->kobj
.parent
!= &dev
->class->subsys
.kset
.kobj
)
548 sysfs_create_link(&dev
->class->subsys
.kset
.kobj
,
549 &dev
->kobj
, dev
->bus_id
);
550 #ifdef CONFIG_SYSFS_DEPRECATED
552 sysfs_create_link(&dev
->kobj
, &dev
->parent
->kobj
,
554 class_name
= make_class_name(dev
->class->name
,
557 sysfs_create_link(&dev
->parent
->kobj
,
558 &dev
->kobj
, class_name
);
563 if ((error
= device_add_attrs(dev
)))
565 if ((error
= device_add_groups(dev
)))
567 if ((error
= device_pm_add(dev
)))
569 if ((error
= bus_add_device(dev
)))
571 kobject_uevent(&dev
->kobj
, KOBJ_ADD
);
572 if ((error
= bus_attach_device(dev
)))
575 klist_add_tail(&dev
->knode_parent
, &parent
->klist_children
);
578 down(&dev
->class->sem
);
579 /* tie the class to the device */
580 list_add_tail(&dev
->node
, &dev
->class->devices
);
582 /* notify any interfaces that the device is here */
583 list_for_each_entry(class_intf
, &dev
->class->interfaces
, node
)
584 if (class_intf
->add_dev
)
585 class_intf
->add_dev(dev
, class_intf
);
586 up(&dev
->class->sem
);
593 bus_remove_device(dev
);
595 device_pm_remove(dev
);
598 blocking_notifier_call_chain(&dev
->bus
->bus_notifier
,
599 BUS_NOTIFY_DEL_DEVICE
, dev
);
600 device_remove_groups(dev
);
602 device_remove_attrs(dev
);
604 if (dev
->devt_attr
) {
605 device_remove_file(dev
, dev
->devt_attr
);
606 kfree(dev
->devt_attr
);
609 device_remove_file(dev
, &dev
->uevent_attr
);
611 kobject_uevent(&dev
->kobj
, KOBJ_REMOVE
);
612 kobject_del(&dev
->kobj
);
621 * device_register - register a device with the system.
622 * @dev: pointer to the device structure
624 * This happens in two clean steps - initialize the device
625 * and add it to the system. The two steps can be called
626 * separately, but this is the easiest and most common.
627 * I.e. you should only call the two helpers separately if
628 * have a clearly defined need to use and refcount the device
629 * before it is added to the hierarchy.
632 int device_register(struct device
*dev
)
634 device_initialize(dev
);
635 return device_add(dev
);
640 * get_device - increment reference count for device.
643 * This simply forwards the call to kobject_get(), though
644 * we do take care to provide for the case that we get a NULL
648 struct device
* get_device(struct device
* dev
)
650 return dev
? to_dev(kobject_get(&dev
->kobj
)) : NULL
;
655 * put_device - decrement reference count.
656 * @dev: device in question.
658 void put_device(struct device
* dev
)
661 kobject_put(&dev
->kobj
);
666 * device_del - delete device from system.
669 * This is the first part of the device unregistration
670 * sequence. This removes the device from the lists we control
671 * from here, has it removed from the other driver model
672 * subsystems it was added to in device_add(), and removes it
673 * from the kobject hierarchy.
675 * NOTE: this should be called manually _iff_ device_add() was
676 * also called manually.
679 void device_del(struct device
* dev
)
681 struct device
* parent
= dev
->parent
;
682 struct class_interface
*class_intf
;
685 klist_del(&dev
->knode_parent
);
686 if (dev
->devt_attr
) {
687 device_remove_file(dev
, dev
->devt_attr
);
688 kfree(dev
->devt_attr
);
691 sysfs_remove_link(&dev
->kobj
, "subsystem");
692 /* If this is not a "fake" compatible device, remove the
693 * symlink from the class to the device. */
694 if (dev
->kobj
.parent
!= &dev
->class->subsys
.kset
.kobj
)
695 sysfs_remove_link(&dev
->class->subsys
.kset
.kobj
,
697 #ifdef CONFIG_SYSFS_DEPRECATED
699 char *class_name
= make_class_name(dev
->class->name
,
702 sysfs_remove_link(&dev
->parent
->kobj
,
705 sysfs_remove_link(&dev
->kobj
, "device");
709 down(&dev
->class->sem
);
710 /* notify any interfaces that the device is now gone */
711 list_for_each_entry(class_intf
, &dev
->class->interfaces
, node
)
712 if (class_intf
->remove_dev
)
713 class_intf
->remove_dev(dev
, class_intf
);
714 /* remove the device from the class list */
715 list_del_init(&dev
->node
);
716 up(&dev
->class->sem
);
718 device_remove_file(dev
, &dev
->uevent_attr
);
719 device_remove_groups(dev
);
720 device_remove_attrs(dev
);
721 bus_remove_device(dev
);
723 /* Notify the platform of the removal, in case they
724 * need to do anything...
726 if (platform_notify_remove
)
727 platform_notify_remove(dev
);
729 blocking_notifier_call_chain(&dev
->bus
->bus_notifier
,
730 BUS_NOTIFY_DEL_DEVICE
, dev
);
731 device_pm_remove(dev
);
732 kobject_uevent(&dev
->kobj
, KOBJ_REMOVE
);
733 kobject_del(&dev
->kobj
);
739 * device_unregister - unregister device from system.
740 * @dev: device going away.
742 * We do this in two parts, like we do device_register(). First,
743 * we remove it from all the subsystems with device_del(), then
744 * we decrement the reference count via put_device(). If that
745 * is the final reference count, the device will be cleaned up
746 * via device_release() above. Otherwise, the structure will
747 * stick around until the final reference to the device is dropped.
749 void device_unregister(struct device
* dev
)
751 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev
->bus_id
);
757 static struct device
* next_device(struct klist_iter
* i
)
759 struct klist_node
* n
= klist_next(i
);
760 return n
? container_of(n
, struct device
, knode_parent
) : NULL
;
764 * device_for_each_child - device child iterator.
765 * @parent: parent struct device.
766 * @data: data for the callback.
767 * @fn: function to be called for each device.
769 * Iterate over @parent's child devices, and call @fn for each,
772 * We check the return of @fn each time. If it returns anything
773 * other than 0, we break out and return that value.
775 int device_for_each_child(struct device
* parent
, void * data
,
776 int (*fn
)(struct device
*, void *))
779 struct device
* child
;
782 klist_iter_init(&parent
->klist_children
, &i
);
783 while ((child
= next_device(&i
)) && !error
)
784 error
= fn(child
, data
);
790 * device_find_child - device iterator for locating a particular device.
791 * @parent: parent struct device
792 * @data: Data to pass to match function
793 * @match: Callback function to check device
795 * This is similar to the device_for_each_child() function above, but it
796 * returns a reference to a device that is 'found' for later use, as
797 * determined by the @match callback.
799 * The callback should return 0 if the device doesn't match and non-zero
800 * if it does. If the callback returns non-zero and a reference to the
801 * current device can be obtained, this function will return to the caller
802 * and not iterate over any more devices.
804 struct device
* device_find_child(struct device
*parent
, void *data
,
805 int (*match
)(struct device
*, void *))
808 struct device
*child
;
813 klist_iter_init(&parent
->klist_children
, &i
);
814 while ((child
= next_device(&i
)))
815 if (match(child
, data
) && get_device(child
))
821 int __init
devices_init(void)
823 return subsystem_register(&devices_subsys
);
826 EXPORT_SYMBOL_GPL(device_for_each_child
);
827 EXPORT_SYMBOL_GPL(device_find_child
);
829 EXPORT_SYMBOL_GPL(device_initialize
);
830 EXPORT_SYMBOL_GPL(device_add
);
831 EXPORT_SYMBOL_GPL(device_register
);
833 EXPORT_SYMBOL_GPL(device_del
);
834 EXPORT_SYMBOL_GPL(device_unregister
);
835 EXPORT_SYMBOL_GPL(get_device
);
836 EXPORT_SYMBOL_GPL(put_device
);
838 EXPORT_SYMBOL_GPL(device_create_file
);
839 EXPORT_SYMBOL_GPL(device_remove_file
);
842 static void device_create_release(struct device
*dev
)
844 pr_debug("%s called for %s\n", __FUNCTION__
, dev
->bus_id
);
849 * device_create - creates a device and registers it with sysfs
850 * @class: pointer to the struct class that this device should be registered to
851 * @parent: pointer to the parent struct device of this new device, if any
852 * @devt: the dev_t for the char device to be added
853 * @fmt: string for the device's name
855 * This function can be used by char device classes. A struct device
856 * will be created in sysfs, registered to the specified class.
858 * A "dev" file will be created, showing the dev_t for the device, if
859 * the dev_t is not 0,0.
860 * If a pointer to a parent struct device is passed in, the newly created
861 * struct device will be a child of that device in sysfs.
862 * The pointer to the struct device will be returned from the call.
863 * Any further sysfs files that might be required can be created using this
866 * Note: the struct class passed to this function must have previously
867 * been created with a call to class_create().
869 struct device
*device_create(struct class *class, struct device
*parent
,
870 dev_t devt
, const char *fmt
, ...)
873 struct device
*dev
= NULL
;
874 int retval
= -ENODEV
;
876 if (class == NULL
|| IS_ERR(class))
879 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
887 dev
->parent
= parent
;
888 dev
->release
= device_create_release
;
891 vsnprintf(dev
->bus_id
, BUS_ID_SIZE
, fmt
, args
);
893 retval
= device_register(dev
);
901 return ERR_PTR(retval
);
903 EXPORT_SYMBOL_GPL(device_create
);
906 * device_destroy - removes a device that was created with device_create()
907 * @class: pointer to the struct class that this device was registered with
908 * @devt: the dev_t of the device that was previously registered
910 * This call unregisters and cleans up a device that was created with a
911 * call to device_create().
913 void device_destroy(struct class *class, dev_t devt
)
915 struct device
*dev
= NULL
;
916 struct device
*dev_tmp
;
919 list_for_each_entry(dev_tmp
, &class->devices
, node
) {
920 if (dev_tmp
->devt
== devt
) {
928 device_unregister(dev
);
930 EXPORT_SYMBOL_GPL(device_destroy
);
933 * device_rename - renames a device
934 * @dev: the pointer to the struct device to be renamed
935 * @new_name: the new name of the device
937 int device_rename(struct device
*dev
, char *new_name
)
939 char *old_class_name
= NULL
;
940 char *new_class_name
= NULL
;
941 char *old_symlink_name
= NULL
;
944 dev
= get_device(dev
);
948 pr_debug("DEVICE: renaming '%s' to '%s'\n", dev
->bus_id
, new_name
);
950 #ifdef CONFIG_SYSFS_DEPRECATED
951 if ((dev
->class) && (dev
->parent
))
952 old_class_name
= make_class_name(dev
->class->name
, &dev
->kobj
);
956 old_symlink_name
= kmalloc(BUS_ID_SIZE
, GFP_KERNEL
);
957 if (!old_symlink_name
) {
959 goto out_free_old_class
;
961 strlcpy(old_symlink_name
, dev
->bus_id
, BUS_ID_SIZE
);
964 strlcpy(dev
->bus_id
, new_name
, BUS_ID_SIZE
);
966 error
= kobject_rename(&dev
->kobj
, new_name
);
968 #ifdef CONFIG_SYSFS_DEPRECATED
969 if (old_class_name
) {
970 new_class_name
= make_class_name(dev
->class->name
, &dev
->kobj
);
971 if (new_class_name
) {
972 sysfs_create_link(&dev
->parent
->kobj
, &dev
->kobj
,
974 sysfs_remove_link(&dev
->parent
->kobj
, old_class_name
);
980 sysfs_remove_link(&dev
->class->subsys
.kset
.kobj
,
982 sysfs_create_link(&dev
->class->subsys
.kset
.kobj
, &dev
->kobj
,
987 kfree(new_class_name
);
988 kfree(old_symlink_name
);
990 kfree(old_class_name
);
996 static int device_move_class_links(struct device
*dev
,
997 struct device
*old_parent
,
998 struct device
*new_parent
)
1000 #ifdef CONFIG_SYSFS_DEPRECATED
1004 class_name
= make_class_name(dev
->class->name
, &dev
->kobj
);
1010 sysfs_remove_link(&dev
->kobj
, "device");
1011 sysfs_remove_link(&old_parent
->kobj
, class_name
);
1014 error
= sysfs_create_link(&dev
->kobj
, &new_parent
->kobj
,
1018 error
= sysfs_create_link(&new_parent
->kobj
, &dev
->kobj
,
1021 sysfs_remove_link(&dev
->kobj
, "device");
1034 * device_move - moves a device to a new parent
1035 * @dev: the pointer to the struct device to be moved
1036 * @new_parent: the new parent of the device (can by NULL)
1038 int device_move(struct device
*dev
, struct device
*new_parent
)
1041 struct device
*old_parent
;
1042 struct kobject
*new_parent_kobj
;
1044 dev
= get_device(dev
);
1048 new_parent
= get_device(new_parent
);
1049 new_parent_kobj
= get_device_parent (dev
, new_parent
);
1050 if (IS_ERR(new_parent_kobj
)) {
1051 error
= PTR_ERR(new_parent_kobj
);
1052 put_device(new_parent
);
1055 pr_debug("DEVICE: moving '%s' to '%s'\n", dev
->bus_id
,
1056 new_parent
? new_parent
->bus_id
: "<NULL>");
1057 error
= kobject_move(&dev
->kobj
, new_parent_kobj
);
1059 put_device(new_parent
);
1062 old_parent
= dev
->parent
;
1063 dev
->parent
= new_parent
;
1065 klist_remove(&dev
->knode_parent
);
1067 klist_add_tail(&dev
->knode_parent
, &new_parent
->klist_children
);
1070 error
= device_move_class_links(dev
, old_parent
, new_parent
);
1072 /* We ignore errors on cleanup since we're hosed anyway... */
1073 device_move_class_links(dev
, new_parent
, old_parent
);
1074 if (!kobject_move(&dev
->kobj
, &old_parent
->kobj
)) {
1076 klist_remove(&dev
->knode_parent
);
1078 klist_add_tail(&dev
->knode_parent
,
1079 &old_parent
->klist_children
);
1081 put_device(new_parent
);
1085 put_device(old_parent
);
1091 EXPORT_SYMBOL_GPL(device_move
);