Commit b78165f0 authored by Jaghathiswari Rankappagounder Natarajan's avatar Jaghathiswari Rankappagounder Natarajan Committed by Greg Kroah-Hartman

drivers: w1: refactor w1_slave_show to make the temp reading functionality separate

Inside the w1_slave_show function refactor the code to read the temp
into a separate function.
Signed-off-by: default avatarJaghathiswari Rankappagounder Natarajan <jaghu@google.com>
Acked-by: default avatarGuenter Roeck <linux@roeck-us.net>
Acked-by: default avatarEvgeniy Polyakov <zbr@ioremap.net>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 2eb79548
...@@ -59,6 +59,12 @@ struct w1_therm_family_data { ...@@ -59,6 +59,12 @@ struct w1_therm_family_data {
atomic_t refcnt; atomic_t refcnt;
}; };
struct therm_info {
u8 rom[9];
u8 crc;
u8 verdict;
};
/* return the address of the refcnt in the family data */ /* return the address of the refcnt in the family data */
#define THERM_REFCNT(family_data) \ #define THERM_REFCNT(family_data) \
(&((struct w1_therm_family_data *)family_data)->refcnt) (&((struct w1_therm_family_data *)family_data)->refcnt)
...@@ -422,33 +428,31 @@ static ssize_t w1_slave_store(struct device *device, ...@@ -422,33 +428,31 @@ static ssize_t w1_slave_store(struct device *device,
return ret ? : size; return ret ? : size;
} }
static ssize_t w1_slave_show(struct device *device, static ssize_t read_therm(struct device *device,
struct device_attribute *attr, char *buf) struct w1_slave *sl, struct therm_info *info)
{ {
struct w1_slave *sl = dev_to_w1_slave(device);
struct w1_master *dev = sl->master; struct w1_master *dev = sl->master;
u8 rom[9], crc, verdict, external_power; u8 external_power;
int i, ret, max_trying = 10; int ret, max_trying = 10;
ssize_t c = PAGE_SIZE;
u8 *family_data = sl->family_data; u8 *family_data = sl->family_data;
ret = mutex_lock_interruptible(&dev->bus_mutex); ret = mutex_lock_interruptible(&dev->bus_mutex);
if (ret != 0) if (ret != 0)
goto post_unlock; goto error;
if (!sl->family_data) { if (!family_data) {
ret = -ENODEV; ret = -ENODEV;
goto pre_unlock; goto mt_unlock;
} }
/* prevent the slave from going away in sleep */ /* prevent the slave from going away in sleep */
atomic_inc(THERM_REFCNT(family_data)); atomic_inc(THERM_REFCNT(family_data));
memset(rom, 0, sizeof(rom)); memset(info->rom, 0, sizeof(info->rom));
while (max_trying--) { while (max_trying--) {
verdict = 0; info->verdict = 0;
crc = 0; info->crc = 0;
if (!w1_reset_select_slave(sl)) { if (!w1_reset_select_slave(sl)) {
int count = 0; int count = 0;
...@@ -474,47 +478,69 @@ static ssize_t w1_slave_show(struct device *device, ...@@ -474,47 +478,69 @@ static ssize_t w1_slave_show(struct device *device,
sleep_rem = msleep_interruptible(tm); sleep_rem = msleep_interruptible(tm);
if (sleep_rem != 0) { if (sleep_rem != 0) {
ret = -EINTR; ret = -EINTR;
goto post_unlock; goto dec_refcnt;
} }
ret = mutex_lock_interruptible(&dev->bus_mutex); ret = mutex_lock_interruptible(&dev->bus_mutex);
if (ret != 0) if (ret != 0)
goto post_unlock; goto dec_refcnt;
} else if (!w1_strong_pullup) { } else if (!w1_strong_pullup) {
sleep_rem = msleep_interruptible(tm); sleep_rem = msleep_interruptible(tm);
if (sleep_rem != 0) { if (sleep_rem != 0) {
ret = -EINTR; ret = -EINTR;
goto pre_unlock; goto dec_refcnt;
} }
} }
if (!w1_reset_select_slave(sl)) { if (!w1_reset_select_slave(sl)) {
w1_write_8(dev, W1_READ_SCRATCHPAD); w1_write_8(dev, W1_READ_SCRATCHPAD);
count = w1_read_block(dev, rom, 9); count = w1_read_block(dev, info->rom, 9);
if (count != 9) { if (count != 9) {
dev_warn(device, "w1_read_block() " dev_warn(device, "w1_read_block() "
"returned %u instead of 9.\n", "returned %u instead of 9.\n",
count); count);
} }
crc = w1_calc_crc8(rom, 8); info->crc = w1_calc_crc8(info->rom, 8);
if (rom[8] == crc) if (info->rom[8] == info->crc)
verdict = 1; info->verdict = 1;
} }
} }
if (verdict) if (info->verdict)
break; break;
} }
dec_refcnt:
atomic_dec(THERM_REFCNT(family_data));
mt_unlock:
mutex_unlock(&dev->bus_mutex);
error:
return ret;
}
static ssize_t w1_slave_show(struct device *device,
struct device_attribute *attr, char *buf)
{
struct w1_slave *sl = dev_to_w1_slave(device);
struct therm_info info;
u8 *family_data = sl->family_data;
int ret, i;
ssize_t c = PAGE_SIZE;
u8 fid = sl->family->fid;
ret = read_therm(device, sl, &info);
if (ret)
return ret;
for (i = 0; i < 9; ++i) for (i = 0; i < 9; ++i)
c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", rom[i]); c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", info.rom[i]);
c -= snprintf(buf + PAGE_SIZE - c, c, ": crc=%02x %s\n", c -= snprintf(buf + PAGE_SIZE - c, c, ": crc=%02x %s\n",
crc, (verdict) ? "YES" : "NO"); info.crc, (info.verdict) ? "YES" : "NO");
if (verdict) if (info.verdict)
memcpy(family_data, rom, sizeof(rom)); memcpy(family_data, info.rom, sizeof(info.rom));
else else
dev_warn(device, "Read failed CRC check\n"); dev_warn(device, "Read failed CRC check\n");
...@@ -523,14 +549,8 @@ static ssize_t w1_slave_show(struct device *device, ...@@ -523,14 +549,8 @@ static ssize_t w1_slave_show(struct device *device,
((u8 *)family_data)[i]); ((u8 *)family_data)[i]);
c -= snprintf(buf + PAGE_SIZE - c, c, "t=%d\n", c -= snprintf(buf + PAGE_SIZE - c, c, "t=%d\n",
w1_convert_temp(rom, sl->family->fid)); w1_convert_temp(info.rom, fid));
ret = PAGE_SIZE - c; ret = PAGE_SIZE - c;
pre_unlock:
mutex_unlock(&dev->bus_mutex);
post_unlock:
atomic_dec(THERM_REFCNT(family_data));
return ret; return ret;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment