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 const void *device_namespace(struct kobject *kobj)
136 struct device *dev = to_dev(kobj);
137 const void *ns = NULL;
139 if (dev->class && dev->class->ns_type)
140 ns = dev->class->namespace(dev);
145 static struct kobj_type device_ktype = {
146 .release = device_release,
147 .sysfs_ops = &dev_sysfs_ops,
148 .namespace = device_namespace,
152 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
154 struct kobj_type *ktype = get_ktype(kobj);
156 if (ktype == &device_ktype) {
157 struct device *dev = to_dev(kobj);
166 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
168 struct device *dev = to_dev(kobj);
171 return dev->bus->name;
173 return dev->class->name;
177 static int dev_uevent(struct kset *kset, struct kobject *kobj,
178 struct kobj_uevent_env *env)
180 struct device *dev = to_dev(kobj);
183 /* add device node properties if present */
184 if (MAJOR(dev->devt)) {
189 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
190 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
191 name = device_get_devnode(dev, &mode, &tmp);
193 add_uevent_var(env, "DEVNAME=%s", name);
196 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
200 if (dev->type && dev->type->name)
201 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
204 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
206 #ifdef CONFIG_SYSFS_DEPRECATED
208 struct device *parent = dev->parent;
210 /* find first bus device in parent chain */
211 while (parent && !parent->bus)
212 parent = parent->parent;
213 if (parent && parent->bus) {
216 path = kobject_get_path(&parent->kobj, GFP_KERNEL);
218 add_uevent_var(env, "PHYSDEVPATH=%s", path);
222 add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name);
225 add_uevent_var(env, "PHYSDEVDRIVER=%s",
226 parent->driver->name);
228 } else if (dev->bus) {
229 add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
232 add_uevent_var(env, "PHYSDEVDRIVER=%s",
237 /* have the bus specific function add its stuff */
238 if (dev->bus && dev->bus->uevent) {
239 retval = dev->bus->uevent(dev, env);
241 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
242 dev_name(dev), __func__, retval);
245 /* have the class specific function add its stuff */
246 if (dev->class && dev->class->dev_uevent) {
247 retval = dev->class->dev_uevent(dev, env);
249 pr_debug("device: '%s': %s: class uevent() "
250 "returned %d\n", dev_name(dev),
254 /* have the device type specific fuction add its stuff */
255 if (dev->type && dev->type->uevent) {
256 retval = dev->type->uevent(dev, env);
258 pr_debug("device: '%s': %s: dev_type uevent() "
259 "returned %d\n", dev_name(dev),
266 static const struct kset_uevent_ops device_uevent_ops = {
267 .filter = dev_uevent_filter,
268 .name = dev_uevent_name,
269 .uevent = dev_uevent,
272 static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
275 struct kobject *top_kobj;
277 struct kobj_uevent_env *env = NULL;
282 /* search the kset, the device belongs to */
283 top_kobj = &dev->kobj;
284 while (!top_kobj->kset && top_kobj->parent)
285 top_kobj = top_kobj->parent;
289 kset = top_kobj->kset;
290 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
294 if (kset->uevent_ops && kset->uevent_ops->filter)
295 if (!kset->uevent_ops->filter(kset, &dev->kobj))
298 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
302 /* let the kset specific function add its keys */
303 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
307 /* copy keys to file */
308 for (i = 0; i < env->envp_idx; i++)
309 count += sprintf(&buf[count], "%s\n", env->envp[i]);
315 static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
316 const char *buf, size_t count)
318 enum kobject_action action;
320 if (kobject_action_type(buf, count, &action) == 0)
321 kobject_uevent(&dev->kobj, action);
323 dev_err(dev, "uevent: unknown action-string\n");
327 static struct device_attribute uevent_attr =
328 __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
330 static int device_add_attributes(struct device *dev,
331 struct device_attribute *attrs)
337 for (i = 0; attr_name(attrs[i]); i++) {
338 error = device_create_file(dev, &attrs[i]);
344 device_remove_file(dev, &attrs[i]);
349 static void device_remove_attributes(struct device *dev,
350 struct device_attribute *attrs)
355 for (i = 0; attr_name(attrs[i]); i++)
356 device_remove_file(dev, &attrs[i]);
359 static int device_add_groups(struct device *dev,
360 const struct attribute_group **groups)
366 for (i = 0; groups[i]; i++) {
367 error = sysfs_create_group(&dev->kobj, groups[i]);
370 sysfs_remove_group(&dev->kobj,
379 static void device_remove_groups(struct device *dev,
380 const struct attribute_group **groups)
385 for (i = 0; groups[i]; i++)
386 sysfs_remove_group(&dev->kobj, groups[i]);
389 static int device_add_attrs(struct device *dev)
391 struct class *class = dev->class;
392 struct device_type *type = dev->type;
396 error = device_add_attributes(dev, class->dev_attrs);
402 error = device_add_groups(dev, type->groups);
404 goto err_remove_class_attrs;
407 error = device_add_groups(dev, dev->groups);
409 goto err_remove_type_groups;
413 err_remove_type_groups:
415 device_remove_groups(dev, type->groups);
416 err_remove_class_attrs:
418 device_remove_attributes(dev, class->dev_attrs);
423 static void device_remove_attrs(struct device *dev)
425 struct class *class = dev->class;
426 struct device_type *type = dev->type;
428 device_remove_groups(dev, dev->groups);
431 device_remove_groups(dev, type->groups);
434 device_remove_attributes(dev, class->dev_attrs);
438 static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
441 return print_dev_t(buf, dev->devt);
444 static struct device_attribute devt_attr =
445 __ATTR(dev, S_IRUGO, show_dev, NULL);
447 /* kset to create /sys/devices/ */
448 struct kset *devices_kset;
451 * device_create_file - create sysfs attribute file for device.
453 * @attr: device attribute descriptor.
455 int device_create_file(struct device *dev,
456 const struct device_attribute *attr)
460 error = sysfs_create_file(&dev->kobj, &attr->attr);
465 * device_remove_file - remove sysfs attribute file.
467 * @attr: device attribute descriptor.
469 void device_remove_file(struct device *dev,
470 const struct device_attribute *attr)
473 sysfs_remove_file(&dev->kobj, &attr->attr);
477 * device_create_bin_file - create sysfs binary attribute file for device.
479 * @attr: device binary attribute descriptor.
481 int device_create_bin_file(struct device *dev,
482 const struct bin_attribute *attr)
486 error = sysfs_create_bin_file(&dev->kobj, attr);
489 EXPORT_SYMBOL_GPL(device_create_bin_file);
492 * device_remove_bin_file - remove sysfs binary attribute file
494 * @attr: device binary attribute descriptor.
496 void device_remove_bin_file(struct device *dev,
497 const struct bin_attribute *attr)
500 sysfs_remove_bin_file(&dev->kobj, attr);
502 EXPORT_SYMBOL_GPL(device_remove_bin_file);
505 * device_schedule_callback_owner - helper to schedule a callback for a device
507 * @func: callback function to invoke later.
508 * @owner: module owning the callback routine
510 * Attribute methods must not unregister themselves or their parent device
511 * (which would amount to the same thing). Attempts to do so will deadlock,
512 * since unregistration is mutually exclusive with driver callbacks.
514 * Instead methods can call this routine, which will attempt to allocate
515 * and schedule a workqueue request to call back @func with @dev as its
516 * argument in the workqueue's process context. @dev will be pinned until
519 * This routine is usually called via the inline device_schedule_callback(),
520 * which automatically sets @owner to THIS_MODULE.
522 * Returns 0 if the request was submitted, -ENOMEM if storage could not
523 * be allocated, -ENODEV if a reference to @owner isn't available.
525 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
526 * underlying sysfs routine (since it is intended for use by attribute
527 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
529 int device_schedule_callback_owner(struct device *dev,
530 void (*func)(struct device *), struct module *owner)
532 return sysfs_schedule_callback(&dev->kobj,
533 (void (*)(void *)) func, dev, owner);
535 EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
537 static void klist_children_get(struct klist_node *n)
539 struct device_private *p = to_device_private_parent(n);
540 struct device *dev = p->device;
545 static void klist_children_put(struct klist_node *n)
547 struct device_private *p = to_device_private_parent(n);
548 struct device *dev = p->device;
554 * device_initialize - init device structure.
557 * This prepares the device for use by other layers by initializing
559 * It is the first half of device_register(), if called by
560 * that function, though it can also be called separately, so one
561 * may use @dev's fields. In particular, get_device()/put_device()
562 * may be used for reference counting of @dev after calling this
565 * NOTE: Use put_device() to give up your reference instead of freeing
566 * @dev directly once you have called this function.
568 void device_initialize(struct device *dev)
570 dev->kobj.kset = devices_kset;
571 kobject_init(&dev->kobj, &device_ktype);
572 INIT_LIST_HEAD(&dev->dma_pools);
573 mutex_init(&dev->mutex);
574 lockdep_set_novalidate_class(&dev->mutex);
575 spin_lock_init(&dev->devres_lock);
576 INIT_LIST_HEAD(&dev->devres_head);
578 set_dev_node(dev, -1);
581 #ifdef CONFIG_SYSFS_DEPRECATED
582 static struct kobject *get_device_parent(struct device *dev,
583 struct device *parent)
585 /* class devices without a parent live in /sys/class/<classname>/ */
586 if (dev->class && (!parent || parent->class != dev->class))
587 return &dev->class->p->class_subsys.kobj;
588 /* all other devices keep their parent */
590 return &parent->kobj;
595 static inline void cleanup_device_parent(struct device *dev) {}
596 static inline void cleanup_glue_dir(struct device *dev,
597 struct kobject *glue_dir) {}
599 static struct kobject *virtual_device_parent(struct device *dev)
601 static struct kobject *virtual_dir = NULL;
604 virtual_dir = kobject_create_and_add("virtual",
605 &devices_kset->kobj);
615 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
617 static void class_dir_release(struct kobject *kobj)
619 struct class_dir *dir = to_class_dir(kobj);
624 struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
626 struct class_dir *dir = to_class_dir(kobj);
627 return dir->class->ns_type;
630 static struct kobj_type class_dir_ktype = {
631 .release = class_dir_release,
632 .sysfs_ops = &kobj_sysfs_ops,
633 .child_ns_type = class_dir_child_ns_type
636 static struct kobject *
637 class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
639 struct class_dir *dir;
642 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
647 kobject_init(&dir->kobj, &class_dir_ktype);
649 dir->kobj.kset = &class->p->class_dirs;
651 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
653 kobject_put(&dir->kobj);
660 static struct kobject *get_device_parent(struct device *dev,
661 struct device *parent)
664 static DEFINE_MUTEX(gdp_mutex);
665 struct kobject *kobj = NULL;
666 struct kobject *parent_kobj;
670 * If we have no parent, we live in "virtual".
671 * Class-devices with a non class-device as parent, live
672 * in a "glue" directory to prevent namespace collisions.
675 parent_kobj = virtual_device_parent(dev);
676 else if (parent->class && !dev->class->ns_type)
677 return &parent->kobj;
679 parent_kobj = &parent->kobj;
681 mutex_lock(&gdp_mutex);
683 /* find our class-directory at the parent and reference it */
684 spin_lock(&dev->class->p->class_dirs.list_lock);
685 list_for_each_entry(k, &dev->class->p->class_dirs.list, entry)
686 if (k->parent == parent_kobj) {
687 kobj = kobject_get(k);
690 spin_unlock(&dev->class->p->class_dirs.list_lock);
692 mutex_unlock(&gdp_mutex);
696 /* or create a new class-directory at the parent device */
697 k = class_dir_create_and_add(dev->class, parent_kobj);
698 /* do not emit an uevent for this simple "glue" directory */
699 mutex_unlock(&gdp_mutex);
704 return &parent->kobj;
708 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
710 /* see if we live in a "glue" directory */
711 if (!glue_dir || !dev->class ||
712 glue_dir->kset != &dev->class->p->class_dirs)
715 kobject_put(glue_dir);
718 static void cleanup_device_parent(struct device *dev)
720 cleanup_glue_dir(dev, dev->kobj.parent);
724 static void setup_parent(struct device *dev, struct device *parent)
726 struct kobject *kobj;
727 kobj = get_device_parent(dev, parent);
729 dev->kobj.parent = kobj;
732 static int device_add_class_symlinks(struct device *dev)
739 error = sysfs_create_link(&dev->kobj,
740 &dev->class->p->class_subsys.kobj,
745 #ifdef CONFIG_SYSFS_DEPRECATED
746 /* stacked class devices need a symlink in the class directory */
747 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
748 device_is_not_partition(dev)) {
749 error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
750 &dev->kobj, dev_name(dev));
755 if (dev->parent && device_is_not_partition(dev)) {
756 struct device *parent = dev->parent;
760 * stacked class devices have the 'device' link
761 * pointing to the bus device instead of the parent
763 while (parent->class && !parent->bus && parent->parent)
764 parent = parent->parent;
766 error = sysfs_create_link(&dev->kobj,
772 class_name = make_class_name(dev->class->name,
775 error = sysfs_create_link(&dev->parent->kobj,
776 &dev->kobj, class_name);
784 if (dev->parent && device_is_not_partition(dev))
785 sysfs_remove_link(&dev->kobj, "device");
787 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
788 device_is_not_partition(dev))
789 sysfs_delete_link(&dev->class->p->class_subsys.kobj, &dev->kobj,
792 /* link in the class directory pointing to the device */
793 error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
794 &dev->kobj, dev_name(dev));
798 if (dev->parent && device_is_not_partition(dev)) {
799 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
807 sysfs_delete_link(&dev->class->p->class_subsys.kobj, &dev->kobj, dev_name(dev));
811 sysfs_remove_link(&dev->kobj, "subsystem");
816 static void device_remove_class_symlinks(struct device *dev)
821 #ifdef CONFIG_SYSFS_DEPRECATED
822 if (dev->parent && device_is_not_partition(dev)) {
825 class_name = make_class_name(dev->class->name, &dev->kobj);
827 sysfs_remove_link(&dev->parent->kobj, class_name);
830 sysfs_remove_link(&dev->kobj, "device");
833 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
834 device_is_not_partition(dev))
835 sysfs_delete_link(&dev->class->p->class_subsys.kobj, &dev->kobj,
838 if (dev->parent && device_is_not_partition(dev))
839 sysfs_remove_link(&dev->kobj, "device");
841 sysfs_delete_link(&dev->class->p->class_subsys.kobj, &dev->kobj, dev_name(dev));
844 sysfs_remove_link(&dev->kobj, "subsystem");
848 * dev_set_name - set a device name
850 * @fmt: format string for the device's name
852 int dev_set_name(struct device *dev, const char *fmt, ...)
857 va_start(vargs, fmt);
858 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
862 EXPORT_SYMBOL_GPL(dev_set_name);
865 * device_to_dev_kobj - select a /sys/dev/ directory for the device
868 * By default we select char/ for new entries. Setting class->dev_obj
869 * to NULL prevents an entry from being created. class->dev_kobj must
870 * be set (or cleared) before any devices are registered to the class
871 * otherwise device_create_sys_dev_entry() and
872 * device_remove_sys_dev_entry() will disagree about the the presence
875 static struct kobject *device_to_dev_kobj(struct device *dev)
877 struct kobject *kobj;
880 kobj = dev->class->dev_kobj;
882 kobj = sysfs_dev_char_kobj;
887 static int device_create_sys_dev_entry(struct device *dev)
889 struct kobject *kobj = device_to_dev_kobj(dev);
894 format_dev_t(devt_str, dev->devt);
895 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
901 static void device_remove_sys_dev_entry(struct device *dev)
903 struct kobject *kobj = device_to_dev_kobj(dev);
907 format_dev_t(devt_str, dev->devt);
908 sysfs_remove_link(kobj, devt_str);
912 int device_private_init(struct device *dev)
914 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
917 dev->p->device = dev;
918 klist_init(&dev->p->klist_children, klist_children_get,
924 * device_add - add device to device hierarchy.
927 * This is part 2 of device_register(), though may be called
928 * separately _iff_ device_initialize() has been called separately.
930 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
931 * to the global and sibling lists for the device, then
932 * adds it to the other relevant subsystems of the driver model.
934 * NOTE: _Never_ directly free @dev after calling this function, even
935 * if it returned an error! Always use put_device() to give up your
938 int device_add(struct device *dev)
940 struct device *parent = NULL;
941 struct class_interface *class_intf;
944 dev = get_device(dev);
949 error = device_private_init(dev);
955 * for statically allocated devices, which should all be converted
956 * some day, we need to initialize the name. We prevent reading back
957 * the name, and force the use of dev_name()
959 if (dev->init_name) {
960 dev_set_name(dev, "%s", dev->init_name);
961 dev->init_name = NULL;
964 if (!dev_name(dev)) {
969 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
971 parent = get_device(dev->parent);
972 setup_parent(dev, parent);
974 /* use parent numa_node */
976 set_dev_node(dev, dev_to_node(parent));
978 /* first, register with generic layer. */
979 /* we require the name to be set before, and pass NULL */
980 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
984 /* notify platform of device entry */
986 platform_notify(dev);
988 error = device_create_file(dev, &uevent_attr);
992 if (MAJOR(dev->devt)) {
993 error = device_create_file(dev, &devt_attr);
995 goto ueventattrError;
997 error = device_create_sys_dev_entry(dev);
1001 devtmpfs_create_node(dev);
1004 error = device_add_class_symlinks(dev);
1007 error = device_add_attrs(dev);
1010 error = bus_add_device(dev);
1013 error = dpm_sysfs_add(dev);
1018 /* Notify clients of device addition. This call must come
1019 * after dpm_sysf_add() and before kobject_uevent().
1022 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1023 BUS_NOTIFY_ADD_DEVICE, dev);
1025 kobject_uevent(&dev->kobj, KOBJ_ADD);
1026 bus_probe_device(dev);
1028 klist_add_tail(&dev->p->knode_parent,
1029 &parent->p->klist_children);
1032 mutex_lock(&dev->class->p->class_mutex);
1033 /* tie the class to the device */
1034 klist_add_tail(&dev->knode_class,
1035 &dev->class->p->class_devices);
1037 /* notify any interfaces that the device is here */
1038 list_for_each_entry(class_intf,
1039 &dev->class->p->class_interfaces, node)
1040 if (class_intf->add_dev)
1041 class_intf->add_dev(dev, class_intf);
1042 mutex_unlock(&dev->class->p->class_mutex);
1048 bus_remove_device(dev);
1050 device_remove_attrs(dev);
1052 device_remove_class_symlinks(dev);
1054 if (MAJOR(dev->devt))
1055 devtmpfs_delete_node(dev);
1056 if (MAJOR(dev->devt))
1057 device_remove_sys_dev_entry(dev);
1059 if (MAJOR(dev->devt))
1060 device_remove_file(dev, &devt_attr);
1062 device_remove_file(dev, &uevent_attr);
1064 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1065 kobject_del(&dev->kobj);
1067 cleanup_device_parent(dev);
1077 * device_register - register a device with the system.
1078 * @dev: pointer to the device structure
1080 * This happens in two clean steps - initialize the device
1081 * and add it to the system. The two steps can be called
1082 * separately, but this is the easiest and most common.
1083 * I.e. you should only call the two helpers separately if
1084 * have a clearly defined need to use and refcount the device
1085 * before it is added to the hierarchy.
1087 * NOTE: _Never_ directly free @dev after calling this function, even
1088 * if it returned an error! Always use put_device() to give up the
1089 * reference initialized in this function instead.
1091 int device_register(struct device *dev)
1093 device_initialize(dev);
1094 return device_add(dev);
1098 * get_device - increment reference count for device.
1101 * This simply forwards the call to kobject_get(), though
1102 * we do take care to provide for the case that we get a NULL
1103 * pointer passed in.
1105 struct device *get_device(struct device *dev)
1107 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
1111 * put_device - decrement reference count.
1112 * @dev: device in question.
1114 void put_device(struct device *dev)
1116 /* might_sleep(); */
1118 kobject_put(&dev->kobj);
1122 * device_del - delete device from system.
1125 * This is the first part of the device unregistration
1126 * sequence. This removes the device from the lists we control
1127 * from here, has it removed from the other driver model
1128 * subsystems it was added to in device_add(), and removes it
1129 * from the kobject hierarchy.
1131 * NOTE: this should be called manually _iff_ device_add() was
1132 * also called manually.
1134 void device_del(struct device *dev)
1136 struct device *parent = dev->parent;
1137 struct class_interface *class_intf;
1139 /* Notify clients of device removal. This call must come
1140 * before dpm_sysfs_remove().
1143 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1144 BUS_NOTIFY_DEL_DEVICE, dev);
1145 device_pm_remove(dev);
1146 dpm_sysfs_remove(dev);
1148 klist_del(&dev->p->knode_parent);
1149 if (MAJOR(dev->devt)) {
1150 devtmpfs_delete_node(dev);
1151 device_remove_sys_dev_entry(dev);
1152 device_remove_file(dev, &devt_attr);
1155 device_remove_class_symlinks(dev);
1157 mutex_lock(&dev->class->p->class_mutex);
1158 /* notify any interfaces that the device is now gone */
1159 list_for_each_entry(class_intf,
1160 &dev->class->p->class_interfaces, node)
1161 if (class_intf->remove_dev)
1162 class_intf->remove_dev(dev, class_intf);
1163 /* remove the device from the class list */
1164 klist_del(&dev->knode_class);
1165 mutex_unlock(&dev->class->p->class_mutex);
1167 device_remove_file(dev, &uevent_attr);
1168 device_remove_attrs(dev);
1169 bus_remove_device(dev);
1172 * Some platform devices are driven without driver attached
1173 * and managed resources may have been acquired. Make sure
1174 * all resources are released.
1176 devres_release_all(dev);
1178 /* Notify the platform of the removal, in case they
1179 * need to do anything...
1181 if (platform_notify_remove)
1182 platform_notify_remove(dev);
1183 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1184 cleanup_device_parent(dev);
1185 kobject_del(&dev->kobj);
1190 * device_unregister - unregister device from system.
1191 * @dev: device going away.
1193 * We do this in two parts, like we do device_register(). First,
1194 * we remove it from all the subsystems with device_del(), then
1195 * we decrement the reference count via put_device(). If that
1196 * is the final reference count, the device will be cleaned up
1197 * via device_release() above. Otherwise, the structure will
1198 * stick around until the final reference to the device is dropped.
1200 void device_unregister(struct device *dev)
1202 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1207 static struct device *next_device(struct klist_iter *i)
1209 struct klist_node *n = klist_next(i);
1210 struct device *dev = NULL;
1211 struct device_private *p;
1214 p = to_device_private_parent(n);
1221 * device_get_devnode - path of device node file
1223 * @mode: returned file access mode
1224 * @tmp: possibly allocated string
1226 * Return the relative path of a possible device node.
1227 * Non-default names may need to allocate a memory to compose
1228 * a name. This memory is returned in tmp and needs to be
1229 * freed by the caller.
1231 const char *device_get_devnode(struct device *dev,
1232 mode_t *mode, const char **tmp)
1238 /* the device type may provide a specific name */
1239 if (dev->type && dev->type->devnode)
1240 *tmp = dev->type->devnode(dev, mode);
1244 /* the class may provide a specific name */
1245 if (dev->class && dev->class->devnode)
1246 *tmp = dev->class->devnode(dev, mode);
1250 /* return name without allocation, tmp == NULL */
1251 if (strchr(dev_name(dev), '!') == NULL)
1252 return dev_name(dev);
1254 /* replace '!' in the name with '/' */
1255 *tmp = kstrdup(dev_name(dev), GFP_KERNEL);
1258 while ((s = strchr(*tmp, '!')))
1264 * device_for_each_child - device child iterator.
1265 * @parent: parent struct device.
1266 * @data: data for the callback.
1267 * @fn: function to be called for each device.
1269 * Iterate over @parent's child devices, and call @fn for each,
1272 * We check the return of @fn each time. If it returns anything
1273 * other than 0, we break out and return that value.
1275 int device_for_each_child(struct device *parent, void *data,
1276 int (*fn)(struct device *dev, void *data))
1278 struct klist_iter i;
1279 struct device *child;
1285 klist_iter_init(&parent->p->klist_children, &i);
1286 while ((child = next_device(&i)) && !error)
1287 error = fn(child, data);
1288 klist_iter_exit(&i);
1293 * device_find_child - device iterator for locating a particular device.
1294 * @parent: parent struct device
1295 * @data: Data to pass to match function
1296 * @match: Callback function to check device
1298 * This is similar to the device_for_each_child() function above, but it
1299 * returns a reference to a device that is 'found' for later use, as
1300 * determined by the @match callback.
1302 * The callback should return 0 if the device doesn't match and non-zero
1303 * if it does. If the callback returns non-zero and a reference to the
1304 * current device can be obtained, this function will return to the caller
1305 * and not iterate over any more devices.
1307 struct device *device_find_child(struct device *parent, void *data,
1308 int (*match)(struct device *dev, void *data))
1310 struct klist_iter i;
1311 struct device *child;
1316 klist_iter_init(&parent->p->klist_children, &i);
1317 while ((child = next_device(&i)))
1318 if (match(child, data) && get_device(child))
1320 klist_iter_exit(&i);
1324 int __init devices_init(void)
1326 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1329 dev_kobj = kobject_create_and_add("dev", NULL);
1332 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1333 if (!sysfs_dev_block_kobj)
1334 goto block_kobj_err;
1335 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1336 if (!sysfs_dev_char_kobj)
1342 kobject_put(sysfs_dev_block_kobj);
1344 kobject_put(dev_kobj);
1346 kset_unregister(devices_kset);
1350 EXPORT_SYMBOL_GPL(device_for_each_child);
1351 EXPORT_SYMBOL_GPL(device_find_child);
1353 EXPORT_SYMBOL_GPL(device_initialize);
1354 EXPORT_SYMBOL_GPL(device_add);
1355 EXPORT_SYMBOL_GPL(device_register);
1357 EXPORT_SYMBOL_GPL(device_del);
1358 EXPORT_SYMBOL_GPL(device_unregister);
1359 EXPORT_SYMBOL_GPL(get_device);
1360 EXPORT_SYMBOL_GPL(put_device);
1362 EXPORT_SYMBOL_GPL(device_create_file);
1363 EXPORT_SYMBOL_GPL(device_remove_file);
1368 struct module *owner;
1371 #define to_root_device(dev) container_of(dev, struct root_device, dev)
1373 static void root_device_release(struct device *dev)
1375 kfree(to_root_device(dev));
1379 * __root_device_register - allocate and register a root device
1380 * @name: root device name
1381 * @owner: owner module of the root device, usually THIS_MODULE
1383 * This function allocates a root device and registers it
1384 * using device_register(). In order to free the returned
1385 * device, use root_device_unregister().
1387 * Root devices are dummy devices which allow other devices
1388 * to be grouped under /sys/devices. Use this function to
1389 * allocate a root device and then use it as the parent of
1390 * any device which should appear under /sys/devices/{name}
1392 * The /sys/devices/{name} directory will also contain a
1393 * 'module' symlink which points to the @owner directory
1396 * Returns &struct device pointer on success, or ERR_PTR() on error.
1398 * Note: You probably want to use root_device_register().
1400 struct device *__root_device_register(const char *name, struct module *owner)
1402 struct root_device *root;
1405 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
1407 return ERR_PTR(err);
1409 err = dev_set_name(&root->dev, "%s", name);
1412 return ERR_PTR(err);
1415 root->dev.release = root_device_release;
1417 err = device_register(&root->dev);
1419 put_device(&root->dev);
1420 return ERR_PTR(err);
1423 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
1425 struct module_kobject *mk = &owner->mkobj;
1427 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
1429 device_unregister(&root->dev);
1430 return ERR_PTR(err);
1432 root->owner = owner;
1438 EXPORT_SYMBOL_GPL(__root_device_register);
1441 * root_device_unregister - unregister and free a root device
1442 * @dev: device going away
1444 * This function unregisters and cleans up a device that was created by
1445 * root_device_register().
1447 void root_device_unregister(struct device *dev)
1449 struct root_device *root = to_root_device(dev);
1452 sysfs_remove_link(&root->dev.kobj, "module");
1454 device_unregister(dev);
1456 EXPORT_SYMBOL_GPL(root_device_unregister);
1459 static void device_create_release(struct device *dev)
1461 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1466 * device_create_vargs - creates a device and registers it with sysfs
1467 * @class: pointer to the struct class that this device should be registered to
1468 * @parent: pointer to the parent struct device of this new device, if any
1469 * @devt: the dev_t for the char device to be added
1470 * @drvdata: the data to be added to the device for callbacks
1471 * @fmt: string for the device's name
1472 * @args: va_list for the device's name
1474 * This function can be used by char device classes. A struct device
1475 * will be created in sysfs, registered to the specified class.
1477 * A "dev" file will be created, showing the dev_t for the device, if
1478 * the dev_t is not 0,0.
1479 * If a pointer to a parent struct device is passed in, the newly created
1480 * struct device will be a child of that device in sysfs.
1481 * The pointer to the struct device will be returned from the call.
1482 * Any further sysfs files that might be required can be created using this
1485 * Returns &struct device pointer on success, or ERR_PTR() on error.
1487 * Note: the struct class passed to this function must have previously
1488 * been created with a call to class_create().
1490 struct device *device_create_vargs(struct class *class, struct device *parent,
1491 dev_t devt, void *drvdata, const char *fmt,
1494 struct device *dev = NULL;
1495 int retval = -ENODEV;
1497 if (class == NULL || IS_ERR(class))
1500 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1508 dev->parent = parent;
1509 dev->release = device_create_release;
1510 dev_set_drvdata(dev, drvdata);
1512 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
1516 retval = device_register(dev);
1524 return ERR_PTR(retval);
1526 EXPORT_SYMBOL_GPL(device_create_vargs);
1529 * device_create - creates a device and registers it with sysfs
1530 * @class: pointer to the struct class that this device should be registered to
1531 * @parent: pointer to the parent struct device of this new device, if any
1532 * @devt: the dev_t for the char device to be added
1533 * @drvdata: the data to be added to the device for callbacks
1534 * @fmt: string for the device's name
1536 * This function can be used by char device classes. A struct device
1537 * will be created in sysfs, registered to the specified class.
1539 * A "dev" file will be created, showing the dev_t for the device, if
1540 * the dev_t is not 0,0.
1541 * If a pointer to a parent struct device is passed in, the newly created
1542 * struct device will be a child of that device in sysfs.
1543 * The pointer to the struct device will be returned from the call.
1544 * Any further sysfs files that might be required can be created using this
1547 * Returns &struct device pointer on success, or ERR_PTR() on error.
1549 * Note: the struct class passed to this function must have previously
1550 * been created with a call to class_create().
1552 struct device *device_create(struct class *class, struct device *parent,
1553 dev_t devt, void *drvdata, const char *fmt, ...)
1558 va_start(vargs, fmt);
1559 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
1563 EXPORT_SYMBOL_GPL(device_create);
1565 static int __match_devt(struct device *dev, void *data)
1569 return dev->devt == *devt;
1573 * device_destroy - removes a device that was created with device_create()
1574 * @class: pointer to the struct class that this device was registered with
1575 * @devt: the dev_t of the device that was previously registered
1577 * This call unregisters and cleans up a device that was created with a
1578 * call to device_create().
1580 void device_destroy(struct class *class, dev_t devt)
1584 dev = class_find_device(class, NULL, &devt, __match_devt);
1587 device_unregister(dev);
1590 EXPORT_SYMBOL_GPL(device_destroy);
1593 * device_rename - renames a device
1594 * @dev: the pointer to the struct device to be renamed
1595 * @new_name: the new name of the device
1597 * It is the responsibility of the caller to provide mutual
1598 * exclusion between two different calls of device_rename
1599 * on the same device to ensure that new_name is valid and
1600 * won't conflict with other devices.
1602 int device_rename(struct device *dev, char *new_name)
1604 char *old_class_name = NULL;
1605 char *new_class_name = NULL;
1606 char *old_device_name = NULL;
1609 dev = get_device(dev);
1613 pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
1614 __func__, new_name);
1616 #ifdef CONFIG_SYSFS_DEPRECATED
1617 if ((dev->class) && (dev->parent))
1618 old_class_name = make_class_name(dev->class->name, &dev->kobj);
1621 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
1622 if (!old_device_name) {
1627 #ifndef CONFIG_SYSFS_DEPRECATED
1629 error = sysfs_rename_link(&dev->class->p->class_subsys.kobj,
1630 &dev->kobj, old_device_name, new_name);
1635 error = kobject_rename(&dev->kobj, new_name);
1639 #ifdef CONFIG_SYSFS_DEPRECATED
1640 if (old_class_name) {
1641 new_class_name = make_class_name(dev->class->name, &dev->kobj);
1642 if (new_class_name) {
1643 error = sysfs_rename_link(&dev->parent->kobj,
1654 kfree(new_class_name);
1655 kfree(old_class_name);
1656 kfree(old_device_name);
1660 EXPORT_SYMBOL_GPL(device_rename);
1662 static int device_move_class_links(struct device *dev,
1663 struct device *old_parent,
1664 struct device *new_parent)
1667 #ifdef CONFIG_SYSFS_DEPRECATED
1670 class_name = make_class_name(dev->class->name, &dev->kobj);
1676 sysfs_remove_link(&dev->kobj, "device");
1677 sysfs_remove_link(&old_parent->kobj, class_name);
1680 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1684 error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
1687 sysfs_remove_link(&dev->kobj, "device");
1695 sysfs_remove_link(&dev->kobj, "device");
1697 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1704 * device_move - moves a device to a new parent
1705 * @dev: the pointer to the struct device to be moved
1706 * @new_parent: the new parent of the device (can by NULL)
1707 * @dpm_order: how to reorder the dpm_list
1709 int device_move(struct device *dev, struct device *new_parent,
1710 enum dpm_order dpm_order)
1713 struct device *old_parent;
1714 struct kobject *new_parent_kobj;
1716 dev = get_device(dev);
1721 new_parent = get_device(new_parent);
1722 new_parent_kobj = get_device_parent(dev, new_parent);
1724 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
1725 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
1726 error = kobject_move(&dev->kobj, new_parent_kobj);
1728 cleanup_glue_dir(dev, new_parent_kobj);
1729 put_device(new_parent);
1732 old_parent = dev->parent;
1733 dev->parent = new_parent;
1735 klist_remove(&dev->p->knode_parent);
1737 klist_add_tail(&dev->p->knode_parent,
1738 &new_parent->p->klist_children);
1739 set_dev_node(dev, dev_to_node(new_parent));
1744 error = device_move_class_links(dev, old_parent, new_parent);
1746 /* We ignore errors on cleanup since we're hosed anyway... */
1747 device_move_class_links(dev, new_parent, old_parent);
1748 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1750 klist_remove(&dev->p->knode_parent);
1751 dev->parent = old_parent;
1753 klist_add_tail(&dev->p->knode_parent,
1754 &old_parent->p->klist_children);
1755 set_dev_node(dev, dev_to_node(old_parent));
1758 cleanup_glue_dir(dev, new_parent_kobj);
1759 put_device(new_parent);
1762 switch (dpm_order) {
1763 case DPM_ORDER_NONE:
1765 case DPM_ORDER_DEV_AFTER_PARENT:
1766 device_pm_move_after(dev, new_parent);
1768 case DPM_ORDER_PARENT_BEFORE_DEV:
1769 device_pm_move_before(new_parent, dev);
1771 case DPM_ORDER_DEV_LAST:
1772 device_pm_move_last(dev);
1776 put_device(old_parent);
1782 EXPORT_SYMBOL_GPL(device_move);
1785 * device_shutdown - call ->shutdown() on each device to shutdown.
1787 void device_shutdown(void)
1791 spin_lock(&devices_kset->list_lock);
1793 * Walk the devices list backward, shutting down each in turn.
1794 * Beware that device unplug events may also start pulling
1795 * devices offline, even as the system is shutting down.
1797 while (!list_empty(&devices_kset->list)) {
1798 dev = list_entry(devices_kset->list.prev, struct device,
1802 * Make sure the device is off the kset list, in the
1803 * event that dev->*->shutdown() doesn't remove it.
1805 list_del_init(&dev->kobj.entry);
1806 spin_unlock(&devices_kset->list_lock);
1808 if (dev->bus && dev->bus->shutdown) {
1809 dev_dbg(dev, "shutdown\n");
1810 dev->bus->shutdown(dev);
1811 } else if (dev->driver && dev->driver->shutdown) {
1812 dev_dbg(dev, "shutdown\n");
1813 dev->driver->shutdown(dev);
1817 spin_lock(&devices_kset->list_lock);
1819 spin_unlock(&devices_kset->list_lock);
1820 async_synchronize_full();
1824 * Device logging functions
1827 #ifdef CONFIG_PRINTK
1829 static int __dev_printk(const char *level, const struct device *dev,
1830 struct va_format *vaf)
1833 return printk("%s(NULL device *): %pV", level, vaf);
1835 return printk("%s%s %s: %pV",
1836 level, dev_driver_string(dev), dev_name(dev), vaf);
1839 int dev_printk(const char *level, const struct device *dev,
1840 const char *fmt, ...)
1842 struct va_format vaf;
1846 va_start(args, fmt);
1851 r = __dev_printk(level, dev, &vaf);
1856 EXPORT_SYMBOL(dev_printk);
1858 #define define_dev_printk_level(func, kern_level) \
1859 int func(const struct device *dev, const char *fmt, ...) \
1861 struct va_format vaf; \
1865 va_start(args, fmt); \
1870 r = __dev_printk(kern_level, dev, &vaf); \
1875 EXPORT_SYMBOL(func);
1877 define_dev_printk_level(dev_emerg, KERN_EMERG);
1878 define_dev_printk_level(dev_alert, KERN_ALERT);
1879 define_dev_printk_level(dev_crit, KERN_CRIT);
1880 define_dev_printk_level(dev_err, KERN_ERR);
1881 define_dev_printk_level(dev_warn, KERN_WARNING);
1882 define_dev_printk_level(dev_notice, KERN_NOTICE);
1883 define_dev_printk_level(_dev_info, KERN_INFO);