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
);
123 if (dev
->uevent_suppress
)
133 static const char *dev_uevent_name(struct kset
*kset
, struct kobject
*kobj
)
135 struct device
*dev
= to_dev(kobj
);
138 return dev
->bus
->name
;
140 return dev
->class->name
;
144 static int dev_uevent(struct kset
*kset
, struct kobject
*kobj
, char **envp
,
145 int num_envp
, char *buffer
, int buffer_size
)
147 struct device
*dev
= to_dev(kobj
);
152 /* add the major/minor if present */
153 if (MAJOR(dev
->devt
)) {
154 add_uevent_var(envp
, num_envp
, &i
,
155 buffer
, buffer_size
, &length
,
156 "MAJOR=%u", MAJOR(dev
->devt
));
157 add_uevent_var(envp
, num_envp
, &i
,
158 buffer
, buffer_size
, &length
,
159 "MINOR=%u", MINOR(dev
->devt
));
162 if (dev
->type
&& dev
->type
->name
)
163 add_uevent_var(envp
, num_envp
, &i
,
164 buffer
, buffer_size
, &length
,
165 "DEVTYPE=%s", dev
->type
->name
);
168 add_uevent_var(envp
, num_envp
, &i
,
169 buffer
, buffer_size
, &length
,
170 "DRIVER=%s", dev
->driver
->name
);
172 #ifdef CONFIG_SYSFS_DEPRECATED
174 struct device
*parent
= dev
->parent
;
176 /* find first bus device in parent chain */
177 while (parent
&& !parent
->bus
)
178 parent
= parent
->parent
;
179 if (parent
&& parent
->bus
) {
182 path
= kobject_get_path(&parent
->kobj
, GFP_KERNEL
);
183 add_uevent_var(envp
, num_envp
, &i
,
184 buffer
, buffer_size
, &length
,
185 "PHYSDEVPATH=%s", path
);
188 add_uevent_var(envp
, num_envp
, &i
,
189 buffer
, buffer_size
, &length
,
190 "PHYSDEVBUS=%s", parent
->bus
->name
);
193 add_uevent_var(envp
, num_envp
, &i
,
194 buffer
, buffer_size
, &length
,
195 "PHYSDEVDRIVER=%s", parent
->driver
->name
);
197 } else if (dev
->bus
) {
198 add_uevent_var(envp
, num_envp
, &i
,
199 buffer
, buffer_size
, &length
,
200 "PHYSDEVBUS=%s", dev
->bus
->name
);
203 add_uevent_var(envp
, num_envp
, &i
,
204 buffer
, buffer_size
, &length
,
205 "PHYSDEVDRIVER=%s", dev
->driver
->name
);
209 /* terminate, set to next free slot, shrink available space */
213 buffer
= &buffer
[length
];
214 buffer_size
-= length
;
216 if (dev
->bus
&& dev
->bus
->uevent
) {
217 /* have the bus specific function add its stuff */
218 retval
= dev
->bus
->uevent(dev
, envp
, num_envp
, buffer
, buffer_size
);
220 pr_debug ("%s: bus uevent() returned %d\n",
221 __FUNCTION__
, retval
);
224 if (dev
->class && dev
->class->dev_uevent
) {
225 /* have the class specific function add its stuff */
226 retval
= dev
->class->dev_uevent(dev
, envp
, num_envp
, buffer
, buffer_size
);
228 pr_debug("%s: class uevent() returned %d\n",
229 __FUNCTION__
, retval
);
232 if (dev
->type
&& dev
->type
->uevent
) {
233 /* have the device type specific fuction add its stuff */
234 retval
= dev
->type
->uevent(dev
, envp
, num_envp
, buffer
, buffer_size
);
236 pr_debug("%s: dev_type uevent() returned %d\n",
237 __FUNCTION__
, retval
);
243 static struct kset_uevent_ops device_uevent_ops
= {
244 .filter
= dev_uevent_filter
,
245 .name
= dev_uevent_name
,
246 .uevent
= dev_uevent
,
249 static ssize_t
store_uevent(struct device
*dev
, struct device_attribute
*attr
,
250 const char *buf
, size_t count
)
252 kobject_uevent(&dev
->kobj
, KOBJ_ADD
);
256 static int device_add_attributes(struct device
*dev
,
257 struct device_attribute
*attrs
)
263 for (i
= 0; attr_name(attrs
[i
]); i
++) {
264 error
= device_create_file(dev
, &attrs
[i
]);
270 device_remove_file(dev
, &attrs
[i
]);
275 static void device_remove_attributes(struct device
*dev
,
276 struct device_attribute
*attrs
)
281 for (i
= 0; attr_name(attrs
[i
]); i
++)
282 device_remove_file(dev
, &attrs
[i
]);
285 static int device_add_groups(struct device
*dev
,
286 struct attribute_group
**groups
)
292 for (i
= 0; groups
[i
]; i
++) {
293 error
= sysfs_create_group(&dev
->kobj
, groups
[i
]);
296 sysfs_remove_group(&dev
->kobj
, groups
[i
]);
304 static void device_remove_groups(struct device
*dev
,
305 struct attribute_group
**groups
)
310 for (i
= 0; groups
[i
]; i
++)
311 sysfs_remove_group(&dev
->kobj
, groups
[i
]);
314 static int device_add_attrs(struct device
*dev
)
316 struct class *class = dev
->class;
317 struct device_type
*type
= dev
->type
;
321 error
= device_add_attributes(dev
, class->dev_attrs
);
327 error
= device_add_groups(dev
, type
->groups
);
329 goto err_remove_class_attrs
;
332 error
= device_add_groups(dev
, dev
->groups
);
334 goto err_remove_type_groups
;
338 err_remove_type_groups
:
340 device_remove_groups(dev
, type
->groups
);
341 err_remove_class_attrs
:
343 device_remove_attributes(dev
, class->dev_attrs
);
348 static void device_remove_attrs(struct device
*dev
)
350 struct class *class = dev
->class;
351 struct device_type
*type
= dev
->type
;
353 device_remove_groups(dev
, dev
->groups
);
356 device_remove_groups(dev
, type
->groups
);
359 device_remove_attributes(dev
, class->dev_attrs
);
363 static ssize_t
show_dev(struct device
*dev
, struct device_attribute
*attr
,
366 return print_dev_t(buf
, dev
->devt
);
370 * devices_subsys - structure to be registered with kobject core.
373 decl_subsys(devices
, &ktype_device
, &device_uevent_ops
);
377 * device_create_file - create sysfs attribute file for device.
379 * @attr: device attribute descriptor.
382 int device_create_file(struct device
* dev
, struct device_attribute
* attr
)
385 if (get_device(dev
)) {
386 error
= sysfs_create_file(&dev
->kobj
, &attr
->attr
);
393 * device_remove_file - remove sysfs attribute file.
395 * @attr: device attribute descriptor.
398 void device_remove_file(struct device
* dev
, struct device_attribute
* attr
)
400 if (get_device(dev
)) {
401 sysfs_remove_file(&dev
->kobj
, &attr
->attr
);
407 * device_create_bin_file - create sysfs binary attribute file for device.
409 * @attr: device binary attribute descriptor.
411 int device_create_bin_file(struct device
*dev
, struct bin_attribute
*attr
)
415 error
= sysfs_create_bin_file(&dev
->kobj
, attr
);
418 EXPORT_SYMBOL_GPL(device_create_bin_file
);
421 * device_remove_bin_file - remove sysfs binary attribute file
423 * @attr: device binary attribute descriptor.
425 void device_remove_bin_file(struct device
*dev
, struct bin_attribute
*attr
)
428 sysfs_remove_bin_file(&dev
->kobj
, attr
);
430 EXPORT_SYMBOL_GPL(device_remove_bin_file
);
433 * device_schedule_callback - helper to schedule a callback for a device
435 * @func: callback function to invoke later.
437 * Attribute methods must not unregister themselves or their parent device
438 * (which would amount to the same thing). Attempts to do so will deadlock,
439 * since unregistration is mutually exclusive with driver callbacks.
441 * Instead methods can call this routine, which will attempt to allocate
442 * and schedule a workqueue request to call back @func with @dev as its
443 * argument in the workqueue's process context. @dev will be pinned until
446 * Returns 0 if the request was submitted, -ENOMEM if storage could not
449 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
450 * underlying sysfs routine (since it is intended for use by attribute
451 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
453 int device_schedule_callback(struct device
*dev
,
454 void (*func
)(struct device
*))
456 return sysfs_schedule_callback(&dev
->kobj
,
457 (void (*)(void *)) func
, dev
);
459 EXPORT_SYMBOL_GPL(device_schedule_callback
);
461 static void klist_children_get(struct klist_node
*n
)
463 struct device
*dev
= container_of(n
, struct device
, knode_parent
);
468 static void klist_children_put(struct klist_node
*n
)
470 struct device
*dev
= container_of(n
, struct device
, knode_parent
);
477 * device_initialize - init device structure.
480 * This prepares the device for use by other layers,
481 * including adding it to the device hierarchy.
482 * It is the first half of device_register(), if called by
483 * that, though it can also be called separately, so one
484 * may use @dev's fields (e.g. the refcount).
487 void device_initialize(struct device
*dev
)
489 kobj_set_kset_s(dev
, devices_subsys
);
490 kobject_init(&dev
->kobj
);
491 klist_init(&dev
->klist_children
, klist_children_get
,
493 INIT_LIST_HEAD(&dev
->dma_pools
);
494 INIT_LIST_HEAD(&dev
->node
);
495 init_MUTEX(&dev
->sem
);
496 spin_lock_init(&dev
->devres_lock
);
497 INIT_LIST_HEAD(&dev
->devres_head
);
498 device_init_wakeup(dev
, 0);
499 set_dev_node(dev
, -1);
502 #ifdef CONFIG_SYSFS_DEPRECATED
503 static struct kobject
* get_device_parent(struct device
*dev
,
504 struct device
*parent
)
506 /* Set the parent to the class, not the parent device */
507 /* this keeps sysfs from having a symlink to make old udevs happy */
509 return &dev
->class->subsys
.kset
.kobj
;
511 return &parent
->kobj
;
516 static struct kobject
*virtual_device_parent(struct device
*dev
)
518 static struct kobject
*virtual_dir
= NULL
;
521 virtual_dir
= kobject_add_dir(&devices_subsys
.kset
.kobj
, "virtual");
526 static struct kobject
* get_device_parent(struct device
*dev
,
527 struct device
*parent
)
530 struct kobject
*kobj
= NULL
;
531 struct kobject
*parent_kobj
;
535 * If we have no parent, we live in "virtual".
536 * Class-devices with a bus-device as parent, live
537 * in a class-directory to prevent namespace collisions.
540 parent_kobj
= virtual_device_parent(dev
);
541 else if (parent
->class)
542 return &parent
->kobj
;
544 parent_kobj
= &parent
->kobj
;
546 /* find our class-directory at the parent and reference it */
547 spin_lock(&dev
->class->class_dirs
.list_lock
);
548 list_for_each_entry(k
, &dev
->class->class_dirs
.list
, entry
)
549 if (k
->parent
== parent_kobj
) {
550 kobj
= kobject_get(k
);
553 spin_unlock(&dev
->class->class_dirs
.list_lock
);
557 /* or create a new class-directory at the parent device */
558 return kobject_kset_add_dir(&dev
->class->class_dirs
,
559 parent_kobj
, dev
->class->name
);
563 return &parent
->kobj
;
568 static int setup_parent(struct device
*dev
, struct device
*parent
)
570 struct kobject
*kobj
;
571 kobj
= get_device_parent(dev
, parent
);
573 return PTR_ERR(kobj
);
575 dev
->kobj
.parent
= kobj
;
580 * device_add - add device to device hierarchy.
583 * This is part 2 of device_register(), though may be called
584 * separately _iff_ device_initialize() has been called separately.
586 * This adds it to the kobject hierarchy via kobject_add(), adds it
587 * to the global and sibling lists for the device, then
588 * adds it to the other relevant subsystems of the driver model.
590 int device_add(struct device
*dev
)
592 struct device
*parent
= NULL
;
593 char *class_name
= NULL
;
594 struct class_interface
*class_intf
;
597 dev
= get_device(dev
);
598 if (!dev
|| !strlen(dev
->bus_id
))
601 pr_debug("DEV: registering device: ID = '%s'\n", dev
->bus_id
);
603 parent
= get_device(dev
->parent
);
604 error
= setup_parent(dev
, parent
);
608 /* first, register with generic layer. */
609 kobject_set_name(&dev
->kobj
, "%s", dev
->bus_id
);
610 error
= kobject_add(&dev
->kobj
);
614 /* notify platform of device entry */
616 platform_notify(dev
);
618 /* notify clients of device entry (new way) */
620 blocking_notifier_call_chain(&dev
->bus
->bus_notifier
,
621 BUS_NOTIFY_ADD_DEVICE
, dev
);
623 dev
->uevent_attr
.attr
.name
= "uevent";
624 dev
->uevent_attr
.attr
.mode
= S_IWUSR
;
626 dev
->uevent_attr
.attr
.owner
= dev
->driver
->owner
;
627 dev
->uevent_attr
.store
= store_uevent
;
628 error
= device_create_file(dev
, &dev
->uevent_attr
);
632 if (MAJOR(dev
->devt
)) {
633 struct device_attribute
*attr
;
634 attr
= kzalloc(sizeof(*attr
), GFP_KERNEL
);
637 goto ueventattrError
;
639 attr
->attr
.name
= "dev";
640 attr
->attr
.mode
= S_IRUGO
;
642 attr
->attr
.owner
= dev
->driver
->owner
;
643 attr
->show
= show_dev
;
644 error
= device_create_file(dev
, attr
);
647 goto ueventattrError
;
650 dev
->devt_attr
= attr
;
654 sysfs_create_link(&dev
->kobj
, &dev
->class->subsys
.kset
.kobj
,
656 /* If this is not a "fake" compatible device, then create the
657 * symlink from the class to the device. */
658 if (dev
->kobj
.parent
!= &dev
->class->subsys
.kset
.kobj
)
659 sysfs_create_link(&dev
->class->subsys
.kset
.kobj
,
660 &dev
->kobj
, dev
->bus_id
);
662 sysfs_create_link(&dev
->kobj
, &dev
->parent
->kobj
,
664 #ifdef CONFIG_SYSFS_DEPRECATED
665 class_name
= make_class_name(dev
->class->name
,
668 sysfs_create_link(&dev
->parent
->kobj
,
669 &dev
->kobj
, class_name
);
674 if ((error
= device_add_attrs(dev
)))
676 if ((error
= device_pm_add(dev
)))
678 if ((error
= bus_add_device(dev
)))
680 kobject_uevent(&dev
->kobj
, KOBJ_ADD
);
681 bus_attach_device(dev
);
683 klist_add_tail(&dev
->knode_parent
, &parent
->klist_children
);
686 down(&dev
->class->sem
);
687 /* tie the class to the device */
688 list_add_tail(&dev
->node
, &dev
->class->devices
);
690 /* notify any interfaces that the device is here */
691 list_for_each_entry(class_intf
, &dev
->class->interfaces
, node
)
692 if (class_intf
->add_dev
)
693 class_intf
->add_dev(dev
, class_intf
);
694 up(&dev
->class->sem
);
701 device_pm_remove(dev
);
704 blocking_notifier_call_chain(&dev
->bus
->bus_notifier
,
705 BUS_NOTIFY_DEL_DEVICE
, dev
);
706 device_remove_attrs(dev
);
708 if (dev
->devt_attr
) {
709 device_remove_file(dev
, dev
->devt_attr
);
710 kfree(dev
->devt_attr
);
714 sysfs_remove_link(&dev
->kobj
, "subsystem");
715 /* If this is not a "fake" compatible device, remove the
716 * symlink from the class to the device. */
717 if (dev
->kobj
.parent
!= &dev
->class->subsys
.kset
.kobj
)
718 sysfs_remove_link(&dev
->class->subsys
.kset
.kobj
,
721 #ifdef CONFIG_SYSFS_DEPRECATED
722 char *class_name
= make_class_name(dev
->class->name
,
725 sysfs_remove_link(&dev
->parent
->kobj
,
729 sysfs_remove_link(&dev
->kobj
, "device");
733 device_remove_file(dev
, &dev
->uevent_attr
);
735 kobject_uevent(&dev
->kobj
, KOBJ_REMOVE
);
736 kobject_del(&dev
->kobj
);
745 * device_register - register a device with the system.
746 * @dev: pointer to the device structure
748 * This happens in two clean steps - initialize the device
749 * and add it to the system. The two steps can be called
750 * separately, but this is the easiest and most common.
751 * I.e. you should only call the two helpers separately if
752 * have a clearly defined need to use and refcount the device
753 * before it is added to the hierarchy.
756 int device_register(struct device
*dev
)
758 device_initialize(dev
);
759 return device_add(dev
);
764 * get_device - increment reference count for device.
767 * This simply forwards the call to kobject_get(), though
768 * we do take care to provide for the case that we get a NULL
772 struct device
* get_device(struct device
* dev
)
774 return dev
? to_dev(kobject_get(&dev
->kobj
)) : NULL
;
779 * put_device - decrement reference count.
780 * @dev: device in question.
782 void put_device(struct device
* dev
)
785 kobject_put(&dev
->kobj
);
790 * device_del - delete device from system.
793 * This is the first part of the device unregistration
794 * sequence. This removes the device from the lists we control
795 * from here, has it removed from the other driver model
796 * subsystems it was added to in device_add(), and removes it
797 * from the kobject hierarchy.
799 * NOTE: this should be called manually _iff_ device_add() was
800 * also called manually.
803 void device_del(struct device
* dev
)
805 struct device
* parent
= dev
->parent
;
806 struct class_interface
*class_intf
;
809 klist_del(&dev
->knode_parent
);
810 if (dev
->devt_attr
) {
811 device_remove_file(dev
, dev
->devt_attr
);
812 kfree(dev
->devt_attr
);
815 sysfs_remove_link(&dev
->kobj
, "subsystem");
816 /* If this is not a "fake" compatible device, remove the
817 * symlink from the class to the device. */
818 if (dev
->kobj
.parent
!= &dev
->class->subsys
.kset
.kobj
)
819 sysfs_remove_link(&dev
->class->subsys
.kset
.kobj
,
822 #ifdef CONFIG_SYSFS_DEPRECATED
823 char *class_name
= make_class_name(dev
->class->name
,
826 sysfs_remove_link(&dev
->parent
->kobj
,
830 sysfs_remove_link(&dev
->kobj
, "device");
833 down(&dev
->class->sem
);
834 /* notify any interfaces that the device is now gone */
835 list_for_each_entry(class_intf
, &dev
->class->interfaces
, node
)
836 if (class_intf
->remove_dev
)
837 class_intf
->remove_dev(dev
, class_intf
);
838 /* remove the device from the class list */
839 list_del_init(&dev
->node
);
840 up(&dev
->class->sem
);
842 /* If we live in a parent class-directory, unreference it */
843 if (dev
->kobj
.parent
->kset
== &dev
->class->class_dirs
) {
848 * if we are the last child of our class, delete
849 * our class-directory at this parent
851 down(&dev
->class->sem
);
852 list_for_each_entry(d
, &dev
->class->devices
, node
) {
855 if (d
->kobj
.parent
== dev
->kobj
.parent
) {
861 kobject_del(dev
->kobj
.parent
);
863 kobject_put(dev
->kobj
.parent
);
864 up(&dev
->class->sem
);
867 device_remove_file(dev
, &dev
->uevent_attr
);
868 device_remove_attrs(dev
);
869 bus_remove_device(dev
);
872 * Some platform devices are driven without driver attached
873 * and managed resources may have been acquired. Make sure
874 * all resources are released.
876 devres_release_all(dev
);
878 /* Notify the platform of the removal, in case they
879 * need to do anything...
881 if (platform_notify_remove
)
882 platform_notify_remove(dev
);
884 blocking_notifier_call_chain(&dev
->bus
->bus_notifier
,
885 BUS_NOTIFY_DEL_DEVICE
, dev
);
886 device_pm_remove(dev
);
887 kobject_uevent(&dev
->kobj
, KOBJ_REMOVE
);
888 kobject_del(&dev
->kobj
);
894 * device_unregister - unregister device from system.
895 * @dev: device going away.
897 * We do this in two parts, like we do device_register(). First,
898 * we remove it from all the subsystems with device_del(), then
899 * we decrement the reference count via put_device(). If that
900 * is the final reference count, the device will be cleaned up
901 * via device_release() above. Otherwise, the structure will
902 * stick around until the final reference to the device is dropped.
904 void device_unregister(struct device
* dev
)
906 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev
->bus_id
);
912 static struct device
* next_device(struct klist_iter
* i
)
914 struct klist_node
* n
= klist_next(i
);
915 return n
? container_of(n
, struct device
, knode_parent
) : NULL
;
919 * device_for_each_child - device child iterator.
920 * @parent: parent struct device.
921 * @data: data for the callback.
922 * @fn: function to be called for each device.
924 * Iterate over @parent's child devices, and call @fn for each,
927 * We check the return of @fn each time. If it returns anything
928 * other than 0, we break out and return that value.
930 int device_for_each_child(struct device
* parent
, void * data
,
931 int (*fn
)(struct device
*, void *))
934 struct device
* child
;
937 klist_iter_init(&parent
->klist_children
, &i
);
938 while ((child
= next_device(&i
)) && !error
)
939 error
= fn(child
, data
);
945 * device_find_child - device iterator for locating a particular device.
946 * @parent: parent struct device
947 * @data: Data to pass to match function
948 * @match: Callback function to check device
950 * This is similar to the device_for_each_child() function above, but it
951 * returns a reference to a device that is 'found' for later use, as
952 * determined by the @match callback.
954 * The callback should return 0 if the device doesn't match and non-zero
955 * if it does. If the callback returns non-zero and a reference to the
956 * current device can be obtained, this function will return to the caller
957 * and not iterate over any more devices.
959 struct device
* device_find_child(struct device
*parent
, void *data
,
960 int (*match
)(struct device
*, void *))
963 struct device
*child
;
968 klist_iter_init(&parent
->klist_children
, &i
);
969 while ((child
= next_device(&i
)))
970 if (match(child
, data
) && get_device(child
))
976 int __init
devices_init(void)
978 return subsystem_register(&devices_subsys
);
981 EXPORT_SYMBOL_GPL(device_for_each_child
);
982 EXPORT_SYMBOL_GPL(device_find_child
);
984 EXPORT_SYMBOL_GPL(device_initialize
);
985 EXPORT_SYMBOL_GPL(device_add
);
986 EXPORT_SYMBOL_GPL(device_register
);
988 EXPORT_SYMBOL_GPL(device_del
);
989 EXPORT_SYMBOL_GPL(device_unregister
);
990 EXPORT_SYMBOL_GPL(get_device
);
991 EXPORT_SYMBOL_GPL(put_device
);
993 EXPORT_SYMBOL_GPL(device_create_file
);
994 EXPORT_SYMBOL_GPL(device_remove_file
);
997 static void device_create_release(struct device
*dev
)
999 pr_debug("%s called for %s\n", __FUNCTION__
, dev
->bus_id
);
1004 * device_create - creates a device and registers it with sysfs
1005 * @class: pointer to the struct class that this device should be registered to
1006 * @parent: pointer to the parent struct device of this new device, if any
1007 * @devt: the dev_t for the char device to be added
1008 * @fmt: string for the device's name
1010 * This function can be used by char device classes. A struct device
1011 * will be created in sysfs, registered to the specified class.
1013 * A "dev" file will be created, showing the dev_t for the device, if
1014 * the dev_t is not 0,0.
1015 * If a pointer to a parent struct device is passed in, the newly created
1016 * struct device will be a child of that device in sysfs.
1017 * The pointer to the struct device will be returned from the call.
1018 * Any further sysfs files that might be required can be created using this
1021 * Note: the struct class passed to this function must have previously
1022 * been created with a call to class_create().
1024 struct device
*device_create(struct class *class, struct device
*parent
,
1025 dev_t devt
, const char *fmt
, ...)
1028 struct device
*dev
= NULL
;
1029 int retval
= -ENODEV
;
1031 if (class == NULL
|| IS_ERR(class))
1034 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1042 dev
->parent
= parent
;
1043 dev
->release
= device_create_release
;
1045 va_start(args
, fmt
);
1046 vsnprintf(dev
->bus_id
, BUS_ID_SIZE
, fmt
, args
);
1048 retval
= device_register(dev
);
1056 return ERR_PTR(retval
);
1058 EXPORT_SYMBOL_GPL(device_create
);
1061 * device_destroy - removes a device that was created with device_create()
1062 * @class: pointer to the struct class that this device was registered with
1063 * @devt: the dev_t of the device that was previously registered
1065 * This call unregisters and cleans up a device that was created with a
1066 * call to device_create().
1068 void device_destroy(struct class *class, dev_t devt
)
1070 struct device
*dev
= NULL
;
1071 struct device
*dev_tmp
;
1074 list_for_each_entry(dev_tmp
, &class->devices
, node
) {
1075 if (dev_tmp
->devt
== devt
) {
1083 device_unregister(dev
);
1085 EXPORT_SYMBOL_GPL(device_destroy
);
1088 * device_rename - renames a device
1089 * @dev: the pointer to the struct device to be renamed
1090 * @new_name: the new name of the device
1092 int device_rename(struct device
*dev
, char *new_name
)
1094 char *old_class_name
= NULL
;
1095 char *new_class_name
= NULL
;
1096 char *old_symlink_name
= NULL
;
1099 dev
= get_device(dev
);
1103 pr_debug("DEVICE: renaming '%s' to '%s'\n", dev
->bus_id
, new_name
);
1105 #ifdef CONFIG_SYSFS_DEPRECATED
1106 if ((dev
->class) && (dev
->parent
))
1107 old_class_name
= make_class_name(dev
->class->name
, &dev
->kobj
);
1111 old_symlink_name
= kmalloc(BUS_ID_SIZE
, GFP_KERNEL
);
1112 if (!old_symlink_name
) {
1114 goto out_free_old_class
;
1116 strlcpy(old_symlink_name
, dev
->bus_id
, BUS_ID_SIZE
);
1119 strlcpy(dev
->bus_id
, new_name
, BUS_ID_SIZE
);
1121 error
= kobject_rename(&dev
->kobj
, new_name
);
1123 #ifdef CONFIG_SYSFS_DEPRECATED
1124 if (old_class_name
) {
1125 new_class_name
= make_class_name(dev
->class->name
, &dev
->kobj
);
1126 if (new_class_name
) {
1127 sysfs_create_link(&dev
->parent
->kobj
, &dev
->kobj
,
1129 sysfs_remove_link(&dev
->parent
->kobj
, old_class_name
);
1135 sysfs_remove_link(&dev
->class->subsys
.kset
.kobj
,
1137 sysfs_create_link(&dev
->class->subsys
.kset
.kobj
, &dev
->kobj
,
1142 kfree(new_class_name
);
1143 kfree(old_symlink_name
);
1145 kfree(old_class_name
);
1149 EXPORT_SYMBOL_GPL(device_rename
);
1151 static int device_move_class_links(struct device
*dev
,
1152 struct device
*old_parent
,
1153 struct device
*new_parent
)
1156 #ifdef CONFIG_SYSFS_DEPRECATED
1159 class_name
= make_class_name(dev
->class->name
, &dev
->kobj
);
1165 sysfs_remove_link(&dev
->kobj
, "device");
1166 sysfs_remove_link(&old_parent
->kobj
, class_name
);
1169 error
= sysfs_create_link(&dev
->kobj
, &new_parent
->kobj
,
1173 error
= sysfs_create_link(&new_parent
->kobj
, &dev
->kobj
,
1176 sysfs_remove_link(&dev
->kobj
, "device");
1185 sysfs_remove_link(&dev
->kobj
, "device");
1187 error
= sysfs_create_link(&dev
->kobj
, &new_parent
->kobj
,
1194 * device_move - moves a device to a new parent
1195 * @dev: the pointer to the struct device to be moved
1196 * @new_parent: the new parent of the device (can by NULL)
1198 int device_move(struct device
*dev
, struct device
*new_parent
)
1201 struct device
*old_parent
;
1202 struct kobject
*new_parent_kobj
;
1204 dev
= get_device(dev
);
1208 new_parent
= get_device(new_parent
);
1209 new_parent_kobj
= get_device_parent (dev
, new_parent
);
1210 if (IS_ERR(new_parent_kobj
)) {
1211 error
= PTR_ERR(new_parent_kobj
);
1212 put_device(new_parent
);
1215 pr_debug("DEVICE: moving '%s' to '%s'\n", dev
->bus_id
,
1216 new_parent
? new_parent
->bus_id
: "<NULL>");
1217 error
= kobject_move(&dev
->kobj
, new_parent_kobj
);
1219 put_device(new_parent
);
1222 old_parent
= dev
->parent
;
1223 dev
->parent
= new_parent
;
1225 klist_remove(&dev
->knode_parent
);
1227 klist_add_tail(&dev
->knode_parent
, &new_parent
->klist_children
);
1230 error
= device_move_class_links(dev
, old_parent
, new_parent
);
1232 /* We ignore errors on cleanup since we're hosed anyway... */
1233 device_move_class_links(dev
, new_parent
, old_parent
);
1234 if (!kobject_move(&dev
->kobj
, &old_parent
->kobj
)) {
1236 klist_remove(&dev
->knode_parent
);
1238 klist_add_tail(&dev
->knode_parent
,
1239 &old_parent
->klist_children
);
1241 put_device(new_parent
);
1245 put_device(old_parent
);
1251 EXPORT_SYMBOL_GPL(device_move
);