mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Mariusz Gorski <marius.gorski@gmail.com>
To: Evgeniy Polyakov <zbr@ioremap.net>, David Fries <David@Fries.net>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: linux-kernel@vger.kernel.org
Subject: [PATCH 1/2] w1: slaves: w1_therm: Extract read_rom function
Date: Tue,  6 Jan 2015 15:29:55 +0100	[thread overview]
Message-ID: <1420554596-10250-2-git-send-email-marius.gorski@gmail.com> (raw)
In-Reply-To: <1420554596-10250-1-git-send-email-marius.gorski@gmail.com>

Extract read_rom function to make it reusable by other attributes.

Signed-off-by: Mariusz Gorski <marius.gorski@gmail.com>
---
 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


  reply	other threads:[~2015-01-06 14:33 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-06 14:29 [PATCH 0/2] w1: slaves: w1_therm: Add sysfs entry for current temperature Mariusz Gorski
2015-01-06 14:29 ` Mariusz Gorski [this message]
2015-01-06 14:29 ` [PATCH 2/2] w1: slaves: w1_therm: Add temp attribute Mariusz Gorski
2015-01-06 17:42   ` Greg Kroah-Hartman
2015-01-06 20:19     ` Mariusz Gorski
2015-01-06 20:26       ` Greg Kroah-Hartman
2015-01-07  2:00   ` David Fries
2015-01-06 15:01 ` [PATCH 0/2] w1: slaves: w1_therm: Add sysfs entry for current temperature Richard Weinberger
2015-01-06 16:12   ` Mariusz Gorski
2015-01-06 16:50     ` Richard Weinberger
2015-01-07  2:27       ` David Fries
2015-01-07  1:59 ` David Fries

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1420554596-10250-2-git-send-email-marius.gorski@gmail.com \
    --to=marius.gorski@gmail.com \
    --cc=David@Fries.net \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=zbr@ioremap.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®