Blackfin: fix cache Kconfig typo
[safe/jmp/linux-2.6] / drivers / misc / enclosure.c
index fafb57f..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 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;
 
        mutex_lock(&container_list_lock);
-       list_for_each_entry(edev, &container_list, node) {
-               if (edev->edev.parent == dev) {
-                       get_device(&edev->edev);
-                       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);
@@ -120,7 +139,7 @@ enclosure_register(struct device *dev, const char *name, int components,
        edev->edev.class = &enclosure_class;
        edev->edev.parent = get_device(dev);
        edev->cb = cb;
-       snprintf(edev->edev.bus_id, BUS_ID_SIZE, "%s", name);
+       dev_set_name(&edev->edev, "%s", name);
        err = device_register(&edev->edev);
        if (err)
                goto err;
@@ -166,6 +185,40 @@ void enclosure_unregister(struct enclosure_device *edev)
 }
 EXPORT_SYMBOL_GPL(enclosure_unregister);
 
+#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);
@@ -178,10 +231,15 @@ static void enclosure_component_release(struct device *dev)
 {
        struct enclosure_component *cdev = to_enclosure_component(dev);
 
-       put_device(cdev->dev);
+       if (cdev->dev) {
+               enclosure_remove_links(cdev);
+               put_device(cdev->dev);
+       }
        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
@@ -217,11 +275,13 @@ enclosure_component_register(struct enclosure_device *edev,
        ecomp->number = number;
        cdev = &ecomp->cdev;
        cdev->parent = get_device(&edev->edev);
-       cdev->class = &enclosure_component_class;
-       if (name)
-               snprintf(cdev->bus_id, BUS_ID_SIZE, "%s", name);
+       if (name && name[0])
+               dev_set_name(cdev, "%s", name);
        else
-               snprintf(cdev->bus_id, BUS_ID_SIZE, "%u", number);
+               dev_set_name(cdev, "%u", number);
+
+       cdev->release = enclosure_component_release;
+       cdev->groups = enclosure_groups;
 
        err = device_register(cdev);
        if (err)
@@ -255,10 +315,15 @@ int enclosure_add_device(struct enclosure_device *edev, int component,
 
        cdev = &edev->component[component];
 
-       device_del(&cdev->cdev);
+       if (cdev->dev == dev)
+               return -EEXIST;
+
+       if (cdev->dev)
+               enclosure_remove_links(cdev);
+
        put_device(cdev->dev);
        cdev->dev = get_device(dev);
-       return device_add(&cdev->cdev);
+       return enclosure_add_links(cdev);
 }
 EXPORT_SYMBOL_GPL(enclosure_add_device);
 
@@ -270,19 +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 enclosure_component *cdev;
+       int i;
 
-       if (!edev || component >= edev->components)
+       if (!edev || !dev)
                return -EINVAL;
 
-       cdev = &edev->component[component];
-
-       device_del(&cdev->cdev);
-       put_device(cdev->dev);
-       cdev->dev = NULL;
-       return device_add(&cdev->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);
 
@@ -442,24 +513,32 @@ static ssize_t get_component_type(struct device *cdev,
 }
 
 
-static struct 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,
-       .dev_attrs      = enclosure_component_attrs,
-       .dev_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)
@@ -469,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);
 }