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 int (*platform_notify
)(struct device
* dev
) = NULL
;
28 int (*platform_notify_remove
)(struct device
* dev
) = NULL
;
31 * sysfs bindings for devices.
35 * dev_driver_string - Return a device's driver name, if at all possible
36 * @dev: struct device to get the name of
38 * Will return the device's driver's name if it is bound to a device. If
39 * the device is not bound to a device, it will return the name of the bus
40 * it is attached to. If it is not attached to a bus either, an empty
41 * string will be returned.
43 const char *dev_driver_string(struct device
*dev
)
45 return dev
->driver
? dev
->driver
->name
:
46 (dev
->bus
? dev
->bus
->name
:
47 (dev
->class ? dev
->class->name
: ""));
49 EXPORT_SYMBOL(dev_driver_string
);
51 #define to_dev(obj) container_of(obj, struct device, kobj)
52 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
55 dev_attr_show(struct kobject
* kobj
, struct attribute
* attr
, char * buf
)
57 struct device_attribute
* dev_attr
= to_dev_attr(attr
);
58 struct device
* dev
= to_dev(kobj
);
62 ret
= dev_attr
->show(dev
, dev_attr
, buf
);
67 dev_attr_store(struct kobject
* kobj
, struct attribute
* attr
,
68 const char * buf
, size_t count
)
70 struct device_attribute
* dev_attr
= to_dev_attr(attr
);
71 struct device
* dev
= to_dev(kobj
);
75 ret
= dev_attr
->store(dev
, dev_attr
, buf
, count
);
79 static struct sysfs_ops dev_sysfs_ops
= {
80 .show
= dev_attr_show
,
81 .store
= dev_attr_store
,
86 * device_release - free device structure.
87 * @kobj: device's kobject.
89 * This is called once the reference count for the object
90 * reaches 0. We forward the call to the device's release
91 * method, which should handle actually freeing the structure.
93 static void device_release(struct kobject
* kobj
)
95 struct device
* dev
= to_dev(kobj
);
99 else if (dev
->type
&& dev
->type
->release
)
100 dev
->type
->release(dev
);
101 else if (dev
->class && dev
->class->dev_release
)
102 dev
->class->dev_release(dev
);
104 printk(KERN_ERR
"Device '%s' does not have a release() function, "
105 "it is broken and must be fixed.\n",
111 static struct kobj_type ktype_device
= {
112 .release
= device_release
,
113 .sysfs_ops
= &dev_sysfs_ops
,
117 static int dev_uevent_filter(struct kset
*kset
, struct kobject
*kobj
)
119 struct kobj_type
*ktype
= get_ktype(kobj
);
121 if (ktype
== &ktype_device
) {
122 struct device
*dev
= to_dev(kobj
);
131 static const char *dev_uevent_name(struct kset
*kset
, struct kobject
*kobj
)
133 struct device
*dev
= to_dev(kobj
);
136 return dev
->bus
->name
;
138 return dev
->class->name
;
142 static int dev_uevent(struct kset
*kset
, struct kobject
*kobj
, char **envp
,
143 int num_envp
, char *buffer
, int buffer_size
)
145 struct device
*dev
= to_dev(kobj
);
150 /* add the major/minor if present */
151 if (MAJOR(dev
->devt
)) {
152 add_uevent_var(envp
, num_envp
, &i
,
153 buffer
, buffer_size
, &length
,
154 "MAJOR=%u", MAJOR(dev
->devt
));
155 add_uevent_var(envp
, num_envp
, &i
,
156 buffer
, buffer_size
, &length
,
157 "MINOR=%u", MINOR(dev
->devt
));
161 add_uevent_var(envp
, num_envp
, &i
,
162 buffer
, buffer_size
, &length
,
163 "DRIVER=%s", dev
->driver
->name
);
165 #ifdef CONFIG_SYSFS_DEPRECATED
167 struct device
*parent
= dev
->parent
;
169 /* find first bus device in parent chain */
170 while (parent
&& !parent
->bus
)
171 parent
= parent
->parent
;
172 if (parent
&& parent
->bus
) {
175 path
= kobject_get_path(&parent
->kobj
, GFP_KERNEL
);
176 add_uevent_var(envp
, num_envp
, &i
,
177 buffer
, buffer_size
, &length
,
178 "PHYSDEVPATH=%s", path
);
181 add_uevent_var(envp
, num_envp
, &i
,
182 buffer
, buffer_size
, &length
,
183 "PHYSDEVBUS=%s", parent
->bus
->name
);
186 add_uevent_var(envp
, num_envp
, &i
,
187 buffer
, buffer_size
, &length
,
188 "PHYSDEVDRIVER=%s", parent
->driver
->name
);
190 } else if (dev
->bus
) {
191 add_uevent_var(envp
, num_envp
, &i
,
192 buffer
, buffer_size
, &length
,
193 "PHYSDEVBUS=%s", dev
->bus
->name
);
196 add_uevent_var(envp
, num_envp
, &i
,
197 buffer
, buffer_size
, &length
,
198 "PHYSDEVDRIVER=%s", dev
->driver
->name
);
202 /* terminate, set to next free slot, shrink available space */
206 buffer
= &buffer
[length
];
207 buffer_size
-= length
;
209 if (dev
->bus
&& dev
->bus
->uevent
) {
210 /* have the bus specific function add its stuff */
211 retval
= dev
->bus
->uevent(dev
, envp
, num_envp
, buffer
, buffer_size
);
213 pr_debug ("%s: bus uevent() returned %d\n",
214 __FUNCTION__
, retval
);
217 if (dev
->class && dev
->class->dev_uevent
) {
218 /* have the class specific function add its stuff */
219 retval
= dev
->class->dev_uevent(dev
, envp
, num_envp
, buffer
, buffer_size
);
221 pr_debug("%s: class uevent() returned %d\n",
222 __FUNCTION__
, retval
);
225 if (dev
->type
&& dev
->type
->uevent
) {
226 /* have the device type specific fuction add its stuff */
227 retval
= dev
->type
->uevent(dev
, envp
, num_envp
, buffer
, buffer_size
);
229 pr_debug("%s: dev_type uevent() returned %d\n",
230 __FUNCTION__
, retval
);
236 static struct kset_uevent_ops device_uevent_ops
= {
237 .filter
= dev_uevent_filter
,
238 .name
= dev_uevent_name
,
239 .uevent
= dev_uevent
,
242 static ssize_t
store_uevent(struct device
*dev
, struct device_attribute
*attr
,
243 const char *buf
, size_t count
)
245 kobject_uevent(&dev
->kobj
, KOBJ_ADD
);
249 static int device_add_attributes(struct device
*dev
,
250 struct device_attribute
*attrs
)
256 for (i
= 0; attr_name(attrs
[i
]); i
++) {
257 error
= device_create_file(dev
, &attrs
[i
]);
263 device_remove_file(dev
, &attrs
[i
]);
268 static void device_remove_attributes(struct device
*dev
,
269 struct device_attribute
*attrs
)
274 for (i
= 0; attr_name(attrs
[i
]); i
++)
275 device_remove_file(dev
, &attrs
[i
]);
278 static int device_add_groups(struct device
*dev
,
279 struct attribute_group
**groups
)
285 for (i
= 0; groups
[i
]; i
++) {
286 error
= sysfs_create_group(&dev
->kobj
, groups
[i
]);
289 sysfs_remove_group(&dev
->kobj
, groups
[i
]);
297 static void device_remove_groups(struct device
*dev
,
298 struct attribute_group
**groups
)
303 for (i
= 0; groups
[i
]; i
++)
304 sysfs_remove_group(&dev
->kobj
, groups
[i
]);
307 static int device_add_attrs(struct device
*dev
)
309 struct class *class = dev
->class;
310 struct device_type
*type
= dev
->type
;
314 error
= device_add_attributes(dev
, class->dev_attrs
);
320 error
= device_add_groups(dev
, type
->groups
);
322 goto err_remove_class_attrs
;
325 error
= device_add_groups(dev
, dev
->groups
);
327 goto err_remove_type_groups
;
331 err_remove_type_groups
:
333 device_remove_groups(dev
, type
->groups
);
334 err_remove_class_attrs
:
336 device_remove_attributes(dev
, class->dev_attrs
);
341 static void device_remove_attrs(struct device
*dev
)
343 struct class *class = dev
->class;
344 struct device_type
*type
= dev
->type
;
346 device_remove_groups(dev
, dev
->groups
);
349 device_remove_groups(dev
, type
->groups
);
352 device_remove_attributes(dev
, class->dev_attrs
);
356 static ssize_t
show_dev(struct device
*dev
, struct device_attribute
*attr
,
359 return print_dev_t(buf
, dev
->devt
);
363 * devices_subsys - structure to be registered with kobject core.
366 decl_subsys(devices
, &ktype_device
, &device_uevent_ops
);
370 * device_create_file - create sysfs attribute file for device.
372 * @attr: device attribute descriptor.
375 int device_create_file(struct device
* dev
, struct device_attribute
* attr
)
378 if (get_device(dev
)) {
379 error
= sysfs_create_file(&dev
->kobj
, &attr
->attr
);
386 * device_remove_file - remove sysfs attribute file.
388 * @attr: device attribute descriptor.
391 void device_remove_file(struct device
* dev
, struct device_attribute
* attr
)
393 if (get_device(dev
)) {
394 sysfs_remove_file(&dev
->kobj
, &attr
->attr
);
400 * device_create_bin_file - create sysfs binary attribute file for device.
402 * @attr: device binary attribute descriptor.
404 int device_create_bin_file(struct device
*dev
, struct bin_attribute
*attr
)
408 error
= sysfs_create_bin_file(&dev
->kobj
, attr
);
411 EXPORT_SYMBOL_GPL(device_create_bin_file
);
414 * device_remove_bin_file - remove sysfs binary attribute file
416 * @attr: device binary attribute descriptor.
418 void device_remove_bin_file(struct device
*dev
, struct bin_attribute
*attr
)
421 sysfs_remove_bin_file(&dev
->kobj
, attr
);
423 EXPORT_SYMBOL_GPL(device_remove_bin_file
);
426 * device_schedule_callback - helper to schedule a callback for a device
428 * @func: callback function to invoke later.
430 * Attribute methods must not unregister themselves or their parent device
431 * (which would amount to the same thing). Attempts to do so will deadlock,
432 * since unregistration is mutually exclusive with driver callbacks.
434 * Instead methods can call this routine, which will attempt to allocate
435 * and schedule a workqueue request to call back @func with @dev as its
436 * argument in the workqueue's process context. @dev will be pinned until
439 * Returns 0 if the request was submitted, -ENOMEM if storage could not
442 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
443 * underlying sysfs routine (since it is intended for use by attribute
444 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
446 int device_schedule_callback(struct device
*dev
,
447 void (*func
)(struct device
*))
449 return sysfs_schedule_callback(&dev
->kobj
,
450 (void (*)(void *)) func
, dev
);
452 EXPORT_SYMBOL_GPL(device_schedule_callback
);
454 static void klist_children_get(struct klist_node
*n
)
456 struct device
*dev
= container_of(n
, struct device
, knode_parent
);
461 static void klist_children_put(struct klist_node
*n
)
463 struct device
*dev
= container_of(n
, struct device
, knode_parent
);
470 * device_initialize - init device structure.
473 * This prepares the device for use by other layers,
474 * including adding it to the device hierarchy.
475 * It is the first half of device_register(), if called by
476 * that, though it can also be called separately, so one
477 * may use @dev's fields (e.g. the refcount).
480 void device_initialize(struct device
*dev
)
482 kobj_set_kset_s(dev
, devices_subsys
);
483 kobject_init(&dev
->kobj
);
484 klist_init(&dev
->klist_children
, klist_children_get
,
486 INIT_LIST_HEAD(&dev
->dma_pools
);
487 INIT_LIST_HEAD(&dev
->node
);
488 init_MUTEX(&dev
->sem
);
489 spin_lock_init(&dev
->devres_lock
);
490 INIT_LIST_HEAD(&dev
->devres_head
);
491 device_init_wakeup(dev
, 0);
492 set_dev_node(dev
, -1);
495 #ifdef CONFIG_SYSFS_DEPRECATED
496 static struct kobject
* get_device_parent(struct device
*dev
,
497 struct device
*parent
)
499 /* Set the parent to the class, not the parent device */
500 /* this keeps sysfs from having a symlink to make old udevs happy */
502 return &dev
->class->subsys
.kset
.kobj
;
504 return &parent
->kobj
;
509 static struct kobject
*virtual_device_parent(struct device
*dev
)
511 static struct kobject
*virtual_dir
= NULL
;
514 virtual_dir
= kobject_add_dir(&devices_subsys
.kset
.kobj
, "virtual");
519 static struct kobject
* get_device_parent(struct device
*dev
,
520 struct device
*parent
)
523 struct kobject
*kobj
= NULL
;
524 struct kobject
*parent_kobj
;
528 * If we have no parent, we live in "virtual".
529 * Class-devices with a bus-device as parent, live
530 * in a class-directory to prevent namespace collisions.
533 parent_kobj
= virtual_device_parent(dev
);
534 else if (parent
->class)
535 return &parent
->kobj
;
537 parent_kobj
= &parent
->kobj
;
539 /* find our class-directory at the parent and reference it */
540 spin_lock(&dev
->class->class_dirs
.list_lock
);
541 list_for_each_entry(k
, &dev
->class->class_dirs
.list
, entry
)
542 if (k
->parent
== parent_kobj
) {
543 kobj
= kobject_get(k
);
546 spin_unlock(&dev
->class->class_dirs
.list_lock
);
550 /* or create a new class-directory at the parent device */
551 return kobject_kset_add_dir(&dev
->class->class_dirs
,
552 parent_kobj
, dev
->class->name
);
556 return &parent
->kobj
;
561 static int setup_parent(struct device
*dev
, struct device
*parent
)
563 struct kobject
*kobj
;
564 kobj
= get_device_parent(dev
, parent
);
566 return PTR_ERR(kobj
);
568 dev
->kobj
.parent
= kobj
;
573 * device_add - add device to device hierarchy.
576 * This is part 2 of device_register(), though may be called
577 * separately _iff_ device_initialize() has been called separately.
579 * This adds it to the kobject hierarchy via kobject_add(), adds it
580 * to the global and sibling lists for the device, then
581 * adds it to the other relevant subsystems of the driver model.
583 int device_add(struct device
*dev
)
585 struct device
*parent
= NULL
;
586 char *class_name
= NULL
;
587 struct class_interface
*class_intf
;
590 dev
= get_device(dev
);
591 if (!dev
|| !strlen(dev
->bus_id
))
594 pr_debug("DEV: registering device: ID = '%s'\n", dev
->bus_id
);
596 parent
= get_device(dev
->parent
);
597 error
= setup_parent(dev
, parent
);
601 /* first, register with generic layer. */
602 kobject_set_name(&dev
->kobj
, "%s", dev
->bus_id
);
603 error
= kobject_add(&dev
->kobj
);
607 /* notify platform of device entry */
609 platform_notify(dev
);
611 /* notify clients of device entry (new way) */
613 blocking_notifier_call_chain(&dev
->bus
->bus_notifier
,
614 BUS_NOTIFY_ADD_DEVICE
, dev
);
616 dev
->uevent_attr
.attr
.name
= "uevent";
617 dev
->uevent_attr
.attr
.mode
= S_IWUSR
;
619 dev
->uevent_attr
.attr
.owner
= dev
->driver
->owner
;
620 dev
->uevent_attr
.store
= store_uevent
;
621 error
= device_create_file(dev
, &dev
->uevent_attr
);
625 if (MAJOR(dev
->devt
)) {
626 struct device_attribute
*attr
;
627 attr
= kzalloc(sizeof(*attr
), GFP_KERNEL
);
630 goto ueventattrError
;
632 attr
->attr
.name
= "dev";
633 attr
->attr
.mode
= S_IRUGO
;
635 attr
->attr
.owner
= dev
->driver
->owner
;
636 attr
->show
= show_dev
;
637 error
= device_create_file(dev
, attr
);
640 goto ueventattrError
;
643 dev
->devt_attr
= attr
;
647 sysfs_create_link(&dev
->kobj
, &dev
->class->subsys
.kset
.kobj
,
649 /* If this is not a "fake" compatible device, then create the
650 * symlink from the class to the device. */
651 if (dev
->kobj
.parent
!= &dev
->class->subsys
.kset
.kobj
)
652 sysfs_create_link(&dev
->class->subsys
.kset
.kobj
,
653 &dev
->kobj
, dev
->bus_id
);
655 sysfs_create_link(&dev
->kobj
, &dev
->parent
->kobj
,
657 #ifdef CONFIG_SYSFS_DEPRECATED
658 class_name
= make_class_name(dev
->class->name
,
661 sysfs_create_link(&dev
->parent
->kobj
,
662 &dev
->kobj
, class_name
);
667 if ((error
= device_add_attrs(dev
)))
669 if ((error
= device_pm_add(dev
)))
671 if ((error
= bus_add_device(dev
)))
673 if (!dev
->uevent_suppress
)
674 kobject_uevent(&dev
->kobj
, KOBJ_ADD
);
675 if ((error
= bus_attach_device(dev
)))
678 klist_add_tail(&dev
->knode_parent
, &parent
->klist_children
);
681 down(&dev
->class->sem
);
682 /* tie the class to the device */
683 list_add_tail(&dev
->node
, &dev
->class->devices
);
685 /* notify any interfaces that the device is here */
686 list_for_each_entry(class_intf
, &dev
->class->interfaces
, node
)
687 if (class_intf
->add_dev
)
688 class_intf
->add_dev(dev
, class_intf
);
689 up(&dev
->class->sem
);
696 bus_remove_device(dev
);
698 device_pm_remove(dev
);
701 blocking_notifier_call_chain(&dev
->bus
->bus_notifier
,
702 BUS_NOTIFY_DEL_DEVICE
, dev
);
703 device_remove_attrs(dev
);
705 if (dev
->devt_attr
) {
706 device_remove_file(dev
, dev
->devt_attr
);
707 kfree(dev
->devt_attr
);
711 sysfs_remove_link(&dev
->kobj
, "subsystem");
712 /* If this is not a "fake" compatible device, remove the
713 * symlink from the class to the device. */
714 if (dev
->kobj
.parent
!= &dev
->class->subsys
.kset
.kobj
)
715 sysfs_remove_link(&dev
->class->subsys
.kset
.kobj
,
718 #ifdef CONFIG_SYSFS_DEPRECATED
719 char *class_name
= make_class_name(dev
->class->name
,
722 sysfs_remove_link(&dev
->parent
->kobj
,
726 sysfs_remove_link(&dev
->kobj
, "device");
730 device_remove_file(dev
, &dev
->uevent_attr
);
732 kobject_uevent(&dev
->kobj
, KOBJ_REMOVE
);
733 kobject_del(&dev
->kobj
);
742 * device_register - register a device with the system.
743 * @dev: pointer to the device structure
745 * This happens in two clean steps - initialize the device
746 * and add it to the system. The two steps can be called
747 * separately, but this is the easiest and most common.
748 * I.e. you should only call the two helpers separately if
749 * have a clearly defined need to use and refcount the device
750 * before it is added to the hierarchy.
753 int device_register(struct device
*dev
)
755 device_initialize(dev
);
756 return device_add(dev
);
761 * get_device - increment reference count for device.
764 * This simply forwards the call to kobject_get(), though
765 * we do take care to provide for the case that we get a NULL
769 struct device
* get_device(struct device
* dev
)
771 return dev
? to_dev(kobject_get(&dev
->kobj
)) : NULL
;
776 * put_device - decrement reference count.
777 * @dev: device in question.
779 void put_device(struct device
* dev
)
782 kobject_put(&dev
->kobj
);
787 * device_del - delete device from system.
790 * This is the first part of the device unregistration
791 * sequence. This removes the device from the lists we control
792 * from here, has it removed from the other driver model
793 * subsystems it was added to in device_add(), and removes it
794 * from the kobject hierarchy.
796 * NOTE: this should be called manually _iff_ device_add() was
797 * also called manually.
800 void device_del(struct device
* dev
)
802 struct device
* parent
= dev
->parent
;
803 struct class_interface
*class_intf
;
806 klist_del(&dev
->knode_parent
);
807 if (dev
->devt_attr
) {
808 device_remove_file(dev
, dev
->devt_attr
);
809 kfree(dev
->devt_attr
);
812 sysfs_remove_link(&dev
->kobj
, "subsystem");
813 /* If this is not a "fake" compatible device, remove the
814 * symlink from the class to the device. */
815 if (dev
->kobj
.parent
!= &dev
->class->subsys
.kset
.kobj
)
816 sysfs_remove_link(&dev
->class->subsys
.kset
.kobj
,
819 #ifdef CONFIG_SYSFS_DEPRECATED
820 char *class_name
= make_class_name(dev
->class->name
,
823 sysfs_remove_link(&dev
->parent
->kobj
,
827 sysfs_remove_link(&dev
->kobj
, "device");
830 down(&dev
->class->sem
);
831 /* notify any interfaces that the device is now gone */
832 list_for_each_entry(class_intf
, &dev
->class->interfaces
, node
)
833 if (class_intf
->remove_dev
)
834 class_intf
->remove_dev(dev
, class_intf
);
835 /* remove the device from the class list */
836 list_del_init(&dev
->node
);
837 up(&dev
->class->sem
);
839 /* If we live in a parent class-directory, unreference it */
840 if (dev
->kobj
.parent
->kset
== &dev
->class->class_dirs
) {
845 * if we are the last child of our class, delete
846 * our class-directory at this parent
848 down(&dev
->class->sem
);
849 list_for_each_entry(d
, &dev
->class->devices
, node
) {
852 if (d
->kobj
.parent
== dev
->kobj
.parent
) {
858 kobject_del(dev
->kobj
.parent
);
860 kobject_put(dev
->kobj
.parent
);
861 up(&dev
->class->sem
);
864 device_remove_file(dev
, &dev
->uevent_attr
);
865 device_remove_attrs(dev
);
866 bus_remove_device(dev
);
869 * Some platform devices are driven without driver attached
870 * and managed resources may have been acquired. Make sure
871 * all resources are released.
873 devres_release_all(dev
);
875 /* Notify the platform of the removal, in case they
876 * need to do anything...
878 if (platform_notify_remove
)
879 platform_notify_remove(dev
);
881 blocking_notifier_call_chain(&dev
->bus
->bus_notifier
,
882 BUS_NOTIFY_DEL_DEVICE
, dev
);
883 device_pm_remove(dev
);
884 kobject_uevent(&dev
->kobj
, KOBJ_REMOVE
);
885 kobject_del(&dev
->kobj
);
891 * device_unregister - unregister device from system.
892 * @dev: device going away.
894 * We do this in two parts, like we do device_register(). First,
895 * we remove it from all the subsystems with device_del(), then
896 * we decrement the reference count via put_device(). If that
897 * is the final reference count, the device will be cleaned up
898 * via device_release() above. Otherwise, the structure will
899 * stick around until the final reference to the device is dropped.
901 void device_unregister(struct device
* dev
)
903 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev
->bus_id
);
909 static struct device
* next_device(struct klist_iter
* i
)
911 struct klist_node
* n
= klist_next(i
);
912 return n
? container_of(n
, struct device
, knode_parent
) : NULL
;
916 * device_for_each_child - device child iterator.
917 * @parent: parent struct device.
918 * @data: data for the callback.
919 * @fn: function to be called for each device.
921 * Iterate over @parent's child devices, and call @fn for each,
924 * We check the return of @fn each time. If it returns anything
925 * other than 0, we break out and return that value.
927 int device_for_each_child(struct device
* parent
, void * data
,
928 int (*fn
)(struct device
*, void *))
931 struct device
* child
;
934 klist_iter_init(&parent
->klist_children
, &i
);
935 while ((child
= next_device(&i
)) && !error
)
936 error
= fn(child
, data
);
942 * device_find_child - device iterator for locating a particular device.
943 * @parent: parent struct device
944 * @data: Data to pass to match function
945 * @match: Callback function to check device
947 * This is similar to the device_for_each_child() function above, but it
948 * returns a reference to a device that is 'found' for later use, as
949 * determined by the @match callback.
951 * The callback should return 0 if the device doesn't match and non-zero
952 * if it does. If the callback returns non-zero and a reference to the
953 * current device can be obtained, this function will return to the caller
954 * and not iterate over any more devices.
956 struct device
* device_find_child(struct device
*parent
, void *data
,
957 int (*match
)(struct device
*, void *))
960 struct device
*child
;
965 klist_iter_init(&parent
->klist_children
, &i
);
966 while ((child
= next_device(&i
)))
967 if (match(child
, data
) && get_device(child
))
973 int __init
devices_init(void)
975 return subsystem_register(&devices_subsys
);
978 EXPORT_SYMBOL_GPL(device_for_each_child
);
979 EXPORT_SYMBOL_GPL(device_find_child
);
981 EXPORT_SYMBOL_GPL(device_initialize
);
982 EXPORT_SYMBOL_GPL(device_add
);
983 EXPORT_SYMBOL_GPL(device_register
);
985 EXPORT_SYMBOL_GPL(device_del
);
986 EXPORT_SYMBOL_GPL(device_unregister
);
987 EXPORT_SYMBOL_GPL(get_device
);
988 EXPORT_SYMBOL_GPL(put_device
);
990 EXPORT_SYMBOL_GPL(device_create_file
);
991 EXPORT_SYMBOL_GPL(device_remove_file
);
994 static void device_create_release(struct device
*dev
)
996 pr_debug("%s called for %s\n", __FUNCTION__
, dev
->bus_id
);
1001 * device_create - creates a device and registers it with sysfs
1002 * @class: pointer to the struct class that this device should be registered to
1003 * @parent: pointer to the parent struct device of this new device, if any
1004 * @devt: the dev_t for the char device to be added
1005 * @fmt: string for the device's name
1007 * This function can be used by char device classes. A struct device
1008 * will be created in sysfs, registered to the specified class.
1010 * A "dev" file will be created, showing the dev_t for the device, if
1011 * the dev_t is not 0,0.
1012 * If a pointer to a parent struct device is passed in, the newly created
1013 * struct device will be a child of that device in sysfs.
1014 * The pointer to the struct device will be returned from the call.
1015 * Any further sysfs files that might be required can be created using this
1018 * Note: the struct class passed to this function must have previously
1019 * been created with a call to class_create().
1021 struct device
*device_create(struct class *class, struct device
*parent
,
1022 dev_t devt
, const char *fmt
, ...)
1025 struct device
*dev
= NULL
;
1026 int retval
= -ENODEV
;
1028 if (class == NULL
|| IS_ERR(class))
1031 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1039 dev
->parent
= parent
;
1040 dev
->release
= device_create_release
;
1042 va_start(args
, fmt
);
1043 vsnprintf(dev
->bus_id
, BUS_ID_SIZE
, fmt
, args
);
1045 retval
= device_register(dev
);
1053 return ERR_PTR(retval
);
1055 EXPORT_SYMBOL_GPL(device_create
);
1058 * device_destroy - removes a device that was created with device_create()
1059 * @class: pointer to the struct class that this device was registered with
1060 * @devt: the dev_t of the device that was previously registered
1062 * This call unregisters and cleans up a device that was created with a
1063 * call to device_create().
1065 void device_destroy(struct class *class, dev_t devt
)
1067 struct device
*dev
= NULL
;
1068 struct device
*dev_tmp
;
1071 list_for_each_entry(dev_tmp
, &class->devices
, node
) {
1072 if (dev_tmp
->devt
== devt
) {
1080 device_unregister(dev
);
1082 EXPORT_SYMBOL_GPL(device_destroy
);
1085 * device_rename - renames a device
1086 * @dev: the pointer to the struct device to be renamed
1087 * @new_name: the new name of the device
1089 int device_rename(struct device
*dev
, char *new_name
)
1091 char *old_class_name
= NULL
;
1092 char *new_class_name
= NULL
;
1093 char *old_symlink_name
= NULL
;
1096 dev
= get_device(dev
);
1100 pr_debug("DEVICE: renaming '%s' to '%s'\n", dev
->bus_id
, new_name
);
1102 #ifdef CONFIG_SYSFS_DEPRECATED
1103 if ((dev
->class) && (dev
->parent
))
1104 old_class_name
= make_class_name(dev
->class->name
, &dev
->kobj
);
1108 old_symlink_name
= kmalloc(BUS_ID_SIZE
, GFP_KERNEL
);
1109 if (!old_symlink_name
) {
1111 goto out_free_old_class
;
1113 strlcpy(old_symlink_name
, dev
->bus_id
, BUS_ID_SIZE
);
1116 strlcpy(dev
->bus_id
, new_name
, BUS_ID_SIZE
);
1118 error
= kobject_rename(&dev
->kobj
, new_name
);
1120 #ifdef CONFIG_SYSFS_DEPRECATED
1121 if (old_class_name
) {
1122 new_class_name
= make_class_name(dev
->class->name
, &dev
->kobj
);
1123 if (new_class_name
) {
1124 sysfs_create_link(&dev
->parent
->kobj
, &dev
->kobj
,
1126 sysfs_remove_link(&dev
->parent
->kobj
, old_class_name
);
1132 sysfs_remove_link(&dev
->class->subsys
.kset
.kobj
,
1134 sysfs_create_link(&dev
->class->subsys
.kset
.kobj
, &dev
->kobj
,
1139 kfree(new_class_name
);
1140 kfree(old_symlink_name
);
1142 kfree(old_class_name
);
1146 EXPORT_SYMBOL_GPL(device_rename
);
1148 static int device_move_class_links(struct device
*dev
,
1149 struct device
*old_parent
,
1150 struct device
*new_parent
)
1153 #ifdef CONFIG_SYSFS_DEPRECATED
1156 class_name
= make_class_name(dev
->class->name
, &dev
->kobj
);
1162 sysfs_remove_link(&dev
->kobj
, "device");
1163 sysfs_remove_link(&old_parent
->kobj
, class_name
);
1166 error
= sysfs_create_link(&dev
->kobj
, &new_parent
->kobj
,
1170 error
= sysfs_create_link(&new_parent
->kobj
, &dev
->kobj
,
1173 sysfs_remove_link(&dev
->kobj
, "device");
1182 sysfs_remove_link(&dev
->kobj
, "device");
1184 error
= sysfs_create_link(&dev
->kobj
, &new_parent
->kobj
,
1191 * device_move - moves a device to a new parent
1192 * @dev: the pointer to the struct device to be moved
1193 * @new_parent: the new parent of the device (can by NULL)
1195 int device_move(struct device
*dev
, struct device
*new_parent
)
1198 struct device
*old_parent
;
1199 struct kobject
*new_parent_kobj
;
1201 dev
= get_device(dev
);
1205 new_parent
= get_device(new_parent
);
1206 new_parent_kobj
= get_device_parent (dev
, new_parent
);
1207 if (IS_ERR(new_parent_kobj
)) {
1208 error
= PTR_ERR(new_parent_kobj
);
1209 put_device(new_parent
);
1212 pr_debug("DEVICE: moving '%s' to '%s'\n", dev
->bus_id
,
1213 new_parent
? new_parent
->bus_id
: "<NULL>");
1214 error
= kobject_move(&dev
->kobj
, new_parent_kobj
);
1216 put_device(new_parent
);
1219 old_parent
= dev
->parent
;
1220 dev
->parent
= new_parent
;
1222 klist_remove(&dev
->knode_parent
);
1224 klist_add_tail(&dev
->knode_parent
, &new_parent
->klist_children
);
1227 error
= device_move_class_links(dev
, old_parent
, new_parent
);
1229 /* We ignore errors on cleanup since we're hosed anyway... */
1230 device_move_class_links(dev
, new_parent
, old_parent
);
1231 if (!kobject_move(&dev
->kobj
, &old_parent
->kobj
)) {
1233 klist_remove(&dev
->knode_parent
);
1235 klist_add_tail(&dev
->knode_parent
,
1236 &old_parent
->klist_children
);
1238 put_device(new_parent
);
1242 put_device(old_parent
);
1248 EXPORT_SYMBOL_GPL(device_move
);