aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/hwmon
diff options
context:
space:
mode:
authorChris Verges <kg4ysn@gmail.com>2012-12-21 04:58:34 -0500
committerGuenter Roeck <linux@roeck-us.net>2012-12-21 20:59:30 -0500
commit0602934f302e016e2ea5dc6951681bfac77455ef (patch)
treeb1a1522519696e7924c925478787e58a51b9ce53 /drivers/hwmon
parenta49f0d1ea3ec94fc7cf33a7c36a16343b74bd565 (diff)
hwmon: (lm73} Detect and report i2c bus errors
If an LM73 device does not exist on an I2C bus, attempts to communicate with the device result in an error code returned from the i2c read/write functions. The current lm73 driver casts that return value from a s32 type to a s16 type, then converts it to a temperature in celsius. Because negative temperatures are valid, it is difficult to distinguish between an error code printed to the response buffer and a negative temperature recorded by the sensor. The solution is to evaluate the return value from the i2c functions before performing any temperature calculations. If the i2c function did not succeed, the error code should be passed back through the virtual file system layer instead of being printed into the response buffer. Before: $ cat /sys/class/hwmon/hwmon0/device/temp1_input -46 After: $ cat /sys/class/hwmon/hwmon0/device/temp1_input cat: read error: No such device or address Signed-off-by: Chris Verges <kg4ysn@gmail.com> Cc: stable@vger.kernel.org Signed-off-by: Guenter Roeck <linux@roeck-us.net>
Diffstat (limited to 'drivers/hwmon')
-rw-r--r--drivers/hwmon/lm73.c16
1 files changed, 11 insertions, 5 deletions
diff --git a/drivers/hwmon/lm73.c b/drivers/hwmon/lm73.c
index 8fa2632cbbaf..7272176a9ec7 100644
--- a/drivers/hwmon/lm73.c
+++ b/drivers/hwmon/lm73.c
@@ -49,6 +49,7 @@ static ssize_t set_temp(struct device *dev, struct device_attribute *da,
49 struct i2c_client *client = to_i2c_client(dev); 49 struct i2c_client *client = to_i2c_client(dev);
50 long temp; 50 long temp;
51 short value; 51 short value;
52 s32 err;
52 53
53 int status = kstrtol(buf, 10, &temp); 54 int status = kstrtol(buf, 10, &temp);
54 if (status < 0) 55 if (status < 0)
@@ -57,8 +58,8 @@ static ssize_t set_temp(struct device *dev, struct device_attribute *da,
57 /* Write value */ 58 /* Write value */
58 value = (short) SENSORS_LIMIT(temp/250, (LM73_TEMP_MIN*4), 59 value = (short) SENSORS_LIMIT(temp/250, (LM73_TEMP_MIN*4),
59 (LM73_TEMP_MAX*4)) << 5; 60 (LM73_TEMP_MAX*4)) << 5;
60 i2c_smbus_write_word_swapped(client, attr->index, value); 61 err = i2c_smbus_write_word_swapped(client, attr->index, value);
61 return count; 62 return (err < 0) ? err : count;
62} 63}
63 64
64static ssize_t show_temp(struct device *dev, struct device_attribute *da, 65static ssize_t show_temp(struct device *dev, struct device_attribute *da,
@@ -66,11 +67,16 @@ static ssize_t show_temp(struct device *dev, struct device_attribute *da,
66{ 67{
67 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 68 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
68 struct i2c_client *client = to_i2c_client(dev); 69 struct i2c_client *client = to_i2c_client(dev);
70 int temp;
71
72 s32 err = i2c_smbus_read_word_swapped(client, attr->index);
73 if (err < 0)
74 return err;
75
69 /* use integer division instead of equivalent right shift to 76 /* use integer division instead of equivalent right shift to
70 guarantee arithmetic shift and preserve the sign */ 77 guarantee arithmetic shift and preserve the sign */
71 int temp = ((s16) (i2c_smbus_read_word_swapped(client, 78 temp = (((s16) err) * 250) / 32;
72 attr->index))*250) / 32; 79 return scnprintf(buf, PAGE_SIZE, "%d\n", temp);
73 return sprintf(buf, "%d\n", temp);
74} 80}
75 81
76 82