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
)
46 printk(KERN_ERR
"Null dev to dev_driver_string\n");
50 return dev
->driver
? dev
->driver
->name
:
51 (dev
->bus
? dev
->bus
->name
:
52 (dev
->class ? dev
->class->name
: ""));
54 EXPORT_SYMBOL(dev_driver_string
);
56 #define to_dev(obj) container_of(obj, struct device, kobj)
57 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
60 dev_attr_show(struct kobject
* kobj
, struct attribute
* attr
, char * buf
)
62 struct device_attribute
* dev_attr
= to_dev_attr(attr
);
63 struct device
* dev
= to_dev(kobj
);
67 ret
= dev_attr
->show(dev
, dev_attr
, buf
);
72 dev_attr_store(struct kobject
* kobj
, struct attribute
* attr
,
73 const char * buf
, size_t count
)
75 struct device_attribute
* dev_attr
= to_dev_attr(attr
);
76 struct device
* dev
= to_dev(kobj
);
80 ret
= dev_attr
->store(dev
, dev_attr
, buf
, count
);
84 static struct sysfs_ops dev_sysfs_ops
= {
85 .show
= dev_attr_show
,
86 .store
= dev_attr_store
,
91 * device_release - free device structure.
92 * @kobj: device's kobject.
94 * This is called once the reference count for the object
95 * reaches 0. We forward the call to the device's release
96 * method, which should handle actually freeing the structure.
98 static void device_release(struct kobject
* kobj
)
100 struct device
* dev
= to_dev(kobj
);
104 else if (dev
->type
&& dev
->type
->release
)
105 dev
->type
->release(dev
);
106 else if (dev
->class && dev
->class->dev_release
)
107 dev
->class->dev_release(dev
);
109 printk(KERN_ERR
"Device '%s' does not have a release() function, "
110 "it is broken and must be fixed.\n",
116 static struct kobj_type device_ktype
= {
117 .release
= device_release
,
118 .sysfs_ops
= &dev_sysfs_ops
,
122 static int dev_uevent_filter(struct kset
*kset
, struct kobject
*kobj
)
124 struct kobj_type
*ktype
= get_ktype(kobj
);
126 if (ktype
== &device_ktype
) {
127 struct device
*dev
= to_dev(kobj
);
128 if (dev
->uevent_suppress
)
138 static const char *dev_uevent_name(struct kset
*kset
, struct kobject
*kobj
)
140 struct device
*dev
= to_dev(kobj
);
143 return dev
->bus
->name
;
145 return dev
->class->name
;
149 static int dev_uevent(struct kset
*kset
, struct kobject
*kobj
,
150 struct kobj_uevent_env
*env
)
152 struct device
*dev
= to_dev(kobj
);
155 /* add the major/minor if present */
156 if (MAJOR(dev
->devt
)) {
157 add_uevent_var(env
, "MAJOR=%u", MAJOR(dev
->devt
));
158 add_uevent_var(env
, "MINOR=%u", MINOR(dev
->devt
));
161 if (dev
->type
&& dev
->type
->name
)
162 add_uevent_var(env
, "DEVTYPE=%s", dev
->type
->name
);
165 add_uevent_var(env
, "DRIVER=%s", dev
->driver
->name
);
167 #ifdef CONFIG_SYSFS_DEPRECATED
169 struct device
*parent
= dev
->parent
;
171 /* find first bus device in parent chain */
172 while (parent
&& !parent
->bus
)
173 parent
= parent
->parent
;
174 if (parent
&& parent
->bus
) {
177 path
= kobject_get_path(&parent
->kobj
, GFP_KERNEL
);
179 add_uevent_var(env
, "PHYSDEVPATH=%s", path
);
183 add_uevent_var(env
, "PHYSDEVBUS=%s", parent
->bus
->name
);
186 add_uevent_var(env
, "PHYSDEVDRIVER=%s",
187 parent
->driver
->name
);
189 } else if (dev
->bus
) {
190 add_uevent_var(env
, "PHYSDEVBUS=%s", dev
->bus
->name
);
193 add_uevent_var(env
, "PHYSDEVDRIVER=%s", dev
->driver
->name
);
197 /* have the bus specific function add its stuff */
198 if (dev
->bus
&& dev
->bus
->uevent
) {
199 retval
= dev
->bus
->uevent(dev
, env
);
201 pr_debug ("%s: bus uevent() returned %d\n",
202 __FUNCTION__
, retval
);
205 /* have the class specific function add its stuff */
206 if (dev
->class && dev
->class->dev_uevent
) {
207 retval
= dev
->class->dev_uevent(dev
, env
);
209 pr_debug("%s: class uevent() returned %d\n",
210 __FUNCTION__
, retval
);
213 /* have the device type specific fuction add its stuff */
214 if (dev
->type
&& dev
->type
->uevent
) {
215 retval
= dev
->type
->uevent(dev
, env
);
217 pr_debug("%s: dev_type uevent() returned %d\n",
218 __FUNCTION__
, retval
);
224 static struct kset_uevent_ops device_uevent_ops
= {
225 .filter
= dev_uevent_filter
,
226 .name
= dev_uevent_name
,
227 .uevent
= dev_uevent
,
230 static ssize_t
show_uevent(struct device
*dev
, struct device_attribute
*attr
,
233 struct kobject
*top_kobj
;
235 struct kobj_uevent_env
*env
= NULL
;
240 /* search the kset, the device belongs to */
241 top_kobj
= &dev
->kobj
;
242 while (!top_kobj
->kset
&& top_kobj
->parent
)
243 top_kobj
= top_kobj
->parent
;
247 kset
= top_kobj
->kset
;
248 if (!kset
->uevent_ops
|| !kset
->uevent_ops
->uevent
)
252 if (kset
->uevent_ops
&& kset
->uevent_ops
->filter
)
253 if (!kset
->uevent_ops
->filter(kset
, &dev
->kobj
))
256 env
= kzalloc(sizeof(struct kobj_uevent_env
), GFP_KERNEL
);
260 /* let the kset specific function add its keys */
261 retval
= kset
->uevent_ops
->uevent(kset
, &dev
->kobj
, env
);
265 /* copy keys to file */
266 for (i
= 0; i
< env
->envp_idx
; i
++)
267 count
+= sprintf(&buf
[count
], "%s\n", env
->envp
[i
]);
273 static ssize_t
store_uevent(struct device
*dev
, struct device_attribute
*attr
,
274 const char *buf
, size_t count
)
276 enum kobject_action action
;
278 if (kobject_action_type(buf
, count
, &action
) == 0) {
279 kobject_uevent(&dev
->kobj
, action
);
283 dev_err(dev
, "uevent: unsupported action-string; this will "
284 "be ignored in a future kernel version\n");
285 kobject_uevent(&dev
->kobj
, KOBJ_ADD
);
290 static struct device_attribute uevent_attr
=
291 __ATTR(uevent
, S_IRUGO
| S_IWUSR
, show_uevent
, store_uevent
);
293 static int device_add_attributes(struct device
*dev
,
294 struct device_attribute
*attrs
)
300 for (i
= 0; attr_name(attrs
[i
]); i
++) {
301 error
= device_create_file(dev
, &attrs
[i
]);
307 device_remove_file(dev
, &attrs
[i
]);
312 static void device_remove_attributes(struct device
*dev
,
313 struct device_attribute
*attrs
)
318 for (i
= 0; attr_name(attrs
[i
]); i
++)
319 device_remove_file(dev
, &attrs
[i
]);
322 static int device_add_groups(struct device
*dev
,
323 struct attribute_group
**groups
)
329 for (i
= 0; groups
[i
]; i
++) {
330 error
= sysfs_create_group(&dev
->kobj
, groups
[i
]);
333 sysfs_remove_group(&dev
->kobj
, groups
[i
]);
341 static void device_remove_groups(struct device
*dev
,
342 struct attribute_group
**groups
)
347 for (i
= 0; groups
[i
]; i
++)
348 sysfs_remove_group(&dev
->kobj
, groups
[i
]);
351 static int device_add_attrs(struct device
*dev
)
353 struct class *class = dev
->class;
354 struct device_type
*type
= dev
->type
;
358 error
= device_add_attributes(dev
, class->dev_attrs
);
364 error
= device_add_groups(dev
, type
->groups
);
366 goto err_remove_class_attrs
;
369 error
= device_add_groups(dev
, dev
->groups
);
371 goto err_remove_type_groups
;
375 err_remove_type_groups
:
377 device_remove_groups(dev
, type
->groups
);
378 err_remove_class_attrs
:
380 device_remove_attributes(dev
, class->dev_attrs
);
385 static void device_remove_attrs(struct device
*dev
)
387 struct class *class = dev
->class;
388 struct device_type
*type
= dev
->type
;
390 device_remove_groups(dev
, dev
->groups
);
393 device_remove_groups(dev
, type
->groups
);
396 device_remove_attributes(dev
, class->dev_attrs
);
400 static ssize_t
show_dev(struct device
*dev
, struct device_attribute
*attr
,
403 return print_dev_t(buf
, dev
->devt
);
406 static struct device_attribute devt_attr
=
407 __ATTR(dev
, S_IRUGO
, show_dev
, NULL
);
410 * devices_subsys - structure to be registered with kobject core.
413 decl_subsys(devices
, &device_ktype
, &device_uevent_ops
);
417 * device_create_file - create sysfs attribute file for device.
419 * @attr: device attribute descriptor.
422 int device_create_file(struct device
* dev
, struct device_attribute
* attr
)
425 if (get_device(dev
)) {
426 error
= sysfs_create_file(&dev
->kobj
, &attr
->attr
);
433 * device_remove_file - remove sysfs attribute file.
435 * @attr: device attribute descriptor.
438 void device_remove_file(struct device
* dev
, struct device_attribute
* attr
)
440 if (get_device(dev
)) {
441 sysfs_remove_file(&dev
->kobj
, &attr
->attr
);
447 * device_create_bin_file - create sysfs binary attribute file for device.
449 * @attr: device binary attribute descriptor.
451 int device_create_bin_file(struct device
*dev
, struct bin_attribute
*attr
)
455 error
= sysfs_create_bin_file(&dev
->kobj
, attr
);
458 EXPORT_SYMBOL_GPL(device_create_bin_file
);
461 * device_remove_bin_file - remove sysfs binary attribute file
463 * @attr: device binary attribute descriptor.
465 void device_remove_bin_file(struct device
*dev
, struct bin_attribute
*attr
)
468 sysfs_remove_bin_file(&dev
->kobj
, attr
);
470 EXPORT_SYMBOL_GPL(device_remove_bin_file
);
473 * device_schedule_callback_owner - helper to schedule a callback for a device
475 * @func: callback function to invoke later.
476 * @owner: module owning the callback routine
478 * Attribute methods must not unregister themselves or their parent device
479 * (which would amount to the same thing). Attempts to do so will deadlock,
480 * since unregistration is mutually exclusive with driver callbacks.
482 * Instead methods can call this routine, which will attempt to allocate
483 * and schedule a workqueue request to call back @func with @dev as its
484 * argument in the workqueue's process context. @dev will be pinned until
487 * This routine is usually called via the inline device_schedule_callback(),
488 * which automatically sets @owner to THIS_MODULE.
490 * Returns 0 if the request was submitted, -ENOMEM if storage could not
491 * be allocated, -ENODEV if a reference to @owner isn't available.
493 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
494 * underlying sysfs routine (since it is intended for use by attribute
495 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
497 int device_schedule_callback_owner(struct device
*dev
,
498 void (*func
)(struct device
*), struct module
*owner
)
500 return sysfs_schedule_callback(&dev
->kobj
,
501 (void (*)(void *)) func
, dev
, owner
);
503 EXPORT_SYMBOL_GPL(device_schedule_callback_owner
);
505 static void klist_children_get(struct klist_node
*n
)
507 struct device
*dev
= container_of(n
, struct device
, knode_parent
);
512 static void klist_children_put(struct klist_node
*n
)
514 struct device
*dev
= container_of(n
, struct device
, knode_parent
);
521 * device_initialize - init device structure.
524 * This prepares the device for use by other layers,
525 * including adding it to the device hierarchy.
526 * It is the first half of device_register(), if called by
527 * that, though it can also be called separately, so one
528 * may use @dev's fields (e.g. the refcount).
531 void device_initialize(struct device
*dev
)
533 kobj_set_kset_s(dev
, devices_subsys
);
534 kobject_init(&dev
->kobj
);
535 klist_init(&dev
->klist_children
, klist_children_get
,
537 INIT_LIST_HEAD(&dev
->dma_pools
);
538 INIT_LIST_HEAD(&dev
->node
);
539 init_MUTEX(&dev
->sem
);
540 spin_lock_init(&dev
->devres_lock
);
541 INIT_LIST_HEAD(&dev
->devres_head
);
542 device_init_wakeup(dev
, 0);
543 set_dev_node(dev
, -1);
546 #ifdef CONFIG_SYSFS_DEPRECATED
547 static struct kobject
* get_device_parent(struct device
*dev
,
548 struct device
*parent
)
551 * Set the parent to the class, not the parent device
552 * for topmost devices in class hierarchy.
553 * This keeps sysfs from having a symlink to make old
556 if (dev
->class && (!parent
|| parent
->class != dev
->class))
557 return &dev
->class->subsys
.kobj
;
559 return &parent
->kobj
;
564 static struct kobject
*virtual_device_parent(struct device
*dev
)
566 static struct kobject
*virtual_dir
= NULL
;
569 virtual_dir
= kobject_add_dir(&devices_subsys
.kobj
, "virtual");
574 static struct kobject
* get_device_parent(struct device
*dev
,
575 struct device
*parent
)
578 struct kobject
*kobj
= NULL
;
579 struct kobject
*parent_kobj
;
583 * If we have no parent, we live in "virtual".
584 * Class-devices with a bus-device as parent, live
585 * in a class-directory to prevent namespace collisions.
588 parent_kobj
= virtual_device_parent(dev
);
589 else if (parent
->class)
590 return &parent
->kobj
;
592 parent_kobj
= &parent
->kobj
;
594 /* find our class-directory at the parent and reference it */
595 spin_lock(&dev
->class->class_dirs
.list_lock
);
596 list_for_each_entry(k
, &dev
->class->class_dirs
.list
, entry
)
597 if (k
->parent
== parent_kobj
) {
598 kobj
= kobject_get(k
);
601 spin_unlock(&dev
->class->class_dirs
.list_lock
);
605 /* or create a new class-directory at the parent device */
606 return kobject_kset_add_dir(&dev
->class->class_dirs
,
607 parent_kobj
, dev
->class->name
);
611 return &parent
->kobj
;
616 static int setup_parent(struct device
*dev
, struct device
*parent
)
618 struct kobject
*kobj
;
619 kobj
= get_device_parent(dev
, parent
);
621 return PTR_ERR(kobj
);
623 dev
->kobj
.parent
= kobj
;
627 static int device_add_class_symlinks(struct device
*dev
)
633 error
= sysfs_create_link(&dev
->kobj
, &dev
->class->subsys
.kobj
,
638 * If this is not a "fake" compatible device, then create the
639 * symlink from the class to the device.
641 if (dev
->kobj
.parent
!= &dev
->class->subsys
.kobj
) {
642 error
= sysfs_create_link(&dev
->class->subsys
.kobj
, &dev
->kobj
,
648 #ifdef CONFIG_SYSFS_DEPRECATED
650 struct device
*parent
= dev
->parent
;
654 * In old sysfs stacked class devices had 'device'
655 * link pointing to real device instead of parent
657 while (parent
->class && !parent
->bus
&& parent
->parent
)
658 parent
= parent
->parent
;
660 error
= sysfs_create_link(&dev
->kobj
,
666 class_name
= make_class_name(dev
->class->name
,
669 error
= sysfs_create_link(&dev
->parent
->kobj
,
670 &dev
->kobj
, class_name
);
676 error
= sysfs_create_link(&dev
->kobj
, &dev
->parent
->kobj
,
684 #ifdef CONFIG_SYSFS_DEPRECATED
687 sysfs_remove_link(&dev
->kobj
, "device");
690 if (dev
->kobj
.parent
!= &dev
->class->subsys
.kobj
)
691 sysfs_remove_link(&dev
->class->subsys
.kobj
, dev
->bus_id
);
693 sysfs_remove_link(&dev
->kobj
, "subsystem");
698 static void device_remove_class_symlinks(struct device
*dev
)
703 #ifdef CONFIG_SYSFS_DEPRECATED
706 class_name
= make_class_name(dev
->class->name
, &dev
->kobj
);
708 sysfs_remove_link(&dev
->parent
->kobj
, class_name
);
712 sysfs_remove_link(&dev
->kobj
, "device");
714 if (dev
->kobj
.parent
!= &dev
->class->subsys
.kobj
)
715 sysfs_remove_link(&dev
->class->subsys
.kobj
, dev
->bus_id
);
716 sysfs_remove_link(&dev
->kobj
, "subsystem");
720 * device_add - add device to device hierarchy.
723 * This is part 2 of device_register(), though may be called
724 * separately _iff_ device_initialize() has been called separately.
726 * This adds it to the kobject hierarchy via kobject_add(), adds it
727 * to the global and sibling lists for the device, then
728 * adds it to the other relevant subsystems of the driver model.
730 int device_add(struct device
*dev
)
732 struct device
*parent
= NULL
;
733 struct class_interface
*class_intf
;
736 dev
= get_device(dev
);
737 if (!dev
|| !strlen(dev
->bus_id
))
740 pr_debug("DEV: registering device: ID = '%s'\n", dev
->bus_id
);
742 parent
= get_device(dev
->parent
);
743 error
= setup_parent(dev
, parent
);
747 /* first, register with generic layer. */
748 kobject_set_name(&dev
->kobj
, "%s", dev
->bus_id
);
749 error
= kobject_add(&dev
->kobj
);
753 /* notify platform of device entry */
755 platform_notify(dev
);
757 /* notify clients of device entry (new way) */
759 blocking_notifier_call_chain(&dev
->bus
->bus_notifier
,
760 BUS_NOTIFY_ADD_DEVICE
, dev
);
762 error
= device_create_file(dev
, &uevent_attr
);
766 if (MAJOR(dev
->devt
)) {
767 error
= device_create_file(dev
, &devt_attr
);
769 goto ueventattrError
;
772 error
= device_add_class_symlinks(dev
);
775 error
= device_add_attrs(dev
);
778 error
= dpm_sysfs_add(dev
);
782 error
= bus_add_device(dev
);
785 kobject_uevent(&dev
->kobj
, KOBJ_ADD
);
786 bus_attach_device(dev
);
788 klist_add_tail(&dev
->knode_parent
, &parent
->klist_children
);
791 down(&dev
->class->sem
);
792 /* tie the class to the device */
793 list_add_tail(&dev
->node
, &dev
->class->devices
);
795 /* notify any interfaces that the device is here */
796 list_for_each_entry(class_intf
, &dev
->class->interfaces
, node
)
797 if (class_intf
->add_dev
)
798 class_intf
->add_dev(dev
, class_intf
);
799 up(&dev
->class->sem
);
805 device_pm_remove(dev
);
806 dpm_sysfs_remove(dev
);
809 blocking_notifier_call_chain(&dev
->bus
->bus_notifier
,
810 BUS_NOTIFY_DEL_DEVICE
, dev
);
811 device_remove_attrs(dev
);
813 device_remove_class_symlinks(dev
);
815 if (MAJOR(dev
->devt
))
816 device_remove_file(dev
, &devt_attr
);
819 sysfs_remove_link(&dev
->kobj
, "subsystem");
820 /* If this is not a "fake" compatible device, remove the
821 * symlink from the class to the device. */
822 if (dev
->kobj
.parent
!= &dev
->class->subsys
.kobj
)
823 sysfs_remove_link(&dev
->class->subsys
.kobj
,
826 #ifdef CONFIG_SYSFS_DEPRECATED
827 char *class_name
= make_class_name(dev
->class->name
,
830 sysfs_remove_link(&dev
->parent
->kobj
,
834 sysfs_remove_link(&dev
->kobj
, "device");
838 device_remove_file(dev
, &uevent_attr
);
840 kobject_uevent(&dev
->kobj
, KOBJ_REMOVE
);
841 kobject_del(&dev
->kobj
);
850 * device_register - register a device with the system.
851 * @dev: pointer to the device structure
853 * This happens in two clean steps - initialize the device
854 * and add it to the system. The two steps can be called
855 * separately, but this is the easiest and most common.
856 * I.e. you should only call the two helpers separately if
857 * have a clearly defined need to use and refcount the device
858 * before it is added to the hierarchy.
861 int device_register(struct device
*dev
)
863 device_initialize(dev
);
864 return device_add(dev
);
869 * get_device - increment reference count for device.
872 * This simply forwards the call to kobject_get(), though
873 * we do take care to provide for the case that we get a NULL
877 struct device
* get_device(struct device
* dev
)
879 return dev
? to_dev(kobject_get(&dev
->kobj
)) : NULL
;
884 * put_device - decrement reference count.
885 * @dev: device in question.
887 void put_device(struct device
* dev
)
890 kobject_put(&dev
->kobj
);
895 * device_del - delete device from system.
898 * This is the first part of the device unregistration
899 * sequence. This removes the device from the lists we control
900 * from here, has it removed from the other driver model
901 * subsystems it was added to in device_add(), and removes it
902 * from the kobject hierarchy.
904 * NOTE: this should be called manually _iff_ device_add() was
905 * also called manually.
908 void device_del(struct device
* dev
)
910 struct device
* parent
= dev
->parent
;
911 struct class_interface
*class_intf
;
914 klist_del(&dev
->knode_parent
);
915 if (MAJOR(dev
->devt
))
916 device_remove_file(dev
, &devt_attr
);
918 sysfs_remove_link(&dev
->kobj
, "subsystem");
919 /* If this is not a "fake" compatible device, remove the
920 * symlink from the class to the device. */
921 if (dev
->kobj
.parent
!= &dev
->class->subsys
.kobj
)
922 sysfs_remove_link(&dev
->class->subsys
.kobj
,
925 #ifdef CONFIG_SYSFS_DEPRECATED
926 char *class_name
= make_class_name(dev
->class->name
,
929 sysfs_remove_link(&dev
->parent
->kobj
,
933 sysfs_remove_link(&dev
->kobj
, "device");
936 down(&dev
->class->sem
);
937 /* notify any interfaces that the device is now gone */
938 list_for_each_entry(class_intf
, &dev
->class->interfaces
, node
)
939 if (class_intf
->remove_dev
)
940 class_intf
->remove_dev(dev
, class_intf
);
941 /* remove the device from the class list */
942 list_del_init(&dev
->node
);
943 up(&dev
->class->sem
);
945 /* If we live in a parent class-directory, unreference it */
946 if (dev
->kobj
.parent
->kset
== &dev
->class->class_dirs
) {
951 * if we are the last child of our class, delete
952 * our class-directory at this parent
954 down(&dev
->class->sem
);
955 list_for_each_entry(d
, &dev
->class->devices
, node
) {
958 if (d
->kobj
.parent
== dev
->kobj
.parent
) {
964 kobject_del(dev
->kobj
.parent
);
966 kobject_put(dev
->kobj
.parent
);
967 up(&dev
->class->sem
);
970 device_remove_file(dev
, &uevent_attr
);
971 device_remove_attrs(dev
);
972 bus_remove_device(dev
);
975 * Some platform devices are driven without driver attached
976 * and managed resources may have been acquired. Make sure
977 * all resources are released.
979 devres_release_all(dev
);
981 /* Notify the platform of the removal, in case they
982 * need to do anything...
984 if (platform_notify_remove
)
985 platform_notify_remove(dev
);
987 blocking_notifier_call_chain(&dev
->bus
->bus_notifier
,
988 BUS_NOTIFY_DEL_DEVICE
, dev
);
989 device_pm_remove(dev
);
990 kobject_uevent(&dev
->kobj
, KOBJ_REMOVE
);
991 kobject_del(&dev
->kobj
);
997 * device_unregister - unregister device from system.
998 * @dev: device going away.
1000 * We do this in two parts, like we do device_register(). First,
1001 * we remove it from all the subsystems with device_del(), then
1002 * we decrement the reference count via put_device(). If that
1003 * is the final reference count, the device will be cleaned up
1004 * via device_release() above. Otherwise, the structure will
1005 * stick around until the final reference to the device is dropped.
1007 void device_unregister(struct device
* dev
)
1009 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev
->bus_id
);
1015 static struct device
* next_device(struct klist_iter
* i
)
1017 struct klist_node
* n
= klist_next(i
);
1018 return n
? container_of(n
, struct device
, knode_parent
) : NULL
;
1022 * device_for_each_child - device child iterator.
1023 * @parent: parent struct device.
1024 * @data: data for the callback.
1025 * @fn: function to be called for each device.
1027 * Iterate over @parent's child devices, and call @fn for each,
1030 * We check the return of @fn each time. If it returns anything
1031 * other than 0, we break out and return that value.
1033 int device_for_each_child(struct device
* parent
, void * data
,
1034 int (*fn
)(struct device
*, void *))
1036 struct klist_iter i
;
1037 struct device
* child
;
1040 klist_iter_init(&parent
->klist_children
, &i
);
1041 while ((child
= next_device(&i
)) && !error
)
1042 error
= fn(child
, data
);
1043 klist_iter_exit(&i
);
1048 * device_find_child - device iterator for locating a particular device.
1049 * @parent: parent struct device
1050 * @data: Data to pass to match function
1051 * @match: Callback function to check device
1053 * This is similar to the device_for_each_child() function above, but it
1054 * returns a reference to a device that is 'found' for later use, as
1055 * determined by the @match callback.
1057 * The callback should return 0 if the device doesn't match and non-zero
1058 * if it does. If the callback returns non-zero and a reference to the
1059 * current device can be obtained, this function will return to the caller
1060 * and not iterate over any more devices.
1062 struct device
* device_find_child(struct device
*parent
, void *data
,
1063 int (*match
)(struct device
*, void *))
1065 struct klist_iter i
;
1066 struct device
*child
;
1071 klist_iter_init(&parent
->klist_children
, &i
);
1072 while ((child
= next_device(&i
)))
1073 if (match(child
, data
) && get_device(child
))
1075 klist_iter_exit(&i
);
1079 int __init
devices_init(void)
1081 return subsystem_register(&devices_subsys
);
1084 EXPORT_SYMBOL_GPL(device_for_each_child
);
1085 EXPORT_SYMBOL_GPL(device_find_child
);
1087 EXPORT_SYMBOL_GPL(device_initialize
);
1088 EXPORT_SYMBOL_GPL(device_add
);
1089 EXPORT_SYMBOL_GPL(device_register
);
1091 EXPORT_SYMBOL_GPL(device_del
);
1092 EXPORT_SYMBOL_GPL(device_unregister
);
1093 EXPORT_SYMBOL_GPL(get_device
);
1094 EXPORT_SYMBOL_GPL(put_device
);
1096 EXPORT_SYMBOL_GPL(device_create_file
);
1097 EXPORT_SYMBOL_GPL(device_remove_file
);
1100 static void device_create_release(struct device
*dev
)
1102 pr_debug("%s called for %s\n", __FUNCTION__
, dev
->bus_id
);
1107 * device_create - creates a device and registers it with sysfs
1108 * @class: pointer to the struct class that this device should be registered to
1109 * @parent: pointer to the parent struct device of this new device, if any
1110 * @devt: the dev_t for the char device to be added
1111 * @fmt: string for the device's name
1113 * This function can be used by char device classes. A struct device
1114 * will be created in sysfs, registered to the specified class.
1116 * A "dev" file will be created, showing the dev_t for the device, if
1117 * the dev_t is not 0,0.
1118 * If a pointer to a parent struct device is passed in, the newly created
1119 * struct device will be a child of that device in sysfs.
1120 * The pointer to the struct device will be returned from the call.
1121 * Any further sysfs files that might be required can be created using this
1124 * Note: the struct class passed to this function must have previously
1125 * been created with a call to class_create().
1127 struct device
*device_create(struct class *class, struct device
*parent
,
1128 dev_t devt
, const char *fmt
, ...)
1131 struct device
*dev
= NULL
;
1132 int retval
= -ENODEV
;
1134 if (class == NULL
|| IS_ERR(class))
1137 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1145 dev
->parent
= parent
;
1146 dev
->release
= device_create_release
;
1148 va_start(args
, fmt
);
1149 vsnprintf(dev
->bus_id
, BUS_ID_SIZE
, fmt
, args
);
1151 retval
= device_register(dev
);
1159 return ERR_PTR(retval
);
1161 EXPORT_SYMBOL_GPL(device_create
);
1164 * device_destroy - removes a device that was created with device_create()
1165 * @class: pointer to the struct class that this device was registered with
1166 * @devt: the dev_t of the device that was previously registered
1168 * This call unregisters and cleans up a device that was created with a
1169 * call to device_create().
1171 void device_destroy(struct class *class, dev_t devt
)
1173 struct device
*dev
= NULL
;
1174 struct device
*dev_tmp
;
1177 list_for_each_entry(dev_tmp
, &class->devices
, node
) {
1178 if (dev_tmp
->devt
== devt
) {
1186 device_unregister(dev
);
1188 EXPORT_SYMBOL_GPL(device_destroy
);
1191 * device_rename - renames a device
1192 * @dev: the pointer to the struct device to be renamed
1193 * @new_name: the new name of the device
1195 int device_rename(struct device
*dev
, char *new_name
)
1197 char *old_class_name
= NULL
;
1198 char *new_class_name
= NULL
;
1199 char *old_device_name
= NULL
;
1202 dev
= get_device(dev
);
1206 pr_debug("DEVICE: renaming '%s' to '%s'\n", dev
->bus_id
, new_name
);
1208 #ifdef CONFIG_SYSFS_DEPRECATED
1209 if ((dev
->class) && (dev
->parent
))
1210 old_class_name
= make_class_name(dev
->class->name
, &dev
->kobj
);
1213 old_device_name
= kmalloc(BUS_ID_SIZE
, GFP_KERNEL
);
1214 if (!old_device_name
) {
1218 strlcpy(old_device_name
, dev
->bus_id
, BUS_ID_SIZE
);
1219 strlcpy(dev
->bus_id
, new_name
, BUS_ID_SIZE
);
1221 error
= kobject_rename(&dev
->kobj
, new_name
);
1223 strlcpy(dev
->bus_id
, old_device_name
, BUS_ID_SIZE
);
1227 #ifdef CONFIG_SYSFS_DEPRECATED
1228 if (old_class_name
) {
1229 new_class_name
= make_class_name(dev
->class->name
, &dev
->kobj
);
1230 if (new_class_name
) {
1231 error
= sysfs_create_link(&dev
->parent
->kobj
,
1232 &dev
->kobj
, new_class_name
);
1235 sysfs_remove_link(&dev
->parent
->kobj
, old_class_name
);
1240 sysfs_remove_link(&dev
->class->subsys
.kobj
, old_device_name
);
1241 error
= sysfs_create_link(&dev
->class->subsys
.kobj
, &dev
->kobj
,
1244 dev_err(dev
, "%s: sysfs_create_symlink failed (%d)\n",
1245 __FUNCTION__
, error
);
1253 kfree(new_class_name
);
1254 kfree(old_class_name
);
1255 kfree(old_device_name
);
1259 EXPORT_SYMBOL_GPL(device_rename
);
1261 static int device_move_class_links(struct device
*dev
,
1262 struct device
*old_parent
,
1263 struct device
*new_parent
)
1266 #ifdef CONFIG_SYSFS_DEPRECATED
1269 class_name
= make_class_name(dev
->class->name
, &dev
->kobj
);
1275 sysfs_remove_link(&dev
->kobj
, "device");
1276 sysfs_remove_link(&old_parent
->kobj
, class_name
);
1279 error
= sysfs_create_link(&dev
->kobj
, &new_parent
->kobj
,
1283 error
= sysfs_create_link(&new_parent
->kobj
, &dev
->kobj
,
1286 sysfs_remove_link(&dev
->kobj
, "device");
1295 sysfs_remove_link(&dev
->kobj
, "device");
1297 error
= sysfs_create_link(&dev
->kobj
, &new_parent
->kobj
,
1304 * device_move - moves a device to a new parent
1305 * @dev: the pointer to the struct device to be moved
1306 * @new_parent: the new parent of the device (can by NULL)
1308 int device_move(struct device
*dev
, struct device
*new_parent
)
1311 struct device
*old_parent
;
1312 struct kobject
*new_parent_kobj
;
1314 dev
= get_device(dev
);
1318 new_parent
= get_device(new_parent
);
1319 new_parent_kobj
= get_device_parent (dev
, new_parent
);
1320 if (IS_ERR(new_parent_kobj
)) {
1321 error
= PTR_ERR(new_parent_kobj
);
1322 put_device(new_parent
);
1325 pr_debug("DEVICE: moving '%s' to '%s'\n", dev
->bus_id
,
1326 new_parent
? new_parent
->bus_id
: "<NULL>");
1327 error
= kobject_move(&dev
->kobj
, new_parent_kobj
);
1329 put_device(new_parent
);
1332 old_parent
= dev
->parent
;
1333 dev
->parent
= new_parent
;
1335 klist_remove(&dev
->knode_parent
);
1337 klist_add_tail(&dev
->knode_parent
, &new_parent
->klist_children
);
1340 error
= device_move_class_links(dev
, old_parent
, new_parent
);
1342 /* We ignore errors on cleanup since we're hosed anyway... */
1343 device_move_class_links(dev
, new_parent
, old_parent
);
1344 if (!kobject_move(&dev
->kobj
, &old_parent
->kobj
)) {
1346 klist_remove(&dev
->knode_parent
);
1348 klist_add_tail(&dev
->knode_parent
,
1349 &old_parent
->klist_children
);
1351 put_device(new_parent
);
1355 put_device(old_parent
);
1361 EXPORT_SYMBOL_GPL(device_move
);