driver core: free devres in device_release
[linux-2.6/btrfs-unstable.git] / drivers / base / core.c
blobc8fe4a5638661e1e61d89ea183c320cd21a4c57f
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/device.h>
14 #include <linux/err.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <linux/kdev_t.h>
20 #include <linux/notifier.h>
21 #include <linux/of.h>
22 #include <linux/of_device.h>
23 #include <linux/genhd.h>
24 #include <linux/kallsyms.h>
25 #include <linux/mutex.h>
26 #include <linux/async.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/netdevice.h>
30 #include "base.h"
31 #include "power/power.h"
33 #ifdef CONFIG_SYSFS_DEPRECATED
34 #ifdef CONFIG_SYSFS_DEPRECATED_V2
35 long sysfs_deprecated = 1;
36 #else
37 long sysfs_deprecated = 0;
38 #endif
39 static __init int sysfs_deprecated_setup(char *arg)
41 return strict_strtol(arg, 10, &sysfs_deprecated);
43 early_param("sysfs.deprecated", sysfs_deprecated_setup);
44 #endif
46 int (*platform_notify)(struct device *dev) = NULL;
47 int (*platform_notify_remove)(struct device *dev) = NULL;
48 static struct kobject *dev_kobj;
49 struct kobject *sysfs_dev_char_kobj;
50 struct kobject *sysfs_dev_block_kobj;
52 #ifdef CONFIG_BLOCK
53 static inline int device_is_not_partition(struct device *dev)
55 return !(dev->type == &part_type);
57 #else
58 static inline int device_is_not_partition(struct device *dev)
60 return 1;
62 #endif
64 /**
65 * dev_driver_string - Return a device's driver name, if at all possible
66 * @dev: struct device to get the name of
68 * Will return the device's driver's name if it is bound to a device. If
69 * the device is not bound to a driver, it will return the name of the bus
70 * it is attached to. If it is not attached to a bus either, an empty
71 * string will be returned.
73 const char *dev_driver_string(const struct device *dev)
75 struct device_driver *drv;
77 /* dev->driver can change to NULL underneath us because of unbinding,
78 * so be careful about accessing it. dev->bus and dev->class should
79 * never change once they are set, so they don't need special care.
81 drv = ACCESS_ONCE(dev->driver);
82 return drv ? drv->name :
83 (dev->bus ? dev->bus->name :
84 (dev->class ? dev->class->name : ""));
86 EXPORT_SYMBOL(dev_driver_string);
88 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
90 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
91 char *buf)
93 struct device_attribute *dev_attr = to_dev_attr(attr);
94 struct device *dev = kobj_to_dev(kobj);
95 ssize_t ret = -EIO;
97 if (dev_attr->show)
98 ret = dev_attr->show(dev, dev_attr, buf);
99 if (ret >= (ssize_t)PAGE_SIZE) {
100 print_symbol("dev_attr_show: %s returned bad count\n",
101 (unsigned long)dev_attr->show);
103 return ret;
106 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
107 const char *buf, size_t count)
109 struct device_attribute *dev_attr = to_dev_attr(attr);
110 struct device *dev = kobj_to_dev(kobj);
111 ssize_t ret = -EIO;
113 if (dev_attr->store)
114 ret = dev_attr->store(dev, dev_attr, buf, count);
115 return ret;
118 static const struct sysfs_ops dev_sysfs_ops = {
119 .show = dev_attr_show,
120 .store = dev_attr_store,
123 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
125 ssize_t device_store_ulong(struct device *dev,
126 struct device_attribute *attr,
127 const char *buf, size_t size)
129 struct dev_ext_attribute *ea = to_ext_attr(attr);
130 char *end;
131 unsigned long new = simple_strtoul(buf, &end, 0);
132 if (end == buf)
133 return -EINVAL;
134 *(unsigned long *)(ea->var) = new;
135 /* Always return full write size even if we didn't consume all */
136 return size;
138 EXPORT_SYMBOL_GPL(device_store_ulong);
140 ssize_t device_show_ulong(struct device *dev,
141 struct device_attribute *attr,
142 char *buf)
144 struct dev_ext_attribute *ea = to_ext_attr(attr);
145 return snprintf(buf, PAGE_SIZE, "%lx\n", *(unsigned long *)(ea->var));
147 EXPORT_SYMBOL_GPL(device_show_ulong);
149 ssize_t device_store_int(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 long new = simple_strtol(buf, &end, 0);
156 if (end == buf || new > INT_MAX || new < INT_MIN)
157 return -EINVAL;
158 *(int *)(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_int);
164 ssize_t device_show_int(struct device *dev,
165 struct device_attribute *attr,
166 char *buf)
168 struct dev_ext_attribute *ea = to_ext_attr(attr);
170 return snprintf(buf, PAGE_SIZE, "%d\n", *(int *)(ea->var));
172 EXPORT_SYMBOL_GPL(device_show_int);
175 * device_release - free device structure.
176 * @kobj: device's kobject.
178 * This is called once the reference count for the object
179 * reaches 0. We forward the call to the device's release
180 * method, which should handle actually freeing the structure.
182 static void device_release(struct kobject *kobj)
184 struct device *dev = kobj_to_dev(kobj);
185 struct device_private *p = dev->p;
188 * Some platform devices are driven without driver attached
189 * and managed resources may have been acquired. Make sure
190 * all resources are released.
192 * Drivers still can add resources into device after device
193 * is deleted but alive, so release devres here to avoid
194 * possible memory leak.
196 devres_release_all(dev);
198 if (dev->release)
199 dev->release(dev);
200 else if (dev->type && dev->type->release)
201 dev->type->release(dev);
202 else if (dev->class && dev->class->dev_release)
203 dev->class->dev_release(dev);
204 else
205 WARN(1, KERN_ERR "Device '%s' does not have a release() "
206 "function, it is broken and must be fixed.\n",
207 dev_name(dev));
208 kfree(p);
211 static const void *device_namespace(struct kobject *kobj)
213 struct device *dev = kobj_to_dev(kobj);
214 const void *ns = NULL;
216 if (dev->class && dev->class->ns_type)
217 ns = dev->class->namespace(dev);
219 return ns;
222 static struct kobj_type device_ktype = {
223 .release = device_release,
224 .sysfs_ops = &dev_sysfs_ops,
225 .namespace = device_namespace,
229 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
231 struct kobj_type *ktype = get_ktype(kobj);
233 if (ktype == &device_ktype) {
234 struct device *dev = kobj_to_dev(kobj);
235 if (dev->bus)
236 return 1;
237 if (dev->class)
238 return 1;
240 return 0;
243 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
245 struct device *dev = kobj_to_dev(kobj);
247 if (dev->bus)
248 return dev->bus->name;
249 if (dev->class)
250 return dev->class->name;
251 return NULL;
254 static int dev_uevent(struct kset *kset, struct kobject *kobj,
255 struct kobj_uevent_env *env)
257 struct device *dev = kobj_to_dev(kobj);
258 int retval = 0;
260 /* add device node properties if present */
261 if (MAJOR(dev->devt)) {
262 const char *tmp;
263 const char *name;
264 umode_t mode = 0;
266 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
267 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
268 name = device_get_devnode(dev, &mode, &tmp);
269 if (name) {
270 add_uevent_var(env, "DEVNAME=%s", name);
271 kfree(tmp);
272 if (mode)
273 add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
277 if (dev->type && dev->type->name)
278 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
280 if (dev->driver)
281 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
283 /* Add common DT information about the device */
284 of_device_uevent(dev, env);
286 /* have the bus specific function add its stuff */
287 if (dev->bus && dev->bus->uevent) {
288 retval = dev->bus->uevent(dev, env);
289 if (retval)
290 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
291 dev_name(dev), __func__, retval);
294 /* have the class specific function add its stuff */
295 if (dev->class && dev->class->dev_uevent) {
296 retval = dev->class->dev_uevent(dev, env);
297 if (retval)
298 pr_debug("device: '%s': %s: class uevent() "
299 "returned %d\n", dev_name(dev),
300 __func__, retval);
303 /* have the device type specific function add its stuff */
304 if (dev->type && dev->type->uevent) {
305 retval = dev->type->uevent(dev, env);
306 if (retval)
307 pr_debug("device: '%s': %s: dev_type uevent() "
308 "returned %d\n", dev_name(dev),
309 __func__, retval);
312 return retval;
315 static const struct kset_uevent_ops device_uevent_ops = {
316 .filter = dev_uevent_filter,
317 .name = dev_uevent_name,
318 .uevent = dev_uevent,
321 static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
322 char *buf)
324 struct kobject *top_kobj;
325 struct kset *kset;
326 struct kobj_uevent_env *env = NULL;
327 int i;
328 size_t count = 0;
329 int retval;
331 /* search the kset, the device belongs to */
332 top_kobj = &dev->kobj;
333 while (!top_kobj->kset && top_kobj->parent)
334 top_kobj = top_kobj->parent;
335 if (!top_kobj->kset)
336 goto out;
338 kset = top_kobj->kset;
339 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
340 goto out;
342 /* respect filter */
343 if (kset->uevent_ops && kset->uevent_ops->filter)
344 if (!kset->uevent_ops->filter(kset, &dev->kobj))
345 goto out;
347 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
348 if (!env)
349 return -ENOMEM;
351 /* let the kset specific function add its keys */
352 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
353 if (retval)
354 goto out;
356 /* copy keys to file */
357 for (i = 0; i < env->envp_idx; i++)
358 count += sprintf(&buf[count], "%s\n", env->envp[i]);
359 out:
360 kfree(env);
361 return count;
364 static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
365 const char *buf, size_t count)
367 enum kobject_action action;
369 if (kobject_action_type(buf, count, &action) == 0)
370 kobject_uevent(&dev->kobj, action);
371 else
372 dev_err(dev, "uevent: unknown action-string\n");
373 return count;
376 static struct device_attribute uevent_attr =
377 __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
379 static int device_add_attributes(struct device *dev,
380 struct device_attribute *attrs)
382 int error = 0;
383 int i;
385 if (attrs) {
386 for (i = 0; attr_name(attrs[i]); i++) {
387 error = device_create_file(dev, &attrs[i]);
388 if (error)
389 break;
391 if (error)
392 while (--i >= 0)
393 device_remove_file(dev, &attrs[i]);
395 return error;
398 static void device_remove_attributes(struct device *dev,
399 struct device_attribute *attrs)
401 int i;
403 if (attrs)
404 for (i = 0; attr_name(attrs[i]); i++)
405 device_remove_file(dev, &attrs[i]);
408 static int device_add_bin_attributes(struct device *dev,
409 struct bin_attribute *attrs)
411 int error = 0;
412 int i;
414 if (attrs) {
415 for (i = 0; attr_name(attrs[i]); i++) {
416 error = device_create_bin_file(dev, &attrs[i]);
417 if (error)
418 break;
420 if (error)
421 while (--i >= 0)
422 device_remove_bin_file(dev, &attrs[i]);
424 return error;
427 static void device_remove_bin_attributes(struct device *dev,
428 struct bin_attribute *attrs)
430 int i;
432 if (attrs)
433 for (i = 0; attr_name(attrs[i]); i++)
434 device_remove_bin_file(dev, &attrs[i]);
437 static int device_add_groups(struct device *dev,
438 const struct attribute_group **groups)
440 int error = 0;
441 int i;
443 if (groups) {
444 for (i = 0; groups[i]; i++) {
445 error = sysfs_create_group(&dev->kobj, groups[i]);
446 if (error) {
447 while (--i >= 0)
448 sysfs_remove_group(&dev->kobj,
449 groups[i]);
450 break;
454 return error;
457 static void device_remove_groups(struct device *dev,
458 const struct attribute_group **groups)
460 int i;
462 if (groups)
463 for (i = 0; groups[i]; i++)
464 sysfs_remove_group(&dev->kobj, groups[i]);
467 static int device_add_attrs(struct device *dev)
469 struct class *class = dev->class;
470 const struct device_type *type = dev->type;
471 int error;
473 if (class) {
474 error = device_add_attributes(dev, class->dev_attrs);
475 if (error)
476 return error;
477 error = device_add_bin_attributes(dev, class->dev_bin_attrs);
478 if (error)
479 goto err_remove_class_attrs;
482 if (type) {
483 error = device_add_groups(dev, type->groups);
484 if (error)
485 goto err_remove_class_bin_attrs;
488 error = device_add_groups(dev, dev->groups);
489 if (error)
490 goto err_remove_type_groups;
492 return 0;
494 err_remove_type_groups:
495 if (type)
496 device_remove_groups(dev, type->groups);
497 err_remove_class_bin_attrs:
498 if (class)
499 device_remove_bin_attributes(dev, class->dev_bin_attrs);
500 err_remove_class_attrs:
501 if (class)
502 device_remove_attributes(dev, class->dev_attrs);
504 return error;
507 static void device_remove_attrs(struct device *dev)
509 struct class *class = dev->class;
510 const struct device_type *type = dev->type;
512 device_remove_groups(dev, dev->groups);
514 if (type)
515 device_remove_groups(dev, type->groups);
517 if (class) {
518 device_remove_attributes(dev, class->dev_attrs);
519 device_remove_bin_attributes(dev, class->dev_bin_attrs);
524 static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
525 char *buf)
527 return print_dev_t(buf, dev->devt);
530 static struct device_attribute devt_attr =
531 __ATTR(dev, S_IRUGO, show_dev, NULL);
533 /* /sys/devices/ */
534 struct kset *devices_kset;
537 * device_create_file - create sysfs attribute file for device.
538 * @dev: device.
539 * @attr: device attribute descriptor.
541 int device_create_file(struct device *dev,
542 const struct device_attribute *attr)
544 int error = 0;
545 if (dev)
546 error = sysfs_create_file(&dev->kobj, &attr->attr);
547 return error;
551 * device_remove_file - remove sysfs attribute file.
552 * @dev: device.
553 * @attr: device attribute descriptor.
555 void device_remove_file(struct device *dev,
556 const struct device_attribute *attr)
558 if (dev)
559 sysfs_remove_file(&dev->kobj, &attr->attr);
563 * device_create_bin_file - create sysfs binary attribute file for device.
564 * @dev: device.
565 * @attr: device binary attribute descriptor.
567 int device_create_bin_file(struct device *dev,
568 const struct bin_attribute *attr)
570 int error = -EINVAL;
571 if (dev)
572 error = sysfs_create_bin_file(&dev->kobj, attr);
573 return error;
575 EXPORT_SYMBOL_GPL(device_create_bin_file);
578 * device_remove_bin_file - remove sysfs binary attribute file
579 * @dev: device.
580 * @attr: device binary attribute descriptor.
582 void device_remove_bin_file(struct device *dev,
583 const struct bin_attribute *attr)
585 if (dev)
586 sysfs_remove_bin_file(&dev->kobj, attr);
588 EXPORT_SYMBOL_GPL(device_remove_bin_file);
591 * device_schedule_callback_owner - helper to schedule a callback for a device
592 * @dev: device.
593 * @func: callback function to invoke later.
594 * @owner: module owning the callback routine
596 * Attribute methods must not unregister themselves or their parent device
597 * (which would amount to the same thing). Attempts to do so will deadlock,
598 * since unregistration is mutually exclusive with driver callbacks.
600 * Instead methods can call this routine, which will attempt to allocate
601 * and schedule a workqueue request to call back @func with @dev as its
602 * argument in the workqueue's process context. @dev will be pinned until
603 * @func returns.
605 * This routine is usually called via the inline device_schedule_callback(),
606 * which automatically sets @owner to THIS_MODULE.
608 * Returns 0 if the request was submitted, -ENOMEM if storage could not
609 * be allocated, -ENODEV if a reference to @owner isn't available.
611 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
612 * underlying sysfs routine (since it is intended for use by attribute
613 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
615 int device_schedule_callback_owner(struct device *dev,
616 void (*func)(struct device *), struct module *owner)
618 return sysfs_schedule_callback(&dev->kobj,
619 (void (*)(void *)) func, dev, owner);
621 EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
623 static void klist_children_get(struct klist_node *n)
625 struct device_private *p = to_device_private_parent(n);
626 struct device *dev = p->device;
628 get_device(dev);
631 static void klist_children_put(struct klist_node *n)
633 struct device_private *p = to_device_private_parent(n);
634 struct device *dev = p->device;
636 put_device(dev);
640 * device_initialize - init device structure.
641 * @dev: device.
643 * This prepares the device for use by other layers by initializing
644 * its fields.
645 * It is the first half of device_register(), if called by
646 * that function, though it can also be called separately, so one
647 * may use @dev's fields. In particular, get_device()/put_device()
648 * may be used for reference counting of @dev after calling this
649 * function.
651 * All fields in @dev must be initialized by the caller to 0, except
652 * for those explicitly set to some other value. The simplest
653 * approach is to use kzalloc() to allocate the structure containing
654 * @dev.
656 * NOTE: Use put_device() to give up your reference instead of freeing
657 * @dev directly once you have called this function.
659 void device_initialize(struct device *dev)
661 dev->kobj.kset = devices_kset;
662 kobject_init(&dev->kobj, &device_ktype);
663 INIT_LIST_HEAD(&dev->dma_pools);
664 mutex_init(&dev->mutex);
665 lockdep_set_novalidate_class(&dev->mutex);
666 spin_lock_init(&dev->devres_lock);
667 INIT_LIST_HEAD(&dev->devres_head);
668 device_pm_init(dev);
669 set_dev_node(dev, -1);
672 static struct kobject *virtual_device_parent(struct device *dev)
674 static struct kobject *virtual_dir = NULL;
676 if (!virtual_dir)
677 virtual_dir = kobject_create_and_add("virtual",
678 &devices_kset->kobj);
680 return virtual_dir;
683 struct class_dir {
684 struct kobject kobj;
685 struct class *class;
688 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
690 static void class_dir_release(struct kobject *kobj)
692 struct class_dir *dir = to_class_dir(kobj);
693 kfree(dir);
696 static const
697 struct kobj_ns_type_operations *class_dir_child_ns_type(struct kobject *kobj)
699 struct class_dir *dir = to_class_dir(kobj);
700 return dir->class->ns_type;
703 static struct kobj_type class_dir_ktype = {
704 .release = class_dir_release,
705 .sysfs_ops = &kobj_sysfs_ops,
706 .child_ns_type = class_dir_child_ns_type
709 static struct kobject *
710 class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
712 struct class_dir *dir;
713 int retval;
715 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
716 if (!dir)
717 return NULL;
719 dir->class = class;
720 kobject_init(&dir->kobj, &class_dir_ktype);
722 dir->kobj.kset = &class->p->glue_dirs;
724 retval = kobject_add(&dir->kobj, parent_kobj, "%s", class->name);
725 if (retval < 0) {
726 kobject_put(&dir->kobj);
727 return NULL;
729 return &dir->kobj;
733 static struct kobject *get_device_parent(struct device *dev,
734 struct device *parent)
736 if (dev->class) {
737 static DEFINE_MUTEX(gdp_mutex);
738 struct kobject *kobj = NULL;
739 struct kobject *parent_kobj;
740 struct kobject *k;
742 #ifdef CONFIG_BLOCK
743 /* block disks show up in /sys/block */
744 if (sysfs_deprecated && dev->class == &block_class) {
745 if (parent && parent->class == &block_class)
746 return &parent->kobj;
747 return &block_class.p->subsys.kobj;
749 #endif
752 * If we have no parent, we live in "virtual".
753 * Class-devices with a non class-device as parent, live
754 * in a "glue" directory to prevent namespace collisions.
756 if (parent == NULL)
757 parent_kobj = virtual_device_parent(dev);
758 else if (parent->class && !dev->class->ns_type)
759 return &parent->kobj;
760 else
761 parent_kobj = &parent->kobj;
763 mutex_lock(&gdp_mutex);
765 /* find our class-directory at the parent and reference it */
766 spin_lock(&dev->class->p->glue_dirs.list_lock);
767 list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
768 if (k->parent == parent_kobj) {
769 kobj = kobject_get(k);
770 break;
772 spin_unlock(&dev->class->p->glue_dirs.list_lock);
773 if (kobj) {
774 mutex_unlock(&gdp_mutex);
775 return kobj;
778 /* or create a new class-directory at the parent device */
779 k = class_dir_create_and_add(dev->class, parent_kobj);
780 /* do not emit an uevent for this simple "glue" directory */
781 mutex_unlock(&gdp_mutex);
782 return k;
785 /* subsystems can specify a default root directory for their devices */
786 if (!parent && dev->bus && dev->bus->dev_root)
787 return &dev->bus->dev_root->kobj;
789 if (parent)
790 return &parent->kobj;
791 return NULL;
794 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
796 /* see if we live in a "glue" directory */
797 if (!glue_dir || !dev->class ||
798 glue_dir->kset != &dev->class->p->glue_dirs)
799 return;
801 kobject_put(glue_dir);
804 static void cleanup_device_parent(struct device *dev)
806 cleanup_glue_dir(dev, dev->kobj.parent);
809 static int device_add_class_symlinks(struct device *dev)
811 int error;
813 if (!dev->class)
814 return 0;
816 error = sysfs_create_link(&dev->kobj,
817 &dev->class->p->subsys.kobj,
818 "subsystem");
819 if (error)
820 goto out;
822 if (dev->parent && device_is_not_partition(dev)) {
823 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
824 "device");
825 if (error)
826 goto out_subsys;
829 #ifdef CONFIG_BLOCK
830 /* /sys/block has directories and does not need symlinks */
831 if (sysfs_deprecated && dev->class == &block_class)
832 return 0;
833 #endif
835 /* link in the class directory pointing to the device */
836 error = sysfs_create_link(&dev->class->p->subsys.kobj,
837 &dev->kobj, dev_name(dev));
838 if (error)
839 goto out_device;
841 return 0;
843 out_device:
844 sysfs_remove_link(&dev->kobj, "device");
846 out_subsys:
847 sysfs_remove_link(&dev->kobj, "subsystem");
848 out:
849 return error;
852 static void device_remove_class_symlinks(struct device *dev)
854 if (!dev->class)
855 return;
857 if (dev->parent && device_is_not_partition(dev))
858 sysfs_remove_link(&dev->kobj, "device");
859 sysfs_remove_link(&dev->kobj, "subsystem");
860 #ifdef CONFIG_BLOCK
861 if (sysfs_deprecated && dev->class == &block_class)
862 return;
863 #endif
864 sysfs_delete_link(&dev->class->p->subsys.kobj, &dev->kobj, dev_name(dev));
868 * dev_set_name - set a device name
869 * @dev: device
870 * @fmt: format string for the device's name
872 int dev_set_name(struct device *dev, const char *fmt, ...)
874 va_list vargs;
875 int err;
877 va_start(vargs, fmt);
878 err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
879 va_end(vargs);
880 return err;
882 EXPORT_SYMBOL_GPL(dev_set_name);
885 * device_to_dev_kobj - select a /sys/dev/ directory for the device
886 * @dev: device
888 * By default we select char/ for new entries. Setting class->dev_obj
889 * to NULL prevents an entry from being created. class->dev_kobj must
890 * be set (or cleared) before any devices are registered to the class
891 * otherwise device_create_sys_dev_entry() and
892 * device_remove_sys_dev_entry() will disagree about the presence of
893 * the link.
895 static struct kobject *device_to_dev_kobj(struct device *dev)
897 struct kobject *kobj;
899 if (dev->class)
900 kobj = dev->class->dev_kobj;
901 else
902 kobj = sysfs_dev_char_kobj;
904 return kobj;
907 static int device_create_sys_dev_entry(struct device *dev)
909 struct kobject *kobj = device_to_dev_kobj(dev);
910 int error = 0;
911 char devt_str[15];
913 if (kobj) {
914 format_dev_t(devt_str, dev->devt);
915 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
918 return error;
921 static void device_remove_sys_dev_entry(struct device *dev)
923 struct kobject *kobj = device_to_dev_kobj(dev);
924 char devt_str[15];
926 if (kobj) {
927 format_dev_t(devt_str, dev->devt);
928 sysfs_remove_link(kobj, devt_str);
932 int device_private_init(struct device *dev)
934 dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
935 if (!dev->p)
936 return -ENOMEM;
937 dev->p->device = dev;
938 klist_init(&dev->p->klist_children, klist_children_get,
939 klist_children_put);
940 INIT_LIST_HEAD(&dev->p->deferred_probe);
941 return 0;
945 * device_add - add device to device hierarchy.
946 * @dev: device.
948 * This is part 2 of device_register(), though may be called
949 * separately _iff_ device_initialize() has been called separately.
951 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
952 * to the global and sibling lists for the device, then
953 * adds it to the other relevant subsystems of the driver model.
955 * Do not call this routine or device_register() more than once for
956 * any device structure. The driver model core is not designed to work
957 * with devices that get unregistered and then spring back to life.
958 * (Among other things, it's very hard to guarantee that all references
959 * to the previous incarnation of @dev have been dropped.) Allocate
960 * and register a fresh new struct device instead.
962 * NOTE: _Never_ directly free @dev after calling this function, even
963 * if it returned an error! Always use put_device() to give up your
964 * reference instead.
966 int device_add(struct device *dev)
968 struct device *parent = NULL;
969 struct kobject *kobj;
970 struct class_interface *class_intf;
971 int error = -EINVAL;
973 dev = get_device(dev);
974 if (!dev)
975 goto done;
977 if (!dev->p) {
978 error = device_private_init(dev);
979 if (error)
980 goto done;
984 * for statically allocated devices, which should all be converted
985 * some day, we need to initialize the name. We prevent reading back
986 * the name, and force the use of dev_name()
988 if (dev->init_name) {
989 dev_set_name(dev, "%s", dev->init_name);
990 dev->init_name = NULL;
993 /* subsystems can specify simple device enumeration */
994 if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
995 dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
997 if (!dev_name(dev)) {
998 error = -EINVAL;
999 goto name_error;
1002 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1004 parent = get_device(dev->parent);
1005 kobj = get_device_parent(dev, parent);
1006 if (kobj)
1007 dev->kobj.parent = kobj;
1009 /* use parent numa_node */
1010 if (parent)
1011 set_dev_node(dev, dev_to_node(parent));
1013 /* first, register with generic layer. */
1014 /* we require the name to be set before, and pass NULL */
1015 error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
1016 if (error)
1017 goto Error;
1019 /* notify platform of device entry */
1020 if (platform_notify)
1021 platform_notify(dev);
1023 error = device_create_file(dev, &uevent_attr);
1024 if (error)
1025 goto attrError;
1027 if (MAJOR(dev->devt)) {
1028 error = device_create_file(dev, &devt_attr);
1029 if (error)
1030 goto ueventattrError;
1032 error = device_create_sys_dev_entry(dev);
1033 if (error)
1034 goto devtattrError;
1036 devtmpfs_create_node(dev);
1039 error = device_add_class_symlinks(dev);
1040 if (error)
1041 goto SymlinkError;
1042 error = device_add_attrs(dev);
1043 if (error)
1044 goto AttrsError;
1045 error = bus_add_device(dev);
1046 if (error)
1047 goto BusError;
1048 error = dpm_sysfs_add(dev);
1049 if (error)
1050 goto DPMError;
1051 device_pm_add(dev);
1053 /* Notify clients of device addition. This call must come
1054 * after dpm_sysfs_add() and before kobject_uevent().
1056 if (dev->bus)
1057 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1058 BUS_NOTIFY_ADD_DEVICE, dev);
1060 kobject_uevent(&dev->kobj, KOBJ_ADD);
1061 bus_probe_device(dev);
1062 if (parent)
1063 klist_add_tail(&dev->p->knode_parent,
1064 &parent->p->klist_children);
1066 if (dev->class) {
1067 mutex_lock(&dev->class->p->mutex);
1068 /* tie the class to the device */
1069 klist_add_tail(&dev->knode_class,
1070 &dev->class->p->klist_devices);
1072 /* notify any interfaces that the device is here */
1073 list_for_each_entry(class_intf,
1074 &dev->class->p->interfaces, node)
1075 if (class_intf->add_dev)
1076 class_intf->add_dev(dev, class_intf);
1077 mutex_unlock(&dev->class->p->mutex);
1079 done:
1080 put_device(dev);
1081 return error;
1082 DPMError:
1083 bus_remove_device(dev);
1084 BusError:
1085 device_remove_attrs(dev);
1086 AttrsError:
1087 device_remove_class_symlinks(dev);
1088 SymlinkError:
1089 if (MAJOR(dev->devt))
1090 devtmpfs_delete_node(dev);
1091 if (MAJOR(dev->devt))
1092 device_remove_sys_dev_entry(dev);
1093 devtattrError:
1094 if (MAJOR(dev->devt))
1095 device_remove_file(dev, &devt_attr);
1096 ueventattrError:
1097 device_remove_file(dev, &uevent_attr);
1098 attrError:
1099 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1100 kobject_del(&dev->kobj);
1101 Error:
1102 cleanup_device_parent(dev);
1103 if (parent)
1104 put_device(parent);
1105 name_error:
1106 kfree(dev->p);
1107 dev->p = NULL;
1108 goto done;
1112 * device_register - register a device with the system.
1113 * @dev: pointer to the device structure
1115 * This happens in two clean steps - initialize the device
1116 * and add it to the system. The two steps can be called
1117 * separately, but this is the easiest and most common.
1118 * I.e. you should only call the two helpers separately if
1119 * have a clearly defined need to use and refcount the device
1120 * before it is added to the hierarchy.
1122 * For more information, see the kerneldoc for device_initialize()
1123 * and device_add().
1125 * NOTE: _Never_ directly free @dev after calling this function, even
1126 * if it returned an error! Always use put_device() to give up the
1127 * reference initialized in this function instead.
1129 int device_register(struct device *dev)
1131 device_initialize(dev);
1132 return device_add(dev);
1136 * get_device - increment reference count for device.
1137 * @dev: device.
1139 * This simply forwards the call to kobject_get(), though
1140 * we do take care to provide for the case that we get a NULL
1141 * pointer passed in.
1143 struct device *get_device(struct device *dev)
1145 return dev ? kobj_to_dev(kobject_get(&dev->kobj)) : NULL;
1149 * put_device - decrement reference count.
1150 * @dev: device in question.
1152 void put_device(struct device *dev)
1154 /* might_sleep(); */
1155 if (dev)
1156 kobject_put(&dev->kobj);
1160 * device_del - delete device from system.
1161 * @dev: device.
1163 * This is the first part of the device unregistration
1164 * sequence. This removes the device from the lists we control
1165 * from here, has it removed from the other driver model
1166 * subsystems it was added to in device_add(), and removes it
1167 * from the kobject hierarchy.
1169 * NOTE: this should be called manually _iff_ device_add() was
1170 * also called manually.
1172 void device_del(struct device *dev)
1174 struct device *parent = dev->parent;
1175 struct class_interface *class_intf;
1177 /* Notify clients of device removal. This call must come
1178 * before dpm_sysfs_remove().
1180 if (dev->bus)
1181 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1182 BUS_NOTIFY_DEL_DEVICE, dev);
1183 device_pm_remove(dev);
1184 dpm_sysfs_remove(dev);
1185 if (parent)
1186 klist_del(&dev->p->knode_parent);
1187 if (MAJOR(dev->devt)) {
1188 devtmpfs_delete_node(dev);
1189 device_remove_sys_dev_entry(dev);
1190 device_remove_file(dev, &devt_attr);
1192 if (dev->class) {
1193 device_remove_class_symlinks(dev);
1195 mutex_lock(&dev->class->p->mutex);
1196 /* notify any interfaces that the device is now gone */
1197 list_for_each_entry(class_intf,
1198 &dev->class->p->interfaces, node)
1199 if (class_intf->remove_dev)
1200 class_intf->remove_dev(dev, class_intf);
1201 /* remove the device from the class list */
1202 klist_del(&dev->knode_class);
1203 mutex_unlock(&dev->class->p->mutex);
1205 device_remove_file(dev, &uevent_attr);
1206 device_remove_attrs(dev);
1207 bus_remove_device(dev);
1208 driver_deferred_probe_del(dev);
1210 /* Notify the platform of the removal, in case they
1211 * need to do anything...
1213 if (platform_notify_remove)
1214 platform_notify_remove(dev);
1215 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1216 cleanup_device_parent(dev);
1217 kobject_del(&dev->kobj);
1218 put_device(parent);
1222 * device_unregister - unregister device from system.
1223 * @dev: device going away.
1225 * We do this in two parts, like we do device_register(). First,
1226 * we remove it from all the subsystems with device_del(), then
1227 * we decrement the reference count via put_device(). If that
1228 * is the final reference count, the device will be cleaned up
1229 * via device_release() above. Otherwise, the structure will
1230 * stick around until the final reference to the device is dropped.
1232 void device_unregister(struct device *dev)
1234 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1235 device_del(dev);
1236 put_device(dev);
1239 static struct device *next_device(struct klist_iter *i)
1241 struct klist_node *n = klist_next(i);
1242 struct device *dev = NULL;
1243 struct device_private *p;
1245 if (n) {
1246 p = to_device_private_parent(n);
1247 dev = p->device;
1249 return dev;
1253 * device_get_devnode - path of device node file
1254 * @dev: device
1255 * @mode: returned file access mode
1256 * @tmp: possibly allocated string
1258 * Return the relative path of a possible device node.
1259 * Non-default names may need to allocate a memory to compose
1260 * a name. This memory is returned in tmp and needs to be
1261 * freed by the caller.
1263 const char *device_get_devnode(struct device *dev,
1264 umode_t *mode, const char **tmp)
1266 char *s;
1268 *tmp = NULL;
1270 /* the device type may provide a specific name */
1271 if (dev->type && dev->type->devnode)
1272 *tmp = dev->type->devnode(dev, mode);
1273 if (*tmp)
1274 return *tmp;
1276 /* the class may provide a specific name */
1277 if (dev->class && dev->class->devnode)
1278 *tmp = dev->class->devnode(dev, mode);
1279 if (*tmp)
1280 return *tmp;
1282 /* return name without allocation, tmp == NULL */
1283 if (strchr(dev_name(dev), '!') == NULL)
1284 return dev_name(dev);
1286 /* replace '!' in the name with '/' */
1287 *tmp = kstrdup(dev_name(dev), GFP_KERNEL);
1288 if (!*tmp)
1289 return NULL;
1290 while ((s = strchr(*tmp, '!')))
1291 s[0] = '/';
1292 return *tmp;
1296 * device_for_each_child - device child iterator.
1297 * @parent: parent struct device.
1298 * @data: data for the callback.
1299 * @fn: function to be called for each device.
1301 * Iterate over @parent's child devices, and call @fn for each,
1302 * passing it @data.
1304 * We check the return of @fn each time. If it returns anything
1305 * other than 0, we break out and return that value.
1307 int device_for_each_child(struct device *parent, void *data,
1308 int (*fn)(struct device *dev, void *data))
1310 struct klist_iter i;
1311 struct device *child;
1312 int error = 0;
1314 if (!parent->p)
1315 return 0;
1317 klist_iter_init(&parent->p->klist_children, &i);
1318 while ((child = next_device(&i)) && !error)
1319 error = fn(child, data);
1320 klist_iter_exit(&i);
1321 return error;
1325 * device_find_child - device iterator for locating a particular device.
1326 * @parent: parent struct device
1327 * @data: Data to pass to match function
1328 * @match: Callback function to check device
1330 * This is similar to the device_for_each_child() function above, but it
1331 * returns a reference to a device that is 'found' for later use, as
1332 * determined by the @match callback.
1334 * The callback should return 0 if the device doesn't match and non-zero
1335 * if it does. If the callback returns non-zero and a reference to the
1336 * current device can be obtained, this function will return to the caller
1337 * and not iterate over any more devices.
1339 struct device *device_find_child(struct device *parent, void *data,
1340 int (*match)(struct device *dev, void *data))
1342 struct klist_iter i;
1343 struct device *child;
1345 if (!parent)
1346 return NULL;
1348 klist_iter_init(&parent->p->klist_children, &i);
1349 while ((child = next_device(&i)))
1350 if (match(child, data) && get_device(child))
1351 break;
1352 klist_iter_exit(&i);
1353 return child;
1356 int __init devices_init(void)
1358 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1359 if (!devices_kset)
1360 return -ENOMEM;
1361 dev_kobj = kobject_create_and_add("dev", NULL);
1362 if (!dev_kobj)
1363 goto dev_kobj_err;
1364 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1365 if (!sysfs_dev_block_kobj)
1366 goto block_kobj_err;
1367 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1368 if (!sysfs_dev_char_kobj)
1369 goto char_kobj_err;
1371 return 0;
1373 char_kobj_err:
1374 kobject_put(sysfs_dev_block_kobj);
1375 block_kobj_err:
1376 kobject_put(dev_kobj);
1377 dev_kobj_err:
1378 kset_unregister(devices_kset);
1379 return -ENOMEM;
1382 EXPORT_SYMBOL_GPL(device_for_each_child);
1383 EXPORT_SYMBOL_GPL(device_find_child);
1385 EXPORT_SYMBOL_GPL(device_initialize);
1386 EXPORT_SYMBOL_GPL(device_add);
1387 EXPORT_SYMBOL_GPL(device_register);
1389 EXPORT_SYMBOL_GPL(device_del);
1390 EXPORT_SYMBOL_GPL(device_unregister);
1391 EXPORT_SYMBOL_GPL(get_device);
1392 EXPORT_SYMBOL_GPL(put_device);
1394 EXPORT_SYMBOL_GPL(device_create_file);
1395 EXPORT_SYMBOL_GPL(device_remove_file);
1397 struct root_device {
1398 struct device dev;
1399 struct module *owner;
1402 inline struct root_device *to_root_device(struct device *d)
1404 return container_of(d, struct root_device, dev);
1407 static void root_device_release(struct device *dev)
1409 kfree(to_root_device(dev));
1413 * __root_device_register - allocate and register a root device
1414 * @name: root device name
1415 * @owner: owner module of the root device, usually THIS_MODULE
1417 * This function allocates a root device and registers it
1418 * using device_register(). In order to free the returned
1419 * device, use root_device_unregister().
1421 * Root devices are dummy devices which allow other devices
1422 * to be grouped under /sys/devices. Use this function to
1423 * allocate a root device and then use it as the parent of
1424 * any device which should appear under /sys/devices/{name}
1426 * The /sys/devices/{name} directory will also contain a
1427 * 'module' symlink which points to the @owner directory
1428 * in sysfs.
1430 * Returns &struct device pointer on success, or ERR_PTR() on error.
1432 * Note: You probably want to use root_device_register().
1434 struct device *__root_device_register(const char *name, struct module *owner)
1436 struct root_device *root;
1437 int err = -ENOMEM;
1439 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
1440 if (!root)
1441 return ERR_PTR(err);
1443 err = dev_set_name(&root->dev, "%s", name);
1444 if (err) {
1445 kfree(root);
1446 return ERR_PTR(err);
1449 root->dev.release = root_device_release;
1451 err = device_register(&root->dev);
1452 if (err) {
1453 put_device(&root->dev);
1454 return ERR_PTR(err);
1457 #ifdef CONFIG_MODULES /* gotta find a "cleaner" way to do this */
1458 if (owner) {
1459 struct module_kobject *mk = &owner->mkobj;
1461 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
1462 if (err) {
1463 device_unregister(&root->dev);
1464 return ERR_PTR(err);
1466 root->owner = owner;
1468 #endif
1470 return &root->dev;
1472 EXPORT_SYMBOL_GPL(__root_device_register);
1475 * root_device_unregister - unregister and free a root device
1476 * @dev: device going away
1478 * This function unregisters and cleans up a device that was created by
1479 * root_device_register().
1481 void root_device_unregister(struct device *dev)
1483 struct root_device *root = to_root_device(dev);
1485 if (root->owner)
1486 sysfs_remove_link(&root->dev.kobj, "module");
1488 device_unregister(dev);
1490 EXPORT_SYMBOL_GPL(root_device_unregister);
1493 static void device_create_release(struct device *dev)
1495 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1496 kfree(dev);
1500 * device_create_vargs - creates a device and registers it with sysfs
1501 * @class: pointer to the struct class that this device should be registered to
1502 * @parent: pointer to the parent struct device of this new device, if any
1503 * @devt: the dev_t for the char device to be added
1504 * @drvdata: the data to be added to the device for callbacks
1505 * @fmt: string for the device's name
1506 * @args: va_list for the device's name
1508 * This function can be used by char device classes. A struct device
1509 * will be created in sysfs, registered to the specified class.
1511 * A "dev" file will be created, showing the dev_t for the device, if
1512 * the dev_t is not 0,0.
1513 * If a pointer to a parent struct device is passed in, the newly created
1514 * struct device will be a child of that device in sysfs.
1515 * The pointer to the struct device will be returned from the call.
1516 * Any further sysfs files that might be required can be created using this
1517 * pointer.
1519 * Returns &struct device pointer on success, or ERR_PTR() on error.
1521 * Note: the struct class passed to this function must have previously
1522 * been created with a call to class_create().
1524 struct device *device_create_vargs(struct class *class, struct device *parent,
1525 dev_t devt, void *drvdata, const char *fmt,
1526 va_list args)
1528 struct device *dev = NULL;
1529 int retval = -ENODEV;
1531 if (class == NULL || IS_ERR(class))
1532 goto error;
1534 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1535 if (!dev) {
1536 retval = -ENOMEM;
1537 goto error;
1540 dev->devt = devt;
1541 dev->class = class;
1542 dev->parent = parent;
1543 dev->release = device_create_release;
1544 dev_set_drvdata(dev, drvdata);
1546 retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
1547 if (retval)
1548 goto error;
1550 retval = device_register(dev);
1551 if (retval)
1552 goto error;
1554 return dev;
1556 error:
1557 put_device(dev);
1558 return ERR_PTR(retval);
1560 EXPORT_SYMBOL_GPL(device_create_vargs);
1563 * device_create - creates a device and registers it with sysfs
1564 * @class: pointer to the struct class that this device should be registered to
1565 * @parent: pointer to the parent struct device of this new device, if any
1566 * @devt: the dev_t for the char device to be added
1567 * @drvdata: the data to be added to the device for callbacks
1568 * @fmt: string for the device's name
1570 * This function can be used by char device classes. A struct device
1571 * will be created in sysfs, registered to the specified class.
1573 * A "dev" file will be created, showing the dev_t for the device, if
1574 * the dev_t is not 0,0.
1575 * If a pointer to a parent struct device is passed in, the newly created
1576 * struct device will be a child of that device in sysfs.
1577 * The pointer to the struct device will be returned from the call.
1578 * Any further sysfs files that might be required can be created using this
1579 * pointer.
1581 * Returns &struct device pointer on success, or ERR_PTR() on error.
1583 * Note: the struct class passed to this function must have previously
1584 * been created with a call to class_create().
1586 struct device *device_create(struct class *class, struct device *parent,
1587 dev_t devt, void *drvdata, const char *fmt, ...)
1589 va_list vargs;
1590 struct device *dev;
1592 va_start(vargs, fmt);
1593 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
1594 va_end(vargs);
1595 return dev;
1597 EXPORT_SYMBOL_GPL(device_create);
1599 static int __match_devt(struct device *dev, void *data)
1601 dev_t *devt = data;
1603 return dev->devt == *devt;
1607 * device_destroy - removes a device that was created with device_create()
1608 * @class: pointer to the struct class that this device was registered with
1609 * @devt: the dev_t of the device that was previously registered
1611 * This call unregisters and cleans up a device that was created with a
1612 * call to device_create().
1614 void device_destroy(struct class *class, dev_t devt)
1616 struct device *dev;
1618 dev = class_find_device(class, NULL, &devt, __match_devt);
1619 if (dev) {
1620 put_device(dev);
1621 device_unregister(dev);
1624 EXPORT_SYMBOL_GPL(device_destroy);
1627 * device_rename - renames a device
1628 * @dev: the pointer to the struct device to be renamed
1629 * @new_name: the new name of the device
1631 * It is the responsibility of the caller to provide mutual
1632 * exclusion between two different calls of device_rename
1633 * on the same device to ensure that new_name is valid and
1634 * won't conflict with other devices.
1636 * Note: Don't call this function. Currently, the networking layer calls this
1637 * function, but that will change. The following text from Kay Sievers offers
1638 * some insight:
1640 * Renaming devices is racy at many levels, symlinks and other stuff are not
1641 * replaced atomically, and you get a "move" uevent, but it's not easy to
1642 * connect the event to the old and new device. Device nodes are not renamed at
1643 * all, there isn't even support for that in the kernel now.
1645 * In the meantime, during renaming, your target name might be taken by another
1646 * driver, creating conflicts. Or the old name is taken directly after you
1647 * renamed it -- then you get events for the same DEVPATH, before you even see
1648 * the "move" event. It's just a mess, and nothing new should ever rely on
1649 * kernel device renaming. Besides that, it's not even implemented now for
1650 * other things than (driver-core wise very simple) network devices.
1652 * We are currently about to change network renaming in udev to completely
1653 * disallow renaming of devices in the same namespace as the kernel uses,
1654 * because we can't solve the problems properly, that arise with swapping names
1655 * of multiple interfaces without races. Means, renaming of eth[0-9]* will only
1656 * be allowed to some other name than eth[0-9]*, for the aforementioned
1657 * reasons.
1659 * Make up a "real" name in the driver before you register anything, or add
1660 * some other attributes for userspace to find the device, or use udev to add
1661 * symlinks -- but never rename kernel devices later, it's a complete mess. We
1662 * don't even want to get into that and try to implement the missing pieces in
1663 * the core. We really have other pieces to fix in the driver core mess. :)
1665 int device_rename(struct device *dev, const char *new_name)
1667 char *old_class_name = NULL;
1668 char *new_class_name = NULL;
1669 char *old_device_name = NULL;
1670 int error;
1672 dev = get_device(dev);
1673 if (!dev)
1674 return -EINVAL;
1676 pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
1677 __func__, new_name);
1679 old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
1680 if (!old_device_name) {
1681 error = -ENOMEM;
1682 goto out;
1685 if (dev->class) {
1686 error = sysfs_rename_link(&dev->class->p->subsys.kobj,
1687 &dev->kobj, old_device_name, new_name);
1688 if (error)
1689 goto out;
1692 error = kobject_rename(&dev->kobj, new_name);
1693 if (error)
1694 goto out;
1696 out:
1697 put_device(dev);
1699 kfree(new_class_name);
1700 kfree(old_class_name);
1701 kfree(old_device_name);
1703 return error;
1705 EXPORT_SYMBOL_GPL(device_rename);
1707 static int device_move_class_links(struct device *dev,
1708 struct device *old_parent,
1709 struct device *new_parent)
1711 int error = 0;
1713 if (old_parent)
1714 sysfs_remove_link(&dev->kobj, "device");
1715 if (new_parent)
1716 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1717 "device");
1718 return error;
1722 * device_move - moves a device to a new parent
1723 * @dev: the pointer to the struct device to be moved
1724 * @new_parent: the new parent of the device (can by NULL)
1725 * @dpm_order: how to reorder the dpm_list
1727 int device_move(struct device *dev, struct device *new_parent,
1728 enum dpm_order dpm_order)
1730 int error;
1731 struct device *old_parent;
1732 struct kobject *new_parent_kobj;
1734 dev = get_device(dev);
1735 if (!dev)
1736 return -EINVAL;
1738 device_pm_lock();
1739 new_parent = get_device(new_parent);
1740 new_parent_kobj = get_device_parent(dev, new_parent);
1742 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
1743 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
1744 error = kobject_move(&dev->kobj, new_parent_kobj);
1745 if (error) {
1746 cleanup_glue_dir(dev, new_parent_kobj);
1747 put_device(new_parent);
1748 goto out;
1750 old_parent = dev->parent;
1751 dev->parent = new_parent;
1752 if (old_parent)
1753 klist_remove(&dev->p->knode_parent);
1754 if (new_parent) {
1755 klist_add_tail(&dev->p->knode_parent,
1756 &new_parent->p->klist_children);
1757 set_dev_node(dev, dev_to_node(new_parent));
1760 if (dev->class) {
1761 error = device_move_class_links(dev, old_parent, new_parent);
1762 if (error) {
1763 /* We ignore errors on cleanup since we're hosed anyway... */
1764 device_move_class_links(dev, new_parent, old_parent);
1765 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1766 if (new_parent)
1767 klist_remove(&dev->p->knode_parent);
1768 dev->parent = old_parent;
1769 if (old_parent) {
1770 klist_add_tail(&dev->p->knode_parent,
1771 &old_parent->p->klist_children);
1772 set_dev_node(dev, dev_to_node(old_parent));
1775 cleanup_glue_dir(dev, new_parent_kobj);
1776 put_device(new_parent);
1777 goto out;
1780 switch (dpm_order) {
1781 case DPM_ORDER_NONE:
1782 break;
1783 case DPM_ORDER_DEV_AFTER_PARENT:
1784 device_pm_move_after(dev, new_parent);
1785 break;
1786 case DPM_ORDER_PARENT_BEFORE_DEV:
1787 device_pm_move_before(new_parent, dev);
1788 break;
1789 case DPM_ORDER_DEV_LAST:
1790 device_pm_move_last(dev);
1791 break;
1794 put_device(old_parent);
1795 out:
1796 device_pm_unlock();
1797 put_device(dev);
1798 return error;
1800 EXPORT_SYMBOL_GPL(device_move);
1803 * device_shutdown - call ->shutdown() on each device to shutdown.
1805 void device_shutdown(void)
1807 struct device *dev;
1809 spin_lock(&devices_kset->list_lock);
1811 * Walk the devices list backward, shutting down each in turn.
1812 * Beware that device unplug events may also start pulling
1813 * devices offline, even as the system is shutting down.
1815 while (!list_empty(&devices_kset->list)) {
1816 dev = list_entry(devices_kset->list.prev, struct device,
1817 kobj.entry);
1820 * hold reference count of device's parent to
1821 * prevent it from being freed because parent's
1822 * lock is to be held
1824 get_device(dev->parent);
1825 get_device(dev);
1827 * Make sure the device is off the kset list, in the
1828 * event that dev->*->shutdown() doesn't remove it.
1830 list_del_init(&dev->kobj.entry);
1831 spin_unlock(&devices_kset->list_lock);
1833 /* hold lock to avoid race with probe/release */
1834 if (dev->parent)
1835 device_lock(dev->parent);
1836 device_lock(dev);
1838 /* Don't allow any more runtime suspends */
1839 pm_runtime_get_noresume(dev);
1840 pm_runtime_barrier(dev);
1842 if (dev->bus && dev->bus->shutdown) {
1843 dev_dbg(dev, "shutdown\n");
1844 dev->bus->shutdown(dev);
1845 } else if (dev->driver && dev->driver->shutdown) {
1846 dev_dbg(dev, "shutdown\n");
1847 dev->driver->shutdown(dev);
1850 device_unlock(dev);
1851 if (dev->parent)
1852 device_unlock(dev->parent);
1854 put_device(dev);
1855 put_device(dev->parent);
1857 spin_lock(&devices_kset->list_lock);
1859 spin_unlock(&devices_kset->list_lock);
1860 async_synchronize_full();
1864 * Device logging functions
1867 #ifdef CONFIG_PRINTK
1868 int __dev_printk(const char *level, const struct device *dev,
1869 struct va_format *vaf)
1871 char dict[128];
1872 size_t dictlen = 0;
1873 const char *subsys;
1875 if (!dev)
1876 return printk("%s(NULL device *): %pV", level, vaf);
1878 if (dev->class)
1879 subsys = dev->class->name;
1880 else if (dev->bus)
1881 subsys = dev->bus->name;
1882 else
1883 goto skip;
1885 dictlen += snprintf(dict + dictlen, sizeof(dict) - dictlen,
1886 "SUBSYSTEM=%s", subsys);
1889 * Add device identifier DEVICE=:
1890 * b12:8 block dev_t
1891 * c127:3 char dev_t
1892 * n8 netdev ifindex
1893 * +sound:card0 subsystem:devname
1895 if (MAJOR(dev->devt)) {
1896 char c;
1898 if (strcmp(subsys, "block") == 0)
1899 c = 'b';
1900 else
1901 c = 'c';
1902 dictlen++;
1903 dictlen += snprintf(dict + dictlen, sizeof(dict) - dictlen,
1904 "DEVICE=%c%u:%u",
1905 c, MAJOR(dev->devt), MINOR(dev->devt));
1906 } else if (strcmp(subsys, "net") == 0) {
1907 struct net_device *net = to_net_dev(dev);
1909 dictlen++;
1910 dictlen += snprintf(dict + dictlen, sizeof(dict) - dictlen,
1911 "DEVICE=n%u", net->ifindex);
1912 } else {
1913 dictlen++;
1914 dictlen += snprintf(dict + dictlen, sizeof(dict) - dictlen,
1915 "DEVICE=+%s:%s", subsys, dev_name(dev));
1917 skip:
1918 return printk_emit(0, level[1] - '0',
1919 dictlen ? dict : NULL, dictlen,
1920 "%s %s: %pV",
1921 dev_driver_string(dev), dev_name(dev), vaf);
1923 EXPORT_SYMBOL(__dev_printk);
1925 int dev_printk(const char *level, const struct device *dev,
1926 const char *fmt, ...)
1928 struct va_format vaf;
1929 va_list args;
1930 int r;
1932 va_start(args, fmt);
1934 vaf.fmt = fmt;
1935 vaf.va = &args;
1937 r = __dev_printk(level, dev, &vaf);
1938 va_end(args);
1940 return r;
1942 EXPORT_SYMBOL(dev_printk);
1944 #define define_dev_printk_level(func, kern_level) \
1945 int func(const struct device *dev, const char *fmt, ...) \
1947 struct va_format vaf; \
1948 va_list args; \
1949 int r; \
1951 va_start(args, fmt); \
1953 vaf.fmt = fmt; \
1954 vaf.va = &args; \
1956 r = __dev_printk(kern_level, dev, &vaf); \
1957 va_end(args); \
1959 return r; \
1961 EXPORT_SYMBOL(func);
1963 define_dev_printk_level(dev_emerg, KERN_EMERG);
1964 define_dev_printk_level(dev_alert, KERN_ALERT);
1965 define_dev_printk_level(dev_crit, KERN_CRIT);
1966 define_dev_printk_level(dev_err, KERN_ERR);
1967 define_dev_printk_level(dev_warn, KERN_WARNING);
1968 define_dev_printk_level(dev_notice, KERN_NOTICE);
1969 define_dev_printk_level(_dev_info, KERN_INFO);
1971 #endif