[PATCH 1/2] w1: slaves: w1_therm: Extract read_rom function

From: Mariusz Gorski
Date: Tue Jan 06 2015 - 09:33:53 EST


Extract read_rom function to make it reusable by other attributes.

Signed-off-by: Mariusz Gorski <marius.gorski@xxxxxxxxx>
---
drivers/w1/slaves/w1_therm.c | 51 ++++++++++++++++++++++++++++----------------
1 file changed, 33 insertions(+), 18 deletions(-)

diff --git a/drivers/w1/slaves/w1_therm.c b/drivers/w1/slaves/w1_therm.c
index 1f11a20..3bad3d6 100644
--- a/drivers/w1/slaves/w1_therm.c
+++ b/drivers/w1/slaves/w1_therm.c
@@ -188,24 +188,20 @@ static inline int w1_convert_temp(u8 rom[9], u8 fid)
}


-static ssize_t w1_slave_show(struct device *device,
- struct device_attribute *attr, char *buf)
+/**
+ * read_rom() - Reads slave's 64-bit ROM + 8-bit checksum
+ * @device: slave device to read from
+ * @rom: buffer to write to
+ * Return: negative value on errors, 0=read or checksum failure, 1=success
+ */
+static int read_rom(struct device *device, u8 rom[9])
{
struct w1_slave *sl = dev_to_w1_slave(device);
struct w1_master *dev = sl->master;
- u8 rom[9], crc, verdict, external_power;
int i, max_trying = 10;
- ssize_t c = PAGE_SIZE;
-
- i = mutex_lock_interruptible(&dev->bus_mutex);
- if (i != 0)
- return i;
-
- memset(rom, 0, sizeof(rom));
+ u8 crc, external_power;

while (max_trying--) {
-
- verdict = 0;
crc = 0;

if (!w1_reset_select_slave(sl)) {
@@ -245,7 +241,6 @@ static ssize_t w1_slave_show(struct device *device,
}

if (!w1_reset_select_slave(sl)) {
-
w1_write_8(dev, W1_READ_SCRATCHPAD);
if ((count = w1_read_block(dev, rom, 9)) != 9) {
dev_warn(device, "w1_read_block() "
@@ -256,18 +251,38 @@ static ssize_t w1_slave_show(struct device *device,
crc = w1_calc_crc8(rom, 8);

if (rom[8] == crc)
- verdict = 1;
+ return 1;
}
}
-
- if (verdict)
- break;
}

+ return 0;
+}
+
+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 w1_master *dev = sl->master;
+ u8 rom[9], verdict;
+ int i;
+ ssize_t c = PAGE_SIZE;
+
+ i = mutex_lock_interruptible(&dev->bus_mutex);
+ if (i != 0)
+ return i;
+
+ memset(rom, 0, sizeof(rom));
+
+ verdict = read_rom(device, rom);
+ if (verdict < 0)
+ /* Propagate errors to upper layers */
+ return verdict;
+
for (i = 0; i < 9; ++i)
c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", rom[i]);
c -= snprintf(buf + PAGE_SIZE - c, c, ": crc=%02x %s\n",
- crc, (verdict) ? "YES" : "NO");
+ w1_calc_crc8(rom, 8), (verdict) ? "YES" : "NO");
if (verdict)
memcpy(sl->family_data, rom, sizeof(rom));
else
--
2.2.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/