Blackfin: fix cache Kconfig typo
[safe/jmp/linux-2.6] / drivers / misc / enclosure.c
index 6fcb0e9..e9eae4a 100644 (file)
 static LIST_HEAD(container_list);
 static DEFINE_MUTEX(container_list_lock);
 static struct class enclosure_class;
-static struct class enclosure_component_class;
 
 /**
- * enclosure_find - find an enclosure given a device
- * @dev:       the device to find for
+ * enclosure_find - find an enclosure given a parent device
+ * @dev:       the parent to match against
+ * @start:     Optional enclosure device to start from (NULL if none)
  *
- * Looks through the list of registered enclosures to see
- * if it can find a match for a device.  Returns NULL if no
- * enclosure is found. Obtains a reference to the enclosure class
- * device which must be released with class_device_put().
+ * Looks through the list of registered enclosures to find all those
+ * with @dev as a parent.  Returns NULL if no enclosure is
+ * found. @start can be used as a starting point to obtain multiple
+ * enclosures per parent (should begin with NULL and then be set to
+ * each returned enclosure device). Obtains a reference to the
+ * enclosure class device which must be released with device_put().
+ * If @start is not NULL, a reference must be taken on it which is
+ * released before returning (this allows a loop through all
+ * enclosures to exit with only the reference on the enclosure of
+ * interest held).  Note that the @dev may correspond to the actual
+ * device housing the enclosure, in which case no iteration via @start
+ * is required.
  */
-struct enclosure_device *enclosure_find(struct device *dev)
+struct enclosure_device *enclosure_find(struct device *dev,
+                                       struct enclosure_device *start)
 {
-       struct enclosure_device *edev = NULL;
+       struct enclosure_device *edev;
 
        mutex_lock(&container_list_lock);
-       list_for_each_entry(edev, &container_list, node) {
-               if (edev->cdev.dev == dev) {
-                       class_device_get(&edev->cdev);
-                       mutex_unlock(&container_list_lock);
-                       return edev;
+       edev = list_prepare_entry(start, &container_list, node);
+       if (start)
+               put_device(&start->edev);
+
+       list_for_each_entry_continue(edev, &container_list, node) {
+               struct device *parent = edev->edev.parent;
+               /* parent might not be immediate, so iterate up to
+                * the root of the tree if necessary */
+               while (parent) {
+                       if (parent == dev) {
+                               get_device(&edev->edev);
+                               mutex_unlock(&container_list_lock);
+                               return edev;
+                       }
+                       parent = parent->parent;
                }
        }
        mutex_unlock(&container_list_lock);
@@ -117,11 +136,11 @@ enclosure_register(struct device *dev, const char *name, int components,
 
        edev->components = components;
 
-       edev->cdev.class = &enclosure_class;
-       edev->cdev.dev = get_device(dev);
+       edev->edev.class = &enclosure_class;
+       edev->edev.parent = get_device(dev);
        edev->cb = cb;
-       snprintf(edev->cdev.class_id, BUS_ID_SIZE, "%s", name);
-       err = class_device_register(&edev->cdev);
+       dev_set_name(&edev->edev, "%s", name);
+       err = device_register(&edev->edev);
        if (err)
                goto err;
 
@@ -135,7 +154,7 @@ enclosure_register(struct device *dev, const char *name, int components,
        return edev;
 
  err:
-       put_device(edev->cdev.dev);
+       put_device(edev->edev.parent);
        kfree(edev);
        return ERR_PTR(err);
 }
@@ -158,29 +177,69 @@ void enclosure_unregister(struct enclosure_device *edev)
 
        for (i = 0; i < edev->components; i++)
                if (edev->component[i].number != -1)
-                       class_device_unregister(&edev->component[i].cdev);
+                       device_unregister(&edev->component[i].cdev);
 
        /* prevent any callbacks into service user */
        edev->cb = &enclosure_null_callbacks;
-       class_device_unregister(&edev->cdev);
+       device_unregister(&edev->edev);
 }
 EXPORT_SYMBOL_GPL(enclosure_unregister);
 
-static void enclosure_release(struct class_device *cdev)
+#define ENCLOSURE_NAME_SIZE    64
+
+static void enclosure_link_name(struct enclosure_component *cdev, char *name)
+{
+       strcpy(name, "enclosure_device:");
+       strcat(name, dev_name(&cdev->cdev));
+}
+
+static void enclosure_remove_links(struct enclosure_component *cdev)
+{
+       char name[ENCLOSURE_NAME_SIZE];
+
+       enclosure_link_name(cdev, name);
+       sysfs_remove_link(&cdev->dev->kobj, name);
+       sysfs_remove_link(&cdev->cdev.kobj, "device");
+}
+
+static int enclosure_add_links(struct enclosure_component *cdev)
+{
+       int error;
+       char name[ENCLOSURE_NAME_SIZE];
+
+       error = sysfs_create_link(&cdev->cdev.kobj, &cdev->dev->kobj, "device");
+       if (error)
+               return error;
+
+       enclosure_link_name(cdev, name);
+       error = sysfs_create_link(&cdev->dev->kobj, &cdev->cdev.kobj, name);
+       if (error)
+               sysfs_remove_link(&cdev->cdev.kobj, "device");
+
+       return error;
+}
+
+static void enclosure_release(struct device *cdev)
 {
        struct enclosure_device *edev = to_enclosure_device(cdev);
 
-       put_device(cdev->dev);
+       put_device(cdev->parent);
        kfree(edev);
 }
 
-static void enclosure_component_release(struct class_device *cdev)
+static void enclosure_component_release(struct device *dev)
 {
-       if (cdev->dev)
+       struct enclosure_component *cdev = to_enclosure_component(dev);
+
+       if (cdev->dev) {
+               enclosure_remove_links(cdev);
                put_device(cdev->dev);
-       class_device_put(cdev->parent);
+       }
+       put_device(dev->parent);
 }
 
+static const struct attribute_group *enclosure_groups[];
+
 /**
  * enclosure_component_register - add a particular component to an enclosure
  * @edev:      the enclosure to add the component
@@ -201,7 +260,7 @@ enclosure_component_register(struct enclosure_device *edev,
                             const char *name)
 {
        struct enclosure_component *ecomp;
-       struct class_device *cdev;
+       struct device *cdev;
        int err;
 
        if (number >= edev->components)
@@ -215,14 +274,16 @@ enclosure_component_register(struct enclosure_device *edev,
        ecomp->type = type;
        ecomp->number = number;
        cdev = &ecomp->cdev;
-       cdev->parent = class_device_get(&edev->cdev);
-       cdev->class = &enclosure_component_class;
-       if (name)
-               snprintf(cdev->class_id, BUS_ID_SIZE, "%s", name);
+       cdev->parent = get_device(&edev->edev);
+       if (name && name[0])
+               dev_set_name(cdev, "%s", name);
        else
-               snprintf(cdev->class_id, BUS_ID_SIZE, "%u", number);
+               dev_set_name(cdev, "%u", number);
+
+       cdev->release = enclosure_component_release;
+       cdev->groups = enclosure_groups;
 
-       err = class_device_register(cdev);
+       err = device_register(cdev);
        if (err)
                ERR_PTR(err);
 
@@ -247,18 +308,22 @@ EXPORT_SYMBOL_GPL(enclosure_component_register);
 int enclosure_add_device(struct enclosure_device *edev, int component,
                         struct device *dev)
 {
-       struct class_device *cdev;
+       struct enclosure_component *cdev;
 
        if (!edev || component >= edev->components)
                return -EINVAL;
 
-       cdev = &edev->component[component].cdev;
+       cdev = &edev->component[component];
+
+       if (cdev->dev == dev)
+               return -EEXIST;
 
-       class_device_del(cdev);
        if (cdev->dev)
-               put_device(cdev->dev);
+               enclosure_remove_links(cdev);
+
+       put_device(cdev->dev);
        cdev->dev = get_device(dev);
-       return class_device_add(cdev);
+       return enclosure_add_links(cdev);
 }
 EXPORT_SYMBOL_GPL(enclosure_add_device);
 
@@ -270,20 +335,25 @@ EXPORT_SYMBOL_GPL(enclosure_add_device);
  * Returns zero on success or an error.
  *
  */
-int enclosure_remove_device(struct enclosure_device *edev, int component)
+int enclosure_remove_device(struct enclosure_device *edev, struct device *dev)
 {
-       struct class_device *cdev;
+       struct enclosure_component *cdev;
+       int i;
 
-       if (!edev || component >= edev->components)
+       if (!edev || !dev)
                return -EINVAL;
 
-       cdev = &edev->component[component].cdev;
-
-       class_device_del(cdev);
-       if (cdev->dev)
-               put_device(cdev->dev);
-       cdev->dev = NULL;
-       return class_device_add(cdev);
+       for (i = 0; i < edev->components; i++) {
+               cdev = &edev->component[i];
+               if (cdev->dev == dev) {
+                       enclosure_remove_links(cdev);
+                       device_del(&cdev->cdev);
+                       put_device(dev);
+                       cdev->dev = NULL;
+                       return device_add(&cdev->cdev);
+               }
+       }
+       return -ENODEV;
 }
 EXPORT_SYMBOL_GPL(enclosure_remove_device);
 
@@ -291,14 +361,16 @@ EXPORT_SYMBOL_GPL(enclosure_remove_device);
  * sysfs pieces below
  */
 
-static ssize_t enclosure_show_components(struct class_device *cdev, char *buf)
+static ssize_t enclosure_show_components(struct device *cdev,
+                                        struct device_attribute *attr,
+                                        char *buf)
 {
        struct enclosure_device *edev = to_enclosure_device(cdev);
 
        return snprintf(buf, 40, "%d\n", edev->components);
 }
 
-static struct class_device_attribute enclosure_attrs[] = {
+static struct device_attribute enclosure_attrs[] = {
        __ATTR(components, S_IRUGO, enclosure_show_components, NULL),
        __ATTR_NULL
 };
@@ -306,8 +378,8 @@ static struct class_device_attribute enclosure_attrs[] = {
 static struct class enclosure_class = {
        .name                   = "enclosure",
        .owner                  = THIS_MODULE,
-       .release                = enclosure_release,
-       .class_dev_attrs        = enclosure_attrs,
+       .dev_release            = enclosure_release,
+       .dev_attrs              = enclosure_attrs,
 };
 
 static const char *const enclosure_status [] = {
@@ -326,7 +398,8 @@ static const char *const enclosure_type [] = {
        [ENCLOSURE_COMPONENT_ARRAY_DEVICE] = "array device",
 };
 
-static ssize_t get_component_fault(struct class_device *cdev, char *buf)
+static ssize_t get_component_fault(struct device *cdev,
+                                  struct device_attribute *attr, char *buf)
 {
        struct enclosure_device *edev = to_enclosure_device(cdev->parent);
        struct enclosure_component *ecomp = to_enclosure_component(cdev);
@@ -336,8 +409,9 @@ static ssize_t get_component_fault(struct class_device *cdev, char *buf)
        return snprintf(buf, 40, "%d\n", ecomp->fault);
 }
 
-static ssize_t set_component_fault(struct class_device *cdev, const char *buf,
-                                  size_t count)
+static ssize_t set_component_fault(struct device *cdev,
+                                  struct device_attribute *attr,
+                                  const char *buf, size_t count)
 {
        struct enclosure_device *edev = to_enclosure_device(cdev->parent);
        struct enclosure_component *ecomp = to_enclosure_component(cdev);
@@ -348,7 +422,8 @@ static ssize_t set_component_fault(struct class_device *cdev, const char *buf,
        return count;
 }
 
-static ssize_t get_component_status(struct class_device *cdev, char *buf)
+static ssize_t get_component_status(struct device *cdev,
+                                   struct device_attribute *attr,char *buf)
 {
        struct enclosure_device *edev = to_enclosure_device(cdev->parent);
        struct enclosure_component *ecomp = to_enclosure_component(cdev);
@@ -358,8 +433,9 @@ static ssize_t get_component_status(struct class_device *cdev, char *buf)
        return snprintf(buf, 40, "%s\n", enclosure_status[ecomp->status]);
 }
 
-static ssize_t set_component_status(struct class_device *cdev, const char *buf,
-                                  size_t count)
+static ssize_t set_component_status(struct device *cdev,
+                                   struct device_attribute *attr,
+                                   const char *buf, size_t count)
 {
        struct enclosure_device *edev = to_enclosure_device(cdev->parent);
        struct enclosure_component *ecomp = to_enclosure_component(cdev);
@@ -380,7 +456,8 @@ static ssize_t set_component_status(struct class_device *cdev, const char *buf,
                return -EINVAL;
 }
 
-static ssize_t get_component_active(struct class_device *cdev, char *buf)
+static ssize_t get_component_active(struct device *cdev,
+                                   struct device_attribute *attr, char *buf)
 {
        struct enclosure_device *edev = to_enclosure_device(cdev->parent);
        struct enclosure_component *ecomp = to_enclosure_component(cdev);
@@ -390,8 +467,9 @@ static ssize_t get_component_active(struct class_device *cdev, char *buf)
        return snprintf(buf, 40, "%d\n", ecomp->active);
 }
 
-static ssize_t set_component_active(struct class_device *cdev, const char *buf,
-                                  size_t count)
+static ssize_t set_component_active(struct device *cdev,
+                                   struct device_attribute *attr,
+                                   const char *buf, size_t count)
 {
        struct enclosure_device *edev = to_enclosure_device(cdev->parent);
        struct enclosure_component *ecomp = to_enclosure_component(cdev);
@@ -402,7 +480,8 @@ static ssize_t set_component_active(struct class_device *cdev, const char *buf,
        return count;
 }
 
-static ssize_t get_component_locate(struct class_device *cdev, char *buf)
+static ssize_t get_component_locate(struct device *cdev,
+                                   struct device_attribute *attr, char *buf)
 {
        struct enclosure_device *edev = to_enclosure_device(cdev->parent);
        struct enclosure_component *ecomp = to_enclosure_component(cdev);
@@ -412,8 +491,9 @@ static ssize_t get_component_locate(struct class_device *cdev, char *buf)
        return snprintf(buf, 40, "%d\n", ecomp->locate);
 }
 
-static ssize_t set_component_locate(struct class_device *cdev, const char *buf,
-                                  size_t count)
+static ssize_t set_component_locate(struct device *cdev,
+                                   struct device_attribute *attr,
+                                   const char *buf, size_t count)
 {
        struct enclosure_device *edev = to_enclosure_device(cdev->parent);
        struct enclosure_component *ecomp = to_enclosure_component(cdev);
@@ -424,7 +504,8 @@ static ssize_t set_component_locate(struct class_device *cdev, const char *buf,
        return count;
 }
 
-static ssize_t get_component_type(struct class_device *cdev, char *buf)
+static ssize_t get_component_type(struct device *cdev,
+                                 struct device_attribute *attr, char *buf)
 {
        struct enclosure_component *ecomp = to_enclosure_component(cdev);
 
@@ -432,24 +513,32 @@ static ssize_t get_component_type(struct class_device *cdev, char *buf)
 }
 
 
-static struct class_device_attribute enclosure_component_attrs[] = {
-       __ATTR(fault, S_IRUGO | S_IWUSR, get_component_fault,
-              set_component_fault),
-       __ATTR(status, S_IRUGO | S_IWUSR, get_component_status,
-              set_component_status),
-       __ATTR(active, S_IRUGO | S_IWUSR, get_component_active,
-              set_component_active),
-       __ATTR(locate, S_IRUGO | S_IWUSR, get_component_locate,
-              set_component_locate),
-       __ATTR(type, S_IRUGO, get_component_type, NULL),
-       __ATTR_NULL
+static DEVICE_ATTR(fault, S_IRUGO | S_IWUSR, get_component_fault,
+                   set_component_fault);
+static DEVICE_ATTR(status, S_IRUGO | S_IWUSR, get_component_status,
+                  set_component_status);
+static DEVICE_ATTR(active, S_IRUGO | S_IWUSR, get_component_active,
+                  set_component_active);
+static DEVICE_ATTR(locate, S_IRUGO | S_IWUSR, get_component_locate,
+                  set_component_locate);
+static DEVICE_ATTR(type, S_IRUGO, get_component_type, NULL);
+
+static struct attribute *enclosure_component_attrs[] = {
+       &dev_attr_fault.attr,
+       &dev_attr_status.attr,
+       &dev_attr_active.attr,
+       &dev_attr_locate.attr,
+       &dev_attr_type.attr,
+       NULL
 };
 
-static struct class enclosure_component_class =  {
-       .name                   = "enclosure_component",
-       .owner                  = THIS_MODULE,
-       .class_dev_attrs        = enclosure_component_attrs,
-       .release                = enclosure_component_release,
+static struct attribute_group enclosure_group = {
+       .attrs = enclosure_component_attrs,
+};
+
+static const struct attribute_group *enclosure_groups[] = {
+       &enclosure_group,
+       NULL
 };
 
 static int __init enclosure_init(void)
@@ -459,20 +548,12 @@ static int __init enclosure_init(void)
        err = class_register(&enclosure_class);
        if (err)
                return err;
-       err = class_register(&enclosure_component_class);
-       if (err)
-               goto err_out;
 
        return 0;
- err_out:
-       class_unregister(&enclosure_class);
-
-       return err;
 }
 
 static void __exit enclosure_exit(void)
 {
-       class_unregister(&enclosure_component_class);
        class_unregister(&enclosure_class);
 }