]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/base/class.c
class: rename "sem" to "class_sem" in internal class structure
[linux-2.6-omap-h63xx.git] / drivers / base / class.c
index e085af0ff94fb93007e7dce12f98443e4c5daa6d..d24d21114ccbb175ee0c6d029897bb700f8018a8 100644 (file)
 #include "base.h"
 
 #define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr)
-#define to_class(obj) container_of(obj, struct class, subsys.kobj)
 
 static ssize_t class_attr_show(struct kobject *kobj, struct attribute *attr,
                               char *buf)
 {
        struct class_attribute *class_attr = to_class_attr(attr);
-       struct class *dc = to_class(kobj);
+       struct class_private *cp = to_class(kobj);
        ssize_t ret = -EIO;
 
        if (class_attr->show)
-               ret = class_attr->show(dc, buf);
+               ret = class_attr->show(cp->class, buf);
        return ret;
 }
 
@@ -39,17 +38,18 @@ static ssize_t class_attr_store(struct kobject *kobj, struct attribute *attr,
                                const char *buf, size_t count)
 {
        struct class_attribute *class_attr = to_class_attr(attr);
-       struct class *dc = to_class(kobj);
+       struct class_private *cp = to_class(kobj);
        ssize_t ret = -EIO;
 
        if (class_attr->store)
-               ret = class_attr->store(dc, buf, count);
+               ret = class_attr->store(cp->class, buf, count);
        return ret;
 }
 
 static void class_release(struct kobject *kobj)
 {
-       struct class *class = to_class(kobj);
+       struct class_private *cp = to_class(kobj);
+       struct class *class = cp->class;
 
        pr_debug("class '%s': release.\n", class->name);
 
@@ -70,7 +70,7 @@ static struct kobj_type class_ktype = {
        .release        = class_release,
 };
 
-/* Hotplug events for classes go to the class_obj subsys */
+/* Hotplug events for classes go to the class class_subsys */
 static struct kset *class_kset;
 
 
@@ -78,7 +78,8 @@ int class_create_file(struct class *cls, const struct class_attribute *attr)
 {
        int error;
        if (cls)
-               error = sysfs_create_file(&cls->subsys.kobj, &attr->attr);
+               error = sysfs_create_file(&cls->p->class_subsys.kobj,
+                                         &attr->attr);
        else
                error = -EINVAL;
        return error;
@@ -87,21 +88,20 @@ int class_create_file(struct class *cls, const struct class_attribute *attr)
 void class_remove_file(struct class *cls, const struct class_attribute *attr)
 {
        if (cls)
-               sysfs_remove_file(&cls->subsys.kobj, &attr->attr);
+               sysfs_remove_file(&cls->p->class_subsys.kobj, &attr->attr);
 }
 
 static struct class *class_get(struct class *cls)
 {
        if (cls)
-               return container_of(kset_get(&cls->subsys),
-                                   struct class, subsys);
-       return NULL;
+               kset_get(&cls->p->class_subsys);
+       return cls;
 }
 
 static void class_put(struct class *cls)
 {
        if (cls)
-               kset_put(&cls->subsys);
+               kset_put(&cls->p->class_subsys);
 }
 
 static int add_class_attrs(struct class *cls)
@@ -136,32 +136,46 @@ static void remove_class_attrs(struct class *cls)
 
 int class_register(struct class *cls)
 {
+       struct class_private *cp;
        int error;
 
        pr_debug("device class '%s': registering\n", cls->name);
 
-       INIT_LIST_HEAD(&cls->devices);
-       INIT_LIST_HEAD(&cls->interfaces);
-       kset_init(&cls->class_dirs);
-       init_MUTEX(&cls->sem);
-       error = kobject_set_name(&cls->subsys.kobj, "%s", cls->name);
-       if (error)
+       cp = kzalloc(sizeof(*cp), GFP_KERNEL);
+       if (!cp)
+               return -ENOMEM;
+       INIT_LIST_HEAD(&cp->class_devices);
+       INIT_LIST_HEAD(&cp->class_interfaces);
+       kset_init(&cp->class_dirs);
+       init_MUTEX(&cp->class_sem);
+       error = kobject_set_name(&cp->class_subsys.kobj, "%s", cls->name);
+       if (error) {
+               kfree(cp);
                return error;
+       }
+
+       /* set the default /sys/dev directory for devices of this class */
+       if (!cls->dev_kobj)
+               cls->dev_kobj = sysfs_dev_char_kobj;
 
 #if defined(CONFIG_SYSFS_DEPRECATED) && defined(CONFIG_BLOCK)
        /* let the block class directory show up in the root of sysfs */
        if (cls != &block_class)
-               cls->subsys.kobj.kset = class_kset;
+               cp->class_subsys.kobj.kset = class_kset;
 #else
-       cls->subsys.kobj.kset = class_kset;
+       cp->class_subsys.kobj.kset = class_kset;
 #endif
-       cls->subsys.kobj.ktype = &class_ktype;
+       cp->class_subsys.kobj.ktype = &class_ktype;
+       cp->class = cls;
+       cls->p = cp;
 
-       error = kset_register(&cls->subsys);
-       if (!error) {
-               error = add_class_attrs(class_get(cls));
-               class_put(cls);
+       error = kset_register(&cp->class_subsys);
+       if (error) {
+               kfree(cp);
+               return error;
        }
+       error = add_class_attrs(class_get(cls));
+       class_put(cls);
        return error;
 }
 
@@ -169,7 +183,7 @@ void class_unregister(struct class *cls)
 {
        pr_debug("device class '%s': unregistering\n", cls->name);
        remove_class_attrs(cls);
-       kset_unregister(&cls->subsys);
+       kset_unregister(&cls->p->class_subsys);
 }
 
 static void class_create_release(struct class *cls)
@@ -252,39 +266,44 @@ char *make_class_name(const char *name, struct kobject *kobj)
 /**
  * class_for_each_device - device iterator
  * @class: the class we're iterating
+ * @start: the device to start with in the list, if any.
  * @data: data for the callback
  * @fn: function to be called for each device
  *
  * Iterate over @class's list of devices, and call @fn for each,
- * passing it @data.
+ * passing it @data.  If @start is set, the list iteration will start
+ * there, otherwise if it is NULL, the iteration starts at the
+ * beginning of the list.
  *
  * We check the return of @fn each time. If it returns anything
  * other than 0, we break out and return that value.
  *
- * Note, we hold class->sem in this function, so it can not be
+ * Note, we hold class->class_sem in this function, so it can not be
  * re-acquired in @fn, otherwise it will self-deadlocking. For
  * example, calls to add or remove class members would be verboten.
  */
-int class_for_each_device(struct class *class, void *data,
-                          int (*fn)(struct device *, void *))
+int class_for_each_device(struct class *class, struct device *start,
+                         void *data, int (*fn)(struct device *, void *))
 {
        struct device *dev;
        int error = 0;
 
        if (!class)
                return -EINVAL;
-       down(&class->sem);
-       list_for_each_entry(dev, &class->devices, node) {
+       down(&class->p->class_sem);
+       list_for_each_entry(dev, &class->p->class_devices, node) {
+               if (start) {
+                       if (start == dev)
+                               start = NULL;
+                       continue;
+               }
                dev = get_device(dev);
-               if (dev) {
-                       error = fn(dev, data);
-                       put_device(dev);
-               } else
-                       error = -ENODEV;
+               error = fn(dev, data);
+               put_device(dev);
                if (error)
                        break;
        }
-       up(&class->sem);
+       up(&class->p->class_sem);
 
        return error;
 }
@@ -293,6 +312,7 @@ EXPORT_SYMBOL_GPL(class_for_each_device);
 /**
  * class_find_device - device iterator for locating a particular device
  * @class: the class we're iterating
+ * @start: Device to begin with
  * @data: data for the match function
  * @match: function to check device
  *
@@ -306,12 +326,13 @@ EXPORT_SYMBOL_GPL(class_for_each_device);
  *
  * Note, you will need to drop the reference with put_device() after use.
  *
- * We hold class->sem in this function, so it can not be
+ * We hold class->class_sem in this function, so it can not be
  * re-acquired in @match, otherwise it will self-deadlocking. For
  * example, calls to add or remove class members would be verboten.
  */
-struct device *class_find_device(struct class *class, void *data,
-                                  int (*match)(struct device *, void *))
+struct device *class_find_device(struct class *class, struct device *start,
+                                void *data,
+                                int (*match)(struct device *, void *))
 {
        struct device *dev;
        int found = 0;
@@ -319,19 +340,21 @@ struct device *class_find_device(struct class *class, void *data,
        if (!class)
                return NULL;
 
-       down(&class->sem);
-       list_for_each_entry(dev, &class->devices, node) {
+       down(&class->p->class_sem);
+       list_for_each_entry(dev, &class->p->class_devices, node) {
+               if (start) {
+                       if (start == dev)
+                               start = NULL;
+                       continue;
+               }
                dev = get_device(dev);
-               if (dev) {
-                       if (match(dev, data)) {
-                               found = 1;
-                               break;
-                       } else
-                               put_device(dev);
-               } else
+               if (match(dev, data)) {
+                       found = 1;
                        break;
+               } else
+                       put_device(dev);
        }
-       up(&class->sem);
+       up(&class->p->class_sem);
 
        return found ? dev : NULL;
 }
@@ -349,13 +372,13 @@ int class_interface_register(struct class_interface *class_intf)
        if (!parent)
                return -EINVAL;
 
-       down(&parent->sem);
-       list_add_tail(&class_intf->node, &parent->interfaces);
+       down(&parent->p->class_sem);
+       list_add_tail(&class_intf->node, &parent->p->class_interfaces);
        if (class_intf->add_dev) {
-               list_for_each_entry(dev, &parent->devices, node)
+               list_for_each_entry(dev, &parent->p->class_devices, node)
                        class_intf->add_dev(dev, class_intf);
        }
-       up(&parent->sem);
+       up(&parent->p->class_sem);
 
        return 0;
 }
@@ -368,13 +391,13 @@ void class_interface_unregister(struct class_interface *class_intf)
        if (!parent)
                return;
 
-       down(&parent->sem);
+       down(&parent->p->class_sem);
        list_del_init(&class_intf->node);
        if (class_intf->remove_dev) {
-               list_for_each_entry(dev, &parent->devices, node)
+               list_for_each_entry(dev, &parent->p->class_devices, node)
                        class_intf->remove_dev(dev, class_intf);
        }
-       up(&parent->sem);
+       up(&parent->p->class_sem);
 
        class_put(parent);
 }