2 * drivers/base/core.c - core driver model code (device registration, etc)
4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs
6 * Copyright (c) 2006 Greg Kroah-Hartman <gregkh@suse.de>
7 * Copyright (c) 2006 Novell, Inc.
9 * This file is released under the GPLv2
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
19 #include <linux/kdev_t.h>
20 #include <linux/notifier.h>
21 #include <linux/genhd.h>
22 #include <linux/kallsyms.h>
23 #include <linux/semaphore.h>
24 #include <linux/mutex.h>
27 #include "power/power.h"
29 int (*platform_notify)(struct device *dev) = NULL;
30 int (*platform_notify_remove)(struct device *dev) = NULL;
31 static struct kobject *dev_kobj;
32 struct kobject *sysfs_dev_char_kobj;
33 struct kobject *sysfs_dev_block_kobj;
39 #if defined(CONFIG_BLOCK) && !defined(DDE_LINUX)
40 static inline int device_is_not_partition(struct device *dev)
42 return !(dev->type == &part_type);
45 static inline int device_is_not_partition(struct device *dev)
52 * dev_driver_string - Return a device's driver name, if at all possible
53 * @dev: struct device to get the name of
55 * Will return the device's driver's name if it is bound to a device. If
56 * the device is not bound to a device, it will return the name of the bus
57 * it is attached to. If it is not attached to a bus either, an empty
58 * string will be returned.
60 const char *dev_driver_string(const struct device *dev)
62 return dev->driver ? dev->driver->name :
63 (dev->bus ? dev->bus->name :
64 (dev->class ? dev->class->name : ""));
66 EXPORT_SYMBOL(dev_driver_string);
68 #define to_dev(obj) container_of(obj, struct device, kobj)
69 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
71 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
74 struct device_attribute *dev_attr = to_dev_attr(attr);
75 struct device *dev = to_dev(kobj);
79 ret = dev_attr->show(dev, dev_attr, buf);
80 if (ret >= (ssize_t)PAGE_SIZE) {
81 print_symbol("dev_attr_show: %s returned bad count\n",
82 (unsigned long)dev_attr->show);
87 static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
88 const char *buf, size_t count)
90 struct device_attribute *dev_attr = to_dev_attr(attr);
91 struct device *dev = to_dev(kobj);
95 ret = dev_attr->store(dev, dev_attr, buf, count);
99 static struct sysfs_ops dev_sysfs_ops = {
100 .show = dev_attr_show,
101 .store = dev_attr_store,
106 * device_release - free device structure.
107 * @kobj: device's kobject.
109 * This is called once the reference count for the object
110 * reaches 0. We forward the call to the device's release
111 * method, which should handle actually freeing the structure.
113 static void device_release(struct kobject *kobj)
115 struct device *dev = to_dev(kobj);
119 else if (dev->type && dev->type->release)
120 dev->type->release(dev);
121 else if (dev->class && dev->class->dev_release)
122 dev->class->dev_release(dev);
124 WARN(1, KERN_ERR "Device '%s' does not have a release() "
125 "function, it is broken and must be fixed.\n",
129 static struct kobj_type device_ktype = {
130 .release = device_release,
131 .sysfs_ops = &dev_sysfs_ops,
135 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
137 struct kobj_type *ktype = get_ktype(kobj);
139 if (ktype == &device_ktype) {
140 struct device *dev = to_dev(kobj);
141 if (dev->uevent_suppress)
151 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
153 struct device *dev = to_dev(kobj);
156 return dev->bus->name;
158 return dev->class->name;
162 static int dev_uevent(struct kset *kset, struct kobject *kobj,
163 struct kobj_uevent_env *env)
165 struct device *dev = to_dev(kobj);
169 /* add the major/minor if present */
170 if (MAJOR(dev->devt)) {
171 add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
172 add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
175 if (dev->type && dev->type->name)
176 add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
179 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
181 #ifdef CONFIG_SYSFS_DEPRECATED
183 struct device *parent = dev->parent;
185 /* find first bus device in parent chain */
186 while (parent && !parent->bus)
187 parent = parent->parent;
188 if (parent && parent->bus) {
191 path = kobject_get_path(&parent->kobj, GFP_KERNEL);
193 add_uevent_var(env, "PHYSDEVPATH=%s", path);
197 add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name);
200 add_uevent_var(env, "PHYSDEVDRIVER=%s",
201 parent->driver->name);
203 } else if (dev->bus) {
204 add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
207 add_uevent_var(env, "PHYSDEVDRIVER=%s",
212 /* have the bus specific function add its stuff */
213 if (dev->bus && dev->bus->uevent) {
214 retval = dev->bus->uevent(dev, env);
216 pr_debug("device: '%s': %s: bus uevent() returned %d\n",
217 dev_name(dev), __func__, retval);
220 /* have the class specific function add its stuff */
221 if (dev->class && dev->class->dev_uevent) {
222 retval = dev->class->dev_uevent(dev, env);
224 pr_debug("device: '%s': %s: class uevent() "
225 "returned %d\n", dev_name(dev),
229 /* have the device type specific fuction add its stuff */
230 if (dev->type && dev->type->uevent) {
231 retval = dev->type->uevent(dev, env);
233 pr_debug("device: '%s': %s: dev_type uevent() "
234 "returned %d\n", dev_name(dev),
242 static struct kset_uevent_ops device_uevent_ops = {
243 .filter = dev_uevent_filter,
244 .name = dev_uevent_name,
245 .uevent = dev_uevent,
248 static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
251 struct kobject *top_kobj;
253 struct kobj_uevent_env *env = NULL;
258 /* search the kset, the device belongs to */
259 top_kobj = &dev->kobj;
260 while (!top_kobj->kset && top_kobj->parent)
261 top_kobj = top_kobj->parent;
265 kset = top_kobj->kset;
266 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
270 if (kset->uevent_ops && kset->uevent_ops->filter)
271 if (!kset->uevent_ops->filter(kset, &dev->kobj))
274 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
278 /* let the kset specific function add its keys */
279 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
283 /* copy keys to file */
284 for (i = 0; i < env->envp_idx; i++)
285 count += sprintf(&buf[count], "%s\n", env->envp[i]);
291 static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
292 const char *buf, size_t count)
294 enum kobject_action action;
296 if (kobject_action_type(buf, count, &action) == 0) {
297 kobject_uevent(&dev->kobj, action);
301 dev_err(dev, "uevent: unsupported action-string; this will "
302 "be ignored in a future kernel version\n");
303 kobject_uevent(&dev->kobj, KOBJ_ADD);
308 static struct device_attribute uevent_attr =
309 __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
311 static int device_add_attributes(struct device *dev,
312 struct device_attribute *attrs)
318 for (i = 0; attr_name(attrs[i]); i++) {
319 error = device_create_file(dev, &attrs[i]);
325 device_remove_file(dev, &attrs[i]);
330 static void device_remove_attributes(struct device *dev,
331 struct device_attribute *attrs)
336 for (i = 0; attr_name(attrs[i]); i++)
337 device_remove_file(dev, &attrs[i]);
340 static int device_add_groups(struct device *dev,
341 struct attribute_group **groups)
347 for (i = 0; groups[i]; i++) {
348 error = sysfs_create_group(&dev->kobj, groups[i]);
351 sysfs_remove_group(&dev->kobj,
360 static void device_remove_groups(struct device *dev,
361 struct attribute_group **groups)
366 for (i = 0; groups[i]; i++)
367 sysfs_remove_group(&dev->kobj, groups[i]);
370 static int device_add_attrs(struct device *dev)
372 struct class *class = dev->class;
373 struct device_type *type = dev->type;
377 error = device_add_attributes(dev, class->dev_attrs);
383 error = device_add_groups(dev, type->groups);
385 goto err_remove_class_attrs;
388 error = device_add_groups(dev, dev->groups);
390 goto err_remove_type_groups;
394 err_remove_type_groups:
396 device_remove_groups(dev, type->groups);
397 err_remove_class_attrs:
399 device_remove_attributes(dev, class->dev_attrs);
404 static void device_remove_attrs(struct device *dev)
406 struct class *class = dev->class;
407 struct device_type *type = dev->type;
409 device_remove_groups(dev, dev->groups);
412 device_remove_groups(dev, type->groups);
415 device_remove_attributes(dev, class->dev_attrs);
419 static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
422 return print_dev_t(buf, dev->devt);
425 static struct device_attribute devt_attr =
426 __ATTR(dev, S_IRUGO, show_dev, NULL);
428 /* kset to create /sys/devices/ */
429 struct kset *devices_kset;
432 * device_create_file - create sysfs attribute file for device.
434 * @attr: device attribute descriptor.
436 int device_create_file(struct device *dev, struct device_attribute *attr)
440 error = sysfs_create_file(&dev->kobj, &attr->attr);
445 * device_remove_file - remove sysfs attribute file.
447 * @attr: device attribute descriptor.
449 void device_remove_file(struct device *dev, struct device_attribute *attr)
452 sysfs_remove_file(&dev->kobj, &attr->attr);
456 * device_create_bin_file - create sysfs binary attribute file for device.
458 * @attr: device binary attribute descriptor.
460 int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
464 error = sysfs_create_bin_file(&dev->kobj, attr);
467 EXPORT_SYMBOL_GPL(device_create_bin_file);
470 * device_remove_bin_file - remove sysfs binary attribute file
472 * @attr: device binary attribute descriptor.
474 void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
477 sysfs_remove_bin_file(&dev->kobj, attr);
479 EXPORT_SYMBOL_GPL(device_remove_bin_file);
482 * device_schedule_callback_owner - helper to schedule a callback for a device
484 * @func: callback function to invoke later.
485 * @owner: module owning the callback routine
487 * Attribute methods must not unregister themselves or their parent device
488 * (which would amount to the same thing). Attempts to do so will deadlock,
489 * since unregistration is mutually exclusive with driver callbacks.
491 * Instead methods can call this routine, which will attempt to allocate
492 * and schedule a workqueue request to call back @func with @dev as its
493 * argument in the workqueue's process context. @dev will be pinned until
496 * This routine is usually called via the inline device_schedule_callback(),
497 * which automatically sets @owner to THIS_MODULE.
499 * Returns 0 if the request was submitted, -ENOMEM if storage could not
500 * be allocated, -ENODEV if a reference to @owner isn't available.
502 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
503 * underlying sysfs routine (since it is intended for use by attribute
504 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
506 int device_schedule_callback_owner(struct device *dev,
507 void (*func)(struct device *), struct module *owner)
509 return sysfs_schedule_callback(&dev->kobj,
510 (void (*)(void *)) func, dev, owner);
512 EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
514 static void klist_children_get(struct klist_node *n)
516 struct device *dev = container_of(n, struct device, knode_parent);
521 static void klist_children_put(struct klist_node *n)
523 struct device *dev = container_of(n, struct device, knode_parent);
529 * device_initialize - init device structure.
532 * This prepares the device for use by other layers by initializing
534 * It is the first half of device_register(), if called by
535 * that function, though it can also be called separately, so one
536 * may use @dev's fields. In particular, get_device()/put_device()
537 * may be used for reference counting of @dev after calling this
540 * NOTE: Use put_device() to give up your reference instead of freeing
541 * @dev directly once you have called this function.
543 void device_initialize(struct device *dev)
545 dev->kobj.kset = devices_kset;
546 kobject_init(&dev->kobj, &device_ktype);
547 klist_init(&dev->klist_children, klist_children_get,
549 INIT_LIST_HEAD(&dev->dma_pools);
550 init_MUTEX(&dev->sem);
551 spin_lock_init(&dev->devres_lock);
552 INIT_LIST_HEAD(&dev->devres_head);
553 device_init_wakeup(dev, 0);
555 set_dev_node(dev, -1);
558 #ifdef CONFIG_SYSFS_DEPRECATED
559 static struct kobject *get_device_parent(struct device *dev,
560 struct device *parent)
562 /* class devices without a parent live in /sys/class/<classname>/ */
563 if (dev->class && (!parent || parent->class != dev->class))
564 return &dev->class->p->class_subsys.kobj;
565 /* all other devices keep their parent */
567 return &parent->kobj;
572 static inline void cleanup_device_parent(struct device *dev) {}
573 static inline void cleanup_glue_dir(struct device *dev,
574 struct kobject *glue_dir) {}
576 static struct kobject *virtual_device_parent(struct device *dev)
578 static struct kobject *virtual_dir = NULL;
581 virtual_dir = kobject_create_and_add("virtual",
582 &devices_kset->kobj);
587 static struct kobject *get_device_parent(struct device *dev,
588 struct device *parent)
593 struct kobject *kobj = NULL;
594 struct kobject *parent_kobj;
598 * If we have no parent, we live in "virtual".
599 * Class-devices with a non class-device as parent, live
600 * in a "glue" directory to prevent namespace collisions.
603 parent_kobj = virtual_device_parent(dev);
604 else if (parent->class)
605 return &parent->kobj;
607 parent_kobj = &parent->kobj;
609 /* find our class-directory at the parent and reference it */
610 spin_lock(&dev->class->p->class_dirs.list_lock);
611 list_for_each_entry(k, &dev->class->p->class_dirs.list, entry)
612 if (k->parent == parent_kobj) {
613 kobj = kobject_get(k);
616 spin_unlock(&dev->class->p->class_dirs.list_lock);
620 /* or create a new class-directory at the parent device */
621 k = kobject_create();
624 k->kset = &dev->class->p->class_dirs;
625 retval = kobject_add(k, parent_kobj, "%s", dev->class->name);
630 /* do not emit an uevent for this simple "glue" directory */
635 return &parent->kobj;
639 static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
641 /* see if we live in a "glue" directory */
642 if (!glue_dir || !dev->class ||
643 glue_dir->kset != &dev->class->p->class_dirs)
646 kobject_put(glue_dir);
649 static void cleanup_device_parent(struct device *dev)
651 cleanup_glue_dir(dev, dev->kobj.parent);
655 static void setup_parent(struct device *dev, struct device *parent)
657 struct kobject *kobj;
658 kobj = get_device_parent(dev, parent);
660 dev->kobj.parent = kobj;
663 static int device_add_class_symlinks(struct device *dev)
670 error = sysfs_create_link(&dev->kobj,
671 &dev->class->p->class_subsys.kobj,
676 #ifdef CONFIG_SYSFS_DEPRECATED
677 /* stacked class devices need a symlink in the class directory */
678 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
679 device_is_not_partition(dev)) {
680 error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
681 &dev->kobj, dev_name(dev));
686 if (dev->parent && device_is_not_partition(dev)) {
687 struct device *parent = dev->parent;
691 * stacked class devices have the 'device' link
692 * pointing to the bus device instead of the parent
694 while (parent->class && !parent->bus && parent->parent)
695 parent = parent->parent;
697 error = sysfs_create_link(&dev->kobj,
703 class_name = make_class_name(dev->class->name,
706 error = sysfs_create_link(&dev->parent->kobj,
707 &dev->kobj, class_name);
715 if (dev->parent && device_is_not_partition(dev))
716 sysfs_remove_link(&dev->kobj, "device");
718 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
719 device_is_not_partition(dev))
720 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
723 /* link in the class directory pointing to the device */
724 error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
725 &dev->kobj, dev_name(dev));
729 if (dev->parent && device_is_not_partition(dev)) {
730 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
738 sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
742 sysfs_remove_link(&dev->kobj, "subsystem");
747 static void device_remove_class_symlinks(struct device *dev)
752 #ifdef CONFIG_SYSFS_DEPRECATED
753 if (dev->parent && device_is_not_partition(dev)) {
756 class_name = make_class_name(dev->class->name, &dev->kobj);
758 sysfs_remove_link(&dev->parent->kobj, class_name);
761 sysfs_remove_link(&dev->kobj, "device");
764 if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
765 device_is_not_partition(dev))
766 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
769 if (dev->parent && device_is_not_partition(dev))
770 sysfs_remove_link(&dev->kobj, "device");
772 sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
775 sysfs_remove_link(&dev->kobj, "subsystem");
779 * dev_set_name - set a device name
781 * @fmt: format string for the device's name
783 int dev_set_name(struct device *dev, const char *fmt, ...)
788 va_start(vargs, fmt);
789 vsnprintf(dev->bus_id, sizeof(dev->bus_id), fmt, vargs);
792 /* ewww... some of these buggers have / in the name... */
793 while ((s = strchr(dev->bus_id, '/')))
798 EXPORT_SYMBOL_GPL(dev_set_name);
801 * device_to_dev_kobj - select a /sys/dev/ directory for the device
804 * By default we select char/ for new entries. Setting class->dev_obj
805 * to NULL prevents an entry from being created. class->dev_kobj must
806 * be set (or cleared) before any devices are registered to the class
807 * otherwise device_create_sys_dev_entry() and
808 * device_remove_sys_dev_entry() will disagree about the the presence
811 static struct kobject *device_to_dev_kobj(struct device *dev)
813 struct kobject *kobj;
816 kobj = dev->class->dev_kobj;
818 kobj = sysfs_dev_char_kobj;
823 static int device_create_sys_dev_entry(struct device *dev)
825 struct kobject *kobj = device_to_dev_kobj(dev);
830 format_dev_t(devt_str, dev->devt);
831 error = sysfs_create_link(kobj, &dev->kobj, devt_str);
837 static void device_remove_sys_dev_entry(struct device *dev)
839 struct kobject *kobj = device_to_dev_kobj(dev);
843 format_dev_t(devt_str, dev->devt);
844 sysfs_remove_link(kobj, devt_str);
849 * device_add - add device to device hierarchy.
852 * This is part 2 of device_register(), though may be called
853 * separately _iff_ device_initialize() has been called separately.
855 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
856 * to the global and sibling lists for the device, then
857 * adds it to the other relevant subsystems of the driver model.
859 * NOTE: _Never_ directly free @dev after calling this function, even
860 * if it returned an error! Always use put_device() to give up your
863 int device_add(struct device *dev)
865 struct device *parent = NULL;
866 struct class_interface *class_intf;
869 dev = get_device(dev);
873 /* Temporarily support init_name if it is set.
874 * It will override bus_id for now */
876 dev_set_name(dev, "%s", dev->init_name);
878 if (!strlen(dev->bus_id))
881 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
883 parent = get_device(dev->parent);
884 setup_parent(dev, parent);
886 /* use parent numa_node */
888 set_dev_node(dev, dev_to_node(parent));
890 /* first, register with generic layer. */
891 error = kobject_add(&dev->kobj, dev->kobj.parent, "%s", dev_name(dev));
895 /* notify platform of device entry */
897 platform_notify(dev);
899 error = device_create_file(dev, &uevent_attr);
903 if (MAJOR(dev->devt)) {
904 error = device_create_file(dev, &devt_attr);
906 goto ueventattrError;
908 error = device_create_sys_dev_entry(dev);
913 error = device_add_class_symlinks(dev);
916 error = device_add_attrs(dev);
919 error = bus_add_device(dev);
922 error = dpm_sysfs_add(dev);
927 /* Notify clients of device addition. This call must come
928 * after dpm_sysf_add() and before kobject_uevent().
931 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
932 BUS_NOTIFY_ADD_DEVICE, dev);
934 kobject_uevent(&dev->kobj, KOBJ_ADD);
935 bus_attach_device(dev);
937 klist_add_tail(&dev->knode_parent, &parent->klist_children);
940 mutex_lock(&dev->class->p->class_mutex);
941 /* tie the class to the device */
942 klist_add_tail(&dev->knode_class,
943 &dev->class->p->class_devices);
945 /* notify any interfaces that the device is here */
946 list_for_each_entry(class_intf,
947 &dev->class->p->class_interfaces, node)
948 if (class_intf->add_dev)
949 class_intf->add_dev(dev, class_intf);
950 mutex_unlock(&dev->class->p->class_mutex);
956 bus_remove_device(dev);
958 device_remove_attrs(dev);
960 device_remove_class_symlinks(dev);
962 if (MAJOR(dev->devt))
963 device_remove_sys_dev_entry(dev);
965 if (MAJOR(dev->devt))
966 device_remove_file(dev, &devt_attr);
968 device_remove_file(dev, &uevent_attr);
970 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
971 kobject_del(&dev->kobj);
973 cleanup_device_parent(dev);
980 * device_register - register a device with the system.
981 * @dev: pointer to the device structure
983 * This happens in two clean steps - initialize the device
984 * and add it to the system. The two steps can be called
985 * separately, but this is the easiest and most common.
986 * I.e. you should only call the two helpers separately if
987 * have a clearly defined need to use and refcount the device
988 * before it is added to the hierarchy.
990 * NOTE: _Never_ directly free @dev after calling this function, even
991 * if it returned an error! Always use put_device() to give up the
992 * reference initialized in this function instead.
994 int device_register(struct device *dev)
996 device_initialize(dev);
997 return device_add(dev);
1001 * get_device - increment reference count for device.
1004 * This simply forwards the call to kobject_get(), though
1005 * we do take care to provide for the case that we get a NULL
1006 * pointer passed in.
1008 struct device *get_device(struct device *dev)
1010 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
1014 * put_device - decrement reference count.
1015 * @dev: device in question.
1017 void put_device(struct device *dev)
1019 /* might_sleep(); */
1021 kobject_put(&dev->kobj);
1025 * device_del - delete device from system.
1028 * This is the first part of the device unregistration
1029 * sequence. This removes the device from the lists we control
1030 * from here, has it removed from the other driver model
1031 * subsystems it was added to in device_add(), and removes it
1032 * from the kobject hierarchy.
1034 * NOTE: this should be called manually _iff_ device_add() was
1035 * also called manually.
1037 void device_del(struct device *dev)
1039 struct device *parent = dev->parent;
1040 struct class_interface *class_intf;
1042 /* Notify clients of device removal. This call must come
1043 * before dpm_sysfs_remove().
1046 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1047 BUS_NOTIFY_DEL_DEVICE, dev);
1048 device_pm_remove(dev);
1049 dpm_sysfs_remove(dev);
1051 klist_del(&dev->knode_parent);
1052 if (MAJOR(dev->devt)) {
1053 device_remove_sys_dev_entry(dev);
1054 device_remove_file(dev, &devt_attr);
1057 device_remove_class_symlinks(dev);
1059 mutex_lock(&dev->class->p->class_mutex);
1060 /* notify any interfaces that the device is now gone */
1061 list_for_each_entry(class_intf,
1062 &dev->class->p->class_interfaces, node)
1063 if (class_intf->remove_dev)
1064 class_intf->remove_dev(dev, class_intf);
1065 /* remove the device from the class list */
1066 klist_del(&dev->knode_class);
1067 mutex_unlock(&dev->class->p->class_mutex);
1069 device_remove_file(dev, &uevent_attr);
1070 device_remove_attrs(dev);
1071 bus_remove_device(dev);
1074 * Some platform devices are driven without driver attached
1075 * and managed resources may have been acquired. Make sure
1076 * all resources are released.
1078 devres_release_all(dev);
1080 /* Notify the platform of the removal, in case they
1081 * need to do anything...
1083 if (platform_notify_remove)
1084 platform_notify_remove(dev);
1085 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1086 cleanup_device_parent(dev);
1087 kobject_del(&dev->kobj);
1092 * device_unregister - unregister device from system.
1093 * @dev: device going away.
1095 * We do this in two parts, like we do device_register(). First,
1096 * we remove it from all the subsystems with device_del(), then
1097 * we decrement the reference count via put_device(). If that
1098 * is the final reference count, the device will be cleaned up
1099 * via device_release() above. Otherwise, the structure will
1100 * stick around until the final reference to the device is dropped.
1102 void device_unregister(struct device *dev)
1104 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1109 static struct device *next_device(struct klist_iter *i)
1111 struct klist_node *n = klist_next(i);
1112 return n ? container_of(n, struct device, knode_parent) : NULL;
1116 * device_for_each_child - device child iterator.
1117 * @parent: parent struct device.
1118 * @data: data for the callback.
1119 * @fn: function to be called for each device.
1121 * Iterate over @parent's child devices, and call @fn for each,
1124 * We check the return of @fn each time. If it returns anything
1125 * other than 0, we break out and return that value.
1127 int device_for_each_child(struct device *parent, void *data,
1128 int (*fn)(struct device *dev, void *data))
1130 struct klist_iter i;
1131 struct device *child;
1134 klist_iter_init(&parent->klist_children, &i);
1135 while ((child = next_device(&i)) && !error)
1136 error = fn(child, data);
1137 klist_iter_exit(&i);
1142 * device_find_child - device iterator for locating a particular device.
1143 * @parent: parent struct device
1144 * @data: Data to pass to match function
1145 * @match: Callback function to check device
1147 * This is similar to the device_for_each_child() function above, but it
1148 * returns a reference to a device that is 'found' for later use, as
1149 * determined by the @match callback.
1151 * The callback should return 0 if the device doesn't match and non-zero
1152 * if it does. If the callback returns non-zero and a reference to the
1153 * current device can be obtained, this function will return to the caller
1154 * and not iterate over any more devices.
1156 struct device *device_find_child(struct device *parent, void *data,
1157 int (*match)(struct device *dev, void *data))
1159 struct klist_iter i;
1160 struct device *child;
1165 klist_iter_init(&parent->klist_children, &i);
1166 while ((child = next_device(&i)))
1167 if (match(child, data) && get_device(child))
1169 klist_iter_exit(&i);
1173 int __init devices_init(void)
1175 devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1178 dev_kobj = kobject_create_and_add("dev", NULL);
1181 sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1182 if (!sysfs_dev_block_kobj)
1183 goto block_kobj_err;
1184 sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1185 if (!sysfs_dev_char_kobj)
1191 kobject_put(sysfs_dev_block_kobj);
1193 kobject_put(dev_kobj);
1195 kset_unregister(devices_kset);
1199 EXPORT_SYMBOL_GPL(device_for_each_child);
1200 EXPORT_SYMBOL_GPL(device_find_child);
1202 EXPORT_SYMBOL_GPL(device_initialize);
1203 EXPORT_SYMBOL_GPL(device_add);
1204 EXPORT_SYMBOL_GPL(device_register);
1206 EXPORT_SYMBOL_GPL(device_del);
1207 EXPORT_SYMBOL_GPL(device_unregister);
1208 EXPORT_SYMBOL_GPL(get_device);
1209 EXPORT_SYMBOL_GPL(put_device);
1211 EXPORT_SYMBOL_GPL(device_create_file);
1212 EXPORT_SYMBOL_GPL(device_remove_file);
1217 struct module *owner;
1220 #define to_root_device(dev) container_of(dev, struct root_device, dev)
1222 static void root_device_release(struct device *dev)
1224 kfree(to_root_device(dev));
1228 * __root_device_register - allocate and register a root device
1229 * @name: root device name
1230 * @owner: owner module of the root device, usually THIS_MODULE
1232 * This function allocates a root device and registers it
1233 * using device_register(). In order to free the returned
1234 * device, use root_device_unregister().
1236 * Root devices are dummy devices which allow other devices
1237 * to be grouped under /sys/devices. Use this function to
1238 * allocate a root device and then use it as the parent of
1239 * any device which should appear under /sys/devices/{name}
1241 * The /sys/devices/{name} directory will also contain a
1242 * 'module' symlink which points to the @owner directory
1245 * Note: You probably want to use root_device_register().
1247 struct device *__root_device_register(const char *name, struct module *owner)
1249 struct root_device *root;
1252 root = kzalloc(sizeof(struct root_device), GFP_KERNEL);
1254 return ERR_PTR(err);
1256 err = dev_set_name(&root->dev, name);
1259 return ERR_PTR(err);
1262 root->dev.release = root_device_release;
1264 err = device_register(&root->dev);
1266 put_device(&root->dev);
1267 return ERR_PTR(err);
1270 #ifdef CONFIG_MODULE /* gotta find a "cleaner" way to do this */
1272 struct module_kobject *mk = &owner->mkobj;
1274 err = sysfs_create_link(&root->dev.kobj, &mk->kobj, "module");
1276 device_unregister(&root->dev);
1277 return ERR_PTR(err);
1279 root->owner = owner;
1285 EXPORT_SYMBOL_GPL(__root_device_register);
1288 * root_device_unregister - unregister and free a root device
1289 * @dev: device going away
1291 * This function unregisters and cleans up a device that was created by
1292 * root_device_register().
1294 void root_device_unregister(struct device *dev)
1296 struct root_device *root = to_root_device(dev);
1299 sysfs_remove_link(&root->dev.kobj, "module");
1301 device_unregister(dev);
1303 EXPORT_SYMBOL_GPL(root_device_unregister);
1306 static void device_create_release(struct device *dev)
1308 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1313 * device_create_vargs - creates a device and registers it with sysfs
1314 * @class: pointer to the struct class that this device should be registered to
1315 * @parent: pointer to the parent struct device of this new device, if any
1316 * @devt: the dev_t for the char device to be added
1317 * @drvdata: the data to be added to the device for callbacks
1318 * @fmt: string for the device's name
1319 * @args: va_list for the device's name
1321 * This function can be used by char device classes. A struct device
1322 * will be created in sysfs, registered to the specified class.
1324 * A "dev" file will be created, showing the dev_t for the device, if
1325 * the dev_t is not 0,0.
1326 * If a pointer to a parent struct device is passed in, the newly created
1327 * struct device will be a child of that device in sysfs.
1328 * The pointer to the struct device will be returned from the call.
1329 * Any further sysfs files that might be required can be created using this
1332 * Note: the struct class passed to this function must have previously
1333 * been created with a call to class_create().
1335 struct device *device_create_vargs(struct class *class, struct device *parent,
1336 dev_t devt, void *drvdata, const char *fmt,
1339 struct device *dev = NULL;
1340 int retval = -ENODEV;
1342 if (class == NULL || IS_ERR(class))
1345 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1353 dev->parent = parent;
1354 dev->release = device_create_release;
1355 dev_set_drvdata(dev, drvdata);
1357 vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
1358 retval = device_register(dev);
1366 return ERR_PTR(retval);
1368 EXPORT_SYMBOL_GPL(device_create_vargs);
1371 * device_create - creates a device and registers it with sysfs
1372 * @class: pointer to the struct class that this device should be registered to
1373 * @parent: pointer to the parent struct device of this new device, if any
1374 * @devt: the dev_t for the char device to be added
1375 * @drvdata: the data to be added to the device for callbacks
1376 * @fmt: string for the device's name
1378 * This function can be used by char device classes. A struct device
1379 * will be created in sysfs, registered to the specified class.
1381 * A "dev" file will be created, showing the dev_t for the device, if
1382 * the dev_t is not 0,0.
1383 * If a pointer to a parent struct device is passed in, the newly created
1384 * struct device will be a child of that device in sysfs.
1385 * The pointer to the struct device will be returned from the call.
1386 * Any further sysfs files that might be required can be created using this
1389 * Note: the struct class passed to this function must have previously
1390 * been created with a call to class_create().
1392 struct device *device_create(struct class *class, struct device *parent,
1393 dev_t devt, void *drvdata, const char *fmt, ...)
1398 va_start(vargs, fmt);
1399 dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
1403 EXPORT_SYMBOL_GPL(device_create);
1405 static int __match_devt(struct device *dev, void *data)
1409 return dev->devt == *devt;
1413 * device_destroy - removes a device that was created with device_create()
1414 * @class: pointer to the struct class that this device was registered with
1415 * @devt: the dev_t of the device that was previously registered
1417 * This call unregisters and cleans up a device that was created with a
1418 * call to device_create().
1420 void device_destroy(struct class *class, dev_t devt)
1424 dev = class_find_device(class, NULL, &devt, __match_devt);
1427 device_unregister(dev);
1430 EXPORT_SYMBOL_GPL(device_destroy);
1433 * device_rename - renames a device
1434 * @dev: the pointer to the struct device to be renamed
1435 * @new_name: the new name of the device
1437 * It is the responsibility of the caller to provide mutual
1438 * exclusion between two different calls of device_rename
1439 * on the same device to ensure that new_name is valid and
1440 * won't conflict with other devices.
1442 int device_rename(struct device *dev, char *new_name)
1444 char *old_class_name = NULL;
1445 char *new_class_name = NULL;
1446 char *old_device_name = NULL;
1449 dev = get_device(dev);
1453 pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
1454 __func__, new_name);
1456 #ifdef CONFIG_SYSFS_DEPRECATED
1457 if ((dev->class) && (dev->parent))
1458 old_class_name = make_class_name(dev->class->name, &dev->kobj);
1461 old_device_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
1462 if (!old_device_name) {
1466 strlcpy(old_device_name, dev->bus_id, BUS_ID_SIZE);
1467 strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
1469 error = kobject_rename(&dev->kobj, new_name);
1471 strlcpy(dev->bus_id, old_device_name, BUS_ID_SIZE);
1475 #ifdef CONFIG_SYSFS_DEPRECATED
1476 if (old_class_name) {
1477 new_class_name = make_class_name(dev->class->name, &dev->kobj);
1478 if (new_class_name) {
1479 error = sysfs_create_link_nowarn(&dev->parent->kobj,
1484 sysfs_remove_link(&dev->parent->kobj, old_class_name);
1489 error = sysfs_create_link_nowarn(&dev->class->p->class_subsys.kobj,
1490 &dev->kobj, dev_name(dev));
1493 sysfs_remove_link(&dev->class->p->class_subsys.kobj,
1501 kfree(new_class_name);
1502 kfree(old_class_name);
1503 kfree(old_device_name);
1507 EXPORT_SYMBOL_GPL(device_rename);
1509 static int device_move_class_links(struct device *dev,
1510 struct device *old_parent,
1511 struct device *new_parent)
1514 #ifdef CONFIG_SYSFS_DEPRECATED
1517 class_name = make_class_name(dev->class->name, &dev->kobj);
1523 sysfs_remove_link(&dev->kobj, "device");
1524 sysfs_remove_link(&old_parent->kobj, class_name);
1527 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1531 error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
1534 sysfs_remove_link(&dev->kobj, "device");
1542 sysfs_remove_link(&dev->kobj, "device");
1544 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1551 * device_move - moves a device to a new parent
1552 * @dev: the pointer to the struct device to be moved
1553 * @new_parent: the new parent of the device (can by NULL)
1555 int device_move(struct device *dev, struct device *new_parent)
1558 struct device *old_parent;
1559 struct kobject *new_parent_kobj;
1561 dev = get_device(dev);
1565 new_parent = get_device(new_parent);
1566 new_parent_kobj = get_device_parent(dev, new_parent);
1568 pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
1569 __func__, new_parent ? dev_name(new_parent) : "<NULL>");
1570 error = kobject_move(&dev->kobj, new_parent_kobj);
1572 cleanup_glue_dir(dev, new_parent_kobj);
1573 put_device(new_parent);
1576 old_parent = dev->parent;
1577 dev->parent = new_parent;
1579 klist_remove(&dev->knode_parent);
1581 klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
1582 set_dev_node(dev, dev_to_node(new_parent));
1587 error = device_move_class_links(dev, old_parent, new_parent);
1589 /* We ignore errors on cleanup since we're hosed anyway... */
1590 device_move_class_links(dev, new_parent, old_parent);
1591 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1593 klist_remove(&dev->knode_parent);
1594 dev->parent = old_parent;
1596 klist_add_tail(&dev->knode_parent,
1597 &old_parent->klist_children);
1598 set_dev_node(dev, dev_to_node(old_parent));
1601 cleanup_glue_dir(dev, new_parent_kobj);
1602 put_device(new_parent);
1606 put_device(old_parent);
1611 EXPORT_SYMBOL_GPL(device_move);
1614 * device_shutdown - call ->shutdown() on each device to shutdown.
1616 void device_shutdown(void)
1618 struct device *dev, *devn;
1620 list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list,
1622 if (dev->bus && dev->bus->shutdown) {
1623 dev_dbg(dev, "shutdown\n");
1624 dev->bus->shutdown(dev);
1625 } else if (dev->driver && dev->driver->shutdown) {
1626 dev_dbg(dev, "shutdown\n");
1627 dev->driver->shutdown(dev);
1630 kobject_put(sysfs_dev_char_kobj);
1631 kobject_put(sysfs_dev_block_kobj);
1632 kobject_put(dev_kobj);