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 the major/minor if present */
166 if (MAJOR(dev->devt)) {
167 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
168 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
171 if (dev->type && dev->type->name)
172 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
175 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
177 #ifdef CONFIG_SYSFS_DEPRECATED
179 struct device *parent = dev->parent;
181 /* find first bus device in parent chain */
182 while (parent && !parent->bus)
183 parent = parent->parent;
184 if (parent && parent->bus) {
187 path = kobject_get_path(&parent->kobj, GFP_KERNEL);
189 add_uevent_var(env, "PHYSDEVPATH=%s", path);
193 add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name);
196 add_uevent_var(env, "PHYSDEVDRIVER=%s",
197 parent->driver->name);
199 } else if (dev->bus) {
200 add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
203 add_uevent_var(env, "PHYSDEVDRIVER=%s",
208 /* have the bus specific function add its stuff */
209 if (dev->bus && dev->bus->uevent) {
210 retval = dev->bus->uevent(dev, env);
212 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
213 dev_name(dev), __func__, retval);
216 /* have the class specific function add its stuff */
217 if (dev->class && dev->class->dev_uevent) {
218 retval = dev->class->dev_uevent(dev, env);
220 pr_debug("device: '%s': %s: class uevent() "
221 "returned %d\n", dev_name(dev),
225 /* have the device type specific fuction add its stuff */
226 if (dev->type && dev->type->uevent) {
227 retval = dev->type->uevent(dev, env);
229 pr_debug("device: '%s': %s: dev_type uevent() "
230 "returned %d\n", dev_name(dev),
237 static struct kset_uevent_ops device_uevent_ops = {
238 .filter = dev_uevent_filter,
239 .name = dev_uevent_name,
240 .uevent = dev_uevent,
243 static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
246 struct kobject *top_kobj;
248 struct kobj_uevent_env *env = NULL;
253 /* search the kset, the device belongs to */
254 top_kobj = &dev->kobj;
255 while (!top_kobj->kset && top_kobj->parent)
256 top_kobj = top_kobj->parent;
260 kset = top_kobj->kset;
261 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
265 if (kset->uevent_ops && kset->uevent_ops->filter)
266 if (!kset->uevent_ops->filter(kset, &dev->kobj))
269 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
273 /* let the kset specific function add its keys */
274 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
278 /* copy keys to file */
279 for (i = 0; i < env->envp_idx; i++)
280 count += sprintf(&buf[count], "%s\n", env->envp[i]);
286 static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
287 const char *buf, size_t count)
289 enum kobject_action action;
291 if (kobject_action_type(buf, count, &action) == 0) {
292 kobject_uevent(&dev->kobj, action);
296 dev_err(dev, "uevent: unsupported action-string; this will "
297 "be ignored in a future kernel version\n");
298 kobject_uevent(&dev->kobj, KOBJ_ADD);
303 static struct device_attribute uevent_attr =
304 __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
306 static int device_add_attributes(struct device *dev,
307 struct device_attribute *attrs)
313 for (i = 0; attr_name(attrs[i]); i++) {
314 error = device_create_file(dev, &attrs[i]);
320 device_remove_file(dev, &attrs[i]);
325 static void device_remove_attributes(struct device *dev,
326 struct device_attribute *attrs)
331 for (i = 0; attr_name(attrs[i]); i++)
332 device_remove_file(dev, &attrs[i]);
335 static int device_add_groups(struct device *dev,
336 struct attribute_group **groups)
342 for (i = 0; groups[i]; i++) {
343 error = sysfs_create_group(&dev->kobj, groups[i]);
346 sysfs_remove_group(&dev->kobj,
355 static void device_remove_groups(struct device *dev,
356 struct attribute_group **groups)
361 for (i = 0; groups[i]; i++)
362 sysfs_remove_group(&dev->kobj, groups[i]);
365 static int device_add_attrs(struct device *dev)
367 struct class *class = dev->class;
368 struct device_type *type = dev->type;
372 error = device_add_attributes(dev, class->dev_attrs);
378 error = device_add_groups(dev, type->groups);
380 goto err_remove_class_attrs;
383 error = device_add_groups(dev, dev->groups);
385 goto err_remove_type_groups;
389 err_remove_type_groups:
391 device_remove_groups(dev, type->groups);
392 err_remove_class_attrs:
394 device_remove_attributes(dev, class->dev_attrs);
399 static void device_remove_attrs(struct device *dev)
401 struct class *class = dev->class;
402 struct device_type *type = dev->type;
404 device_remove_groups(dev, dev->groups);
407 device_remove_groups(dev, type->groups);
410 device_remove_attributes(dev, class->dev_attrs);
414 static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
417 return print_dev_t(buf, dev->devt);
420 static struct device_attribute devt_attr =
421 __ATTR(dev, S_IRUGO, show_dev, NULL);
423 /* kset to create /sys/devices/ */
424 struct kset *devices_kset;
427 * device_create_file - create sysfs attribute file for device.
429 * @attr: device attribute descriptor.
431 int device_create_file(struct device *dev, struct device_attribute *attr)
435 error = sysfs_create_file(&dev->kobj, &attr->attr);
440 * device_remove_file - remove sysfs attribute file.
442 * @attr: device attribute descriptor.
444 void device_remove_file(struct device *dev, struct device_attribute *attr)
447 sysfs_remove_file(&dev->kobj, &attr->attr);
451 * device_create_bin_file - create sysfs binary attribute file for device.
453 * @attr: device binary attribute descriptor.
455 int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
459 error = sysfs_create_bin_file(&dev->kobj, attr);
462 EXPORT_SYMBOL_GPL(device_create_bin_file);
465 * device_remove_bin_file - remove sysfs binary attribute file
467 * @attr: device binary attribute descriptor.
469 void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
472 sysfs_remove_bin_file(&dev->kobj, attr);
474 EXPORT_SYMBOL_GPL(device_remove_bin_file);
477 * device_schedule_callback_owner - helper to schedule a callback for a device
479 * @func: callback function to invoke later.
480 * @owner: module owning the callback routine
482 * Attribute methods must not unregister themselves or their parent device
483 * (which would amount to the same thing). Attempts to do so will deadlock,
484 * since unregistration is mutually exclusive with driver callbacks.
486 * Instead methods can call this routine, which will attempt to allocate
487 * and schedule a workqueue request to call back @func with @dev as its
488 * argument in the workqueue's process context. @dev will be pinned until
491 * This routine is usually called via the inline device_schedule_callback(),
492 * which automatically sets @owner to THIS_MODULE.
494 * Returns 0 if the request was submitted, -ENOMEM if storage could not
495 * be allocated, -ENODEV if a reference to @owner isn't available.
497 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
498 * underlying sysfs routine (since it is intended for use by attribute
499 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
501 int device_schedule_callback_owner(struct device *dev,
502 void (*func)(struct device *), struct module *owner)
504 return sysfs_schedule_callback(&dev->kobj,
505 (void (*)(void *)) func, dev, owner);
507 EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
509 static void klist_children_get(struct klist_node *n)
511 struct device_private *p = to_device_private_parent(n);
512 struct device *dev = p->device;
517 static void klist_children_put(struct klist_node *n)
519 struct device_private *p = to_device_private_parent(n);
520 struct device *dev = p->device;
526 * device_initialize - init device structure.
529 * This prepares the device for use by other layers by initializing
531 * It is the first half of device_register(), if called by
532 * that function, though it can also be called separately, so one
533 * may use @dev's fields. In particular, get_device()/put_device()
534 * may be used for reference counting of @dev after calling this
537 * NOTE: Use put_device() to give up your reference instead of freeing
538 * @dev directly once you have called this function.
540 void device_initialize(struct device *dev)
542 dev->kobj.kset = devices_kset;
543 kobject_init(&dev->kobj, &device_ktype);
544 INIT_LIST_HEAD(&dev->dma_pools);
545 init_MUTEX(&dev->sem);
546 spin_lock_init(&dev->devres_lock);
547 INIT_LIST_HEAD(&dev->devres_head);
548 device_init_wakeup(dev, 0);
550 set_dev_node(dev, -1);
553 #ifdef CONFIG_SYSFS_DEPRECATED
554 static struct kobject *get_device_parent(struct device *dev,
555 struct device *parent)
557 /* class devices without a parent live in /sys/class/<classname>/ */
558 if (dev->class && (!parent || parent->class != dev->class))
559 return &dev->class->p->class_subsys.kobj;
560 /* all other devices keep their parent */
562 return &parent->kobj;
567 static inline void cleanup_device_parent(struct device *dev) {}
568 static inline void cleanup_glue_dir(struct device *dev,
569 struct kobject *glue_dir) {}
571 static struct kobject *virtual_device_parent(struct device *dev)
573 static struct kobject *virtual_dir = NULL;
576 virtual_dir = kobject_create_and_add("virtual",
577 &devices_kset->kobj);
582 static struct kobject *get_device_parent(struct device *dev,
583 struct device *parent)
588 struct kobject *kobj = NULL;
589 struct kobject *parent_kobj;
593 * If we have no parent, we live in "virtual".
594 * Class-devices with a non class-device as parent, live
595 * in a "glue" directory to prevent namespace collisions.
598 parent_kobj = virtual_device_parent(dev);
599 else if (parent->class)
600 return &parent->kobj;
602 parent_kobj = &parent->kobj;
604 /* find our class-directory at the parent and reference it */
605 spin_lock(&dev->class->p->class_dirs.list_lock);
606 list_for_each_entry(k, &dev->class->p->class_dirs.list, entry)
607 if (k->parent == parent_kobj) {
608 kobj = kobject_get(k);
611 spin_unlock(&dev->class->p->class_dirs.list_lock);
615 /* or create a new class-directory at the parent device */
616 k = kobject_create();
619 k->kset = &dev->class->p->class_dirs;
620 retval = kobject_add(k, parent_kobj, "%s", dev->class->name);
625 /* do not emit an uevent for this simple "glue" directory */
630 return &parent->kobj;
634 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
636 /* see if we live in a "glue" directory */
637 if (!glue_dir || !dev->class ||
638 glue_dir->kset != &dev->class->p->class_dirs)
641 kobject_put(glue_dir);
644 static void cleanup_device_parent(struct device *dev)
646 cleanup_glue_dir(dev, dev->kobj.parent);
650 static void setup_parent(struct device *dev, struct device *parent)
652 struct kobject *kobj;
653 kobj = get_device_parent(dev, parent);
655 dev->kobj.parent = kobj;
658 static int device_add_class_symlinks(struct device *dev)
665 error = sysfs_create_link(&dev->kobj,
666 &dev->class->p->class_subsys.kobj,
671 #ifdef CONFIG_SYSFS_DEPRECATED
672 /* stacked class devices need a symlink in the class directory */
673 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
674 device_is_not_partition(dev)) {
675 error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
676 &dev->kobj, dev_name(dev));
681 if (dev->parent && device_is_not_partition(dev)) {
682 struct device *parent = dev->parent;
686 * stacked class devices have the 'device' link
687 * pointing to the bus device instead of the parent
689 while (parent->class && !parent->bus && parent->parent)
690 parent = parent->parent;
692 error = sysfs_create_link(&dev->kobj,
698 class_name = make_class_name(dev->class->name,
701 error = sysfs_create_link(&dev->parent->kobj,
702 &dev->kobj, class_name);
710 if (dev->parent && device_is_not_partition(dev))
711 sysfs_remove_link(&dev->kobj, "device");
713 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
714 device_is_not_partition(dev))
715 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
718 /* link in the class directory pointing to the device */
719 error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
720 &dev->kobj, dev_name(dev));
724 if (dev->parent && device_is_not_partition(dev)) {
725 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
733 sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
737 sysfs_remove_link(&dev->kobj, "subsystem");
742 static void device_remove_class_symlinks(struct device *dev)
747 #ifdef CONFIG_SYSFS_DEPRECATED
748 if (dev->parent && device_is_not_partition(dev)) {
751 class_name = make_class_name(dev->class->name, &dev->kobj);
753 sysfs_remove_link(&dev->parent->kobj, class_name);
756 sysfs_remove_link(&dev->kobj, "device");
759 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
760 device_is_not_partition(dev))
761 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
764 if (dev->parent && device_is_not_partition(dev))
765 sysfs_remove_link(&dev->kobj, "device");
767 sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
770 sysfs_remove_link(&dev->kobj, "subsystem");
774 * dev_set_name - set a device name
776 * @fmt: format string for the device's name
778 int dev_set_name(struct device *dev, const char *fmt, ...)
783 va_start(vargs, fmt);
784 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
788 EXPORT_SYMBOL_GPL(dev_set_name);
791 * device_to_dev_kobj - select a /sys/dev/ directory for the device
794 * By default we select char/ for new entries. Setting class->dev_obj
795 * to NULL prevents an entry from being created. class->dev_kobj must
796 * be set (or cleared) before any devices are registered to the class
797 * otherwise device_create_sys_dev_entry() and
798 * device_remove_sys_dev_entry() will disagree about the the presence
801 static struct kobject *device_to_dev_kobj(struct device *dev)
803 struct kobject *kobj;
806 kobj = dev->class->dev_kobj;
808 kobj = sysfs_dev_char_kobj;
813 static int device_create_sys_dev_entry(struct device *dev)
815 struct kobject *kobj = device_to_dev_kobj(dev);
820 format_dev_t(devt_str, dev->devt);
821 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
827 static void device_remove_sys_dev_entry(struct device *dev)
829 struct kobject *kobj = device_to_dev_kobj(dev);
833 format_dev_t(devt_str, dev->devt);
834 sysfs_remove_link(kobj, devt_str);
839 * device_add - add device to device hierarchy.
842 * This is part 2 of device_register(), though may be called
843 * separately _iff_ device_initialize() has been called separately.
845 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
846 * to the global and sibling lists for the device, then
847 * adds it to the other relevant subsystems of the driver model.
849 * NOTE: _Never_ directly free @dev after calling this function, even
850 * if it returned an error! Always use put_device() to give up your
853 int device_add(struct device *dev)
855 struct device *parent = NULL;
856 struct class_interface *class_intf;
859 dev = get_device(dev);
863 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
868 dev->p->device = dev;
869 klist_init(&dev->p->klist_children, klist_children_get,
873 * for statically allocated devices, which should all be converted
874 * some day, we need to initialize the name. We prevent reading back
875 * the name, and force the use of dev_name()
877 if (dev->init_name) {
878 dev_set_name(dev, dev->init_name);
879 dev->init_name = NULL;
885 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
887 parent = get_device(dev->parent);
888 setup_parent(dev, parent);
890 /* use parent numa_node */
892 set_dev_node(dev, dev_to_node(parent));
894 /* first, register with generic layer. */
895 /* we require the name to be set before, and pass NULL */
896 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
900 /* notify platform of device entry */
902 platform_notify(dev);
904 error = device_create_file(dev, &uevent_attr);
908 if (MAJOR(dev->devt)) {
909 error = device_create_file(dev, &devt_attr);
911 goto ueventattrError;
913 error = device_create_sys_dev_entry(dev);
918 error = device_add_class_symlinks(dev);
921 error = device_add_attrs(dev);
924 error = bus_add_device(dev);
927 error = dpm_sysfs_add(dev);
932 /* Notify clients of device addition. This call must come
933 * after dpm_sysf_add() and before kobject_uevent().
936 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
937 BUS_NOTIFY_ADD_DEVICE, dev);
939 kobject_uevent(&dev->kobj, KOBJ_ADD);
940 bus_attach_device(dev);
942 klist_add_tail(&dev->p->knode_parent,
943 &parent->p->klist_children);
946 mutex_lock(&dev->class->p->class_mutex);
947 /* tie the class to the device */
948 klist_add_tail(&dev->knode_class,
949 &dev->class->p->class_devices);
951 /* notify any interfaces that the device is here */
952 list_for_each_entry(class_intf,
953 &dev->class->p->class_interfaces, node)
954 if (class_intf->add_dev)
955 class_intf->add_dev(dev, class_intf);
956 mutex_unlock(&dev->class->p->class_mutex);
962 bus_remove_device(dev);
964 device_remove_attrs(dev);
966 device_remove_class_symlinks(dev);
968 if (MAJOR(dev->devt))
969 device_remove_sys_dev_entry(dev);
971 if (MAJOR(dev->devt))
972 device_remove_file(dev, &devt_attr);
974 device_remove_file(dev, &uevent_attr);
976 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
977 kobject_del(&dev->kobj);
979 cleanup_device_parent(dev);
989 * device_register - register a device with the system.
990 * @dev: pointer to the device structure
992 * This happens in two clean steps - initialize the device
993 * and add it to the system. The two steps can be called
994 * separately, but this is the easiest and most common.
995 * I.e. you should only call the two helpers separately if
996 * have a clearly defined need to use and refcount the device
997 * before it is added to the hierarchy.
999 * NOTE: _Never_ directly free @dev after calling this function, even
1000 * if it returned an error! Always use put_device() to give up the
1001 * reference initialized in this function instead.
1003 int device_register(struct device *dev)
1005 device_initialize(dev);
1006 return device_add(dev);
1010 * get_device - increment reference count for device.
1013 * This simply forwards the call to kobject_get(), though
1014 * we do take care to provide for the case that we get a NULL
1015 * pointer passed in.
1017 struct device *get_device(struct device *dev)
1019 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
1023 * put_device - decrement reference count.
1024 * @dev: device in question.
1026 void put_device(struct device *dev)
1028 /* might_sleep(); */
1030 kobject_put(&dev->kobj);
1034 * device_del - delete device from system.
1037 * This is the first part of the device unregistration
1038 * sequence. This removes the device from the lists we control
1039 * from here, has it removed from the other driver model
1040 * subsystems it was added to in device_add(), and removes it
1041 * from the kobject hierarchy.
1043 * NOTE: this should be called manually _iff_ device_add() was
1044 * also called manually.
1046 void device_del(struct device *dev)
1048 struct device *parent = dev->parent;
1049 struct class_interface *class_intf;
1051 /* Notify clients of device removal. This call must come
1052 * before dpm_sysfs_remove().
1055 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1056 BUS_NOTIFY_DEL_DEVICE, dev);
1057 device_pm_remove(dev);
1058 dpm_sysfs_remove(dev);
1060 klist_del(&dev->p->knode_parent);
1061 if (MAJOR(dev->devt)) {
1062 device_remove_sys_dev_entry(dev);
1063 device_remove_file(dev, &devt_attr);
1066 device_remove_class_symlinks(dev);
1068 mutex_lock(&dev->class->p->class_mutex);
1069 /* notify any interfaces that the device is now gone */
1070 list_for_each_entry(class_intf,
1071 &dev->class->p->class_interfaces, node)
1072 if (class_intf->remove_dev)
1073 class_intf->remove_dev(dev, class_intf);
1074 /* remove the device from the class list */
1075 klist_del(&dev->knode_class);
1076 mutex_unlock(&dev->class->p->class_mutex);
1078 device_remove_file(dev, &uevent_attr);
1079 device_remove_attrs(dev);
1080 bus_remove_device(dev);
1083 * Some platform devices are driven without driver attached
1084 * and managed resources may have been acquired. Make sure
1085 * all resources are released.
1087 devres_release_all(dev);
1089 /* Notify the platform of the removal, in case they
1090 * need to do anything...
1092 if (platform_notify_remove)
1093 platform_notify_remove(dev);
1094 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1095 cleanup_device_parent(dev);
1096 kobject_del(&dev->kobj);
1101 * device_unregister - unregister device from system.
1102 * @dev: device going away.
1104 * We do this in two parts, like we do device_register(). First,
1105 * we remove it from all the subsystems with device_del(), then
1106 * we decrement the reference count via put_device(). If that
1107 * is the final reference count, the device will be cleaned up
1108 * via device_release() above. Otherwise, the structure will
1109 * stick around until the final reference to the device is dropped.
1111 void device_unregister(struct device *dev)
1113 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1118 static struct device *next_device(struct klist_iter *i)
1120 struct klist_node *n = klist_next(i);
1121 struct device *dev = NULL;
1122 struct device_private *p;
1125 p = to_device_private_parent(n);
1132 * device_for_each_child - device child iterator.
1133 * @parent: parent struct device.
1134 * @data: data for the callback.
1135 * @fn: function to be called for each device.
1137 * Iterate over @parent's child devices, and call @fn for each,
1140 * We check the return of @fn each time. If it returns anything
1141 * other than 0, we break out and return that value.
1143 int device_for_each_child(struct device *parent, void *data,
1144 int (*fn)(struct device *dev, void *data))
1146 struct klist_iter i;
1147 struct device *child;
1153 klist_iter_init(&parent->p->klist_children, &i);
1154 while ((child = next_device(&i)) && !error)
1155 error = fn(child, data);
1156 klist_iter_exit(&i);
1161 * device_find_child - device iterator for locating a particular device.
1162 * @parent: parent struct device
1163 * @data: Data to pass to match function
1164 * @match: Callback function to check device
1166 * This is similar to the device_for_each_child() function above, but it
1167 * returns a reference to a device that is 'found' for later use, as
1168 * determined by the @match callback.
1170 * The callback should return 0 if the device doesn't match and non-zero
1171 * if it does. If the callback returns non-zero and a reference to the
1172 * current device can be obtained, this function will return to the caller
1173 * and not iterate over any more devices.
1175 struct device *device_find_child(struct device *parent, void *data,
1176 int (*match)(struct device *dev, void *data))
1178 struct klist_iter i;
1179 struct device *child;
1184 klist_iter_init(&parent->p->klist_children, &i);
1185 while ((child = next_device(&i)))
1186 if (match(child, data) && get_device(child))
1188 klist_iter_exit(&i);
1192 int __init devices_init(void)
1194 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1197 dev_kobj = kobject_create_and_add("dev", NULL);
1200 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1201 if (!sysfs_dev_block_kobj)
1202 goto block_kobj_err;
1203 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1204 if (!sysfs_dev_char_kobj)
1210 kobject_put(sysfs_dev_block_kobj);
1212 kobject_put(dev_kobj);
1214 kset_unregister(devices_kset);
1218 EXPORT_SYMBOL_GPL(device_for_each_child);
1219 EXPORT_SYMBOL_GPL(device_find_child);
1221 EXPORT_SYMBOL_GPL(device_initialize);
1222 EXPORT_SYMBOL_GPL(device_add);
1223 EXPORT_SYMBOL_GPL(device_register);
1225 EXPORT_SYMBOL_GPL(device_del);
1226 EXPORT_SYMBOL_GPL(device_unregister);
1227 EXPORT_SYMBOL_GPL(get_device);
1228 EXPORT_SYMBOL_GPL(put_device);
1230 EXPORT_SYMBOL_GPL(device_create_file);
1231 EXPORT_SYMBOL_GPL(device_remove_file);
1236 struct module *owner;
1239 #define to_root_device(dev) container_of(dev, struct root_device, dev)
1241 static void root_device_release(struct device *dev)
1243 kfree(to_root_device(dev));
1247 * __root_device_register - allocate and register a root device
1248 * @name: root device name
1249 * @owner: owner module of the root device, usually THIS_MODULE
1251 * This function allocates a root device and registers it
1252 * using device_register(). In order to free the returned
1253 * device, use root_device_unregister().
1255 * Root devices are dummy devices which allow other devices
1256 * to be grouped under /sys/devices. Use this function to
1257 * allocate a root device and then use it as the parent of
1258 * any device which should appear under /sys/devices/{name}
1260 * The /sys/devices/{name} directory will also contain a
1261 * 'module' symlink which points to the @owner directory
1264 * Note: You probably want to use root_device_register().
1266 struct device *__root_device_register(const char *name, struct module *owner)
1268 struct root_device *root;
1271 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
1273 return ERR_PTR(err);
1275 err = dev_set_name(&root->dev, name);
1278 return ERR_PTR(err);
1281 root->dev.release = root_device_release;
1283 err = device_register(&root->dev);
1285 put_device(&root->dev);
1286 return ERR_PTR(err);
1289 #ifdef CONFIG_MODULE /* gotta find a "cleaner" way to do this */
1291 struct module_kobject *mk = &owner->mkobj;
1293 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
1295 device_unregister(&root->dev);
1296 return ERR_PTR(err);
1298 root->owner = owner;
1304 EXPORT_SYMBOL_GPL(__root_device_register);
1307 * root_device_unregister - unregister and free a root device
1308 * @dev: device going away
1310 * This function unregisters and cleans up a device that was created by
1311 * root_device_register().
1313 void root_device_unregister(struct device *dev)
1315 struct root_device *root = to_root_device(dev);
1318 sysfs_remove_link(&root->dev.kobj, "module");
1320 device_unregister(dev);
1322 EXPORT_SYMBOL_GPL(root_device_unregister);
1325 static void device_create_release(struct device *dev)
1327 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1332 * device_create_vargs - creates a device and registers it with sysfs
1333 * @class: pointer to the struct class that this device should be registered to
1334 * @parent: pointer to the parent struct device of this new device, if any
1335 * @devt: the dev_t for the char device to be added
1336 * @drvdata: the data to be added to the device for callbacks
1337 * @fmt: string for the device's name
1338 * @args: va_list for the device's name
1340 * This function can be used by char device classes. A struct device
1341 * will be created in sysfs, registered to the specified class.
1343 * A "dev" file will be created, showing the dev_t for the device, if
1344 * the dev_t is not 0,0.
1345 * If a pointer to a parent struct device is passed in, the newly created
1346 * struct device will be a child of that device in sysfs.
1347 * The pointer to the struct device will be returned from the call.
1348 * Any further sysfs files that might be required can be created using this
1351 * Note: the struct class passed to this function must have previously
1352 * been created with a call to class_create().
1354 struct device *device_create_vargs(struct class *class, struct device *parent,
1355 dev_t devt, void *drvdata, const char *fmt,
1358 struct device *dev = NULL;
1359 int retval = -ENODEV;
1361 if (class == NULL || IS_ERR(class))
1364 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1372 dev->parent = parent;
1373 dev->release = device_create_release;
1374 dev_set_drvdata(dev, drvdata);
1376 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
1380 retval = device_register(dev);
1388 return ERR_PTR(retval);
1390 EXPORT_SYMBOL_GPL(device_create_vargs);
1393 * device_create - creates a device and registers it with sysfs
1394 * @class: pointer to the struct class that this device should be registered to
1395 * @parent: pointer to the parent struct device of this new device, if any
1396 * @devt: the dev_t for the char device to be added
1397 * @drvdata: the data to be added to the device for callbacks
1398 * @fmt: string for the device's name
1400 * This function can be used by char device classes. A struct device
1401 * will be created in sysfs, registered to the specified class.
1403 * A "dev" file will be created, showing the dev_t for the device, if
1404 * the dev_t is not 0,0.
1405 * If a pointer to a parent struct device is passed in, the newly created
1406 * struct device will be a child of that device in sysfs.
1407 * The pointer to the struct device will be returned from the call.
1408 * Any further sysfs files that might be required can be created using this
1411 * Note: the struct class passed to this function must have previously
1412 * been created with a call to class_create().
1414 struct device *device_create(struct class *class, struct device *parent,
1415 dev_t devt, void *drvdata, const char *fmt, ...)
1420 va_start(vargs, fmt);
1421 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
1425 EXPORT_SYMBOL_GPL(device_create);
1427 static int __match_devt(struct device *dev, void *data)
1431 return dev->devt == *devt;
1435 * device_destroy - removes a device that was created with device_create()
1436 * @class: pointer to the struct class that this device was registered with
1437 * @devt: the dev_t of the device that was previously registered
1439 * This call unregisters and cleans up a device that was created with a
1440 * call to device_create().
1442 void device_destroy(struct class *class, dev_t devt)
1446 dev = class_find_device(class, NULL, &devt, __match_devt);
1449 device_unregister(dev);
1452 EXPORT_SYMBOL_GPL(device_destroy);
1455 * device_rename - renames a device
1456 * @dev: the pointer to the struct device to be renamed
1457 * @new_name: the new name of the device
1459 * It is the responsibility of the caller to provide mutual
1460 * exclusion between two different calls of device_rename
1461 * on the same device to ensure that new_name is valid and
1462 * won't conflict with other devices.
1464 int device_rename(struct device *dev, char *new_name)
1466 char *old_class_name = NULL;
1467 char *new_class_name = NULL;
1468 char *old_device_name = NULL;
1471 dev = get_device(dev);
1475 pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
1476 __func__, new_name);
1478 #ifdef CONFIG_SYSFS_DEPRECATED
1479 if ((dev->class) && (dev->parent))
1480 old_class_name = make_class_name(dev->class->name, &dev->kobj);
1483 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
1484 if (!old_device_name) {
1489 error = kobject_rename(&dev->kobj, new_name);
1493 #ifdef CONFIG_SYSFS_DEPRECATED
1494 if (old_class_name) {
1495 new_class_name = make_class_name(dev->class->name, &dev->kobj);
1496 if (new_class_name) {
1497 error = sysfs_create_link_nowarn(&dev->parent->kobj,
1502 sysfs_remove_link(&dev->parent->kobj, old_class_name);
1507 error = sysfs_create_link_nowarn(&dev->class->p->class_subsys.kobj,
1508 &dev->kobj, dev_name(dev));
1511 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
1519 kfree(new_class_name);
1520 kfree(old_class_name);
1521 kfree(old_device_name);
1525 EXPORT_SYMBOL_GPL(device_rename);
1527 static int device_move_class_links(struct device *dev,
1528 struct device *old_parent,
1529 struct device *new_parent)
1532 #ifdef CONFIG_SYSFS_DEPRECATED
1535 class_name = make_class_name(dev->class->name, &dev->kobj);
1541 sysfs_remove_link(&dev->kobj, "device");
1542 sysfs_remove_link(&old_parent->kobj, class_name);
1545 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1549 error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
1552 sysfs_remove_link(&dev->kobj, "device");
1560 sysfs_remove_link(&dev->kobj, "device");
1562 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1569 * device_move - moves a device to a new parent
1570 * @dev: the pointer to the struct device to be moved
1571 * @new_parent: the new parent of the device (can by NULL)
1572 * @dpm_order: how to reorder the dpm_list
1574 int device_move(struct device *dev, struct device *new_parent,
1575 enum dpm_order dpm_order)
1578 struct device *old_parent;
1579 struct kobject *new_parent_kobj;
1581 dev = get_device(dev);
1586 new_parent = get_device(new_parent);
1587 new_parent_kobj = get_device_parent(dev, new_parent);
1589 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
1590 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
1591 error = kobject_move(&dev->kobj, new_parent_kobj);
1593 cleanup_glue_dir(dev, new_parent_kobj);
1594 put_device(new_parent);
1597 old_parent = dev->parent;
1598 dev->parent = new_parent;
1600 klist_remove(&dev->p->knode_parent);
1602 klist_add_tail(&dev->p->knode_parent,
1603 &new_parent->p->klist_children);
1604 set_dev_node(dev, dev_to_node(new_parent));
1609 error = device_move_class_links(dev, old_parent, new_parent);
1611 /* We ignore errors on cleanup since we're hosed anyway... */
1612 device_move_class_links(dev, new_parent, old_parent);
1613 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1615 klist_remove(&dev->p->knode_parent);
1616 dev->parent = old_parent;
1618 klist_add_tail(&dev->p->knode_parent,
1619 &old_parent->p->klist_children);
1620 set_dev_node(dev, dev_to_node(old_parent));
1623 cleanup_glue_dir(dev, new_parent_kobj);
1624 put_device(new_parent);
1627 switch (dpm_order) {
1628 case DPM_ORDER_NONE:
1630 case DPM_ORDER_DEV_AFTER_PARENT:
1631 device_pm_move_after(dev, new_parent);
1633 case DPM_ORDER_PARENT_BEFORE_DEV:
1634 device_pm_move_before(new_parent, dev);
1636 case DPM_ORDER_DEV_LAST:
1637 device_pm_move_last(dev);
1641 put_device(old_parent);
1647 EXPORT_SYMBOL_GPL(device_move);
1650 * device_shutdown - call ->shutdown() on each device to shutdown.
1652 void device_shutdown(void)
1654 struct device *dev, *devn;
1656 list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list,
1658 if (dev->bus && dev->bus->shutdown) {
1659 dev_dbg(dev, "shutdown\n");
1660 dev->bus->shutdown(dev);
1661 } else if (dev->driver && dev->driver->shutdown) {
1662 dev_dbg(dev, "shutdown\n");
1663 dev->driver->shutdown(dev);
1666 kobject_put(sysfs_dev_char_kobj);
1667 kobject_put(sysfs_dev_block_kobj);
1668 kobject_put(dev_kobj);
1669 async_synchronize_full();