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 return dev->driver ? dev->driver->name :
60 (dev->bus ? dev->bus->name :
61 (dev->class ? dev->class->name : ""));
63 EXPORT_SYMBOL(dev_driver_string);
65 #define to_dev(obj) container_of(obj, struct device, kobj)
66 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
68 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
71 struct device_attribute *dev_attr = to_dev_attr(attr);
72 struct device *dev = to_dev(kobj);
76 ret = dev_attr->show(dev, dev_attr, buf);
77 if (ret >= (ssize_t)PAGE_SIZE) {
78 print_symbol("dev_attr_show: %s returned bad count\n",
79 (unsigned long)dev_attr->show);
84 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
85 const char *buf, size_t count)
87 struct device_attribute *dev_attr = to_dev_attr(attr);
88 struct device *dev = to_dev(kobj);
92 ret = dev_attr->store(dev, dev_attr, buf, count);
96 static struct sysfs_ops dev_sysfs_ops = {
97 .show = dev_attr_show,
98 .store = dev_attr_store,
103 * device_release - free device structure.
104 * @kobj: device's kobject.
106 * This is called once the reference count for the object
107 * reaches 0. We forward the call to the device's release
108 * method, which should handle actually freeing the structure.
110 static void device_release(struct kobject *kobj)
112 struct device *dev = to_dev(kobj);
113 struct device_private *p = dev->p;
117 else if (dev->type && dev->type->release)
118 dev->type->release(dev);
119 else if (dev->class && dev->class->dev_release)
120 dev->class->dev_release(dev);
122 WARN(1, KERN_ERR "Device '%s' does not have a release() "
123 "function, it is broken and must be fixed.\n",
128 static struct kobj_type device_ktype = {
129 .release = device_release,
130 .sysfs_ops = &dev_sysfs_ops,
134 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
136 struct kobj_type *ktype = get_ktype(kobj);
138 if (ktype == &device_ktype) {
139 struct device *dev = to_dev(kobj);
148 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
150 struct device *dev = to_dev(kobj);
153 return dev->bus->name;
155 return dev->class->name;
159 static int dev_uevent(struct kset *kset, struct kobject *kobj,
160 struct kobj_uevent_env *env)
162 struct device *dev = to_dev(kobj);
165 /* add device node properties if present */
166 if (MAJOR(dev->devt)) {
171 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
172 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
173 name = device_get_devnode(dev, &mode, &tmp);
175 add_uevent_var(env, "DEVNAME=%s", name);
178 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
182 if (dev->type && dev->type->name)
183 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
186 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
188 #ifdef CONFIG_SYSFS_DEPRECATED
190 struct device *parent = dev->parent;
192 /* find first bus device in parent chain */
193 while (parent && !parent->bus)
194 parent = parent->parent;
195 if (parent && parent->bus) {
198 path = kobject_get_path(&parent->kobj, GFP_KERNEL);
200 add_uevent_var(env, "PHYSDEVPATH=%s", path);
204 add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name);
207 add_uevent_var(env, "PHYSDEVDRIVER=%s",
208 parent->driver->name);
210 } else if (dev->bus) {
211 add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
214 add_uevent_var(env, "PHYSDEVDRIVER=%s",
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 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);
307 dev_err(dev, "uevent: unsupported action-string; this will "
308 "be ignored in a future kernel version\n");
309 kobject_uevent(&dev->kobj, KOBJ_ADD);
314 static struct device_attribute uevent_attr =
315 __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
317 static int device_add_attributes(struct device *dev,
318 struct device_attribute *attrs)
324 for (i = 0; attr_name(attrs[i]); i++) {
325 error = device_create_file(dev, &attrs[i]);
331 device_remove_file(dev, &attrs[i]);
336 static void device_remove_attributes(struct device *dev,
337 struct device_attribute *attrs)
342 for (i = 0; attr_name(attrs[i]); i++)
343 device_remove_file(dev, &attrs[i]);
346 static int device_add_groups(struct device *dev,
347 const struct attribute_group **groups)
353 for (i = 0; groups[i]; i++) {
354 error = sysfs_create_group(&dev->kobj, groups[i]);
357 sysfs_remove_group(&dev->kobj,
366 static void device_remove_groups(struct device *dev,
367 const struct attribute_group **groups)
372 for (i = 0; groups[i]; i++)
373 sysfs_remove_group(&dev->kobj, groups[i]);
376 static int device_add_attrs(struct device *dev)
378 struct class *class = dev->class;
379 struct device_type *type = dev->type;
383 error = device_add_attributes(dev, class->dev_attrs);
389 error = device_add_groups(dev, type->groups);
391 goto err_remove_class_attrs;
394 error = device_add_groups(dev, dev->groups);
396 goto err_remove_type_groups;
400 err_remove_type_groups:
402 device_remove_groups(dev, type->groups);
403 err_remove_class_attrs:
405 device_remove_attributes(dev, class->dev_attrs);
410 static void device_remove_attrs(struct device *dev)
412 struct class *class = dev->class;
413 struct device_type *type = dev->type;
415 device_remove_groups(dev, dev->groups);
418 device_remove_groups(dev, type->groups);
421 device_remove_attributes(dev, class->dev_attrs);
425 static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
428 return print_dev_t(buf, dev->devt);
431 static struct device_attribute devt_attr =
432 __ATTR(dev, S_IRUGO, show_dev, NULL);
434 /* kset to create /sys/devices/ */
435 struct kset *devices_kset;
438 * device_create_file - create sysfs attribute file for device.
440 * @attr: device attribute descriptor.
442 int device_create_file(struct device *dev, struct device_attribute *attr)
446 error = sysfs_create_file(&dev->kobj, &attr->attr);
451 * device_remove_file - remove sysfs attribute file.
453 * @attr: device attribute descriptor.
455 void device_remove_file(struct device *dev, struct device_attribute *attr)
458 sysfs_remove_file(&dev->kobj, &attr->attr);
462 * device_create_bin_file - create sysfs binary attribute file for device.
464 * @attr: device binary attribute descriptor.
466 int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
470 error = sysfs_create_bin_file(&dev->kobj, attr);
473 EXPORT_SYMBOL_GPL(device_create_bin_file);
476 * device_remove_bin_file - remove sysfs binary attribute file
478 * @attr: device binary attribute descriptor.
480 void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
483 sysfs_remove_bin_file(&dev->kobj, attr);
485 EXPORT_SYMBOL_GPL(device_remove_bin_file);
488 * device_schedule_callback_owner - helper to schedule a callback for a device
490 * @func: callback function to invoke later.
491 * @owner: module owning the callback routine
493 * Attribute methods must not unregister themselves or their parent device
494 * (which would amount to the same thing). Attempts to do so will deadlock,
495 * since unregistration is mutually exclusive with driver callbacks.
497 * Instead methods can call this routine, which will attempt to allocate
498 * and schedule a workqueue request to call back @func with @dev as its
499 * argument in the workqueue's process context. @dev will be pinned until
502 * This routine is usually called via the inline device_schedule_callback(),
503 * which automatically sets @owner to THIS_MODULE.
505 * Returns 0 if the request was submitted, -ENOMEM if storage could not
506 * be allocated, -ENODEV if a reference to @owner isn't available.
508 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
509 * underlying sysfs routine (since it is intended for use by attribute
510 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
512 int device_schedule_callback_owner(struct device *dev,
513 void (*func)(struct device *), struct module *owner)
515 return sysfs_schedule_callback(&dev->kobj,
516 (void (*)(void *)) func, dev, owner);
518 EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
520 static void klist_children_get(struct klist_node *n)
522 struct device_private *p = to_device_private_parent(n);
523 struct device *dev = p->device;
528 static void klist_children_put(struct klist_node *n)
530 struct device_private *p = to_device_private_parent(n);
531 struct device *dev = p->device;
537 * device_initialize - init device structure.
540 * This prepares the device for use by other layers by initializing
542 * It is the first half of device_register(), if called by
543 * that function, though it can also be called separately, so one
544 * may use @dev's fields. In particular, get_device()/put_device()
545 * may be used for reference counting of @dev after calling this
548 * NOTE: Use put_device() to give up your reference instead of freeing
549 * @dev directly once you have called this function.
551 void device_initialize(struct device *dev)
553 dev->kobj.kset = devices_kset;
554 kobject_init(&dev->kobj, &device_ktype);
555 INIT_LIST_HEAD(&dev->dma_pools);
556 init_MUTEX(&dev->sem);
557 spin_lock_init(&dev->devres_lock);
558 INIT_LIST_HEAD(&dev->devres_head);
559 device_init_wakeup(dev, 0);
561 set_dev_node(dev, -1);
564 #ifdef CONFIG_SYSFS_DEPRECATED
565 static struct kobject *get_device_parent(struct device *dev,
566 struct device *parent)
568 /* class devices without a parent live in /sys/class/<classname>/ */
569 if (dev->class && (!parent || parent->class != dev->class))
570 return &dev->class->p->class_subsys.kobj;
571 /* all other devices keep their parent */
573 return &parent->kobj;
578 static inline void cleanup_device_parent(struct device *dev) {}
579 static inline void cleanup_glue_dir(struct device *dev,
580 struct kobject *glue_dir) {}
582 static struct kobject *virtual_device_parent(struct device *dev)
584 static struct kobject *virtual_dir = NULL;
587 virtual_dir = kobject_create_and_add("virtual",
588 &devices_kset->kobj);
593 static struct kobject *get_device_parent(struct device *dev,
594 struct device *parent)
599 struct kobject *kobj = NULL;
600 struct kobject *parent_kobj;
604 * If we have no parent, we live in "virtual".
605 * Class-devices with a non class-device as parent, live
606 * in a "glue" directory to prevent namespace collisions.
609 parent_kobj = virtual_device_parent(dev);
610 else if (parent->class)
611 return &parent->kobj;
613 parent_kobj = &parent->kobj;
615 /* find our class-directory at the parent and reference it */
616 spin_lock(&dev->class->p->class_dirs.list_lock);
617 list_for_each_entry(k, &dev->class->p->class_dirs.list, entry)
618 if (k->parent == parent_kobj) {
619 kobj = kobject_get(k);
622 spin_unlock(&dev->class->p->class_dirs.list_lock);
626 /* or create a new class-directory at the parent device */
627 k = kobject_create();
630 k->kset = &dev->class->p->class_dirs;
631 retval = kobject_add(k, parent_kobj, "%s", dev->class->name);
636 /* do not emit an uevent for this simple "glue" directory */
641 return &parent->kobj;
645 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
647 /* see if we live in a "glue" directory */
648 if (!glue_dir || !dev->class ||
649 glue_dir->kset != &dev->class->p->class_dirs)
652 kobject_put(glue_dir);
655 static void cleanup_device_parent(struct device *dev)
657 cleanup_glue_dir(dev, dev->kobj.parent);
661 static void setup_parent(struct device *dev, struct device *parent)
663 struct kobject *kobj;
664 kobj = get_device_parent(dev, parent);
666 dev->kobj.parent = kobj;
669 static int device_add_class_symlinks(struct device *dev)
676 error = sysfs_create_link(&dev->kobj,
677 &dev->class->p->class_subsys.kobj,
682 #ifdef CONFIG_SYSFS_DEPRECATED
683 /* stacked class devices need a symlink in the class directory */
684 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
685 device_is_not_partition(dev)) {
686 error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
687 &dev->kobj, dev_name(dev));
692 if (dev->parent && device_is_not_partition(dev)) {
693 struct device *parent = dev->parent;
697 * stacked class devices have the 'device' link
698 * pointing to the bus device instead of the parent
700 while (parent->class && !parent->bus && parent->parent)
701 parent = parent->parent;
703 error = sysfs_create_link(&dev->kobj,
709 class_name = make_class_name(dev->class->name,
712 error = sysfs_create_link(&dev->parent->kobj,
713 &dev->kobj, class_name);
721 if (dev->parent && device_is_not_partition(dev))
722 sysfs_remove_link(&dev->kobj, "device");
724 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
725 device_is_not_partition(dev))
726 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
729 /* link in the class directory pointing to the device */
730 error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
731 &dev->kobj, dev_name(dev));
735 if (dev->parent && device_is_not_partition(dev)) {
736 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
744 sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
748 sysfs_remove_link(&dev->kobj, "subsystem");
753 static void device_remove_class_symlinks(struct device *dev)
758 #ifdef CONFIG_SYSFS_DEPRECATED
759 if (dev->parent && device_is_not_partition(dev)) {
762 class_name = make_class_name(dev->class->name, &dev->kobj);
764 sysfs_remove_link(&dev->parent->kobj, class_name);
767 sysfs_remove_link(&dev->kobj, "device");
770 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
771 device_is_not_partition(dev))
772 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
775 if (dev->parent && device_is_not_partition(dev))
776 sysfs_remove_link(&dev->kobj, "device");
778 sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
781 sysfs_remove_link(&dev->kobj, "subsystem");
785 * dev_set_name - set a device name
787 * @fmt: format string for the device's name
789 int dev_set_name(struct device *dev, const char *fmt, ...)
794 va_start(vargs, fmt);
795 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
799 EXPORT_SYMBOL_GPL(dev_set_name);
802 * device_to_dev_kobj - select a /sys/dev/ directory for the device
805 * By default we select char/ for new entries. Setting class->dev_obj
806 * to NULL prevents an entry from being created. class->dev_kobj must
807 * be set (or cleared) before any devices are registered to the class
808 * otherwise device_create_sys_dev_entry() and
809 * device_remove_sys_dev_entry() will disagree about the the presence
812 static struct kobject *device_to_dev_kobj(struct device *dev)
814 struct kobject *kobj;
817 kobj = dev->class->dev_kobj;
819 kobj = sysfs_dev_char_kobj;
824 static int device_create_sys_dev_entry(struct device *dev)
826 struct kobject *kobj = device_to_dev_kobj(dev);
831 format_dev_t(devt_str, dev->devt);
832 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
838 static void device_remove_sys_dev_entry(struct device *dev)
840 struct kobject *kobj = device_to_dev_kobj(dev);
844 format_dev_t(devt_str, dev->devt);
845 sysfs_remove_link(kobj, devt_str);
849 int device_private_init(struct device *dev)
851 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
854 dev->p->device = dev;
855 klist_init(&dev->p->klist_children, klist_children_get,
861 * device_add - add device to device hierarchy.
864 * This is part 2 of device_register(), though may be called
865 * separately _iff_ device_initialize() has been called separately.
867 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
868 * to the global and sibling lists for the device, then
869 * adds it to the other relevant subsystems of the driver model.
871 * NOTE: _Never_ directly free @dev after calling this function, even
872 * if it returned an error! Always use put_device() to give up your
875 int device_add(struct device *dev)
877 struct device *parent = NULL;
878 struct class_interface *class_intf;
881 dev = get_device(dev);
886 error = device_private_init(dev);
892 * for statically allocated devices, which should all be converted
893 * some day, we need to initialize the name. We prevent reading back
894 * the name, and force the use of dev_name()
896 if (dev->init_name) {
897 dev_set_name(dev, "%s", dev->init_name);
898 dev->init_name = NULL;
904 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
906 parent = get_device(dev->parent);
907 setup_parent(dev, parent);
909 /* use parent numa_node */
911 set_dev_node(dev, dev_to_node(parent));
913 /* first, register with generic layer. */
914 /* we require the name to be set before, and pass NULL */
915 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
919 /* notify platform of device entry */
921 platform_notify(dev);
923 error = device_create_file(dev, &uevent_attr);
927 if (MAJOR(dev->devt)) {
928 error = device_create_file(dev, &devt_attr);
930 goto ueventattrError;
932 error = device_create_sys_dev_entry(dev);
936 devtmpfs_create_node(dev);
939 error = device_add_class_symlinks(dev);
942 error = device_add_attrs(dev);
945 error = bus_add_device(dev);
948 error = dpm_sysfs_add(dev);
953 /* Notify clients of device addition. This call must come
954 * after dpm_sysf_add() and before kobject_uevent().
957 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
958 BUS_NOTIFY_ADD_DEVICE, dev);
960 kobject_uevent(&dev->kobj, KOBJ_ADD);
961 bus_probe_device(dev);
963 klist_add_tail(&dev->p->knode_parent,
964 &parent->p->klist_children);
967 mutex_lock(&dev->class->p->class_mutex);
968 /* tie the class to the device */
969 klist_add_tail(&dev->knode_class,
970 &dev->class->p->class_devices);
972 /* notify any interfaces that the device is here */
973 list_for_each_entry(class_intf,
974 &dev->class->p->class_interfaces, node)
975 if (class_intf->add_dev)
976 class_intf->add_dev(dev, class_intf);
977 mutex_unlock(&dev->class->p->class_mutex);
983 bus_remove_device(dev);
985 device_remove_attrs(dev);
987 device_remove_class_symlinks(dev);
989 if (MAJOR(dev->devt))
990 device_remove_sys_dev_entry(dev);
992 if (MAJOR(dev->devt))
993 device_remove_file(dev, &devt_attr);
995 device_remove_file(dev, &uevent_attr);
997 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
998 kobject_del(&dev->kobj);
1000 cleanup_device_parent(dev);
1010 * device_register - register a device with the system.
1011 * @dev: pointer to the device structure
1013 * This happens in two clean steps - initialize the device
1014 * and add it to the system. The two steps can be called
1015 * separately, but this is the easiest and most common.
1016 * I.e. you should only call the two helpers separately if
1017 * have a clearly defined need to use and refcount the device
1018 * before it is added to the hierarchy.
1020 * NOTE: _Never_ directly free @dev after calling this function, even
1021 * if it returned an error! Always use put_device() to give up the
1022 * reference initialized in this function instead.
1024 int device_register(struct device *dev)
1026 device_initialize(dev);
1027 return device_add(dev);
1031 * get_device - increment reference count for device.
1034 * This simply forwards the call to kobject_get(), though
1035 * we do take care to provide for the case that we get a NULL
1036 * pointer passed in.
1038 struct device *get_device(struct device *dev)
1040 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
1044 * put_device - decrement reference count.
1045 * @dev: device in question.
1047 void put_device(struct device *dev)
1049 /* might_sleep(); */
1051 kobject_put(&dev->kobj);
1055 * device_del - delete device from system.
1058 * This is the first part of the device unregistration
1059 * sequence. This removes the device from the lists we control
1060 * from here, has it removed from the other driver model
1061 * subsystems it was added to in device_add(), and removes it
1062 * from the kobject hierarchy.
1064 * NOTE: this should be called manually _iff_ device_add() was
1065 * also called manually.
1067 void device_del(struct device *dev)
1069 struct device *parent = dev->parent;
1070 struct class_interface *class_intf;
1072 /* Notify clients of device removal. This call must come
1073 * before dpm_sysfs_remove().
1076 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1077 BUS_NOTIFY_DEL_DEVICE, dev);
1078 device_pm_remove(dev);
1079 dpm_sysfs_remove(dev);
1081 klist_del(&dev->p->knode_parent);
1082 if (MAJOR(dev->devt)) {
1083 devtmpfs_delete_node(dev);
1084 device_remove_sys_dev_entry(dev);
1085 device_remove_file(dev, &devt_attr);
1088 device_remove_class_symlinks(dev);
1090 mutex_lock(&dev->class->p->class_mutex);
1091 /* notify any interfaces that the device is now gone */
1092 list_for_each_entry(class_intf,
1093 &dev->class->p->class_interfaces, node)
1094 if (class_intf->remove_dev)
1095 class_intf->remove_dev(dev, class_intf);
1096 /* remove the device from the class list */
1097 klist_del(&dev->knode_class);
1098 mutex_unlock(&dev->class->p->class_mutex);
1100 device_remove_file(dev, &uevent_attr);
1101 device_remove_attrs(dev);
1102 bus_remove_device(dev);
1105 * Some platform devices are driven without driver attached
1106 * and managed resources may have been acquired. Make sure
1107 * all resources are released.
1109 devres_release_all(dev);
1111 /* Notify the platform of the removal, in case they
1112 * need to do anything...
1114 if (platform_notify_remove)
1115 platform_notify_remove(dev);
1116 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1117 cleanup_device_parent(dev);
1118 kobject_del(&dev->kobj);
1123 * device_unregister - unregister device from system.
1124 * @dev: device going away.
1126 * We do this in two parts, like we do device_register(). First,
1127 * we remove it from all the subsystems with device_del(), then
1128 * we decrement the reference count via put_device(). If that
1129 * is the final reference count, the device will be cleaned up
1130 * via device_release() above. Otherwise, the structure will
1131 * stick around until the final reference to the device is dropped.
1133 void device_unregister(struct device *dev)
1135 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1140 static struct device *next_device(struct klist_iter *i)
1142 struct klist_node *n = klist_next(i);
1143 struct device *dev = NULL;
1144 struct device_private *p;
1147 p = to_device_private_parent(n);
1154 * device_get_devnode - path of device node file
1156 * @mode: returned file access mode
1157 * @tmp: possibly allocated string
1159 * Return the relative path of a possible device node.
1160 * Non-default names may need to allocate a memory to compose
1161 * a name. This memory is returned in tmp and needs to be
1162 * freed by the caller.
1164 const char *device_get_devnode(struct device *dev,
1165 mode_t *mode, const char **tmp)
1171 /* the device type may provide a specific name */
1172 if (dev->type && dev->type->devnode)
1173 *tmp = dev->type->devnode(dev, mode);
1177 /* the class may provide a specific name */
1178 if (dev->class && dev->class->devnode)
1179 *tmp = dev->class->devnode(dev, mode);
1183 /* return name without allocation, tmp == NULL */
1184 if (strchr(dev_name(dev), '!') == NULL)
1185 return dev_name(dev);
1187 /* replace '!' in the name with '/' */
1188 *tmp = kstrdup(dev_name(dev), GFP_KERNEL);
1191 while ((s = strchr(*tmp, '!')))
1197 * device_for_each_child - device child iterator.
1198 * @parent: parent struct device.
1199 * @data: data for the callback.
1200 * @fn: function to be called for each device.
1202 * Iterate over @parent's child devices, and call @fn for each,
1205 * We check the return of @fn each time. If it returns anything
1206 * other than 0, we break out and return that value.
1208 int device_for_each_child(struct device *parent, void *data,
1209 int (*fn)(struct device *dev, void *data))
1211 struct klist_iter i;
1212 struct device *child;
1218 klist_iter_init(&parent->p->klist_children, &i);
1219 while ((child = next_device(&i)) && !error)
1220 error = fn(child, data);
1221 klist_iter_exit(&i);
1226 * device_find_child - device iterator for locating a particular device.
1227 * @parent: parent struct device
1228 * @data: Data to pass to match function
1229 * @match: Callback function to check device
1231 * This is similar to the device_for_each_child() function above, but it
1232 * returns a reference to a device that is 'found' for later use, as
1233 * determined by the @match callback.
1235 * The callback should return 0 if the device doesn't match and non-zero
1236 * if it does. If the callback returns non-zero and a reference to the
1237 * current device can be obtained, this function will return to the caller
1238 * and not iterate over any more devices.
1240 struct device *device_find_child(struct device *parent, void *data,
1241 int (*match)(struct device *dev, void *data))
1243 struct klist_iter i;
1244 struct device *child;
1249 klist_iter_init(&parent->p->klist_children, &i);
1250 while ((child = next_device(&i)))
1251 if (match(child, data) && get_device(child))
1253 klist_iter_exit(&i);
1257 int __init devices_init(void)
1259 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1262 dev_kobj = kobject_create_and_add("dev", NULL);
1265 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1266 if (!sysfs_dev_block_kobj)
1267 goto block_kobj_err;
1268 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1269 if (!sysfs_dev_char_kobj)
1275 kobject_put(sysfs_dev_block_kobj);
1277 kobject_put(dev_kobj);
1279 kset_unregister(devices_kset);
1283 EXPORT_SYMBOL_GPL(device_for_each_child);
1284 EXPORT_SYMBOL_GPL(device_find_child);
1286 EXPORT_SYMBOL_GPL(device_initialize);
1287 EXPORT_SYMBOL_GPL(device_add);
1288 EXPORT_SYMBOL_GPL(device_register);
1290 EXPORT_SYMBOL_GPL(device_del);
1291 EXPORT_SYMBOL_GPL(device_unregister);
1292 EXPORT_SYMBOL_GPL(get_device);
1293 EXPORT_SYMBOL_GPL(put_device);
1295 EXPORT_SYMBOL_GPL(device_create_file);
1296 EXPORT_SYMBOL_GPL(device_remove_file);
1301 struct module *owner;
1304 #define to_root_device(dev) container_of(dev, struct root_device, dev)
1306 static void root_device_release(struct device *dev)
1308 kfree(to_root_device(dev));
1312 * __root_device_register - allocate and register a root device
1313 * @name: root device name
1314 * @owner: owner module of the root device, usually THIS_MODULE
1316 * This function allocates a root device and registers it
1317 * using device_register(). In order to free the returned
1318 * device, use root_device_unregister().
1320 * Root devices are dummy devices which allow other devices
1321 * to be grouped under /sys/devices. Use this function to
1322 * allocate a root device and then use it as the parent of
1323 * any device which should appear under /sys/devices/{name}
1325 * The /sys/devices/{name} directory will also contain a
1326 * 'module' symlink which points to the @owner directory
1329 * Note: You probably want to use root_device_register().
1331 struct device *__root_device_register(const char *name, struct module *owner)
1333 struct root_device *root;
1336 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
1338 return ERR_PTR(err);
1340 err = dev_set_name(&root->dev, "%s", name);
1343 return ERR_PTR(err);
1346 root->dev.release = root_device_release;
1348 err = device_register(&root->dev);
1350 put_device(&root->dev);
1351 return ERR_PTR(err);
1354 #ifdef CONFIG_MODULE /* gotta find a "cleaner" way to do this */
1356 struct module_kobject *mk = &owner->mkobj;
1358 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
1360 device_unregister(&root->dev);
1361 return ERR_PTR(err);
1363 root->owner = owner;
1369 EXPORT_SYMBOL_GPL(__root_device_register);
1372 * root_device_unregister - unregister and free a root device
1373 * @dev: device going away
1375 * This function unregisters and cleans up a device that was created by
1376 * root_device_register().
1378 void root_device_unregister(struct device *dev)
1380 struct root_device *root = to_root_device(dev);
1383 sysfs_remove_link(&root->dev.kobj, "module");
1385 device_unregister(dev);
1387 EXPORT_SYMBOL_GPL(root_device_unregister);
1390 static void device_create_release(struct device *dev)
1392 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1397 * device_create_vargs - creates a device and registers it with sysfs
1398 * @class: pointer to the struct class that this device should be registered to
1399 * @parent: pointer to the parent struct device of this new device, if any
1400 * @devt: the dev_t for the char device to be added
1401 * @drvdata: the data to be added to the device for callbacks
1402 * @fmt: string for the device's name
1403 * @args: va_list for the device's name
1405 * This function can be used by char device classes. A struct device
1406 * will be created in sysfs, registered to the specified class.
1408 * A "dev" file will be created, showing the dev_t for the device, if
1409 * the dev_t is not 0,0.
1410 * If a pointer to a parent struct device is passed in, the newly created
1411 * struct device will be a child of that device in sysfs.
1412 * The pointer to the struct device will be returned from the call.
1413 * Any further sysfs files that might be required can be created using this
1416 * Note: the struct class passed to this function must have previously
1417 * been created with a call to class_create().
1419 struct device *device_create_vargs(struct class *class, struct device *parent,
1420 dev_t devt, void *drvdata, const char *fmt,
1423 struct device *dev = NULL;
1424 int retval = -ENODEV;
1426 if (class == NULL || IS_ERR(class))
1429 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1437 dev->parent = parent;
1438 dev->release = device_create_release;
1439 dev_set_drvdata(dev, drvdata);
1441 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
1445 retval = device_register(dev);
1453 return ERR_PTR(retval);
1455 EXPORT_SYMBOL_GPL(device_create_vargs);
1458 * device_create - creates a device and registers it with sysfs
1459 * @class: pointer to the struct class that this device should be registered to
1460 * @parent: pointer to the parent struct device of this new device, if any
1461 * @devt: the dev_t for the char device to be added
1462 * @drvdata: the data to be added to the device for callbacks
1463 * @fmt: string for the device's name
1465 * This function can be used by char device classes. A struct device
1466 * will be created in sysfs, registered to the specified class.
1468 * A "dev" file will be created, showing the dev_t for the device, if
1469 * the dev_t is not 0,0.
1470 * If a pointer to a parent struct device is passed in, the newly created
1471 * struct device will be a child of that device in sysfs.
1472 * The pointer to the struct device will be returned from the call.
1473 * Any further sysfs files that might be required can be created using this
1476 * Note: the struct class passed to this function must have previously
1477 * been created with a call to class_create().
1479 struct device *device_create(struct class *class, struct device *parent,
1480 dev_t devt, void *drvdata, const char *fmt, ...)
1485 va_start(vargs, fmt);
1486 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
1490 EXPORT_SYMBOL_GPL(device_create);
1492 static int __match_devt(struct device *dev, void *data)
1496 return dev->devt == *devt;
1500 * device_destroy - removes a device that was created with device_create()
1501 * @class: pointer to the struct class that this device was registered with
1502 * @devt: the dev_t of the device that was previously registered
1504 * This call unregisters and cleans up a device that was created with a
1505 * call to device_create().
1507 void device_destroy(struct class *class, dev_t devt)
1511 dev = class_find_device(class, NULL, &devt, __match_devt);
1514 device_unregister(dev);
1517 EXPORT_SYMBOL_GPL(device_destroy);
1520 * device_rename - renames a device
1521 * @dev: the pointer to the struct device to be renamed
1522 * @new_name: the new name of the device
1524 * It is the responsibility of the caller to provide mutual
1525 * exclusion between two different calls of device_rename
1526 * on the same device to ensure that new_name is valid and
1527 * won't conflict with other devices.
1529 int device_rename(struct device *dev, char *new_name)
1531 char *old_class_name = NULL;
1532 char *new_class_name = NULL;
1533 char *old_device_name = NULL;
1536 dev = get_device(dev);
1540 pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
1541 __func__, new_name);
1543 #ifdef CONFIG_SYSFS_DEPRECATED
1544 if ((dev->class) && (dev->parent))
1545 old_class_name = make_class_name(dev->class->name, &dev->kobj);
1548 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
1549 if (!old_device_name) {
1554 error = kobject_rename(&dev->kobj, new_name);
1558 #ifdef CONFIG_SYSFS_DEPRECATED
1559 if (old_class_name) {
1560 new_class_name = make_class_name(dev->class->name, &dev->kobj);
1561 if (new_class_name) {
1562 error = sysfs_create_link_nowarn(&dev->parent->kobj,
1567 sysfs_remove_link(&dev->parent->kobj, old_class_name);
1572 error = sysfs_create_link_nowarn(&dev->class->p->class_subsys.kobj,
1573 &dev->kobj, dev_name(dev));
1576 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
1584 kfree(new_class_name);
1585 kfree(old_class_name);
1586 kfree(old_device_name);
1590 EXPORT_SYMBOL_GPL(device_rename);
1592 static int device_move_class_links(struct device *dev,
1593 struct device *old_parent,
1594 struct device *new_parent)
1597 #ifdef CONFIG_SYSFS_DEPRECATED
1600 class_name = make_class_name(dev->class->name, &dev->kobj);
1606 sysfs_remove_link(&dev->kobj, "device");
1607 sysfs_remove_link(&old_parent->kobj, class_name);
1610 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1614 error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
1617 sysfs_remove_link(&dev->kobj, "device");
1625 sysfs_remove_link(&dev->kobj, "device");
1627 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1634 * device_move - moves a device to a new parent
1635 * @dev: the pointer to the struct device to be moved
1636 * @new_parent: the new parent of the device (can by NULL)
1637 * @dpm_order: how to reorder the dpm_list
1639 int device_move(struct device *dev, struct device *new_parent,
1640 enum dpm_order dpm_order)
1643 struct device *old_parent;
1644 struct kobject *new_parent_kobj;
1646 dev = get_device(dev);
1651 new_parent = get_device(new_parent);
1652 new_parent_kobj = get_device_parent(dev, new_parent);
1654 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
1655 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
1656 error = kobject_move(&dev->kobj, new_parent_kobj);
1658 cleanup_glue_dir(dev, new_parent_kobj);
1659 put_device(new_parent);
1662 old_parent = dev->parent;
1663 dev->parent = new_parent;
1665 klist_remove(&dev->p->knode_parent);
1667 klist_add_tail(&dev->p->knode_parent,
1668 &new_parent->p->klist_children);
1669 set_dev_node(dev, dev_to_node(new_parent));
1674 error = device_move_class_links(dev, old_parent, new_parent);
1676 /* We ignore errors on cleanup since we're hosed anyway... */
1677 device_move_class_links(dev, new_parent, old_parent);
1678 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1680 klist_remove(&dev->p->knode_parent);
1681 dev->parent = old_parent;
1683 klist_add_tail(&dev->p->knode_parent,
1684 &old_parent->p->klist_children);
1685 set_dev_node(dev, dev_to_node(old_parent));
1688 cleanup_glue_dir(dev, new_parent_kobj);
1689 put_device(new_parent);
1692 switch (dpm_order) {
1693 case DPM_ORDER_NONE:
1695 case DPM_ORDER_DEV_AFTER_PARENT:
1696 device_pm_move_after(dev, new_parent);
1698 case DPM_ORDER_PARENT_BEFORE_DEV:
1699 device_pm_move_before(new_parent, dev);
1701 case DPM_ORDER_DEV_LAST:
1702 device_pm_move_last(dev);
1706 put_device(old_parent);
1712 EXPORT_SYMBOL_GPL(device_move);
1715 * device_shutdown - call ->shutdown() on each device to shutdown.
1717 void device_shutdown(void)
1719 struct device *dev, *devn;
1721 list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list,
1723 if (dev->bus && dev->bus->shutdown) {
1724 dev_dbg(dev, "shutdown\n");
1725 dev->bus->shutdown(dev);
1726 } else if (dev->driver && dev->driver->shutdown) {
1727 dev_dbg(dev, "shutdown\n");
1728 dev->driver->shutdown(dev);
1731 kobject_put(sysfs_dev_char_kobj);
1732 kobject_put(sysfs_dev_block_kobj);
1733 kobject_put(dev_kobj);
1734 async_synchronize_full();