Merge branch 'fix/hda' into for-linus
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / base / core.c
blob7ecb1938e590f34526beaaeda2a3f6683f4024ce
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 return dev->driver ? dev->driver->name :
60 (dev->bus ? dev->bus->name :
61 (dev->class ? dev->class->name : ""));
63 EXPORT_SYMBOL(dev_driver_string);
65 #define to_dev(obj) container_of(obj, struct device, kobj)
66 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
68 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
69 char *buf)
71 struct device_attribute *dev_attr = to_dev_attr(attr);
72 struct device *dev = to_dev(kobj);
73 ssize_t ret = -EIO;
75 if (dev_attr->show)
76 ret = dev_attr->show(dev, dev_attr, buf);
77 if (ret >= (ssize_t)PAGE_SIZE) {
78 print_symbol("dev_attr_show: %s returned bad count\n",
79 (unsigned long)dev_attr->show);
81 return ret;
84 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
85 const char *buf, size_t count)
87 struct device_attribute *dev_attr = to_dev_attr(attr);
88 struct device *dev = to_dev(kobj);
89 ssize_t ret = -EIO;
91 if (dev_attr->store)
92 ret = dev_attr->store(dev, dev_attr, buf, count);
93 return ret;
96 static struct sysfs_ops dev_sysfs_ops = {
97 .show = dev_attr_show,
98 .store = dev_attr_store,
103 * device_release - free device structure.
104 * @kobj: device's kobject.
106 * This is called once the reference count for the object
107 * reaches 0. We forward the call to the device's release
108 * method, which should handle actually freeing the structure.
110 static void device_release(struct kobject *kobj)
112 struct device *dev = to_dev(kobj);
113 struct device_private *p = dev->p;
115 if (dev->release)
116 dev->release(dev);
117 else if (dev->type && dev->type->release)
118 dev->type->release(dev);
119 else if (dev->class && dev->class->dev_release)
120 dev->class->dev_release(dev);
121 else
122 WARN(1, KERN_ERR "Device '%s' does not have a release() "
123 "function, it is broken and must be fixed.\n",
124 dev_name(dev));
125 kfree(p);
128 static struct kobj_type device_ktype = {
129 .release = device_release,
130 .sysfs_ops = &dev_sysfs_ops,
134 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
136 struct kobj_type *ktype = get_ktype(kobj);
138 if (ktype == &device_ktype) {
139 struct device *dev = to_dev(kobj);
140 if (dev->bus)
141 return 1;
142 if (dev->class)
143 return 1;
145 return 0;
148 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
150 struct device *dev = to_dev(kobj);
152 if (dev->bus)
153 return dev->bus->name;
154 if (dev->class)
155 return dev->class->name;
156 return NULL;
159 static int dev_uevent(struct kset *kset, struct kobject *kobj,
160 struct kobj_uevent_env *env)
162 struct device *dev = to_dev(kobj);
163 int retval = 0;
165 /* add device node properties if present */
166 if (MAJOR(dev->devt)) {
167 const char *tmp;
168 const char *name;
170 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
171 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
172 name = device_get_nodename(dev, &tmp);
173 if (name) {
174 add_uevent_var(env, "DEVNAME=%s", name);
175 kfree(tmp);
179 if (dev->type && dev->type->name)
180 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
182 if (dev->driver)
183 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
185 #ifdef CONFIG_SYSFS_DEPRECATED
186 if (dev->class) {
187 struct device *parent = dev->parent;
189 /* find first bus device in parent chain */
190 while (parent && !parent->bus)
191 parent = parent->parent;
192 if (parent && parent->bus) {
193 const char *path;
195 path = kobject_get_path(&parent->kobj, GFP_KERNEL);
196 if (path) {
197 add_uevent_var(env, "PHYSDEVPATH=%s", path);
198 kfree(path);
201 add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name);
203 if (parent->driver)
204 add_uevent_var(env, "PHYSDEVDRIVER=%s",
205 parent->driver->name);
207 } else if (dev->bus) {
208 add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
210 if (dev->driver)
211 add_uevent_var(env, "PHYSDEVDRIVER=%s",
212 dev->driver->name);
214 #endif
216 /* have the bus specific function add its stuff */
217 if (dev->bus && dev->bus->uevent) {
218 retval = dev->bus->uevent(dev, env);
219 if (retval)
220 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
221 dev_name(dev), __func__, retval);
224 /* have the class specific function add its stuff */
225 if (dev->class && dev->class->dev_uevent) {
226 retval = dev->class->dev_uevent(dev, env);
227 if (retval)
228 pr_debug("device: '%s': %s: class uevent() "
229 "returned %d\n", dev_name(dev),
230 __func__, retval);
233 /* have the device type specific fuction add its stuff */
234 if (dev->type && dev->type->uevent) {
235 retval = dev->type->uevent(dev, env);
236 if (retval)
237 pr_debug("device: '%s': %s: dev_type uevent() "
238 "returned %d\n", dev_name(dev),
239 __func__, retval);
242 return retval;
245 static struct kset_uevent_ops device_uevent_ops = {
246 .filter = dev_uevent_filter,
247 .name = dev_uevent_name,
248 .uevent = dev_uevent,
251 static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
252 char *buf)
254 struct kobject *top_kobj;
255 struct kset *kset;
256 struct kobj_uevent_env *env = NULL;
257 int i;
258 size_t count = 0;
259 int retval;
261 /* search the kset, the device belongs to */
262 top_kobj = &dev->kobj;
263 while (!top_kobj->kset && top_kobj->parent)
264 top_kobj = top_kobj->parent;
265 if (!top_kobj->kset)
266 goto out;
268 kset = top_kobj->kset;
269 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
270 goto out;
272 /* respect filter */
273 if (kset->uevent_ops && kset->uevent_ops->filter)
274 if (!kset->uevent_ops->filter(kset, &dev->kobj))
275 goto out;
277 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
278 if (!env)
279 return -ENOMEM;
281 /* let the kset specific function add its keys */
282 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
283 if (retval)
284 goto out;
286 /* copy keys to file */
287 for (i = 0; i < env->envp_idx; i++)
288 count += sprintf(&buf[count], "%s\n", env->envp[i]);
289 out:
290 kfree(env);
291 return count;
294 static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
295 const char *buf, size_t count)
297 enum kobject_action action;
299 if (kobject_action_type(buf, count, &action) == 0) {
300 kobject_uevent(&dev->kobj, action);
301 goto out;
304 dev_err(dev, "uevent: unsupported action-string; this will "
305 "be ignored in a future kernel version\n");
306 kobject_uevent(&dev->kobj, KOBJ_ADD);
307 out:
308 return count;
311 static struct device_attribute uevent_attr =
312 __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
314 static int device_add_attributes(struct device *dev,
315 struct device_attribute *attrs)
317 int error = 0;
318 int i;
320 if (attrs) {
321 for (i = 0; attr_name(attrs[i]); i++) {
322 error = device_create_file(dev, &attrs[i]);
323 if (error)
324 break;
326 if (error)
327 while (--i >= 0)
328 device_remove_file(dev, &attrs[i]);
330 return error;
333 static void device_remove_attributes(struct device *dev,
334 struct device_attribute *attrs)
336 int i;
338 if (attrs)
339 for (i = 0; attr_name(attrs[i]); i++)
340 device_remove_file(dev, &attrs[i]);
343 static int device_add_groups(struct device *dev,
344 struct attribute_group **groups)
346 int error = 0;
347 int i;
349 if (groups) {
350 for (i = 0; groups[i]; i++) {
351 error = sysfs_create_group(&dev->kobj, groups[i]);
352 if (error) {
353 while (--i >= 0)
354 sysfs_remove_group(&dev->kobj,
355 groups[i]);
356 break;
360 return error;
363 static void device_remove_groups(struct device *dev,
364 struct attribute_group **groups)
366 int i;
368 if (groups)
369 for (i = 0; groups[i]; i++)
370 sysfs_remove_group(&dev->kobj, groups[i]);
373 static int device_add_attrs(struct device *dev)
375 struct class *class = dev->class;
376 struct device_type *type = dev->type;
377 int error;
379 if (class) {
380 error = device_add_attributes(dev, class->dev_attrs);
381 if (error)
382 return error;
385 if (type) {
386 error = device_add_groups(dev, type->groups);
387 if (error)
388 goto err_remove_class_attrs;
391 error = device_add_groups(dev, dev->groups);
392 if (error)
393 goto err_remove_type_groups;
395 return 0;
397 err_remove_type_groups:
398 if (type)
399 device_remove_groups(dev, type->groups);
400 err_remove_class_attrs:
401 if (class)
402 device_remove_attributes(dev, class->dev_attrs);
404 return error;
407 static void device_remove_attrs(struct device *dev)
409 struct class *class = dev->class;
410 struct device_type *type = dev->type;
412 device_remove_groups(dev, dev->groups);
414 if (type)
415 device_remove_groups(dev, type->groups);
417 if (class)
418 device_remove_attributes(dev, class->dev_attrs);
422 static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
423 char *buf)
425 return print_dev_t(buf, dev->devt);
428 static struct device_attribute devt_attr =
429 __ATTR(dev, S_IRUGO, show_dev, NULL);
431 /* kset to create /sys/devices/ */
432 struct kset *devices_kset;
435 * device_create_file - create sysfs attribute file for device.
436 * @dev: device.
437 * @attr: device attribute descriptor.
439 int device_create_file(struct device *dev, struct device_attribute *attr)
441 int error = 0;
442 if (dev)
443 error = sysfs_create_file(&dev->kobj, &attr->attr);
444 return error;
448 * device_remove_file - remove sysfs attribute file.
449 * @dev: device.
450 * @attr: device attribute descriptor.
452 void device_remove_file(struct device *dev, struct device_attribute *attr)
454 if (dev)
455 sysfs_remove_file(&dev->kobj, &attr->attr);
459 * device_create_bin_file - create sysfs binary attribute file for device.
460 * @dev: device.
461 * @attr: device binary attribute descriptor.
463 int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
465 int error = -EINVAL;
466 if (dev)
467 error = sysfs_create_bin_file(&dev->kobj, attr);
468 return error;
470 EXPORT_SYMBOL_GPL(device_create_bin_file);
473 * device_remove_bin_file - remove sysfs binary attribute file
474 * @dev: device.
475 * @attr: device binary attribute descriptor.
477 void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
479 if (dev)
480 sysfs_remove_bin_file(&dev->kobj, attr);
482 EXPORT_SYMBOL_GPL(device_remove_bin_file);
485 * device_schedule_callback_owner - helper to schedule a callback for a device
486 * @dev: device.
487 * @func: callback function to invoke later.
488 * @owner: module owning the callback routine
490 * Attribute methods must not unregister themselves or their parent device
491 * (which would amount to the same thing). Attempts to do so will deadlock,
492 * since unregistration is mutually exclusive with driver callbacks.
494 * Instead methods can call this routine, which will attempt to allocate
495 * and schedule a workqueue request to call back @func with @dev as its
496 * argument in the workqueue's process context. @dev will be pinned until
497 * @func returns.
499 * This routine is usually called via the inline device_schedule_callback(),
500 * which automatically sets @owner to THIS_MODULE.
502 * Returns 0 if the request was submitted, -ENOMEM if storage could not
503 * be allocated, -ENODEV if a reference to @owner isn't available.
505 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
506 * underlying sysfs routine (since it is intended for use by attribute
507 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
509 int device_schedule_callback_owner(struct device *dev,
510 void (*func)(struct device *), struct module *owner)
512 return sysfs_schedule_callback(&dev->kobj,
513 (void (*)(void *)) func, dev, owner);
515 EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
517 static void klist_children_get(struct klist_node *n)
519 struct device_private *p = to_device_private_parent(n);
520 struct device *dev = p->device;
522 get_device(dev);
525 static void klist_children_put(struct klist_node *n)
527 struct device_private *p = to_device_private_parent(n);
528 struct device *dev = p->device;
530 put_device(dev);
534 * device_initialize - init device structure.
535 * @dev: device.
537 * This prepares the device for use by other layers by initializing
538 * its fields.
539 * It is the first half of device_register(), if called by
540 * that function, though it can also be called separately, so one
541 * may use @dev's fields. In particular, get_device()/put_device()
542 * may be used for reference counting of @dev after calling this
543 * function.
545 * NOTE: Use put_device() to give up your reference instead of freeing
546 * @dev directly once you have called this function.
548 void device_initialize(struct device *dev)
550 dev->kobj.kset = devices_kset;
551 kobject_init(&dev->kobj, &device_ktype);
552 INIT_LIST_HEAD(&dev->dma_pools);
553 init_MUTEX(&dev->sem);
554 spin_lock_init(&dev->devres_lock);
555 INIT_LIST_HEAD(&dev->devres_head);
556 device_init_wakeup(dev, 0);
557 device_pm_init(dev);
558 set_dev_node(dev, -1);
561 #ifdef CONFIG_SYSFS_DEPRECATED
562 static struct kobject *get_device_parent(struct device *dev,
563 struct device *parent)
565 /* class devices without a parent live in /sys/class/<classname>/ */
566 if (dev->class && (!parent || parent->class != dev->class))
567 return &dev->class->p->class_subsys.kobj;
568 /* all other devices keep their parent */
569 else if (parent)
570 return &parent->kobj;
572 return NULL;
575 static inline void cleanup_device_parent(struct device *dev) {}
576 static inline void cleanup_glue_dir(struct device *dev,
577 struct kobject *glue_dir) {}
578 #else
579 static struct kobject *virtual_device_parent(struct device *dev)
581 static struct kobject *virtual_dir = NULL;
583 if (!virtual_dir)
584 virtual_dir = kobject_create_and_add("virtual",
585 &devices_kset->kobj);
587 return virtual_dir;
590 static struct kobject *get_device_parent(struct device *dev,
591 struct device *parent)
593 int retval;
595 if (dev->class) {
596 struct kobject *kobj = NULL;
597 struct kobject *parent_kobj;
598 struct kobject *k;
601 * If we have no parent, we live in "virtual".
602 * Class-devices with a non class-device as parent, live
603 * in a "glue" directory to prevent namespace collisions.
605 if (parent == NULL)
606 parent_kobj = virtual_device_parent(dev);
607 else if (parent->class)
608 return &parent->kobj;
609 else
610 parent_kobj = &parent->kobj;
612 /* find our class-directory at the parent and reference it */
613 spin_lock(&dev->class->p->class_dirs.list_lock);
614 list_for_each_entry(k, &dev->class->p->class_dirs.list, entry)
615 if (k->parent == parent_kobj) {
616 kobj = kobject_get(k);
617 break;
619 spin_unlock(&dev->class->p->class_dirs.list_lock);
620 if (kobj)
621 return kobj;
623 /* or create a new class-directory at the parent device */
624 k = kobject_create();
625 if (!k)
626 return NULL;
627 k->kset = &dev->class->p->class_dirs;
628 retval = kobject_add(k, parent_kobj, "%s", dev->class->name);
629 if (retval < 0) {
630 kobject_put(k);
631 return NULL;
633 /* do not emit an uevent for this simple "glue" directory */
634 return k;
637 if (parent)
638 return &parent->kobj;
639 return NULL;
642 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
644 /* see if we live in a "glue" directory */
645 if (!glue_dir || !dev->class ||
646 glue_dir->kset != &dev->class->p->class_dirs)
647 return;
649 kobject_put(glue_dir);
652 static void cleanup_device_parent(struct device *dev)
654 cleanup_glue_dir(dev, dev->kobj.parent);
656 #endif
658 static void setup_parent(struct device *dev, struct device *parent)
660 struct kobject *kobj;
661 kobj = get_device_parent(dev, parent);
662 if (kobj)
663 dev->kobj.parent = kobj;
666 static int device_add_class_symlinks(struct device *dev)
668 int error;
670 if (!dev->class)
671 return 0;
673 error = sysfs_create_link(&dev->kobj,
674 &dev->class->p->class_subsys.kobj,
675 "subsystem");
676 if (error)
677 goto out;
679 #ifdef CONFIG_SYSFS_DEPRECATED
680 /* stacked class devices need a symlink in the class directory */
681 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
682 device_is_not_partition(dev)) {
683 error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
684 &dev->kobj, dev_name(dev));
685 if (error)
686 goto out_subsys;
689 if (dev->parent && device_is_not_partition(dev)) {
690 struct device *parent = dev->parent;
691 char *class_name;
694 * stacked class devices have the 'device' link
695 * pointing to the bus device instead of the parent
697 while (parent->class && !parent->bus && parent->parent)
698 parent = parent->parent;
700 error = sysfs_create_link(&dev->kobj,
701 &parent->kobj,
702 "device");
703 if (error)
704 goto out_busid;
706 class_name = make_class_name(dev->class->name,
707 &dev->kobj);
708 if (class_name)
709 error = sysfs_create_link(&dev->parent->kobj,
710 &dev->kobj, class_name);
711 kfree(class_name);
712 if (error)
713 goto out_device;
715 return 0;
717 out_device:
718 if (dev->parent && device_is_not_partition(dev))
719 sysfs_remove_link(&dev->kobj, "device");
720 out_busid:
721 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
722 device_is_not_partition(dev))
723 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
724 dev_name(dev));
725 #else
726 /* link in the class directory pointing to the device */
727 error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
728 &dev->kobj, dev_name(dev));
729 if (error)
730 goto out_subsys;
732 if (dev->parent && device_is_not_partition(dev)) {
733 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
734 "device");
735 if (error)
736 goto out_busid;
738 return 0;
740 out_busid:
741 sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
742 #endif
744 out_subsys:
745 sysfs_remove_link(&dev->kobj, "subsystem");
746 out:
747 return error;
750 static void device_remove_class_symlinks(struct device *dev)
752 if (!dev->class)
753 return;
755 #ifdef CONFIG_SYSFS_DEPRECATED
756 if (dev->parent && device_is_not_partition(dev)) {
757 char *class_name;
759 class_name = make_class_name(dev->class->name, &dev->kobj);
760 if (class_name) {
761 sysfs_remove_link(&dev->parent->kobj, class_name);
762 kfree(class_name);
764 sysfs_remove_link(&dev->kobj, "device");
767 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
768 device_is_not_partition(dev))
769 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
770 dev_name(dev));
771 #else
772 if (dev->parent && device_is_not_partition(dev))
773 sysfs_remove_link(&dev->kobj, "device");
775 sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
776 #endif
778 sysfs_remove_link(&dev->kobj, "subsystem");
782 * dev_set_name - set a device name
783 * @dev: device
784 * @fmt: format string for the device's name
786 int dev_set_name(struct device *dev, const char *fmt, ...)
788 va_list vargs;
789 int err;
791 va_start(vargs, fmt);
792 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
793 va_end(vargs);
794 return err;
796 EXPORT_SYMBOL_GPL(dev_set_name);
799 * device_to_dev_kobj - select a /sys/dev/ directory for the device
800 * @dev: device
802 * By default we select char/ for new entries. Setting class->dev_obj
803 * to NULL prevents an entry from being created. class->dev_kobj must
804 * be set (or cleared) before any devices are registered to the class
805 * otherwise device_create_sys_dev_entry() and
806 * device_remove_sys_dev_entry() will disagree about the the presence
807 * of the link.
809 static struct kobject *device_to_dev_kobj(struct device *dev)
811 struct kobject *kobj;
813 if (dev->class)
814 kobj = dev->class->dev_kobj;
815 else
816 kobj = sysfs_dev_char_kobj;
818 return kobj;
821 static int device_create_sys_dev_entry(struct device *dev)
823 struct kobject *kobj = device_to_dev_kobj(dev);
824 int error = 0;
825 char devt_str[15];
827 if (kobj) {
828 format_dev_t(devt_str, dev->devt);
829 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
832 return error;
835 static void device_remove_sys_dev_entry(struct device *dev)
837 struct kobject *kobj = device_to_dev_kobj(dev);
838 char devt_str[15];
840 if (kobj) {
841 format_dev_t(devt_str, dev->devt);
842 sysfs_remove_link(kobj, devt_str);
847 * device_add - add device to device hierarchy.
848 * @dev: device.
850 * This is part 2 of device_register(), though may be called
851 * separately _iff_ device_initialize() has been called separately.
853 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
854 * to the global and sibling lists for the device, then
855 * adds it to the other relevant subsystems of the driver model.
857 * NOTE: _Never_ directly free @dev after calling this function, even
858 * if it returned an error! Always use put_device() to give up your
859 * reference instead.
861 int device_add(struct device *dev)
863 struct device *parent = NULL;
864 struct class_interface *class_intf;
865 int error = -EINVAL;
867 dev = get_device(dev);
868 if (!dev)
869 goto done;
871 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
872 if (!dev->p) {
873 error = -ENOMEM;
874 goto done;
876 dev->p->device = dev;
877 klist_init(&dev->p->klist_children, klist_children_get,
878 klist_children_put);
881 * for statically allocated devices, which should all be converted
882 * some day, we need to initialize the name. We prevent reading back
883 * the name, and force the use of dev_name()
885 if (dev->init_name) {
886 dev_set_name(dev, "%s", dev->init_name);
887 dev->init_name = NULL;
890 if (!dev_name(dev))
891 goto name_error;
893 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
895 parent = get_device(dev->parent);
896 setup_parent(dev, parent);
898 /* use parent numa_node */
899 if (parent)
900 set_dev_node(dev, dev_to_node(parent));
902 /* first, register with generic layer. */
903 /* we require the name to be set before, and pass NULL */
904 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
905 if (error)
906 goto Error;
908 /* notify platform of device entry */
909 if (platform_notify)
910 platform_notify(dev);
912 error = device_create_file(dev, &uevent_attr);
913 if (error)
914 goto attrError;
916 if (MAJOR(dev->devt)) {
917 error = device_create_file(dev, &devt_attr);
918 if (error)
919 goto ueventattrError;
921 error = device_create_sys_dev_entry(dev);
922 if (error)
923 goto devtattrError;
926 error = device_add_class_symlinks(dev);
927 if (error)
928 goto SymlinkError;
929 error = device_add_attrs(dev);
930 if (error)
931 goto AttrsError;
932 error = bus_add_device(dev);
933 if (error)
934 goto BusError;
935 error = dpm_sysfs_add(dev);
936 if (error)
937 goto DPMError;
938 device_pm_add(dev);
940 /* Notify clients of device addition. This call must come
941 * after dpm_sysf_add() and before kobject_uevent().
943 if (dev->bus)
944 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
945 BUS_NOTIFY_ADD_DEVICE, dev);
947 kobject_uevent(&dev->kobj, KOBJ_ADD);
948 bus_attach_device(dev);
949 if (parent)
950 klist_add_tail(&dev->p->knode_parent,
951 &parent->p->klist_children);
953 if (dev->class) {
954 mutex_lock(&dev->class->p->class_mutex);
955 /* tie the class to the device */
956 klist_add_tail(&dev->knode_class,
957 &dev->class->p->class_devices);
959 /* notify any interfaces that the device is here */
960 list_for_each_entry(class_intf,
961 &dev->class->p->class_interfaces, node)
962 if (class_intf->add_dev)
963 class_intf->add_dev(dev, class_intf);
964 mutex_unlock(&dev->class->p->class_mutex);
966 done:
967 put_device(dev);
968 return error;
969 DPMError:
970 bus_remove_device(dev);
971 BusError:
972 device_remove_attrs(dev);
973 AttrsError:
974 device_remove_class_symlinks(dev);
975 SymlinkError:
976 if (MAJOR(dev->devt))
977 device_remove_sys_dev_entry(dev);
978 devtattrError:
979 if (MAJOR(dev->devt))
980 device_remove_file(dev, &devt_attr);
981 ueventattrError:
982 device_remove_file(dev, &uevent_attr);
983 attrError:
984 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
985 kobject_del(&dev->kobj);
986 Error:
987 cleanup_device_parent(dev);
988 if (parent)
989 put_device(parent);
990 name_error:
991 kfree(dev->p);
992 dev->p = NULL;
993 goto done;
997 * device_register - register a device with the system.
998 * @dev: pointer to the device structure
1000 * This happens in two clean steps - initialize the device
1001 * and add it to the system. The two steps can be called
1002 * separately, but this is the easiest and most common.
1003 * I.e. you should only call the two helpers separately if
1004 * have a clearly defined need to use and refcount the device
1005 * before it is added to the hierarchy.
1007 * NOTE: _Never_ directly free @dev after calling this function, even
1008 * if it returned an error! Always use put_device() to give up the
1009 * reference initialized in this function instead.
1011 int device_register(struct device *dev)
1013 device_initialize(dev);
1014 return device_add(dev);
1018 * get_device - increment reference count for device.
1019 * @dev: device.
1021 * This simply forwards the call to kobject_get(), though
1022 * we do take care to provide for the case that we get a NULL
1023 * pointer passed in.
1025 struct device *get_device(struct device *dev)
1027 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
1031 * put_device - decrement reference count.
1032 * @dev: device in question.
1034 void put_device(struct device *dev)
1036 /* might_sleep(); */
1037 if (dev)
1038 kobject_put(&dev->kobj);
1042 * device_del - delete device from system.
1043 * @dev: device.
1045 * This is the first part of the device unregistration
1046 * sequence. This removes the device from the lists we control
1047 * from here, has it removed from the other driver model
1048 * subsystems it was added to in device_add(), and removes it
1049 * from the kobject hierarchy.
1051 * NOTE: this should be called manually _iff_ device_add() was
1052 * also called manually.
1054 void device_del(struct device *dev)
1056 struct device *parent = dev->parent;
1057 struct class_interface *class_intf;
1059 /* Notify clients of device removal. This call must come
1060 * before dpm_sysfs_remove().
1062 if (dev->bus)
1063 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1064 BUS_NOTIFY_DEL_DEVICE, dev);
1065 device_pm_remove(dev);
1066 dpm_sysfs_remove(dev);
1067 if (parent)
1068 klist_del(&dev->p->knode_parent);
1069 if (MAJOR(dev->devt)) {
1070 device_remove_sys_dev_entry(dev);
1071 device_remove_file(dev, &devt_attr);
1073 if (dev->class) {
1074 device_remove_class_symlinks(dev);
1076 mutex_lock(&dev->class->p->class_mutex);
1077 /* notify any interfaces that the device is now gone */
1078 list_for_each_entry(class_intf,
1079 &dev->class->p->class_interfaces, node)
1080 if (class_intf->remove_dev)
1081 class_intf->remove_dev(dev, class_intf);
1082 /* remove the device from the class list */
1083 klist_del(&dev->knode_class);
1084 mutex_unlock(&dev->class->p->class_mutex);
1086 device_remove_file(dev, &uevent_attr);
1087 device_remove_attrs(dev);
1088 bus_remove_device(dev);
1091 * Some platform devices are driven without driver attached
1092 * and managed resources may have been acquired. Make sure
1093 * all resources are released.
1095 devres_release_all(dev);
1097 /* Notify the platform of the removal, in case they
1098 * need to do anything...
1100 if (platform_notify_remove)
1101 platform_notify_remove(dev);
1102 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1103 cleanup_device_parent(dev);
1104 kobject_del(&dev->kobj);
1105 put_device(parent);
1109 * device_unregister - unregister device from system.
1110 * @dev: device going away.
1112 * We do this in two parts, like we do device_register(). First,
1113 * we remove it from all the subsystems with device_del(), then
1114 * we decrement the reference count via put_device(). If that
1115 * is the final reference count, the device will be cleaned up
1116 * via device_release() above. Otherwise, the structure will
1117 * stick around until the final reference to the device is dropped.
1119 void device_unregister(struct device *dev)
1121 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1122 device_del(dev);
1123 put_device(dev);
1126 static struct device *next_device(struct klist_iter *i)
1128 struct klist_node *n = klist_next(i);
1129 struct device *dev = NULL;
1130 struct device_private *p;
1132 if (n) {
1133 p = to_device_private_parent(n);
1134 dev = p->device;
1136 return dev;
1140 * device_get_nodename - path of device node file
1141 * @dev: device
1142 * @tmp: possibly allocated string
1144 * Return the relative path of a possible device node.
1145 * Non-default names may need to allocate a memory to compose
1146 * a name. This memory is returned in tmp and needs to be
1147 * freed by the caller.
1149 const char *device_get_nodename(struct device *dev, const char **tmp)
1151 char *s;
1153 *tmp = NULL;
1155 /* the device type may provide a specific name */
1156 if (dev->type && dev->type->nodename)
1157 *tmp = dev->type->nodename(dev);
1158 if (*tmp)
1159 return *tmp;
1161 /* the class may provide a specific name */
1162 if (dev->class && dev->class->nodename)
1163 *tmp = dev->class->nodename(dev);
1164 if (*tmp)
1165 return *tmp;
1167 /* return name without allocation, tmp == NULL */
1168 if (strchr(dev_name(dev), '!') == NULL)
1169 return dev_name(dev);
1171 /* replace '!' in the name with '/' */
1172 *tmp = kstrdup(dev_name(dev), GFP_KERNEL);
1173 if (!*tmp)
1174 return NULL;
1175 while ((s = strchr(*tmp, '!')))
1176 s[0] = '/';
1177 return *tmp;
1181 * device_for_each_child - device child iterator.
1182 * @parent: parent struct device.
1183 * @data: data for the callback.
1184 * @fn: function to be called for each device.
1186 * Iterate over @parent's child devices, and call @fn for each,
1187 * passing it @data.
1189 * We check the return of @fn each time. If it returns anything
1190 * other than 0, we break out and return that value.
1192 int device_for_each_child(struct device *parent, void *data,
1193 int (*fn)(struct device *dev, void *data))
1195 struct klist_iter i;
1196 struct device *child;
1197 int error = 0;
1199 if (!parent->p)
1200 return 0;
1202 klist_iter_init(&parent->p->klist_children, &i);
1203 while ((child = next_device(&i)) && !error)
1204 error = fn(child, data);
1205 klist_iter_exit(&i);
1206 return error;
1210 * device_find_child - device iterator for locating a particular device.
1211 * @parent: parent struct device
1212 * @data: Data to pass to match function
1213 * @match: Callback function to check device
1215 * This is similar to the device_for_each_child() function above, but it
1216 * returns a reference to a device that is 'found' for later use, as
1217 * determined by the @match callback.
1219 * The callback should return 0 if the device doesn't match and non-zero
1220 * if it does. If the callback returns non-zero and a reference to the
1221 * current device can be obtained, this function will return to the caller
1222 * and not iterate over any more devices.
1224 struct device *device_find_child(struct device *parent, void *data,
1225 int (*match)(struct device *dev, void *data))
1227 struct klist_iter i;
1228 struct device *child;
1230 if (!parent)
1231 return NULL;
1233 klist_iter_init(&parent->p->klist_children, &i);
1234 while ((child = next_device(&i)))
1235 if (match(child, data) && get_device(child))
1236 break;
1237 klist_iter_exit(&i);
1238 return child;
1241 int __init devices_init(void)
1243 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1244 if (!devices_kset)
1245 return -ENOMEM;
1246 dev_kobj = kobject_create_and_add("dev", NULL);
1247 if (!dev_kobj)
1248 goto dev_kobj_err;
1249 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1250 if (!sysfs_dev_block_kobj)
1251 goto block_kobj_err;
1252 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1253 if (!sysfs_dev_char_kobj)
1254 goto char_kobj_err;
1256 return 0;
1258 char_kobj_err:
1259 kobject_put(sysfs_dev_block_kobj);
1260 block_kobj_err:
1261 kobject_put(dev_kobj);
1262 dev_kobj_err:
1263 kset_unregister(devices_kset);
1264 return -ENOMEM;
1267 EXPORT_SYMBOL_GPL(device_for_each_child);
1268 EXPORT_SYMBOL_GPL(device_find_child);
1270 EXPORT_SYMBOL_GPL(device_initialize);
1271 EXPORT_SYMBOL_GPL(device_add);
1272 EXPORT_SYMBOL_GPL(device_register);
1274 EXPORT_SYMBOL_GPL(device_del);
1275 EXPORT_SYMBOL_GPL(device_unregister);
1276 EXPORT_SYMBOL_GPL(get_device);
1277 EXPORT_SYMBOL_GPL(put_device);
1279 EXPORT_SYMBOL_GPL(device_create_file);
1280 EXPORT_SYMBOL_GPL(device_remove_file);
1282 struct root_device
1284 struct device dev;
1285 struct module *owner;
1288 #define to_root_device(dev) container_of(dev, struct root_device, dev)
1290 static void root_device_release(struct device *dev)
1292 kfree(to_root_device(dev));
1296 * __root_device_register - allocate and register a root device
1297 * @name: root device name
1298 * @owner: owner module of the root device, usually THIS_MODULE
1300 * This function allocates a root device and registers it
1301 * using device_register(). In order to free the returned
1302 * device, use root_device_unregister().
1304 * Root devices are dummy devices which allow other devices
1305 * to be grouped under /sys/devices. Use this function to
1306 * allocate a root device and then use it as the parent of
1307 * any device which should appear under /sys/devices/{name}
1309 * The /sys/devices/{name} directory will also contain a
1310 * 'module' symlink which points to the @owner directory
1311 * in sysfs.
1313 * Note: You probably want to use root_device_register().
1315 struct device *__root_device_register(const char *name, struct module *owner)
1317 struct root_device *root;
1318 int err = -ENOMEM;
1320 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
1321 if (!root)
1322 return ERR_PTR(err);
1324 err = dev_set_name(&root->dev, "%s", name);
1325 if (err) {
1326 kfree(root);
1327 return ERR_PTR(err);
1330 root->dev.release = root_device_release;
1332 err = device_register(&root->dev);
1333 if (err) {
1334 put_device(&root->dev);
1335 return ERR_PTR(err);
1338 #ifdef CONFIG_MODULE /* gotta find a "cleaner" way to do this */
1339 if (owner) {
1340 struct module_kobject *mk = &owner->mkobj;
1342 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
1343 if (err) {
1344 device_unregister(&root->dev);
1345 return ERR_PTR(err);
1347 root->owner = owner;
1349 #endif
1351 return &root->dev;
1353 EXPORT_SYMBOL_GPL(__root_device_register);
1356 * root_device_unregister - unregister and free a root device
1357 * @dev: device going away
1359 * This function unregisters and cleans up a device that was created by
1360 * root_device_register().
1362 void root_device_unregister(struct device *dev)
1364 struct root_device *root = to_root_device(dev);
1366 if (root->owner)
1367 sysfs_remove_link(&root->dev.kobj, "module");
1369 device_unregister(dev);
1371 EXPORT_SYMBOL_GPL(root_device_unregister);
1374 static void device_create_release(struct device *dev)
1376 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1377 kfree(dev);
1381 * device_create_vargs - creates a device and registers it with sysfs
1382 * @class: pointer to the struct class that this device should be registered to
1383 * @parent: pointer to the parent struct device of this new device, if any
1384 * @devt: the dev_t for the char device to be added
1385 * @drvdata: the data to be added to the device for callbacks
1386 * @fmt: string for the device's name
1387 * @args: va_list for the device's name
1389 * This function can be used by char device classes. A struct device
1390 * will be created in sysfs, registered to the specified class.
1392 * A "dev" file will be created, showing the dev_t for the device, if
1393 * the dev_t is not 0,0.
1394 * If a pointer to a parent struct device is passed in, the newly created
1395 * struct device will be a child of that device in sysfs.
1396 * The pointer to the struct device will be returned from the call.
1397 * Any further sysfs files that might be required can be created using this
1398 * pointer.
1400 * Note: the struct class passed to this function must have previously
1401 * been created with a call to class_create().
1403 struct device *device_create_vargs(struct class *class, struct device *parent,
1404 dev_t devt, void *drvdata, const char *fmt,
1405 va_list args)
1407 struct device *dev = NULL;
1408 int retval = -ENODEV;
1410 if (class == NULL || IS_ERR(class))
1411 goto error;
1413 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1414 if (!dev) {
1415 retval = -ENOMEM;
1416 goto error;
1419 dev->devt = devt;
1420 dev->class = class;
1421 dev->parent = parent;
1422 dev->release = device_create_release;
1423 dev_set_drvdata(dev, drvdata);
1425 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
1426 if (retval)
1427 goto error;
1429 retval = device_register(dev);
1430 if (retval)
1431 goto error;
1433 return dev;
1435 error:
1436 put_device(dev);
1437 return ERR_PTR(retval);
1439 EXPORT_SYMBOL_GPL(device_create_vargs);
1442 * device_create - creates a device and registers it with sysfs
1443 * @class: pointer to the struct class that this device should be registered to
1444 * @parent: pointer to the parent struct device of this new device, if any
1445 * @devt: the dev_t for the char device to be added
1446 * @drvdata: the data to be added to the device for callbacks
1447 * @fmt: string for the device's name
1449 * This function can be used by char device classes. A struct device
1450 * will be created in sysfs, registered to the specified class.
1452 * A "dev" file will be created, showing the dev_t for the device, if
1453 * the dev_t is not 0,0.
1454 * If a pointer to a parent struct device is passed in, the newly created
1455 * struct device will be a child of that device in sysfs.
1456 * The pointer to the struct device will be returned from the call.
1457 * Any further sysfs files that might be required can be created using this
1458 * pointer.
1460 * Note: the struct class passed to this function must have previously
1461 * been created with a call to class_create().
1463 struct device *device_create(struct class *class, struct device *parent,
1464 dev_t devt, void *drvdata, const char *fmt, ...)
1466 va_list vargs;
1467 struct device *dev;
1469 va_start(vargs, fmt);
1470 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
1471 va_end(vargs);
1472 return dev;
1474 EXPORT_SYMBOL_GPL(device_create);
1476 static int __match_devt(struct device *dev, void *data)
1478 dev_t *devt = data;
1480 return dev->devt == *devt;
1484 * device_destroy - removes a device that was created with device_create()
1485 * @class: pointer to the struct class that this device was registered with
1486 * @devt: the dev_t of the device that was previously registered
1488 * This call unregisters and cleans up a device that was created with a
1489 * call to device_create().
1491 void device_destroy(struct class *class, dev_t devt)
1493 struct device *dev;
1495 dev = class_find_device(class, NULL, &devt, __match_devt);
1496 if (dev) {
1497 put_device(dev);
1498 device_unregister(dev);
1501 EXPORT_SYMBOL_GPL(device_destroy);
1504 * device_rename - renames a device
1505 * @dev: the pointer to the struct device to be renamed
1506 * @new_name: the new name of the device
1508 * It is the responsibility of the caller to provide mutual
1509 * exclusion between two different calls of device_rename
1510 * on the same device to ensure that new_name is valid and
1511 * won't conflict with other devices.
1513 int device_rename(struct device *dev, char *new_name)
1515 char *old_class_name = NULL;
1516 char *new_class_name = NULL;
1517 char *old_device_name = NULL;
1518 int error;
1520 dev = get_device(dev);
1521 if (!dev)
1522 return -EINVAL;
1524 pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
1525 __func__, new_name);
1527 #ifdef CONFIG_SYSFS_DEPRECATED
1528 if ((dev->class) && (dev->parent))
1529 old_class_name = make_class_name(dev->class->name, &dev->kobj);
1530 #endif
1532 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
1533 if (!old_device_name) {
1534 error = -ENOMEM;
1535 goto out;
1538 error = kobject_rename(&dev->kobj, new_name);
1539 if (error)
1540 goto out;
1542 #ifdef CONFIG_SYSFS_DEPRECATED
1543 if (old_class_name) {
1544 new_class_name = make_class_name(dev->class->name, &dev->kobj);
1545 if (new_class_name) {
1546 error = sysfs_create_link_nowarn(&dev->parent->kobj,
1547 &dev->kobj,
1548 new_class_name);
1549 if (error)
1550 goto out;
1551 sysfs_remove_link(&dev->parent->kobj, old_class_name);
1554 #else
1555 if (dev->class) {
1556 error = sysfs_create_link_nowarn(&dev->class->p->class_subsys.kobj,
1557 &dev->kobj, dev_name(dev));
1558 if (error)
1559 goto out;
1560 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
1561 old_device_name);
1563 #endif
1565 out:
1566 put_device(dev);
1568 kfree(new_class_name);
1569 kfree(old_class_name);
1570 kfree(old_device_name);
1572 return error;
1574 EXPORT_SYMBOL_GPL(device_rename);
1576 static int device_move_class_links(struct device *dev,
1577 struct device *old_parent,
1578 struct device *new_parent)
1580 int error = 0;
1581 #ifdef CONFIG_SYSFS_DEPRECATED
1582 char *class_name;
1584 class_name = make_class_name(dev->class->name, &dev->kobj);
1585 if (!class_name) {
1586 error = -ENOMEM;
1587 goto out;
1589 if (old_parent) {
1590 sysfs_remove_link(&dev->kobj, "device");
1591 sysfs_remove_link(&old_parent->kobj, class_name);
1593 if (new_parent) {
1594 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1595 "device");
1596 if (error)
1597 goto out;
1598 error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
1599 class_name);
1600 if (error)
1601 sysfs_remove_link(&dev->kobj, "device");
1602 } else
1603 error = 0;
1604 out:
1605 kfree(class_name);
1606 return error;
1607 #else
1608 if (old_parent)
1609 sysfs_remove_link(&dev->kobj, "device");
1610 if (new_parent)
1611 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1612 "device");
1613 return error;
1614 #endif
1618 * device_move - moves a device to a new parent
1619 * @dev: the pointer to the struct device to be moved
1620 * @new_parent: the new parent of the device (can by NULL)
1621 * @dpm_order: how to reorder the dpm_list
1623 int device_move(struct device *dev, struct device *new_parent,
1624 enum dpm_order dpm_order)
1626 int error;
1627 struct device *old_parent;
1628 struct kobject *new_parent_kobj;
1630 dev = get_device(dev);
1631 if (!dev)
1632 return -EINVAL;
1634 device_pm_lock();
1635 new_parent = get_device(new_parent);
1636 new_parent_kobj = get_device_parent(dev, new_parent);
1638 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
1639 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
1640 error = kobject_move(&dev->kobj, new_parent_kobj);
1641 if (error) {
1642 cleanup_glue_dir(dev, new_parent_kobj);
1643 put_device(new_parent);
1644 goto out;
1646 old_parent = dev->parent;
1647 dev->parent = new_parent;
1648 if (old_parent)
1649 klist_remove(&dev->p->knode_parent);
1650 if (new_parent) {
1651 klist_add_tail(&dev->p->knode_parent,
1652 &new_parent->p->klist_children);
1653 set_dev_node(dev, dev_to_node(new_parent));
1656 if (!dev->class)
1657 goto out_put;
1658 error = device_move_class_links(dev, old_parent, new_parent);
1659 if (error) {
1660 /* We ignore errors on cleanup since we're hosed anyway... */
1661 device_move_class_links(dev, new_parent, old_parent);
1662 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1663 if (new_parent)
1664 klist_remove(&dev->p->knode_parent);
1665 dev->parent = old_parent;
1666 if (old_parent) {
1667 klist_add_tail(&dev->p->knode_parent,
1668 &old_parent->p->klist_children);
1669 set_dev_node(dev, dev_to_node(old_parent));
1672 cleanup_glue_dir(dev, new_parent_kobj);
1673 put_device(new_parent);
1674 goto out;
1676 switch (dpm_order) {
1677 case DPM_ORDER_NONE:
1678 break;
1679 case DPM_ORDER_DEV_AFTER_PARENT:
1680 device_pm_move_after(dev, new_parent);
1681 break;
1682 case DPM_ORDER_PARENT_BEFORE_DEV:
1683 device_pm_move_before(new_parent, dev);
1684 break;
1685 case DPM_ORDER_DEV_LAST:
1686 device_pm_move_last(dev);
1687 break;
1689 out_put:
1690 put_device(old_parent);
1691 out:
1692 device_pm_unlock();
1693 put_device(dev);
1694 return error;
1696 EXPORT_SYMBOL_GPL(device_move);
1699 * device_shutdown - call ->shutdown() on each device to shutdown.
1701 void device_shutdown(void)
1703 struct device *dev, *devn;
1705 list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list,
1706 kobj.entry) {
1707 if (dev->bus && dev->bus->shutdown) {
1708 dev_dbg(dev, "shutdown\n");
1709 dev->bus->shutdown(dev);
1710 } else if (dev->driver && dev->driver->shutdown) {
1711 dev_dbg(dev, "shutdown\n");
1712 dev->driver->shutdown(dev);
1715 kobject_put(sysfs_dev_char_kobj);
1716 kobject_put(sysfs_dev_block_kobj);
1717 kobject_put(dev_kobj);
1718 async_synchronize_full();