initial commit with v2.6.32.60
[linux-2.6.32.60-moxart.git] / drivers / base / core.c
blobf33d768164ac983cb68ee6c3ade5c80d947038da
1 /*
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>
21 #include <linux/genhd.h>
22 #include <linux/kallsyms.h>
23 #include <linux/semaphore.h>
24 #include <linux/mutex.h>
25 #include <linux/async.h>
27 #include "base.h"
28 #include "power/power.h"
30 int (*platform_notify)(struct device *dev) = NULL;
31 int (*platform_notify_remove)(struct device *dev) = NULL;
32 static struct kobject *dev_kobj;
33 struct kobject *sysfs_dev_char_kobj;
34 struct kobject *sysfs_dev_block_kobj;
36 #ifdef CONFIG_BLOCK
37 static inline int device_is_not_partition(struct device *dev)
39 return !(dev->type == &part_type);
41 #else
42 static inline int device_is_not_partition(struct device *dev)
44 return 1;
46 #endif
48 /**
49 * dev_driver_string - Return a device's driver name, if at all possible
50 * @dev: struct device to get the name of
52 * Will return the device's driver's name if it is bound to a device. If
53 * the device is not bound to a device, it will return the name of the bus
54 * it is attached to. If it is not attached to a bus either, an empty
55 * string will be returned.
57 const char *dev_driver_string(const struct device *dev)
59 struct device_driver *drv;
61 /* dev->driver can change to NULL underneath us because of unbinding,
62 * so be careful about accessing it. dev->bus and dev->class should
63 * never change once they are set, so they don't need special care.
65 drv = ACCESS_ONCE(dev->driver);
66 return drv ? drv->name :
67 (dev->bus ? dev->bus->name :
68 (dev->class ? dev->class->name : ""));
70 EXPORT_SYMBOL(dev_driver_string);
72 #define to_dev(obj) container_of(obj, struct device, kobj)
73 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
75 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
76 char *buf)
78 struct device_attribute *dev_attr = to_dev_attr(attr);
79 struct device *dev = to_dev(kobj);
80 ssize_t ret = -EIO;
82 if (dev_attr->show)
83 ret = dev_attr->show(dev, dev_attr, buf);
84 if (ret >= (ssize_t)PAGE_SIZE) {
85 print_symbol("dev_attr_show: %s returned bad count\n",
86 (unsigned long)dev_attr->show);
88 return ret;
91 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
92 const char *buf, size_t count)
94 struct device_attribute *dev_attr = to_dev_attr(attr);
95 struct device *dev = to_dev(kobj);
96 ssize_t ret = -EIO;
98 if (dev_attr->store)
99 ret = dev_attr->store(dev, dev_attr, buf, count);
100 return ret;
103 static struct sysfs_ops dev_sysfs_ops = {
104 .show = dev_attr_show,
105 .store = dev_attr_store,
110 * device_release - free device structure.
111 * @kobj: device's kobject.
113 * This is called once the reference count for the object
114 * reaches 0. We forward the call to the device's release
115 * method, which should handle actually freeing the structure.
117 static void device_release(struct kobject *kobj)
119 struct device *dev = to_dev(kobj);
120 struct device_private *p = dev->p;
122 if (dev->release)
123 dev->release(dev);
124 else if (dev->type && dev->type->release)
125 dev->type->release(dev);
126 else if (dev->class && dev->class->dev_release)
127 dev->class->dev_release(dev);
128 else
129 WARN(1, KERN_ERR "Device '%s' does not have a release() "
130 "function, it is broken and must be fixed.\n",
131 dev_name(dev));
132 kfree(p);
135 static struct kobj_type device_ktype = {
136 .release = device_release,
137 .sysfs_ops = &dev_sysfs_ops,
141 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
143 struct kobj_type *ktype = get_ktype(kobj);
145 if (ktype == &device_ktype) {
146 struct device *dev = to_dev(kobj);
147 if (dev->bus)
148 return 1;
149 if (dev->class)
150 return 1;
152 return 0;
155 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
157 struct device *dev = to_dev(kobj);
159 if (dev->bus)
160 return dev->bus->name;
161 if (dev->class)
162 return dev->class->name;
163 return NULL;
166 static int dev_uevent(struct kset *kset, struct kobject *kobj,
167 struct kobj_uevent_env *env)
169 struct device *dev = to_dev(kobj);
170 int retval = 0;
172 /* add device node properties if present */
173 if (MAJOR(dev->devt)) {
174 const char *tmp;
175 const char *name;
176 mode_t mode = 0;
178 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
179 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
180 name = device_get_devnode(dev, &mode, &tmp);
181 if (name) {
182 add_uevent_var(env, "DEVNAME=%s", name);
183 kfree(tmp);
184 if (mode)
185 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
189 if (dev->type && dev->type->name)
190 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
192 if (dev->driver)
193 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
195 #ifdef CONFIG_SYSFS_DEPRECATED
196 if (dev->class) {
197 struct device *parent = dev->parent;
199 /* find first bus device in parent chain */
200 while (parent && !parent->bus)
201 parent = parent->parent;
202 if (parent && parent->bus) {
203 const char *path;
205 path = kobject_get_path(&parent->kobj, GFP_KERNEL);
206 if (path) {
207 add_uevent_var(env, "PHYSDEVPATH=%s", path);
208 kfree(path);
211 add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name);
213 if (parent->driver)
214 add_uevent_var(env, "PHYSDEVDRIVER=%s",
215 parent->driver->name);
217 } else if (dev->bus) {
218 add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
220 if (dev->driver)
221 add_uevent_var(env, "PHYSDEVDRIVER=%s",
222 dev->driver->name);
224 #endif
226 /* have the bus specific function add its stuff */
227 if (dev->bus && dev->bus->uevent) {
228 retval = dev->bus->uevent(dev, env);
229 if (retval)
230 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
231 dev_name(dev), __func__, retval);
234 /* have the class specific function add its stuff */
235 if (dev->class && dev->class->dev_uevent) {
236 retval = dev->class->dev_uevent(dev, env);
237 if (retval)
238 pr_debug("device: '%s': %s: class uevent() "
239 "returned %d\n", dev_name(dev),
240 __func__, retval);
243 /* have the device type specific fuction add its stuff */
244 if (dev->type && dev->type->uevent) {
245 retval = dev->type->uevent(dev, env);
246 if (retval)
247 pr_debug("device: '%s': %s: dev_type uevent() "
248 "returned %d\n", dev_name(dev),
249 __func__, retval);
252 return retval;
255 static struct kset_uevent_ops device_uevent_ops = {
256 .filter = dev_uevent_filter,
257 .name = dev_uevent_name,
258 .uevent = dev_uevent,
261 static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
262 char *buf)
264 struct kobject *top_kobj;
265 struct kset *kset;
266 struct kobj_uevent_env *env = NULL;
267 int i;
268 size_t count = 0;
269 int retval;
271 /* search the kset, the device belongs to */
272 top_kobj = &dev->kobj;
273 while (!top_kobj->kset && top_kobj->parent)
274 top_kobj = top_kobj->parent;
275 if (!top_kobj->kset)
276 goto out;
278 kset = top_kobj->kset;
279 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
280 goto out;
282 /* respect filter */
283 if (kset->uevent_ops && kset->uevent_ops->filter)
284 if (!kset->uevent_ops->filter(kset, &dev->kobj))
285 goto out;
287 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
288 if (!env)
289 return -ENOMEM;
291 /* let the kset specific function add its keys */
292 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
293 if (retval)
294 goto out;
296 /* copy keys to file */
297 for (i = 0; i < env->envp_idx; i++)
298 count += sprintf(&buf[count], "%s\n", env->envp[i]);
299 out:
300 kfree(env);
301 return count;
304 static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
305 const char *buf, size_t count)
307 enum kobject_action action;
309 if (kobject_action_type(buf, count, &action) == 0) {
310 kobject_uevent(&dev->kobj, action);
311 goto out;
314 dev_err(dev, "uevent: unsupported action-string; this will "
315 "be ignored in a future kernel version\n");
316 kobject_uevent(&dev->kobj, KOBJ_ADD);
317 out:
318 return count;
321 static struct device_attribute uevent_attr =
322 __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
324 static int device_add_attributes(struct device *dev,
325 struct device_attribute *attrs)
327 int error = 0;
328 int i;
330 if (attrs) {
331 for (i = 0; attr_name(attrs[i]); i++) {
332 error = device_create_file(dev, &attrs[i]);
333 if (error)
334 break;
336 if (error)
337 while (--i >= 0)
338 device_remove_file(dev, &attrs[i]);
340 return error;
343 static void device_remove_attributes(struct device *dev,
344 struct device_attribute *attrs)
346 int i;
348 if (attrs)
349 for (i = 0; attr_name(attrs[i]); i++)
350 device_remove_file(dev, &attrs[i]);
353 static int device_add_groups(struct device *dev,
354 const struct attribute_group **groups)
356 int error = 0;
357 int i;
359 if (groups) {
360 for (i = 0; groups[i]; i++) {
361 error = sysfs_create_group(&dev->kobj, groups[i]);
362 if (error) {
363 while (--i >= 0)
364 sysfs_remove_group(&dev->kobj,
365 groups[i]);
366 break;
370 return error;
373 static void device_remove_groups(struct device *dev,
374 const struct attribute_group **groups)
376 int i;
378 if (groups)
379 for (i = 0; groups[i]; i++)
380 sysfs_remove_group(&dev->kobj, groups[i]);
383 static int device_add_attrs(struct device *dev)
385 struct class *class = dev->class;
386 struct device_type *type = dev->type;
387 int error;
389 if (class) {
390 error = device_add_attributes(dev, class->dev_attrs);
391 if (error)
392 return error;
395 if (type) {
396 error = device_add_groups(dev, type->groups);
397 if (error)
398 goto err_remove_class_attrs;
401 error = device_add_groups(dev, dev->groups);
402 if (error)
403 goto err_remove_type_groups;
405 return 0;
407 err_remove_type_groups:
408 if (type)
409 device_remove_groups(dev, type->groups);
410 err_remove_class_attrs:
411 if (class)
412 device_remove_attributes(dev, class->dev_attrs);
414 return error;
417 static void device_remove_attrs(struct device *dev)
419 struct class *class = dev->class;
420 struct device_type *type = dev->type;
422 device_remove_groups(dev, dev->groups);
424 if (type)
425 device_remove_groups(dev, type->groups);
427 if (class)
428 device_remove_attributes(dev, class->dev_attrs);
432 static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
433 char *buf)
435 return print_dev_t(buf, dev->devt);
438 static struct device_attribute devt_attr =
439 __ATTR(dev, S_IRUGO, show_dev, NULL);
441 /* kset to create /sys/devices/ */
442 struct kset *devices_kset;
445 * device_create_file - create sysfs attribute file for device.
446 * @dev: device.
447 * @attr: device attribute descriptor.
449 int device_create_file(struct device *dev, struct device_attribute *attr)
451 int error = 0;
452 if (dev)
453 error = sysfs_create_file(&dev->kobj, &attr->attr);
454 return error;
458 * device_remove_file - remove sysfs attribute file.
459 * @dev: device.
460 * @attr: device attribute descriptor.
462 void device_remove_file(struct device *dev, struct device_attribute *attr)
464 if (dev)
465 sysfs_remove_file(&dev->kobj, &attr->attr);
469 * device_create_bin_file - create sysfs binary attribute file for device.
470 * @dev: device.
471 * @attr: device binary attribute descriptor.
473 int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
475 int error = -EINVAL;
476 if (dev)
477 error = sysfs_create_bin_file(&dev->kobj, attr);
478 return error;
480 EXPORT_SYMBOL_GPL(device_create_bin_file);
483 * device_remove_bin_file - remove sysfs binary attribute file
484 * @dev: device.
485 * @attr: device binary attribute descriptor.
487 void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
489 if (dev)
490 sysfs_remove_bin_file(&dev->kobj, attr);
492 EXPORT_SYMBOL_GPL(device_remove_bin_file);
495 * device_schedule_callback_owner - helper to schedule a callback for a device
496 * @dev: device.
497 * @func: callback function to invoke later.
498 * @owner: module owning the callback routine
500 * Attribute methods must not unregister themselves or their parent device
501 * (which would amount to the same thing). Attempts to do so will deadlock,
502 * since unregistration is mutually exclusive with driver callbacks.
504 * Instead methods can call this routine, which will attempt to allocate
505 * and schedule a workqueue request to call back @func with @dev as its
506 * argument in the workqueue's process context. @dev will be pinned until
507 * @func returns.
509 * This routine is usually called via the inline device_schedule_callback(),
510 * which automatically sets @owner to THIS_MODULE.
512 * Returns 0 if the request was submitted, -ENOMEM if storage could not
513 * be allocated, -ENODEV if a reference to @owner isn't available.
515 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
516 * underlying sysfs routine (since it is intended for use by attribute
517 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
519 int device_schedule_callback_owner(struct device *dev,
520 void (*func)(struct device *), struct module *owner)
522 return sysfs_schedule_callback(&dev->kobj,
523 (void (*)(void *)) func, dev, owner);
525 EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
527 static void klist_children_get(struct klist_node *n)
529 struct device_private *p = to_device_private_parent(n);
530 struct device *dev = p->device;
532 get_device(dev);
535 static void klist_children_put(struct klist_node *n)
537 struct device_private *p = to_device_private_parent(n);
538 struct device *dev = p->device;
540 put_device(dev);
544 * device_initialize - init device structure.
545 * @dev: device.
547 * This prepares the device for use by other layers by initializing
548 * its fields.
549 * It is the first half of device_register(), if called by
550 * that function, though it can also be called separately, so one
551 * may use @dev's fields. In particular, get_device()/put_device()
552 * may be used for reference counting of @dev after calling this
553 * function.
555 * NOTE: Use put_device() to give up your reference instead of freeing
556 * @dev directly once you have called this function.
558 void device_initialize(struct device *dev)
560 dev->kobj.kset = devices_kset;
561 kobject_init(&dev->kobj, &device_ktype);
562 INIT_LIST_HEAD(&dev->dma_pools);
563 init_MUTEX(&dev->sem);
564 spin_lock_init(&dev->devres_lock);
565 INIT_LIST_HEAD(&dev->devres_head);
566 device_init_wakeup(dev, 0);
567 device_pm_init(dev);
568 set_dev_node(dev, -1);
571 #ifdef CONFIG_SYSFS_DEPRECATED
572 static struct kobject *get_device_parent(struct device *dev,
573 struct device *parent)
575 /* class devices without a parent live in /sys/class/<classname>/ */
576 if (dev->class && (!parent || parent->class != dev->class))
577 return &dev->class->p->class_subsys.kobj;
578 /* all other devices keep their parent */
579 else if (parent)
580 return &parent->kobj;
582 return NULL;
585 static inline void cleanup_device_parent(struct device *dev) {}
586 static inline void cleanup_glue_dir(struct device *dev,
587 struct kobject *glue_dir) {}
588 #else
589 static struct kobject *virtual_device_parent(struct device *dev)
591 static struct kobject *virtual_dir = NULL;
593 if (!virtual_dir)
594 virtual_dir = kobject_create_and_add("virtual",
595 &devices_kset->kobj);
597 return virtual_dir;
600 static struct kobject *get_device_parent(struct device *dev,
601 struct device *parent)
603 int retval;
605 if (dev->class) {
606 static DEFINE_MUTEX(gdp_mutex);
607 struct kobject *kobj = NULL;
608 struct kobject *parent_kobj;
609 struct kobject *k;
612 * If we have no parent, we live in "virtual".
613 * Class-devices with a non class-device as parent, live
614 * in a "glue" directory to prevent namespace collisions.
616 if (parent == NULL)
617 parent_kobj = virtual_device_parent(dev);
618 else if (parent->class)
619 return &parent->kobj;
620 else
621 parent_kobj = &parent->kobj;
623 mutex_lock(&gdp_mutex);
625 /* find our class-directory at the parent and reference it */
626 spin_lock(&dev->class->p->class_dirs.list_lock);
627 list_for_each_entry(k, &dev->class->p->class_dirs.list, entry)
628 if (k->parent == parent_kobj) {
629 kobj = kobject_get(k);
630 break;
632 spin_unlock(&dev->class->p->class_dirs.list_lock);
633 if (kobj) {
634 mutex_unlock(&gdp_mutex);
635 return kobj;
638 /* or create a new class-directory at the parent device */
639 k = kobject_create();
640 if (!k) {
641 mutex_unlock(&gdp_mutex);
642 return NULL;
644 k->kset = &dev->class->p->class_dirs;
645 retval = kobject_add(k, parent_kobj, "%s", dev->class->name);
646 if (retval < 0) {
647 mutex_unlock(&gdp_mutex);
648 kobject_put(k);
649 return NULL;
651 /* do not emit an uevent for this simple "glue" directory */
652 mutex_unlock(&gdp_mutex);
653 return k;
656 if (parent)
657 return &parent->kobj;
658 return NULL;
661 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
663 /* see if we live in a "glue" directory */
664 if (!glue_dir || !dev->class ||
665 glue_dir->kset != &dev->class->p->class_dirs)
666 return;
668 kobject_put(glue_dir);
671 static void cleanup_device_parent(struct device *dev)
673 cleanup_glue_dir(dev, dev->kobj.parent);
675 #endif
677 static void setup_parent(struct device *dev, struct device *parent)
679 struct kobject *kobj;
680 kobj = get_device_parent(dev, parent);
681 if (kobj)
682 dev->kobj.parent = kobj;
685 static int device_add_class_symlinks(struct device *dev)
687 int error;
689 if (!dev->class)
690 return 0;
692 error = sysfs_create_link(&dev->kobj,
693 &dev->class->p->class_subsys.kobj,
694 "subsystem");
695 if (error)
696 goto out;
698 #ifdef CONFIG_SYSFS_DEPRECATED
699 /* stacked class devices need a symlink in the class directory */
700 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
701 device_is_not_partition(dev)) {
702 error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
703 &dev->kobj, dev_name(dev));
704 if (error)
705 goto out_subsys;
708 if (dev->parent && device_is_not_partition(dev)) {
709 struct device *parent = dev->parent;
710 char *class_name;
713 * stacked class devices have the 'device' link
714 * pointing to the bus device instead of the parent
716 while (parent->class && !parent->bus && parent->parent)
717 parent = parent->parent;
719 error = sysfs_create_link(&dev->kobj,
720 &parent->kobj,
721 "device");
722 if (error)
723 goto out_busid;
725 class_name = make_class_name(dev->class->name,
726 &dev->kobj);
727 if (class_name)
728 error = sysfs_create_link(&dev->parent->kobj,
729 &dev->kobj, class_name);
730 kfree(class_name);
731 if (error)
732 goto out_device;
734 return 0;
736 out_device:
737 if (dev->parent && device_is_not_partition(dev))
738 sysfs_remove_link(&dev->kobj, "device");
739 out_busid:
740 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
741 device_is_not_partition(dev))
742 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
743 dev_name(dev));
744 #else
745 /* link in the class directory pointing to the device */
746 error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
747 &dev->kobj, dev_name(dev));
748 if (error)
749 goto out_subsys;
751 if (dev->parent && device_is_not_partition(dev)) {
752 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
753 "device");
754 if (error)
755 goto out_busid;
757 return 0;
759 out_busid:
760 sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
761 #endif
763 out_subsys:
764 sysfs_remove_link(&dev->kobj, "subsystem");
765 out:
766 return error;
769 static void device_remove_class_symlinks(struct device *dev)
771 if (!dev->class)
772 return;
774 #ifdef CONFIG_SYSFS_DEPRECATED
775 if (dev->parent && device_is_not_partition(dev)) {
776 char *class_name;
778 class_name = make_class_name(dev->class->name, &dev->kobj);
779 if (class_name) {
780 sysfs_remove_link(&dev->parent->kobj, class_name);
781 kfree(class_name);
783 sysfs_remove_link(&dev->kobj, "device");
786 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
787 device_is_not_partition(dev))
788 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
789 dev_name(dev));
790 #else
791 if (dev->parent && device_is_not_partition(dev))
792 sysfs_remove_link(&dev->kobj, "device");
794 sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
795 #endif
797 sysfs_remove_link(&dev->kobj, "subsystem");
801 * dev_set_name - set a device name
802 * @dev: device
803 * @fmt: format string for the device's name
805 int dev_set_name(struct device *dev, const char *fmt, ...)
807 va_list vargs;
808 int err;
810 va_start(vargs, fmt);
811 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
812 va_end(vargs);
813 return err;
815 EXPORT_SYMBOL_GPL(dev_set_name);
818 * device_to_dev_kobj - select a /sys/dev/ directory for the device
819 * @dev: device
821 * By default we select char/ for new entries. Setting class->dev_obj
822 * to NULL prevents an entry from being created. class->dev_kobj must
823 * be set (or cleared) before any devices are registered to the class
824 * otherwise device_create_sys_dev_entry() and
825 * device_remove_sys_dev_entry() will disagree about the the presence
826 * of the link.
828 static struct kobject *device_to_dev_kobj(struct device *dev)
830 struct kobject *kobj;
832 if (dev->class)
833 kobj = dev->class->dev_kobj;
834 else
835 kobj = sysfs_dev_char_kobj;
837 return kobj;
840 static int device_create_sys_dev_entry(struct device *dev)
842 struct kobject *kobj = device_to_dev_kobj(dev);
843 int error = 0;
844 char devt_str[15];
846 if (kobj) {
847 format_dev_t(devt_str, dev->devt);
848 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
851 return error;
854 static void device_remove_sys_dev_entry(struct device *dev)
856 struct kobject *kobj = device_to_dev_kobj(dev);
857 char devt_str[15];
859 if (kobj) {
860 format_dev_t(devt_str, dev->devt);
861 sysfs_remove_link(kobj, devt_str);
865 int device_private_init(struct device *dev)
867 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
868 if (!dev->p)
869 return -ENOMEM;
870 dev->p->device = dev;
871 klist_init(&dev->p->klist_children, klist_children_get,
872 klist_children_put);
873 return 0;
877 * device_add - add device to device hierarchy.
878 * @dev: device.
880 * This is part 2 of device_register(), though may be called
881 * separately _iff_ device_initialize() has been called separately.
883 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
884 * to the global and sibling lists for the device, then
885 * adds it to the other relevant subsystems of the driver model.
887 * NOTE: _Never_ directly free @dev after calling this function, even
888 * if it returned an error! Always use put_device() to give up your
889 * reference instead.
891 int device_add(struct device *dev)
893 struct device *parent = NULL;
894 struct class_interface *class_intf;
895 int error = -EINVAL;
897 dev = get_device(dev);
898 if (!dev)
899 goto done;
901 if (!dev->p) {
902 error = device_private_init(dev);
903 if (error)
904 goto done;
908 * for statically allocated devices, which should all be converted
909 * some day, we need to initialize the name. We prevent reading back
910 * the name, and force the use of dev_name()
912 if (dev->init_name) {
913 dev_set_name(dev, "%s", dev->init_name);
914 dev->init_name = NULL;
917 if (!dev_name(dev))
918 goto name_error;
920 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
922 parent = get_device(dev->parent);
923 setup_parent(dev, parent);
925 /* use parent numa_node */
926 if (parent)
927 set_dev_node(dev, dev_to_node(parent));
929 /* first, register with generic layer. */
930 /* we require the name to be set before, and pass NULL */
931 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
932 if (error)
933 goto Error;
935 /* notify platform of device entry */
936 if (platform_notify)
937 platform_notify(dev);
939 error = device_create_file(dev, &uevent_attr);
940 if (error)
941 goto attrError;
943 if (MAJOR(dev->devt)) {
944 error = device_create_file(dev, &devt_attr);
945 if (error)
946 goto ueventattrError;
948 error = device_create_sys_dev_entry(dev);
949 if (error)
950 goto devtattrError;
952 devtmpfs_create_node(dev);
955 error = device_add_class_symlinks(dev);
956 if (error)
957 goto SymlinkError;
958 error = device_add_attrs(dev);
959 if (error)
960 goto AttrsError;
961 error = bus_add_device(dev);
962 if (error)
963 goto BusError;
964 error = dpm_sysfs_add(dev);
965 if (error)
966 goto DPMError;
967 device_pm_add(dev);
969 /* Notify clients of device addition. This call must come
970 * after dpm_sysf_add() and before kobject_uevent().
972 if (dev->bus)
973 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
974 BUS_NOTIFY_ADD_DEVICE, dev);
976 kobject_uevent(&dev->kobj, KOBJ_ADD);
977 bus_probe_device(dev);
978 if (parent)
979 klist_add_tail(&dev->p->knode_parent,
980 &parent->p->klist_children);
982 if (dev->class) {
983 mutex_lock(&dev->class->p->class_mutex);
984 /* tie the class to the device */
985 klist_add_tail(&dev->knode_class,
986 &dev->class->p->class_devices);
988 /* notify any interfaces that the device is here */
989 list_for_each_entry(class_intf,
990 &dev->class->p->class_interfaces, node)
991 if (class_intf->add_dev)
992 class_intf->add_dev(dev, class_intf);
993 mutex_unlock(&dev->class->p->class_mutex);
995 done:
996 put_device(dev);
997 return error;
998 DPMError:
999 bus_remove_device(dev);
1000 BusError:
1001 device_remove_attrs(dev);
1002 AttrsError:
1003 device_remove_class_symlinks(dev);
1004 SymlinkError:
1005 if (MAJOR(dev->devt))
1006 device_remove_sys_dev_entry(dev);
1007 devtattrError:
1008 if (MAJOR(dev->devt))
1009 device_remove_file(dev, &devt_attr);
1010 ueventattrError:
1011 device_remove_file(dev, &uevent_attr);
1012 attrError:
1013 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1014 kobject_del(&dev->kobj);
1015 Error:
1016 cleanup_device_parent(dev);
1017 if (parent)
1018 put_device(parent);
1019 name_error:
1020 kfree(dev->p);
1021 dev->p = NULL;
1022 goto done;
1026 * device_register - register a device with the system.
1027 * @dev: pointer to the device structure
1029 * This happens in two clean steps - initialize the device
1030 * and add it to the system. The two steps can be called
1031 * separately, but this is the easiest and most common.
1032 * I.e. you should only call the two helpers separately if
1033 * have a clearly defined need to use and refcount the device
1034 * before it is added to the hierarchy.
1036 * NOTE: _Never_ directly free @dev after calling this function, even
1037 * if it returned an error! Always use put_device() to give up the
1038 * reference initialized in this function instead.
1040 int device_register(struct device *dev)
1042 device_initialize(dev);
1043 return device_add(dev);
1047 * get_device - increment reference count for device.
1048 * @dev: device.
1050 * This simply forwards the call to kobject_get(), though
1051 * we do take care to provide for the case that we get a NULL
1052 * pointer passed in.
1054 struct device *get_device(struct device *dev)
1056 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
1060 * put_device - decrement reference count.
1061 * @dev: device in question.
1063 void put_device(struct device *dev)
1065 /* might_sleep(); */
1066 if (dev)
1067 kobject_put(&dev->kobj);
1071 * device_del - delete device from system.
1072 * @dev: device.
1074 * This is the first part of the device unregistration
1075 * sequence. This removes the device from the lists we control
1076 * from here, has it removed from the other driver model
1077 * subsystems it was added to in device_add(), and removes it
1078 * from the kobject hierarchy.
1080 * NOTE: this should be called manually _iff_ device_add() was
1081 * also called manually.
1083 void device_del(struct device *dev)
1085 struct device *parent = dev->parent;
1086 struct class_interface *class_intf;
1088 /* Notify clients of device removal. This call must come
1089 * before dpm_sysfs_remove().
1091 if (dev->bus)
1092 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1093 BUS_NOTIFY_DEL_DEVICE, dev);
1094 device_pm_remove(dev);
1095 dpm_sysfs_remove(dev);
1096 if (parent)
1097 klist_del(&dev->p->knode_parent);
1098 if (MAJOR(dev->devt)) {
1099 devtmpfs_delete_node(dev);
1100 device_remove_sys_dev_entry(dev);
1101 device_remove_file(dev, &devt_attr);
1103 if (dev->class) {
1104 device_remove_class_symlinks(dev);
1106 mutex_lock(&dev->class->p->class_mutex);
1107 /* notify any interfaces that the device is now gone */
1108 list_for_each_entry(class_intf,
1109 &dev->class->p->class_interfaces, node)
1110 if (class_intf->remove_dev)
1111 class_intf->remove_dev(dev, class_intf);
1112 /* remove the device from the class list */
1113 klist_del(&dev->knode_class);
1114 mutex_unlock(&dev->class->p->class_mutex);
1116 device_remove_file(dev, &uevent_attr);
1117 device_remove_attrs(dev);
1118 bus_remove_device(dev);
1121 * Some platform devices are driven without driver attached
1122 * and managed resources may have been acquired. Make sure
1123 * all resources are released.
1125 devres_release_all(dev);
1127 /* Notify the platform of the removal, in case they
1128 * need to do anything...
1130 if (platform_notify_remove)
1131 platform_notify_remove(dev);
1132 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1133 cleanup_device_parent(dev);
1134 kobject_del(&dev->kobj);
1135 put_device(parent);
1139 * device_unregister - unregister device from system.
1140 * @dev: device going away.
1142 * We do this in two parts, like we do device_register(). First,
1143 * we remove it from all the subsystems with device_del(), then
1144 * we decrement the reference count via put_device(). If that
1145 * is the final reference count, the device will be cleaned up
1146 * via device_release() above. Otherwise, the structure will
1147 * stick around until the final reference to the device is dropped.
1149 void device_unregister(struct device *dev)
1151 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1152 device_del(dev);
1153 put_device(dev);
1156 static struct device *next_device(struct klist_iter *i)
1158 struct klist_node *n = klist_next(i);
1159 struct device *dev = NULL;
1160 struct device_private *p;
1162 if (n) {
1163 p = to_device_private_parent(n);
1164 dev = p->device;
1166 return dev;
1170 * device_get_devnode - path of device node file
1171 * @dev: device
1172 * @mode: returned file access mode
1173 * @tmp: possibly allocated string
1175 * Return the relative path of a possible device node.
1176 * Non-default names may need to allocate a memory to compose
1177 * a name. This memory is returned in tmp and needs to be
1178 * freed by the caller.
1180 const char *device_get_devnode(struct device *dev,
1181 mode_t *mode, const char **tmp)
1183 char *s;
1185 *tmp = NULL;
1187 /* the device type may provide a specific name */
1188 if (dev->type && dev->type->devnode)
1189 *tmp = dev->type->devnode(dev, mode);
1190 if (*tmp)
1191 return *tmp;
1193 /* the class may provide a specific name */
1194 if (dev->class && dev->class->devnode)
1195 *tmp = dev->class->devnode(dev, mode);
1196 if (*tmp)
1197 return *tmp;
1199 /* return name without allocation, tmp == NULL */
1200 if (strchr(dev_name(dev), '!') == NULL)
1201 return dev_name(dev);
1203 /* replace '!' in the name with '/' */
1204 *tmp = kstrdup(dev_name(dev), GFP_KERNEL);
1205 if (!*tmp)
1206 return NULL;
1207 while ((s = strchr(*tmp, '!')))
1208 s[0] = '/';
1209 return *tmp;
1213 * device_for_each_child - device child iterator.
1214 * @parent: parent struct device.
1215 * @data: data for the callback.
1216 * @fn: function to be called for each device.
1218 * Iterate over @parent's child devices, and call @fn for each,
1219 * passing it @data.
1221 * We check the return of @fn each time. If it returns anything
1222 * other than 0, we break out and return that value.
1224 int device_for_each_child(struct device *parent, void *data,
1225 int (*fn)(struct device *dev, void *data))
1227 struct klist_iter i;
1228 struct device *child;
1229 int error = 0;
1231 if (!parent->p)
1232 return 0;
1234 klist_iter_init(&parent->p->klist_children, &i);
1235 while ((child = next_device(&i)) && !error)
1236 error = fn(child, data);
1237 klist_iter_exit(&i);
1238 return error;
1242 * device_find_child - device iterator for locating a particular device.
1243 * @parent: parent struct device
1244 * @data: Data to pass to match function
1245 * @match: Callback function to check device
1247 * This is similar to the device_for_each_child() function above, but it
1248 * returns a reference to a device that is 'found' for later use, as
1249 * determined by the @match callback.
1251 * The callback should return 0 if the device doesn't match and non-zero
1252 * if it does. If the callback returns non-zero and a reference to the
1253 * current device can be obtained, this function will return to the caller
1254 * and not iterate over any more devices.
1256 struct device *device_find_child(struct device *parent, void *data,
1257 int (*match)(struct device *dev, void *data))
1259 struct klist_iter i;
1260 struct device *child;
1262 if (!parent)
1263 return NULL;
1265 klist_iter_init(&parent->p->klist_children, &i);
1266 while ((child = next_device(&i)))
1267 if (match(child, data) && get_device(child))
1268 break;
1269 klist_iter_exit(&i);
1270 return child;
1273 int __init devices_init(void)
1275 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1276 if (!devices_kset)
1277 return -ENOMEM;
1278 dev_kobj = kobject_create_and_add("dev", NULL);
1279 if (!dev_kobj)
1280 goto dev_kobj_err;
1281 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1282 if (!sysfs_dev_block_kobj)
1283 goto block_kobj_err;
1284 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1285 if (!sysfs_dev_char_kobj)
1286 goto char_kobj_err;
1288 return 0;
1290 char_kobj_err:
1291 kobject_put(sysfs_dev_block_kobj);
1292 block_kobj_err:
1293 kobject_put(dev_kobj);
1294 dev_kobj_err:
1295 kset_unregister(devices_kset);
1296 return -ENOMEM;
1299 EXPORT_SYMBOL_GPL(device_for_each_child);
1300 EXPORT_SYMBOL_GPL(device_find_child);
1302 EXPORT_SYMBOL_GPL(device_initialize);
1303 EXPORT_SYMBOL_GPL(device_add);
1304 EXPORT_SYMBOL_GPL(device_register);
1306 EXPORT_SYMBOL_GPL(device_del);
1307 EXPORT_SYMBOL_GPL(device_unregister);
1308 EXPORT_SYMBOL_GPL(get_device);
1309 EXPORT_SYMBOL_GPL(put_device);
1311 EXPORT_SYMBOL_GPL(device_create_file);
1312 EXPORT_SYMBOL_GPL(device_remove_file);
1314 struct root_device
1316 struct device dev;
1317 struct module *owner;
1320 #define to_root_device(dev) container_of(dev, struct root_device, dev)
1322 static void root_device_release(struct device *dev)
1324 kfree(to_root_device(dev));
1328 * __root_device_register - allocate and register a root device
1329 * @name: root device name
1330 * @owner: owner module of the root device, usually THIS_MODULE
1332 * This function allocates a root device and registers it
1333 * using device_register(). In order to free the returned
1334 * device, use root_device_unregister().
1336 * Root devices are dummy devices which allow other devices
1337 * to be grouped under /sys/devices. Use this function to
1338 * allocate a root device and then use it as the parent of
1339 * any device which should appear under /sys/devices/{name}
1341 * The /sys/devices/{name} directory will also contain a
1342 * 'module' symlink which points to the @owner directory
1343 * in sysfs.
1345 * Note: You probably want to use root_device_register().
1347 struct device *__root_device_register(const char *name, struct module *owner)
1349 struct root_device *root;
1350 int err = -ENOMEM;
1352 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
1353 if (!root)
1354 return ERR_PTR(err);
1356 err = dev_set_name(&root->dev, "%s", name);
1357 if (err) {
1358 kfree(root);
1359 return ERR_PTR(err);
1362 root->dev.release = root_device_release;
1364 err = device_register(&root->dev);
1365 if (err) {
1366 put_device(&root->dev);
1367 return ERR_PTR(err);
1370 #ifdef CONFIG_MODULE /* gotta find a "cleaner" way to do this */
1371 if (owner) {
1372 struct module_kobject *mk = &owner->mkobj;
1374 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
1375 if (err) {
1376 device_unregister(&root->dev);
1377 return ERR_PTR(err);
1379 root->owner = owner;
1381 #endif
1383 return &root->dev;
1385 EXPORT_SYMBOL_GPL(__root_device_register);
1388 * root_device_unregister - unregister and free a root device
1389 * @dev: device going away
1391 * This function unregisters and cleans up a device that was created by
1392 * root_device_register().
1394 void root_device_unregister(struct device *dev)
1396 struct root_device *root = to_root_device(dev);
1398 if (root->owner)
1399 sysfs_remove_link(&root->dev.kobj, "module");
1401 device_unregister(dev);
1403 EXPORT_SYMBOL_GPL(root_device_unregister);
1406 static void device_create_release(struct device *dev)
1408 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1409 kfree(dev);
1413 * device_create_vargs - creates a device and registers it with sysfs
1414 * @class: pointer to the struct class that this device should be registered to
1415 * @parent: pointer to the parent struct device of this new device, if any
1416 * @devt: the dev_t for the char device to be added
1417 * @drvdata: the data to be added to the device for callbacks
1418 * @fmt: string for the device's name
1419 * @args: va_list for the device's name
1421 * This function can be used by char device classes. A struct device
1422 * will be created in sysfs, registered to the specified class.
1424 * A "dev" file will be created, showing the dev_t for the device, if
1425 * the dev_t is not 0,0.
1426 * If a pointer to a parent struct device is passed in, the newly created
1427 * struct device will be a child of that device in sysfs.
1428 * The pointer to the struct device will be returned from the call.
1429 * Any further sysfs files that might be required can be created using this
1430 * pointer.
1432 * Note: the struct class passed to this function must have previously
1433 * been created with a call to class_create().
1435 struct device *device_create_vargs(struct class *class, struct device *parent,
1436 dev_t devt, void *drvdata, const char *fmt,
1437 va_list args)
1439 struct device *dev = NULL;
1440 int retval = -ENODEV;
1442 if (class == NULL || IS_ERR(class))
1443 goto error;
1445 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1446 if (!dev) {
1447 retval = -ENOMEM;
1448 goto error;
1451 dev->devt = devt;
1452 dev->class = class;
1453 dev->parent = parent;
1454 dev->release = device_create_release;
1455 dev_set_drvdata(dev, drvdata);
1457 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
1458 if (retval)
1459 goto error;
1461 retval = device_register(dev);
1462 if (retval)
1463 goto error;
1465 return dev;
1467 error:
1468 put_device(dev);
1469 return ERR_PTR(retval);
1471 EXPORT_SYMBOL_GPL(device_create_vargs);
1474 * device_create - creates a device and registers it with sysfs
1475 * @class: pointer to the struct class that this device should be registered to
1476 * @parent: pointer to the parent struct device of this new device, if any
1477 * @devt: the dev_t for the char device to be added
1478 * @drvdata: the data to be added to the device for callbacks
1479 * @fmt: string for the device's name
1481 * This function can be used by char device classes. A struct device
1482 * will be created in sysfs, registered to the specified class.
1484 * A "dev" file will be created, showing the dev_t for the device, if
1485 * the dev_t is not 0,0.
1486 * If a pointer to a parent struct device is passed in, the newly created
1487 * struct device will be a child of that device in sysfs.
1488 * The pointer to the struct device will be returned from the call.
1489 * Any further sysfs files that might be required can be created using this
1490 * pointer.
1492 * Note: the struct class passed to this function must have previously
1493 * been created with a call to class_create().
1495 struct device *device_create(struct class *class, struct device *parent,
1496 dev_t devt, void *drvdata, const char *fmt, ...)
1498 va_list vargs;
1499 struct device *dev;
1501 va_start(vargs, fmt);
1502 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
1503 va_end(vargs);
1504 return dev;
1506 EXPORT_SYMBOL_GPL(device_create);
1508 static int __match_devt(struct device *dev, void *data)
1510 dev_t *devt = data;
1512 return dev->devt == *devt;
1516 * device_destroy - removes a device that was created with device_create()
1517 * @class: pointer to the struct class that this device was registered with
1518 * @devt: the dev_t of the device that was previously registered
1520 * This call unregisters and cleans up a device that was created with a
1521 * call to device_create().
1523 void device_destroy(struct class *class, dev_t devt)
1525 struct device *dev;
1527 dev = class_find_device(class, NULL, &devt, __match_devt);
1528 if (dev) {
1529 put_device(dev);
1530 device_unregister(dev);
1533 EXPORT_SYMBOL_GPL(device_destroy);
1536 * device_rename - renames a device
1537 * @dev: the pointer to the struct device to be renamed
1538 * @new_name: the new name of the device
1540 * It is the responsibility of the caller to provide mutual
1541 * exclusion between two different calls of device_rename
1542 * on the same device to ensure that new_name is valid and
1543 * won't conflict with other devices.
1545 int device_rename(struct device *dev, char *new_name)
1547 char *old_class_name = NULL;
1548 char *new_class_name = NULL;
1549 char *old_device_name = NULL;
1550 int error;
1552 dev = get_device(dev);
1553 if (!dev)
1554 return -EINVAL;
1556 pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
1557 __func__, new_name);
1559 #ifdef CONFIG_SYSFS_DEPRECATED
1560 if ((dev->class) && (dev->parent))
1561 old_class_name = make_class_name(dev->class->name, &dev->kobj);
1562 #endif
1564 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
1565 if (!old_device_name) {
1566 error = -ENOMEM;
1567 goto out;
1570 error = kobject_rename(&dev->kobj, new_name);
1571 if (error)
1572 goto out;
1574 #ifdef CONFIG_SYSFS_DEPRECATED
1575 if (old_class_name) {
1576 new_class_name = make_class_name(dev->class->name, &dev->kobj);
1577 if (new_class_name) {
1578 error = sysfs_create_link_nowarn(&dev->parent->kobj,
1579 &dev->kobj,
1580 new_class_name);
1581 if (error)
1582 goto out;
1583 sysfs_remove_link(&dev->parent->kobj, old_class_name);
1586 #else
1587 if (dev->class) {
1588 error = sysfs_create_link_nowarn(&dev->class->p->class_subsys.kobj,
1589 &dev->kobj, dev_name(dev));
1590 if (error)
1591 goto out;
1592 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
1593 old_device_name);
1595 #endif
1597 out:
1598 put_device(dev);
1600 kfree(new_class_name);
1601 kfree(old_class_name);
1602 kfree(old_device_name);
1604 return error;
1606 EXPORT_SYMBOL_GPL(device_rename);
1608 static int device_move_class_links(struct device *dev,
1609 struct device *old_parent,
1610 struct device *new_parent)
1612 int error = 0;
1613 #ifdef CONFIG_SYSFS_DEPRECATED
1614 char *class_name;
1616 class_name = make_class_name(dev->class->name, &dev->kobj);
1617 if (!class_name) {
1618 error = -ENOMEM;
1619 goto out;
1621 if (old_parent) {
1622 sysfs_remove_link(&dev->kobj, "device");
1623 sysfs_remove_link(&old_parent->kobj, class_name);
1625 if (new_parent) {
1626 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1627 "device");
1628 if (error)
1629 goto out;
1630 error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
1631 class_name);
1632 if (error)
1633 sysfs_remove_link(&dev->kobj, "device");
1634 } else
1635 error = 0;
1636 out:
1637 kfree(class_name);
1638 return error;
1639 #else
1640 if (old_parent)
1641 sysfs_remove_link(&dev->kobj, "device");
1642 if (new_parent)
1643 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1644 "device");
1645 return error;
1646 #endif
1650 * device_move - moves a device to a new parent
1651 * @dev: the pointer to the struct device to be moved
1652 * @new_parent: the new parent of the device (can by NULL)
1653 * @dpm_order: how to reorder the dpm_list
1655 int device_move(struct device *dev, struct device *new_parent,
1656 enum dpm_order dpm_order)
1658 int error;
1659 struct device *old_parent;
1660 struct kobject *new_parent_kobj;
1662 dev = get_device(dev);
1663 if (!dev)
1664 return -EINVAL;
1666 device_pm_lock();
1667 new_parent = get_device(new_parent);
1668 new_parent_kobj = get_device_parent(dev, new_parent);
1670 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
1671 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
1672 error = kobject_move(&dev->kobj, new_parent_kobj);
1673 if (error) {
1674 cleanup_glue_dir(dev, new_parent_kobj);
1675 put_device(new_parent);
1676 goto out;
1678 old_parent = dev->parent;
1679 dev->parent = new_parent;
1680 if (old_parent)
1681 klist_remove(&dev->p->knode_parent);
1682 if (new_parent) {
1683 klist_add_tail(&dev->p->knode_parent,
1684 &new_parent->p->klist_children);
1685 set_dev_node(dev, dev_to_node(new_parent));
1688 if (!dev->class)
1689 goto out_put;
1690 error = device_move_class_links(dev, old_parent, new_parent);
1691 if (error) {
1692 /* We ignore errors on cleanup since we're hosed anyway... */
1693 device_move_class_links(dev, new_parent, old_parent);
1694 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1695 if (new_parent)
1696 klist_remove(&dev->p->knode_parent);
1697 dev->parent = old_parent;
1698 if (old_parent) {
1699 klist_add_tail(&dev->p->knode_parent,
1700 &old_parent->p->klist_children);
1701 set_dev_node(dev, dev_to_node(old_parent));
1704 cleanup_glue_dir(dev, new_parent_kobj);
1705 put_device(new_parent);
1706 goto out;
1708 switch (dpm_order) {
1709 case DPM_ORDER_NONE:
1710 break;
1711 case DPM_ORDER_DEV_AFTER_PARENT:
1712 device_pm_move_after(dev, new_parent);
1713 break;
1714 case DPM_ORDER_PARENT_BEFORE_DEV:
1715 device_pm_move_before(new_parent, dev);
1716 break;
1717 case DPM_ORDER_DEV_LAST:
1718 device_pm_move_last(dev);
1719 break;
1721 out_put:
1722 put_device(old_parent);
1723 out:
1724 device_pm_unlock();
1725 put_device(dev);
1726 return error;
1728 EXPORT_SYMBOL_GPL(device_move);
1731 * device_shutdown - call ->shutdown() on each device to shutdown.
1733 void device_shutdown(void)
1735 struct device *dev, *devn;
1737 list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list,
1738 kobj.entry) {
1739 if (dev->bus && dev->bus->shutdown) {
1740 dev_dbg(dev, "shutdown\n");
1741 dev->bus->shutdown(dev);
1742 } else if (dev->driver && dev->driver->shutdown) {
1743 dev_dbg(dev, "shutdown\n");
1744 dev->driver->shutdown(dev);
1747 kobject_put(sysfs_dev_char_kobj);
1748 kobject_put(sysfs_dev_block_kobj);
1749 kobject_put(dev_kobj);
1750 async_synchronize_full();