Driver core: suppress uevents via filter
[linux-2.6/openmoko-kernel.git] / drivers / base / core.c
blobc34a4d8842e12c1fb911c405b290637876a6ce92
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 return dev->driver ? dev->driver->name :
46 (dev->bus ? dev->bus->name :
47 (dev->class ? dev->class->name : ""));
49 EXPORT_SYMBOL(dev_driver_string);
51 #define to_dev(obj) container_of(obj, struct device, kobj)
52 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
54 static ssize_t
55 dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
57 struct device_attribute * dev_attr = to_dev_attr(attr);
58 struct device * dev = to_dev(kobj);
59 ssize_t ret = -EIO;
61 if (dev_attr->show)
62 ret = dev_attr->show(dev, dev_attr, buf);
63 return ret;
66 static ssize_t
67 dev_attr_store(struct kobject * kobj, struct attribute * attr,
68 const char * buf, size_t count)
70 struct device_attribute * dev_attr = to_dev_attr(attr);
71 struct device * dev = to_dev(kobj);
72 ssize_t ret = -EIO;
74 if (dev_attr->store)
75 ret = dev_attr->store(dev, dev_attr, buf, count);
76 return ret;
79 static struct sysfs_ops dev_sysfs_ops = {
80 .show = dev_attr_show,
81 .store = dev_attr_store,
85 /**
86 * device_release - free device structure.
87 * @kobj: device's kobject.
89 * This is called once the reference count for the object
90 * reaches 0. We forward the call to the device's release
91 * method, which should handle actually freeing the structure.
93 static void device_release(struct kobject * kobj)
95 struct device * dev = to_dev(kobj);
97 if (dev->release)
98 dev->release(dev);
99 else if (dev->type && dev->type->release)
100 dev->type->release(dev);
101 else if (dev->class && dev->class->dev_release)
102 dev->class->dev_release(dev);
103 else {
104 printk(KERN_ERR "Device '%s' does not have a release() function, "
105 "it is broken and must be fixed.\n",
106 dev->bus_id);
107 WARN_ON(1);
111 static struct kobj_type ktype_device = {
112 .release = device_release,
113 .sysfs_ops = &dev_sysfs_ops,
117 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
119 struct kobj_type *ktype = get_ktype(kobj);
121 if (ktype == &ktype_device) {
122 struct device *dev = to_dev(kobj);
123 if (dev->uevent_suppress)
124 return 0;
125 if (dev->bus)
126 return 1;
127 if (dev->class)
128 return 1;
130 return 0;
133 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
135 struct device *dev = to_dev(kobj);
137 if (dev->bus)
138 return dev->bus->name;
139 if (dev->class)
140 return dev->class->name;
141 return NULL;
144 static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
145 int num_envp, char *buffer, int buffer_size)
147 struct device *dev = to_dev(kobj);
148 int i = 0;
149 int length = 0;
150 int retval = 0;
152 /* add the major/minor if present */
153 if (MAJOR(dev->devt)) {
154 add_uevent_var(envp, num_envp, &i,
155 buffer, buffer_size, &length,
156 "MAJOR=%u", MAJOR(dev->devt));
157 add_uevent_var(envp, num_envp, &i,
158 buffer, buffer_size, &length,
159 "MINOR=%u", MINOR(dev->devt));
162 if (dev->type && dev->type->name)
163 add_uevent_var(envp, num_envp, &i,
164 buffer, buffer_size, &length,
165 "DEVTYPE=%s", dev->type->name);
167 if (dev->driver)
168 add_uevent_var(envp, num_envp, &i,
169 buffer, buffer_size, &length,
170 "DRIVER=%s", dev->driver->name);
172 #ifdef CONFIG_SYSFS_DEPRECATED
173 if (dev->class) {
174 struct device *parent = dev->parent;
176 /* find first bus device in parent chain */
177 while (parent && !parent->bus)
178 parent = parent->parent;
179 if (parent && parent->bus) {
180 const char *path;
182 path = kobject_get_path(&parent->kobj, GFP_KERNEL);
183 add_uevent_var(envp, num_envp, &i,
184 buffer, buffer_size, &length,
185 "PHYSDEVPATH=%s", path);
186 kfree(path);
188 add_uevent_var(envp, num_envp, &i,
189 buffer, buffer_size, &length,
190 "PHYSDEVBUS=%s", parent->bus->name);
192 if (parent->driver)
193 add_uevent_var(envp, num_envp, &i,
194 buffer, buffer_size, &length,
195 "PHYSDEVDRIVER=%s", parent->driver->name);
197 } else if (dev->bus) {
198 add_uevent_var(envp, num_envp, &i,
199 buffer, buffer_size, &length,
200 "PHYSDEVBUS=%s", dev->bus->name);
202 if (dev->driver)
203 add_uevent_var(envp, num_envp, &i,
204 buffer, buffer_size, &length,
205 "PHYSDEVDRIVER=%s", dev->driver->name);
207 #endif
209 /* terminate, set to next free slot, shrink available space */
210 envp[i] = NULL;
211 envp = &envp[i];
212 num_envp -= i;
213 buffer = &buffer[length];
214 buffer_size -= length;
216 if (dev->bus && dev->bus->uevent) {
217 /* have the bus specific function add its stuff */
218 retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size);
219 if (retval)
220 pr_debug ("%s: bus uevent() returned %d\n",
221 __FUNCTION__, retval);
224 if (dev->class && dev->class->dev_uevent) {
225 /* have the class specific function add its stuff */
226 retval = dev->class->dev_uevent(dev, envp, num_envp, buffer, buffer_size);
227 if (retval)
228 pr_debug("%s: class uevent() returned %d\n",
229 __FUNCTION__, retval);
232 if (dev->type && dev->type->uevent) {
233 /* have the device type specific fuction add its stuff */
234 retval = dev->type->uevent(dev, envp, num_envp, buffer, buffer_size);
235 if (retval)
236 pr_debug("%s: dev_type uevent() returned %d\n",
237 __FUNCTION__, retval);
240 return retval;
243 static struct kset_uevent_ops device_uevent_ops = {
244 .filter = dev_uevent_filter,
245 .name = dev_uevent_name,
246 .uevent = dev_uevent,
249 static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
250 const char *buf, size_t count)
252 kobject_uevent(&dev->kobj, KOBJ_ADD);
253 return count;
256 static int device_add_attributes(struct device *dev,
257 struct device_attribute *attrs)
259 int error = 0;
260 int i;
262 if (attrs) {
263 for (i = 0; attr_name(attrs[i]); i++) {
264 error = device_create_file(dev, &attrs[i]);
265 if (error)
266 break;
268 if (error)
269 while (--i >= 0)
270 device_remove_file(dev, &attrs[i]);
272 return error;
275 static void device_remove_attributes(struct device *dev,
276 struct device_attribute *attrs)
278 int i;
280 if (attrs)
281 for (i = 0; attr_name(attrs[i]); i++)
282 device_remove_file(dev, &attrs[i]);
285 static int device_add_groups(struct device *dev,
286 struct attribute_group **groups)
288 int error = 0;
289 int i;
291 if (groups) {
292 for (i = 0; groups[i]; i++) {
293 error = sysfs_create_group(&dev->kobj, groups[i]);
294 if (error) {
295 while (--i >= 0)
296 sysfs_remove_group(&dev->kobj, groups[i]);
297 break;
301 return error;
304 static void device_remove_groups(struct device *dev,
305 struct attribute_group **groups)
307 int i;
309 if (groups)
310 for (i = 0; groups[i]; i++)
311 sysfs_remove_group(&dev->kobj, groups[i]);
314 static int device_add_attrs(struct device *dev)
316 struct class *class = dev->class;
317 struct device_type *type = dev->type;
318 int error;
320 if (class) {
321 error = device_add_attributes(dev, class->dev_attrs);
322 if (error)
323 return error;
326 if (type) {
327 error = device_add_groups(dev, type->groups);
328 if (error)
329 goto err_remove_class_attrs;
332 error = device_add_groups(dev, dev->groups);
333 if (error)
334 goto err_remove_type_groups;
336 return 0;
338 err_remove_type_groups:
339 if (type)
340 device_remove_groups(dev, type->groups);
341 err_remove_class_attrs:
342 if (class)
343 device_remove_attributes(dev, class->dev_attrs);
345 return error;
348 static void device_remove_attrs(struct device *dev)
350 struct class *class = dev->class;
351 struct device_type *type = dev->type;
353 device_remove_groups(dev, dev->groups);
355 if (type)
356 device_remove_groups(dev, type->groups);
358 if (class)
359 device_remove_attributes(dev, class->dev_attrs);
363 static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
364 char *buf)
366 return print_dev_t(buf, dev->devt);
370 * devices_subsys - structure to be registered with kobject core.
373 decl_subsys(devices, &ktype_device, &device_uevent_ops);
377 * device_create_file - create sysfs attribute file for device.
378 * @dev: device.
379 * @attr: device attribute descriptor.
382 int device_create_file(struct device * dev, struct device_attribute * attr)
384 int error = 0;
385 if (get_device(dev)) {
386 error = sysfs_create_file(&dev->kobj, &attr->attr);
387 put_device(dev);
389 return error;
393 * device_remove_file - remove sysfs attribute file.
394 * @dev: device.
395 * @attr: device attribute descriptor.
398 void device_remove_file(struct device * dev, struct device_attribute * attr)
400 if (get_device(dev)) {
401 sysfs_remove_file(&dev->kobj, &attr->attr);
402 put_device(dev);
407 * device_create_bin_file - create sysfs binary attribute file for device.
408 * @dev: device.
409 * @attr: device binary attribute descriptor.
411 int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
413 int error = -EINVAL;
414 if (dev)
415 error = sysfs_create_bin_file(&dev->kobj, attr);
416 return error;
418 EXPORT_SYMBOL_GPL(device_create_bin_file);
421 * device_remove_bin_file - remove sysfs binary attribute file
422 * @dev: device.
423 * @attr: device binary attribute descriptor.
425 void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
427 if (dev)
428 sysfs_remove_bin_file(&dev->kobj, attr);
430 EXPORT_SYMBOL_GPL(device_remove_bin_file);
433 * device_schedule_callback - helper to schedule a callback for a device
434 * @dev: device.
435 * @func: callback function to invoke later.
437 * Attribute methods must not unregister themselves or their parent device
438 * (which would amount to the same thing). Attempts to do so will deadlock,
439 * since unregistration is mutually exclusive with driver callbacks.
441 * Instead methods can call this routine, which will attempt to allocate
442 * and schedule a workqueue request to call back @func with @dev as its
443 * argument in the workqueue's process context. @dev will be pinned until
444 * @func returns.
446 * Returns 0 if the request was submitted, -ENOMEM if storage could not
447 * be allocated.
449 * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an
450 * underlying sysfs routine (since it is intended for use by attribute
451 * methods), and if sysfs isn't available you'll get nothing but -ENOSYS.
453 int device_schedule_callback(struct device *dev,
454 void (*func)(struct device *))
456 return sysfs_schedule_callback(&dev->kobj,
457 (void (*)(void *)) func, dev);
459 EXPORT_SYMBOL_GPL(device_schedule_callback);
461 static void klist_children_get(struct klist_node *n)
463 struct device *dev = container_of(n, struct device, knode_parent);
465 get_device(dev);
468 static void klist_children_put(struct klist_node *n)
470 struct device *dev = container_of(n, struct device, knode_parent);
472 put_device(dev);
477 * device_initialize - init device structure.
478 * @dev: device.
480 * This prepares the device for use by other layers,
481 * including adding it to the device hierarchy.
482 * It is the first half of device_register(), if called by
483 * that, though it can also be called separately, so one
484 * may use @dev's fields (e.g. the refcount).
487 void device_initialize(struct device *dev)
489 kobj_set_kset_s(dev, devices_subsys);
490 kobject_init(&dev->kobj);
491 klist_init(&dev->klist_children, klist_children_get,
492 klist_children_put);
493 INIT_LIST_HEAD(&dev->dma_pools);
494 INIT_LIST_HEAD(&dev->node);
495 init_MUTEX(&dev->sem);
496 spin_lock_init(&dev->devres_lock);
497 INIT_LIST_HEAD(&dev->devres_head);
498 device_init_wakeup(dev, 0);
499 set_dev_node(dev, -1);
502 #ifdef CONFIG_SYSFS_DEPRECATED
503 static struct kobject * get_device_parent(struct device *dev,
504 struct device *parent)
506 /* Set the parent to the class, not the parent device */
507 /* this keeps sysfs from having a symlink to make old udevs happy */
508 if (dev->class)
509 return &dev->class->subsys.kset.kobj;
510 else if (parent)
511 return &parent->kobj;
513 return NULL;
515 #else
516 static struct kobject *virtual_device_parent(struct device *dev)
518 static struct kobject *virtual_dir = NULL;
520 if (!virtual_dir)
521 virtual_dir = kobject_add_dir(&devices_subsys.kset.kobj, "virtual");
523 return virtual_dir;
526 static struct kobject * get_device_parent(struct device *dev,
527 struct device *parent)
529 if (dev->class) {
530 struct kobject *kobj = NULL;
531 struct kobject *parent_kobj;
532 struct kobject *k;
535 * If we have no parent, we live in "virtual".
536 * Class-devices with a bus-device as parent, live
537 * in a class-directory to prevent namespace collisions.
539 if (parent == NULL)
540 parent_kobj = virtual_device_parent(dev);
541 else if (parent->class)
542 return &parent->kobj;
543 else
544 parent_kobj = &parent->kobj;
546 /* find our class-directory at the parent and reference it */
547 spin_lock(&dev->class->class_dirs.list_lock);
548 list_for_each_entry(k, &dev->class->class_dirs.list, entry)
549 if (k->parent == parent_kobj) {
550 kobj = kobject_get(k);
551 break;
553 spin_unlock(&dev->class->class_dirs.list_lock);
554 if (kobj)
555 return kobj;
557 /* or create a new class-directory at the parent device */
558 return kobject_kset_add_dir(&dev->class->class_dirs,
559 parent_kobj, dev->class->name);
562 if (parent)
563 return &parent->kobj;
564 return NULL;
566 #endif
568 static int setup_parent(struct device *dev, struct device *parent)
570 struct kobject *kobj;
571 kobj = get_device_parent(dev, parent);
572 if (IS_ERR(kobj))
573 return PTR_ERR(kobj);
574 if (kobj)
575 dev->kobj.parent = kobj;
576 return 0;
580 * device_add - add device to device hierarchy.
581 * @dev: device.
583 * This is part 2 of device_register(), though may be called
584 * separately _iff_ device_initialize() has been called separately.
586 * This adds it to the kobject hierarchy via kobject_add(), adds it
587 * to the global and sibling lists for the device, then
588 * adds it to the other relevant subsystems of the driver model.
590 int device_add(struct device *dev)
592 struct device *parent = NULL;
593 char *class_name = NULL;
594 struct class_interface *class_intf;
595 int error = -EINVAL;
597 dev = get_device(dev);
598 if (!dev || !strlen(dev->bus_id))
599 goto Error;
601 pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
603 parent = get_device(dev->parent);
604 error = setup_parent(dev, parent);
605 if (error)
606 goto Error;
608 /* first, register with generic layer. */
609 kobject_set_name(&dev->kobj, "%s", dev->bus_id);
610 error = kobject_add(&dev->kobj);
611 if (error)
612 goto Error;
614 /* notify platform of device entry */
615 if (platform_notify)
616 platform_notify(dev);
618 /* notify clients of device entry (new way) */
619 if (dev->bus)
620 blocking_notifier_call_chain(&dev->bus->bus_notifier,
621 BUS_NOTIFY_ADD_DEVICE, dev);
623 dev->uevent_attr.attr.name = "uevent";
624 dev->uevent_attr.attr.mode = S_IWUSR;
625 if (dev->driver)
626 dev->uevent_attr.attr.owner = dev->driver->owner;
627 dev->uevent_attr.store = store_uevent;
628 error = device_create_file(dev, &dev->uevent_attr);
629 if (error)
630 goto attrError;
632 if (MAJOR(dev->devt)) {
633 struct device_attribute *attr;
634 attr = kzalloc(sizeof(*attr), GFP_KERNEL);
635 if (!attr) {
636 error = -ENOMEM;
637 goto ueventattrError;
639 attr->attr.name = "dev";
640 attr->attr.mode = S_IRUGO;
641 if (dev->driver)
642 attr->attr.owner = dev->driver->owner;
643 attr->show = show_dev;
644 error = device_create_file(dev, attr);
645 if (error) {
646 kfree(attr);
647 goto ueventattrError;
650 dev->devt_attr = attr;
653 if (dev->class) {
654 sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
655 "subsystem");
656 /* If this is not a "fake" compatible device, then create the
657 * symlink from the class to the device. */
658 if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
659 sysfs_create_link(&dev->class->subsys.kset.kobj,
660 &dev->kobj, dev->bus_id);
661 if (parent) {
662 sysfs_create_link(&dev->kobj, &dev->parent->kobj,
663 "device");
664 #ifdef CONFIG_SYSFS_DEPRECATED
665 class_name = make_class_name(dev->class->name,
666 &dev->kobj);
667 if (class_name)
668 sysfs_create_link(&dev->parent->kobj,
669 &dev->kobj, class_name);
670 #endif
674 if ((error = device_add_attrs(dev)))
675 goto AttrsError;
676 if ((error = device_pm_add(dev)))
677 goto PMError;
678 if ((error = bus_add_device(dev)))
679 goto BusError;
680 kobject_uevent(&dev->kobj, KOBJ_ADD);
681 bus_attach_device(dev);
682 if (parent)
683 klist_add_tail(&dev->knode_parent, &parent->klist_children);
685 if (dev->class) {
686 down(&dev->class->sem);
687 /* tie the class to the device */
688 list_add_tail(&dev->node, &dev->class->devices);
690 /* notify any interfaces that the device is here */
691 list_for_each_entry(class_intf, &dev->class->interfaces, node)
692 if (class_intf->add_dev)
693 class_intf->add_dev(dev, class_intf);
694 up(&dev->class->sem);
696 Done:
697 kfree(class_name);
698 put_device(dev);
699 return error;
700 BusError:
701 device_pm_remove(dev);
702 PMError:
703 if (dev->bus)
704 blocking_notifier_call_chain(&dev->bus->bus_notifier,
705 BUS_NOTIFY_DEL_DEVICE, dev);
706 device_remove_attrs(dev);
707 AttrsError:
708 if (dev->devt_attr) {
709 device_remove_file(dev, dev->devt_attr);
710 kfree(dev->devt_attr);
713 if (dev->class) {
714 sysfs_remove_link(&dev->kobj, "subsystem");
715 /* If this is not a "fake" compatible device, remove the
716 * symlink from the class to the device. */
717 if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
718 sysfs_remove_link(&dev->class->subsys.kset.kobj,
719 dev->bus_id);
720 if (parent) {
721 #ifdef CONFIG_SYSFS_DEPRECATED
722 char *class_name = make_class_name(dev->class->name,
723 &dev->kobj);
724 if (class_name)
725 sysfs_remove_link(&dev->parent->kobj,
726 class_name);
727 kfree(class_name);
728 #endif
729 sysfs_remove_link(&dev->kobj, "device");
732 ueventattrError:
733 device_remove_file(dev, &dev->uevent_attr);
734 attrError:
735 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
736 kobject_del(&dev->kobj);
737 Error:
738 if (parent)
739 put_device(parent);
740 goto Done;
745 * device_register - register a device with the system.
746 * @dev: pointer to the device structure
748 * This happens in two clean steps - initialize the device
749 * and add it to the system. The two steps can be called
750 * separately, but this is the easiest and most common.
751 * I.e. you should only call the two helpers separately if
752 * have a clearly defined need to use and refcount the device
753 * before it is added to the hierarchy.
756 int device_register(struct device *dev)
758 device_initialize(dev);
759 return device_add(dev);
764 * get_device - increment reference count for device.
765 * @dev: device.
767 * This simply forwards the call to kobject_get(), though
768 * we do take care to provide for the case that we get a NULL
769 * pointer passed in.
772 struct device * get_device(struct device * dev)
774 return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
779 * put_device - decrement reference count.
780 * @dev: device in question.
782 void put_device(struct device * dev)
784 if (dev)
785 kobject_put(&dev->kobj);
790 * device_del - delete device from system.
791 * @dev: device.
793 * This is the first part of the device unregistration
794 * sequence. This removes the device from the lists we control
795 * from here, has it removed from the other driver model
796 * subsystems it was added to in device_add(), and removes it
797 * from the kobject hierarchy.
799 * NOTE: this should be called manually _iff_ device_add() was
800 * also called manually.
803 void device_del(struct device * dev)
805 struct device * parent = dev->parent;
806 struct class_interface *class_intf;
808 if (parent)
809 klist_del(&dev->knode_parent);
810 if (dev->devt_attr) {
811 device_remove_file(dev, dev->devt_attr);
812 kfree(dev->devt_attr);
814 if (dev->class) {
815 sysfs_remove_link(&dev->kobj, "subsystem");
816 /* If this is not a "fake" compatible device, remove the
817 * symlink from the class to the device. */
818 if (dev->kobj.parent != &dev->class->subsys.kset.kobj)
819 sysfs_remove_link(&dev->class->subsys.kset.kobj,
820 dev->bus_id);
821 if (parent) {
822 #ifdef CONFIG_SYSFS_DEPRECATED
823 char *class_name = make_class_name(dev->class->name,
824 &dev->kobj);
825 if (class_name)
826 sysfs_remove_link(&dev->parent->kobj,
827 class_name);
828 kfree(class_name);
829 #endif
830 sysfs_remove_link(&dev->kobj, "device");
833 down(&dev->class->sem);
834 /* notify any interfaces that the device is now gone */
835 list_for_each_entry(class_intf, &dev->class->interfaces, node)
836 if (class_intf->remove_dev)
837 class_intf->remove_dev(dev, class_intf);
838 /* remove the device from the class list */
839 list_del_init(&dev->node);
840 up(&dev->class->sem);
842 /* If we live in a parent class-directory, unreference it */
843 if (dev->kobj.parent->kset == &dev->class->class_dirs) {
844 struct device *d;
845 int other = 0;
848 * if we are the last child of our class, delete
849 * our class-directory at this parent
851 down(&dev->class->sem);
852 list_for_each_entry(d, &dev->class->devices, node) {
853 if (d == dev)
854 continue;
855 if (d->kobj.parent == dev->kobj.parent) {
856 other = 1;
857 break;
860 if (!other)
861 kobject_del(dev->kobj.parent);
863 kobject_put(dev->kobj.parent);
864 up(&dev->class->sem);
867 device_remove_file(dev, &dev->uevent_attr);
868 device_remove_attrs(dev);
869 bus_remove_device(dev);
872 * Some platform devices are driven without driver attached
873 * and managed resources may have been acquired. Make sure
874 * all resources are released.
876 devres_release_all(dev);
878 /* Notify the platform of the removal, in case they
879 * need to do anything...
881 if (platform_notify_remove)
882 platform_notify_remove(dev);
883 if (dev->bus)
884 blocking_notifier_call_chain(&dev->bus->bus_notifier,
885 BUS_NOTIFY_DEL_DEVICE, dev);
886 device_pm_remove(dev);
887 kobject_uevent(&dev->kobj, KOBJ_REMOVE);
888 kobject_del(&dev->kobj);
889 if (parent)
890 put_device(parent);
894 * device_unregister - unregister device from system.
895 * @dev: device going away.
897 * We do this in two parts, like we do device_register(). First,
898 * we remove it from all the subsystems with device_del(), then
899 * we decrement the reference count via put_device(). If that
900 * is the final reference count, the device will be cleaned up
901 * via device_release() above. Otherwise, the structure will
902 * stick around until the final reference to the device is dropped.
904 void device_unregister(struct device * dev)
906 pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
907 device_del(dev);
908 put_device(dev);
912 static struct device * next_device(struct klist_iter * i)
914 struct klist_node * n = klist_next(i);
915 return n ? container_of(n, struct device, knode_parent) : NULL;
919 * device_for_each_child - device child iterator.
920 * @parent: parent struct device.
921 * @data: data for the callback.
922 * @fn: function to be called for each device.
924 * Iterate over @parent's child devices, and call @fn for each,
925 * passing it @data.
927 * We check the return of @fn each time. If it returns anything
928 * other than 0, we break out and return that value.
930 int device_for_each_child(struct device * parent, void * data,
931 int (*fn)(struct device *, void *))
933 struct klist_iter i;
934 struct device * child;
935 int error = 0;
937 klist_iter_init(&parent->klist_children, &i);
938 while ((child = next_device(&i)) && !error)
939 error = fn(child, data);
940 klist_iter_exit(&i);
941 return error;
945 * device_find_child - device iterator for locating a particular device.
946 * @parent: parent struct device
947 * @data: Data to pass to match function
948 * @match: Callback function to check device
950 * This is similar to the device_for_each_child() function above, but it
951 * returns a reference to a device that is 'found' for later use, as
952 * determined by the @match callback.
954 * The callback should return 0 if the device doesn't match and non-zero
955 * if it does. If the callback returns non-zero and a reference to the
956 * current device can be obtained, this function will return to the caller
957 * and not iterate over any more devices.
959 struct device * device_find_child(struct device *parent, void *data,
960 int (*match)(struct device *, void *))
962 struct klist_iter i;
963 struct device *child;
965 if (!parent)
966 return NULL;
968 klist_iter_init(&parent->klist_children, &i);
969 while ((child = next_device(&i)))
970 if (match(child, data) && get_device(child))
971 break;
972 klist_iter_exit(&i);
973 return child;
976 int __init devices_init(void)
978 return subsystem_register(&devices_subsys);
981 EXPORT_SYMBOL_GPL(device_for_each_child);
982 EXPORT_SYMBOL_GPL(device_find_child);
984 EXPORT_SYMBOL_GPL(device_initialize);
985 EXPORT_SYMBOL_GPL(device_add);
986 EXPORT_SYMBOL_GPL(device_register);
988 EXPORT_SYMBOL_GPL(device_del);
989 EXPORT_SYMBOL_GPL(device_unregister);
990 EXPORT_SYMBOL_GPL(get_device);
991 EXPORT_SYMBOL_GPL(put_device);
993 EXPORT_SYMBOL_GPL(device_create_file);
994 EXPORT_SYMBOL_GPL(device_remove_file);
997 static void device_create_release(struct device *dev)
999 pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
1000 kfree(dev);
1004 * device_create - creates a device and registers it with sysfs
1005 * @class: pointer to the struct class that this device should be registered to
1006 * @parent: pointer to the parent struct device of this new device, if any
1007 * @devt: the dev_t for the char device to be added
1008 * @fmt: string for the device's name
1010 * This function can be used by char device classes. A struct device
1011 * will be created in sysfs, registered to the specified class.
1013 * A "dev" file will be created, showing the dev_t for the device, if
1014 * the dev_t is not 0,0.
1015 * If a pointer to a parent struct device is passed in, the newly created
1016 * struct device will be a child of that device in sysfs.
1017 * The pointer to the struct device will be returned from the call.
1018 * Any further sysfs files that might be required can be created using this
1019 * pointer.
1021 * Note: the struct class passed to this function must have previously
1022 * been created with a call to class_create().
1024 struct device *device_create(struct class *class, struct device *parent,
1025 dev_t devt, const char *fmt, ...)
1027 va_list args;
1028 struct device *dev = NULL;
1029 int retval = -ENODEV;
1031 if (class == NULL || IS_ERR(class))
1032 goto error;
1034 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1035 if (!dev) {
1036 retval = -ENOMEM;
1037 goto error;
1040 dev->devt = devt;
1041 dev->class = class;
1042 dev->parent = parent;
1043 dev->release = device_create_release;
1045 va_start(args, fmt);
1046 vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
1047 va_end(args);
1048 retval = device_register(dev);
1049 if (retval)
1050 goto error;
1052 return dev;
1054 error:
1055 kfree(dev);
1056 return ERR_PTR(retval);
1058 EXPORT_SYMBOL_GPL(device_create);
1061 * device_destroy - removes a device that was created with device_create()
1062 * @class: pointer to the struct class that this device was registered with
1063 * @devt: the dev_t of the device that was previously registered
1065 * This call unregisters and cleans up a device that was created with a
1066 * call to device_create().
1068 void device_destroy(struct class *class, dev_t devt)
1070 struct device *dev = NULL;
1071 struct device *dev_tmp;
1073 down(&class->sem);
1074 list_for_each_entry(dev_tmp, &class->devices, node) {
1075 if (dev_tmp->devt == devt) {
1076 dev = dev_tmp;
1077 break;
1080 up(&class->sem);
1082 if (dev)
1083 device_unregister(dev);
1085 EXPORT_SYMBOL_GPL(device_destroy);
1088 * device_rename - renames a device
1089 * @dev: the pointer to the struct device to be renamed
1090 * @new_name: the new name of the device
1092 int device_rename(struct device *dev, char *new_name)
1094 char *old_class_name = NULL;
1095 char *new_class_name = NULL;
1096 char *old_symlink_name = NULL;
1097 int error;
1099 dev = get_device(dev);
1100 if (!dev)
1101 return -EINVAL;
1103 pr_debug("DEVICE: renaming '%s' to '%s'\n", dev->bus_id, new_name);
1105 #ifdef CONFIG_SYSFS_DEPRECATED
1106 if ((dev->class) && (dev->parent))
1107 old_class_name = make_class_name(dev->class->name, &dev->kobj);
1108 #endif
1110 if (dev->class) {
1111 old_symlink_name = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
1112 if (!old_symlink_name) {
1113 error = -ENOMEM;
1114 goto out_free_old_class;
1116 strlcpy(old_symlink_name, dev->bus_id, BUS_ID_SIZE);
1119 strlcpy(dev->bus_id, new_name, BUS_ID_SIZE);
1121 error = kobject_rename(&dev->kobj, new_name);
1123 #ifdef CONFIG_SYSFS_DEPRECATED
1124 if (old_class_name) {
1125 new_class_name = make_class_name(dev->class->name, &dev->kobj);
1126 if (new_class_name) {
1127 sysfs_create_link(&dev->parent->kobj, &dev->kobj,
1128 new_class_name);
1129 sysfs_remove_link(&dev->parent->kobj, old_class_name);
1132 #endif
1134 if (dev->class) {
1135 sysfs_remove_link(&dev->class->subsys.kset.kobj,
1136 old_symlink_name);
1137 sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
1138 dev->bus_id);
1140 put_device(dev);
1142 kfree(new_class_name);
1143 kfree(old_symlink_name);
1144 out_free_old_class:
1145 kfree(old_class_name);
1147 return error;
1149 EXPORT_SYMBOL_GPL(device_rename);
1151 static int device_move_class_links(struct device *dev,
1152 struct device *old_parent,
1153 struct device *new_parent)
1155 int error = 0;
1156 #ifdef CONFIG_SYSFS_DEPRECATED
1157 char *class_name;
1159 class_name = make_class_name(dev->class->name, &dev->kobj);
1160 if (!class_name) {
1161 error = -ENOMEM;
1162 goto out;
1164 if (old_parent) {
1165 sysfs_remove_link(&dev->kobj, "device");
1166 sysfs_remove_link(&old_parent->kobj, class_name);
1168 if (new_parent) {
1169 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1170 "device");
1171 if (error)
1172 goto out;
1173 error = sysfs_create_link(&new_parent->kobj, &dev->kobj,
1174 class_name);
1175 if (error)
1176 sysfs_remove_link(&dev->kobj, "device");
1178 else
1179 error = 0;
1180 out:
1181 kfree(class_name);
1182 return error;
1183 #else
1184 if (old_parent)
1185 sysfs_remove_link(&dev->kobj, "device");
1186 if (new_parent)
1187 error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
1188 "device");
1189 return error;
1190 #endif
1194 * device_move - moves a device to a new parent
1195 * @dev: the pointer to the struct device to be moved
1196 * @new_parent: the new parent of the device (can by NULL)
1198 int device_move(struct device *dev, struct device *new_parent)
1200 int error;
1201 struct device *old_parent;
1202 struct kobject *new_parent_kobj;
1204 dev = get_device(dev);
1205 if (!dev)
1206 return -EINVAL;
1208 new_parent = get_device(new_parent);
1209 new_parent_kobj = get_device_parent (dev, new_parent);
1210 if (IS_ERR(new_parent_kobj)) {
1211 error = PTR_ERR(new_parent_kobj);
1212 put_device(new_parent);
1213 goto out;
1215 pr_debug("DEVICE: moving '%s' to '%s'\n", dev->bus_id,
1216 new_parent ? new_parent->bus_id : "<NULL>");
1217 error = kobject_move(&dev->kobj, new_parent_kobj);
1218 if (error) {
1219 put_device(new_parent);
1220 goto out;
1222 old_parent = dev->parent;
1223 dev->parent = new_parent;
1224 if (old_parent)
1225 klist_remove(&dev->knode_parent);
1226 if (new_parent)
1227 klist_add_tail(&dev->knode_parent, &new_parent->klist_children);
1228 if (!dev->class)
1229 goto out_put;
1230 error = device_move_class_links(dev, old_parent, new_parent);
1231 if (error) {
1232 /* We ignore errors on cleanup since we're hosed anyway... */
1233 device_move_class_links(dev, new_parent, old_parent);
1234 if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
1235 if (new_parent)
1236 klist_remove(&dev->knode_parent);
1237 if (old_parent)
1238 klist_add_tail(&dev->knode_parent,
1239 &old_parent->klist_children);
1241 put_device(new_parent);
1242 goto out;
1244 out_put:
1245 put_device(old_parent);
1246 out:
1247 put_device(dev);
1248 return error;
1251 EXPORT_SYMBOL_GPL(device_move);