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 * Detect the LANANA-assigned LOCAL/EXPERIMENTAL majors
33 bool is_lanana_major(unsigned int major
)
35 if (major
>= 60 && major
<= 63)
37 if (major
>= 120 && major
<= 127)
39 if (major
>= 240 && major
<= 254)
45 * sysfs bindings for devices.
49 * dev_driver_string - Return a device's driver name, if at all possible
50 * @dev: struct device to get the name of
52 * Will return the device's driver's name if it is bound to a device. If
53 * the device is not bound to a device, it will return the name of the bus
54 * it is attached to. If it is not attached to a bus either, an empty
55 * string will be returned.
57 const char *dev_driver_string(struct device
*dev
)
59 return dev
->driver
? dev
->driver
->name
:
60 (dev
->bus
? dev
->bus
->name
: "");
62 EXPORT_SYMBOL(dev_driver_string
);
64 #define to_dev(obj) container_of(obj, struct device, kobj)
65 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
68 dev_attr_show(struct kobject
* kobj
, struct attribute
* attr
, char * buf
)
70 struct device_attribute
* dev_attr
= to_dev_attr(attr
);
71 struct device
* dev
= to_dev(kobj
);
75 ret
= dev_attr
->show(dev
, dev_attr
, buf
);
80 dev_attr_store(struct kobject
* kobj
, struct attribute
* attr
,
81 const char * buf
, size_t count
)
83 struct device_attribute
* dev_attr
= to_dev_attr(attr
);
84 struct device
* dev
= to_dev(kobj
);
88 ret
= dev_attr
->store(dev
, dev_attr
, buf
, count
);
92 static struct sysfs_ops dev_sysfs_ops
= {
93 .show
= dev_attr_show
,
94 .store
= dev_attr_store
,
99 * device_release - free device structure.
100 * @kobj: device's kobject.
102 * This is called once the reference count for the object
103 * reaches 0. We forward the call to the device's release
104 * method, which should handle actually freeing the structure.
106 static void device_release(struct kobject
* kobj
)
108 struct device
* dev
= to_dev(kobj
);
112 else if (dev
->type
&& dev
->type
->release
)
113 dev
->type
->release(dev
);
114 else if (dev
->class && dev
->class->dev_release
)
115 dev
->class->dev_release(dev
);
117 printk(KERN_ERR
"Device '%s' does not have a release() function, "
118 "it is broken and must be fixed.\n",
124 static struct kobj_type ktype_device
= {
125 .release
= device_release
,
126 .sysfs_ops
= &dev_sysfs_ops
,
130 static int dev_uevent_filter(struct kset
*kset
, struct kobject
*kobj
)
132 struct kobj_type
*ktype
= get_ktype(kobj
);
134 if (ktype
== &ktype_device
) {
135 struct device
*dev
= to_dev(kobj
);
144 static const char *dev_uevent_name(struct kset
*kset
, struct kobject
*kobj
)
146 struct device
*dev
= to_dev(kobj
);
149 return dev
->bus
->name
;
151 return dev
->class->name
;
155 static int dev_uevent(struct kset
*kset
, struct kobject
*kobj
, char **envp
,
156 int num_envp
, char *buffer
, int buffer_size
)
158 struct device
*dev
= to_dev(kobj
);
163 /* add the major/minor if present */
164 if (MAJOR(dev
->devt
)) {
165 add_uevent_var(envp
, num_envp
, &i
,
166 buffer
, buffer_size
, &length
,
167 "MAJOR=%u", MAJOR(dev
->devt
));
168 add_uevent_var(envp
, num_envp
, &i
,
169 buffer
, buffer_size
, &length
,
170 "MINOR=%u", MINOR(dev
->devt
));
174 add_uevent_var(envp
, num_envp
, &i
,
175 buffer
, buffer_size
, &length
,
176 "DRIVER=%s", dev
->driver
->name
);
178 #ifdef CONFIG_SYSFS_DEPRECATED
180 struct device
*parent
= dev
->parent
;
182 /* find first bus device in parent chain */
183 while (parent
&& !parent
->bus
)
184 parent
= parent
->parent
;
185 if (parent
&& parent
->bus
) {
188 path
= kobject_get_path(&parent
->kobj
, GFP_KERNEL
);
189 add_uevent_var(envp
, num_envp
, &i
,
190 buffer
, buffer_size
, &length
,
191 "PHYSDEVPATH=%s", path
);
194 add_uevent_var(envp
, num_envp
, &i
,
195 buffer
, buffer_size
, &length
,
196 "PHYSDEVBUS=%s", parent
->bus
->name
);
199 add_uevent_var(envp
, num_envp
, &i
,
200 buffer
, buffer_size
, &length
,
201 "PHYSDEVDRIVER=%s", parent
->driver
->name
);
203 } else if (dev
->bus
) {
204 add_uevent_var(envp
, num_envp
, &i
,
205 buffer
, buffer_size
, &length
,
206 "PHYSDEVBUS=%s", dev
->bus
->name
);
209 add_uevent_var(envp
, num_envp
, &i
,
210 buffer
, buffer_size
, &length
,
211 "PHYSDEVDRIVER=%s", dev
->driver
->name
);
215 /* terminate, set to next free slot, shrink available space */
219 buffer
= &buffer
[length
];
220 buffer_size
-= length
;
222 if (dev
->bus
&& dev
->bus
->uevent
) {
223 /* have the bus specific function add its stuff */
224 retval
= dev
->bus
->uevent(dev
, envp
, num_envp
, buffer
, buffer_size
);
226 pr_debug ("%s: bus uevent() returned %d\n",
227 __FUNCTION__
, retval
);
230 if (dev
->class && dev
->class->dev_uevent
) {
231 /* have the class specific function add its stuff */
232 retval
= dev
->class->dev_uevent(dev
, envp
, num_envp
, buffer
, buffer_size
);
234 pr_debug("%s: class uevent() returned %d\n",
235 __FUNCTION__
, retval
);
238 if (dev
->type
&& dev
->type
->uevent
) {
239 /* have the device type specific fuction add its stuff */
240 retval
= dev
->type
->uevent(dev
, envp
, num_envp
, buffer
, buffer_size
);
242 pr_debug("%s: dev_type uevent() returned %d\n",
243 __FUNCTION__
, retval
);
249 static struct kset_uevent_ops device_uevent_ops
= {
250 .filter
= dev_uevent_filter
,
251 .name
= dev_uevent_name
,
252 .uevent
= dev_uevent
,
255 static ssize_t
store_uevent(struct device
*dev
, struct device_attribute
*attr
,
256 const char *buf
, size_t count
)
258 kobject_uevent(&dev
->kobj
, KOBJ_ADD
);
262 static int device_add_groups(struct device
*dev
)
268 for (i
= 0; dev
->groups
[i
]; i
++) {
269 error
= sysfs_create_group(&dev
->kobj
, dev
->groups
[i
]);
272 sysfs_remove_group(&dev
->kobj
, dev
->groups
[i
]);
281 static void device_remove_groups(struct device
*dev
)
285 for (i
= 0; dev
->groups
[i
]; i
++) {
286 sysfs_remove_group(&dev
->kobj
, dev
->groups
[i
]);
291 static int device_add_attrs(struct device
*dev
)
293 struct class *class = dev
->class;
294 struct device_type
*type
= dev
->type
;
298 if (class && class->dev_attrs
) {
299 for (i
= 0; attr_name(class->dev_attrs
[i
]); i
++) {
300 error
= device_create_file(dev
, &class->dev_attrs
[i
]);
306 device_remove_file(dev
, &class->dev_attrs
[i
]);
309 if (type
&& type
->attrs
) {
310 for (i
= 0; attr_name(type
->attrs
[i
]); i
++) {
311 error
= device_create_file(dev
, &type
->attrs
[i
]);
317 device_remove_file(dev
, &type
->attrs
[i
]);
323 static void device_remove_attrs(struct device
*dev
)
325 struct class *class = dev
->class;
326 struct device_type
*type
= dev
->type
;
329 if (class && class->dev_attrs
) {
330 for (i
= 0; attr_name(class->dev_attrs
[i
]); i
++)
331 device_remove_file(dev
, &class->dev_attrs
[i
]);
334 if (type
&& type
->attrs
) {
335 for (i
= 0; attr_name(type
->attrs
[i
]); i
++)
336 device_remove_file(dev
, &type
->attrs
[i
]);
341 static ssize_t
show_dev(struct device
*dev
, struct device_attribute
*attr
,
344 return print_dev_t(buf
, dev
->devt
);
348 * devices_subsys - structure to be registered with kobject core.
351 decl_subsys(devices
, &ktype_device
, &device_uevent_ops
);
355 * device_create_file - create sysfs attribute file for device.
357 * @attr: device attribute descriptor.
360 int device_create_file(struct device
* dev
, struct device_attribute
* attr
)
363 if (get_device(dev
)) {
364 error
= sysfs_create_file(&dev
->kobj
, &attr
->attr
);
371 * device_remove_file - remove sysfs attribute file.
373 * @attr: device attribute descriptor.
376 void device_remove_file(struct device
* dev
, struct device_attribute
* attr
)
378 if (get_device(dev
)) {
379 sysfs_remove_file(&dev
->kobj
, &attr
->attr
);
385 * device_create_bin_file - create sysfs binary attribute file for device.
387 * @attr: device binary attribute descriptor.
389 int device_create_bin_file(struct device
*dev
, struct bin_attribute
*attr
)
393 error
= sysfs_create_bin_file(&dev
->kobj
, attr
);
396 EXPORT_SYMBOL_GPL(device_create_bin_file
);
399 * device_remove_bin_file - remove sysfs binary attribute file
401 * @attr: device binary attribute descriptor.
403 void device_remove_bin_file(struct device
*dev
, struct bin_attribute
*attr
)
406 sysfs_remove_bin_file(&dev
->kobj
, attr
);
408 EXPORT_SYMBOL_GPL(device_remove_bin_file
);
411 * device_schedule_callback - helper to schedule a callback for a device
413 * @func: callback function to invoke later.
415 * Attribute methods must not unregister themselves or their parent device
416 * (which would amount to the same thing). Attempts to do so will deadlock,
417 * since unregistration is mutually exclusive with driver callbacks.
419 * Instead methods can call this routine, which will attempt to allocate
420 * and schedule a workqueue request to call back @func with @dev as its
421 * argument in the workqueue's process context. @dev will be pinned until
424 * Returns 0 if the request was submitted, -ENOMEM if storage could not
427 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
428 * underlying sysfs routine (since it is intended for use by attribute
429 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
431 int device_schedule_callback(struct device
*dev
,
432 void (*func
)(struct device
*))
434 return sysfs_schedule_callback(&dev
->kobj
,
435 (void (*)(void *)) func
, dev
);
437 EXPORT_SYMBOL_GPL(device_schedule_callback
);
439 static void klist_children_get(struct klist_node
*n
)
441 struct device
*dev
= container_of(n
, struct device
, knode_parent
);
446 static void klist_children_put(struct klist_node
*n
)
448 struct device
*dev
= container_of(n
, struct device
, knode_parent
);
455 * device_initialize - init device structure.
458 * This prepares the device for use by other layers,
459 * including adding it to the device hierarchy.
460 * It is the first half of device_register(), if called by
461 * that, though it can also be called separately, so one
462 * may use @dev's fields (e.g. the refcount).
465 void device_initialize(struct device
*dev
)
467 kobj_set_kset_s(dev
, devices_subsys
);
468 kobject_init(&dev
->kobj
);
469 klist_init(&dev
->klist_children
, klist_children_get
,
471 INIT_LIST_HEAD(&dev
->dma_pools
);
472 INIT_LIST_HEAD(&dev
->node
);
473 init_MUTEX(&dev
->sem
);
474 spin_lock_init(&dev
->devres_lock
);
475 INIT_LIST_HEAD(&dev
->devres_head
);
476 device_init_wakeup(dev
, 0);
477 set_dev_node(dev
, -1);
480 #ifdef CONFIG_SYSFS_DEPRECATED
481 static struct kobject
* get_device_parent(struct device
*dev
,
482 struct device
*parent
)
484 /* Set the parent to the class, not the parent device */
485 /* this keeps sysfs from having a symlink to make old udevs happy */
487 return &dev
->class->subsys
.kset
.kobj
;
489 return &parent
->kobj
;
494 static struct kobject
* virtual_device_parent(struct device
*dev
)
497 return ERR_PTR(-ENODEV
);
499 if (!dev
->class->virtual_dir
) {
500 static struct kobject
*virtual_dir
= NULL
;
503 virtual_dir
= kobject_add_dir(&devices_subsys
.kset
.kobj
, "virtual");
504 dev
->class->virtual_dir
= kobject_add_dir(virtual_dir
, dev
->class->name
);
507 return dev
->class->virtual_dir
;
510 static struct kobject
* get_device_parent(struct device
*dev
,
511 struct device
*parent
)
513 /* if this is a class device, and has no parent, create one */
514 if ((dev
->class) && (parent
== NULL
)) {
515 return virtual_device_parent(dev
);
517 return &parent
->kobj
;
522 static int setup_parent(struct device
*dev
, struct device
*parent
)
524 struct kobject
*kobj
;
525 kobj
= get_device_parent(dev
, parent
);
527 return PTR_ERR(kobj
);
529 dev
->kobj
.parent
= kobj
;
534 * device_add - add device to device hierarchy.
537 * This is part 2 of device_register(), though may be called
538 * separately _iff_ device_initialize() has been called separately.
540 * This adds it to the kobject hierarchy via kobject_add(), adds it
541 * to the global and sibling lists for the device, then
542 * adds it to the other relevant subsystems of the driver model.
544 int device_add(struct device
*dev
)
546 struct device
*parent
= NULL
;
547 char *class_name
= NULL
;
548 struct class_interface
*class_intf
;
551 dev
= get_device(dev
);
552 if (!dev
|| !strlen(dev
->bus_id
))
555 pr_debug("DEV: registering device: ID = '%s'\n", dev
->bus_id
);
557 parent
= get_device(dev
->parent
);
559 error
= setup_parent(dev
, parent
);
563 /* first, register with generic layer. */
564 kobject_set_name(&dev
->kobj
, "%s", dev
->bus_id
);
565 error
= kobject_add(&dev
->kobj
);
569 /* notify platform of device entry */
571 platform_notify(dev
);
573 /* notify clients of device entry (new way) */
575 blocking_notifier_call_chain(&dev
->bus
->bus_notifier
,
576 BUS_NOTIFY_ADD_DEVICE
, dev
);
578 dev
->uevent_attr
.attr
.name
= "uevent";
579 dev
->uevent_attr
.attr
.mode
= S_IWUSR
;
581 dev
->uevent_attr
.attr
.owner
= dev
->driver
->owner
;
582 dev
->uevent_attr
.store
= store_uevent
;
583 error
= device_create_file(dev
, &dev
->uevent_attr
);
587 if (MAJOR(dev
->devt
)) {
588 struct device_attribute
*attr
;
589 attr
= kzalloc(sizeof(*attr
), GFP_KERNEL
);
592 goto ueventattrError
;
594 attr
->attr
.name
= "dev";
595 attr
->attr
.mode
= S_IRUGO
;
597 attr
->attr
.owner
= dev
->driver
->owner
;
598 attr
->show
= show_dev
;
599 error
= device_create_file(dev
, attr
);
602 goto ueventattrError
;
605 dev
->devt_attr
= attr
;
609 sysfs_create_link(&dev
->kobj
, &dev
->class->subsys
.kset
.kobj
,
611 /* If this is not a "fake" compatible device, then create the
612 * symlink from the class to the device. */
613 if (dev
->kobj
.parent
!= &dev
->class->subsys
.kset
.kobj
)
614 sysfs_create_link(&dev
->class->subsys
.kset
.kobj
,
615 &dev
->kobj
, dev
->bus_id
);
617 sysfs_create_link(&dev
->kobj
, &dev
->parent
->kobj
,
619 #ifdef CONFIG_SYSFS_DEPRECATED
620 class_name
= make_class_name(dev
->class->name
,
623 sysfs_create_link(&dev
->parent
->kobj
,
624 &dev
->kobj
, class_name
);
629 if ((error
= device_add_attrs(dev
)))
631 if ((error
= device_add_groups(dev
)))
633 if ((error
= device_pm_add(dev
)))
635 if ((error
= bus_add_device(dev
)))
637 if (!dev
->uevent_suppress
)
638 kobject_uevent(&dev
->kobj
, KOBJ_ADD
);
639 if ((error
= bus_attach_device(dev
)))
642 klist_add_tail(&dev
->knode_parent
, &parent
->klist_children
);
645 down(&dev
->class->sem
);
646 /* tie the class to the device */
647 list_add_tail(&dev
->node
, &dev
->class->devices
);
649 /* notify any interfaces that the device is here */
650 list_for_each_entry(class_intf
, &dev
->class->interfaces
, node
)
651 if (class_intf
->add_dev
)
652 class_intf
->add_dev(dev
, class_intf
);
653 up(&dev
->class->sem
);
660 bus_remove_device(dev
);
662 device_pm_remove(dev
);
665 blocking_notifier_call_chain(&dev
->bus
->bus_notifier
,
666 BUS_NOTIFY_DEL_DEVICE
, dev
);
667 device_remove_groups(dev
);
669 device_remove_attrs(dev
);
671 if (dev
->devt_attr
) {
672 device_remove_file(dev
, dev
->devt_attr
);
673 kfree(dev
->devt_attr
);
677 sysfs_remove_link(&dev
->kobj
, "subsystem");
678 /* If this is not a "fake" compatible device, remove the
679 * symlink from the class to the device. */
680 if (dev
->kobj
.parent
!= &dev
->class->subsys
.kset
.kobj
)
681 sysfs_remove_link(&dev
->class->subsys
.kset
.kobj
,
684 #ifdef CONFIG_SYSFS_DEPRECATED
685 char *class_name
= make_class_name(dev
->class->name
,
688 sysfs_remove_link(&dev
->parent
->kobj
,
692 sysfs_remove_link(&dev
->kobj
, "device");
695 down(&dev
->class->sem
);
696 /* notify any interfaces that the device is now gone */
697 list_for_each_entry(class_intf
, &dev
->class->interfaces
, node
)
698 if (class_intf
->remove_dev
)
699 class_intf
->remove_dev(dev
, class_intf
);
700 /* remove the device from the class list */
701 list_del_init(&dev
->node
);
702 up(&dev
->class->sem
);
705 device_remove_file(dev
, &dev
->uevent_attr
);
707 kobject_uevent(&dev
->kobj
, KOBJ_REMOVE
);
708 kobject_del(&dev
->kobj
);
717 * device_register - register a device with the system.
718 * @dev: pointer to the device structure
720 * This happens in two clean steps - initialize the device
721 * and add it to the system. The two steps can be called
722 * separately, but this is the easiest and most common.
723 * I.e. you should only call the two helpers separately if
724 * have a clearly defined need to use and refcount the device
725 * before it is added to the hierarchy.
728 int device_register(struct device
*dev
)
730 device_initialize(dev
);
731 return device_add(dev
);
736 * get_device - increment reference count for device.
739 * This simply forwards the call to kobject_get(), though
740 * we do take care to provide for the case that we get a NULL
744 struct device
* get_device(struct device
* dev
)
746 return dev
? to_dev(kobject_get(&dev
->kobj
)) : NULL
;
751 * put_device - decrement reference count.
752 * @dev: device in question.
754 void put_device(struct device
* dev
)
757 kobject_put(&dev
->kobj
);
762 * device_del - delete device from system.
765 * This is the first part of the device unregistration
766 * sequence. This removes the device from the lists we control
767 * from here, has it removed from the other driver model
768 * subsystems it was added to in device_add(), and removes it
769 * from the kobject hierarchy.
771 * NOTE: this should be called manually _iff_ device_add() was
772 * also called manually.
775 void device_del(struct device
* dev
)
777 struct device
* parent
= dev
->parent
;
778 struct class_interface
*class_intf
;
781 klist_del(&dev
->knode_parent
);
782 if (dev
->devt_attr
) {
783 device_remove_file(dev
, dev
->devt_attr
);
784 kfree(dev
->devt_attr
);
787 sysfs_remove_link(&dev
->kobj
, "subsystem");
788 /* If this is not a "fake" compatible device, remove the
789 * symlink from the class to the device. */
790 if (dev
->kobj
.parent
!= &dev
->class->subsys
.kset
.kobj
)
791 sysfs_remove_link(&dev
->class->subsys
.kset
.kobj
,
794 #ifdef CONFIG_SYSFS_DEPRECATED
795 char *class_name
= make_class_name(dev
->class->name
,
798 sysfs_remove_link(&dev
->parent
->kobj
,
802 sysfs_remove_link(&dev
->kobj
, "device");
805 down(&dev
->class->sem
);
806 /* notify any interfaces that the device is now gone */
807 list_for_each_entry(class_intf
, &dev
->class->interfaces
, node
)
808 if (class_intf
->remove_dev
)
809 class_intf
->remove_dev(dev
, class_intf
);
810 /* remove the device from the class list */
811 list_del_init(&dev
->node
);
812 up(&dev
->class->sem
);
814 device_remove_file(dev
, &dev
->uevent_attr
);
815 device_remove_groups(dev
);
816 device_remove_attrs(dev
);
817 bus_remove_device(dev
);
820 * Some platform devices are driven without driver attached
821 * and managed resources may have been acquired. Make sure
822 * all resources are released.
824 devres_release_all(dev
);
826 /* Notify the platform of the removal, in case they
827 * need to do anything...
829 if (platform_notify_remove
)
830 platform_notify_remove(dev
);
832 blocking_notifier_call_chain(&dev
->bus
->bus_notifier
,
833 BUS_NOTIFY_DEL_DEVICE
, dev
);
834 device_pm_remove(dev
);
835 kobject_uevent(&dev
->kobj
, KOBJ_REMOVE
);
836 kobject_del(&dev
->kobj
);
842 * device_unregister - unregister device from system.
843 * @dev: device going away.
845 * We do this in two parts, like we do device_register(). First,
846 * we remove it from all the subsystems with device_del(), then
847 * we decrement the reference count via put_device(). If that
848 * is the final reference count, the device will be cleaned up
849 * via device_release() above. Otherwise, the structure will
850 * stick around until the final reference to the device is dropped.
852 void device_unregister(struct device
* dev
)
854 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev
->bus_id
);
860 static struct device
* next_device(struct klist_iter
* i
)
862 struct klist_node
* n
= klist_next(i
);
863 return n
? container_of(n
, struct device
, knode_parent
) : NULL
;
867 * device_for_each_child - device child iterator.
868 * @parent: parent struct device.
869 * @data: data for the callback.
870 * @fn: function to be called for each device.
872 * Iterate over @parent's child devices, and call @fn for each,
875 * We check the return of @fn each time. If it returns anything
876 * other than 0, we break out and return that value.
878 int device_for_each_child(struct device
* parent
, void * data
,
879 int (*fn
)(struct device
*, void *))
882 struct device
* child
;
885 klist_iter_init(&parent
->klist_children
, &i
);
886 while ((child
= next_device(&i
)) && !error
)
887 error
= fn(child
, data
);
893 * device_find_child - device iterator for locating a particular device.
894 * @parent: parent struct device
895 * @data: Data to pass to match function
896 * @match: Callback function to check device
898 * This is similar to the device_for_each_child() function above, but it
899 * returns a reference to a device that is 'found' for later use, as
900 * determined by the @match callback.
902 * The callback should return 0 if the device doesn't match and non-zero
903 * if it does. If the callback returns non-zero and a reference to the
904 * current device can be obtained, this function will return to the caller
905 * and not iterate over any more devices.
907 struct device
* device_find_child(struct device
*parent
, void *data
,
908 int (*match
)(struct device
*, void *))
911 struct device
*child
;
916 klist_iter_init(&parent
->klist_children
, &i
);
917 while ((child
= next_device(&i
)))
918 if (match(child
, data
) && get_device(child
))
924 int __init
devices_init(void)
926 return subsystem_register(&devices_subsys
);
929 EXPORT_SYMBOL_GPL(device_for_each_child
);
930 EXPORT_SYMBOL_GPL(device_find_child
);
932 EXPORT_SYMBOL_GPL(device_initialize
);
933 EXPORT_SYMBOL_GPL(device_add
);
934 EXPORT_SYMBOL_GPL(device_register
);
936 EXPORT_SYMBOL_GPL(device_del
);
937 EXPORT_SYMBOL_GPL(device_unregister
);
938 EXPORT_SYMBOL_GPL(get_device
);
939 EXPORT_SYMBOL_GPL(put_device
);
941 EXPORT_SYMBOL_GPL(device_create_file
);
942 EXPORT_SYMBOL_GPL(device_remove_file
);
945 static void device_create_release(struct device
*dev
)
947 pr_debug("%s called for %s\n", __FUNCTION__
, dev
->bus_id
);
952 * device_create - creates a device and registers it with sysfs
953 * @class: pointer to the struct class that this device should be registered to
954 * @parent: pointer to the parent struct device of this new device, if any
955 * @devt: the dev_t for the char device to be added
956 * @fmt: string for the device's name
958 * This function can be used by char device classes. A struct device
959 * will be created in sysfs, registered to the specified class.
961 * A "dev" file will be created, showing the dev_t for the device, if
962 * the dev_t is not 0,0.
963 * If a pointer to a parent struct device is passed in, the newly created
964 * struct device will be a child of that device in sysfs.
965 * The pointer to the struct device will be returned from the call.
966 * Any further sysfs files that might be required can be created using this
969 * Note: the struct class passed to this function must have previously
970 * been created with a call to class_create().
972 struct device
*device_create(struct class *class, struct device
*parent
,
973 dev_t devt
, const char *fmt
, ...)
976 struct device
*dev
= NULL
;
977 int retval
= -ENODEV
;
979 if (class == NULL
|| IS_ERR(class))
982 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
990 dev
->parent
= parent
;
991 dev
->release
= device_create_release
;
994 vsnprintf(dev
->bus_id
, BUS_ID_SIZE
, fmt
, args
);
996 retval
= device_register(dev
);
1004 return ERR_PTR(retval
);
1006 EXPORT_SYMBOL_GPL(device_create
);
1009 * device_destroy - removes a device that was created with device_create()
1010 * @class: pointer to the struct class that this device was registered with
1011 * @devt: the dev_t of the device that was previously registered
1013 * This call unregisters and cleans up a device that was created with a
1014 * call to device_create().
1016 void device_destroy(struct class *class, dev_t devt
)
1018 struct device
*dev
= NULL
;
1019 struct device
*dev_tmp
;
1022 list_for_each_entry(dev_tmp
, &class->devices
, node
) {
1023 if (dev_tmp
->devt
== devt
) {
1031 device_unregister(dev
);
1033 EXPORT_SYMBOL_GPL(device_destroy
);
1036 * device_rename - renames a device
1037 * @dev: the pointer to the struct device to be renamed
1038 * @new_name: the new name of the device
1040 int device_rename(struct device
*dev
, char *new_name
)
1042 char *old_class_name
= NULL
;
1043 char *new_class_name
= NULL
;
1044 char *old_symlink_name
= NULL
;
1047 dev
= get_device(dev
);
1051 pr_debug("DEVICE: renaming '%s' to '%s'\n", dev
->bus_id
, new_name
);
1053 #ifdef CONFIG_SYSFS_DEPRECATED
1054 if ((dev
->class) && (dev
->parent
))
1055 old_class_name
= make_class_name(dev
->class->name
, &dev
->kobj
);
1059 old_symlink_name
= kmalloc(BUS_ID_SIZE
, GFP_KERNEL
);
1060 if (!old_symlink_name
) {
1062 goto out_free_old_class
;
1064 strlcpy(old_symlink_name
, dev
->bus_id
, BUS_ID_SIZE
);
1067 strlcpy(dev
->bus_id
, new_name
, BUS_ID_SIZE
);
1069 error
= kobject_rename(&dev
->kobj
, new_name
);
1071 #ifdef CONFIG_SYSFS_DEPRECATED
1072 if (old_class_name
) {
1073 new_class_name
= make_class_name(dev
->class->name
, &dev
->kobj
);
1074 if (new_class_name
) {
1075 sysfs_create_link(&dev
->parent
->kobj
, &dev
->kobj
,
1077 sysfs_remove_link(&dev
->parent
->kobj
, old_class_name
);
1083 sysfs_remove_link(&dev
->class->subsys
.kset
.kobj
,
1085 sysfs_create_link(&dev
->class->subsys
.kset
.kobj
, &dev
->kobj
,
1090 kfree(new_class_name
);
1091 kfree(old_symlink_name
);
1093 kfree(old_class_name
);
1097 EXPORT_SYMBOL_GPL(device_rename
);
1099 static int device_move_class_links(struct device
*dev
,
1100 struct device
*old_parent
,
1101 struct device
*new_parent
)
1104 #ifdef CONFIG_SYSFS_DEPRECATED
1107 class_name
= make_class_name(dev
->class->name
, &dev
->kobj
);
1113 sysfs_remove_link(&dev
->kobj
, "device");
1114 sysfs_remove_link(&old_parent
->kobj
, class_name
);
1117 error
= sysfs_create_link(&dev
->kobj
, &new_parent
->kobj
,
1121 error
= sysfs_create_link(&new_parent
->kobj
, &dev
->kobj
,
1124 sysfs_remove_link(&dev
->kobj
, "device");
1133 sysfs_remove_link(&dev
->kobj
, "device");
1135 error
= sysfs_create_link(&dev
->kobj
, &new_parent
->kobj
,
1142 * device_move - moves a device to a new parent
1143 * @dev: the pointer to the struct device to be moved
1144 * @new_parent: the new parent of the device (can by NULL)
1146 int device_move(struct device
*dev
, struct device
*new_parent
)
1149 struct device
*old_parent
;
1150 struct kobject
*new_parent_kobj
;
1152 dev
= get_device(dev
);
1156 new_parent
= get_device(new_parent
);
1157 new_parent_kobj
= get_device_parent (dev
, new_parent
);
1158 if (IS_ERR(new_parent_kobj
)) {
1159 error
= PTR_ERR(new_parent_kobj
);
1160 put_device(new_parent
);
1163 pr_debug("DEVICE: moving '%s' to '%s'\n", dev
->bus_id
,
1164 new_parent
? new_parent
->bus_id
: "<NULL>");
1165 error
= kobject_move(&dev
->kobj
, new_parent_kobj
);
1167 put_device(new_parent
);
1170 old_parent
= dev
->parent
;
1171 dev
->parent
= new_parent
;
1173 klist_remove(&dev
->knode_parent
);
1175 klist_add_tail(&dev
->knode_parent
, &new_parent
->klist_children
);
1178 error
= device_move_class_links(dev
, old_parent
, new_parent
);
1180 /* We ignore errors on cleanup since we're hosed anyway... */
1181 device_move_class_links(dev
, new_parent
, old_parent
);
1182 if (!kobject_move(&dev
->kobj
, &old_parent
->kobj
)) {
1184 klist_remove(&dev
->knode_parent
);
1186 klist_add_tail(&dev
->knode_parent
,
1187 &old_parent
->klist_children
);
1189 put_device(new_parent
);
1193 put_device(old_parent
);
1199 EXPORT_SYMBOL_GPL(device_move
);