Revert "ALSA: hda: Flush interrupts on disabling"
[linux/fpc-iii.git] / drivers / base / core.c
blob69a71074dc65b6eeef29fd33b7364aed86104228
1 /*
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/cpufreq.h>
14 #include <linux/device.h>
15 #include <linux/err.h>
16 #include <linux/fwnode.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/slab.h>
20 #include <linux/string.h>
21 #include <linux/kdev_t.h>
22 #include <linux/notifier.h>
23 #include <linux/of.h>
24 #include <linux/of_device.h>
25 #include <linux/genhd.h>
26 #include <linux/kallsyms.h>
27 #include <linux/mutex.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/netdevice.h>
30 #include <linux/sysfs.h>
32 #include "base.h"
33 #include "power/power.h"
35 #ifdef CONFIG_SYSFS_DEPRECATED
36 #ifdef CONFIG_SYSFS_DEPRECATED_V2
37 long sysfs_deprecated = 1;
38 #else
39 long sysfs_deprecated = 0;
40 #endif
41 static int __init sysfs_deprecated_setup(char *arg)
43 return kstrtol(arg, 10, &sysfs_deprecated);
45 early_param("sysfs.deprecated", sysfs_deprecated_setup);
46 #endif
48 int (*platform_notify)(struct device *dev) = NULL;
49 int (*platform_notify_remove)(struct device *dev) = NULL;
50 static struct kobject *dev_kobj;
51 struct kobject *sysfs_dev_char_kobj;
52 struct kobject *sysfs_dev_block_kobj;
54 static DEFINE_MUTEX(device_hotplug_lock);
56 void lock_device_hotplug(void)
58 mutex_lock(&device_hotplug_lock);
61 void unlock_device_hotplug(void)
63 mutex_unlock(&device_hotplug_lock);
66 int lock_device_hotplug_sysfs(void)
68 if (mutex_trylock(&device_hotplug_lock))
69 return 0;
71 /* Avoid busy looping (5 ms of sleep should do). */
72 msleep(5);
73 return restart_syscall();
76 #ifdef CONFIG_BLOCK
77 static inline int device_is_not_partition(struct device *dev)
79 return !(dev->type == &part_type);
81 #else
82 static inline int device_is_not_partition(struct device *dev)
84 return 1;
86 #endif
88 /**
89 * dev_driver_string - Return a device's driver name, if at all possible
90 * @dev: struct device to get the name of
92 * Will return the device's driver's name if it is bound to a device. If
93 * the device is not bound to a driver, it will return the name of the bus
94 * it is attached to. If it is not attached to a bus either, an empty
95 * string will be returned.
97 const char *dev_driver_string(const struct device *dev)
99 struct device_driver *drv;
101 /* dev->driver can change to NULL underneath us because of unbinding,
102 * so be careful about accessing it. dev->bus and dev->class should
103 * never change once they are set, so they don't need special care.
105 drv = ACCESS_ONCE(dev->driver);
106 return drv ? drv->name :
107 (dev->bus ? dev->bus->name :
108 (dev->class ? dev->class->name : ""));
110 EXPORT_SYMBOL(dev_driver_string);
112 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
114 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
115 char *buf)
117 struct device_attribute *dev_attr = to_dev_attr(attr);
118 struct device *dev = kobj_to_dev(kobj);
119 ssize_t ret = -EIO;
121 if (dev_attr->show)
122 ret = dev_attr->show(dev, dev_attr, buf);
123 if (ret >= (ssize_t)PAGE_SIZE) {
124 print_symbol("dev_attr_show: %s returned bad count\n",
125 (unsigned long)dev_attr->show);
127 return ret;
130 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
131 const char *buf, size_t count)
133 struct device_attribute *dev_attr = to_dev_attr(attr);
134 struct device *dev = kobj_to_dev(kobj);
135 ssize_t ret = -EIO;
137 if (dev_attr->store)
138 ret = dev_attr->store(dev, dev_attr, buf, count);
139 return ret;
142 static const struct sysfs_ops dev_sysfs_ops = {
143 .show = dev_attr_show,
144 .store = dev_attr_store,
147 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
149 ssize_t device_store_ulong(struct device *dev,
150 struct device_attribute *attr,
151 const char *buf, size_t size)
153 struct dev_ext_attribute *ea = to_ext_attr(attr);
154 char *end;
155 unsigned long new = simple_strtoul(buf, &end, 0);
156 if (end == buf)
157 return -EINVAL;
158 *(unsigned long *)(ea->var) = new;
159 /* Always return full write size even if we didn't consume all */
160 return size;
162 EXPORT_SYMBOL_GPL(device_store_ulong);
164 ssize_t device_show_ulong(struct device *dev,
165 struct device_attribute *attr,
166 char *buf)
168 struct dev_ext_attribute *ea = to_ext_attr(attr);
169 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
171 EXPORT_SYMBOL_GPL(device_show_ulong);
173 ssize_t device_store_int(struct device *dev,
174 struct device_attribute *attr,
175 const char *buf, size_t size)
177 struct dev_ext_attribute *ea = to_ext_attr(attr);
178 char *end;
179 long new = simple_strtol(buf, &end, 0);
180 if (end == buf || new > INT_MAX || new < INT_MIN)
181 return -EINVAL;
182 *(int *)(ea->var) = new;
183 /* Always return full write size even if we didn't consume all */
184 return size;
186 EXPORT_SYMBOL_GPL(device_store_int);
188 ssize_t device_show_int(struct device *dev,
189 struct device_attribute *attr,
190 char *buf)
192 struct dev_ext_attribute *ea = to_ext_attr(attr);
194 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
196 EXPORT_SYMBOL_GPL(device_show_int);
198 ssize_t device_store_bool(struct device *dev, struct device_attribute *attr,
199 const char *buf, size_t size)
201 struct dev_ext_attribute *ea = to_ext_attr(attr);
203 if (strtobool(buf, ea->var) < 0)
204 return -EINVAL;
206 return size;
208 EXPORT_SYMBOL_GPL(device_store_bool);
210 ssize_t device_show_bool(struct device *dev, struct device_attribute *attr,
211 char *buf)
213 struct dev_ext_attribute *ea = to_ext_attr(attr);
215 return snprintf(buf, PAGE_SIZE, "%d\n", *(bool *)(ea->var));
217 EXPORT_SYMBOL_GPL(device_show_bool);
220 * device_release - free device structure.
221 * @kobj: device's kobject.
223 * This is called once the reference count for the object
224 * reaches 0. We forward the call to the device's release
225 * method, which should handle actually freeing the structure.
227 static void device_release(struct kobject *kobj)
229 struct device *dev = kobj_to_dev(kobj);
230 struct device_private *p = dev->p;
233 * Some platform devices are driven without driver attached
234 * and managed resources may have been acquired. Make sure
235 * all resources are released.
237 * Drivers still can add resources into device after device
238 * is deleted but alive, so release devres here to avoid
239 * possible memory leak.
241 devres_release_all(dev);
243 if (dev->release)
244 dev->release(dev);
245 else if (dev->type && dev->type->release)
246 dev->type->release(dev);
247 else if (dev->class && dev->class->dev_release)
248 dev->class->dev_release(dev);
249 else
250 WARN(1, KERN_ERR "Device '%s' does not have a release() "
251 "function, it is broken and must be fixed.\n",
252 dev_name(dev));
253 kfree(p);
256 static const void *device_namespace(struct kobject *kobj)
258 struct device *dev = kobj_to_dev(kobj);
259 const void *ns = NULL;
261 if (dev->class && dev->class->ns_type)
262 ns = dev->class->namespace(dev);
264 return ns;
267 static struct kobj_type device_ktype = {
268 .release = device_release,
269 .sysfs_ops = &dev_sysfs_ops,
270 .namespace = device_namespace,
274 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
276 struct kobj_type *ktype = get_ktype(kobj);
278 if (ktype == &device_ktype) {
279 struct device *dev = kobj_to_dev(kobj);
280 if (dev->bus)
281 return 1;
282 if (dev->class)
283 return 1;
285 return 0;
288 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
290 struct device *dev = kobj_to_dev(kobj);
292 if (dev->bus)
293 return dev->bus->name;
294 if (dev->class)
295 return dev->class->name;
296 return NULL;
299 static int dev_uevent(struct kset *kset, struct kobject *kobj,
300 struct kobj_uevent_env *env)
302 struct device *dev = kobj_to_dev(kobj);
303 int retval = 0;
305 /* add device node properties if present */
306 if (MAJOR(dev->devt)) {
307 const char *tmp;
308 const char *name;
309 umode_t mode = 0;
310 kuid_t uid = GLOBAL_ROOT_UID;
311 kgid_t gid = GLOBAL_ROOT_GID;
313 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
314 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
315 name = device_get_devnode(dev, &mode, &uid, &gid, &tmp);
316 if (name) {
317 add_uevent_var(env, "DEVNAME=%s", name);
318 if (mode)
319 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
320 if (!uid_eq(uid, GLOBAL_ROOT_UID))
321 add_uevent_var(env, "DEVUID=%u", from_kuid(&init_user_ns, uid));
322 if (!gid_eq(gid, GLOBAL_ROOT_GID))
323 add_uevent_var(env, "DEVGID=%u", from_kgid(&init_user_ns, gid));
324 kfree(tmp);
328 if (dev->type && dev->type->name)
329 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
331 if (dev->driver)
332 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
334 /* Add common DT information about the device */
335 of_device_uevent(dev, env);
337 /* have the bus specific function add its stuff */
338 if (dev->bus && dev->bus->uevent) {
339 retval = dev->bus->uevent(dev, env);
340 if (retval)
341 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
342 dev_name(dev), __func__, retval);
345 /* have the class specific function add its stuff */
346 if (dev->class && dev->class->dev_uevent) {
347 retval = dev->class->dev_uevent(dev, env);
348 if (retval)
349 pr_debug("device: '%s': %s: class uevent() "
350 "returned %d\n", dev_name(dev),
351 __func__, retval);
354 /* have the device type specific function add its stuff */
355 if (dev->type && dev->type->uevent) {
356 retval = dev->type->uevent(dev, env);
357 if (retval)
358 pr_debug("device: '%s': %s: dev_type uevent() "
359 "returned %d\n", dev_name(dev),
360 __func__, retval);
363 return retval;
366 static const struct kset_uevent_ops device_uevent_ops = {
367 .filter = dev_uevent_filter,
368 .name = dev_uevent_name,
369 .uevent = dev_uevent,
372 static ssize_t uevent_show(struct device *dev, struct device_attribute *attr,
373 char *buf)
375 struct kobject *top_kobj;
376 struct kset *kset;
377 struct kobj_uevent_env *env = NULL;
378 int i;
379 size_t count = 0;
380 int retval;
382 /* search the kset, the device belongs to */
383 top_kobj = &dev->kobj;
384 while (!top_kobj->kset && top_kobj->parent)
385 top_kobj = top_kobj->parent;
386 if (!top_kobj->kset)
387 goto out;
389 kset = top_kobj->kset;
390 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
391 goto out;
393 /* respect filter */
394 if (kset->uevent_ops && kset->uevent_ops->filter)
395 if (!kset->uevent_ops->filter(kset, &dev->kobj))
396 goto out;
398 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
399 if (!env)
400 return -ENOMEM;
402 /* let the kset specific function add its keys */
403 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
404 if (retval)
405 goto out;
407 /* copy keys to file */
408 for (i = 0; i < env->envp_idx; i++)
409 count += sprintf(&buf[count], "%s\n", env->envp[i]);
410 out:
411 kfree(env);
412 return count;
415 static ssize_t uevent_store(struct device *dev, struct device_attribute *attr,
416 const char *buf, size_t count)
418 enum kobject_action action;
420 if (kobject_action_type(buf, count, &action) == 0)
421 kobject_uevent(&dev->kobj, action);
422 else
423 dev_err(dev, "uevent: unknown action-string\n");
424 return count;
426 static DEVICE_ATTR_RW(uevent);
428 static ssize_t online_show(struct device *dev, struct device_attribute *attr,
429 char *buf)
431 bool val;
433 device_lock(dev);
434 val = !dev->offline;
435 device_unlock(dev);
436 return sprintf(buf, "%u\n", val);
439 static ssize_t online_store(struct device *dev, struct device_attribute *attr,
440 const char *buf, size_t count)
442 bool val;
443 int ret;
445 ret = strtobool(buf, &val);
446 if (ret < 0)
447 return ret;
449 ret = lock_device_hotplug_sysfs();
450 if (ret)
451 return ret;
453 ret = val ? device_online(dev) : device_offline(dev);
454 unlock_device_hotplug();
455 return ret < 0 ? ret : count;
457 static DEVICE_ATTR_RW(online);
459 int device_add_groups(struct device *dev, const struct attribute_group **groups)
461 return sysfs_create_groups(&dev->kobj, groups);
464 void device_remove_groups(struct device *dev,
465 const struct attribute_group **groups)
467 sysfs_remove_groups(&dev->kobj, groups);
470 static int device_add_attrs(struct device *dev)
472 struct class *class = dev->class;
473 const struct device_type *type = dev->type;
474 int error;
476 if (class) {
477 error = device_add_groups(dev, class->dev_groups);
478 if (error)
479 return error;
482 if (type) {
483 error = device_add_groups(dev, type->groups);
484 if (error)
485 goto err_remove_class_groups;
488 error = device_add_groups(dev, dev->groups);
489 if (error)
490 goto err_remove_type_groups;
492 if (device_supports_offline(dev) && !dev->offline_disabled) {
493 error = device_create_file(dev, &dev_attr_online);
494 if (error)
495 goto err_remove_dev_groups;
498 return 0;
500 err_remove_dev_groups:
501 device_remove_groups(dev, dev->groups);
502 err_remove_type_groups:
503 if (type)
504 device_remove_groups(dev, type->groups);
505 err_remove_class_groups:
506 if (class)
507 device_remove_groups(dev, class->dev_groups);
509 return error;
512 static void device_remove_attrs(struct device *dev)
514 struct class *class = dev->class;
515 const struct device_type *type = dev->type;
517 device_remove_file(dev, &dev_attr_online);
518 device_remove_groups(dev, dev->groups);
520 if (type)
521 device_remove_groups(dev, type->groups);
523 if (class)
524 device_remove_groups(dev, class->dev_groups);
527 static ssize_t dev_show(struct device *dev, struct device_attribute *attr,
528 char *buf)
530 return print_dev_t(buf, dev->devt);
532 static DEVICE_ATTR_RO(dev);
534 /* /sys/devices/ */
535 struct kset *devices_kset;
538 * devices_kset_move_before - Move device in the devices_kset's list.
539 * @deva: Device to move.
540 * @devb: Device @deva should come before.
542 static void devices_kset_move_before(struct device *deva, struct device *devb)
544 if (!devices_kset)
545 return;
546 pr_debug("devices_kset: Moving %s before %s\n",
547 dev_name(deva), dev_name(devb));
548 spin_lock(&devices_kset->list_lock);
549 list_move_tail(&deva->kobj.entry, &devb->kobj.entry);
550 spin_unlock(&devices_kset->list_lock);
554 * devices_kset_move_after - Move device in the devices_kset's list.
555 * @deva: Device to move
556 * @devb: Device @deva should come after.
558 static void devices_kset_move_after(struct device *deva, struct device *devb)
560 if (!devices_kset)
561 return;
562 pr_debug("devices_kset: Moving %s after %s\n",
563 dev_name(deva), dev_name(devb));
564 spin_lock(&devices_kset->list_lock);
565 list_move(&deva->kobj.entry, &devb->kobj.entry);
566 spin_unlock(&devices_kset->list_lock);
570 * devices_kset_move_last - move the device to the end of devices_kset's list.
571 * @dev: device to move
573 void devices_kset_move_last(struct device *dev)
575 if (!devices_kset)
576 return;
577 pr_debug("devices_kset: Moving %s to end of list\n", dev_name(dev));
578 spin_lock(&devices_kset->list_lock);
579 list_move_tail(&dev->kobj.entry, &devices_kset->list);
580 spin_unlock(&devices_kset->list_lock);
584 * device_create_file - create sysfs attribute file for device.
585 * @dev: device.
586 * @attr: device attribute descriptor.
588 int device_create_file(struct device *dev,
589 const struct device_attribute *attr)
591 int error = 0;
593 if (dev) {
594 WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
595 "Attribute %s: write permission without 'store'\n",
596 attr->attr.name);
597 WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
598 "Attribute %s: read permission without 'show'\n",
599 attr->attr.name);
600 error = sysfs_create_file(&dev->kobj, &attr->attr);
603 return error;
605 EXPORT_SYMBOL_GPL(device_create_file);
608 * device_remove_file - remove sysfs attribute file.
609 * @dev: device.
610 * @attr: device attribute descriptor.
612 void device_remove_file(struct device *dev,
613 const struct device_attribute *attr)
615 if (dev)
616 sysfs_remove_file(&dev->kobj, &attr->attr);
618 EXPORT_SYMBOL_GPL(device_remove_file);
621 * device_remove_file_self - remove sysfs attribute file from its own method.
622 * @dev: device.
623 * @attr: device attribute descriptor.
625 * See kernfs_remove_self() for details.
627 bool device_remove_file_self(struct device *dev,
628 const struct device_attribute *attr)
630 if (dev)
631 return sysfs_remove_file_self(&dev->kobj, &attr->attr);
632 else
633 return false;
635 EXPORT_SYMBOL_GPL(device_remove_file_self);
638 * device_create_bin_file - create sysfs binary attribute file for device.
639 * @dev: device.
640 * @attr: device binary attribute descriptor.
642 int device_create_bin_file(struct device *dev,
643 const struct bin_attribute *attr)
645 int error = -EINVAL;
646 if (dev)
647 error = sysfs_create_bin_file(&dev->kobj, attr);
648 return error;
650 EXPORT_SYMBOL_GPL(device_create_bin_file);
653 * device_remove_bin_file - remove sysfs binary attribute file
654 * @dev: device.
655 * @attr: device binary attribute descriptor.
657 void device_remove_bin_file(struct device *dev,
658 const struct bin_attribute *attr)
660 if (dev)
661 sysfs_remove_bin_file(&dev->kobj, attr);
663 EXPORT_SYMBOL_GPL(device_remove_bin_file);
665 static void klist_children_get(struct klist_node *n)
667 struct device_private *p = to_device_private_parent(n);
668 struct device *dev = p->device;
670 get_device(dev);
673 static void klist_children_put(struct klist_node *n)
675 struct device_private *p = to_device_private_parent(n);
676 struct device *dev = p->device;
678 put_device(dev);
682 * device_initialize - init device structure.
683 * @dev: device.
685 * This prepares the device for use by other layers by initializing
686 * its fields.
687 * It is the first half of device_register(), if called by
688 * that function, though it can also be called separately, so one
689 * may use @dev's fields. In particular, get_device()/put_device()
690 * may be used for reference counting of @dev after calling this
691 * function.
693 * All fields in @dev must be initialized by the caller to 0, except
694 * for those explicitly set to some other value. The simplest
695 * approach is to use kzalloc() to allocate the structure containing
696 * @dev.
698 * NOTE: Use put_device() to give up your reference instead of freeing
699 * @dev directly once you have called this function.
701 void device_initialize(struct device *dev)
703 dev->kobj.kset = devices_kset;
704 kobject_init(&dev->kobj, &device_ktype);
705 INIT_LIST_HEAD(&dev->dma_pools);
706 mutex_init(&dev->mutex);
707 lockdep_set_novalidate_class(&dev->mutex);
708 spin_lock_init(&dev->devres_lock);
709 INIT_LIST_HEAD(&dev->devres_head);
710 device_pm_init(dev);
711 set_dev_node(dev, -1);
712 #ifdef CONFIG_GENERIC_MSI_IRQ
713 INIT_LIST_HEAD(&dev->msi_list);
714 #endif
716 EXPORT_SYMBOL_GPL(device_initialize);
718 struct kobject *virtual_device_parent(struct device *dev)
720 static struct kobject *virtual_dir = NULL;
722 if (!virtual_dir)
723 virtual_dir = kobject_create_and_add("virtual",
724 &devices_kset->kobj);
726 return virtual_dir;
729 struct class_dir {
730 struct kobject kobj;
731 struct class *class;
734 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
736 static void class_dir_release(struct kobject *kobj)
738 struct class_dir *dir = to_class_dir(kobj);
739 kfree(dir);
742 static const
743 struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
745 struct class_dir *dir = to_class_dir(kobj);
746 return dir->class->ns_type;
749 static struct kobj_type class_dir_ktype = {
750 .release = class_dir_release,
751 .sysfs_ops = &kobj_sysfs_ops,
752 .child_ns_type = class_dir_child_ns_type
755 static struct kobject *
756 class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
758 struct class_dir *dir;
759 int retval;
761 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
762 if (!dir)
763 return ERR_PTR(-ENOMEM);
765 dir->class = class;
766 kobject_init(&dir->kobj, &class_dir_ktype);
768 dir->kobj.kset = &class->p->glue_dirs;
770 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
771 if (retval < 0) {
772 kobject_put(&dir->kobj);
773 return ERR_PTR(retval);
775 return &dir->kobj;
778 static DEFINE_MUTEX(gdp_mutex);
780 static struct kobject *get_device_parent(struct device *dev,
781 struct device *parent)
783 if (dev->class) {
784 struct kobject *kobj = NULL;
785 struct kobject *parent_kobj;
786 struct kobject *k;
788 #ifdef CONFIG_BLOCK
789 /* block disks show up in /sys/block */
790 if (sysfs_deprecated && dev->class == &block_class) {
791 if (parent && parent->class == &block_class)
792 return &parent->kobj;
793 return &block_class.p->subsys.kobj;
795 #endif
798 * If we have no parent, we live in "virtual".
799 * Class-devices with a non class-device as parent, live
800 * in a "glue" directory to prevent namespace collisions.
802 if (parent == NULL)
803 parent_kobj = virtual_device_parent(dev);
804 else if (parent->class && !dev->class->ns_type)
805 return &parent->kobj;
806 else
807 parent_kobj = &parent->kobj;
809 mutex_lock(&gdp_mutex);
811 /* find our class-directory at the parent and reference it */
812 spin_lock(&dev->class->p->glue_dirs.list_lock);
813 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
814 if (k->parent == parent_kobj) {
815 kobj = kobject_get(k);
816 break;
818 spin_unlock(&dev->class->p->glue_dirs.list_lock);
819 if (kobj) {
820 mutex_unlock(&gdp_mutex);
821 return kobj;
824 /* or create a new class-directory at the parent device */
825 k = class_dir_create_and_add(dev->class, parent_kobj);
826 /* do not emit an uevent for this simple "glue" directory */
827 mutex_unlock(&gdp_mutex);
828 return k;
831 /* subsystems can specify a default root directory for their devices */
832 if (!parent && dev->bus && dev->bus->dev_root)
833 return &dev->bus->dev_root->kobj;
835 if (parent)
836 return &parent->kobj;
837 return NULL;
840 static inline bool live_in_glue_dir(struct kobject *kobj,
841 struct device *dev)
843 if (!kobj || !dev->class ||
844 kobj->kset != &dev->class->p->glue_dirs)
845 return false;
846 return true;
849 static inline struct kobject *get_glue_dir(struct device *dev)
851 return dev->kobj.parent;
855 * make sure cleaning up dir as the last step, we need to make
856 * sure .release handler of kobject is run with holding the
857 * global lock
859 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
861 unsigned int ref;
863 /* see if we live in a "glue" directory */
864 if (!live_in_glue_dir(glue_dir, dev))
865 return;
867 mutex_lock(&gdp_mutex);
869 * There is a race condition between removing glue directory
870 * and adding a new device under the glue directory.
872 * CPU1: CPU2:
874 * device_add()
875 * get_device_parent()
876 * class_dir_create_and_add()
877 * kobject_add_internal()
878 * create_dir() // create glue_dir
880 * device_add()
881 * get_device_parent()
882 * kobject_get() // get glue_dir
884 * device_del()
885 * cleanup_glue_dir()
886 * kobject_del(glue_dir)
888 * kobject_add()
889 * kobject_add_internal()
890 * create_dir() // in glue_dir
891 * sysfs_create_dir_ns()
892 * kernfs_create_dir_ns(sd)
894 * sysfs_remove_dir() // glue_dir->sd=NULL
895 * sysfs_put() // free glue_dir->sd
897 * // sd is freed
898 * kernfs_new_node(sd)
899 * kernfs_get(glue_dir)
900 * kernfs_add_one()
901 * kernfs_put()
903 * Before CPU1 remove last child device under glue dir, if CPU2 add
904 * a new device under glue dir, the glue_dir kobject reference count
905 * will be increase to 2 in kobject_get(k). And CPU2 has been called
906 * kernfs_create_dir_ns(). Meanwhile, CPU1 call sysfs_remove_dir()
907 * and sysfs_put(). This result in glue_dir->sd is freed.
909 * Then the CPU2 will see a stale "empty" but still potentially used
910 * glue dir around in kernfs_new_node().
912 * In order to avoid this happening, we also should make sure that
913 * kernfs_node for glue_dir is released in CPU1 only when refcount
914 * for glue_dir kobj is 1.
916 ref = atomic_read(&glue_dir->kref.refcount);
917 if (!kobject_has_children(glue_dir) && !--ref)
918 kobject_del(glue_dir);
919 kobject_put(glue_dir);
920 mutex_unlock(&gdp_mutex);
923 static int device_add_class_symlinks(struct device *dev)
925 struct device_node *of_node = dev_of_node(dev);
926 int error;
928 if (of_node) {
929 error = sysfs_create_link(&dev->kobj, &of_node->kobj,"of_node");
930 if (error)
931 dev_warn(dev, "Error %d creating of_node link\n",error);
932 /* An error here doesn't warrant bringing down the device */
935 if (!dev->class)
936 return 0;
938 error = sysfs_create_link(&dev->kobj,
939 &dev->class->p->subsys.kobj,
940 "subsystem");
941 if (error)
942 goto out_devnode;
944 if (dev->parent && device_is_not_partition(dev)) {
945 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
946 "device");
947 if (error)
948 goto out_subsys;
951 #ifdef CONFIG_BLOCK
952 /* /sys/block has directories and does not need symlinks */
953 if (sysfs_deprecated && dev->class == &block_class)
954 return 0;
955 #endif
957 /* link in the class directory pointing to the device */
958 error = sysfs_create_link(&dev->class->p->subsys.kobj,
959 &dev->kobj, dev_name(dev));
960 if (error)
961 goto out_device;
963 return 0;
965 out_device:
966 sysfs_remove_link(&dev->kobj, "device");
968 out_subsys:
969 sysfs_remove_link(&dev->kobj, "subsystem");
970 out_devnode:
971 sysfs_remove_link(&dev->kobj, "of_node");
972 return error;
975 static void device_remove_class_symlinks(struct device *dev)
977 if (dev_of_node(dev))
978 sysfs_remove_link(&dev->kobj, "of_node");
980 if (!dev->class)
981 return;
983 if (dev->parent && device_is_not_partition(dev))
984 sysfs_remove_link(&dev->kobj, "device");
985 sysfs_remove_link(&dev->kobj, "subsystem");
986 #ifdef CONFIG_BLOCK
987 if (sysfs_deprecated && dev->class == &block_class)
988 return;
989 #endif
990 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
994 * dev_set_name - set a device name
995 * @dev: device
996 * @fmt: format string for the device's name
998 int dev_set_name(struct device *dev, const char *fmt, ...)
1000 va_list vargs;
1001 int err;
1003 va_start(vargs, fmt);
1004 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
1005 va_end(vargs);
1006 return err;
1008 EXPORT_SYMBOL_GPL(dev_set_name);
1011 * device_to_dev_kobj - select a /sys/dev/ directory for the device
1012 * @dev: device
1014 * By default we select char/ for new entries. Setting class->dev_obj
1015 * to NULL prevents an entry from being created. class->dev_kobj must
1016 * be set (or cleared) before any devices are registered to the class
1017 * otherwise device_create_sys_dev_entry() and
1018 * device_remove_sys_dev_entry() will disagree about the presence of
1019 * the link.
1021 static struct kobject *device_to_dev_kobj(struct device *dev)
1023 struct kobject *kobj;
1025 if (dev->class)
1026 kobj = dev->class->dev_kobj;
1027 else
1028 kobj = sysfs_dev_char_kobj;
1030 return kobj;
1033 static int device_create_sys_dev_entry(struct device *dev)
1035 struct kobject *kobj = device_to_dev_kobj(dev);
1036 int error = 0;
1037 char devt_str[15];
1039 if (kobj) {
1040 format_dev_t(devt_str, dev->devt);
1041 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
1044 return error;
1047 static void device_remove_sys_dev_entry(struct device *dev)
1049 struct kobject *kobj = device_to_dev_kobj(dev);
1050 char devt_str[15];
1052 if (kobj) {
1053 format_dev_t(devt_str, dev->devt);
1054 sysfs_remove_link(kobj, devt_str);
1058 int device_private_init(struct device *dev)
1060 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
1061 if (!dev->p)
1062 return -ENOMEM;
1063 dev->p->device = dev;
1064 klist_init(&dev->p->klist_children, klist_children_get,
1065 klist_children_put);
1066 INIT_LIST_HEAD(&dev->p->deferred_probe);
1067 return 0;
1071 * device_add - add device to device hierarchy.
1072 * @dev: device.
1074 * This is part 2 of device_register(), though may be called
1075 * separately _iff_ device_initialize() has been called separately.
1077 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
1078 * to the global and sibling lists for the device, then
1079 * adds it to the other relevant subsystems of the driver model.
1081 * Do not call this routine or device_register() more than once for
1082 * any device structure. The driver model core is not designed to work
1083 * with devices that get unregistered and then spring back to life.
1084 * (Among other things, it's very hard to guarantee that all references
1085 * to the previous incarnation of @dev have been dropped.) Allocate
1086 * and register a fresh new struct device instead.
1088 * NOTE: _Never_ directly free @dev after calling this function, even
1089 * if it returned an error! Always use put_device() to give up your
1090 * reference instead.
1092 int device_add(struct device *dev)
1094 struct device *parent = NULL;
1095 struct kobject *kobj;
1096 struct class_interface *class_intf;
1097 int error = -EINVAL;
1098 struct kobject *glue_dir = NULL;
1100 dev = get_device(dev);
1101 if (!dev)
1102 goto done;
1104 if (!dev->p) {
1105 error = device_private_init(dev);
1106 if (error)
1107 goto done;
1111 * for statically allocated devices, which should all be converted
1112 * some day, we need to initialize the name. We prevent reading back
1113 * the name, and force the use of dev_name()
1115 if (dev->init_name) {
1116 dev_set_name(dev, "%s", dev->init_name);
1117 dev->init_name = NULL;
1120 /* subsystems can specify simple device enumeration */
1121 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
1122 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
1124 if (!dev_name(dev)) {
1125 error = -EINVAL;
1126 goto name_error;
1129 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1131 parent = get_device(dev->parent);
1132 kobj = get_device_parent(dev, parent);
1133 if (IS_ERR(kobj)) {
1134 error = PTR_ERR(kobj);
1135 goto parent_error;
1137 if (kobj)
1138 dev->kobj.parent = kobj;
1140 /* use parent numa_node */
1141 if (parent && (dev_to_node(dev) == NUMA_NO_NODE))
1142 set_dev_node(dev, dev_to_node(parent));
1144 /* first, register with generic layer. */
1145 /* we require the name to be set before, and pass NULL */
1146 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
1147 if (error) {
1148 glue_dir = get_glue_dir(dev);
1149 goto Error;
1152 /* notify platform of device entry */
1153 if (platform_notify)
1154 platform_notify(dev);
1156 error = device_create_file(dev, &dev_attr_uevent);
1157 if (error)
1158 goto attrError;
1160 error = device_add_class_symlinks(dev);
1161 if (error)
1162 goto SymlinkError;
1163 error = device_add_attrs(dev);
1164 if (error)
1165 goto AttrsError;
1166 error = bus_add_device(dev);
1167 if (error)
1168 goto BusError;
1169 error = dpm_sysfs_add(dev);
1170 if (error)
1171 goto DPMError;
1172 device_pm_add(dev);
1174 if (MAJOR(dev->devt)) {
1175 error = device_create_file(dev, &dev_attr_dev);
1176 if (error)
1177 goto DevAttrError;
1179 error = device_create_sys_dev_entry(dev);
1180 if (error)
1181 goto SysEntryError;
1183 devtmpfs_create_node(dev);
1186 /* Notify clients of device addition. This call must come
1187 * after dpm_sysfs_add() and before kobject_uevent().
1189 if (dev->bus)
1190 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1191 BUS_NOTIFY_ADD_DEVICE, dev);
1193 kobject_uevent(&dev->kobj, KOBJ_ADD);
1194 bus_probe_device(dev);
1195 if (parent)
1196 klist_add_tail(&dev->p->knode_parent,
1197 &parent->p->klist_children);
1199 if (dev->class) {
1200 mutex_lock(&dev->class->p->mutex);
1201 /* tie the class to the device */
1202 klist_add_tail(&dev->knode_class,
1203 &dev->class->p->klist_devices);
1205 /* notify any interfaces that the device is here */
1206 list_for_each_entry(class_intf,
1207 &dev->class->p->interfaces, node)
1208 if (class_intf->add_dev)
1209 class_intf->add_dev(dev, class_intf);
1210 mutex_unlock(&dev->class->p->mutex);
1212 done:
1213 put_device(dev);
1214 return error;
1215 SysEntryError:
1216 if (MAJOR(dev->devt))
1217 device_remove_file(dev, &dev_attr_dev);
1218 DevAttrError:
1219 device_pm_remove(dev);
1220 dpm_sysfs_remove(dev);
1221 DPMError:
1222 bus_remove_device(dev);
1223 BusError:
1224 device_remove_attrs(dev);
1225 AttrsError:
1226 device_remove_class_symlinks(dev);
1227 SymlinkError:
1228 device_remove_file(dev, &dev_attr_uevent);
1229 attrError:
1230 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1231 glue_dir = get_glue_dir(dev);
1232 kobject_del(&dev->kobj);
1233 Error:
1234 cleanup_glue_dir(dev, glue_dir);
1235 parent_error:
1236 put_device(parent);
1237 name_error:
1238 kfree(dev->p);
1239 dev->p = NULL;
1240 goto done;
1242 EXPORT_SYMBOL_GPL(device_add);
1245 * device_register - register a device with the system.
1246 * @dev: pointer to the device structure
1248 * This happens in two clean steps - initialize the device
1249 * and add it to the system. The two steps can be called
1250 * separately, but this is the easiest and most common.
1251 * I.e. you should only call the two helpers separately if
1252 * have a clearly defined need to use and refcount the device
1253 * before it is added to the hierarchy.
1255 * For more information, see the kerneldoc for device_initialize()
1256 * and device_add().
1258 * NOTE: _Never_ directly free @dev after calling this function, even
1259 * if it returned an error! Always use put_device() to give up the
1260 * reference initialized in this function instead.
1262 int device_register(struct device *dev)
1264 device_initialize(dev);
1265 return device_add(dev);
1267 EXPORT_SYMBOL_GPL(device_register);
1270 * get_device - increment reference count for device.
1271 * @dev: device.
1273 * This simply forwards the call to kobject_get(), though
1274 * we do take care to provide for the case that we get a NULL
1275 * pointer passed in.
1277 struct device *get_device(struct device *dev)
1279 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
1281 EXPORT_SYMBOL_GPL(get_device);
1284 * put_device - decrement reference count.
1285 * @dev: device in question.
1287 void put_device(struct device *dev)
1289 /* might_sleep(); */
1290 if (dev)
1291 kobject_put(&dev->kobj);
1293 EXPORT_SYMBOL_GPL(put_device);
1296 * device_del - delete device from system.
1297 * @dev: device.
1299 * This is the first part of the device unregistration
1300 * sequence. This removes the device from the lists we control
1301 * from here, has it removed from the other driver model
1302 * subsystems it was added to in device_add(), and removes it
1303 * from the kobject hierarchy.
1305 * NOTE: this should be called manually _iff_ device_add() was
1306 * also called manually.
1308 void device_del(struct device *dev)
1310 struct device *parent = dev->parent;
1311 struct kobject *glue_dir = NULL;
1312 struct class_interface *class_intf;
1314 /* Notify clients of device removal. This call must come
1315 * before dpm_sysfs_remove().
1317 if (dev->bus)
1318 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1319 BUS_NOTIFY_DEL_DEVICE, dev);
1320 dpm_sysfs_remove(dev);
1321 if (parent)
1322 klist_del(&dev->p->knode_parent);
1323 if (MAJOR(dev->devt)) {
1324 devtmpfs_delete_node(dev);
1325 device_remove_sys_dev_entry(dev);
1326 device_remove_file(dev, &dev_attr_dev);
1328 if (dev->class) {
1329 device_remove_class_symlinks(dev);
1331 mutex_lock(&dev->class->p->mutex);
1332 /* notify any interfaces that the device is now gone */
1333 list_for_each_entry(class_intf,
1334 &dev->class->p->interfaces, node)
1335 if (class_intf->remove_dev)
1336 class_intf->remove_dev(dev, class_intf);
1337 /* remove the device from the class list */
1338 klist_del(&dev->knode_class);
1339 mutex_unlock(&dev->class->p->mutex);
1341 device_remove_file(dev, &dev_attr_uevent);
1342 device_remove_attrs(dev);
1343 bus_remove_device(dev);
1344 device_pm_remove(dev);
1345 driver_deferred_probe_del(dev);
1346 device_remove_properties(dev);
1348 /* Notify the platform of the removal, in case they
1349 * need to do anything...
1351 if (platform_notify_remove)
1352 platform_notify_remove(dev);
1353 if (dev->bus)
1354 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1355 BUS_NOTIFY_REMOVED_DEVICE, dev);
1356 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1357 glue_dir = get_glue_dir(dev);
1358 kobject_del(&dev->kobj);
1359 cleanup_glue_dir(dev, glue_dir);
1360 put_device(parent);
1362 EXPORT_SYMBOL_GPL(device_del);
1365 * device_unregister - unregister device from system.
1366 * @dev: device going away.
1368 * We do this in two parts, like we do device_register(). First,
1369 * we remove it from all the subsystems with device_del(), then
1370 * we decrement the reference count via put_device(). If that
1371 * is the final reference count, the device will be cleaned up
1372 * via device_release() above. Otherwise, the structure will
1373 * stick around until the final reference to the device is dropped.
1375 void device_unregister(struct device *dev)
1377 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1378 device_del(dev);
1379 put_device(dev);
1381 EXPORT_SYMBOL_GPL(device_unregister);
1383 static struct device *prev_device(struct klist_iter *i)
1385 struct klist_node *n = klist_prev(i);
1386 struct device *dev = NULL;
1387 struct device_private *p;
1389 if (n) {
1390 p = to_device_private_parent(n);
1391 dev = p->device;
1393 return dev;
1396 static struct device *next_device(struct klist_iter *i)
1398 struct klist_node *n = klist_next(i);
1399 struct device *dev = NULL;
1400 struct device_private *p;
1402 if (n) {
1403 p = to_device_private_parent(n);
1404 dev = p->device;
1406 return dev;
1410 * device_get_devnode - path of device node file
1411 * @dev: device
1412 * @mode: returned file access mode
1413 * @uid: returned file owner
1414 * @gid: returned file group
1415 * @tmp: possibly allocated string
1417 * Return the relative path of a possible device node.
1418 * Non-default names may need to allocate a memory to compose
1419 * a name. This memory is returned in tmp and needs to be
1420 * freed by the caller.
1422 const char *device_get_devnode(struct device *dev,
1423 umode_t *mode, kuid_t *uid, kgid_t *gid,
1424 const char **tmp)
1426 char *s;
1428 *tmp = NULL;
1430 /* the device type may provide a specific name */
1431 if (dev->type && dev->type->devnode)
1432 *tmp = dev->type->devnode(dev, mode, uid, gid);
1433 if (*tmp)
1434 return *tmp;
1436 /* the class may provide a specific name */
1437 if (dev->class && dev->class->devnode)
1438 *tmp = dev->class->devnode(dev, mode);
1439 if (*tmp)
1440 return *tmp;
1442 /* return name without allocation, tmp == NULL */
1443 if (strchr(dev_name(dev), '!') == NULL)
1444 return dev_name(dev);
1446 /* replace '!' in the name with '/' */
1447 s = kstrdup(dev_name(dev), GFP_KERNEL);
1448 if (!s)
1449 return NULL;
1450 strreplace(s, '!', '/');
1451 return *tmp = s;
1455 * device_for_each_child - device child iterator.
1456 * @parent: parent struct device.
1457 * @fn: function to be called for each device.
1458 * @data: data for the callback.
1460 * Iterate over @parent's child devices, and call @fn for each,
1461 * passing it @data.
1463 * We check the return of @fn each time. If it returns anything
1464 * other than 0, we break out and return that value.
1466 int device_for_each_child(struct device *parent, void *data,
1467 int (*fn)(struct device *dev, void *data))
1469 struct klist_iter i;
1470 struct device *child;
1471 int error = 0;
1473 if (!parent->p)
1474 return 0;
1476 klist_iter_init(&parent->p->klist_children, &i);
1477 while ((child = next_device(&i)) && !error)
1478 error = fn(child, data);
1479 klist_iter_exit(&i);
1480 return error;
1482 EXPORT_SYMBOL_GPL(device_for_each_child);
1485 * device_for_each_child_reverse - device child iterator in reversed order.
1486 * @parent: parent struct device.
1487 * @fn: function to be called for each device.
1488 * @data: data for the callback.
1490 * Iterate over @parent's child devices, and call @fn for each,
1491 * passing it @data.
1493 * We check the return of @fn each time. If it returns anything
1494 * other than 0, we break out and return that value.
1496 int device_for_each_child_reverse(struct device *parent, void *data,
1497 int (*fn)(struct device *dev, void *data))
1499 struct klist_iter i;
1500 struct device *child;
1501 int error = 0;
1503 if (!parent->p)
1504 return 0;
1506 klist_iter_init(&parent->p->klist_children, &i);
1507 while ((child = prev_device(&i)) && !error)
1508 error = fn(child, data);
1509 klist_iter_exit(&i);
1510 return error;
1512 EXPORT_SYMBOL_GPL(device_for_each_child_reverse);
1515 * device_find_child - device iterator for locating a particular device.
1516 * @parent: parent struct device
1517 * @match: Callback function to check device
1518 * @data: Data to pass to match function
1520 * This is similar to the device_for_each_child() function above, but it
1521 * returns a reference to a device that is 'found' for later use, as
1522 * determined by the @match callback.
1524 * The callback should return 0 if the device doesn't match and non-zero
1525 * if it does. If the callback returns non-zero and a reference to the
1526 * current device can be obtained, this function will return to the caller
1527 * and not iterate over any more devices.
1529 * NOTE: you will need to drop the reference with put_device() after use.
1531 struct device *device_find_child(struct device *parent, void *data,
1532 int (*match)(struct device *dev, void *data))
1534 struct klist_iter i;
1535 struct device *child;
1537 if (!parent)
1538 return NULL;
1540 klist_iter_init(&parent->p->klist_children, &i);
1541 while ((child = next_device(&i)))
1542 if (match(child, data) && get_device(child))
1543 break;
1544 klist_iter_exit(&i);
1545 return child;
1547 EXPORT_SYMBOL_GPL(device_find_child);
1549 int __init devices_init(void)
1551 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1552 if (!devices_kset)
1553 return -ENOMEM;
1554 dev_kobj = kobject_create_and_add("dev", NULL);
1555 if (!dev_kobj)
1556 goto dev_kobj_err;
1557 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1558 if (!sysfs_dev_block_kobj)
1559 goto block_kobj_err;
1560 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1561 if (!sysfs_dev_char_kobj)
1562 goto char_kobj_err;
1564 return 0;
1566 char_kobj_err:
1567 kobject_put(sysfs_dev_block_kobj);
1568 block_kobj_err:
1569 kobject_put(dev_kobj);
1570 dev_kobj_err:
1571 kset_unregister(devices_kset);
1572 return -ENOMEM;
1575 static int device_check_offline(struct device *dev, void *not_used)
1577 int ret;
1579 ret = device_for_each_child(dev, NULL, device_check_offline);
1580 if (ret)
1581 return ret;
1583 return device_supports_offline(dev) && !dev->offline ? -EBUSY : 0;
1587 * device_offline - Prepare the device for hot-removal.
1588 * @dev: Device to be put offline.
1590 * Execute the device bus type's .offline() callback, if present, to prepare
1591 * the device for a subsequent hot-removal. If that succeeds, the device must
1592 * not be used until either it is removed or its bus type's .online() callback
1593 * is executed.
1595 * Call under device_hotplug_lock.
1597 int device_offline(struct device *dev)
1599 int ret;
1601 if (dev->offline_disabled)
1602 return -EPERM;
1604 ret = device_for_each_child(dev, NULL, device_check_offline);
1605 if (ret)
1606 return ret;
1608 device_lock(dev);
1609 if (device_supports_offline(dev)) {
1610 if (dev->offline) {
1611 ret = 1;
1612 } else {
1613 ret = dev->bus->offline(dev);
1614 if (!ret) {
1615 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
1616 dev->offline = true;
1620 device_unlock(dev);
1622 return ret;
1626 * device_online - Put the device back online after successful device_offline().
1627 * @dev: Device to be put back online.
1629 * If device_offline() has been successfully executed for @dev, but the device
1630 * has not been removed subsequently, execute its bus type's .online() callback
1631 * to indicate that the device can be used again.
1633 * Call under device_hotplug_lock.
1635 int device_online(struct device *dev)
1637 int ret = 0;
1639 device_lock(dev);
1640 if (device_supports_offline(dev)) {
1641 if (dev->offline) {
1642 ret = dev->bus->online(dev);
1643 if (!ret) {
1644 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
1645 dev->offline = false;
1647 } else {
1648 ret = 1;
1651 device_unlock(dev);
1653 return ret;
1656 struct root_device {
1657 struct device dev;
1658 struct module *owner;
1661 static inline struct root_device *to_root_device(struct device *d)
1663 return container_of(d, struct root_device, dev);
1666 static void root_device_release(struct device *dev)
1668 kfree(to_root_device(dev));
1672 * __root_device_register - allocate and register a root device
1673 * @name: root device name
1674 * @owner: owner module of the root device, usually THIS_MODULE
1676 * This function allocates a root device and registers it
1677 * using device_register(). In order to free the returned
1678 * device, use root_device_unregister().
1680 * Root devices are dummy devices which allow other devices
1681 * to be grouped under /sys/devices. Use this function to
1682 * allocate a root device and then use it as the parent of
1683 * any device which should appear under /sys/devices/{name}
1685 * The /sys/devices/{name} directory will also contain a
1686 * 'module' symlink which points to the @owner directory
1687 * in sysfs.
1689 * Returns &struct device pointer on success, or ERR_PTR() on error.
1691 * Note: You probably want to use root_device_register().
1693 struct device *__root_device_register(const char *name, struct module *owner)
1695 struct root_device *root;
1696 int err = -ENOMEM;
1698 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
1699 if (!root)
1700 return ERR_PTR(err);
1702 err = dev_set_name(&root->dev, "%s", name);
1703 if (err) {
1704 kfree(root);
1705 return ERR_PTR(err);
1708 root->dev.release = root_device_release;
1710 err = device_register(&root->dev);
1711 if (err) {
1712 put_device(&root->dev);
1713 return ERR_PTR(err);
1716 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
1717 if (owner) {
1718 struct module_kobject *mk = &owner->mkobj;
1720 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
1721 if (err) {
1722 device_unregister(&root->dev);
1723 return ERR_PTR(err);
1725 root->owner = owner;
1727 #endif
1729 return &root->dev;
1731 EXPORT_SYMBOL_GPL(__root_device_register);
1734 * root_device_unregister - unregister and free a root device
1735 * @dev: device going away
1737 * This function unregisters and cleans up a device that was created by
1738 * root_device_register().
1740 void root_device_unregister(struct device *dev)
1742 struct root_device *root = to_root_device(dev);
1744 if (root->owner)
1745 sysfs_remove_link(&root->dev.kobj, "module");
1747 device_unregister(dev);
1749 EXPORT_SYMBOL_GPL(root_device_unregister);
1752 static void device_create_release(struct device *dev)
1754 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1755 kfree(dev);
1758 static struct device *
1759 device_create_groups_vargs(struct class *class, struct device *parent,
1760 dev_t devt, void *drvdata,
1761 const struct attribute_group **groups,
1762 const char *fmt, va_list args)
1764 struct device *dev = NULL;
1765 int retval = -ENODEV;
1767 if (class == NULL || IS_ERR(class))
1768 goto error;
1770 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1771 if (!dev) {
1772 retval = -ENOMEM;
1773 goto error;
1776 device_initialize(dev);
1777 dev->devt = devt;
1778 dev->class = class;
1779 dev->parent = parent;
1780 dev->groups = groups;
1781 dev->release = device_create_release;
1782 dev_set_drvdata(dev, drvdata);
1784 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
1785 if (retval)
1786 goto error;
1788 retval = device_add(dev);
1789 if (retval)
1790 goto error;
1792 return dev;
1794 error:
1795 put_device(dev);
1796 return ERR_PTR(retval);
1800 * device_create_vargs - creates a device and registers it with sysfs
1801 * @class: pointer to the struct class that this device should be registered to
1802 * @parent: pointer to the parent struct device of this new device, if any
1803 * @devt: the dev_t for the char device to be added
1804 * @drvdata: the data to be added to the device for callbacks
1805 * @fmt: string for the device's name
1806 * @args: va_list for the device's name
1808 * This function can be used by char device classes. A struct device
1809 * will be created in sysfs, registered to the specified class.
1811 * A "dev" file will be created, showing the dev_t for the device, if
1812 * the dev_t is not 0,0.
1813 * If a pointer to a parent struct device is passed in, the newly created
1814 * struct device will be a child of that device in sysfs.
1815 * The pointer to the struct device will be returned from the call.
1816 * Any further sysfs files that might be required can be created using this
1817 * pointer.
1819 * Returns &struct device pointer on success, or ERR_PTR() on error.
1821 * Note: the struct class passed to this function must have previously
1822 * been created with a call to class_create().
1824 struct device *device_create_vargs(struct class *class, struct device *parent,
1825 dev_t devt, void *drvdata, const char *fmt,
1826 va_list args)
1828 return device_create_groups_vargs(class, parent, devt, drvdata, NULL,
1829 fmt, args);
1831 EXPORT_SYMBOL_GPL(device_create_vargs);
1834 * device_create - creates a device and registers it with sysfs
1835 * @class: pointer to the struct class that this device should be registered to
1836 * @parent: pointer to the parent struct device of this new device, if any
1837 * @devt: the dev_t for the char device to be added
1838 * @drvdata: the data to be added to the device for callbacks
1839 * @fmt: string for the device's name
1841 * This function can be used by char device classes. A struct device
1842 * will be created in sysfs, registered to the specified class.
1844 * A "dev" file will be created, showing the dev_t for the device, if
1845 * the dev_t is not 0,0.
1846 * If a pointer to a parent struct device is passed in, the newly created
1847 * struct device will be a child of that device in sysfs.
1848 * The pointer to the struct device will be returned from the call.
1849 * Any further sysfs files that might be required can be created using this
1850 * pointer.
1852 * Returns &struct device pointer on success, or ERR_PTR() on error.
1854 * Note: the struct class passed to this function must have previously
1855 * been created with a call to class_create().
1857 struct device *device_create(struct class *class, struct device *parent,
1858 dev_t devt, void *drvdata, const char *fmt, ...)
1860 va_list vargs;
1861 struct device *dev;
1863 va_start(vargs, fmt);
1864 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
1865 va_end(vargs);
1866 return dev;
1868 EXPORT_SYMBOL_GPL(device_create);
1871 * device_create_with_groups - creates a device and registers it with sysfs
1872 * @class: pointer to the struct class that this device should be registered to
1873 * @parent: pointer to the parent struct device of this new device, if any
1874 * @devt: the dev_t for the char device to be added
1875 * @drvdata: the data to be added to the device for callbacks
1876 * @groups: NULL-terminated list of attribute groups to be created
1877 * @fmt: string for the device's name
1879 * This function can be used by char device classes. A struct device
1880 * will be created in sysfs, registered to the specified class.
1881 * Additional attributes specified in the groups parameter will also
1882 * be created automatically.
1884 * A "dev" file will be created, showing the dev_t for the device, if
1885 * the dev_t is not 0,0.
1886 * If a pointer to a parent struct device is passed in, the newly created
1887 * struct device will be a child of that device in sysfs.
1888 * The pointer to the struct device will be returned from the call.
1889 * Any further sysfs files that might be required can be created using this
1890 * pointer.
1892 * Returns &struct device pointer on success, or ERR_PTR() on error.
1894 * Note: the struct class passed to this function must have previously
1895 * been created with a call to class_create().
1897 struct device *device_create_with_groups(struct class *class,
1898 struct device *parent, dev_t devt,
1899 void *drvdata,
1900 const struct attribute_group **groups,
1901 const char *fmt, ...)
1903 va_list vargs;
1904 struct device *dev;
1906 va_start(vargs, fmt);
1907 dev = device_create_groups_vargs(class, parent, devt, drvdata, groups,
1908 fmt, vargs);
1909 va_end(vargs);
1910 return dev;
1912 EXPORT_SYMBOL_GPL(device_create_with_groups);
1914 static int __match_devt(struct device *dev, const void *data)
1916 const dev_t *devt = data;
1918 return dev->devt == *devt;
1922 * device_destroy - removes a device that was created with device_create()
1923 * @class: pointer to the struct class that this device was registered with
1924 * @devt: the dev_t of the device that was previously registered
1926 * This call unregisters and cleans up a device that was created with a
1927 * call to device_create().
1929 void device_destroy(struct class *class, dev_t devt)
1931 struct device *dev;
1933 dev = class_find_device(class, NULL, &devt, __match_devt);
1934 if (dev) {
1935 put_device(dev);
1936 device_unregister(dev);
1939 EXPORT_SYMBOL_GPL(device_destroy);
1942 * device_rename - renames a device
1943 * @dev: the pointer to the struct device to be renamed
1944 * @new_name: the new name of the device
1946 * It is the responsibility of the caller to provide mutual
1947 * exclusion between two different calls of device_rename
1948 * on the same device to ensure that new_name is valid and
1949 * won't conflict with other devices.
1951 * Note: Don't call this function. Currently, the networking layer calls this
1952 * function, but that will change. The following text from Kay Sievers offers
1953 * some insight:
1955 * Renaming devices is racy at many levels, symlinks and other stuff are not
1956 * replaced atomically, and you get a "move" uevent, but it's not easy to
1957 * connect the event to the old and new device. Device nodes are not renamed at
1958 * all, there isn't even support for that in the kernel now.
1960 * In the meantime, during renaming, your target name might be taken by another
1961 * driver, creating conflicts. Or the old name is taken directly after you
1962 * renamed it -- then you get events for the same DEVPATH, before you even see
1963 * the "move" event. It's just a mess, and nothing new should ever rely on
1964 * kernel device renaming. Besides that, it's not even implemented now for
1965 * other things than (driver-core wise very simple) network devices.
1967 * We are currently about to change network renaming in udev to completely
1968 * disallow renaming of devices in the same namespace as the kernel uses,
1969 * because we can't solve the problems properly, that arise with swapping names
1970 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
1971 * be allowed to some other name than eth[0-9]*, for the aforementioned
1972 * reasons.
1974 * Make up a "real" name in the driver before you register anything, or add
1975 * some other attributes for userspace to find the device, or use udev to add
1976 * symlinks -- but never rename kernel devices later, it's a complete mess. We
1977 * don't even want to get into that and try to implement the missing pieces in
1978 * the core. We really have other pieces to fix in the driver core mess. :)
1980 int device_rename(struct device *dev, const char *new_name)
1982 struct kobject *kobj = &dev->kobj;
1983 char *old_device_name = NULL;
1984 int error;
1986 dev = get_device(dev);
1987 if (!dev)
1988 return -EINVAL;
1990 dev_dbg(dev, "renaming to %s\n", new_name);
1992 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
1993 if (!old_device_name) {
1994 error = -ENOMEM;
1995 goto out;
1998 if (dev->class) {
1999 error = sysfs_rename_link_ns(&dev->class->p->subsys.kobj,
2000 kobj, old_device_name,
2001 new_name, kobject_namespace(kobj));
2002 if (error)
2003 goto out;
2006 error = kobject_rename(kobj, new_name);
2007 if (error)
2008 goto out;
2010 out:
2011 put_device(dev);
2013 kfree(old_device_name);
2015 return error;
2017 EXPORT_SYMBOL_GPL(device_rename);
2019 static int device_move_class_links(struct device *dev,
2020 struct device *old_parent,
2021 struct device *new_parent)
2023 int error = 0;
2025 if (old_parent)
2026 sysfs_remove_link(&dev->kobj, "device");
2027 if (new_parent)
2028 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
2029 "device");
2030 return error;
2034 * device_move - moves a device to a new parent
2035 * @dev: the pointer to the struct device to be moved
2036 * @new_parent: the new parent of the device (can by NULL)
2037 * @dpm_order: how to reorder the dpm_list
2039 int device_move(struct device *dev, struct device *new_parent,
2040 enum dpm_order dpm_order)
2042 int error;
2043 struct device *old_parent;
2044 struct kobject *new_parent_kobj;
2046 dev = get_device(dev);
2047 if (!dev)
2048 return -EINVAL;
2050 device_pm_lock();
2051 new_parent = get_device(new_parent);
2052 new_parent_kobj = get_device_parent(dev, new_parent);
2053 if (IS_ERR(new_parent_kobj)) {
2054 error = PTR_ERR(new_parent_kobj);
2055 put_device(new_parent);
2056 goto out;
2059 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
2060 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
2061 error = kobject_move(&dev->kobj, new_parent_kobj);
2062 if (error) {
2063 cleanup_glue_dir(dev, new_parent_kobj);
2064 put_device(new_parent);
2065 goto out;
2067 old_parent = dev->parent;
2068 dev->parent = new_parent;
2069 if (old_parent)
2070 klist_remove(&dev->p->knode_parent);
2071 if (new_parent) {
2072 klist_add_tail(&dev->p->knode_parent,
2073 &new_parent->p->klist_children);
2074 set_dev_node(dev, dev_to_node(new_parent));
2077 if (dev->class) {
2078 error = device_move_class_links(dev, old_parent, new_parent);
2079 if (error) {
2080 /* We ignore errors on cleanup since we're hosed anyway... */
2081 device_move_class_links(dev, new_parent, old_parent);
2082 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
2083 if (new_parent)
2084 klist_remove(&dev->p->knode_parent);
2085 dev->parent = old_parent;
2086 if (old_parent) {
2087 klist_add_tail(&dev->p->knode_parent,
2088 &old_parent->p->klist_children);
2089 set_dev_node(dev, dev_to_node(old_parent));
2092 cleanup_glue_dir(dev, new_parent_kobj);
2093 put_device(new_parent);
2094 goto out;
2097 switch (dpm_order) {
2098 case DPM_ORDER_NONE:
2099 break;
2100 case DPM_ORDER_DEV_AFTER_PARENT:
2101 device_pm_move_after(dev, new_parent);
2102 devices_kset_move_after(dev, new_parent);
2103 break;
2104 case DPM_ORDER_PARENT_BEFORE_DEV:
2105 device_pm_move_before(new_parent, dev);
2106 devices_kset_move_before(new_parent, dev);
2107 break;
2108 case DPM_ORDER_DEV_LAST:
2109 device_pm_move_last(dev);
2110 devices_kset_move_last(dev);
2111 break;
2114 put_device(old_parent);
2115 out:
2116 device_pm_unlock();
2117 put_device(dev);
2118 return error;
2120 EXPORT_SYMBOL_GPL(device_move);
2123 * device_shutdown - call ->shutdown() on each device to shutdown.
2125 void device_shutdown(void)
2127 struct device *dev, *parent;
2129 wait_for_device_probe();
2130 device_block_probing();
2132 cpufreq_suspend();
2134 spin_lock(&devices_kset->list_lock);
2136 * Walk the devices list backward, shutting down each in turn.
2137 * Beware that device unplug events may also start pulling
2138 * devices offline, even as the system is shutting down.
2140 while (!list_empty(&devices_kset->list)) {
2141 dev = list_entry(devices_kset->list.prev, struct device,
2142 kobj.entry);
2145 * hold reference count of device's parent to
2146 * prevent it from being freed because parent's
2147 * lock is to be held
2149 parent = get_device(dev->parent);
2150 get_device(dev);
2152 * Make sure the device is off the kset list, in the
2153 * event that dev->*->shutdown() doesn't remove it.
2155 list_del_init(&dev->kobj.entry);
2156 spin_unlock(&devices_kset->list_lock);
2158 /* hold lock to avoid race with probe/release */
2159 if (parent)
2160 device_lock(parent);
2161 device_lock(dev);
2163 /* Don't allow any more runtime suspends */
2164 pm_runtime_get_noresume(dev);
2165 pm_runtime_barrier(dev);
2167 if (dev->class && dev->class->shutdown) {
2168 if (initcall_debug)
2169 dev_info(dev, "shutdown\n");
2170 dev->class->shutdown(dev);
2171 } else if (dev->bus && dev->bus->shutdown) {
2172 if (initcall_debug)
2173 dev_info(dev, "shutdown\n");
2174 dev->bus->shutdown(dev);
2175 } else if (dev->driver && dev->driver->shutdown) {
2176 if (initcall_debug)
2177 dev_info(dev, "shutdown\n");
2178 dev->driver->shutdown(dev);
2181 device_unlock(dev);
2182 if (parent)
2183 device_unlock(parent);
2185 put_device(dev);
2186 put_device(parent);
2188 spin_lock(&devices_kset->list_lock);
2190 spin_unlock(&devices_kset->list_lock);
2194 * Device logging functions
2197 #ifdef CONFIG_PRINTK
2198 static int
2199 create_syslog_header(const struct device *dev, char *hdr, size_t hdrlen)
2201 const char *subsys;
2202 size_t pos = 0;
2204 if (dev->class)
2205 subsys = dev->class->name;
2206 else if (dev->bus)
2207 subsys = dev->bus->name;
2208 else
2209 return 0;
2211 pos += snprintf(hdr + pos, hdrlen - pos, "SUBSYSTEM=%s", subsys);
2212 if (pos >= hdrlen)
2213 goto overflow;
2216 * Add device identifier DEVICE=:
2217 * b12:8 block dev_t
2218 * c127:3 char dev_t
2219 * n8 netdev ifindex
2220 * +sound:card0 subsystem:devname
2222 if (MAJOR(dev->devt)) {
2223 char c;
2225 if (strcmp(subsys, "block") == 0)
2226 c = 'b';
2227 else
2228 c = 'c';
2229 pos++;
2230 pos += snprintf(hdr + pos, hdrlen - pos,
2231 "DEVICE=%c%u:%u",
2232 c, MAJOR(dev->devt), MINOR(dev->devt));
2233 } else if (strcmp(subsys, "net") == 0) {
2234 struct net_device *net = to_net_dev(dev);
2236 pos++;
2237 pos += snprintf(hdr + pos, hdrlen - pos,
2238 "DEVICE=n%u", net->ifindex);
2239 } else {
2240 pos++;
2241 pos += snprintf(hdr + pos, hdrlen - pos,
2242 "DEVICE=+%s:%s", subsys, dev_name(dev));
2245 if (pos >= hdrlen)
2246 goto overflow;
2248 return pos;
2250 overflow:
2251 dev_WARN(dev, "device/subsystem name too long");
2252 return 0;
2255 int dev_vprintk_emit(int level, const struct device *dev,
2256 const char *fmt, va_list args)
2258 char hdr[128];
2259 size_t hdrlen;
2261 hdrlen = create_syslog_header(dev, hdr, sizeof(hdr));
2263 return vprintk_emit(0, level, hdrlen ? hdr : NULL, hdrlen, fmt, args);
2265 EXPORT_SYMBOL(dev_vprintk_emit);
2267 int dev_printk_emit(int level, const struct device *dev, const char *fmt, ...)
2269 va_list args;
2270 int r;
2272 va_start(args, fmt);
2274 r = dev_vprintk_emit(level, dev, fmt, args);
2276 va_end(args);
2278 return r;
2280 EXPORT_SYMBOL(dev_printk_emit);
2282 static void __dev_printk(const char *level, const struct device *dev,
2283 struct va_format *vaf)
2285 if (dev)
2286 dev_printk_emit(level[1] - '0', dev, "%s %s: %pV",
2287 dev_driver_string(dev), dev_name(dev), vaf);
2288 else
2289 printk("%s(NULL device *): %pV", level, vaf);
2292 void dev_printk(const char *level, const struct device *dev,
2293 const char *fmt, ...)
2295 struct va_format vaf;
2296 va_list args;
2298 va_start(args, fmt);
2300 vaf.fmt = fmt;
2301 vaf.va = &args;
2303 __dev_printk(level, dev, &vaf);
2305 va_end(args);
2307 EXPORT_SYMBOL(dev_printk);
2309 #define define_dev_printk_level(func, kern_level) \
2310 void func(const struct device *dev, const char *fmt, ...) \
2312 struct va_format vaf; \
2313 va_list args; \
2315 va_start(args, fmt); \
2317 vaf.fmt = fmt; \
2318 vaf.va = &args; \
2320 __dev_printk(kern_level, dev, &vaf); \
2322 va_end(args); \
2324 EXPORT_SYMBOL(func);
2326 define_dev_printk_level(dev_emerg, KERN_EMERG);
2327 define_dev_printk_level(dev_alert, KERN_ALERT);
2328 define_dev_printk_level(dev_crit, KERN_CRIT);
2329 define_dev_printk_level(dev_err, KERN_ERR);
2330 define_dev_printk_level(dev_warn, KERN_WARNING);
2331 define_dev_printk_level(dev_notice, KERN_NOTICE);
2332 define_dev_printk_level(_dev_info, KERN_INFO);
2334 #endif
2336 static inline bool fwnode_is_primary(struct fwnode_handle *fwnode)
2338 return fwnode && !IS_ERR(fwnode->secondary);
2342 * set_primary_fwnode - Change the primary firmware node of a given device.
2343 * @dev: Device to handle.
2344 * @fwnode: New primary firmware node of the device.
2346 * Set the device's firmware node pointer to @fwnode, but if a secondary
2347 * firmware node of the device is present, preserve it.
2349 void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
2351 if (fwnode) {
2352 struct fwnode_handle *fn = dev->fwnode;
2354 if (fwnode_is_primary(fn))
2355 fn = fn->secondary;
2357 if (fn) {
2358 WARN_ON(fwnode->secondary);
2359 fwnode->secondary = fn;
2361 dev->fwnode = fwnode;
2362 } else {
2363 dev->fwnode = fwnode_is_primary(dev->fwnode) ?
2364 dev->fwnode->secondary : NULL;
2367 EXPORT_SYMBOL_GPL(set_primary_fwnode);
2370 * set_secondary_fwnode - Change the secondary firmware node of a given device.
2371 * @dev: Device to handle.
2372 * @fwnode: New secondary firmware node of the device.
2374 * If a primary firmware node of the device is present, set its secondary
2375 * pointer to @fwnode. Otherwise, set the device's firmware node pointer to
2376 * @fwnode.
2378 void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode)
2380 if (fwnode)
2381 fwnode->secondary = ERR_PTR(-ENODEV);
2383 if (fwnode_is_primary(dev->fwnode))
2384 dev->fwnode->secondary = fwnode;
2385 else
2386 dev->fwnode = fwnode;