2 * drivers/base/core.c - core driver model code (device registration, etc)
4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs
6 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (c) 2006 Novell, Inc.
9 * This file is released under the GPLv2
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <linux/kdev_t.h>
20 #include <linux/notifier.h>
22 #include <linux/of_device.h>
23 #include <linux/genhd.h>
24 #include <linux/kallsyms.h>
25 #include <linux/mutex.h>
26 #include <linux/async.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/netdevice.h>
31 #include "power/power.h"
33 #ifdef CONFIG_SYSFS_DEPRECATED
34 #ifdef CONFIG_SYSFS_DEPRECATED_V2
35 long sysfs_deprecated
= 1;
37 long sysfs_deprecated
= 0;
39 static __init
int sysfs_deprecated_setup(char *arg
)
41 return strict_strtol(arg
, 10, &sysfs_deprecated
);
43 early_param("sysfs.deprecated", sysfs_deprecated_setup
);
46 int (*platform_notify
)(struct device
*dev
) = NULL
;
47 int (*platform_notify_remove
)(struct device
*dev
) = NULL
;
48 static struct kobject
*dev_kobj
;
49 struct kobject
*sysfs_dev_char_kobj
;
50 struct kobject
*sysfs_dev_block_kobj
;
53 static inline int device_is_not_partition(struct device
*dev
)
55 return !(dev
->type
== &part_type
);
58 static inline int device_is_not_partition(struct device
*dev
)
65 * dev_driver_string - Return a device's driver name, if at all possible
66 * @dev: struct device to get the name of
68 * Will return the device's driver's name if it is bound to a device. If
69 * the device is not bound to a driver, it will return the name of the bus
70 * it is attached to. If it is not attached to a bus either, an empty
71 * string will be returned.
73 const char *dev_driver_string(const struct device
*dev
)
75 struct device_driver
*drv
;
77 /* dev->driver can change to NULL underneath us because of unbinding,
78 * so be careful about accessing it. dev->bus and dev->class should
79 * never change once they are set, so they don't need special care.
81 drv
= ACCESS_ONCE(dev
->driver
);
82 return drv
? drv
->name
:
83 (dev
->bus
? dev
->bus
->name
:
84 (dev
->class ? dev
->class->name
: ""));
86 EXPORT_SYMBOL(dev_driver_string
);
88 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
90 static ssize_t
dev_attr_show(struct kobject
*kobj
, struct attribute
*attr
,
93 struct device_attribute
*dev_attr
= to_dev_attr(attr
);
94 struct device
*dev
= kobj_to_dev(kobj
);
98 ret
= dev_attr
->show(dev
, dev_attr
, buf
);
99 if (ret
>= (ssize_t
)PAGE_SIZE
) {
100 print_symbol("dev_attr_show: %s returned bad count\n",
101 (unsigned long)dev_attr
->show
);
106 static ssize_t
dev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
107 const char *buf
, size_t count
)
109 struct device_attribute
*dev_attr
= to_dev_attr(attr
);
110 struct device
*dev
= kobj_to_dev(kobj
);
114 ret
= dev_attr
->store(dev
, dev_attr
, buf
, count
);
118 static const struct sysfs_ops dev_sysfs_ops
= {
119 .show
= dev_attr_show
,
120 .store
= dev_attr_store
,
123 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
125 ssize_t
device_store_ulong(struct device
*dev
,
126 struct device_attribute
*attr
,
127 const char *buf
, size_t size
)
129 struct dev_ext_attribute
*ea
= to_ext_attr(attr
);
131 unsigned long new = simple_strtoul(buf
, &end
, 0);
134 *(unsigned long *)(ea
->var
) = new;
135 /* Always return full write size even if we didn't consume all */
138 EXPORT_SYMBOL_GPL(device_store_ulong
);
140 ssize_t
device_show_ulong(struct device
*dev
,
141 struct device_attribute
*attr
,
144 struct dev_ext_attribute
*ea
= to_ext_attr(attr
);
145 return snprintf(buf
, PAGE_SIZE
, "%lx\n", *(unsigned long *)(ea
->var
));
147 EXPORT_SYMBOL_GPL(device_show_ulong
);
149 ssize_t
device_store_int(struct device
*dev
,
150 struct device_attribute
*attr
,
151 const char *buf
, size_t size
)
153 struct dev_ext_attribute
*ea
= to_ext_attr(attr
);
155 long new = simple_strtol(buf
, &end
, 0);
156 if (end
== buf
|| new > INT_MAX
|| new < INT_MIN
)
158 *(int *)(ea
->var
) = new;
159 /* Always return full write size even if we didn't consume all */
162 EXPORT_SYMBOL_GPL(device_store_int
);
164 ssize_t
device_show_int(struct device
*dev
,
165 struct device_attribute
*attr
,
168 struct dev_ext_attribute
*ea
= to_ext_attr(attr
);
170 return snprintf(buf
, PAGE_SIZE
, "%d\n", *(int *)(ea
->var
));
172 EXPORT_SYMBOL_GPL(device_show_int
);
174 ssize_t
device_store_bool(struct device
*dev
, struct device_attribute
*attr
,
175 const char *buf
, size_t size
)
177 struct dev_ext_attribute
*ea
= to_ext_attr(attr
);
179 if (strtobool(buf
, ea
->var
) < 0)
184 EXPORT_SYMBOL_GPL(device_store_bool
);
186 ssize_t
device_show_bool(struct device
*dev
, struct device_attribute
*attr
,
189 struct dev_ext_attribute
*ea
= to_ext_attr(attr
);
191 return snprintf(buf
, PAGE_SIZE
, "%d\n", *(bool *)(ea
->var
));
193 EXPORT_SYMBOL_GPL(device_show_bool
);
196 * device_release - free device structure.
197 * @kobj: device's kobject.
199 * This is called once the reference count for the object
200 * reaches 0. We forward the call to the device's release
201 * method, which should handle actually freeing the structure.
203 static void device_release(struct kobject
*kobj
)
205 struct device
*dev
= kobj_to_dev(kobj
);
206 struct device_private
*p
= dev
->p
;
209 * Some platform devices are driven without driver attached
210 * and managed resources may have been acquired. Make sure
211 * all resources are released.
213 * Drivers still can add resources into device after device
214 * is deleted but alive, so release devres here to avoid
215 * possible memory leak.
217 devres_release_all(dev
);
221 else if (dev
->type
&& dev
->type
->release
)
222 dev
->type
->release(dev
);
223 else if (dev
->class && dev
->class->dev_release
)
224 dev
->class->dev_release(dev
);
226 WARN(1, KERN_ERR
"Device '%s' does not have a release() "
227 "function, it is broken and must be fixed.\n",
232 static const void *device_namespace(struct kobject
*kobj
)
234 struct device
*dev
= kobj_to_dev(kobj
);
235 const void *ns
= NULL
;
237 if (dev
->class && dev
->class->ns_type
)
238 ns
= dev
->class->namespace(dev
);
243 static struct kobj_type device_ktype
= {
244 .release
= device_release
,
245 .sysfs_ops
= &dev_sysfs_ops
,
246 .namespace = device_namespace
,
250 static int dev_uevent_filter(struct kset
*kset
, struct kobject
*kobj
)
252 struct kobj_type
*ktype
= get_ktype(kobj
);
254 if (ktype
== &device_ktype
) {
255 struct device
*dev
= kobj_to_dev(kobj
);
264 static const char *dev_uevent_name(struct kset
*kset
, struct kobject
*kobj
)
266 struct device
*dev
= kobj_to_dev(kobj
);
269 return dev
->bus
->name
;
271 return dev
->class->name
;
275 static int dev_uevent(struct kset
*kset
, struct kobject
*kobj
,
276 struct kobj_uevent_env
*env
)
278 struct device
*dev
= kobj_to_dev(kobj
);
281 /* add device node properties if present */
282 if (MAJOR(dev
->devt
)) {
287 add_uevent_var(env
, "MAJOR=%u", MAJOR(dev
->devt
));
288 add_uevent_var(env
, "MINOR=%u", MINOR(dev
->devt
));
289 name
= device_get_devnode(dev
, &mode
, &tmp
);
291 add_uevent_var(env
, "DEVNAME=%s", name
);
294 add_uevent_var(env
, "DEVMODE=%#o", mode
& 0777);
298 if (dev
->type
&& dev
->type
->name
)
299 add_uevent_var(env
, "DEVTYPE=%s", dev
->type
->name
);
302 add_uevent_var(env
, "DRIVER=%s", dev
->driver
->name
);
304 /* Add common DT information about the device */
305 of_device_uevent(dev
, env
);
307 /* have the bus specific function add its stuff */
308 if (dev
->bus
&& dev
->bus
->uevent
) {
309 retval
= dev
->bus
->uevent(dev
, env
);
311 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
312 dev_name(dev
), __func__
, retval
);
315 /* have the class specific function add its stuff */
316 if (dev
->class && dev
->class->dev_uevent
) {
317 retval
= dev
->class->dev_uevent(dev
, env
);
319 pr_debug("device: '%s': %s: class uevent() "
320 "returned %d\n", dev_name(dev
),
324 /* have the device type specific function add its stuff */
325 if (dev
->type
&& dev
->type
->uevent
) {
326 retval
= dev
->type
->uevent(dev
, env
);
328 pr_debug("device: '%s': %s: dev_type uevent() "
329 "returned %d\n", dev_name(dev
),
336 static const struct kset_uevent_ops device_uevent_ops
= {
337 .filter
= dev_uevent_filter
,
338 .name
= dev_uevent_name
,
339 .uevent
= dev_uevent
,
342 static ssize_t
show_uevent(struct device
*dev
, struct device_attribute
*attr
,
345 struct kobject
*top_kobj
;
347 struct kobj_uevent_env
*env
= NULL
;
352 /* search the kset, the device belongs to */
353 top_kobj
= &dev
->kobj
;
354 while (!top_kobj
->kset
&& top_kobj
->parent
)
355 top_kobj
= top_kobj
->parent
;
359 kset
= top_kobj
->kset
;
360 if (!kset
->uevent_ops
|| !kset
->uevent_ops
->uevent
)
364 if (kset
->uevent_ops
&& kset
->uevent_ops
->filter
)
365 if (!kset
->uevent_ops
->filter(kset
, &dev
->kobj
))
368 env
= kzalloc(sizeof(struct kobj_uevent_env
), GFP_KERNEL
);
372 /* let the kset specific function add its keys */
373 retval
= kset
->uevent_ops
->uevent(kset
, &dev
->kobj
, env
);
377 /* copy keys to file */
378 for (i
= 0; i
< env
->envp_idx
; i
++)
379 count
+= sprintf(&buf
[count
], "%s\n", env
->envp
[i
]);
385 static ssize_t
store_uevent(struct device
*dev
, struct device_attribute
*attr
,
386 const char *buf
, size_t count
)
388 enum kobject_action action
;
390 if (kobject_action_type(buf
, count
, &action
) == 0)
391 kobject_uevent(&dev
->kobj
, action
);
393 dev_err(dev
, "uevent: unknown action-string\n");
397 static struct device_attribute uevent_attr
=
398 __ATTR(uevent
, S_IRUGO
| S_IWUSR
, show_uevent
, store_uevent
);
400 static int device_add_attributes(struct device
*dev
,
401 struct device_attribute
*attrs
)
407 for (i
= 0; attr_name(attrs
[i
]); i
++) {
408 error
= device_create_file(dev
, &attrs
[i
]);
414 device_remove_file(dev
, &attrs
[i
]);
419 static void device_remove_attributes(struct device
*dev
,
420 struct device_attribute
*attrs
)
425 for (i
= 0; attr_name(attrs
[i
]); i
++)
426 device_remove_file(dev
, &attrs
[i
]);
429 static int device_add_bin_attributes(struct device
*dev
,
430 struct bin_attribute
*attrs
)
436 for (i
= 0; attr_name(attrs
[i
]); i
++) {
437 error
= device_create_bin_file(dev
, &attrs
[i
]);
443 device_remove_bin_file(dev
, &attrs
[i
]);
448 static void device_remove_bin_attributes(struct device
*dev
,
449 struct bin_attribute
*attrs
)
454 for (i
= 0; attr_name(attrs
[i
]); i
++)
455 device_remove_bin_file(dev
, &attrs
[i
]);
458 static int device_add_groups(struct device
*dev
,
459 const struct attribute_group
**groups
)
465 for (i
= 0; groups
[i
]; i
++) {
466 error
= sysfs_create_group(&dev
->kobj
, groups
[i
]);
469 sysfs_remove_group(&dev
->kobj
,
478 static void device_remove_groups(struct device
*dev
,
479 const struct attribute_group
**groups
)
484 for (i
= 0; groups
[i
]; i
++)
485 sysfs_remove_group(&dev
->kobj
, groups
[i
]);
488 static int device_add_attrs(struct device
*dev
)
490 struct class *class = dev
->class;
491 const struct device_type
*type
= dev
->type
;
495 error
= device_add_attributes(dev
, class->dev_attrs
);
498 error
= device_add_bin_attributes(dev
, class->dev_bin_attrs
);
500 goto err_remove_class_attrs
;
504 error
= device_add_groups(dev
, type
->groups
);
506 goto err_remove_class_bin_attrs
;
509 error
= device_add_groups(dev
, dev
->groups
);
511 goto err_remove_type_groups
;
515 err_remove_type_groups
:
517 device_remove_groups(dev
, type
->groups
);
518 err_remove_class_bin_attrs
:
520 device_remove_bin_attributes(dev
, class->dev_bin_attrs
);
521 err_remove_class_attrs
:
523 device_remove_attributes(dev
, class->dev_attrs
);
528 static void device_remove_attrs(struct device
*dev
)
530 struct class *class = dev
->class;
531 const struct device_type
*type
= dev
->type
;
533 device_remove_groups(dev
, dev
->groups
);
536 device_remove_groups(dev
, type
->groups
);
539 device_remove_attributes(dev
, class->dev_attrs
);
540 device_remove_bin_attributes(dev
, class->dev_bin_attrs
);
545 static ssize_t
show_dev(struct device
*dev
, struct device_attribute
*attr
,
548 return print_dev_t(buf
, dev
->devt
);
551 static struct device_attribute devt_attr
=
552 __ATTR(dev
, S_IRUGO
, show_dev
, NULL
);
555 struct kset
*devices_kset
;
558 * device_create_file - create sysfs attribute file for device.
560 * @attr: device attribute descriptor.
562 int device_create_file(struct device
*dev
,
563 const struct device_attribute
*attr
)
568 WARN(((attr
->attr
.mode
& S_IWUGO
) && !attr
->store
),
569 "Write permission without 'store'\n");
570 WARN(((attr
->attr
.mode
& S_IRUGO
) && !attr
->show
),
571 "Read permission without 'show'\n");
572 error
= sysfs_create_file(&dev
->kobj
, &attr
->attr
);
579 * device_remove_file - remove sysfs attribute file.
581 * @attr: device attribute descriptor.
583 void device_remove_file(struct device
*dev
,
584 const struct device_attribute
*attr
)
587 sysfs_remove_file(&dev
->kobj
, &attr
->attr
);
591 * device_create_bin_file - create sysfs binary attribute file for device.
593 * @attr: device binary attribute descriptor.
595 int device_create_bin_file(struct device
*dev
,
596 const struct bin_attribute
*attr
)
600 error
= sysfs_create_bin_file(&dev
->kobj
, attr
);
603 EXPORT_SYMBOL_GPL(device_create_bin_file
);
606 * device_remove_bin_file - remove sysfs binary attribute file
608 * @attr: device binary attribute descriptor.
610 void device_remove_bin_file(struct device
*dev
,
611 const struct bin_attribute
*attr
)
614 sysfs_remove_bin_file(&dev
->kobj
, attr
);
616 EXPORT_SYMBOL_GPL(device_remove_bin_file
);
619 * device_schedule_callback_owner - helper to schedule a callback for a device
621 * @func: callback function to invoke later.
622 * @owner: module owning the callback routine
624 * Attribute methods must not unregister themselves or their parent device
625 * (which would amount to the same thing). Attempts to do so will deadlock,
626 * since unregistration is mutually exclusive with driver callbacks.
628 * Instead methods can call this routine, which will attempt to allocate
629 * and schedule a workqueue request to call back @func with @dev as its
630 * argument in the workqueue's process context. @dev will be pinned until
633 * This routine is usually called via the inline device_schedule_callback(),
634 * which automatically sets @owner to THIS_MODULE.
636 * Returns 0 if the request was submitted, -ENOMEM if storage could not
637 * be allocated, -ENODEV if a reference to @owner isn't available.
639 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
640 * underlying sysfs routine (since it is intended for use by attribute
641 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
643 int device_schedule_callback_owner(struct device
*dev
,
644 void (*func
)(struct device
*), struct module
*owner
)
646 return sysfs_schedule_callback(&dev
->kobj
,
647 (void (*)(void *)) func
, dev
, owner
);
649 EXPORT_SYMBOL_GPL(device_schedule_callback_owner
);
651 static void klist_children_get(struct klist_node
*n
)
653 struct device_private
*p
= to_device_private_parent(n
);
654 struct device
*dev
= p
->device
;
659 static void klist_children_put(struct klist_node
*n
)
661 struct device_private
*p
= to_device_private_parent(n
);
662 struct device
*dev
= p
->device
;
668 * device_initialize - init device structure.
671 * This prepares the device for use by other layers by initializing
673 * It is the first half of device_register(), if called by
674 * that function, though it can also be called separately, so one
675 * may use @dev's fields. In particular, get_device()/put_device()
676 * may be used for reference counting of @dev after calling this
679 * All fields in @dev must be initialized by the caller to 0, except
680 * for those explicitly set to some other value. The simplest
681 * approach is to use kzalloc() to allocate the structure containing
684 * NOTE: Use put_device() to give up your reference instead of freeing
685 * @dev directly once you have called this function.
687 void device_initialize(struct device
*dev
)
689 dev
->kobj
.kset
= devices_kset
;
690 kobject_init(&dev
->kobj
, &device_ktype
);
691 INIT_LIST_HEAD(&dev
->dma_pools
);
692 mutex_init(&dev
->mutex
);
693 lockdep_set_novalidate_class(&dev
->mutex
);
694 spin_lock_init(&dev
->devres_lock
);
695 INIT_LIST_HEAD(&dev
->devres_head
);
697 set_dev_node(dev
, -1);
700 static struct kobject
*virtual_device_parent(struct device
*dev
)
702 static struct kobject
*virtual_dir
= NULL
;
705 virtual_dir
= kobject_create_and_add("virtual",
706 &devices_kset
->kobj
);
716 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
718 static void class_dir_release(struct kobject
*kobj
)
720 struct class_dir
*dir
= to_class_dir(kobj
);
725 struct kobj_ns_type_operations
*class_dir_child_ns_type(struct kobject
*kobj
)
727 struct class_dir
*dir
= to_class_dir(kobj
);
728 return dir
->class->ns_type
;
731 static struct kobj_type class_dir_ktype
= {
732 .release
= class_dir_release
,
733 .sysfs_ops
= &kobj_sysfs_ops
,
734 .child_ns_type
= class_dir_child_ns_type
737 static struct kobject
*
738 class_dir_create_and_add(struct class *class, struct kobject
*parent_kobj
)
740 struct class_dir
*dir
;
743 dir
= kzalloc(sizeof(*dir
), GFP_KERNEL
);
748 kobject_init(&dir
->kobj
, &class_dir_ktype
);
750 dir
->kobj
.kset
= &class->p
->glue_dirs
;
752 retval
= kobject_add(&dir
->kobj
, parent_kobj
, "%s", class->name
);
754 kobject_put(&dir
->kobj
);
761 static struct kobject
*get_device_parent(struct device
*dev
,
762 struct device
*parent
)
765 static DEFINE_MUTEX(gdp_mutex
);
766 struct kobject
*kobj
= NULL
;
767 struct kobject
*parent_kobj
;
771 /* block disks show up in /sys/block */
772 if (sysfs_deprecated
&& dev
->class == &block_class
) {
773 if (parent
&& parent
->class == &block_class
)
774 return &parent
->kobj
;
775 return &block_class
.p
->subsys
.kobj
;
780 * If we have no parent, we live in "virtual".
781 * Class-devices with a non class-device as parent, live
782 * in a "glue" directory to prevent namespace collisions.
785 parent_kobj
= virtual_device_parent(dev
);
786 else if (parent
->class && !dev
->class->ns_type
)
787 return &parent
->kobj
;
789 parent_kobj
= &parent
->kobj
;
791 mutex_lock(&gdp_mutex
);
793 /* find our class-directory at the parent and reference it */
794 spin_lock(&dev
->class->p
->glue_dirs
.list_lock
);
795 list_for_each_entry(k
, &dev
->class->p
->glue_dirs
.list
, entry
)
796 if (k
->parent
== parent_kobj
) {
797 kobj
= kobject_get(k
);
800 spin_unlock(&dev
->class->p
->glue_dirs
.list_lock
);
802 mutex_unlock(&gdp_mutex
);
806 /* or create a new class-directory at the parent device */
807 k
= class_dir_create_and_add(dev
->class, parent_kobj
);
808 /* do not emit an uevent for this simple "glue" directory */
809 mutex_unlock(&gdp_mutex
);
813 /* subsystems can specify a default root directory for their devices */
814 if (!parent
&& dev
->bus
&& dev
->bus
->dev_root
)
815 return &dev
->bus
->dev_root
->kobj
;
818 return &parent
->kobj
;
822 static void cleanup_glue_dir(struct device
*dev
, struct kobject
*glue_dir
)
824 /* see if we live in a "glue" directory */
825 if (!glue_dir
|| !dev
->class ||
826 glue_dir
->kset
!= &dev
->class->p
->glue_dirs
)
829 kobject_put(glue_dir
);
832 static void cleanup_device_parent(struct device
*dev
)
834 cleanup_glue_dir(dev
, dev
->kobj
.parent
);
837 static int device_add_class_symlinks(struct device
*dev
)
844 error
= sysfs_create_link(&dev
->kobj
,
845 &dev
->class->p
->subsys
.kobj
,
850 if (dev
->parent
&& device_is_not_partition(dev
)) {
851 error
= sysfs_create_link(&dev
->kobj
, &dev
->parent
->kobj
,
858 /* /sys/block has directories and does not need symlinks */
859 if (sysfs_deprecated
&& dev
->class == &block_class
)
863 /* link in the class directory pointing to the device */
864 error
= sysfs_create_link(&dev
->class->p
->subsys
.kobj
,
865 &dev
->kobj
, dev_name(dev
));
872 sysfs_remove_link(&dev
->kobj
, "device");
875 sysfs_remove_link(&dev
->kobj
, "subsystem");
880 static void device_remove_class_symlinks(struct device
*dev
)
885 if (dev
->parent
&& device_is_not_partition(dev
))
886 sysfs_remove_link(&dev
->kobj
, "device");
887 sysfs_remove_link(&dev
->kobj
, "subsystem");
889 if (sysfs_deprecated
&& dev
->class == &block_class
)
892 sysfs_delete_link(&dev
->class->p
->subsys
.kobj
, &dev
->kobj
, dev_name(dev
));
896 * dev_set_name - set a device name
898 * @fmt: format string for the device's name
900 int dev_set_name(struct device
*dev
, const char *fmt
, ...)
905 va_start(vargs
, fmt
);
906 err
= kobject_set_name_vargs(&dev
->kobj
, fmt
, vargs
);
910 EXPORT_SYMBOL_GPL(dev_set_name
);
913 * device_to_dev_kobj - select a /sys/dev/ directory for the device
916 * By default we select char/ for new entries. Setting class->dev_obj
917 * to NULL prevents an entry from being created. class->dev_kobj must
918 * be set (or cleared) before any devices are registered to the class
919 * otherwise device_create_sys_dev_entry() and
920 * device_remove_sys_dev_entry() will disagree about the presence of
923 static struct kobject
*device_to_dev_kobj(struct device
*dev
)
925 struct kobject
*kobj
;
928 kobj
= dev
->class->dev_kobj
;
930 kobj
= sysfs_dev_char_kobj
;
935 static int device_create_sys_dev_entry(struct device
*dev
)
937 struct kobject
*kobj
= device_to_dev_kobj(dev
);
942 format_dev_t(devt_str
, dev
->devt
);
943 error
= sysfs_create_link(kobj
, &dev
->kobj
, devt_str
);
949 static void device_remove_sys_dev_entry(struct device
*dev
)
951 struct kobject
*kobj
= device_to_dev_kobj(dev
);
955 format_dev_t(devt_str
, dev
->devt
);
956 sysfs_remove_link(kobj
, devt_str
);
960 int device_private_init(struct device
*dev
)
962 dev
->p
= kzalloc(sizeof(*dev
->p
), GFP_KERNEL
);
965 dev
->p
->device
= dev
;
966 klist_init(&dev
->p
->klist_children
, klist_children_get
,
968 INIT_LIST_HEAD(&dev
->p
->deferred_probe
);
973 * device_add - add device to device hierarchy.
976 * This is part 2 of device_register(), though may be called
977 * separately _iff_ device_initialize() has been called separately.
979 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
980 * to the global and sibling lists for the device, then
981 * adds it to the other relevant subsystems of the driver model.
983 * Do not call this routine or device_register() more than once for
984 * any device structure. The driver model core is not designed to work
985 * with devices that get unregistered and then spring back to life.
986 * (Among other things, it's very hard to guarantee that all references
987 * to the previous incarnation of @dev have been dropped.) Allocate
988 * and register a fresh new struct device instead.
990 * NOTE: _Never_ directly free @dev after calling this function, even
991 * if it returned an error! Always use put_device() to give up your
994 int device_add(struct device
*dev
)
996 struct device
*parent
= NULL
;
997 struct kobject
*kobj
;
998 struct class_interface
*class_intf
;
1001 dev
= get_device(dev
);
1006 error
= device_private_init(dev
);
1012 * for statically allocated devices, which should all be converted
1013 * some day, we need to initialize the name. We prevent reading back
1014 * the name, and force the use of dev_name()
1016 if (dev
->init_name
) {
1017 dev_set_name(dev
, "%s", dev
->init_name
);
1018 dev
->init_name
= NULL
;
1021 /* subsystems can specify simple device enumeration */
1022 if (!dev_name(dev
) && dev
->bus
&& dev
->bus
->dev_name
)
1023 dev_set_name(dev
, "%s%u", dev
->bus
->dev_name
, dev
->id
);
1025 if (!dev_name(dev
)) {
1030 pr_debug("device: '%s': %s\n", dev_name(dev
), __func__
);
1032 parent
= get_device(dev
->parent
);
1033 kobj
= get_device_parent(dev
, parent
);
1035 dev
->kobj
.parent
= kobj
;
1037 /* use parent numa_node */
1039 set_dev_node(dev
, dev_to_node(parent
));
1041 /* first, register with generic layer. */
1042 /* we require the name to be set before, and pass NULL */
1043 error
= kobject_add(&dev
->kobj
, dev
->kobj
.parent
, NULL
);
1047 /* notify platform of device entry */
1048 if (platform_notify
)
1049 platform_notify(dev
);
1051 error
= device_create_file(dev
, &uevent_attr
);
1055 if (MAJOR(dev
->devt
)) {
1056 error
= device_create_file(dev
, &devt_attr
);
1058 goto ueventattrError
;
1060 error
= device_create_sys_dev_entry(dev
);
1064 devtmpfs_create_node(dev
);
1067 error
= device_add_class_symlinks(dev
);
1070 error
= device_add_attrs(dev
);
1073 error
= bus_add_device(dev
);
1076 error
= dpm_sysfs_add(dev
);
1081 /* Notify clients of device addition. This call must come
1082 * after dpm_sysfs_add() and before kobject_uevent().
1085 blocking_notifier_call_chain(&dev
->bus
->p
->bus_notifier
,
1086 BUS_NOTIFY_ADD_DEVICE
, dev
);
1088 kobject_uevent(&dev
->kobj
, KOBJ_ADD
);
1089 bus_probe_device(dev
);
1091 klist_add_tail(&dev
->p
->knode_parent
,
1092 &parent
->p
->klist_children
);
1095 mutex_lock(&dev
->class->p
->mutex
);
1096 /* tie the class to the device */
1097 klist_add_tail(&dev
->knode_class
,
1098 &dev
->class->p
->klist_devices
);
1100 /* notify any interfaces that the device is here */
1101 list_for_each_entry(class_intf
,
1102 &dev
->class->p
->interfaces
, node
)
1103 if (class_intf
->add_dev
)
1104 class_intf
->add_dev(dev
, class_intf
);
1105 mutex_unlock(&dev
->class->p
->mutex
);
1111 bus_remove_device(dev
);
1113 device_remove_attrs(dev
);
1115 device_remove_class_symlinks(dev
);
1117 if (MAJOR(dev
->devt
))
1118 devtmpfs_delete_node(dev
);
1119 if (MAJOR(dev
->devt
))
1120 device_remove_sys_dev_entry(dev
);
1122 if (MAJOR(dev
->devt
))
1123 device_remove_file(dev
, &devt_attr
);
1125 device_remove_file(dev
, &uevent_attr
);
1127 kobject_uevent(&dev
->kobj
, KOBJ_REMOVE
);
1128 kobject_del(&dev
->kobj
);
1130 cleanup_device_parent(dev
);
1140 * device_register - register a device with the system.
1141 * @dev: pointer to the device structure
1143 * This happens in two clean steps - initialize the device
1144 * and add it to the system. The two steps can be called
1145 * separately, but this is the easiest and most common.
1146 * I.e. you should only call the two helpers separately if
1147 * have a clearly defined need to use and refcount the device
1148 * before it is added to the hierarchy.
1150 * For more information, see the kerneldoc for device_initialize()
1153 * NOTE: _Never_ directly free @dev after calling this function, even
1154 * if it returned an error! Always use put_device() to give up the
1155 * reference initialized in this function instead.
1157 int device_register(struct device
*dev
)
1159 device_initialize(dev
);
1160 return device_add(dev
);
1164 * get_device - increment reference count for device.
1167 * This simply forwards the call to kobject_get(), though
1168 * we do take care to provide for the case that we get a NULL
1169 * pointer passed in.
1171 struct device
*get_device(struct device
*dev
)
1173 return dev
? kobj_to_dev(kobject_get(&dev
->kobj
)) : NULL
;
1177 * put_device - decrement reference count.
1178 * @dev: device in question.
1180 void put_device(struct device
*dev
)
1182 /* might_sleep(); */
1184 kobject_put(&dev
->kobj
);
1188 * device_del - delete device from system.
1191 * This is the first part of the device unregistration
1192 * sequence. This removes the device from the lists we control
1193 * from here, has it removed from the other driver model
1194 * subsystems it was added to in device_add(), and removes it
1195 * from the kobject hierarchy.
1197 * NOTE: this should be called manually _iff_ device_add() was
1198 * also called manually.
1200 void device_del(struct device
*dev
)
1202 struct device
*parent
= dev
->parent
;
1203 struct class_interface
*class_intf
;
1205 /* Notify clients of device removal. This call must come
1206 * before dpm_sysfs_remove().
1209 blocking_notifier_call_chain(&dev
->bus
->p
->bus_notifier
,
1210 BUS_NOTIFY_DEL_DEVICE
, dev
);
1211 dpm_sysfs_remove(dev
);
1213 klist_del(&dev
->p
->knode_parent
);
1214 if (MAJOR(dev
->devt
)) {
1215 devtmpfs_delete_node(dev
);
1216 device_remove_sys_dev_entry(dev
);
1217 device_remove_file(dev
, &devt_attr
);
1220 device_remove_class_symlinks(dev
);
1222 mutex_lock(&dev
->class->p
->mutex
);
1223 /* notify any interfaces that the device is now gone */
1224 list_for_each_entry(class_intf
,
1225 &dev
->class->p
->interfaces
, node
)
1226 if (class_intf
->remove_dev
)
1227 class_intf
->remove_dev(dev
, class_intf
);
1228 /* remove the device from the class list */
1229 klist_del(&dev
->knode_class
);
1230 mutex_unlock(&dev
->class->p
->mutex
);
1232 device_remove_file(dev
, &uevent_attr
);
1233 device_remove_attrs(dev
);
1234 bus_remove_device(dev
);
1235 device_pm_remove(dev
);
1236 driver_deferred_probe_del(dev
);
1238 /* Notify the platform of the removal, in case they
1239 * need to do anything...
1241 if (platform_notify_remove
)
1242 platform_notify_remove(dev
);
1243 kobject_uevent(&dev
->kobj
, KOBJ_REMOVE
);
1244 cleanup_device_parent(dev
);
1245 kobject_del(&dev
->kobj
);
1250 * device_unregister - unregister device from system.
1251 * @dev: device going away.
1253 * We do this in two parts, like we do device_register(). First,
1254 * we remove it from all the subsystems with device_del(), then
1255 * we decrement the reference count via put_device(). If that
1256 * is the final reference count, the device will be cleaned up
1257 * via device_release() above. Otherwise, the structure will
1258 * stick around until the final reference to the device is dropped.
1260 void device_unregister(struct device
*dev
)
1262 pr_debug("device: '%s': %s\n", dev_name(dev
), __func__
);
1267 static struct device
*next_device(struct klist_iter
*i
)
1269 struct klist_node
*n
= klist_next(i
);
1270 struct device
*dev
= NULL
;
1271 struct device_private
*p
;
1274 p
= to_device_private_parent(n
);
1281 * device_get_devnode - path of device node file
1283 * @mode: returned file access mode
1284 * @tmp: possibly allocated string
1286 * Return the relative path of a possible device node.
1287 * Non-default names may need to allocate a memory to compose
1288 * a name. This memory is returned in tmp and needs to be
1289 * freed by the caller.
1291 const char *device_get_devnode(struct device
*dev
,
1292 umode_t
*mode
, const char **tmp
)
1298 /* the device type may provide a specific name */
1299 if (dev
->type
&& dev
->type
->devnode
)
1300 *tmp
= dev
->type
->devnode(dev
, mode
);
1304 /* the class may provide a specific name */
1305 if (dev
->class && dev
->class->devnode
)
1306 *tmp
= dev
->class->devnode(dev
, mode
);
1310 /* return name without allocation, tmp == NULL */
1311 if (strchr(dev_name(dev
), '!') == NULL
)
1312 return dev_name(dev
);
1314 /* replace '!' in the name with '/' */
1315 *tmp
= kstrdup(dev_name(dev
), GFP_KERNEL
);
1318 while ((s
= strchr(*tmp
, '!')))
1324 * device_for_each_child - device child iterator.
1325 * @parent: parent struct device.
1326 * @data: data for the callback.
1327 * @fn: function to be called for each device.
1329 * Iterate over @parent's child devices, and call @fn for each,
1332 * We check the return of @fn each time. If it returns anything
1333 * other than 0, we break out and return that value.
1335 int device_for_each_child(struct device
*parent
, void *data
,
1336 int (*fn
)(struct device
*dev
, void *data
))
1338 struct klist_iter i
;
1339 struct device
*child
;
1345 klist_iter_init(&parent
->p
->klist_children
, &i
);
1346 while ((child
= next_device(&i
)) && !error
)
1347 error
= fn(child
, data
);
1348 klist_iter_exit(&i
);
1353 * device_find_child - device iterator for locating a particular device.
1354 * @parent: parent struct device
1355 * @data: Data to pass to match function
1356 * @match: Callback function to check device
1358 * This is similar to the device_for_each_child() function above, but it
1359 * returns a reference to a device that is 'found' for later use, as
1360 * determined by the @match callback.
1362 * The callback should return 0 if the device doesn't match and non-zero
1363 * if it does. If the callback returns non-zero and a reference to the
1364 * current device can be obtained, this function will return to the caller
1365 * and not iterate over any more devices.
1367 struct device
*device_find_child(struct device
*parent
, void *data
,
1368 int (*match
)(struct device
*dev
, void *data
))
1370 struct klist_iter i
;
1371 struct device
*child
;
1376 klist_iter_init(&parent
->p
->klist_children
, &i
);
1377 while ((child
= next_device(&i
)))
1378 if (match(child
, data
) && get_device(child
))
1380 klist_iter_exit(&i
);
1384 int __init
devices_init(void)
1386 devices_kset
= kset_create_and_add("devices", &device_uevent_ops
, NULL
);
1389 dev_kobj
= kobject_create_and_add("dev", NULL
);
1392 sysfs_dev_block_kobj
= kobject_create_and_add("block", dev_kobj
);
1393 if (!sysfs_dev_block_kobj
)
1394 goto block_kobj_err
;
1395 sysfs_dev_char_kobj
= kobject_create_and_add("char", dev_kobj
);
1396 if (!sysfs_dev_char_kobj
)
1402 kobject_put(sysfs_dev_block_kobj
);
1404 kobject_put(dev_kobj
);
1406 kset_unregister(devices_kset
);
1410 EXPORT_SYMBOL_GPL(device_for_each_child
);
1411 EXPORT_SYMBOL_GPL(device_find_child
);
1413 EXPORT_SYMBOL_GPL(device_initialize
);
1414 EXPORT_SYMBOL_GPL(device_add
);
1415 EXPORT_SYMBOL_GPL(device_register
);
1417 EXPORT_SYMBOL_GPL(device_del
);
1418 EXPORT_SYMBOL_GPL(device_unregister
);
1419 EXPORT_SYMBOL_GPL(get_device
);
1420 EXPORT_SYMBOL_GPL(put_device
);
1422 EXPORT_SYMBOL_GPL(device_create_file
);
1423 EXPORT_SYMBOL_GPL(device_remove_file
);
1425 struct root_device
{
1427 struct module
*owner
;
1430 static inline struct root_device
*to_root_device(struct device
*d
)
1432 return container_of(d
, struct root_device
, dev
);
1435 static void root_device_release(struct device
*dev
)
1437 kfree(to_root_device(dev
));
1441 * __root_device_register - allocate and register a root device
1442 * @name: root device name
1443 * @owner: owner module of the root device, usually THIS_MODULE
1445 * This function allocates a root device and registers it
1446 * using device_register(). In order to free the returned
1447 * device, use root_device_unregister().
1449 * Root devices are dummy devices which allow other devices
1450 * to be grouped under /sys/devices. Use this function to
1451 * allocate a root device and then use it as the parent of
1452 * any device which should appear under /sys/devices/{name}
1454 * The /sys/devices/{name} directory will also contain a
1455 * 'module' symlink which points to the @owner directory
1458 * Returns &struct device pointer on success, or ERR_PTR() on error.
1460 * Note: You probably want to use root_device_register().
1462 struct device
*__root_device_register(const char *name
, struct module
*owner
)
1464 struct root_device
*root
;
1467 root
= kzalloc(sizeof(struct root_device
), GFP_KERNEL
);
1469 return ERR_PTR(err
);
1471 err
= dev_set_name(&root
->dev
, "%s", name
);
1474 return ERR_PTR(err
);
1477 root
->dev
.release
= root_device_release
;
1479 err
= device_register(&root
->dev
);
1481 put_device(&root
->dev
);
1482 return ERR_PTR(err
);
1485 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
1487 struct module_kobject
*mk
= &owner
->mkobj
;
1489 err
= sysfs_create_link(&root
->dev
.kobj
, &mk
->kobj
, "module");
1491 device_unregister(&root
->dev
);
1492 return ERR_PTR(err
);
1494 root
->owner
= owner
;
1500 EXPORT_SYMBOL_GPL(__root_device_register
);
1503 * root_device_unregister - unregister and free a root device
1504 * @dev: device going away
1506 * This function unregisters and cleans up a device that was created by
1507 * root_device_register().
1509 void root_device_unregister(struct device
*dev
)
1511 struct root_device
*root
= to_root_device(dev
);
1514 sysfs_remove_link(&root
->dev
.kobj
, "module");
1516 device_unregister(dev
);
1518 EXPORT_SYMBOL_GPL(root_device_unregister
);
1521 static void device_create_release(struct device
*dev
)
1523 pr_debug("device: '%s': %s\n", dev_name(dev
), __func__
);
1528 * device_create_vargs - creates a device and registers it with sysfs
1529 * @class: pointer to the struct class that this device should be registered to
1530 * @parent: pointer to the parent struct device of this new device, if any
1531 * @devt: the dev_t for the char device to be added
1532 * @drvdata: the data to be added to the device for callbacks
1533 * @fmt: string for the device's name
1534 * @args: va_list for the device's name
1536 * This function can be used by char device classes. A struct device
1537 * will be created in sysfs, registered to the specified class.
1539 * A "dev" file will be created, showing the dev_t for the device, if
1540 * the dev_t is not 0,0.
1541 * If a pointer to a parent struct device is passed in, the newly created
1542 * struct device will be a child of that device in sysfs.
1543 * The pointer to the struct device will be returned from the call.
1544 * Any further sysfs files that might be required can be created using this
1547 * Returns &struct device pointer on success, or ERR_PTR() on error.
1549 * Note: the struct class passed to this function must have previously
1550 * been created with a call to class_create().
1552 struct device
*device_create_vargs(struct class *class, struct device
*parent
,
1553 dev_t devt
, void *drvdata
, const char *fmt
,
1556 struct device
*dev
= NULL
;
1557 int retval
= -ENODEV
;
1559 if (class == NULL
|| IS_ERR(class))
1562 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1570 dev
->parent
= parent
;
1571 dev
->release
= device_create_release
;
1572 dev_set_drvdata(dev
, drvdata
);
1574 retval
= kobject_set_name_vargs(&dev
->kobj
, fmt
, args
);
1578 retval
= device_register(dev
);
1586 return ERR_PTR(retval
);
1588 EXPORT_SYMBOL_GPL(device_create_vargs
);
1591 * device_create - creates a device and registers it with sysfs
1592 * @class: pointer to the struct class that this device should be registered to
1593 * @parent: pointer to the parent struct device of this new device, if any
1594 * @devt: the dev_t for the char device to be added
1595 * @drvdata: the data to be added to the device for callbacks
1596 * @fmt: string for the device's name
1598 * This function can be used by char device classes. A struct device
1599 * will be created in sysfs, registered to the specified class.
1601 * A "dev" file will be created, showing the dev_t for the device, if
1602 * the dev_t is not 0,0.
1603 * If a pointer to a parent struct device is passed in, the newly created
1604 * struct device will be a child of that device in sysfs.
1605 * The pointer to the struct device will be returned from the call.
1606 * Any further sysfs files that might be required can be created using this
1609 * Returns &struct device pointer on success, or ERR_PTR() on error.
1611 * Note: the struct class passed to this function must have previously
1612 * been created with a call to class_create().
1614 struct device
*device_create(struct class *class, struct device
*parent
,
1615 dev_t devt
, void *drvdata
, const char *fmt
, ...)
1620 va_start(vargs
, fmt
);
1621 dev
= device_create_vargs(class, parent
, devt
, drvdata
, fmt
, vargs
);
1625 EXPORT_SYMBOL_GPL(device_create
);
1627 static int __match_devt(struct device
*dev
, const void *data
)
1629 const dev_t
*devt
= data
;
1631 return dev
->devt
== *devt
;
1635 * device_destroy - removes a device that was created with device_create()
1636 * @class: pointer to the struct class that this device was registered with
1637 * @devt: the dev_t of the device that was previously registered
1639 * This call unregisters and cleans up a device that was created with a
1640 * call to device_create().
1642 void device_destroy(struct class *class, dev_t devt
)
1646 dev
= class_find_device(class, NULL
, &devt
, __match_devt
);
1649 device_unregister(dev
);
1652 EXPORT_SYMBOL_GPL(device_destroy
);
1655 * device_rename - renames a device
1656 * @dev: the pointer to the struct device to be renamed
1657 * @new_name: the new name of the device
1659 * It is the responsibility of the caller to provide mutual
1660 * exclusion between two different calls of device_rename
1661 * on the same device to ensure that new_name is valid and
1662 * won't conflict with other devices.
1664 * Note: Don't call this function. Currently, the networking layer calls this
1665 * function, but that will change. The following text from Kay Sievers offers
1668 * Renaming devices is racy at many levels, symlinks and other stuff are not
1669 * replaced atomically, and you get a "move" uevent, but it's not easy to
1670 * connect the event to the old and new device. Device nodes are not renamed at
1671 * all, there isn't even support for that in the kernel now.
1673 * In the meantime, during renaming, your target name might be taken by another
1674 * driver, creating conflicts. Or the old name is taken directly after you
1675 * renamed it -- then you get events for the same DEVPATH, before you even see
1676 * the "move" event. It's just a mess, and nothing new should ever rely on
1677 * kernel device renaming. Besides that, it's not even implemented now for
1678 * other things than (driver-core wise very simple) network devices.
1680 * We are currently about to change network renaming in udev to completely
1681 * disallow renaming of devices in the same namespace as the kernel uses,
1682 * because we can't solve the problems properly, that arise with swapping names
1683 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
1684 * be allowed to some other name than eth[0-9]*, for the aforementioned
1687 * Make up a "real" name in the driver before you register anything, or add
1688 * some other attributes for userspace to find the device, or use udev to add
1689 * symlinks -- but never rename kernel devices later, it's a complete mess. We
1690 * don't even want to get into that and try to implement the missing pieces in
1691 * the core. We really have other pieces to fix in the driver core mess. :)
1693 int device_rename(struct device
*dev
, const char *new_name
)
1695 char *old_device_name
= NULL
;
1698 dev
= get_device(dev
);
1702 pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev
),
1703 __func__
, new_name
);
1705 old_device_name
= kstrdup(dev_name(dev
), GFP_KERNEL
);
1706 if (!old_device_name
) {
1712 error
= sysfs_rename_link(&dev
->class->p
->subsys
.kobj
,
1713 &dev
->kobj
, old_device_name
, new_name
);
1718 error
= kobject_rename(&dev
->kobj
, new_name
);
1725 kfree(old_device_name
);
1729 EXPORT_SYMBOL_GPL(device_rename
);
1731 static int device_move_class_links(struct device
*dev
,
1732 struct device
*old_parent
,
1733 struct device
*new_parent
)
1738 sysfs_remove_link(&dev
->kobj
, "device");
1740 error
= sysfs_create_link(&dev
->kobj
, &new_parent
->kobj
,
1746 * device_move - moves a device to a new parent
1747 * @dev: the pointer to the struct device to be moved
1748 * @new_parent: the new parent of the device (can by NULL)
1749 * @dpm_order: how to reorder the dpm_list
1751 int device_move(struct device
*dev
, struct device
*new_parent
,
1752 enum dpm_order dpm_order
)
1755 struct device
*old_parent
;
1756 struct kobject
*new_parent_kobj
;
1758 dev
= get_device(dev
);
1763 new_parent
= get_device(new_parent
);
1764 new_parent_kobj
= get_device_parent(dev
, new_parent
);
1766 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev
),
1767 __func__
, new_parent
? dev_name(new_parent
) : "<NULL>");
1768 error
= kobject_move(&dev
->kobj
, new_parent_kobj
);
1770 cleanup_glue_dir(dev
, new_parent_kobj
);
1771 put_device(new_parent
);
1774 old_parent
= dev
->parent
;
1775 dev
->parent
= new_parent
;
1777 klist_remove(&dev
->p
->knode_parent
);
1779 klist_add_tail(&dev
->p
->knode_parent
,
1780 &new_parent
->p
->klist_children
);
1781 set_dev_node(dev
, dev_to_node(new_parent
));
1785 error
= device_move_class_links(dev
, old_parent
, new_parent
);
1787 /* We ignore errors on cleanup since we're hosed anyway... */
1788 device_move_class_links(dev
, new_parent
, old_parent
);
1789 if (!kobject_move(&dev
->kobj
, &old_parent
->kobj
)) {
1791 klist_remove(&dev
->p
->knode_parent
);
1792 dev
->parent
= old_parent
;
1794 klist_add_tail(&dev
->p
->knode_parent
,
1795 &old_parent
->p
->klist_children
);
1796 set_dev_node(dev
, dev_to_node(old_parent
));
1799 cleanup_glue_dir(dev
, new_parent_kobj
);
1800 put_device(new_parent
);
1804 switch (dpm_order
) {
1805 case DPM_ORDER_NONE
:
1807 case DPM_ORDER_DEV_AFTER_PARENT
:
1808 device_pm_move_after(dev
, new_parent
);
1810 case DPM_ORDER_PARENT_BEFORE_DEV
:
1811 device_pm_move_before(new_parent
, dev
);
1813 case DPM_ORDER_DEV_LAST
:
1814 device_pm_move_last(dev
);
1818 put_device(old_parent
);
1824 EXPORT_SYMBOL_GPL(device_move
);
1827 * device_shutdown - call ->shutdown() on each device to shutdown.
1829 void device_shutdown(void)
1833 spin_lock(&devices_kset
->list_lock
);
1835 * Walk the devices list backward, shutting down each in turn.
1836 * Beware that device unplug events may also start pulling
1837 * devices offline, even as the system is shutting down.
1839 while (!list_empty(&devices_kset
->list
)) {
1840 dev
= list_entry(devices_kset
->list
.prev
, struct device
,
1844 * hold reference count of device's parent to
1845 * prevent it from being freed because parent's
1846 * lock is to be held
1848 get_device(dev
->parent
);
1851 * Make sure the device is off the kset list, in the
1852 * event that dev->*->shutdown() doesn't remove it.
1854 list_del_init(&dev
->kobj
.entry
);
1855 spin_unlock(&devices_kset
->list_lock
);
1857 /* hold lock to avoid race with probe/release */
1859 device_lock(dev
->parent
);
1862 /* Don't allow any more runtime suspends */
1863 pm_runtime_get_noresume(dev
);
1864 pm_runtime_barrier(dev
);
1866 if (dev
->bus
&& dev
->bus
->shutdown
) {
1868 dev_info(dev
, "shutdown\n");
1869 dev
->bus
->shutdown(dev
);
1870 } else if (dev
->driver
&& dev
->driver
->shutdown
) {
1872 dev_info(dev
, "shutdown\n");
1873 dev
->driver
->shutdown(dev
);
1878 device_unlock(dev
->parent
);
1881 put_device(dev
->parent
);
1883 spin_lock(&devices_kset
->list_lock
);
1885 spin_unlock(&devices_kset
->list_lock
);
1886 async_synchronize_full();
1890 * Device logging functions
1893 #ifdef CONFIG_PRINTK
1895 create_syslog_header(const struct device
*dev
, char *hdr
, size_t hdrlen
)
1901 subsys
= dev
->class->name
;
1903 subsys
= dev
->bus
->name
;
1907 pos
+= snprintf(hdr
+ pos
, hdrlen
- pos
, "SUBSYSTEM=%s", subsys
);
1910 * Add device identifier DEVICE=:
1914 * +sound:card0 subsystem:devname
1916 if (MAJOR(dev
->devt
)) {
1919 if (strcmp(subsys
, "block") == 0)
1924 pos
+= snprintf(hdr
+ pos
, hdrlen
- pos
,
1926 c
, MAJOR(dev
->devt
), MINOR(dev
->devt
));
1927 } else if (strcmp(subsys
, "net") == 0) {
1928 struct net_device
*net
= to_net_dev(dev
);
1931 pos
+= snprintf(hdr
+ pos
, hdrlen
- pos
,
1932 "DEVICE=n%u", net
->ifindex
);
1935 pos
+= snprintf(hdr
+ pos
, hdrlen
- pos
,
1936 "DEVICE=+%s:%s", subsys
, dev_name(dev
));
1941 EXPORT_SYMBOL(create_syslog_header
);
1943 int dev_vprintk_emit(int level
, const struct device
*dev
,
1944 const char *fmt
, va_list args
)
1949 hdrlen
= create_syslog_header(dev
, hdr
, sizeof(hdr
));
1951 return vprintk_emit(0, level
, hdrlen
? hdr
: NULL
, hdrlen
, fmt
, args
);
1953 EXPORT_SYMBOL(dev_vprintk_emit
);
1955 int dev_printk_emit(int level
, const struct device
*dev
, const char *fmt
, ...)
1960 va_start(args
, fmt
);
1962 r
= dev_vprintk_emit(level
, dev
, fmt
, args
);
1968 EXPORT_SYMBOL(dev_printk_emit
);
1970 static int __dev_printk(const char *level
, const struct device
*dev
,
1971 struct va_format
*vaf
)
1974 return printk("%s(NULL device *): %pV", level
, vaf
);
1976 return dev_printk_emit(level
[1] - '0', dev
,
1978 dev_driver_string(dev
), dev_name(dev
), vaf
);
1981 int dev_printk(const char *level
, const struct device
*dev
,
1982 const char *fmt
, ...)
1984 struct va_format vaf
;
1988 va_start(args
, fmt
);
1993 r
= __dev_printk(level
, dev
, &vaf
);
1999 EXPORT_SYMBOL(dev_printk
);
2001 #define define_dev_printk_level(func, kern_level) \
2002 int func(const struct device *dev, const char *fmt, ...) \
2004 struct va_format vaf; \
2008 va_start(args, fmt); \
2013 r = __dev_printk(kern_level, dev, &vaf); \
2019 EXPORT_SYMBOL(func);
2021 define_dev_printk_level(dev_emerg
, KERN_EMERG
);
2022 define_dev_printk_level(dev_alert
, KERN_ALERT
);
2023 define_dev_printk_level(dev_crit
, KERN_CRIT
);
2024 define_dev_printk_level(dev_err
, KERN_ERR
);
2025 define_dev_printk_level(dev_warn
, KERN_WARNING
);
2026 define_dev_printk_level(dev_notice
, KERN_NOTICE
);
2027 define_dev_printk_level(_dev_info
, KERN_INFO
);