hwmon: (smsc47m192) Fix temperature limit and vrm write operations
authorGuenter Roeck <linux@roeck-us.net>
Fri, 18 Jul 2014 14:31:18 +0000 (07:31 -0700)
committerBen Hutchings <ben@decadent.org.uk>
Sat, 13 Sep 2014 22:41:39 +0000 (23:41 +0100)
commit 043572d5444116b9d9ad8ae763cf069e7accbc30 upstream.

Temperature limit clamps are applied after converting the temperature
from milli-degrees C to degrees C, so either the clamp limit needs
to be specified in degrees C, not milli-degrees C, or clamping must
happen before converting to degrees C. Use the latter method to avoid
overflows.

vrm is an u8, so the written value needs to be limited to [0, 255].

Cc: Axel Lin <axel.lin@ingics.com>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
Reviewed-by: Jean Delvare <jdelvare@suse.de>
[bwh: Backported to 3.2:
 - Driver is not using clamp_val(); keep using SENSORS_LIMIT() for consistency
 - Driver is not using kstrtoul(); make the minimum change to set_vrm() so
   we can validate the value before assigning]
Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
drivers/hwmon/smsc47m192.c

index 40b2667..7d1373d 100644 (file)
@@ -84,7 +84,7 @@ static inline u8 IN_TO_REG(unsigned long val, int n)
    REG: 1C/bit, two's complement */
 static inline s8 TEMP_TO_REG(int val)
 {
-       return SENSORS_LIMIT(SCALE(val, 1, 1000), -128000, 127000);
+       return SCALE(SENSORS_LIMIT(val, -128000, 127000), 1, 1000);
 }
 
 static inline int TEMP_FROM_REG(s8 val)
@@ -349,7 +349,13 @@ static ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
                const char *buf, size_t count)
 {
        struct smsc47m192_data *data = dev_get_drvdata(dev);
-       data->vrm = simple_strtoul(buf, NULL, 10);
+       unsigned long val;
+
+       val = simple_strtoul(buf, NULL, 10);
+       if (val > 255)
+               return -EINVAL;
+       data->vrm = val;
        return count;
 }
 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);