hp_accel: fix race in device removal
[safe/jmp/linux-2.6] / drivers / hwmon / it87.c
index 96306a6..5be09c0 100644 (file)
@@ -192,6 +192,9 @@ static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 };
 
 #define IT87_REG_CHIPID        0x58
 
+#define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
+#define IT87_REG_AUTO_PWM(nr, i)  (0x65 + (nr) * 8 + (i))
+
 #define IN_TO_REG(val)  (SENSORS_LIMIT((((val) + 8)/16),0,255))
 #define IN_FROM_REG(val) ((val) * 16)
 
@@ -293,6 +296,10 @@ struct it87_data {
        u8 pwm_ctrl[3];         /* Register value */
        u8 pwm_duty[3];         /* Manual PWM value set by user (bit 6-0) */
        u8 pwm_temp_map[3];     /* PWM to temp. chan. mapping (bits 1-0) */
+
+       /* Automatic fan speed control registers */
+       u8 auto_pwm[3][4];      /* [nr][3] is hard-coded */
+       s8 auto_temp[3][5];     /* [nr][0] is point1_temp_hyst */
 };
 
 static inline int has_16bit_fans(const struct it87_data *data)
@@ -307,6 +314,15 @@ static inline int has_16bit_fans(const struct it87_data *data)
            || data->type == it8720;
 }
 
+static inline int has_old_autopwm(const struct it87_data *data)
+{
+       /* The old automatic fan speed control interface is implemented
+          by IT8705F chips up to revision F and IT8712F chips up to
+          revision G. */
+       return (data->type == it87 && data->revision < 0x03)
+           || (data->type == it8712 && data->revision < 0x08);
+}
+
 static int it87_probe(struct platform_device *pdev);
 static int __devexit it87_remove(struct platform_device *pdev);
 
@@ -363,7 +379,10 @@ static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
        int nr = sensor_attr->index;
 
        struct it87_data *data = dev_get_drvdata(dev);
-       unsigned long val = simple_strtoul(buf, NULL, 10);
+       unsigned long val;
+
+       if (strict_strtoul(buf, 10, &val) < 0)
+               return -EINVAL;
 
        mutex_lock(&data->update_lock);
        data->in_min[nr] = IN_TO_REG(val);
@@ -379,7 +398,10 @@ static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
        int nr = sensor_attr->index;
 
        struct it87_data *data = dev_get_drvdata(dev);
-       unsigned long val = simple_strtoul(buf, NULL, 10);
+       unsigned long val;
+
+       if (strict_strtoul(buf, 10, &val) < 0)
+               return -EINVAL;
 
        mutex_lock(&data->update_lock);
        data->in_max[nr] = IN_TO_REG(val);
@@ -452,7 +474,10 @@ static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
        int nr = sensor_attr->index;
 
        struct it87_data *data = dev_get_drvdata(dev);
-       int val = simple_strtol(buf, NULL, 10);
+       long val;
+
+       if (strict_strtol(buf, 10, &val) < 0)
+               return -EINVAL;
 
        mutex_lock(&data->update_lock);
        data->temp_high[nr] = TEMP_TO_REG(val);
@@ -467,7 +492,10 @@ static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
        int nr = sensor_attr->index;
 
        struct it87_data *data = dev_get_drvdata(dev);
-       int val = simple_strtol(buf, NULL, 10);
+       long val;
+
+       if (strict_strtol(buf, 10, &val) < 0)
+               return -EINVAL;
 
        mutex_lock(&data->update_lock);
        data->temp_low[nr] = TEMP_TO_REG(val);
@@ -510,12 +538,15 @@ static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
        int nr = sensor_attr->index;
 
        struct it87_data *data = dev_get_drvdata(dev);
-       int val = simple_strtol(buf, NULL, 10);
+       long val;
+       u8 reg;
 
-       mutex_lock(&data->update_lock);
+       if (strict_strtol(buf, 10, &val) < 0)
+               return -EINVAL;
 
-       data->sensor &= ~(1 << nr);
-       data->sensor &= ~(8 << nr);
+       reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
+       reg &= ~(1 << nr);
+       reg &= ~(8 << nr);
        if (val == 2) { /* backwards compatibility */
                dev_warn(dev, "Sensor type 2 is deprecated, please use 4 "
                         "instead\n");
@@ -523,14 +554,16 @@ static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
        }
        /* 3 = thermal diode; 4 = thermistor; 0 = disabled */
        if (val == 3)
-               data->sensor |= 1 << nr;
+               reg |= 1 << nr;
        else if (val == 4)
-               data->sensor |= 8 << nr;
-       else if (val != 0) {
-               mutex_unlock(&data->update_lock);
+               reg |= 8 << nr;
+       else if (val != 0)
                return -EINVAL;
-       }
+
+       mutex_lock(&data->update_lock);
+       data->sensor = reg;
        it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
+       data->valid = 0;        /* Force cache refresh */
        mutex_unlock(&data->update_lock);
        return count;
 }
@@ -618,9 +651,12 @@ static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
        int nr = sensor_attr->index;
 
        struct it87_data *data = dev_get_drvdata(dev);
-       int val = simple_strtol(buf, NULL, 10);
+       long val;
        u8 reg;
 
+       if (strict_strtol(buf, 10, &val) < 0)
+               return -EINVAL;
+
        mutex_lock(&data->update_lock);
        reg = it87_read_value(data, IT87_REG_FAN_DIV);
        switch (nr) {
@@ -647,10 +683,13 @@ static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
        int nr = sensor_attr->index;
 
        struct it87_data *data = dev_get_drvdata(dev);
-       unsigned long val = simple_strtoul(buf, NULL, 10);
+       unsigned long val;
        int min;
        u8 old;
 
+       if (strict_strtoul(buf, 10, &val) < 0)
+               return -EINVAL;
+
        mutex_lock(&data->update_lock);
        old = it87_read_value(data, IT87_REG_FAN_DIV);
 
@@ -682,6 +721,32 @@ static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
        mutex_unlock(&data->update_lock);
        return count;
 }
+
+/* Returns 0 if OK, -EINVAL otherwise */
+static int check_trip_points(struct device *dev, int nr)
+{
+       const struct it87_data *data = dev_get_drvdata(dev);
+       int i, err = 0;
+
+       if (has_old_autopwm(data)) {
+               for (i = 0; i < 3; i++) {
+                       if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
+                               err = -EINVAL;
+               }
+               for (i = 0; i < 2; i++) {
+                       if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
+                               err = -EINVAL;
+               }
+       }
+
+       if (err) {
+               dev_err(dev, "Inconsistent trip points, not switching to "
+                       "automatic mode\n");
+               dev_err(dev, "Adjust the trip points and try again\n");
+       }
+       return err;
+}
+
 static ssize_t set_pwm_enable(struct device *dev,
                struct device_attribute *attr, const char *buf, size_t count)
 {
@@ -689,11 +754,17 @@ static ssize_t set_pwm_enable(struct device *dev,
        int nr = sensor_attr->index;
 
        struct it87_data *data = dev_get_drvdata(dev);
-       int val = simple_strtol(buf, NULL, 10);
+       long val;
 
-       if (val < 0 || val > 2)
+       if (strict_strtol(buf, 10, &val) < 0 || val < 0 || val > 2)
                return -EINVAL;
 
+       /* Check trip points before switching to automatic mode */
+       if (val == 2) {
+               if (check_trip_points(dev, nr) < 0)
+                       return -EINVAL;
+       }
+
        mutex_lock(&data->update_lock);
 
        if (val == 0) {
@@ -727,9 +798,9 @@ static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
        int nr = sensor_attr->index;
 
        struct it87_data *data = dev_get_drvdata(dev);
-       int val = simple_strtol(buf, NULL, 10);
+       long val;
 
-       if (val < 0 || val > 255)
+       if (strict_strtol(buf, 10, &val) < 0 || val < 0 || val > 255)
                return -EINVAL;
 
        mutex_lock(&data->update_lock);
@@ -747,9 +818,12 @@ static ssize_t set_pwm_freq(struct device *dev,
                struct device_attribute *attr, const char *buf, size_t count)
 {
        struct it87_data *data = dev_get_drvdata(dev);
-       unsigned long val = simple_strtoul(buf, NULL, 10);
+       unsigned long val;
        int i;
 
+       if (strict_strtoul(buf, 10, &val) < 0)
+               return -EINVAL;
+
        /* Search for the nearest available frequency */
        for (i = 0; i < 7; i++) {
                if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
@@ -789,6 +863,13 @@ static ssize_t set_pwm_temp_map(struct device *dev,
        long val;
        u8 reg;
 
+       /* This check can go away if we ever support automatic fan speed
+          control on newer chips. */
+       if (!has_old_autopwm(data)) {
+               dev_notice(dev, "Mapping change disabled for safety reasons\n");
+               return -EINVAL;
+       }
+
        if (strict_strtol(buf, 10, &val) < 0)
                return -EINVAL;
 
@@ -818,6 +899,72 @@ static ssize_t set_pwm_temp_map(struct device *dev,
        return count;
 }
 
+static ssize_t show_auto_pwm(struct device *dev,
+               struct device_attribute *attr, char *buf)
+{
+       struct it87_data *data = it87_update_device(dev);
+       struct sensor_device_attribute_2 *sensor_attr =
+                       to_sensor_dev_attr_2(attr);
+       int nr = sensor_attr->nr;
+       int point = sensor_attr->index;
+
+       return sprintf(buf, "%d\n", PWM_FROM_REG(data->auto_pwm[nr][point]));
+}
+
+static ssize_t set_auto_pwm(struct device *dev,
+               struct device_attribute *attr, const char *buf, size_t count)
+{
+       struct it87_data *data = dev_get_drvdata(dev);
+       struct sensor_device_attribute_2 *sensor_attr =
+                       to_sensor_dev_attr_2(attr);
+       int nr = sensor_attr->nr;
+       int point = sensor_attr->index;
+       long val;
+
+       if (strict_strtol(buf, 10, &val) < 0 || val < 0 || val > 255)
+               return -EINVAL;
+
+       mutex_lock(&data->update_lock);
+       data->auto_pwm[nr][point] = PWM_TO_REG(val);
+       it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
+                        data->auto_pwm[nr][point]);
+       mutex_unlock(&data->update_lock);
+       return count;
+}
+
+static ssize_t show_auto_temp(struct device *dev,
+               struct device_attribute *attr, char *buf)
+{
+       struct it87_data *data = it87_update_device(dev);
+       struct sensor_device_attribute_2 *sensor_attr =
+                       to_sensor_dev_attr_2(attr);
+       int nr = sensor_attr->nr;
+       int point = sensor_attr->index;
+
+       return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
+}
+
+static ssize_t set_auto_temp(struct device *dev,
+               struct device_attribute *attr, const char *buf, size_t count)
+{
+       struct it87_data *data = dev_get_drvdata(dev);
+       struct sensor_device_attribute_2 *sensor_attr =
+                       to_sensor_dev_attr_2(attr);
+       int nr = sensor_attr->nr;
+       int point = sensor_attr->index;
+       long val;
+
+       if (strict_strtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
+               return -EINVAL;
+
+       mutex_lock(&data->update_lock);
+       data->auto_temp[nr][point] = TEMP_TO_REG(val);
+       it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
+                        data->auto_temp[nr][point]);
+       mutex_unlock(&data->update_lock);
+       return count;
+}
+
 #define show_fan_offset(offset)                                        \
 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO,                \
                show_fan, NULL, offset - 1);                    \
@@ -839,8 +986,34 @@ static DEVICE_ATTR(pwm##offset##_freq,                                     \
                (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO),            \
                show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL));    \
 static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels_temp,            \
-               S_IRUGO, show_pwm_temp_map, set_pwm_temp_map,           \
-               offset - 1);
+               S_IRUGO | S_IWUSR, show_pwm_temp_map, set_pwm_temp_map, \
+               offset - 1);                                            \
+static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_pwm,             \
+               S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm,         \
+               offset - 1, 0);                                         \
+static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_pwm,             \
+               S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm,         \
+               offset - 1, 1);                                         \
+static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_pwm,             \
+               S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm,         \
+               offset - 1, 2);                                         \
+static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_pwm,             \
+               S_IRUGO, show_auto_pwm, NULL, offset - 1, 3);           \
+static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp,            \
+               S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp,       \
+               offset - 1, 1);                                         \
+static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp_hyst,       \
+               S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp,       \
+               offset - 1, 0);                                         \
+static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_temp,            \
+               S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp,       \
+               offset - 1, 2);                                         \
+static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_temp,            \
+               S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp,       \
+               offset - 1, 3);                                         \
+static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_temp,            \
+               S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp,       \
+               offset - 1, 4);
 
 show_pwm_offset(1);
 show_pwm_offset(2);
@@ -871,7 +1044,10 @@ static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
        struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
        int nr = sensor_attr->index;
        struct it87_data *data = dev_get_drvdata(dev);
-       int val = simple_strtol(buf, NULL, 10);
+       long val;
+
+       if (strict_strtol(buf, 10, &val) < 0)
+               return -EINVAL;
 
        mutex_lock(&data->update_lock);
        data->fan_min[nr] = FAN16_TO_REG(val);
@@ -992,9 +1168,11 @@ static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
                const char *buf, size_t count)
 {
        struct it87_data *data = dev_get_drvdata(dev);
-       u32 val;
+       unsigned long val;
+
+       if (strict_strtoul(buf, 10, &val) < 0)
+               return -EINVAL;
 
-       val = simple_strtoul(buf, NULL, 10);
        data->vrm = val;
 
        return count;
@@ -1190,6 +1368,47 @@ static const struct attribute_group it87_group_pwm[3] = {
        { .attrs = it87_attributes_pwm[2] },
 };
 
+static struct attribute *it87_attributes_autopwm[3][9+1] = { {
+       &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
+       &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
+       &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
+       &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
+       &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
+       &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
+       &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
+       &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
+       &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
+       NULL
+}, {
+       &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
+       &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
+       &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
+       &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
+       &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
+       &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
+       &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
+       &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
+       &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
+       NULL
+}, {
+       &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
+       &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
+       &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
+       &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
+       &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
+       &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
+       &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
+       &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
+       &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
+       NULL
+} };
+
+static const struct attribute_group it87_group_autopwm[3] = {
+       { .attrs = it87_attributes_autopwm[0] },
+       { .attrs = it87_attributes_autopwm[1] },
+       { .attrs = it87_attributes_autopwm[2] },
+};
+
 static struct attribute *it87_attributes_fan_beep[] = {
        &sensor_dev_attr_fan1_beep.dev_attr.attr,
        &sensor_dev_attr_fan2_beep.dev_attr.attr,
@@ -1353,6 +1572,9 @@ static void it87_remove_files(struct device *dev)
                if (sio_data->skip_pwm & (1 << 0))
                        continue;
                sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
+               if (has_old_autopwm(data))
+                       sysfs_remove_group(&dev->kobj,
+                                          &it87_group_autopwm[i]);
        }
        if (!sio_data->skip_vid)
                sysfs_remove_group(&dev->kobj, &it87_group_vid);
@@ -1462,6 +1684,13 @@ static int __devinit it87_probe(struct platform_device *pdev)
                                                 &it87_group_pwm[i]);
                        if (err)
                                goto ERROR4;
+
+                       if (!has_old_autopwm(data))
+                               continue;
+                       err = sysfs_create_group(&dev->kobj,
+                                                &it87_group_autopwm[i]);
+                       if (err)
+                               goto ERROR4;
                }
        }
 
@@ -1595,6 +1824,7 @@ static void __devinit it87_init_device(struct platform_device *pdev)
        for (i = 0; i < 3; i++) {
                data->pwm_temp_map[i] = i;
                data->pwm_duty[i] = 0x7f;       /* Full speed */
+               data->auto_pwm[i][3] = 0x7f;    /* Full speed, hard-coded */
        }
 
        /* Some chips seem to have default value 0xff for all limit
@@ -1613,14 +1843,10 @@ static void __devinit it87_init_device(struct platform_device *pdev)
                        it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
        }
 
-       /* Check if temperature channels are reset manually or by some reason */
-       tmp = it87_read_value(data, IT87_REG_TEMP_ENABLE);
-       if ((tmp & 0x3f) == 0) {
-               /* Temp1,Temp3=thermistor; Temp2=thermal diode */
-               tmp = (tmp & 0xc0) | 0x2a;
-               it87_write_value(data, IT87_REG_TEMP_ENABLE, tmp);
-       }
-       data->sensor = tmp;
+       /* Temperature channels are not forcibly enabled, as they can be
+        * set to two different sensor types and we can't guess which one
+        * is correct for a given system. These channels can be enabled at
+        * run-time through the temp{1-3}_type sysfs accessors if needed. */
 
        /* Check if voltage monitors are reset manually or by some reason */
        tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
@@ -1674,6 +1900,17 @@ static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
                data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
        else                            /* Manual mode */
                data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
+
+       if (has_old_autopwm(data)) {
+               int i;
+
+               for (i = 0; i < 5 ; i++)
+                       data->auto_temp[nr][i] = it87_read_value(data,
+                                               IT87_REG_AUTO_TEMP(nr, i));
+               for (i = 0; i < 3 ; i++)
+                       data->auto_pwm[nr][i] = it87_read_value(data,
+                                               IT87_REG_AUTO_PWM(nr, i));
+       }
 }
 
 static struct it87_data *it87_update_device(struct device *dev)