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 int (*platform_notify)(struct device *dev) = NULL;
30 int (*platform_notify_remove)(struct device *dev) = NULL;
31 static struct kobject *dev_kobj;
32 struct kobject *sysfs_dev_char_kobj;
33 struct kobject *sysfs_dev_block_kobj;
36 static inline int device_is_not_partition(struct device *dev)
38 return !(dev->type == &part_type);
41 static inline int device_is_not_partition(struct device *dev)
48 * dev_driver_string - Return a device's driver name, if at all possible
49 * @dev: struct device to get the name of
51 * Will return the device's driver's name if it is bound to a device. If
52 * the device is not bound to a device, it will return the name of the bus
53 * it is attached to. If it is not attached to a bus either, an empty
54 * string will be returned.
56 const char *dev_driver_string(const struct device *dev)
58 struct device_driver *drv;
60 /* dev->driver can change to NULL underneath us because of unbinding,
61 * so be careful about accessing it. dev->bus and dev->class should
62 * never change once they are set, so they don't need special care.
64 drv = ACCESS_ONCE(dev->driver);
65 return drv ? drv->name :
66 (dev->bus ? dev->bus->name :
67 (dev->class ? dev->class->name : ""));
69 EXPORT_SYMBOL(dev_driver_string);
71 #define to_dev(obj) container_of(obj, struct device, kobj)
72 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
74 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
77 struct device_attribute *dev_attr = to_dev_attr(attr);
78 struct device *dev = to_dev(kobj);
82 ret = dev_attr->show(dev, dev_attr, buf);
83 if (ret >= (ssize_t)PAGE_SIZE) {
84 print_symbol("dev_attr_show: %s returned bad count\n",
85 (unsigned long)dev_attr->show);
90 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
91 const char *buf, size_t count)
93 struct device_attribute *dev_attr = to_dev_attr(attr);
94 struct device *dev = to_dev(kobj);
98 ret = dev_attr->store(dev, dev_attr, buf, count);
102 static const struct sysfs_ops dev_sysfs_ops = {
103 .show = dev_attr_show,
104 .store = dev_attr_store,
109 * device_release - free device structure.
110 * @kobj: device's kobject.
112 * This is called once the reference count for the object
113 * reaches 0. We forward the call to the device's release
114 * method, which should handle actually freeing the structure.
116 static void device_release(struct kobject *kobj)
118 struct device *dev = to_dev(kobj);
119 struct device_private *p = dev->p;
123 else if (dev->type && dev->type->release)
124 dev->type->release(dev);
125 else if (dev->class && dev->class->dev_release)
126 dev->class->dev_release(dev);
128 WARN(1, KERN_ERR "Device '%s' does not have a release() "
129 "function, it is broken and must be fixed.\n",
134 static struct kobj_type device_ktype = {
135 .release = device_release,
136 .sysfs_ops = &dev_sysfs_ops,
140 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
142 struct kobj_type *ktype = get_ktype(kobj);
144 if (ktype == &device_ktype) {
145 struct device *dev = to_dev(kobj);
154 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
156 struct device *dev = to_dev(kobj);
159 return dev->bus->name;
161 return dev->class->name;
165 static int dev_uevent(struct kset *kset, struct kobject *kobj,
166 struct kobj_uevent_env *env)
168 struct device *dev = to_dev(kobj);
171 /* add device node properties if present */
172 if (MAJOR(dev->devt)) {
177 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
178 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
179 name = device_get_devnode(dev, &mode, &tmp);
181 add_uevent_var(env, "DEVNAME=%s", name);
184 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
188 if (dev->type && dev->type->name)
189 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
192 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
194 #ifdef CONFIG_SYSFS_DEPRECATED
196 struct device *parent = dev->parent;
198 /* find first bus device in parent chain */
199 while (parent && !parent->bus)
200 parent = parent->parent;
201 if (parent && parent->bus) {
204 path = kobject_get_path(&parent->kobj, GFP_KERNEL);
206 add_uevent_var(env, "PHYSDEVPATH=%s", path);
210 add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name);
213 add_uevent_var(env, "PHYSDEVDRIVER=%s",
214 parent->driver->name);
216 } else if (dev->bus) {
217 add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
220 add_uevent_var(env, "PHYSDEVDRIVER=%s",
225 /* have the bus specific function add its stuff */
226 if (dev->bus && dev->bus->uevent) {
227 retval = dev->bus->uevent(dev, env);
229 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
230 dev_name(dev), __func__, retval);
233 /* have the class specific function add its stuff */
234 if (dev->class && dev->class->dev_uevent) {
235 retval = dev->class->dev_uevent(dev, env);
237 pr_debug("device: '%s': %s: class uevent() "
238 "returned %d\n", dev_name(dev),
242 /* have the device type specific fuction add its stuff */
243 if (dev->type && dev->type->uevent) {
244 retval = dev->type->uevent(dev, env);
246 pr_debug("device: '%s': %s: dev_type uevent() "
247 "returned %d\n", dev_name(dev),
254 static const struct kset_uevent_ops device_uevent_ops = {
255 .filter = dev_uevent_filter,
256 .name = dev_uevent_name,
257 .uevent = dev_uevent,
260 static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
263 struct kobject *top_kobj;
265 struct kobj_uevent_env *env = NULL;
270 /* search the kset, the device belongs to */
271 top_kobj = &dev->kobj;
272 while (!top_kobj->kset && top_kobj->parent)
273 top_kobj = top_kobj->parent;
277 kset = top_kobj->kset;
278 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
282 if (kset->uevent_ops && kset->uevent_ops->filter)
283 if (!kset->uevent_ops->filter(kset, &dev->kobj))
286 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
290 /* let the kset specific function add its keys */
291 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
295 /* copy keys to file */
296 for (i = 0; i < env->envp_idx; i++)
297 count += sprintf(&buf[count], "%s\n", env->envp[i]);
303 static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
304 const char *buf, size_t count)
306 enum kobject_action action;
308 if (kobject_action_type(buf, count, &action) == 0)
309 kobject_uevent(&dev->kobj, action);
311 dev_err(dev, "uevent: unknown action-string\n");
315 static struct device_attribute uevent_attr =
316 __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
318 static int device_add_attributes(struct device *dev,
319 struct device_attribute *attrs)
325 for (i = 0; attr_name(attrs[i]); i++) {
326 error = device_create_file(dev, &attrs[i]);
332 device_remove_file(dev, &attrs[i]);
337 static void device_remove_attributes(struct device *dev,
338 struct device_attribute *attrs)
343 for (i = 0; attr_name(attrs[i]); i++)
344 device_remove_file(dev, &attrs[i]);
347 static int device_add_groups(struct device *dev,
348 const struct attribute_group **groups)
354 for (i = 0; groups[i]; i++) {
355 error = sysfs_create_group(&dev->kobj, groups[i]);
358 sysfs_remove_group(&dev->kobj,
367 static void device_remove_groups(struct device *dev,
368 const struct attribute_group **groups)
373 for (i = 0; groups[i]; i++)
374 sysfs_remove_group(&dev->kobj, groups[i]);
377 static int device_add_attrs(struct device *dev)
379 struct class *class = dev->class;
380 struct device_type *type = dev->type;
384 error = device_add_attributes(dev, class->dev_attrs);
390 error = device_add_groups(dev, type->groups);
392 goto err_remove_class_attrs;
395 error = device_add_groups(dev, dev->groups);
397 goto err_remove_type_groups;
401 err_remove_type_groups:
403 device_remove_groups(dev, type->groups);
404 err_remove_class_attrs:
406 device_remove_attributes(dev, class->dev_attrs);
411 static void device_remove_attrs(struct device *dev)
413 struct class *class = dev->class;
414 struct device_type *type = dev->type;
416 device_remove_groups(dev, dev->groups);
419 device_remove_groups(dev, type->groups);
422 device_remove_attributes(dev, class->dev_attrs);
426 static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
429 return print_dev_t(buf, dev->devt);
432 static struct device_attribute devt_attr =
433 __ATTR(dev, S_IRUGO, show_dev, NULL);
435 /* kset to create /sys/devices/ */
436 struct kset *devices_kset;
439 * device_create_file - create sysfs attribute file for device.
441 * @attr: device attribute descriptor.
443 int device_create_file(struct device *dev,
444 const struct device_attribute *attr)
448 error = sysfs_create_file(&dev->kobj, &attr->attr);
453 * device_remove_file - remove sysfs attribute file.
455 * @attr: device attribute descriptor.
457 void device_remove_file(struct device *dev,
458 const struct device_attribute *attr)
461 sysfs_remove_file(&dev->kobj, &attr->attr);
465 * device_create_bin_file - create sysfs binary attribute file for device.
467 * @attr: device binary attribute descriptor.
469 int device_create_bin_file(struct device *dev,
470 const struct bin_attribute *attr)
474 error = sysfs_create_bin_file(&dev->kobj, attr);
477 EXPORT_SYMBOL_GPL(device_create_bin_file);
480 * device_remove_bin_file - remove sysfs binary attribute file
482 * @attr: device binary attribute descriptor.
484 void device_remove_bin_file(struct device *dev,
485 const struct bin_attribute *attr)
488 sysfs_remove_bin_file(&dev->kobj, attr);
490 EXPORT_SYMBOL_GPL(device_remove_bin_file);
493 * device_schedule_callback_owner - helper to schedule a callback for a device
495 * @func: callback function to invoke later.
496 * @owner: module owning the callback routine
498 * Attribute methods must not unregister themselves or their parent device
499 * (which would amount to the same thing). Attempts to do so will deadlock,
500 * since unregistration is mutually exclusive with driver callbacks.
502 * Instead methods can call this routine, which will attempt to allocate
503 * and schedule a workqueue request to call back @func with @dev as its
504 * argument in the workqueue's process context. @dev will be pinned until
507 * This routine is usually called via the inline device_schedule_callback(),
508 * which automatically sets @owner to THIS_MODULE.
510 * Returns 0 if the request was submitted, -ENOMEM if storage could not
511 * be allocated, -ENODEV if a reference to @owner isn't available.
513 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
514 * underlying sysfs routine (since it is intended for use by attribute
515 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
517 int device_schedule_callback_owner(struct device *dev,
518 void (*func)(struct device *), struct module *owner)
520 return sysfs_schedule_callback(&dev->kobj,
521 (void (*)(void *)) func, dev, owner);
523 EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
525 static void klist_children_get(struct klist_node *n)
527 struct device_private *p = to_device_private_parent(n);
528 struct device *dev = p->device;
533 static void klist_children_put(struct klist_node *n)
535 struct device_private *p = to_device_private_parent(n);
536 struct device *dev = p->device;
542 * device_initialize - init device structure.
545 * This prepares the device for use by other layers by initializing
547 * It is the first half of device_register(), if called by
548 * that function, though it can also be called separately, so one
549 * may use @dev's fields. In particular, get_device()/put_device()
550 * may be used for reference counting of @dev after calling this
553 * NOTE: Use put_device() to give up your reference instead of freeing
554 * @dev directly once you have called this function.
556 void device_initialize(struct device *dev)
558 dev->kobj.kset = devices_kset;
559 kobject_init(&dev->kobj, &device_ktype);
560 INIT_LIST_HEAD(&dev->dma_pools);
561 mutex_init(&dev->mutex);
562 lockdep_set_novalidate_class(&dev->mutex);
563 spin_lock_init(&dev->devres_lock);
564 INIT_LIST_HEAD(&dev->devres_head);
566 set_dev_node(dev, -1);
569 #ifdef CONFIG_SYSFS_DEPRECATED
570 static struct kobject *get_device_parent(struct device *dev,
571 struct device *parent)
573 /* class devices without a parent live in /sys/class/<classname>/ */
574 if (dev->class && (!parent || parent->class != dev->class))
575 return &dev->class->p->class_subsys.kobj;
576 /* all other devices keep their parent */
578 return &parent->kobj;
583 static inline void cleanup_device_parent(struct device *dev) {}
584 static inline void cleanup_glue_dir(struct device *dev,
585 struct kobject *glue_dir) {}
587 static struct kobject *virtual_device_parent(struct device *dev)
589 static struct kobject *virtual_dir = NULL;
592 virtual_dir = kobject_create_and_add("virtual",
593 &devices_kset->kobj);
598 static struct kobject *get_device_parent(struct device *dev,
599 struct device *parent)
604 static DEFINE_MUTEX(gdp_mutex);
605 struct kobject *kobj = NULL;
606 struct kobject *parent_kobj;
610 * If we have no parent, we live in "virtual".
611 * Class-devices with a non class-device as parent, live
612 * in a "glue" directory to prevent namespace collisions.
615 parent_kobj = virtual_device_parent(dev);
616 else if (parent->class)
617 return &parent->kobj;
619 parent_kobj = &parent->kobj;
621 mutex_lock(&gdp_mutex);
623 /* find our class-directory at the parent and reference it */
624 spin_lock(&dev->class->p->class_dirs.list_lock);
625 list_for_each_entry(k, &dev->class->p->class_dirs.list, entry)
626 if (k->parent == parent_kobj) {
627 kobj = kobject_get(k);
630 spin_unlock(&dev->class->p->class_dirs.list_lock);
632 mutex_unlock(&gdp_mutex);
636 /* or create a new class-directory at the parent device */
637 k = kobject_create();
639 mutex_unlock(&gdp_mutex);
642 k->kset = &dev->class->p->class_dirs;
643 retval = kobject_add(k, parent_kobj, "%s", dev->class->name);
645 mutex_unlock(&gdp_mutex);
649 /* do not emit an uevent for this simple "glue" directory */
650 mutex_unlock(&gdp_mutex);
655 return &parent->kobj;
659 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
661 /* see if we live in a "glue" directory */
662 if (!glue_dir || !dev->class ||
663 glue_dir->kset != &dev->class->p->class_dirs)
666 kobject_put(glue_dir);
669 static void cleanup_device_parent(struct device *dev)
671 cleanup_glue_dir(dev, dev->kobj.parent);
675 static void setup_parent(struct device *dev, struct device *parent)
677 struct kobject *kobj;
678 kobj = get_device_parent(dev, parent);
680 dev->kobj.parent = kobj;
683 static int device_add_class_symlinks(struct device *dev)
690 error = sysfs_create_link(&dev->kobj,
691 &dev->class->p->class_subsys.kobj,
696 #ifdef CONFIG_SYSFS_DEPRECATED
697 /* stacked class devices need a symlink in the class directory */
698 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
699 device_is_not_partition(dev)) {
700 error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
701 &dev->kobj, dev_name(dev));
706 if (dev->parent && device_is_not_partition(dev)) {
707 struct device *parent = dev->parent;
711 * stacked class devices have the 'device' link
712 * pointing to the bus device instead of the parent
714 while (parent->class && !parent->bus && parent->parent)
715 parent = parent->parent;
717 error = sysfs_create_link(&dev->kobj,
723 class_name = make_class_name(dev->class->name,
726 error = sysfs_create_link(&dev->parent->kobj,
727 &dev->kobj, class_name);
735 if (dev->parent && device_is_not_partition(dev))
736 sysfs_remove_link(&dev->kobj, "device");
738 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
739 device_is_not_partition(dev))
740 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
743 /* link in the class directory pointing to the device */
744 error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
745 &dev->kobj, dev_name(dev));
749 if (dev->parent && device_is_not_partition(dev)) {
750 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
758 sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
762 sysfs_remove_link(&dev->kobj, "subsystem");
767 static void device_remove_class_symlinks(struct device *dev)
772 #ifdef CONFIG_SYSFS_DEPRECATED
773 if (dev->parent && device_is_not_partition(dev)) {
776 class_name = make_class_name(dev->class->name, &dev->kobj);
778 sysfs_remove_link(&dev->parent->kobj, class_name);
781 sysfs_remove_link(&dev->kobj, "device");
784 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
785 device_is_not_partition(dev))
786 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
789 if (dev->parent && device_is_not_partition(dev))
790 sysfs_remove_link(&dev->kobj, "device");
792 sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
795 sysfs_remove_link(&dev->kobj, "subsystem");
799 * dev_set_name - set a device name
801 * @fmt: format string for the device's name
803 int dev_set_name(struct device *dev, const char *fmt, ...)
808 va_start(vargs, fmt);
809 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
813 EXPORT_SYMBOL_GPL(dev_set_name);
816 * device_to_dev_kobj - select a /sys/dev/ directory for the device
819 * By default we select char/ for new entries. Setting class->dev_obj
820 * to NULL prevents an entry from being created. class->dev_kobj must
821 * be set (or cleared) before any devices are registered to the class
822 * otherwise device_create_sys_dev_entry() and
823 * device_remove_sys_dev_entry() will disagree about the the presence
826 static struct kobject *device_to_dev_kobj(struct device *dev)
828 struct kobject *kobj;
831 kobj = dev->class->dev_kobj;
833 kobj = sysfs_dev_char_kobj;
838 static int device_create_sys_dev_entry(struct device *dev)
840 struct kobject *kobj = device_to_dev_kobj(dev);
845 format_dev_t(devt_str, dev->devt);
846 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
852 static void device_remove_sys_dev_entry(struct device *dev)
854 struct kobject *kobj = device_to_dev_kobj(dev);
858 format_dev_t(devt_str, dev->devt);
859 sysfs_remove_link(kobj, devt_str);
863 int device_private_init(struct device *dev)
865 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
868 dev->p->device = dev;
869 klist_init(&dev->p->klist_children, klist_children_get,
875 * device_add - add device to device hierarchy.
878 * This is part 2 of device_register(), though may be called
879 * separately _iff_ device_initialize() has been called separately.
881 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
882 * to the global and sibling lists for the device, then
883 * adds it to the other relevant subsystems of the driver model.
885 * NOTE: _Never_ directly free @dev after calling this function, even
886 * if it returned an error! Always use put_device() to give up your
889 int device_add(struct device *dev)
891 struct device *parent = NULL;
892 struct class_interface *class_intf;
895 dev = get_device(dev);
900 error = device_private_init(dev);
906 * for statically allocated devices, which should all be converted
907 * some day, we need to initialize the name. We prevent reading back
908 * the name, and force the use of dev_name()
910 if (dev->init_name) {
911 dev_set_name(dev, "%s", dev->init_name);
912 dev->init_name = NULL;
915 if (!dev_name(dev)) {
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 devtmpfs_delete_node(dev);
1007 if (MAJOR(dev->devt))
1008 device_remove_sys_dev_entry(dev);
1010 if (MAJOR(dev->devt))
1011 device_remove_file(dev, &devt_attr);
1013 device_remove_file(dev, &uevent_attr);
1015 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1016 kobject_del(&dev->kobj);
1018 cleanup_device_parent(dev);
1028 * device_register - register a device with the system.
1029 * @dev: pointer to the device structure
1031 * This happens in two clean steps - initialize the device
1032 * and add it to the system. The two steps can be called
1033 * separately, but this is the easiest and most common.
1034 * I.e. you should only call the two helpers separately if
1035 * have a clearly defined need to use and refcount the device
1036 * before it is added to the hierarchy.
1038 * NOTE: _Never_ directly free @dev after calling this function, even
1039 * if it returned an error! Always use put_device() to give up the
1040 * reference initialized in this function instead.
1042 int device_register(struct device *dev)
1044 device_initialize(dev);
1045 return device_add(dev);
1049 * get_device - increment reference count for device.
1052 * This simply forwards the call to kobject_get(), though
1053 * we do take care to provide for the case that we get a NULL
1054 * pointer passed in.
1056 struct device *get_device(struct device *dev)
1058 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
1062 * put_device - decrement reference count.
1063 * @dev: device in question.
1065 void put_device(struct device *dev)
1067 /* might_sleep(); */
1069 kobject_put(&dev->kobj);
1073 * device_del - delete device from system.
1076 * This is the first part of the device unregistration
1077 * sequence. This removes the device from the lists we control
1078 * from here, has it removed from the other driver model
1079 * subsystems it was added to in device_add(), and removes it
1080 * from the kobject hierarchy.
1082 * NOTE: this should be called manually _iff_ device_add() was
1083 * also called manually.
1085 void device_del(struct device *dev)
1087 struct device *parent = dev->parent;
1088 struct class_interface *class_intf;
1090 /* Notify clients of device removal. This call must come
1091 * before dpm_sysfs_remove().
1094 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1095 BUS_NOTIFY_DEL_DEVICE, dev);
1096 device_pm_remove(dev);
1097 dpm_sysfs_remove(dev);
1099 klist_del(&dev->p->knode_parent);
1100 if (MAJOR(dev->devt)) {
1101 devtmpfs_delete_node(dev);
1102 device_remove_sys_dev_entry(dev);
1103 device_remove_file(dev, &devt_attr);
1106 device_remove_class_symlinks(dev);
1108 mutex_lock(&dev->class->p->class_mutex);
1109 /* notify any interfaces that the device is now gone */
1110 list_for_each_entry(class_intf,
1111 &dev->class->p->class_interfaces, node)
1112 if (class_intf->remove_dev)
1113 class_intf->remove_dev(dev, class_intf);
1114 /* remove the device from the class list */
1115 klist_del(&dev->knode_class);
1116 mutex_unlock(&dev->class->p->class_mutex);
1118 device_remove_file(dev, &uevent_attr);
1119 device_remove_attrs(dev);
1120 bus_remove_device(dev);
1123 * Some platform devices are driven without driver attached
1124 * and managed resources may have been acquired. Make sure
1125 * all resources are released.
1127 devres_release_all(dev);
1129 /* Notify the platform of the removal, in case they
1130 * need to do anything...
1132 if (platform_notify_remove)
1133 platform_notify_remove(dev);
1134 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1135 cleanup_device_parent(dev);
1136 kobject_del(&dev->kobj);
1141 * device_unregister - unregister device from system.
1142 * @dev: device going away.
1144 * We do this in two parts, like we do device_register(). First,
1145 * we remove it from all the subsystems with device_del(), then
1146 * we decrement the reference count via put_device(). If that
1147 * is the final reference count, the device will be cleaned up
1148 * via device_release() above. Otherwise, the structure will
1149 * stick around until the final reference to the device is dropped.
1151 void device_unregister(struct device *dev)
1153 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1158 static struct device *next_device(struct klist_iter *i)
1160 struct klist_node *n = klist_next(i);
1161 struct device *dev = NULL;
1162 struct device_private *p;
1165 p = to_device_private_parent(n);
1172 * device_get_devnode - path of device node file
1174 * @mode: returned file access mode
1175 * @tmp: possibly allocated string
1177 * Return the relative path of a possible device node.
1178 * Non-default names may need to allocate a memory to compose
1179 * a name. This memory is returned in tmp and needs to be
1180 * freed by the caller.
1182 const char *device_get_devnode(struct device *dev,
1183 mode_t *mode, const char **tmp)
1189 /* the device type may provide a specific name */
1190 if (dev->type && dev->type->devnode)
1191 *tmp = dev->type->devnode(dev, mode);
1195 /* the class may provide a specific name */
1196 if (dev->class && dev->class->devnode)
1197 *tmp = dev->class->devnode(dev, mode);
1201 /* return name without allocation, tmp == NULL */
1202 if (strchr(dev_name(dev), '!') == NULL)
1203 return dev_name(dev);
1205 /* replace '!' in the name with '/' */
1206 *tmp = kstrdup(dev_name(dev), GFP_KERNEL);
1209 while ((s = strchr(*tmp, '!')))
1215 * device_for_each_child - device child iterator.
1216 * @parent: parent struct device.
1217 * @data: data for the callback.
1218 * @fn: function to be called for each device.
1220 * Iterate over @parent's child devices, and call @fn for each,
1223 * We check the return of @fn each time. If it returns anything
1224 * other than 0, we break out and return that value.
1226 int device_for_each_child(struct device *parent, void *data,
1227 int (*fn)(struct device *dev, void *data))
1229 struct klist_iter i;
1230 struct device *child;
1236 klist_iter_init(&parent->p->klist_children, &i);
1237 while ((child = next_device(&i)) && !error)
1238 error = fn(child, data);
1239 klist_iter_exit(&i);
1244 * device_find_child - device iterator for locating a particular device.
1245 * @parent: parent struct device
1246 * @data: Data to pass to match function
1247 * @match: Callback function to check device
1249 * This is similar to the device_for_each_child() function above, but it
1250 * returns a reference to a device that is 'found' for later use, as
1251 * determined by the @match callback.
1253 * The callback should return 0 if the device doesn't match and non-zero
1254 * if it does. If the callback returns non-zero and a reference to the
1255 * current device can be obtained, this function will return to the caller
1256 * and not iterate over any more devices.
1258 struct device *device_find_child(struct device *parent, void *data,
1259 int (*match)(struct device *dev, void *data))
1261 struct klist_iter i;
1262 struct device *child;
1267 klist_iter_init(&parent->p->klist_children, &i);
1268 while ((child = next_device(&i)))
1269 if (match(child, data) && get_device(child))
1271 klist_iter_exit(&i);
1275 int __init devices_init(void)
1277 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1280 dev_kobj = kobject_create_and_add("dev", NULL);
1283 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1284 if (!sysfs_dev_block_kobj)
1285 goto block_kobj_err;
1286 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1287 if (!sysfs_dev_char_kobj)
1293 kobject_put(sysfs_dev_block_kobj);
1295 kobject_put(dev_kobj);
1297 kset_unregister(devices_kset);
1301 EXPORT_SYMBOL_GPL(device_for_each_child);
1302 EXPORT_SYMBOL_GPL(device_find_child);
1304 EXPORT_SYMBOL_GPL(device_initialize);
1305 EXPORT_SYMBOL_GPL(device_add);
1306 EXPORT_SYMBOL_GPL(device_register);
1308 EXPORT_SYMBOL_GPL(device_del);
1309 EXPORT_SYMBOL_GPL(device_unregister);
1310 EXPORT_SYMBOL_GPL(get_device);
1311 EXPORT_SYMBOL_GPL(put_device);
1313 EXPORT_SYMBOL_GPL(device_create_file);
1314 EXPORT_SYMBOL_GPL(device_remove_file);
1319 struct module *owner;
1322 #define to_root_device(dev) container_of(dev, struct root_device, dev)
1324 static void root_device_release(struct device *dev)
1326 kfree(to_root_device(dev));
1330 * __root_device_register - allocate and register a root device
1331 * @name: root device name
1332 * @owner: owner module of the root device, usually THIS_MODULE
1334 * This function allocates a root device and registers it
1335 * using device_register(). In order to free the returned
1336 * device, use root_device_unregister().
1338 * Root devices are dummy devices which allow other devices
1339 * to be grouped under /sys/devices. Use this function to
1340 * allocate a root device and then use it as the parent of
1341 * any device which should appear under /sys/devices/{name}
1343 * The /sys/devices/{name} directory will also contain a
1344 * 'module' symlink which points to the @owner directory
1347 * Returns &struct device pointer on success, or ERR_PTR() on error.
1349 * Note: You probably want to use root_device_register().
1351 struct device *__root_device_register(const char *name, struct module *owner)
1353 struct root_device *root;
1356 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
1358 return ERR_PTR(err);
1360 err = dev_set_name(&root->dev, "%s", name);
1363 return ERR_PTR(err);
1366 root->dev.release = root_device_release;
1368 err = device_register(&root->dev);
1370 put_device(&root->dev);
1371 return ERR_PTR(err);
1374 #ifdef CONFIG_MODULE /* gotta find a "cleaner" way to do this */
1376 struct module_kobject *mk = &owner->mkobj;
1378 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
1380 device_unregister(&root->dev);
1381 return ERR_PTR(err);
1383 root->owner = owner;
1389 EXPORT_SYMBOL_GPL(__root_device_register);
1392 * root_device_unregister - unregister and free a root device
1393 * @dev: device going away
1395 * This function unregisters and cleans up a device that was created by
1396 * root_device_register().
1398 void root_device_unregister(struct device *dev)
1400 struct root_device *root = to_root_device(dev);
1403 sysfs_remove_link(&root->dev.kobj, "module");
1405 device_unregister(dev);
1407 EXPORT_SYMBOL_GPL(root_device_unregister);
1410 static void device_create_release(struct device *dev)
1412 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1417 * device_create_vargs - creates a device and registers it with sysfs
1418 * @class: pointer to the struct class that this device should be registered to
1419 * @parent: pointer to the parent struct device of this new device, if any
1420 * @devt: the dev_t for the char device to be added
1421 * @drvdata: the data to be added to the device for callbacks
1422 * @fmt: string for the device's name
1423 * @args: va_list for the device's name
1425 * This function can be used by char device classes. A struct device
1426 * will be created in sysfs, registered to the specified class.
1428 * A "dev" file will be created, showing the dev_t for the device, if
1429 * the dev_t is not 0,0.
1430 * If a pointer to a parent struct device is passed in, the newly created
1431 * struct device will be a child of that device in sysfs.
1432 * The pointer to the struct device will be returned from the call.
1433 * Any further sysfs files that might be required can be created using this
1436 * Returns &struct device pointer on success, or ERR_PTR() on error.
1438 * Note: the struct class passed to this function must have previously
1439 * been created with a call to class_create().
1441 struct device *device_create_vargs(struct class *class, struct device *parent,
1442 dev_t devt, void *drvdata, const char *fmt,
1445 struct device *dev = NULL;
1446 int retval = -ENODEV;
1448 if (class == NULL || IS_ERR(class))
1451 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1459 dev->parent = parent;
1460 dev->release = device_create_release;
1461 dev_set_drvdata(dev, drvdata);
1463 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
1467 retval = device_register(dev);
1475 return ERR_PTR(retval);
1477 EXPORT_SYMBOL_GPL(device_create_vargs);
1480 * device_create - creates a device and registers it with sysfs
1481 * @class: pointer to the struct class that this device should be registered to
1482 * @parent: pointer to the parent struct device of this new device, if any
1483 * @devt: the dev_t for the char device to be added
1484 * @drvdata: the data to be added to the device for callbacks
1485 * @fmt: string for the device's name
1487 * This function can be used by char device classes. A struct device
1488 * will be created in sysfs, registered to the specified class.
1490 * A "dev" file will be created, showing the dev_t for the device, if
1491 * the dev_t is not 0,0.
1492 * If a pointer to a parent struct device is passed in, the newly created
1493 * struct device will be a child of that device in sysfs.
1494 * The pointer to the struct device will be returned from the call.
1495 * Any further sysfs files that might be required can be created using this
1498 * Returns &struct device pointer on success, or ERR_PTR() on error.
1500 * Note: the struct class passed to this function must have previously
1501 * been created with a call to class_create().
1503 struct device *device_create(struct class *class, struct device *parent,
1504 dev_t devt, void *drvdata, const char *fmt, ...)
1509 va_start(vargs, fmt);
1510 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
1514 EXPORT_SYMBOL_GPL(device_create);
1516 static int __match_devt(struct device *dev, void *data)
1520 return dev->devt == *devt;
1524 * device_destroy - removes a device that was created with device_create()
1525 * @class: pointer to the struct class that this device was registered with
1526 * @devt: the dev_t of the device that was previously registered
1528 * This call unregisters and cleans up a device that was created with a
1529 * call to device_create().
1531 void device_destroy(struct class *class, dev_t devt)
1535 dev = class_find_device(class, NULL, &devt, __match_devt);
1538 device_unregister(dev);
1541 EXPORT_SYMBOL_GPL(device_destroy);
1544 * device_rename - renames a device
1545 * @dev: the pointer to the struct device to be renamed
1546 * @new_name: the new name of the device
1548 * It is the responsibility of the caller to provide mutual
1549 * exclusion between two different calls of device_rename
1550 * on the same device to ensure that new_name is valid and
1551 * won't conflict with other devices.
1553 int device_rename(struct device *dev, char *new_name)
1555 char *old_class_name = NULL;
1556 char *new_class_name = NULL;
1557 char *old_device_name = NULL;
1560 dev = get_device(dev);
1564 pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
1565 __func__, new_name);
1567 #ifdef CONFIG_SYSFS_DEPRECATED
1568 if ((dev->class) && (dev->parent))
1569 old_class_name = make_class_name(dev->class->name, &dev->kobj);
1572 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
1573 if (!old_device_name) {
1578 error = kobject_rename(&dev->kobj, new_name);
1582 #ifdef CONFIG_SYSFS_DEPRECATED
1583 if (old_class_name) {
1584 new_class_name = make_class_name(dev->class->name, &dev->kobj);
1585 if (new_class_name) {
1586 error = sysfs_rename_link(&dev->parent->kobj,
1594 error = sysfs_rename_link(&dev->class->p->class_subsys.kobj,
1595 &dev->kobj, old_device_name, new_name);
1602 kfree(new_class_name);
1603 kfree(old_class_name);
1604 kfree(old_device_name);
1608 EXPORT_SYMBOL_GPL(device_rename);
1610 static int device_move_class_links(struct device *dev,
1611 struct device *old_parent,
1612 struct device *new_parent)
1615 #ifdef CONFIG_SYSFS_DEPRECATED
1618 class_name = make_class_name(dev->class->name, &dev->kobj);
1624 sysfs_remove_link(&dev->kobj, "device");
1625 sysfs_remove_link(&old_parent->kobj, class_name);
1628 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1632 error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
1635 sysfs_remove_link(&dev->kobj, "device");
1643 sysfs_remove_link(&dev->kobj, "device");
1645 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1652 * device_move - moves a device to a new parent
1653 * @dev: the pointer to the struct device to be moved
1654 * @new_parent: the new parent of the device (can by NULL)
1655 * @dpm_order: how to reorder the dpm_list
1657 int device_move(struct device *dev, struct device *new_parent,
1658 enum dpm_order dpm_order)
1661 struct device *old_parent;
1662 struct kobject *new_parent_kobj;
1664 dev = get_device(dev);
1669 new_parent = get_device(new_parent);
1670 new_parent_kobj = get_device_parent(dev, new_parent);
1672 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
1673 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
1674 error = kobject_move(&dev->kobj, new_parent_kobj);
1676 cleanup_glue_dir(dev, new_parent_kobj);
1677 put_device(new_parent);
1680 old_parent = dev->parent;
1681 dev->parent = new_parent;
1683 klist_remove(&dev->p->knode_parent);
1685 klist_add_tail(&dev->p->knode_parent,
1686 &new_parent->p->klist_children);
1687 set_dev_node(dev, dev_to_node(new_parent));
1692 error = device_move_class_links(dev, old_parent, new_parent);
1694 /* We ignore errors on cleanup since we're hosed anyway... */
1695 device_move_class_links(dev, new_parent, old_parent);
1696 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1698 klist_remove(&dev->p->knode_parent);
1699 dev->parent = old_parent;
1701 klist_add_tail(&dev->p->knode_parent,
1702 &old_parent->p->klist_children);
1703 set_dev_node(dev, dev_to_node(old_parent));
1706 cleanup_glue_dir(dev, new_parent_kobj);
1707 put_device(new_parent);
1710 switch (dpm_order) {
1711 case DPM_ORDER_NONE:
1713 case DPM_ORDER_DEV_AFTER_PARENT:
1714 device_pm_move_after(dev, new_parent);
1716 case DPM_ORDER_PARENT_BEFORE_DEV:
1717 device_pm_move_before(new_parent, dev);
1719 case DPM_ORDER_DEV_LAST:
1720 device_pm_move_last(dev);
1724 put_device(old_parent);
1730 EXPORT_SYMBOL_GPL(device_move);
1733 * device_shutdown - call ->shutdown() on each device to shutdown.
1735 void device_shutdown(void)
1737 struct device *dev, *devn;
1739 list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list,
1741 if (dev->bus && dev->bus->shutdown) {
1742 dev_dbg(dev, "shutdown\n");
1743 dev->bus->shutdown(dev);
1744 } else if (dev->driver && dev->driver->shutdown) {
1745 dev_dbg(dev, "shutdown\n");
1746 dev->driver->shutdown(dev);
1749 async_synchronize_full();