From patchwork Wed Aug 30 23:34:34 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jaghathiswari Rankappagounder Natarajan X-Patchwork-Id: 9930967 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id B083C60380 for ; Wed, 30 Aug 2017 23:35:00 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A2B5E287D7 for ; Wed, 30 Aug 2017 23:35:00 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 97341287E3; Wed, 30 Aug 2017 23:35:00 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 200E6287DC for ; Wed, 30 Aug 2017 23:35:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751405AbdH3Xer (ORCPT ); Wed, 30 Aug 2017 19:34:47 -0400 Received: from mail-pf0-f177.google.com ([209.85.192.177]:33601 "EHLO mail-pf0-f177.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751127AbdH3Xem (ORCPT ); Wed, 30 Aug 2017 19:34:42 -0400 Received: by mail-pf0-f177.google.com with SMTP id r62so23368847pfj.0 for ; Wed, 30 Aug 2017 16:34:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=QB+oMXhWRXd82EFif/Jx3yPC7C/A/IGyoOOLLfkjBu0=; b=jT8KTRQCDA/E5V/S90iKkZHjkYEUjn55RUas0zg7fvVqmEqw61WdGIgmM/OEdxDO2J ThZooGDk/bw4uiG1LlZ/kTUvpC/90QuhfgJdMbsC+59V4xBiXaGgdNskdFBGQpRWrx0o XpvRbxf38CZN69GfP8T7VKRlLvIklLLx1t5XBtWJpo1ksoIY591HAjXXnA9QhOOiQX1O X/jHnSvFon7KsNkR8StuqXmTcjD1nriGKlrx4LdybfadwR/aju+r9Aclt1v5WwKys6AO vut5IbIyu0aP6FZ+I1UU7G5hZCB05rZogog7rb09T8ZInRhdYSRVmFws+vnIv6QcqCLc Qdww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=QB+oMXhWRXd82EFif/Jx3yPC7C/A/IGyoOOLLfkjBu0=; b=ZTbNTPxfTdMKfvsx1pwI34ZrKxbRi9VmYGHvj7Gkis91lVoSEquu4EkcIUqUvWjOCZ lCtoHl5baAhomMwSHtfYPZRqnnOCwlzkAp3288Mfc3PZHuHTn+VmDYqx4vm/+2Y5zrlV 6vgHebvn3+E0zowwZ8WerhFwigHbGZdLo//DmSWeMe7Pw+pddgtDG79958IHI/ZzIOGD eE2aMQJ5Y6d79+tEpuGwU8jpQSydgZSJlStHiHJiTqAD9TWdSZ9mGK1hb/6r00pwwGPD jjVCgVGNdd2d76mqihkyKRrXewf04qUdOMjqH7FhvGNgbyriUf5+0cFJJgbyb8+fz+by Q71Q== X-Gm-Message-State: AHYfb5g8XzaBQQ5xR6LNlKGaHbEKzXR9qimL21fleklWX0PXzt+ez8SK xodRj/HoFmG0nNrS X-Google-Smtp-Source: ADKCNb5Agjv2oMukhiEgGI+zoj6ZFc1y4/DvPYFNSeUr4Q/Ic65hy2EM0JhJk05Acpbx3mz34XrLFA== X-Received: by 10.84.216.87 with SMTP id f23mr293114plj.283.1504136081710; Wed, 30 Aug 2017 16:34:41 -0700 (PDT) Received: from jaghu22.svl.corp.google.com ([100.123.242.38]) by smtp.gmail.com with ESMTPSA id n74sm10288826pfk.45.2017.08.30.16.34.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 30 Aug 2017 16:34:40 -0700 (PDT) From: Jaghathiswari Rankappagounder Natarajan To: zbr@ioremap.net, linux-kernel@vger.kernel.org, linux@roeck-us.net, jdelvare@suse.com, linux-hwmon@vger.kernel.org, gregkh@linuxfoundation.org Cc: Jaghathiswari Rankappagounder Natarajan Subject: [PATCH linux v6 2/3] drivers: w1: refactor w1_slave_show to make the temp reading functionality separate Date: Wed, 30 Aug 2017 16:34:34 -0700 Message-Id: <20170830233435.16500-3-jaghu@google.com> X-Mailer: git-send-email 2.14.1.581.gf28d330327-goog In-Reply-To: <20170830233435.16500-1-jaghu@google.com> References: <20170830233435.16500-1-jaghu@google.com> Sender: linux-hwmon-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Inside the w1_slave_show function refactor the code to read the temp into a separate function. Signed-off-by: Jaghathiswari Rankappagounder Natarajan Acked-by: Guenter Roeck Acked-by: Evgeniy Polyakov --- v2 - made changes to support hwmon_device_register_with_info mentioned by Guenter. v3 - used IS_REACHABLE(CONFIG_HWMON). - removed hwmon device first before removing slave. v4 - moved && to be in the previous line. v5 - no change v6 - rebased on top of char-misc-next branch drivers/w1/slaves/w1_therm.c | 82 +++++++++++++++++++++++++++----------------- 1 file changed, 51 insertions(+), 31 deletions(-) -- 2.14.1.581.gf28d330327-goog -- To unsubscribe from this list: send the line "unsubscribe linux-hwmon" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/drivers/w1/slaves/w1_therm.c b/drivers/w1/slaves/w1_therm.c index cb3fc3c6b0d1..faddc2488c91 100644 --- a/drivers/w1/slaves/w1_therm.c +++ b/drivers/w1/slaves/w1_therm.c @@ -59,6 +59,12 @@ struct w1_therm_family_data { atomic_t refcnt; }; +struct therm_info { + u8 rom[9]; + u8 crc; + u8 verdict; +}; + /* return the address of the refcnt in the family data */ #define THERM_REFCNT(family_data) \ (&((struct w1_therm_family_data *)family_data)->refcnt) @@ -422,33 +428,31 @@ static ssize_t w1_slave_store(struct device *device, return ret ? : size; } -static ssize_t w1_slave_show(struct device *device, - struct device_attribute *attr, char *buf) +static ssize_t read_therm(struct device *device, + struct w1_slave *sl, struct therm_info *info) { - struct w1_slave *sl = dev_to_w1_slave(device); struct w1_master *dev = sl->master; - u8 rom[9], crc, verdict, external_power; - int i, ret, max_trying = 10; - ssize_t c = PAGE_SIZE; + u8 external_power; + int ret, max_trying = 10; u8 *family_data = sl->family_data; ret = mutex_lock_interruptible(&dev->bus_mutex); if (ret != 0) - goto post_unlock; + goto error; - if (!sl->family_data) { + if (!family_data) { ret = -ENODEV; - goto pre_unlock; + goto mt_unlock; } /* prevent the slave from going away in sleep */ atomic_inc(THERM_REFCNT(family_data)); - memset(rom, 0, sizeof(rom)); + memset(info->rom, 0, sizeof(info->rom)); while (max_trying--) { - verdict = 0; - crc = 0; + info->verdict = 0; + info->crc = 0; if (!w1_reset_select_slave(sl)) { int count = 0; @@ -474,47 +478,69 @@ static ssize_t w1_slave_show(struct device *device, sleep_rem = msleep_interruptible(tm); if (sleep_rem != 0) { ret = -EINTR; - goto post_unlock; + goto dec_refcnt; } ret = mutex_lock_interruptible(&dev->bus_mutex); if (ret != 0) - goto post_unlock; + goto dec_refcnt; } else if (!w1_strong_pullup) { sleep_rem = msleep_interruptible(tm); if (sleep_rem != 0) { ret = -EINTR; - goto pre_unlock; + goto dec_refcnt; } } if (!w1_reset_select_slave(sl)) { 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) { dev_warn(device, "w1_read_block() " "returned %u instead of 9.\n", count); } - crc = w1_calc_crc8(rom, 8); + info->crc = w1_calc_crc8(info->rom, 8); - if (rom[8] == crc) - verdict = 1; + if (info->rom[8] == info->crc) + info->verdict = 1; } } - if (verdict) + if (info->verdict) 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) - 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", - crc, (verdict) ? "YES" : "NO"); - if (verdict) - memcpy(family_data, rom, sizeof(rom)); + info.crc, (info.verdict) ? "YES" : "NO"); + if (info.verdict) + memcpy(family_data, info.rom, sizeof(info.rom)); else dev_warn(device, "Read failed CRC check\n"); @@ -523,14 +549,8 @@ static ssize_t w1_slave_show(struct device *device, ((u8 *)family_data)[i]); 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; - -pre_unlock: - mutex_unlock(&dev->bus_mutex); - -post_unlock: - atomic_dec(THERM_REFCNT(family_data)); return ret; }