clean-move-defconfigs-stable.patch
[linux-2.6/openmoko-kernel/knife-kernel.git] / drivers / base / core.c
blob49fdee25b530f9a9a9fde875518a50efff732760
1 /*
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>
24 #include "base.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.
34 /**
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 if (!dev) {
46 printk(KERN_ERR"Null dev to dev_driver_string\n");
47 dump_stack();
48 return "*NULL*";
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)
59 static ssize_t
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);
64 ssize_t ret = -EIO;
66 if (dev_attr->show)
67 ret = dev_attr->show(dev, dev_attr, buf);
68 return ret;
71 static ssize_t
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);
77 ssize_t ret = -EIO;
79 if (dev_attr->store)
80 ret = dev_attr->store(dev, dev_attr, buf, count);
81 return ret;
84 static struct sysfs_ops dev_sysfs_ops = {
85 .show = dev_attr_show,
86 .store = dev_attr_store,
90 /**
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);
102 if (dev->release)
103 dev->release(dev);
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);
108 else {
109 printk(KERN_ERR "Device '%s' does not have a release() function, "
110 "it is broken and must be fixed.\n",
111 dev->bus_id);
112 WARN_ON(1);
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)
129 return 0;
130 if (dev->bus)
131 return 1;
132 if (dev->class)
133 return 1;
135 return 0;
138 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
140 struct device *dev = to_dev(kobj);
142 if (dev->bus)
143 return dev->bus->name;
144 if (dev->class)
145 return dev->class->name;
146 return NULL;
149 static int dev_uevent(struct kset *kset, struct kobject *kobj,
150 struct kobj_uevent_env *env)
152 struct device *dev = to_dev(kobj);
153 int retval = 0;
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);
164 if (dev->driver)
165 add_uevent_var(env, "DRIVER=%s", dev->driver->name);
167 #ifdef CONFIG_SYSFS_DEPRECATED
168 if (dev->class) {
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) {
175 const char *path;
177 path = kobject_get_path(&parent->kobj, GFP_KERNEL);
178 if (path) {
179 add_uevent_var(env, "PHYSDEVPATH=%s", path);
180 kfree(path);
183 add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name);
185 if (parent->driver)
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);
192 if (dev->driver)
193 add_uevent_var(env, "PHYSDEVDRIVER=%s", dev->driver->name);
195 #endif
197 /* have the bus specific function add its stuff */
198 if (dev->bus && dev->bus->uevent) {
199 retval = dev->bus->uevent(dev, env);
200 if (retval)
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);
208 if (retval)
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);
216 if (retval)
217 pr_debug("%s: dev_type uevent() returned %d\n",
218 __FUNCTION__, retval);
221 return 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,
231 char *buf)
233 struct kobject *top_kobj;
234 struct kset *kset;
235 struct kobj_uevent_env *env = NULL;
236 int i;
237 size_t count = 0;
238 int retval;
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;
244 if (!top_kobj->kset)
245 goto out;
247 kset = top_kobj->kset;
248 if (!kset->uevent_ops || !kset->uevent_ops->uevent)
249 goto out;
251 /* respect filter */
252 if (kset->uevent_ops && kset->uevent_ops->filter)
253 if (!kset->uevent_ops->filter(kset, &dev->kobj))
254 goto out;
256 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
257 if (!env)
258 return -ENOMEM;
260 /* let the kset specific function add its keys */
261 retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
262 if (retval)
263 goto out;
265 /* copy keys to file */
266 for (i = 0; i < env->envp_idx; i++)
267 count += sprintf(&buf[count], "%s\n", env->envp[i]);
268 out:
269 kfree(env);
270 return count;
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);
280 goto out;
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);
286 out:
287 return count;
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)
296 int error = 0;
297 int i;
299 if (attrs) {
300 for (i = 0; attr_name(attrs[i]); i++) {
301 error = device_create_file(dev, &attrs[i]);
302 if (error)
303 break;
305 if (error)
306 while (--i >= 0)
307 device_remove_file(dev, &attrs[i]);
309 return error;
312 static void device_remove_attributes(struct device *dev,
313 struct device_attribute *attrs)
315 int i;
317 if (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)
325 int error = 0;
326 int i;
328 if (groups) {
329 for (i = 0; groups[i]; i++) {
330 error = sysfs_create_group(&dev->kobj, groups[i]);
331 if (error) {
332 while (--i >= 0)
333 sysfs_remove_group(&dev->kobj, groups[i]);
334 break;
338 return error;
341 static void device_remove_groups(struct device *dev,
342 struct attribute_group **groups)
344 int i;
346 if (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;
355 int error;
357 if (class) {
358 error = device_add_attributes(dev, class->dev_attrs);
359 if (error)
360 return error;
363 if (type) {
364 error = device_add_groups(dev, type->groups);
365 if (error)
366 goto err_remove_class_attrs;
369 error = device_add_groups(dev, dev->groups);
370 if (error)
371 goto err_remove_type_groups;
373 return 0;
375 err_remove_type_groups:
376 if (type)
377 device_remove_groups(dev, type->groups);
378 err_remove_class_attrs:
379 if (class)
380 device_remove_attributes(dev, class->dev_attrs);
382 return error;
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);
392 if (type)
393 device_remove_groups(dev, type->groups);
395 if (class)
396 device_remove_attributes(dev, class->dev_attrs);
400 static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
401 char *buf)
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.
418 * @dev: device.
419 * @attr: device attribute descriptor.
422 int device_create_file(struct device * dev, struct device_attribute * attr)
424 int error = 0;
425 if (get_device(dev)) {
426 error = sysfs_create_file(&dev->kobj, &attr->attr);
427 put_device(dev);
429 return error;
433 * device_remove_file - remove sysfs attribute file.
434 * @dev: device.
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);
442 put_device(dev);
447 * device_create_bin_file - create sysfs binary attribute file for device.
448 * @dev: device.
449 * @attr: device binary attribute descriptor.
451 int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
453 int error = -EINVAL;
454 if (dev)
455 error = sysfs_create_bin_file(&dev->kobj, attr);
456 return error;
458 EXPORT_SYMBOL_GPL(device_create_bin_file);
461 * device_remove_bin_file - remove sysfs binary attribute file
462 * @dev: device.
463 * @attr: device binary attribute descriptor.
465 void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
467 if (dev)
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
474 * @dev: 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
485 * @func returns.
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);
509 get_device(dev);
512 static void klist_children_put(struct klist_node *n)
514 struct device *dev = container_of(n, struct device, knode_parent);
516 put_device(dev);
521 * device_initialize - init device structure.
522 * @dev: device.
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,
536 klist_children_put);
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
554 * udevs happy
556 if (dev->class && (!parent || parent->class != dev->class))
557 return &dev->class->subsys.kobj;
558 else if (parent)
559 return &parent->kobj;
561 return NULL;
563 #else
564 static struct kobject *virtual_device_parent(struct device *dev)
566 static struct kobject *virtual_dir = NULL;
568 if (!virtual_dir)
569 virtual_dir = kobject_add_dir(&devices_subsys.kobj, "virtual");
571 return virtual_dir;
574 static struct kobject * get_device_parent(struct device *dev,
575 struct device *parent)
577 if (dev->class) {
578 struct kobject *kobj = NULL;
579 struct kobject *parent_kobj;
580 struct kobject *k;
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.
587 if (parent == NULL)
588 parent_kobj = virtual_device_parent(dev);
589 else if (parent->class)
590 return &parent->kobj;
591 else
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);
599 break;
601 spin_unlock(&dev->class->class_dirs.list_lock);
602 if (kobj)
603 return kobj;
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);
610 if (parent)
611 return &parent->kobj;
612 return NULL;
614 #endif
616 static int setup_parent(struct device *dev, struct device *parent)
618 struct kobject *kobj;
619 kobj = get_device_parent(dev, parent);
620 if (IS_ERR(kobj))
621 return PTR_ERR(kobj);
622 if (kobj)
623 dev->kobj.parent = kobj;
624 return 0;
627 static int device_add_class_symlinks(struct device *dev)
629 int error;
631 if (!dev->class)
632 return 0;
633 error = sysfs_create_link(&dev->kobj, &dev->class->subsys.kobj,
634 "subsystem");
635 if (error)
636 goto out;
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,
643 dev->bus_id);
644 if (error)
645 goto out_subsys;
647 if (dev->parent) {
648 #ifdef CONFIG_SYSFS_DEPRECATED
650 struct device *parent = dev->parent;
651 char *class_name;
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,
661 &parent->kobj,
662 "device");
663 if (error)
664 goto out_busid;
666 class_name = make_class_name(dev->class->name,
667 &dev->kobj);
668 if (class_name)
669 error = sysfs_create_link(&dev->parent->kobj,
670 &dev->kobj, class_name);
671 kfree(class_name);
672 if (error)
673 goto out_device;
675 #else
676 error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
677 "device");
678 if (error)
679 goto out_busid;
680 #endif
682 return 0;
684 #ifdef CONFIG_SYSFS_DEPRECATED
685 out_device:
686 if (dev->parent)
687 sysfs_remove_link(&dev->kobj, "device");
688 #endif
689 out_busid:
690 if (dev->kobj.parent != &dev->class->subsys.kobj)
691 sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);
692 out_subsys:
693 sysfs_remove_link(&dev->kobj, "subsystem");
694 out:
695 return error;
698 static void device_remove_class_symlinks(struct device *dev)
700 if (!dev->class)
701 return;
702 if (dev->parent) {
703 #ifdef CONFIG_SYSFS_DEPRECATED
704 char *class_name;
706 class_name = make_class_name(dev->class->name, &dev->kobj);
707 if (class_name) {
708 sysfs_remove_link(&dev->parent->kobj, class_name);
709 kfree(class_name);
711 #endif
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.
721 * @dev: device.
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;
734 int error = -EINVAL;
736 dev = get_device(dev);
737 if (!dev || !strlen(dev->bus_id))
738 goto Error;
740 pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
742 parent = get_device(dev->parent);
743 error = setup_parent(dev, parent);
744 if (error)
745 goto Error;
747 /* first, register with generic layer. */
748 kobject_set_name(&dev->kobj, "%s", dev->bus_id);
749 error = kobject_add(&dev->kobj);
750 if (error)
751 goto Error;
753 /* notify platform of device entry */
754 if (platform_notify)
755 platform_notify(dev);
757 /* notify clients of device entry (new way) */
758 if (dev->bus)
759 blocking_notifier_call_chain(&dev->bus->bus_notifier,
760 BUS_NOTIFY_ADD_DEVICE, dev);
762 error = device_create_file(dev, &uevent_attr);
763 if (error)
764 goto attrError;
766 if (MAJOR(dev->devt)) {
767 error = device_create_file(dev, &devt_attr);
768 if (error)
769 goto ueventattrError;
772 error = device_add_class_symlinks(dev);
773 if (error)
774 goto SymlinkError;
775 error = device_add_attrs(dev);
776 if (error)
777 goto AttrsError;
778 error = dpm_sysfs_add(dev);
779 if (error)
780 goto PMError;
781 device_pm_add(dev);
782 error = bus_add_device(dev);
783 if (error)
784 goto BusError;
785 kobject_uevent(&dev->kobj, KOBJ_ADD);
786 bus_attach_device(dev);
787 if (parent)
788 klist_add_tail(&dev->knode_parent, &parent->klist_children);
790 if (dev->class) {
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);
801 Done:
802 put_device(dev);
803 return error;
804 BusError:
805 device_pm_remove(dev);
806 dpm_sysfs_remove(dev);
807 PMError:
808 if (dev->bus)
809 blocking_notifier_call_chain(&dev->bus->bus_notifier,
810 BUS_NOTIFY_DEL_DEVICE, dev);
811 device_remove_attrs(dev);
812 AttrsError:
813 device_remove_class_symlinks(dev);
814 SymlinkError:
815 if (MAJOR(dev->devt))
816 device_remove_file(dev, &devt_attr);
818 if (dev->class) {
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,
824 dev->bus_id);
825 if (parent) {
826 #ifdef CONFIG_SYSFS_DEPRECATED
827 char *class_name = make_class_name(dev->class->name,
828 &dev->kobj);
829 if (class_name)
830 sysfs_remove_link(&dev->parent->kobj,
831 class_name);
832 kfree(class_name);
833 #endif
834 sysfs_remove_link(&dev->kobj, "device");
837 ueventattrError:
838 device_remove_file(dev, &uevent_attr);
839 attrError:
840 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
841 kobject_del(&dev->kobj);
842 Error:
843 if (parent)
844 put_device(parent);
845 goto Done;
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.
870 * @dev: 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
874 * pointer passed in.
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)
889 if (dev)
890 kobject_put(&dev->kobj);
895 * device_del - delete device from system.
896 * @dev: device.
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;
913 if (parent)
914 klist_del(&dev->knode_parent);
915 if (MAJOR(dev->devt))
916 device_remove_file(dev, &devt_attr);
917 if (dev->class) {
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,
923 dev->bus_id);
924 if (parent) {
925 #ifdef CONFIG_SYSFS_DEPRECATED
926 char *class_name = make_class_name(dev->class->name,
927 &dev->kobj);
928 if (class_name)
929 sysfs_remove_link(&dev->parent->kobj,
930 class_name);
931 kfree(class_name);
932 #endif
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) {
947 struct device *d;
948 int other = 0;
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) {
956 if (d == dev)
957 continue;
958 if (d->kobj.parent == dev->kobj.parent) {
959 other = 1;
960 break;
963 if (!other)
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);
986 if (dev->bus)
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);
992 if (parent)
993 put_device(parent);
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);
1010 device_del(dev);
1011 put_device(dev);
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,
1028 * passing it @data.
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;
1038 int error = 0;
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);
1044 return error;
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;
1068 if (!parent)
1069 return NULL;
1071 klist_iter_init(&parent->klist_children, &i);
1072 while ((child = next_device(&i)))
1073 if (match(child, data) && get_device(child))
1074 break;
1075 klist_iter_exit(&i);
1076 return child;
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);
1103 kfree(dev);
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
1122 * pointer.
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, ...)
1130 va_list args;
1131 struct device *dev = NULL;
1132 int retval = -ENODEV;
1134 if (class == NULL || IS_ERR(class))
1135 goto error;
1137 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1138 if (!dev) {
1139 retval = -ENOMEM;
1140 goto error;
1143 dev->devt = devt;
1144 dev->class = class;
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);
1150 va_end(args);
1151 retval = device_register(dev);
1152 if (retval)
1153 goto error;
1155 return dev;
1157 error:
1158 kfree(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;
1176 down(&class->sem);
1177 list_for_each_entry(dev_tmp, &class->devices, node) {
1178 if (dev_tmp->devt == devt) {
1179 dev = dev_tmp;
1180 break;
1183 up(&class->sem);
1185 if (dev)
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;
1200 int error;
1202 dev = get_device(dev);
1203 if (!dev)
1204 return -EINVAL;
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);
1211 #endif
1213 old_device_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
1214 if (!old_device_name) {
1215 error = -ENOMEM;
1216 goto out;
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);
1222 if (error) {
1223 strlcpy(dev->bus_id, old_device_name, BUS_ID_SIZE);
1224 goto out;
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);
1233 if (error)
1234 goto out;
1235 sysfs_remove_link(&dev->parent->kobj, old_class_name);
1238 #else
1239 if (dev->class) {
1240 sysfs_remove_link(&dev->class->subsys.kobj, old_device_name);
1241 error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj,
1242 dev->bus_id);
1243 if (error) {
1244 dev_err(dev, "%s: sysfs_create_symlink failed (%d)\n",
1245 __FUNCTION__, error);
1248 #endif
1250 out:
1251 put_device(dev);
1253 kfree(new_class_name);
1254 kfree(old_class_name);
1255 kfree(old_device_name);
1257 return error;
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)
1265 int error = 0;
1266 #ifdef CONFIG_SYSFS_DEPRECATED
1267 char *class_name;
1269 class_name = make_class_name(dev->class->name, &dev->kobj);
1270 if (!class_name) {
1271 error = -ENOMEM;
1272 goto out;
1274 if (old_parent) {
1275 sysfs_remove_link(&dev->kobj, "device");
1276 sysfs_remove_link(&old_parent->kobj, class_name);
1278 if (new_parent) {
1279 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1280 "device");
1281 if (error)
1282 goto out;
1283 error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
1284 class_name);
1285 if (error)
1286 sysfs_remove_link(&dev->kobj, "device");
1288 else
1289 error = 0;
1290 out:
1291 kfree(class_name);
1292 return error;
1293 #else
1294 if (old_parent)
1295 sysfs_remove_link(&dev->kobj, "device");
1296 if (new_parent)
1297 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1298 "device");
1299 return error;
1300 #endif
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)
1310 int error;
1311 struct device *old_parent;
1312 struct kobject *new_parent_kobj;
1314 dev = get_device(dev);
1315 if (!dev)
1316 return -EINVAL;
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);
1323 goto out;
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);
1328 if (error) {
1329 put_device(new_parent);
1330 goto out;
1332 old_parent = dev->parent;
1333 dev->parent = new_parent;
1334 if (old_parent)
1335 klist_remove(&dev->knode_parent);
1336 if (new_parent)
1337 klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
1338 if (!dev->class)
1339 goto out_put;
1340 error = device_move_class_links(dev, old_parent, new_parent);
1341 if (error) {
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)) {
1345 if (new_parent)
1346 klist_remove(&dev->knode_parent);
1347 if (old_parent)
1348 klist_add_tail(&dev->knode_parent,
1349 &old_parent->klist_children);
1351 put_device(new_parent);
1352 goto out;
1354 out_put:
1355 put_device(old_parent);
1356 out:
1357 put_device(dev);
1358 return error;
1361 EXPORT_SYMBOL_GPL(device_move);