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/fwnode.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/string.h>
20 #include <linux/kdev_t.h>
21 #include <linux/notifier.h>
23 #include <linux/of_device.h>
24 #include <linux/genhd.h>
25 #include <linux/kallsyms.h>
26 #include <linux/mutex.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/netdevice.h>
29 #include <linux/sysfs.h>
32 #include "power/power.h"
34 #ifdef CONFIG_SYSFS_DEPRECATED
35 #ifdef CONFIG_SYSFS_DEPRECATED_V2
36 long sysfs_deprecated = 1;
38 long sysfs_deprecated = 0;
40 static int __init sysfs_deprecated_setup(char *arg)
42 return kstrtol(arg, 10, &sysfs_deprecated);
44 early_param("sysfs.deprecated", sysfs_deprecated_setup);
47 int (*platform_notify)(struct device *dev) = NULL;
48 int (*platform_notify_remove)(struct device *dev) = NULL;
49 static struct kobject *dev_kobj;
50 struct kobject *sysfs_dev_char_kobj;
51 struct kobject *sysfs_dev_block_kobj;
53 static DEFINE_MUTEX(device_hotplug_lock);
55 void lock_device_hotplug(void)
57 mutex_lock(&device_hotplug_lock);
60 void unlock_device_hotplug(void)
62 mutex_unlock(&device_hotplug_lock);
65 int lock_device_hotplug_sysfs(void)
67 if (mutex_trylock(&device_hotplug_lock))
70 /* Avoid busy looping (5 ms of sleep should do). */
72 return restart_syscall();
75 void lock_device_hotplug_assert(void)
77 lockdep_assert_held(&device_hotplug_lock);
81 static inline int device_is_not_partition(struct device *dev)
83 return !(dev->type == &part_type);
86 static inline int device_is_not_partition(struct device *dev)
93 * dev_driver_string - Return a device's driver name, if at all possible
94 * @dev: struct device to get the name of
96 * Will return the device's driver's name if it is bound to a device. If
97 * the device is not bound to a driver, it will return the name of the bus
98 * it is attached to. If it is not attached to a bus either, an empty
99 * string will be returned.
101 const char *dev_driver_string(const struct device *dev)
103 struct device_driver *drv;
105 /* dev->driver can change to NULL underneath us because of unbinding,
106 * so be careful about accessing it. dev->bus and dev->class should
107 * never change once they are set, so they don't need special care.
109 drv = ACCESS_ONCE(dev->driver);
110 return drv ? drv->name :
111 (dev->bus ? dev->bus->name :
112 (dev->class ? dev->class->name : ""));
114 EXPORT_SYMBOL(dev_driver_string);
116 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
118 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
121 struct device_attribute *dev_attr = to_dev_attr(attr);
122 struct device *dev = kobj_to_dev(kobj);
126 ret = dev_attr->show(dev, dev_attr, buf);
127 if (ret >= (ssize_t)PAGE_SIZE) {
128 print_symbol("dev_attr_show: %s returned bad count\n",
129 (unsigned long)dev_attr->show);
134 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
135 const char *buf, size_t count)
137 struct device_attribute *dev_attr = to_dev_attr(attr);
138 struct device *dev = kobj_to_dev(kobj);
142 ret = dev_attr->store(dev, dev_attr, buf, count);
146 static const struct sysfs_ops dev_sysfs_ops = {
147 .show = dev_attr_show,
148 .store = dev_attr_store,
151 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
153 ssize_t device_store_ulong(struct device *dev,
154 struct device_attribute *attr,
155 const char *buf, size_t size)
157 struct dev_ext_attribute *ea = to_ext_attr(attr);
159 unsigned long new = simple_strtoul(buf, &end, 0);
162 *(unsigned long *)(ea->var) = new;
163 /* Always return full write size even if we didn't consume all */
166 EXPORT_SYMBOL_GPL(device_store_ulong);
168 ssize_t device_show_ulong(struct device *dev,
169 struct device_attribute *attr,
172 struct dev_ext_attribute *ea = to_ext_attr(attr);
173 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
175 EXPORT_SYMBOL_GPL(device_show_ulong);
177 ssize_t device_store_int(struct device *dev,
178 struct device_attribute *attr,
179 const char *buf, size_t size)
181 struct dev_ext_attribute *ea = to_ext_attr(attr);
183 long new = simple_strtol(buf, &end, 0);
184 if (end == buf || new > INT_MAX || new < INT_MIN)
186 *(int *)(ea->var) = new;
187 /* Always return full write size even if we didn't consume all */
190 EXPORT_SYMBOL_GPL(device_store_int);
192 ssize_t device_show_int(struct device *dev,
193 struct device_attribute *attr,
196 struct dev_ext_attribute *ea = to_ext_attr(attr);
198 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
200 EXPORT_SYMBOL_GPL(device_show_int);
202 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
203 const char *buf, size_t size)
205 struct dev_ext_attribute *ea = to_ext_attr(attr);
207 if (strtobool(buf, ea->var) < 0)
212 EXPORT_SYMBOL_GPL(device_store_bool);
214 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
217 struct dev_ext_attribute *ea = to_ext_attr(attr);
219 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
221 EXPORT_SYMBOL_GPL(device_show_bool);
224 * device_release - free device structure.
225 * @kobj: device's kobject.
227 * This is called once the reference count for the object
228 * reaches 0. We forward the call to the device's release
229 * method, which should handle actually freeing the structure.
231 static void device_release(struct kobject *kobj)
233 struct device *dev = kobj_to_dev(kobj);
234 struct device_private *p = dev->p;
237 * Some platform devices are driven without driver attached
238 * and managed resources may have been acquired. Make sure
239 * all resources are released.
241 * Drivers still can add resources into device after device
242 * is deleted but alive, so release devres here to avoid
243 * possible memory leak.
245 devres_release_all(dev);
249 else if (dev->type && dev->type->release)
250 dev->type->release(dev);
251 else if (dev->class && dev->class->dev_release)
252 dev->class->dev_release(dev);
254 WARN(1, KERN_ERR "Device '%s' does not have a release() "
255 "function, it is broken and must be fixed.\n",
260 static const void *device_namespace(struct kobject *kobj)
262 struct device *dev = kobj_to_dev(kobj);
263 const void *ns = NULL;
265 if (dev->class && dev->class->ns_type)
266 ns = dev->class->namespace(dev);
271 static struct kobj_type device_ktype = {
272 .release = device_release,
273 .sysfs_ops = &dev_sysfs_ops,
274 .namespace = device_namespace,
278 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
280 struct kobj_type *ktype = get_ktype(kobj);
282 if (ktype == &device_ktype) {
283 struct device *dev = kobj_to_dev(kobj);
292 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
294 struct device *dev = kobj_to_dev(kobj);
297 return dev->bus->name;
299 return dev->class->name;
303 static int dev_uevent(struct kset *kset, struct kobject *kobj,
304 struct kobj_uevent_env *env)
306 struct device *dev = kobj_to_dev(kobj);
309 /* add device node properties if present */
310 if (MAJOR(dev->devt)) {
314 kuid_t uid = GLOBAL_ROOT_UID;
315 kgid_t gid = GLOBAL_ROOT_GID;
317 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
318 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
319 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
321 add_uevent_var(env, "DEVNAME=%s", name);
323 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
324 if (!uid_eq(uid, GLOBAL_ROOT_UID))
325 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
326 if (!gid_eq(gid, GLOBAL_ROOT_GID))
327 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
332 if (dev->type && dev->type->name)
333 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
336 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
338 /* Add common DT information about the device */
339 of_device_uevent(dev, env);
341 /* have the bus specific function add its stuff */
342 if (dev->bus && dev->bus->uevent) {
343 retval = dev->bus->uevent(dev, env);
345 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
346 dev_name(dev), __func__, retval);
349 /* have the class specific function add its stuff */
350 if (dev->class && dev->class->dev_uevent) {
351 retval = dev->class->dev_uevent(dev, env);
353 pr_debug("device: '%s': %s: class uevent() "
354 "returned %d\n", dev_name(dev),
358 /* have the device type specific function add its stuff */
359 if (dev->type && dev->type->uevent) {
360 retval = dev->type->uevent(dev, env);
362 pr_debug("device: '%s': %s: dev_type uevent() "
363 "returned %d\n", dev_name(dev),
370 static const struct kset_uevent_ops device_uevent_ops = {
371 .filter = dev_uevent_filter,
372 .name = dev_uevent_name,
373 .uevent = dev_uevent,
376 static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
379 struct kobject *top_kobj;
381 struct kobj_uevent_env *env = NULL;
386 /* search the kset, the device belongs to */
387 top_kobj = &dev->kobj;
388 while (!top_kobj->kset && top_kobj->parent)
389 top_kobj = top_kobj->parent;
393 kset = top_kobj->kset;
394 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
398 if (kset->uevent_ops && kset->uevent_ops->filter)
399 if (!kset->uevent_ops->filter(kset, &dev->kobj))
402 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
406 /* let the kset specific function add its keys */
407 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
411 /* copy keys to file */
412 for (i = 0; i < env->envp_idx; i++)
413 count += sprintf(&buf[count], "%s\n", env->envp[i]);
419 static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
420 const char *buf, size_t count)
422 enum kobject_action action;
424 if (kobject_action_type(buf, count, &action) == 0)
425 kobject_uevent(&dev->kobj, action);
427 dev_err(dev, "uevent: unknown action-string\n");
430 static DEVICE_ATTR_RW(uevent);
432 static ssize_t online_show(struct device *dev, struct device_attribute *attr,
440 return sprintf(buf, "%u\n", val);
443 static ssize_t online_store(struct device *dev, struct device_attribute *attr,
444 const char *buf, size_t count)
449 ret = strtobool(buf, &val);
453 ret = lock_device_hotplug_sysfs();
457 ret = val ? device_online(dev) : device_offline(dev);
458 unlock_device_hotplug();
459 return ret < 0 ? ret : count;
461 static DEVICE_ATTR_RW(online);
463 int device_add_groups(struct device *dev, const struct attribute_group **groups)
465 return sysfs_create_groups(&dev->kobj, groups);
468 void device_remove_groups(struct device *dev,
469 const struct attribute_group **groups)
471 sysfs_remove_groups(&dev->kobj, groups);
474 static int device_add_attrs(struct device *dev)
476 struct class *class = dev->class;
477 const struct device_type *type = dev->type;
481 error = device_add_groups(dev, class->dev_groups);
487 error = device_add_groups(dev, type->groups);
489 goto err_remove_class_groups;
492 error = device_add_groups(dev, dev->groups);
494 goto err_remove_type_groups;
496 if (device_supports_offline(dev) && !dev->offline_disabled) {
497 error = device_create_file(dev, &dev_attr_online);
499 goto err_remove_dev_groups;
504 err_remove_dev_groups:
505 device_remove_groups(dev, dev->groups);
506 err_remove_type_groups:
508 device_remove_groups(dev, type->groups);
509 err_remove_class_groups:
511 device_remove_groups(dev, class->dev_groups);
516 static void device_remove_attrs(struct device *dev)
518 struct class *class = dev->class;
519 const struct device_type *type = dev->type;
521 device_remove_file(dev, &dev_attr_online);
522 device_remove_groups(dev, dev->groups);
525 device_remove_groups(dev, type->groups);
528 device_remove_groups(dev, class->dev_groups);
531 static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
534 return print_dev_t(buf, dev->devt);
536 static DEVICE_ATTR_RO(dev);
539 struct kset *devices_kset;
542 * devices_kset_move_before - Move device in the devices_kset's list.
543 * @deva: Device to move.
544 * @devb: Device @deva should come before.
546 static void devices_kset_move_before(struct device *deva, struct device *devb)
550 pr_debug("devices_kset: Moving %s before %s\n",
551 dev_name(deva), dev_name(devb));
552 spin_lock(&devices_kset->list_lock);
553 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
554 spin_unlock(&devices_kset->list_lock);
558 * devices_kset_move_after - Move device in the devices_kset's list.
559 * @deva: Device to move
560 * @devb: Device @deva should come after.
562 static void devices_kset_move_after(struct device *deva, struct device *devb)
566 pr_debug("devices_kset: Moving %s after %s\n",
567 dev_name(deva), dev_name(devb));
568 spin_lock(&devices_kset->list_lock);
569 list_move(&deva->kobj.entry, &devb->kobj.entry);
570 spin_unlock(&devices_kset->list_lock);
574 * devices_kset_move_last - move the device to the end of devices_kset's list.
575 * @dev: device to move
577 void devices_kset_move_last(struct device *dev)
581 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
582 spin_lock(&devices_kset->list_lock);
583 list_move_tail(&dev->kobj.entry, &devices_kset->list);
584 spin_unlock(&devices_kset->list_lock);
588 * device_create_file - create sysfs attribute file for device.
590 * @attr: device attribute descriptor.
592 int device_create_file(struct device *dev,
593 const struct device_attribute *attr)
598 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
599 "Attribute %s: write permission without 'store'\n",
601 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
602 "Attribute %s: read permission without 'show'\n",
604 error = sysfs_create_file(&dev->kobj, &attr->attr);
609 EXPORT_SYMBOL_GPL(device_create_file);
612 * device_remove_file - remove sysfs attribute file.
614 * @attr: device attribute descriptor.
616 void device_remove_file(struct device *dev,
617 const struct device_attribute *attr)
620 sysfs_remove_file(&dev->kobj, &attr->attr);
622 EXPORT_SYMBOL_GPL(device_remove_file);
625 * device_remove_file_self - remove sysfs attribute file from its own method.
627 * @attr: device attribute descriptor.
629 * See kernfs_remove_self() for details.
631 bool device_remove_file_self(struct device *dev,
632 const struct device_attribute *attr)
635 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
639 EXPORT_SYMBOL_GPL(device_remove_file_self);
642 * device_create_bin_file - create sysfs binary attribute file for device.
644 * @attr: device binary attribute descriptor.
646 int device_create_bin_file(struct device *dev,
647 const struct bin_attribute *attr)
651 error = sysfs_create_bin_file(&dev->kobj, attr);
654 EXPORT_SYMBOL_GPL(device_create_bin_file);
657 * device_remove_bin_file - remove sysfs binary attribute file
659 * @attr: device binary attribute descriptor.
661 void device_remove_bin_file(struct device *dev,
662 const struct bin_attribute *attr)
665 sysfs_remove_bin_file(&dev->kobj, attr);
667 EXPORT_SYMBOL_GPL(device_remove_bin_file);
669 static void klist_children_get(struct klist_node *n)
671 struct device_private *p = to_device_private_parent(n);
672 struct device *dev = p->device;
677 static void klist_children_put(struct klist_node *n)
679 struct device_private *p = to_device_private_parent(n);
680 struct device *dev = p->device;
686 * device_initialize - init device structure.
689 * This prepares the device for use by other layers by initializing
691 * It is the first half of device_register(), if called by
692 * that function, though it can also be called separately, so one
693 * may use @dev's fields. In particular, get_device()/put_device()
694 * may be used for reference counting of @dev after calling this
697 * All fields in @dev must be initialized by the caller to 0, except
698 * for those explicitly set to some other value. The simplest
699 * approach is to use kzalloc() to allocate the structure containing
702 * NOTE: Use put_device() to give up your reference instead of freeing
703 * @dev directly once you have called this function.
705 void device_initialize(struct device *dev)
707 dev->kobj.kset = devices_kset;
708 kobject_init(&dev->kobj, &device_ktype);
709 INIT_LIST_HEAD(&dev->dma_pools);
710 mutex_init(&dev->mutex);
711 lockdep_set_novalidate_class(&dev->mutex);
712 spin_lock_init(&dev->devres_lock);
713 INIT_LIST_HEAD(&dev->devres_head);
715 set_dev_node(dev, -1);
716 #ifdef CONFIG_GENERIC_MSI_IRQ
717 INIT_LIST_HEAD(&dev->msi_list);
720 EXPORT_SYMBOL_GPL(device_initialize);
722 struct kobject *virtual_device_parent(struct device *dev)
724 static struct kobject *virtual_dir = NULL;
727 virtual_dir = kobject_create_and_add("virtual",
728 &devices_kset->kobj);
738 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
740 static void class_dir_release(struct kobject *kobj)
742 struct class_dir *dir = to_class_dir(kobj);
747 struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
749 struct class_dir *dir = to_class_dir(kobj);
750 return dir->class->ns_type;
753 static struct kobj_type class_dir_ktype = {
754 .release = class_dir_release,
755 .sysfs_ops = &kobj_sysfs_ops,
756 .child_ns_type = class_dir_child_ns_type
759 static struct kobject *
760 class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
762 struct class_dir *dir;
765 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
767 return ERR_PTR(-ENOMEM);
770 kobject_init(&dir->kobj, &class_dir_ktype);
772 dir->kobj.kset = &class->p->glue_dirs;
774 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
776 kobject_put(&dir->kobj);
777 return ERR_PTR(retval);
782 static DEFINE_MUTEX(gdp_mutex);
784 static struct kobject *get_device_parent(struct device *dev,
785 struct device *parent)
788 struct kobject *kobj = NULL;
789 struct kobject *parent_kobj;
793 /* block disks show up in /sys/block */
794 if (sysfs_deprecated && dev->class == &block_class) {
795 if (parent && parent->class == &block_class)
796 return &parent->kobj;
797 return &block_class.p->subsys.kobj;
802 * If we have no parent, we live in "virtual".
803 * Class-devices with a non class-device as parent, live
804 * in a "glue" directory to prevent namespace collisions.
807 parent_kobj = virtual_device_parent(dev);
808 else if (parent->class && !dev->class->ns_type)
809 return &parent->kobj;
811 parent_kobj = &parent->kobj;
813 mutex_lock(&gdp_mutex);
815 /* find our class-directory at the parent and reference it */
816 spin_lock(&dev->class->p->glue_dirs.list_lock);
817 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
818 if (k->parent == parent_kobj) {
819 kobj = kobject_get(k);
822 spin_unlock(&dev->class->p->glue_dirs.list_lock);
824 mutex_unlock(&gdp_mutex);
828 /* or create a new class-directory at the parent device */
829 k = class_dir_create_and_add(dev->class, parent_kobj);
830 /* do not emit an uevent for this simple "glue" directory */
831 mutex_unlock(&gdp_mutex);
835 /* subsystems can specify a default root directory for their devices */
836 if (!parent && dev->bus && dev->bus->dev_root)
837 return &dev->bus->dev_root->kobj;
840 return &parent->kobj;
844 static inline bool live_in_glue_dir(struct kobject *kobj,
847 if (!kobj || !dev->class ||
848 kobj->kset != &dev->class->p->glue_dirs)
853 static inline struct kobject *get_glue_dir(struct device *dev)
855 return dev->kobj.parent;
859 * make sure cleaning up dir as the last step, we need to make
860 * sure .release handler of kobject is run with holding the
863 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
865 /* see if we live in a "glue" directory */
866 if (!live_in_glue_dir(glue_dir, dev))
869 mutex_lock(&gdp_mutex);
870 if (!kobject_has_children(glue_dir))
871 kobject_del(glue_dir);
872 kobject_put(glue_dir);
873 mutex_unlock(&gdp_mutex);
876 static int device_add_class_symlinks(struct device *dev)
878 struct device_node *of_node = dev_of_node(dev);
882 error = sysfs_create_link(&dev->kobj, &of_node->kobj,"of_node");
884 dev_warn(dev, "Error %d creating of_node link\n",error);
885 /* An error here doesn't warrant bringing down the device */
891 error = sysfs_create_link(&dev->kobj,
892 &dev->class->p->subsys.kobj,
897 if (dev->parent && device_is_not_partition(dev)) {
898 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
905 /* /sys/block has directories and does not need symlinks */
906 if (sysfs_deprecated && dev->class == &block_class)
910 /* link in the class directory pointing to the device */
911 error = sysfs_create_link(&dev->class->p->subsys.kobj,
912 &dev->kobj, dev_name(dev));
919 sysfs_remove_link(&dev->kobj, "device");
922 sysfs_remove_link(&dev->kobj, "subsystem");
924 sysfs_remove_link(&dev->kobj, "of_node");
928 static void device_remove_class_symlinks(struct device *dev)
930 if (dev_of_node(dev))
931 sysfs_remove_link(&dev->kobj, "of_node");
936 if (dev->parent && device_is_not_partition(dev))
937 sysfs_remove_link(&dev->kobj, "device");
938 sysfs_remove_link(&dev->kobj, "subsystem");
940 if (sysfs_deprecated && dev->class == &block_class)
943 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
947 * dev_set_name - set a device name
949 * @fmt: format string for the device's name
951 int dev_set_name(struct device *dev, const char *fmt, ...)
956 va_start(vargs, fmt);
957 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
961 EXPORT_SYMBOL_GPL(dev_set_name);
964 * device_to_dev_kobj - select a /sys/dev/ directory for the device
967 * By default we select char/ for new entries. Setting class->dev_obj
968 * to NULL prevents an entry from being created. class->dev_kobj must
969 * be set (or cleared) before any devices are registered to the class
970 * otherwise device_create_sys_dev_entry() and
971 * device_remove_sys_dev_entry() will disagree about the presence of
974 static struct kobject *device_to_dev_kobj(struct device *dev)
976 struct kobject *kobj;
979 kobj = dev->class->dev_kobj;
981 kobj = sysfs_dev_char_kobj;
986 static int device_create_sys_dev_entry(struct device *dev)
988 struct kobject *kobj = device_to_dev_kobj(dev);
993 format_dev_t(devt_str, dev->devt);
994 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
1000 static void device_remove_sys_dev_entry(struct device *dev)
1002 struct kobject *kobj = device_to_dev_kobj(dev);
1006 format_dev_t(devt_str, dev->devt);
1007 sysfs_remove_link(kobj, devt_str);
1011 int device_private_init(struct device *dev)
1013 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
1016 dev->p->device = dev;
1017 klist_init(&dev->p->klist_children, klist_children_get,
1018 klist_children_put);
1019 INIT_LIST_HEAD(&dev->p->deferred_probe);
1024 * device_add - add device to device hierarchy.
1027 * This is part 2 of device_register(), though may be called
1028 * separately _iff_ device_initialize() has been called separately.
1030 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
1031 * to the global and sibling lists for the device, then
1032 * adds it to the other relevant subsystems of the driver model.
1034 * Do not call this routine or device_register() more than once for
1035 * any device structure. The driver model core is not designed to work
1036 * with devices that get unregistered and then spring back to life.
1037 * (Among other things, it's very hard to guarantee that all references
1038 * to the previous incarnation of @dev have been dropped.) Allocate
1039 * and register a fresh new struct device instead.
1041 * NOTE: _Never_ directly free @dev after calling this function, even
1042 * if it returned an error! Always use put_device() to give up your
1043 * reference instead.
1045 int device_add(struct device *dev)
1047 struct device *parent = NULL;
1048 struct kobject *kobj;
1049 struct class_interface *class_intf;
1050 int error = -EINVAL;
1051 struct kobject *glue_dir = NULL;
1053 dev = get_device(dev);
1058 error = device_private_init(dev);
1064 * for statically allocated devices, which should all be converted
1065 * some day, we need to initialize the name. We prevent reading back
1066 * the name, and force the use of dev_name()
1068 if (dev->init_name) {
1069 dev_set_name(dev, "%s", dev->init_name);
1070 dev->init_name = NULL;
1073 /* subsystems can specify simple device enumeration */
1074 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
1075 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
1077 if (!dev_name(dev)) {
1082 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1084 parent = get_device(dev->parent);
1085 kobj = get_device_parent(dev, parent);
1087 error = PTR_ERR(kobj);
1091 dev->kobj.parent = kobj;
1093 /* use parent numa_node */
1094 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
1095 set_dev_node(dev, dev_to_node(parent));
1097 /* first, register with generic layer. */
1098 /* we require the name to be set before, and pass NULL */
1099 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
1101 glue_dir = get_glue_dir(dev);
1105 /* notify platform of device entry */
1106 if (platform_notify)
1107 platform_notify(dev);
1109 error = device_create_file(dev, &dev_attr_uevent);
1113 error = device_add_class_symlinks(dev);
1116 error = device_add_attrs(dev);
1119 error = bus_add_device(dev);
1122 error = dpm_sysfs_add(dev);
1127 if (MAJOR(dev->devt)) {
1128 error = device_create_file(dev, &dev_attr_dev);
1132 error = device_create_sys_dev_entry(dev);
1136 devtmpfs_create_node(dev);
1139 /* Notify clients of device addition. This call must come
1140 * after dpm_sysfs_add() and before kobject_uevent().
1143 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1144 BUS_NOTIFY_ADD_DEVICE, dev);
1146 kobject_uevent(&dev->kobj, KOBJ_ADD);
1147 bus_probe_device(dev);
1149 klist_add_tail(&dev->p->knode_parent,
1150 &parent->p->klist_children);
1153 mutex_lock(&dev->class->p->mutex);
1154 /* tie the class to the device */
1155 klist_add_tail(&dev->knode_class,
1156 &dev->class->p->klist_devices);
1158 /* notify any interfaces that the device is here */
1159 list_for_each_entry(class_intf,
1160 &dev->class->p->interfaces, node)
1161 if (class_intf->add_dev)
1162 class_intf->add_dev(dev, class_intf);
1163 mutex_unlock(&dev->class->p->mutex);
1169 if (MAJOR(dev->devt))
1170 device_remove_file(dev, &dev_attr_dev);
1172 device_pm_remove(dev);
1173 dpm_sysfs_remove(dev);
1175 bus_remove_device(dev);
1177 device_remove_attrs(dev);
1179 device_remove_class_symlinks(dev);
1181 device_remove_file(dev, &dev_attr_uevent);
1183 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1184 glue_dir = get_glue_dir(dev);
1185 kobject_del(&dev->kobj);
1187 cleanup_glue_dir(dev, glue_dir);
1195 EXPORT_SYMBOL_GPL(device_add);
1198 * device_register - register a device with the system.
1199 * @dev: pointer to the device structure
1201 * This happens in two clean steps - initialize the device
1202 * and add it to the system. The two steps can be called
1203 * separately, but this is the easiest and most common.
1204 * I.e. you should only call the two helpers separately if
1205 * have a clearly defined need to use and refcount the device
1206 * before it is added to the hierarchy.
1208 * For more information, see the kerneldoc for device_initialize()
1211 * NOTE: _Never_ directly free @dev after calling this function, even
1212 * if it returned an error! Always use put_device() to give up the
1213 * reference initialized in this function instead.
1215 int device_register(struct device *dev)
1217 device_initialize(dev);
1218 return device_add(dev);
1220 EXPORT_SYMBOL_GPL(device_register);
1223 * get_device - increment reference count for device.
1226 * This simply forwards the call to kobject_get(), though
1227 * we do take care to provide for the case that we get a NULL
1228 * pointer passed in.
1230 struct device *get_device(struct device *dev)
1232 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
1234 EXPORT_SYMBOL_GPL(get_device);
1237 * put_device - decrement reference count.
1238 * @dev: device in question.
1240 void put_device(struct device *dev)
1242 /* might_sleep(); */
1244 kobject_put(&dev->kobj);
1246 EXPORT_SYMBOL_GPL(put_device);
1249 * device_del - delete device from system.
1252 * This is the first part of the device unregistration
1253 * sequence. This removes the device from the lists we control
1254 * from here, has it removed from the other driver model
1255 * subsystems it was added to in device_add(), and removes it
1256 * from the kobject hierarchy.
1258 * NOTE: this should be called manually _iff_ device_add() was
1259 * also called manually.
1261 void device_del(struct device *dev)
1263 struct device *parent = dev->parent;
1264 struct kobject *glue_dir = NULL;
1265 struct class_interface *class_intf;
1267 /* Notify clients of device removal. This call must come
1268 * before dpm_sysfs_remove().
1271 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1272 BUS_NOTIFY_DEL_DEVICE, dev);
1273 dpm_sysfs_remove(dev);
1275 klist_del(&dev->p->knode_parent);
1276 if (MAJOR(dev->devt)) {
1277 devtmpfs_delete_node(dev);
1278 device_remove_sys_dev_entry(dev);
1279 device_remove_file(dev, &dev_attr_dev);
1282 device_remove_class_symlinks(dev);
1284 mutex_lock(&dev->class->p->mutex);
1285 /* notify any interfaces that the device is now gone */
1286 list_for_each_entry(class_intf,
1287 &dev->class->p->interfaces, node)
1288 if (class_intf->remove_dev)
1289 class_intf->remove_dev(dev, class_intf);
1290 /* remove the device from the class list */
1291 klist_del(&dev->knode_class);
1292 mutex_unlock(&dev->class->p->mutex);
1294 device_remove_file(dev, &dev_attr_uevent);
1295 device_remove_attrs(dev);
1296 bus_remove_device(dev);
1297 device_pm_remove(dev);
1298 driver_deferred_probe_del(dev);
1300 /* Notify the platform of the removal, in case they
1301 * need to do anything...
1303 if (platform_notify_remove)
1304 platform_notify_remove(dev);
1306 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1307 BUS_NOTIFY_REMOVED_DEVICE, dev);
1308 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1309 glue_dir = get_glue_dir(dev);
1310 kobject_del(&dev->kobj);
1311 cleanup_glue_dir(dev, glue_dir);
1314 EXPORT_SYMBOL_GPL(device_del);
1317 * device_unregister - unregister device from system.
1318 * @dev: device going away.
1320 * We do this in two parts, like we do device_register(). First,
1321 * we remove it from all the subsystems with device_del(), then
1322 * we decrement the reference count via put_device(). If that
1323 * is the final reference count, the device will be cleaned up
1324 * via device_release() above. Otherwise, the structure will
1325 * stick around until the final reference to the device is dropped.
1327 void device_unregister(struct device *dev)
1329 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1333 EXPORT_SYMBOL_GPL(device_unregister);
1335 static struct device *prev_device(struct klist_iter *i)
1337 struct klist_node *n = klist_prev(i);
1338 struct device *dev = NULL;
1339 struct device_private *p;
1342 p = to_device_private_parent(n);
1348 static struct device *next_device(struct klist_iter *i)
1350 struct klist_node *n = klist_next(i);
1351 struct device *dev = NULL;
1352 struct device_private *p;
1355 p = to_device_private_parent(n);
1362 * device_get_devnode - path of device node file
1364 * @mode: returned file access mode
1365 * @uid: returned file owner
1366 * @gid: returned file group
1367 * @tmp: possibly allocated string
1369 * Return the relative path of a possible device node.
1370 * Non-default names may need to allocate a memory to compose
1371 * a name. This memory is returned in tmp and needs to be
1372 * freed by the caller.
1374 const char *device_get_devnode(struct device *dev,
1375 umode_t *mode, kuid_t *uid, kgid_t *gid,
1382 /* the device type may provide a specific name */
1383 if (dev->type && dev->type->devnode)
1384 *tmp = dev->type->devnode(dev, mode, uid, gid);
1388 /* the class may provide a specific name */
1389 if (dev->class && dev->class->devnode)
1390 *tmp = dev->class->devnode(dev, mode);
1394 /* return name without allocation, tmp == NULL */
1395 if (strchr(dev_name(dev), '!') == NULL)
1396 return dev_name(dev);
1398 /* replace '!' in the name with '/' */
1399 s = kstrdup(dev_name(dev), GFP_KERNEL);
1402 strreplace(s, '!', '/');
1407 * device_for_each_child - device child iterator.
1408 * @parent: parent struct device.
1409 * @fn: function to be called for each device.
1410 * @data: data for the callback.
1412 * Iterate over @parent's child devices, and call @fn for each,
1415 * We check the return of @fn each time. If it returns anything
1416 * other than 0, we break out and return that value.
1418 int device_for_each_child(struct device *parent, void *data,
1419 int (*fn)(struct device *dev, void *data))
1421 struct klist_iter i;
1422 struct device *child;
1428 klist_iter_init(&parent->p->klist_children, &i);
1429 while ((child = next_device(&i)) && !error)
1430 error = fn(child, data);
1431 klist_iter_exit(&i);
1434 EXPORT_SYMBOL_GPL(device_for_each_child);
1437 * device_for_each_child_reverse - device child iterator in reversed order.
1438 * @parent: parent struct device.
1439 * @fn: function to be called for each device.
1440 * @data: data for the callback.
1442 * Iterate over @parent's child devices, and call @fn for each,
1445 * We check the return of @fn each time. If it returns anything
1446 * other than 0, we break out and return that value.
1448 int device_for_each_child_reverse(struct device *parent, void *data,
1449 int (*fn)(struct device *dev, void *data))
1451 struct klist_iter i;
1452 struct device *child;
1458 klist_iter_init(&parent->p->klist_children, &i);
1459 while ((child = prev_device(&i)) && !error)
1460 error = fn(child, data);
1461 klist_iter_exit(&i);
1464 EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
1467 * device_find_child - device iterator for locating a particular device.
1468 * @parent: parent struct device
1469 * @match: Callback function to check device
1470 * @data: Data to pass to match function
1472 * This is similar to the device_for_each_child() function above, but it
1473 * returns a reference to a device that is 'found' for later use, as
1474 * determined by the @match callback.
1476 * The callback should return 0 if the device doesn't match and non-zero
1477 * if it does. If the callback returns non-zero and a reference to the
1478 * current device can be obtained, this function will return to the caller
1479 * and not iterate over any more devices.
1481 * NOTE: you will need to drop the reference with put_device() after use.
1483 struct device *device_find_child(struct device *parent, void *data,
1484 int (*match)(struct device *dev, void *data))
1486 struct klist_iter i;
1487 struct device *child;
1492 klist_iter_init(&parent->p->klist_children, &i);
1493 while ((child = next_device(&i)))
1494 if (match(child, data) && get_device(child))
1496 klist_iter_exit(&i);
1499 EXPORT_SYMBOL_GPL(device_find_child);
1501 int __init devices_init(void)
1503 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1506 dev_kobj = kobject_create_and_add("dev", NULL);
1509 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1510 if (!sysfs_dev_block_kobj)
1511 goto block_kobj_err;
1512 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1513 if (!sysfs_dev_char_kobj)
1519 kobject_put(sysfs_dev_block_kobj);
1521 kobject_put(dev_kobj);
1523 kset_unregister(devices_kset);
1527 static int device_check_offline(struct device *dev, void *not_used)
1531 ret = device_for_each_child(dev, NULL, device_check_offline);
1535 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
1539 * device_offline - Prepare the device for hot-removal.
1540 * @dev: Device to be put offline.
1542 * Execute the device bus type's .offline() callback, if present, to prepare
1543 * the device for a subsequent hot-removal. If that succeeds, the device must
1544 * not be used until either it is removed or its bus type's .online() callback
1547 * Call under device_hotplug_lock.
1549 int device_offline(struct device *dev)
1553 if (dev->offline_disabled)
1556 ret = device_for_each_child(dev, NULL, device_check_offline);
1561 if (device_supports_offline(dev)) {
1565 ret = dev->bus->offline(dev);
1567 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
1568 dev->offline = true;
1578 * device_online - Put the device back online after successful device_offline().
1579 * @dev: Device to be put back online.
1581 * If device_offline() has been successfully executed for @dev, but the device
1582 * has not been removed subsequently, execute its bus type's .online() callback
1583 * to indicate that the device can be used again.
1585 * Call under device_hotplug_lock.
1587 int device_online(struct device *dev)
1592 if (device_supports_offline(dev)) {
1594 ret = dev->bus->online(dev);
1596 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
1597 dev->offline = false;
1608 struct root_device {
1610 struct module *owner;
1613 static inline struct root_device *to_root_device(struct device *d)
1615 return container_of(d, struct root_device, dev);
1618 static void root_device_release(struct device *dev)
1620 kfree(to_root_device(dev));
1624 * __root_device_register - allocate and register a root device
1625 * @name: root device name
1626 * @owner: owner module of the root device, usually THIS_MODULE
1628 * This function allocates a root device and registers it
1629 * using device_register(). In order to free the returned
1630 * device, use root_device_unregister().
1632 * Root devices are dummy devices which allow other devices
1633 * to be grouped under /sys/devices. Use this function to
1634 * allocate a root device and then use it as the parent of
1635 * any device which should appear under /sys/devices/{name}
1637 * The /sys/devices/{name} directory will also contain a
1638 * 'module' symlink which points to the @owner directory
1641 * Returns &struct device pointer on success, or ERR_PTR() on error.
1643 * Note: You probably want to use root_device_register().
1645 struct device *__root_device_register(const char *name, struct module *owner)
1647 struct root_device *root;
1650 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
1652 return ERR_PTR(err);
1654 err = dev_set_name(&root->dev, "%s", name);
1657 return ERR_PTR(err);
1660 root->dev.release = root_device_release;
1662 err = device_register(&root->dev);
1664 put_device(&root->dev);
1665 return ERR_PTR(err);
1668 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
1670 struct module_kobject *mk = &owner->mkobj;
1672 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
1674 device_unregister(&root->dev);
1675 return ERR_PTR(err);
1677 root->owner = owner;
1683 EXPORT_SYMBOL_GPL(__root_device_register);
1686 * root_device_unregister - unregister and free a root device
1687 * @dev: device going away
1689 * This function unregisters and cleans up a device that was created by
1690 * root_device_register().
1692 void root_device_unregister(struct device *dev)
1694 struct root_device *root = to_root_device(dev);
1697 sysfs_remove_link(&root->dev.kobj, "module");
1699 device_unregister(dev);
1701 EXPORT_SYMBOL_GPL(root_device_unregister);
1704 static void device_create_release(struct device *dev)
1706 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1710 static struct device *
1711 device_create_groups_vargs(struct class *class, struct device *parent,
1712 dev_t devt, void *drvdata,
1713 const struct attribute_group **groups,
1714 const char *fmt, va_list args)
1716 struct device *dev = NULL;
1717 int retval = -ENODEV;
1719 if (class == NULL || IS_ERR(class))
1722 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1728 device_initialize(dev);
1731 dev->parent = parent;
1732 dev->groups = groups;
1733 dev->release = device_create_release;
1734 dev_set_drvdata(dev, drvdata);
1736 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
1740 retval = device_add(dev);
1748 return ERR_PTR(retval);
1752 * device_create_vargs - creates a device and registers it with sysfs
1753 * @class: pointer to the struct class that this device should be registered to
1754 * @parent: pointer to the parent struct device of this new device, if any
1755 * @devt: the dev_t for the char device to be added
1756 * @drvdata: the data to be added to the device for callbacks
1757 * @fmt: string for the device's name
1758 * @args: va_list for the device's name
1760 * This function can be used by char device classes. A struct device
1761 * will be created in sysfs, registered to the specified class.
1763 * A "dev" file will be created, showing the dev_t for the device, if
1764 * the dev_t is not 0,0.
1765 * If a pointer to a parent struct device is passed in, the newly created
1766 * struct device will be a child of that device in sysfs.
1767 * The pointer to the struct device will be returned from the call.
1768 * Any further sysfs files that might be required can be created using this
1771 * Returns &struct device pointer on success, or ERR_PTR() on error.
1773 * Note: the struct class passed to this function must have previously
1774 * been created with a call to class_create().
1776 struct device *device_create_vargs(struct class *class, struct device *parent,
1777 dev_t devt, void *drvdata, const char *fmt,
1780 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
1783 EXPORT_SYMBOL_GPL(device_create_vargs);
1786 * device_create - creates a device and registers it with sysfs
1787 * @class: pointer to the struct class that this device should be registered to
1788 * @parent: pointer to the parent struct device of this new device, if any
1789 * @devt: the dev_t for the char device to be added
1790 * @drvdata: the data to be added to the device for callbacks
1791 * @fmt: string for the device's name
1793 * This function can be used by char device classes. A struct device
1794 * will be created in sysfs, registered to the specified class.
1796 * A "dev" file will be created, showing the dev_t for the device, if
1797 * the dev_t is not 0,0.
1798 * If a pointer to a parent struct device is passed in, the newly created
1799 * struct device will be a child of that device in sysfs.
1800 * The pointer to the struct device will be returned from the call.
1801 * Any further sysfs files that might be required can be created using this
1804 * Returns &struct device pointer on success, or ERR_PTR() on error.
1806 * Note: the struct class passed to this function must have previously
1807 * been created with a call to class_create().
1809 struct device *device_create(struct class *class, struct device *parent,
1810 dev_t devt, void *drvdata, const char *fmt, ...)
1815 va_start(vargs, fmt);
1816 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
1820 EXPORT_SYMBOL_GPL(device_create);
1823 * device_create_with_groups - creates a device and registers it with sysfs
1824 * @class: pointer to the struct class that this device should be registered to
1825 * @parent: pointer to the parent struct device of this new device, if any
1826 * @devt: the dev_t for the char device to be added
1827 * @drvdata: the data to be added to the device for callbacks
1828 * @groups: NULL-terminated list of attribute groups to be created
1829 * @fmt: string for the device's name
1831 * This function can be used by char device classes. A struct device
1832 * will be created in sysfs, registered to the specified class.
1833 * Additional attributes specified in the groups parameter will also
1834 * be created automatically.
1836 * A "dev" file will be created, showing the dev_t for the device, if
1837 * the dev_t is not 0,0.
1838 * If a pointer to a parent struct device is passed in, the newly created
1839 * struct device will be a child of that device in sysfs.
1840 * The pointer to the struct device will be returned from the call.
1841 * Any further sysfs files that might be required can be created using this
1844 * Returns &struct device pointer on success, or ERR_PTR() on error.
1846 * Note: the struct class passed to this function must have previously
1847 * been created with a call to class_create().
1849 struct device *device_create_with_groups(struct class *class,
1850 struct device *parent, dev_t devt,
1852 const struct attribute_group **groups,
1853 const char *fmt, ...)
1858 va_start(vargs, fmt);
1859 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
1864 EXPORT_SYMBOL_GPL(device_create_with_groups);
1866 static int __match_devt(struct device *dev, const void *data)
1868 const dev_t *devt = data;
1870 return dev->devt == *devt;
1874 * device_destroy - removes a device that was created with device_create()
1875 * @class: pointer to the struct class that this device was registered with
1876 * @devt: the dev_t of the device that was previously registered
1878 * This call unregisters and cleans up a device that was created with a
1879 * call to device_create().
1881 void device_destroy(struct class *class, dev_t devt)
1885 dev = class_find_device(class, NULL, &devt, __match_devt);
1888 device_unregister(dev);
1891 EXPORT_SYMBOL_GPL(device_destroy);
1894 * device_rename - renames a device
1895 * @dev: the pointer to the struct device to be renamed
1896 * @new_name: the new name of the device
1898 * It is the responsibility of the caller to provide mutual
1899 * exclusion between two different calls of device_rename
1900 * on the same device to ensure that new_name is valid and
1901 * won't conflict with other devices.
1903 * Note: Don't call this function. Currently, the networking layer calls this
1904 * function, but that will change. The following text from Kay Sievers offers
1907 * Renaming devices is racy at many levels, symlinks and other stuff are not
1908 * replaced atomically, and you get a "move" uevent, but it's not easy to
1909 * connect the event to the old and new device. Device nodes are not renamed at
1910 * all, there isn't even support for that in the kernel now.
1912 * In the meantime, during renaming, your target name might be taken by another
1913 * driver, creating conflicts. Or the old name is taken directly after you
1914 * renamed it -- then you get events for the same DEVPATH, before you even see
1915 * the "move" event. It's just a mess, and nothing new should ever rely on
1916 * kernel device renaming. Besides that, it's not even implemented now for
1917 * other things than (driver-core wise very simple) network devices.
1919 * We are currently about to change network renaming in udev to completely
1920 * disallow renaming of devices in the same namespace as the kernel uses,
1921 * because we can't solve the problems properly, that arise with swapping names
1922 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
1923 * be allowed to some other name than eth[0-9]*, for the aforementioned
1926 * Make up a "real" name in the driver before you register anything, or add
1927 * some other attributes for userspace to find the device, or use udev to add
1928 * symlinks -- but never rename kernel devices later, it's a complete mess. We
1929 * don't even want to get into that and try to implement the missing pieces in
1930 * the core. We really have other pieces to fix in the driver core mess. :)
1932 int device_rename(struct device *dev, const char *new_name)
1934 struct kobject *kobj = &dev->kobj;
1935 char *old_device_name = NULL;
1938 dev = get_device(dev);
1942 dev_dbg(dev, "renaming to %s\n", new_name);
1944 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
1945 if (!old_device_name) {
1951 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
1952 kobj, old_device_name,
1953 new_name, kobject_namespace(kobj));
1958 error = kobject_rename(kobj, new_name);
1965 kfree(old_device_name);
1969 EXPORT_SYMBOL_GPL(device_rename);
1971 static int device_move_class_links(struct device *dev,
1972 struct device *old_parent,
1973 struct device *new_parent)
1978 sysfs_remove_link(&dev->kobj, "device");
1980 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1986 * device_move - moves a device to a new parent
1987 * @dev: the pointer to the struct device to be moved
1988 * @new_parent: the new parent of the device (can by NULL)
1989 * @dpm_order: how to reorder the dpm_list
1991 int device_move(struct device *dev, struct device *new_parent,
1992 enum dpm_order dpm_order)
1995 struct device *old_parent;
1996 struct kobject *new_parent_kobj;
1998 dev = get_device(dev);
2003 new_parent = get_device(new_parent);
2004 new_parent_kobj = get_device_parent(dev, new_parent);
2005 if (IS_ERR(new_parent_kobj)) {
2006 error = PTR_ERR(new_parent_kobj);
2007 put_device(new_parent);
2011 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
2012 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
2013 error = kobject_move(&dev->kobj, new_parent_kobj);
2015 cleanup_glue_dir(dev, new_parent_kobj);
2016 put_device(new_parent);
2019 old_parent = dev->parent;
2020 dev->parent = new_parent;
2022 klist_remove(&dev->p->knode_parent);
2024 klist_add_tail(&dev->p->knode_parent,
2025 &new_parent->p->klist_children);
2026 set_dev_node(dev, dev_to_node(new_parent));
2030 error = device_move_class_links(dev, old_parent, new_parent);
2032 /* We ignore errors on cleanup since we're hosed anyway... */
2033 device_move_class_links(dev, new_parent, old_parent);
2034 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
2036 klist_remove(&dev->p->knode_parent);
2037 dev->parent = old_parent;
2039 klist_add_tail(&dev->p->knode_parent,
2040 &old_parent->p->klist_children);
2041 set_dev_node(dev, dev_to_node(old_parent));
2044 cleanup_glue_dir(dev, new_parent_kobj);
2045 put_device(new_parent);
2049 switch (dpm_order) {
2050 case DPM_ORDER_NONE:
2052 case DPM_ORDER_DEV_AFTER_PARENT:
2053 device_pm_move_after(dev, new_parent);
2054 devices_kset_move_after(dev, new_parent);
2056 case DPM_ORDER_PARENT_BEFORE_DEV:
2057 device_pm_move_before(new_parent, dev);
2058 devices_kset_move_before(new_parent, dev);
2060 case DPM_ORDER_DEV_LAST:
2061 device_pm_move_last(dev);
2062 devices_kset_move_last(dev);
2066 put_device(old_parent);
2072 EXPORT_SYMBOL_GPL(device_move);
2075 * device_shutdown - call ->shutdown() on each device to shutdown.
2077 void device_shutdown(void)
2079 struct device *dev, *parent;
2081 spin_lock(&devices_kset->list_lock);
2083 * Walk the devices list backward, shutting down each in turn.
2084 * Beware that device unplug events may also start pulling
2085 * devices offline, even as the system is shutting down.
2087 while (!list_empty(&devices_kset->list)) {
2088 dev = list_entry(devices_kset->list.prev, struct device,
2092 * hold reference count of device's parent to
2093 * prevent it from being freed because parent's
2094 * lock is to be held
2096 parent = get_device(dev->parent);
2099 * Make sure the device is off the kset list, in the
2100 * event that dev->*->shutdown() doesn't remove it.
2102 list_del_init(&dev->kobj.entry);
2103 spin_unlock(&devices_kset->list_lock);
2105 /* hold lock to avoid race with probe/release */
2107 device_lock(parent);
2110 /* Don't allow any more runtime suspends */
2111 pm_runtime_get_noresume(dev);
2112 pm_runtime_barrier(dev);
2114 if (dev->class && dev->class->shutdown) {
2116 dev_info(dev, "shutdown\n");
2117 dev->class->shutdown(dev);
2118 } else if (dev->bus && dev->bus->shutdown) {
2120 dev_info(dev, "shutdown\n");
2121 dev->bus->shutdown(dev);
2122 } else if (dev->driver && dev->driver->shutdown) {
2124 dev_info(dev, "shutdown\n");
2125 dev->driver->shutdown(dev);
2130 device_unlock(parent);
2135 spin_lock(&devices_kset->list_lock);
2137 spin_unlock(&devices_kset->list_lock);
2141 * Device logging functions
2144 #ifdef CONFIG_PRINTK
2146 create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
2152 subsys = dev->class->name;
2154 subsys = dev->bus->name;
2158 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
2163 * Add device identifier DEVICE=:
2167 * +sound:card0 subsystem:devname
2169 if (MAJOR(dev->devt)) {
2172 if (strcmp(subsys, "block") == 0)
2177 pos += snprintf(hdr + pos, hdrlen - pos,
2179 c, MAJOR(dev->devt), MINOR(dev->devt));
2180 } else if (strcmp(subsys, "net") == 0) {
2181 struct net_device *net = to_net_dev(dev);
2184 pos += snprintf(hdr + pos, hdrlen - pos,
2185 "DEVICE=n%u", net->ifindex);
2188 pos += snprintf(hdr + pos, hdrlen - pos,
2189 "DEVICE=+%s:%s", subsys, dev_name(dev));
2198 dev_WARN(dev, "device/subsystem name too long");
2202 int dev_vprintk_emit(int level, const struct device *dev,
2203 const char *fmt, va_list args)
2208 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
2210 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
2212 EXPORT_SYMBOL(dev_vprintk_emit);
2214 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
2219 va_start(args, fmt);
2221 r = dev_vprintk_emit(level, dev, fmt, args);
2227 EXPORT_SYMBOL(dev_printk_emit);
2229 static void __dev_printk(const char *level, const struct device *dev,
2230 struct va_format *vaf)
2233 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
2234 dev_driver_string(dev), dev_name(dev), vaf);
2236 printk("%s(NULL device *): %pV", level, vaf);
2239 void dev_printk(const char *level, const struct device *dev,
2240 const char *fmt, ...)
2242 struct va_format vaf;
2245 va_start(args, fmt);
2250 __dev_printk(level, dev, &vaf);
2254 EXPORT_SYMBOL(dev_printk);
2256 #define define_dev_printk_level(func, kern_level) \
2257 void func(const struct device *dev, const char *fmt, ...) \
2259 struct va_format vaf; \
2262 va_start(args, fmt); \
2267 __dev_printk(kern_level, dev, &vaf); \
2271 EXPORT_SYMBOL(func);
2273 define_dev_printk_level(dev_emerg, KERN_EMERG);
2274 define_dev_printk_level(dev_alert, KERN_ALERT);
2275 define_dev_printk_level(dev_crit, KERN_CRIT);
2276 define_dev_printk_level(dev_err, KERN_ERR);
2277 define_dev_printk_level(dev_warn, KERN_WARNING);
2278 define_dev_printk_level(dev_notice, KERN_NOTICE);
2279 define_dev_printk_level(_dev_info, KERN_INFO);
2283 static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
2285 return fwnode && !IS_ERR(fwnode->secondary);
2289 * set_primary_fwnode - Change the primary firmware node of a given device.
2290 * @dev: Device to handle.
2291 * @fwnode: New primary firmware node of the device.
2293 * Set the device's firmware node pointer to @fwnode, but if a secondary
2294 * firmware node of the device is present, preserve it.
2296 void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
2299 struct fwnode_handle *fn = dev->fwnode;
2301 if (fwnode_is_primary(fn))
2304 fwnode->secondary = fn;
2305 dev->fwnode = fwnode;
2307 dev->fwnode = fwnode_is_primary(dev->fwnode) ?
2308 dev->fwnode->secondary : NULL;
2311 EXPORT_SYMBOL_GPL(set_primary_fwnode);
2314 * set_secondary_fwnode - Change the secondary firmware node of a given device.
2315 * @dev: Device to handle.
2316 * @fwnode: New secondary firmware node of the device.
2318 * If a primary firmware node of the device is present, set its secondary
2319 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
2322 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
2325 fwnode->secondary = ERR_PTR(-ENODEV);
2327 if (fwnode_is_primary(dev->fwnode))
2328 dev->fwnode->secondary = fwnode;
2330 dev->fwnode = fwnode;