]> Pileus Git - ~andy/linux/blobdiff - drivers/hwmon/acpi_power_meter.c
Merge commit 'v3.5-rc5' into next
[~andy/linux] / drivers / hwmon / acpi_power_meter.c
index 9140236a0182b4f73d80efbab7d6fdac11aca539..34ad5a27a7e9fcd68b7597f799a99387f421c835 100644 (file)
@@ -107,15 +107,7 @@ struct acpi_power_meter_resource {
        struct kobject          *holders_dir;
 };
 
-struct ro_sensor_template {
-       char *label;
-       ssize_t (*show)(struct device *dev,
-                       struct device_attribute *devattr,
-                       char *buf);
-       int index;
-};
-
-struct rw_sensor_template {
+struct sensor_template {
        char *label;
        ssize_t (*show)(struct device *dev,
                        struct device_attribute *devattr,
@@ -469,52 +461,67 @@ static ssize_t show_name(struct device *dev,
        return sprintf(buf, "%s\n", ACPI_POWER_METER_NAME);
 }
 
+#define RO_SENSOR_TEMPLATE(_label, _show, _index)      \
+       {                                               \
+               .label = _label,                        \
+               .show  = _show,                         \
+               .index = _index,                        \
+       }
+
+#define RW_SENSOR_TEMPLATE(_label, _show, _set, _index)        \
+       {                                               \
+               .label = _label,                        \
+               .show  = _show,                         \
+               .set   = _set,                          \
+               .index = _index,                        \
+       }
+
 /* Sensor descriptions.  If you add a sensor, update NUM_SENSORS above! */
-static struct ro_sensor_template meter_ro_attrs[] = {
-{POWER_AVERAGE_NAME, show_power, 0},
-{"power1_accuracy", show_accuracy, 0},
-{"power1_average_interval_min", show_val, 0},
-{"power1_average_interval_max", show_val, 1},
-{"power1_is_battery", show_val, 5},
-{NULL, NULL, 0},
+static struct sensor_template meter_attrs[] = {
+       RO_SENSOR_TEMPLATE(POWER_AVERAGE_NAME, show_power, 0),
+       RO_SENSOR_TEMPLATE("power1_accuracy", show_accuracy, 0),
+       RO_SENSOR_TEMPLATE("power1_average_interval_min", show_val, 0),
+       RO_SENSOR_TEMPLATE("power1_average_interval_max", show_val, 1),
+       RO_SENSOR_TEMPLATE("power1_is_battery", show_val, 5),
+       RW_SENSOR_TEMPLATE(POWER_AVG_INTERVAL_NAME, show_avg_interval,
+               set_avg_interval, 0),
+       {},
 };
 
-static struct rw_sensor_template meter_rw_attrs[] = {
-{POWER_AVG_INTERVAL_NAME, show_avg_interval, set_avg_interval, 0},
-{NULL, NULL, NULL, 0},
+static struct sensor_template misc_cap_attrs[] = {
+       RO_SENSOR_TEMPLATE("power1_cap_min", show_val, 2),
+       RO_SENSOR_TEMPLATE("power1_cap_max", show_val, 3),
+       RO_SENSOR_TEMPLATE("power1_cap_hyst", show_val, 4),
+       RO_SENSOR_TEMPLATE(POWER_ALARM_NAME, show_val, 6),
+       {},
 };
 
-static struct ro_sensor_template misc_cap_attrs[] = {
-{"power1_cap_min", show_val, 2},
-{"power1_cap_max", show_val, 3},
-{"power1_cap_hyst", show_val, 4},
-{POWER_ALARM_NAME, show_val, 6},
-{NULL, NULL, 0},
+static struct sensor_template ro_cap_attrs[] = {
+       RO_SENSOR_TEMPLATE(POWER_CAP_NAME, show_cap, 0),
+       {},
 };
 
-static struct ro_sensor_template ro_cap_attrs[] = {
-{POWER_CAP_NAME, show_cap, 0},
-{NULL, NULL, 0},
+static struct sensor_template rw_cap_attrs[] = {
+       RW_SENSOR_TEMPLATE(POWER_CAP_NAME, show_cap, set_cap, 0),
+       {},
 };
 
-static struct rw_sensor_template rw_cap_attrs[] = {
-{POWER_CAP_NAME, show_cap, set_cap, 0},
-{NULL, NULL, NULL, 0},
+static struct sensor_template trip_attrs[] = {
+       RW_SENSOR_TEMPLATE("power1_average_min", show_val, set_trip, 7),
+       RW_SENSOR_TEMPLATE("power1_average_max", show_val, set_trip, 8),
+       {},
 };
 
-static struct rw_sensor_template trip_attrs[] = {
-{"power1_average_min", show_val, set_trip, 7},
-{"power1_average_max", show_val, set_trip, 8},
-{NULL, NULL, NULL, 0},
+static struct sensor_template misc_attrs[] = {
+       RO_SENSOR_TEMPLATE("name", show_name, 0),
+       RO_SENSOR_TEMPLATE("power1_model_number", show_str, 0),
+       RO_SENSOR_TEMPLATE("power1_oem_info", show_str, 2),
+       RO_SENSOR_TEMPLATE("power1_serial_number", show_str, 1),
+       {},
 };
 
-static struct ro_sensor_template misc_attrs[] = {
-{"name", show_name, 0},
-{"power1_model_number", show_str, 0},
-{"power1_oem_info", show_str, 2},
-{"power1_serial_number", show_str, 1},
-{NULL, NULL, 0},
-};
+#undef RO_SENSOR_TEMPLATE
+#undef RW_SENSOR_TEMPLATE
 
 /* Read power domain data */
 static void remove_domain_devices(struct acpi_power_meter_resource *resource)
@@ -619,49 +626,24 @@ end:
 }
 
 /* Registration and deregistration */
-static int register_ro_attrs(struct acpi_power_meter_resource *resource,
-                            struct ro_sensor_template *ro)
+static int register_attrs(struct acpi_power_meter_resource *resource,
+                         struct sensor_template *attrs)
 {
        struct device *dev = &resource->acpi_dev->dev;
        struct sensor_device_attribute *sensors =
                &resource->sensors[resource->num_sensors];
        int res = 0;
 
-       while (ro->label) {
-               sensors->dev_attr.attr.name = ro->label;
+       while (attrs->label) {
+               sensors->dev_attr.attr.name = attrs->label;
                sensors->dev_attr.attr.mode = S_IRUGO;
-               sensors->dev_attr.show = ro->show;
-               sensors->index = ro->index;
+               sensors->dev_attr.show = attrs->show;
+               sensors->index = attrs->index;
 
-               sysfs_attr_init(&sensors->dev_attr.attr);
-               res = device_create_file(dev, &sensors->dev_attr);
-               if (res) {
-                       sensors->dev_attr.attr.name = NULL;
-                       goto error;
+               if (attrs->set) {
+                       sensors->dev_attr.attr.mode |= S_IWUSR;
+                       sensors->dev_attr.store = attrs->set;
                }
-               sensors++;
-               resource->num_sensors++;
-               ro++;
-       }
-
-error:
-       return res;
-}
-
-static int register_rw_attrs(struct acpi_power_meter_resource *resource,
-                            struct rw_sensor_template *rw)
-{
-       struct device *dev = &resource->acpi_dev->dev;
-       struct sensor_device_attribute *sensors =
-               &resource->sensors[resource->num_sensors];
-       int res = 0;
-
-       while (rw->label) {
-               sensors->dev_attr.attr.name = rw->label;
-               sensors->dev_attr.attr.mode = S_IRUGO | S_IWUSR;
-               sensors->dev_attr.show = rw->show;
-               sensors->dev_attr.store = rw->set;
-               sensors->index = rw->index;
 
                sysfs_attr_init(&sensors->dev_attr.attr);
                res = device_create_file(dev, &sensors->dev_attr);
@@ -671,7 +653,7 @@ static int register_rw_attrs(struct acpi_power_meter_resource *resource,
                }
                sensors++;
                resource->num_sensors++;
-               rw++;
+               attrs++;
        }
 
 error:
@@ -703,10 +685,7 @@ static int setup_attrs(struct acpi_power_meter_resource *resource)
                return res;
 
        if (resource->caps.flags & POWER_METER_CAN_MEASURE) {
-               res = register_ro_attrs(resource, meter_ro_attrs);
-               if (res)
-                       goto error;
-               res = register_rw_attrs(resource, meter_rw_attrs);
+               res = register_attrs(resource, meter_attrs);
                if (res)
                        goto error;
        }
@@ -718,28 +697,27 @@ static int setup_attrs(struct acpi_power_meter_resource *resource)
                        goto skip_unsafe_cap;
                }
 
-               if (resource->caps.configurable_cap) {
-                       res = register_rw_attrs(resource, rw_cap_attrs);
-                       if (res)
-                               goto error;
-               } else {
-                       res = register_ro_attrs(resource, ro_cap_attrs);
-                       if (res)
-                               goto error;
-               }
-               res = register_ro_attrs(resource, misc_cap_attrs);
+               if (resource->caps.configurable_cap)
+                       res = register_attrs(resource, rw_cap_attrs);
+               else
+                       res = register_attrs(resource, ro_cap_attrs);
+
+               if (res)
+                       goto error;
+
+               res = register_attrs(resource, misc_cap_attrs);
                if (res)
                        goto error;
        }
-skip_unsafe_cap:
 
+skip_unsafe_cap:
        if (resource->caps.flags & POWER_METER_CAN_TRIP) {
-               res = register_rw_attrs(resource, trip_attrs);
+               res = register_attrs(resource, trip_attrs);
                if (res)
                        goto error;
        }
 
-       res = register_ro_attrs(resource, misc_attrs);
+       res = register_attrs(resource, misc_attrs);
        if (res)
                goto error;