fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / drivers / base / sys.c
index 9102e37..04e5db4 100644 (file)
@@ -12,7 +12,6 @@
  * add themselves as children of the system bus.
  */
 
-#include <linux/config.h>
 #include <linux/sysdev.h>
 #include <linux/err.h>
 #include <linux/module.h>
 #include <linux/slab.h>
 #include <linux/string.h>
 #include <linux/pm.h>
+#include <linux/device.h>
+#include <asm/semaphore.h>
 
+#include "base.h"
 
 extern struct subsystem devices_subsys;
 
@@ -37,7 +39,7 @@ sysdev_show(struct kobject * kobj, struct attribute * attr, char * buffer)
 
        if (sysdev_attr->show)
                return sysdev_attr->show(sysdev, buffer);
-       return 0;
+       return -EIO;
 }
 
 
@@ -50,7 +52,7 @@ sysdev_store(struct kobject * kobj, struct attribute * attr,
 
        if (sysdev_attr->store)
                return sysdev_attr->store(sysdev, buffer, count);
-       return 0;
+       return -EIO;
 }
 
 static struct sysfs_ops sysfs_ops = {
@@ -77,10 +79,59 @@ void sysdev_remove_file(struct sys_device * s, struct sysdev_attribute * a)
 EXPORT_SYMBOL_GPL(sysdev_create_file);
 EXPORT_SYMBOL_GPL(sysdev_remove_file);
 
+#define to_sysdev_class(k) container_of(k, struct sysdev_class, kset.kobj)
+#define to_sysdev_class_attr(a) container_of(a, \
+       struct sysdev_class_attribute, attr)
+
+static ssize_t sysdev_class_show(struct kobject *kobj, struct attribute *attr,
+                                char *buffer)
+{
+       struct sysdev_class * class = to_sysdev_class(kobj);
+       struct sysdev_class_attribute *class_attr = to_sysdev_class_attr(attr);
+
+       if (class_attr->show)
+               return class_attr->show(class, buffer);
+       return -EIO;
+}
+
+static ssize_t sysdev_class_store(struct kobject *kobj, struct attribute *attr,
+                                 const char *buffer, size_t count)
+{
+       struct sysdev_class * class = to_sysdev_class(kobj);
+       struct sysdev_class_attribute * class_attr = to_sysdev_class_attr(attr);
+
+       if (class_attr->store)
+               return class_attr->store(class, buffer, count);
+       return -EIO;
+}
+
+static struct sysfs_ops sysfs_class_ops = {
+       .show   = sysdev_class_show,
+       .store  = sysdev_class_store,
+};
+
+static struct kobj_type ktype_sysdev_class = {
+       .sysfs_ops      = &sysfs_class_ops,
+};
+
+int sysdev_class_create_file(struct sysdev_class *c,
+                            struct sysdev_class_attribute *a)
+{
+       return sysfs_create_file(&c->kset.kobj, &a->attr);
+}
+EXPORT_SYMBOL_GPL(sysdev_class_create_file);
+
+void sysdev_class_remove_file(struct sysdev_class *c,
+                             struct sysdev_class_attribute *a)
+{
+       sysfs_remove_file(&c->kset.kobj, &a->attr);
+}
+EXPORT_SYMBOL_GPL(sysdev_class_remove_file);
+
 /*
  * declare system_subsys
  */
-static decl_subsys(system, &ktype_sysdev, NULL);
+static decl_subsys(system, &ktype_sysdev_class, NULL);
 
 int sysdev_class_register(struct sysdev_class * cls)
 {
@@ -289,6 +340,27 @@ void sysdev_shutdown(void)
        up(&sysdev_drivers_lock);
 }
 
+static void __sysdev_resume(struct sys_device *dev)
+{
+       struct sysdev_class *cls = dev->cls;
+       struct sysdev_driver *drv;
+
+       /* First, call the class-specific one */
+       if (cls->resume)
+               cls->resume(dev);
+
+       /* Call auxillary drivers next. */
+       list_for_each_entry(drv, &cls->drivers, entry) {
+               if (drv->resume)
+                       drv->resume(dev);
+       }
+
+       /* Call global drivers. */
+       list_for_each_entry(drv, &sysdev_drivers, entry) {
+               if (drv->resume)
+                       drv->resume(dev);
+       }
+}
 
 /**
  *     sysdev_suspend - Suspend all system devices.
@@ -306,38 +378,93 @@ void sysdev_shutdown(void)
 int sysdev_suspend(pm_message_t state)
 {
        struct sysdev_class * cls;
+       struct sys_device *sysdev, *err_dev;
+       struct sysdev_driver *drv, *err_drv;
+       int ret;
 
        pr_debug("Suspending System Devices\n");
 
        list_for_each_entry_reverse(cls, &system_subsys.kset.list,
                                    kset.kobj.entry) {
-               struct sys_device * sysdev;
 
                pr_debug("Suspending type '%s':\n",
                         kobject_name(&cls->kset.kobj));
 
                list_for_each_entry(sysdev, &cls->kset.list, kobj.entry) {
-                       struct sysdev_driver * drv;
                        pr_debug(" %s\n", kobject_name(&sysdev->kobj));
 
                        /* Call global drivers first. */
                        list_for_each_entry(drv, &sysdev_drivers, entry) {
-                               if (drv->suspend)
-                                       drv->suspend(sysdev, state);
+                               if (drv->suspend) {
+                                       ret = drv->suspend(sysdev, state);
+                                       if (ret)
+                                               goto gbl_driver;
+                               }
                        }
 
                        /* Call auxillary drivers next. */
                        list_for_each_entry(drv, &cls->drivers, entry) {
-                               if (drv->suspend)
-                                       drv->suspend(sysdev, state);
+                               if (drv->suspend) {
+                                       ret = drv->suspend(sysdev, state);
+                                       if (ret)
+                                               goto aux_driver;
+                               }
                        }
 
                        /* Now call the generic one */
-                       if (cls->suspend)
-                               cls->suspend(sysdev, state);
+                       if (cls->suspend) {
+                               ret = cls->suspend(sysdev, state);
+                               if (ret)
+                                       goto cls_driver;
+                       }
                }
        }
        return 0;
+       /* resume current sysdev */
+cls_driver:
+       drv = NULL;
+       printk(KERN_ERR "Class suspend failed for %s\n",
+               kobject_name(&sysdev->kobj));
+
+aux_driver:
+       if (drv)
+               printk(KERN_ERR "Class driver suspend failed for %s\n",
+                               kobject_name(&sysdev->kobj));
+       list_for_each_entry(err_drv, &cls->drivers, entry) {
+               if (err_drv == drv)
+                       break;
+               if (err_drv->resume)
+                       err_drv->resume(sysdev);
+       }
+       drv = NULL;
+
+gbl_driver:
+       if (drv)
+               printk(KERN_ERR "sysdev driver suspend failed for %s\n",
+                               kobject_name(&sysdev->kobj));
+       list_for_each_entry(err_drv, &sysdev_drivers, entry) {
+               if (err_drv == drv)
+                       break;
+               if (err_drv->resume)
+                       err_drv->resume(sysdev);
+       }
+       /* resume other sysdevs in current class */
+       list_for_each_entry(err_dev, &cls->kset.list, kobj.entry) {
+               if (err_dev == sysdev)
+                       break;
+               pr_debug(" %s\n", kobject_name(&err_dev->kobj));
+               __sysdev_resume(err_dev);
+       }
+
+       /* resume other classes */
+       list_for_each_entry_continue(cls, &system_subsys.kset.list,
+                                       kset.kobj.entry) {
+               list_for_each_entry(err_dev, &cls->kset.list, kobj.entry) {
+                       pr_debug(" %s\n", kobject_name(&err_dev->kobj));
+                       __sysdev_resume(err_dev);
+               }
+       }
+       return ret;
 }
 
 
@@ -363,25 +490,9 @@ int sysdev_resume(void)
                         kobject_name(&cls->kset.kobj));
 
                list_for_each_entry(sysdev, &cls->kset.list, kobj.entry) {
-                       struct sysdev_driver * drv;
                        pr_debug(" %s\n", kobject_name(&sysdev->kobj));
 
-                       /* First, call the class-specific one */
-                       if (cls->resume)
-                               cls->resume(sysdev);
-
-                       /* Call auxillary drivers next. */
-                       list_for_each_entry(drv, &cls->drivers, entry) {
-                               if (drv->resume)
-                                       drv->resume(sysdev);
-                       }
-
-                       /* Call global drivers. */
-                       list_for_each_entry(drv, &sysdev_drivers, entry) {
-                               if (drv->resume)
-                                       drv->resume(sysdev);
-                       }
-
+                       __sysdev_resume(sysdev);
                }
        }
        return 0;