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/mutex.h>
24 #include <linux/async.h>
27 #include "power/power.h"
29 #ifdef CONFIG_SYSFS_DEPRECATED
30 #ifdef CONFIG_SYSFS_DEPRECATED_V2
31 long sysfs_deprecated
= 1;
33 long sysfs_deprecated
= 0;
35 static __init
int sysfs_deprecated_setup(char *arg
)
37 return strict_strtol(arg
, 10, &sysfs_deprecated
);
39 early_param("sysfs.deprecated", sysfs_deprecated_setup
);
42 int (*platform_notify
)(struct device
*dev
) = NULL
;
43 int (*platform_notify_remove
)(struct device
*dev
) = NULL
;
44 static struct kobject
*dev_kobj
;
45 struct kobject
*sysfs_dev_char_kobj
;
46 struct kobject
*sysfs_dev_block_kobj
;
49 static inline int device_is_not_partition(struct device
*dev
)
51 return !(dev
->type
== &part_type
);
54 static inline int device_is_not_partition(struct device
*dev
)
61 * dev_driver_string - Return a device's driver name, if at all possible
62 * @dev: struct device to get the name of
64 * Will return the device's driver's name if it is bound to a device. If
65 * the device is not bound to a device, it will return the name of the bus
66 * it is attached to. If it is not attached to a bus either, an empty
67 * string will be returned.
69 const char *dev_driver_string(const struct device
*dev
)
71 struct device_driver
*drv
;
73 /* dev->driver can change to NULL underneath us because of unbinding,
74 * so be careful about accessing it. dev->bus and dev->class should
75 * never change once they are set, so they don't need special care.
77 drv
= ACCESS_ONCE(dev
->driver
);
78 return drv
? drv
->name
:
79 (dev
->bus
? dev
->bus
->name
:
80 (dev
->class ? dev
->class->name
: ""));
82 EXPORT_SYMBOL(dev_driver_string
);
84 #define to_dev(obj) container_of(obj, struct device, kobj)
85 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
87 static ssize_t
dev_attr_show(struct kobject
*kobj
, struct attribute
*attr
,
90 struct device_attribute
*dev_attr
= to_dev_attr(attr
);
91 struct device
*dev
= to_dev(kobj
);
95 ret
= dev_attr
->show(dev
, dev_attr
, buf
);
96 if (ret
>= (ssize_t
)PAGE_SIZE
) {
97 print_symbol("dev_attr_show: %s returned bad count\n",
98 (unsigned long)dev_attr
->show
);
103 static ssize_t
dev_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
104 const char *buf
, size_t count
)
106 struct device_attribute
*dev_attr
= to_dev_attr(attr
);
107 struct device
*dev
= to_dev(kobj
);
111 ret
= dev_attr
->store(dev
, dev_attr
, buf
, count
);
115 static const struct sysfs_ops dev_sysfs_ops
= {
116 .show
= dev_attr_show
,
117 .store
= dev_attr_store
,
122 * device_release - free device structure.
123 * @kobj: device's kobject.
125 * This is called once the reference count for the object
126 * reaches 0. We forward the call to the device's release
127 * method, which should handle actually freeing the structure.
129 static void device_release(struct kobject
*kobj
)
131 struct device
*dev
= to_dev(kobj
);
132 struct device_private
*p
= dev
->p
;
136 else if (dev
->type
&& dev
->type
->release
)
137 dev
->type
->release(dev
);
138 else if (dev
->class && dev
->class->dev_release
)
139 dev
->class->dev_release(dev
);
141 WARN(1, KERN_ERR
"Device '%s' does not have a release() "
142 "function, it is broken and must be fixed.\n",
147 static const void *device_namespace(struct kobject
*kobj
)
149 struct device
*dev
= to_dev(kobj
);
150 const void *ns
= NULL
;
152 if (dev
->class && dev
->class->ns_type
)
153 ns
= dev
->class->namespace(dev
);
158 static struct kobj_type device_ktype
= {
159 .release
= device_release
,
160 .sysfs_ops
= &dev_sysfs_ops
,
161 .namespace = device_namespace
,
165 static int dev_uevent_filter(struct kset
*kset
, struct kobject
*kobj
)
167 struct kobj_type
*ktype
= get_ktype(kobj
);
169 if (ktype
== &device_ktype
) {
170 struct device
*dev
= to_dev(kobj
);
179 static const char *dev_uevent_name(struct kset
*kset
, struct kobject
*kobj
)
181 struct device
*dev
= to_dev(kobj
);
184 return dev
->bus
->name
;
186 return dev
->class->name
;
190 static int dev_uevent(struct kset
*kset
, struct kobject
*kobj
,
191 struct kobj_uevent_env
*env
)
193 struct device
*dev
= to_dev(kobj
);
196 /* add device node properties if present */
197 if (MAJOR(dev
->devt
)) {
202 add_uevent_var(env
, "MAJOR=%u", MAJOR(dev
->devt
));
203 add_uevent_var(env
, "MINOR=%u", MINOR(dev
->devt
));
204 name
= device_get_devnode(dev
, &mode
, &tmp
);
206 add_uevent_var(env
, "DEVNAME=%s", name
);
209 add_uevent_var(env
, "DEVMODE=%#o", mode
& 0777);
213 if (dev
->type
&& dev
->type
->name
)
214 add_uevent_var(env
, "DEVTYPE=%s", dev
->type
->name
);
217 add_uevent_var(env
, "DRIVER=%s", dev
->driver
->name
);
219 /* have the bus specific function add its stuff */
220 if (dev
->bus
&& dev
->bus
->uevent
) {
221 retval
= dev
->bus
->uevent(dev
, env
);
223 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
224 dev_name(dev
), __func__
, retval
);
227 /* have the class specific function add its stuff */
228 if (dev
->class && dev
->class->dev_uevent
) {
229 retval
= dev
->class->dev_uevent(dev
, env
);
231 pr_debug("device: '%s': %s: class uevent() "
232 "returned %d\n", dev_name(dev
),
236 /* have the device type specific fuction add its stuff */
237 if (dev
->type
&& dev
->type
->uevent
) {
238 retval
= dev
->type
->uevent(dev
, env
);
240 pr_debug("device: '%s': %s: dev_type uevent() "
241 "returned %d\n", dev_name(dev
),
248 static const struct kset_uevent_ops device_uevent_ops
= {
249 .filter
= dev_uevent_filter
,
250 .name
= dev_uevent_name
,
251 .uevent
= dev_uevent
,
254 static ssize_t
show_uevent(struct device
*dev
, struct device_attribute
*attr
,
257 struct kobject
*top_kobj
;
259 struct kobj_uevent_env
*env
= NULL
;
264 /* search the kset, the device belongs to */
265 top_kobj
= &dev
->kobj
;
266 while (!top_kobj
->kset
&& top_kobj
->parent
)
267 top_kobj
= top_kobj
->parent
;
271 kset
= top_kobj
->kset
;
272 if (!kset
->uevent_ops
|| !kset
->uevent_ops
->uevent
)
276 if (kset
->uevent_ops
&& kset
->uevent_ops
->filter
)
277 if (!kset
->uevent_ops
->filter(kset
, &dev
->kobj
))
280 env
= kzalloc(sizeof(struct kobj_uevent_env
), GFP_KERNEL
);
284 /* let the kset specific function add its keys */
285 retval
= kset
->uevent_ops
->uevent(kset
, &dev
->kobj
, env
);
289 /* copy keys to file */
290 for (i
= 0; i
< env
->envp_idx
; i
++)
291 count
+= sprintf(&buf
[count
], "%s\n", env
->envp
[i
]);
297 static ssize_t
store_uevent(struct device
*dev
, struct device_attribute
*attr
,
298 const char *buf
, size_t count
)
300 enum kobject_action action
;
302 if (kobject_action_type(buf
, count
, &action
) == 0)
303 kobject_uevent(&dev
->kobj
, action
);
305 dev_err(dev
, "uevent: unknown action-string\n");
309 static struct device_attribute uevent_attr
=
310 __ATTR(uevent
, S_IRUGO
| S_IWUSR
, show_uevent
, store_uevent
);
312 static int device_add_attributes(struct device
*dev
,
313 struct device_attribute
*attrs
)
319 for (i
= 0; attr_name(attrs
[i
]); i
++) {
320 error
= device_create_file(dev
, &attrs
[i
]);
326 device_remove_file(dev
, &attrs
[i
]);
331 static void device_remove_attributes(struct device
*dev
,
332 struct device_attribute
*attrs
)
337 for (i
= 0; attr_name(attrs
[i
]); i
++)
338 device_remove_file(dev
, &attrs
[i
]);
341 static int device_add_groups(struct device
*dev
,
342 const struct attribute_group
**groups
)
348 for (i
= 0; groups
[i
]; i
++) {
349 error
= sysfs_create_group(&dev
->kobj
, groups
[i
]);
352 sysfs_remove_group(&dev
->kobj
,
361 static void device_remove_groups(struct device
*dev
,
362 const struct attribute_group
**groups
)
367 for (i
= 0; groups
[i
]; i
++)
368 sysfs_remove_group(&dev
->kobj
, groups
[i
]);
371 static int device_add_attrs(struct device
*dev
)
373 struct class *class = dev
->class;
374 struct device_type
*type
= dev
->type
;
378 error
= device_add_attributes(dev
, class->dev_attrs
);
384 error
= device_add_groups(dev
, type
->groups
);
386 goto err_remove_class_attrs
;
389 error
= device_add_groups(dev
, dev
->groups
);
391 goto err_remove_type_groups
;
395 err_remove_type_groups
:
397 device_remove_groups(dev
, type
->groups
);
398 err_remove_class_attrs
:
400 device_remove_attributes(dev
, class->dev_attrs
);
405 static void device_remove_attrs(struct device
*dev
)
407 struct class *class = dev
->class;
408 struct device_type
*type
= dev
->type
;
410 device_remove_groups(dev
, dev
->groups
);
413 device_remove_groups(dev
, type
->groups
);
416 device_remove_attributes(dev
, class->dev_attrs
);
420 static ssize_t
show_dev(struct device
*dev
, struct device_attribute
*attr
,
423 return print_dev_t(buf
, dev
->devt
);
426 static struct device_attribute devt_attr
=
427 __ATTR(dev
, S_IRUGO
, show_dev
, NULL
);
429 /* kset to create /sys/devices/ */
430 struct kset
*devices_kset
;
433 * device_create_file - create sysfs attribute file for device.
435 * @attr: device attribute descriptor.
437 int device_create_file(struct device
*dev
,
438 const struct device_attribute
*attr
)
442 error
= sysfs_create_file(&dev
->kobj
, &attr
->attr
);
447 * device_remove_file - remove sysfs attribute file.
449 * @attr: device attribute descriptor.
451 void device_remove_file(struct device
*dev
,
452 const struct device_attribute
*attr
)
455 sysfs_remove_file(&dev
->kobj
, &attr
->attr
);
459 * device_create_bin_file - create sysfs binary attribute file for device.
461 * @attr: device binary attribute descriptor.
463 int device_create_bin_file(struct device
*dev
,
464 const struct bin_attribute
*attr
)
468 error
= sysfs_create_bin_file(&dev
->kobj
, attr
);
471 EXPORT_SYMBOL_GPL(device_create_bin_file
);
474 * device_remove_bin_file - remove sysfs binary attribute file
476 * @attr: device binary attribute descriptor.
478 void device_remove_bin_file(struct device
*dev
,
479 const struct bin_attribute
*attr
)
482 sysfs_remove_bin_file(&dev
->kobj
, attr
);
484 EXPORT_SYMBOL_GPL(device_remove_bin_file
);
487 * device_schedule_callback_owner - helper to schedule a callback for a device
489 * @func: callback function to invoke later.
490 * @owner: module owning the callback routine
492 * Attribute methods must not unregister themselves or their parent device
493 * (which would amount to the same thing). Attempts to do so will deadlock,
494 * since unregistration is mutually exclusive with driver callbacks.
496 * Instead methods can call this routine, which will attempt to allocate
497 * and schedule a workqueue request to call back @func with @dev as its
498 * argument in the workqueue's process context. @dev will be pinned until
501 * This routine is usually called via the inline device_schedule_callback(),
502 * which automatically sets @owner to THIS_MODULE.
504 * Returns 0 if the request was submitted, -ENOMEM if storage could not
505 * be allocated, -ENODEV if a reference to @owner isn't available.
507 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
508 * underlying sysfs routine (since it is intended for use by attribute
509 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
511 int device_schedule_callback_owner(struct device
*dev
,
512 void (*func
)(struct device
*), struct module
*owner
)
514 return sysfs_schedule_callback(&dev
->kobj
,
515 (void (*)(void *)) func
, dev
, owner
);
517 EXPORT_SYMBOL_GPL(device_schedule_callback_owner
);
519 static void klist_children_get(struct klist_node
*n
)
521 struct device_private
*p
= to_device_private_parent(n
);
522 struct device
*dev
= p
->device
;
527 static void klist_children_put(struct klist_node
*n
)
529 struct device_private
*p
= to_device_private_parent(n
);
530 struct device
*dev
= p
->device
;
536 * device_initialize - init device structure.
539 * This prepares the device for use by other layers by initializing
541 * It is the first half of device_register(), if called by
542 * that function, though it can also be called separately, so one
543 * may use @dev's fields. In particular, get_device()/put_device()
544 * may be used for reference counting of @dev after calling this
547 * NOTE: Use put_device() to give up your reference instead of freeing
548 * @dev directly once you have called this function.
550 void device_initialize(struct device
*dev
)
552 dev
->kobj
.kset
= devices_kset
;
553 kobject_init(&dev
->kobj
, &device_ktype
);
554 INIT_LIST_HEAD(&dev
->dma_pools
);
555 mutex_init(&dev
->mutex
);
556 lockdep_set_novalidate_class(&dev
->mutex
);
557 spin_lock_init(&dev
->devres_lock
);
558 INIT_LIST_HEAD(&dev
->devres_head
);
560 set_dev_node(dev
, -1);
563 static struct kobject
*virtual_device_parent(struct device
*dev
)
565 static struct kobject
*virtual_dir
= NULL
;
568 virtual_dir
= kobject_create_and_add("virtual",
569 &devices_kset
->kobj
);
579 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
581 static void class_dir_release(struct kobject
*kobj
)
583 struct class_dir
*dir
= to_class_dir(kobj
);
588 struct kobj_ns_type_operations
*class_dir_child_ns_type(struct kobject
*kobj
)
590 struct class_dir
*dir
= to_class_dir(kobj
);
591 return dir
->class->ns_type
;
594 static struct kobj_type class_dir_ktype
= {
595 .release
= class_dir_release
,
596 .sysfs_ops
= &kobj_sysfs_ops
,
597 .child_ns_type
= class_dir_child_ns_type
600 static struct kobject
*
601 class_dir_create_and_add(struct class *class, struct kobject
*parent_kobj
)
603 struct class_dir
*dir
;
606 dir
= kzalloc(sizeof(*dir
), GFP_KERNEL
);
611 kobject_init(&dir
->kobj
, &class_dir_ktype
);
613 dir
->kobj
.kset
= &class->p
->class_dirs
;
615 retval
= kobject_add(&dir
->kobj
, parent_kobj
, "%s", class->name
);
617 kobject_put(&dir
->kobj
);
624 static struct kobject
*get_device_parent(struct device
*dev
,
625 struct device
*parent
)
628 static DEFINE_MUTEX(gdp_mutex
);
629 struct kobject
*kobj
= NULL
;
630 struct kobject
*parent_kobj
;
633 /* block disks show up in /sys/block */
634 if (sysfs_deprecated
&& dev
->class == &block_class
) {
635 if (parent
&& parent
->class == &block_class
)
636 return &parent
->kobj
;
637 return &block_class
.p
->class_subsys
.kobj
;
641 * If we have no parent, we live in "virtual".
642 * Class-devices with a non class-device as parent, live
643 * in a "glue" directory to prevent namespace collisions.
646 parent_kobj
= virtual_device_parent(dev
);
647 else if (parent
->class && !dev
->class->ns_type
)
648 return &parent
->kobj
;
650 parent_kobj
= &parent
->kobj
;
652 mutex_lock(&gdp_mutex
);
654 /* find our class-directory at the parent and reference it */
655 spin_lock(&dev
->class->p
->class_dirs
.list_lock
);
656 list_for_each_entry(k
, &dev
->class->p
->class_dirs
.list
, entry
)
657 if (k
->parent
== parent_kobj
) {
658 kobj
= kobject_get(k
);
661 spin_unlock(&dev
->class->p
->class_dirs
.list_lock
);
663 mutex_unlock(&gdp_mutex
);
667 /* or create a new class-directory at the parent device */
668 k
= class_dir_create_and_add(dev
->class, parent_kobj
);
669 /* do not emit an uevent for this simple "glue" directory */
670 mutex_unlock(&gdp_mutex
);
675 return &parent
->kobj
;
679 static void cleanup_glue_dir(struct device
*dev
, struct kobject
*glue_dir
)
681 /* see if we live in a "glue" directory */
682 if (!glue_dir
|| !dev
->class ||
683 glue_dir
->kset
!= &dev
->class->p
->class_dirs
)
686 kobject_put(glue_dir
);
689 static void cleanup_device_parent(struct device
*dev
)
691 cleanup_glue_dir(dev
, dev
->kobj
.parent
);
694 static void setup_parent(struct device
*dev
, struct device
*parent
)
696 struct kobject
*kobj
;
697 kobj
= get_device_parent(dev
, parent
);
699 dev
->kobj
.parent
= kobj
;
702 static int device_add_class_symlinks(struct device
*dev
)
709 error
= sysfs_create_link(&dev
->kobj
,
710 &dev
->class->p
->class_subsys
.kobj
,
715 if (dev
->parent
&& device_is_not_partition(dev
)) {
716 error
= sysfs_create_link(&dev
->kobj
, &dev
->parent
->kobj
,
722 /* /sys/block has directories and does not need symlinks */
723 if (sysfs_deprecated
&& dev
->class == &block_class
)
726 /* link in the class directory pointing to the device */
727 error
= sysfs_create_link(&dev
->class->p
->class_subsys
.kobj
,
728 &dev
->kobj
, dev_name(dev
));
735 sysfs_remove_link(&dev
->kobj
, "device");
738 sysfs_remove_link(&dev
->kobj
, "subsystem");
743 static void device_remove_class_symlinks(struct device
*dev
)
748 if (dev
->parent
&& device_is_not_partition(dev
))
749 sysfs_remove_link(&dev
->kobj
, "device");
750 sysfs_remove_link(&dev
->kobj
, "subsystem");
751 if (sysfs_deprecated
&& dev
->class == &block_class
)
753 sysfs_delete_link(&dev
->class->p
->class_subsys
.kobj
, &dev
->kobj
, dev_name(dev
));
757 * dev_set_name - set a device name
759 * @fmt: format string for the device's name
761 int dev_set_name(struct device
*dev
, const char *fmt
, ...)
766 va_start(vargs
, fmt
);
767 err
= kobject_set_name_vargs(&dev
->kobj
, fmt
, vargs
);
771 EXPORT_SYMBOL_GPL(dev_set_name
);
774 * device_to_dev_kobj - select a /sys/dev/ directory for the device
777 * By default we select char/ for new entries. Setting class->dev_obj
778 * to NULL prevents an entry from being created. class->dev_kobj must
779 * be set (or cleared) before any devices are registered to the class
780 * otherwise device_create_sys_dev_entry() and
781 * device_remove_sys_dev_entry() will disagree about the the presence
784 static struct kobject
*device_to_dev_kobj(struct device
*dev
)
786 struct kobject
*kobj
;
789 kobj
= dev
->class->dev_kobj
;
791 kobj
= sysfs_dev_char_kobj
;
796 static int device_create_sys_dev_entry(struct device
*dev
)
798 struct kobject
*kobj
= device_to_dev_kobj(dev
);
803 format_dev_t(devt_str
, dev
->devt
);
804 error
= sysfs_create_link(kobj
, &dev
->kobj
, devt_str
);
810 static void device_remove_sys_dev_entry(struct device
*dev
)
812 struct kobject
*kobj
= device_to_dev_kobj(dev
);
816 format_dev_t(devt_str
, dev
->devt
);
817 sysfs_remove_link(kobj
, devt_str
);
821 int device_private_init(struct device
*dev
)
823 dev
->p
= kzalloc(sizeof(*dev
->p
), GFP_KERNEL
);
826 dev
->p
->device
= dev
;
827 klist_init(&dev
->p
->klist_children
, klist_children_get
,
833 * device_add - add device to device hierarchy.
836 * This is part 2 of device_register(), though may be called
837 * separately _iff_ device_initialize() has been called separately.
839 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
840 * to the global and sibling lists for the device, then
841 * adds it to the other relevant subsystems of the driver model.
843 * NOTE: _Never_ directly free @dev after calling this function, even
844 * if it returned an error! Always use put_device() to give up your
847 int device_add(struct device
*dev
)
849 struct device
*parent
= NULL
;
850 struct class_interface
*class_intf
;
853 dev
= get_device(dev
);
858 error
= device_private_init(dev
);
864 * for statically allocated devices, which should all be converted
865 * some day, we need to initialize the name. We prevent reading back
866 * the name, and force the use of dev_name()
868 if (dev
->init_name
) {
869 dev_set_name(dev
, "%s", dev
->init_name
);
870 dev
->init_name
= NULL
;
873 if (!dev_name(dev
)) {
878 pr_debug("device: '%s': %s\n", dev_name(dev
), __func__
);
880 parent
= get_device(dev
->parent
);
881 setup_parent(dev
, parent
);
883 /* use parent numa_node */
885 set_dev_node(dev
, dev_to_node(parent
));
887 /* first, register with generic layer. */
888 /* we require the name to be set before, and pass NULL */
889 error
= kobject_add(&dev
->kobj
, dev
->kobj
.parent
, NULL
);
893 /* notify platform of device entry */
895 platform_notify(dev
);
897 error
= device_create_file(dev
, &uevent_attr
);
901 if (MAJOR(dev
->devt
)) {
902 error
= device_create_file(dev
, &devt_attr
);
904 goto ueventattrError
;
906 error
= device_create_sys_dev_entry(dev
);
910 devtmpfs_create_node(dev
);
913 error
= device_add_class_symlinks(dev
);
916 error
= device_add_attrs(dev
);
919 error
= bus_add_device(dev
);
922 error
= dpm_sysfs_add(dev
);
927 /* Notify clients of device addition. This call must come
928 * after dpm_sysf_add() and before kobject_uevent().
931 blocking_notifier_call_chain(&dev
->bus
->p
->bus_notifier
,
932 BUS_NOTIFY_ADD_DEVICE
, dev
);
934 kobject_uevent(&dev
->kobj
, KOBJ_ADD
);
935 bus_probe_device(dev
);
937 klist_add_tail(&dev
->p
->knode_parent
,
938 &parent
->p
->klist_children
);
941 mutex_lock(&dev
->class->p
->class_mutex
);
942 /* tie the class to the device */
943 klist_add_tail(&dev
->knode_class
,
944 &dev
->class->p
->class_devices
);
946 /* notify any interfaces that the device is here */
947 list_for_each_entry(class_intf
,
948 &dev
->class->p
->class_interfaces
, node
)
949 if (class_intf
->add_dev
)
950 class_intf
->add_dev(dev
, class_intf
);
951 mutex_unlock(&dev
->class->p
->class_mutex
);
957 bus_remove_device(dev
);
959 device_remove_attrs(dev
);
961 device_remove_class_symlinks(dev
);
963 if (MAJOR(dev
->devt
))
964 devtmpfs_delete_node(dev
);
965 if (MAJOR(dev
->devt
))
966 device_remove_sys_dev_entry(dev
);
968 if (MAJOR(dev
->devt
))
969 device_remove_file(dev
, &devt_attr
);
971 device_remove_file(dev
, &uevent_attr
);
973 kobject_uevent(&dev
->kobj
, KOBJ_REMOVE
);
974 kobject_del(&dev
->kobj
);
976 cleanup_device_parent(dev
);
986 * device_register - register a device with the system.
987 * @dev: pointer to the device structure
989 * This happens in two clean steps - initialize the device
990 * and add it to the system. The two steps can be called
991 * separately, but this is the easiest and most common.
992 * I.e. you should only call the two helpers separately if
993 * have a clearly defined need to use and refcount the device
994 * before it is added to the hierarchy.
996 * NOTE: _Never_ directly free @dev after calling this function, even
997 * if it returned an error! Always use put_device() to give up the
998 * reference initialized in this function instead.
1000 int device_register(struct device
*dev
)
1002 device_initialize(dev
);
1003 return device_add(dev
);
1007 * get_device - increment reference count for device.
1010 * This simply forwards the call to kobject_get(), though
1011 * we do take care to provide for the case that we get a NULL
1012 * pointer passed in.
1014 struct device
*get_device(struct device
*dev
)
1016 return dev
? to_dev(kobject_get(&dev
->kobj
)) : NULL
;
1020 * put_device - decrement reference count.
1021 * @dev: device in question.
1023 void put_device(struct device
*dev
)
1025 /* might_sleep(); */
1027 kobject_put(&dev
->kobj
);
1031 * device_del - delete device from system.
1034 * This is the first part of the device unregistration
1035 * sequence. This removes the device from the lists we control
1036 * from here, has it removed from the other driver model
1037 * subsystems it was added to in device_add(), and removes it
1038 * from the kobject hierarchy.
1040 * NOTE: this should be called manually _iff_ device_add() was
1041 * also called manually.
1043 void device_del(struct device
*dev
)
1045 struct device
*parent
= dev
->parent
;
1046 struct class_interface
*class_intf
;
1048 /* Notify clients of device removal. This call must come
1049 * before dpm_sysfs_remove().
1052 blocking_notifier_call_chain(&dev
->bus
->p
->bus_notifier
,
1053 BUS_NOTIFY_DEL_DEVICE
, dev
);
1054 device_pm_remove(dev
);
1055 dpm_sysfs_remove(dev
);
1057 klist_del(&dev
->p
->knode_parent
);
1058 if (MAJOR(dev
->devt
)) {
1059 devtmpfs_delete_node(dev
);
1060 device_remove_sys_dev_entry(dev
);
1061 device_remove_file(dev
, &devt_attr
);
1064 device_remove_class_symlinks(dev
);
1066 mutex_lock(&dev
->class->p
->class_mutex
);
1067 /* notify any interfaces that the device is now gone */
1068 list_for_each_entry(class_intf
,
1069 &dev
->class->p
->class_interfaces
, node
)
1070 if (class_intf
->remove_dev
)
1071 class_intf
->remove_dev(dev
, class_intf
);
1072 /* remove the device from the class list */
1073 klist_del(&dev
->knode_class
);
1074 mutex_unlock(&dev
->class->p
->class_mutex
);
1076 device_remove_file(dev
, &uevent_attr
);
1077 device_remove_attrs(dev
);
1078 bus_remove_device(dev
);
1081 * Some platform devices are driven without driver attached
1082 * and managed resources may have been acquired. Make sure
1083 * all resources are released.
1085 devres_release_all(dev
);
1087 /* Notify the platform of the removal, in case they
1088 * need to do anything...
1090 if (platform_notify_remove
)
1091 platform_notify_remove(dev
);
1092 kobject_uevent(&dev
->kobj
, KOBJ_REMOVE
);
1093 cleanup_device_parent(dev
);
1094 kobject_del(&dev
->kobj
);
1099 * device_unregister - unregister device from system.
1100 * @dev: device going away.
1102 * We do this in two parts, like we do device_register(). First,
1103 * we remove it from all the subsystems with device_del(), then
1104 * we decrement the reference count via put_device(). If that
1105 * is the final reference count, the device will be cleaned up
1106 * via device_release() above. Otherwise, the structure will
1107 * stick around until the final reference to the device is dropped.
1109 void device_unregister(struct device
*dev
)
1111 pr_debug("device: '%s': %s\n", dev_name(dev
), __func__
);
1116 static struct device
*next_device(struct klist_iter
*i
)
1118 struct klist_node
*n
= klist_next(i
);
1119 struct device
*dev
= NULL
;
1120 struct device_private
*p
;
1123 p
= to_device_private_parent(n
);
1130 * device_get_devnode - path of device node file
1132 * @mode: returned file access mode
1133 * @tmp: possibly allocated string
1135 * Return the relative path of a possible device node.
1136 * Non-default names may need to allocate a memory to compose
1137 * a name. This memory is returned in tmp and needs to be
1138 * freed by the caller.
1140 const char *device_get_devnode(struct device
*dev
,
1141 mode_t
*mode
, const char **tmp
)
1147 /* the device type may provide a specific name */
1148 if (dev
->type
&& dev
->type
->devnode
)
1149 *tmp
= dev
->type
->devnode(dev
, mode
);
1153 /* the class may provide a specific name */
1154 if (dev
->class && dev
->class->devnode
)
1155 *tmp
= dev
->class->devnode(dev
, mode
);
1159 /* return name without allocation, tmp == NULL */
1160 if (strchr(dev_name(dev
), '!') == NULL
)
1161 return dev_name(dev
);
1163 /* replace '!' in the name with '/' */
1164 *tmp
= kstrdup(dev_name(dev
), GFP_KERNEL
);
1167 while ((s
= strchr(*tmp
, '!')))
1173 * device_for_each_child - device child iterator.
1174 * @parent: parent struct device.
1175 * @data: data for the callback.
1176 * @fn: function to be called for each device.
1178 * Iterate over @parent's child devices, and call @fn for each,
1181 * We check the return of @fn each time. If it returns anything
1182 * other than 0, we break out and return that value.
1184 int device_for_each_child(struct device
*parent
, void *data
,
1185 int (*fn
)(struct device
*dev
, void *data
))
1187 struct klist_iter i
;
1188 struct device
*child
;
1194 klist_iter_init(&parent
->p
->klist_children
, &i
);
1195 while ((child
= next_device(&i
)) && !error
)
1196 error
= fn(child
, data
);
1197 klist_iter_exit(&i
);
1202 * device_find_child - device iterator for locating a particular device.
1203 * @parent: parent struct device
1204 * @data: Data to pass to match function
1205 * @match: Callback function to check device
1207 * This is similar to the device_for_each_child() function above, but it
1208 * returns a reference to a device that is 'found' for later use, as
1209 * determined by the @match callback.
1211 * The callback should return 0 if the device doesn't match and non-zero
1212 * if it does. If the callback returns non-zero and a reference to the
1213 * current device can be obtained, this function will return to the caller
1214 * and not iterate over any more devices.
1216 struct device
*device_find_child(struct device
*parent
, void *data
,
1217 int (*match
)(struct device
*dev
, void *data
))
1219 struct klist_iter i
;
1220 struct device
*child
;
1225 klist_iter_init(&parent
->p
->klist_children
, &i
);
1226 while ((child
= next_device(&i
)))
1227 if (match(child
, data
) && get_device(child
))
1229 klist_iter_exit(&i
);
1233 int __init
devices_init(void)
1235 devices_kset
= kset_create_and_add("devices", &device_uevent_ops
, NULL
);
1238 dev_kobj
= kobject_create_and_add("dev", NULL
);
1241 sysfs_dev_block_kobj
= kobject_create_and_add("block", dev_kobj
);
1242 if (!sysfs_dev_block_kobj
)
1243 goto block_kobj_err
;
1244 sysfs_dev_char_kobj
= kobject_create_and_add("char", dev_kobj
);
1245 if (!sysfs_dev_char_kobj
)
1251 kobject_put(sysfs_dev_block_kobj
);
1253 kobject_put(dev_kobj
);
1255 kset_unregister(devices_kset
);
1259 EXPORT_SYMBOL_GPL(device_for_each_child
);
1260 EXPORT_SYMBOL_GPL(device_find_child
);
1262 EXPORT_SYMBOL_GPL(device_initialize
);
1263 EXPORT_SYMBOL_GPL(device_add
);
1264 EXPORT_SYMBOL_GPL(device_register
);
1266 EXPORT_SYMBOL_GPL(device_del
);
1267 EXPORT_SYMBOL_GPL(device_unregister
);
1268 EXPORT_SYMBOL_GPL(get_device
);
1269 EXPORT_SYMBOL_GPL(put_device
);
1271 EXPORT_SYMBOL_GPL(device_create_file
);
1272 EXPORT_SYMBOL_GPL(device_remove_file
);
1277 struct module
*owner
;
1280 #define to_root_device(dev) container_of(dev, struct root_device, dev)
1282 static void root_device_release(struct device
*dev
)
1284 kfree(to_root_device(dev
));
1288 * __root_device_register - allocate and register a root device
1289 * @name: root device name
1290 * @owner: owner module of the root device, usually THIS_MODULE
1292 * This function allocates a root device and registers it
1293 * using device_register(). In order to free the returned
1294 * device, use root_device_unregister().
1296 * Root devices are dummy devices which allow other devices
1297 * to be grouped under /sys/devices. Use this function to
1298 * allocate a root device and then use it as the parent of
1299 * any device which should appear under /sys/devices/{name}
1301 * The /sys/devices/{name} directory will also contain a
1302 * 'module' symlink which points to the @owner directory
1305 * Returns &struct device pointer on success, or ERR_PTR() on error.
1307 * Note: You probably want to use root_device_register().
1309 struct device
*__root_device_register(const char *name
, struct module
*owner
)
1311 struct root_device
*root
;
1314 root
= kzalloc(sizeof(struct root_device
), GFP_KERNEL
);
1316 return ERR_PTR(err
);
1318 err
= dev_set_name(&root
->dev
, "%s", name
);
1321 return ERR_PTR(err
);
1324 root
->dev
.release
= root_device_release
;
1326 err
= device_register(&root
->dev
);
1328 put_device(&root
->dev
);
1329 return ERR_PTR(err
);
1332 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
1334 struct module_kobject
*mk
= &owner
->mkobj
;
1336 err
= sysfs_create_link(&root
->dev
.kobj
, &mk
->kobj
, "module");
1338 device_unregister(&root
->dev
);
1339 return ERR_PTR(err
);
1341 root
->owner
= owner
;
1347 EXPORT_SYMBOL_GPL(__root_device_register
);
1350 * root_device_unregister - unregister and free a root device
1351 * @dev: device going away
1353 * This function unregisters and cleans up a device that was created by
1354 * root_device_register().
1356 void root_device_unregister(struct device
*dev
)
1358 struct root_device
*root
= to_root_device(dev
);
1361 sysfs_remove_link(&root
->dev
.kobj
, "module");
1363 device_unregister(dev
);
1365 EXPORT_SYMBOL_GPL(root_device_unregister
);
1368 static void device_create_release(struct device
*dev
)
1370 pr_debug("device: '%s': %s\n", dev_name(dev
), __func__
);
1375 * device_create_vargs - creates a device and registers it with sysfs
1376 * @class: pointer to the struct class that this device should be registered to
1377 * @parent: pointer to the parent struct device of this new device, if any
1378 * @devt: the dev_t for the char device to be added
1379 * @drvdata: the data to be added to the device for callbacks
1380 * @fmt: string for the device's name
1381 * @args: va_list for the device's name
1383 * This function can be used by char device classes. A struct device
1384 * will be created in sysfs, registered to the specified class.
1386 * A "dev" file will be created, showing the dev_t for the device, if
1387 * the dev_t is not 0,0.
1388 * If a pointer to a parent struct device is passed in, the newly created
1389 * struct device will be a child of that device in sysfs.
1390 * The pointer to the struct device will be returned from the call.
1391 * Any further sysfs files that might be required can be created using this
1394 * Returns &struct device pointer on success, or ERR_PTR() on error.
1396 * Note: the struct class passed to this function must have previously
1397 * been created with a call to class_create().
1399 struct device
*device_create_vargs(struct class *class, struct device
*parent
,
1400 dev_t devt
, void *drvdata
, const char *fmt
,
1403 struct device
*dev
= NULL
;
1404 int retval
= -ENODEV
;
1406 if (class == NULL
|| IS_ERR(class))
1409 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1417 dev
->parent
= parent
;
1418 dev
->release
= device_create_release
;
1419 dev_set_drvdata(dev
, drvdata
);
1421 retval
= kobject_set_name_vargs(&dev
->kobj
, fmt
, args
);
1425 retval
= device_register(dev
);
1433 return ERR_PTR(retval
);
1435 EXPORT_SYMBOL_GPL(device_create_vargs
);
1438 * device_create - creates a device and registers it with sysfs
1439 * @class: pointer to the struct class that this device should be registered to
1440 * @parent: pointer to the parent struct device of this new device, if any
1441 * @devt: the dev_t for the char device to be added
1442 * @drvdata: the data to be added to the device for callbacks
1443 * @fmt: string for the device's name
1445 * This function can be used by char device classes. A struct device
1446 * will be created in sysfs, registered to the specified class.
1448 * A "dev" file will be created, showing the dev_t for the device, if
1449 * the dev_t is not 0,0.
1450 * If a pointer to a parent struct device is passed in, the newly created
1451 * struct device will be a child of that device in sysfs.
1452 * The pointer to the struct device will be returned from the call.
1453 * Any further sysfs files that might be required can be created using this
1456 * Returns &struct device pointer on success, or ERR_PTR() on error.
1458 * Note: the struct class passed to this function must have previously
1459 * been created with a call to class_create().
1461 struct device
*device_create(struct class *class, struct device
*parent
,
1462 dev_t devt
, void *drvdata
, const char *fmt
, ...)
1467 va_start(vargs
, fmt
);
1468 dev
= device_create_vargs(class, parent
, devt
, drvdata
, fmt
, vargs
);
1472 EXPORT_SYMBOL_GPL(device_create
);
1474 static int __match_devt(struct device
*dev
, void *data
)
1478 return dev
->devt
== *devt
;
1482 * device_destroy - removes a device that was created with device_create()
1483 * @class: pointer to the struct class that this device was registered with
1484 * @devt: the dev_t of the device that was previously registered
1486 * This call unregisters and cleans up a device that was created with a
1487 * call to device_create().
1489 void device_destroy(struct class *class, dev_t devt
)
1493 dev
= class_find_device(class, NULL
, &devt
, __match_devt
);
1496 device_unregister(dev
);
1499 EXPORT_SYMBOL_GPL(device_destroy
);
1502 * device_rename - renames a device
1503 * @dev: the pointer to the struct device to be renamed
1504 * @new_name: the new name of the device
1506 * It is the responsibility of the caller to provide mutual
1507 * exclusion between two different calls of device_rename
1508 * on the same device to ensure that new_name is valid and
1509 * won't conflict with other devices.
1511 int device_rename(struct device
*dev
, const char *new_name
)
1513 char *old_class_name
= NULL
;
1514 char *new_class_name
= NULL
;
1515 char *old_device_name
= NULL
;
1518 dev
= get_device(dev
);
1522 pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev
),
1523 __func__
, new_name
);
1525 old_device_name
= kstrdup(dev_name(dev
), GFP_KERNEL
);
1526 if (!old_device_name
) {
1532 error
= sysfs_rename_link(&dev
->class->p
->class_subsys
.kobj
,
1533 &dev
->kobj
, old_device_name
, new_name
);
1538 error
= kobject_rename(&dev
->kobj
, new_name
);
1545 kfree(new_class_name
);
1546 kfree(old_class_name
);
1547 kfree(old_device_name
);
1551 EXPORT_SYMBOL_GPL(device_rename
);
1553 static int device_move_class_links(struct device
*dev
,
1554 struct device
*old_parent
,
1555 struct device
*new_parent
)
1560 sysfs_remove_link(&dev
->kobj
, "device");
1562 error
= sysfs_create_link(&dev
->kobj
, &new_parent
->kobj
,
1568 * device_move - moves a device to a new parent
1569 * @dev: the pointer to the struct device to be moved
1570 * @new_parent: the new parent of the device (can by NULL)
1571 * @dpm_order: how to reorder the dpm_list
1573 int device_move(struct device
*dev
, struct device
*new_parent
,
1574 enum dpm_order dpm_order
)
1577 struct device
*old_parent
;
1578 struct kobject
*new_parent_kobj
;
1580 dev
= get_device(dev
);
1585 new_parent
= get_device(new_parent
);
1586 new_parent_kobj
= get_device_parent(dev
, new_parent
);
1588 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev
),
1589 __func__
, new_parent
? dev_name(new_parent
) : "<NULL>");
1590 error
= kobject_move(&dev
->kobj
, new_parent_kobj
);
1592 cleanup_glue_dir(dev
, new_parent_kobj
);
1593 put_device(new_parent
);
1596 old_parent
= dev
->parent
;
1597 dev
->parent
= new_parent
;
1599 klist_remove(&dev
->p
->knode_parent
);
1601 klist_add_tail(&dev
->p
->knode_parent
,
1602 &new_parent
->p
->klist_children
);
1603 set_dev_node(dev
, dev_to_node(new_parent
));
1608 error
= device_move_class_links(dev
, old_parent
, new_parent
);
1610 /* We ignore errors on cleanup since we're hosed anyway... */
1611 device_move_class_links(dev
, new_parent
, old_parent
);
1612 if (!kobject_move(&dev
->kobj
, &old_parent
->kobj
)) {
1614 klist_remove(&dev
->p
->knode_parent
);
1615 dev
->parent
= old_parent
;
1617 klist_add_tail(&dev
->p
->knode_parent
,
1618 &old_parent
->p
->klist_children
);
1619 set_dev_node(dev
, dev_to_node(old_parent
));
1622 cleanup_glue_dir(dev
, new_parent_kobj
);
1623 put_device(new_parent
);
1626 switch (dpm_order
) {
1627 case DPM_ORDER_NONE
:
1629 case DPM_ORDER_DEV_AFTER_PARENT
:
1630 device_pm_move_after(dev
, new_parent
);
1632 case DPM_ORDER_PARENT_BEFORE_DEV
:
1633 device_pm_move_before(new_parent
, dev
);
1635 case DPM_ORDER_DEV_LAST
:
1636 device_pm_move_last(dev
);
1640 put_device(old_parent
);
1646 EXPORT_SYMBOL_GPL(device_move
);
1649 * device_shutdown - call ->shutdown() on each device to shutdown.
1651 void device_shutdown(void)
1655 spin_lock(&devices_kset
->list_lock
);
1657 * Walk the devices list backward, shutting down each in turn.
1658 * Beware that device unplug events may also start pulling
1659 * devices offline, even as the system is shutting down.
1661 while (!list_empty(&devices_kset
->list
)) {
1662 dev
= list_entry(devices_kset
->list
.prev
, struct device
,
1666 * Make sure the device is off the kset list, in the
1667 * event that dev->*->shutdown() doesn't remove it.
1669 list_del_init(&dev
->kobj
.entry
);
1670 spin_unlock(&devices_kset
->list_lock
);
1672 if (dev
->bus
&& dev
->bus
->shutdown
) {
1673 dev_dbg(dev
, "shutdown\n");
1674 dev
->bus
->shutdown(dev
);
1675 } else if (dev
->driver
&& dev
->driver
->shutdown
) {
1676 dev_dbg(dev
, "shutdown\n");
1677 dev
->driver
->shutdown(dev
);
1681 spin_lock(&devices_kset
->list_lock
);
1683 spin_unlock(&devices_kset
->list_lock
);
1684 async_synchronize_full();
1688 * Device logging functions
1691 #ifdef CONFIG_PRINTK
1693 static int __dev_printk(const char *level
, const struct device
*dev
,
1694 struct va_format
*vaf
)
1697 return printk("%s(NULL device *): %pV", level
, vaf
);
1699 return printk("%s%s %s: %pV",
1700 level
, dev_driver_string(dev
), dev_name(dev
), vaf
);
1703 int dev_printk(const char *level
, const struct device
*dev
,
1704 const char *fmt
, ...)
1706 struct va_format vaf
;
1710 va_start(args
, fmt
);
1715 r
= __dev_printk(level
, dev
, &vaf
);
1720 EXPORT_SYMBOL(dev_printk
);
1722 #define define_dev_printk_level(func, kern_level) \
1723 int func(const struct device *dev, const char *fmt, ...) \
1725 struct va_format vaf; \
1729 va_start(args, fmt); \
1734 r = __dev_printk(kern_level, dev, &vaf); \
1739 EXPORT_SYMBOL(func);
1741 define_dev_printk_level(dev_emerg
, KERN_EMERG
);
1742 define_dev_printk_level(dev_alert
, KERN_ALERT
);
1743 define_dev_printk_level(dev_crit
, KERN_CRIT
);
1744 define_dev_printk_level(dev_err
, KERN_ERR
);
1745 define_dev_printk_level(dev_warn
, KERN_WARNING
);
1746 define_dev_printk_level(dev_notice
, KERN_NOTICE
);
1747 define_dev_printk_level(_dev_info
, KERN_INFO
);