Merge commit '75d04a3773ecee617847de963ae4195d6aa74c28' into drm-intel-next-queued
[firefly-linux-kernel-4.4.55.git] / drivers / misc / enclosure.c
index 958ee988a1e15e31e3fe434f80c354c3372c5535..38552a31304aff8cf2747d0f6278897bd6b87d72 100644 (file)
@@ -145,8 +145,11 @@ enclosure_register(struct device *dev, const char *name, int components,
        if (err)
                goto err;
 
-       for (i = 0; i < components; i++)
+       for (i = 0; i < components; i++) {
                edev->component[i].number = -1;
+               edev->component[i].slot = -1;
+               edev->component[i].power_status = 1;
+       }
 
        mutex_lock(&container_list_lock);
        list_add_tail(&edev->node, &container_list);
@@ -581,6 +584,40 @@ static ssize_t set_component_locate(struct device *cdev,
        return count;
 }
 
+static ssize_t get_component_power_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);
+
+       if (edev->cb->get_power_status)
+               edev->cb->get_power_status(edev, ecomp);
+       return snprintf(buf, 40, "%s\n", ecomp->power_status ? "on" : "off");
+}
+
+static ssize_t set_component_power_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);
+       int val;
+
+       if (strncmp(buf, "on", 2) == 0 &&
+           (buf[2] == '\n' || buf[2] == '\0'))
+               val = 1;
+       else if (strncmp(buf, "off", 3) == 0 &&
+           (buf[3] == '\n' || buf[3] == '\0'))
+               val = 0;
+       else
+               return -EINVAL;
+
+       if (edev->cb->set_power_status)
+               edev->cb->set_power_status(edev, ecomp, val);
+       return count;
+}
+
 static ssize_t get_component_type(struct device *cdev,
                                  struct device_attribute *attr, char *buf)
 {
@@ -589,6 +626,20 @@ static ssize_t get_component_type(struct device *cdev,
        return snprintf(buf, 40, "%s\n", enclosure_type[ecomp->type]);
 }
 
+static ssize_t get_component_slot(struct device *cdev,
+                                 struct device_attribute *attr, char *buf)
+{
+       struct enclosure_component *ecomp = to_enclosure_component(cdev);
+       int slot;
+
+       /* if the enclosure does not override then use 'number' as a stand-in */
+       if (ecomp->slot >= 0)
+               slot = ecomp->slot;
+       else
+               slot = ecomp->number;
+
+       return snprintf(buf, 40, "%d\n", slot);
+}
 
 static DEVICE_ATTR(fault, S_IRUGO | S_IWUSR, get_component_fault,
                    set_component_fault);
@@ -598,14 +649,19 @@ 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(power_status, S_IRUGO | S_IWUSR, get_component_power_status,
+                  set_component_power_status);
 static DEVICE_ATTR(type, S_IRUGO, get_component_type, NULL);
+static DEVICE_ATTR(slot, S_IRUGO, get_component_slot, 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_power_status.attr,
        &dev_attr_type.attr,
+       &dev_attr_slot.attr,
        NULL
 };
 ATTRIBUTE_GROUPS(enclosure_component);