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>
22 #include <asm/semaphore.h>
25 #include "power/power.h"
27 extern const char *kobject_actions[];
29 int (*platform_notify)(struct device * dev) = NULL;
30 int (*platform_notify_remove)(struct device * dev) = NULL;
33 * sysfs bindings for devices.
37 * dev_driver_string - Return a device's driver name, if at all possible
38 * @dev: struct device to get the name of
40 * Will return the device's driver's name if it is bound to a device. If
41 * the device is not bound to a device, it will return the name of the bus
42 * it is attached to. If it is not attached to a bus either, an empty
43 * string will be returned.
45 const char *dev_driver_string(struct device *dev)
47 return dev->driver ? dev->driver->name :
48 (dev->bus ? dev->bus->name :
49 (dev->class ? dev->class->name : ""));
51 EXPORT_SYMBOL(dev_driver_string);
53 #define to_dev(obj) container_of(obj, struct device, kobj)
54 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
57 dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
59 struct device_attribute * dev_attr = to_dev_attr(attr);
60 struct device * dev = to_dev(kobj);
64 ret = dev_attr->show(dev, dev_attr, buf);
69 dev_attr_store(struct kobject * kobj, struct attribute * attr,
70 const char * buf, size_t count)
72 struct device_attribute * dev_attr = to_dev_attr(attr);
73 struct device * dev = to_dev(kobj);
77 ret = dev_attr->store(dev, dev_attr, buf, count);
81 static struct sysfs_ops dev_sysfs_ops = {
82 .show = dev_attr_show,
83 .store = dev_attr_store,
88 * device_release - free device structure.
89 * @kobj: device's kobject.
91 * This is called once the reference count for the object
92 * reaches 0. We forward the call to the device's release
93 * method, which should handle actually freeing the structure.
95 static void device_release(struct kobject * kobj)
97 struct device * dev = to_dev(kobj);
101 else if (dev->type && dev->type->release)
102 dev->type->release(dev);
103 else if (dev->class && dev->class->dev_release)
104 dev->class->dev_release(dev);
106 printk(KERN_ERR "Device '%s' does not have a release() function, "
107 "it is broken and must be fixed.\n",
113 static struct kobj_type ktype_device = {
114 .release = device_release,
115 .sysfs_ops = &dev_sysfs_ops,
119 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
121 struct kobj_type *ktype = get_ktype(kobj);
123 if (ktype == &ktype_device) {
124 struct device *dev = to_dev(kobj);
125 if (dev->uevent_suppress)
135 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
137 struct device *dev = to_dev(kobj);
140 return dev->bus->name;
142 return dev->class->name;
146 static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
147 int num_envp, char *buffer, int buffer_size)
149 struct device *dev = to_dev(kobj);
154 /* add the major/minor if present */
155 if (MAJOR(dev->devt)) {
156 add_uevent_var(envp, num_envp, &i,
157 buffer, buffer_size, &length,
158 "MAJOR=%u", MAJOR(dev->devt));
159 add_uevent_var(envp, num_envp, &i,
160 buffer, buffer_size, &length,
161 "MINOR=%u", MINOR(dev->devt));
164 if (dev->type && dev->type->name)
165 add_uevent_var(envp, num_envp, &i,
166 buffer, buffer_size, &length,
167 "DEVTYPE=%s", dev->type->name);
170 add_uevent_var(envp, num_envp, &i,
171 buffer, buffer_size, &length,
172 "DRIVER=%s", dev->driver->name);
174 #ifdef CONFIG_SYSFS_DEPRECATED
176 struct device *parent = dev->parent;
178 /* find first bus device in parent chain */
179 while (parent && !parent->bus)
180 parent = parent->parent;
181 if (parent && parent->bus) {
184 path = kobject_get_path(&parent->kobj, GFP_KERNEL);
186 add_uevent_var(envp, num_envp, &i,
187 buffer, buffer_size, &length,
188 "PHYSDEVPATH=%s", path);
192 add_uevent_var(envp, num_envp, &i,
193 buffer, buffer_size, &length,
194 "PHYSDEVBUS=%s", parent->bus->name);
197 add_uevent_var(envp, num_envp, &i,
198 buffer, buffer_size, &length,
199 "PHYSDEVDRIVER=%s", parent->driver->name);
201 } else if (dev->bus) {
202 add_uevent_var(envp, num_envp, &i,
203 buffer, buffer_size, &length,
204 "PHYSDEVBUS=%s", dev->bus->name);
207 add_uevent_var(envp, num_envp, &i,
208 buffer, buffer_size, &length,
209 "PHYSDEVDRIVER=%s", dev->driver->name);
213 /* terminate, set to next free slot, shrink available space */
217 buffer = &buffer[length];
218 buffer_size -= length;
220 if (dev->bus && dev->bus->uevent) {
221 /* have the bus specific function add its stuff */
222 retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size);
224 pr_debug ("%s: bus uevent() returned %d\n",
225 __FUNCTION__, retval);
228 if (dev->class && dev->class->dev_uevent) {
229 /* have the class specific function add its stuff */
230 retval = dev->class->dev_uevent(dev, envp, num_envp, buffer, buffer_size);
232 pr_debug("%s: class uevent() returned %d\n",
233 __FUNCTION__, retval);
236 if (dev->type && dev->type->uevent) {
237 /* have the device type specific fuction add its stuff */
238 retval = dev->type->uevent(dev, envp, num_envp, buffer, buffer_size);
240 pr_debug("%s: dev_type uevent() returned %d\n",
241 __FUNCTION__, retval);
247 static struct kset_uevent_ops device_uevent_ops = {
248 .filter = dev_uevent_filter,
249 .name = dev_uevent_name,
250 .uevent = dev_uevent,
253 static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
256 struct kobject *top_kobj;
265 /* search the kset, the device belongs to */
266 top_kobj = &dev->kobj;
267 if (!top_kobj->kset && top_kobj->parent) {
269 top_kobj = top_kobj->parent;
270 } while (!top_kobj->kset && top_kobj->parent);
274 kset = top_kobj->kset;
275 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
279 if (kset->uevent_ops && kset->uevent_ops->filter)
280 if (!kset->uevent_ops->filter(kset, &dev->kobj))
283 data = (char *)get_zeroed_page(GFP_KERNEL);
287 /* let the kset specific function add its keys */
289 retval = kset->uevent_ops->uevent(kset, &dev->kobj,
290 envp, ARRAY_SIZE(envp),
295 /* copy keys to file */
296 for (i = 0; envp[i]; i++) {
298 count += sprintf(pos, "%s\n", envp[i]);
301 free_page((unsigned long)data);
305 static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
306 const char *buf, size_t count)
309 enum kobject_action action;
311 if (len && buf[len-1] == '\n')
314 for (action = 0; action < KOBJ_MAX; action++) {
315 if (strncmp(kobject_actions[action], buf, len) != 0)
317 if (kobject_actions[action][len] != '\0')
319 kobject_uevent(&dev->kobj, action);
323 dev_err(dev, "uevent: unsupported action-string; this will "
324 "be ignored in a future kernel version\n");
325 kobject_uevent(&dev->kobj, KOBJ_ADD);
330 static struct device_attribute uevent_attr =
331 __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);
333 static int device_add_attributes(struct device *dev,
334 struct device_attribute *attrs)
340 for (i = 0; attr_name(attrs[i]); i++) {
341 error = device_create_file(dev, &attrs[i]);
347 device_remove_file(dev, &attrs[i]);
352 static void device_remove_attributes(struct device *dev,
353 struct device_attribute *attrs)
358 for (i = 0; attr_name(attrs[i]); i++)
359 device_remove_file(dev, &attrs[i]);
362 static int device_add_groups(struct device *dev,
363 struct attribute_group **groups)
369 for (i = 0; groups[i]; i++) {
370 error = sysfs_create_group(&dev->kobj, groups[i]);
373 sysfs_remove_group(&dev->kobj, groups[i]);
381 static void device_remove_groups(struct device *dev,
382 struct attribute_group **groups)
387 for (i = 0; groups[i]; i++)
388 sysfs_remove_group(&dev->kobj, groups[i]);
391 static int device_add_attrs(struct device *dev)
393 struct class *class = dev->class;
394 struct device_type *type = dev->type;
398 error = device_add_attributes(dev, class->dev_attrs);
404 error = device_add_groups(dev, type->groups);
406 goto err_remove_class_attrs;
409 error = device_add_groups(dev, dev->groups);
411 goto err_remove_type_groups;
415 err_remove_type_groups:
417 device_remove_groups(dev, type->groups);
418 err_remove_class_attrs:
420 device_remove_attributes(dev, class->dev_attrs);
425 static void device_remove_attrs(struct device *dev)
427 struct class *class = dev->class;
428 struct device_type *type = dev->type;
430 device_remove_groups(dev, dev->groups);
433 device_remove_groups(dev, type->groups);
436 device_remove_attributes(dev, class->dev_attrs);
440 static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
443 return print_dev_t(buf, dev->devt);
446 static struct device_attribute devt_attr =
447 __ATTR(dev, S_IRUGO, show_dev, NULL);
450 * devices_subsys - structure to be registered with kobject core.
453 decl_subsys(devices, &ktype_device, &device_uevent_ops);
457 * device_create_file - create sysfs attribute file for device.
459 * @attr: device attribute descriptor.
462 int device_create_file(struct device * dev, struct device_attribute * attr)
465 if (get_device(dev)) {
466 error = sysfs_create_file(&dev->kobj, &attr->attr);
473 * device_remove_file - remove sysfs attribute file.
475 * @attr: device attribute descriptor.
478 void device_remove_file(struct device * dev, struct device_attribute * attr)
480 if (get_device(dev)) {
481 sysfs_remove_file(&dev->kobj, &attr->attr);
487 * device_create_bin_file - create sysfs binary attribute file for device.
489 * @attr: device binary attribute descriptor.
491 int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
495 error = sysfs_create_bin_file(&dev->kobj, attr);
498 EXPORT_SYMBOL_GPL(device_create_bin_file);
501 * device_remove_bin_file - remove sysfs binary attribute file
503 * @attr: device binary attribute descriptor.
505 void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
508 sysfs_remove_bin_file(&dev->kobj, attr);
510 EXPORT_SYMBOL_GPL(device_remove_bin_file);
513 * device_schedule_callback_owner - helper to schedule a callback for a device
515 * @func: callback function to invoke later.
516 * @owner: module owning the callback routine
518 * Attribute methods must not unregister themselves or their parent device
519 * (which would amount to the same thing). Attempts to do so will deadlock,
520 * since unregistration is mutually exclusive with driver callbacks.
522 * Instead methods can call this routine, which will attempt to allocate
523 * and schedule a workqueue request to call back @func with @dev as its
524 * argument in the workqueue's process context. @dev will be pinned until
527 * This routine is usually called via the inline device_schedule_callback(),
528 * which automatically sets @owner to THIS_MODULE.
530 * Returns 0 if the request was submitted, -ENOMEM if storage could not
531 * be allocated, -ENODEV if a reference to @owner isn't available.
533 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
534 * underlying sysfs routine (since it is intended for use by attribute
535 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
537 int device_schedule_callback_owner(struct device *dev,
538 void (*func)(struct device *), struct module *owner)
540 return sysfs_schedule_callback(&dev->kobj,
541 (void (*)(void *)) func, dev, owner);
543 EXPORT_SYMBOL_GPL(device_schedule_callback_owner);
545 static void klist_children_get(struct klist_node *n)
547 struct device *dev = container_of(n, struct device, knode_parent);
552 static void klist_children_put(struct klist_node *n)
554 struct device *dev = container_of(n, struct device, knode_parent);
561 * device_initialize - init device structure.
564 * This prepares the device for use by other layers,
565 * including adding it to the device hierarchy.
566 * It is the first half of device_register(), if called by
567 * that, though it can also be called separately, so one
568 * may use @dev's fields (e.g. the refcount).
571 void device_initialize(struct device *dev)
573 kobj_set_kset_s(dev, devices_subsys);
574 kobject_init(&dev->kobj);
575 klist_init(&dev->klist_children, klist_children_get,
577 INIT_LIST_HEAD(&dev->dma_pools);
578 INIT_LIST_HEAD(&dev->node);
579 init_MUTEX(&dev->sem);
580 spin_lock_init(&dev->devres_lock);
581 INIT_LIST_HEAD(&dev->devres_head);
582 device_init_wakeup(dev, 0);
583 set_dev_node(dev, -1);
586 #ifdef CONFIG_SYSFS_DEPRECATED
587 static struct kobject * get_device_parent(struct device *dev,
588 struct device *parent)
590 /* Set the parent to the class, not the parent device */
591 /* this keeps sysfs from having a symlink to make old udevs happy */
593 return &dev->class->subsys.kobj;
595 return &parent->kobj;
600 static struct kobject *virtual_device_parent(struct device *dev)
602 static struct kobject *virtual_dir = NULL;
605 virtual_dir = kobject_add_dir(&devices_subsys.kobj, "virtual");
610 static struct kobject * get_device_parent(struct device *dev,
611 struct device *parent)
614 struct kobject *kobj = NULL;
615 struct kobject *parent_kobj;
619 * If we have no parent, we live in "virtual".
620 * Class-devices with a bus-device as parent, live
621 * in a class-directory to prevent namespace collisions.
624 parent_kobj = virtual_device_parent(dev);
625 else if (parent->class)
626 return &parent->kobj;
628 parent_kobj = &parent->kobj;
630 /* find our class-directory at the parent and reference it */
631 spin_lock(&dev->class->class_dirs.list_lock);
632 list_for_each_entry(k, &dev->class->class_dirs.list, entry)
633 if (k->parent == parent_kobj) {
634 kobj = kobject_get(k);
637 spin_unlock(&dev->class->class_dirs.list_lock);
641 /* or create a new class-directory at the parent device */
642 return kobject_kset_add_dir(&dev->class->class_dirs,
643 parent_kobj, dev->class->name);
647 return &parent->kobj;
652 static int setup_parent(struct device *dev, struct device *parent)
654 struct kobject *kobj;
655 kobj = get_device_parent(dev, parent);
657 return PTR_ERR(kobj);
659 dev->kobj.parent = kobj;
663 static int device_add_class_symlinks(struct device *dev)
669 error = sysfs_create_link(&dev->kobj, &dev->class->subsys.kobj,
674 * If this is not a "fake" compatible device, then create the
675 * symlink from the class to the device.
677 if (dev->kobj.parent != &dev->class->subsys.kobj) {
678 error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj,
683 /* only bus-device parents get a "device"-link */
684 if (dev->parent && dev->parent->bus) {
685 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
689 #ifdef CONFIG_SYSFS_DEPRECATED
691 char * class_name = make_class_name(dev->class->name,
694 error = sysfs_create_link(&dev->parent->kobj,
695 &dev->kobj, class_name);
704 #ifdef CONFIG_SYSFS_DEPRECATED
707 sysfs_remove_link(&dev->kobj, "device");
710 if (dev->kobj.parent != &dev->class->subsys.kobj)
711 sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
713 sysfs_remove_link(&dev->kobj, "subsystem");
718 static void device_remove_class_symlinks(struct device *dev)
723 #ifdef CONFIG_SYSFS_DEPRECATED
726 class_name = make_class_name(dev->class->name, &dev->kobj);
728 sysfs_remove_link(&dev->parent->kobj, class_name);
732 sysfs_remove_link(&dev->kobj, "device");
734 if (dev->kobj.parent != &dev->class->subsys.kobj)
735 sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
736 sysfs_remove_link(&dev->kobj, "subsystem");
740 * device_add - add device to device hierarchy.
743 * This is part 2 of device_register(), though may be called
744 * separately _iff_ device_initialize() has been called separately.
746 * This adds it to the kobject hierarchy via kobject_add(), adds it
747 * to the global and sibling lists for the device, then
748 * adds it to the other relevant subsystems of the driver model.
750 int device_add(struct device *dev)
752 struct device *parent = NULL;
753 struct class_interface *class_intf;
756 dev = get_device(dev);
757 if (!dev || !strlen(dev->bus_id))
760 pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
762 parent = get_device(dev->parent);
763 error = setup_parent(dev, parent);
767 /* first, register with generic layer. */
768 kobject_set_name(&dev->kobj, "%s", dev->bus_id);
769 error = kobject_add(&dev->kobj);
773 /* notify platform of device entry */
775 platform_notify(dev);
777 /* notify clients of device entry (new way) */
779 blocking_notifier_call_chain(&dev->bus->bus_notifier,
780 BUS_NOTIFY_ADD_DEVICE, dev);
782 error = device_create_file(dev, &uevent_attr);
786 if (MAJOR(dev->devt)) {
787 error = device_create_file(dev, &devt_attr);
789 goto ueventattrError;
792 error = device_add_class_symlinks(dev);
795 error = device_add_attrs(dev);
798 error = device_pm_add(dev);
801 error = bus_add_device(dev);
804 kobject_uevent(&dev->kobj, KOBJ_ADD);
805 bus_attach_device(dev);
807 klist_add_tail(&dev->knode_parent, &parent->klist_children);
810 down(&dev->class->sem);
811 /* tie the class to the device */
812 list_add_tail(&dev->node, &dev->class->devices);
814 /* notify any interfaces that the device is here */
815 list_for_each_entry(class_intf, &dev->class->interfaces, node)
816 if (class_intf->add_dev)
817 class_intf->add_dev(dev, class_intf);
818 up(&dev->class->sem);
824 device_pm_remove(dev);
827 blocking_notifier_call_chain(&dev->bus->bus_notifier,
828 BUS_NOTIFY_DEL_DEVICE, dev);
829 device_remove_attrs(dev);
831 device_remove_class_symlinks(dev);
833 if (MAJOR(dev->devt))
834 device_remove_file(dev, &devt_attr);
837 sysfs_remove_link(&dev->kobj, "subsystem");
838 /* If this is not a "fake" compatible device, remove the
839 * symlink from the class to the device. */
840 if (dev->kobj.parent != &dev->class->subsys.kobj)
841 sysfs_remove_link(&dev->class->subsys.kobj,
844 #ifdef CONFIG_SYSFS_DEPRECATED
845 char *class_name = make_class_name(dev->class->name,
848 sysfs_remove_link(&dev->parent->kobj,
852 sysfs_remove_link(&dev->kobj, "device");
856 device_remove_file(dev, &uevent_attr);
858 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
859 kobject_del(&dev->kobj);
868 * device_register - register a device with the system.
869 * @dev: pointer to the device structure
871 * This happens in two clean steps - initialize the device
872 * and add it to the system. The two steps can be called
873 * separately, but this is the easiest and most common.
874 * I.e. you should only call the two helpers separately if
875 * have a clearly defined need to use and refcount the device
876 * before it is added to the hierarchy.
879 int device_register(struct device *dev)
881 device_initialize(dev);
882 return device_add(dev);
887 * get_device - increment reference count for device.
890 * This simply forwards the call to kobject_get(), though
891 * we do take care to provide for the case that we get a NULL
895 struct device * get_device(struct device * dev)
897 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
902 * put_device - decrement reference count.
903 * @dev: device in question.
905 void put_device(struct device * dev)
908 kobject_put(&dev->kobj);
913 * device_del - delete device from system.
916 * This is the first part of the device unregistration
917 * sequence. This removes the device from the lists we control
918 * from here, has it removed from the other driver model
919 * subsystems it was added to in device_add(), and removes it
920 * from the kobject hierarchy.
922 * NOTE: this should be called manually _iff_ device_add() was
923 * also called manually.
926 void device_del(struct device * dev)
928 struct device * parent = dev->parent;
929 struct class_interface *class_intf;
932 klist_del(&dev->knode_parent);
933 if (MAJOR(dev->devt))
934 device_remove_file(dev, &devt_attr);
936 sysfs_remove_link(&dev->kobj, "subsystem");
937 /* If this is not a "fake" compatible device, remove the
938 * symlink from the class to the device. */
939 if (dev->kobj.parent != &dev->class->subsys.kobj)
940 sysfs_remove_link(&dev->class->subsys.kobj,
943 #ifdef CONFIG_SYSFS_DEPRECATED
944 char *class_name = make_class_name(dev->class->name,
947 sysfs_remove_link(&dev->parent->kobj,
951 sysfs_remove_link(&dev->kobj, "device");
954 down(&dev->class->sem);
955 /* notify any interfaces that the device is now gone */
956 list_for_each_entry(class_intf, &dev->class->interfaces, node)
957 if (class_intf->remove_dev)
958 class_intf->remove_dev(dev, class_intf);
959 /* remove the device from the class list */
960 list_del_init(&dev->node);
961 up(&dev->class->sem);
963 /* If we live in a parent class-directory, unreference it */
964 if (dev->kobj.parent->kset == &dev->class->class_dirs) {
969 * if we are the last child of our class, delete
970 * our class-directory at this parent
972 down(&dev->class->sem);
973 list_for_each_entry(d, &dev->class->devices, node) {
976 if (d->kobj.parent == dev->kobj.parent) {
982 kobject_del(dev->kobj.parent);
984 kobject_put(dev->kobj.parent);
985 up(&dev->class->sem);
988 device_remove_file(dev, &uevent_attr);
989 device_remove_attrs(dev);
990 bus_remove_device(dev);
993 * Some platform devices are driven without driver attached
994 * and managed resources may have been acquired. Make sure
995 * all resources are released.
997 devres_release_all(dev);
999 /* Notify the platform of the removal, in case they
1000 * need to do anything...
1002 if (platform_notify_remove)
1003 platform_notify_remove(dev);
1005 blocking_notifier_call_chain(&dev->bus->bus_notifier,
1006 BUS_NOTIFY_DEL_DEVICE, dev);
1007 device_pm_remove(dev);
1008 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1009 kobject_del(&dev->kobj);
1015 * device_unregister - unregister device from system.
1016 * @dev: device going away.
1018 * We do this in two parts, like we do device_register(). First,
1019 * we remove it from all the subsystems with device_del(), then
1020 * we decrement the reference count via put_device(). If that
1021 * is the final reference count, the device will be cleaned up
1022 * via device_release() above. Otherwise, the structure will
1023 * stick around until the final reference to the device is dropped.
1025 void device_unregister(struct device * dev)
1027 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
1033 static struct device * next_device(struct klist_iter * i)
1035 struct klist_node * n = klist_next(i);
1036 return n ? container_of(n, struct device, knode_parent) : NULL;
1040 * device_for_each_child - device child iterator.
1041 * @parent: parent struct device.
1042 * @data: data for the callback.
1043 * @fn: function to be called for each device.
1045 * Iterate over @parent's child devices, and call @fn for each,
1048 * We check the return of @fn each time. If it returns anything
1049 * other than 0, we break out and return that value.
1051 int device_for_each_child(struct device * parent, void * data,
1052 int (*fn)(struct device *, void *))
1054 struct klist_iter i;
1055 struct device * child;
1058 klist_iter_init(&parent->klist_children, &i);
1059 while ((child = next_device(&i)) && !error)
1060 error = fn(child, data);
1061 klist_iter_exit(&i);
1066 * device_find_child - device iterator for locating a particular device.
1067 * @parent: parent struct device
1068 * @data: Data to pass to match function
1069 * @match: Callback function to check device
1071 * This is similar to the device_for_each_child() function above, but it
1072 * returns a reference to a device that is 'found' for later use, as
1073 * determined by the @match callback.
1075 * The callback should return 0 if the device doesn't match and non-zero
1076 * if it does. If the callback returns non-zero and a reference to the
1077 * current device can be obtained, this function will return to the caller
1078 * and not iterate over any more devices.
1080 struct device * device_find_child(struct device *parent, void *data,
1081 int (*match)(struct device *, void *))
1083 struct klist_iter i;
1084 struct device *child;
1089 klist_iter_init(&parent->klist_children, &i);
1090 while ((child = next_device(&i)))
1091 if (match(child, data) && get_device(child))
1093 klist_iter_exit(&i);
1097 int __init devices_init(void)
1099 return subsystem_register(&devices_subsys);
1102 EXPORT_SYMBOL_GPL(device_for_each_child);
1103 EXPORT_SYMBOL_GPL(device_find_child);
1105 EXPORT_SYMBOL_GPL(device_initialize);
1106 EXPORT_SYMBOL_GPL(device_add);
1107 EXPORT_SYMBOL_GPL(device_register);
1109 EXPORT_SYMBOL_GPL(device_del);
1110 EXPORT_SYMBOL_GPL(device_unregister);
1111 EXPORT_SYMBOL_GPL(get_device);
1112 EXPORT_SYMBOL_GPL(put_device);
1114 EXPORT_SYMBOL_GPL(device_create_file);
1115 EXPORT_SYMBOL_GPL(device_remove_file);
1118 static void device_create_release(struct device *dev)
1120 pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
1125 * device_create - creates a device and registers it with sysfs
1126 * @class: pointer to the struct class that this device should be registered to
1127 * @parent: pointer to the parent struct device of this new device, if any
1128 * @devt: the dev_t for the char device to be added
1129 * @fmt: string for the device's name
1131 * This function can be used by char device classes. A struct device
1132 * will be created in sysfs, registered to the specified class.
1134 * A "dev" file will be created, showing the dev_t for the device, if
1135 * the dev_t is not 0,0.
1136 * If a pointer to a parent struct device is passed in, the newly created
1137 * struct device will be a child of that device in sysfs.
1138 * The pointer to the struct device will be returned from the call.
1139 * Any further sysfs files that might be required can be created using this
1142 * Note: the struct class passed to this function must have previously
1143 * been created with a call to class_create().
1145 struct device *device_create(struct class *class, struct device *parent,
1146 dev_t devt, const char *fmt, ...)
1149 struct device *dev = NULL;
1150 int retval = -ENODEV;
1152 if (class == NULL || IS_ERR(class))
1155 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1163 dev->parent = parent;
1164 dev->release = device_create_release;
1166 va_start(args, fmt);
1167 vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
1169 retval = device_register(dev);
1177 return ERR_PTR(retval);
1179 EXPORT_SYMBOL_GPL(device_create);
1182 * device_destroy - removes a device that was created with device_create()
1183 * @class: pointer to the struct class that this device was registered with
1184 * @devt: the dev_t of the device that was previously registered
1186 * This call unregisters and cleans up a device that was created with a
1187 * call to device_create().
1189 void device_destroy(struct class *class, dev_t devt)
1191 struct device *dev = NULL;
1192 struct device *dev_tmp;
1195 list_for_each_entry(dev_tmp, &class->devices, node) {
1196 if (dev_tmp->devt == devt) {
1204 device_unregister(dev);
1206 EXPORT_SYMBOL_GPL(device_destroy);
1209 * device_rename - renames a device
1210 * @dev: the pointer to the struct device to be renamed
1211 * @new_name: the new name of the device
1213 int device_rename(struct device *dev, char *new_name)
1215 char *old_class_name = NULL;
1216 char *new_class_name = NULL;
1217 char *old_device_name = NULL;
1220 dev = get_device(dev);
1224 pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name);
1226 #ifdef CONFIG_SYSFS_DEPRECATED
1227 if ((dev->class) && (dev->parent))
1228 old_class_name = make_class_name(dev->class->name, &dev->kobj);
1231 old_device_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
1232 if (!old_device_name) {
1236 strlcpy(old_device_name, dev->bus_id, BUS_ID_SIZE);
1237 strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
1239 error = kobject_rename(&dev->kobj, new_name);
1241 strlcpy(dev->bus_id, old_device_name, BUS_ID_SIZE);
1245 #ifdef CONFIG_SYSFS_DEPRECATED
1246 if (old_class_name) {
1247 new_class_name = make_class_name(dev->class->name, &dev->kobj);
1248 if (new_class_name) {
1249 error = sysfs_create_link(&dev->parent->kobj,
1250 &dev->kobj, new_class_name);
1253 sysfs_remove_link(&dev->parent->kobj, old_class_name);
1259 sysfs_remove_link(&dev->class->subsys.kobj, old_device_name);
1260 error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj,
1263 /* Uh... how to unravel this if restoring can fail? */
1264 dev_err(dev, "%s: sysfs_create_symlink failed (%d)\n",
1265 __FUNCTION__, error);
1271 kfree(new_class_name);
1272 kfree(old_class_name);
1273 kfree(old_device_name);
1277 EXPORT_SYMBOL_GPL(device_rename);
1279 static int device_move_class_links(struct device *dev,
1280 struct device *old_parent,
1281 struct device *new_parent)
1284 #ifdef CONFIG_SYSFS_DEPRECATED
1287 class_name = make_class_name(dev->class->name, &dev->kobj);
1293 sysfs_remove_link(&dev->kobj, "device");
1294 sysfs_remove_link(&old_parent->kobj, class_name);
1297 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1301 error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
1304 sysfs_remove_link(&dev->kobj, "device");
1313 sysfs_remove_link(&dev->kobj, "device");
1315 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1322 * device_move - moves a device to a new parent
1323 * @dev: the pointer to the struct device to be moved
1324 * @new_parent: the new parent of the device (can by NULL)
1326 int device_move(struct device *dev, struct device *new_parent)
1329 struct device *old_parent;
1330 struct kobject *new_parent_kobj;
1332 dev = get_device(dev);
1336 new_parent = get_device(new_parent);
1337 new_parent_kobj = get_device_parent (dev, new_parent);
1338 if (IS_ERR(new_parent_kobj)) {
1339 error = PTR_ERR(new_parent_kobj);
1340 put_device(new_parent);
1343 pr_debug("DEVICE: moving '%s' to '%s'\n", dev->bus_id,
1344 new_parent ? new_parent->bus_id : "<NULL>");
1345 error = kobject_move(&dev->kobj, new_parent_kobj);
1347 put_device(new_parent);
1350 old_parent = dev->parent;
1351 dev->parent = new_parent;
1353 klist_remove(&dev->knode_parent);
1355 klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
1358 error = device_move_class_links(dev, old_parent, new_parent);
1360 /* We ignore errors on cleanup since we're hosed anyway... */
1361 device_move_class_links(dev, new_parent, old_parent);
1362 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1364 klist_remove(&dev->knode_parent);
1366 klist_add_tail(&dev->knode_parent,
1367 &old_parent->klist_children);
1369 put_device(new_parent);
1373 put_device(old_parent);
1379 EXPORT_SYMBOL_GPL(device_move);