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)) {
170 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
171 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
172 name = device_get_nodename(dev, &tmp);
174 add_uevent_var(env, "DEVNAME=%s", name);
179 if (dev->type && dev->type->name)
180 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
183 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
185 #ifdef CONFIG_SYSFS_DEPRECATED
187 struct device *parent = dev->parent;
189 /* find first bus device in parent chain */
190 while (parent && !parent->bus)
191 parent = parent->parent;
192 if (parent && parent->bus) {
195 path = kobject_get_path(&parent->kobj, GFP_KERNEL);
197 add_uevent_var(env, "PHYSDEVPATH=%s", path);
201 add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name);
204 add_uevent_var(env, "PHYSDEVDRIVER=%s",
205 parent->driver->name);
207 } else if (dev->bus) {
208 add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
211 add_uevent_var(env, "PHYSDEVDRIVER=%s",
216 /* have the bus specific function add its stuff */
217 if (dev->bus && dev->bus->uevent) {
218 retval = dev->bus->uevent(dev, env);
220 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
221 dev_name(dev), __func__, retval);
224 /* have the class specific function add its stuff */
225 if (dev->class && dev->class->dev_uevent) {
226 retval = dev->class->dev_uevent(dev, env);
228 pr_debug("device: '%s': %s: class uevent() "
229 "returned %d\n", dev_name(dev),
233 /* have the device type specific fuction add its stuff */
234 if (dev->type && dev->type->uevent) {
235 retval = dev->type->uevent(dev, env);
237 pr_debug("device: '%s': %s: dev_type uevent() "
238 "returned %d\n", dev_name(dev),
245 static struct kset_uevent_ops device_uevent_ops = {
246 .filter = dev_uevent_filter,
247 .name = dev_uevent_name,
248 .uevent = dev_uevent,
251 static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
254 struct kobject *top_kobj;
256 struct kobj_uevent_env *env = NULL;
261 /* search the kset, the device belongs to */
262 top_kobj = &dev->kobj;
263 while (!top_kobj->kset && top_kobj->parent)
264 top_kobj = top_kobj->parent;
268 kset = top_kobj->kset;
269 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
273 if (kset->uevent_ops && kset->uevent_ops->filter)
274 if (!kset->uevent_ops->filter(kset, &dev->kobj))
277 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
281 /* let the kset specific function add its keys */
282 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
286 /* copy keys to file */
287 for (i = 0; i < env->envp_idx; i++)
288 count += sprintf(&buf[count], "%s\n", env->envp[i]);
294 static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
295 const char *buf, size_t count)
297 enum kobject_action action;
299 if (kobject_action_type(buf, count, &action) == 0) {
300 kobject_uevent(&dev->kobj, action);
304 dev_err(dev, "uevent: unsupported action-string; this will "
305 "be ignored in a future kernel version\n");
306 kobject_uevent(&dev->kobj, KOBJ_ADD);
311 static struct device_attribute uevent_attr =
312 __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
314 static int device_add_attributes(struct device *dev,
315 struct device_attribute *attrs)
321 for (i = 0; attr_name(attrs[i]); i++) {
322 error = device_create_file(dev, &attrs[i]);
328 device_remove_file(dev, &attrs[i]);
333 static void device_remove_attributes(struct device *dev,
334 struct device_attribute *attrs)
339 for (i = 0; attr_name(attrs[i]); i++)
340 device_remove_file(dev, &attrs[i]);
343 static int device_add_groups(struct device *dev,
344 const struct attribute_group **groups)
350 for (i = 0; groups[i]; i++) {
351 error = sysfs_create_group(&dev->kobj, groups[i]);
354 sysfs_remove_group(&dev->kobj,
363 static void device_remove_groups(struct device *dev,
364 const struct attribute_group **groups)
369 for (i = 0; groups[i]; i++)
370 sysfs_remove_group(&dev->kobj, groups[i]);
373 static int device_add_attrs(struct device *dev)
375 struct class *class = dev->class;
376 struct device_type *type = dev->type;
380 error = device_add_attributes(dev, class->dev_attrs);
386 error = device_add_groups(dev, type->groups);
388 goto err_remove_class_attrs;
391 error = device_add_groups(dev, dev->groups);
393 goto err_remove_type_groups;
397 err_remove_type_groups:
399 device_remove_groups(dev, type->groups);
400 err_remove_class_attrs:
402 device_remove_attributes(dev, class->dev_attrs);
407 static void device_remove_attrs(struct device *dev)
409 struct class *class = dev->class;
410 struct device_type *type = dev->type;
412 device_remove_groups(dev, dev->groups);
415 device_remove_groups(dev, type->groups);
418 device_remove_attributes(dev, class->dev_attrs);
422 static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
425 return print_dev_t(buf, dev->devt);
428 static struct device_attribute devt_attr =
429 __ATTR(dev, S_IRUGO, show_dev, NULL);
431 /* kset to create /sys/devices/ */
432 struct kset *devices_kset;
435 * device_create_file - create sysfs attribute file for device.
437 * @attr: device attribute descriptor.
439 int device_create_file(struct device *dev, struct device_attribute *attr)
443 error = sysfs_create_file(&dev->kobj, &attr->attr);
448 * device_remove_file - remove sysfs attribute file.
450 * @attr: device attribute descriptor.
452 void device_remove_file(struct device *dev, struct device_attribute *attr)
455 sysfs_remove_file(&dev->kobj, &attr->attr);
459 * device_create_bin_file - create sysfs binary attribute file for device.
461 * @attr: device binary attribute descriptor.
463 int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
467 error = sysfs_create_bin_file(&dev->kobj, attr);
470 EXPORT_SYMBOL_GPL(device_create_bin_file);
473 * device_remove_bin_file - remove sysfs binary attribute file
475 * @attr: device binary attribute descriptor.
477 void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
480 sysfs_remove_bin_file(&dev->kobj, attr);
482 EXPORT_SYMBOL_GPL(device_remove_bin_file);
485 * device_schedule_callback_owner - helper to schedule a callback for a device
487 * @func: callback function to invoke later.
488 * @owner: module owning the callback routine
490 * Attribute methods must not unregister themselves or their parent device
491 * (which would amount to the same thing). Attempts to do so will deadlock,
492 * since unregistration is mutually exclusive with driver callbacks.
494 * Instead methods can call this routine, which will attempt to allocate
495 * and schedule a workqueue request to call back @func with @dev as its
496 * argument in the workqueue's process context. @dev will be pinned until
499 * This routine is usually called via the inline device_schedule_callback(),
500 * which automatically sets @owner to THIS_MODULE.
502 * Returns 0 if the request was submitted, -ENOMEM if storage could not
503 * be allocated, -ENODEV if a reference to @owner isn't available.
505 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
506 * underlying sysfs routine (since it is intended for use by attribute
507 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
509 int device_schedule_callback_owner(struct device *dev,
510 void (*func)(struct device *), struct module *owner)
512 return sysfs_schedule_callback(&dev->kobj,
513 (void (*)(void *)) func, dev, owner);
515 EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
517 static void klist_children_get(struct klist_node *n)
519 struct device_private *p = to_device_private_parent(n);
520 struct device *dev = p->device;
525 static void klist_children_put(struct klist_node *n)
527 struct device_private *p = to_device_private_parent(n);
528 struct device *dev = p->device;
534 * device_initialize - init device structure.
537 * This prepares the device for use by other layers by initializing
539 * It is the first half of device_register(), if called by
540 * that function, though it can also be called separately, so one
541 * may use @dev's fields. In particular, get_device()/put_device()
542 * may be used for reference counting of @dev after calling this
545 * NOTE: Use put_device() to give up your reference instead of freeing
546 * @dev directly once you have called this function.
548 void device_initialize(struct device *dev)
550 dev->kobj.kset = devices_kset;
551 kobject_init(&dev->kobj, &device_ktype);
552 INIT_LIST_HEAD(&dev->dma_pools);
553 init_MUTEX(&dev->sem);
554 spin_lock_init(&dev->devres_lock);
555 INIT_LIST_HEAD(&dev->devres_head);
556 device_init_wakeup(dev, 0);
558 set_dev_node(dev, -1);
561 #ifdef CONFIG_SYSFS_DEPRECATED
562 static struct kobject *get_device_parent(struct device *dev,
563 struct device *parent)
565 /* class devices without a parent live in /sys/class/<classname>/ */
566 if (dev->class && (!parent || parent->class != dev->class))
567 return &dev->class->p->class_subsys.kobj;
568 /* all other devices keep their parent */
570 return &parent->kobj;
575 static inline void cleanup_device_parent(struct device *dev) {}
576 static inline void cleanup_glue_dir(struct device *dev,
577 struct kobject *glue_dir) {}
579 static struct kobject *virtual_device_parent(struct device *dev)
581 static struct kobject *virtual_dir = NULL;
584 virtual_dir = kobject_create_and_add("virtual",
585 &devices_kset->kobj);
590 static struct kobject *get_device_parent(struct device *dev,
591 struct device *parent)
596 struct kobject *kobj = NULL;
597 struct kobject *parent_kobj;
601 * If we have no parent, we live in "virtual".
602 * Class-devices with a non class-device as parent, live
603 * in a "glue" directory to prevent namespace collisions.
606 parent_kobj = virtual_device_parent(dev);
607 else if (parent->class)
608 return &parent->kobj;
610 parent_kobj = &parent->kobj;
612 /* find our class-directory at the parent and reference it */
613 spin_lock(&dev->class->p->class_dirs.list_lock);
614 list_for_each_entry(k, &dev->class->p->class_dirs.list, entry)
615 if (k->parent == parent_kobj) {
616 kobj = kobject_get(k);
619 spin_unlock(&dev->class->p->class_dirs.list_lock);
623 /* or create a new class-directory at the parent device */
624 k = kobject_create();
627 k->kset = &dev->class->p->class_dirs;
628 retval = kobject_add(k, parent_kobj, "%s", dev->class->name);
633 /* do not emit an uevent for this simple "glue" directory */
638 return &parent->kobj;
642 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
644 /* see if we live in a "glue" directory */
645 if (!glue_dir || !dev->class ||
646 glue_dir->kset != &dev->class->p->class_dirs)
649 kobject_put(glue_dir);
652 static void cleanup_device_parent(struct device *dev)
654 cleanup_glue_dir(dev, dev->kobj.parent);
658 static void setup_parent(struct device *dev, struct device *parent)
660 struct kobject *kobj;
661 kobj = get_device_parent(dev, parent);
663 dev->kobj.parent = kobj;
666 static int device_add_class_symlinks(struct device *dev)
673 error = sysfs_create_link(&dev->kobj,
674 &dev->class->p->class_subsys.kobj,
679 #ifdef CONFIG_SYSFS_DEPRECATED
680 /* stacked class devices need a symlink in the class directory */
681 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
682 device_is_not_partition(dev)) {
683 error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
684 &dev->kobj, dev_name(dev));
689 if (dev->parent && device_is_not_partition(dev)) {
690 struct device *parent = dev->parent;
694 * stacked class devices have the 'device' link
695 * pointing to the bus device instead of the parent
697 while (parent->class && !parent->bus && parent->parent)
698 parent = parent->parent;
700 error = sysfs_create_link(&dev->kobj,
706 class_name = make_class_name(dev->class->name,
709 error = sysfs_create_link(&dev->parent->kobj,
710 &dev->kobj, class_name);
718 if (dev->parent && device_is_not_partition(dev))
719 sysfs_remove_link(&dev->kobj, "device");
721 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
722 device_is_not_partition(dev))
723 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
726 /* link in the class directory pointing to the device */
727 error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
728 &dev->kobj, dev_name(dev));
732 if (dev->parent && device_is_not_partition(dev)) {
733 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
741 sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
745 sysfs_remove_link(&dev->kobj, "subsystem");
750 static void device_remove_class_symlinks(struct device *dev)
755 #ifdef CONFIG_SYSFS_DEPRECATED
756 if (dev->parent && device_is_not_partition(dev)) {
759 class_name = make_class_name(dev->class->name, &dev->kobj);
761 sysfs_remove_link(&dev->parent->kobj, class_name);
764 sysfs_remove_link(&dev->kobj, "device");
767 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
768 device_is_not_partition(dev))
769 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
772 if (dev->parent && device_is_not_partition(dev))
773 sysfs_remove_link(&dev->kobj, "device");
775 sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
778 sysfs_remove_link(&dev->kobj, "subsystem");
782 * dev_set_name - set a device name
784 * @fmt: format string for the device's name
786 int dev_set_name(struct device *dev, const char *fmt, ...)
791 va_start(vargs, fmt);
792 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
796 EXPORT_SYMBOL_GPL(dev_set_name);
799 * device_to_dev_kobj - select a /sys/dev/ directory for the device
802 * By default we select char/ for new entries. Setting class->dev_obj
803 * to NULL prevents an entry from being created. class->dev_kobj must
804 * be set (or cleared) before any devices are registered to the class
805 * otherwise device_create_sys_dev_entry() and
806 * device_remove_sys_dev_entry() will disagree about the the presence
809 static struct kobject *device_to_dev_kobj(struct device *dev)
811 struct kobject *kobj;
814 kobj = dev->class->dev_kobj;
816 kobj = sysfs_dev_char_kobj;
821 static int device_create_sys_dev_entry(struct device *dev)
823 struct kobject *kobj = device_to_dev_kobj(dev);
828 format_dev_t(devt_str, dev->devt);
829 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
835 static void device_remove_sys_dev_entry(struct device *dev)
837 struct kobject *kobj = device_to_dev_kobj(dev);
841 format_dev_t(devt_str, dev->devt);
842 sysfs_remove_link(kobj, devt_str);
846 int device_private_init(struct device *dev)
848 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
851 dev->p->device = dev;
852 klist_init(&dev->p->klist_children, klist_children_get,
858 * device_add - add device to device hierarchy.
861 * This is part 2 of device_register(), though may be called
862 * separately _iff_ device_initialize() has been called separately.
864 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
865 * to the global and sibling lists for the device, then
866 * adds it to the other relevant subsystems of the driver model.
868 * NOTE: _Never_ directly free @dev after calling this function, even
869 * if it returned an error! Always use put_device() to give up your
872 int device_add(struct device *dev)
874 struct device *parent = NULL;
875 struct class_interface *class_intf;
878 dev = get_device(dev);
883 error = device_private_init(dev);
889 * for statically allocated devices, which should all be converted
890 * some day, we need to initialize the name. We prevent reading back
891 * the name, and force the use of dev_name()
893 if (dev->init_name) {
894 dev_set_name(dev, "%s", dev->init_name);
895 dev->init_name = NULL;
901 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
903 parent = get_device(dev->parent);
904 setup_parent(dev, parent);
906 /* use parent numa_node */
908 set_dev_node(dev, dev_to_node(parent));
910 /* first, register with generic layer. */
911 /* we require the name to be set before, and pass NULL */
912 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
916 /* notify platform of device entry */
918 platform_notify(dev);
920 error = device_create_file(dev, &uevent_attr);
924 if (MAJOR(dev->devt)) {
925 error = device_create_file(dev, &devt_attr);
927 goto ueventattrError;
929 error = device_create_sys_dev_entry(dev);
934 error = device_add_class_symlinks(dev);
937 error = device_add_attrs(dev);
940 error = bus_add_device(dev);
943 error = dpm_sysfs_add(dev);
948 /* Notify clients of device addition. This call must come
949 * after dpm_sysf_add() and before kobject_uevent().
952 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
953 BUS_NOTIFY_ADD_DEVICE, dev);
955 kobject_uevent(&dev->kobj, KOBJ_ADD);
956 bus_probe_device(dev);
958 klist_add_tail(&dev->p->knode_parent,
959 &parent->p->klist_children);
962 mutex_lock(&dev->class->p->class_mutex);
963 /* tie the class to the device */
964 klist_add_tail(&dev->knode_class,
965 &dev->class->p->class_devices);
967 /* notify any interfaces that the device is here */
968 list_for_each_entry(class_intf,
969 &dev->class->p->class_interfaces, node)
970 if (class_intf->add_dev)
971 class_intf->add_dev(dev, class_intf);
972 mutex_unlock(&dev->class->p->class_mutex);
978 bus_remove_device(dev);
980 device_remove_attrs(dev);
982 device_remove_class_symlinks(dev);
984 if (MAJOR(dev->devt))
985 device_remove_sys_dev_entry(dev);
987 if (MAJOR(dev->devt))
988 device_remove_file(dev, &devt_attr);
990 device_remove_file(dev, &uevent_attr);
992 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
993 kobject_del(&dev->kobj);
995 cleanup_device_parent(dev);
1005 * device_register - register a device with the system.
1006 * @dev: pointer to the device structure
1008 * This happens in two clean steps - initialize the device
1009 * and add it to the system. The two steps can be called
1010 * separately, but this is the easiest and most common.
1011 * I.e. you should only call the two helpers separately if
1012 * have a clearly defined need to use and refcount the device
1013 * before it is added to the hierarchy.
1015 * NOTE: _Never_ directly free @dev after calling this function, even
1016 * if it returned an error! Always use put_device() to give up the
1017 * reference initialized in this function instead.
1019 int device_register(struct device *dev)
1021 device_initialize(dev);
1022 return device_add(dev);
1026 * get_device - increment reference count for device.
1029 * This simply forwards the call to kobject_get(), though
1030 * we do take care to provide for the case that we get a NULL
1031 * pointer passed in.
1033 struct device *get_device(struct device *dev)
1035 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
1039 * put_device - decrement reference count.
1040 * @dev: device in question.
1042 void put_device(struct device *dev)
1044 /* might_sleep(); */
1046 kobject_put(&dev->kobj);
1050 * device_del - delete device from system.
1053 * This is the first part of the device unregistration
1054 * sequence. This removes the device from the lists we control
1055 * from here, has it removed from the other driver model
1056 * subsystems it was added to in device_add(), and removes it
1057 * from the kobject hierarchy.
1059 * NOTE: this should be called manually _iff_ device_add() was
1060 * also called manually.
1062 void device_del(struct device *dev)
1064 struct device *parent = dev->parent;
1065 struct class_interface *class_intf;
1067 /* Notify clients of device removal. This call must come
1068 * before dpm_sysfs_remove().
1071 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1072 BUS_NOTIFY_DEL_DEVICE, dev);
1073 device_pm_remove(dev);
1074 dpm_sysfs_remove(dev);
1076 klist_del(&dev->p->knode_parent);
1077 if (MAJOR(dev->devt)) {
1078 device_remove_sys_dev_entry(dev);
1079 device_remove_file(dev, &devt_attr);
1082 device_remove_class_symlinks(dev);
1084 mutex_lock(&dev->class->p->class_mutex);
1085 /* notify any interfaces that the device is now gone */
1086 list_for_each_entry(class_intf,
1087 &dev->class->p->class_interfaces, node)
1088 if (class_intf->remove_dev)
1089 class_intf->remove_dev(dev, class_intf);
1090 /* remove the device from the class list */
1091 klist_del(&dev->knode_class);
1092 mutex_unlock(&dev->class->p->class_mutex);
1094 device_remove_file(dev, &uevent_attr);
1095 device_remove_attrs(dev);
1096 bus_remove_device(dev);
1099 * Some platform devices are driven without driver attached
1100 * and managed resources may have been acquired. Make sure
1101 * all resources are released.
1103 devres_release_all(dev);
1105 /* Notify the platform of the removal, in case they
1106 * need to do anything...
1108 if (platform_notify_remove)
1109 platform_notify_remove(dev);
1110 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1111 cleanup_device_parent(dev);
1112 kobject_del(&dev->kobj);
1117 * device_unregister - unregister device from system.
1118 * @dev: device going away.
1120 * We do this in two parts, like we do device_register(). First,
1121 * we remove it from all the subsystems with device_del(), then
1122 * we decrement the reference count via put_device(). If that
1123 * is the final reference count, the device will be cleaned up
1124 * via device_release() above. Otherwise, the structure will
1125 * stick around until the final reference to the device is dropped.
1127 void device_unregister(struct device *dev)
1129 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1134 static struct device *next_device(struct klist_iter *i)
1136 struct klist_node *n = klist_next(i);
1137 struct device *dev = NULL;
1138 struct device_private *p;
1141 p = to_device_private_parent(n);
1148 * device_get_nodename - path of device node file
1150 * @tmp: possibly allocated string
1152 * Return the relative path of a possible device node.
1153 * Non-default names may need to allocate a memory to compose
1154 * a name. This memory is returned in tmp and needs to be
1155 * freed by the caller.
1157 const char *device_get_nodename(struct device *dev, const char **tmp)
1163 /* the device type may provide a specific name */
1164 if (dev->type && dev->type->nodename)
1165 *tmp = dev->type->nodename(dev);
1169 /* the class may provide a specific name */
1170 if (dev->class && dev->class->nodename)
1171 *tmp = dev->class->nodename(dev);
1175 /* return name without allocation, tmp == NULL */
1176 if (strchr(dev_name(dev), '!') == NULL)
1177 return dev_name(dev);
1179 /* replace '!' in the name with '/' */
1180 *tmp = kstrdup(dev_name(dev), GFP_KERNEL);
1183 while ((s = strchr(*tmp, '!')))
1189 * device_for_each_child - device child iterator.
1190 * @parent: parent struct device.
1191 * @data: data for the callback.
1192 * @fn: function to be called for each device.
1194 * Iterate over @parent's child devices, and call @fn for each,
1197 * We check the return of @fn each time. If it returns anything
1198 * other than 0, we break out and return that value.
1200 int device_for_each_child(struct device *parent, void *data,
1201 int (*fn)(struct device *dev, void *data))
1203 struct klist_iter i;
1204 struct device *child;
1210 klist_iter_init(&parent->p->klist_children, &i);
1211 while ((child = next_device(&i)) && !error)
1212 error = fn(child, data);
1213 klist_iter_exit(&i);
1218 * device_find_child - device iterator for locating a particular device.
1219 * @parent: parent struct device
1220 * @data: Data to pass to match function
1221 * @match: Callback function to check device
1223 * This is similar to the device_for_each_child() function above, but it
1224 * returns a reference to a device that is 'found' for later use, as
1225 * determined by the @match callback.
1227 * The callback should return 0 if the device doesn't match and non-zero
1228 * if it does. If the callback returns non-zero and a reference to the
1229 * current device can be obtained, this function will return to the caller
1230 * and not iterate over any more devices.
1232 struct device *device_find_child(struct device *parent, void *data,
1233 int (*match)(struct device *dev, void *data))
1235 struct klist_iter i;
1236 struct device *child;
1241 klist_iter_init(&parent->p->klist_children, &i);
1242 while ((child = next_device(&i)))
1243 if (match(child, data) && get_device(child))
1245 klist_iter_exit(&i);
1249 int __init devices_init(void)
1251 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1254 dev_kobj = kobject_create_and_add("dev", NULL);
1257 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1258 if (!sysfs_dev_block_kobj)
1259 goto block_kobj_err;
1260 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1261 if (!sysfs_dev_char_kobj)
1267 kobject_put(sysfs_dev_block_kobj);
1269 kobject_put(dev_kobj);
1271 kset_unregister(devices_kset);
1275 EXPORT_SYMBOL_GPL(device_for_each_child);
1276 EXPORT_SYMBOL_GPL(device_find_child);
1278 EXPORT_SYMBOL_GPL(device_initialize);
1279 EXPORT_SYMBOL_GPL(device_add);
1280 EXPORT_SYMBOL_GPL(device_register);
1282 EXPORT_SYMBOL_GPL(device_del);
1283 EXPORT_SYMBOL_GPL(device_unregister);
1284 EXPORT_SYMBOL_GPL(get_device);
1285 EXPORT_SYMBOL_GPL(put_device);
1287 EXPORT_SYMBOL_GPL(device_create_file);
1288 EXPORT_SYMBOL_GPL(device_remove_file);
1293 struct module *owner;
1296 #define to_root_device(dev) container_of(dev, struct root_device, dev)
1298 static void root_device_release(struct device *dev)
1300 kfree(to_root_device(dev));
1304 * __root_device_register - allocate and register a root device
1305 * @name: root device name
1306 * @owner: owner module of the root device, usually THIS_MODULE
1308 * This function allocates a root device and registers it
1309 * using device_register(). In order to free the returned
1310 * device, use root_device_unregister().
1312 * Root devices are dummy devices which allow other devices
1313 * to be grouped under /sys/devices. Use this function to
1314 * allocate a root device and then use it as the parent of
1315 * any device which should appear under /sys/devices/{name}
1317 * The /sys/devices/{name} directory will also contain a
1318 * 'module' symlink which points to the @owner directory
1321 * Note: You probably want to use root_device_register().
1323 struct device *__root_device_register(const char *name, struct module *owner)
1325 struct root_device *root;
1328 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
1330 return ERR_PTR(err);
1332 err = dev_set_name(&root->dev, "%s", name);
1335 return ERR_PTR(err);
1338 root->dev.release = root_device_release;
1340 err = device_register(&root->dev);
1342 put_device(&root->dev);
1343 return ERR_PTR(err);
1346 #ifdef CONFIG_MODULE /* gotta find a "cleaner" way to do this */
1348 struct module_kobject *mk = &owner->mkobj;
1350 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
1352 device_unregister(&root->dev);
1353 return ERR_PTR(err);
1355 root->owner = owner;
1361 EXPORT_SYMBOL_GPL(__root_device_register);
1364 * root_device_unregister - unregister and free a root device
1365 * @dev: device going away
1367 * This function unregisters and cleans up a device that was created by
1368 * root_device_register().
1370 void root_device_unregister(struct device *dev)
1372 struct root_device *root = to_root_device(dev);
1375 sysfs_remove_link(&root->dev.kobj, "module");
1377 device_unregister(dev);
1379 EXPORT_SYMBOL_GPL(root_device_unregister);
1382 static void device_create_release(struct device *dev)
1384 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1389 * device_create_vargs - creates a device and registers it with sysfs
1390 * @class: pointer to the struct class that this device should be registered to
1391 * @parent: pointer to the parent struct device of this new device, if any
1392 * @devt: the dev_t for the char device to be added
1393 * @drvdata: the data to be added to the device for callbacks
1394 * @fmt: string for the device's name
1395 * @args: va_list for the device's name
1397 * This function can be used by char device classes. A struct device
1398 * will be created in sysfs, registered to the specified class.
1400 * A "dev" file will be created, showing the dev_t for the device, if
1401 * the dev_t is not 0,0.
1402 * If a pointer to a parent struct device is passed in, the newly created
1403 * struct device will be a child of that device in sysfs.
1404 * The pointer to the struct device will be returned from the call.
1405 * Any further sysfs files that might be required can be created using this
1408 * Note: the struct class passed to this function must have previously
1409 * been created with a call to class_create().
1411 struct device *device_create_vargs(struct class *class, struct device *parent,
1412 dev_t devt, void *drvdata, const char *fmt,
1415 struct device *dev = NULL;
1416 int retval = -ENODEV;
1418 if (class == NULL || IS_ERR(class))
1421 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1429 dev->parent = parent;
1430 dev->release = device_create_release;
1431 dev_set_drvdata(dev, drvdata);
1433 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
1437 retval = device_register(dev);
1445 return ERR_PTR(retval);
1447 EXPORT_SYMBOL_GPL(device_create_vargs);
1450 * device_create - creates a device and registers it with sysfs
1451 * @class: pointer to the struct class that this device should be registered to
1452 * @parent: pointer to the parent struct device of this new device, if any
1453 * @devt: the dev_t for the char device to be added
1454 * @drvdata: the data to be added to the device for callbacks
1455 * @fmt: string for the device's name
1457 * This function can be used by char device classes. A struct device
1458 * will be created in sysfs, registered to the specified class.
1460 * A "dev" file will be created, showing the dev_t for the device, if
1461 * the dev_t is not 0,0.
1462 * If a pointer to a parent struct device is passed in, the newly created
1463 * struct device will be a child of that device in sysfs.
1464 * The pointer to the struct device will be returned from the call.
1465 * Any further sysfs files that might be required can be created using this
1468 * Note: the struct class passed to this function must have previously
1469 * been created with a call to class_create().
1471 struct device *device_create(struct class *class, struct device *parent,
1472 dev_t devt, void *drvdata, const char *fmt, ...)
1477 va_start(vargs, fmt);
1478 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
1482 EXPORT_SYMBOL_GPL(device_create);
1484 static int __match_devt(struct device *dev, void *data)
1488 return dev->devt == *devt;
1492 * device_destroy - removes a device that was created with device_create()
1493 * @class: pointer to the struct class that this device was registered with
1494 * @devt: the dev_t of the device that was previously registered
1496 * This call unregisters and cleans up a device that was created with a
1497 * call to device_create().
1499 void device_destroy(struct class *class, dev_t devt)
1503 dev = class_find_device(class, NULL, &devt, __match_devt);
1506 device_unregister(dev);
1509 EXPORT_SYMBOL_GPL(device_destroy);
1512 * device_rename - renames a device
1513 * @dev: the pointer to the struct device to be renamed
1514 * @new_name: the new name of the device
1516 * It is the responsibility of the caller to provide mutual
1517 * exclusion between two different calls of device_rename
1518 * on the same device to ensure that new_name is valid and
1519 * won't conflict with other devices.
1521 int device_rename(struct device *dev, char *new_name)
1523 char *old_class_name = NULL;
1524 char *new_class_name = NULL;
1525 char *old_device_name = NULL;
1528 dev = get_device(dev);
1532 pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
1533 __func__, new_name);
1535 #ifdef CONFIG_SYSFS_DEPRECATED
1536 if ((dev->class) && (dev->parent))
1537 old_class_name = make_class_name(dev->class->name, &dev->kobj);
1540 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
1541 if (!old_device_name) {
1546 error = kobject_rename(&dev->kobj, new_name);
1550 #ifdef CONFIG_SYSFS_DEPRECATED
1551 if (old_class_name) {
1552 new_class_name = make_class_name(dev->class->name, &dev->kobj);
1553 if (new_class_name) {
1554 error = sysfs_create_link_nowarn(&dev->parent->kobj,
1559 sysfs_remove_link(&dev->parent->kobj, old_class_name);
1564 error = sysfs_create_link_nowarn(&dev->class->p->class_subsys.kobj,
1565 &dev->kobj, dev_name(dev));
1568 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
1576 kfree(new_class_name);
1577 kfree(old_class_name);
1578 kfree(old_device_name);
1582 EXPORT_SYMBOL_GPL(device_rename);
1584 static int device_move_class_links(struct device *dev,
1585 struct device *old_parent,
1586 struct device *new_parent)
1589 #ifdef CONFIG_SYSFS_DEPRECATED
1592 class_name = make_class_name(dev->class->name, &dev->kobj);
1598 sysfs_remove_link(&dev->kobj, "device");
1599 sysfs_remove_link(&old_parent->kobj, class_name);
1602 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1606 error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
1609 sysfs_remove_link(&dev->kobj, "device");
1617 sysfs_remove_link(&dev->kobj, "device");
1619 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1626 * device_move - moves a device to a new parent
1627 * @dev: the pointer to the struct device to be moved
1628 * @new_parent: the new parent of the device (can by NULL)
1629 * @dpm_order: how to reorder the dpm_list
1631 int device_move(struct device *dev, struct device *new_parent,
1632 enum dpm_order dpm_order)
1635 struct device *old_parent;
1636 struct kobject *new_parent_kobj;
1638 dev = get_device(dev);
1643 new_parent = get_device(new_parent);
1644 new_parent_kobj = get_device_parent(dev, new_parent);
1646 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
1647 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
1648 error = kobject_move(&dev->kobj, new_parent_kobj);
1650 cleanup_glue_dir(dev, new_parent_kobj);
1651 put_device(new_parent);
1654 old_parent = dev->parent;
1655 dev->parent = new_parent;
1657 klist_remove(&dev->p->knode_parent);
1659 klist_add_tail(&dev->p->knode_parent,
1660 &new_parent->p->klist_children);
1661 set_dev_node(dev, dev_to_node(new_parent));
1666 error = device_move_class_links(dev, old_parent, new_parent);
1668 /* We ignore errors on cleanup since we're hosed anyway... */
1669 device_move_class_links(dev, new_parent, old_parent);
1670 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1672 klist_remove(&dev->p->knode_parent);
1673 dev->parent = old_parent;
1675 klist_add_tail(&dev->p->knode_parent,
1676 &old_parent->p->klist_children);
1677 set_dev_node(dev, dev_to_node(old_parent));
1680 cleanup_glue_dir(dev, new_parent_kobj);
1681 put_device(new_parent);
1684 switch (dpm_order) {
1685 case DPM_ORDER_NONE:
1687 case DPM_ORDER_DEV_AFTER_PARENT:
1688 device_pm_move_after(dev, new_parent);
1690 case DPM_ORDER_PARENT_BEFORE_DEV:
1691 device_pm_move_before(new_parent, dev);
1693 case DPM_ORDER_DEV_LAST:
1694 device_pm_move_last(dev);
1698 put_device(old_parent);
1704 EXPORT_SYMBOL_GPL(device_move);
1707 * device_shutdown - call ->shutdown() on each device to shutdown.
1709 void device_shutdown(void)
1711 struct device *dev, *devn;
1713 list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list,
1715 if (dev->bus && dev->bus->shutdown) {
1716 dev_dbg(dev, "shutdown\n");
1717 dev->bus->shutdown(dev);
1718 } else if (dev->driver && dev->driver->shutdown) {
1719 dev_dbg(dev, "shutdown\n");
1720 dev->driver->shutdown(dev);
1723 kobject_put(sysfs_dev_char_kobj);
1724 kobject_put(sysfs_dev_block_kobj);
1725 kobject_put(dev_kobj);
1726 async_synchronize_full();