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
);
175 * device_release - free device structure.
176 * @kobj: device's kobject.
178 * This is called once the reference count for the object
179 * reaches 0. We forward the call to the device's release
180 * method, which should handle actually freeing the structure.
182 static void device_release(struct kobject
*kobj
)
184 struct device
*dev
= kobj_to_dev(kobj
);
185 struct device_private
*p
= dev
->p
;
188 * Some platform devices are driven without driver attached
189 * and managed resources may have been acquired. Make sure
190 * all resources are released.
192 * Drivers still can add resources into device after device
193 * is deleted but alive, so release devres here to avoid
194 * possible memory leak.
196 devres_release_all(dev
);
200 else if (dev
->type
&& dev
->type
->release
)
201 dev
->type
->release(dev
);
202 else if (dev
->class && dev
->class->dev_release
)
203 dev
->class->dev_release(dev
);
205 WARN(1, KERN_ERR
"Device '%s' does not have a release() "
206 "function, it is broken and must be fixed.\n",
211 static const void *device_namespace(struct kobject
*kobj
)
213 struct device
*dev
= kobj_to_dev(kobj
);
214 const void *ns
= NULL
;
216 if (dev
->class && dev
->class->ns_type
)
217 ns
= dev
->class->namespace(dev
);
222 static struct kobj_type device_ktype
= {
223 .release
= device_release
,
224 .sysfs_ops
= &dev_sysfs_ops
,
225 .namespace = device_namespace
,
229 static int dev_uevent_filter(struct kset
*kset
, struct kobject
*kobj
)
231 struct kobj_type
*ktype
= get_ktype(kobj
);
233 if (ktype
== &device_ktype
) {
234 struct device
*dev
= kobj_to_dev(kobj
);
243 static const char *dev_uevent_name(struct kset
*kset
, struct kobject
*kobj
)
245 struct device
*dev
= kobj_to_dev(kobj
);
248 return dev
->bus
->name
;
250 return dev
->class->name
;
254 static int dev_uevent(struct kset
*kset
, struct kobject
*kobj
,
255 struct kobj_uevent_env
*env
)
257 struct device
*dev
= kobj_to_dev(kobj
);
260 /* add device node properties if present */
261 if (MAJOR(dev
->devt
)) {
266 add_uevent_var(env
, "MAJOR=%u", MAJOR(dev
->devt
));
267 add_uevent_var(env
, "MINOR=%u", MINOR(dev
->devt
));
268 name
= device_get_devnode(dev
, &mode
, &tmp
);
270 add_uevent_var(env
, "DEVNAME=%s", name
);
273 add_uevent_var(env
, "DEVMODE=%#o", mode
& 0777);
277 if (dev
->type
&& dev
->type
->name
)
278 add_uevent_var(env
, "DEVTYPE=%s", dev
->type
->name
);
281 add_uevent_var(env
, "DRIVER=%s", dev
->driver
->name
);
283 /* Add common DT information about the device */
284 of_device_uevent(dev
, env
);
286 /* have the bus specific function add its stuff */
287 if (dev
->bus
&& dev
->bus
->uevent
) {
288 retval
= dev
->bus
->uevent(dev
, env
);
290 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
291 dev_name(dev
), __func__
, retval
);
294 /* have the class specific function add its stuff */
295 if (dev
->class && dev
->class->dev_uevent
) {
296 retval
= dev
->class->dev_uevent(dev
, env
);
298 pr_debug("device: '%s': %s: class uevent() "
299 "returned %d\n", dev_name(dev
),
303 /* have the device type specific function add its stuff */
304 if (dev
->type
&& dev
->type
->uevent
) {
305 retval
= dev
->type
->uevent(dev
, env
);
307 pr_debug("device: '%s': %s: dev_type uevent() "
308 "returned %d\n", dev_name(dev
),
315 static const struct kset_uevent_ops device_uevent_ops
= {
316 .filter
= dev_uevent_filter
,
317 .name
= dev_uevent_name
,
318 .uevent
= dev_uevent
,
321 static ssize_t
show_uevent(struct device
*dev
, struct device_attribute
*attr
,
324 struct kobject
*top_kobj
;
326 struct kobj_uevent_env
*env
= NULL
;
331 /* search the kset, the device belongs to */
332 top_kobj
= &dev
->kobj
;
333 while (!top_kobj
->kset
&& top_kobj
->parent
)
334 top_kobj
= top_kobj
->parent
;
338 kset
= top_kobj
->kset
;
339 if (!kset
->uevent_ops
|| !kset
->uevent_ops
->uevent
)
343 if (kset
->uevent_ops
&& kset
->uevent_ops
->filter
)
344 if (!kset
->uevent_ops
->filter(kset
, &dev
->kobj
))
347 env
= kzalloc(sizeof(struct kobj_uevent_env
), GFP_KERNEL
);
351 /* let the kset specific function add its keys */
352 retval
= kset
->uevent_ops
->uevent(kset
, &dev
->kobj
, env
);
356 /* copy keys to file */
357 for (i
= 0; i
< env
->envp_idx
; i
++)
358 count
+= sprintf(&buf
[count
], "%s\n", env
->envp
[i
]);
364 static ssize_t
store_uevent(struct device
*dev
, struct device_attribute
*attr
,
365 const char *buf
, size_t count
)
367 enum kobject_action action
;
369 if (kobject_action_type(buf
, count
, &action
) == 0)
370 kobject_uevent(&dev
->kobj
, action
);
372 dev_err(dev
, "uevent: unknown action-string\n");
376 static struct device_attribute uevent_attr
=
377 __ATTR(uevent
, S_IRUGO
| S_IWUSR
, show_uevent
, store_uevent
);
379 static int device_add_attributes(struct device
*dev
,
380 struct device_attribute
*attrs
)
386 for (i
= 0; attr_name(attrs
[i
]); i
++) {
387 error
= device_create_file(dev
, &attrs
[i
]);
393 device_remove_file(dev
, &attrs
[i
]);
398 static void device_remove_attributes(struct device
*dev
,
399 struct device_attribute
*attrs
)
404 for (i
= 0; attr_name(attrs
[i
]); i
++)
405 device_remove_file(dev
, &attrs
[i
]);
408 static int device_add_bin_attributes(struct device
*dev
,
409 struct bin_attribute
*attrs
)
415 for (i
= 0; attr_name(attrs
[i
]); i
++) {
416 error
= device_create_bin_file(dev
, &attrs
[i
]);
422 device_remove_bin_file(dev
, &attrs
[i
]);
427 static void device_remove_bin_attributes(struct device
*dev
,
428 struct bin_attribute
*attrs
)
433 for (i
= 0; attr_name(attrs
[i
]); i
++)
434 device_remove_bin_file(dev
, &attrs
[i
]);
437 static int device_add_groups(struct device
*dev
,
438 const struct attribute_group
**groups
)
444 for (i
= 0; groups
[i
]; i
++) {
445 error
= sysfs_create_group(&dev
->kobj
, groups
[i
]);
448 sysfs_remove_group(&dev
->kobj
,
457 static void device_remove_groups(struct device
*dev
,
458 const struct attribute_group
**groups
)
463 for (i
= 0; groups
[i
]; i
++)
464 sysfs_remove_group(&dev
->kobj
, groups
[i
]);
467 static int device_add_attrs(struct device
*dev
)
469 struct class *class = dev
->class;
470 const struct device_type
*type
= dev
->type
;
474 error
= device_add_attributes(dev
, class->dev_attrs
);
477 error
= device_add_bin_attributes(dev
, class->dev_bin_attrs
);
479 goto err_remove_class_attrs
;
483 error
= device_add_groups(dev
, type
->groups
);
485 goto err_remove_class_bin_attrs
;
488 error
= device_add_groups(dev
, dev
->groups
);
490 goto err_remove_type_groups
;
494 err_remove_type_groups
:
496 device_remove_groups(dev
, type
->groups
);
497 err_remove_class_bin_attrs
:
499 device_remove_bin_attributes(dev
, class->dev_bin_attrs
);
500 err_remove_class_attrs
:
502 device_remove_attributes(dev
, class->dev_attrs
);
507 static void device_remove_attrs(struct device
*dev
)
509 struct class *class = dev
->class;
510 const struct device_type
*type
= dev
->type
;
512 device_remove_groups(dev
, dev
->groups
);
515 device_remove_groups(dev
, type
->groups
);
518 device_remove_attributes(dev
, class->dev_attrs
);
519 device_remove_bin_attributes(dev
, class->dev_bin_attrs
);
524 static ssize_t
show_dev(struct device
*dev
, struct device_attribute
*attr
,
527 return print_dev_t(buf
, dev
->devt
);
530 static struct device_attribute devt_attr
=
531 __ATTR(dev
, S_IRUGO
, show_dev
, NULL
);
534 struct kset
*devices_kset
;
537 * device_create_file - create sysfs attribute file for device.
539 * @attr: device attribute descriptor.
541 int device_create_file(struct device
*dev
,
542 const struct device_attribute
*attr
)
546 error
= sysfs_create_file(&dev
->kobj
, &attr
->attr
);
551 * device_remove_file - remove sysfs attribute file.
553 * @attr: device attribute descriptor.
555 void device_remove_file(struct device
*dev
,
556 const struct device_attribute
*attr
)
559 sysfs_remove_file(&dev
->kobj
, &attr
->attr
);
563 * device_create_bin_file - create sysfs binary attribute file for device.
565 * @attr: device binary attribute descriptor.
567 int device_create_bin_file(struct device
*dev
,
568 const struct bin_attribute
*attr
)
572 error
= sysfs_create_bin_file(&dev
->kobj
, attr
);
575 EXPORT_SYMBOL_GPL(device_create_bin_file
);
578 * device_remove_bin_file - remove sysfs binary attribute file
580 * @attr: device binary attribute descriptor.
582 void device_remove_bin_file(struct device
*dev
,
583 const struct bin_attribute
*attr
)
586 sysfs_remove_bin_file(&dev
->kobj
, attr
);
588 EXPORT_SYMBOL_GPL(device_remove_bin_file
);
591 * device_schedule_callback_owner - helper to schedule a callback for a device
593 * @func: callback function to invoke later.
594 * @owner: module owning the callback routine
596 * Attribute methods must not unregister themselves or their parent device
597 * (which would amount to the same thing). Attempts to do so will deadlock,
598 * since unregistration is mutually exclusive with driver callbacks.
600 * Instead methods can call this routine, which will attempt to allocate
601 * and schedule a workqueue request to call back @func with @dev as its
602 * argument in the workqueue's process context. @dev will be pinned until
605 * This routine is usually called via the inline device_schedule_callback(),
606 * which automatically sets @owner to THIS_MODULE.
608 * Returns 0 if the request was submitted, -ENOMEM if storage could not
609 * be allocated, -ENODEV if a reference to @owner isn't available.
611 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
612 * underlying sysfs routine (since it is intended for use by attribute
613 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
615 int device_schedule_callback_owner(struct device
*dev
,
616 void (*func
)(struct device
*), struct module
*owner
)
618 return sysfs_schedule_callback(&dev
->kobj
,
619 (void (*)(void *)) func
, dev
, owner
);
621 EXPORT_SYMBOL_GPL(device_schedule_callback_owner
);
623 static void klist_children_get(struct klist_node
*n
)
625 struct device_private
*p
= to_device_private_parent(n
);
626 struct device
*dev
= p
->device
;
631 static void klist_children_put(struct klist_node
*n
)
633 struct device_private
*p
= to_device_private_parent(n
);
634 struct device
*dev
= p
->device
;
640 * device_initialize - init device structure.
643 * This prepares the device for use by other layers by initializing
645 * It is the first half of device_register(), if called by
646 * that function, though it can also be called separately, so one
647 * may use @dev's fields. In particular, get_device()/put_device()
648 * may be used for reference counting of @dev after calling this
651 * All fields in @dev must be initialized by the caller to 0, except
652 * for those explicitly set to some other value. The simplest
653 * approach is to use kzalloc() to allocate the structure containing
656 * NOTE: Use put_device() to give up your reference instead of freeing
657 * @dev directly once you have called this function.
659 void device_initialize(struct device
*dev
)
661 dev
->kobj
.kset
= devices_kset
;
662 kobject_init(&dev
->kobj
, &device_ktype
);
663 INIT_LIST_HEAD(&dev
->dma_pools
);
664 mutex_init(&dev
->mutex
);
665 lockdep_set_novalidate_class(&dev
->mutex
);
666 spin_lock_init(&dev
->devres_lock
);
667 INIT_LIST_HEAD(&dev
->devres_head
);
669 set_dev_node(dev
, -1);
672 static struct kobject
*virtual_device_parent(struct device
*dev
)
674 static struct kobject
*virtual_dir
= NULL
;
677 virtual_dir
= kobject_create_and_add("virtual",
678 &devices_kset
->kobj
);
688 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
690 static void class_dir_release(struct kobject
*kobj
)
692 struct class_dir
*dir
= to_class_dir(kobj
);
697 struct kobj_ns_type_operations
*class_dir_child_ns_type(struct kobject
*kobj
)
699 struct class_dir
*dir
= to_class_dir(kobj
);
700 return dir
->class->ns_type
;
703 static struct kobj_type class_dir_ktype
= {
704 .release
= class_dir_release
,
705 .sysfs_ops
= &kobj_sysfs_ops
,
706 .child_ns_type
= class_dir_child_ns_type
709 static struct kobject
*
710 class_dir_create_and_add(struct class *class, struct kobject
*parent_kobj
)
712 struct class_dir
*dir
;
715 dir
= kzalloc(sizeof(*dir
), GFP_KERNEL
);
720 kobject_init(&dir
->kobj
, &class_dir_ktype
);
722 dir
->kobj
.kset
= &class->p
->glue_dirs
;
724 retval
= kobject_add(&dir
->kobj
, parent_kobj
, "%s", class->name
);
726 kobject_put(&dir
->kobj
);
733 static struct kobject
*get_device_parent(struct device
*dev
,
734 struct device
*parent
)
737 static DEFINE_MUTEX(gdp_mutex
);
738 struct kobject
*kobj
= NULL
;
739 struct kobject
*parent_kobj
;
743 /* block disks show up in /sys/block */
744 if (sysfs_deprecated
&& dev
->class == &block_class
) {
745 if (parent
&& parent
->class == &block_class
)
746 return &parent
->kobj
;
747 return &block_class
.p
->subsys
.kobj
;
752 * If we have no parent, we live in "virtual".
753 * Class-devices with a non class-device as parent, live
754 * in a "glue" directory to prevent namespace collisions.
757 parent_kobj
= virtual_device_parent(dev
);
758 else if (parent
->class && !dev
->class->ns_type
)
759 return &parent
->kobj
;
761 parent_kobj
= &parent
->kobj
;
763 mutex_lock(&gdp_mutex
);
765 /* find our class-directory at the parent and reference it */
766 spin_lock(&dev
->class->p
->glue_dirs
.list_lock
);
767 list_for_each_entry(k
, &dev
->class->p
->glue_dirs
.list
, entry
)
768 if (k
->parent
== parent_kobj
) {
769 kobj
= kobject_get(k
);
772 spin_unlock(&dev
->class->p
->glue_dirs
.list_lock
);
774 mutex_unlock(&gdp_mutex
);
778 /* or create a new class-directory at the parent device */
779 k
= class_dir_create_and_add(dev
->class, parent_kobj
);
780 /* do not emit an uevent for this simple "glue" directory */
781 mutex_unlock(&gdp_mutex
);
785 /* subsystems can specify a default root directory for their devices */
786 if (!parent
&& dev
->bus
&& dev
->bus
->dev_root
)
787 return &dev
->bus
->dev_root
->kobj
;
790 return &parent
->kobj
;
794 static void cleanup_glue_dir(struct device
*dev
, struct kobject
*glue_dir
)
796 /* see if we live in a "glue" directory */
797 if (!glue_dir
|| !dev
->class ||
798 glue_dir
->kset
!= &dev
->class->p
->glue_dirs
)
801 kobject_put(glue_dir
);
804 static void cleanup_device_parent(struct device
*dev
)
806 cleanup_glue_dir(dev
, dev
->kobj
.parent
);
809 static int device_add_class_symlinks(struct device
*dev
)
816 error
= sysfs_create_link(&dev
->kobj
,
817 &dev
->class->p
->subsys
.kobj
,
822 if (dev
->parent
&& device_is_not_partition(dev
)) {
823 error
= sysfs_create_link(&dev
->kobj
, &dev
->parent
->kobj
,
830 /* /sys/block has directories and does not need symlinks */
831 if (sysfs_deprecated
&& dev
->class == &block_class
)
835 /* link in the class directory pointing to the device */
836 error
= sysfs_create_link(&dev
->class->p
->subsys
.kobj
,
837 &dev
->kobj
, dev_name(dev
));
844 sysfs_remove_link(&dev
->kobj
, "device");
847 sysfs_remove_link(&dev
->kobj
, "subsystem");
852 static void device_remove_class_symlinks(struct device
*dev
)
857 if (dev
->parent
&& device_is_not_partition(dev
))
858 sysfs_remove_link(&dev
->kobj
, "device");
859 sysfs_remove_link(&dev
->kobj
, "subsystem");
861 if (sysfs_deprecated
&& dev
->class == &block_class
)
864 sysfs_delete_link(&dev
->class->p
->subsys
.kobj
, &dev
->kobj
, dev_name(dev
));
868 * dev_set_name - set a device name
870 * @fmt: format string for the device's name
872 int dev_set_name(struct device
*dev
, const char *fmt
, ...)
877 va_start(vargs
, fmt
);
878 err
= kobject_set_name_vargs(&dev
->kobj
, fmt
, vargs
);
882 EXPORT_SYMBOL_GPL(dev_set_name
);
885 * device_to_dev_kobj - select a /sys/dev/ directory for the device
888 * By default we select char/ for new entries. Setting class->dev_obj
889 * to NULL prevents an entry from being created. class->dev_kobj must
890 * be set (or cleared) before any devices are registered to the class
891 * otherwise device_create_sys_dev_entry() and
892 * device_remove_sys_dev_entry() will disagree about the presence of
895 static struct kobject
*device_to_dev_kobj(struct device
*dev
)
897 struct kobject
*kobj
;
900 kobj
= dev
->class->dev_kobj
;
902 kobj
= sysfs_dev_char_kobj
;
907 static int device_create_sys_dev_entry(struct device
*dev
)
909 struct kobject
*kobj
= device_to_dev_kobj(dev
);
914 format_dev_t(devt_str
, dev
->devt
);
915 error
= sysfs_create_link(kobj
, &dev
->kobj
, devt_str
);
921 static void device_remove_sys_dev_entry(struct device
*dev
)
923 struct kobject
*kobj
= device_to_dev_kobj(dev
);
927 format_dev_t(devt_str
, dev
->devt
);
928 sysfs_remove_link(kobj
, devt_str
);
932 int device_private_init(struct device
*dev
)
934 dev
->p
= kzalloc(sizeof(*dev
->p
), GFP_KERNEL
);
937 dev
->p
->device
= dev
;
938 klist_init(&dev
->p
->klist_children
, klist_children_get
,
940 INIT_LIST_HEAD(&dev
->p
->deferred_probe
);
945 * device_add - add device to device hierarchy.
948 * This is part 2 of device_register(), though may be called
949 * separately _iff_ device_initialize() has been called separately.
951 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
952 * to the global and sibling lists for the device, then
953 * adds it to the other relevant subsystems of the driver model.
955 * Do not call this routine or device_register() more than once for
956 * any device structure. The driver model core is not designed to work
957 * with devices that get unregistered and then spring back to life.
958 * (Among other things, it's very hard to guarantee that all references
959 * to the previous incarnation of @dev have been dropped.) Allocate
960 * and register a fresh new struct device instead.
962 * NOTE: _Never_ directly free @dev after calling this function, even
963 * if it returned an error! Always use put_device() to give up your
966 int device_add(struct device
*dev
)
968 struct device
*parent
= NULL
;
969 struct kobject
*kobj
;
970 struct class_interface
*class_intf
;
973 dev
= get_device(dev
);
978 error
= device_private_init(dev
);
984 * for statically allocated devices, which should all be converted
985 * some day, we need to initialize the name. We prevent reading back
986 * the name, and force the use of dev_name()
988 if (dev
->init_name
) {
989 dev_set_name(dev
, "%s", dev
->init_name
);
990 dev
->init_name
= NULL
;
993 /* subsystems can specify simple device enumeration */
994 if (!dev_name(dev
) && dev
->bus
&& dev
->bus
->dev_name
)
995 dev_set_name(dev
, "%s%u", dev
->bus
->dev_name
, dev
->id
);
997 if (!dev_name(dev
)) {
1002 pr_debug("device: '%s': %s\n", dev_name(dev
), __func__
);
1004 parent
= get_device(dev
->parent
);
1005 kobj
= get_device_parent(dev
, parent
);
1007 dev
->kobj
.parent
= kobj
;
1009 /* use parent numa_node */
1011 set_dev_node(dev
, dev_to_node(parent
));
1013 /* first, register with generic layer. */
1014 /* we require the name to be set before, and pass NULL */
1015 error
= kobject_add(&dev
->kobj
, dev
->kobj
.parent
, NULL
);
1019 /* notify platform of device entry */
1020 if (platform_notify
)
1021 platform_notify(dev
);
1023 error
= device_create_file(dev
, &uevent_attr
);
1027 if (MAJOR(dev
->devt
)) {
1028 error
= device_create_file(dev
, &devt_attr
);
1030 goto ueventattrError
;
1032 error
= device_create_sys_dev_entry(dev
);
1036 devtmpfs_create_node(dev
);
1039 error
= device_add_class_symlinks(dev
);
1042 error
= device_add_attrs(dev
);
1045 error
= bus_add_device(dev
);
1048 error
= dpm_sysfs_add(dev
);
1053 /* Notify clients of device addition. This call must come
1054 * after dpm_sysfs_add() and before kobject_uevent().
1057 blocking_notifier_call_chain(&dev
->bus
->p
->bus_notifier
,
1058 BUS_NOTIFY_ADD_DEVICE
, dev
);
1060 kobject_uevent(&dev
->kobj
, KOBJ_ADD
);
1061 bus_probe_device(dev
);
1063 klist_add_tail(&dev
->p
->knode_parent
,
1064 &parent
->p
->klist_children
);
1067 mutex_lock(&dev
->class->p
->mutex
);
1068 /* tie the class to the device */
1069 klist_add_tail(&dev
->knode_class
,
1070 &dev
->class->p
->klist_devices
);
1072 /* notify any interfaces that the device is here */
1073 list_for_each_entry(class_intf
,
1074 &dev
->class->p
->interfaces
, node
)
1075 if (class_intf
->add_dev
)
1076 class_intf
->add_dev(dev
, class_intf
);
1077 mutex_unlock(&dev
->class->p
->mutex
);
1083 bus_remove_device(dev
);
1085 device_remove_attrs(dev
);
1087 device_remove_class_symlinks(dev
);
1089 if (MAJOR(dev
->devt
))
1090 devtmpfs_delete_node(dev
);
1091 if (MAJOR(dev
->devt
))
1092 device_remove_sys_dev_entry(dev
);
1094 if (MAJOR(dev
->devt
))
1095 device_remove_file(dev
, &devt_attr
);
1097 device_remove_file(dev
, &uevent_attr
);
1099 kobject_uevent(&dev
->kobj
, KOBJ_REMOVE
);
1100 kobject_del(&dev
->kobj
);
1102 cleanup_device_parent(dev
);
1112 * device_register - register a device with the system.
1113 * @dev: pointer to the device structure
1115 * This happens in two clean steps - initialize the device
1116 * and add it to the system. The two steps can be called
1117 * separately, but this is the easiest and most common.
1118 * I.e. you should only call the two helpers separately if
1119 * have a clearly defined need to use and refcount the device
1120 * before it is added to the hierarchy.
1122 * For more information, see the kerneldoc for device_initialize()
1125 * NOTE: _Never_ directly free @dev after calling this function, even
1126 * if it returned an error! Always use put_device() to give up the
1127 * reference initialized in this function instead.
1129 int device_register(struct device
*dev
)
1131 device_initialize(dev
);
1132 return device_add(dev
);
1136 * get_device - increment reference count for device.
1139 * This simply forwards the call to kobject_get(), though
1140 * we do take care to provide for the case that we get a NULL
1141 * pointer passed in.
1143 struct device
*get_device(struct device
*dev
)
1145 return dev
? kobj_to_dev(kobject_get(&dev
->kobj
)) : NULL
;
1149 * put_device - decrement reference count.
1150 * @dev: device in question.
1152 void put_device(struct device
*dev
)
1154 /* might_sleep(); */
1156 kobject_put(&dev
->kobj
);
1160 * device_del - delete device from system.
1163 * This is the first part of the device unregistration
1164 * sequence. This removes the device from the lists we control
1165 * from here, has it removed from the other driver model
1166 * subsystems it was added to in device_add(), and removes it
1167 * from the kobject hierarchy.
1169 * NOTE: this should be called manually _iff_ device_add() was
1170 * also called manually.
1172 void device_del(struct device
*dev
)
1174 struct device
*parent
= dev
->parent
;
1175 struct class_interface
*class_intf
;
1177 /* Notify clients of device removal. This call must come
1178 * before dpm_sysfs_remove().
1181 blocking_notifier_call_chain(&dev
->bus
->p
->bus_notifier
,
1182 BUS_NOTIFY_DEL_DEVICE
, dev
);
1183 device_pm_remove(dev
);
1184 dpm_sysfs_remove(dev
);
1186 klist_del(&dev
->p
->knode_parent
);
1187 if (MAJOR(dev
->devt
)) {
1188 devtmpfs_delete_node(dev
);
1189 device_remove_sys_dev_entry(dev
);
1190 device_remove_file(dev
, &devt_attr
);
1193 device_remove_class_symlinks(dev
);
1195 mutex_lock(&dev
->class->p
->mutex
);
1196 /* notify any interfaces that the device is now gone */
1197 list_for_each_entry(class_intf
,
1198 &dev
->class->p
->interfaces
, node
)
1199 if (class_intf
->remove_dev
)
1200 class_intf
->remove_dev(dev
, class_intf
);
1201 /* remove the device from the class list */
1202 klist_del(&dev
->knode_class
);
1203 mutex_unlock(&dev
->class->p
->mutex
);
1205 device_remove_file(dev
, &uevent_attr
);
1206 device_remove_attrs(dev
);
1207 bus_remove_device(dev
);
1208 driver_deferred_probe_del(dev
);
1210 /* Notify the platform of the removal, in case they
1211 * need to do anything...
1213 if (platform_notify_remove
)
1214 platform_notify_remove(dev
);
1215 kobject_uevent(&dev
->kobj
, KOBJ_REMOVE
);
1216 cleanup_device_parent(dev
);
1217 kobject_del(&dev
->kobj
);
1222 * device_unregister - unregister device from system.
1223 * @dev: device going away.
1225 * We do this in two parts, like we do device_register(). First,
1226 * we remove it from all the subsystems with device_del(), then
1227 * we decrement the reference count via put_device(). If that
1228 * is the final reference count, the device will be cleaned up
1229 * via device_release() above. Otherwise, the structure will
1230 * stick around until the final reference to the device is dropped.
1232 void device_unregister(struct device
*dev
)
1234 pr_debug("device: '%s': %s\n", dev_name(dev
), __func__
);
1239 static struct device
*next_device(struct klist_iter
*i
)
1241 struct klist_node
*n
= klist_next(i
);
1242 struct device
*dev
= NULL
;
1243 struct device_private
*p
;
1246 p
= to_device_private_parent(n
);
1253 * device_get_devnode - path of device node file
1255 * @mode: returned file access mode
1256 * @tmp: possibly allocated string
1258 * Return the relative path of a possible device node.
1259 * Non-default names may need to allocate a memory to compose
1260 * a name. This memory is returned in tmp and needs to be
1261 * freed by the caller.
1263 const char *device_get_devnode(struct device
*dev
,
1264 umode_t
*mode
, const char **tmp
)
1270 /* the device type may provide a specific name */
1271 if (dev
->type
&& dev
->type
->devnode
)
1272 *tmp
= dev
->type
->devnode(dev
, mode
);
1276 /* the class may provide a specific name */
1277 if (dev
->class && dev
->class->devnode
)
1278 *tmp
= dev
->class->devnode(dev
, mode
);
1282 /* return name without allocation, tmp == NULL */
1283 if (strchr(dev_name(dev
), '!') == NULL
)
1284 return dev_name(dev
);
1286 /* replace '!' in the name with '/' */
1287 *tmp
= kstrdup(dev_name(dev
), GFP_KERNEL
);
1290 while ((s
= strchr(*tmp
, '!')))
1296 * device_for_each_child - device child iterator.
1297 * @parent: parent struct device.
1298 * @data: data for the callback.
1299 * @fn: function to be called for each device.
1301 * Iterate over @parent's child devices, and call @fn for each,
1304 * We check the return of @fn each time. If it returns anything
1305 * other than 0, we break out and return that value.
1307 int device_for_each_child(struct device
*parent
, void *data
,
1308 int (*fn
)(struct device
*dev
, void *data
))
1310 struct klist_iter i
;
1311 struct device
*child
;
1317 klist_iter_init(&parent
->p
->klist_children
, &i
);
1318 while ((child
= next_device(&i
)) && !error
)
1319 error
= fn(child
, data
);
1320 klist_iter_exit(&i
);
1325 * device_find_child - device iterator for locating a particular device.
1326 * @parent: parent struct device
1327 * @data: Data to pass to match function
1328 * @match: Callback function to check device
1330 * This is similar to the device_for_each_child() function above, but it
1331 * returns a reference to a device that is 'found' for later use, as
1332 * determined by the @match callback.
1334 * The callback should return 0 if the device doesn't match and non-zero
1335 * if it does. If the callback returns non-zero and a reference to the
1336 * current device can be obtained, this function will return to the caller
1337 * and not iterate over any more devices.
1339 struct device
*device_find_child(struct device
*parent
, void *data
,
1340 int (*match
)(struct device
*dev
, void *data
))
1342 struct klist_iter i
;
1343 struct device
*child
;
1348 klist_iter_init(&parent
->p
->klist_children
, &i
);
1349 while ((child
= next_device(&i
)))
1350 if (match(child
, data
) && get_device(child
))
1352 klist_iter_exit(&i
);
1356 int __init
devices_init(void)
1358 devices_kset
= kset_create_and_add("devices", &device_uevent_ops
, NULL
);
1361 dev_kobj
= kobject_create_and_add("dev", NULL
);
1364 sysfs_dev_block_kobj
= kobject_create_and_add("block", dev_kobj
);
1365 if (!sysfs_dev_block_kobj
)
1366 goto block_kobj_err
;
1367 sysfs_dev_char_kobj
= kobject_create_and_add("char", dev_kobj
);
1368 if (!sysfs_dev_char_kobj
)
1374 kobject_put(sysfs_dev_block_kobj
);
1376 kobject_put(dev_kobj
);
1378 kset_unregister(devices_kset
);
1382 EXPORT_SYMBOL_GPL(device_for_each_child
);
1383 EXPORT_SYMBOL_GPL(device_find_child
);
1385 EXPORT_SYMBOL_GPL(device_initialize
);
1386 EXPORT_SYMBOL_GPL(device_add
);
1387 EXPORT_SYMBOL_GPL(device_register
);
1389 EXPORT_SYMBOL_GPL(device_del
);
1390 EXPORT_SYMBOL_GPL(device_unregister
);
1391 EXPORT_SYMBOL_GPL(get_device
);
1392 EXPORT_SYMBOL_GPL(put_device
);
1394 EXPORT_SYMBOL_GPL(device_create_file
);
1395 EXPORT_SYMBOL_GPL(device_remove_file
);
1397 struct root_device
{
1399 struct module
*owner
;
1402 inline struct root_device
*to_root_device(struct device
*d
)
1404 return container_of(d
, struct root_device
, dev
);
1407 static void root_device_release(struct device
*dev
)
1409 kfree(to_root_device(dev
));
1413 * __root_device_register - allocate and register a root device
1414 * @name: root device name
1415 * @owner: owner module of the root device, usually THIS_MODULE
1417 * This function allocates a root device and registers it
1418 * using device_register(). In order to free the returned
1419 * device, use root_device_unregister().
1421 * Root devices are dummy devices which allow other devices
1422 * to be grouped under /sys/devices. Use this function to
1423 * allocate a root device and then use it as the parent of
1424 * any device which should appear under /sys/devices/{name}
1426 * The /sys/devices/{name} directory will also contain a
1427 * 'module' symlink which points to the @owner directory
1430 * Returns &struct device pointer on success, or ERR_PTR() on error.
1432 * Note: You probably want to use root_device_register().
1434 struct device
*__root_device_register(const char *name
, struct module
*owner
)
1436 struct root_device
*root
;
1439 root
= kzalloc(sizeof(struct root_device
), GFP_KERNEL
);
1441 return ERR_PTR(err
);
1443 err
= dev_set_name(&root
->dev
, "%s", name
);
1446 return ERR_PTR(err
);
1449 root
->dev
.release
= root_device_release
;
1451 err
= device_register(&root
->dev
);
1453 put_device(&root
->dev
);
1454 return ERR_PTR(err
);
1457 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
1459 struct module_kobject
*mk
= &owner
->mkobj
;
1461 err
= sysfs_create_link(&root
->dev
.kobj
, &mk
->kobj
, "module");
1463 device_unregister(&root
->dev
);
1464 return ERR_PTR(err
);
1466 root
->owner
= owner
;
1472 EXPORT_SYMBOL_GPL(__root_device_register
);
1475 * root_device_unregister - unregister and free a root device
1476 * @dev: device going away
1478 * This function unregisters and cleans up a device that was created by
1479 * root_device_register().
1481 void root_device_unregister(struct device
*dev
)
1483 struct root_device
*root
= to_root_device(dev
);
1486 sysfs_remove_link(&root
->dev
.kobj
, "module");
1488 device_unregister(dev
);
1490 EXPORT_SYMBOL_GPL(root_device_unregister
);
1493 static void device_create_release(struct device
*dev
)
1495 pr_debug("device: '%s': %s\n", dev_name(dev
), __func__
);
1500 * device_create_vargs - creates a device and registers it with sysfs
1501 * @class: pointer to the struct class that this device should be registered to
1502 * @parent: pointer to the parent struct device of this new device, if any
1503 * @devt: the dev_t for the char device to be added
1504 * @drvdata: the data to be added to the device for callbacks
1505 * @fmt: string for the device's name
1506 * @args: va_list for the device's name
1508 * This function can be used by char device classes. A struct device
1509 * will be created in sysfs, registered to the specified class.
1511 * A "dev" file will be created, showing the dev_t for the device, if
1512 * the dev_t is not 0,0.
1513 * If a pointer to a parent struct device is passed in, the newly created
1514 * struct device will be a child of that device in sysfs.
1515 * The pointer to the struct device will be returned from the call.
1516 * Any further sysfs files that might be required can be created using this
1519 * Returns &struct device pointer on success, or ERR_PTR() on error.
1521 * Note: the struct class passed to this function must have previously
1522 * been created with a call to class_create().
1524 struct device
*device_create_vargs(struct class *class, struct device
*parent
,
1525 dev_t devt
, void *drvdata
, const char *fmt
,
1528 struct device
*dev
= NULL
;
1529 int retval
= -ENODEV
;
1531 if (class == NULL
|| IS_ERR(class))
1534 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1542 dev
->parent
= parent
;
1543 dev
->release
= device_create_release
;
1544 dev_set_drvdata(dev
, drvdata
);
1546 retval
= kobject_set_name_vargs(&dev
->kobj
, fmt
, args
);
1550 retval
= device_register(dev
);
1558 return ERR_PTR(retval
);
1560 EXPORT_SYMBOL_GPL(device_create_vargs
);
1563 * device_create - creates a device and registers it with sysfs
1564 * @class: pointer to the struct class that this device should be registered to
1565 * @parent: pointer to the parent struct device of this new device, if any
1566 * @devt: the dev_t for the char device to be added
1567 * @drvdata: the data to be added to the device for callbacks
1568 * @fmt: string for the device's name
1570 * This function can be used by char device classes. A struct device
1571 * will be created in sysfs, registered to the specified class.
1573 * A "dev" file will be created, showing the dev_t for the device, if
1574 * the dev_t is not 0,0.
1575 * If a pointer to a parent struct device is passed in, the newly created
1576 * struct device will be a child of that device in sysfs.
1577 * The pointer to the struct device will be returned from the call.
1578 * Any further sysfs files that might be required can be created using this
1581 * Returns &struct device pointer on success, or ERR_PTR() on error.
1583 * Note: the struct class passed to this function must have previously
1584 * been created with a call to class_create().
1586 struct device
*device_create(struct class *class, struct device
*parent
,
1587 dev_t devt
, void *drvdata
, const char *fmt
, ...)
1592 va_start(vargs
, fmt
);
1593 dev
= device_create_vargs(class, parent
, devt
, drvdata
, fmt
, vargs
);
1597 EXPORT_SYMBOL_GPL(device_create
);
1599 static int __match_devt(struct device
*dev
, void *data
)
1603 return dev
->devt
== *devt
;
1607 * device_destroy - removes a device that was created with device_create()
1608 * @class: pointer to the struct class that this device was registered with
1609 * @devt: the dev_t of the device that was previously registered
1611 * This call unregisters and cleans up a device that was created with a
1612 * call to device_create().
1614 void device_destroy(struct class *class, dev_t devt
)
1618 dev
= class_find_device(class, NULL
, &devt
, __match_devt
);
1621 device_unregister(dev
);
1624 EXPORT_SYMBOL_GPL(device_destroy
);
1627 * device_rename - renames a device
1628 * @dev: the pointer to the struct device to be renamed
1629 * @new_name: the new name of the device
1631 * It is the responsibility of the caller to provide mutual
1632 * exclusion between two different calls of device_rename
1633 * on the same device to ensure that new_name is valid and
1634 * won't conflict with other devices.
1636 * Note: Don't call this function. Currently, the networking layer calls this
1637 * function, but that will change. The following text from Kay Sievers offers
1640 * Renaming devices is racy at many levels, symlinks and other stuff are not
1641 * replaced atomically, and you get a "move" uevent, but it's not easy to
1642 * connect the event to the old and new device. Device nodes are not renamed at
1643 * all, there isn't even support for that in the kernel now.
1645 * In the meantime, during renaming, your target name might be taken by another
1646 * driver, creating conflicts. Or the old name is taken directly after you
1647 * renamed it -- then you get events for the same DEVPATH, before you even see
1648 * the "move" event. It's just a mess, and nothing new should ever rely on
1649 * kernel device renaming. Besides that, it's not even implemented now for
1650 * other things than (driver-core wise very simple) network devices.
1652 * We are currently about to change network renaming in udev to completely
1653 * disallow renaming of devices in the same namespace as the kernel uses,
1654 * because we can't solve the problems properly, that arise with swapping names
1655 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
1656 * be allowed to some other name than eth[0-9]*, for the aforementioned
1659 * Make up a "real" name in the driver before you register anything, or add
1660 * some other attributes for userspace to find the device, or use udev to add
1661 * symlinks -- but never rename kernel devices later, it's a complete mess. We
1662 * don't even want to get into that and try to implement the missing pieces in
1663 * the core. We really have other pieces to fix in the driver core mess. :)
1665 int device_rename(struct device
*dev
, const char *new_name
)
1667 char *old_class_name
= NULL
;
1668 char *new_class_name
= NULL
;
1669 char *old_device_name
= NULL
;
1672 dev
= get_device(dev
);
1676 pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev
),
1677 __func__
, new_name
);
1679 old_device_name
= kstrdup(dev_name(dev
), GFP_KERNEL
);
1680 if (!old_device_name
) {
1686 error
= sysfs_rename_link(&dev
->class->p
->subsys
.kobj
,
1687 &dev
->kobj
, old_device_name
, new_name
);
1692 error
= kobject_rename(&dev
->kobj
, new_name
);
1699 kfree(new_class_name
);
1700 kfree(old_class_name
);
1701 kfree(old_device_name
);
1705 EXPORT_SYMBOL_GPL(device_rename
);
1707 static int device_move_class_links(struct device
*dev
,
1708 struct device
*old_parent
,
1709 struct device
*new_parent
)
1714 sysfs_remove_link(&dev
->kobj
, "device");
1716 error
= sysfs_create_link(&dev
->kobj
, &new_parent
->kobj
,
1722 * device_move - moves a device to a new parent
1723 * @dev: the pointer to the struct device to be moved
1724 * @new_parent: the new parent of the device (can by NULL)
1725 * @dpm_order: how to reorder the dpm_list
1727 int device_move(struct device
*dev
, struct device
*new_parent
,
1728 enum dpm_order dpm_order
)
1731 struct device
*old_parent
;
1732 struct kobject
*new_parent_kobj
;
1734 dev
= get_device(dev
);
1739 new_parent
= get_device(new_parent
);
1740 new_parent_kobj
= get_device_parent(dev
, new_parent
);
1742 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev
),
1743 __func__
, new_parent
? dev_name(new_parent
) : "<NULL>");
1744 error
= kobject_move(&dev
->kobj
, new_parent_kobj
);
1746 cleanup_glue_dir(dev
, new_parent_kobj
);
1747 put_device(new_parent
);
1750 old_parent
= dev
->parent
;
1751 dev
->parent
= new_parent
;
1753 klist_remove(&dev
->p
->knode_parent
);
1755 klist_add_tail(&dev
->p
->knode_parent
,
1756 &new_parent
->p
->klist_children
);
1757 set_dev_node(dev
, dev_to_node(new_parent
));
1761 error
= device_move_class_links(dev
, old_parent
, new_parent
);
1763 /* We ignore errors on cleanup since we're hosed anyway... */
1764 device_move_class_links(dev
, new_parent
, old_parent
);
1765 if (!kobject_move(&dev
->kobj
, &old_parent
->kobj
)) {
1767 klist_remove(&dev
->p
->knode_parent
);
1768 dev
->parent
= old_parent
;
1770 klist_add_tail(&dev
->p
->knode_parent
,
1771 &old_parent
->p
->klist_children
);
1772 set_dev_node(dev
, dev_to_node(old_parent
));
1775 cleanup_glue_dir(dev
, new_parent_kobj
);
1776 put_device(new_parent
);
1780 switch (dpm_order
) {
1781 case DPM_ORDER_NONE
:
1783 case DPM_ORDER_DEV_AFTER_PARENT
:
1784 device_pm_move_after(dev
, new_parent
);
1786 case DPM_ORDER_PARENT_BEFORE_DEV
:
1787 device_pm_move_before(new_parent
, dev
);
1789 case DPM_ORDER_DEV_LAST
:
1790 device_pm_move_last(dev
);
1794 put_device(old_parent
);
1800 EXPORT_SYMBOL_GPL(device_move
);
1803 * device_shutdown - call ->shutdown() on each device to shutdown.
1805 void device_shutdown(void)
1809 spin_lock(&devices_kset
->list_lock
);
1811 * Walk the devices list backward, shutting down each in turn.
1812 * Beware that device unplug events may also start pulling
1813 * devices offline, even as the system is shutting down.
1815 while (!list_empty(&devices_kset
->list
)) {
1816 dev
= list_entry(devices_kset
->list
.prev
, struct device
,
1820 * hold reference count of device's parent to
1821 * prevent it from being freed because parent's
1822 * lock is to be held
1824 get_device(dev
->parent
);
1827 * Make sure the device is off the kset list, in the
1828 * event that dev->*->shutdown() doesn't remove it.
1830 list_del_init(&dev
->kobj
.entry
);
1831 spin_unlock(&devices_kset
->list_lock
);
1833 /* hold lock to avoid race with probe/release */
1835 device_lock(dev
->parent
);
1838 /* Don't allow any more runtime suspends */
1839 pm_runtime_get_noresume(dev
);
1840 pm_runtime_barrier(dev
);
1842 if (dev
->bus
&& dev
->bus
->shutdown
) {
1843 dev_dbg(dev
, "shutdown\n");
1844 dev
->bus
->shutdown(dev
);
1845 } else if (dev
->driver
&& dev
->driver
->shutdown
) {
1846 dev_dbg(dev
, "shutdown\n");
1847 dev
->driver
->shutdown(dev
);
1852 device_unlock(dev
->parent
);
1855 put_device(dev
->parent
);
1857 spin_lock(&devices_kset
->list_lock
);
1859 spin_unlock(&devices_kset
->list_lock
);
1860 async_synchronize_full();
1864 * Device logging functions
1867 #ifdef CONFIG_PRINTK
1868 int __dev_printk(const char *level
, const struct device
*dev
,
1869 struct va_format
*vaf
)
1872 const char *level_extra
= "";
1877 return printk("%s(NULL device *): %pV", level
, vaf
);
1880 subsys
= dev
->class->name
;
1882 subsys
= dev
->bus
->name
;
1886 dictlen
+= snprintf(dict
+ dictlen
, sizeof(dict
) - dictlen
,
1887 "SUBSYSTEM=%s", subsys
);
1890 * Add device identifier DEVICE=:
1894 * +sound:card0 subsystem:devname
1896 if (MAJOR(dev
->devt
)) {
1899 if (strcmp(subsys
, "block") == 0)
1904 dictlen
+= snprintf(dict
+ dictlen
, sizeof(dict
) - dictlen
,
1906 c
, MAJOR(dev
->devt
), MINOR(dev
->devt
));
1907 } else if (strcmp(subsys
, "net") == 0) {
1908 struct net_device
*net
= to_net_dev(dev
);
1911 dictlen
+= snprintf(dict
+ dictlen
, sizeof(dict
) - dictlen
,
1912 "DEVICE=n%u", net
->ifindex
);
1915 dictlen
+= snprintf(dict
+ dictlen
, sizeof(dict
) - dictlen
,
1916 "DEVICE=+%s:%s", subsys
, dev_name(dev
));
1920 level_extra
= &level
[2]; /* skip past KERN_SOH "L" */
1922 return printk_emit(0, level
[1] - '0',
1923 dictlen
? dict
: NULL
, dictlen
,
1925 dev_driver_string(dev
), dev_name(dev
),
1928 EXPORT_SYMBOL(__dev_printk
);
1930 int dev_printk(const char *level
, const struct device
*dev
,
1931 const char *fmt
, ...)
1933 struct va_format vaf
;
1937 va_start(args
, fmt
);
1942 r
= __dev_printk(level
, dev
, &vaf
);
1947 EXPORT_SYMBOL(dev_printk
);
1949 #define define_dev_printk_level(func, kern_level) \
1950 int func(const struct device *dev, const char *fmt, ...) \
1952 struct va_format vaf; \
1956 va_start(args, fmt); \
1961 r = __dev_printk(kern_level, dev, &vaf); \
1966 EXPORT_SYMBOL(func);
1968 define_dev_printk_level(dev_emerg
, KERN_EMERG
);
1969 define_dev_printk_level(dev_alert
, KERN_ALERT
);
1970 define_dev_printk_level(dev_crit
, KERN_CRIT
);
1971 define_dev_printk_level(dev_err
, KERN_ERR
);
1972 define_dev_printk_level(dev_warn
, KERN_WARNING
);
1973 define_dev_printk_level(dev_notice
, KERN_NOTICE
);
1974 define_dev_printk_level(_dev_info
, KERN_INFO
);