2 * drivers/base/core.c - core driver model code (device registration, etc)
4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs
6 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (c) 2006 Novell, Inc.
9 * This file is released under the GPLv2
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <linux/kdev_t.h>
20 #include <linux/notifier.h>
21 #include <linux/genhd.h>
22 #include <linux/kallsyms.h>
23 #include <linux/semaphore.h>
24 #include <linux/mutex.h>
25 #include <linux/async.h>
28 #include "power/power.h"
30 int (*platform_notify
)(struct device
*dev
) = NULL
;
31 int (*platform_notify_remove
)(struct device
*dev
) = NULL
;
32 static struct kobject
*dev_kobj
;
33 struct kobject
*sysfs_dev_char_kobj
;
34 struct kobject
*sysfs_dev_block_kobj
;
37 static inline int device_is_not_partition(struct device
*dev
)
39 return !(dev
->type
== &part_type
);
42 static inline int device_is_not_partition(struct device
*dev
)
49 * dev_driver_string - Return a device's driver name, if at all possible
50 * @dev: struct device to get the name of
52 * Will return the device's driver's name if it is bound to a device. If
53 * the device is not bound to a device, it will return the name of the bus
54 * it is attached to. If it is not attached to a bus either, an empty
55 * string will be returned.
57 const char *dev_driver_string(const struct device
*dev
)
59 struct device_driver
*drv
;
61 /* dev->driver can change to NULL underneath us because of unbinding,
62 * so be careful about accessing it. dev->bus and dev->class should
63 * never change once they are set, so they don't need special care.
65 drv
= ACCESS_ONCE(dev
->driver
);
66 return drv
? drv
->name
:
67 (dev
->bus
? dev
->bus
->name
:
68 (dev
->class ? dev
->class->name
: ""));
70 EXPORT_SYMBOL(dev_driver_string
);
72 #define to_dev(obj) container_of(obj, struct device, kobj)
73 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
75 static ssize_t
dev_attr_show(struct kobject
*kobj
, struct attribute
*attr
,
78 struct device_attribute
*dev_attr
= to_dev_attr(attr
);
79 struct device
*dev
= to_dev(kobj
);
83 ret
= dev_attr
->show(dev
, dev_attr
, buf
);
84 if (ret
>= (ssize_t
)PAGE_SIZE
) {
85 print_symbol("dev_attr_show: %s returned bad count\n",
86 (unsigned long)dev_attr
->show
);
91 static ssize_t
dev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
92 const char *buf
, size_t count
)
94 struct device_attribute
*dev_attr
= to_dev_attr(attr
);
95 struct device
*dev
= to_dev(kobj
);
99 ret
= dev_attr
->store(dev
, dev_attr
, buf
, count
);
103 static struct sysfs_ops dev_sysfs_ops
= {
104 .show
= dev_attr_show
,
105 .store
= dev_attr_store
,
110 * device_release - free device structure.
111 * @kobj: device's kobject.
113 * This is called once the reference count for the object
114 * reaches 0. We forward the call to the device's release
115 * method, which should handle actually freeing the structure.
117 static void device_release(struct kobject
*kobj
)
119 struct device
*dev
= to_dev(kobj
);
120 struct device_private
*p
= dev
->p
;
124 else if (dev
->type
&& dev
->type
->release
)
125 dev
->type
->release(dev
);
126 else if (dev
->class && dev
->class->dev_release
)
127 dev
->class->dev_release(dev
);
129 WARN(1, KERN_ERR
"Device '%s' does not have a release() "
130 "function, it is broken and must be fixed.\n",
135 static struct kobj_type device_ktype
= {
136 .release
= device_release
,
137 .sysfs_ops
= &dev_sysfs_ops
,
141 static int dev_uevent_filter(struct kset
*kset
, struct kobject
*kobj
)
143 struct kobj_type
*ktype
= get_ktype(kobj
);
145 if (ktype
== &device_ktype
) {
146 struct device
*dev
= to_dev(kobj
);
155 static const char *dev_uevent_name(struct kset
*kset
, struct kobject
*kobj
)
157 struct device
*dev
= to_dev(kobj
);
160 return dev
->bus
->name
;
162 return dev
->class->name
;
166 static int dev_uevent(struct kset
*kset
, struct kobject
*kobj
,
167 struct kobj_uevent_env
*env
)
169 struct device
*dev
= to_dev(kobj
);
172 /* add device node properties if present */
173 if (MAJOR(dev
->devt
)) {
178 add_uevent_var(env
, "MAJOR=%u", MAJOR(dev
->devt
));
179 add_uevent_var(env
, "MINOR=%u", MINOR(dev
->devt
));
180 name
= device_get_devnode(dev
, &mode
, &tmp
);
182 add_uevent_var(env
, "DEVNAME=%s", name
);
185 add_uevent_var(env
, "DEVMODE=%#o", mode
& 0777);
189 if (dev
->type
&& dev
->type
->name
)
190 add_uevent_var(env
, "DEVTYPE=%s", dev
->type
->name
);
193 add_uevent_var(env
, "DRIVER=%s", dev
->driver
->name
);
195 #ifdef CONFIG_SYSFS_DEPRECATED
197 struct device
*parent
= dev
->parent
;
199 /* find first bus device in parent chain */
200 while (parent
&& !parent
->bus
)
201 parent
= parent
->parent
;
202 if (parent
&& parent
->bus
) {
205 path
= kobject_get_path(&parent
->kobj
, GFP_KERNEL
);
207 add_uevent_var(env
, "PHYSDEVPATH=%s", path
);
211 add_uevent_var(env
, "PHYSDEVBUS=%s", parent
->bus
->name
);
214 add_uevent_var(env
, "PHYSDEVDRIVER=%s",
215 parent
->driver
->name
);
217 } else if (dev
->bus
) {
218 add_uevent_var(env
, "PHYSDEVBUS=%s", dev
->bus
->name
);
221 add_uevent_var(env
, "PHYSDEVDRIVER=%s",
226 /* have the bus specific function add its stuff */
227 if (dev
->bus
&& dev
->bus
->uevent
) {
228 retval
= dev
->bus
->uevent(dev
, env
);
230 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
231 dev_name(dev
), __func__
, retval
);
234 /* have the class specific function add its stuff */
235 if (dev
->class && dev
->class->dev_uevent
) {
236 retval
= dev
->class->dev_uevent(dev
, env
);
238 pr_debug("device: '%s': %s: class uevent() "
239 "returned %d\n", dev_name(dev
),
243 /* have the device type specific fuction add its stuff */
244 if (dev
->type
&& dev
->type
->uevent
) {
245 retval
= dev
->type
->uevent(dev
, env
);
247 pr_debug("device: '%s': %s: dev_type uevent() "
248 "returned %d\n", dev_name(dev
),
255 static struct kset_uevent_ops device_uevent_ops
= {
256 .filter
= dev_uevent_filter
,
257 .name
= dev_uevent_name
,
258 .uevent
= dev_uevent
,
261 static ssize_t
show_uevent(struct device
*dev
, struct device_attribute
*attr
,
264 struct kobject
*top_kobj
;
266 struct kobj_uevent_env
*env
= NULL
;
271 /* search the kset, the device belongs to */
272 top_kobj
= &dev
->kobj
;
273 while (!top_kobj
->kset
&& top_kobj
->parent
)
274 top_kobj
= top_kobj
->parent
;
278 kset
= top_kobj
->kset
;
279 if (!kset
->uevent_ops
|| !kset
->uevent_ops
->uevent
)
283 if (kset
->uevent_ops
&& kset
->uevent_ops
->filter
)
284 if (!kset
->uevent_ops
->filter(kset
, &dev
->kobj
))
287 env
= kzalloc(sizeof(struct kobj_uevent_env
), GFP_KERNEL
);
291 /* let the kset specific function add its keys */
292 retval
= kset
->uevent_ops
->uevent(kset
, &dev
->kobj
, env
);
296 /* copy keys to file */
297 for (i
= 0; i
< env
->envp_idx
; i
++)
298 count
+= sprintf(&buf
[count
], "%s\n", env
->envp
[i
]);
304 static ssize_t
store_uevent(struct device
*dev
, struct device_attribute
*attr
,
305 const char *buf
, size_t count
)
307 enum kobject_action action
;
309 if (kobject_action_type(buf
, count
, &action
) == 0) {
310 kobject_uevent(&dev
->kobj
, action
);
314 dev_err(dev
, "uevent: unsupported action-string; this will "
315 "be ignored in a future kernel version\n");
316 kobject_uevent(&dev
->kobj
, KOBJ_ADD
);
321 static struct device_attribute uevent_attr
=
322 __ATTR(uevent
, S_IRUGO
| S_IWUSR
, show_uevent
, store_uevent
);
324 static int device_add_attributes(struct device
*dev
,
325 struct device_attribute
*attrs
)
331 for (i
= 0; attr_name(attrs
[i
]); i
++) {
332 error
= device_create_file(dev
, &attrs
[i
]);
338 device_remove_file(dev
, &attrs
[i
]);
343 static void device_remove_attributes(struct device
*dev
,
344 struct device_attribute
*attrs
)
349 for (i
= 0; attr_name(attrs
[i
]); i
++)
350 device_remove_file(dev
, &attrs
[i
]);
353 static int device_add_groups(struct device
*dev
,
354 const struct attribute_group
**groups
)
360 for (i
= 0; groups
[i
]; i
++) {
361 error
= sysfs_create_group(&dev
->kobj
, groups
[i
]);
364 sysfs_remove_group(&dev
->kobj
,
373 static void device_remove_groups(struct device
*dev
,
374 const struct attribute_group
**groups
)
379 for (i
= 0; groups
[i
]; i
++)
380 sysfs_remove_group(&dev
->kobj
, groups
[i
]);
383 static int device_add_attrs(struct device
*dev
)
385 struct class *class = dev
->class;
386 struct device_type
*type
= dev
->type
;
390 error
= device_add_attributes(dev
, class->dev_attrs
);
396 error
= device_add_groups(dev
, type
->groups
);
398 goto err_remove_class_attrs
;
401 error
= device_add_groups(dev
, dev
->groups
);
403 goto err_remove_type_groups
;
407 err_remove_type_groups
:
409 device_remove_groups(dev
, type
->groups
);
410 err_remove_class_attrs
:
412 device_remove_attributes(dev
, class->dev_attrs
);
417 static void device_remove_attrs(struct device
*dev
)
419 struct class *class = dev
->class;
420 struct device_type
*type
= dev
->type
;
422 device_remove_groups(dev
, dev
->groups
);
425 device_remove_groups(dev
, type
->groups
);
428 device_remove_attributes(dev
, class->dev_attrs
);
432 static ssize_t
show_dev(struct device
*dev
, struct device_attribute
*attr
,
435 return print_dev_t(buf
, dev
->devt
);
438 static struct device_attribute devt_attr
=
439 __ATTR(dev
, S_IRUGO
, show_dev
, NULL
);
441 /* kset to create /sys/devices/ */
442 struct kset
*devices_kset
;
445 * device_create_file - create sysfs attribute file for device.
447 * @attr: device attribute descriptor.
449 int device_create_file(struct device
*dev
, struct device_attribute
*attr
)
453 error
= sysfs_create_file(&dev
->kobj
, &attr
->attr
);
458 * device_remove_file - remove sysfs attribute file.
460 * @attr: device attribute descriptor.
462 void device_remove_file(struct device
*dev
, struct device_attribute
*attr
)
465 sysfs_remove_file(&dev
->kobj
, &attr
->attr
);
469 * device_create_bin_file - create sysfs binary attribute file for device.
471 * @attr: device binary attribute descriptor.
473 int device_create_bin_file(struct device
*dev
, struct bin_attribute
*attr
)
477 error
= sysfs_create_bin_file(&dev
->kobj
, attr
);
480 EXPORT_SYMBOL_GPL(device_create_bin_file
);
483 * device_remove_bin_file - remove sysfs binary attribute file
485 * @attr: device binary attribute descriptor.
487 void device_remove_bin_file(struct device
*dev
, struct bin_attribute
*attr
)
490 sysfs_remove_bin_file(&dev
->kobj
, attr
);
492 EXPORT_SYMBOL_GPL(device_remove_bin_file
);
495 * device_schedule_callback_owner - helper to schedule a callback for a device
497 * @func: callback function to invoke later.
498 * @owner: module owning the callback routine
500 * Attribute methods must not unregister themselves or their parent device
501 * (which would amount to the same thing). Attempts to do so will deadlock,
502 * since unregistration is mutually exclusive with driver callbacks.
504 * Instead methods can call this routine, which will attempt to allocate
505 * and schedule a workqueue request to call back @func with @dev as its
506 * argument in the workqueue's process context. @dev will be pinned until
509 * This routine is usually called via the inline device_schedule_callback(),
510 * which automatically sets @owner to THIS_MODULE.
512 * Returns 0 if the request was submitted, -ENOMEM if storage could not
513 * be allocated, -ENODEV if a reference to @owner isn't available.
515 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
516 * underlying sysfs routine (since it is intended for use by attribute
517 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
519 int device_schedule_callback_owner(struct device
*dev
,
520 void (*func
)(struct device
*), struct module
*owner
)
522 return sysfs_schedule_callback(&dev
->kobj
,
523 (void (*)(void *)) func
, dev
, owner
);
525 EXPORT_SYMBOL_GPL(device_schedule_callback_owner
);
527 static void klist_children_get(struct klist_node
*n
)
529 struct device_private
*p
= to_device_private_parent(n
);
530 struct device
*dev
= p
->device
;
535 static void klist_children_put(struct klist_node
*n
)
537 struct device_private
*p
= to_device_private_parent(n
);
538 struct device
*dev
= p
->device
;
544 * device_initialize - init device structure.
547 * This prepares the device for use by other layers by initializing
549 * It is the first half of device_register(), if called by
550 * that function, though it can also be called separately, so one
551 * may use @dev's fields. In particular, get_device()/put_device()
552 * may be used for reference counting of @dev after calling this
555 * NOTE: Use put_device() to give up your reference instead of freeing
556 * @dev directly once you have called this function.
558 void device_initialize(struct device
*dev
)
560 dev
->kobj
.kset
= devices_kset
;
561 kobject_init(&dev
->kobj
, &device_ktype
);
562 INIT_LIST_HEAD(&dev
->dma_pools
);
563 init_MUTEX(&dev
->sem
);
564 spin_lock_init(&dev
->devres_lock
);
565 INIT_LIST_HEAD(&dev
->devres_head
);
566 device_init_wakeup(dev
, 0);
568 set_dev_node(dev
, -1);
571 #ifdef CONFIG_SYSFS_DEPRECATED
572 static struct kobject
*get_device_parent(struct device
*dev
,
573 struct device
*parent
)
575 /* class devices without a parent live in /sys/class/<classname>/ */
576 if (dev
->class && (!parent
|| parent
->class != dev
->class))
577 return &dev
->class->p
->class_subsys
.kobj
;
578 /* all other devices keep their parent */
580 return &parent
->kobj
;
585 static inline void cleanup_device_parent(struct device
*dev
) {}
586 static inline void cleanup_glue_dir(struct device
*dev
,
587 struct kobject
*glue_dir
) {}
589 static struct kobject
*virtual_device_parent(struct device
*dev
)
591 static struct kobject
*virtual_dir
= NULL
;
594 virtual_dir
= kobject_create_and_add("virtual",
595 &devices_kset
->kobj
);
600 static struct kobject
*get_device_parent(struct device
*dev
,
601 struct device
*parent
)
606 static DEFINE_MUTEX(gdp_mutex
);
607 struct kobject
*kobj
= NULL
;
608 struct kobject
*parent_kobj
;
612 * If we have no parent, we live in "virtual".
613 * Class-devices with a non class-device as parent, live
614 * in a "glue" directory to prevent namespace collisions.
617 parent_kobj
= virtual_device_parent(dev
);
618 else if (parent
->class)
619 return &parent
->kobj
;
621 parent_kobj
= &parent
->kobj
;
623 mutex_lock(&gdp_mutex
);
625 /* find our class-directory at the parent and reference it */
626 spin_lock(&dev
->class->p
->class_dirs
.list_lock
);
627 list_for_each_entry(k
, &dev
->class->p
->class_dirs
.list
, entry
)
628 if (k
->parent
== parent_kobj
) {
629 kobj
= kobject_get(k
);
632 spin_unlock(&dev
->class->p
->class_dirs
.list_lock
);
634 mutex_unlock(&gdp_mutex
);
638 /* or create a new class-directory at the parent device */
639 k
= kobject_create();
641 mutex_unlock(&gdp_mutex
);
644 k
->kset
= &dev
->class->p
->class_dirs
;
645 retval
= kobject_add(k
, parent_kobj
, "%s", dev
->class->name
);
647 mutex_unlock(&gdp_mutex
);
651 /* do not emit an uevent for this simple "glue" directory */
652 mutex_unlock(&gdp_mutex
);
657 return &parent
->kobj
;
661 static void cleanup_glue_dir(struct device
*dev
, struct kobject
*glue_dir
)
663 /* see if we live in a "glue" directory */
664 if (!glue_dir
|| !dev
->class ||
665 glue_dir
->kset
!= &dev
->class->p
->class_dirs
)
668 kobject_put(glue_dir
);
671 static void cleanup_device_parent(struct device
*dev
)
673 cleanup_glue_dir(dev
, dev
->kobj
.parent
);
677 static void setup_parent(struct device
*dev
, struct device
*parent
)
679 struct kobject
*kobj
;
680 kobj
= get_device_parent(dev
, parent
);
682 dev
->kobj
.parent
= kobj
;
685 static int device_add_class_symlinks(struct device
*dev
)
692 error
= sysfs_create_link(&dev
->kobj
,
693 &dev
->class->p
->class_subsys
.kobj
,
698 #ifdef CONFIG_SYSFS_DEPRECATED
699 /* stacked class devices need a symlink in the class directory */
700 if (dev
->kobj
.parent
!= &dev
->class->p
->class_subsys
.kobj
&&
701 device_is_not_partition(dev
)) {
702 error
= sysfs_create_link(&dev
->class->p
->class_subsys
.kobj
,
703 &dev
->kobj
, dev_name(dev
));
708 if (dev
->parent
&& device_is_not_partition(dev
)) {
709 struct device
*parent
= dev
->parent
;
713 * stacked class devices have the 'device' link
714 * pointing to the bus device instead of the parent
716 while (parent
->class && !parent
->bus
&& parent
->parent
)
717 parent
= parent
->parent
;
719 error
= sysfs_create_link(&dev
->kobj
,
725 class_name
= make_class_name(dev
->class->name
,
728 error
= sysfs_create_link(&dev
->parent
->kobj
,
729 &dev
->kobj
, class_name
);
737 if (dev
->parent
&& device_is_not_partition(dev
))
738 sysfs_remove_link(&dev
->kobj
, "device");
740 if (dev
->kobj
.parent
!= &dev
->class->p
->class_subsys
.kobj
&&
741 device_is_not_partition(dev
))
742 sysfs_remove_link(&dev
->class->p
->class_subsys
.kobj
,
745 /* link in the class directory pointing to the device */
746 error
= sysfs_create_link(&dev
->class->p
->class_subsys
.kobj
,
747 &dev
->kobj
, dev_name(dev
));
751 if (dev
->parent
&& device_is_not_partition(dev
)) {
752 error
= sysfs_create_link(&dev
->kobj
, &dev
->parent
->kobj
,
760 sysfs_remove_link(&dev
->class->p
->class_subsys
.kobj
, dev_name(dev
));
764 sysfs_remove_link(&dev
->kobj
, "subsystem");
769 static void device_remove_class_symlinks(struct device
*dev
)
774 #ifdef CONFIG_SYSFS_DEPRECATED
775 if (dev
->parent
&& device_is_not_partition(dev
)) {
778 class_name
= make_class_name(dev
->class->name
, &dev
->kobj
);
780 sysfs_remove_link(&dev
->parent
->kobj
, class_name
);
783 sysfs_remove_link(&dev
->kobj
, "device");
786 if (dev
->kobj
.parent
!= &dev
->class->p
->class_subsys
.kobj
&&
787 device_is_not_partition(dev
))
788 sysfs_remove_link(&dev
->class->p
->class_subsys
.kobj
,
791 if (dev
->parent
&& device_is_not_partition(dev
))
792 sysfs_remove_link(&dev
->kobj
, "device");
794 sysfs_remove_link(&dev
->class->p
->class_subsys
.kobj
, dev_name(dev
));
797 sysfs_remove_link(&dev
->kobj
, "subsystem");
801 * dev_set_name - set a device name
803 * @fmt: format string for the device's name
805 int dev_set_name(struct device
*dev
, const char *fmt
, ...)
810 va_start(vargs
, fmt
);
811 err
= kobject_set_name_vargs(&dev
->kobj
, fmt
, vargs
);
815 EXPORT_SYMBOL_GPL(dev_set_name
);
818 * device_to_dev_kobj - select a /sys/dev/ directory for the device
821 * By default we select char/ for new entries. Setting class->dev_obj
822 * to NULL prevents an entry from being created. class->dev_kobj must
823 * be set (or cleared) before any devices are registered to the class
824 * otherwise device_create_sys_dev_entry() and
825 * device_remove_sys_dev_entry() will disagree about the the presence
828 static struct kobject
*device_to_dev_kobj(struct device
*dev
)
830 struct kobject
*kobj
;
833 kobj
= dev
->class->dev_kobj
;
835 kobj
= sysfs_dev_char_kobj
;
840 static int device_create_sys_dev_entry(struct device
*dev
)
842 struct kobject
*kobj
= device_to_dev_kobj(dev
);
847 format_dev_t(devt_str
, dev
->devt
);
848 error
= sysfs_create_link(kobj
, &dev
->kobj
, devt_str
);
854 static void device_remove_sys_dev_entry(struct device
*dev
)
856 struct kobject
*kobj
= device_to_dev_kobj(dev
);
860 format_dev_t(devt_str
, dev
->devt
);
861 sysfs_remove_link(kobj
, devt_str
);
865 int device_private_init(struct device
*dev
)
867 dev
->p
= kzalloc(sizeof(*dev
->p
), GFP_KERNEL
);
870 dev
->p
->device
= dev
;
871 klist_init(&dev
->p
->klist_children
, klist_children_get
,
877 * device_add - add device to device hierarchy.
880 * This is part 2 of device_register(), though may be called
881 * separately _iff_ device_initialize() has been called separately.
883 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
884 * to the global and sibling lists for the device, then
885 * adds it to the other relevant subsystems of the driver model.
887 * NOTE: _Never_ directly free @dev after calling this function, even
888 * if it returned an error! Always use put_device() to give up your
891 int device_add(struct device
*dev
)
893 struct device
*parent
= NULL
;
894 struct class_interface
*class_intf
;
897 dev
= get_device(dev
);
902 error
= device_private_init(dev
);
908 * for statically allocated devices, which should all be converted
909 * some day, we need to initialize the name. We prevent reading back
910 * the name, and force the use of dev_name()
912 if (dev
->init_name
) {
913 dev_set_name(dev
, "%s", dev
->init_name
);
914 dev
->init_name
= NULL
;
920 pr_debug("device: '%s': %s\n", dev_name(dev
), __func__
);
922 parent
= get_device(dev
->parent
);
923 setup_parent(dev
, parent
);
925 /* use parent numa_node */
927 set_dev_node(dev
, dev_to_node(parent
));
929 /* first, register with generic layer. */
930 /* we require the name to be set before, and pass NULL */
931 error
= kobject_add(&dev
->kobj
, dev
->kobj
.parent
, NULL
);
935 /* notify platform of device entry */
937 platform_notify(dev
);
939 error
= device_create_file(dev
, &uevent_attr
);
943 if (MAJOR(dev
->devt
)) {
944 error
= device_create_file(dev
, &devt_attr
);
946 goto ueventattrError
;
948 error
= device_create_sys_dev_entry(dev
);
952 devtmpfs_create_node(dev
);
955 error
= device_add_class_symlinks(dev
);
958 error
= device_add_attrs(dev
);
961 error
= bus_add_device(dev
);
964 error
= dpm_sysfs_add(dev
);
969 /* Notify clients of device addition. This call must come
970 * after dpm_sysf_add() and before kobject_uevent().
973 blocking_notifier_call_chain(&dev
->bus
->p
->bus_notifier
,
974 BUS_NOTIFY_ADD_DEVICE
, dev
);
976 kobject_uevent(&dev
->kobj
, KOBJ_ADD
);
977 bus_probe_device(dev
);
979 klist_add_tail(&dev
->p
->knode_parent
,
980 &parent
->p
->klist_children
);
983 mutex_lock(&dev
->class->p
->class_mutex
);
984 /* tie the class to the device */
985 klist_add_tail(&dev
->knode_class
,
986 &dev
->class->p
->class_devices
);
988 /* notify any interfaces that the device is here */
989 list_for_each_entry(class_intf
,
990 &dev
->class->p
->class_interfaces
, node
)
991 if (class_intf
->add_dev
)
992 class_intf
->add_dev(dev
, class_intf
);
993 mutex_unlock(&dev
->class->p
->class_mutex
);
999 bus_remove_device(dev
);
1001 device_remove_attrs(dev
);
1003 device_remove_class_symlinks(dev
);
1005 if (MAJOR(dev
->devt
))
1006 device_remove_sys_dev_entry(dev
);
1008 if (MAJOR(dev
->devt
))
1009 device_remove_file(dev
, &devt_attr
);
1011 device_remove_file(dev
, &uevent_attr
);
1013 kobject_uevent(&dev
->kobj
, KOBJ_REMOVE
);
1014 kobject_del(&dev
->kobj
);
1016 cleanup_device_parent(dev
);
1026 * device_register - register a device with the system.
1027 * @dev: pointer to the device structure
1029 * This happens in two clean steps - initialize the device
1030 * and add it to the system. The two steps can be called
1031 * separately, but this is the easiest and most common.
1032 * I.e. you should only call the two helpers separately if
1033 * have a clearly defined need to use and refcount the device
1034 * before it is added to the hierarchy.
1036 * NOTE: _Never_ directly free @dev after calling this function, even
1037 * if it returned an error! Always use put_device() to give up the
1038 * reference initialized in this function instead.
1040 int device_register(struct device
*dev
)
1042 device_initialize(dev
);
1043 return device_add(dev
);
1047 * get_device - increment reference count for device.
1050 * This simply forwards the call to kobject_get(), though
1051 * we do take care to provide for the case that we get a NULL
1052 * pointer passed in.
1054 struct device
*get_device(struct device
*dev
)
1056 return dev
? to_dev(kobject_get(&dev
->kobj
)) : NULL
;
1060 * put_device - decrement reference count.
1061 * @dev: device in question.
1063 void put_device(struct device
*dev
)
1065 /* might_sleep(); */
1067 kobject_put(&dev
->kobj
);
1071 * device_del - delete device from system.
1074 * This is the first part of the device unregistration
1075 * sequence. This removes the device from the lists we control
1076 * from here, has it removed from the other driver model
1077 * subsystems it was added to in device_add(), and removes it
1078 * from the kobject hierarchy.
1080 * NOTE: this should be called manually _iff_ device_add() was
1081 * also called manually.
1083 void device_del(struct device
*dev
)
1085 struct device
*parent
= dev
->parent
;
1086 struct class_interface
*class_intf
;
1088 /* Notify clients of device removal. This call must come
1089 * before dpm_sysfs_remove().
1092 blocking_notifier_call_chain(&dev
->bus
->p
->bus_notifier
,
1093 BUS_NOTIFY_DEL_DEVICE
, dev
);
1094 device_pm_remove(dev
);
1095 dpm_sysfs_remove(dev
);
1097 klist_del(&dev
->p
->knode_parent
);
1098 if (MAJOR(dev
->devt
)) {
1099 devtmpfs_delete_node(dev
);
1100 device_remove_sys_dev_entry(dev
);
1101 device_remove_file(dev
, &devt_attr
);
1104 device_remove_class_symlinks(dev
);
1106 mutex_lock(&dev
->class->p
->class_mutex
);
1107 /* notify any interfaces that the device is now gone */
1108 list_for_each_entry(class_intf
,
1109 &dev
->class->p
->class_interfaces
, node
)
1110 if (class_intf
->remove_dev
)
1111 class_intf
->remove_dev(dev
, class_intf
);
1112 /* remove the device from the class list */
1113 klist_del(&dev
->knode_class
);
1114 mutex_unlock(&dev
->class->p
->class_mutex
);
1116 device_remove_file(dev
, &uevent_attr
);
1117 device_remove_attrs(dev
);
1118 bus_remove_device(dev
);
1121 * Some platform devices are driven without driver attached
1122 * and managed resources may have been acquired. Make sure
1123 * all resources are released.
1125 devres_release_all(dev
);
1127 /* Notify the platform of the removal, in case they
1128 * need to do anything...
1130 if (platform_notify_remove
)
1131 platform_notify_remove(dev
);
1132 kobject_uevent(&dev
->kobj
, KOBJ_REMOVE
);
1133 cleanup_device_parent(dev
);
1134 kobject_del(&dev
->kobj
);
1139 * device_unregister - unregister device from system.
1140 * @dev: device going away.
1142 * We do this in two parts, like we do device_register(). First,
1143 * we remove it from all the subsystems with device_del(), then
1144 * we decrement the reference count via put_device(). If that
1145 * is the final reference count, the device will be cleaned up
1146 * via device_release() above. Otherwise, the structure will
1147 * stick around until the final reference to the device is dropped.
1149 void device_unregister(struct device
*dev
)
1151 pr_debug("device: '%s': %s\n", dev_name(dev
), __func__
);
1156 static struct device
*next_device(struct klist_iter
*i
)
1158 struct klist_node
*n
= klist_next(i
);
1159 struct device
*dev
= NULL
;
1160 struct device_private
*p
;
1163 p
= to_device_private_parent(n
);
1170 * device_get_devnode - path of device node file
1172 * @mode: returned file access mode
1173 * @tmp: possibly allocated string
1175 * Return the relative path of a possible device node.
1176 * Non-default names may need to allocate a memory to compose
1177 * a name. This memory is returned in tmp and needs to be
1178 * freed by the caller.
1180 const char *device_get_devnode(struct device
*dev
,
1181 mode_t
*mode
, const char **tmp
)
1187 /* the device type may provide a specific name */
1188 if (dev
->type
&& dev
->type
->devnode
)
1189 *tmp
= dev
->type
->devnode(dev
, mode
);
1193 /* the class may provide a specific name */
1194 if (dev
->class && dev
->class->devnode
)
1195 *tmp
= dev
->class->devnode(dev
, mode
);
1199 /* return name without allocation, tmp == NULL */
1200 if (strchr(dev_name(dev
), '!') == NULL
)
1201 return dev_name(dev
);
1203 /* replace '!' in the name with '/' */
1204 *tmp
= kstrdup(dev_name(dev
), GFP_KERNEL
);
1207 while ((s
= strchr(*tmp
, '!')))
1213 * device_for_each_child - device child iterator.
1214 * @parent: parent struct device.
1215 * @data: data for the callback.
1216 * @fn: function to be called for each device.
1218 * Iterate over @parent's child devices, and call @fn for each,
1221 * We check the return of @fn each time. If it returns anything
1222 * other than 0, we break out and return that value.
1224 int device_for_each_child(struct device
*parent
, void *data
,
1225 int (*fn
)(struct device
*dev
, void *data
))
1227 struct klist_iter i
;
1228 struct device
*child
;
1234 klist_iter_init(&parent
->p
->klist_children
, &i
);
1235 while ((child
= next_device(&i
)) && !error
)
1236 error
= fn(child
, data
);
1237 klist_iter_exit(&i
);
1242 * device_find_child - device iterator for locating a particular device.
1243 * @parent: parent struct device
1244 * @data: Data to pass to match function
1245 * @match: Callback function to check device
1247 * This is similar to the device_for_each_child() function above, but it
1248 * returns a reference to a device that is 'found' for later use, as
1249 * determined by the @match callback.
1251 * The callback should return 0 if the device doesn't match and non-zero
1252 * if it does. If the callback returns non-zero and a reference to the
1253 * current device can be obtained, this function will return to the caller
1254 * and not iterate over any more devices.
1256 struct device
*device_find_child(struct device
*parent
, void *data
,
1257 int (*match
)(struct device
*dev
, void *data
))
1259 struct klist_iter i
;
1260 struct device
*child
;
1265 klist_iter_init(&parent
->p
->klist_children
, &i
);
1266 while ((child
= next_device(&i
)))
1267 if (match(child
, data
) && get_device(child
))
1269 klist_iter_exit(&i
);
1273 int __init
devices_init(void)
1275 devices_kset
= kset_create_and_add("devices", &device_uevent_ops
, NULL
);
1278 dev_kobj
= kobject_create_and_add("dev", NULL
);
1281 sysfs_dev_block_kobj
= kobject_create_and_add("block", dev_kobj
);
1282 if (!sysfs_dev_block_kobj
)
1283 goto block_kobj_err
;
1284 sysfs_dev_char_kobj
= kobject_create_and_add("char", dev_kobj
);
1285 if (!sysfs_dev_char_kobj
)
1291 kobject_put(sysfs_dev_block_kobj
);
1293 kobject_put(dev_kobj
);
1295 kset_unregister(devices_kset
);
1299 EXPORT_SYMBOL_GPL(device_for_each_child
);
1300 EXPORT_SYMBOL_GPL(device_find_child
);
1302 EXPORT_SYMBOL_GPL(device_initialize
);
1303 EXPORT_SYMBOL_GPL(device_add
);
1304 EXPORT_SYMBOL_GPL(device_register
);
1306 EXPORT_SYMBOL_GPL(device_del
);
1307 EXPORT_SYMBOL_GPL(device_unregister
);
1308 EXPORT_SYMBOL_GPL(get_device
);
1309 EXPORT_SYMBOL_GPL(put_device
);
1311 EXPORT_SYMBOL_GPL(device_create_file
);
1312 EXPORT_SYMBOL_GPL(device_remove_file
);
1317 struct module
*owner
;
1320 #define to_root_device(dev) container_of(dev, struct root_device, dev)
1322 static void root_device_release(struct device
*dev
)
1324 kfree(to_root_device(dev
));
1328 * __root_device_register - allocate and register a root device
1329 * @name: root device name
1330 * @owner: owner module of the root device, usually THIS_MODULE
1332 * This function allocates a root device and registers it
1333 * using device_register(). In order to free the returned
1334 * device, use root_device_unregister().
1336 * Root devices are dummy devices which allow other devices
1337 * to be grouped under /sys/devices. Use this function to
1338 * allocate a root device and then use it as the parent of
1339 * any device which should appear under /sys/devices/{name}
1341 * The /sys/devices/{name} directory will also contain a
1342 * 'module' symlink which points to the @owner directory
1345 * Note: You probably want to use root_device_register().
1347 struct device
*__root_device_register(const char *name
, struct module
*owner
)
1349 struct root_device
*root
;
1352 root
= kzalloc(sizeof(struct root_device
), GFP_KERNEL
);
1354 return ERR_PTR(err
);
1356 err
= dev_set_name(&root
->dev
, "%s", name
);
1359 return ERR_PTR(err
);
1362 root
->dev
.release
= root_device_release
;
1364 err
= device_register(&root
->dev
);
1366 put_device(&root
->dev
);
1367 return ERR_PTR(err
);
1370 #ifdef CONFIG_MODULE /* gotta find a "cleaner" way to do this */
1372 struct module_kobject
*mk
= &owner
->mkobj
;
1374 err
= sysfs_create_link(&root
->dev
.kobj
, &mk
->kobj
, "module");
1376 device_unregister(&root
->dev
);
1377 return ERR_PTR(err
);
1379 root
->owner
= owner
;
1385 EXPORT_SYMBOL_GPL(__root_device_register
);
1388 * root_device_unregister - unregister and free a root device
1389 * @dev: device going away
1391 * This function unregisters and cleans up a device that was created by
1392 * root_device_register().
1394 void root_device_unregister(struct device
*dev
)
1396 struct root_device
*root
= to_root_device(dev
);
1399 sysfs_remove_link(&root
->dev
.kobj
, "module");
1401 device_unregister(dev
);
1403 EXPORT_SYMBOL_GPL(root_device_unregister
);
1406 static void device_create_release(struct device
*dev
)
1408 pr_debug("device: '%s': %s\n", dev_name(dev
), __func__
);
1413 * device_create_vargs - creates a device and registers it with sysfs
1414 * @class: pointer to the struct class that this device should be registered to
1415 * @parent: pointer to the parent struct device of this new device, if any
1416 * @devt: the dev_t for the char device to be added
1417 * @drvdata: the data to be added to the device for callbacks
1418 * @fmt: string for the device's name
1419 * @args: va_list for the device's name
1421 * This function can be used by char device classes. A struct device
1422 * will be created in sysfs, registered to the specified class.
1424 * A "dev" file will be created, showing the dev_t for the device, if
1425 * the dev_t is not 0,0.
1426 * If a pointer to a parent struct device is passed in, the newly created
1427 * struct device will be a child of that device in sysfs.
1428 * The pointer to the struct device will be returned from the call.
1429 * Any further sysfs files that might be required can be created using this
1432 * Note: the struct class passed to this function must have previously
1433 * been created with a call to class_create().
1435 struct device
*device_create_vargs(struct class *class, struct device
*parent
,
1436 dev_t devt
, void *drvdata
, const char *fmt
,
1439 struct device
*dev
= NULL
;
1440 int retval
= -ENODEV
;
1442 if (class == NULL
|| IS_ERR(class))
1445 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1453 dev
->parent
= parent
;
1454 dev
->release
= device_create_release
;
1455 dev_set_drvdata(dev
, drvdata
);
1457 retval
= kobject_set_name_vargs(&dev
->kobj
, fmt
, args
);
1461 retval
= device_register(dev
);
1469 return ERR_PTR(retval
);
1471 EXPORT_SYMBOL_GPL(device_create_vargs
);
1474 * device_create - creates a device and registers it with sysfs
1475 * @class: pointer to the struct class that this device should be registered to
1476 * @parent: pointer to the parent struct device of this new device, if any
1477 * @devt: the dev_t for the char device to be added
1478 * @drvdata: the data to be added to the device for callbacks
1479 * @fmt: string for the device's name
1481 * This function can be used by char device classes. A struct device
1482 * will be created in sysfs, registered to the specified class.
1484 * A "dev" file will be created, showing the dev_t for the device, if
1485 * the dev_t is not 0,0.
1486 * If a pointer to a parent struct device is passed in, the newly created
1487 * struct device will be a child of that device in sysfs.
1488 * The pointer to the struct device will be returned from the call.
1489 * Any further sysfs files that might be required can be created using this
1492 * Note: the struct class passed to this function must have previously
1493 * been created with a call to class_create().
1495 struct device
*device_create(struct class *class, struct device
*parent
,
1496 dev_t devt
, void *drvdata
, const char *fmt
, ...)
1501 va_start(vargs
, fmt
);
1502 dev
= device_create_vargs(class, parent
, devt
, drvdata
, fmt
, vargs
);
1506 EXPORT_SYMBOL_GPL(device_create
);
1508 static int __match_devt(struct device
*dev
, void *data
)
1512 return dev
->devt
== *devt
;
1516 * device_destroy - removes a device that was created with device_create()
1517 * @class: pointer to the struct class that this device was registered with
1518 * @devt: the dev_t of the device that was previously registered
1520 * This call unregisters and cleans up a device that was created with a
1521 * call to device_create().
1523 void device_destroy(struct class *class, dev_t devt
)
1527 dev
= class_find_device(class, NULL
, &devt
, __match_devt
);
1530 device_unregister(dev
);
1533 EXPORT_SYMBOL_GPL(device_destroy
);
1536 * device_rename - renames a device
1537 * @dev: the pointer to the struct device to be renamed
1538 * @new_name: the new name of the device
1540 * It is the responsibility of the caller to provide mutual
1541 * exclusion between two different calls of device_rename
1542 * on the same device to ensure that new_name is valid and
1543 * won't conflict with other devices.
1545 int device_rename(struct device
*dev
, char *new_name
)
1547 char *old_class_name
= NULL
;
1548 char *new_class_name
= NULL
;
1549 char *old_device_name
= NULL
;
1552 dev
= get_device(dev
);
1556 pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev
),
1557 __func__
, new_name
);
1559 #ifdef CONFIG_SYSFS_DEPRECATED
1560 if ((dev
->class) && (dev
->parent
))
1561 old_class_name
= make_class_name(dev
->class->name
, &dev
->kobj
);
1564 old_device_name
= kstrdup(dev_name(dev
), GFP_KERNEL
);
1565 if (!old_device_name
) {
1570 error
= kobject_rename(&dev
->kobj
, new_name
);
1574 #ifdef CONFIG_SYSFS_DEPRECATED
1575 if (old_class_name
) {
1576 new_class_name
= make_class_name(dev
->class->name
, &dev
->kobj
);
1577 if (new_class_name
) {
1578 error
= sysfs_create_link_nowarn(&dev
->parent
->kobj
,
1583 sysfs_remove_link(&dev
->parent
->kobj
, old_class_name
);
1588 error
= sysfs_create_link_nowarn(&dev
->class->p
->class_subsys
.kobj
,
1589 &dev
->kobj
, dev_name(dev
));
1592 sysfs_remove_link(&dev
->class->p
->class_subsys
.kobj
,
1600 kfree(new_class_name
);
1601 kfree(old_class_name
);
1602 kfree(old_device_name
);
1606 EXPORT_SYMBOL_GPL(device_rename
);
1608 static int device_move_class_links(struct device
*dev
,
1609 struct device
*old_parent
,
1610 struct device
*new_parent
)
1613 #ifdef CONFIG_SYSFS_DEPRECATED
1616 class_name
= make_class_name(dev
->class->name
, &dev
->kobj
);
1622 sysfs_remove_link(&dev
->kobj
, "device");
1623 sysfs_remove_link(&old_parent
->kobj
, class_name
);
1626 error
= sysfs_create_link(&dev
->kobj
, &new_parent
->kobj
,
1630 error
= sysfs_create_link(&new_parent
->kobj
, &dev
->kobj
,
1633 sysfs_remove_link(&dev
->kobj
, "device");
1641 sysfs_remove_link(&dev
->kobj
, "device");
1643 error
= sysfs_create_link(&dev
->kobj
, &new_parent
->kobj
,
1650 * device_move - moves a device to a new parent
1651 * @dev: the pointer to the struct device to be moved
1652 * @new_parent: the new parent of the device (can by NULL)
1653 * @dpm_order: how to reorder the dpm_list
1655 int device_move(struct device
*dev
, struct device
*new_parent
,
1656 enum dpm_order dpm_order
)
1659 struct device
*old_parent
;
1660 struct kobject
*new_parent_kobj
;
1662 dev
= get_device(dev
);
1667 new_parent
= get_device(new_parent
);
1668 new_parent_kobj
= get_device_parent(dev
, new_parent
);
1670 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev
),
1671 __func__
, new_parent
? dev_name(new_parent
) : "<NULL>");
1672 error
= kobject_move(&dev
->kobj
, new_parent_kobj
);
1674 cleanup_glue_dir(dev
, new_parent_kobj
);
1675 put_device(new_parent
);
1678 old_parent
= dev
->parent
;
1679 dev
->parent
= new_parent
;
1681 klist_remove(&dev
->p
->knode_parent
);
1683 klist_add_tail(&dev
->p
->knode_parent
,
1684 &new_parent
->p
->klist_children
);
1685 set_dev_node(dev
, dev_to_node(new_parent
));
1690 error
= device_move_class_links(dev
, old_parent
, new_parent
);
1692 /* We ignore errors on cleanup since we're hosed anyway... */
1693 device_move_class_links(dev
, new_parent
, old_parent
);
1694 if (!kobject_move(&dev
->kobj
, &old_parent
->kobj
)) {
1696 klist_remove(&dev
->p
->knode_parent
);
1697 dev
->parent
= old_parent
;
1699 klist_add_tail(&dev
->p
->knode_parent
,
1700 &old_parent
->p
->klist_children
);
1701 set_dev_node(dev
, dev_to_node(old_parent
));
1704 cleanup_glue_dir(dev
, new_parent_kobj
);
1705 put_device(new_parent
);
1708 switch (dpm_order
) {
1709 case DPM_ORDER_NONE
:
1711 case DPM_ORDER_DEV_AFTER_PARENT
:
1712 device_pm_move_after(dev
, new_parent
);
1714 case DPM_ORDER_PARENT_BEFORE_DEV
:
1715 device_pm_move_before(new_parent
, dev
);
1717 case DPM_ORDER_DEV_LAST
:
1718 device_pm_move_last(dev
);
1722 put_device(old_parent
);
1728 EXPORT_SYMBOL_GPL(device_move
);
1731 * device_shutdown - call ->shutdown() on each device to shutdown.
1733 void device_shutdown(void)
1735 struct device
*dev
, *devn
;
1737 list_for_each_entry_safe_reverse(dev
, devn
, &devices_kset
->list
,
1739 if (dev
->bus
&& dev
->bus
->shutdown
) {
1740 dev_dbg(dev
, "shutdown\n");
1741 dev
->bus
->shutdown(dev
);
1742 } else if (dev
->driver
&& dev
->driver
->shutdown
) {
1743 dev_dbg(dev
, "shutdown\n");
1744 dev
->driver
->shutdown(dev
);
1747 kobject_put(sysfs_dev_char_kobj
);
1748 kobject_put(sysfs_dev_block_kobj
);
1749 kobject_put(dev_kobj
);
1750 async_synchronize_full();